execution_network.c 31.2 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
/******************************************************************************
 * 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 network callback function definition
 ********************************************************************************/
#include "execution_network.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/mount.h>
#include <lcr/lcrcontainer.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/eventfd.h>
#include <malloc.h>

#include "log.h"
#include "utils.h"
L
LiuHao 已提交
33
#include "isulad_config.h"
O
overweight 已提交
34 35 36
#include "config.h"
#include "containers_store.h"
#include "namespace.h"
D
dogsheng 已提交
37
#include "path.h"
W
wujing 已提交
38
#include "selinux_label.h"
O
overweight 已提交
39 40 41 42 43 44

static int write_hostname_to_file(const char *rootfs, const char *hostname)
{
    int ret = 0;
    char *file_path = NULL;

D
dogsheng 已提交
45 46
    if (realpath_in_scope(rootfs, "/etc/hostname", &file_path) < 0) {
        SYSERROR("Failed to get real path '/etc/hostname' under rootfs '%s'", rootfs);
L
LiuHao 已提交
47
        isulad_set_error_message("Failed to get real path '/etc/hostname' under rootfs '%s'", rootfs);
O
overweight 已提交
48 49 50
        goto error_out;
    }
    if (hostname != NULL) {
51
        ret = util_write_file(file_path, hostname, strlen(hostname), NETWORK_MOUNT_FILE_MODE);
O
overweight 已提交
52 53
        if (ret) {
            SYSERROR("Failed to write %s", file_path);
L
LiuHao 已提交
54
            isulad_set_error_message("Failed to write %s: %s", file_path, strerror(errno));
O
overweight 已提交
55 56 57 58 59 60 61 62 63 64 65
            goto error_out;
        }
    }

error_out:
    free(file_path);
    return ret;
}

static int fopen_network(FILE **fp, char **file_path, const char *rootfs, const char *filename)
{
D
dogsheng 已提交
66 67
    if (realpath_in_scope(rootfs, filename, file_path) < 0) {
        SYSERROR("Failed to get real path '%s' under rootfs '%s'", filename, rootfs);
L
LiuHao 已提交
68
        isulad_set_error_message("Failed to get real path '%s' under rootfs '%s'", filename, rootfs);
O
overweight 已提交
69 70 71 72 73
        return -1;
    }
    *fp = util_fopen(*file_path, "a+");
    if (*fp == NULL) {
        SYSERROR("Failed to open %s", *file_path);
L
LiuHao 已提交
74
        isulad_set_error_message("Failed to open %s: %s", *file_path, strerror(errno));
O
overweight 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        return -1;
    }
    return 0;
}

static int get_content_and_hosts_map(FILE *fp, char **content, json_map_string_bool *hosts_map)
{
    int ret = 0;
    size_t length = 0;
    char *pline = NULL;
    char *tmp = NULL;
    char *host_name = NULL;
    char *host_ip = NULL;
    char *saveptr = NULL;

    while (getline(&pline, &length, fp) != -1) {
        char *tmp_str = NULL;
        char host_key[MAX_BUFFER_SIZE] = { 0 };
        if (pline == NULL) {
            ERROR("get hosts content failed");
            return -1;
        }
        if (pline[0] == '#') {
            tmp = util_string_append(pline, *content);
            free(*content);
            *content = tmp;
            continue;
        }
        tmp_str = util_strdup_s(pline);
        if (tmp_str == NULL) {
            ERROR("Out of memory");
            ret = -1;
            goto out;
        }
        util_trim_newline(tmp_str);
        host_ip = strtok_r(tmp_str, " ", &saveptr);
        host_name = strtok_r(NULL, " ", &saveptr);
        if (host_ip != NULL && host_name != NULL) {
O
openeuler-iSula 已提交
113 114
            int nret = snprintf(host_key, sizeof(host_key), "%s:%s", host_ip, host_name);
            if ((size_t)nret >= sizeof(host_key) || nret < 0) {
O
overweight 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                free(tmp_str);
                ERROR("Out of memory");
                ret = -1;
                goto out;
            }
            if (append_json_map_string_bool(hosts_map, host_key, true)) {
                free(tmp_str);
                ERROR("append data to hosts map failed");
                ret = -1;
                goto out;
            }
            tmp = util_string_append(pline, *content);
            free(*content);
            *content = tmp;
        }
        free(tmp_str);
    }

out:
    free(pline);
    return ret;
}

static int write_content_to_file(const char *file_path, const char *content)
{
    int ret = 0;

    if (content != NULL) {
143
        ret = util_write_file(file_path, content, strlen(content), NETWORK_MOUNT_FILE_MODE);
O
overweight 已提交
144 145
        if (ret != 0) {
            SYSERROR("Failed to write file %s", file_path);
L
LiuHao 已提交
146
            isulad_set_error_message("Failed to write file %s: %s", file_path, strerror(errno));
O
overweight 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            return ret;
        }
    }
    return ret;
}

static int merge_hosts_content(const host_config *host_spec, char **content, json_map_string_bool *hosts_map)
{
    size_t i, j;
    char *tmp = NULL;
    char *saveptr = NULL;

    for (i = 0; i < host_spec->extra_hosts_len; i++) {
        bool need_to_add = true;
        char *host_name = NULL;
        char *host_ip = NULL;
        char *hosts = NULL;
        char host_key[MAX_BUFFER_SIZE] = { 0 };
        hosts = util_strdup_s(host_spec->extra_hosts[i]);
        if (hosts == NULL) {
            ERROR("Out of memory");
            return -1;
        }
        host_name = strtok_r(hosts, ":", &saveptr);
        host_ip = strtok_r(NULL, ":", &saveptr);
        if (host_name == NULL || host_ip == NULL) {
            free(hosts);
            ERROR("extra host '%s' format error.", host_spec->extra_hosts[i]);
            return -1;
        }
O
openeuler-iSula 已提交
177 178
        int nret = snprintf(host_key, sizeof(host_key), "%s:%s", host_ip, host_name);
        if ((size_t)nret >= sizeof(host_key) || nret < 0) {
O
overweight 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
            free(hosts);
            ERROR("Out of memory");
            return -1;
        }
        for (j = 0; j < hosts_map->len; j++) {
            if (strcmp(host_key, hosts_map->keys[j]) == 0) {
                need_to_add = false;
                break;
            }
        }
        if (need_to_add) {
            tmp = util_string_append(host_ip, *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append(" ", *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append(host_name, *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append("\n", *content);
            free(*content);
            *content = tmp;
            if (append_json_map_string_bool(hosts_map, host_key, true)) {
                free(hosts);
                ERROR("append data to hosts map failed");
                return -1;
            }
        }
        free(hosts);
    }
    return 0;
}

static int merge_hosts(const host_config *host_spec, const char *rootfs)
{
    int ret = 0;
    char *content = NULL;
    char *file_path = NULL;
    FILE *fp = NULL;
    json_map_string_bool *hosts_map = NULL;

    hosts_map = (json_map_string_bool *)util_common_calloc_s(sizeof(json_map_string_bool));
    if (hosts_map == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto error_out;
    }
    ret = fopen_network(&fp, &file_path, rootfs, "/etc/hosts");
    if (ret != 0) {
        goto error_out;
    }
    ret = get_content_and_hosts_map(fp, &content, hosts_map);
    if (ret != 0) {
        goto error_out;
    }
    ret = merge_hosts_content(host_spec, &content, hosts_map);
    if (ret != 0) {
        goto error_out;
    }
    ret = write_content_to_file(file_path, content);
    if (ret != 0) {
        goto error_out;
    }

error_out:
    free(content);
    free(file_path);
    if (fp != NULL) {
        fclose(fp);
    }
    free_json_map_string_bool(hosts_map);
    return ret;
}

static int merge_dns_search(const host_config *host_spec, char **content, const char *token, char *saveptr)
{
    int ret = 0;
    size_t i, j;
    size_t content_len = strlen(*content);
    char *tmp = NULL;
    json_map_string_bool *dns_search_map = NULL;

    dns_search_map = (json_map_string_bool *)util_common_calloc_s(sizeof(json_map_string_bool));
    if (dns_search_map == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto error_out;
    }
    while (token != NULL) {
        token = strtok_r(NULL, " ", &saveptr);
        if (token != NULL) {
            if (append_json_map_string_bool(dns_search_map, token, true)) {
                ERROR("append data to dns search map failed");
                ret = -1;
                goto error_out;
            }
        }
    }
    for (i = 0; i < host_spec->dns_search_len; i++) {
        bool need_to_add = true;
        for (j = 0; j < dns_search_map->len; j++) {
            if (strcmp(host_spec->dns_search[i], dns_search_map->keys[j]) == 0) {
                need_to_add = false;
                break;
            }
        }
        if (need_to_add) {
            if (strlen(*content) > 0) {
                (*content)[strlen(*content) - 1] = ' ';
            }
            tmp = util_string_append(host_spec->dns_search[i], *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append(" ", *content);
            free(*content);
            *content = tmp;
            if (append_json_map_string_bool(dns_search_map, host_spec->dns_search[i], true)) {
                ERROR("append data to dns search map failed");
                ret = -1;
                goto error_out;
            }
        }
    }
D
dogsheng 已提交
303
    if (*content != NULL && strlen(*content) > content_len) {
O
overweight 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        (*content)[strlen(*content) - 1] = '\n';
    }

error_out:
    free_json_map_string_bool(dns_search_map);
    return ret;
}

static int merge_dns_options(const host_config *host_spec, char **content, const char *token, char *saveptr)
{
    int ret = 0;
    size_t i, j;
    size_t content_len = strlen(*content);
    char *tmp = NULL;
    json_map_string_bool *dns_options_map = NULL;

    dns_options_map = (json_map_string_bool *)util_common_calloc_s(sizeof(json_map_string_bool));
    if (dns_options_map == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto error_out;
    }
    while (token != NULL) {
        token = strtok_r(NULL, " ", &saveptr);
        if (token != NULL) {
            if (append_json_map_string_bool(dns_options_map, token, true)) {
                ERROR("append data to dns options map failed");
                ret = -1;
                goto error_out;
            }
        }
    }
    for (i = 0; i < host_spec->dns_options_len; i++) {
        bool need_to_add = true;
        for (j = 0; j < dns_options_map->len; j++) {
            if (strcmp(host_spec->dns_options[i], dns_options_map->keys[j]) == 0) {
                need_to_add = false;
                break;
            }
        }
        if (need_to_add) {
            if (strlen(*content) > 0) {
                (*content)[strlen(*content) - 1] = ' ';
            }
            tmp = util_string_append(host_spec->dns_options[i], *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append(" ", *content);
            free(*content);
            *content = tmp;
            if (append_json_map_string_bool(dns_options_map, host_spec->dns_options[i], true)) {
                ERROR("append data to dns options map failed");
                ret = -1;
                goto error_out;
            }
        }
    }
D
dogsheng 已提交
361
    if (*content != NULL && strlen(*content) > content_len) {
O
overweight 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 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 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 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
        (*content)[strlen(*content) - 1] = '\n';
    }

error_out:
    free_json_map_string_bool(dns_options_map);
    return ret;
}

static int merge_dns(const host_config *host_spec, char **content, json_map_string_bool *dns_map)
{
    size_t i, j;
    char *tmp = NULL;

    for (i = 0; i < host_spec->dns_len; i++) {
        bool need_to_add = true;
        for (j = 0; j < dns_map->len; j++) {
            if (strcmp(host_spec->dns[i], dns_map->keys[j]) == 0) {
                need_to_add = false;
                break;
            }
        }
        if (need_to_add) {
            tmp = util_string_append("nameserver ", *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append(host_spec->dns[i], *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append("\n", *content);
            free(*content);
            *content = tmp;
            if (append_json_map_string_bool(dns_map, host_spec->dns[i], true)) {
                ERROR("append data to dns map failed");
                return -1;
            }
        }
    }
    return 0;
}

static bool is_need_add(const char *dns_search, const json_map_string_bool *dns_search_map)
{
    bool need_to_add = true;
    size_t j;

    for (j = 0; j < dns_search_map->len; j++) {
        if (strcmp(dns_search, dns_search_map->keys[j]) == 0) {
            need_to_add = false;
            break;
        }
    }

    return need_to_add;
}

static int generate_new_search(const host_config *host_spec,
                               json_map_string_bool *dns_search_map,
                               char **content,
                               bool search)
{
    char *tmp = NULL;

    if (!search && host_spec->dns_search_len > 0) {
        size_t i;
        tmp = util_string_append("search ", *content);
        free(*content);
        *content = tmp;
        for (i = 0; i < host_spec->dns_search_len; i++) {
            if (!is_need_add(host_spec->dns_search[i], dns_search_map)) {
                continue;
            }

            if (append_json_map_string_bool(dns_search_map, host_spec->dns_search[i], true)) {
                ERROR("append data to dns search map failed");
                return -1;
            }
            tmp = util_string_append(host_spec->dns_search[i], *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append(" ", *content);
            free(*content);
            *content = tmp;
        }
        tmp = util_string_append("\n", *content);
        free(*content);
        *content = tmp;
    }
    return 0;
}

static int generate_new_options(const host_config *host_spec,
                                json_map_string_bool *dns_options_map,
                                char **content,
                                bool options)
{
    char *tmp = NULL;

    if (!options && host_spec->dns_options_len > 0) {
        size_t i;
        tmp = util_string_append("options ", *content);
        free(*content);
        *content = tmp;
        for (i = 0; i < host_spec->dns_options_len; i++) {
            if (!is_need_add(host_spec->dns_options[i], dns_options_map)) {
                continue;
            }

            if (append_json_map_string_bool(dns_options_map, host_spec->dns_options[i], true)) {
                ERROR("append data to dns options map failed");
                return -1;
            }
            tmp = util_string_append(host_spec->dns_options[i], *content);
            free(*content);
            *content = tmp;
            tmp = util_string_append(" ", *content);
            free(*content);
            *content = tmp;
        }
        tmp = util_string_append("\n", *content);
        free(*content);
        *content = tmp;
    }
    return 0;
}

static int generate_new_search_and_options(const host_config *host_spec, char **content, bool search, bool options)
{
    int ret = 0;
    json_map_string_bool *dns_search_map = NULL;
    json_map_string_bool *dns_options_map = NULL;

    dns_search_map = (json_map_string_bool *)util_common_calloc_s(sizeof(json_map_string_bool));
    if (dns_search_map == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto error_out;
    }
    dns_options_map = (json_map_string_bool *)util_common_calloc_s(sizeof(json_map_string_bool));
    if (dns_options_map == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto error_out;
    }
    ret = generate_new_search(host_spec, dns_search_map, content, search);
    if (ret) {
        goto error_out;
    }
    ret = generate_new_options(host_spec, dns_options_map, content, options);
    if (ret) {
        goto error_out;
    }

error_out:
    free_json_map_string_bool(dns_search_map);
    free_json_map_string_bool(dns_options_map);
    return ret;
}

static int resolve_handle_content(const char *pline, const host_config *host_spec,
                                  char **content, json_map_string_bool *dns_map, bool *search, bool *options)
{
    int ret = 0;
    char *tmp = NULL;
    char *token = NULL;
    char *saveptr = NULL;
    char *tmp_str = NULL;

    if (pline[0] == '#') {
        tmp = util_string_append(pline, *content);
        free(*content);
        *content = tmp;
        return 0;
    }
    tmp_str = util_strdup_s(pline);
    if (tmp_str == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto cleanup;
    }
    util_trim_newline(tmp_str);
    tmp_str = util_trim_space(tmp_str);
    if (strcmp("", tmp_str) == 0) {
        goto cleanup;
    }
    token = strtok_r(tmp_str, " ", &saveptr);
    if (token == NULL) {
        ret = -1;
        goto cleanup;
    }
    if (strcmp(token, "search") == 0) {
        *search = true;
        tmp = util_string_append(pline, *content);
        if (tmp == NULL) {
            ERROR("Out of memory");
            ret = -1;
            goto cleanup;
        }
        free(*content);
        *content = tmp;
        ret = merge_dns_search(host_spec, content, token, saveptr);
    } else if (strcmp(token, "options") == 0) {
        *options = true;
        tmp = util_string_append(pline, *content);
        if (tmp == NULL) {
            ERROR("Out of memory");
            ret = -1;
            goto cleanup;
        }
        free(*content);
        *content = tmp;
        ret = merge_dns_options(host_spec, content, token, saveptr);
    } else if (strcmp(token, "nameserver") == 0) {
        tmp = util_string_append(pline, *content);
        free(*content);
        *content = tmp;
        token = strtok_r(NULL, " ", &saveptr);
        if (token == NULL) {
            ret = -1;
            goto cleanup;
        }
        if (append_json_map_string_bool(dns_map, token, true)) {
            ERROR("append data to dns map failed");
            ret = -1;
            goto cleanup;
        }
    }
cleanup:
    free(tmp_str);
    return ret;
}

593
static int merge_resolv(const host_config *host_spec, const char *rootfs, const char *resolv_conf_path)
O
overweight 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
{
    int ret = 0;
    size_t length = 0;
    bool search = false;
    bool options = false;
    char *pline = NULL;
    char *content = NULL;
    char *file_path = NULL;
    FILE *fp = NULL;
    json_map_string_bool *dns_map = NULL;

    dns_map = (json_map_string_bool *)util_common_calloc_s(sizeof(json_map_string_bool));
    if (dns_map == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto error_out;
    }
611
    ret = fopen_network(&fp, &file_path, rootfs, resolv_conf_path);
O
overweight 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 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
    if (ret != 0) {
        goto error_out;
    }

    while (getline(&pline, &length, fp) != -1) {
        if (pline == NULL) {
            ERROR("get resolv content failed");
            ret = -1;
            goto error_out;
        }
        ret = resolve_handle_content(pline, host_spec, &content, dns_map, &search, &options);
        if (ret != 0) {
            goto error_out;
        }
    }
    ret = merge_dns(host_spec, &content, dns_map);
    if (ret) {
        goto error_out;
    }
    ret = generate_new_search_and_options(host_spec, &content, search, options);
    if (ret) {
        goto error_out;
    }
    ret = write_content_to_file(file_path, content);
    if (ret) {
        goto error_out;
    }

error_out:
    free(pline);
    free(file_path);
    free(content);
    if (fp != NULL) {
        fclose(fp);
    }
    free_json_map_string_bool(dns_map);
    return ret;
}

static int chown_network(const char *user_remap, const char *rootfs, const char *filename)
{
    int ret = 0;
    char *file_path = NULL;
    unsigned int host_uid = 0;
    unsigned int host_gid = 0;
    unsigned int size = 0;

    if (user_remap == NULL) {
        return 0;
    }
    ret = util_parse_user_remap(user_remap, &host_uid, &host_gid, &size);
    if (ret) {
        ERROR("Failed to parse user remap:'%s'", user_remap);
        ret = -1;
        goto out;
    }
O
openeuler-iSula 已提交
668 669
    if (realpath_in_scope(rootfs, filename, &file_path) < 0) {
        SYSERROR("Failed to get real path '%s' under rootfs '%s'", filename, rootfs);
L
LiuHao 已提交
670
        isulad_set_error_message("Failed to get real path '%s' under rootfs '%s'", filename, rootfs);
O
overweight 已提交
671 672 673 674 675
        ret = -1;
        goto out;
    }
    if (chown(file_path, host_uid, host_gid) != 0) {
        SYSERROR("Failed to chown network file '%s' to %u:%u", filename, host_uid, host_gid);
L
LiuHao 已提交
676 677 678 679 680
        isulad_set_error_message("Failed to chown network file '%s' to %u:%u: %s",
                                 filename,
                                 host_uid,
                                 host_gid,
                                 strerror(errno));
O
overweight 已提交
681 682 683 684 685 686 687 688 689
        ret = -1;
        goto out;
    }

out:
    free(file_path);
    return ret;
}

690
static int merge_network_for_universal_container(const host_config *host_spec, const char *runtime_root, const char *id)
O
overweight 已提交
691 692
{
    int ret = 0;
693 694
    int nret = 0;
    char root_path[PATH_MAX] = {0x00};
O
overweight 已提交
695

696 697
    if (runtime_root == NULL || id == NULL) {
        ERROR("empty runtime root or id");
O
overweight 已提交
698 699
        return -1;
    }
700 701 702 703 704

    nret = snprintf(root_path, PATH_MAX, "%s/%s", runtime_root, id);
    if (nret < 0 || nret >= PATH_MAX) {
        ERROR("Failed to print string");
        return -1;
O
overweight 已提交
705
    }
706 707 708 709 710 711 712 713 714 715 716 717 718

    ret = merge_resolv(host_spec, root_path, "/resolv.conf");
    if (ret) {
        return -1;
    }

    return 0;
}

static int merge_network_for_syscontainer(const host_config *host_spec, const char *rootfs, const char *hostname)
{
    int ret = 0;

O
overweight 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    ret = write_hostname_to_file(rootfs, hostname);
    if (ret) {
        return -1;
    }
    ret = chown_network(host_spec->user_remap, rootfs, "/etc/hostname");
    if (ret) {
        return -1;
    }
    ret = merge_hosts(host_spec, rootfs);
    if (ret) {
        return -1;
    }
    ret = chown_network(host_spec->user_remap, rootfs, "/etc/hosts");
    if (ret) {
        return -1;
    }
735
    ret = merge_resolv(host_spec, rootfs, "/etc/resolv.conf");
O
overweight 已提交
736 737 738 739 740 741 742 743 744 745
    if (ret) {
        return -1;
    }
    ret = chown_network(host_spec->user_remap, rootfs, "/etc/resolv.conf");
    if (ret) {
        return -1;
    }
    return 0;
}

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
int merge_network(const host_config *host_spec, const char *rootfs, const char *runtime_root,
                  const char *id, const char *hostname)
{
    int ret = 0;

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

    if (!host_spec->system_container || rootfs == NULL) {
        ret = merge_network_for_universal_container(host_spec, runtime_root, id);
    } else {
        ret = merge_network_for_syscontainer(host_spec, rootfs, hostname);
    }

    return ret;
}

O
overweight 已提交
764 765 766 767 768 769 770
static container_t *get_networked_container(const char *id, const char *connected_id, bool check_state)
{
    container_t *nc = NULL;

    nc = containers_store_get(connected_id);
    if (nc == NULL) {
        ERROR("No such container: %s", connected_id);
L
LiuHao 已提交
771
        isulad_set_error_message("No such container: %s", connected_id);
O
overweight 已提交
772 773 774 775
        return NULL;
    }
    if (strcmp(id, nc->common_config->id) == 0) {
        ERROR("cannot join own network");
L
LiuHao 已提交
776
        isulad_set_error_message("cannot join own network");
O
overweight 已提交
777 778 779 780 781 782 783
        goto cleanup;
    }
    if (!check_state) {
        return nc;
    }
    if (!is_running(nc->state)) {
        ERROR("cannot join network of a non running container: %s", connected_id);
L
LiuHao 已提交
784
        isulad_set_error_message("cannot join network of a non running container: %s", connected_id);
O
overweight 已提交
785 786 787 788
        goto cleanup;
    }
    if (is_restarting(nc->state)) {
        ERROR("Container %s is restarting, wait until the container is running", connected_id);
L
LiuHao 已提交
789
        isulad_set_error_message("Container %s is restarting, wait until the container is running", connected_id);
O
overweight 已提交
790 791 792 793 794 795 796 797 798 799 800
        goto cleanup;
    }

    return nc;

cleanup:
    container_unref(nc);
    return NULL;
}

static int init_container_network_confs_container(const char *id, const host_config *hc,
801
                                                  container_config_v2_common_config *v2_spec)
O
overweight 已提交
802 803 804 805 806 807 808 809 810 811 812 813
{
    int ret = 0;
    size_t len = strlen(SHARE_NAMESPACE_PREFIX);
    container_t *nc = NULL;

    nc = get_networked_container(id, hc->network_mode + len, false);
    if (nc == NULL) {
        ERROR("Error to get networked container");
        return -1;
    }

    if (nc->common_config->hostname_path != NULL) {
814 815
        free(v2_spec->hostname_path);
        v2_spec->hostname_path = util_strdup_s(nc->common_config->hostname_path);
O
overweight 已提交
816 817
    }
    if (nc->common_config->hosts_path != NULL) {
818 819
        free(v2_spec->hosts_path);
        v2_spec->hosts_path = util_strdup_s(nc->common_config->hosts_path);
O
overweight 已提交
820 821
    }
    if (nc->common_config->resolv_conf_path != NULL) {
822 823
        free(v2_spec->resolv_conf_path);
        v2_spec->resolv_conf_path = util_strdup_s(nc->common_config->resolv_conf_path);
O
overweight 已提交
824 825 826
    }

    if (nc->common_config->config != NULL && nc->common_config->config->hostname != NULL) {
827 828
        free(v2_spec->config->hostname);
        v2_spec->config->hostname = util_strdup_s(nc->common_config->config->hostname);
O
overweight 已提交
829 830 831 832 833 834
    }

    container_unref(nc);
    return ret;
}

835 836
static int create_default_hostname(const char *id, const char *rootpath, bool share_host,
                                   container_config_v2_common_config *v2_spec)
O
overweight 已提交
837 838
{
    int ret = 0;
839
    int nret = 0;
O
overweight 已提交
840
    char file_path[PATH_MAX] = { 0x0 };
841
    char hostname_content[MAX_HOST_NAME_LEN + 2] = { 0 };
O
overweight 已提交
842

843 844
    if (v2_spec->config->hostname == NULL) {
        if (share_host) {
O
overweight 已提交
845 846 847 848
            char hostname[MAX_HOST_NAME_LEN] = { 0x00 };
            ret = gethostname(hostname, sizeof(hostname));
            if (ret != 0) {
                ERROR("Get hostname error");
849
                goto out;
O
overweight 已提交
850
            }
851 852 853
            v2_spec->config->hostname = util_strdup_s(hostname);
        } else {
            v2_spec->config->hostname = util_strdup_s("localhost");
O
overweight 已提交
854 855 856
        }
    }

857 858
    nret = snprintf(file_path, PATH_MAX, "%s/%s/%s", rootpath, id, "hostname");
    if (nret < 0 || nret >= PATH_MAX) {
O
overweight 已提交
859 860
        ERROR("Failed to print string");
        ret = -1;
861
        goto out;
O
overweight 已提交
862
    }
863 864 865

    nret = snprintf(hostname_content, MAX_HOST_NAME_LEN + 2, "%s\n", v2_spec->config->hostname);
    if (nret < 0 || (size_t)nret >= sizeof(hostname_content)) {
O
overweight 已提交
866 867
        ERROR("Failed to print string");
        ret = -1;
868
        goto out;
O
overweight 已提交
869 870
    }

871

872
    if (util_write_file(file_path, hostname_content, strlen(hostname_content), NETWORK_MOUNT_FILE_MODE) != 0) {
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 904 905 906 907 908 909 910 911 912 913 914 915 916 917
        ERROR("Failed to create default hostname");
        ret = -1;
        goto out;
    }

    free(v2_spec->hostname_path);
    v2_spec->hostname_path = util_strdup_s(file_path);

out:
    return ret;
}

static int write_default_hosts(const char *file_path, const char *hostname)
{
    int ret = 0;
    char *content = NULL;
    size_t content_len = 0;
    const char *default_config = "127.0.0.1       localhost\n"
                                 "::1     localhost ip6-localhost ip6-loopback\n"
                                 "fe00::0 ip6-localnet\n"
                                 "ff00::0 ip6-mcastprefix\n"
                                 "ff02::1 ip6-allnodes\n"
                                 "ff02::2 ip6-allrouters\n";
    const char *loop_ip = "127.0.0.1    ";

    if (strlen(hostname) > (((SIZE_MAX - strlen(default_config)) - strlen(loop_ip)) - 2)) {
        ret = -1;
        goto out_free;
    }

    content_len = strlen(default_config) + strlen(loop_ip) + strlen(hostname) + 1 + 1;
    content = util_common_calloc_s(content_len);
    if (content == NULL) {
        ERROR("Memory out");
        ret = -1;
        goto out_free;
    }

    ret = snprintf(content, content_len, "%s%s%s\n", default_config, loop_ip, hostname);
    if (ret < 0 || (size_t)ret >=  content_len) {
        ERROR("Failed to generate default hosts");
        ret = -1;
        goto out_free;
    }

918
    ret = util_write_file(file_path, content, strlen(content), NETWORK_MOUNT_FILE_MODE);
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    if (ret != 0) {
        ret = -1;
        goto out_free;
    }

out_free:
    free(content);
    return ret;
}


static int create_default_hosts(const char *id, const char *rootpath, bool share_host,
                                container_config_v2_common_config *v2_spec)
{
    int ret = 0;
    char file_path[PATH_MAX] = { 0x0 };

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

    if (share_host && util_file_exists(ETC_HOSTS)) {
944
        ret = util_copy_file(ETC_HOSTS, file_path, NETWORK_MOUNT_FILE_MODE);
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
    } else {
        ret = write_default_hosts(file_path, v2_spec->config->hostname);
    }

    if (ret != 0) {
        ERROR("Failed to create default hosts");
        goto out;
    }

    free(v2_spec->hosts_path);
    v2_spec->hosts_path = util_strdup_s(file_path);

out:
    return ret;
}

static int write_default_resolve(const char *file_path)
{
    const char *default_ipv4_dns = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\n";;

965
    return util_write_file(file_path, default_ipv4_dns, strlen(default_ipv4_dns), NETWORK_MOUNT_FILE_MODE);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
}

static int create_default_resolv(const char *id, const char *rootpath, container_config_v2_common_config *v2_spec)
{
    int ret = 0;
    char file_path[PATH_MAX] = { 0x0 };

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

    if (util_file_exists(RESOLV_CONF_PATH)) {
981
        ret = util_copy_file(RESOLV_CONF_PATH, file_path, NETWORK_MOUNT_FILE_MODE);
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
    } else {
        ret = write_default_resolve(file_path);
    }

    if (ret != 0) {
        ERROR("Failed to create default resolv.conf");
        goto out;
    }

    free(v2_spec->resolv_conf_path);
    v2_spec->resolv_conf_path = util_strdup_s(file_path);

out:
    return ret;
}



int init_container_network_confs(const char *id, const char *rootpath, const host_config *hc,
                                 container_config_v2_common_config *v2_spec)
{
    int ret = 0;
    bool share_host = is_host(hc->network_mode);

    // is container mode
    if (is_container(hc->network_mode)) {
        return init_container_network_confs_container(id, hc, v2_spec);
    }

    if (create_default_hostname(id, rootpath, share_host, v2_spec) != 0) {
        ERROR("Failed to create default hostname");
        ret = -1;
        goto out;
    }

    if (create_default_hosts(id, rootpath, share_host, v2_spec) != 0) {
        ERROR("Failed to create default hosts");
        ret = -1;
        goto out;
    }

    if (create_default_resolv(id, rootpath, v2_spec) != 0) {
        ERROR("Failed to create default resolv.conf");
        ret = -1;
        goto out;
    }

out:
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
    return ret;
}

int container_initialize_networking(const container_t *cont)
{
    int ret = 0;
    size_t len = strlen(SHARE_NAMESPACE_PREFIX);
    container_t *nc = NULL;
    host_config *hc = cont->hostconfig;

    // is container mode
    if (is_container(hc->network_mode)) {
        nc = get_networked_container(cont->common_config->id, hc->network_mode + len, true);
        if (nc == NULL) {
            ERROR("Error to get networked container");
            return -1;
        }
    }

    container_unref(nc);
    return ret;
}

D
dogsheng 已提交
1053