cgroup.c 47.3 KB
Newer Older
1 2 3
/*
 * cgroup.c: Tools for managing cgroups
 *
4
 * Copyright (C) 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15
 * Copyright IBM Corp. 2008
 *
 * See COPYING.LIB for the License of this software
 *
 * Authors:
 *  Dan Smith <danms@us.ibm.com>
 */
#include <config.h>

#include <stdio.h>
#include <stdint.h>
16
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
17
# include <mntent.h>
D
Daniel P. Berrange 已提交
18
#endif
19 20 21 22 23 24
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
25
#include <signal.h>
26
#include <libgen.h>
27
#include <dirent.h>
28 29 30 31 32

#include "internal.h"
#include "util.h"
#include "memory.h"
#include "cgroup.h"
33
#include "logging.h"
E
Eric Blake 已提交
34
#include "virfile.h"
35
#include "virhash.h"
36 37 38

#define CGROUP_MAX_VAL 512

39
VIR_ENUM_IMPL(virCgroupController, VIR_CGROUP_CONTROLLER_LAST,
R
Ryota Ozaki 已提交
40
              "cpu", "cpuacct", "cpuset", "memory", "devices",
41
              "freezer", "blkio");
42 43 44 45 46 47

struct virCgroupController {
    int type;
    char *mountPoint;
    char *placement;
};
48 49 50

struct virCgroup {
    char *path;
51 52

    struct virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST];
53 54
};

55 56 57 58 59 60 61 62 63 64
typedef enum {
    VIR_CGROUP_NONE = 0, /* create subdir under each cgroup if possible. */
    VIR_CGROUP_MEM_HIERACHY = 1 << 0, /* call virCgroupSetMemoryUseHierarchy
                                       * before creating subcgroups and
                                       * attaching tasks
                                       */
    VIR_CGROUP_VCPU = 1 << 1, /* create subdir only under the cgroup cpu,
                               * cpuacct and cpuset if possible. */
} virCgroupFlags;

65 66 67 68 69 70 71
/**
 * virCgroupFree:
 *
 * @group: The group structure to free
 */
void virCgroupFree(virCgroupPtr *group)
{
72 73 74 75 76 77 78 79
    int i;

    if (*group == NULL)
        return;

    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
        VIR_FREE((*group)->controllers[i].mountPoint);
        VIR_FREE((*group)->controllers[i].placement);
80
    }
81 82 83

    VIR_FREE((*group)->path);
    VIR_FREE(*group);
84 85
}

L
Lai Jiangshan 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
/**
 * virCgroupMounted: query whether a cgroup subsystem is mounted or not
 *
 * @cgroup: The group structure to be queried
 * @controller: cgroup subsystem id
 *
 * Returns true if a cgroup is subsystem is mounted.
 */
bool virCgroupMounted(virCgroupPtr cgroup, int controller)
{
    return cgroup->controllers[controller].mountPoint != NULL;
}

99
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
100 101 102 103 104
/*
 * Process /proc/mounts figuring out what controllers are
 * mounted and where
 */
static int virCgroupDetectMounts(virCgroupPtr group)
105
{
106
    int i;
107
    FILE *mounts = NULL;
108 109 110 111 112
    struct mntent entry;
    char buf[CGROUP_MAX_VAL];

    mounts = fopen("/proc/mounts", "r");
    if (mounts == NULL) {
113
        VIR_ERROR(_("Unable to open /proc/mounts"));
114
        return -ENOENT;
115 116 117
    }

    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
118 119
        if (STRNEQ(entry.mnt_type, "cgroup"))
            continue;
120

121 122 123 124 125 126 127 128 129 130 131 132 133
        for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
            const char *typestr = virCgroupControllerTypeToString(i);
            int typelen = strlen(typestr);
            char *tmp = entry.mnt_opts;
            while (tmp) {
                char *next = strchr(tmp, ',');
                int len;
                if (next) {
                    len = next-tmp;
                    next++;
                } else {
                    len = strlen(tmp);
                }
134 135 136 137
                /* NB, the same controller can appear >1 time in mount list
                 * due to bind mounts from one location to another. Pick the
                 * first entry only
                 */
138
                if (typelen == len && STREQLEN(typestr, tmp, len) &&
139
                    !group->controllers[i].mountPoint &&
140 141 142 143 144
                    !(group->controllers[i].mountPoint = strdup(entry.mnt_dir)))
                    goto no_memory;
                tmp = next;
            }
        }
145 146
    }

147
    VIR_FORCE_FCLOSE(mounts);
148

149
    return 0;
150

151
no_memory:
152
    VIR_FORCE_FCLOSE(mounts);
153
    return -ENOMEM;
154 155
}

156 157 158 159 160

/*
 * Process /proc/self/cgroup figuring out what cgroup
 * sub-path the current process is assigned to. ie not
 * neccessarily in the root
161
 */
162
static int virCgroupDetectPlacement(virCgroupPtr group)
163 164
{
    int i;
165 166
    FILE *mapping  = NULL;
    char line[1024];
167

168 169
    mapping = fopen("/proc/self/cgroup", "r");
    if (mapping == NULL) {
170
        VIR_ERROR(_("Unable to open /proc/self/cgroup"));
171
        return -ENOENT;
172 173
    }

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    while (fgets(line, sizeof(line), mapping) != NULL) {
        char *controllers = strchr(line, ':');
        char *path = controllers ? strchr(controllers+1, ':') : NULL;
        char *nl = path ? strchr(path, '\n') : NULL;

        if (!controllers || !path)
            continue;

        if (nl)
            *nl = '\0';

        *path = '\0';
        controllers++;
        path++;

        for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
            const char *typestr = virCgroupControllerTypeToString(i);
            int typelen = strlen(typestr);
            char *tmp = controllers;
            while (tmp) {
                char *next = strchr(tmp, ',');
                int len;
                if (next) {
                    len = next-tmp;
                    next++;
                } else {
                    len = strlen(tmp);
                }
                if (typelen == len && STREQLEN(typestr, tmp, len) &&
                    !(group->controllers[i].placement = strdup(STREQ(path, "/") ? "" : path)))
                    goto no_memory;

                tmp = next;
            }
        }
    }

211
    VIR_FORCE_FCLOSE(mapping);
212

213
    return 0;
214 215

no_memory:
216
    VIR_FORCE_FCLOSE(mapping);
217 218
    return -ENOMEM;

219 220
}

221
static int virCgroupDetect(virCgroupPtr group)
222
{
223 224 225
    int any = 0;
    int rc;
    int i;
226

227 228
    rc = virCgroupDetectMounts(group);
    if (rc < 0) {
229
        VIR_ERROR(_("Failed to detect mounts for %s"), group->path);
230
        return rc;
231 232
    }

233 234 235 236 237 238 239
    /* Check that at least 1 controller is available */
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
        if (group->controllers[i].mountPoint != NULL)
            any = 1;
    }
    if (!any)
        return -ENXIO;
240 241


242
    rc = virCgroupDetectPlacement(group);
243

244 245 246 247 248
    if (rc == 0) {
        /* Check that for every mounted controller, we found our placement */
        for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
            if (!group->controllers[i].mountPoint)
                continue;
249

250
            if (!group->controllers[i].placement) {
251
                VIR_ERROR(_("Could not find placement for controller %s at %s"),
252 253 254 255 256
                          virCgroupControllerTypeToString(i),
                          group->controllers[i].placement);
                rc = -ENOENT;
                break;
            }
257

258 259 260 261 262 263
            VIR_DEBUG("Detected mount/mapping %i:%s at %s in %s", i,
                      virCgroupControllerTypeToString(i),
                      group->controllers[i].mountPoint,
                      group->controllers[i].placement);
        }
    } else {
264
        VIR_ERROR(_("Failed to detect mapping for %s"), group->path);
265 266 267 268
    }

    return rc;
}
D
Daniel P. Berrange 已提交
269
#endif
270

271

272 273 274 275
int virCgroupPathOfController(virCgroupPtr group,
                              int controller,
                              const char *key,
                              char **path)
276
{
277 278 279 280 281 282 283 284 285 286 287 288 289
    if (controller == -1) {
        int i;
        for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
            if (group->controllers[i].mountPoint &&
                group->controllers[i].placement) {
                controller = i;
                break;
            }
        }
    }
    if (controller == -1)
        return -ENOSYS;

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    if (group->controllers[controller].mountPoint == NULL)
        return -ENOENT;

    if (group->controllers[controller].placement == NULL)
        return -ENOENT;

    if (virAsprintf(path, "%s%s%s/%s",
                    group->controllers[controller].mountPoint,
                    group->controllers[controller].placement,
                    STREQ(group->path, "/") ? "" : group->path,
                    key ? key : "") == -1)
        return -ENOMEM;

    return 0;
}


307
static int virCgroupSetValueStr(virCgroupPtr group,
308
                                int controller,
309 310 311 312 313 314
                                const char *key,
                                const char *value)
{
    int rc = 0;
    char *keypath = NULL;

315
    rc = virCgroupPathOfController(group, controller, key, &keypath);
316 317 318
    if (rc != 0)
        return rc;

319
    VIR_DEBUG("Set value '%s' to '%s'", keypath, value);
320
    rc = virFileWriteStr(keypath, value, 0);
321 322
    if (rc < 0) {
        rc = -errno;
323
        VIR_DEBUG("Failed to write value '%s': %m", value);
324 325
    } else {
        rc = 0;
326 327 328 329 330 331 332 333
    }

    VIR_FREE(keypath);

    return rc;
}

static int virCgroupGetValueStr(virCgroupPtr group,
334
                                int controller,
335 336 337 338 339 340
                                const char *key,
                                char **value)
{
    int rc;
    char *keypath = NULL;

341
    *value = NULL;
342

343
    rc = virCgroupPathOfController(group, controller, key, &keypath);
344
    if (rc != 0) {
345
        VIR_DEBUG("No path of %s, %s", group->path, key);
346 347 348
        return rc;
    }

349
    VIR_DEBUG("Get value %s", keypath);
350

351
    rc = virFileReadAll(keypath, 1024, value);
352 353
    if (rc < 0) {
        rc = -errno;
354
        VIR_DEBUG("Failed to read %s: %m\n", keypath);
355
    } else {
356 357 358 359
        /* Terminated with '\n' has sometimes harmful effects to the caller */
        char *p = strchr(*value, '\n');
        if (p) *p = '\0';

360
        rc = 0;
361 362 363 364 365 366 367
    }

    VIR_FREE(keypath);

    return rc;
}

368
static int virCgroupSetValueU64(virCgroupPtr group,
369
                                int controller,
370
                                const char *key,
D
Daniel P. Berrange 已提交
371
                                unsigned long long int value)
372 373 374 375
{
    char *strval = NULL;
    int rc;

D
Daniel P. Berrange 已提交
376
    if (virAsprintf(&strval, "%llu", value) == -1)
377 378
        return -ENOMEM;

379
    rc = virCgroupSetValueStr(group, controller, key, strval);
380 381 382 383 384 385 386

    VIR_FREE(strval);

    return rc;
}


387 388

static int virCgroupSetValueI64(virCgroupPtr group,
389
                                int controller,
390
                                const char *key,
D
Daniel P. Berrange 已提交
391
                                long long int value)
392 393 394 395
{
    char *strval = NULL;
    int rc;

D
Daniel P. Berrange 已提交
396
    if (virAsprintf(&strval, "%lld", value) == -1)
397 398
        return -ENOMEM;

399
    rc = virCgroupSetValueStr(group, controller, key, strval);
400 401 402 403 404 405 406

    VIR_FREE(strval);

    return rc;
}

static int virCgroupGetValueI64(virCgroupPtr group,
407
                                int controller,
408
                                const char *key,
D
Daniel P. Berrange 已提交
409
                                long long int *value)
410 411 412 413
{
    char *strval = NULL;
    int rc = 0;

414
    rc = virCgroupGetValueStr(group, controller, key, &strval);
415 416 417
    if (rc != 0)
        goto out;

418
    if (virStrToLong_ll(strval, NULL, 10, value) < 0)
419 420 421 422 423 424 425
        rc = -EINVAL;
out:
    VIR_FREE(strval);

    return rc;
}

426
static int virCgroupGetValueU64(virCgroupPtr group,
427
                                int controller,
428
                                const char *key,
D
Daniel P. Berrange 已提交
429
                                unsigned long long int *value)
430 431 432 433
{
    char *strval = NULL;
    int rc = 0;

434
    rc = virCgroupGetValueStr(group, controller, key, &strval);
435 436 437
    if (rc != 0)
        goto out;

D
Daniel P. Berrange 已提交
438
    if (virStrToLong_ull(strval, NULL, 10, value) < 0)
439 440 441 442 443 444 445 446
        rc = -EINVAL;
out:
    VIR_FREE(strval);

    return rc;
}


447
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
448
static int virCgroupCpuSetInherit(virCgroupPtr parent, virCgroupPtr group)
449 450 451 452 453 454 455 456
{
    int i;
    int rc = 0;
    const char *inherit_values[] = {
        "cpuset.cpus",
        "cpuset.mems",
    };

457 458 459
    VIR_DEBUG("Setting up inheritance %s -> %s", parent->path, group->path);
    for (i = 0; i < ARRAY_CARDINALITY(inherit_values) ; i++) {
        char *value;
460

461 462 463 464
        rc = virCgroupGetValueStr(parent,
                                  VIR_CGROUP_CONTROLLER_CPUSET,
                                  inherit_values[i],
                                  &value);
465
        if (rc != 0) {
466
            VIR_ERROR(_("Failed to get %s %d"), inherit_values[i], rc);
467 468 469 470 471 472 473 474 475
            break;
        }

        VIR_DEBUG("Inherit %s = %s", inherit_values[i], value);

        rc = virCgroupSetValueStr(group,
                                  VIR_CGROUP_CONTROLLER_CPUSET,
                                  inherit_values[i],
                                  value);
476
        VIR_FREE(value);
477 478

        if (rc != 0) {
479
            VIR_ERROR(_("Failed to set %s %d"), inherit_values[i], rc);
480 481 482 483 484 485 486
            break;
        }
    }

    return rc;
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
static int virCgroupSetMemoryUseHierarchy(virCgroupPtr group)
{
    int rc = 0;
    unsigned long long value;
    const char *filename = "memory.use_hierarchy";

    rc = virCgroupGetValueU64(group,
                              VIR_CGROUP_CONTROLLER_MEMORY,
                              filename, &value);
    if (rc != 0) {
        VIR_ERROR(_("Failed to read %s/%s (%d)"), group->path, filename, rc);
        return rc;
    }

    /* Setting twice causes error, so if already enabled, skip setting */
    if (value == 1)
        return 0;

    VIR_DEBUG("Setting up %s/%s", group->path, filename);
    rc = virCgroupSetValueU64(group,
                              VIR_CGROUP_CONTROLLER_MEMORY,
                              filename, 1);

    if (rc != 0) {
        VIR_ERROR(_("Failed to set %s/%s (%d)"), group->path, filename, rc);
    }

    return rc;
}

static int virCgroupMakeGroup(virCgroupPtr parent, virCgroupPtr group,
518
                              int create, unsigned int flags)
519 520 521 522
{
    int i;
    int rc = 0;

523
    VIR_DEBUG("Make group %s", group->path);
524
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
525 526
        char *path = NULL;

527 528
        /* Skip over controllers that aren't mounted */
        if (!group->controllers[i].mountPoint)
529 530
            continue;

531 532 533 534 535 536 537
        /* We need to control cpu bandwidth for each vcpu now */
        if ((flags & VIR_CGROUP_VCPU) && (i != VIR_CGROUP_CONTROLLER_CPU)) {
            /* treat it as unmounted and we can use virCgroupAddTask */
            VIR_FREE(group->controllers[i].mountPoint);
            continue;
        }

538 539 540
        rc = virCgroupPathOfController(group, i, "", &path);
        if (rc < 0)
            return rc;
541 542 543
        /* As of Feb 2011, clang can't see that the above function
         * call did not modify group. */
        sa_assert(group->controllers[i].mountPoint);
544

545
        VIR_DEBUG("Make controller %s", path);
546
        if (access(path, F_OK) != 0) {
547 548
            if (!create ||
                mkdir(path, 0755) < 0) {
549 550 551 552 553 554 555 556 557 558 559 560 561 562
                /* With a kernel that doesn't support multi-level directory
                 * for blkio controller, libvirt will fail and disable all
                 * other controllers even though they are available. So
                 * treat blkio as unmounted if mkdir fails. */
                if (i == VIR_CGROUP_CONTROLLER_BLKIO) {
                    rc = 0;
                    VIR_FREE(group->controllers[i].mountPoint);
                    VIR_FREE(path);
                    continue;
                } else {
                    rc = -errno;
                    VIR_FREE(path);
                    break;
                }
563
            }
564 565 566 567
            if (group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint != NULL &&
                (i == VIR_CGROUP_CONTROLLER_CPUSET ||
                 STREQ(group->controllers[i].mountPoint, group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint))) {
                rc = virCgroupCpuSetInherit(parent, group);
568 569
                if (rc != 0) {
                    VIR_FREE(path);
570
                    break;
571
                }
572
            }
573 574 575 576
            /*
             * Note that virCgroupSetMemoryUseHierarchy should always be
             * called prior to creating subcgroups and attaching tasks.
             */
577 578
            if ((flags & VIR_CGROUP_MEM_HIERACHY) &&
                (group->controllers[VIR_CGROUP_CONTROLLER_MEMORY].mountPoint != NULL) &&
579 580 581 582 583 584 585 586
                (i == VIR_CGROUP_CONTROLLER_MEMORY ||
                 STREQ(group->controllers[i].mountPoint, group->controllers[VIR_CGROUP_CONTROLLER_MEMORY].mountPoint))) {
                rc = virCgroupSetMemoryUseHierarchy(group);
                if (rc != 0) {
                    VIR_FREE(path);
                    break;
                }
            }
587 588 589 590 591 592 593 594
        }

        VIR_FREE(path);
    }

    return rc;
}

595 596 597

static int virCgroupNew(const char *path,
                        virCgroupPtr *group)
598 599 600 601
{
    int rc = 0;
    char *typpath = NULL;

602 603
    VIR_DEBUG("New group %s", path);
    *group = NULL;
604

605
    if (VIR_ALLOC((*group)) != 0) {
606 607 608 609
        rc = -ENOMEM;
        goto err;
    }

610
    if (!((*group)->path = strdup(path))) {
611 612 613 614
        rc = -ENOMEM;
        goto err;
    }

615 616 617
    rc = virCgroupDetect(*group);
    if (rc < 0)
        goto err;
618 619 620

    return rc;
err:
621 622
    virCgroupFree(group);
    *group = NULL;
623 624 625 626 627 628

    VIR_FREE(typpath);

    return rc;
}

629
static int virCgroupAppRoot(int privileged,
630 631
                            virCgroupPtr *group,
                            int create)
632
{
633 634
    virCgroupPtr rootgrp = NULL;
    int rc;
635

636 637 638
    rc = virCgroupNew("/", &rootgrp);
    if (rc != 0)
        return rc;
639

640 641 642 643 644
    if (privileged) {
        rc = virCgroupNew("/libvirt", group);
    } else {
        char *rootname;
        char *username;
645
        username = virGetUserName(getuid());
646 647 648 649 650 651 652 653 654 655
        if (!username) {
            rc = -ENOMEM;
            goto cleanup;
        }
        rc = virAsprintf(&rootname, "/libvirt-%s", username);
        VIR_FREE(username);
        if (rc < 0) {
            rc = -ENOMEM;
            goto cleanup;
        }
656

657 658
        rc = virCgroupNew(rootname, group);
        VIR_FREE(rootname);
659 660
    }
    if (rc != 0)
661
        goto cleanup;
662

663
    rc = virCgroupMakeGroup(rootgrp, *group, create, VIR_CGROUP_NONE);
664

665 666
cleanup:
    virCgroupFree(&rootgrp);
667 668
    return rc;
}
D
Daniel P. Berrange 已提交
669
#endif
670

671
#if defined _DIRENT_HAVE_D_TYPE
672 673 674 675 676 677 678 679
static int virCgroupRemoveRecursively(char *grppath)
{
    DIR *grpdir;
    struct dirent *ent;
    int rc = 0;

    grpdir = opendir(grppath);
    if (grpdir == NULL) {
680 681
        if (errno == ENOENT)
            return 0;
682
        rc = -errno;
683
        VIR_ERROR(_("Unable to open %s (%d)"), grppath, errno);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
        return rc;
    }

    for (;;) {
        char *path;

        errno = 0;
        ent = readdir(grpdir);
        if (ent == NULL) {
            if ((rc = -errno))
                VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
            break;
        }

        if (ent->d_name[0] == '.') continue;
        if (ent->d_type != DT_DIR) continue;

        if (virAsprintf(&path, "%s/%s", grppath, ent->d_name) == -1) {
            rc = -ENOMEM;
            break;
        }
        rc = virCgroupRemoveRecursively(path);
        VIR_FREE(path);
        if (rc != 0)
            break;
    }
    closedir(grpdir);

712
    VIR_DEBUG("Removing cgroup %s", grppath);
713 714 715 716 717 718 719
    if (rmdir(grppath) != 0 && errno != ENOENT) {
        rc = -errno;
        VIR_ERROR(_("Unable to remove %s (%d)"), grppath, errno);
    }

    return rc;
}
720 721 722 723 724 725 726
#else
static int virCgroupRemoveRecursively(char *grppath ATTRIBUTE_UNUSED)
{
    /* Claim no support */
    return -ENXIO;
}
#endif
727

728 729 730 731 732
/**
 * virCgroupRemove:
 *
 * @group: The group to be removed
 *
733 734 735 736 737
 * It first removes all child groups recursively
 * in depth first order and then removes @group
 * because the presence of the child groups
 * prevents removing @group.
 *
738 739 740 741 742 743 744 745
 * Returns: 0 on success
 */
int virCgroupRemove(virCgroupPtr group)
{
    int rc = 0;
    int i;
    char *grppath = NULL;

746
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
747 748
        /* Skip over controllers not mounted */
        if (!group->controllers[i].mountPoint)
749 750
            continue;

751 752 753 754 755
        if (virCgroupPathOfController(group,
                                      i,
                                      NULL,
                                      &grppath) != 0)
            continue;
756

757
        VIR_DEBUG("Removing cgroup %s and all child cgroups", grppath);
758
        rc = virCgroupRemoveRecursively(grppath);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
        VIR_FREE(grppath);
    }

    return rc;
}

/**
 * virCgroupAddTask:
 *
 * @group: The cgroup to add a task to
 * @pid: The pid of the task to add
 *
 * Returns: 0 on success
 */
int virCgroupAddTask(virCgroupPtr group, pid_t pid)
{
    int rc = 0;
    int i;

778
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
779 780 781
        /* Skip over controllers not mounted */
        if (!group->controllers[i].mountPoint)
            continue;
782

783
        rc = virCgroupSetValueU64(group, i, "tasks", (unsigned long long)pid);
784 785 786 787 788 789 790
        if (rc != 0)
            break;
    }

    return rc;
}

791

792
/**
793
 * virCgroupForDriver:
794
 *
795
 * @name: name of this driver (e.g., xen, qemu, lxc)
796 797 798 799
 * @group: Pointer to returned virCgroupPtr
 *
 * Returns 0 on success
 */
800
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
801 802 803 804
int virCgroupForDriver(const char *name,
                       virCgroupPtr *group,
                       int privileged,
                       int create)
805 806
{
    int rc;
807
    char *path = NULL;
808
    virCgroupPtr rootgrp = NULL;
809

810
    rc = virCgroupAppRoot(privileged, &rootgrp, create);
811
    if (rc != 0)
812 813
        goto out;

814 815
    if (virAsprintf(&path, "%s/%s", rootgrp->path, name) < 0) {
        rc = -ENOMEM;
816
        goto out;
817
    }
818

819 820 821
    rc = virCgroupNew(path, group);
    VIR_FREE(path);

822
    if (rc == 0) {
823
        rc = virCgroupMakeGroup(rootgrp, *group, create, VIR_CGROUP_NONE);
824 825 826
        if (rc != 0)
            virCgroupFree(group);
    }
827

828
out:
829
    virCgroupFree(&rootgrp);
830 831 832

    return rc;
}
D
Daniel P. Berrange 已提交
833 834 835 836 837 838 839 840 841 842
#else
int virCgroupForDriver(const char *name ATTRIBUTE_UNUSED,
                       virCgroupPtr *group ATTRIBUTE_UNUSED,
                       int privileged ATTRIBUTE_UNUSED,
                       int create ATTRIBUTE_UNUSED)
{
    /* Claim no support */
    return -ENXIO;
}
#endif
843

844 845 846 847 848 849 850 851 852 853

/**
 * virCgroupForDomain:
 *
 * @driver: group for driver owning the domain
 * @name: name of the domain
 * @group: Pointer to returned virCgroupPtr
 *
 * Returns 0 on success
 */
854
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
855 856 857 858 859 860 861 862
int virCgroupForDomain(virCgroupPtr driver,
                       const char *name,
                       virCgroupPtr *group,
                       int create)
{
    int rc;
    char *path;

863 864 865
    if (driver == NULL)
        return -EINVAL;

866 867 868 869 870 871
    if (virAsprintf(&path, "%s/%s", driver->path, name) < 0)
        return -ENOMEM;

    rc = virCgroupNew(path, group);
    VIR_FREE(path);

872
    if (rc == 0) {
873 874 875 876 877 878 879 880 881 882
        /*
         * Create a cgroup with memory.use_hierarchy enabled to
         * surely account memory usage of lxc with ns subsystem
         * enabled. (To be exact, memory and ns subsystems are
         * enabled at the same time.)
         *
         * The reason why doing it here, not a upper group, say
         * a group for driver, is to avoid overhead to track
         * cumulative usage that we don't need.
         */
883
        rc = virCgroupMakeGroup(driver, *group, create, VIR_CGROUP_MEM_HIERACHY);
884 885 886 887 888 889
        if (rc != 0)
            virCgroupFree(group);
    }

    return rc;
}
D
Daniel P. Berrange 已提交
890 891 892 893 894 895 896 897 898
#else
int virCgroupForDomain(virCgroupPtr driver ATTRIBUTE_UNUSED,
                       const char *name ATTRIBUTE_UNUSED,
                       virCgroupPtr *group ATTRIBUTE_UNUSED,
                       int create ATTRIBUTE_UNUSED)
{
    return -ENXIO;
}
#endif
899

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
/**
 * virCgroupForVcpu:
 *
 * @driver: group for the domain
 * @vcpuid: id of the vcpu
 * @group: Pointer to returned virCgroupPtr
 *
 * Returns 0 on success
 */
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
int virCgroupForVcpu(virCgroupPtr driver,
                     int vcpuid,
                     virCgroupPtr *group,
                     int create)
{
    int rc;
    char *path;

    if (driver == NULL)
        return -EINVAL;

    if (virAsprintf(&path, "%s/vcpu%d", driver->path, vcpuid) < 0)
        return -ENOMEM;

    rc = virCgroupNew(path, group);
    VIR_FREE(path);

    if (rc == 0) {
        rc = virCgroupMakeGroup(driver, *group, create, VIR_CGROUP_VCPU);
        if (rc != 0)
            virCgroupFree(group);
    }

    return rc;
}
#else
int virCgroupForVcpu(virCgroupPtr driver ATTRIBUTE_UNUSED,
                     int vcpuid ATTRIBUTE_UNUSED,
                     virCgroupPtr *group ATTRIBUTE_UNUSED,
                     int create ATTRIBUTE_UNUSED)
{
    return -ENXIO;
}
#endif

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
/**
 * virCgroupSetBlkioWeight:
 *
 * @group: The cgroup to change io weight for
 * @weight: The Weight for this cgroup
 *
 * Returns: 0 on success
 */
int virCgroupSetBlkioWeight(virCgroupPtr group, unsigned int weight)
{
    if (weight > 1000 || weight < 100)
        return -EINVAL;

    return virCgroupSetValueU64(group,
                                VIR_CGROUP_CONTROLLER_BLKIO,
                                "blkio.weight",
                                weight);
}

/**
 * virCgroupGetBlkioWeight:
 *
 * @group: The cgroup to get weight for
 * @Weight: Pointer to returned weight
 *
 * Returns: 0 on success
 */
int virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight)
{
    unsigned long long tmp;
    int ret;
    ret = virCgroupGetValueU64(group,
                               VIR_CGROUP_CONTROLLER_BLKIO,
                               "blkio.weight", &tmp);
    if (ret == 0)
        *weight = tmp;
    return ret;
}

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
/**
 * virCgroupSetBlkioDeviceWeight:
 *
 * @group: The cgroup to change io device weight device for
 * @path: The device with a weight to alter
 * @weight: The new device weight (100-1000), or 0 to clear
 *
 * device_weight is treated as a write-only parameter, so
 * there isn't a getter counterpart.
 *
 * Returns: 0 on success, -errno on failure
 */
#if defined(major) && defined(minor)
int virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
                                  const char *path,
                                  unsigned int weight)
{
    char *str;
    struct stat sb;
    int ret;

    if (weight && (weight > 1000 || weight < 100))
        return -EINVAL;

    if (stat(path, &sb) < 0)
        return -errno;

    if (!S_ISBLK(sb.st_mode))
        return -EINVAL;

    if (virAsprintf(&str, "%d:%d %d", major(sb.st_rdev), minor(sb.st_rdev),
                    weight) < 0)
        return -errno;

    ret = virCgroupSetValueStr(group,
                               VIR_CGROUP_CONTROLLER_BLKIO,
                               "blkio.weight_device",
                               str);
    VIR_FREE(str);
    return ret;
}
#else
        int
 virCgroupSetBlkioDeviceWeight(virCgroupPtr group ATTRIBUTE_UNUSED,
                                  const char *path ATTRIBUTE_UNUSED,
                                  unsigned int weight ATTRIBUTE_UNUSED)
{
    return -ENOSYS;
}
#endif

1035 1036 1037 1038 1039 1040 1041 1042
/**
 * virCgroupSetMemory:
 *
 * @group: The cgroup to change memory for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
1043
int virCgroupSetMemory(virCgroupPtr group, unsigned long long kb)
1044
{
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;

    if (kb > maxkb)
        return -EINVAL;
    else if (kb == maxkb)
        return virCgroupSetValueI64(group,
                                    VIR_CGROUP_CONTROLLER_MEMORY,
                                    "memory.limit_in_bytes",
                                    -1);
    else
        return virCgroupSetValueU64(group,
                                    VIR_CGROUP_CONTROLLER_MEMORY,
                                    "memory.limit_in_bytes",
                                    kb << 10);
1059 1060
}

R
Ryota Ozaki 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
/**
 * virCgroupGetMemoryUsage:
 *
 * @group: The cgroup to change memory for
 * @kb: Pointer to returned used memory in kilobytes
 *
 * Returns: 0 on success
 */
int virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb)
{
C
Cole Robinson 已提交
1071
    long long unsigned int usage_in_bytes;
R
Ryota Ozaki 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080
    int ret;
    ret = virCgroupGetValueU64(group,
                               VIR_CGROUP_CONTROLLER_MEMORY,
                               "memory.usage_in_bytes", &usage_in_bytes);
    if (ret == 0)
        *kb = (unsigned long) usage_in_bytes >> 10;
    return ret;
}

1081 1082 1083 1084 1085 1086 1087 1088
/**
 * virCgroupSetMemoryHardLimit:
 *
 * @group: The cgroup to change memory hard limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
1089
int virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb)
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
{
    return virCgroupSetMemory(group, kb);
}

/**
 * virCgroupGetMemoryHardLimit:
 *
 * @group: The cgroup to get the memory hard limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
1102
int virCgroupGetMemoryHardLimit(virCgroupPtr group, unsigned long long *kb)
1103 1104 1105 1106 1107 1108 1109
{
    long long unsigned int limit_in_bytes;
    int ret;
    ret = virCgroupGetValueU64(group,
                               VIR_CGROUP_CONTROLLER_MEMORY,
                               "memory.limit_in_bytes", &limit_in_bytes);
    if (ret == 0)
1110
        *kb = limit_in_bytes >> 10;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    return ret;
}

/**
 * virCgroupSetMemorySoftLimit:
 *
 * @group: The cgroup to change memory soft limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
1122
int virCgroupSetMemorySoftLimit(virCgroupPtr group, unsigned long long kb)
1123
{
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;

    if (kb > maxkb)
        return -EINVAL;
    else if (kb == maxkb)
        return virCgroupSetValueI64(group,
                                    VIR_CGROUP_CONTROLLER_MEMORY,
                                    "memory.soft_limit_in_bytes",
                                    -1);
    else
        return virCgroupSetValueU64(group,
                                    VIR_CGROUP_CONTROLLER_MEMORY,
                                    "memory.soft_limit_in_bytes",
                                    kb << 10);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
}


/**
 * virCgroupGetMemorySoftLimit:
 *
 * @group: The cgroup to get the memory soft limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
1149
int virCgroupGetMemorySoftLimit(virCgroupPtr group, unsigned long long *kb)
1150 1151 1152 1153 1154 1155 1156
{
    long long unsigned int limit_in_bytes;
    int ret;
    ret = virCgroupGetValueU64(group,
                               VIR_CGROUP_CONTROLLER_MEMORY,
                               "memory.soft_limit_in_bytes", &limit_in_bytes);
    if (ret == 0)
1157
        *kb = limit_in_bytes >> 10;
1158 1159 1160 1161
    return ret;
}

/**
1162
 * virCgroupSetMemSwapHardLimit:
1163
 *
1164 1165
 * @group: The cgroup to change mem+swap hard limit for
 * @kb: The mem+swap amount in kilobytes
1166 1167 1168
 *
 * Returns: 0 on success
 */
1169
int virCgroupSetMemSwapHardLimit(virCgroupPtr group, unsigned long long kb)
1170
{
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;

    if (kb > maxkb)
        return -EINVAL;
    else if (kb == maxkb)
        return virCgroupSetValueI64(group,
                                    VIR_CGROUP_CONTROLLER_MEMORY,
                                    "memory.memsw.limit_in_bytes",
                                    -1);
    else
        return virCgroupSetValueU64(group,
                                    VIR_CGROUP_CONTROLLER_MEMORY,
                                    "memory.memsw.limit_in_bytes",
                                    kb << 10);
1185 1186 1187
}

/**
1188
 * virCgroupGetMemSwapHardLimit:
1189
 *
1190 1191
 * @group: The cgroup to get mem+swap hard limit for
 * @kb: The mem+swap amount in kilobytes
1192 1193 1194
 *
 * Returns: 0 on success
 */
1195
int virCgroupGetMemSwapHardLimit(virCgroupPtr group, unsigned long long *kb)
1196 1197 1198 1199 1200 1201 1202
{
    long long unsigned int limit_in_bytes;
    int ret;
    ret = virCgroupGetValueU64(group,
                               VIR_CGROUP_CONTROLLER_MEMORY,
                               "memory.memsw.limit_in_bytes", &limit_in_bytes);
    if (ret == 0)
1203
        *kb = limit_in_bytes >> 10;
1204 1205 1206
    return ret;
}

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
/**
 * virCgroupSetCpusetMems:
 *
 * @group: The cgroup to set cpuset.mems for
 * @mems: the numa nodes to set
 *
 * Returns: 0 on success
 */
int virCgroupSetCpusetMems(virCgroupPtr group, const char *mems)
{
    return virCgroupSetValueStr(group,
                                VIR_CGROUP_CONTROLLER_CPUSET,
                                "cpuset.mems",
                                mems);
}

/**
 * virCgroupGetCpusetMems:
 *
 * @group: The cgroup to get cpuset.mems for
 * @mems: the numa nodes to get
 *
 * Returns: 0 on success
 */
int virCgroupGetCpusetMems(virCgroupPtr group, char **mems)
{
    return virCgroupGetValueStr(group,
                                VIR_CGROUP_CONTROLLER_CPUSET,
                                "cpuset.mems",
                                mems);
}

1239 1240 1241
/**
 * virCgroupDenyAllDevices:
 *
1242
 * @group: The cgroup to deny all permissions, for all devices
1243 1244 1245 1246 1247 1248
 *
 * Returns: 0 on success
 */
int virCgroupDenyAllDevices(virCgroupPtr group)
{
    return virCgroupSetValueStr(group,
1249 1250 1251
                                VIR_CGROUP_CONTROLLER_DEVICES,
                                "devices.deny",
                                "a");
1252 1253 1254 1255 1256 1257 1258 1259 1260
}

/**
 * virCgroupAllowDevice:
 *
 * @group: The cgroup to allow a device for
 * @type: The device type (i.e., 'c' or 'b')
 * @major: The major number of the device
 * @minor: The minor number of the device
1261
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
1262 1263 1264
 *
 * Returns: 0 on success
 */
1265 1266
int virCgroupAllowDevice(virCgroupPtr group, char type, int major, int minor,
                         int perms)
1267 1268 1269 1270
{
    int rc;
    char *devstr = NULL;

1271 1272 1273 1274
    if (virAsprintf(&devstr, "%c %i:%i %s%s%s", type, major, minor,
                    perms & VIR_CGROUP_DEVICE_READ ? "r" : "",
                    perms & VIR_CGROUP_DEVICE_WRITE ? "w" : "",
                    perms & VIR_CGROUP_DEVICE_MKNOD ? "m" : "") == -1) {
1275 1276 1277 1278 1279
        rc = -ENOMEM;
        goto out;
    }

    rc = virCgroupSetValueStr(group,
1280
                              VIR_CGROUP_CONTROLLER_DEVICES,
1281 1282 1283 1284 1285 1286 1287
                              "devices.allow",
                              devstr);
out:
    VIR_FREE(devstr);

    return rc;
}
1288

1289 1290 1291 1292 1293 1294
/**
 * virCgroupAllowDeviceMajor:
 *
 * @group: The cgroup to allow an entire device major type for
 * @type: The device type (i.e., 'c' or 'b')
 * @major: The major number of the device type
1295
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
1296 1297 1298
 *
 * Returns: 0 on success
 */
1299 1300
int virCgroupAllowDeviceMajor(virCgroupPtr group, char type, int major,
                              int perms)
1301 1302 1303 1304
{
    int rc;
    char *devstr = NULL;

1305 1306 1307 1308
    if (virAsprintf(&devstr, "%c %i:* %s%s%s", type, major,
                    perms & VIR_CGROUP_DEVICE_READ ? "r" : "",
                    perms & VIR_CGROUP_DEVICE_WRITE ? "w" : "",
                    perms & VIR_CGROUP_DEVICE_MKNOD ? "m" : "") == -1) {
1309 1310 1311 1312 1313
        rc = -ENOMEM;
        goto out;
    }

    rc = virCgroupSetValueStr(group,
1314
                              VIR_CGROUP_CONTROLLER_DEVICES,
1315 1316 1317 1318 1319 1320 1321 1322
                              "devices.allow",
                              devstr);
 out:
    VIR_FREE(devstr);

    return rc;
}

1323 1324 1325 1326 1327
/**
 * virCgroupAllowDevicePath:
 *
 * @group: The cgroup to allow the device for
 * @path: the device to allow
1328
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
1329 1330 1331 1332
 *
 * Queries the type of device and its major/minor number, and
 * adds that to the cgroup ACL
 *
1333 1334
 * Returns: 0 on success, 1 if path exists but is not a device, or
 * negative errno value on failure
1335
 */
D
Daniel P. Berrange 已提交
1336
#if defined(major) && defined(minor)
1337
int virCgroupAllowDevicePath(virCgroupPtr group, const char *path, int perms)
1338 1339 1340 1341 1342 1343 1344
{
    struct stat sb;

    if (stat(path, &sb) < 0)
        return -errno;

    if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
1345
        return 1;
1346 1347 1348 1349

    return virCgroupAllowDevice(group,
                                S_ISCHR(sb.st_mode) ? 'c' : 'b',
                                major(sb.st_rdev),
1350 1351
                                minor(sb.st_rdev),
                                perms);
1352
}
D
Daniel P. Berrange 已提交
1353 1354
#else
int virCgroupAllowDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED,
1355 1356
                             const char *path ATTRIBUTE_UNUSED,
                             int perms ATTRIBUTE_UNUSED)
D
Daniel P. Berrange 已提交
1357 1358 1359 1360 1361
{
    return -ENOSYS;
}
#endif

1362 1363 1364 1365 1366 1367 1368 1369

/**
 * virCgroupDenyDevice:
 *
 * @group: The cgroup to deny a device for
 * @type: The device type (i.e., 'c' or 'b')
 * @major: The major number of the device
 * @minor: The minor number of the device
1370
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to deny
1371 1372 1373
 *
 * Returns: 0 on success
 */
1374 1375
int virCgroupDenyDevice(virCgroupPtr group, char type, int major, int minor,
                        int perms)
1376 1377 1378 1379
{
    int rc;
    char *devstr = NULL;

1380 1381 1382 1383
    if (virAsprintf(&devstr, "%c %i:%i %s%s%s", type, major, minor,
                    perms & VIR_CGROUP_DEVICE_READ ? "r" : "",
                    perms & VIR_CGROUP_DEVICE_WRITE ? "w" : "",
                    perms & VIR_CGROUP_DEVICE_MKNOD ? "m" : "") == -1) {
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
        rc = -ENOMEM;
        goto out;
    }

    rc = virCgroupSetValueStr(group,
                              VIR_CGROUP_CONTROLLER_DEVICES,
                              "devices.deny",
                              devstr);
out:
    VIR_FREE(devstr);

    return rc;
}

/**
 * virCgroupDenyDeviceMajor:
 *
 * @group: The cgroup to deny an entire device major type for
 * @type: The device type (i.e., 'c' or 'b')
 * @major: The major number of the device type
1404
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to deny
1405 1406 1407
 *
 * Returns: 0 on success
 */
1408 1409
int virCgroupDenyDeviceMajor(virCgroupPtr group, char type, int major,
                             int perms)
1410 1411 1412 1413
{
    int rc;
    char *devstr = NULL;

1414 1415 1416 1417
    if (virAsprintf(&devstr, "%c %i:* %s%s%s", type, major,
                    perms & VIR_CGROUP_DEVICE_READ ? "r" : "",
                    perms & VIR_CGROUP_DEVICE_WRITE ? "w" : "",
                    perms & VIR_CGROUP_DEVICE_MKNOD ? "m" : "") == -1) {
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
        rc = -ENOMEM;
        goto out;
    }

    rc = virCgroupSetValueStr(group,
                              VIR_CGROUP_CONTROLLER_DEVICES,
                              "devices.deny",
                              devstr);
 out:
    VIR_FREE(devstr);

    return rc;
}

D
Daniel P. Berrange 已提交
1432
#if defined(major) && defined(minor)
1433
int virCgroupDenyDevicePath(virCgroupPtr group, const char *path, int perms)
1434 1435 1436 1437 1438 1439 1440
{
    struct stat sb;

    if (stat(path, &sb) < 0)
        return -errno;

    if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
1441
        return 1;
1442 1443 1444 1445

    return virCgroupDenyDevice(group,
                               S_ISCHR(sb.st_mode) ? 'c' : 'b',
                               major(sb.st_rdev),
1446 1447
                               minor(sb.st_rdev),
                               perms);
1448
}
D
Daniel P. Berrange 已提交
1449 1450
#else
int virCgroupDenyDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED,
1451 1452
                            const char *path ATTRIBUTE_UNUSED,
                            int perms ATTRIBUTE_UNUSED)
D
Daniel P. Berrange 已提交
1453 1454 1455 1456
{
    return -ENOSYS;
}
#endif
1457

1458
int virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares)
1459
{
1460 1461
    return virCgroupSetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
D
Daniel P. Berrange 已提交
1462
                                "cpu.shares", shares);
1463 1464
}

1465
int virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares)
1466
{
1467 1468
    return virCgroupGetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
D
Daniel P. Berrange 已提交
1469
                                "cpu.shares", shares);
1470
}
1471

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
/**
 * virCgroupSetCpuCfsPeriod:
 *
 * @group: The cgroup to change cpu.cfs_period_us for
 * @cfs_period: The bandwidth period in usecs
 *
 * Returns: 0 on success
 */
int virCgroupSetCpuCfsPeriod(virCgroupPtr group, unsigned long long cfs_period)
{
    /* The cfs_period shoule be greater or equal than 1ms, and less or equal
     * than 1s.
     */
    if (cfs_period < 1000 || cfs_period > 1000000)
        return -EINVAL;

    return virCgroupSetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
                                "cpu.cfs_period_us", cfs_period);
}

/**
 * virCgroupGetCpuCfsPeriod:
 *
 * @group: The cgroup to get cpu.cfs_period_us for
 * @cfs_period: Pointer to the returned bandwidth period in usecs
 *
 * Returns: 0 on success
 */
int virCgroupGetCpuCfsPeriod(virCgroupPtr group, unsigned long long *cfs_period)
{
    return virCgroupGetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
                                "cpu.cfs_period_us", cfs_period);
}

/**
 * virCgroupSetCpuCfsQuota:
 *
 * @group: The cgroup to change cpu.cfs_quota_us for
 * @cfs_quota: the cpu bandwidth (in usecs) that this tg will be allowed to
 *             consume over period
 *
 * Returns: 0 on success
 */
int virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota)
{
    if (cfs_quota >= 0) {
        /* The cfs_quota shoule be greater or equal than 1ms */
        if (cfs_quota < 1000)
            return -EINVAL;

        /* check overflow */
        if (cfs_quota > ULLONG_MAX / 1000)
            return -EINVAL;
    }

    return virCgroupSetValueI64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
                                "cpu.cfs_quota_us", cfs_quota);
}

/**
 * virCgroupGetCpuCfsQuota:
 *
 * @group: The cgroup to get cpu.cfs_quota_us for
 * @cfs_quota: Pointer to the returned cpu bandwidth (in usecs) that this tg
 *             will be allowed to consume over period
 *
 * Returns: 0 on success
 */
int virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota)
{
    return virCgroupGetValueI64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
                                "cpu.cfs_quota_us", cfs_quota);
}

1550 1551
int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
{
1552 1553
    return virCgroupGetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPUACCT,
D
Daniel P. Berrange 已提交
1554
                                "cpuacct.usage", usage);
1555
}
R
Ryota Ozaki 已提交
1556 1557 1558 1559

int virCgroupSetFreezerState(virCgroupPtr group, const char *state)
{
    return virCgroupSetValueStr(group,
1560
                                VIR_CGROUP_CONTROLLER_FREEZER,
R
Ryota Ozaki 已提交
1561 1562 1563 1564 1565
                                "freezer.state", state);
}

int virCgroupGetFreezerState(virCgroupPtr group, char **state)
{
1566
    return virCgroupGetValueStr(group,
1567
                                VIR_CGROUP_CONTROLLER_FREEZER,
R
Ryota Ozaki 已提交
1568 1569
                                "freezer.state", state);
}
1570

1571

E
Eric Blake 已提交
1572
#if defined HAVE_KILL && defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
1573 1574 1575 1576 1577 1578
static int virCgroupKillInternal(virCgroupPtr group, int signum, virHashTablePtr pids)
{
    int rc;
    int killedAny = 0;
    char *keypath = NULL;
    bool done = false;
E
Eric Blake 已提交
1579 1580 1581
    FILE *fp = NULL;
    VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
              group, group->path, signum, pids);
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604

    rc = virCgroupPathOfController(group, -1, "tasks", &keypath);
    if (rc != 0) {
        VIR_DEBUG("No path of %s, tasks", group->path);
        return rc;
    }

    /* PIDs may be forking as we kill them, so loop
     * until there are no new PIDs found
     */
    while (!done) {
        done = true;
        if (!(fp = fopen(keypath, "r"))) {
            rc = -errno;
            VIR_DEBUG("Failed to read %s: %m\n", keypath);
            goto cleanup;
        } else {
            while (!feof(fp)) {
                unsigned long pid;
                if (fscanf(fp, "%lu", &pid) != 1) {
                    if (feof(fp))
                        break;
                    rc = -errno;
E
Eric Blake 已提交
1605 1606
                    VIR_DEBUG("Failed to read %s: %m\n", keypath);
                    goto cleanup;
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
                }
                if (virHashLookup(pids, (void*)pid))
                    continue;

                VIR_DEBUG("pid=%lu", pid);
                if (kill((pid_t)pid, signum) < 0) {
                    if (errno != ESRCH) {
                        rc = -errno;
                        goto cleanup;
                    }
                    /* Leave RC == 0 since we didn't kill one */
                } else {
                    killedAny = 1;
                    done = false;
                }

E
Eric Blake 已提交
1623
                ignore_value(virHashAddEntry(pids, (void*)pid, (void*)1));
1624 1625 1626 1627 1628 1629 1630 1631 1632
            }
            VIR_FORCE_FCLOSE(fp);
        }
    }

    rc = killedAny ? 1 : 0;

cleanup:
    VIR_FREE(keypath);
E
Eric Blake 已提交
1633
    VIR_FORCE_FCLOSE(fp);
1634 1635 1636 1637 1638

    return rc;
}


1639
static uint32_t virCgroupPidCode(const void *name)
1640
{
1641
    return (uint32_t)(intptr_t)name;
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
}
static bool virCgroupPidEqual(const void *namea, const void *nameb)
{
    return namea == nameb;
}
static void *virCgroupPidCopy(const void *name)
{
    return (void*)name;
}

/*
 * Returns
 *   < 0 : errno that occurred
 *     0 : no PIDs killed
 *     1 : at least one PID killed
 */
int virCgroupKill(virCgroupPtr group, int signum)
{
    VIR_DEBUG("group=%p path=%s signum=%d", group, group->path, signum);
    int rc;
    /* The 'tasks' file in cgroups can contain duplicated
     * pids, so we use a hash to track which we've already
     * killed.
     */
    virHashTablePtr pids = virHashCreateFull(100,
                                             NULL,
                                             virCgroupPidCode,
                                             virCgroupPidEqual,
                                             virCgroupPidCopy,
                                             NULL);

    rc = virCgroupKillInternal(group, signum, pids);

    virHashFree(pids);

    return rc;
}


static int virCgroupKillRecursiveInternal(virCgroupPtr group, int signum, virHashTablePtr pids, bool dormdir)
{
    int rc;
    int killedAny = 0;
    char *keypath = NULL;
    DIR *dp;
    virCgroupPtr subgroup = NULL;
    struct dirent *ent;
    VIR_DEBUG("group=%p path=%s signum=%d pids=%p", group, group->path, signum, pids);

    rc = virCgroupPathOfController(group, -1, "", &keypath);
    if (rc != 0) {
        VIR_DEBUG("No path of %s, tasks", group->path);
        return rc;
    }

    if ((rc = virCgroupKillInternal(group, signum, pids)) != 0)
        return rc;

    VIR_DEBUG("Iterate over children of %s", keypath);
    if (!(dp = opendir(keypath))) {
        rc = -errno;
        return rc;
    }

    while ((ent = readdir(dp))) {
        char *subpath;

        if (STREQ(ent->d_name, "."))
            continue;
        if (STREQ(ent->d_name, ".."))
            continue;
        if (ent->d_type != DT_DIR)
            continue;

        VIR_DEBUG("Process subdir %s", ent->d_name);
        if (virAsprintf(&subpath, "%s/%s", group->path, ent->d_name) < 0) {
            rc = -ENOMEM;
            goto cleanup;
        }

        if ((rc = virCgroupNew(subpath, &subgroup)) != 0)
            goto cleanup;

        if ((rc = virCgroupKillRecursiveInternal(subgroup, signum, pids, true)) < 0)
            goto cleanup;
        if (rc == 1)
            killedAny = 1;

        if (dormdir)
            virCgroupRemove(subgroup);

        virCgroupFree(&subgroup);
    }

    rc = killedAny;

cleanup:
    virCgroupFree(&subgroup);
    closedir(dp);

    return rc;
}

int virCgroupKillRecursive(virCgroupPtr group, int signum)
{
    int rc;
    VIR_DEBUG("group=%p path=%s signum=%d", group, group->path, signum);
    virHashTablePtr pids = virHashCreateFull(100,
                                             NULL,
                                             virCgroupPidCode,
                                             virCgroupPidEqual,
                                             virCgroupPidCopy,
                                             NULL);

    rc = virCgroupKillRecursiveInternal(group, signum, pids, false);

    virHashFree(pids);

    return rc;
}


int virCgroupKillPainfully(virCgroupPtr group)
{
    int i;
    int rc;
    VIR_DEBUG("cgroup=%p path=%s", group, group->path);
    for (i = 0 ; i < 15 ; i++) {
        int signum;
        if (i == 0)
            signum = SIGTERM;
        else if (i == 8)
            signum = SIGKILL;
        else
            signum = 0; /* Just check for existance */

        rc = virCgroupKillRecursive(group, signum);
        VIR_DEBUG("Iteration %d rc=%d", i, rc);
        /* If rc == -1 we hit error, if 0 we ran out of PIDs */
        if (rc <= 0)
            break;

        usleep(200 * 1000);
    }
    VIR_DEBUG("Complete %d", rc);
    return rc;
}
1789

E
Eric Blake 已提交
1790
#else /* !(HAVE_KILL, HAVE_MNTENT_H, HAVE_GETMNTENT_R) */
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
int virCgroupKill(virCgroupPtr group ATTRIBUTE_UNUSED,
                  int signum ATTRIBUTE_UNUSED)
{
    return -ENOSYS;
}
int virCgroupKillRecursive(virCgroupPtr group ATTRIBUTE_UNUSED,
                           int signum ATTRIBUTE_UNUSED)
{
    return -ENOSYS;
}

int virCgroupKillPainfully(virCgroupPtr group ATTRIBUTE_UNUSED)
{
    return -ENOSYS;
}
E
Eric Blake 已提交
1806
#endif /* HAVE_KILL, HAVE_MNTENT_H, HAVE_GETMNTENT_R */