vircgroupmock.c 17.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#ifdef __linux__
22
# include "virmock.h"
23 24 25
# include <unistd.h>
# include <fcntl.h>
# include <sys/stat.h>
26 27 28 29 30 31 32

# ifdef MAJOR_IN_MKDEV
#  include <sys/mkdev.h>
# elif MAJOR_IN_SYSMACROS
#  include <sys/sysmacros.h>
# endif

33
# include <stdarg.h>
34
# include "testutilslxc.h"
35 36
# include "virstring.h"
# include "virfile.h"
37
# include "viralloc.h"
38
# include "vircgroupv2devices.h"
39

40 41 42 43
static int (*real_open)(const char *path, int flags, ...);
static FILE *(*real_fopen)(const char *path, const char *mode);
static int (*real_access)(const char *path, int mode);
static int (*real_mkdir)(const char *path, mode_t mode);
44 45 46 47 48

/* Don't make static, since it causes problems with clang
 * when passed as an arg to asprintf()
 * vircgroupmock.c:462:22: error: static variable 'fakesysfsdir' is used in an inline function with external linkage [-Werror,-Wstatic-in-inline]
 */
49
char *fakerootdir;
50
char *fakesysfscgroupdir;
51 52
const char *fakedevicedir0 = FAKEDEVDIR0;
const char *fakedevicedir1 = FAKEDEVDIR1;
53 54


P
Pavel Hrdina 已提交
55
# define SYSFS_CGROUP_PREFIX "/not/really/sys/fs/cgroup"
56 57
# define SYSFS_CPU_PRESENT "/sys/devices/system/cpu/present"
# define SYSFS_CPU_PRESENT_MOCKED "devices_system_cpu_present"
58 59 60 61 62 63 64 65 66 67 68

/*
 * The plan:
 *
 * We fake out /proc/mounts, so make it look as is cgroups
 * are mounted on /not/really/sys/fs/cgroup. We don't
 * use /sys/fs/cgroup, because we want to make it easy to
 * detect places where we've not mocked enough syscalls.
 *
 * In any open/acces/mkdir calls we look at path and if
 * it starts with /not/really/sys/fs/cgroup, we rewrite
69 70 71
 * the path to point at a subdirectory of the temporary
 * directory referred to by LIBVIRT_FAKE_ROOT_DIR env
 * variable that is set by the main test suite
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
 *
 * In mkdir() calls, we simulate the cgroups behaviour
 * whereby creating the directory auto-creates a bunch
 * of files beneath it
 */

static int make_file(const char *path,
                     const char *name,
                     const char *value)
{
    int fd = -1;
    int ret = -1;
    char *filepath = NULL;

    if (asprintf(&filepath, "%s/%s", path, name) < 0)
        return -1;

89
    if ((fd = real_open(filepath, O_CREAT|O_WRONLY, 0600)) < 0)
90 91 92 93 94 95
        goto cleanup;

    if (write(fd, value, strlen(value)) != strlen(value))
        goto cleanup;

    ret = 0;
96
 cleanup:
97 98 99 100 101 102 103
    if (fd != -1 && close(fd) < 0)
        ret = -1;
    free(filepath);

    return ret;
}

104 105

static int make_controller_v1(const char *path, mode_t mode)
106 107 108 109
{
    int ret = -1;
    const char *controller;

110
    if (!STRPREFIX(path, fakesysfscgroupdir)) {
111 112 113
        errno = EINVAL;
        return -1;
    }
114
    controller = path + strlen(fakesysfscgroupdir) + 1;
115 116 117 118 119 120

    if (STREQ(controller, "cpu"))
        return symlink("cpu,cpuacct", path);
    if (STREQ(controller, "cpuacct"))
        return symlink("cpu,cpuacct", path);

121
    if (real_mkdir(path, mode) < 0)
122 123
        goto cleanup;

124 125 126 127
# define MAKE_FILE(name, value) \
    do { \
        if (make_file(path, name, value) < 0) \
            goto cleanup; \
128 129 130 131 132 133 134 135 136 137
    } while (0)

    if (STRPREFIX(controller, "cpu,cpuacct")) {
        MAKE_FILE("cpu.cfs_period_us", "100000\n");
        MAKE_FILE("cpu.cfs_quota_us", "-1\n");
        MAKE_FILE("cpu.shares", "1024\n");
        MAKE_FILE("cpuacct.stat",
                  "user 216687025\n"
                  "system 43421396\n");
        MAKE_FILE("cpuacct.usage", "2787788855799582\n");
138 139 140 141 142 143 144 145 146
        MAKE_FILE("cpuacct.usage_percpu",
                  "7059492996 0 0 0 0 0 0 0 4180532496 0 0 0 0 0 0 0 "
                  "1957541268 0 0 0 0 0 0 0 2065932204 0 0 0 0 0 0 0 "
                  "18228689414 0 0 0 0 0 0 0 4245525148 0 0 0 0 0 0 0 "
                  "2911161568 0 0 0 0 0 0 0 1407758136 0 0 0 0 0 0 0 "
                  "1836807700 0 0 0 0 0 0 0 1065296618 0 0 0 0 0 0 0 "
                  "2046213266 0 0 0 0 0 0 0 747889778 0 0 0 0 0 0 0 "
                  "709566900 0 0 0 0 0 0 0 444777342 0 0 0 0 0 0 0 "
                  "5683512916 0 0 0 0 0 0 0 635751356 0 0 0 0 0 0 0\n");
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 194 195 196 197 198 199 200 201
    } else if (STRPREFIX(controller, "cpuset")) {
        if (STREQ(controller, "cpuset"))
            MAKE_FILE("cpuset.cpus", "0-1");
        else
            MAKE_FILE("cpuset.cpus", ""); /* Values don't inherit */
        MAKE_FILE("cpuset.memory_migrate", "0\n");
        if (STREQ(controller, "cpuset"))
            MAKE_FILE("cpuset.mems", "0");
        else
            MAKE_FILE("cpuset.mems", ""); /* Values don't inherit */
    } else if (STRPREFIX(controller, "memory")) {
        MAKE_FILE("memory.limit_in_bytes", "9223372036854775807\n");
        MAKE_FILE("memory.memsw.limit_in_bytes", ""); /* Not supported */
        MAKE_FILE("memory.memsw.usage_in_bytes", ""); /* Not supported */
        MAKE_FILE("memory.soft_limit_in_bytes", "9223372036854775807\n");
        MAKE_FILE("memory.stat",
                  "cache 1336619008\n"
                  "rss 97792000\n"
                  "mapped_file 42090496\n"
                  "pgpgin 13022605027\n"
                  "pgpgout 13023820533\n"
                  "pgfault 54429417056\n"
                  "pgmajfault 315715\n"
                  "inactive_anon 145887232\n"
                  "active_anon 67100672\n"
                  "inactive_file 627400704\n"
                  "active_file 661872640\n"
                  "unevictable 3690496\n"
                  "hierarchical_memory_limit 9223372036854775807\n"
                  "total_cache 1336635392\n"
                  "total_rss 118689792\n"
                  "total_mapped_file 42106880\n"
                  "total_pgpgin 13022606816\n"
                  "total_pgpgout 13023820793\n"
                  "total_pgfault 54429422313\n"
                  "total_pgmajfault 315715\n"
                  "total_inactive_anon 145891328\n"
                  "total_active_anon 88010752\n"
                  "total_inactive_file 627400704\n"
                  "total_active_file 661872640\n"
                  "total_unevictable 3690496\n"
                  "recent_rotated_anon 112807028\n"
                  "recent_rotated_file 2547948\n"
                  "recent_scanned_anon 113796164\n"
                  "recent_scanned_file 8199863\n");
        MAKE_FILE("memory.usage_in_bytes", "1455321088\n");
        MAKE_FILE("memory.use_hierarchy", "0\n");
    } else if (STRPREFIX(controller, "freezer")) {
        MAKE_FILE("freezer.state", "THAWED");
    } else if (STRPREFIX(controller, "blkio")) {
        MAKE_FILE("blkio.throttle.io_service_bytes",
                  "8:0 Read 59542107136\n"
                  "8:0 Write 411440480256\n"
                  "8:0 Sync 248486822912\n"
                  "8:0 Async 222495764480\n"
202 203 204 205 206 207
                  "8:0 Total 470982587392\n"
                  "9:0 Read 59542107137\n"
                  "9:0 Write 411440480257\n"
                  "9:0 Sync 248486822912\n"
                  "9:0 Async 222495764480\n"
                  "9:0 Total 470982587392\n");
208 209 210 211 212
        MAKE_FILE("blkio.throttle.io_serviced",
                  "8:0 Read 4832583\n"
                  "8:0 Write 36641903\n"
                  "8:0 Sync 30723171\n"
                  "8:0 Async 10751315\n"
213 214 215 216 217 218
                  "8:0 Total 41474486\n"
                  "9:0 Read 4832584\n"
                  "9:0 Write 36641904\n"
                  "9:0 Sync 30723171\n"
                  "9:0 Async 10751315\n"
                  "9:0 Total 41474486\n");
219 220 221 222 223 224 225 226 227 228 229 230
        MAKE_FILE("blkio.throttle.read_bps_device", "");
        MAKE_FILE("blkio.throttle.read_iops_device", "");
        MAKE_FILE("blkio.throttle.write_bps_device", "");
        MAKE_FILE("blkio.throttle.write_iops_device", "");
        MAKE_FILE("blkio.weight", "1000\n");
        MAKE_FILE("blkio.weight_device", "");

    } else {
        errno = EINVAL;
        goto cleanup;
    }

231 232
# undef MAKE_FILE

233
    ret = 0;
234
 cleanup:
235 236 237
    return ret;
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

static int make_controller_v2(const char *path, mode_t mode)
{
    if (!STRPREFIX(path, fakesysfscgroupdir)) {
        errno = EINVAL;
        return -1;
    }

    if (real_mkdir(path, mode) < 0 && errno != EEXIST)
        return -1;

# define MAKE_FILE(name, value) \
    do { \
        if (make_file(path, name, value) < 0) \
            return -1; \
    } while (0)

    MAKE_FILE("cgroup.controllers", "cpu io memory\n");
    MAKE_FILE("cgroup.subtree_control", "");
    MAKE_FILE("cgroup.type", "domain\n");
    MAKE_FILE("cpu.max", "max 100000\n");
    MAKE_FILE("cpu.stat",
              "usage_usec 0\n"
              "user_usec 0\n"
              "system_usec 0\n"
              "nr_periods 0\n"
              "nr_throttled 0\n"
              "throttled_usec 0\n");
    MAKE_FILE("cpu.weight", "100\n");
    MAKE_FILE("memory.current", "1455321088\n");
    MAKE_FILE("memory.high", "max\n");
    MAKE_FILE("memory.max", "max\n");
    MAKE_FILE("memory.stat",
              "anon 0\n"
              "file 0\n"
              "kernel_stack 0\n"
              "slab 0\n"
              "sock 0\n"
              "shmem 0\n"
              "file_mapped 0\n"
              "file_dirty 0\n"
              "file_writeback 0\n"
              "inactive_anon 0\n"
              "active_anon 0\n"
              "inactive_file 0\n"
              "active_file 0\n"
              "unevictable 0\n"
              "slab_reclaimable 0\n"
              "slab_unreclaimable 0\n"
              "pgfault 0\n"
              "pgmajfault 0\n"
              "pgrefill 0\n"
              "pgscan 0\n"
              "pgsteal 0\n"
              "pgactivate 0\n"
              "pgdeactivate 0\n"
              "pglazyfree 0\n"
              "pglazyfreed 0\n"
              "workingset_refault 0\n"
              "workingset_activate 0\n"
              "workingset_nodereclaim 0\n");
    MAKE_FILE("memory.swap.current", "0\n");
    MAKE_FILE("memory.swap.max", "max\n");
    MAKE_FILE("io.stat", "8:0 rbytes=26828800 wbytes=77062144 rios=2256 wios=7849 dbytes=0 dios=0\n");
    MAKE_FILE("io.max", "");
    MAKE_FILE("io.weight", "default 100\n");

# undef MAKE_FILE

    return 0;
}


311 312
static void init_syms(void)
{
313
    if (real_fopen)
314 315
        return;

316 317 318 319
    VIR_MOCK_REAL_INIT(fopen);
    VIR_MOCK_REAL_INIT(access);
    VIR_MOCK_REAL_INIT(mkdir);
    VIR_MOCK_REAL_INIT(open);
320 321
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

static int make_controller(const char *path, mode_t mode)
{
    const char *mock;
    bool unified = false;
    bool hybrid = false;

    mock = getenv("VIR_CGROUP_MOCK_MODE");
    if (mock) {
        if (STREQ(mock, "unified")) {
            unified = true;
        } else if (STREQ(mock, "hybrid")) {
            hybrid = true;
        } else {
            fprintf(stderr, "invalid mode '%s'\n", mock);
            abort();
        }
    }

    if (unified || (hybrid && strstr(path, "unified"))) {
        return make_controller_v2(path, mode);
    } else {
        return make_controller_v1(path, mode);
    }
}


349 350
static void init_sysfs(void)
{
351 352 353 354
    const char *mock;
    char *newfakerootdir;
    bool unified = false;
    bool hybrid = false;
355

356
    if (!(newfakerootdir = getenv("LIBVIRT_FAKE_ROOT_DIR"))) {
357
        fprintf(stderr, "Missing LIBVIRT_FAKE_ROOT_DIR env variable\n");
358 359 360
        abort();
    }

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    if (fakerootdir && STREQ(fakerootdir, newfakerootdir))
        return;

    fakerootdir = newfakerootdir;

    mock = getenv("VIR_CGROUP_MOCK_MODE");
    if (mock) {
        if (STREQ(mock, "unified")) {
            unified = true;
        } else if (STREQ(mock, "hybrid")) {
            hybrid = true;
        } else {
            fprintf(stderr, "invalid mode '%s'\n", mock);
            abort();
        }
    }

    VIR_FREE(fakesysfscgroupdir);

380 381
    fakesysfscgroupdir = g_strdup_printf("%s%s",
                                         fakerootdir, SYSFS_CGROUP_PREFIX);
382

383 384
    if (virFileMakePath(fakesysfscgroupdir) < 0) {
        fprintf(stderr, "Cannot create %s\n", fakesysfscgroupdir);
385 386 387
        abort();
    }

388 389 390
# define MAKE_CONTROLLER(subpath) \
    do { \
        char *path; \
391
        if (asprintf(&path, "%s/%s", fakesysfscgroupdir, subpath) < 0) \
392 393 394 395 396 397 398
            abort(); \
        if (make_controller(path, 0755) < 0) { \
            fprintf(stderr, "Cannot initialize %s\n", path); \
            free(path); \
            abort(); \
        } \
        free(path); \
399 400
    } while (0)

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    if (unified) {
        MAKE_CONTROLLER("");
    } else if (hybrid) {
        MAKE_CONTROLLER("unified");
        MAKE_CONTROLLER("cpuset");
        MAKE_CONTROLLER("freezer");
    } else {
        MAKE_CONTROLLER("cpu");
        MAKE_CONTROLLER("cpuacct");
        MAKE_CONTROLLER("cpu,cpuacct");
        MAKE_CONTROLLER("cpuset");
        MAKE_CONTROLLER("blkio");
        MAKE_CONTROLLER("memory");
        MAKE_CONTROLLER("freezer");

        if (make_file(fakesysfscgroupdir,
                      SYSFS_CPU_PRESENT_MOCKED, "8-23,48-159\n") < 0)
            abort();
    }
420 421 422 423 424
}


FILE *fopen(const char *path, const char *mode)
{
425 426 427 428
    char *filepath = NULL;
    const char *type = NULL;
    FILE *rc = NULL;
    const char *filename = getenv("VIR_CGROUP_MOCK_FILENAME");
429

430
    init_syms();
431

432 433
    if (STREQ(path, "/proc/mounts")) {
        if (STREQ(mode, "r")) {
434
            type = "mounts";
435 436 437 438 439 440 441
        } else {
            errno = EACCES;
            return NULL;
        }
    }
    if (STREQ(path, "/proc/cgroups")) {
        if (STREQ(mode, "r")) {
442
            type = "cgroups";
443 444 445 446 447 448 449
        } else {
            errno = EACCES;
            return NULL;
        }
    }
    if (STREQ(path, "/proc/self/cgroup")) {
        if (STREQ(mode, "r")) {
450
            type = "self.cgroup";
451 452 453 454 455 456
        } else {
            errno = EACCES;
            return NULL;
        }
    }

457
    if (type) {
458 459 460 461
        if (!filename) {
            errno = EACCES;
            return NULL;
        }
462 463
        filepath = g_strdup_printf("%s/vircgroupdata/%s.%s",
                                   abs_srcdir, filename, type);
464 465 466 467 468
        rc = real_fopen(filepath, mode);
        free(filepath);
        return rc;
    }

469
    return real_fopen(path, mode);
470 471 472 473 474 475 476 477
}

int access(const char *path, int mode)
{
    int ret;

    init_syms();

478
    if (STRPREFIX(path, SYSFS_CGROUP_PREFIX)) {
479 480
        init_sysfs();
        char *newpath;
P
Pavel Hrdina 已提交
481
        if (asprintf(&newpath, "%s%s",
482 483
                     fakesysfscgroupdir,
                     path + strlen(SYSFS_CGROUP_PREFIX)) < 0) {
484 485 486
            errno = ENOMEM;
            return -1;
        }
487
        ret = real_access(newpath, mode);
488
        free(newpath);
489
    } else if (STREQ(path, "/proc/cgroups") ||
490 491
               STREQ(path, "/proc/self/cgroup") ||
               STREQ(path, SYSFS_CPU_PRESENT)) {
492 493 494 495 496 497
        /* These files are readable for all. */
        ret = (mode == F_OK || mode == R_OK) ? 0 : -1;
    } else if (STREQ(path, "/proc/mounts")) {
        /* This one is accessible anytime for anybody. In fact, it's just
         * a symlink to /proc/self/mounts. */
        ret = 0;
498
    } else {
499
        ret = real_access(path, mode);
500 501 502 503
    }
    return ret;
}

504 505 506 507 508 509 510 511 512 513 514 515
# define VIR_MOCK_STAT_HOOK \
    do { \
        if (STRPREFIX(path, fakedevicedir0)) { \
            sb->st_mode = S_IFBLK; \
            sb->st_rdev = makedev(8, 0); \
            return 0; \
        } else if (STRPREFIX(path, fakedevicedir1)) { \
            sb->st_mode = S_IFBLK; \
            sb->st_rdev = makedev(9, 0); \
            return 0; \
        } \
    } while (0)
516

517
# include "virmockstathelpers.c"
518

519 520
static int
virMockStatRedirect(const char *path, char **newpath)
521
{
522 523
    if (STREQ(path, SYSFS_CPU_PRESENT)) {
        init_sysfs();
524
        if (asprintf(newpath, "%s/%s",
525
                     fakesysfscgroupdir,
526
                     SYSFS_CPU_PRESENT_MOCKED) < 0)
527
            return -1;
528
    } else if (STRPREFIX(path, SYSFS_CGROUP_PREFIX)) {
529
        init_sysfs();
530
        if (asprintf(newpath, "%s%s",
531
                     fakesysfscgroupdir,
532
                     path + strlen(SYSFS_CGROUP_PREFIX)) < 0)
533
            return -1;
534
    }
535
    return 0;
536 537
}

538

539 540 541 542 543 544
int mkdir(const char *path, mode_t mode)
{
    int ret;

    init_syms();

545
    if (STRPREFIX(path, SYSFS_CGROUP_PREFIX)) {
546 547
        init_sysfs();
        char *newpath;
P
Pavel Hrdina 已提交
548
        if (asprintf(&newpath, "%s%s",
549 550
                     fakesysfscgroupdir,
                     path + strlen(SYSFS_CGROUP_PREFIX)) < 0) {
551 552 553 554 555 556
            errno = ENOMEM;
            return -1;
        }
        ret = make_controller(newpath, mode);
        free(newpath);
    } else {
557
        ret = real_mkdir(path, mode);
558 559 560 561 562 563 564 565 566 567 568
    }
    return ret;
}

int open(const char *path, int flags, ...)
{
    int ret;
    char *newpath = NULL;

    init_syms();

569 570 571
    if (STREQ(path, SYSFS_CPU_PRESENT)) {
        init_sysfs();
        if (asprintf(&newpath, "%s/%s",
572
                     fakesysfscgroupdir,
573 574 575 576 577 578
                     SYSFS_CPU_PRESENT_MOCKED) < 0) {
            errno = ENOMEM;
            return -1;
        }
    }

579
    if (STRPREFIX(path, SYSFS_CGROUP_PREFIX)) {
580
        init_sysfs();
P
Pavel Hrdina 已提交
581
        if (asprintf(&newpath, "%s%s",
582 583
                     fakesysfscgroupdir,
                     path + strlen(SYSFS_CGROUP_PREFIX)) < 0) {
584 585 586 587 588 589 590 591
            errno = ENOMEM;
            return -1;
        }
    }
    if (flags & O_CREAT) {
        va_list ap;
        mode_t mode;
        va_start(ap, flags);
592
        mode = (mode_t) va_arg(ap, int);
593
        va_end(ap);
594
        ret = real_open(newpath ? newpath : path, flags, mode);
595
    } else {
596
        ret = real_open(newpath ? newpath : path, flags);
597 598 599 600
    }
    free(newpath);
    return ret;
}
601 602 603 604 605 606

bool
virCgroupV2DevicesAvailable(virCgroupPtr group G_GNUC_UNUSED)
{
    return true;
}
607 608 609
#else
/* Nothing to override on non-__linux__ platforms */
#endif