specs.c 57.5 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
 * iSulad licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide container specs functions
 ******************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/utsname.h>
#include <sched.h>
#include <ctype.h>

#include "error.h"
#include "log.h"
#include "specs.h"
#include "oci_runtime_spec.h"
#include "oci_runtime_hooks.h"
#include "docker_seccomp.h"
#include "host_config.h"
#include "utils.h"
#include "config.h"
L
LiuHao 已提交
40
#include "isulad_config.h"
O
overweight 已提交
41 42 43 44 45 46
#include "namespace.h"
#include "specs_security.h"
#include "specs_mount.h"
#include "specs_extend.h"
#include "image.h"
#include "path.h"
47
#include "constants.h"
W
wujing 已提交
48
#include "selinux_label.h"
O
overweight 已提交
49 50 51 52 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

#ifndef CLONE_NEWUTS
#define CLONE_NEWUTS            0x04000000
#endif

#ifndef CLONE_NEWUSER
#define CLONE_NEWUSER           0x10000000
#endif

#ifndef CLONE_NEWNET
#define CLONE_NEWNET            0x40000000
#endif

#ifndef CLONE_NEWNS
#define CLONE_NEWNS             0x00020000
#endif

#ifndef CLONE_NEWPID
#define CLONE_NEWPID            0x20000000
#endif

#ifndef CLONE_NEWIPC
#define CLONE_NEWIPC            0x08000000
#endif

#ifndef CLONE_NEWCGROUP
#define CLONE_NEWCGROUP         0x02000000
#endif

78 79 80 81 82 83 84 85 86 87
static int make_sure_oci_spec_annotations(oci_runtime_spec *oci_spec)
{
    if (oci_spec->annotations == NULL) {
        oci_spec->annotations = util_common_calloc_s(sizeof(json_map_string_string));
        if (oci_spec->annotations == NULL) {
            return -1;
        }
    }
    return 0;
}
O
overweight 已提交
88

89
static int merge_annotations(oci_runtime_spec *oci_spec, const container_config *container_spec)
O
overweight 已提交
90 91 92 93
{
    int ret = 0;
    size_t i;

94 95 96 97 98 99 100
    ret = make_sure_oci_spec_annotations(oci_spec);
    if (ret < 0) {
        goto out;
    }

    if (container_spec->annotations != NULL && container_spec->annotations->len) {
        if (oci_spec->annotations->len > LIST_SIZE_MAX - container_spec->annotations->len) {
O
overweight 已提交
101
            ERROR("Too many annotations to add, the limit is %d", LIST_SIZE_MAX);
L
LiuHao 已提交
102
            isulad_set_error_message("Too many annotations to add, the limit is %d", LIST_SIZE_MAX);
O
overweight 已提交
103 104 105
            ret = -1;
            goto out;
        }
106 107 108
        for (i = 0; i < container_spec->annotations->len; i++) {
            ret = append_json_map_string_string(oci_spec->annotations, container_spec->annotations->keys[i],
                                                container_spec->annotations->values[i]);
O
overweight 已提交
109
            if (ret != 0) {
110 111
                ERROR("Failed to append annotation:%s, value:%s", container_spec->annotations->keys[i],
                      container_spec->annotations->values[i]);
O
overweight 已提交
112 113 114 115 116 117 118 119
                goto out;
            }
        }
    }
out:
    return ret;
}

120
static int make_annotations_log_console(const container_config *container_spec)
O
overweight 已提交
121 122 123
{
    int ret = 0;
    int nret = 0;
L
LiuHao 已提交
124
    char tmp_str[ISULAD_NUMSTRLEN64] = {0};
O
overweight 已提交
125

126 127 128 129
    if (container_spec->log_config != NULL) {
        if (container_spec->log_config->log_file != NULL) {
            if (append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_FILE,
                                              container_spec->log_config->log_file)) {
O
overweight 已提交
130 131 132 133 134 135
                ERROR("append log console file failed");
                ret = -1;
                goto out;
            }
        }

O
openeuler-iSula 已提交
136
        nret = snprintf(tmp_str, sizeof(tmp_str), "%llu",
137
                        (unsigned long long)(container_spec->log_config->log_file_rotate));
O
openeuler-iSula 已提交
138
        if (nret < 0 || (size_t)nret >= sizeof(tmp_str)) {
O
overweight 已提交
139 140 141 142 143
            ERROR("create rotate string failed");
            ret = -1;
            goto out;
        }

144
        if (append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_ROTATE, tmp_str)) {
O
overweight 已提交
145 146 147 148 149
            ERROR("append log console file rotate failed");
            ret = -1;
            goto out;
        }

150 151 152
        if (container_spec->log_config->log_file_size != NULL) {
            if (append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_SIZE,
                                              container_spec->log_config->log_file_size)) {
O
overweight 已提交
153 154 155 156 157 158 159 160 161 162 163
                ERROR("append log console file size failed");
                ret = -1;
                goto out;
            }
        }
    }

out:
    return ret;
}

164
static int make_annotations_network_mode(const container_config *container_spec, const host_config *host_spec)
O
overweight 已提交
165 166 167 168
{
    int ret = 0;

    if (host_spec->network_mode != NULL) {
169
        if (append_json_map_string_string(container_spec->annotations, "host.network.mode", host_spec->network_mode)) {
O
overweight 已提交
170 171 172 173 174 175 176 177 178 179
            ERROR("append network mode failed");
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

180
static int make_annotations_system_container(const container_config *container_spec, const host_config *host_spec)
O
overweight 已提交
181 182 183 184
{
    int ret = 0;

    if (host_spec->system_container) {
185
        if (append_json_map_string_string(container_spec->annotations, "system.container", "true")) {
O
overweight 已提交
186 187 188 189 190 191 192 193 194 195
            ERROR("Realloc annotations failed");
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

196
static int make_annotations_cgroup_dir(const container_config *container_spec, const host_config *host_spec)
O
overweight 已提交
197 198 199 200 201 202
{
    int ret = 0;
    char cleaned[PATH_MAX] = { 0 };
    char *default_cgroup_parent = NULL;
    char *path = NULL;

L
LiuHao 已提交
203
    default_cgroup_parent = conf_get_isulad_cgroup_parent();
O
overweight 已提交
204 205 206 207 208 209
    if (host_spec->cgroup_parent != NULL) {
        path = host_spec->cgroup_parent;
    } else if (default_cgroup_parent != NULL) {
        path = default_cgroup_parent;
    }
    if (path == NULL) {
210
        path = "/isulad";
O
overweight 已提交
211 212 213 214 215 216
    }
    if (cleanpath(path, cleaned, sizeof(cleaned)) == NULL) {
        ERROR("Failed to clean path: %s", path);
        ret = -1;
        goto out;
    }
217
    if (append_json_map_string_string(container_spec->annotations, "cgroup.dir", cleaned)) {
O
overweight 已提交
218 219 220 221 222 223 224 225 226 227
        ERROR("Realloc annotations failed");
        ret = -1;
        goto out;
    }

out:
    free(default_cgroup_parent);
    return ret;
}

228
static int make_annotations_oom_score_adj(const container_config *container_spec, const host_config *host_spec)
O
overweight 已提交
229 230
{
    int ret = 0;
L
LiuHao 已提交
231
    char tmp_str[ISULAD_NUMSTRLEN64 + 1] = { 0 };
O
overweight 已提交
232 233 234

    // oom_score_adj default value is 0, So there is no need to explicitly set this value
    if (host_spec->oom_score_adj != 0) {
O
openeuler-iSula 已提交
235 236
        int nret = snprintf(tmp_str, sizeof(tmp_str), "%d", host_spec->oom_score_adj);
        if (nret < 0 || (size_t)nret >= sizeof(tmp_str)) {
O
overweight 已提交
237 238 239 240
            ERROR("create oom score adj string failed");
            ret = -1;
            goto out;
        }
241
        if (append_json_map_string_string(container_spec->annotations, "proc.oom_score_adj", tmp_str)) {
O
overweight 已提交
242 243 244 245 246 247 248 249 250 251
            ERROR("append oom score adj which configure proc filesystem for the container failed ");
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

252
static int make_annotations_files_limit(const container_config *container_spec, const host_config *host_spec)
O
overweight 已提交
253 254
{
    int ret = 0;
L
LiuHao 已提交
255
    char tmp_str[ISULAD_NUMSTRLEN64 + 1] = { 0 };
O
overweight 已提交
256 257 258 259 260

    // Not supported in oci runtime-spec, add 'files.limit' to annotations
    if (host_spec->files_limit != 0) {
        // need create new file limit item in annotations
        int64_t filelimit = host_spec->files_limit;
O
openeuler-iSula 已提交
261 262
        int nret = snprintf(tmp_str, sizeof(tmp_str), "%lld", (long long)filelimit);
        if (nret < 0 || (size_t)nret >= sizeof(tmp_str)) {
O
overweight 已提交
263 264 265 266 267
            ERROR("create files limit string failed");
            ret = -1;
            goto out;
        }

268
        if (append_json_map_string_string(container_spec->annotations, "files.limit", tmp_str)) {
O
overweight 已提交
269 270 271 272 273 274 275 276 277 278
            ERROR("append files limit failed");
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

279
static int make_sure_container_spec_annotations(container_config *container_spec)
O
overweight 已提交
280
{
281 282 283
    if (container_spec->annotations == NULL) {
        container_spec->annotations = util_common_calloc_s(sizeof(json_map_string_string));
        if (container_spec->annotations == NULL) {
O
overweight 已提交
284 285 286 287 288 289
            return -1;
        }
    }
    return 0;
}

290 291 292 293 294 295
static inline bool is_valid_umask_value(const char *value)
{
    return (strcmp(value, UMASK_NORMAL) == 0 || strcmp(value, UMASK_SECURE) == 0);
}

static int add_native_umask(const container_config *container_spec)
O
overweight 已提交
296 297
{
    int ret = 0;
298 299
    size_t i = 0;
    char *umask = NULL;
O
overweight 已提交
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
    for (i = 0; i < container_spec->annotations->len; i++) {
        if (strcmp(container_spec->annotations->keys[i], ANNOTATION_UMAKE_KEY) == 0) {
            if (!is_valid_umask_value(container_spec->annotations->values[i])) {
                ERROR("native.umask option %s not supported", container_spec->annotations->values[i]);
                isulad_set_error_message("native.umask option %s not supported", container_spec->annotations->values[i]);
                ret = -1;
            }
            goto out;
        }
    }

    umask = conf_get_isulad_native_umask();
    if (umask == NULL) {
        ERROR("Failed to get default native umask");
        ret = -1;
        goto out;
    }

    if (append_json_map_string_string(container_spec->annotations, ANNOTATION_UMAKE_KEY, umask)) {
        ERROR("Failed to append annotations: native.umask=%s", umask);
        ret = -1;
        goto out;
    }

out:
    free(umask);
    return ret;
}

static int make_annotations(oci_runtime_spec *oci_spec, container_config *container_spec, host_config *host_spec)
{
    int ret = 0;

    ret = make_sure_container_spec_annotations(container_spec);
O
overweight 已提交
335 336 337 338
    if (ret < 0) {
        goto out;
    }

339
    ret = make_annotations_network_mode(container_spec, host_spec);
O
overweight 已提交
340 341 342 343 344
    if (ret != 0) {
        ret = -1;
        goto out;
    }

345
    ret = make_annotations_system_container(container_spec, host_spec);
O
overweight 已提交
346 347 348 349 350
    if (ret != 0) {
        ret = -1;
        goto out;
    }

351
    ret = make_annotations_cgroup_dir(container_spec, host_spec);
O
overweight 已提交
352 353 354 355 356
    if (ret != 0) {
        ret = -1;
        goto out;
    }

357
    ret = make_annotations_oom_score_adj(container_spec, host_spec);
O
overweight 已提交
358 359 360 361 362
    if (ret != 0) {
        ret = -1;
        goto out;
    }

363
    ret = make_annotations_files_limit(container_spec, host_spec);
O
overweight 已提交
364 365 366 367 368
    if (ret != 0) {
        ret = -1;
        goto out;
    }

369
    ret = make_annotations_log_console(container_spec);
O
overweight 已提交
370 371 372 373 374
    if (ret != 0) {
        ret = -1;
        goto out;
    }

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    /* add rootfs.mount */
    ret = add_rootfs_mount(container_spec);
    if (ret != 0) {
        ERROR("Failed to add rootfs mount");
        goto out;
    }

    /* add native.umask */
    ret = add_native_umask(container_spec);
    if (ret != 0) {
        ERROR("Failed to add native umask");
        goto out;
    }

    if (merge_annotations(oci_spec, container_spec)) {
O
overweight 已提交
390 391 392 393 394 395 396 397
        ret = -1;
        goto out;
    }

out:
    return ret;
}

L
LiuHao 已提交
398
/* default_spec returns default oci spec used by isulad. */
Z
zhuchunyi 已提交
399
oci_runtime_spec *default_spec(bool system_container)
O
overweight 已提交
400 401
{
    const char *oci_file = OCICONFIG_PATH;
Z
zhuchunyi 已提交
402 403 404
    if (system_container) {
        oci_file = OCI_SYSTEM_CONTAINER_CONFIG_PATH;
    }
O
overweight 已提交
405 406 407 408 409 410 411
    oci_runtime_spec *oci_spec = NULL;
    parser_error err = NULL;

    /* parse the input oci file */
    oci_spec = oci_runtime_spec_parse_file(oci_file, NULL, &err);
    if (oci_spec == NULL) {
        ERROR("Failed to parse OCI specification file \"%s\", error message: %s", oci_file, err);
L
LiuHao 已提交
412
        isulad_set_error_message("Can not read the default /etc/default/isulad/config.json file: %s", err);
O
overweight 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
        goto out;
    }

out:
    free(err);
    return oci_spec;
}

static int make_sure_oci_spec_root(oci_runtime_spec *oci_spec)
{
    if (oci_spec->root == NULL) {
        oci_spec->root = util_common_calloc_s(sizeof(oci_runtime_spec_root));
        if (oci_spec->root == NULL) {
            return -1;
        }
    }
    return 0;
}

static int merge_root(oci_runtime_spec *oci_spec, const char *rootfs, const host_config *host_spec)
{
    int ret = 0;

    ret = make_sure_oci_spec_root(oci_spec);
    if (ret < 0) {
        goto out;
    }

    // fill root path properties
    if (rootfs != NULL) {
        free(oci_spec->root->path);
        oci_spec->root->path = util_strdup_s(rootfs);
    }
    if (host_spec->readonly_rootfs) {
        oci_spec->root->readonly = host_spec->readonly_rootfs;
    }

out:
    return ret;
}

static int merge_blkio_weight(oci_runtime_spec *oci_spec, uint16_t blkio_weight)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_blkio(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->block_io->weight = blkio_weight;

out:
    return ret;
}

static int make_sure_oci_spec_linux_resources_cpu(oci_runtime_spec *oci_spec)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources(oci_spec);
    if (ret < 0) {
        return -1;
    }

    if (oci_spec->linux->resources->cpu == NULL) {
479
        oci_spec->linux->resources->cpu = util_common_calloc_s(sizeof(defs_resources_cpu));
O
overweight 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 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 543 544 545 546 547 548 549 550 551 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 590 591 592 593 594 595 596 597 598 599 600 601 602 603
        if (oci_spec->linux->resources->cpu == NULL) {
            return -1;
        }
    }
    return 0;
}

static int merge_cpu_shares(oci_runtime_spec *oci_spec, int64_t cpu_shares)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_cpu(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->cpu->shares = (uint64_t)cpu_shares;

out:
    return ret;
}

static int merge_cpu_period(oci_runtime_spec *oci_spec, int64_t cpu_period)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_cpu(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->cpu->period = (uint64_t)cpu_period;

out:
    return ret;
}

static int merge_cpu_realtime_period(oci_runtime_spec *oci_spec, int64_t cpu_rt_period)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_cpu(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->cpu->realtime_period = (uint64_t)cpu_rt_period;

out:
    return ret;
}

static int merge_cpu_realtime_runtime(oci_runtime_spec *oci_spec, int64_t cpu_rt_runtime)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_cpu(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->cpu->realtime_runtime = cpu_rt_runtime;

out:
    return ret;
}

static int merge_cpu_quota(oci_runtime_spec *oci_spec, int64_t cpu_quota)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_cpu(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->cpu->quota = cpu_quota;

out:
    return ret;
}

static int merge_cpuset_cpus(oci_runtime_spec *oci_spec, const char *cpuset_cpus)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_cpu(oci_spec);
    if (ret < 0) {
        goto out;
    }

    free(oci_spec->linux->resources->cpu->cpus);
    oci_spec->linux->resources->cpu->cpus = util_strdup_s(cpuset_cpus);

out:
    return ret;
}

static int merge_cpuset_mems(oci_runtime_spec *oci_spec, const char *cpuset_mems)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_cpu(oci_spec);
    if (ret < 0) {
        goto out;
    }

    free(oci_spec->linux->resources->cpu->mems);
    oci_spec->linux->resources->cpu->mems = util_strdup_s(cpuset_mems);

out:
    return ret;
}

static int make_sure_oci_spec_linux_resources_mem(oci_runtime_spec *oci_spec)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources(oci_spec);
    if (ret < 0) {
        return -1;
    }

    if (oci_spec->linux->resources->memory == NULL) {
604
        oci_spec->linux->resources->memory = util_common_calloc_s(sizeof(defs_resources_memory));
O
overweight 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
        if (oci_spec->linux->resources->memory == NULL) {
            return -1;
        }
    }
    return 0;
}

static int merge_memory_limit(oci_runtime_spec *oci_spec, int64_t memory)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_mem(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->memory->limit = memory;

out:
    return ret;
}

static int merge_memory_oom_kill_disable(oci_runtime_spec *oci_spec, bool oom_kill_disable)
{
    int ret = 0;

631 632 633
    ret = make_sure_oci_spec_linux_resources_mem(oci_spec);
    if (ret < 0) {
        goto out;
O
overweight 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    }

    oci_spec->linux->resources->memory->disable_oom_killer = oom_kill_disable;

out:
    return ret;
}

static int merge_memory_swap(oci_runtime_spec *oci_spec, int64_t memory_swap)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_mem(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->memory->swap = memory_swap;

out:
    return ret;
}

static int merge_memory_reservation(oci_runtime_spec *oci_spec, int64_t memory_reservation)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_mem(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->memory->reservation = memory_reservation;

out:
    return ret;
}

static int merge_kernel_memory(oci_runtime_spec *oci_spec, int64_t kernel_memory)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_mem(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->memory->kernel = kernel_memory;

out:
    return ret;
}

static int merge_hugetlbs(oci_runtime_spec *oci_spec, host_config_hugetlbs_element **hugetlbs, size_t hugetlbs_len)
{
    int ret = 0;
    size_t i = 0;
    size_t new_size, old_size;
692
    defs_resources_hugepage_limits_element **hugepage_limits_temp = NULL;
O
overweight 已提交
693 694 695 696 697 698

    ret = make_sure_oci_spec_linux_resources(oci_spec);
    if (ret < 0) {
        goto out;
    }

699
    if (hugetlbs_len > SIZE_MAX / sizeof(defs_resources_hugepage_limits_element *) -
O
overweight 已提交
700 701 702 703 704 705
        oci_spec->linux->resources->hugepage_limits_len) {
        ERROR("Too many hugetlbs to merge!");
        ret = -1;
        goto out;
    }
    old_size = oci_spec->linux->resources->hugepage_limits_len *
706
               sizeof(defs_resources_hugepage_limits_element *);
O
overweight 已提交
707
    new_size = (oci_spec->linux->resources->hugepage_limits_len + hugetlbs_len)
708
               * sizeof(defs_resources_hugepage_limits_element *);
O
overweight 已提交
709 710 711 712 713 714 715 716 717 718 719 720
    ret = mem_realloc((void **)&hugepage_limits_temp, new_size,
                      oci_spec->linux->resources->hugepage_limits,  old_size);
    if (ret != 0) {
        ERROR("Failed to realloc memory for hugepage limits");
        ret = -1;
        goto out;
    }

    oci_spec->linux->resources->hugepage_limits = hugepage_limits_temp;

    for (i = 0; i < hugetlbs_len; i++) {
        oci_spec->linux->resources->hugepage_limits[oci_spec->linux->resources->hugepage_limits_len]
721
            = util_common_calloc_s(sizeof(defs_resources_hugepage_limits_element));
O
overweight 已提交
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
        if (oci_spec->linux->resources->hugepage_limits[oci_spec->linux->resources->hugepage_limits_len] == NULL) {
            ERROR("Failed to malloc memory for hugepage limits");
            ret = -1;
            goto out;
        }
        oci_spec->linux->resources->hugepage_limits[oci_spec->linux->resources->hugepage_limits_len]->limit
            = hugetlbs[i]->limit;
        oci_spec->linux->resources->hugepage_limits[oci_spec->linux->resources->hugepage_limits_len]->page_size
            = util_strdup_s(hugetlbs[i]->page_size);
        oci_spec->linux->resources->hugepage_limits_len++;
    }
out:
    return ret;
}

static int make_sure_oci_spec_hooks(oci_runtime_spec *oci_spec)
{
    if (oci_spec->hooks == NULL) {
        oci_spec->hooks = util_common_calloc_s(sizeof(oci_runtime_spec_hooks));
        if (oci_spec->hooks == NULL) {
            return -1;
        }
    }
    return 0;
}

static int merge_hook_spec(oci_runtime_spec *oci_spec, const char *hook_spec)
{
    int ret = 0;
    parser_error err = NULL;
    oci_runtime_spec_hooks *hooks = NULL;

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

    ret = make_sure_oci_spec_hooks(oci_spec);
    if (ret < 0) {
        goto out;
    }

    hooks = oci_runtime_spec_hooks_parse_file(hook_spec, NULL, &err);
    if (hooks == NULL) {
        ERROR("Failed to parse hook-spec file: %s", err);
        ret = -1;
        goto out;
    }
    ret = merge_hooks(oci_spec->hooks, hooks);
    free_oci_runtime_spec_hooks(hooks);
    if (ret < 0) {
        goto out;
    }

out:
    free(err);
    return ret;
}

780
static void clean_correlated_selinux(defs_process *process)
O
overweight 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 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 899 900 901 902 903
{
    if (process == NULL) {
        return;
    }

    free(process->selinux_label);
    process->selinux_label = NULL;
}

static void clean_correlated_read_only_path(oci_runtime_config_linux *linux)
{
    if (linux == NULL) {
        return;
    }

    if (linux->readonly_paths != NULL && linux->readonly_paths_len) {
        size_t i;
        for (i = 0; i < linux->readonly_paths_len; i++) {
            free(linux->readonly_paths[i]);
            linux->readonly_paths[i] = NULL;
        }
        free(linux->readonly_paths);
        linux->readonly_paths = NULL;
        linux->readonly_paths_len = 0;
    }
}

static void clean_correlated_masked_path(oci_runtime_config_linux *linux)
{
    if (linux == NULL) {
        return;
    }

    if (linux->masked_paths != NULL && linux->masked_paths_len) {
        size_t i;
        for (i = 0; i < linux->masked_paths_len; i++) {
            free(linux->masked_paths[i]);
            linux->masked_paths[i] = NULL;
        }
        free(linux->masked_paths);
        linux->masked_paths = NULL;
        linux->masked_paths_len = 0;
    }
}

static void clean_correlated_seccomp(oci_runtime_config_linux *linux)
{
    if (linux == NULL) {
        return;
    }

    free_oci_runtime_config_linux_seccomp(linux->seccomp);
    linux->seccomp = NULL;
}

static void clean_correlated_items(const oci_runtime_spec *oci_spec)
{
    if (oci_spec == NULL) {
        return;
    }

    clean_correlated_selinux(oci_spec->process);
    clean_correlated_masked_path(oci_spec->linux);
    clean_correlated_read_only_path(oci_spec->linux);
    clean_correlated_seccomp(oci_spec->linux);
}

static int adapt_settings_for_privileged(oci_runtime_spec *oci_spec, bool privileged)
{
    int ret = 0;
    size_t all_caps_len = 0;

    if (!privileged) {
        return 0;
    }

    all_caps_len = util_get_all_caps_len();
    if (oci_spec == NULL) {
        ret = -1;
        goto out;
    }

    clean_correlated_items(oci_spec);

    ret = set_mounts_readwrite_option(oci_spec);
    if (ret != 0) {
        goto out;
    }

    /* add all capabilities */
    ret = make_sure_oci_spec_process(oci_spec);
    if (ret < 0) {
        goto out;
    }

    ret = refill_oci_process_capabilities(&oci_spec->process->capabilities, g_all_caps, all_caps_len);
    if (ret != 0) {
        ERROR("Failed to copy all capabilities");
        ret = -1;
        goto out;
    }

    ret = merge_all_devices_and_all_permission(oci_spec);
    if (ret != 0) {
        ERROR("Failed to merge all devices on host and all devices's cgroup permission");
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static int make_sure_oci_spec_linux_resources_pids(oci_runtime_spec *oci_spec)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources(oci_spec);
    if (ret < 0) {
        return -1;
    }

    if (oci_spec->linux->resources->pids == NULL) {
904
        oci_spec->linux->resources->pids = util_common_calloc_s(sizeof(defs_resources_pids));
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
        if (oci_spec->linux->resources->pids == NULL) {
            return -1;
        }
    }
    return 0;
}

static int merge_pids_limit(oci_runtime_spec *oci_spec, int64_t pids_limit)
{
    int ret = 0;

    ret = make_sure_oci_spec_linux_resources_pids(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->linux->resources->pids->limit = pids_limit;

out:
    return ret;
}

static int merge_hostname(oci_runtime_spec *oci_spec, const host_config *host_spec,
928
                          container_config *container_spec)
O
overweight 已提交
929 930
{
    free(oci_spec->hostname);
931 932 933
    oci_spec->hostname = util_strdup_s(container_spec->hostname);

    return 0;
O
overweight 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
}

static int merge_conf_cgroup_cpu_int64(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;

    /* cpu shares */
    if (host_spec->cpu_shares != 0) {
        ret = merge_cpu_shares(oci_spec, host_spec->cpu_shares);
        if (ret != 0) {
            ERROR("Failed to merge cgroup cpu shares");
            goto out;
        }
    }

    /* cpu period */
    if (host_spec->cpu_period != 0) {
        ret = merge_cpu_period(oci_spec, host_spec->cpu_period);
        if (ret != 0) {
            ERROR("Failed to merge cgroup cpu period");
            goto out;
        }
    }

    /* cpu realtime period */
    if (host_spec->cpu_realtime_period != 0) {
        ret = merge_cpu_realtime_period(oci_spec, host_spec->cpu_realtime_period);
        if (ret != 0) {
            ERROR("Failed to merge cgroup cpu realtime period");
            goto out;
        }
    }

    /* cpu realtime runtime */
    if (host_spec->cpu_realtime_runtime != 0) {
        ret = merge_cpu_realtime_runtime(oci_spec, host_spec->cpu_realtime_runtime);
        if (ret != 0) {
            ERROR("Failed to merge cgroup cpu realtime runtime");
            goto out;
        }
    }

    /* cpu quota */
    if (host_spec->cpu_quota != 0) {
        ret = merge_cpu_quota(oci_spec, host_spec->cpu_quota);
        if (ret != 0) {
            ERROR("Failed to merge cgroup cpu quota");
            goto out;
        }
    }

out:
    return ret;
}

static int merge_conf_cgroup_cpu(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;

    ret = merge_conf_cgroup_cpu_int64(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    /* cpuset-cpus */
    if (util_valid_str(host_spec->cpuset_cpus)) {
        ret = merge_cpuset_cpus(oci_spec, host_spec->cpuset_cpus);
        if (ret != 0) {
            ERROR("Failed to merge cgroup cpuset cpus");
            goto out;
        }
    }

    /* cpuset mems */
    if (util_valid_str(host_spec->cpuset_mems)) {
        ret = merge_cpuset_mems(oci_spec, host_spec->cpuset_mems);
        if (ret != 0) {
            ERROR("Failed to merge cgroup cpuset mems");
            goto out;
        }
    }

out:
    return ret;
}

static int merge_conf_cgroup_memory(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;

    /* memory limit */
    if (host_spec->memory != 0) {
        ret = merge_memory_limit(oci_spec, host_spec->memory);
        if (ret != 0) {
            ERROR("Failed to merge cgroup memory limit");
            goto out;
        }
    }

    if (host_spec->oom_kill_disable) {
        if (host_spec->memory == 0) {
            WARN("Disabling the OOM killer on containers without setting memory limit may be dangerous.");
        }

        ret = merge_memory_oom_kill_disable(oci_spec, host_spec->oom_kill_disable);
        if (ret != 0) {
            ERROR("Failed to merge cgroup memory oom kill disable");
            goto out;
        }
    }

D
dogsheng 已提交
1045
    /* memory swap */
O
overweight 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
    if (host_spec->memory_swap != 0) {
        ret = merge_memory_swap(oci_spec, host_spec->memory_swap);
        if (ret != 0) {
            ERROR("Failed to merge cgroup memory swap");
            goto out;
        }
    }

    /* memory reservation */
    if (host_spec->memory_reservation != 0) {
        ret = merge_memory_reservation(oci_spec, host_spec->memory_reservation);
        if (ret != 0) {
            ERROR("Failed to merge cgroup memory reservation");
            goto out;
        }
    }

    /* kernel_memory */
    if (host_spec->kernel_memory != 0) {
        ret = merge_kernel_memory(oci_spec, host_spec->kernel_memory);
        if (ret != 0) {
            ERROR("Failed to merge cgroup kernel_memory");
            goto out;
        }
    }

out:
    return ret;
}

static int merge_conf_blkio_weight(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;

    /* blkio weight */
    if (host_spec->blkio_weight != 0) {
        ret = merge_blkio_weight(oci_spec, host_spec->blkio_weight);
        if (ret != 0) {
            ERROR("Failed to merge cgroup blkio weight");
            goto out;
        }
    }
out:
    return ret;
}

static int do_merge_one_ulimit_override(const oci_runtime_spec *oci_spec,
1093
                                        defs_process_rlimits_element *rlimit)
O
overweight 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
{
    size_t j;
    bool exists = false;

    for (j = 0; j < oci_spec->process->rlimits_len; j++) {
        if (oci_spec->process->rlimits[j]->type == NULL) {
            ERROR("rlimit type is empty");
            free(rlimit->type);
            free(rlimit);
            return -1;
        }
        if (strcmp(oci_spec->process->rlimits[j]->type, rlimit->type) == 0) {
            exists = true;
            break;
        }
    }
    if (exists) {
        /* override ulimit */
1112
        free_defs_process_rlimits_element(oci_spec->process->rlimits[j]);
O
overweight 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
        oci_spec->process->rlimits[j] = rlimit;
    } else {
        oci_spec->process->rlimits[oci_spec->process->rlimits_len] = rlimit;
        oci_spec->process->rlimits_len++;
    }

    return 0;
}

static int merge_one_ulimit_override(const oci_runtime_spec *oci_spec, const host_config_ulimits_element *ulimit)
{
1124
    defs_process_rlimits_element *rlimit = NULL;
O
overweight 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

    if (trans_ulimit_to_rlimit(&rlimit, ulimit) != 0) {
        return -1;
    }

    return do_merge_one_ulimit_override(oci_spec, rlimit);
}

static int merge_ulimits_override(oci_runtime_spec *oci_spec, host_config_ulimits_element **ulimits, size_t ulimits_len)
{
    int ret = 0;
    size_t i = 0;

    if (oci_spec == NULL || ulimits == NULL || ulimits_len == 0) {
        return -1;
    }

    ret = merge_ulimits_pre(oci_spec, ulimits_len);
    if (ret < 0) {
        goto out;
    }

    for (i = 0; i < ulimits_len; i++) {
        ret = merge_one_ulimit_override(oci_spec, ulimits[i]);
        if (ret != 0) {
            ret = -1;
            goto out;
        }
    }
out:
    return ret;
}

static int merge_conf_ulimits(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;

    /* rlimits */
    if (host_spec->ulimits != NULL && host_spec->ulimits_len != 0) {
        if (host_spec->ulimits_len > LIST_SIZE_MAX) {
            ERROR("Too many ulimits to add, the limit is %d", LIST_SIZE_MAX);
L
LiuHao 已提交
1166
            isulad_set_error_message("Too many ulimits to add, the limit is %d", LIST_SIZE_MAX);
O
overweight 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
            ret = -1;
            goto out;
        }
        ret = merge_ulimits_override(oci_spec, host_spec->ulimits, host_spec->ulimits_len);
        if (ret != 0) {
            ERROR("Failed to merge rlimits");
            goto out;
        }
    }

out:
    return ret;
}

static int merge_conf_hugetlbs(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;

    /* hugepage limits */
    if (host_spec->hugetlbs_len != 0 && host_spec->hugetlbs != NULL) {
        if (host_spec->hugetlbs_len > LIST_SIZE_MAX) {
            ERROR("Too many hugetlbs to add, the limit is %d", LIST_SIZE_MAX);
L
LiuHao 已提交
1189
            isulad_set_error_message("Too many hugetlbs to add, the limit is %d", LIST_SIZE_MAX);
O
overweight 已提交
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 1220
            ret = -1;
            goto out;
        }
        ret = merge_hugetlbs(oci_spec, host_spec->hugetlbs, host_spec->hugetlbs_len);
        if (ret != 0) {
            ERROR("Failed to merge cgroup hugepage limits");
            goto out;
        }
    }

out:
    return ret;
}

static int merge_conf_pids_limit(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = 0;

    /* pids limit */
    if (host_spec->pids_limit != 0) {
        ret = merge_pids_limit(oci_spec, host_spec->pids_limit);
        if (ret != 0) {
            ERROR("Failed to merge pids limit");
            goto out;
        }
    }

out:
    return ret;
}

1221
int merge_conf_cgroup(oci_runtime_spec *oci_spec, const host_config *host_spec)
O
overweight 已提交
1222 1223 1224
{
    int ret = 0;

L
LiFeng 已提交
1225 1226 1227 1228 1229
    if (oci_spec == NULL || host_spec == NULL) {
        ret = -1;
        goto out;
    }

O
overweight 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    ret = merge_conf_cgroup_cpu(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_conf_cgroup_memory(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_conf_blkio_weight(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_conf_ulimits(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_conf_hugetlbs(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_conf_pids_limit(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

out:
    return ret;
}

static int prepare_process_args(oci_runtime_spec *oci_spec, size_t args_len)
{
    int ret = 0;

    ret = make_sure_oci_spec_process(oci_spec);
    if (ret < 0) {
        return -1;
    }

    if (oci_spec->process->args_len != 0 && oci_spec->process->args != NULL) {
        size_t i;
        for (i = 0; i < oci_spec->process->args_len; i++) {
            free(oci_spec->process->args[i]);
            oci_spec->process->args[i] = NULL;
        }
        free(oci_spec->process->args);
        oci_spec->process->args = NULL;
        oci_spec->process->args_len = 0;
    }

    if (args_len > (SIZE_MAX / sizeof(char *))) {
        return -1;
    }

    oci_spec->process->args = util_common_calloc_s(args_len * sizeof(char *));
    if (oci_spec->process->args == NULL) {
        return -1;
    }
    return 0;
}

1295
static int replace_entrypoint_cmds_from_spec(const oci_runtime_spec *oci_spec, container_config *container_spec)
O
overweight 已提交
1296 1297 1298
{
    if (oci_spec->process->args_len == 0) {
        ERROR("No command specified");
L
LiuHao 已提交
1299
        isulad_set_error_message("No command specified");
O
overweight 已提交
1300 1301 1302
        return -1;
    }
    return dup_array_of_strings((const char **)(oci_spec->process->args), oci_spec->process->args_len,
1303
                                &(container_spec->cmd), &(container_spec->cmd_len));
O
overweight 已提交
1304 1305
}

1306
static int merge_conf_args(oci_runtime_spec *oci_spec, container_config *container_spec)
O
overweight 已提交
1307 1308 1309 1310 1311 1312
{
    int ret = 0;
    size_t argslen = 0;
    size_t i = 0;

    // Reset entrypoint if we do not want to use entrypoint from image
1313 1314 1315 1316 1317 1318
    if (container_spec->entrypoint_len == 1 && container_spec->entrypoint[0][0] == '\0') {
        free(container_spec->entrypoint[0]);
        container_spec->entrypoint[0] = NULL;
        free(container_spec->entrypoint);
        container_spec->entrypoint = NULL;
        container_spec->entrypoint_len = 0;
O
overweight 已提交
1319 1320
    }

1321 1322 1323
    argslen = container_spec->cmd_len;
    if (container_spec->entrypoint_len != 0) {
        argslen += container_spec->entrypoint_len;
O
overweight 已提交
1324 1325 1326 1327
    }

    if (argslen > LIST_SIZE_MAX) {
        ERROR("Too many commands to add, the limit is %d", LIST_SIZE_MAX);
L
LiuHao 已提交
1328
        isulad_set_error_message("Too many commands to add, the limit is %d", LIST_SIZE_MAX);
O
overweight 已提交
1329 1330 1331 1332
        return -1;
    }

    if (argslen == 0) {
1333
        return replace_entrypoint_cmds_from_spec(oci_spec, container_spec);
O
overweight 已提交
1334 1335 1336 1337 1338 1339 1340 1341
    }

    if (prepare_process_args(oci_spec, argslen) < 0) {
        ret = -1;
        goto out;
    }

    // append commands... to entrypoint
1342 1343
    for (i = 0; container_spec->entrypoint != NULL && i < container_spec->entrypoint_len; i++) {
        oci_spec->process->args[oci_spec->process->args_len] = util_strdup_s(container_spec->entrypoint[i]);
O
overweight 已提交
1344 1345 1346
        oci_spec->process->args_len++;
    }

1347 1348
    for (i = 0; container_spec->cmd != NULL && i < container_spec->cmd_len; i++) {
        oci_spec->process->args[oci_spec->process->args_len] = util_strdup_s(container_spec->cmd[i]);
O
overweight 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
        oci_spec->process->args_len++;
    }

out:
    return ret;
}

static int merge_share_namespace_helper(const oci_runtime_spec *oci_spec, const char *mode, const char *type)
{
    int ret = -1;
    char *tmp_mode = NULL;
    size_t len = 0;
    size_t org_len = 0;
    size_t i = 0;
1363
    defs_namespace_reference **work_ns = NULL;
O
overweight 已提交
1364 1365 1366 1367 1368 1369 1370

    org_len = oci_spec->linux->namespaces_len;
    len = oci_spec->linux->namespaces_len;
    work_ns = oci_spec->linux->namespaces;

    tmp_mode = get_share_namespace_path(type, mode);
    for (i = 0; i < org_len; i++) {
1371
        if (strcmp(type, work_ns[i]->type) == 0) {
O
overweight 已提交
1372 1373 1374 1375 1376 1377 1378 1379
            free(work_ns[i]->path);
            work_ns[i]->path = NULL;
            if (tmp_mode != NULL) {
                work_ns[i]->path = util_strdup_s(tmp_mode);
            }
            break;
        }
    }
1380

O
overweight 已提交
1381
    if (i >= org_len) {
1382
        if (len > (SIZE_MAX / sizeof(defs_namespace_reference *)) - 1) {
O
overweight 已提交
1383 1384 1385 1386 1387
            ret = -1;
            ERROR("Out of memory");
            goto out;
        }

1388 1389
        ret = mem_realloc((void **)&work_ns, (len + 1) * sizeof(defs_namespace_reference *),
                          (void *)work_ns, len * sizeof(defs_namespace_reference *));
O
overweight 已提交
1390 1391 1392 1393
        if (ret != 0) {
            ERROR("Out of memory");
            goto out;
        }
1394
        work_ns[len] = util_common_calloc_s(sizeof(defs_namespace_reference));
O
overweight 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 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 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
        if (work_ns[len] == NULL) {
            ERROR("Out of memory");
            ret = -1;
            goto out;
        }
        work_ns[len]->type = util_strdup_s(type);
        if (tmp_mode != NULL) {
            work_ns[len]->path = util_strdup_s(tmp_mode);
        }
        len++;
    }
    ret = 0;
out:
    free(tmp_mode);
    if (work_ns != NULL) {
        oci_spec->linux->namespaces = work_ns;
        oci_spec->linux->namespaces_len = len;
    }
    return ret;
}

static int merge_share_single_namespace(const oci_runtime_spec *oci_spec, const char *mode, const char *type)
{
    if (mode == NULL) {
        return 0;
    }

    return merge_share_namespace_helper(oci_spec, mode, type);
}

static int merge_share_namespace(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
    int ret = -1;

    if (oci_spec == NULL || host_spec == NULL) {
        goto out;
    }

    if (make_sure_oci_spec_linux(oci_spec) < 0) {
        goto out;
    }

    // user
    if (merge_share_single_namespace(oci_spec, host_spec->userns_mode, TYPE_NAMESPACE_USER) != 0) {
        ret = -1;
        goto out;
    }

    // user remap
    if (host_spec->user_remap != NULL && merge_share_single_namespace(oci_spec, "user", TYPE_NAMESPACE_USER) != 0) {
        ret = -1;
        goto out;
    }

    // network
    if (merge_share_single_namespace(oci_spec, host_spec->network_mode, TYPE_NAMESPACE_NETWORK) != 0) {
        ret = -1;
        goto out;
    }

    // ipc
    if (merge_share_single_namespace(oci_spec, host_spec->ipc_mode, TYPE_NAMESPACE_IPC) != 0) {
        ret = -1;
        goto out;
    }

    // pid
    if (merge_share_single_namespace(oci_spec, host_spec->pid_mode, TYPE_NAMESPACE_PID) != 0) {
        ret = -1;
        goto out;
    }

    // uts
    if (merge_share_single_namespace(oci_spec, host_spec->uts_mode, TYPE_NAMESPACE_UTS) != 0) {
        ret = -1;
        goto out;
    }

    ret = 0;
out:
    return ret;
}

static int merge_working_dir(oci_runtime_spec *oci_spec, const char *working_dir)
{
    int ret = 0;

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

    ret = make_sure_oci_spec_process(oci_spec);
    if (ret < 0) {
        goto out;
    }

    free(oci_spec->process->cwd);
    oci_spec->process->cwd = util_strdup_s(working_dir);

out:
    return ret;
}

Z
zhuchunyi 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
static int change_tmpfs_mount_size(const oci_runtime_spec *oci_spec, int64_t memory_limit)
{
    int ret = 0;
    size_t i = 0;
    char size_opt[MOUNT_PROPERTIES_SIZE] = { 0 };

    if (oci_spec->mounts == NULL) {
        goto out;
    }
    if (memory_limit <= 0) {
        goto out;
    }
D
dogsheng 已提交
1510
    /* set tmpfs mount size to half of container memory limit */
O
openeuler-iSula 已提交
1511 1512
    int nret = snprintf(size_opt, sizeof(size_opt), "size=%lldk", (long long int)(memory_limit / 2048));
    if (nret < 0 || (size_t)nret >= sizeof(size_opt)) {
Z
zhuchunyi 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    for (i = 0; i < oci_spec->mounts_len; i++) {
        if (strcmp("tmpfs", oci_spec->mounts[i]->type) != 0) {
            continue;
        }
        if (strcmp("/run", oci_spec->mounts[i]->destination)  == 0 || \
            strcmp("/run/lock", oci_spec->mounts[i]->destination)  == 0 || \
            strcmp("/tmp", oci_spec->mounts[i]->destination) == 0) {
            ret = util_array_append(&oci_spec->mounts[i]->options, size_opt);
            if (ret != 0) {
                ERROR("append mount size option failed");
                goto out;
            }
            oci_spec->mounts[i]->options_len++;
        }
    }

out:
    return ret;
}

O
overweight 已提交
1537
static int merge_settings_for_system_container(oci_runtime_spec *oci_spec, host_config *host_spec,
1538
                                               container_config *container_spec)
O
overweight 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
{
    int ret = -1;

    if (oci_spec == NULL || host_spec == NULL) {
        return -1;
    }

    if (!host_spec->system_container) {
        return 0;
    }

    ret = adapt_settings_for_system_container(oci_spec, host_spec);
    if (ret != 0) {
        ERROR("Failed to adapt settings for system container");
        goto out;
    }
Z
zhuchunyi 已提交
1555
    if (change_tmpfs_mount_size(oci_spec, host_spec->memory) != 0) {
O
overweight 已提交
1556
        ret = -1;
Z
zhuchunyi 已提交
1557
        ERROR("Failed to change tmpfs mount size for system container");
O
overweight 已提交
1558 1559 1560 1561
        goto out;
    }

    // append mounts of oci_spec
1562 1563
    if (container_spec->ns_change_opt != NULL) {
        ret = adapt_settings_for_mounts(oci_spec, container_spec);
O
overweight 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
        if (ret != 0) {
            ERROR("Failed to adapt settings for ns_change_opt");
            goto out;
        }
    }

out:
    return ret;
}

static int merge_resources_conf(oci_runtime_spec *oci_spec, host_config *host_spec,
1575
                                container_config_v2_common_config *v2_spec)
O
overweight 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
{
    int ret = 0;

    ret = merge_share_namespace(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_conf_cgroup(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_conf_device(oci_spec, host_spec);
    if (ret != 0) {
        goto out;
    }

1594
    ret = merge_conf_mounts(oci_spec, host_spec, v2_spec);
O
overweight 已提交
1595 1596 1597 1598 1599 1600 1601
    if (ret) {
        goto out;
    }
out:
    return ret;
}

1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
static int merge_terminal(oci_runtime_spec *oci_spec, bool terminal)
{
    int ret = 0;

    ret = make_sure_oci_spec_process(oci_spec);
    if (ret < 0) {
        goto out;
    }

    oci_spec->process->terminal = terminal;

out:
    return ret;
}

O
overweight 已提交
1617
static int merge_process_conf(oci_runtime_spec *oci_spec, const host_config *host_spec,
1618
                              container_config *container_spec)
O
overweight 已提交
1619 1620 1621
{
    int ret = 0;

1622
    ret = merge_conf_args(oci_spec, container_spec);
O
overweight 已提交
1623 1624 1625 1626 1627
    if (ret != 0) {
        goto out;
    }

    /* environment variables */
1628
    ret = merge_env(oci_spec, (const char **)container_spec->env, container_spec->env_len);
O
overweight 已提交
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
    if (ret != 0) {
        ERROR("Failed to merge environment variables");
        goto out;
    }

    /* env target file */
    ret = merge_env_target_file(oci_spec, host_spec->env_target_file);
    if (ret != 0) {
        ERROR("Failed to merge env target file");
        goto out;
    }

    /* working dir */
1642
    ret = merge_working_dir(oci_spec, container_spec->working_dir);
O
overweight 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
    if (ret != 0) {
        ERROR("Failed to merge working dir");
        goto out;
    }

    /* hook-spec file */
    ret = merge_hook_spec(oci_spec, host_spec->hook_spec);
    if (ret != 0) {
        ERROR("Failed to merge hook spec");
        goto out;
    }

1655 1656 1657 1658 1659 1660 1661
    /* merge whether allocate a pseudo-TTY */
    ret = merge_terminal(oci_spec, container_spec->tty);
    if (ret != 0) {
        ERROR("Failed to merge process terminal");
        goto out;
    }

O
overweight 已提交
1662 1663 1664 1665
out:
    return ret;
}

W
wujing 已提交
1666
static int split_security_opt(const char *security_opt, char ***items, size_t *items_size)
O
overweight 已提交
1667 1668 1669
{
    int ret = 0;

W
wujing 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
    if (strings_contains_any(security_opt, "=")) {
        *items = util_string_split_n(security_opt, '=', 2);
        if (*items == NULL) {
            ERROR("Out of memory");
            ret = -1;
            goto out;
        }
        *items_size = util_array_len((const char **)*items);
    } else if (strings_contains_any(security_opt, ":")) {
        *items = util_string_split_n(security_opt, ':', 2);
        if (*items == NULL) {
            ERROR("Out of memory");
            ret = -1;
            goto out;
        }
        *items_size = util_array_len((const char **)*items);
        WARN("Security options with `:` as a separator are deprecated and will be completely unsupported"
             " in new version, use `=` instead.");
    }

out:
    return ret;
}

1694
int parse_security_opt(const host_config *host_spec, bool *no_new_privileges,
W
wujing 已提交
1695 1696
                       char ***label_opts, size_t *label_opts_len,
                       char **seccomp_profile)
W
wujing 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 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 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
{
    int ret = 0;
    size_t i;
    char **items = NULL;
    size_t items_size = 0;

    if (host_spec->security_opt == NULL || host_spec->security_opt_len == 0) {
        return 0;
    }
    if (host_spec->security_opt_len > LIST_SIZE_MAX) {
        ERROR("Too many security option to add, the limit is %d", LIST_SIZE_MAX);
        isulad_set_error_message("Too many security option to add, the limit is %d", LIST_SIZE_MAX);
        ret = -1;
        goto out;
    }

    for (i = 0; i < host_spec->security_opt_len; i++) {
        if (strcmp(host_spec->security_opt[i], "no-new-privileges") == 0) {
            *no_new_privileges = true;
            continue;
        } else if (strcmp(host_spec->security_opt[i], "disable") == 0) {
            ret = util_array_append(label_opts, "disable");
            if (ret != 0) {
                ERROR("Failed to append disable label");
                ret = -1;
                goto out;
            }
            (*label_opts_len)++;
            continue;
        }

        if (split_security_opt(host_spec->security_opt[i], &items, &items_size)) {
            ret = -1;
            goto out;
        }

        if (items_size != 2) {
            ERROR("invalid --security-opt: %s", host_spec->security_opt[i]);
            ret = -1;
            goto out;
        }

        if (strcmp(items[0], "label") == 0) {
            ret = util_array_append(label_opts, items[1]);
            if (ret != 0) {
                ERROR("Failed to append label");
                ret = -1;
                goto out;
            }
            (*label_opts_len)++;
        } else if (strcmp(items[0], "seccomp") == 0) {
            free(*seccomp_profile);
            *seccomp_profile = util_strdup_s(items[1]);
        } else {
            ERROR("invalid --security-opt: %s", host_spec->security_opt[i]);
            ret = -1;
            goto out;
        }
        util_free_array(items);
        items = NULL;
    }

out:
    util_free_array(items);
    return ret;
}

static int to_host_config_selinux_labels(const char **labels, size_t len, char ***dst, size_t *dst_len)
{
    int ret = 0;
    size_t i;
    char *item = NULL;

    for (i = 0; i < len; i++) {
        item = util_string_append(labels[i], "label=");
        if (item == NULL) {
            ERROR("Failed to append string");
            ret = -1;
            goto out;
        }
        if (util_array_append(dst, item) != 0) {
            ERROR("Failed to append label");
            ret = -1;
            goto out;
        }
    }
    *dst_len = util_array_len((const char **)*dst);

out:
    free(item);
    return ret;
}

static int handle_host_or_privileged_mode(host_config *hc)
{
    int ret = 0;
    char **labels = NULL;
    size_t labels_len = 0;

    if (get_disable_security_opt(&labels, &labels_len) != 0) {
        ret = -1;
        goto out;
    }

    if (to_host_config_selinux_labels((const char **)labels, labels_len,
                                      &hc->security_opt, &hc->security_opt_len) != 0) {
        ret = -1;
        goto out;
    }

out:
    util_free_array(labels);
    return ret;
}

static int handle_ipc_pid_label(host_config *hc, const char **ipc_label, size_t ipc_label_len,
                                const char **pid_label, size_t pid_label_len)
{
    int ret = 0;
    size_t i;

    if (pid_label != NULL && ipc_label != NULL) {
        if (pid_label_len != ipc_label_len) {
            ERROR("--ipc and --pid containers SELinux labels aren't the same");
            ret = -1;
            goto out;
        }
        for (i = 0; i < pid_label_len; i++) {
            if (strcmp(pid_label[i], ipc_label[i]) != 0) {
                ERROR("--ipc and --pid containers SELinux labels aren't the same");
                ret = -1;
                goto out;
            }
        }
        if (to_host_config_selinux_labels((const char **)pid_label, pid_label_len,
                                          &hc->security_opt, &hc->security_opt_len) != 0) {
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

static int handle_connected_container_mode(host_config *hc)
{
    int ret = 0;
    char **ipc_label = NULL;
    size_t ipc_label_len = 0;
    char **pid_label = NULL;
    size_t pid_label_len = 0;

    char *ipc_container = connected_container(hc->ipc_mode);
    char *pid_container = connected_container(hc->pid_mode);
    if (ipc_container != NULL) {
        char *ipc_process_label = get_container_process_label(ipc_container);
        if (dup_security_opt(ipc_process_label, &ipc_label, &ipc_label_len) != 0) {
            free(ipc_process_label);
            ret = -1;
            goto out;
        }
        if (pid_container == NULL) {
            if (to_host_config_selinux_labels((const char **)ipc_label, ipc_label_len, &hc->security_opt,
                                              &hc->security_opt_len) != 0) {
                free(ipc_process_label);
                ret = -1;
                goto out;
            }
        }
        free(ipc_process_label);
    }

    if (pid_container != NULL) {
        char *pid_process_label = get_container_process_label(pid_container);

        if (dup_security_opt(pid_process_label, &pid_label, &pid_label_len) != 0) {
            free(pid_process_label);
            ret = -1;
            goto out;
        }
        if (ipc_container == NULL) {
            if (to_host_config_selinux_labels((const char **)pid_label, pid_label_len, &hc->security_opt,
                                              &hc->security_opt_len) != 0) {
                free(pid_process_label);
                ret = -1;
                goto out;
            }
        }
        free(pid_process_label);
    }

    if (handle_ipc_pid_label(hc, (const char **)ipc_label, ipc_label_len,
                             (const char **)pid_label, pid_label_len) != 0) {
        ret = -1;
        goto out;
    }

out:
    util_free_array(ipc_label);
    util_free_array(pid_label);
    free(ipc_container);
    free(pid_container);
    return ret;
}

static int generate_security_opt(host_config *hc)
{
    size_t i;

    for (i = 0; i < hc->security_opt_len; i++) {
        char **items = util_string_split(hc->security_opt[i], '=');
        if (*items == NULL) {
            ERROR("Out of memory");
            return -1;
        }
        size_t len = util_array_len((const char **)(items));
        if (len != 0 && strcmp(items[0], "label") == 0) {
            util_free_array(items);
            return 0;
        }
        util_free_array(items);
    }

    if (is_host(hc->ipc_mode) || is_host(hc->pid_mode) || hc->privileged) {
        return handle_host_or_privileged_mode(hc);
    }

    return handle_connected_container_mode(hc);
}


static int merge_security_conf(oci_runtime_spec *oci_spec, host_config *host_spec,
                               container_config_v2_common_config *v2_spec)
{
    int ret = 0;
    bool no_new_privileges = false;
    char **label_opts = NULL;
    size_t label_opts_len = 0;
    char *seccomp_profile = NULL;

    ret = generate_security_opt(host_spec);
    if (ret != 0) {
        ERROR("Failed to generate security opt");
        goto out;
    }

O
overweight 已提交
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
    ret = merge_caps(oci_spec, (const char **)host_spec->cap_add, host_spec->cap_add_len,
                     (const char **)host_spec->cap_drop, host_spec->cap_drop_len);
    if (ret) {
        ERROR("Failed to merge caps");
        goto out;
    }

    ret = merge_default_seccomp_spec(oci_spec, oci_spec->process->capabilities);
    if (ret != 0) {
        ERROR("Failed to merge default seccomp file");
        goto out;
    }

W
wujing 已提交
1957 1958 1959 1960 1961 1962 1963
    ret = parse_security_opt(host_spec, &no_new_privileges, &label_opts,
                             &label_opts_len, &seccomp_profile);
    if (ret != 0) {
        ERROR("Failed to parse security opt");
        goto out;
    }

O
overweight 已提交
1964
    // merge external parameter
W
wujing 已提交
1965
    ret = merge_seccomp(oci_spec, seccomp_profile);
O
overweight 已提交
1966 1967 1968 1969
    if (ret != 0) {
        ERROR("Failed to merge user seccomp file");
        goto out;
    }
W
wujing 已提交
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
    v2_spec->seccomp_profile = util_strdup_s(seccomp_profile);

    ret = merge_no_new_privileges(oci_spec, no_new_privileges);
    if (ret != 0) {
        ERROR("Failed to merge no new privileges");
        goto out;
    }
    v2_spec->no_new_privileges = no_new_privileges;

    ret = merge_selinux(oci_spec, v2_spec, (const char **)label_opts, label_opts_len);
    if (ret != 0) {
        ERROR("Failed to merge selinux config");
        goto out;
    }
O
overweight 已提交
1984 1985

out:
W
wujing 已提交
1986 1987
    util_free_array(label_opts);
    free(seccomp_profile);
O
overweight 已提交
1988 1989 1990 1991
    return ret;
}


1992 1993
int merge_all_specs(host_config *host_spec, const char *real_rootfs,
                    container_config_v2_common_config *v2_spec, oci_runtime_spec *oci_spec)
O
overweight 已提交
1994 1995 1996
{
    int ret = 0;

1997
    ret = merge_root(oci_spec, real_rootfs, host_spec);
O
overweight 已提交
1998
    if (ret != 0) {
1999
        ERROR("Failed to merge root");
O
overweight 已提交
2000 2001
        goto out;
    }
2002
    v2_spec->base_fs = util_strdup_s(real_rootfs);
O
overweight 已提交
2003

W
wujing 已提交
2004 2005 2006 2007 2008
    ret = merge_security_conf(oci_spec, host_spec, v2_spec);
    if (ret != 0) {
        ERROR("Failed to merge user security config");
        goto out;
    }
2009 2010 2011 2012 2013 2014 2015

    ret = merge_resources_conf(oci_spec, host_spec, v2_spec);
    if (ret != 0) {
        goto out;
    }

    ret = merge_process_conf(oci_spec, host_spec, v2_spec->config);
O
overweight 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
    if (ret != 0) {
        goto out;
    }

    // merge sysctl
    ret = merge_sysctls(oci_spec, host_spec->sysctls);
    if (ret != 0) {
        ret = -1;
        goto out;
    }

    /* settings for system container */
2028
    ret = merge_settings_for_system_container(oci_spec, host_spec, v2_spec->config);
O
overweight 已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
    if (ret != 0) {
        ERROR("Failed to merge system container conf");
        goto out;
    }

    /* settings for privileged */
    ret = adapt_settings_for_privileged(oci_spec, host_spec->privileged);
    if (ret != 0) {
        ERROR("Failed to adapt settings for privileged container");
        goto out;
    }

2041
    ret = merge_hostname(oci_spec, host_spec, v2_spec->config);
O
overweight 已提交
2042 2043 2044 2045 2046
    if (ret != 0) {
        ERROR("Failed to merge hostname");
        goto out;
    }

2047
    ret = make_annotations(oci_spec, v2_spec->config, host_spec);
O
overweight 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
    if (ret != 0) {
        ret = -1;
        goto out;
    }

    ret = make_userns_remap(oci_spec, host_spec->user_remap);
    if (ret != 0) {
        ERROR("Failed to make user remap for container");
        goto out;
    }

out:
    return ret;
}

L
LiFeng 已提交
2063
int merge_oci_cgroups_path(const char *id, oci_runtime_spec *oci_spec, const host_config *host_spec)
2064 2065 2066 2067 2068
{
    int ret = 0;
    char *default_cgroup_parent = NULL;
    char *path = NULL;

L
LiFeng 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
    if (id == NULL || oci_spec == NULL || host_spec == NULL) {
        ERROR("Invalid arguments");
        ret = -1;
        goto out;
    }

    if (make_sure_oci_spec_linux(oci_spec) != 0) {
        ERROR("Failed to make oci spec linux");
        ret = -1;
        goto out;
    }

2081 2082 2083 2084 2085 2086 2087
    default_cgroup_parent = conf_get_isulad_cgroup_parent();
    path = default_cgroup_parent;
    if (host_spec->cgroup_parent != NULL) {
        path = host_spec->cgroup_parent;
    }

    if (path == NULL) {
L
LiFeng 已提交
2088 2089
        free(oci_spec->linux->cgroups_path);
        oci_spec->linux->cgroups_path = util_path_join("/isulad", id);
2090 2091 2092
        return 0;
    }

L
LiFeng 已提交
2093 2094
    free(oci_spec->linux->cgroups_path);
    oci_spec->linux->cgroups_path = util_path_join(path, id);
2095 2096 2097 2098 2099 2100

out:
    UTIL_FREE_AND_SET_NULL(default_cgroup_parent);
    return ret;
}

O
overweight 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
/* merge the default config with host config and custom config */
int merge_global_config(oci_runtime_spec *oci_spec)
{
    int ret = 0;

    ret = merge_global_hook(oci_spec);
    if (ret != 0) {
        ERROR("Failed to merge global hooks");
        goto out;
    }

    ret = merge_global_ulimit(oci_spec);
    if (ret != 0) {
        ERROR("Failed to merge global ulimit");
        goto out;
    }

out:
    return ret;
}

/* read oci config */
2123
oci_runtime_spec *load_oci_config(const char *rootpath, const char *name)
O
overweight 已提交
2124 2125 2126 2127 2128 2129
{
    int nret;
    char filename[PATH_MAX] = { 0x00 };
    parser_error err = NULL;
    oci_runtime_spec *ociconfig = NULL;

2130
    nret = snprintf(filename, sizeof(filename), "%s/%s/%s", rootpath, name, OCI_CONFIG_JSON);
O
openeuler-iSula 已提交
2131
    if (nret < 0 || (size_t)nret >= sizeof(filename)) {
O
overweight 已提交
2132 2133 2134 2135 2136 2137 2138
        ERROR("Failed to print string");
        goto out;
    }

    ociconfig = oci_runtime_spec_parse_file(filename, NULL, &err);
    if (ociconfig == NULL) {
        ERROR("Failed to parse oci config file:%s", err);
L
LiuHao 已提交
2139
        isulad_set_error_message("Parse oci config file failed:%s", err);
O
overweight 已提交
2140 2141 2142 2143 2144 2145
        goto out;
    }
out:
    free(err);
    return ociconfig;
}
Z
zhuchunyi 已提交
2146

2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
int save_oci_config(const char *id, const char *rootpath, const oci_runtime_spec *oci_spec)
{
    int ret = 0;
    int nret = 0;
    char *json_container = NULL;
    char file_path[PATH_MAX] = { 0x0 };
    struct parser_context ctx = { OPT_PARSE_STRICT, stderr };
    parser_error err = NULL;

    nret = snprintf(file_path, PATH_MAX, "%s/%s/%s", rootpath, id, OCI_CONFIG_JSON);
    if (nret < 0 || nret >= PATH_MAX) {
        ERROR("Failed to print string");
        ret = -1;
        goto out_free;
    }

    json_container = oci_runtime_spec_generate_json(oci_spec, &ctx, &err);
    if (json_container == NULL) {
        ERROR("Failed to generate json: %s", err);
        ret = -1;
        goto out_free;
    }

2170
    if (util_write_file(file_path, json_container, strlen(json_container), DEFAULT_SECURE_FILE_MODE) != 0) {
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
        ERROR("write json container failed: %s", strerror(errno));
        ret = -1;
        goto out_free;
    }

out_free:
    free(err);
    free(json_container);
    return ret;
}