cgroup.c 23.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * cgroup.c: Tools for managing cgroups
 *
 * 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>
15
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
16
# include <mntent.h>
D
Daniel P. Berrange 已提交
17
#endif
18 19 20 21 22 23 24 25 26 27 28 29 30
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libgen.h>

#include "internal.h"
#include "util.h"
#include "memory.h"
#include "cgroup.h"
31
#include "logging.h"
32 33 34

#define CGROUP_MAX_VAL 512

35
VIR_ENUM_IMPL(virCgroupController, VIR_CGROUP_CONTROLLER_LAST,
R
Ryota Ozaki 已提交
36 37
              "cpu", "cpuacct", "cpuset", "memory", "devices",
              "freezer");
38 39 40 41 42 43

struct virCgroupController {
    int type;
    char *mountPoint;
    char *placement;
};
44 45 46

struct virCgroup {
    char *path;
47 48

    struct virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST];
49 50 51 52 53 54 55 56 57
};

/**
 * virCgroupFree:
 *
 * @group: The group structure to free
 */
void virCgroupFree(virCgroupPtr *group)
{
58 59 60 61 62 63 64 65
    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);
66
    }
67 68 69

    VIR_FREE((*group)->path);
    VIR_FREE(*group);
70 71
}

72
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
73 74 75 76 77
/*
 * Process /proc/mounts figuring out what controllers are
 * mounted and where
 */
static int virCgroupDetectMounts(virCgroupPtr group)
78
{
79
    int i;
80
    FILE *mounts = NULL;
81 82 83 84 85
    struct mntent entry;
    char buf[CGROUP_MAX_VAL];

    mounts = fopen("/proc/mounts", "r");
    if (mounts == NULL) {
86
        VIR_ERROR0(_("Unable to open /proc/mounts"));
87
        return -ENOENT;
88 89 90
    }

    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
91 92
        if (STRNEQ(entry.mnt_type, "cgroup"))
            continue;
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        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);
                }
                if (typelen == len && STREQLEN(typestr, tmp, len) &&
                    !(group->controllers[i].mountPoint = strdup(entry.mnt_dir)))
                    goto no_memory;
                tmp = next;
            }
        }
113 114 115 116
    }

    fclose(mounts);

117
    return 0;
118

119 120 121 122
no_memory:
    if (mounts)
        fclose(mounts);
    return -ENOMEM;
123 124
}

125 126 127 128 129

/*
 * Process /proc/self/cgroup figuring out what cgroup
 * sub-path the current process is assigned to. ie not
 * neccessarily in the root
130
 */
131
static int virCgroupDetectPlacement(virCgroupPtr group)
132 133
{
    int i;
134 135
    FILE *mapping  = NULL;
    char line[1024];
136

137 138
    mapping = fopen("/proc/self/cgroup", "r");
    if (mapping == NULL) {
139
        VIR_ERROR0(_("Unable to open /proc/self/cgroup"));
140
        return -ENOENT;
141 142
    }

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    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;
            }
        }
    }

    fclose(mapping);

182
    return 0;
183 184

no_memory:
185
    fclose(mapping);
186 187
    return -ENOMEM;

188 189
}

190
static int virCgroupDetect(virCgroupPtr group)
191
{
192 193 194
    int any = 0;
    int rc;
    int i;
195

196 197 198 199
    rc = virCgroupDetectMounts(group);
    if (rc < 0) {
        VIR_ERROR("Failed to detect mounts for %s", group->path);
        return rc;
200 201
    }

202 203 204 205 206 207 208
    /* 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;
209 210


211
    rc = virCgroupDetectPlacement(group);
212

213 214 215 216 217
    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;
218

219 220 221 222 223 224 225
            if (!group->controllers[i].placement) {
                VIR_ERROR("Could not find placement for controller %s at %s",
                          virCgroupControllerTypeToString(i),
                          group->controllers[i].placement);
                rc = -ENOENT;
                break;
            }
226

227 228 229 230 231 232 233
            VIR_DEBUG("Detected mount/mapping %i:%s at %s in %s", i,
                      virCgroupControllerTypeToString(i),
                      group->controllers[i].mountPoint,
                      group->controllers[i].placement);
        }
    } else {
        VIR_ERROR("Failed to detect mapping for %s", group->path);
234 235 236 237
    }

    return rc;
}
D
Daniel P. Berrange 已提交
238
#endif
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

static int virCgroupPathOfController(virCgroupPtr group,
                                     int controller,
                                     const char *key,
                                     char **path)
{
    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;
}


263
static int virCgroupSetValueStr(virCgroupPtr group,
264
                                int controller,
265 266 267 268 269 270
                                const char *key,
                                const char *value)
{
    int rc = 0;
    char *keypath = NULL;

271
    rc = virCgroupPathOfController(group, controller, key, &keypath);
272 273 274
    if (rc != 0)
        return rc;

275 276
    VIR_DEBUG("Set value %s", keypath);
    rc = virFileWriteStr(keypath, value);
277 278 279
    if (rc < 0) {
        DEBUG("Failed to write value '%s': %m", value);
        rc = -errno;
280 281
    } else {
        rc = 0;
282 283 284 285 286 287 288 289
    }

    VIR_FREE(keypath);

    return rc;
}

static int virCgroupGetValueStr(virCgroupPtr group,
290
                                int controller,
291 292 293 294 295 296
                                const char *key,
                                char **value)
{
    int rc;
    char *keypath = NULL;

297
    *value = NULL;
298

299
    rc = virCgroupPathOfController(group, controller, key, &keypath);
300 301 302 303 304
    if (rc != 0) {
        DEBUG("No path of %s, %s", group->path, key);
        return rc;
    }

305
    VIR_DEBUG("Get value %s", keypath);
306

307
    rc = virFileReadAll(keypath, 1024, value);
308 309 310
    if (rc < 0) {
        DEBUG("Failed to read %s: %m\n", keypath);
        rc = -errno;
311
    } else {
312 313 314 315
        /* Terminated with '\n' has sometimes harmful effects to the caller */
        char *p = strchr(*value, '\n');
        if (p) *p = '\0';

316
        rc = 0;
317 318 319 320 321 322 323
    }

    VIR_FREE(keypath);

    return rc;
}

324
static int virCgroupSetValueU64(virCgroupPtr group,
325
                                int controller,
326
                                const char *key,
D
Daniel P. Berrange 已提交
327
                                unsigned long long int value)
328 329 330 331
{
    char *strval = NULL;
    int rc;

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

335
    rc = virCgroupSetValueStr(group, controller, key, strval);
336 337 338 339 340 341 342

    VIR_FREE(strval);

    return rc;
}


343 344 345 346
#if 0
/* This is included for completeness, but not yet used */

static int virCgroupSetValueI64(virCgroupPtr group,
347
                                int controller,
348
                                const char *key,
D
Daniel P. Berrange 已提交
349
                                long long int value)
350 351 352 353
{
    char *strval = NULL;
    int rc;

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

357
    rc = virCgroupSetValueStr(group, controller, key, strval);
358 359 360 361 362 363 364

    VIR_FREE(strval);

    return rc;
}

static int virCgroupGetValueI64(virCgroupPtr group,
365
                                int controller,
366
                                const char *key,
D
Daniel P. Berrange 已提交
367
                                long long int *value)
368 369 370 371
{
    char *strval = NULL;
    int rc = 0;

372
    rc = virCgroupGetValueStr(group, controller, key, &strval);
373 374 375
    if (rc != 0)
        goto out;

376
    if (virStrToLong_ll(strval, NULL, 10, value) < 0)
377 378 379 380 381 382
        rc = -EINVAL;
out:
    VIR_FREE(strval);

    return rc;
}
383
#endif
384

385
static int virCgroupGetValueU64(virCgroupPtr group,
386
                                int controller,
387
                                const char *key,
D
Daniel P. Berrange 已提交
388
                                unsigned long long int *value)
389 390 391 392
{
    char *strval = NULL;
    int rc = 0;

393
    rc = virCgroupGetValueStr(group, controller, key, &strval);
394 395 396
    if (rc != 0)
        goto out;

D
Daniel P. Berrange 已提交
397
    if (virStrToLong_ull(strval, NULL, 10, value) < 0)
398 399 400 401 402 403 404 405
        rc = -EINVAL;
out:
    VIR_FREE(strval);

    return rc;
}


406
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
407
static int virCgroupCpuSetInherit(virCgroupPtr parent, virCgroupPtr group)
408 409 410 411 412 413 414 415
{
    int i;
    int rc = 0;
    const char *inherit_values[] = {
        "cpuset.cpus",
        "cpuset.mems",
    };

416 417 418
    VIR_DEBUG("Setting up inheritance %s -> %s", parent->path, group->path);
    for (i = 0; i < ARRAY_CARDINALITY(inherit_values) ; i++) {
        char *value;
419

420 421 422 423
        rc = virCgroupGetValueStr(parent,
                                  VIR_CGROUP_CONTROLLER_CPUSET,
                                  inherit_values[i],
                                  &value);
424
        if (rc != 0) {
425 426 427 428 429 430 431 432 433 434
            VIR_ERROR("Failed to get %s %d", inherit_values[i], rc);
            break;
        }

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

        rc = virCgroupSetValueStr(group,
                                  VIR_CGROUP_CONTROLLER_CPUSET,
                                  inherit_values[i],
                                  value);
435
        VIR_FREE(value);
436 437 438

        if (rc != 0) {
            VIR_ERROR("Failed to set %s %d", inherit_values[i], rc);
439 440 441 442 443 444 445
            break;
        }
    }

    return rc;
}

446
static int virCgroupMakeGroup(virCgroupPtr parent, virCgroupPtr group, int create)
447 448 449 450
{
    int i;
    int rc = 0;

451
    VIR_DEBUG("Make group %s", group->path);
452
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
453 454
        char *path = NULL;

455 456
        /* Skip over controllers that aren't mounted */
        if (!group->controllers[i].mountPoint)
457 458
            continue;

459 460 461
        rc = virCgroupPathOfController(group, i, "", &path);
        if (rc < 0)
            return rc;
462

463
        VIR_DEBUG("Make controller %s", path);
464
        if (access(path, F_OK) != 0) {
465 466
            if (!create ||
                mkdir(path, 0755) < 0) {
467 468 469 470
                rc = -errno;
                VIR_FREE(path);
                break;
            }
471 472 473 474
            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);
475 476
                if (rc != 0) {
                    VIR_FREE(path);
477
                    break;
478
                }
479
            }
480 481 482 483 484 485 486 487
        }

        VIR_FREE(path);
    }

    return rc;
}

488 489 490

static int virCgroupNew(const char *path,
                        virCgroupPtr *group)
491 492 493 494
{
    int rc = 0;
    char *typpath = NULL;

495 496
    VIR_DEBUG("New group %s", path);
    *group = NULL;
497

498
    if (VIR_ALLOC((*group)) != 0) {
499 500 501 502
        rc = -ENOMEM;
        goto err;
    }

503
    if (!((*group)->path = strdup(path))) {
504 505 506 507
        rc = -ENOMEM;
        goto err;
    }

508 509 510
    rc = virCgroupDetect(*group);
    if (rc < 0)
        goto err;
511 512 513

    return rc;
err:
514 515
    virCgroupFree(group);
    *group = NULL;
516 517 518 519 520 521

    VIR_FREE(typpath);

    return rc;
}

522
static int virCgroupAppRoot(int privileged,
523 524
                            virCgroupPtr *group,
                            int create)
525
{
526 527
    virCgroupPtr rootgrp = NULL;
    int rc;
528

529 530 531
    rc = virCgroupNew("/", &rootgrp);
    if (rc != 0)
        return rc;
532

533 534 535 536 537
    if (privileged) {
        rc = virCgroupNew("/libvirt", group);
    } else {
        char *rootname;
        char *username;
538
        username = virGetUserName(getuid());
539 540 541 542 543 544 545 546 547 548
        if (!username) {
            rc = -ENOMEM;
            goto cleanup;
        }
        rc = virAsprintf(&rootname, "/libvirt-%s", username);
        VIR_FREE(username);
        if (rc < 0) {
            rc = -ENOMEM;
            goto cleanup;
        }
549

550 551
        rc = virCgroupNew(rootname, group);
        VIR_FREE(rootname);
552 553
    }
    if (rc != 0)
554
        goto cleanup;
555

556
    rc = virCgroupMakeGroup(rootgrp, *group, create);
557

558 559
cleanup:
    virCgroupFree(&rootgrp);
560 561
    return rc;
}
D
Daniel P. Berrange 已提交
562
#endif
563

564 565 566 567 568 569 570 571 572 573 574 575 576
/**
 * virCgroupRemove:
 *
 * @group: The group to be removed
 *
 * Returns: 0 on success
 */
int virCgroupRemove(virCgroupPtr group)
{
    int rc = 0;
    int i;
    char *grppath = NULL;

577
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
578 579
        /* Skip over controllers not mounted */
        if (!group->controllers[i].mountPoint)
580 581
            continue;

582 583 584 585 586
        if (virCgroupPathOfController(group,
                                      i,
                                      NULL,
                                      &grppath) != 0)
            continue;
587

588 589 590 591
        DEBUG("Removing cgroup %s", grppath);
        if (rmdir(grppath) != 0 && errno != ENOENT) {
            rc = -errno;
        }
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
        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;

611
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
612 613 614
        /* Skip over controllers not mounted */
        if (!group->controllers[i].mountPoint)
            continue;
615

616
        rc = virCgroupSetValueU64(group, i, "tasks", (unsigned long long)pid);
617 618 619 620 621 622 623
        if (rc != 0)
            break;
    }

    return rc;
}

624

625
/**
626
 * virCgroupForDriver:
627
 *
628
 * @name: name of this driver (e.g., xen, qemu, lxc)
629 630 631 632
 * @group: Pointer to returned virCgroupPtr
 *
 * Returns 0 on success
 */
633
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
634 635 636 637
int virCgroupForDriver(const char *name,
                       virCgroupPtr *group,
                       int privileged,
                       int create)
638 639
{
    int rc;
640
    char *path = NULL;
641
    virCgroupPtr rootgrp = NULL;
642

643
    rc = virCgroupAppRoot(privileged, &rootgrp, create);
644
    if (rc != 0)
645 646
        goto out;

647 648
    if (virAsprintf(&path, "%s/%s", rootgrp->path, name) < 0) {
        rc = -ENOMEM;
649
        goto out;
650
    }
651

652 653 654
    rc = virCgroupNew(path, group);
    VIR_FREE(path);

655 656
    if (rc == 0) {
        rc = virCgroupMakeGroup(rootgrp, *group, create);
657 658 659
        if (rc != 0)
            virCgroupFree(group);
    }
660

661
out:
662
    virCgroupFree(&rootgrp);
663 664 665

    return rc;
}
D
Daniel P. Berrange 已提交
666 667 668 669 670 671 672 673 674 675
#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
676

677 678 679 680 681 682 683 684 685 686

/**
 * virCgroupForDomain:
 *
 * @driver: group for driver owning the domain
 * @name: name of the domain
 * @group: Pointer to returned virCgroupPtr
 *
 * Returns 0 on success
 */
687
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
688 689 690 691 692 693 694 695
int virCgroupForDomain(virCgroupPtr driver,
                       const char *name,
                       virCgroupPtr *group,
                       int create)
{
    int rc;
    char *path;

696 697 698
    if (driver == NULL)
        return -EINVAL;

699 700 701 702 703 704
    if (virAsprintf(&path, "%s/%s", driver->path, name) < 0)
        return -ENOMEM;

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

705 706
    if (rc == 0) {
        rc = virCgroupMakeGroup(driver, *group, create);
707 708 709 710 711 712
        if (rc != 0)
            virCgroupFree(group);
    }

    return rc;
}
D
Daniel P. Berrange 已提交
713 714 715 716 717 718 719 720 721
#else
int virCgroupForDomain(virCgroupPtr driver ATTRIBUTE_UNUSED,
                       const char *name ATTRIBUTE_UNUSED,
                       virCgroupPtr *group ATTRIBUTE_UNUSED,
                       int create ATTRIBUTE_UNUSED)
{
    return -ENXIO;
}
#endif
722

723 724 725 726 727 728 729 730 731 732 733
/**
 * virCgroupSetMemory:
 *
 * @group: The cgroup to change memory for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
int virCgroupSetMemory(virCgroupPtr group, unsigned long kb)
{
    return virCgroupSetValueU64(group,
734
                                VIR_CGROUP_CONTROLLER_MEMORY,
735 736 737 738
                                "memory.limit_in_bytes",
                                kb << 10);
}

R
Ryota Ozaki 已提交
739 740 741 742 743 744 745 746 747 748
/**
 * 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 已提交
749
    long long unsigned int usage_in_bytes;
R
Ryota Ozaki 已提交
750 751 752 753 754 755 756 757 758
    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;
}

759 760 761 762 763 764 765 766 767 768
/**
 * virCgroupDenyAllDevices:
 *
 * @group: The cgroup to deny devices for
 *
 * Returns: 0 on success
 */
int virCgroupDenyAllDevices(virCgroupPtr group)
{
    return virCgroupSetValueStr(group,
769 770 771
                                VIR_CGROUP_CONTROLLER_DEVICES,
                                "devices.deny",
                                "a");
772 773 774 775 776 777 778 779 780 781 782 783
}

/**
 * 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
 *
 * Returns: 0 on success
 */
784
int virCgroupAllowDevice(virCgroupPtr group, char type, int major, int minor)
785 786 787 788
{
    int rc;
    char *devstr = NULL;

789
    if (virAsprintf(&devstr, "%c %i:%i rwm", type, major, minor) == -1) {
790 791 792 793 794
        rc = -ENOMEM;
        goto out;
    }

    rc = virCgroupSetValueStr(group,
795
                              VIR_CGROUP_CONTROLLER_DEVICES,
796 797 798 799 800 801 802
                              "devices.allow",
                              devstr);
out:
    VIR_FREE(devstr);

    return rc;
}
803

804 805 806 807 808 809 810 811 812
/**
 * 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
 *
 * Returns: 0 on success
 */
813
int virCgroupAllowDeviceMajor(virCgroupPtr group, char type, int major)
814 815 816 817
{
    int rc;
    char *devstr = NULL;

818
    if (virAsprintf(&devstr, "%c %i:* rwm", type, major) == -1) {
819 820 821 822 823
        rc = -ENOMEM;
        goto out;
    }

    rc = virCgroupSetValueStr(group,
824
                              VIR_CGROUP_CONTROLLER_DEVICES,
825 826 827 828 829 830 831 832
                              "devices.allow",
                              devstr);
 out:
    VIR_FREE(devstr);

    return rc;
}

833 834 835 836 837 838 839 840 841 842 843
/**
 * virCgroupAllowDevicePath:
 *
 * @group: The cgroup to allow the device for
 * @path: the device to allow
 *
 * Queries the type of device and its major/minor number, and
 * adds that to the cgroup ACL
 *
 * Returns: 0 on success
 */
D
Daniel P. Berrange 已提交
844
#if defined(major) && defined(minor)
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
int virCgroupAllowDevicePath(virCgroupPtr group, const char *path)
{
    struct stat sb;

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

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

    return virCgroupAllowDevice(group,
                                S_ISCHR(sb.st_mode) ? 'c' : 'b',
                                major(sb.st_rdev),
                                minor(sb.st_rdev));
}
D
Daniel P. Berrange 已提交
860 861 862 863 864 865 866 867
#else
int virCgroupAllowDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED,
                             const char *path ATTRIBUTE_UNUSED)
{
    return -ENOSYS;
}
#endif

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 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

/**
 * 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
 *
 * Returns: 0 on success
 */
int virCgroupDenyDevice(virCgroupPtr group, char type, int major, int minor)
{
    int rc;
    char *devstr = NULL;

    if (virAsprintf(&devstr, "%c %i:%i rwm", type, major, minor) == -1) {
        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
 *
 * Returns: 0 on success
 */
int virCgroupDenyDeviceMajor(virCgroupPtr group, char type, int major)
{
    int rc;
    char *devstr = NULL;

    if (virAsprintf(&devstr, "%c %i:* rwm", type, major) == -1) {
        rc = -ENOMEM;
        goto out;
    }

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

    return rc;
}

D
Daniel P. Berrange 已提交
928
#if defined(major) && defined(minor)
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
int virCgroupDenyDevicePath(virCgroupPtr group, const char *path)
{
    struct stat sb;

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

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

    return virCgroupDenyDevice(group,
                               S_ISCHR(sb.st_mode) ? 'c' : 'b',
                               major(sb.st_rdev),
                               minor(sb.st_rdev));
}
D
Daniel P. Berrange 已提交
944 945 946 947 948 949 950
#else
int virCgroupDenyDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED,
                            const char *path ATTRIBUTE_UNUSED)
{
    return -ENOSYS;
}
#endif
951

952
int virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares)
953
{
954 955
    return virCgroupSetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
D
Daniel P. Berrange 已提交
956
                                "cpu.shares", shares);
957 958
}

959
int virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares)
960
{
961 962
    return virCgroupGetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPU,
D
Daniel P. Berrange 已提交
963
                                "cpu.shares", shares);
964
}
965 966 967

int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
{
968 969
    return virCgroupGetValueU64(group,
                                VIR_CGROUP_CONTROLLER_CPUACCT,
D
Daniel P. Berrange 已提交
970
                                "cpuacct.usage", usage);
971
}
R
Ryota Ozaki 已提交
972 973 974 975 976 977 978 979 980 981

int virCgroupSetFreezerState(virCgroupPtr group, const char *state)
{
    return virCgroupSetValueStr(group,
                                VIR_CGROUP_CONTROLLER_CPU,
                                "freezer.state", state);
}

int virCgroupGetFreezerState(virCgroupPtr group, char **state)
{
982
    return virCgroupGetValueStr(group,
R
Ryota Ozaki 已提交
983 984 985
                                VIR_CGROUP_CONTROLLER_CPU,
                                "freezer.state", state);
}