conf.c 67.0 KB
Newer Older
O
overweight 已提交
1
/******************************************************************************
H
haozi007 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * lcr: utils library for iSula
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
 *
 * Authors:
 * Haozi007 <liuhao27@huawei.com>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ********************************************************************************/

O
overweight 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/utsname.h>
#include <linux/oom.h>

#include "conf.h"
#include "lcrcontainer.h"
#include "lcrcontainer_extend.h"
#include "error.h"
#include "utils.h"
H
haozi007 已提交
39
#include "log.h"
O
overweight 已提交
40 41 42 43 44 45 46 47 48 49 50 51
#include "buffer.h"

#define SUB_UID_PATH "/etc/subuid"
#define SUB_GID_PATH "/etc/subgid"
#define ID_MAP_LEN 100

/* files limit checker */
static int files_limit_checker(const char *value)
{
    long long limit = 0;
    int ret = 0;

L
LiFeng 已提交
52
    ret = lcr_util_safe_llong(value, &limit);
O
overweight 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    if (ret) {
        ret = -1;
    }

    return ret;
}

/* check console log file */
static int check_console_log_file(const char *value)
{
    int ret = 0;

    if (value == NULL) {
        return -1;
    }

    if (strcmp(value, "none") == 0) {
        ret = 1;
    }

    return ret;
}

/* check console log filesize */
static int check_console_log_filesize(const char *value)
{
    int ret = -1;
    int64_t tmp = 0;
    int64_t min = 4 * SIZE_KB;

    if (value == NULL) {
        return ret;
    }

L
LiFeng 已提交
87
    if (lcr_parse_byte_size_string(value, &tmp) == 0 && tmp >= min) {
O
overweight 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        ret = 0;
    }

    return ret;
}

/* check oom score adj */
static int check_oom_score_adj(const char *value)
{
    int ret = -1;
    int tmp = 0;
    int min = OOM_SCORE_ADJ_MIN;
    int max = OOM_SCORE_ADJ_MAX;

    if (value == NULL) {
        return ret;
    }

L
LiFeng 已提交
106
    if (lcr_util_safe_int(value, &tmp) == 0 && tmp >= min && tmp <= max) {
O
overweight 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        ret = 0;
    }
    lcr_set_error_message(LCR_ERR_RUNTIME, "Invalid value %s, range for oom score adj is [%d, %d]", value, min, max);
    return ret;
}
/* check console log filerotate */
static int check_console_log_filerotate(const char *value)
{
    int ret = -1;
    unsigned int tmp = 0;

    if (value == NULL) {
        return ret;
    }

L
LiFeng 已提交
122
    if (lcr_util_safe_uint(value, &tmp) == 0) {
O
overweight 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
        ret = 0;
    }

    return ret;
}

/* check rootfs mount */
static int check_rootfs_mount(const char *value)
{
    if (value == NULL) {
        return -1;
    }

L
LiFeng 已提交
136
    if (!lcr_util_dir_exists(value)) {
O
overweight 已提交
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
        lcr_set_error_message(LCR_ERR_RUNTIME, "Container rootfs mount path '%s' is not exist", value);
        return -1;
    }

    return 0;
}

static inline bool is_native_umask_normal(const char *value)
{
    return strcmp(value, "normal") == 0;
}

static inline bool is_native_umask_secure(const char *value)
{
    return strcmp(value, "secure") == 0;
}


/* check umask */
static int check_native_umask(const char *value)
{
    if (value == NULL) {
        return -1;
    }

    if (!is_native_umask_normal(value) && !is_native_umask_secure(value)) {
        ERROR("Invalid native umask: %s", value);
        return -1;
    }

    return 0;
}

/* check system container */
static int check_system_container(const char *value)
{
    if (value == NULL) {
        return -1;
    }

    if (strcmp(value, "true") != 0) {
        ERROR("Invalid system container: %s", value);
        return -1;
    }

    return 0;
}

/* check cgroup dir */
static int check_cgroup_dir(const char *value)
{
    if (value == NULL) {
        return -1;
    }
    return 0;
}

L
LiuHao 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static int check_empty_value(const char *value)
{
    if (value == NULL || strlen(value) == 0) {
        return -1;
    }
    return 0;
}

static int check_console_log_driver(const char *value)
{
    if (value == NULL || strlen(value) == 0) {
        return -1;
    }
    if (strcmp(value, "syslog") == 0) {
        return 0;
    }
    if (strcmp(value, "json-file") == 0) {
        return 0;
    }
    return -1;
}

O
overweight 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static const lcr_annotation_item_t g_require_annotations[] = {
    {
        "files.limit",
        "lxc.cgroup.files.limit",
        files_limit_checker,
    },
    {
        "log.console.file",
        "lxc.console.logfile",
        check_console_log_file,
    },
    {
        "log.console.filesize",
        "lxc.console.size",
        check_console_log_filesize,
    },
    {
        "log.console.filerotate",
        "lxc.console.rotate",
        check_console_log_filerotate,
    },
L
LiuHao 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    {
        "log.console.driver",
        "lxc.console.logdriver",
        check_console_log_driver,
    },
    {
        "log.console.tag",
        "lxc.console.syslog_tag",
        check_empty_value,
    },
    {
        "log.console.facility",
        "lxc.console.syslog_facility",
        check_empty_value,
    },
O
overweight 已提交
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
    {
        "rootfs.mount",
        "lxc.rootfs.mount",
        check_rootfs_mount,
    },
    {
        "cgroup.dir",
        "lxc.cgroup.dir",
        check_cgroup_dir,
    },
    {
        "native.umask",
        "lxc.isulad.umask",
        check_native_umask,
    },
    {
        "system.container",
        "lxc.isulad.systemd",
        check_system_container,
    },
    {
        "proc.oom_score_adj",
        "lxc.proc.oom_score_adj",
        check_oom_score_adj,
    },
};

/* create lcr list node */
struct lcr_list *create_lcr_list_node(const char *key, const char *value)
{
    struct lcr_list *node = NULL;
    lcr_config_item_t *entry = NULL;

L
LiFeng 已提交
285
    node = lcr_util_common_calloc_s(sizeof(*node));
O
overweight 已提交
286 287 288
    if (node == NULL) {
        return NULL;
    }
L
LiFeng 已提交
289
    entry = lcr_util_common_calloc_s(sizeof(*entry));
O
overweight 已提交
290 291 292 293
    if (entry == NULL) {
        free(node);
        return NULL;
    }
L
LiFeng 已提交
294
    entry->name = lcr_util_strdup_s(key);
O
overweight 已提交
295

L
LiFeng 已提交
296
    entry->value = lcr_util_strdup_s(value);
O
overweight 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

    node->elem = entry;
    return node;
}

/* free lcr list node */
void free_lcr_list_node(struct lcr_list *node)
{
    lcr_config_item_t *entry = NULL;

    if (node == NULL) {
        return;
    }

    entry = node->elem;
    if (entry != NULL) {
        free(entry->name);
        free(entry->value);
    }
    free(node->elem);
    node->elem = NULL;
    free(node);
}

/* trans oci hostname */
struct lcr_list *trans_oci_hostname(const char *hostname)
{
    if (hostname == NULL) {
        return NULL;
    }

    return create_lcr_list_node("lxc.uts.name", hostname);
}

static bool valid_sep_len(size_t sep_len, size_t len)
{
    if (sep_len && (sep_len != 1) && (len > SIZE_MAX / sep_len + 1)) {
        return false;
    }

    return true;
}

/* capabilities join */
static char *capabilities_join(const char *sep, const char **parts, size_t len)
{
    char *result = NULL;
    size_t sep_len;
    size_t result_len;
    size_t iter;

    sep_len = strlen(sep);
    if (valid_sep_len(sep_len, len) == false) {
        return NULL;
    }
    result_len = (len - 1) * sep_len;
    /* calculate new string length
     * dont calculate `CAP_`
     */
    for (iter = 0; iter < len; iter++) {
        if (result_len > 4 && (result_len - 4 >= SIZE_MAX - strlen(parts[iter]))) {
            return NULL;
        }
        result_len += strlen(parts[iter]) - 4;
    }

    result = calloc(result_len + 1, 1);
    if (result == NULL) {
        return NULL;
    }

    for (iter = 0; iter < len - 1; iter++) {
O
openeuler-iSula 已提交
369 370
        (void)strcat(result, &(parts[iter][4]));
        (void)strcat(result, sep);
O
overweight 已提交
371
    }
O
openeuler-iSula 已提交
372
    (void)strcat(result, &(parts[len - 1][4]));
O
overweight 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385

    // Lower case
    for (iter = 0; iter < result_len; iter++) {
        if (result[iter] >= 'A' && result[iter] <= 'Z') {
            result[iter] = (char)(result[iter] + 32);
        }
    }

    return result;
}

#define UID_MAX_SIZE 21
/* UID to use within a private user namespace for init */
H
haozi007 已提交
386
static int trans_oci_process_init_uid(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
387 388 389 390 391 392
{
    struct lcr_list *node = NULL;
    char buf[UID_MAX_SIZE] = { 0 };
    int nret;
    int ret = -1;
    if (proc->user != NULL && proc->user->uid != INVALID_INT) {
O
openeuler-iSula 已提交
393 394
        nret = snprintf(buf, sizeof(buf), "%u", (unsigned int)proc->user->uid);
        if (nret < 0 || (size_t)nret >= sizeof(buf)) {
O
overweight 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
            goto out;
        }

        node = create_lcr_list_node("lxc.init.uid", buf);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* GID to use within a private user namespace for init */
H
haozi007 已提交
410
static int trans_oci_process_init_gid(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
411 412 413 414 415 416
{
    struct lcr_list *node = NULL;
    char buf[UID_MAX_SIZE] = { 0 };
    int nret;
    int ret = -1;
    if (proc->user != NULL && proc->user->gid != INVALID_INT) {
O
openeuler-iSula 已提交
417 418
        nret = snprintf(buf, sizeof(buf), "%u", (unsigned int)proc->user->gid);
        if (nret < 0 || (size_t)nret >= sizeof(buf)) {
O
overweight 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
            goto out;
        }

        node = create_lcr_list_node("lxc.init.gid", buf);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* additional groups for init command */
H
haozi007 已提交
434
static int trans_oci_process_init_groups(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
435 436 437 438 439 440 441 442 443 444 445
{
    struct lcr_list *node = NULL;
    int nret;
    size_t i = 0;
    int ret = -1;
    if (proc->user != NULL && proc->user->additional_gids != NULL && proc->user->additional_gids_len > 0) {
        if (proc->user->additional_gids_len > (SIZE_MAX / (LCR_NUMSTRLEN64 + 1))) {
            goto out;
        }

        size_t total_len = (LCR_NUMSTRLEN64 + 1) * proc->user->additional_gids_len;
L
LiFeng 已提交
446
        char *gids = lcr_util_common_calloc_s(total_len);
O
overweight 已提交
447 448 449 450
        if (gids == NULL) {
            goto out;
        }

O
openeuler-iSula 已提交
451 452
        nret = snprintf(gids, total_len, "%u", (unsigned int)(proc->user->additional_gids[0]));
        if (nret < 0 || (size_t)nret >= total_len) {
O
overweight 已提交
453 454 455 456 457
            free(gids);
            goto out;
        }
        for (i = 1; i < proc->user->additional_gids_len; i++) {
            size_t old_len = strlen(gids);
O
openeuler-iSula 已提交
458 459 460
            nret = snprintf(gids + old_len, total_len - old_len, " %u",
                            (unsigned int)(proc->user->additional_gids[i]));
            if (nret < 0 || (size_t)nret >= (total_len - old_len)) {
O
overweight 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
                free(gids);
                goto out;
            }
        }

        node = create_lcr_list_node("lxc.isulad.init.groups", gids);
        free(gids);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* Sets the command to use as the init system for the containers */
H
haozi007 已提交
479
static int trans_oci_process_init_args(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
{
    struct lcr_list *node = NULL;
    size_t i = 0;
    int ret = -1;
    for (i = 0; i < proc->args_len; i++) {
        node = create_lcr_list_node("lxc.isulad.init.args", proc->args[i]);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* working directory to use within container */
H
haozi007 已提交
497
static int trans_oci_process_init_cwd(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
{
    struct lcr_list *node = NULL;
    int ret = -1;
    if (proc->cwd != NULL) {
        node = create_lcr_list_node("lxc.init.cwd", proc->cwd);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* trans oci process init */
H
haozi007 已提交
514
static int trans_oci_process_init(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
{
    int ret = -1;
    if (trans_oci_process_init_uid(proc, conf)) {
        goto out;
    }

    if (trans_oci_process_init_gid(proc, conf)) {
        goto out;
    }

    if (trans_oci_process_init_groups(proc, conf)) {
        goto out;
    }

    if (trans_oci_process_init_args(proc, conf)) {
        goto out;
    }

    if (trans_oci_process_init_cwd(proc, conf)) {
        goto out;
    }

    ret = 0;
out:
    return ret;
}

/* trans oci process env and cap */
H
haozi007 已提交
543
static int trans_oci_process_env_and_cap(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
544 545 546 547 548 549 550
{
    struct lcr_list *node = NULL;
    char *boundings = NULL;
    int ret = -1;
    size_t i;

    for (i = 0; i < proc->env_len; i++) {
L
LiFeng 已提交
551
        char *replaced = lcr_util_string_replace(" ", SPACE_MAGIC_STR, proc->env[i]);
O
overweight 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
        if (replaced == NULL) {
            ERROR("memory allocation error");
            goto out;
        }
        node = create_lcr_list_node("lxc.environment", replaced);
        free(replaced);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }

    if (proc->capabilities != NULL && proc->capabilities->bounding_len > 0) {
        boundings =
            capabilities_join(" ", (const char **)(proc->capabilities->bounding), proc->capabilities->bounding_len);
        if (boundings == NULL) {
            ERROR("Failed to join bounding capabilities");
            goto out;
        }
        node = create_lcr_list_node("lxc.cap.keep", boundings);
        free(boundings);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    } else {
        node = create_lcr_list_node("lxc.cap.keep", "ISULAD_KEEP_NONE");
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* trans oci process prlimit */
H
haozi007 已提交
590
static int trans_oci_process_prlimit(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
591 592 593 594 595 596 597
{
    struct lcr_list *node = NULL;
    int ret = -1;
    int nret;
    size_t i;

    for (i = 0; i < proc->rlimits_len; i++) {
H
haozi007 已提交
598
        defs_process_rlimits_element *lr = proc->rlimits[i];
O
overweight 已提交
599 600 601
        char buf_key[30] = { 0 };
        char buf_value[60] = { 0 };
        size_t j;
L
LiFeng 已提交
602
        char *type = lcr_util_strdup_s(lr->type);
O
overweight 已提交
603 604 605 606 607 608 609

        // Lower case type,eg. RLIMIT_NOFILE -> RLIMIT_nofile
        for (j = strlen("RLIMIT_"); j < strlen(type); j++) {
            type[j] = (char)tolower(type[j]);
        }

        // Skip `RLIMIT_`
O
openeuler-iSula 已提交
610
        nret = snprintf(buf_key, sizeof(buf_key), "lxc.prlimit.%s", &(type[7]));
O
overweight 已提交
611
        free(type);
O
openeuler-iSula 已提交
612
        if (nret < 0 || (size_t)nret >= sizeof(buf_key)) {
O
overweight 已提交
613 614 615 616
            goto out;
        }

        // We always use format `soft_limit:hard_limit`
O
openeuler-iSula 已提交
617 618 619
        nret = snprintf(buf_value, sizeof(buf_value), "%llu:%llu", (unsigned long long)lr->soft,
                        (unsigned long long)lr->hard);
        if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
            goto out;
        }

        node = create_lcr_list_node(buf_key, buf_value);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* trans oci process no new privs */
H
haozi007 已提交
635
static int trans_oci_process_no_new_privs(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
{
    struct lcr_list *node = NULL;
    int ret = -1;

    if (proc->no_new_privileges) {
        node = create_lcr_list_node("lxc.no_new_privs", "1");
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

H
haozi007 已提交
652
static int trans_oci_process_apparmor(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
{
    struct lcr_list *node = NULL;
    int ret = -1;

    if (proc->apparmor_profile != NULL) {
        node = create_lcr_list_node("lxc.aa_profile", proc->apparmor_profile);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }

    ret = 0;
out:
    return ret;
}

H
haozi007 已提交
670
static int trans_oci_process_selinux(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
671 672 673 674 675
{
    struct lcr_list *node = NULL;
    int ret = -1;

    if (proc->selinux_label != NULL) {
676
        node = create_lcr_list_node("lxc.selinux.context", proc->selinux_label);
O
overweight 已提交
677 678 679 680 681 682 683 684 685 686 687 688
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }

    ret = 0;
out:
    return ret;
}

/* trans oci process apparmor and selinux */
H
haozi007 已提交
689
static int trans_oci_process_apparmor_and_selinux(const defs_process *proc, struct lcr_list *conf)
O
overweight 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
{
    int ret = -1;

    if (trans_oci_process_apparmor(proc, conf) != 0) {
        goto out;
    }

    if (trans_oci_process_selinux(proc, conf) != 0) {
        goto out;
    }

    ret = 0;
out:
    return ret;
}

/* trans oci process */
H
haozi007 已提交
707
struct lcr_list *trans_oci_process(const defs_process *proc)
O
overweight 已提交
708 709 710
{
    struct lcr_list *conf = NULL;

L
LiFeng 已提交
711
    conf = lcr_util_common_calloc_s(sizeof(struct lcr_list));
O
overweight 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    if (trans_oci_process_init(proc, conf)) {
        goto out_free;
    }

    if (trans_oci_process_env_and_cap(proc, conf)) {
        goto out_free;
    }

    if (trans_oci_process_prlimit(proc, conf)) {
        goto out_free;
    }

    if (trans_oci_process_no_new_privs(proc, conf)) {
        goto out_free;
    }

    if (trans_oci_process_apparmor_and_selinux(proc, conf)) {
        goto out_free;
    }

    return conf;

out_free:
    lcr_free_config(conf);
    free(conf);
    return NULL;
}

#define APPEND_COMMA_END_SIZE 2
/* trans oci root rootfs */
static int trans_oci_root_rootfs(const oci_runtime_spec_root *root, struct lcr_list *conf)
{
    struct lcr_list *node = NULL;
    int ret = -1;

    if ((root != NULL) && root->path != NULL) {
        if (strcmp(root->path, "/") != 0) {
            node = create_lcr_list_node("lxc.rootfs.path", root->path);
            if (node == NULL) {
                goto out;
            }
            lcr_list_add_tail(conf, node);
        }
    }
    ret = 0;
out:
    return ret;
}

static inline bool is_root_readonly(const oci_runtime_spec_root *root)
{
    return root != NULL && root->readonly;
}

/* trans oci root rootfsoptions */
static int trans_oci_root_rootfs_options(const oci_runtime_spec_root *root, struct lcr_list *conf,
                                         const oci_runtime_config_linux *linux)
{
    struct lcr_list *node = NULL;
    char *value = NULL;
    char *tmpvalue = NULL;
    int ret = -1;
    int nret;

    if (is_root_readonly(root)) {
L
LiFeng 已提交
782
        value = lcr_util_strdup_s("ro");
O
overweight 已提交
783 784 785 786 787 788 789 790 791 792
    }

    if ((linux != NULL) && linux->rootfs_propagation != NULL) {
        if (value != NULL) {
            size_t newsize;
            if (strlen(value) > (SIZE_MAX - strlen(linux->rootfs_propagation)) - APPEND_COMMA_END_SIZE) {
                ERROR("Out of range!");
                goto out;
            }
            newsize = strlen(linux->rootfs_propagation) + strlen(value) + APPEND_COMMA_END_SIZE;
L
LiFeng 已提交
793
            nret = lcr_mem_realloc((void **)&tmpvalue, newsize, value, strlen(value));
O
overweight 已提交
794 795 796 797 798
            if (nret < 0) {
                ERROR("Out of memory");
                goto out;
            }
            value = tmpvalue;
O
openeuler-iSula 已提交
799 800
            nret = snprintf(value + strlen(value), newsize - strlen(value), ",%s", linux->rootfs_propagation);
            if (nret < 0 || (size_t)nret >= (newsize - strlen(value))) {
O
overweight 已提交
801 802 803 804
                ERROR("Failed to print string");
                goto out;
            }
        } else {
L
LiFeng 已提交
805
            value = lcr_util_strdup_s(linux->rootfs_propagation);
O
overweight 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
        }
    }

    if (value != NULL) {
        node = create_lcr_list_node("lxc.rootfs.options", value);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    free(value);
    return ret;
}

/* trans oci root */
struct lcr_list *trans_oci_root(const oci_runtime_spec_root *root, const oci_runtime_config_linux *linux)
{
    struct lcr_list *conf = NULL;

L
LiFeng 已提交
827
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    if (trans_oci_root_rootfs(root, conf)) {
        goto out_free;
    }

    if (trans_oci_root_rootfs_options(root, conf, linux)) {
        goto out_free;
    }

    return conf;
out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

static inline bool is_mount_options_invalid(const defs_mount *mount)
{
    return mount == NULL || mount->type == NULL;
}

static inline bool is_mount_type_bind(const char *type)
{
    return strcmp(type, "bind") == 0;
}

static inline bool is_mount_type_cgroup(const char *type)
{
    return strcmp(type, "cgroup") == 0;
}

static inline bool is_mount_type_sysfs(const char *type)
{
    return strcmp(type, "sysfs") == 0;
}

static inline bool is_mount_type_proc(const char *type)
{
    return strcmp(type, "proc") == 0;
}

/* trans mount to lxc options */
static char *trans_mount_to_lxc_options(const defs_mount *mount)
{
    char *result = NULL;
    char *prefix = NULL;
    char *lxc_options = NULL;
    int rc;
    bool isdir = true;
    struct stat st;

    if (is_mount_options_invalid(mount)) {
        return NULL;
    }

    if (is_mount_type_bind(mount->type)) {
        rc = stat(mount->source, &st);
        if (rc != 0) {
            ERROR("Failed to get stat of %s", mount->source);
            goto free_out;
        }
        isdir = S_ISDIR(st.st_mode);
    }

    lxc_options = isdir ? ",create=dir" : ",create=file";

L
LiFeng 已提交
899
    prefix = lcr_util_string_join(",", (const char **)mount->options, mount->options_len);
O
overweight 已提交
900
    if (prefix == NULL) {
L
LiFeng 已提交
901
        prefix = lcr_util_strdup_s("defaults");
O
overweight 已提交
902 903
    }

L
LiFeng 已提交
904
    result = lcr_util_string_append(lxc_options, prefix);
O
overweight 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    free(prefix);
    return result;

free_out:
    free(prefix);
    free(result);
    return NULL;
}

static bool get_mount_option_ro(const defs_mount *mount)
{
    bool ro = false;

    if ((mount->options != NULL) && mount->options_len) {
        size_t i = 0;
        for (i = 0; i < mount->options_len; i++) {
            if ((mount->options[i] != NULL) && !strcmp(mount->options[i], "ro")) {
                ro = true;
                break;
            }
        }
    }

    return ro;
}

static char *get_mount_readmode_options(const defs_mount *mount, const char *type)
{
    char *options = NULL;
    bool readonly = get_mount_option_ro(mount);

    if (is_mount_type_cgroup(type)) {
        if (readonly) {
L
LiFeng 已提交
938
            options = lcr_util_strdup_s("ro:force");
O
overweight 已提交
939
        } else {
L
LiFeng 已提交
940
            options = lcr_util_strdup_s("rw:force");
O
overweight 已提交
941 942 943
        }
    } else {
        if (readonly) {
L
LiFeng 已提交
944
            options = lcr_util_strdup_s("ro");
O
overweight 已提交
945
        } else {
L
LiFeng 已提交
946
            options = lcr_util_strdup_s("rw");
O
overweight 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
        }
    }

    return options;
}

/* trans mount auto to lxc */
static struct lcr_list *trans_mount_auto_to_lxc(const defs_mount *mount)
{
    struct lcr_list *node = NULL;
    size_t buf_len = 0;
    char *buf = NULL;
    char *options = NULL;
    int ret;
    char *type = NULL;

    if (is_mount_options_invalid(mount)) {
        ERROR("oci container mounts element(type) is empty");
        return NULL;
    }
    type = mount->type;
    if (is_mount_type_sysfs(type)) {
        type = "sys";
    }

    options = get_mount_readmode_options(mount, type);
    if (options == NULL) {
        ERROR("Failed to trans to lxc options");
        goto out_free;
    }

    buf_len = strlen(type) + strlen(options) + 2;
    buf = calloc(buf_len, 1);
    if (buf == NULL) {
        DEBUG("Out of memory");
        goto out_free;
    }

O
openeuler-iSula 已提交
985 986
    ret = snprintf(buf, buf_len, "%s:%s", type, options);
    if (ret < 0 || (size_t)ret >= buf_len) {
O
overweight 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
        DEBUG("Failed to print string");
        goto out_free;
    }
    node = create_lcr_list_node("lxc.mount.auto", buf);

out_free:
    free(options);
    free(buf);
    return node;
}

/* trans mount entry to lxc */
static struct lcr_list *trans_mount_entry_to_lxc(const defs_mount *mount)
{
    struct lcr_list *node = NULL;
    size_t buf_len = 0;
    char *buf = NULL;
    char *options = NULL;
    char *replaced_dest = NULL;
    int ret;

L
LiFeng 已提交
1008
    char *replaced_source = lcr_util_string_replace(" ", SPACE_MAGIC_STR, mount->source);
O
overweight 已提交
1009 1010 1011 1012
    if (replaced_source == NULL) {
        ERROR("memory allocation error");
        goto err_out;
    }
L
LiFeng 已提交
1013
    replaced_dest = lcr_util_string_replace(" ", SPACE_MAGIC_STR, mount->destination);
O
overweight 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
    if (replaced_dest == NULL) {
        ERROR("memory allocation error");
        free(replaced_source);
        goto err_out;
    }

    options = trans_mount_to_lxc_options(mount);
    if (options == NULL) {
        ERROR("Failed to trans to lxc options");
        goto out_free;
    }

    buf_len = strlen(replaced_dest) + strlen(mount->type) + strlen(replaced_source) + strlen(options) + 8;
    buf = calloc(buf_len, 1);
    if (buf == NULL) {
        ERROR("Out of memory");
        goto out_free;
    }

O
openeuler-iSula 已提交
1033 1034
    ret = snprintf(buf, buf_len, "%s %s %s %s 0 0", replaced_source, replaced_dest + 1, mount->type, options);
    if (ret < 0 || (size_t)ret >= buf_len) {
O
overweight 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        ERROR("Failed to print string");
        goto out_free;
    }
    node = create_lcr_list_node("lxc.mount.entry", buf);

out_free:
    free(options);
    free(buf);
    free(replaced_source);
    free(replaced_dest);
err_out:
    return node;
}

L
LiFeng 已提交
1049
bool is_system_container(const oci_runtime_spec *container)
O
overweight 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
{
    size_t i = 0;
    for (i = 0; container->annotations != NULL && i < container->annotations->len; i++) {
        if (strcmp(container->annotations->keys[i], "system.container") == 0) {
            return true;
        }
    }
    return false;
}

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
static bool is_external_rootfs(const oci_runtime_spec *container)
{
    size_t i = 0;
    for (i = 0; container->annotations != NULL && i < container->annotations->len; i++) {
        if (strcmp(container->annotations->keys[i], "external.rootfs") == 0) {
            return true;
        }
    }
    return false;
}

O
overweight 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
static struct lcr_list *trans_oci_mounts_normal(const defs_mount *tmp)
{
    struct lcr_list *node = NULL;
    if (is_mount_type_cgroup(tmp->type) || is_mount_type_proc(tmp->type) || is_mount_type_sysfs(tmp->type)) {
        node = trans_mount_auto_to_lxc(tmp);
    } else {
        node = trans_mount_entry_to_lxc(tmp);
    }

    return node;
}

static struct lcr_list *trans_oci_mounts_system_container(const defs_mount *tmp)
{
    struct lcr_list *node = NULL;
    if (is_mount_type_cgroup(tmp->type) || (is_mount_type_proc(tmp->source) && is_mount_type_proc(tmp->type)) ||
        is_mount_type_sysfs(tmp->type)) {
        node = trans_mount_auto_to_lxc(tmp);
    } else {
        node = trans_mount_entry_to_lxc(tmp);
    }

    return node;
}

static struct lcr_list *trans_oci_mounts_node(const oci_runtime_spec *c, const defs_mount *tmp)
{
    struct lcr_list *node = NULL;
    // system container
    if (is_system_container(c)) {
        node = trans_oci_mounts_system_container(tmp);
    } else {
        node = trans_oci_mounts_normal(tmp);
    }

    return node;
}

static inline bool is_mount_destination_dev(const char *destination)
{
    return destination != NULL && strcmp(destination, "/dev") == 0;
}

1114
static inline bool should_ignore_dev_mount(const defs_mount *tmp, bool system_container, bool external_rootfs)
O
overweight 已提交
1115
{
1116
    return system_container && external_rootfs && is_mount_destination_dev(tmp->destination);
O
overweight 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
}

/* trans oci mounts */
struct lcr_list *trans_oci_mounts(const oci_runtime_spec *c)
{
    struct lcr_list *conf = NULL;
    struct lcr_list *node = NULL;
    defs_mount *tmp = NULL;
    size_t i;
    bool system_container = is_system_container(c);
1127
    bool external_rootfs = is_external_rootfs(c);
O
overweight 已提交
1128

L
LiFeng 已提交
1129
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    for (i = 0; i < c->mounts_len; i++) {
        tmp = c->mounts[i];
        if (tmp->type == NULL) {
            goto out_free;
        }

1141
        if (should_ignore_dev_mount(tmp, system_container, external_rootfs)) {
O
overweight 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
            continue;
        }
        node = trans_oci_mounts_node(c, tmp);
        if (node == NULL) {
            goto out_free;
        }
        lcr_list_add_tail(conf, node);
    }

    return conf;
out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

static int trans_one_oci_id_mapping(struct lcr_list *conf, const char *typ, const defs_id_mapping *id, const char *path)
{
    int nret;
    struct lcr_list *node = NULL;
    char buf_value[300] = { 0 };
    char subid[ID_MAP_LEN] = { 0 };

O
openeuler-iSula 已提交
1166 1167
    nret = snprintf(buf_value, sizeof(buf_value), "%s %u %u %u", typ, id->container_id, id->host_id, id->size);
    if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176
        return -1;
    }

    node = create_lcr_list_node("lxc.idmap", buf_value);
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);

O
openeuler-iSula 已提交
1177 1178
    nret = snprintf(subid, sizeof(subid), "%u:%u:%u", id->container_id, id->host_id, id->size);
    if (nret < 0 || (size_t)nret >= sizeof(subid)) {
O
overweight 已提交
1179 1180
        return -1;
    }
L
LiFeng 已提交
1181
    nret = lcr_util_atomic_write_file(path, subid);
O
overweight 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    if (nret < 0) {
        return -1;
    }
    return 0;
}

static int trans_oci_uid_mapping(struct lcr_list *conf, defs_id_mapping **uid_mappings, size_t uid_mappings_len)
{
    size_t i;

    for (i = 0; uid_mappings != NULL && i < uid_mappings_len; i++) {
        int nret = trans_one_oci_id_mapping(conf, "u", uid_mappings[i], SUB_UID_PATH);
        if (nret < 0) {
            return nret;
        }
    }
    return 0;
}

static int trans_oci_gid_mapping(struct lcr_list *conf, defs_id_mapping **gid_mappings, size_t gid_mappings_len)
{
    size_t i;

    for (i = 0; gid_mappings != NULL && i < gid_mappings_len; i++) {
        int nret = trans_one_oci_id_mapping(conf, "g", gid_mappings[i], SUB_GID_PATH);
        if (nret < 0) {
            return nret;
        }
    }
    return 0;
}

/* trans oci id mapping */
static struct lcr_list *trans_oci_id_mapping(const oci_runtime_config_linux *l)
{
    struct lcr_list *conf = NULL;
    int nret = 0;

L
LiFeng 已提交
1220
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    nret = trans_oci_uid_mapping(conf, l->uid_mappings, l->uid_mappings_len);
    if (nret < 0) {
        goto out_free;
    }

    nret = trans_oci_gid_mapping(conf, l->gid_mappings, l->gid_mappings_len);
    if (nret < 0) {
        goto out_free;
    }

    return conf;

out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

#define WILDCARD (-1LL)

static int trans_conf_int(struct lcr_list *conf, const char *lxc_key, int val)
{
    struct lcr_list *node = NULL;
    char buf_value[300] = { 0 };
    int nret;

O
openeuler-iSula 已提交
1253 1254
    nret = snprintf(buf_value, sizeof(buf_value), "%d", val);
    if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
        return -1;
    }
    node = create_lcr_list_node(lxc_key, buf_value);
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);
    return 0;
}

static int trans_conf_uint32(struct lcr_list *conf, const char *lxc_key, uint32_t val)
{
    struct lcr_list *node = NULL;
    char buf_value[300] = { 0 };
    int nret;

O
openeuler-iSula 已提交
1271 1272
    nret = snprintf(buf_value, sizeof(buf_value), "%u", (unsigned int)val);
    if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
        return -1;
    }
    node = create_lcr_list_node(lxc_key, buf_value);
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);
    return 0;
}

static int trans_conf_int64(struct lcr_list *conf, const char *lxc_key, int64_t val)
{
    struct lcr_list *node = NULL;
    char buf_value[300] = { 0 };
    int nret;

O
openeuler-iSula 已提交
1289 1290
    nret = snprintf(buf_value, sizeof(buf_value), "%lld", (long long)val);
    if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
        return -1;
    }
    node = create_lcr_list_node(lxc_key, buf_value);
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);
    return 0;
}

static int trans_conf_uint64(struct lcr_list *conf, const char *lxc_key, uint64_t val)
{
    struct lcr_list *node = NULL;
    char buf_value[300] = { 0 };
    int nret;

O
openeuler-iSula 已提交
1307 1308
    nret = snprintf(buf_value, sizeof(buf_value), "%llu", (unsigned long long)val);
    if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
        return -1;
    }
    node = create_lcr_list_node(lxc_key, buf_value);
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);
    return 0;
}

/* trans resources mem swap */
H
haozi007 已提交
1320
static int trans_resources_mem_swap(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
{
    int ret = -1;
    int nret;

    if (res->memory->reservation != INVALID_INT) {
        /* set soft limit of memory usage */
        nret = trans_conf_int64(conf, "lxc.cgroup.memory.soft_limit_in_bytes", res->memory->reservation);
        if (nret < 0) {
            goto out;
        }
    }
    if (res->memory->swap != INVALID_INT) {
        /* set limit of memory+swap usage */
        nret = trans_conf_int64(conf, "lxc.cgroup.memory.memsw.limit_in_bytes", res->memory->swap);
        if (nret < 0) {
            goto out;
        }
    }

L
LiFeng 已提交
1340
    if (res->memory->swappiness != -1) {
O
overweight 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
        /* set swappiness parameter of vmscan */
        nret = trans_conf_uint64(conf, "lxc.cgroup.memory.swappiness", res->memory->swappiness);
        if (nret < 0) {
            goto out;
        }
    }
    ret = 0;
out:
    return ret;
}

H
haozi007 已提交
1352
static int trans_resources_mem_limit(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
{
    if (res->memory->limit != INVALID_INT) {
        /* set limit of memory usage */
        int nret = trans_conf_int64(conf, "lxc.cgroup.memory.limit_in_bytes", res->memory->limit);
        if (nret < 0) {
            return -1;
        }
    }
    return 0;
}

/* trans resources mem kernel */
H
haozi007 已提交
1365
static int trans_resources_mem_kernel(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
{
    int ret = -1;
    int nret;

    if (res->memory->kernel != INVALID_INT) {
        /* set hard limit for kernel memory */
        nret = trans_conf_int64(conf, "lxc.cgroup.memory.kmem.limit_in_bytes", res->memory->kernel);
        if (nret < 0) {
            goto out;
        }
    }
    if (res->memory->kernel_tcp != INVALID_INT) {
        /* set hard limit for tcp buf memory */
        nret = trans_conf_int64(conf, "lxc.cgroup.memory.kmem.tcp.limit_in_bytes", res->memory->kernel_tcp);
        if (nret < 0) {
            goto out;
        }
    }
    ret = 0;
out:
    return ret;
}

H
haozi007 已提交
1389
static int trans_resources_mem_disable_oom(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
{
    struct lcr_list *node = NULL;
    if (res->memory->disable_oom_killer) {
        node = create_lcr_list_node("lxc.cgroup.memory.oom_control", "1");
        if (node == NULL) {
            return -1;
        }
        lcr_list_add_tail(conf, node);
    }
    return 0;
}

/* trans resources memory */
H
haozi007 已提交
1403
static int trans_resources_memory(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
{
    int ret = -1;

    if (res->memory == NULL) {
        return 0;
    }

    if (trans_resources_mem_limit(res, conf) != 0) {
        goto out;
    }

    if (trans_resources_mem_swap(res, conf) != 0) {
        goto out;
    }

    if (trans_resources_mem_kernel(res, conf) != 0) {
        goto out;
    }

    if (trans_resources_mem_disable_oom(res, conf) != 0) {
        goto out;
    }
    ret = 0;
out:
    return ret;
}

H
haozi007 已提交
1431
static int trans_resources_devices_node(const defs_device_cgroup *lrd, struct lcr_list *conf,
O
overweight 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
                                        const char *buf_value)
{
    struct lcr_list *node = NULL;
    int ret = -1;

    if (lrd->allow == true) {
        node = create_lcr_list_node("lxc.cgroup.devices.allow", buf_value);
    } else {
        node = create_lcr_list_node("lxc.cgroup.devices.deny", buf_value);
    }
    if (node == NULL) {
        goto out;
    }
    lcr_list_add_tail(conf, node);

    ret = 0;
out:
    return ret;
}

H
haozi007 已提交
1452
static int trans_resources_devices_no_match(const defs_device_cgroup *lrd, char *buf_value,
O
overweight 已提交
1453 1454 1455 1456
                                            size_t size)
{
    int ret = 0;
    if (lrd->minor != WILDCARD) {
O
openeuler-iSula 已提交
1457 1458
        ret = snprintf(buf_value, size, "%s %lld:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->major),
                       (long long)lrd->minor, lrd->access ? lrd->access : "rwm");
O
overweight 已提交
1459
    } else {
O
openeuler-iSula 已提交
1460 1461
        ret = snprintf(buf_value, size, "%s %lld:* %s", lrd->type ? lrd->type : "a", (long long)(lrd->major),
                       lrd->access ? lrd->access : "rwm");
O
overweight 已提交
1462 1463 1464 1465 1466
    }

    return ret;
}

H
haozi007 已提交
1467
static int trans_resources_devices_match(const defs_device_cgroup *lrd, char *buf_value, size_t size)
O
overweight 已提交
1468 1469 1470
{
    int ret = 0;
    if (lrd->minor != WILDCARD) {
O
openeuler-iSula 已提交
1471 1472
        ret = snprintf(buf_value, size, "%s *:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->minor),
                       lrd->access ? lrd->access : "rwm");
O
overweight 已提交
1473
    } else {
O
openeuler-iSula 已提交
1474
        ret = snprintf(buf_value, size, "%s *:* %s", lrd->type ? lrd->type : "a", lrd->access ? lrd->access : "rwm");
O
overweight 已提交
1475 1476 1477 1478 1479
    }

    return ret;
}

H
haozi007 已提交
1480
static int trans_resources_devices_ret(const defs_device_cgroup *lrd, char *buf_value, size_t size)
O
overweight 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
{
    int ret = 0;
    if (lrd->major != WILDCARD) {
        ret = trans_resources_devices_no_match(lrd, buf_value, size);
    } else {
        ret = trans_resources_devices_match(lrd, buf_value, size);
    }

    return ret;
}

/* trans resources devices */
H
haozi007 已提交
1493
static int trans_resources_devices(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1494 1495 1496 1497 1498 1499
{
    int ret = -1;
    size_t i = 0;
    char buf_value[300] = { 0 };

    for (i = 0; i < res->devices_len; i++) {
H
haozi007 已提交
1500
        defs_device_cgroup *lrd = res->devices[i];
O
overweight 已提交
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
        if (trans_resources_devices_ret(lrd, buf_value, sizeof(buf_value)) < 0) {
            goto out;
        }

        if (trans_resources_devices_node(lrd, conf, buf_value) < 0) {
            goto out;
        }
    }
    ret = 0;
out:
    return ret;
}

/* trans resources cpu cfs */
H
haozi007 已提交
1515
static int trans_resources_cpu_cfs(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
{
    int ret = -1;

    if (res->cpu->quota != INVALID_INT) {
        if (trans_conf_int64(conf, "lxc.cgroup.cpu.cfs_quota_us", res->cpu->quota) < 0) {
            goto out;
        }
    }
    if (res->cpu->period != INVALID_INT) {
        if (trans_conf_uint64(conf, "lxc.cgroup.cpu.cfs_period_us", res->cpu->period) < 0) {
            goto out;
        }
    }
    ret = 0;
out:
    return ret;
}

/* trans resources cpu rt */
H
haozi007 已提交
1535
static int trans_resources_cpu_rt(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
{
    int ret = -1;

    if (res->cpu->realtime_runtime != INVALID_INT) {
        if (trans_conf_int64(conf, "lxc.cgroup.cpu.rt_runtime_us", res->cpu->realtime_runtime) < 0) {
            goto out;
        }
    }
    if (res->cpu->realtime_period != INVALID_INT) {
        if (trans_conf_uint64(conf, "lxc.cgroup.cpu.rt_period_us", res->cpu->realtime_period) < 0) {
            goto out;
        }
    }
    ret = 0;
out:
    return ret;
}

/* trans resources cpu set */
H
haozi007 已提交
1555
static int trans_resources_cpu_set(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
{
    struct lcr_list *node = NULL;
    int ret = -1;

    if (res->cpu->cpus != NULL) {
        node = create_lcr_list_node("lxc.cgroup.cpuset.cpus", res->cpu->cpus);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    if (res->cpu->mems != NULL) {
        node = create_lcr_list_node("lxc.cgroup.cpuset.mems", res->cpu->mems);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }
    ret = 0;
out:
    return ret;
}

/* trans resources cpu shares */
H
haozi007 已提交
1580
static int trans_resources_cpu_shares(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
{
    if (res->cpu->shares != INVALID_INT) {
        int nret = trans_conf_int64(conf, "lxc.cgroup.cpu.shares", (int64_t)(res->cpu->shares));
        if (nret < 0) {
            return -1;
        }
    }
    return 0;
}

/* trans resources cpu */
H
haozi007 已提交
1592
static int trans_resources_cpu(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
{
    int ret = -1;

    if (res->cpu == NULL) {
        return 0;
    }

    if (trans_resources_cpu_cfs(res, conf)) {
        goto out;
    }

    if (trans_resources_cpu_rt(res, conf)) {
        goto out;
    }

    if (trans_resources_cpu_set(res, conf)) {
        goto out;
    }

    if (trans_resources_cpu_shares(res, conf)) {
        goto out;
    }

    ret = 0;

out:
    return ret;
}

/* trans resources blkio weight */
H
haozi007 已提交
1623
static int trans_blkio_weight(const defs_resources_block_io *block_io, struct lcr_list *conf)
O
overweight 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
{
    int ret = -1;

    if (block_io->weight != INVALID_INT) {
        if (trans_conf_int(conf, "lxc.cgroup.blkio.weight", block_io->weight) < 0) {
            goto out;
        }
    }
    if (block_io->leaf_weight != INVALID_INT) {
        if (trans_conf_int(conf, "lxc.cgroup.blkio.leaf_weight", block_io->leaf_weight) < 0) {
            goto out;
        }
    }
    ret = 0;

out:
    return ret;
}

/* trans resources blkio wdevice */
H
haozi007 已提交
1644
static int trans_blkio_wdevice(const defs_resources_block_io *block_io, struct lcr_list *conf)
O
overweight 已提交
1645 1646 1647 1648 1649 1650 1651 1652
{
    struct lcr_list *node = NULL;
    int ret = -1;
    size_t i = 0;
    char buf_value[300] = { 0 };

    for (i = 0; i < block_io->weight_device_len; i++) {
        int nret;
H
haozi007 已提交
1653
        defs_block_io_device_weight *wd = block_io->weight_device[i];
O
overweight 已提交
1654
        if ((wd != NULL) && wd->weight != INVALID_INT) {
O
openeuler-iSula 已提交
1655 1656 1657
            nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), (long long)wd->minor,
                            wd->weight);
            if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
                goto out;
            }

            node = create_lcr_list_node("lxc.cgroup.blkio.weight_device", buf_value);
            if (node == NULL) {
                goto out;
            }
            lcr_list_add_tail(conf, node);
        }
        if ((wd != NULL) && wd->leaf_weight != INVALID_INT) {
O
openeuler-iSula 已提交
1668 1669 1670
            nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major),
                            (long long)(wd->minor), wd->leaf_weight);
            if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
                goto out;
            }

            node = create_lcr_list_node("lxc.cgroup.blkio.leaf_weight_device", buf_value);
            if (node == NULL) {
                goto out;
            }
            lcr_list_add_tail(conf, node);
        }
    }
    ret = 0;
out:
    return ret;
}

/* trans resources blkio throttle */
H
haozi007 已提交
1687
static int trans_blkio_throttle(defs_block_io_device_throttle **throttle, size_t len,
O
overweight 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
                                const char *lxc_key, struct lcr_list *conf)
{
    struct lcr_list *node = NULL;
    int ret = -1;
    size_t i;

    if ((throttle == NULL) || len == 0) {
        return 0;
    }

    for (i = 0; i < len; i++) {
        if (throttle[i] && throttle[i]->rate != INVALID_INT) {
            int nret;
            char buf_value[300] = { 0x00 };
O
openeuler-iSula 已提交
1702 1703 1704
            nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %llu", (long long)throttle[i]->major,
                            (long long)(throttle[i]->minor), (unsigned long long)(throttle[i]->rate));
            if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
                goto out;
            }

            node = create_lcr_list_node(lxc_key, buf_value);
            if (node == NULL) {
                goto out;
            }
            lcr_list_add_tail(conf, node);
        }
    }
    ret = 0;
out:
    return ret;
}

/* trans resources blkio */
H
haozi007 已提交
1721
static int trans_resources_blkio(const defs_resources_block_io *block_io, struct lcr_list *conf)
O
overweight 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
{
    int ret = -1;

    if (block_io == NULL) {
        return 0;
    }

    if (trans_blkio_weight(block_io, conf)) {
        goto out;
    }

    if (trans_blkio_wdevice(block_io, conf)) {
        goto out;
    }

    if (trans_blkio_throttle(block_io->throttle_read_bps_device, block_io->throttle_read_bps_device_len,
                             "lxc.cgroup.blkio.throttle.read_bps_device", conf)) {
        goto out;
    }

    if (trans_blkio_throttle(block_io->throttle_write_bps_device, block_io->throttle_write_bps_device_len,
                             "lxc.cgroup.blkio.throttle.write_bps_device", conf)) {
        goto out;
    }

    if (trans_blkio_throttle(block_io->throttle_read_iops_device, block_io->throttle_read_iops_device_len,
                             "lxc.cgroup.blkio.throttle.read_iops_device", conf)) {
        goto out;
    }

    if (trans_blkio_throttle(block_io->throttle_write_iops_device, block_io->throttle_write_iops_device_len,
                             "lxc.cgroup.blkio.throttle.write_iops_device", conf)) {
        goto out;
    }

    ret = 0;
out:
    return ret;
}

/* trans resources hugetlb */
H
haozi007 已提交
1763
static int trans_resources_hugetlb(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1764 1765 1766 1767 1768 1769
{
    int ret = -1;
    size_t i = 0;
    char buf_key[300] = { 0 };

    for (i = 0; i < res->hugepage_limits_len; i++) {
H
haozi007 已提交
1770
        defs_resources_hugepage_limits_element *lrhl = res->hugepage_limits[i];
O
overweight 已提交
1771
        if (lrhl->page_size != NULL) {
O
openeuler-iSula 已提交
1772 1773
            int nret = snprintf(buf_key, sizeof(buf_key), "lxc.cgroup.hugetlb.%s.limit_in_bytes", lrhl->page_size);
            if (nret < 0 || (size_t)nret >= sizeof(buf_key)) {
O
overweight 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
                goto out;
            }

            if (trans_conf_uint64(conf, buf_key, lrhl->limit) < 0) {
                return -1;
            }
        }
    }

    ret = 0;
out:
    return ret;
}

/* trans resources network */
H
haozi007 已提交
1789
static int trans_resources_network(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
{
    int ret = -1;
    size_t i = 0;
    char buf_value[300] = { 0 };

    if (!res->network) {
        return 0;
    }

    if (res->network->class_id != INVALID_INT) {
        if (trans_conf_uint32(conf, "lxc.cgroup.net_cls.classid", res->network->class_id) < 0) {
            return -1;
        }
    }

    for (i = 0; i < res->network->priorities_len; i++) {
H
haozi007 已提交
1806
        defs_network_interface_priority *lrnp = res->network->priorities[i];
O
overweight 已提交
1807
        if ((lrnp != NULL) && lrnp->name != NULL && lrnp->priority != INVALID_INT) {
O
openeuler-iSula 已提交
1808 1809
            int nret = snprintf(buf_value, sizeof(buf_value), "%s %u", lrnp->name, lrnp->priority);
            if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
                goto out;
            }

            struct lcr_list *node = create_lcr_list_node("lxc.cgroup.net_prio.ifpriomap", buf_value);
            if (node == NULL) {
                goto out;
            }
            lcr_list_add_tail(conf, node);
        }
    }

    ret = 0;
out:
    return ret;
}

/* trans resources pids */
H
haozi007 已提交
1827
static int trans_resources_pids(const defs_resources *res, struct lcr_list *conf)
O
overweight 已提交
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
{
    int ret = -1;
    char buf_value[300] = { 0 };

    if (res->pids == NULL) {
        return 0;
    }

    if (res->pids->limit != INVALID_INT) {
        int nret;
        if (res->pids->limit == -1) {
O
openeuler-iSula 已提交
1839
            nret = snprintf(buf_value, sizeof(buf_value), "max");
O
overweight 已提交
1840
        } else {
O
openeuler-iSula 已提交
1841
            nret = snprintf(buf_value, sizeof(buf_value), "%lld", (long long)(res->pids->limit));
O
overweight 已提交
1842
        }
O
openeuler-iSula 已提交
1843
        if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
            goto out;
        }

        struct lcr_list *node = create_lcr_list_node("lxc.cgroup.pids.max", buf_value);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }

    ret = 0;
out:
    return ret;
}

/* trans oci resources */
H
haozi007 已提交
1860
static struct lcr_list *trans_oci_resources(const defs_resources *res)
O
overweight 已提交
1861 1862 1863
{
    struct lcr_list *conf = NULL;

L
LiFeng 已提交
1864
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    if (trans_resources_devices(res, conf)) {
        goto out_free;
    }

    if (trans_resources_memory(res, conf)) {
        goto out_free;
    }

    if (trans_resources_cpu(res, conf)) {
        goto out_free;
    }

    if (trans_resources_blkio(res->block_io, conf)) {
        goto out_free;
    }

    if (trans_resources_hugetlb(res, conf)) {
        goto out_free;
    }

    if (trans_resources_network(res, conf)) {
        goto out_free;
    }

    if (trans_resources_pids(res, conf)) {
        goto out_free;
    }

    return conf;

out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

struct namespace_map_def {
    char *ns_name;
    char *lxc_name;
};

static char *trans_oci_namespace_to_lxc(const char *typ)
{
    struct namespace_map_def namespaces_map[] = {
        { "pid", "lxc.namespace.share.pid" },       { "network", "lxc.namespace.share.net" },
        { "ipc", "lxc.namespace.share.ipc" },       { "uts", "lxc.namespace.share.uts" },
        { "mount", "lxc.namespace.share.mnt" },     { "user", "lxc.namespace.share.user" },
        { "cgroup", "lxc.namespace.share.cgroup" }, { NULL, NULL }
    };
    const struct namespace_map_def *p = NULL;

    for (p = namespaces_map; p != NULL && p->ns_name != NULL; p++) {
        if (strcmp(typ, p->ns_name) == 0) {
L
LiFeng 已提交
1924
            return lcr_util_strdup_s(p->lxc_name);
O
overweight 已提交
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
        }
    }
    return NULL;
}

/* trans oci namespaces */
static struct lcr_list *trans_oci_namespaces(const oci_runtime_config_linux *l)
{
    struct lcr_list *conf = NULL;
    struct lcr_list *node = NULL;
    size_t i;
H
haozi007 已提交
1936
    defs_namespace_reference *ns = NULL;
O
overweight 已提交
1937

L
LiFeng 已提交
1938
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    for (i = 0; i < l->namespaces_len; i++) {
        char *ns_name = NULL;
        ns = l->namespaces[i];

        if (ns->type == NULL || ns->path == NULL) {
            continue;
        }

        ns_name = trans_oci_namespace_to_lxc(ns->type);
        if (ns_name == NULL) {
            continue;
        }

        node = create_lcr_list_node(ns_name, ns->path);
        free(ns_name);
        if (node == NULL) {
            goto out_free;
        }
        lcr_list_add_tail(conf, node);
    }

    return conf;

out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

/* trans oci mask ro paths */
static struct lcr_list *trans_oci_mask_ro_paths(const oci_runtime_config_linux *l)
{
    struct lcr_list *conf = NULL;
    struct lcr_list *node = NULL;
    size_t i;
    char *path = NULL;

L
LiFeng 已提交
1982
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    for (i = 0; i < l->masked_paths_len; i++) {
        path = l->masked_paths[i];
        if (path == NULL) {
            continue;
        }
        node = create_lcr_list_node("lxc.isulad.rootfs.maskedpaths", path);
        if (node == NULL) {
            goto out_free;
        }
        lcr_list_add_tail(conf, node);
    }

    for (i = 0; i < l->readonly_paths_len; i++) {
        path = l->readonly_paths[i];
        if (path == NULL) {
            continue;
        }
        node = create_lcr_list_node("lxc.isulad.rootfs.ropaths", path);
        if (node == NULL) {
            goto out_free;
        }
        lcr_list_add_tail(conf, node);
    }

    return conf;

out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

#define POPULATE_DEVICE_SIZE (300 + PATH_MAX)
/* trans oci linux devices */
static struct lcr_list *trans_oci_linux_devices(const oci_runtime_config_linux *l)
{
    struct lcr_list *conf = NULL;
    struct lcr_list *node = NULL;
    size_t i = 0;
    int nret = 0;
H
haozi007 已提交
2029
    defs_device *device = NULL;
O
overweight 已提交
2030 2031
    char buf_value[POPULATE_DEVICE_SIZE] = { 0 };

L
LiFeng 已提交
2032
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    for (i = 0; i < l->devices_len; i++) {
        device = l->devices[i];

        if (device->type == NULL || device->path == NULL) {
            continue;
        }

        /* lxc.populate_device = PATH_IN_CONTAINER:DEVICETYPE:MAJOR:MINOR:MODE:UID:GID
         * For e.g. lxc.populate_device = /dev/sda:b:8:0:0666:0:0
         */
O
openeuler-iSula 已提交
2048 2049 2050 2051
        nret = snprintf(buf_value, sizeof(buf_value), "%s:%s:%lld:%lld:%d:%u:%u", device->path, device->type,
                        (long long int)(device->major), (long long int)(device->minor), device->file_mode, device->uid,
                        device->gid);
        if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
O
overweight 已提交
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
            ERROR("Failed to get populate device string");
            goto out_free;
        }

        node = create_lcr_list_node("lxc.isulad.populate.device", buf_value);
        if (node == NULL) {
            goto out_free;
        }
        lcr_list_add_tail(conf, node);
    }

    return conf;

out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

static inline bool is_seccomp_action_kill(const char *value)
{
    return strcmp(value, "SCMP_ACT_KILL") == 0;
}

static inline bool is_seccomp_action_trap(const char *value)
{
    return strcmp(value, "SCMP_ACT_TRAP") == 0;
}

static inline bool is_seccomp_action_allow(const char *value)
{
    return strcmp(value, "SCMP_ACT_ALLOW") == 0;
}

static inline bool is_seccomp_action_trace(const char *value)
{
    return strcmp(value, "SCMP_ACT_TRACE") == 0;
}

static inline bool is_seccomp_action_errno(const char *value)
{
    return strcmp(value, "SCMP_ACT_ERRNO") == 0;
}

/* seccomp trans action */
static char *seccomp_trans_action(const char *action)
{
    if (is_seccomp_action_kill(action)) {
L
LiFeng 已提交
2101
        return lcr_util_strdup_s("kill");
O
overweight 已提交
2102
    } else if (is_seccomp_action_trap(action)) {
L
LiFeng 已提交
2103
        return lcr_util_strdup_s("trap");
O
overweight 已提交
2104
    } else if (is_seccomp_action_allow(action)) {
L
LiFeng 已提交
2105
        return lcr_util_strdup_s("allow");
O
overweight 已提交
2106
    } else if (is_seccomp_action_trace(action)) {
L
LiFeng 已提交
2107
        return lcr_util_strdup_s("trace 1");
O
overweight 已提交
2108
    } else if (is_seccomp_action_errno(action)) {
L
LiFeng 已提交
2109
        return lcr_util_strdup_s("errno 1");
O
overweight 已提交
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
    }

    return NULL;
}

static bool is_action_allow(const char *value)
{
    return strcmp(value, "allow") == 0;
}

#define DEFAULT_ACTION_OFFSET 12
/* seccomp append head info */
static int seccomp_append_head_info(const char *action, Buffer *buffer)
{
    int ret = 0;
    char *default_action = NULL;

    if (action == NULL) {
        return -1;
    }

    default_action = seccomp_trans_action(action);
    if (default_action == NULL) {
        ERROR("Failed to translate seccomp action");
        return -1;
    }

    if (is_action_allow(default_action)) {
        ret = buffer_nappendf(buffer, strlen(default_action) + DEFAULT_ACTION_OFFSET, "blacklist %s\n", default_action);
    } else {
        ret = buffer_nappendf(buffer, strlen(default_action) + DEFAULT_ACTION_OFFSET, "whitelist %s\n", default_action);
    }
    if (ret != 0) {
        ERROR("Failed to append seccomp config head info\n");
        ret = -1;
        goto out;
    }

out:
    free(default_action);
    return ret;
}

/* get hostarch */
static char *get_hostarch(void)
{
    struct utsname uts;
    size_t len;
    size_t i;
    /* no x32 kernels */
    lcr_host_arch arch_type[] = {
        { "i686", "[x86]", 4 },      { "x32", "[x32]", 3 },           { "x86_64", "[x86_64]", 6 },
        { "armv7", "[arm]", 5 },     { "aarch64", "[arm64]", 7 },     { "ppc64le", "[ppc64le]", 7 },
        { "ppc64", "[ppc64]", 5 },   { "ppc", "[ppc]", 3 },           { "mips64n32", "[mips64n32]", 9 },
        { "mips64", "[mips64]", 6 }, { "mips", "[mips]", 4 },         { "s390x", "[s390x]", 5 },
        { "s390", "[s390]", 4 },     { "parisc64", "[parisc64]", 8 }, { "parisc", "[parisc]", 6 },
    };

    if (uname(&uts) < 0) {
        SYSERROR("Failed to read host arch");
        return NULL;
    }
    len = sizeof(arch_type) / sizeof(lcr_host_arch);
    for (i = 0; i < len; i++) {
        if (i == 0 || i == 1 || i == 2) {
            if (strcmp(uts.machine, arch_type[i].arch) == 0) {
L
LiFeng 已提交
2176
                return lcr_util_strdup_s(arch_type[i].value);
O
overweight 已提交
2177 2178 2179
            }
        } else {
            if (strncmp(uts.machine, arch_type[i].arch, (size_t)(arch_type[i].num)) == 0) {
L
LiFeng 已提交
2180
                return lcr_util_strdup_s(arch_type[i].value);
O
overweight 已提交
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
            }
        }
    }

    ERROR("Failed to get machine type");
    return NULL;
}

/* seccomp trans arch */
static char *seccomp_trans_arch(const char *arch)
{
    lcr_arch_value arch_type[] = {
        { "SCMP_ARCH_X86", "[x86]" },
        { "SCMP_ARCH_X86_64", "[x86_64]" },
        { "SCMP_ARCH_X32", "[x32]" },
        { "SCMP_ARCH_ARM", "[arm]" },
        { "SCMP_ARCH_AARCH64", "[arm64]" },
        { "SCMP_ARCH_MIPS", "[mips]" },
        { "SCMP_ARCH_MIPS64", "[mips64]" },
        { "SCMP_ARCH_MIPS64N32", "[mips64n32]" },
        { "SCMP_ARCH_MIPSEL", "[mipsel]" },
        { "SCMP_ARCH_MIPSEL64", "[mipsel64]" },
        { "SCMP_ARCH_MIPSEL64N32", "[mipsel64n32]" },
        { "SCMP_ARCH_PPC", "[ppc]" },
        { "SCMP_ARCH_PPC64", "[ppc64]" },
        { "SCMP_ARCH_PPC64LE", "[ppc64le]" },
        { "SCMP_ARCH_S390", "[s390]" },
        { "SCMP_ARCH_S390X", "[s390x]" },
        { "SCMP_ARCH_PARISC", "[parisc]" },
        { "SCMP_ARCH_PARISC64", "[parisc64]" },
        { "SCMP_ARCH_ALL", "[all]" },
        { "SCMP_ARCH_AUTO", "" },
    };
    size_t len;
    size_t i = 0;
    len = sizeof(arch_type) / sizeof(lcr_arch_value);
    for (i = 0; i < len; i++) {
        if (strcmp(arch_type[i].arch, "SCMP_ARCH_AUTO") == 0) {
            return get_hostarch();
        } else if (strcmp(arch, arch_type[i].arch) == 0) {
L
LiFeng 已提交
2221
            return lcr_util_strdup_s(arch_type[i].value);
O
overweight 已提交
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
        }
    }
    return NULL;
}

/* seccomp append arch */
static int seccomp_append_arch(char *arch, Buffer *buffer)
{
    int ret = 0;
    char *trans_arch = NULL;

    if (arch == NULL) {
        return -1;
    }

    trans_arch = seccomp_trans_arch(arch);
    if (trans_arch == NULL) {
        ERROR("Failed to translate seccomp arch: %s", arch);
        return -1;
    }

    if (buffer_nappendf(buffer, strlen(trans_arch) + 2, "%s\n", trans_arch)) {
        ERROR("Failed to append seccomp config head info\n");
        ret = -1;
    }

    free(trans_arch);
    return ret;
}

/* seccomp append rule */
H
haozi007 已提交
2253
static int seccomp_append_rule(const defs_syscall *syscall, size_t i, Buffer *buffer, char *action)
O
overweight 已提交
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
{
    int ret = 0;
    size_t j = 0;

    if (syscall->names[i] == NULL) {
        ERROR("Failed to get syscall name");
        ret = -1;
        goto out;
    }
    if (buffer_nappendf(buffer, strlen(syscall->names[i]) + strlen(action) + 2, "%s %s", syscall->names[i], action)) {
        ERROR("Failed to append syscall name and action\n");
        ret = -1;
        goto out;
    }

    for (j = 0; j < syscall->args_len; j++) {
        if ((syscall->args[j] == NULL) || (syscall->args[j]->op == NULL)) {
            ERROR("Failed to get syscall args");
            ret = -1;
            goto out;
        }
        if (buffer_nappendf(buffer, 20 * 3 + strlen(syscall->args[j]->op), " [%u,%llu,%s,%llu]",
                            syscall->args[j]->index, syscall->args[j]->value, syscall->args[j]->op,
                            syscall->args[j]->value_two)) {
            ERROR("Failed to append syscall rules\n");
            ret = -1;
            goto out;
        }
    }

    if (buffer_nappendf(buffer, 2, "\n")) {
        ERROR("Failed to append newline\n");
        ret = -1;
        goto out;
    }
out:
    return ret;
}

/* seccomp append rules */
H
haozi007 已提交
2294
static int seccomp_append_rules(const defs_syscall *syscall, Buffer *buffer)
O
overweight 已提交
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
{
    int ret = 0;
    size_t i = 0;
    char *action = NULL;

    if (syscall == NULL) {
        return -1;
    }

    if ((syscall->action == NULL) || syscall->names_len == 0) {
        return -1;
    }

    action = seccomp_trans_action(syscall->action);
    if (action == NULL) {
        ERROR("Failed to translate action");
        ret = -1;
        goto out;
    }

    for (i = 0; i < syscall->names_len; i++) {
        if (seccomp_append_rule(syscall, i, buffer, action)) {
            ret = -1;
            goto out;
        }
    }
out:
    free(action);
    return ret;
}

static struct lcr_list *trans_oci_linux_sysctl(const json_map_string_string *sysctl)
{
    struct lcr_list *conf = NULL;
    struct lcr_list *node = NULL;
    size_t i;

L
LiFeng 已提交
2332
    conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
2333 2334 2335 2336 2337 2338 2339
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    for (i = 0; i < sysctl->len; i++) {
        char sysk[BUFSIZ] = { 0 };
O
openeuler-iSula 已提交
2340 2341
        int nret = snprintf(sysk, sizeof(sysk), "lxc.sysctl.%s", sysctl->keys[i]);
        if (nret < 0 || (size_t)nret >= sizeof(sysk)) {
O
overweight 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
            ERROR("Failed to print string");
            goto out_free;
        }
        node = create_lcr_list_node(sysk, sysctl->values[i]);
        if (node == NULL) {
            goto out_free;
        }
        lcr_list_add_tail(conf, node);
    }

    return conf;

out_free:
    lcr_free_config(conf);
    free(conf);
    return NULL;
}

static int append_seccomp_with_archs(const oci_runtime_config_linux_seccomp *seccomp, Buffer *buffer)
{
    int ret = 0;
    size_t i = 0;
    size_t j = 0;

    for (i = 0; i < seccomp->architectures_len; i++) {
        if (seccomp_append_arch(seccomp->architectures[i], buffer)) {
            ret = -1;
            goto out;
        }
D
dogsheng 已提交
2371
        /* append rules */
O
overweight 已提交
2372 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 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
        for (j = 0; j < seccomp->syscalls_len; j++) {
            if (seccomp_append_rules(seccomp->syscalls[j], buffer)) {
                ret = -1;
                goto out;
            }
        }
    }
out:
    return ret;
}

/* lxc seccomp conf format:
	To support limit syscall arguments, extend the version 2 file to the following format:

	syscall_name action [index,value,op,valueTwo] [index,value,op]...
	for one arguments, [index,value,valueTwo,op]

	index: the index for syscall arguments (type uint)

	value: the value for syscall arguments (type uint64)

	op: the operator for syscall arguments(string), a valid list of constants as of libseccomp v2.3.2 is
	SCMP_CMP_NE,SCMP_CMP_LE,SCMP_CMP_LE, SCMP_CMP_EQ, SCMP_CMP_GE,
	SCMP_CMP_GT, SCMP_CMP_MASKED_EQ, or !=,<=,==,>=,>,&=

	valueTwo: the value for syscall arguments only used for mask eq (type uint64, optional)

For example:

	2
	blacklist allow
	reject_force_umount  # comment this to allow umount -f;  not recommended
	[all]
	kexec_load errno 1 [0,1,SCMP_CMP_LE][3,1,==][5,1,SCMP_CMP_MASKED_EQ,1]
	open_by_handle_at errno 1
	init_module errno 1
	finit_module errno 1
	delete_module errno 1

*/
static int trans_oci_seccomp(const oci_runtime_config_linux_seccomp *seccomp, char **seccomp_conf)
{
    int ret = 0;
    size_t j = 0;
    size_t init_size = 4 * SIZE_KB;

    Buffer *buffer = buffer_alloc(init_size);
    if (buffer == NULL) {
        ERROR("Failed to malloc output_buffer\n");
        return -1;
    }

    /* config version */
    if (buffer_nappendf(buffer, 3, "2\n")) {
        ERROR("Failed to append seccomp config version\n");
        ret = -1;
        goto out_free;
    }

    /* append head info */
    if (seccomp_append_head_info(seccomp->default_action, buffer)) {
        ret = -1;
        goto out_free;
    }

    /* append architectures */
    if (seccomp->architectures_len != 0) {
        ret = append_seccomp_with_archs(seccomp, buffer);
        if (ret != 0) {
            goto out_free;
        }
    } else {
        // add rules directly(eg: blacklist)
        for (j = 0; j < seccomp->syscalls_len; j++) {
            if (seccomp_append_rules(seccomp->syscalls[j], buffer)) {
                ret = -1;
                goto out_free;
            }
        }
    }
    *seccomp_conf = buffer_to_s(buffer);
    if (*seccomp_conf == NULL) {
        ret = -1;
        goto out_free;
    }

out_free:
    buffer_free(buffer);
    return ret;
}

2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
static int trans_oci_file_selinux(const oci_runtime_config_linux *l, struct lcr_list *conf)
{
    struct lcr_list *node = NULL;
    int ret = -1;

    if (l->mount_label != NULL) {
        node = create_lcr_list_node("lxc.selinux.mount_context", l->mount_label);
        if (node == NULL) {
            goto out;
        }
        lcr_list_add_tail(conf, node);
    }

    ret = 0;

out:
    return ret;
}

O
overweight 已提交
2482 2483 2484 2485 2486 2487
/* trans oci linux */
struct lcr_list *trans_oci_linux(const oci_runtime_config_linux *l, char **seccomp_conf)
{
    int ret = 0;
    struct lcr_list *tmp = NULL;

L
LiFeng 已提交
2488
    struct lcr_list *conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    // UID/GID Mapping
    tmp = trans_oci_id_mapping(l);
    if (tmp == NULL) {
        goto out_free;
    }
    lcr_list_merge(conf, tmp);

    // Resources
    if (l->resources != NULL) {
        tmp = trans_oci_resources(l->resources);
        if (tmp == NULL) {
            goto out_free;
        }
        lcr_list_merge(conf, tmp);
    }

    // linux devices
    tmp = trans_oci_linux_devices(l);
    if (tmp == NULL) {
        goto out_free;
    }
    lcr_list_merge(conf, tmp);

    // Namespaces
    tmp = trans_oci_namespaces(l);
    if (tmp == NULL) {
        goto out_free;
    }
    lcr_list_merge(conf, tmp);

    // MaskedPaths and ReadonlyPaths
    tmp = trans_oci_mask_ro_paths(l);
    if (tmp == NULL) {
        goto out_free;
    }
    lcr_list_merge(conf, tmp);

    // sysctl
    if (l->sysctl != NULL && l->uid_mappings == NULL && l->gid_mappings == NULL) {
        tmp = trans_oci_linux_sysctl(l->sysctl);
        if (tmp == NULL) {
            goto out_free;
        }
        lcr_list_merge(conf, tmp);
    }

    // seccomp
    if (l->seccomp != NULL && seccomp_conf != NULL) {
        ret = trans_oci_seccomp(l->seccomp, seccomp_conf);
2543
        if (ret != 0) {
O
overweight 已提交
2544 2545 2546 2547
            goto out_free;
        }
    }

2548 2549 2550 2551 2552 2553
    // selinux mount label
    ret = trans_oci_file_selinux(l, conf);
    if (ret != 0) {
        goto out_free;
    }

O
overweight 已提交
2554
    return conf;
2555

O
overweight 已提交
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

/* trans annotations */
struct lcr_list *trans_annotations(const json_map_string_string *anno)
{
    size_t i, j;
    size_t len;
    int ret = 0;

L
LiFeng 已提交
2570
    struct lcr_list *conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    len = sizeof(g_require_annotations) / sizeof(lcr_annotation_item_t);

    // trans annotations
    for (i = 0; i < anno->len; i++) {
        if (anno->keys[i] == NULL) {
            continue;
        }
        for (j = 0; j < len; j++) {
            if (strcmp(anno->keys[i], g_require_annotations[j].name) != 0) {
                continue;
            }

            ret = g_require_annotations[j].checker(anno->values[i]);
            if (ret == -1) {
                ERROR("item: %s, value: %s, checker failed", anno->keys[i], anno->values[i]);
                goto out_free;
            } else if (ret == 1) {
                DEBUG("Skip this config item: %s", anno->keys[i]);
                continue;
            }

            struct lcr_list *node = create_lcr_list_node(g_require_annotations[j].lxc_item_name, anno->values[i]);
            if (node == NULL) {
                goto out_free;
            }
            lcr_list_add_tail(conf, node);
            break;
        }
    }

    return conf;
out_free:
    lcr_free_config(conf);
    free(conf);

    return NULL;
}

static int add_needed_pty_conf(struct lcr_list *conf)
{
    struct lcr_list *node = create_lcr_list_node("lxc.pty.max", "1024");
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);

    return 0;
}

static int add_needed_net_conf(struct lcr_list *conf)
{
    struct lcr_list *node = create_lcr_list_node("lxc.net.0.type", "empty");
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);

    node = create_lcr_list_node("lxc.net.0.flags", "up");
    if (node == NULL) {
        return -1;
    }
    lcr_list_add_tail(conf, node);
    return 0;
}

/* get needed lxc conf */
struct lcr_list *get_needed_lxc_conf()
{
L
LiFeng 已提交
2644
    struct lcr_list *conf = lcr_util_common_calloc_s(sizeof(*conf));
O
overweight 已提交
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
    if (conf == NULL) {
        return NULL;
    }
    lcr_list_init(conf);

    if (add_needed_pty_conf(conf) < 0) {
        goto out_free;
    }
    if (add_needed_net_conf(conf) < 0) {
        goto out_free;
    }

    return conf;
out_free:
    lcr_free_config(conf);
    free(conf);
    return NULL;
}