vircgroup.c 93.6 KB
Newer Older
1
/*
2
 * vircgroup.c: methods for managing control cgroups
3
 *
4
 * Copyright (C) 2010-2015 Red Hat, Inc.
5 6
 * Copyright IBM Corp. 2008
 *
O
Osier Yang 已提交
7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22
 */
#include <config.h>

23
#ifdef __linux__
24
# include <mntent.h>
25
# include <sys/mount.h>
26 27
# include <fcntl.h>
# include <sys/stat.h>
28

29 30 31 32 33
# ifdef MAJOR_IN_MKDEV
#  include <sys/mkdev.h>
# elif MAJOR_IN_SYSMACROS
#  include <sys/sysmacros.h>
# endif
34

35 36 37 38 39
# include <sys/types.h>
# include <signal.h>
# include <dirent.h>
# include <unistd.h>
#endif /* __linux__ */
40

41
#define LIBVIRT_VIRCGROUPPRIV_H_ALLOW
42 43
#include "vircgrouppriv.h"

44
#include "virutil.h"
45
#include "viralloc.h"
46
#include "vircgroupbackend.h"
47
#include "virerror.h"
48
#include "virlog.h"
E
Eric Blake 已提交
49
#include "virfile.h"
50
#include "virhash.h"
51
#include "virhashcode.h"
52
#include "virstring.h"
53
#include "virsystemd.h"
54
#include "virtypedparam.h"
55
#include "virhostcpu.h"
56
#include "virthread.h"
57

58 59
VIR_LOG_INIT("util.cgroup");

60 61
#define VIR_FROM_THIS VIR_FROM_CGROUP

62
#define CGROUP_NB_TOTAL_CPU_STAT_PARAM 3
63
#define CGROUP_NB_PER_CPU_STAT_PARAM   1
64

65 66
VIR_ENUM_IMPL(virCgroupController,
              VIR_CGROUP_CONTROLLER_LAST,
R
Ryota Ozaki 已提交
67
              "cpu", "cpuacct", "cpuset", "memory", "devices",
68
              "freezer", "blkio", "net_cls", "perf_event",
69 70
              "name=systemd",
);
71

E
Eric Blake 已提交
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * virCgroupGetDevicePermsString:
 *
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits
 *
 * Returns string corresponding to the appropriate bits set.
 */
const char *
virCgroupGetDevicePermsString(int perms)
{
    if (perms & VIR_CGROUP_DEVICE_READ) {
        if (perms & VIR_CGROUP_DEVICE_WRITE) {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "rwm";
            else
                return "rw";
        } else {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "rm";
            else
                return "r";
        }
    } else {
        if (perms & VIR_CGROUP_DEVICE_WRITE) {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "wm";
            else
                return "w";
        } else {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "m";
            else
                return "";
        }
    }
}


111
#ifdef __linux__
E
Eric Blake 已提交
112 113
bool
virCgroupAvailable(void)
114
{
115 116
    size_t i;
    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
117

118
    if (!backends)
119 120
        return false;

121 122 123
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (backends[i] && backends[i]->available())
            return true;
124 125
    }

126
    return false;
127 128
}

E
Eric Blake 已提交
129 130 131 132 133 134

static int
virCgroupPartitionNeedsEscaping(const char *path)
{
    FILE *fp = NULL;
    int ret = 0;
135
    g_autofree char *line = NULL;
E
Eric Blake 已提交
136 137 138 139 140 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 182 183 184 185 186 187 188 189 190 191 192 193
    size_t buflen;

    /* If it starts with 'cgroup.' or a '_' of any
     * of the controller names from /proc/cgroups,
     * then we must prefix a '_'
     */
    if (STRPREFIX(path, "cgroup."))
        return 1;

    if (path[0] == '_' ||
        path[0] == '.')
        return 1;

    if (!(fp = fopen("/proc/cgroups", "r"))) {
        /* The API contract is that we return ENXIO
         * if cgroups are not available on a host */
        if (errno == ENOENT)
            errno = ENXIO;
        virReportSystemError(errno, "%s",
                             _("Cannot open /proc/cgroups"));
        return -1;
    }

    /*
     * Data looks like this:
     * #subsys_name hierarchy num_cgroups enabled
     * cpuset  2 4  1
     * cpu     3 48 1
     * cpuacct 3 48 1
     * memory  4 4  1
     * devices 5 4  1
     * freezer 6 4  1
     * net_cls 7 1  1
     */
    while (getline(&line, &buflen, fp) > 0) {
        char *tmp;
        size_t len;

        if (STRPREFIX(line, "#subsys_name"))
            continue;

        tmp = strchrnul(line, ' ');
        *tmp = '\0';
        len = tmp - line;

        if (STRPREFIX(path, line) &&
            path[len] == '.') {
            ret = 1;
            goto cleanup;
        }
    }

    if (ferror(fp)) {
        virReportSystemError(errno, "%s",
                             _("Error while reading /proc/cgroups"));
        goto cleanup;
    }

194
 cleanup:
E
Eric Blake 已提交
195 196 197 198 199
    VIR_FORCE_FCLOSE(fp);
    return ret;
}


200
int
E
Eric Blake 已提交
201 202 203
virCgroupPartitionEscape(char **path)
{
    int rc;
204
    char *newstr = NULL;
E
Eric Blake 已提交
205 206 207 208

    if ((rc = virCgroupPartitionNeedsEscaping(*path)) <= 0)
        return rc;

209
    if (virAsprintf(&newstr, "_%s", *path) < 0)
E
Eric Blake 已提交
210 211
        return -1;

212 213 214
    VIR_FREE(*path);
    *path = newstr;

E
Eric Blake 已提交
215 216
    return 0;
}
E
Eric Blake 已提交
217 218


219 220 221 222
/*
 * Process /proc/mounts figuring out what controllers are
 * mounted and where
 */
223
static int
224
virCgroupDetectMounts(virCgroupPtr group)
225
{
226
    FILE *mounts = NULL;
227 228
    struct mntent entry;
    char buf[CGROUP_MAX_VAL];
229
    int ret = -1;
230
    size_t i;
231

232
    mounts = fopen("/proc/mounts", "r");
233
    if (mounts == NULL) {
234
        virReportSystemError(errno, "%s", _("Unable to open /proc/mounts"));
235
        return -1;
236 237 238
    }

    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
239 240 241 242 243 244 245 246
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->detectMounts(group,
                                                 entry.mnt_type,
                                                 entry.mnt_opts,
                                                 entry.mnt_dir) < 0) {
                goto cleanup;
            }
247
        }
248 249
    }

250 251
    ret = 0;
 cleanup:
252
    VIR_FORCE_FCLOSE(mounts);
253
    return ret;
254 255
}

256 257

/*
258 259 260 261
 * virCgroupDetectPlacement:
 * @group: the group to process
 * @path: the relative path to append, not starting with '/'
 *
262 263
 * Process /proc/self/cgroup figuring out what cgroup
 * sub-path the current process is assigned to. ie not
264 265 266 267 268 269 270 271 272 273 274 275 276 277
 * necessarily in the root. The contents of this file
 * looks like
 *
 * 9:perf_event:/
 * 8:blkio:/
 * 7:net_cls:/
 * 6:freezer:/
 * 5:devices:/
 * 4:memory:/
 * 3:cpuacct,cpu:/
 * 2:cpuset:/
 * 1:name=systemd:/user/berrange/2
 *
 * It then appends @path to each detected path.
278
 */
E
Eric Blake 已提交
279 280 281 282
static int
virCgroupDetectPlacement(virCgroupPtr group,
                         pid_t pid,
                         const char *path)
283
{
284 285
    FILE *mapping  = NULL;
    char line[1024];
286
    int ret = -1;
287
    g_autofree char *procfile = NULL;
288

289
    VIR_DEBUG("Detecting placement for pid %lld path %s",
M
Michal Privoznik 已提交
290
              (long long) pid, path);
291
    if (pid == -1) {
292
        procfile = g_strdup("/proc/self/cgroup");
293
    } else {
M
Michal Privoznik 已提交
294 295
        if (virAsprintf(&procfile, "/proc/%lld/cgroup",
                        (long long) pid) < 0)
296 297 298 299
            goto cleanup;
    }

    mapping = fopen(procfile, "r");
300
    if (mapping == NULL) {
301 302 303 304
        virReportSystemError(errno,
                             _("Unable to open '%s'"),
                             procfile);
        goto cleanup;
305 306
    }

307
    while (fgets(line, sizeof(line), mapping) != NULL) {
308
        size_t i;
309
        char *controllers = strchr(line, ':');
310 311
        char *selfpath = controllers ? strchr(controllers + 1, ':') : NULL;
        char *nl = selfpath ? strchr(selfpath, '\n') : NULL;
312

313
        if (!controllers || !selfpath)
314 315 316 317 318
            continue;

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

319
        *selfpath = '\0';
320
        controllers++;
321
        selfpath++;
322

323 324 325 326 327 328
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->detectPlacement(group, path, controllers,
                                                    selfpath) < 0) {
                goto cleanup;
            }
329 330 331
        }
    }

332
    ret = 0;
333

334
 cleanup:
335
    VIR_FORCE_FCLOSE(mapping);
336
    return ret;
337 338
}

E
Eric Blake 已提交
339

340 341 342 343 344 345 346
static int
virCgroupDetect(virCgroupPtr group,
                pid_t pid,
                int controllers,
                const char *path,
                virCgroupPtr parent)
{
347
    size_t i;
348 349
    bool backendAvailable = false;
    int controllersAvailable = 0;
350
    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
351 352 353 354

    VIR_DEBUG("group=%p controllers=%d path=%s parent=%p",
              group, controllers, path, parent);

355 356 357 358 359
    if (!backends)
        return -1;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (backends[i] && backends[i]->available()) {
360 361
            group->backends[i] = backends[i];
            backendAvailable = true;
362 363 364
        }
    }

365
    if (!backendAvailable) {
366 367 368 369 370
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no cgroup backend available"));
        return -1;
    }

371
    if (parent) {
372 373 374 375 376 377
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->copyMounts(group, parent) < 0) {
                return -1;
            }
        }
378 379 380 381 382
    } else {
        if (virCgroupDetectMounts(group) < 0)
            return -1;
    }

383 384 385
    /* In some cases we can copy part of the placement info
     * based on the parent cgroup...
     */
386 387 388 389 390 391 392 393
    if (parent || path[0] == '/') {
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->copyPlacement(group, path, parent) < 0) {
                return -1;
            }
        }
    }
394 395 396 397

    /* ... but use /proc/cgroups to fill in the rest */
    if (virCgroupDetectPlacement(group, pid, path) < 0)
        return -1;
398

399
    /* Check that for every mounted controller, we found our placement */
400 401 402 403 404 405
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->validatePlacement(group, pid) < 0) {
            return -1;
        }
    }
406

407 408
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i]) {
409
            int rc = group->backends[i]->detectControllers(group, controllers, parent);
410 411 412 413 414 415 416 417 418 419 420 421 422
            if (rc < 0)
                return -1;
            controllersAvailable |= rc;
        }
    }

    /* Check that at least 1 controller is available */
    if (controllersAvailable == 0) {
        virReportSystemError(ENXIO, "%s",
                             _("At least one cgroup controller is required"));
        return -1;
    }

423
    return 0;
424 425
}

426

427
char *
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
virCgroupGetBlockDevString(const char *path)
{
    char *ret = NULL;
    struct stat sb;

    if (stat(path, &sb) < 0) {
        virReportSystemError(errno,
                             _("Path '%s' is not accessible"),
                             path);
        return NULL;
    }

    if (!S_ISBLK(sb.st_mode)) {
        virReportSystemError(EINVAL,
                             _("Path '%s' must be a block device"),
                             path);
        return NULL;
    }

    /* Automatically append space after the string since all callers
     * use it anyway */
    if (virAsprintf(&ret, "%d:%d ", major(sb.st_rdev), minor(sb.st_rdev)) < 0)
        return NULL;

    return ret;
}


456
int
457
virCgroupSetValueRaw(const char *path,
E
Eric Blake 已提交
458
                     const char *value)
459
{
460
    char *tmp;
461

462 463
    VIR_DEBUG("Set value '%s' to '%s'", path, value);
    if (virFileWriteStr(path, value, 0) < 0) {
464
        if (errno == EINVAL &&
465
            (tmp = strrchr(path, '/'))) {
466 467 468
            virReportSystemError(errno,
                                 _("Invalid value '%s' for '%s'"),
                                 value, tmp + 1);
469
            return -1;
470
        }
471
        virReportSystemError(errno,
472
                             _("Unable to write to '%s'"), path);
473
        return -1;
474 475
    }

476
    return 0;
477 478
}

E
Eric Blake 已提交
479

480
int
481
virCgroupGetValueRaw(const char *path,
E
Eric Blake 已提交
482
                     char **value)
483
{
484
    int rc;
485

486
    *value = NULL;
487

488
    VIR_DEBUG("Get value %s", path);
489

490
    if ((rc = virFileReadAll(path, 1024*1024, value)) < 0) {
491
        virReportSystemError(errno,
492
                             _("Unable to read from '%s'"), path);
493
        return -1;
494 495
    }

496 497 498
    /* Terminated with '\n' has sometimes harmful effects to the caller */
    if (rc > 0 && (*value)[rc - 1] == '\n')
        (*value)[rc - 1] = '\0';
499

500
    return 0;
501 502
}

E
Eric Blake 已提交
503

504 505 506 507 508 509
int
virCgroupSetValueStr(virCgroupPtr group,
                     int controller,
                     const char *key,
                     const char *value)
{
510
    g_autofree char *keypath = NULL;
511 512 513 514 515 516 517 518 519 520 521 522 523 524

    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
        return -1;

    return virCgroupSetValueRaw(keypath, value);
}


int
virCgroupGetValueStr(virCgroupPtr group,
                     int controller,
                     const char *key,
                     char **value)
{
525
    g_autofree char *keypath = NULL;
526 527 528 529 530 531 532 533

    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
        return -1;

    return virCgroupGetValueRaw(keypath, value);
}


534
int
535
virCgroupGetValueForBlkDev(const char *str,
536 537 538
                           const char *path,
                           char **value)
{
539
    g_autofree char *prefix = NULL;
540 541
    char **lines = NULL;
    int ret = -1;
542 543

    if (!(prefix = virCgroupGetBlockDevString(path)))
544
        goto error;
545 546

    if (!(lines = virStringSplit(str, "\n", -1)))
547
        goto error;
548

549
    *value = g_strdup(virStringListGetFirstWithPrefix(lines, prefix));
550

551 552 553 554
    ret = 0;
 error:
    virStringListFree(lines);
    return ret;
555 556 557
}


558
int
E
Eric Blake 已提交
559 560 561 562
virCgroupSetValueU64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     unsigned long long int value)
563
{
564
    g_autofree char *strval = NULL;
565

566 567
    if (virAsprintf(&strval, "%llu", value) < 0)
        return -1;
568

569
    return virCgroupSetValueStr(group, controller, key, strval);
570 571 572
}


573
int
E
Eric Blake 已提交
574 575 576 577
virCgroupSetValueI64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     long long int value)
578
{
579
    g_autofree char *strval = NULL;
580

581 582
    if (virAsprintf(&strval, "%lld", value) < 0)
        return -1;
583

584
    return virCgroupSetValueStr(group, controller, key, strval);
585 586
}

E
Eric Blake 已提交
587

588
int
E
Eric Blake 已提交
589 590 591 592
virCgroupGetValueI64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     long long int *value)
593
{
594
    g_autofree char *strval = NULL;
595

596
    if (virCgroupGetValueStr(group, controller, key, &strval) < 0)
597
        return -1;
598

599 600 601 602
    if (virStrToLong_ll(strval, NULL, 10, value) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to parse '%s' as an integer"),
                       strval);
603
        return -1;
604
    }
605

606
    return 0;
607 608
}

E
Eric Blake 已提交
609

610
int
E
Eric Blake 已提交
611 612 613 614
virCgroupGetValueU64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     unsigned long long int *value)
615
{
616
    g_autofree char *strval = NULL;
617

618
    if (virCgroupGetValueStr(group, controller, key, &strval) < 0)
619
        return -1;
620

621 622 623 624
    if (virStrToLong_ull(strval, NULL, 10, value) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to parse '%s' as an integer"),
                       strval);
625
        return -1;
626
    }
627

628
    return 0;
629 630 631
}


E
Eric Blake 已提交
632 633 634 635 636
static int
virCgroupMakeGroup(virCgroupPtr parent,
                   virCgroupPtr group,
                   bool create,
                   unsigned int flags)
637
{
638 639 640 641 642 643 644 645
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->makeGroup(parent, group, create, flags) < 0) {
            virCgroupRemove(group);
            return -1;
        }
646 647
    }

648
    return 0;
649 650
}

651

652 653 654 655 656 657 658 659 660 661 662 663 664 665
/**
 * virCgroupNew:
 * @path: path for the new group
 * @parent: parent group, or NULL
 * @controllers: bitmask of controllers to activate
 *
 * Create a new cgroup storing it in @group.
 *
 * If @path starts with a '/' it is treated as an
 * absolute path, and @parent is ignored. Otherwise
 * it is treated as being relative to @parent. If
 * @parent is NULL, then the placement of the current
 * process is used.
 *
666
 * Returns 0 on success, -1 on error
667
 */
668
int
E
Eric Blake 已提交
669 670 671 672 673
virCgroupNew(pid_t pid,
             const char *path,
             virCgroupPtr parent,
             int controllers,
             virCgroupPtr *group)
674
{
675 676
    VIR_DEBUG("pid=%lld path=%s parent=%p controllers=%d group=%p",
              (long long) pid, path, parent, controllers, group);
677
    *group = NULL;
678

679 680
    if (VIR_ALLOC((*group)) < 0)
        goto error;
681

682
    if (path[0] == '/' || !parent) {
683
        (*group)->path = g_strdup(path);
684 685 686 687
    } else {
        if (virAsprintf(&(*group)->path, "%s%s%s",
                        parent->path,
                        STREQ(parent->path, "") ? "" : "/",
688 689
                        path) < 0)
            goto error;
690 691
    }

692
    if (virCgroupDetect(*group, pid, controllers, path, parent) < 0)
693
        goto error;
694

695 696
    return 0;

697
 error:
698
    virCgroupFree(group);
699
    *group = NULL;
700

701
    return -1;
702
}
703

704

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
static int
virCgroupAddTaskInternal(virCgroupPtr group,
                         pid_t pid,
                         unsigned int flags)
{
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->addTask(group, pid, flags) < 0) {
            return -1;
        }
    }

    return 0;
}


723
/**
724
 * virCgroupAddProcess:
725
 *
726 727
 * @group: The cgroup to add a process to
 * @pid: The pid of the process to add
728
 *
729
 * Will add the process to all controllers, except the
730 731 732 733 734
 * systemd unit controller.
 *
 * Returns: 0 on success, -1 on error
 */
int
735
virCgroupAddProcess(virCgroupPtr group, pid_t pid)
736
{
737
    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_PROCESS);
738 739 740
}

/**
741
 * virCgroupAddMachineProcess:
742
 *
743 744
 * @group: The cgroup to add a process to
 * @pid: The pid of the process to add
745
 *
746
 * Will add the process to all controllers, including the
747 748 749 750 751
 * systemd unit controller.
 *
 * Returns: 0 on success, -1 on error
 */
int
752
virCgroupAddMachineProcess(virCgroupPtr group, pid_t pid)
753
{
754 755 756
    return virCgroupAddTaskInternal(group, pid,
                                    VIR_CGROUP_TASK_PROCESS |
                                    VIR_CGROUP_TASK_SYSTEMD);
757 758
}

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
/**
 * virCgroupAddThread:
 *
 * @group: The cgroup to add a thread to
 * @pid: The pid of the thread to add
 *
 * Will add the thread to all controllers, except the
 * systemd unit controller.
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupAddThread(virCgroupPtr group,
                   pid_t pid)
{
774
    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_THREAD);
775 776
}

E
Eric Blake 已提交
777 778 779

static int
virCgroupSetPartitionSuffix(const char *path, char **res)
780
{
781
    char **tokens;
782
    size_t i;
783
    int ret = -1;
784

785
    if (!(tokens = virStringSplit(path, "/", 0)))
786
        return ret;
787

788
    for (i = 0; tokens[i] != NULL; i++) {
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
        /* Whitelist the 3 top level fixed dirs
         * NB i == 0 is "", since we have leading '/'
         */
        if (i == 1 &&
            (STREQ(tokens[i], "machine") ||
             STREQ(tokens[i], "system") ||
             STREQ(tokens[i], "user"))) {
            continue;
        }
        /* If there is no suffix set already, then
         * add ".partition"
         */
        if (STRNEQ(tokens[i], "") &&
            !strchr(tokens[i], '.')) {
            if (VIR_REALLOC_N(tokens[i],
804
                              strlen(tokens[i]) + strlen(".partition") + 1) < 0)
805
                goto cleanup;
806 807
            strcat(tokens[i], ".partition");
        }
808

809
        if (virCgroupPartitionEscape(&(tokens[i])) < 0)
810
            goto cleanup;
811 812
    }

813
    if (!(*res = virStringListJoin((const char **)tokens, "/")))
814
        goto cleanup;
815

816 817 818 819 820
    ret = 0;

 cleanup:
    virStringListFree(tokens);
    return ret;
821 822
}

E
Eric Blake 已提交
823

824 825 826 827 828 829 830
/**
 * virCgroupNewPartition:
 * @path: path for the partition
 * @create: true to create the cgroup tree
 * @controllers: mask of controllers to create
 *
 * Creates a new cgroup to represent the resource
831
 * partition path identified by @path.
832
 *
833
 * Returns 0 on success, -1 on failure
834
 */
E
Eric Blake 已提交
835 836 837 838 839
int
virCgroupNewPartition(const char *path,
                      bool create,
                      int controllers,
                      virCgroupPtr *group)
840
{
841
    int ret = -1;
842 843
    g_autofree char *parentPath = NULL;
    g_autofree char *newPath = NULL;
844
    virCgroupPtr parent = NULL;
845 846 847
    VIR_DEBUG("path=%s create=%d controllers=%x",
              path, create, controllers);

848 849 850 851 852 853
    if (path[0] != '/') {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Partition path '%s' must start with '/'"),
                       path);
        return -1;
    }
854

855
    if (virCgroupSetPartitionSuffix(path, &newPath) < 0)
856
        goto cleanup;
857

858 859
    if (virCgroupNew(-1, newPath, NULL, controllers, group) < 0)
        goto cleanup;
860

861
    if (STRNEQ(newPath, "/")) {
862
        char *tmp;
863
        parentPath = g_strdup(newPath);
864 865 866 867 868

        tmp = strrchr(parentPath, '/');
        tmp++;
        *tmp = '\0';

869
        if (virCgroupNew(-1, parentPath, NULL, controllers, &parent) < 0)
870
            goto cleanup;
871

872
        if (virCgroupMakeGroup(parent, *group, create, VIR_CGROUP_NONE) < 0)
873
            goto cleanup;
874 875
    }

876 877 878
    ret = 0;
 cleanup:
    if (ret != 0)
879 880
        virCgroupFree(group);
    virCgroupFree(&parent);
881
    return ret;
882 883
}

884

G
Gao feng 已提交
885
/**
886
* virCgroupNewSelf:
G
Gao feng 已提交
887 888 889
*
* @group: Pointer to returned virCgroupPtr
*
890 891 892
* Obtain a cgroup representing the config of the
* current process
*
893
* Returns 0 on success, or -1 on error
G
Gao feng 已提交
894
*/
E
Eric Blake 已提交
895 896
int
virCgroupNewSelf(virCgroupPtr *group)
G
Gao feng 已提交
897
{
898
    return virCgroupNewDetect(-1, -1, group);
G
Gao feng 已提交
899
}
900

901

902 903 904 905 906 907 908 909
/**
 * virCgroupNewDomainPartition:
 *
 * @partition: partition holding the domain
 * @driver: name of the driver
 * @name: name of the domain
 * @group: Pointer to returned virCgroupPtr
 *
910
 * Returns 0 on success, or -1 on error
911
 */
E
Eric Blake 已提交
912 913 914 915 916 917
int
virCgroupNewDomainPartition(virCgroupPtr partition,
                            const char *driver,
                            const char *name,
                            bool create,
                            virCgroupPtr *group)
918
{
919
    g_autofree char *grpname = NULL;
920

921
    if (virAsprintf(&grpname, "%s.libvirt-%s",
922
                    name, driver) < 0)
923
        return -1;
924

925
    if (virCgroupPartitionEscape(&grpname) < 0)
926
        return -1;
927

928
    if (virCgroupNew(-1, grpname, partition, -1, group) < 0)
929
        return -1;
930 931 932 933 934 935 936 937 938 939 940

    /*
     * 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.
     */
E
Eric Blake 已提交
941 942
    if (virCgroupMakeGroup(partition, *group, create,
                           VIR_CGROUP_MEM_HIERACHY) < 0) {
943
        virCgroupFree(group);
944
        return -1;
945 946
    }

947
    return 0;
948
}
949

E
Eric Blake 已提交
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
/**
 * virCgroupNewThread:
 *
 * @domain: group for the domain
 * @name: enum to generate the name for the new thread
 * @id: id of the vcpu or iothread
 * @create: true to create if not already existing
 * @group: Pointer to returned virCgroupPtr
 *
 * Returns 0 on success, or -1 on error
 */
int
virCgroupNewThread(virCgroupPtr domain,
                   virCgroupThreadName nameval,
                   int id,
                   bool create,
                   virCgroupPtr *group)
{
969
    g_autofree char *name = NULL;
970 971 972 973 974
    int controllers;

    switch (nameval) {
    case VIR_CGROUP_THREAD_VCPU:
        if (virAsprintf(&name, "vcpu%d", id) < 0)
975
            return -1;
976 977
        break;
    case VIR_CGROUP_THREAD_EMULATOR:
978
        name = g_strdup("emulator");
979 980 981
        break;
    case VIR_CGROUP_THREAD_IOTHREAD:
        if (virAsprintf(&name, "iothread%d", id) < 0)
982
            return -1;
983 984 985 986
        break;
    case VIR_CGROUP_THREAD_LAST:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected name value %d"), nameval);
987
        return -1;
988 989 990 991 992 993 994
    }

    controllers = ((1 << VIR_CGROUP_CONTROLLER_CPU) |
                   (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
                   (1 << VIR_CGROUP_CONTROLLER_CPUSET));

    if (virCgroupNew(-1, name, domain, controllers, group) < 0)
995
        return -1;
996

997
    if (virCgroupMakeGroup(domain, *group, create, VIR_CGROUP_THREAD) < 0) {
998
        virCgroupFree(group);
999
        return -1;
1000 1001
    }

1002
    return 0;
1003 1004 1005
}


E
Eric Blake 已提交
1006 1007 1008 1009
int
virCgroupNewDetect(pid_t pid,
                   int controllers,
                   virCgroupPtr *group)
1010
{
1011
    return virCgroupNew(pid, "", NULL, controllers, group);
1012 1013
}

E
Eric Blake 已提交
1014

1015 1016 1017
/*
 * Returns 0 on success (but @group may be NULL), -1 on fatal error
 */
E
Eric Blake 已提交
1018 1019 1020 1021 1022
int
virCgroupNewDetectMachine(const char *name,
                          const char *drivername,
                          pid_t pid,
                          int controllers,
1023
                          char *machinename,
E
Eric Blake 已提交
1024
                          virCgroupPtr *group)
1025
{
1026 1027
    size_t i;

1028
    if (virCgroupNewDetect(pid, controllers, group) < 0) {
1029 1030 1031 1032 1033
        if (virCgroupNewIgnoreError())
            return 0;
        return -1;
    }

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if ((*group)->backends[i] &&
            !(*group)->backends[i]->validateMachineGroup(*group, name,
                                                         drivername,
                                                         machinename)) {
            VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'",
                      name, drivername);
            virCgroupFree(group);
            return 0;
        }
1044 1045 1046 1047 1048
    }

    return 0;
}

E
Eric Blake 已提交
1049

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
static int
virCgroupEnableMissingControllers(char *path,
                                  pid_t pidleader,
                                  int controllers,
                                  virCgroupPtr *group)
{
    virCgroupPtr parent = NULL;
    char *offset = path;
    int ret = -1;

    if (virCgroupNew(pidleader,
1061
                     "/",
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
                     NULL,
                     controllers,
                     &parent) < 0)
        return ret;

    for (;;) {
        virCgroupPtr tmp;
        char *t = strchr(offset + 1, '/');
        if (t)
            *t = '\0';

        if (virCgroupNew(pidleader,
                         path,
                         parent,
                         controllers,
                         &tmp) < 0)
            goto cleanup;

1080
        if (virCgroupMakeGroup(parent, tmp, true, VIR_CGROUP_SYSTEMD) < 0) {
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
            virCgroupFree(&tmp);
            goto cleanup;
        }
        if (t) {
            *t = '/';
            offset = t;
            virCgroupFree(&parent);
            parent = tmp;
        } else {
            *group = tmp;
            break;
        }
    }

    ret = 0;
 cleanup:
    virCgroupFree(&parent);
    return ret;
}


1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
/*
 * Returns 0 on success, -1 on fatal error, -2 on systemd not available
 */
static int
virCgroupNewMachineSystemd(const char *name,
                           const char *drivername,
                           const unsigned char *uuid,
                           const char *rootdir,
                           pid_t pidleader,
                           bool isContainer,
1112 1113
                           size_t nnicindexes,
                           int *nicindexes,
1114 1115
                           const char *partition,
                           int controllers,
1116
                           unsigned int maxthreads,
1117
                           virCgroupPtr *group)
1118
{
1119
    int rv;
1120
    virCgroupPtr init;
1121
    g_autofree char *path = NULL;
1122
    size_t i;
1123 1124 1125 1126 1127 1128 1129 1130

    VIR_DEBUG("Trying to setup machine '%s' via systemd", name);
    if ((rv = virSystemdCreateMachine(name,
                                      drivername,
                                      uuid,
                                      rootdir,
                                      pidleader,
                                      isContainer,
1131 1132
                                      nnicindexes,
                                      nicindexes,
1133 1134
                                      partition,
                                      maxthreads)) < 0)
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
        return rv;

    if (controllers != -1)
        controllers |= (1 << VIR_CGROUP_CONTROLLER_SYSTEMD);

    VIR_DEBUG("Detecting systemd placement");
    if (virCgroupNewDetect(pidleader,
                           controllers,
                           &init) < 0)
        return -1;
1145

1146 1147 1148 1149 1150 1151
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (init->backends[i] &&
            (path = init->backends[i]->stealPlacement(init))) {
            break;
        }
    }
1152
    virCgroupFree(&init);
1153 1154

    if (!path || STREQ(path, "/") || path[0] != '/') {
1155 1156
        VIR_DEBUG("Systemd didn't setup its controller, path=%s",
                  NULLSTR(path));
1157
        return -2;
1158 1159
    }

1160 1161
    if (virCgroupEnableMissingControllers(path, pidleader,
                                          controllers, group) < 0) {
1162
        return -1;
1163 1164
    }

1165
    if (virCgroupAddProcess(*group, pidleader) < 0) {
1166 1167 1168
        virErrorPtr saved;

        virErrorPreserveLast(&saved);
1169 1170
        virCgroupRemove(*group);
        virCgroupFree(group);
1171
        virErrorRestore(&saved);
1172 1173
    }

1174
    return 0;
1175
}
1176

E
Eric Blake 已提交
1177

1178 1179 1180
/*
 * Returns 0 on success, -1 on fatal error
 */
1181
int virCgroupTerminateMachine(const char *name)
1182
{
1183
    return virSystemdTerminateMachine(name);
1184 1185 1186
}


1187 1188 1189
static int
virCgroupNewMachineManual(const char *name,
                          const char *drivername,
1190
                          pid_t pidleader,
1191 1192 1193 1194
                          const char *partition,
                          int controllers,
                          virCgroupPtr *group)
{
1195 1196
    virCgroupPtr parent = NULL;
    int ret = -1;
1197 1198

    VIR_DEBUG("Fallback to non-systemd setup");
1199 1200 1201 1202 1203
    if (virCgroupNewPartition(partition,
                              STREQ(partition, "/machine"),
                              controllers,
                              &parent) < 0) {
        if (virCgroupNewIgnoreError())
1204
            goto done;
1205

1206
        goto cleanup;
1207 1208 1209 1210 1211 1212 1213
    }

    if (virCgroupNewDomainPartition(parent,
                                    drivername,
                                    name,
                                    true,
                                    group) < 0)
1214
        goto cleanup;
1215

1216
    if (virCgroupAddProcess(*group, pidleader) < 0) {
1217 1218 1219
        virErrorPtr saved;

        virErrorPreserveLast(&saved);
1220
        virCgroupRemove(*group);
1221
        virCgroupFree(group);
1222
        virErrorRestore(&saved);
1223 1224
    }

1225 1226 1227 1228
 done:
    ret = 0;

 cleanup:
1229
    virCgroupFree(&parent);
1230
    return ret;
1231 1232
}

E
Eric Blake 已提交
1233 1234 1235 1236 1237 1238 1239 1240

int
virCgroupNewMachine(const char *name,
                    const char *drivername,
                    const unsigned char *uuid,
                    const char *rootdir,
                    pid_t pidleader,
                    bool isContainer,
1241 1242
                    size_t nnicindexes,
                    int *nicindexes,
E
Eric Blake 已提交
1243 1244
                    const char *partition,
                    int controllers,
1245
                    unsigned int maxthreads,
E
Eric Blake 已提交
1246
                    virCgroupPtr *group)
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
{
    int rv;

    *group = NULL;

    if ((rv = virCgroupNewMachineSystemd(name,
                                         drivername,
                                         uuid,
                                         rootdir,
                                         pidleader,
                                         isContainer,
1258 1259
                                         nnicindexes,
                                         nicindexes,
1260 1261
                                         partition,
                                         controllers,
1262
                                         maxthreads,
1263 1264 1265 1266 1267 1268 1269 1270
                                         group)) == 0)
        return 0;

    if (rv == -1)
        return -1;

    return virCgroupNewMachineManual(name,
                                     drivername,
1271
                                     pidleader,
1272 1273 1274 1275 1276
                                     partition,
                                     controllers,
                                     group);
}

E
Eric Blake 已提交
1277 1278 1279

bool
virCgroupNewIgnoreError(void)
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
{
    if (virLastErrorIsSystemErrno(ENXIO) ||
        virLastErrorIsSystemErrno(EPERM) ||
        virLastErrorIsSystemErrno(EACCES)) {
        virResetLastError();
        VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
        return true;
    }
    return false;
}

E
Eric Blake 已提交
1291

E
Eric Blake 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
/**
 * virCgroupHasController: query whether a cgroup controller is present
 *
 * @cgroup: The group structure to be queried, or NULL
 * @controller: cgroup subsystem id
 *
 * Returns true if a cgroup controller is mounted and is associated
 * with this cgroup object.
 */
bool
virCgroupHasController(virCgroupPtr cgroup, int controller)
{
1304 1305
    size_t i;

E
Eric Blake 已提交
1306 1307 1308 1309
    if (!cgroup)
        return false;
    if (controller < 0 || controller >= VIR_CGROUP_CONTROLLER_LAST)
        return false;
1310

1311 1312 1313 1314 1315 1316 1317 1318
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (cgroup->backends[i] &&
            cgroup->backends[i]->hasController(cgroup, controller)) {
            return true;
        }
    }

    return false;
E
Eric Blake 已提交
1319 1320 1321 1322 1323
}


int
virCgroupPathOfController(virCgroupPtr group,
1324
                          unsigned int controller,
E
Eric Blake 已提交
1325 1326 1327
                          const char *key,
                          char **path)
{
1328 1329 1330
    if (controller >= VIR_CGROUP_CONTROLLER_LAST) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid controller id '%d'"), controller);
E
Eric Blake 已提交
1331 1332 1333
        return -1;
    }

1334 1335
    VIR_CGROUP_BACKEND_CALL(group, controller, pathOfController, -1,
                            controller, key, path);
E
Eric Blake 已提交
1336 1337 1338
}


1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
/**
 * virCgroupGetBlkioIoServiced:
 *
 * @group: The cgroup to get throughput for
 * @bytes_read: Pointer to returned bytes read
 * @bytes_write: Pointer to returned bytes written
 * @requests_read: Pointer to returned read io ops
 * @requests_write: Pointer to returned write io ops
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioIoServiced(virCgroupPtr group,
                            long long *bytes_read,
                            long long *bytes_write,
                            long long *requests_read,
                            long long *requests_write)
{
1357 1358
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioIoServiced, -1,
1359 1360
                            bytes_read, bytes_write,
                            requests_read, requests_write);
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
}


/**
 * virCgroupGetBlkioIoDeviceServiced:
 *
 * @group: The cgroup to get throughput for
 * @path: The device to get throughput for
 * @bytes_read: Pointer to returned bytes read
 * @bytes_write: Pointer to returned bytes written
 * @requests_read: Pointer to returned read io ops
 * @requests_write: Pointer to returned write io ops
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
                                  const char *path,
                                  long long *bytes_read,
                                  long long *bytes_write,
                                  long long *requests_read,
                                  long long *requests_write)
{
1384 1385
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioIoDeviceServiced, -1,
1386 1387
                            path, bytes_read, bytes_write,
                            requests_read, requests_write);
1388 1389 1390
}


1391 1392 1393 1394 1395 1396
/**
 * virCgroupSetBlkioWeight:
 *
 * @group: The cgroup to change io weight for
 * @weight: The Weight for this cgroup
 *
1397
 * Returns: 0 on success, -1 on error
1398
 */
E
Eric Blake 已提交
1399 1400
int
virCgroupSetBlkioWeight(virCgroupPtr group, unsigned int weight)
1401
{
1402 1403
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioWeight, -1, weight);
1404 1405
}

E
Eric Blake 已提交
1406

1407 1408 1409 1410 1411 1412
/**
 * virCgroupGetBlkioWeight:
 *
 * @group: The cgroup to get weight for
 * @Weight: Pointer to returned weight
 *
1413
 * Returns: 0 on success, -1 on error
1414
 */
E
Eric Blake 已提交
1415 1416
int
virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight)
1417
{
1418 1419
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioWeight, -1, weight);
1420 1421
}

1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
/**
 * virCgroupSetBlkioDeviceReadIops:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @riops: The new device read iops throttle, or 0 to clear
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupSetBlkioDeviceReadIops(virCgroupPtr group,
                                const char *path,
                                unsigned int riops)
{
1435 1436
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceReadIops, -1, path, riops);
1437 1438
}

E
Eric Blake 已提交
1439

1440
/**
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
 * virCgroupSetBlkioDeviceWriteIops:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @wiops: The new device write iops throttle, or 0 to clear
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupSetBlkioDeviceWriteIops(virCgroupPtr group,
                                 const char *path,
                                 unsigned int wiops)
{
1453 1454
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceWriteIops, -1, path, wiops);
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
}


/**
 * virCgroupSetBlkioDeviceReadBps:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @rbps: The new device read bps throttle, or 0 to clear
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupSetBlkioDeviceReadBps(virCgroupPtr group,
                               const char *path,
                               unsigned long long rbps)
{
1471 1472
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceReadBps, -1, path, rbps);
1473 1474 1475 1476 1477 1478 1479
}

/**
 * virCgroupSetBlkioDeviceWriteBps:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @wbps: The new device write bps throttle, or 0 to clear
1480
 *
1481 1482 1483 1484 1485 1486 1487
 * Returns: 0 on success, -1 on error
 */
int
virCgroupSetBlkioDeviceWriteBps(virCgroupPtr group,
                                const char *path,
                                unsigned long long wbps)
{
1488 1489
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceWriteBps, -1, path, wbps);
1490 1491 1492 1493 1494 1495 1496
}


/**
 * virCgroupSetBlkioDeviceWeight:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
1497 1498
 * @weight: The new device weight (100-1000),
 * (10-1000) after kernel 2.6.39, or 0 to clear
1499
 *
1500
 * Returns: 0 on success, -1 on error
1501
 */
E
Eric Blake 已提交
1502 1503 1504 1505
int
virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
                              const char *path,
                              unsigned int weight)
1506
{
1507 1508
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceWeight, -1, path, weight);
1509
}
1510

1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
/**
 * virCgroupGetBlkioDeviceReadIops:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @riops: Returned device read iops throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioDeviceReadIops(virCgroupPtr group,
                                const char *path,
                                unsigned int *riops)
{
1524 1525
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceReadIops, -1, path, riops);
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
}

/**
 * virCgroupGetBlkioDeviceWriteIops:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @wiops: Returned device write iops throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioDeviceWriteIops(virCgroupPtr group,
                                 const char *path,
                                 unsigned int *wiops)
{
1541 1542
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceWriteIops, -1, path, wiops);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
}

/**
 * virCgroupGetBlkioDeviceReadBps:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @rbps: Returned device read bps throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioDeviceReadBps(virCgroupPtr group,
                               const char *path,
                               unsigned long long *rbps)
{
1558 1559
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceReadBps, -1, path, rbps);
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
}

/**
 * virCgroupGetBlkioDeviceWriteBps:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @wbps: Returned device write bps throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioDeviceWriteBps(virCgroupPtr group,
                                const char *path,
                                unsigned long long *wbps)
{
1575 1576
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceWriteBps, -1, path, wbps);
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
}

/**
 * virCgroupGetBlkioDeviceWeight:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @weight: Returned device weight, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioDeviceWeight(virCgroupPtr group,
                              const char *path,
                              unsigned int *weight)
{
1592 1593
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceWeight, -1, path, weight);
1594 1595
}

1596

1597 1598 1599 1600 1601 1602 1603 1604
/**
 * virCgroupSetMemory:
 *
 * @group: The cgroup to change memory for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1605 1606
int
virCgroupSetMemory(virCgroupPtr group, unsigned long long kb)
1607
{
1608 1609
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemory, -1, kb);
1610 1611
}

E
Eric Blake 已提交
1612

1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
/**
 * virCgroupGetMemoryStat:
 *
 * @group: The cgroup to change memory for
 * @cache: page cache memory in KiB
 * @activeAnon: anonymous and swap cache memory in KiB
 * @inactiveAnon: anonymous and swap cache memory in KiB
 * @activeFile: file-backed memory in KiB
 * @inactiveFile: file-backed memory in KiB
 * @unevictable: memory that cannot be reclaimed KiB
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetMemoryStat(virCgroupPtr group,
                       unsigned long long *cache,
                       unsigned long long *activeAnon,
                       unsigned long long *inactiveAnon,
                       unsigned long long *activeFile,
                       unsigned long long *inactiveFile,
                       unsigned long long *unevictable)
{
1635 1636
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemoryStat, -1, cache,
1637 1638 1639
                            activeAnon, inactiveAnon,
                            activeFile, inactiveFile,
                            unevictable);
1640 1641 1642
}


R
Ryota Ozaki 已提交
1643 1644 1645 1646 1647 1648 1649 1650
/**
 * virCgroupGetMemoryUsage:
 *
 * @group: The cgroup to change memory for
 * @kb: Pointer to returned used memory in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1651 1652
int
virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb)
R
Ryota Ozaki 已提交
1653
{
1654 1655
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemoryUsage, -1, kb);
R
Ryota Ozaki 已提交
1656 1657
}

E
Eric Blake 已提交
1658

1659 1660 1661 1662 1663 1664 1665 1666
/**
 * virCgroupSetMemoryHardLimit:
 *
 * @group: The cgroup to change memory hard limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1667 1668
int
virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb)
1669
{
1670 1671
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemoryHardLimit, -1, kb);
1672 1673
}

E
Eric Blake 已提交
1674

1675 1676 1677 1678 1679 1680 1681 1682
/**
 * virCgroupGetMemoryHardLimit:
 *
 * @group: The cgroup to get the memory hard limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1683 1684
int
virCgroupGetMemoryHardLimit(virCgroupPtr group, unsigned long long *kb)
1685
{
1686 1687
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemoryHardLimit, -1, kb);
1688 1689
}

E
Eric Blake 已提交
1690

1691 1692 1693 1694 1695 1696 1697 1698
/**
 * virCgroupSetMemorySoftLimit:
 *
 * @group: The cgroup to change memory soft limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1699 1700
int
virCgroupSetMemorySoftLimit(virCgroupPtr group, unsigned long long kb)
1701
{
1702 1703
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemorySoftLimit, -1, kb);
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
}


/**
 * virCgroupGetMemorySoftLimit:
 *
 * @group: The cgroup to get the memory soft limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1715 1716
int
virCgroupGetMemorySoftLimit(virCgroupPtr group, unsigned long long *kb)
1717
{
1718 1719
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemorySoftLimit, -1, kb);
1720 1721
}

E
Eric Blake 已提交
1722

1723
/**
1724
 * virCgroupSetMemSwapHardLimit:
1725
 *
1726 1727
 * @group: The cgroup to change mem+swap hard limit for
 * @kb: The mem+swap amount in kilobytes
1728 1729 1730
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1731 1732
int
virCgroupSetMemSwapHardLimit(virCgroupPtr group, unsigned long long kb)
1733
{
1734 1735
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemSwapHardLimit, -1, kb);
1736 1737
}

E
Eric Blake 已提交
1738

1739
/**
1740
 * virCgroupGetMemSwapHardLimit:
1741
 *
1742 1743
 * @group: The cgroup to get mem+swap hard limit for
 * @kb: The mem+swap amount in kilobytes
1744 1745 1746
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1747 1748
int
virCgroupGetMemSwapHardLimit(virCgroupPtr group, unsigned long long *kb)
1749
{
1750 1751
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemSwapHardLimit, -1, kb);
1752 1753
}

E
Eric Blake 已提交
1754

G
Gao feng 已提交
1755 1756 1757 1758 1759 1760 1761 1762
/**
 * virCgroupGetMemSwapUsage:
 *
 * @group: The cgroup to get mem+swap usage for
 * @kb: The mem+swap amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1763 1764
int
virCgroupGetMemSwapUsage(virCgroupPtr group, unsigned long long *kb)
G
Gao feng 已提交
1765
{
1766 1767
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemSwapUsage, -1, kb);
G
Gao feng 已提交
1768 1769
}

E
Eric Blake 已提交
1770

1771 1772 1773 1774 1775 1776 1777 1778
/**
 * virCgroupSetCpusetMems:
 *
 * @group: The cgroup to set cpuset.mems for
 * @mems: the numa nodes to set
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1779 1780
int
virCgroupSetCpusetMems(virCgroupPtr group, const char *mems)
1781
{
1782 1783
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            setCpusetMems, -1, mems);
1784 1785
}

E
Eric Blake 已提交
1786

1787 1788 1789 1790 1791 1792 1793 1794
/**
 * virCgroupGetCpusetMems:
 *
 * @group: The cgroup to get cpuset.mems for
 * @mems: the numa nodes to get
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1795 1796
int
virCgroupGetCpusetMems(virCgroupPtr group, char **mems)
1797
{
1798 1799
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            getCpusetMems, -1, mems);
1800 1801
}

E
Eric Blake 已提交
1802

1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
/**
 * virCgroupSetCpusetMemoryMigrate:
 *
 * @group: The cgroup to set cpuset.memory_migrate for
 * @migrate: Whether to migrate the memory on change or not
 *
 * Returns: 0 on success
 */
int
virCgroupSetCpusetMemoryMigrate(virCgroupPtr group, bool migrate)
{
1814 1815
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            setCpusetMemoryMigrate, -1, migrate);
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
}


/**
 * virCgroupGetCpusetMemoryMigrate:
 *
 * @group: The cgroup to get cpuset.memory_migrate for
 * @migrate: Migration setting
 *
 * Returns: 0 on success
 */
int
virCgroupGetCpusetMemoryMigrate(virCgroupPtr group, bool *migrate)
{
1830 1831
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            getCpusetMemoryMigrate, -1, migrate);
1832 1833 1834
}


1835 1836 1837 1838 1839 1840
/**
 * virCgroupSetCpusetCpus:
 *
 * @group: The cgroup to set cpuset.cpus for
 * @cpus: the cpus to set
 *
N
Nitesh Konkar 已提交
1841
 * Returns: 0 on success
1842
 */
E
Eric Blake 已提交
1843 1844
int
virCgroupSetCpusetCpus(virCgroupPtr group, const char *cpus)
1845
{
1846 1847
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            setCpusetCpus, -1, cpus);
1848 1849
}

E
Eric Blake 已提交
1850

1851 1852 1853 1854 1855 1856
/**
 * virCgroupGetCpusetCpus:
 *
 * @group: The cgroup to get cpuset.cpus for
 * @cpus: the cpus to get
 *
N
Nitesh Konkar 已提交
1857
 * Returns: 0 on success
1858
 */
E
Eric Blake 已提交
1859 1860
int
virCgroupGetCpusetCpus(virCgroupPtr group, char **cpus)
1861
{
1862 1863
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            getCpusetCpus, -1, cpus);
1864 1865
}

E
Eric Blake 已提交
1866

1867 1868 1869
/**
 * virCgroupDenyAllDevices:
 *
1870
 * @group: The cgroup to deny all permissions, for all devices
1871 1872 1873
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1874 1875
int
virCgroupDenyAllDevices(virCgroupPtr group)
1876
{
1877 1878
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            denyAllDevices, -1);
1879 1880
}

1881 1882 1883
/**
 * virCgroupAllowAllDevices:
 *
1884
 * Allows the permission for all devices by setting lines similar
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
 * to these ones (obviously the 'm' permission is an example):
 *
 * 'b *:* m'
 * 'c *:* m'
 *
 * @group: The cgroup to allow devices for
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
 *
 * Returns: 0 on success
 */
int
virCgroupAllowAllDevices(virCgroupPtr group, int perms)
{
1898 1899
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            allowAllDevices, -1, perms);
1900 1901
}

E
Eric Blake 已提交
1902

1903 1904 1905 1906 1907
/**
 * virCgroupAllowDevice:
 *
 * @group: The cgroup to allow a device for
 * @type: The device type (i.e., 'c' or 'b')
1908 1909
 * @major: The major number of the device, a negative value means '*'
 * @minor: The minor number of the device, a negative value means '*'
1910
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
1911 1912 1913
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1914 1915 1916
int
virCgroupAllowDevice(virCgroupPtr group, char type, int major, int minor,
                     int perms)
1917
{
1918 1919
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            allowDevice, -1, type, major, minor, perms);
1920
}
1921

E
Eric Blake 已提交
1922

1923 1924 1925 1926 1927
/**
 * virCgroupAllowDevicePath:
 *
 * @group: The cgroup to allow the device for
 * @path: the device to allow
1928
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
1929
 * @ignoreEacces: Ignore lack of permission (mostly for NFS mounts)
1930 1931 1932 1933
 *
 * Queries the type of device and its major/minor number, and
 * adds that to the cgroup ACL
 *
1934
 * Returns: 0 on success, 1 if path exists but is not a device or is not
1935
 * accessible, or * -1 on error
1936
 */
E
Eric Blake 已提交
1937
int
1938 1939 1940 1941
virCgroupAllowDevicePath(virCgroupPtr group,
                         const char *path,
                         int perms,
                         bool ignoreEacces)
1942 1943 1944
{
    struct stat sb;

1945
    if (stat(path, &sb) < 0) {
1946 1947 1948
        if (errno == EACCES && ignoreEacces)
            return 1;

1949 1950 1951 1952 1953
        virReportSystemError(errno,
                             _("Path '%s' is not accessible"),
                             path);
        return -1;
    }
1954 1955

    if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
1956
        return 1;
1957

1958 1959
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            allowDevice, -1,
1960 1961 1962 1963
                            S_ISCHR(sb.st_mode) ? 'c' : 'b',
                            major(sb.st_rdev),
                            minor(sb.st_rdev),
                            perms);
1964
}
D
Daniel P. Berrange 已提交
1965

1966 1967 1968 1969 1970 1971

/**
 * virCgroupDenyDevice:
 *
 * @group: The cgroup to deny a device for
 * @type: The device type (i.e., 'c' or 'b')
1972 1973
 * @major: The major number of the device, a negative value means '*'
 * @minor: The minor number of the device, a negative value means '*'
1974
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to deny
1975 1976 1977
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1978 1979 1980
int
virCgroupDenyDevice(virCgroupPtr group, char type, int major, int minor,
                    int perms)
1981
{
1982 1983
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            denyDevice, -1, type, major, minor, perms);
1984 1985
}

E
Eric Blake 已提交
1986

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
/**
 * virCgroupDenyDevicePath:
 *
 * @group: The cgroup to deny the device for
 * @path: the device to deny
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
 * @ignoreEacces: Ignore lack of permission (mostly for NFS mounts)
 *
 * Queries the type of device and its major/minor number, and
 * removes it from the cgroup ACL
 *
 * Returns: 0 on success, 1 if path exists but is not a device or is not
 * accessible, or -1 on error.
 */
E
Eric Blake 已提交
2001
int
2002 2003 2004 2005
virCgroupDenyDevicePath(virCgroupPtr group,
                        const char *path,
                        int perms,
                        bool ignoreEacces)
2006 2007 2008
{
    struct stat sb;

2009
    if (stat(path, &sb) < 0) {
2010 2011 2012
        if (errno == EACCES && ignoreEacces)
            return 1;

2013 2014 2015 2016 2017
        virReportSystemError(errno,
                             _("Path '%s' is not accessible"),
                             path);
        return -1;
    }
2018 2019

    if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
2020
        return 1;
2021

2022 2023
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            denyDevice, -1,
2024 2025 2026 2027
                            S_ISCHR(sb.st_mode) ? 'c' : 'b',
                            major(sb.st_rdev),
                            minor(sb.st_rdev),
                            perms);
2028 2029
}

E
Eric Blake 已提交
2030

2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
/* This function gets the sums of cpu time consumed by all vcpus.
 * For example, if there are 4 physical cpus, and 2 vcpus in a domain,
 * then for each vcpu, the cpuacct.usage_percpu looks like this:
 *   t0 t1 t2 t3
 * and we have 2 groups of such data:
 *   v\p   0   1   2   3
 *   0   t00 t01 t02 t03
 *   1   t10 t11 t12 t13
 * for each pcpu, the sum is cpu time consumed by all vcpus.
 *   s0 = t00 + t10
 *   s1 = t01 + t11
 *   s2 = t02 + t12
 *   s3 = t03 + t13
 */
static int
virCgroupGetPercpuVcpuSum(virCgroupPtr group,
2047
                          virBitmapPtr guestvcpus,
2048
                          unsigned long long *sum_cpu_time,
2049 2050
                          size_t nsum,
                          virBitmapPtr cpumap)
2051
{
2052
    int ret = -1;
2053
    ssize_t i = -1;
2054
    virCgroupPtr group_vcpu = NULL;
2055

2056
    while ((i = virBitmapNextSetBit(guestvcpus, i)) >= 0) {
2057
        g_autofree char *buf = NULL;
2058 2059
        char *pos;
        unsigned long long tmp;
2060
        ssize_t j;
2061

J
John Ferlan 已提交
2062 2063
        if (virCgroupNewThread(group, VIR_CGROUP_THREAD_VCPU, i,
                               false, &group_vcpu) < 0)
2064
            goto cleanup;
2065 2066

        if (virCgroupGetCpuacctPercpuUsage(group_vcpu, &buf) < 0)
2067
            goto cleanup;
2068 2069

        pos = buf;
2070 2071 2072
        for (j = virBitmapNextSetBit(cpumap, -1);
             j >= 0 && j < nsum;
             j = virBitmapNextSetBit(cpumap, j)) {
2073 2074 2075
            if (virStrToLong_ull(pos, &pos, 10, &tmp) < 0) {
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("cpuacct parse error"));
2076
                goto cleanup;
2077 2078 2079
            }
            sum_cpu_time[j] += tmp;
        }
2080

2081
        virCgroupFree(&group_vcpu);
2082 2083
    }

2084 2085
    ret = 0;
 cleanup:
2086
    virCgroupFree(&group_vcpu);
2087
    return ret;
2088 2089 2090
}


2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
/**
 * virCgroupGetPercpuStats:
 * @cgroup: cgroup data structure
 * @params: typed parameter array where data is returned
 * @nparams: cardinality of @params
 * @start_cpu: offset of physical CPU to get data for
 * @ncpus: number of physical CPUs to get data for
 * @nvcpupids: number of vCPU threads for a domain (actual number of vcpus)
 *
 * This function is the worker that retrieves data in the appropriate format
 * for the terribly designed 'virDomainGetCPUStats' API. Sharing semantics with
 * the API, this function has two modes of operation depending on magic settings
 * of the input arguments. Please refer to docs of 'virDomainGetCPUStats' for
 * the usage patterns of the similarly named arguments.
 *
 * @nvcpupids determines the count of active vcpu threads for the vm. If the
 * threads could not be detected the percpu data is skipped.
 *
 * Please DON'T use this function anywhere else.
 */
2111 2112 2113 2114 2115
int
virCgroupGetPercpuStats(virCgroupPtr group,
                        virTypedParameterPtr params,
                        unsigned int nparams,
                        int start_cpu,
2116
                        unsigned int ncpus,
2117
                        virBitmapPtr guestvcpus)
2118
{
2119
    int ret = -1;
2120
    size_t i;
2121
    int need_cpus, total_cpus;
2122
    char *pos;
2123 2124
    g_autofree char *buf = NULL;
    g_autofree unsigned long long *sum_cpu_time = NULL;
2125 2126 2127
    virTypedParameterPtr ent;
    int param_idx;
    unsigned long long cpu_time;
2128
    virBitmapPtr cpumap = NULL;
2129 2130

    /* return the number of supported params */
2131
    if (nparams == 0 && ncpus != 0) {
2132
        if (!guestvcpus)
2133 2134 2135 2136
            return CGROUP_NB_PER_CPU_STAT_PARAM;
        else
            return CGROUP_NB_PER_CPU_STAT_PARAM + 1;
    }
2137 2138

    /* To parse account file, we need to know how many cpus are present.  */
2139
    if (!(cpumap = virHostCPUGetPresentBitmap()))
2140
        return -1;
2141

2142 2143
    total_cpus = virBitmapSize(cpumap);

2144
    /* return total number of cpus */
2145 2146 2147 2148
    if (ncpus == 0) {
        ret = total_cpus;
        goto cleanup;
    }
2149

2150
    if (start_cpu >= total_cpus) {
2151 2152
        virReportError(VIR_ERR_INVALID_ARG,
                       _("start_cpu %d larger than maximum of %d"),
2153
                       start_cpu, total_cpus - 1);
2154
        goto cleanup;
2155 2156 2157 2158
    }

    /* we get percpu cputime accounting info. */
    if (virCgroupGetCpuacctPercpuUsage(group, &buf))
2159
        goto cleanup;
2160 2161 2162 2163 2164 2165
    pos = buf;

    /* return percpu cputime in index 0 */
    param_idx = 0;

    /* number of cpus to compute */
J
Ján Tomko 已提交
2166
    need_cpus = MIN(total_cpus, start_cpu + ncpus);
2167

J
Ján Tomko 已提交
2168
    for (i = 0; i < need_cpus; i++) {
J
Ján Tomko 已提交
2169
        if (!virBitmapIsBitSet(cpumap, i)) {
2170 2171
            cpu_time = 0;
        } else if (virStrToLong_ull(pos, &pos, 10, &cpu_time) < 0) {
2172 2173
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("cpuacct parse error"));
2174
            goto cleanup;
2175 2176 2177 2178 2179 2180
        }
        if (i < start_cpu)
            continue;
        ent = &params[(i - start_cpu) * nparams + param_idx];
        if (virTypedParameterAssign(ent, VIR_DOMAIN_CPU_STATS_CPUTIME,
                                    VIR_TYPED_PARAM_ULLONG, cpu_time) < 0)
2181
            goto cleanup;
2182 2183
    }

2184
    /* return percpu vcputime in index 1 */
2185
    param_idx = 1;
2186

2187
    if (guestvcpus && param_idx < nparams) {
2188
        if (VIR_ALLOC_N(sum_cpu_time, need_cpus) < 0)
2189
            goto cleanup;
2190 2191
        if (virCgroupGetPercpuVcpuSum(group, guestvcpus, sum_cpu_time,
                                      need_cpus, cpumap) < 0)
2192
            goto cleanup;
2193 2194

        for (i = start_cpu; i < need_cpus; i++) {
2195 2196
            int idx = (i - start_cpu) * nparams + param_idx;
            if (virTypedParameterAssign(&params[idx],
2197 2198 2199
                                        VIR_DOMAIN_CPU_STATS_VCPUTIME,
                                        VIR_TYPED_PARAM_ULLONG,
                                        sum_cpu_time[i]) < 0)
2200
                goto cleanup;
2201 2202 2203
        }

        param_idx++;
2204 2205
    }

2206 2207 2208 2209 2210
    ret = param_idx;

 cleanup:
    virBitmapFree(cpumap);
    return ret;
2211 2212
}

2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262

int
virCgroupGetDomainTotalCpuStats(virCgroupPtr group,
                                virTypedParameterPtr params,
                                int nparams)
{
    unsigned long long cpu_time;
    int ret;

    if (nparams == 0) /* return supported number of params */
        return CGROUP_NB_TOTAL_CPU_STAT_PARAM;
    /* entry 0 is cputime */
    ret = virCgroupGetCpuacctUsage(group, &cpu_time);
    if (ret < 0) {
        virReportSystemError(-ret, "%s", _("unable to get cpu account"));
        return -1;
    }

    if (virTypedParameterAssign(&params[0], VIR_DOMAIN_CPU_STATS_CPUTIME,
                                VIR_TYPED_PARAM_ULLONG, cpu_time) < 0)
        return -1;

    if (nparams > 1) {
        unsigned long long user;
        unsigned long long sys;

        ret = virCgroupGetCpuacctStat(group, &user, &sys);
        if (ret < 0) {
            virReportSystemError(-ret, "%s", _("unable to get cpu account"));
            return -1;
        }

        if (virTypedParameterAssign(&params[1],
                                    VIR_DOMAIN_CPU_STATS_USERTIME,
                                    VIR_TYPED_PARAM_ULLONG, user) < 0)
            return -1;
        if (nparams > 2 &&
            virTypedParameterAssign(&params[2],
                                    VIR_DOMAIN_CPU_STATS_SYSTEMTIME,
                                    VIR_TYPED_PARAM_ULLONG, sys) < 0)
            return -1;

        if (nparams > CGROUP_NB_TOTAL_CPU_STAT_PARAM)
            nparams = CGROUP_NB_TOTAL_CPU_STAT_PARAM;
    }

    return nparams;
}


E
Eric Blake 已提交
2263 2264
int
virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares)
2265
{
2266 2267
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            setCpuShares, -1, shares);
2268 2269
}

E
Eric Blake 已提交
2270 2271 2272

int
virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares)
2273
{
2274 2275
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            getCpuShares, -1, shares);
2276
}
2277

E
Eric Blake 已提交
2278

2279 2280 2281 2282 2283 2284 2285 2286
/**
 * virCgroupSetCpuCfsPeriod:
 *
 * @group: The cgroup to change cpu.cfs_period_us for
 * @cfs_period: The bandwidth period in usecs
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
2287 2288
int
virCgroupSetCpuCfsPeriod(virCgroupPtr group, unsigned long long cfs_period)
2289
{
2290 2291
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            setCpuCfsPeriod, -1, cfs_period);
2292 2293
}

E
Eric Blake 已提交
2294

2295 2296 2297 2298 2299 2300 2301 2302
/**
 * 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
 */
E
Eric Blake 已提交
2303 2304
int
virCgroupGetCpuCfsPeriod(virCgroupPtr group, unsigned long long *cfs_period)
2305
{
2306 2307
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            getCpuCfsPeriod, -1, cfs_period);
2308 2309
}

E
Eric Blake 已提交
2310

2311 2312 2313 2314 2315 2316 2317 2318 2319
/**
 * 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
 */
E
Eric Blake 已提交
2320 2321
int
virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota)
2322
{
2323 2324
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            setCpuCfsQuota, -1, cfs_quota);
2325 2326
}

E
Eric Blake 已提交
2327 2328 2329

int
virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage)
2330
{
2331 2332
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUACCT,
                            getCpuacctPercpuUsage, -1, usage);
2333 2334
}

E
Eric Blake 已提交
2335

2336
int
E
Eric Blake 已提交
2337 2338 2339 2340 2341
virCgroupRemoveRecursively(char *grppath)
{
    DIR *grpdir;
    struct dirent *ent;
    int rc = 0;
E
Eric Blake 已提交
2342
    int direrr;
E
Eric Blake 已提交
2343

J
Ján Tomko 已提交
2344
    if (virDirOpenQuiet(&grpdir, grppath) < 0) {
E
Eric Blake 已提交
2345 2346 2347 2348 2349 2350 2351
        if (errno == ENOENT)
            return 0;
        rc = -errno;
        VIR_ERROR(_("Unable to open %s (%d)"), grppath, errno);
        return rc;
    }

E
Eric Blake 已提交
2352 2353 2354
    /* This is best-effort cleanup: we want to log failures with just
     * VIR_ERROR instead of normal virReportError */
    while ((direrr = virDirRead(grpdir, &ent, NULL)) > 0) {
2355
        g_autofree char *path = NULL;
E
Eric Blake 已提交
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366

        if (ent->d_type != DT_DIR) continue;

        if (virAsprintf(&path, "%s/%s", grppath, ent->d_name) == -1) {
            rc = -ENOMEM;
            break;
        }
        rc = virCgroupRemoveRecursively(path);
        if (rc != 0)
            break;
    }
E
Eric Blake 已提交
2367 2368 2369 2370 2371
    if (direrr < 0) {
        rc = -errno;
        VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
    }

J
Ján Tomko 已提交
2372
    VIR_DIR_CLOSE(grpdir);
E
Eric Blake 已提交
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398

    VIR_DEBUG("Removing cgroup %s", grppath);
    if (rmdir(grppath) != 0 && errno != ENOENT) {
        rc = -errno;
        VIR_ERROR(_("Unable to remove %s (%d)"), grppath, errno);
    }

    return rc;
}


/**
 * virCgroupRemove:
 *
 * @group: The group to be removed
 *
 * 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.
 *
 * Returns: 0 on success
 */
int
virCgroupRemove(virCgroupPtr group)
{
2399 2400 2401
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
2402 2403 2404 2405
        if (group->backends[i]) {
            int rc = group->backends[i]->remove(group);
            if (rc < 0)
                return rc;
2406 2407 2408 2409
        }
    }

    return 0;
E
Eric Blake 已提交
2410 2411 2412
}


2413 2414 2415
/*
 * Returns 1 if some PIDs are killed, 0 if none are killed, or -1 on error
 */
E
Eric Blake 已提交
2416
static int
2417 2418 2419 2420 2421
virCgroupKillInternal(virCgroupPtr group,
                      int signum,
                      virHashTablePtr pids,
                      int controller,
                      const char *taskFile)
2422
{
2423
    int ret = -1;
2424
    bool killedAny = false;
2425
    g_autofree char *keypath = NULL;
2426
    bool done = false;
E
Eric Blake 已提交
2427 2428 2429
    FILE *fp = NULL;
    VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
              group, group->path, signum, pids);
2430

2431
    if (virCgroupPathOfController(group, controller, taskFile, &keypath) < 0)
2432
        return -1;
2433 2434 2435 2436 2437 2438 2439

    /* 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"))) {
2440 2441 2442 2443 2444 2445
            if (errno == ENOENT) {
                VIR_DEBUG("No file %s, assuming done", keypath);
                killedAny = false;
                goto done;
            }

2446 2447 2448
            virReportSystemError(errno,
                                 _("Failed to read %s"),
                                 keypath);
2449 2450 2451
            goto cleanup;
        } else {
            while (!feof(fp)) {
M
Michal Privoznik 已提交
2452 2453
                long pid_value;
                if (fscanf(fp, "%ld", &pid_value) != 1) {
2454 2455
                    if (feof(fp))
                        break;
2456 2457 2458
                    virReportSystemError(errno,
                                         _("Failed to read %s"),
                                         keypath);
E
Eric Blake 已提交
2459
                    goto cleanup;
2460
                }
2461
                if (virHashLookup(pids, (void*)pid_value))
2462 2463
                    continue;

M
Michal Privoznik 已提交
2464
                VIR_DEBUG("pid=%ld", pid_value);
2465 2466
                /* Cgroups is a Linux concept, so this cast is safe.  */
                if (kill((pid_t)pid_value, signum) < 0) {
2467
                    if (errno != ESRCH) {
2468
                        virReportSystemError(errno,
M
Michal Privoznik 已提交
2469
                                             _("Failed to kill process %ld"),
2470
                                             pid_value);
2471 2472 2473 2474
                        goto cleanup;
                    }
                    /* Leave RC == 0 since we didn't kill one */
                } else {
2475
                    killedAny = true;
2476 2477 2478
                    done = false;
                }

2479
                ignore_value(virHashAddEntry(pids, (void*)pid_value, (void*)1));
2480 2481 2482 2483 2484
            }
            VIR_FORCE_FCLOSE(fp);
        }
    }

2485
 done:
2486
    ret = killedAny ? 1 : 0;
2487

2488
 cleanup:
E
Eric Blake 已提交
2489
    VIR_FORCE_FCLOSE(fp);
2490

2491
    return ret;
2492 2493 2494
}


E
Eric Blake 已提交
2495 2496
static uint32_t
virCgroupPidCode(const void *name, uint32_t seed)
2497
{
M
Michal Privoznik 已提交
2498
    long pid_value = (long)(intptr_t)name;
2499
    return virHashCodeGen(&pid_value, sizeof(pid_value), seed);
2500
}
E
Eric Blake 已提交
2501 2502 2503 2504


static bool
virCgroupPidEqual(const void *namea, const void *nameb)
2505 2506 2507
{
    return namea == nameb;
}
E
Eric Blake 已提交
2508 2509 2510 2511


static void *
virCgroupPidCopy(const void *name)
2512 2513 2514 2515
{
    return (void*)name;
}

E
Eric Blake 已提交
2516

2517
int
E
Eric Blake 已提交
2518 2519 2520
virCgroupKillRecursiveInternal(virCgroupPtr group,
                               int signum,
                               virHashTablePtr pids,
2521 2522
                               int controller,
                               const char *taskFile,
E
Eric Blake 已提交
2523
                               bool dormdir)
2524
{
2525
    int ret = -1;
2526
    int rc;
2527
    bool killedAny = false;
2528
    g_autofree char *keypath = NULL;
2529
    DIR *dp = NULL;
2530
    virCgroupPtr subgroup = NULL;
2531
    struct dirent *ent;
E
Eric Blake 已提交
2532
    int direrr;
E
Eric Blake 已提交
2533 2534
    VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
              group, group->path, signum, pids);
2535

2536
    if (virCgroupPathOfController(group, controller, "", &keypath) < 0)
2537
        return -1;
2538

2539 2540
    if ((rc = virCgroupKillInternal(group, signum, pids,
                                    controller, taskFile)) < 0) {
2541
        goto cleanup;
2542
    }
2543 2544
    if (rc == 1)
        killedAny = true;
2545

2546
    VIR_DEBUG("Iterate over children of %s (killedAny=%d)", keypath, killedAny);
J
Ján Tomko 已提交
2547
    if ((rc = virDirOpenIfExists(&dp, keypath)) < 0)
2548
        goto cleanup;
J
Ján Tomko 已提交
2549 2550 2551 2552 2553

    if (rc == 0) {
        VIR_DEBUG("Path %s does not exist, assuming done", keypath);
        killedAny = false;
        goto done;
2554 2555
    }

E
Eric Blake 已提交
2556
    while ((direrr = virDirRead(dp, &ent, keypath)) > 0) {
2557 2558 2559 2560 2561
        if (ent->d_type != DT_DIR)
            continue;

        VIR_DEBUG("Process subdir %s", ent->d_name);

2562
        if (virCgroupNew(-1, ent->d_name, group, -1, &subgroup) < 0)
2563 2564
            goto cleanup;

E
Eric Blake 已提交
2565
        if ((rc = virCgroupKillRecursiveInternal(subgroup, signum, pids,
2566
                                                 controller, taskFile, true)) < 0)
2567 2568
            goto cleanup;
        if (rc == 1)
2569
            killedAny = true;
2570 2571 2572

        if (dormdir)
            virCgroupRemove(subgroup);
2573

2574
        virCgroupFree(&subgroup);
2575
    }
E
Eric Blake 已提交
2576 2577
    if (direrr < 0)
        goto cleanup;
2578

2579
 done:
2580
    ret = killedAny ? 1 : 0;
2581

2582
 cleanup:
2583
    virCgroupFree(&subgroup);
J
Ján Tomko 已提交
2584
    VIR_DIR_CLOSE(dp);
2585
    return ret;
2586 2587
}

E
Eric Blake 已提交
2588 2589 2590

int
virCgroupKillRecursive(virCgroupPtr group, int signum)
2591
{
2592 2593 2594
    int ret = 0;
    int rc;
    size_t i;
2595
    bool backendAvailable = false;
2596
    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
2597
    virHashTablePtr pids = virHashCreateFull(100,
2598
                                             NULL, NULL,
2599 2600 2601 2602
                                             virCgroupPidCode,
                                             virCgroupPidEqual,
                                             virCgroupPidCopy,
                                             NULL);
2603

2604
    VIR_DEBUG("group=%p path=%s signum=%d", group, group->path, signum);
2605

2606
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
2607 2608
        if (backends && backends[i] && backends[i]->available()) {
            backendAvailable = true;
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
            rc = backends[i]->killRecursive(group, signum, pids);
            if (rc < 0) {
                ret = -1;
                goto cleanup;
            }
            if (rc > 0)
                ret = rc;
        }
    }

2619 2620 2621 2622 2623 2624
    if (!backends || !backendAvailable) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no cgroup backend available"));
        goto cleanup;
    }

2625 2626
 cleanup:
    virHashFree(pids);
2627
    return ret;
2628 2629 2630
}


E
Eric Blake 已提交
2631 2632
int
virCgroupKillPainfully(virCgroupPtr group)
2633
{
2634
    size_t i;
2635
    int ret;
2636
    VIR_DEBUG("cgroup=%p path=%s", group, group->path);
2637
    for (i = 0; i < 15; i++) {
2638 2639 2640 2641 2642 2643
        int signum;
        if (i == 0)
            signum = SIGTERM;
        else if (i == 8)
            signum = SIGKILL;
        else
J
Ján Tomko 已提交
2644
            signum = 0; /* Just check for existence */
2645

2646 2647 2648 2649
        ret = virCgroupKillRecursive(group, signum);
        VIR_DEBUG("Iteration %zu rc=%d", i, ret);
        /* If ret == -1 we hit error, if 0 we ran out of PIDs */
        if (ret <= 0)
2650 2651
            break;

2652
        g_usleep(200 * 1000);
2653
    }
2654 2655
    VIR_DEBUG("Complete %d", ret);
    return ret;
2656
}
2657

E
Eric Blake 已提交
2658

2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
/**
 * 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)
{
2671 2672
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            getCpuCfsQuota, -1, cfs_quota);
2673 2674 2675 2676 2677 2678
}


int
virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
{
2679 2680
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUACCT,
                            getCpuacctUsage, -1, usage);
2681 2682 2683 2684 2685 2686 2687
}


int
virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user,
                        unsigned long long *sys)
{
2688 2689
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUACCT,
                            getCpuacctStat, -1, user, sys);
2690 2691 2692 2693 2694 2695
}


int
virCgroupSetFreezerState(virCgroupPtr group, const char *state)
{
2696 2697
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_FREEZER,
                            setFreezerState, -1, state);
2698 2699 2700 2701 2702 2703
}


int
virCgroupGetFreezerState(virCgroupPtr group, char **state)
{
2704 2705
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_FREEZER,
                            getFreezerState, -1, state);
2706 2707 2708
}


E
Eric Blake 已提交
2709
int
2710 2711
virCgroupBindMount(virCgroupPtr group, const char *oldroot,
                   const char *mountopts)
2712
{
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->bindMount(group, oldroot, mountopts) < 0) {
            return -1;
        }
    }

    return 0;
2723
}
2724 2725


2726 2727 2728 2729 2730
int virCgroupSetOwner(virCgroupPtr cgroup,
                      uid_t uid,
                      gid_t gid,
                      int controllers)
{
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (cgroup->backends[i] &&
            cgroup->backends[i]->setOwner(cgroup, uid, gid, controllers) < 0) {
            return -1;
        }
    }

    return 0;
2741 2742 2743
}


2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
/**
 * virCgroupSupportsCpuBW():
 * Check whether the host supports CFS bandwidth.
 *
 * Return true when CFS bandwidth is supported,
 * false when CFS bandwidth is not supported.
 */
bool
virCgroupSupportsCpuBW(virCgroupPtr cgroup)
{
2754 2755
    VIR_CGROUP_BACKEND_CALL(cgroup, VIR_CGROUP_CONTROLLER_CPU,
                            supportsCpuBW, false);
2756 2757
}

2758 2759 2760
int
virCgroupHasEmptyTasks(virCgroupPtr cgroup, int controller)
{
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (cgroup->backends[i]) {
            int rc = cgroup->backends[i]->hasEmptyTasks(cgroup, controller);
            if (rc <= 0)
                return rc;
        }
    }

    return 1;
2772
}
2773

2774 2775 2776
bool
virCgroupControllerAvailable(int controller)
{
2777 2778
    virCgroupPtr cgroup;
    bool ret = false;
2779 2780

    if (virCgroupNewSelf(&cgroup) < 0)
2781
        return ret;
2782

2783
    ret = virCgroupHasController(cgroup, controller);
2784
    virCgroupFree(&cgroup);
2785
    return ret;
2786 2787
}

2788
#else /* !__linux__ */
2789

2790 2791 2792 2793 2794 2795 2796
bool
virCgroupAvailable(void)
{
    return false;
}


2797
int
J
Ján Tomko 已提交
2798 2799 2800 2801
virCgroupNewPartition(const char *path G_GNUC_UNUSED,
                      bool create G_GNUC_UNUSED,
                      int controllers G_GNUC_UNUSED,
                      virCgroupPtr *group G_GNUC_UNUSED)
2802 2803 2804 2805 2806 2807 2808 2809
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2810
virCgroupNewSelf(virCgroupPtr *group G_GNUC_UNUSED)
2811 2812 2813 2814 2815 2816 2817 2818
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2819 2820 2821 2822 2823
virCgroupNewDomainPartition(virCgroupPtr partition G_GNUC_UNUSED,
                            const char *driver G_GNUC_UNUSED,
                            const char *name G_GNUC_UNUSED,
                            bool create G_GNUC_UNUSED,
                            virCgroupPtr *group G_GNUC_UNUSED)
2824 2825 2826 2827 2828 2829 2830
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2831
int
J
Ján Tomko 已提交
2832 2833 2834 2835 2836
virCgroupNewThread(virCgroupPtr domain G_GNUC_UNUSED,
                   virCgroupThreadName nameval G_GNUC_UNUSED,
                   int id G_GNUC_UNUSED,
                   bool create G_GNUC_UNUSED,
                   virCgroupPtr *group G_GNUC_UNUSED)
2837 2838 2839 2840 2841 2842 2843
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2844
int
J
Ján Tomko 已提交
2845 2846 2847
virCgroupNewDetect(pid_t pid G_GNUC_UNUSED,
                   int controllers G_GNUC_UNUSED,
                   virCgroupPtr *group G_GNUC_UNUSED)
2848 2849 2850 2851 2852 2853 2854
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2855
int
J
Ján Tomko 已提交
2856 2857 2858 2859 2860 2861
virCgroupNewDetectMachine(const char *name G_GNUC_UNUSED,
                          const char *drivername G_GNUC_UNUSED,
                          pid_t pid G_GNUC_UNUSED,
                          int controllers G_GNUC_UNUSED,
                          char *machinename G_GNUC_UNUSED,
                          virCgroupPtr *group G_GNUC_UNUSED)
2862 2863 2864 2865 2866 2867
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

J
Ján Tomko 已提交
2868

J
Ján Tomko 已提交
2869
int virCgroupTerminateMachine(const char *name G_GNUC_UNUSED)
J
Ján Tomko 已提交
2870 2871 2872 2873 2874 2875 2876
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2877
int
J
Ján Tomko 已提交
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
virCgroupNewMachine(const char *name G_GNUC_UNUSED,
                    const char *drivername G_GNUC_UNUSED,
                    const unsigned char *uuid G_GNUC_UNUSED,
                    const char *rootdir G_GNUC_UNUSED,
                    pid_t pidleader G_GNUC_UNUSED,
                    bool isContainer G_GNUC_UNUSED,
                    size_t nnicindexes G_GNUC_UNUSED,
                    int *nicindexes G_GNUC_UNUSED,
                    const char *partition G_GNUC_UNUSED,
                    int controllers G_GNUC_UNUSED,
                    unsigned int maxthreads G_GNUC_UNUSED,
                    virCgroupPtr *group G_GNUC_UNUSED)
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


bool
virCgroupNewIgnoreError(void)
{
    VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
    return true;
}

2904 2905

bool
J
Ján Tomko 已提交
2906 2907
virCgroupHasController(virCgroupPtr cgroup G_GNUC_UNUSED,
                       int controller G_GNUC_UNUSED)
2908 2909 2910 2911 2912
{
    return false;
}


2913
int
J
Ján Tomko 已提交
2914 2915 2916 2917
virCgroupPathOfController(virCgroupPtr group G_GNUC_UNUSED,
                          unsigned int controller G_GNUC_UNUSED,
                          const char *key G_GNUC_UNUSED,
                          char **path G_GNUC_UNUSED)
2918 2919 2920 2921 2922 2923 2924
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2925
int
J
Ján Tomko 已提交
2926 2927
virCgroupAddProcess(virCgroupPtr group G_GNUC_UNUSED,
                    pid_t pid G_GNUC_UNUSED)
2928 2929 2930 2931 2932 2933 2934
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2935
int
J
Ján Tomko 已提交
2936 2937
virCgroupAddMachineProcess(virCgroupPtr group G_GNUC_UNUSED,
                           pid_t pid G_GNUC_UNUSED)
2938 2939 2940 2941 2942 2943 2944
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2945
int
J
Ján Tomko 已提交
2946 2947
virCgroupAddThread(virCgroupPtr group G_GNUC_UNUSED,
                   pid_t pid G_GNUC_UNUSED)
2948 2949 2950 2951 2952 2953 2954
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2955
int
J
Ján Tomko 已提交
2956 2957 2958 2959 2960
virCgroupGetBlkioIoServiced(virCgroupPtr group G_GNUC_UNUSED,
                            long long *bytes_read G_GNUC_UNUSED,
                            long long *bytes_write G_GNUC_UNUSED,
                            long long *requests_read G_GNUC_UNUSED,
                            long long *requests_write G_GNUC_UNUSED)
2961 2962 2963 2964 2965 2966 2967 2968
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2969 2970 2971 2972 2973 2974
virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group G_GNUC_UNUSED,
                                  const char *path G_GNUC_UNUSED,
                                  long long *bytes_read G_GNUC_UNUSED,
                                  long long *bytes_write G_GNUC_UNUSED,
                                  long long *requests_read G_GNUC_UNUSED,
                                  long long *requests_write G_GNUC_UNUSED)
2975 2976 2977 2978 2979 2980 2981
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2982
int
J
Ján Tomko 已提交
2983 2984
virCgroupSetBlkioWeight(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned int weight G_GNUC_UNUSED)
2985 2986 2987 2988 2989 2990 2991 2992
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2993 2994
virCgroupGetBlkioWeight(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned int *weight G_GNUC_UNUSED)
2995 2996 2997 2998 2999 3000 3001
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3002
int
J
Ján Tomko 已提交
3003 3004 3005
virCgroupSetBlkioDeviceWeight(virCgroupPtr group G_GNUC_UNUSED,
                              const char *path G_GNUC_UNUSED,
                              unsigned int weight G_GNUC_UNUSED)
3006 3007 3008 3009 3010 3011
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3012
int
J
Ján Tomko 已提交
3013 3014 3015
virCgroupSetBlkioDeviceReadIops(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned int riops G_GNUC_UNUSED)
3016 3017 3018 3019 3020 3021 3022
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

int
J
Ján Tomko 已提交
3023 3024 3025
virCgroupSetBlkioDeviceWriteIops(virCgroupPtr group G_GNUC_UNUSED,
                                 const char *path G_GNUC_UNUSED,
                                 unsigned int wiops G_GNUC_UNUSED)
3026 3027 3028 3029 3030 3031 3032
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

int
J
Ján Tomko 已提交
3033 3034 3035
virCgroupSetBlkioDeviceReadBps(virCgroupPtr group G_GNUC_UNUSED,
                               const char *path G_GNUC_UNUSED,
                               unsigned long long rbps G_GNUC_UNUSED)
3036 3037 3038 3039 3040 3041 3042
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

int
J
Ján Tomko 已提交
3043 3044 3045
virCgroupSetBlkioDeviceWriteBps(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned long long wbps G_GNUC_UNUSED)
3046 3047 3048 3049 3050 3051
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3052
int
J
Ján Tomko 已提交
3053 3054 3055
virCgroupGetBlkioDeviceWeight(virCgroupPtr group G_GNUC_UNUSED,
                              const char *path G_GNUC_UNUSED,
                              unsigned int *weight G_GNUC_UNUSED)
3056 3057 3058 3059 3060 3061 3062
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

int
J
Ján Tomko 已提交
3063 3064 3065
virCgroupGetBlkioDeviceReadIops(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned int *riops G_GNUC_UNUSED)
3066 3067 3068 3069 3070 3071 3072
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

int
J
Ján Tomko 已提交
3073 3074 3075
virCgroupGetBlkioDeviceWriteIops(virCgroupPtr group G_GNUC_UNUSED,
                                 const char *path G_GNUC_UNUSED,
                                 unsigned int *wiops G_GNUC_UNUSED)
3076 3077 3078 3079 3080 3081 3082
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

int
J
Ján Tomko 已提交
3083 3084 3085
virCgroupGetBlkioDeviceReadBps(virCgroupPtr group G_GNUC_UNUSED,
                               const char *path G_GNUC_UNUSED,
                               unsigned long long *rbps G_GNUC_UNUSED)
3086 3087 3088 3089 3090 3091 3092
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

int
J
Ján Tomko 已提交
3093 3094 3095
virCgroupGetBlkioDeviceWriteBps(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned long long *wbps G_GNUC_UNUSED)
3096 3097 3098 3099 3100
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}
3101

3102
int
J
Ján Tomko 已提交
3103 3104
virCgroupSetMemory(virCgroupPtr group G_GNUC_UNUSED,
                   unsigned long long kb G_GNUC_UNUSED)
3105 3106 3107 3108 3109 3110 3111
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


P
Pavel Hrdina 已提交
3112
int
J
Ján Tomko 已提交
3113 3114 3115 3116 3117 3118 3119
virCgroupGetMemoryStat(virCgroupPtr group G_GNUC_UNUSED,
                       unsigned long long *cache G_GNUC_UNUSED,
                       unsigned long long *activeAnon G_GNUC_UNUSED,
                       unsigned long long *inactiveAnon G_GNUC_UNUSED,
                       unsigned long long *activeFile G_GNUC_UNUSED,
                       unsigned long long *inactiveFile G_GNUC_UNUSED,
                       unsigned long long *unevictable G_GNUC_UNUSED)
P
Pavel Hrdina 已提交
3120 3121 3122 3123 3124 3125 3126
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3127
int
J
Ján Tomko 已提交
3128 3129
virCgroupGetMemoryUsage(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned long *kb G_GNUC_UNUSED)
3130 3131 3132 3133 3134 3135 3136 3137
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3138 3139
virCgroupSetMemoryHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long kb G_GNUC_UNUSED)
3140 3141 3142 3143 3144 3145 3146 3147
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3148 3149
virCgroupGetMemoryHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long *kb G_GNUC_UNUSED)
3150 3151 3152 3153 3154 3155 3156 3157
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3158 3159
virCgroupSetMemorySoftLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long kb G_GNUC_UNUSED)
3160 3161 3162 3163 3164 3165 3166 3167
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3168 3169
virCgroupGetMemorySoftLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long *kb G_GNUC_UNUSED)
3170 3171 3172 3173 3174 3175 3176 3177
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3178 3179
virCgroupSetMemSwapHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                             unsigned long long kb G_GNUC_UNUSED)
3180 3181 3182 3183 3184 3185 3186 3187
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3188 3189
virCgroupGetMemSwapHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                             unsigned long long *kb G_GNUC_UNUSED)
3190 3191 3192 3193 3194 3195 3196 3197
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3198 3199
virCgroupGetMemSwapUsage(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long *kb G_GNUC_UNUSED)
3200 3201 3202 3203 3204 3205 3206 3207
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3208 3209
virCgroupSetCpusetMems(virCgroupPtr group G_GNUC_UNUSED,
                       const char *mems G_GNUC_UNUSED)
3210 3211 3212 3213 3214 3215 3216 3217
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3218 3219
virCgroupGetCpusetMems(virCgroupPtr group G_GNUC_UNUSED,
                       char **mems G_GNUC_UNUSED)
3220 3221 3222 3223 3224 3225
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3226 3227

int
J
Ján Tomko 已提交
3228 3229
virCgroupSetCpusetMemoryMigrate(virCgroupPtr group G_GNUC_UNUSED,
                                bool migrate G_GNUC_UNUSED)
3230 3231 3232 3233 3234 3235 3236 3237
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3238 3239
virCgroupGetCpusetMemoryMigrate(virCgroupPtr group G_GNUC_UNUSED,
                                bool *migrate G_GNUC_UNUSED)
3240 3241 3242 3243 3244 3245
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3246 3247

int
J
Ján Tomko 已提交
3248 3249
virCgroupSetCpusetCpus(virCgroupPtr group G_GNUC_UNUSED,
                       const char *cpus G_GNUC_UNUSED)
3250 3251 3252 3253 3254 3255 3256 3257
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3258 3259
virCgroupGetCpusetCpus(virCgroupPtr group G_GNUC_UNUSED,
                       char **cpus G_GNUC_UNUSED)
3260 3261 3262 3263 3264 3265
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3266
int
J
Ján Tomko 已提交
3267 3268
virCgroupAllowAllDevices(virCgroupPtr group G_GNUC_UNUSED,
                         int perms G_GNUC_UNUSED)
3269 3270 3271 3272 3273
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}
3274 3275

int
J
Ján Tomko 已提交
3276
virCgroupDenyAllDevices(virCgroupPtr group G_GNUC_UNUSED)
3277 3278 3279 3280 3281 3282 3283 3284
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3285 3286 3287 3288 3289
virCgroupAllowDevice(virCgroupPtr group G_GNUC_UNUSED,
                     char type G_GNUC_UNUSED,
                     int major G_GNUC_UNUSED,
                     int minor G_GNUC_UNUSED,
                     int perms G_GNUC_UNUSED)
3290 3291 3292 3293 3294 3295 3296
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3297
int
J
Ján Tomko 已提交
3298 3299 3300 3301
virCgroupAllowDevicePath(virCgroupPtr group G_GNUC_UNUSED,
                         const char *path G_GNUC_UNUSED,
                         int perms G_GNUC_UNUSED,
                         bool ignoreEaccess G_GNUC_UNUSED)
3302 3303 3304 3305 3306 3307 3308
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3309
int
J
Ján Tomko 已提交
3310 3311 3312 3313 3314
virCgroupDenyDevice(virCgroupPtr group G_GNUC_UNUSED,
                    char type G_GNUC_UNUSED,
                    int major G_GNUC_UNUSED,
                    int minor G_GNUC_UNUSED,
                    int perms G_GNUC_UNUSED)
3315 3316 3317 3318 3319 3320 3321
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3322
int
J
Ján Tomko 已提交
3323 3324 3325 3326
virCgroupDenyDevicePath(virCgroupPtr group G_GNUC_UNUSED,
                        const char *path G_GNUC_UNUSED,
                        int perms G_GNUC_UNUSED,
                        bool ignoreEacces G_GNUC_UNUSED)
3327 3328 3329 3330 3331 3332 3333
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3334
int
J
Ján Tomko 已提交
3335 3336
virCgroupSetCpuShares(virCgroupPtr group G_GNUC_UNUSED,
                      unsigned long long shares G_GNUC_UNUSED)
3337 3338 3339 3340 3341 3342 3343 3344
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3345 3346
virCgroupGetCpuShares(virCgroupPtr group G_GNUC_UNUSED,
                      unsigned long long *shares G_GNUC_UNUSED)
3347 3348 3349 3350 3351 3352 3353 3354
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3355 3356
virCgroupSetCpuCfsPeriod(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long cfs_period G_GNUC_UNUSED)
3357 3358 3359 3360 3361 3362 3363 3364
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3365 3366
virCgroupGetCpuCfsPeriod(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long *cfs_period G_GNUC_UNUSED)
3367 3368 3369 3370 3371 3372 3373 3374
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3375 3376
virCgroupSetCpuCfsQuota(virCgroupPtr group G_GNUC_UNUSED,
                        long long cfs_quota G_GNUC_UNUSED)
3377 3378 3379 3380 3381 3382 3383 3384
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3385
virCgroupRemove(virCgroupPtr group G_GNUC_UNUSED)
3386 3387 3388 3389 3390 3391 3392
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3393
int
J
Ján Tomko 已提交
3394 3395
virCgroupKillRecursive(virCgroupPtr group G_GNUC_UNUSED,
                       int signum G_GNUC_UNUSED)
3396 3397 3398 3399 3400 3401 3402 3403
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3404
virCgroupKillPainfully(virCgroupPtr group G_GNUC_UNUSED)
3405 3406 3407 3408 3409 3410 3411
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3412
int
J
Ján Tomko 已提交
3413 3414
virCgroupGetCpuCfsQuota(virCgroupPtr group G_GNUC_UNUSED,
                        long long *cfs_quota G_GNUC_UNUSED)
3415 3416 3417 3418 3419 3420 3421 3422
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3423 3424
virCgroupGetCpuacctUsage(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long *usage G_GNUC_UNUSED)
3425 3426 3427 3428 3429 3430 3431 3432
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3433 3434
virCgroupGetCpuacctPercpuUsage(virCgroupPtr group G_GNUC_UNUSED,
                               char **usage G_GNUC_UNUSED)
3435 3436 3437 3438 3439 3440 3441 3442
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3443 3444 3445
virCgroupGetCpuacctStat(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned long long *user G_GNUC_UNUSED,
                        unsigned long long *sys G_GNUC_UNUSED)
3446 3447 3448 3449 3450 3451 3452
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3453
int
J
Ján Tomko 已提交
3454 3455 3456
virCgroupGetDomainTotalCpuStats(virCgroupPtr group G_GNUC_UNUSED,
                                virTypedParameterPtr params G_GNUC_UNUSED,
                                int nparams G_GNUC_UNUSED)
3457 3458 3459 3460 3461 3462 3463
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3464
int
J
Ján Tomko 已提交
3465 3466
virCgroupSetFreezerState(virCgroupPtr group G_GNUC_UNUSED,
                         const char *state G_GNUC_UNUSED)
3467 3468 3469 3470 3471 3472 3473 3474
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3475 3476
virCgroupGetFreezerState(virCgroupPtr group G_GNUC_UNUSED,
                         char **state G_GNUC_UNUSED)
3477 3478 3479 3480 3481 3482 3483
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


E
Eric Blake 已提交
3484
int
J
Ján Tomko 已提交
3485 3486 3487
virCgroupBindMount(virCgroupPtr group G_GNUC_UNUSED,
                   const char *oldroot G_GNUC_UNUSED,
                   const char *mountopts G_GNUC_UNUSED)
3488
{
3489 3490 3491
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
3492
}
3493

3494 3495

bool
J
Ján Tomko 已提交
3496
virCgroupSupportsCpuBW(virCgroupPtr cgroup G_GNUC_UNUSED)
3497 3498 3499 3500 3501
{
    VIR_DEBUG("Control groups not supported on this platform");
    return false;
}

E
Eric Blake 已提交
3502 3503

int
J
Ján Tomko 已提交
3504 3505 3506 3507 3508 3509
virCgroupGetPercpuStats(virCgroupPtr group G_GNUC_UNUSED,
                        virTypedParameterPtr params G_GNUC_UNUSED,
                        unsigned int nparams G_GNUC_UNUSED,
                        int start_cpu G_GNUC_UNUSED,
                        unsigned int ncpus G_GNUC_UNUSED,
                        virBitmapPtr guestvcpus G_GNUC_UNUSED)
E
Eric Blake 已提交
3510 3511 3512 3513 3514 3515 3516 3517
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3518 3519 3520 3521
virCgroupSetOwner(virCgroupPtr cgroup G_GNUC_UNUSED,
                  uid_t uid G_GNUC_UNUSED,
                  gid_t gid G_GNUC_UNUSED,
                  int controllers G_GNUC_UNUSED)
E
Eric Blake 已提交
3522 3523 3524 3525 3526 3527
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3528
int
J
Ján Tomko 已提交
3529 3530
virCgroupHasEmptyTasks(virCgroupPtr cgroup G_GNUC_UNUSED,
                       int controller G_GNUC_UNUSED)
3531 3532 3533 3534 3535 3536
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3537
bool
J
Ján Tomko 已提交
3538
virCgroupControllerAvailable(int controller G_GNUC_UNUSED)
3539 3540 3541
{
    return false;
}
3542
#endif /* !__linux__ */
3543 3544


3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
/**
 * virCgroupFree:
 *
 * @group: The group structure to free
 */
void
virCgroupFree(virCgroupPtr *group)
{
    size_t i;

    if (*group == NULL)
        return;

    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
        VIR_FREE((*group)->legacy[i].mountPoint);
        VIR_FREE((*group)->legacy[i].linkPoint);
        VIR_FREE((*group)->legacy[i].placement);
    }

    VIR_FREE((*group)->unified.mountPoint);
    VIR_FREE((*group)->unified.placement);

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


3572 3573 3574 3575 3576
int
virCgroupDelThread(virCgroupPtr cgroup,
                   virCgroupThreadName nameval,
                   int idx)
{
3577
    virCgroupPtr new_cgroup = NULL;
3578 3579 3580 3581 3582 3583 3584

    if (cgroup) {
        if (virCgroupNewThread(cgroup, nameval, idx, false, &new_cgroup) < 0)
            return -1;

        /* Remove the offlined cgroup */
        virCgroupRemove(new_cgroup);
3585
        virCgroupFree(&new_cgroup);
3586 3587 3588 3589
    }

    return 0;
}