verify.c 55.2 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * 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.
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide container verify functions
 ******************************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <unistd.h>
#include <sys/utsname.h>
#include <libgen.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <linux/oom.h>

#include "constants.h"
#include "error.h"
L
LiuHao 已提交
30
#include "libisulad.h"
H
haozi007 已提交
31
#include "isula_libutils/log.h"
O
overweight 已提交
32
#include "sysinfo.h"
33
#include "specs_api.h"
O
overweight 已提交
34
#include "verify.h"
L
LiuHao 已提交
35
#include "isulad_config.h"
W
wujing 已提交
36
#include "selinux_label.h"
L
lifeng68 已提交
37
#include "image_api.h"
O
overweight 已提交
38 39 40 41 42 43

/* verify hook timeout */
static int verify_hook_timeout(int t)
{
    if (t < 0) {
        ERROR("Hook spec timeout invalid");
L
LiuHao 已提交
44
        isulad_set_error_message("Invalid timeout: %d", t);
O
overweight 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
        return -1;
    }

    return 0;
}

/* verify hook path */

static int verify_hook_root_and_no_other(const struct stat *st, const char *path)
{
    /* validate file owner */
    if (st->st_uid != 0) {
        ERROR("Hook file %s isn't right,file owner should be root", path);
L
LiuHao 已提交
58
        isulad_set_error_message("Hook file %s isn't right,file owner should be root", path);
O
overweight 已提交
59 60 61 62 63 64
        return -1;
    }

    /* validate file if can be written by other user */
    if (st->st_mode & S_IWOTH) {
        ERROR("Hook path %s isn't right,file should not be written by non-root", path);
L
LiuHao 已提交
65
        isulad_set_error_message("%s should not be written by non-root", path);
O
overweight 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        return -1;
    }
    return 0;
}

static int verify_hook_path(const char *path)
{
    int ret = 0;
    struct stat st;

    /* validate absolute path */
    ret = util_validate_absolute_path(path);
    if (ret != 0) {
        ERROR("Hook path %s must be an absolute path", path);
L
LiuHao 已提交
80
        isulad_set_error_message("%s is not an absolute path", path);
O
overweight 已提交
81 82 83 84 85 86 87
        goto out;
    }

    ret = stat(path, &st);
    /* validate file exits */
    if (ret < 0) {
        ERROR("Hook path %s isn't exist", path);
L
LiuHao 已提交
88
        isulad_set_error_message("Cann't find path: %s", path);
O
overweight 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        ret = -1;
        goto out;
    }

    if (verify_hook_root_and_no_other(&st, path) != 0) {
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static int verify_hook_conf(const defs_hook *p)
{
    int ret = 0;
    /* validate hookpath */
    ret = verify_hook_path(p->path);
    if (ret != 0) {
        goto out;
    }
    /* validate timeout */
    ret = verify_hook_timeout(p->timeout);
out:
    return ret;
}

static inline bool is_mem_limit_minimum(int64_t limit)
{
    /* It's not kernel limit, we want this 4M limit to supply a reasonable functional container */
#define LINUX_MIN_MEMORY 4194304

    return limit != 0 && limit < LINUX_MIN_MEMORY;
}

/* check memroy limit and memory swap */
static int verify_mem_limit_swap(const sysinfo_t *sysinfo, int64_t limit, int64_t swap, bool update)
{
    int ret = 0;

    /* check the minimum memory limit */
    if (is_mem_limit_minimum(limit)) {
        ERROR("Minimum memory limit allowed is 4MB");
L
LiuHao 已提交
132
        isulad_set_error_message("Minimum memory limit allowed is 4MB");
O
overweight 已提交
133 134 135 136 137 138
        ret = -1;
        goto out;
    }

    if (limit > 0 && !(sysinfo->cgmeminfo.limit)) {
        ERROR("Your kernel does not support memory limit capabilities. Limitation discarded.");
L
lifeng68 已提交
139
        isulad_set_error_message("Your kernel does not support memory limit capabilities. Limitation discarded.");
O
overweight 已提交
140 141 142 143 144 145
        ret = -1;
        goto out;
    }

    if (limit > 0 && swap != 0 && !(sysinfo->cgmeminfo.swap)) {
        ERROR("Your kernel does not support swap limit capabilities, memory limited without swap.");
L
lifeng68 已提交
146
        isulad_set_error_message("Your kernel does not support swap limit capabilities, memory limited without swap.");
O
overweight 已提交
147 148 149 150 151 152
        ret = -1;
        goto out;
    }

    if (limit > 0 && swap > 0 && swap < limit) {
        ERROR("Minimum memoryswap limit should be larger than memory limit, see usage.");
L
LiuHao 已提交
153
        isulad_set_error_message("Minimum memoryswap limit should be larger than memory limit");
O
overweight 已提交
154 155 156 157 158 159
        ret = -1;
        goto out;
    }

    if (limit == 0 && swap > 0 && !update) {
        ERROR("You should always set the Memory limit when using Memoryswap limit, see usage.");
L
LiuHao 已提交
160
        isulad_set_error_message("You should set the memory limit when using memoryswap limit");
O
overweight 已提交
161 162 163 164 165 166 167 168 169 170
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static inline bool is_swappiness_invalid(uint64_t swapiness)
{
L
LiFeng 已提交
171
    return (int64_t)swapiness < -1 || (int64_t)swapiness > 100;
O
overweight 已提交
172 173 174 175 176 177 178 179 180
}

/* verify memory swappiness */
static int verify_memory_swappiness(const sysinfo_t *sysinfo, uint64_t swapiness)
{
    int ret = 0;

    if ((int64_t)swapiness != -1 && !(sysinfo->cgmeminfo.swappiness)) {
        ERROR("Your kernel does not support memory swappiness capabilities, memory swappiness discarded.");
L
LiuHao 已提交
181
        isulad_set_error_message(
L
lifeng68 已提交
182
                "Your kernel does not support memory swappiness capabilities, memory swappiness discarded.");
O
overweight 已提交
183 184 185 186 187 188
        ret = -1;
        goto out;
    }

    if (is_swappiness_invalid(swapiness)) {
        ERROR("Invalid value: %lld, valid memory swappiness range is 0-100", (long long)swapiness);
L
LiuHao 已提交
189
        isulad_set_error_message("Invalid value: %lld, valid memory swappiness range is 0-100", (long long)swapiness);
O
overweight 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        ret = -1;
        goto out;
    }

out:
    return ret;
}

/* verify memory reservation */
static int verify_memory_reservation(const sysinfo_t *sysinfo, int64_t limit, int64_t reservation)
{
    int ret = 0;

    if (reservation > 0 && !(sysinfo->cgmeminfo.reservation)) {
        ERROR("Your kernel does not support memory soft limit capabilities. Limitation discarded");
L
LiuHao 已提交
205
        isulad_set_error_message("Your kernel does not support memory soft limit capabilities. Limitation discarded");
O
overweight 已提交
206 207 208 209 210 211 212
        ret = -1;
        goto out;
    }

    /* check the minimum memory limit */
    if (is_mem_limit_minimum(reservation)) {
        ERROR("Minimum memory reservation allowed is 4MB");
L
LiuHao 已提交
213
        isulad_set_error_message("Minimum memory reservation allowed is 4MB");
O
overweight 已提交
214 215 216 217 218 219
        ret = -1;
        goto out;
    }

    if (limit > 0 && reservation > 0 && limit < reservation) {
        ERROR("Minimum memory limit should be larger than memory reservation limit, see usage.");
L
LiuHao 已提交
220
        isulad_set_error_message("Minimum memory limit should be larger than memory reservation limit, see usage.");
O
overweight 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        ret = -1;
        goto out;
    }

out:
    return ret;
}

/* check kernel version */
static bool check_kernel_version(const char *version)
{
    struct utsname uts;
    int ret;

    ret = uname(&uts);
    if (ret < 0) {
        WARN("Can not get kernel version: %s", strerror(errno));
    } else {
        if (strverscmp(uts.release, version) < 0) {
            return false;
        }
    }
    return true;
}

/* verify memory kernel */
static int verify_memory_kernel(const sysinfo_t *sysinfo, int64_t kernel)
{
    int ret = 0;

    if (kernel > 0 && !(sysinfo->cgmeminfo.kernel)) {
        ERROR("Your kernel does not support kernel memory limit capabilities. Limitation discarded.");
L
lifeng68 已提交
253 254
        isulad_set_error_message(
                "Your kernel does not support kernel memory limit capabilities. Limitation discarded.");
O
overweight 已提交
255 256 257 258 259 260
        ret = -1;
        goto out;
    }

    if (is_mem_limit_minimum(kernel)) {
        ERROR("Minimum kernel memory limit allowed is 4MB");
L
LiuHao 已提交
261
        isulad_set_error_message("Minimum kernel memory limit allowed is 4MB");
O
overweight 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        ret = -1;
        goto out;
    }

    if (kernel > 0 && !check_kernel_version("4.0.0")) {
        WARN("You specified a kernel memory limit on a kernel older than 4.0. "
             "Kernel memory limits are experimental on older kernels, "
             "it won't work as expected and can cause your system to be unstable.");
    }

out:
    return ret;
}

/* verify pids limit */
static int verify_pids_limit(const sysinfo_t *sysinfo, int64_t pids_limit)
{
    int ret = 0;

    if (pids_limit != 0 && !(sysinfo->pidsinfo.pidslimit)) {
        ERROR("Your kernel does not support pids limit capabilities, pids limit discarded.");
L
LiuHao 已提交
283
        isulad_set_error_message("Your kernel does not support pids limit capabilities, pids limit discarded.");
O
overweight 已提交
284 285 286 287 288 289 290 291 292 293 294 295
        ret = -1;
    }
    return ret;
}

/* verify files limit */
static int verify_files_limit(const sysinfo_t *sysinfo, int64_t files_limit)
{
    int ret = 0;

    if (files_limit != 0 && !(sysinfo->filesinfo.fileslimit)) {
        ERROR("Your kernel does not support files limit capabilities, files limit discarded.");
L
LiuHao 已提交
296
        isulad_set_error_message("Your kernel does not support files limit capabilities, files limit discarded.");
O
overweight 已提交
297 298 299 300 301 302 303 304 305 306 307 308
        ret = -1;
    }
    return ret;
}

/* verify oom control */
static int verify_oom_control(const sysinfo_t *sysinfo, bool oomdisable)
{
    int ret = 0;

    if (oomdisable && !(sysinfo->cgmeminfo.oomkilldisable)) {
        ERROR("Your kernel does not support OomKillDisable, OomKillDisable discarded");
L
LiuHao 已提交
309
        isulad_set_error_message("Your kernel does not support OomKillDisable, OomKillDisable discarded");
O
overweight 已提交
310 311 312 313 314 315 316
        ret = -1;
    }

    return ret;
}

/* verify resources memory */
317
static int verify_resources_memory(const sysinfo_t *sysinfo, const defs_resources_memory *memory)
O
overweight 已提交
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
{
    int ret = 0;

    ret = verify_mem_limit_swap(sysinfo, memory->limit, memory->swap, false);
    if (ret != 0) {
        goto out;
    }

    ret = verify_memory_swappiness(sysinfo, memory->swappiness);
    if (ret != 0) {
        goto out;
    }

    ret = verify_memory_reservation(sysinfo, memory->limit, memory->reservation);
    if (ret != 0) {
        goto out;
    }

    ret = verify_memory_kernel(sysinfo, memory->kernel);
    if (ret != 0) {
        goto out;
    }

    ret = verify_oom_control(sysinfo, memory->disable_oom_killer);
    if (ret != 0) {
        goto out;
    }

out:
    return ret;
}

/* verify resources pids */
351
static int verify_resources_pids(const sysinfo_t *sysinfo, const defs_resources_pids *pids)
O
overweight 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
    int ret = 0;

    ret = verify_pids_limit(sysinfo, pids->limit);
    if (ret != 0) {
        goto out;
    }

out:
    return ret;
}

// ValidateResources performs platform specific validation of the resource settings
// cpu-rt-runtime and cpu-rt-period can not be greater than their parent, cpu-rt-runtime requires sys_nice
static int verify_cpu_realtime(const sysinfo_t *sysinfo, int64_t realtime_period, int64_t realtime_runtime)
{
    int ret = 0;

    if (realtime_period > 0 && !(sysinfo->cgcpuinfo.cpu_rt_period)) {
        ERROR("Invalid --cpu-rt-period: Your kernel does not support cgroup rt period");
L
LiuHao 已提交
372
        isulad_set_error_message("Invalid --cpu-rt-period: Your kernel does not support cgroup rt period");
O
overweight 已提交
373 374 375 376 377 378
        ret = -1;
        goto out;
    }

    if (realtime_runtime > 0 && !(sysinfo->cgcpuinfo.cpu_rt_runtime)) {
        ERROR("Invalid --cpu-rt-runtime: Your kernel does not support cgroup rt runtime");
L
LiuHao 已提交
379
        isulad_set_error_message("Invalid --cpu-rt-period: Your kernel does not support cgroup rt runtime");
O
overweight 已提交
380 381 382 383 384 385
        ret = -1;
        goto out;
    }

    if (realtime_period != 0 && realtime_runtime != 0 && realtime_runtime > realtime_period) {
        ERROR("Invalid --cpu-rt-runtime: rt runtime cannot be higher than rt period");
L
LiuHao 已提交
386
        isulad_set_error_message("Invalid --cpu-rt-runtime: rt runtime cannot be higher than rt period");
O
overweight 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400
        ret = -1;
        goto out;
    }
out:
    return ret;
}

/* verify cpu shares */
static int verify_cpu_shares(const sysinfo_t *sysinfo, int64_t cpu_shares)
{
    int ret = 0;

    if (cpu_shares > 0 && !(sysinfo->cgcpuinfo.cpu_shares)) {
        ERROR("Your kernel does not support cgroup cpu shares. Shares discarded.");
L
lifeng68 已提交
401
        isulad_set_error_message("Your kernel does not support cgroup cpu shares. Shares discarded.");
O
overweight 已提交
402 403 404 405 406 407 408 409 410 411 412 413
        ret = -1;
    }

    return ret;
}

static int verify_cpu_cfs_period(const sysinfo_t *sysinfo, int64_t cpu_cfs_period)
{
    int ret = 0;

    if (cpu_cfs_period > 0 && !(sysinfo->cgcpuinfo.cpu_cfs_period)) {
        ERROR("Your kernel does not support CPU cfs period. Period discarded.");
L
LiuHao 已提交
414
        isulad_set_error_message("Your kernel does not support CPU cfs period. Period discarded.");
O
overweight 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
        ret = -1;
        goto out;
    }
out:
    return ret;
}

static inline bool is_cpu_cfs_quota_invalid(int64_t cpu_cfs_quota)
{
    return cpu_cfs_quota > 0 && cpu_cfs_quota < 1000;
}

static int verify_cpu_cfs_quota(const sysinfo_t *sysinfo, int64_t cpu_cfs_quota)
{
    int ret = 0;

    if (cpu_cfs_quota > 0 && !(sysinfo->cgcpuinfo.cpu_cfs_quota)) {
        ERROR("Your kernel does not support CPU cfs quato. Quota discarded.");
L
LiuHao 已提交
433
        isulad_set_error_message("Your kernel does not support CPU cfs quato. Quota discarded.");
O
overweight 已提交
434 435 436 437 438 439
        ret = -1;
        goto out;
    }

    if (is_cpu_cfs_quota_invalid(cpu_cfs_quota)) {
        ERROR("CPU cfs quota can not be less than 1ms (i.e. 1000)");
L
LiuHao 已提交
440
        isulad_set_error_message("CPU cfs quota can not be less than 1ms (i.e. 1000)");
O
overweight 已提交
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 479 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
        ret = -1;
        goto out;
    }
out:
    return ret;
}

/* verify cpu cfs scheduler */
static int verify_cpu_cfs_scheduler(const sysinfo_t *sysinfo, int64_t cpu_cfs_period, int64_t cpu_cfs_quota)
{
    int ret = 0;

    ret = verify_cpu_cfs_period(sysinfo, cpu_cfs_period);
    if (ret != 0) {
        goto out;
    }

    ret = verify_cpu_cfs_quota(sysinfo, cpu_cfs_quota);
    if (ret != 0) {
        goto out;
    }

out:
    return ret;
}

/* max cpu */
int max_cpu(const char *cores)
{
    int max = -1;
    char *str = NULL;
    char *tmp = NULL;
    char *chr = NULL;

    str = util_strdup_s(cores);
    if (str == NULL) {
        goto out;
    }
    tmp = str;
    chr = strchr(tmp, ',');
    for (; chr != NULL || *tmp != '\0'; chr = strchr(tmp, ',')) {
        char *subchr = NULL;
        int value = 0;
        if (chr != NULL) {
            *chr++ = '\0';
        }
        subchr = strchr(tmp, '-');
        if (subchr != NULL) {
            *subchr++ = '\0';
        } else {
            subchr = tmp;
        }
        if (util_safe_int(subchr, &value) || value < 0) {
            max = -1;
            goto out;
        }

        if (value > max) {
            max = value;
        }
        if (chr != NULL) {
            tmp = chr;
        } else {
            break;
        }
    }
out:
    free(str);
    return max;
}

/* check cpu */
static bool check_cpu(const char *provided, const char *available)
{
    int max_available = 0;
    int max_request = 0;
    if (provided == NULL) {
        return true;
    }
    max_available = max_cpu(available);
    max_request = max_cpu(provided);
    if (max_available == -1 || max_request == -1) {
        ERROR("failed to get the number of cpus");
        return false;
    }
    if (max_request > max_available) {
        ERROR("invalid maxRequest is %d, max available: %d", max_request, max_available);
L
LiuHao 已提交
528
        isulad_set_error_message("invalid maxRequest is %d, max available: %d", max_request, max_available);
O
overweight 已提交
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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
        return false;
    }
    return true;
}

/* parse unit list */
int parse_unit_list(const char *val, bool *available_list)
{
    int ret = -1;
    char *str = NULL;
    char *tmp = NULL;
    char *chr = NULL;
    if (val == NULL) {
        return 0;
    }
    str = util_strdup_s(val);
    tmp = str;
    chr = strchr(tmp, ',');
    for (; chr != NULL || *tmp != '\0'; chr = strchr(tmp, ',')) {
        char *subchr = NULL;
        if (chr != NULL) {
            *chr++ = '\0';
        }
        subchr = strchr(tmp, '-');
        if (subchr == NULL) {
            int value = 0;
            if (util_safe_int(tmp, &value) || value < 0) {
                goto out;
            }
            available_list[value] = true;
        } else {
            int min = 0;
            int max = 0;
            int i = 0;
            *subchr++ = '\0';
            if (util_safe_int(tmp, &min) || min < 0) {
                goto out;
            }
            if (util_safe_int(subchr, &max) || max < 0) {
                goto out;
            }
            for (i = min; i <= max; i++) {
                available_list[i] = true;
            }
        }
        if (chr != NULL) {
            tmp = chr;
        } else {
            break;
        }
    }
    ret = 0;
out:
    free(str);
    return ret;
}

/* is cpuset list available */
static bool is_cpuset_list_available(const char *provided, const char *available)
{
    int cpu_num = 0;
    int i = 0;
    bool ret = false;
    bool *parsed_provided = NULL;
    bool *parsed_available = NULL;

    cpu_num = get_nprocs();
    if (cpu_num <= 0) {
        ERROR("failed to get the number of processors configured by the operating system!");
        goto out;
    }
    if ((size_t)cpu_num > SIZE_MAX / sizeof(bool)) {
        ERROR("invalid cpu num");
        goto out;
    }
    parsed_provided = util_common_calloc_s(sizeof(bool) * (unsigned int)cpu_num);
    if (parsed_provided == NULL) {
        ERROR("memory alloc failed!");
        goto out;
    }
    parsed_available = util_common_calloc_s(sizeof(bool) * (unsigned int)cpu_num);
    if (parsed_available == NULL) {
        ERROR("memory alloc failed!");
        goto out;
    }

    if (!check_cpu(provided, available)) {
        goto out;
    }

L
lifeng68 已提交
619
    if (parse_unit_list(provided, parsed_provided) < 0 || parse_unit_list(available, parsed_available) < 0) {
O
overweight 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
        goto out;
    }
    for (i = 0; i < cpu_num; i++) {
        if (!parsed_provided[i] || parsed_available[i]) {
            continue;
        }
        goto out;
    }
    ret = true;
out:
    free(parsed_provided);
    free(parsed_available);
    return ret;
}

/* is cpuset cpus available */
bool is_cpuset_cpus_available(const sysinfo_t *sysinfo, const char *cpus)
{
    bool ret = false;
    ret = is_cpuset_list_available(cpus, sysinfo->cpusetinfo.cpus);
    if (!ret) {
        ERROR("Checking cpuset.cpus got invalid format: %s.", cpus);
L
LiuHao 已提交
642
        isulad_set_error_message("Checking cpuset.cpus got invalid format: %s.", cpus);
O
overweight 已提交
643 644 645 646 647 648 649 650 651 652 653
    }
    return ret;
}

/* is cpuset mems available */
bool is_cpuset_mems_available(const sysinfo_t *sysinfo, const char *mems)
{
    bool ret = false;
    ret = is_cpuset_list_available(mems, sysinfo->cpusetinfo.mems);
    if (!ret) {
        ERROR("Checking cpuset.mems got invalid format: %s.", mems);
L
LiuHao 已提交
654
        isulad_set_error_message("Checking cpuset.mems got invalid format: %s.", mems);
O
overweight 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667
    }
    return ret;
}

// cpuset subsystem checks and adjustments
static int verify_resources_cpuset(const sysinfo_t *sysinfo, const char *cpus, const char *mems)
{
    int ret = 0;
    bool cpus_available = false;
    bool mems_available = false;

    if (cpus != NULL && !(sysinfo->cpusetinfo.cpuset)) {
        ERROR("Your kernel does not support cpuset. Cpuset discarded.");
L
LiuHao 已提交
668
        isulad_set_error_message("Your kernel does not support cpuset. Cpuset discarded.");
O
overweight 已提交
669 670 671 672 673 674
        ret = -1;
        goto out;
    }

    if (mems != NULL && !(sysinfo->cpusetinfo.cpuset)) {
        ERROR("Your kernel does not support cpuset. Cpuset discarded.");
L
LiuHao 已提交
675
        isulad_set_error_message("Your kernel does not support cpuset. Cpuset discarded.");
O
overweight 已提交
676 677 678 679 680 681 682
        ret = -1;
        goto out;
    }

    cpus_available = is_cpuset_cpus_available(sysinfo, cpus);
    if (!cpus_available) {
        ERROR("Requested CPUs are not available - requested %s, available: %s.", cpus, sysinfo->cpusetinfo.cpus);
L
LiuHao 已提交
683 684
        isulad_set_error_message("Requested CPUs are not available - requested %s, available: %s.", cpus,
                                 sysinfo->cpusetinfo.cpus);
O
overweight 已提交
685 686 687 688 689 690
        ret = -1;
        goto out;
    }

    mems_available = is_cpuset_mems_available(sysinfo, mems);
    if (!mems_available) {
L
lifeng68 已提交
691 692 693 694
        ERROR("Requested memory nodes are not available - requested %s, available: %s.", mems,
              sysinfo->cpusetinfo.mems);
        isulad_set_error_message("Requested memory nodes are not available - requested %s, available: %s.", mems,
                                 sysinfo->cpusetinfo.mems);
O
overweight 已提交
695 696 697 698 699 700 701 702 703
        ret = -1;
        goto out;
    }

out:
    return ret;
}

/* verify resources cpu */
704
static int verify_resources_cpu(const sysinfo_t *sysinfo, const defs_resources_cpu *cpu)
O
overweight 已提交
705 706 707 708 709 710 711 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
{
    int ret = 0;

    ret = verify_cpu_realtime(sysinfo, (int64_t)(cpu->realtime_period), cpu->realtime_runtime);
    if (ret != 0) {
        goto out;
    }

    ret = verify_cpu_shares(sysinfo, (int64_t)(cpu->shares));
    if (ret != 0) {
        goto out;
    }

    ret = verify_cpu_cfs_scheduler(sysinfo, (int64_t)(cpu->period), cpu->quota);
    if (ret != 0) {
        goto out;
    }

    ret = verify_resources_cpuset(sysinfo, cpu->cpus, cpu->mems);
    if (ret != 0) {
        goto out;
    }
out:
    return ret;
}

static inline bool is_blkio_weight_invalid(int weight)
{
    return weight > 0 && (weight < 10 || weight > 1000);
}

/* verify blkio weight */
static int verify_blkio_weight(const sysinfo_t *sysinfo, int weight)
{
    int ret = 0;

    if (weight > 0 && !(sysinfo->blkioinfo.blkio_weight)) {
        ERROR("Your kernel does not support Block I/O weight. Weight discarded.");
L
LiuHao 已提交
743
        isulad_set_error_message("Your kernel does not support Block I/O weight. Weight discarded.");
O
overweight 已提交
744 745 746 747 748
        ret = -1;
        goto out;
    }
    if (is_blkio_weight_invalid(weight)) {
        ERROR("Range of blkio weight is from 10 to 1000.");
L
LiuHao 已提交
749
        isulad_set_error_message("Range of blkio weight is from 10 to 1000.");
O
overweight 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static inline bool is_hostconfig_blkio_weight_invalid(uint16_t weight)
{
    return weight > 0 && (weight < 10 || weight > 1000);
}

/* verify hostconfig blkio weight */
static int verify_hostconfig_blkio_weight(const sysinfo_t *sysinfo, uint16_t weight)
{
    int ret = 0;

    if (weight > 0 && !(sysinfo->blkioinfo.blkio_weight)) {
        ERROR("Your kernel does not support Block I/O weight. Weight in host config discarded.");
L
LiuHao 已提交
770
        isulad_set_error_message("Your kernel does not support Block I/O weight. Weight in host config discarded.");
O
overweight 已提交
771 772 773 774 775
        ret = -1;
        goto out;
    }
    if (is_hostconfig_blkio_weight_invalid(weight)) {
        ERROR("Range of blkio weight is from 10 to 1000.");
L
LiuHao 已提交
776
        isulad_set_error_message("Range of blkio weight is from 10 to 1000.");
O
overweight 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
        ret = -1;
        goto out;
    }

out:
    return ret;
}

/* verify blkio device */
static int verify_blkio_device(const sysinfo_t *sysinfo, size_t weight_device_len)
{
    int ret = 0;

    if (weight_device_len > 0 && !(sysinfo->blkioinfo.blkio_weight_device)) {
        ERROR("Your kernel does not support Block I/O weight_device.");
L
LiuHao 已提交
792
        isulad_set_error_message("Your kernel does not support Block I/O weight_device.");
O
overweight 已提交
793 794 795 796 797 798 799 800 801 802 803 804
        ret = -1;
    }

    return ret;
}

/* verify oom score adj */
static int verify_oom_score_adj(int oom_score_adj)
{
    int ret = 0;
    if (oom_score_adj < OOM_SCORE_ADJ_MIN || oom_score_adj > OOM_SCORE_ADJ_MAX) {
        ERROR("Invalid value %d, range for oom score adj is [-1000, 1000].", oom_score_adj);
L
LiuHao 已提交
805
        isulad_set_error_message("Invalid value %d, range for oom score adj is [-1000, 1000].", oom_score_adj);
O
overweight 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819
        ret = -1;
    }
    return ret;
}

#ifdef ENABLE_OCI_IMAGE
static bool is_storage_opts_valid(const json_map_string_string *storage_opts)
{
    size_t i;

    for (i = 0; i < storage_opts->len; i++) {
        if (strcmp(storage_opts->keys[i], "size") != 0) {
            // Only check key here, check value by image driver
            ERROR("Unknown storage option: %s", storage_opts->keys[i]);
L
LiuHao 已提交
820
            isulad_set_error_message("Unknown storage option: %s", storage_opts->keys[i]);
O
overweight 已提交
821 822 823 824 825 826 827 828 829 830 831
            return false;
        }
    }
    return true;
}

/* verify storage options */
static int verify_storage_opts(const host_config *hc)
{
    int ret = 0;
    json_map_string_string *storage_opts = NULL;
832
    struct graphdriver_status *driver_status = NULL;
O
overweight 已提交
833 834 835 836 837

    if (hc != NULL) {
        storage_opts = hc->storage_opt;
    }

838
    driver_status = im_graphdriver_get_status();
839 840 841 842
    if (driver_status == NULL) {
        ERROR("Failed to get graph driver status info!");
        ret = -1;
        goto cleanup;
O
overweight 已提交
843 844
    }

845
    if (storage_opts == NULL || storage_opts->len == 0 || strcmp(driver_status->driver_name, "overlay2") != 0) {
O
overweight 已提交
846 847 848 849
        goto cleanup;
    }

    if (storage_opts->len > 0) {
850
        if (strcmp(driver_status->backing_fs, "xfs") == 0) {
O
overweight 已提交
851 852 853 854 855 856 857 858 859 860
            WARN("Filesystem quota for overlay2 over xfs is not totally support");
        }
    }

    if (!is_storage_opts_valid(storage_opts)) {
        ret = -1;
        goto cleanup;
    }

cleanup:
861
    im_free_graphdriver_status(driver_status);
O
overweight 已提交
862 863 864 865 866 867 868 869 870 871 872 873
    return ret;
}
#endif

/* verify blkio rw bps device */
static int verify_blkio_rw_bps_device(const sysinfo_t *sysinfo, size_t throttle_read_bps_device_len,
                                      size_t throttle_write_bps_device_len)
{
    int ret = 0;

    if (throttle_read_bps_device_len > 0 && !(sysinfo->blkioinfo.blkio_read_bps_device)) {
        ERROR("Your kernel does not support Block read limit in bytes per second");
L
LiuHao 已提交
874
        isulad_set_error_message("Your kernel does not support Block read limit in bytes per second");
O
overweight 已提交
875 876 877 878 879 880
        ret = -1;
        goto out;
    }

    if (throttle_write_bps_device_len > 0 && !(sysinfo->blkioinfo.blkio_write_bps_device)) {
        ERROR("Your kernel does not support Block write limit in bytes per second");
L
LiuHao 已提交
881
        isulad_set_error_message("Your kernel does not support Block write limit in bytes per second");
O
overweight 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
        ret = -1;
        goto out;
    }
out:
    return ret;
}

/* verify blkio rw iops device */
static int verify_blkio_rw_iops_device(const sysinfo_t *sysinfo, size_t throttle_read_iops_device_len,
                                       size_t throttle_write_iops_device_len)
{
    int ret = 0;

    if (throttle_read_iops_device_len > 0 && !(sysinfo->blkioinfo.blkio_read_iops_device)) {
        ERROR("Your kernel does not support Block read limit in IO per second");
L
LiuHao 已提交
897
        isulad_set_error_message("Your kernel does not support Block read limit in IO per second");
O
overweight 已提交
898 899 900 901 902 903
        ret = -1;
        goto out;
    }

    if (throttle_write_iops_device_len > 0 && !(sysinfo->blkioinfo.blkio_write_iops_device)) {
        ERROR("Your kernel does not support Block write limit in IO per second");
L
LiuHao 已提交
904
        isulad_set_error_message("Your kernel does not support Block write limit in IO per second");
O
overweight 已提交
905 906 907 908 909 910 911 912
        ret = -1;
        goto out;
    }
out:
    return ret;
}

/* verify resources blkio */
913
static int verify_resources_blkio(const sysinfo_t *sysinfo, const defs_resources_block_io *blkio)
O
overweight 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
{
    int ret = 0;

    ret = verify_blkio_weight(sysinfo, blkio->weight);
    if (ret != 0) {
        goto out;
    }

    ret = verify_blkio_device(sysinfo, blkio->weight_device_len);
    if (ret != 0) {
        goto out;
    }

    ret = verify_blkio_rw_bps_device(sysinfo, blkio->throttle_read_bps_device_len,
                                     blkio->throttle_write_bps_device_len);
    if (ret != 0) {
        goto out;
    }

    ret = verify_blkio_rw_iops_device(sysinfo, blkio->throttle_read_iops_device_len,
                                      blkio->throttle_write_iops_device_len);
    if (ret != 0) {
        goto out;
    }
out:
    return ret;
}

static bool check_hugetlbs_repeated(size_t newlen, const char *pagesize,
943 944
                                    const defs_resources_hugepage_limits_element *hugetlb,
                                    defs_resources_hugepage_limits_element **newtlb)
O
overweight 已提交
945 946 947 948 949 950
{
    bool repeated = false;
    size_t j;

    for (j = 0; j < newlen; j++) {
        if (newtlb[j] != NULL && newtlb[j]->page_size != NULL && !strcmp(newtlb[j]->page_size, pagesize)) {
L
lifeng68 已提交
951 952
            WARN("hugetlb-limit setting of %s is repeated, former setting %lu will be replaced with %lu", pagesize,
                 newtlb[j]->limit, hugetlb->limit);
O
overweight 已提交
953 954 955 956 957 958 959 960 961 962
            newtlb[j]->limit = hugetlb->limit;
            repeated = true;
            goto out;
        }
    }

out:
    return repeated;
}

L
lifeng68 已提交
963
static void free_hugetlbs_array(defs_resources_hugepage_limits_element **hugetlb, size_t hugetlb_len)
O
overweight 已提交
964 965 966 967 968 969 970 971 972
{
    size_t i;

    if (hugetlb == NULL) {
        return;
    }

    for (i = 0; i < hugetlb_len; i++) {
        if (hugetlb[i] != NULL) {
973
            free_defs_resources_hugepage_limits_element(hugetlb[i]);
O
overweight 已提交
974 975 976 977 978 979 980
            hugetlb[i] = NULL;
        }
    }
    free(hugetlb);
}

/* verify resources hugetlbs */
L
lifeng68 已提交
981
static int verify_resources_hugetlbs(const sysinfo_t *sysinfo, defs_resources_hugepage_limits_element ***hugetlb,
O
overweight 已提交
982 983 984
                                     size_t *hugetlb_len)
{
    int ret = 0;
985
    defs_resources_hugepage_limits_element **newhugetlb = NULL;
O
overweight 已提交
986 987 988 989 990
    size_t newlen = 0;
    size_t i;

    if (!sysinfo->hugetlbinfo.hugetlblimit) {
        ERROR("Your kernel does not support hugetlb limit. --hugetlb-limit discarded.");
L
lifeng68 已提交
991
        isulad_set_error_message("Your kernel does not support hugetlb limit. --hugetlb-limit discarded.");
O
overweight 已提交
992 993 994 995 996 997 998
        ret = -1;
        goto out;
    }

    for (i = 0; i < *hugetlb_len; i++) {
        char *pagesize = NULL;
        size_t newsize, oldsize;
999
        defs_resources_hugepage_limits_element **tmphugetlb;
O
overweight 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

        pagesize = validate_hugetlb((*hugetlb)[i]->page_size, (*hugetlb)[i]->limit);
        if (pagesize == NULL) {
            ret = -1;
            goto out;
        }

        if (check_hugetlbs_repeated(newlen, pagesize, (*hugetlb)[i], newhugetlb)) {
            free(pagesize);
            continue;
        }

        // append new hugetlb
1013
        if (newlen > SIZE_MAX / sizeof(defs_resources_hugepage_limits_element *) - 1) {
O
overweight 已提交
1014 1015 1016 1017 1018
            free(pagesize);
            ERROR("Too many new hugetlb to append!");
            ret = -1;
            goto out;
        }
1019 1020
        newsize = sizeof(defs_resources_hugepage_limits_element *) * (newlen + 1);
        oldsize = newsize - sizeof(defs_resources_hugepage_limits_element *);
O
overweight 已提交
1021 1022 1023 1024 1025 1026 1027 1028
        ret = mem_realloc((void **)&tmphugetlb, newsize, newhugetlb, oldsize);
        if (ret < 0) {
            free(pagesize);
            ERROR("Out of memory");
            ret = -1;
            goto out;
        }
        newhugetlb = tmphugetlb;
1029
        newhugetlb[newlen] = util_common_calloc_s(sizeof(defs_resources_hugepage_limits_element));
O
overweight 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
        if (newhugetlb[newlen] == NULL) {
            free(pagesize);
            ERROR("Out of memory");
            ret = -1;
            goto out;
        }
        newhugetlb[newlen]->limit = (*hugetlb)[i]->limit;
        newhugetlb[newlen]->page_size = pagesize;
        newlen++;
    }
out:
    if (ret != 0 && newhugetlb != NULL) {
        free_hugetlbs_array(newhugetlb, newlen);
    } else if (ret == 0) {
        free_hugetlbs_array(*hugetlb, *hugetlb_len);
        *hugetlb = newhugetlb;
        *hugetlb_len = newlen;
    }
    return ret;
}

/* adapt memory swap */
static int adapt_memory_swap(const sysinfo_t *sysinfo, const int64_t *limit, int64_t *swap)
{
    if (*limit > 0 && *swap == 0 && sysinfo->cgmeminfo.swap) {
        if (*limit > (INT64_MAX / 2)) {
            ERROR("Memory swap out of range!");
L
LiuHao 已提交
1057
            isulad_set_error_message("Memory swap out of range!");
O
overweight 已提交
1058 1059 1060 1061 1062 1063 1064 1065
            return -1;
        }
        *swap = (*limit) * 2;
    }
    return 0;
}

/* adapt resources memory */
1066
static int adapt_resources_memory(const sysinfo_t *sysinfo, defs_resources_memory *memory)
O
overweight 已提交
1067 1068 1069 1070 1071
{
    return adapt_memory_swap(sysinfo, &(memory->limit), &(memory->swap));
}

/* verify linux resources */
1072
static int verify_linux_resources(const sysinfo_t *sysinfo, defs_resources *resources)
O
overweight 已提交
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 1114 1115
{
    int ret = 0;

    // memory
    if (resources->memory != NULL) {
        ret = verify_resources_memory(sysinfo, resources->memory);
        if (ret != 0) {
            goto out;
        }
    }
    // pids
    if (resources->pids != NULL) {
        ret = verify_resources_pids(sysinfo, resources->pids);
        if (ret != 0) {
            goto out;
        }
    }
    // cpu
    if (resources->cpu != NULL) {
        ret = verify_resources_cpu(sysinfo, resources->cpu);
        if (ret != 0) {
            goto out;
        }
    }
    // hugetlb
    if (resources->hugepage_limits_len && resources->hugepage_limits != NULL) {
        ret = verify_resources_hugetlbs(sysinfo, &(resources->hugepage_limits), &(resources->hugepage_limits_len));
        if (ret != 0) {
            goto out;
        }
    }
    // blkio
    if (resources->block_io != NULL) {
        ret = verify_resources_blkio(sysinfo, resources->block_io);
        if (ret != 0) {
            goto out;
        }
    }
out:
    return ret;
}

/* adapt linux resources */
1116
static int adapt_linux_resources(const sysinfo_t *sysinfo, defs_resources *resources)
O
overweight 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
{
    int ret = 0;

    // memory
    if (resources->memory != NULL) {
        ret = adapt_resources_memory(sysinfo, resources->memory);
        if (ret != 0) {
            goto out;
        }
    }
out:
    return ret;
}

static bool verify_oci_linux_sysctl(const oci_runtime_config_linux *l)
{
    size_t i = 0;

    if (l->sysctl == NULL) {
        return true;
    }
    for (i = 0; i < l->sysctl->len; i++) {
        if (strcmp("kernel.pid_max", l->sysctl->keys[i]) == 0) {
            if (!pid_max_kernel_namespaced()) {
L
LiuHao 已提交
1141 1142
                isulad_set_error_message("Sysctl '%s' is not kernel namespaced, it cannot be changed",
                                         l->sysctl->keys[i]);
O
overweight 已提交
1143 1144 1145 1146 1147 1148
                return false;
            } else {
                return true;
            }
        }
        if (!check_sysctl_valid(l->sysctl->keys[i])) {
L
lifeng68 已提交
1149
            isulad_set_error_message("Sysctl %s=%s is not whitelist", l->sysctl->keys[i], l->sysctl->values[i]);
O
overweight 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 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 1220 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 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
            return false;
        }
    }
    return true;
}

/* verify oci linux */
static int verify_oci_linux(const sysinfo_t *sysinfo, const oci_runtime_config_linux *l)
{
    int ret = 0;

    // Resources
    if (l->resources != NULL) {
        ret = verify_linux_resources(sysinfo, l->resources);
        if (ret != 0) {
            goto out;
        }
    }
    if (!verify_oci_linux_sysctl(l)) {
        ret = -1;
        goto out;
    }
out:
    return ret;
}

static int verify_oci_hook_prestart(const oci_runtime_spec_hooks *h)
{
    size_t i;
    for (i = 0; i < h->prestart_len; i++) {
        int ret = 0;
        ret = verify_hook_conf(h->prestart[i]);
        if (ret != 0) {
            return ret;
        }
    }
    return 0;
}

static int verify_oci_hook_poststart(const oci_runtime_spec_hooks *h)
{
    size_t i;
    for (i = 0; i < h->poststart_len; i++) {
        int ret = 0;
        ret = verify_hook_conf(h->poststart[i]);
        if (ret != 0) {
            return ret;
        }
    }
    return 0;
}

static int verify_oci_hook_poststop(const oci_runtime_spec_hooks *h)
{
    size_t i;
    for (i = 0; i < h->poststop_len; i++) {
        int ret = 0;
        ret = verify_hook_conf(h->poststop[i]);
        if (ret != 0) {
            return ret;
        }
    }
    return 0;
}

/* verify oci hook */
int verify_oci_hook(const oci_runtime_spec_hooks *h)
{
    int ret = 0;

    // Prestart
    ret = verify_oci_hook_prestart(h);
    if (ret != 0) {
        goto out;
    }

    // Poststart
    ret = verify_oci_hook_poststart(h);
    if (ret != 0) {
        goto out;
    }

    // Poststop
    ret = verify_oci_hook_poststop(h);
    if (ret != 0) {
        goto out;
    }

out:
    return ret;
}

/* adapt oci linux */
static int adapt_oci_linux(const sysinfo_t *sysinfo, oci_runtime_config_linux *l)
{
    int ret = 0;

    // Resources
    if (l->resources != NULL) {
        ret = adapt_linux_resources(sysinfo, l->resources);
        if (ret != 0) {
            goto out;
        }
    }
out:
    return ret;
}

/* get source mount */
static int get_source_mount(const char *src, char **srcpath, char **optional)
{
    mountinfo_t **minfos = NULL;
    mountinfo_t *info = NULL;
    int ret = 0;
L
lifeng68 已提交
1264
    char real_path[PATH_MAX + 1] = { 0 };
O
overweight 已提交
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 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 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
    char *dirc = NULL;
    char *dname = NULL;

    if (realpath(src, real_path) == NULL) {
        ERROR("Failed to get real path for %s : %s", src, strerror(errno));
        return -1;
    }

    minfos = getmountsinfo();
    if (minfos == NULL) {
        ERROR("Failed to get mounts info");
        ret = -1;
        goto out;
    }

    info = find_mount_info(minfos, real_path);
    if (info != NULL) {
        *srcpath = util_strdup_s(real_path);
        *optional = info->optional ? util_strdup_s(info->optional) : NULL;
        goto out;
    }

    dirc = util_strdup_s(real_path);
    dname = dirc;
    while (strcmp(dirc, "/")) {
        dname = dirname(dname);
        info = find_mount_info(minfos, dname);
        if (info != NULL) {
            *srcpath = util_strdup_s(dname);
            *optional = info->optional ? util_strdup_s(info->optional) : NULL;
            goto out;
        }
    }
    ERROR("Could not find source mount of %s", src);
    ret = -1;
out:
    free(dirc);
    free_mounts_info(minfos);
    return ret;
}

static inline bool is_optional_shared(const char *optional)
{
    return optional != NULL && strncmp(optional, "shared:", strlen("shared:")) == 0;
}

/* ensure shared */
static int ensure_shared(const char *src)
{
    int ret = 0;
    char *srcpath = NULL;
    char *optional = NULL;

    ret = get_source_mount(src, &srcpath, &optional);
    if (ret != 0) {
        goto out;
    }
    if (is_optional_shared(optional)) {
        goto out;
    }
    ERROR("Path %s is mounted on %s but it is not a shared mount", src, srcpath);
    ret = -1;
out:
    free(srcpath);
    free(optional);
    return ret;
}

static inline bool is_optional_slave(const char *optional)
{
    return optional != NULL && strncmp(optional, "master:", strlen("master:")) == 0;
}

/* ensure shared or slave */
static int ensure_shared_or_slave(const char *src)
{
    int ret = 0;
    char *srcpath = NULL;
    char *optional = NULL;

    ret = get_source_mount(src, &srcpath, &optional);
    if (ret != 0) {
        goto out;
    }
    if (is_optional_shared(optional) || is_optional_slave(optional)) {
        goto out;
    }
    ERROR("Path %s is mounted on %s but it is not a shared or slave mount", src, srcpath);
    ret = -1;
out:

    free(srcpath);
    free(optional);
    return ret;
}

static inline bool is_propagation_shared(const char *propagation)
{
    return strcmp(propagation, "shared") == 0 || strcmp(propagation, "rshared") == 0;
}

static void set_mount_propagation_shared(const oci_runtime_spec *container)
{
    if (container->linux->rootfs_propagation == NULL) {
        container->linux->rootfs_propagation = util_strdup_s("shared");
        return;
    }

    if (!is_propagation_shared(container->linux->rootfs_propagation)) {
        free(container->linux->rootfs_propagation);
        container->linux->rootfs_propagation = util_strdup_s("shared");
    }
}

static inline bool is_propagation_slave(const char *propagation)
{
    return strcmp(propagation, "slave") == 0 || strcmp(propagation, "rslave") == 0;
}

static void set_mount_propagation_slave(const oci_runtime_spec *container)
{
    if (container->linux->rootfs_propagation == NULL) {
        container->linux->rootfs_propagation = util_strdup_s("rslave");
        return;
    }

    if (!is_propagation_shared(container->linux->rootfs_propagation) &&
        !is_propagation_slave(container->linux->rootfs_propagation)) {
        free(container->linux->rootfs_propagation);
        container->linux->rootfs_propagation = util_strdup_s("rslave");
    }
}

static int make_mount_propagation(const oci_runtime_spec *container, const char *source, const char *option)
{
    int ret;

    if (is_propagation_shared(option)) {
        ret = ensure_shared(source);
        if (ret != 0) {
            return ret;
        }
        set_mount_propagation_shared(container);
    } else if (is_propagation_slave(option)) {
        ret = ensure_shared_or_slave(source);
        if (ret != 0) {
            return ret;
        }
        set_mount_propagation_slave(container);
    }

    return 0;
}

/* set mounts */
static int set_mounts(const oci_runtime_spec *container)
{
    int ret = 0;
    size_t i, k;
    defs_mount **m = NULL;

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

    m = container->mounts;
    for (i = 0; i < container->mounts_len; i++) {
        for (k = 0; k < m[i]->options_len; k++) {
            ret = make_mount_propagation(container, m[i]->source, m[i]->options[k]);
            if (ret != 0) {
                goto out;
            }
        }
    }

out:
    return ret;
}

/* verify custom mount */
static int verify_custom_mount(defs_mount **mounts, size_t len)
{
    int ret = 0;
W
wujing 已提交
1448
    size_t i;
O
overweight 已提交
1449 1450
    defs_mount *iter = NULL;

W
wujing 已提交
1451
    for (i = 0; i < len; ++i) {
O
overweight 已提交
1452 1453 1454 1455 1456
        iter = *(mounts + i);
        if (iter == NULL || strcmp(iter->type, "bind")) {
            continue;
        }

L
lifeng68 已提交
1457
        if (!util_file_exists(iter->source) && util_mkdir_p(iter->source, CONFIG_DIRECTORY_MODE)) {
O
overweight 已提交
1458
            ERROR("Failed to create directory '%s': %s", iter->source, strerror(errno));
L
LiuHao 已提交
1459
            isulad_try_set_error_message("Failed to create directory '%s': %s", iter->source, strerror(errno));
O
overweight 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
/* verify oci linux */
static int verify_process_env(const defs_process *process)
{
    int ret = 0;
    size_t i = 0;
    char *new_env = NULL;

    for (i = 0; i < process->env_len; i++) {
        if (util_validate_env(process->env[i], &new_env) != 0) {
            ERROR("Invalid environment %s", process->env[i]);
            isulad_set_error_message("Invalid environment %s", process->env[i]);
            ret = -1;
            goto out;
        }
        free(new_env);
        new_env = NULL;
    }

out:
    free(new_env);
    return ret;
}

O
overweight 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
static int verify_container_linux(const oci_runtime_spec *container, const sysinfo_t *sysinfo)
{
    int ret = 0;

    /* verify and adapt container settings */
    if (container->linux != NULL) {
        ret = verify_oci_linux(sysinfo, container->linux);
        if (ret != 0) {
            goto out;
        }
        ret = adapt_oci_linux(sysinfo, container->linux);
        if (ret != 0) {
            goto out;
        }
    }

1508 1509 1510 1511 1512 1513 1514 1515
    /* verify oci spec process settings */
    if (container->process != NULL) {
        ret = verify_process_env(container->process);
        if (ret != 0) {
            goto out;
        }
    }

O
overweight 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
out:
    return ret;
}

static int verify_container_mounts(const oci_runtime_spec *container)
{
    int ret = 0;

    /* verify custom mount info, ensure source path exist */
    if (container->mounts != NULL && container->mounts_len > 0) {
        ret = verify_custom_mount(container->mounts, container->mounts_len);
        if (ret != 0) {
            goto out;
        }
        ret = set_mounts(container);
        if (ret != 0) {
            goto out;
        }
    }

out:
    return ret;
}

/* verify container settings */
int verify_container_settings(const oci_runtime_spec *container)
{
    int ret = 0;
    sysinfo_t *sysinfo = NULL;

    sysinfo = get_sys_info(true);
    if (sysinfo == NULL) {
        ERROR("Can not get system info");
        ret = -1;
        goto out;
    }

    if (!util_valid_host_name(container->hostname)) {
        ERROR("Invalid container hostname %s", container->hostname);
L
LiuHao 已提交
1555 1556
        isulad_set_error_message("Invalid container hostname (%s), only %s and less than 64 bytes are allowed.",
                                 container->hostname, HOST_NAME_REGEXP);
O
overweight 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 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 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
        ret = -1;
        goto out;
    }

    /* verify and adapt container settings */
    ret = verify_container_linux(container, sysinfo);
    if (ret != 0) {
        goto out;
    }

    ret = verify_container_mounts(container);
    if (ret != 0) {
        goto out;
    }

    /* verify hook settings */
    if (container->hooks != NULL && verify_oci_hook(container->hooks)) {
        ERROR("Verify hook file failed");
        ret = -1;
        goto out;
    }

out:
    free_sysinfo(sysinfo);
    return ret;
}

static void free_hugetlb_array(host_config_hugetlbs_element **hugetlb, size_t len)
{
    size_t i;

    if (hugetlb == NULL) {
        return;
    }

    for (i = 0; i < len; i++) {
        free_host_config_hugetlbs_element(hugetlb[i]);
        hugetlb[i] = NULL;
    }
    free(hugetlb);
}

static int append_hugetlb_array(host_config_hugetlbs_element ***hugetlb, size_t len)
{
    size_t newsize;
    size_t oldsize;
    host_config_hugetlbs_element **tmphugetlb;
    int ret;

    if (len > SIZE_MAX / sizeof(host_config_hugetlbs_element *) - 1) {
        return -1;
    }

    newsize = sizeof(host_config_hugetlbs_element *) * (len + 1);
    oldsize = newsize - sizeof(host_config_hugetlbs_element *);
    ret = mem_realloc((void **)&tmphugetlb, newsize, *hugetlb, oldsize);
    if (ret < 0) {
        return -1;
    }

    *hugetlb = tmphugetlb;
    (*hugetlb)[len] = util_common_calloc_s(sizeof(host_config_hugetlbs_element));
    if ((*hugetlb)[len] == NULL) {
        return -1;
    }

    return 0;
}

static int add_hugetbl_element(host_config_hugetlbs_element ***hugetlb, size_t *len,
                               const host_config_hugetlbs_element *element)
{
    char *pagesize = NULL;
    size_t j;
    int ret = 0;

    pagesize = validate_hugetlb(element->page_size, element->limit);
    if (pagesize == NULL) {
        return -1;
    }

    for (j = 0; j < *len; j++) {
        if (strcmp((*hugetlb)[j]->page_size, pagesize) == 0) {
            WARN("Hostconfig: hugetlb-limit setting of %s is repeated, "
H
haozi007 已提交
1641
                 "former setting %lu will be replaced with %lu",
O
overweight 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
                 pagesize, (*hugetlb)[j]->limit, element->limit);
            (*hugetlb)[j]->limit = element->limit;
            goto out;
        }
    }

    // append new hugetlb
    ret = append_hugetlb_array(hugetlb, *len);
    if (ret < 0) {
        ERROR("Out of memory");
        goto out;
    }
    (*hugetlb)[*len]->page_size = pagesize;
    (*hugetlb)[*len]->limit = element->limit;
    (*len)++;
    return 0;

out:
    free(pagesize);
    return ret;
}

/* verify host config hugetlbs */
static int verify_host_config_hugetlbs(const sysinfo_t *sysinfo, host_config_hugetlbs_element ***hugetlb,
                                       size_t *hugetlb_len)
{
    int ret;
    host_config_hugetlbs_element **newhugetlb = NULL;
    size_t newlen = 0;
    size_t i = 0;

    if (*hugetlb == NULL || *hugetlb_len == 0) {
        return 0;
    }

    if (!sysinfo->hugetlbinfo.hugetlblimit) {
        ERROR("Your kernel does not support hugetlb limit. --hugetlb-limit discarded.");
L
LiuHao 已提交
1679
        isulad_set_error_message("Your kernel does not support hugetlb limit. --hugetlb-limit discarded.");
O
overweight 已提交
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
        ret = -1;
        goto out;
    }

    for (i = 0; i < *hugetlb_len; i++) {
        ret = add_hugetbl_element(&newhugetlb, &newlen, (*hugetlb)[i]);
        if (ret != 0) {
            goto out;
        }
    }

    free_hugetlb_array(*hugetlb, *hugetlb_len);
    *hugetlb = newhugetlb;
    *hugetlb_len = newlen;
    return 0;

out:
    free_hugetlb_array(newhugetlb, newlen);
    return ret;
}

static int host_config_settings_memory(const sysinfo_t *sysinfo, const host_config *hostconfig, bool update)
{
    int ret = 0;

    ret = verify_mem_limit_swap(sysinfo, hostconfig->memory, hostconfig->memory_swap, update);
    if (ret != 0) {
        goto out;
    }

    ret = verify_memory_reservation(sysinfo, hostconfig->memory, hostconfig->memory_reservation);
    if (ret != 0) {
        goto out;
    }

    ret = verify_memory_kernel(sysinfo, hostconfig->kernel_memory);
    if (ret != 0) {
        goto out;
    }

L
LiFeng 已提交
1720 1721 1722 1723 1724 1725 1726
    if (hostconfig->memory_swappiness != NULL) {
        ret = verify_memory_swappiness(sysinfo, *(hostconfig->memory_swappiness));
        if (ret != 0) {
            goto out;
        }
    }

O
overweight 已提交
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
out:
    return ret;
}

static int host_config_settings_cpu(const sysinfo_t *sysinfo, const host_config *hostconfig)
{
    int ret = 0;

    ret = verify_cpu_realtime(sysinfo, hostconfig->cpu_realtime_period, hostconfig->cpu_realtime_runtime);
    if (ret != 0) {
        goto out;
    }

    ret = verify_cpu_shares(sysinfo, hostconfig->cpu_shares);
    if (ret != 0) {
        goto out;
    }

    ret = verify_cpu_cfs_scheduler(sysinfo, hostconfig->cpu_period, hostconfig->cpu_quota);
    if (ret != 0) {
        goto out;
    }

    // cpuset
    ret = verify_resources_cpuset(sysinfo, hostconfig->cpuset_cpus, hostconfig->cpuset_mems);
    if (ret != 0) {
        goto out;
    }

out:
    return ret;
}

static int host_config_settings_blkio(const sysinfo_t *sysinfo, const host_config *hostconfig)
{
    int ret = 0;

    ret = verify_hostconfig_blkio_weight(sysinfo, hostconfig->blkio_weight);
    if (ret != 0) {
        goto out;
    }

    ret = verify_blkio_device(sysinfo, hostconfig->blkio_weight_device_len);
    if (ret != 0) {
        goto out;
    }

out:
    return ret;
}

static inline bool is_restart_policy_always(const char *policy)
{
    return strcmp(policy, "always") == 0;
}

static inline bool is_restart_policy_on_reboot(const char *policy)
{
    return strcmp(policy, "on-reboot") == 0;
}

static inline bool is_restart_policy_no(const char *policy)
{
    return strcmp(policy, "no") == 0;
}

static inline bool is_restart_policy_on_failure(const char *policy)
{
    return strcmp(policy, "on-failure") == 0;
}

static int verify_restart_policy_name(const host_config_restart_policy *rp, const host_config *hostconfig)
{
    if (is_restart_policy_always(rp->name) || is_restart_policy_no(rp->name) || is_restart_policy_on_reboot(rp->name)) {
        if (rp->maximum_retry_count != 0) {
            ERROR("Maximum retry count cannot be used with restart policy '%s'", rp->name);
L
LiuHao 已提交
1803
            isulad_set_error_message("Maximum retry count cannot be used with restart policy '%s'", rp->name);
O
overweight 已提交
1804 1805 1806 1807 1808
            return -1;
        }
    } else if (is_restart_policy_on_failure(rp->name)) {
        if (rp->maximum_retry_count < 0) {
            ERROR("Maximum retry count cannot be negative");
L
LiuHao 已提交
1809
            isulad_set_error_message("Maximum retry count cannot be negative");
O
overweight 已提交
1810 1811 1812 1813
            return -1;
        }
    } else {
        ERROR("Invalid restart policy '%s'", rp->name);
L
LiuHao 已提交
1814
        isulad_set_error_message("Invalid restart policy '%s'", rp->name);
O
overweight 已提交
1815 1816 1817 1818 1819
        return -1;
    }

    if (hostconfig->auto_remove && !is_restart_policy_no(rp->name)) {
        ERROR("Can't create 'AutoRemove' container with restart policy");
L
LiuHao 已提交
1820
        isulad_set_error_message("Can't create 'AutoRemove' container with restart policy");
O
overweight 已提交
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
        return -1;
    }

    return 0;
}

static int host_config_settings_restart_policy(const host_config *hostconfig)
{
    host_config_restart_policy *rp = NULL;

    if (hostconfig == NULL || hostconfig->restart_policy == NULL) {
        return 0;
    }

    rp = hostconfig->restart_policy;
    if (rp->name == NULL || rp->name[0] == '\0') {
        if (rp->maximum_retry_count != 0) {
            ERROR("Maximum retry count cannot be used with empty restart policy");
L
LiuHao 已提交
1839
            isulad_set_error_message("Maximum retry count cannot be used with empty restart policy");
O
overweight 已提交
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
            return -1;
        }
        return 0;
    }

    return verify_restart_policy_name(rp, hostconfig);
}

static int host_config_settings_with_sysinfo(host_config *hostconfig, bool update)
{
    int ret = 0;
    sysinfo_t *sysinfo = NULL;

    sysinfo = get_sys_info(true);
    if (sysinfo == NULL) {
        ERROR("Can not get system info");
        return -1;
    }

    ret = verify_host_config_hugetlbs(sysinfo, &(hostconfig->hugetlbs), &(hostconfig->hugetlbs_len));
    if (ret != 0) {
        goto out;
    }

    // memory
    ret = host_config_settings_memory(sysinfo, hostconfig, update);
    if (ret != 0) {
        goto out;
    }

    ret = verify_pids_limit(sysinfo, hostconfig->pids_limit);
    if (ret != 0) {
        goto out;
    }

    ret = verify_files_limit(sysinfo, hostconfig->files_limit);
    if (ret != 0) {
        goto out;
    }

    // cpu & cpuset
    ret = host_config_settings_cpu(sysinfo, hostconfig);
    if (ret != 0) {
        goto out;
    }

    // blkio
    ret = host_config_settings_blkio(sysinfo, hostconfig);
    if (ret != 0) {
        goto out;
    }

out:
    free_sysinfo(sysinfo);
    return ret;
}

/* verify host config settings */
int verify_host_config_settings(host_config *hostconfig, bool update)
{
    int ret = 0;

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

    // restart policy
    ret = host_config_settings_restart_policy(hostconfig);
    if (ret != 0) {
        goto out;
    }

    ret = host_config_settings_with_sysinfo(hostconfig, update);
    if (ret != 0) {
        goto out;
    }

    // oom score adj
    ret = verify_oom_score_adj(hostconfig->oom_score_adj);
    if (ret != 0) {
        goto out;
    }

#ifdef ENABLE_OCI_IMAGE
    // storage options
    ret = verify_storage_opts(hostconfig);
    if (ret != 0) {
        goto out;
    }
#endif

out:
    return ret;
}

W
wujing 已提交
1935 1936 1937 1938 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
static int relabel_mounts_if_needed(defs_mount **mounts, size_t len, const char *mount_label)
{
    int ret = 0;
    size_t i, j;
    defs_mount *iter = NULL;

    for (i = 0; i < len; ++i) {
        bool need_relabel = false;
        bool is_shared = false;
        iter = *(mounts + i);
        if (iter == NULL) {
            continue;
        }

        for (j = 0; j < iter->options_len; j++) {
            if (strcmp(iter->options[j], "Z") == 0) {
                need_relabel = true;
                is_shared = false;
            } else if (strcmp(iter->options[j], "z") == 0) {
                need_relabel = true;
                is_shared = true;
            }
        }

        if (need_relabel && relabel(iter->source, mount_label, is_shared) != 0) {
            ERROR("Error setting label on mount source '%s'", iter->source);
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

O
overweight 已提交
1970
/* verify container settings start */
1971
int verify_container_settings_start(const oci_runtime_spec *oci_spec)
O
overweight 已提交
1972 1973 1974 1975
{
    int ret = 0;

    /* verify custom mount info, ensure source path exist */
1976
    if (oci_spec->mounts != NULL && oci_spec->mounts_len > 0) {
W
wujing 已提交
1977 1978 1979 1980 1981
        if (verify_custom_mount(oci_spec->mounts, oci_spec->mounts_len) != 0) {
            ERROR("Failed to verify custom mount");
            ret = -1;
            goto out;
        }
L
lifeng68 已提交
1982
        if (relabel_mounts_if_needed(oci_spec->mounts, oci_spec->mounts_len, oci_spec->linux->mount_label) != 0) {
W
wujing 已提交
1983 1984 1985 1986
            ERROR("Failed to relabel mount");
            ret = -1;
            goto out;
        }
O
overweight 已提交
1987
    }
1988

W
wujing 已提交
1989
out:
O
overweight 已提交
1990 1991 1992 1993 1994 1995 1996 1997
    return ret;
}

static inline bool is_less_than_one_second(int64_t timeout)
{
    return timeout != 0 && timeout < Time_Second;
}

1998
int verify_health_check_parameter(const container_config *container_spec)
O
overweight 已提交
1999 2000 2001
{
    int ret = 0;

2002
    if (container_spec == NULL || container_spec->healthcheck == NULL) {
O
overweight 已提交
2003 2004 2005
        return ret;
    }

2006
    if (is_less_than_one_second(container_spec->healthcheck->interval)) {
O
overweight 已提交
2007
        ERROR("Interval in Healthcheck cannot be less than one second");
L
LiuHao 已提交
2008
        isulad_set_error_message("Interval in Healthcheck cannot be less than one second");
O
overweight 已提交
2009 2010 2011
        ret = -1;
        goto out;
    }
2012
    if (is_less_than_one_second(container_spec->healthcheck->timeout)) {
O
overweight 已提交
2013
        ERROR("Timeout in Healthcheck cannot be less than one second");
L
LiuHao 已提交
2014
        isulad_set_error_message("Timeout in Healthcheck cannot be less than one second");
O
overweight 已提交
2015 2016 2017
        ret = -1;
        goto out;
    }
2018
    if (is_less_than_one_second(container_spec->healthcheck->start_period)) {
O
overweight 已提交
2019
        ERROR("StartPeriod in Healthcheck cannot be less than one second");
L
LiuHao 已提交
2020
        isulad_set_error_message("StartPeriod in Healthcheck cannot be less than one second");
O
overweight 已提交
2021 2022 2023
        ret = -1;
        goto out;
    }
2024
    if (container_spec->healthcheck->retries < 0) {
O
overweight 已提交
2025
        ERROR("--health-retries cannot be negative");
L
LiuHao 已提交
2026
        isulad_set_error_message("--health-retries cannot be negative");
O
overweight 已提交
2027 2028 2029 2030 2031 2032 2033
        ret = -1;
        goto out;
    }

out:
    return ret;
}