execution.c 56.1 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/******************************************************************************
 * 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 list callback function definition
 ********************************************************************************/

#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 "constants.h"
#include "log.h"
#include "engine.h"
#include "console.h"
L
LiuHao 已提交
35
#include "isulad_config.h"
O
overweight 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "config.h"
#include "image.h"
#include "execution.h"
#include "verify.h"
#include "container_inspect.h"
#include "containers_store.h"
#include "supervisor.h"
#include "containers_gc.h"
#include "execution_extend.h"
#include "execution_information.h"
#include "execution_stream.h"
#include "execution_create.h"
#include "plugin.h"
#include "health_check.h"
#include "execution_network.h"
D
dogsheng 已提交
51
#include "runtime.h"
O
overweight 已提交
52 53 54
#include "specs_extend.h"
#include "utils.h"
#include "error.h"
W
wujing 已提交
55
#include "collector.h"
O
overweight 已提交
56

L
LiuHao 已提交
57

O
overweight 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
static int filter_by_label(const container_t *cont, const container_get_id_request *request)
{
    int ret = 0;
    size_t i, len_key, len_val;
    char *p_equal = NULL;
    json_map_string_string *labels = NULL;

    if (request->label == NULL) {
        ret = 0;
        goto out;
    }

    if (cont->common_config->config == NULL || cont->common_config->config->labels == NULL || \
        cont->common_config->config->labels->len == 0) {
        ERROR("No such container: %s", request->id_or_name);
L
LiuHao 已提交
73
        isulad_set_error_message("No such container: %s", request->id_or_name);
O
overweight 已提交
74 75 76 77 78 79
        ret = -1;
        goto out;
    }
    p_equal = strchr(request->label, '=');
    if (p_equal == NULL) {
        ERROR("Invalid label: %s", request->label);
L
LiuHao 已提交
80
        isulad_set_error_message("Invalid label: %s", request->label);
O
overweight 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        ret = -1;
        goto out;
    }
    len_key = (size_t)(p_equal - request->label);
    len_val = (strlen(request->label) - len_key) - 1;
    labels = cont->common_config->config->labels;
    for (i = 0; i < labels->len; i++) {
        if (strlen(labels->keys[i]) == len_key && strncmp(labels->keys[i], request->label, len_key) == 0 && \
            strlen(labels->values[i]) == len_val && strncmp(labels->values[i], p_equal + 1, len_val) == 0) {
            ret = 0;
            goto out;
        }
    }
    ret = -1;
    ERROR("No such container: %s", request->id_or_name);
L
LiuHao 已提交
96
    isulad_set_error_message("No such container: %s", request->id_or_name);
O
overweight 已提交
97 98 99 100 101 102 103 104 105 106 107 108

out:
    return ret;
}

static void pack_get_id_response(container_get_id_response *response, const char *id, uint32_t cc)
{
    if (response == NULL) {
        return;
    }

    response->cc = cc;
L
LiuHao 已提交
109 110
    if (g_isulad_errmsg != NULL) {
        response->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
111 112 113 114 115 116 117
        DAEMON_CLEAR_ERRMSG();
    }
    if (id != NULL) {
        response->id = util_strdup_s(id);
    }
}

G
g00501519 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static void pack_get_runtime_response(container_get_runtime_response *response, const char *runtime, uint32_t cc)
{
    if (response == NULL) {
        return;
    }

    response->cc = cc;
    if (g_isulad_errmsg != NULL) {
        response->errmsg = util_strdup_s(g_isulad_errmsg);
        DAEMON_CLEAR_ERRMSG();
    }
    if (runtime != NULL) {
        response->runtime = util_strdup_s(runtime);
    }
}

O
overweight 已提交
134 135 136 137 138 139
/*
 * This function gets long id of container by name or short id
 */
static int container_get_id_cb(const container_get_id_request *request, container_get_id_response **response)
{
    char *id = NULL;
L
LiuHao 已提交
140
    uint32_t cc = ISULAD_SUCCESS;
O
overweight 已提交
141 142 143 144 145 146 147 148 149 150 151
    container_t *cont = NULL;

    DAEMON_CLEAR_ERRMSG();
    if (request == NULL || response == NULL) {
        ERROR("Invalid NULL input");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(container_get_id_response));
    if (*response == NULL) {
        ERROR("Out of memory");
L
LiuHao 已提交
152
        cc = ISULAD_ERR_MEMOUT;
O
overweight 已提交
153 154 155 156 157
        goto pack_response;
    }

    if (!util_valid_container_id_or_name(request->id_or_name)) {
        ERROR("Invalid container name: %s", request->id_or_name ? request->id_or_name : "");
L
LiuHao 已提交
158 159
        isulad_set_error_message("Invalid container name: %s", request->id_or_name ? request->id_or_name : "");
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
160 161 162 163 164
        goto pack_response;
    }

    cont = containers_store_get(request->id_or_name);
    if (cont == NULL) {
L
LiuHao 已提交
165
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
166
        ERROR("No such container: %s", request->id_or_name);
L
LiuHao 已提交
167
        isulad_set_error_message("No such container: %s", request->id_or_name);
O
overweight 已提交
168 169 170 171
        goto pack_response;
    }

    if (filter_by_label(cont, request) != 0) {
L
LiuHao 已提交
172
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
173 174 175 176 177 178 179 180 181
        goto pack_response;
    }

    id = cont->common_config->id;

pack_response:
    pack_get_id_response(*response, id, cc);

    container_unref(cont);
L
LiuHao 已提交
182
    return (cc == ISULAD_SUCCESS) ? 0 : -1;
O
overweight 已提交
183 184
}

G
g00501519 已提交
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
static int container_get_runtime_cb(const char *real_id, container_get_runtime_response **response)
{
    char *runtime = NULL;
    uint32_t cc = ISULAD_SUCCESS;
    container_t *cont = NULL;

    DAEMON_CLEAR_ERRMSG();
    if (real_id == NULL || response == NULL) {
        ERROR("Invalid NULL input");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(container_get_runtime_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        cc = ISULAD_ERR_MEMOUT;
        goto pack_response;
    }

    if (!util_valid_container_id_or_name(real_id)) {
        ERROR("Invalid container name: %s", real_id);
        isulad_set_error_message("Invalid container name: %s", real_id);
        cc = ISULAD_ERR_EXEC;
        goto pack_response;
    }

    cont = containers_store_get(real_id);
    if (cont == NULL) {
        cc = ISULAD_ERR_EXEC;
        ERROR("No such container: %s", real_id);
        isulad_set_error_message("No such container: %s", real_id);
        goto pack_response;
    }

    runtime = cont->runtime;

pack_response:
    pack_get_runtime_response(*response, runtime, cc);

    container_unref(cont);
    return (cc == ISULAD_SUCCESS) ? 0 : -1;
}

O
overweight 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static int send_signal_to_process(pid_t pid, unsigned long long start_time, uint32_t signal)
{
    if (util_process_alive(pid, start_time) == false) {
        if (signal == SIGTERM || signal == SIGKILL) {
            WARN("Process %d is not alive", pid);
            return 0;
        } else {
            ERROR("Process (pid=%d) is not alive, can not kill with signal %u", pid, signal);
            return -1;
        }
    } else {
        int ret = kill(pid, (int)signal);
        if (ret < 0) {
            ERROR("Can not kill process (pid=%d) with signal %u: %s", pid, signal, strerror(errno));
            return -1;
        }
    }

    return 0;
}

static int umount_dev_tmpfs_for_system_container(const container_t *cont)
{
251
    if (cont->hostconfig != NULL && cont->hostconfig->system_container && cont->hostconfig->external_rootfs != NULL) {
O
overweight 已提交
252
        char rootfs_dev_path[PATH_MAX] = { 0 };
O
openeuler-iSula 已提交
253 254
        int nret = snprintf(rootfs_dev_path, sizeof(rootfs_dev_path), "%s/dev", cont->common_config->base_fs);
        if ((size_t)nret >= sizeof(rootfs_dev_path) || nret < 0) {
O
overweight 已提交
255 256 257
            ERROR("Out of memory");
            return -1;
        }
Z
zhangsong34 已提交
258
        if (umount(rootfs_dev_path) < 0 && errno != ENOENT) {
259
            WARN("Failed to umount dev tmpfs: %s, error: %s", rootfs_dev_path, strerror(errno));
O
overweight 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272
        }
    }
    return 0;
}

static int do_clean_container(const container_t *cont, pid_t pid)
{
    int ret = 0;
    char *engine_log_path = NULL;
    char *loglevel = NULL;
    char *logdriver = NULL;
    const char *id = cont->common_config->id;
    const char *runtime = cont->runtime;
D
dogsheng 已提交
273
    rt_clean_params_t params = { 0 };
O
overweight 已提交
274 275 276 277 278 279 280

    if (conf_get_daemon_log_config(&loglevel, &logdriver, &engine_log_path) != 0) {
        ERROR("Failed to get log config");
        ret = -1;
        goto out;
    }

D
dogsheng 已提交
281 282 283 284 285 286 287
    params.rootpath = cont->root_path;
    params.statepath = cont->state_path;
    params.logpath = engine_log_path;
    params.loglevel = loglevel;
    params.pid = pid;

    ret = runtime_clean_resource(id, runtime, &params);
O
overweight 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    if (ret != 0) {
        ERROR("Failed to clean failed started container %s", id);
        ret = -1;
        goto out;
    }

    if (im_umount_container_rootfs(cont->common_config->image_type, cont->common_config->image, id)) {
        ERROR("Failed to umount rootfs for container %s", id);
        ret = -1;
        goto out;
    }

    if (umount_dev_tmpfs_for_system_container(cont) < 0) {
        ret = -1;
        goto out;
    }

out:
    free(loglevel);
    free(engine_log_path);
    free(logdriver);
    return ret;
}

int clean_container_resource(const char *id, const char *runtime, pid_t pid)
{
    int ret = 0;
    container_t *cont = NULL;

    if (id == NULL || runtime == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    cont = containers_store_get(id);
    if (cont == NULL) {
        WARN("No such container:%s", id);
        goto out;
    }

    ret = do_clean_container(cont, pid);
    if (ret != 0) {
        ERROR("Runtime clean container resource failed");
        ret = -1;
        goto out;
    }
out:
    container_unref(cont);
    return ret;
}

static int start_request_check(const container_start_request *h)
{
    int ret = 0;

    if (h == NULL || h->id == NULL) {
        ERROR("recive NULL Request id");
        ret = -1;
        goto out;
    }

    if (!util_valid_container_id_or_name(h->id)) {
        ERROR("Invalid container name %s", h->id);
L
LiuHao 已提交
352
        isulad_set_error_message("Invalid container name %s", h->id);
O
overweight 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static int prepare_start_state_files(const container_t *cont, char **exit_fifo, int *exit_fifo_fd, char **pid_file)
{
    int ret = 0;
    int nret = 0;
    char container_state[PATH_MAX] = { 0 };
    char pidfile[PATH_MAX] = { 0 };
    const char *id = cont->common_config->id;

O
openeuler-iSula 已提交
369 370
    nret = snprintf(container_state, sizeof(container_state), "%s/%s", cont->state_path, id);
    if (nret < 0 || (size_t)nret >= sizeof(container_state)) {
O
overweight 已提交
371 372 373 374 375 376 377 378 379 380 381 382
        ERROR("Failed to sprintf container_state");
        ret = -1;
        goto out;
    }

    nret = util_mkdir_p(container_state, TEMP_DIRECTORY_MODE);
    if (nret < 0) {
        ERROR("Unable to create container state directory %s.", container_state);
        ret = -1;
        goto out;
    }

O
openeuler-iSula 已提交
383 384
    nret = snprintf(pidfile, sizeof(pidfile), "%s/pid.file", container_state);
    if (nret < 0 || (size_t)nret >= sizeof(pidfile)) {
O
overweight 已提交
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
        ERROR("Failed to sprintf pidfile");
        ret = -1;
        goto out;
    }
    *pid_file = util_strdup_s(pidfile);
    if (*pid_file == NULL) {
        ERROR("Failed to dup pid file in state directory %s", container_state);
        ret = -1;
        goto out;
    }

    *exit_fifo = exit_fifo_create(container_state);
    if (*exit_fifo == NULL) {
        ERROR("Failed to create exit FIFO in state directory %s", container_state);
        ret = -1;
        goto out;
    }

    *exit_fifo_fd = exit_fifo_open(*exit_fifo);
    if (*exit_fifo_fd < 0) {
        ERROR("Failed to open exit FIFO %s", *exit_fifo);
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static void umount_rootfs_on_failure(const container_t *cont)
{
    const char *id = cont->common_config->id;
    int nret = im_umount_container_rootfs(cont->common_config->image_type, cont->common_config->image, id);
    if (nret != 0) {
        ERROR("Failed to umount rootfs for container %s", id);
    }
}

static int chmod_runtime_bundle_permission(const char *runtime)
{
    int ret = 0;
    char *bundle_dir = NULL;
    char *engine_dir = NULL;
    char *root_dir = NULL;

    bundle_dir = conf_get_routine_rootdir(runtime);
    if (bundle_dir == NULL) {
        ret = -1;
        goto error_out;
    }

    engine_dir = conf_get_engine_rootpath();
    if (engine_dir == NULL) {
        ret = -1;
        goto error_out;
    }

L
LiuHao 已提交
442
    root_dir = conf_get_isulad_rootdir();
O
overweight 已提交
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
    if (root_dir == NULL) {
        ret = -1;
        goto error_out;
    }

    ret = chmod(bundle_dir, USER_REMAP_DIRECTORY_MODE);
    if (ret != 0) {
        ERROR("Failed to chmod bundle dir '%s' for user remap", bundle_dir);
        goto error_out;
    }
    ret = chmod(engine_dir, USER_REMAP_DIRECTORY_MODE);
    if (ret != 0) {
        ERROR("Failed to chmod engine dir '%s' for user remap", engine_dir);
        goto error_out;
    }
    ret = chmod(root_dir, USER_REMAP_DIRECTORY_MODE);
    if (ret != 0) {
        ERROR("Failed to chmod root dir '%s' for user remap", root_dir);
        goto error_out;
    }

error_out:
    free(bundle_dir);
    free(engine_dir);
    free(root_dir);
    return ret;
}

static int mount_host_channel(const host_config_host_channel *host_channel, const char *user_remap)
{
    char properties[MOUNT_PROPERTIES_SIZE] = { 0 };

    if (host_channel == NULL) {
        return 0;
    }
    if (detect_mount(host_channel->path_on_host)) {
        return 0;
    }
O
openeuler-iSula 已提交
481 482 483
    int nret = snprintf(properties, sizeof(properties), "mode=1777,size=%llu",
                        (long long unsigned int)host_channel->size);
    if (nret < 0 || (size_t)nret >= sizeof(properties)) {
O
overweight 已提交
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
        ERROR("Failed to generate mount properties");
        return -1;
    }
    if (mount("tmpfs", host_channel->path_on_host, "tmpfs",
              MS_NOEXEC | MS_NOSUID | MS_NODEV,
              (void *)properties)) {
        ERROR("Failed to mount host path '%s'", host_channel->path_on_host);
        return -1;
    }
    if (user_remap != NULL) {
        unsigned int host_uid = 0;
        unsigned int host_gid = 0;
        unsigned int size = 0;
        if (util_parse_user_remap(user_remap, &host_uid, &host_gid, &size)) {
            ERROR("Failed to split string '%s'.", user_remap);
            return -1;
        }
        if (chown(host_channel->path_on_host, host_uid, host_gid) != 0) {
            ERROR("Failed to chown host path '%s'.", host_channel->path_on_host);
            return -1;
        }
    }
    return 0;
}

static int mount_dev_tmpfs_for_system_container(const container_t *cont)
{
    char rootfs_dev_path[PATH_MAX] = { 0 };

    if (cont == NULL || cont->hostconfig == NULL || cont->common_config == NULL) {
        return 0;
    }
516
    if (!cont->hostconfig->system_container || cont->hostconfig->external_rootfs == NULL) {
O
overweight 已提交
517 518
        return 0;
    }
O
openeuler-iSula 已提交
519 520
    int nret = snprintf(rootfs_dev_path, sizeof(rootfs_dev_path), "%s/dev", cont->common_config->base_fs);
    if (nret < 0 || (size_t)nret >= sizeof(rootfs_dev_path)) {
O
overweight 已提交
521 522 523 524 525 526 527 528 529
        ERROR("Out of memory");
        return -1;
    }
    if (!util_dir_exists(rootfs_dev_path)) {
        if (util_mkdir_p(rootfs_dev_path, CONFIG_DIRECTORY_MODE)) {
            ERROR("Failed to mkdir '%s'", rootfs_dev_path);
            return -1;
        }
    }
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    /* set /dev mount size to half of container memory limit */
    if (cont->hostconfig->memory > 0) {
        char mnt_opt[MOUNT_PROPERTIES_SIZE] = { 0 };
        nret = snprintf(mnt_opt, sizeof(mnt_opt), "size=%lld,mode=755", (long long int)(cont->hostconfig->memory / 2));
        if (nret < 0 || (size_t)nret >= sizeof(mnt_opt)) {
            ERROR("Out of memory");
            return -1;
        }
        if (mount("tmpfs", rootfs_dev_path, "tmpfs", 0, mnt_opt) != 0) {
            ERROR("Failed to mount dev tmpfs on '%s'", rootfs_dev_path);
            return -1;
        }
    } else {
        if (mount("tmpfs", rootfs_dev_path, "tmpfs", 0, "mode=755") != 0) {
            ERROR("Failed to mount dev tmpfs on '%s'", rootfs_dev_path);
            return -1;
        }
O
overweight 已提交
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
    }
    if (cont->hostconfig->user_remap != NULL) {
        unsigned int host_uid = 0;
        unsigned int host_gid = 0;
        unsigned int size = 0;
        if (util_parse_user_remap(cont->hostconfig->user_remap, &host_uid, &host_gid, &size)) {
            ERROR("Failed to split string '%s'.", cont->hostconfig->user_remap);
            return -1;
        }
        if (chown(rootfs_dev_path, host_uid, host_gid) != 0) {
            ERROR("Failed to chown host path '%s'.", rootfs_dev_path);
            return -1;
        }
    }
    return 0;
}

static int prepare_user_remap_config(const container_t *cont)
{
    if (cont == NULL) {
        return 0;
    }

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

    if (cont->hostconfig->user_remap != NULL) {
        if (chmod_runtime_bundle_permission(cont->runtime)) {
            ERROR("Failed to chmod bundle permission for user remap");
            return -1;
        }
    }

    if (cont->hostconfig->host_channel != NULL) {
        if (mount_host_channel(cont->hostconfig->host_channel, cont->hostconfig->user_remap)) {
            ERROR("Failed to mount host channel");
            return -1;
        }
    }
    return 0;
}

590
static int generate_user_and_groups_conf(const container_t *cont, defs_process_user **puser)
O
overweight 已提交
591 592 593 594 595 596 597 598 599
{
    int ret = -1;
    char *username = NULL;

    if (cont == NULL || cont->common_config == NULL) {
        ERROR("Can not found container config");
        return -1;
    }

600
    *puser = util_common_calloc_s(sizeof(defs_process_user));
O
overweight 已提交
601 602 603 604 605 606 607 608 609
    if (*puser == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (cont->common_config->config != NULL) {
        username = cont->common_config->config->user;
    }

610
    /* username may be NULL, we will handle it as UID 0 in get_user */
O
overweight 已提交
611 612 613 614
    ret = im_get_user_conf(cont->common_config->image_type, cont->common_config->base_fs, cont->hostconfig, username,
                           *puser);
    if (ret != 0) {
        ERROR("Get user failed with '%s'", username ? username : "");
615
        free_defs_process_user(*puser);
O
overweight 已提交
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 668 669 670 671 672 673 674 675 676
        *puser = NULL;
    }

    return ret;
}

static int create_env_path_dir(const char *env_path)
{
    int ret = 0;
    size_t len = 0;
    size_t i = 0;
    char *dir = NULL;

    len = strlen(env_path);
    if (len == 0) {
        return 0;
    }
    dir = util_strdup_s(env_path);
    for (i = len - 1; i > 0; i--) {
        if (dir[i] == '/') {
            dir[i] = '\0';
            break;
        }
    }
    if (strlen(dir) == 0) {
        free(dir);
        return 0;
    }
    ret = util_mkdir_p(dir, DEFAULT_SECURE_DIRECTORY_MODE);
    free(dir);
    return ret;
}

static int write_env_content(const char *env_path, const char **env, size_t env_len)
{
    int ret = 0;
    int fd = -1;
    size_t i = 0;
    ssize_t nret = 0;

    ret = create_env_path_dir(env_path);
    if (ret < 0) {
        ERROR("Failed to create env path dir");
        return ret;
    }
    fd = util_open(env_path, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_SECURE_FILE_MODE);
    if (fd < 0) {
        SYSERROR("Failed to create env file: %s", env_path);
        ret = -1;
        goto out;
    }
    if (env != NULL) {
        for (i = 0; i < env_len; i++) {
            size_t len = strlen(env[i]) + strlen("\n") + 1;
            char *env_content = NULL;
            env_content = util_common_calloc_s(len);
            if (env_content == NULL) {
                ERROR("Out of memory");
                ret = -1;
                goto out;
            }
O
openeuler-iSula 已提交
677 678
            nret = snprintf(env_content, len, "%s\n", env[i]);
            if (nret < 0 || (size_t)nret >= len) {
O
overweight 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
                ERROR("Out of memory");
                free(env_content);
                ret = -1;
                goto out;
            }
            nret = util_write_nointr(fd, env_content, strlen(env_content));
            if (nret < 0 || nret != len - 1) {
                SYSERROR("Write env file failed");
                free(env_content);
                ret = -1;
                goto out;
            }
            free(env_content);
        }
    }
out:
    if (fd >= 0) {
        close(fd);
    }
    return ret;
}

static int write_env_to_target_file(const container_t *cont)
{
    int ret = 0;
    char *env_path = NULL;

    if (cont->hostconfig->env_target_file == NULL || cont->common_config->config == NULL) {
        return 0;
    }
    env_path = util_path_join(cont->common_config->base_fs, cont->hostconfig->env_target_file);
    if (env_path == NULL) {
        ERROR("Failed to get env target file path: %s", cont->hostconfig->env_target_file);
        return -1;
    }
    ret = write_env_content(env_path,
                            (const char **)cont->common_config->config->env,
                            cont->common_config->config->env_len);
    free(env_path);
    return ret;
}

static int do_post_start_on_success(const char *id, const char *runtime,
D
dogsheng 已提交
722
                                    const char *pidfile, int exit_fifo_fd, const container_pid_t *pid_info)
O
overweight 已提交
723 724 725 726
{
    int ret = 0;

    // exit_fifo_fd was closed in supervisor_add_exit_monitor
D
dogsheng 已提交
727
    if (supervisor_add_exit_monitor(exit_fifo_fd, pid_info, id, runtime)) {
O
overweight 已提交
728 729 730 731 732 733
        ERROR("Failed to add exit monitor to supervisor");
        ret = -1;
    }
    return ret;
}

D
dogsheng 已提交
734
static void clean_resources_on_failure(const container_t *cont, const char *engine_log_path, const char *loglevel)
O
overweight 已提交
735 736 737 738
{
    int ret = 0;
    const char *id = cont->common_config->id;
    const char *runtime = cont->runtime;
D
dogsheng 已提交
739
    rt_clean_params_t params = { 0 };
O
overweight 已提交
740

D
dogsheng 已提交
741 742 743 744 745
    params.rootpath = cont->root_path;
    params.statepath = cont->state_path;
    params.logpath = engine_log_path;
    params.loglevel = loglevel;
    params.pid = 0;
O
overweight 已提交
746

D
dogsheng 已提交
747
    ret = runtime_clean_resource(id, runtime, &params);
O
overweight 已提交
748 749 750 751 752 753 754
    if (ret != 0) {
        ERROR("Failed to clean failed started container %s", id);
    }

    return;
}

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
static int renew_oci_config(const container_t *cont, const oci_runtime_spec *oci_spec)
{
    int ret = 0;
    defs_process_user *puser = NULL;

    if (generate_user_and_groups_conf(cont, &puser) != 0) {
        ret = -1;
        goto out;
    }

    free_defs_process_user(oci_spec->process->user);
    oci_spec->process->user = puser;
    puser = NULL;

out:
    free_defs_process_user(puser);
    return ret;
}

O
overweight 已提交
774
static int do_start_container(container_t *cont, const char *console_fifos[], bool reset_rm,
D
dogsheng 已提交
775
                              container_pid_t *pid_info)
O
overweight 已提交
776 777 778 779 780 781 782 783 784 785 786 787
{
    int ret = 0;
    int nret = 0;
    int exit_fifo_fd = -1;
    bool tty = false;
    bool open_stdin = false;
    unsigned int start_timeout = 0;
    char *engine_log_path = NULL;
    char *loglevel = NULL;
    char *logdriver = NULL;
    char *exit_fifo = NULL;
    char *pidfile = NULL;
788
    char bundle[PATH_MAX] = { 0 };
O
overweight 已提交
789 790
    const char *runtime = cont->runtime;
    const char *id = cont->common_config->id;
791 792
    oci_runtime_spec *oci_spec = NULL;
    rt_create_params_t create_params = { 0 };
D
dogsheng 已提交
793
    rt_start_params_t start_params = { 0 };
O
overweight 已提交
794

795 796 797 798 799 800 801
    nret = snprintf(bundle, sizeof(bundle), "%s/%s", cont->root_path, id);
    if (nret < 0 || (size_t)nret >= sizeof(bundle)) {
        ERROR("Failed to print bundle string");
        ret = -1;
        goto out;
    }
    DEBUG("bd:%s, state:%s", bundle, cont->state_path);
O
overweight 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    if (mount_dev_tmpfs_for_system_container(cont) < 0) {
        ret = -1;
        goto out;
    }

    if (prepare_user_remap_config(cont) != 0) {
        ret = -1;
        goto out;
    }

    if (write_env_to_target_file(cont) < 0) {
        ret = -1;
        goto out;
    }

    if (reset_rm && !reset_restart_manager(cont, true)) {
        ERROR("Failed to reset restart manager");
L
LiuHao 已提交
819
        isulad_set_error_message("Failed to reset restart manager");
O
overweight 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
        ret = -1;
        goto out;
    }

    if (conf_get_daemon_log_config(&loglevel, &logdriver, &engine_log_path) != 0) {
        ret = -1;
        goto out;
    }

    nret = prepare_start_state_files(cont, &exit_fifo, &exit_fifo_fd, &pidfile);
    if (nret != 0) {
        ret = -1;
        goto out;
    }

835 836 837
    oci_spec = load_oci_config(cont->root_path, id);
    if (oci_spec == NULL) {
        ERROR("Failed to load oci config");
O
overweight 已提交
838 839 840 841
        ret = -1;
        goto close_exit_fd;
    }

842 843 844
    nret = im_mount_container_rootfs(cont->common_config->image_type, cont->common_config->image, id);
    if (nret != 0) {
        ERROR("Failed to mount rootfs for container %s", id);
O
overweight 已提交
845 846 847 848
        ret = -1;
        goto close_exit_fd;
    }

849
    if (renew_oci_config(cont, oci_spec) != 0) {
O
overweight 已提交
850 851 852 853
        ret = -1;
        goto close_exit_fd;
    }

854
    if (verify_container_settings_start(oci_spec) != 0) {
D
dogsheng 已提交
855 856 857 858
        ret = -1;
        goto close_exit_fd;
    }

L
LiFeng 已提交
859 860 861 862 863 864
    if (save_oci_config(id, cont->root_path, oci_spec) != 0) {
        ERROR("Failed to save container settings");
        ret = -1;
        goto close_exit_fd;
    }

O
overweight 已提交
865 866 867 868 869 870
    start_timeout = conf_get_start_timeout();
    if (cont->common_config->config != NULL) {
        tty = cont->common_config->config->tty;
        open_stdin = cont->common_config->config->open_stdin;
    }

871 872 873 874 875 876 877
    if (plugin_event_container_pre_start(cont)) {
        ERROR("Plugin event pre start failed ");
        plugin_event_container_post_stop(cont); /* ignore error */
        ret = -1;
        goto close_exit_fd;
    }

878 879 880 881 882 883 884
    create_params.bundle = bundle;
    create_params.state = cont->state_path;
    create_params.oci_config_data = oci_spec;
    create_params.terminal = tty;
    create_params.stdin = console_fifos[0];
    create_params.stdout = console_fifos[1];
    create_params.stderr = console_fifos[2];
885
    create_params.exit_fifo = exit_fifo;
886 887
    create_params.tty = tty;
    create_params.open_stdin = open_stdin;
888 889 890 891 892 893

    if (runtime_create(id, runtime, &create_params) != 0) {
        ret = -1;
        goto close_exit_fd;
    }

D
dogsheng 已提交
894
    start_params.rootpath = cont->root_path;
895
    start_params.state = cont->state_path;
D
dogsheng 已提交
896 897 898 899 900 901 902 903 904 905
    start_params.tty = tty;
    start_params.open_stdin = open_stdin;
    start_params.logpath = engine_log_path;
    start_params.loglevel = loglevel;
    start_params.console_fifos = console_fifos;
    start_params.start_timeout = start_timeout;
    start_params.container_pidfile = pidfile;
    start_params.exit_fifo = exit_fifo;

    ret = runtime_start(id, runtime, &start_params, pid_info);
O
overweight 已提交
906 907 908 909
    if (ret == 0) {
        if (do_post_start_on_success(id, runtime, pidfile, exit_fifo_fd, pid_info) != 0) {
            ERROR("Failed to do post start on runtime start success");
            ret = -1;
D
dogsheng 已提交
910
            goto clean_resources;
O
overweight 已提交
911 912
        }
    } else {
D
dogsheng 已提交
913
        goto close_exit_fd;
O
overweight 已提交
914 915 916 917 918
    }
    goto out;

close_exit_fd:
    close(exit_fifo_fd);
D
dogsheng 已提交
919 920 921

clean_resources:
    clean_resources_on_failure(cont, engine_log_path, loglevel);
922

O
overweight 已提交
923 924 925 926 927 928
out:
    free(loglevel);
    free(engine_log_path);
    free(logdriver);
    free(exit_fifo);
    free(pidfile);
929
    free_oci_runtime_spec(oci_spec);
O
overweight 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
    if (ret != 0) {
        umount_rootfs_on_failure(cont);
        (void)umount_dev_tmpfs_for_system_container(cont);
    }
    return ret;
}

static bool save_after_auto_remove(container_t *cont)
{
    if (cont->hostconfig != NULL && cont->hostconfig->auto_remove) {
        int nret = set_container_to_removal(cont);
        if (nret != 0) {
            ERROR("Failed to set container %s state to removal", cont->common_config->id);
            return true;
        }
        container_unlock(cont);
        nret = cleanup_container(cont, true);
        container_lock(cont);
        if (nret != 0) {
            ERROR("Failed to cleanup container %s", cont->common_config->id);
            return true;
        }
D
dogsheng 已提交
952
        return false; /* do not save container if already auto removed */
O
overweight 已提交
953 954 955 956 957 958 959 960
    }

    return true;
}

int start_container(container_t *cont, const char *console_fifos[], bool reset_rm)
{
    int ret = 0;
D
dogsheng 已提交
961 962
    container_pid_t pid_info = { 0 };
    int exit_code = 125;
O
overweight 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975 976

    if (cont == NULL || console_fifos == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    container_lock(cont);

    if (reset_rm && is_running(cont->state)) {
        ret = 0;
        goto out;
    }

D
dogsheng 已提交
977 978
    if (is_paused(cont->state)) {
        ERROR("Cannot start a paused container, try unpause instead");
L
LiuHao 已提交
979
        isulad_set_error_message("Cannot start a paused container, try unpause instead.");
D
dogsheng 已提交
980 981 982 983
        ret = -1;
        goto out;
    }

O
overweight 已提交
984 985
    if (is_removal_in_progress(cont->state) || is_dead(cont->state)) {
        ERROR("Container is marked for removal and cannot be started.");
L
LiuHao 已提交
986
        isulad_set_error_message("Container is marked for removal and cannot be started.");
O
overweight 已提交
987 988 989 990 991
        ret = -1;
        goto out;
    }

    if (gc_is_gc_progress(cont->common_config->id)) {
L
LiuHao 已提交
992
        isulad_set_error_message("You cannot start container %s in garbage collector progress.", cont->common_config->id);
O
overweight 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
        ERROR("You cannot start container %s in garbage collector progress.", cont->common_config->id);
        ret = -1;
        goto out;
    }

    if (container_initialize_networking(cont) != 0) {
        ERROR("Failed to initialize networking");
        ret = -1;
        goto out;
    }

    ret = do_start_container(cont, console_fifos, reset_rm, &pid_info);
    if (ret != 0) {
        ERROR("Runtime start container failed");
        ret = -1;
D
dogsheng 已提交
1008
        goto set_stopped;
O
overweight 已提交
1009
    } else {
D
dogsheng 已提交
1010
        state_set_running(cont->state, &pid_info, true);
O
overweight 已提交
1011 1012 1013 1014 1015
        cont->common_config->has_been_manually_stopped = false;
        init_health_monitor(cont->common_config->id);
        goto save_container;
    }

D
dogsheng 已提交
1016
set_stopped:
L
LiuHao 已提交
1017 1018
    container_state_set_error(cont->state, (const char *)g_isulad_errmsg);
    util_contain_errmsg(g_isulad_errmsg, &exit_code);
D
dogsheng 已提交
1019 1020 1021 1022 1023 1024
    state_set_stopped(cont->state, exit_code);
    container_wait_stop_cond_broadcast(cont);
    if (!save_after_auto_remove(cont)) {
        goto out;
    }

O
overweight 已提交
1025 1026 1027 1028 1029 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 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
save_container:
    if (container_to_disk(cont)) {
        ERROR("Failed to save container \"%s\" to disk", cont->common_config->id);
        ret = -1;
        goto out;
    }
out:
    container_unlock(cont);
    return ret;
}

static int prepare_start_io(container_t *cont, const container_start_request *request,
                            char **fifopath, char *fifos[], int stdinfd, struct io_write_wrapper *stdout_handler,
                            struct io_write_wrapper *stderr_handler)
{
    int ret = 0;
    char *id = NULL;
    pthread_t tid = 0;

    id = cont->common_config->id;

    if (request->attach_stdin || request->attach_stdout || request->attach_stderr) {
        if (create_daemon_fifos(id, cont->runtime, request->attach_stdin, request->attach_stdout,
                                request->attach_stderr, "start", fifos, fifopath)) {
            ret = -1;
            goto out;
        }

        if (ready_copy_io_data(-1, true, request->stdin, request->stdout, request->stderr,
                               stdinfd, stdout_handler, stderr_handler, (const char **)fifos, &tid)) {
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

static void pack_start_response(container_start_response *response, uint32_t cc, const char *id)
{
    if (response == NULL) {
        return;
    }

    response->cc = cc;
L
LiuHao 已提交
1071 1072
    if (g_isulad_errmsg != NULL) {
        response->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
        DAEMON_CLEAR_ERRMSG();
    }
    if (id != NULL) {
        response->id = util_strdup_s(id);
    }
}

static int container_start_prepare(container_t *cont, const container_start_request *request,
                                   int stdinfd, struct io_write_wrapper *stdout_handler,
                                   struct io_write_wrapper *stderr_handler,
                                   char **fifopath, char *fifos[])
{
    const char *id = cont->common_config->id;

    if (container_to_disk_locking(cont)) {
        ERROR("Failed to save container \"%s\" to disk", id);
L
LiuHao 已提交
1089
        isulad_set_error_message("Failed to save container \"%s\" to disk", id);
O
overweight 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
        return -1;
    }

    if (prepare_start_io(cont, request, fifopath, fifos, stdinfd, stdout_handler, stderr_handler) != 0) {
        return -1;
    }

    return 0;
}

static int container_start_cb(const container_start_request *request, container_start_response **response,
                              int stdinfd, struct io_write_wrapper *stdout_handler,
                              struct io_write_wrapper *stderr_handler)
{
L
LiuHao 已提交
1104
    uint32_t cc = ISULAD_SUCCESS;
O
overweight 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    char *id = NULL;
    char *fifos[3] = { NULL, NULL, NULL };
    char *fifopath = NULL;
    container_t *cont = NULL;

    DAEMON_CLEAR_ERRMSG();

    if (request == NULL || response == NULL) {
        ERROR("Invalid NULL input");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(container_start_response));
    if (*response == NULL) {
        ERROR("Out of memory");
L
LiuHao 已提交
1120
        cc = ISULAD_ERR_MEMOUT;
O
overweight 已提交
1121 1122 1123 1124
        goto pack_response;
    }

    if (start_request_check(request)) {
L
LiuHao 已提交
1125
        cc = ISULAD_ERR_INPUT;
O
overweight 已提交
1126 1127 1128 1129 1130
        goto pack_response;
    }

    cont = containers_store_get(request->id);
    if (cont == NULL) {
L
LiuHao 已提交
1131
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1132
        ERROR("No such container:%s", request->id);
L
LiuHao 已提交
1133
        isulad_set_error_message("No such container:%s", request->id);
O
overweight 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
        goto pack_response;
    }

    id = cont->common_config->id;
    set_log_prefix(id);

    EVENT("Event: {Object: %s, Type: Starting}", id);

    state_set_starting(cont->state);

    if (is_running(cont->state)) {
        INFO("Container is already running");
        goto pack_response;
    }

    if (container_start_prepare(cont, request, stdinfd, stdout_handler, stderr_handler, &fifopath, fifos) != 0) {
L
LiuHao 已提交
1150
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1151 1152 1153
        goto pack_response;
    }

D
dogsheng 已提交
1154
    if (start_container(cont, (const char **)fifos, true) != 0) {
L
LiuHao 已提交
1155
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1156 1157 1158 1159
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: Running}", id);
W
wujing 已提交
1160
    (void)isulad_monitor_send_container_event(id, START, -1, 0, NULL, NULL);
O
overweight 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

pack_response:
    delete_daemon_fifos(fifopath, (const char **)fifos);
    free(fifos[0]);
    free(fifos[1]);
    free(fifos[2]);
    free(fifopath);
    pack_start_response(*response, cc, id);
    if (cont != NULL) {
        state_reset_starting(cont->state);
        container_unref(cont);
    }
    free_log_prefix();
    malloc_trim(0);
L
LiuHao 已提交
1175
    return (cc == ISULAD_SUCCESS) ? 0 : -1;
O
overweight 已提交
1176 1177 1178 1179 1180
}

static int kill_with_signal(container_t *cont, uint32_t signal)
{
    int ret = 0;
W
wujing 已提交
1181
    int nret = 0;
O
overweight 已提交
1182
    const char *id = cont->common_config->id;
D
dogsheng 已提交
1183 1184
    bool need_unpause = is_paused(cont->state);
    rt_resume_params_t params = { 0 };
W
wujing 已提交
1185
    char annotations[EXTRA_ANNOTATION_MAX] = {0};
O
overweight 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

    if (container_exit_on_next(cont)) {
        ERROR("Failed to cancel restart manager");
        ret = -1;
        goto out;
    }
    cont->common_config->has_been_manually_stopped = true;
    (void)container_to_disk(cont);

    if (!is_running(cont->state)) {
        INFO("Container %s is already stopped", id);
        ret = 0;
        goto out;
    }
    if (is_restarting(cont->state)) {
        INFO("Container %s is currently restarting we do not need to send the signal to the process", id);
        ret = 0;
        goto out;
    }

    stop_health_checks(id);

    ret = send_signal_to_process(cont->state->state->pid, cont->state->state->start_time, signal);
    if (ret != 0) {
D
dogsheng 已提交
1210 1211 1212 1213
        ERROR("Failed to send signal to container %s with signal %d", id, signal);
    }
    if (signal == SIGKILL && need_unpause) {
        params.rootpath = cont->root_path;
1214
        params.state = cont->state_path;
D
dogsheng 已提交
1215 1216 1217 1218 1219
        if (runtime_resume(id, cont->runtime, &params) != 0) {
            ERROR("Cannot unpause container: %s", id);
            ret = -1;
            goto out;
        }
O
overweight 已提交
1220 1221
    }

W
wujing 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230
    nret = snprintf(annotations, sizeof(annotations), "signal=%d", signal);
    if (nret < 0 || (size_t)nret >= sizeof(annotations)) {
        ERROR("Failed to get signal string", id);
        ret = -1;
        goto out;
    }

    (void)isulad_monitor_send_container_event(id, KILL, -1, 0, NULL, annotations);

O
overweight 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
out:
    return ret;
}

static int force_kill(container_t *cont)
{
    int ret = 0;
    const char *id = cont->common_config->id;

    ret = kill_with_signal(cont, SIGKILL);
    if (ret != 0) {
        WARN("Failed to stop Container(%s), try to wait 'STOPPED' for 90 seconds", id);
    }
    ret = container_wait_stop(cont, 90);
    if (ret != 0) {
        WARN("Container(%s) stuck for 90 seconds, try to kill the monitor of container", id);
        ret = send_signal_to_process(cont->state->state->p_pid, cont->state->state->p_start_time, SIGKILL);
        if (ret != 0) {
            ERROR("Container stuck for 90 seconds and failed to kill the monitor of container, "
                  "please check the config");
L
LiuHao 已提交
1251 1252
            isulad_set_error_message("Container stuck for 90 seconds "
                                     "and failed to kill the monitor of container, please check configuration files");
O
overweight 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
            goto out;
        }
        ret = container_wait_stop(cont, -1);
    }
out:
    return ret;
}

int stop_container(container_t *cont, int timeout, bool force, bool restart)
{
    int ret = 0;
    char *id = NULL;

    if (cont == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    id = cont->common_config->id;

    container_lock(cont);

O
openeuler-iSula 已提交
1275 1276
    if (is_paused(cont->state)) {
        ERROR("Container %s is paused. Unpause the container before stopping or killing", id);
L
LiuHao 已提交
1277
        isulad_set_error_message("Container %s is paused. Unpause the container before stopping or killing", id);
O
openeuler-iSula 已提交
1278 1279 1280
        ret = -1;
        goto out;
    }
O
overweight 已提交
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
    // set AutoRemove flag to false before stop so the container won't be
    // removed during restart process
    if (restart) {
        cont->hostconfig->auto_remove = false;
    }

    if (!force) {
        ret = kill_with_signal(cont, SIGTERM);
        if (ret) {
            ERROR("Failed to grace shutdown container %s", id);
        }
        ret = container_wait_stop(cont, timeout);
        if (ret != 0) {
            ERROR("Failed to wait Container(%s) 'STOPPED' for %d seconds, force killing",
                  id, timeout);
            ret = force_kill(cont);
            if (ret != 0) {
                ERROR("Failed to force kill container %s", id);
                goto out;
            }
        }
    } else {
        ret = force_kill(cont);
        if (ret != 0) {
            ERROR("Failed to force kill container %s", id);
            goto out;
        }
    }
out:
    if (restart) {
        cont->hostconfig->auto_remove = cont->hostconfig->auto_remove_bak;
    }
    container_unlock(cont);
    return ret;
}

static int restart_container(container_t *cont)
{
    int ret = 0;
    char timebuffer[512] = { 0 };
    const char *id = cont->common_config->id;
    const char *runtime = cont->runtime;
    const char *rootpath = cont->root_path;
D
dogsheng 已提交
1324
    rt_restart_params_t params = { 0 };
O
overweight 已提交
1325 1326 1327 1328 1329

    container_lock(cont);

    if (is_removal_in_progress(cont->state) || is_dead(cont->state)) {
        ERROR("Container is marked for removal and cannot be started.");
L
LiuHao 已提交
1330
        isulad_set_error_message("Container is marked for removal and cannot be started.");
O
overweight 已提交
1331 1332 1333 1334 1335
        goto out;
    }

    (void)get_now_time_buffer(timebuffer, sizeof(timebuffer));

D
dogsheng 已提交
1336 1337 1338
    params.rootpath = rootpath;

    ret = runtime_restart(id, runtime, &params);
O
overweight 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    if (ret == -2) {
        goto out;
    }

    if (ret == 0) {
        update_start_and_finish_time(cont->state, timebuffer);
    }

    if (container_to_disk(cont)) {
        ERROR("Failed to save container \"%s\" to disk", cont->common_config->id);
        ret = -1;
        goto out;
    }
out:
    container_unlock(cont);
    return ret;
}

static uint32_t stop_and_start(container_t *cont, int timeout)
{
    int ret = 0;
L
LiuHao 已提交
1360
    uint32_t cc = ISULAD_SUCCESS;
O
overweight 已提交
1361 1362 1363 1364 1365
    const char *console_fifos[3] = { NULL, NULL, NULL };
    const char *id = cont->common_config->id;

    ret = stop_container(cont, timeout, false, true);
    if (ret != 0) {
L
LiuHao 已提交
1366 1367
        cc = ISULAD_ERR_EXEC;
        container_state_set_error(cont->state, (const char *)g_isulad_errmsg);
O
overweight 已提交
1368 1369 1370 1371 1372 1373 1374
        goto out;
    }

    /* begin start container */
    state_set_starting(cont->state);
    if (container_to_disk_locking(cont)) {
        ERROR("Failed to save container \"%s\" to disk", id);
L
LiuHao 已提交
1375 1376
        cc = ISULAD_ERR_EXEC;
        isulad_set_error_message("Failed to save container \"%s\" to disk", id);
O
overweight 已提交
1377 1378 1379 1380 1381 1382 1383 1384
        goto out;
    }

    if (is_running(cont->state)) {
        INFO("Container is already running");
        goto out;
    }

D
dogsheng 已提交
1385
    if (start_container(cont, console_fifos, true) != 0) {
L
LiuHao 已提交
1386
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
        goto out;
    }
out:
    state_reset_starting(cont->state);
    return cc;
}

static void pack_restart_response(container_restart_response *response, uint32_t cc, const char *id)
{
    if (response == NULL) {
        return;
    }
    response->cc = cc;
L
LiuHao 已提交
1400 1401
    if (g_isulad_errmsg != NULL) {
        response->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
        DAEMON_CLEAR_ERRMSG();
    }
    if (id != NULL) {
        response->id = util_strdup_s(id);
    }
}

static uint32_t do_restart_container(container_t *cont, int timeout)
{
    int ret = 0;

    ret = restart_container(cont);
    if (ret == -1) {
L
LiuHao 已提交
1415 1416
        container_state_set_error(cont->state, (const char *)g_isulad_errmsg);
        return ISULAD_ERR_EXEC;
1417
    } else if (ret == RUNTIME_NOT_IMPLEMENT_RESET) {
O
overweight 已提交
1418 1419 1420 1421
        /* runtime don't implement restart, use stop and start */
        return stop_and_start(cont, timeout);
    }

L
LiuHao 已提交
1422
    return ISULAD_SUCCESS;
O
overweight 已提交
1423 1424 1425 1426 1427 1428
}

static int container_restart_cb(const container_restart_request *request,
                                container_restart_response **response)
{
    int timeout = 0;
L
LiuHao 已提交
1429
    uint32_t cc = ISULAD_SUCCESS;
O
overweight 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
    char *name = NULL;
    char *id = NULL;
    container_t *cont = NULL;

    DAEMON_CLEAR_ERRMSG();

    if (request == NULL || response == NULL) {
        ERROR("Invalid NULL input");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(container_restart_response));
    if (*response == NULL) {
        ERROR("Out of memory");
L
LiuHao 已提交
1444
        cc = ISULAD_ERR_MEMOUT;
O
overweight 已提交
1445 1446 1447 1448 1449 1450 1451
        goto pack_response;
    }

    name = request->id;
    timeout = request->timeout;

    if (!util_valid_container_id_or_name(name)) {
L
LiuHao 已提交
1452
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1453
        ERROR("Invalid container name %s", name);
L
LiuHao 已提交
1454
        isulad_set_error_message("Invalid container name %s", name);
O
overweight 已提交
1455 1456 1457 1458 1459
        goto pack_response;
    }

    cont = containers_store_get(name);
    if (cont == NULL) {
L
LiuHao 已提交
1460
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1461
        ERROR("No such container: %s", name);
L
LiuHao 已提交
1462
        isulad_set_error_message("No such container:%s", name);
O
overweight 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
        goto pack_response;
    }

    id = cont->common_config->id;

    set_log_prefix(id);

    EVENT("Event: {Object: %s, Type: restarting}", id);

    if (gc_is_gc_progress(id)) {
L
LiuHao 已提交
1473
        isulad_set_error_message("You cannot restart container %s in garbage collector progress.", id);
O
overweight 已提交
1474
        ERROR("You cannot restart container %s in garbage collector progress.", id);
L
LiuHao 已提交
1475
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1476 1477 1478 1479
        goto pack_response;
    }

    cc = do_restart_container(cont, timeout);
L
LiuHao 已提交
1480
    if (cc != ISULAD_SUCCESS) {
O
overweight 已提交
1481 1482 1483 1484
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: Restarted}", id);
W
wujing 已提交
1485 1486
    (void)isulad_monitor_send_container_event(id, RESTART, -1, 0, NULL, NULL);

O
overweight 已提交
1487 1488 1489 1490
pack_response:
    pack_restart_response(*response, cc, id);
    container_unref(cont);
    free_log_prefix();
L
LiuHao 已提交
1491
    return (cc == ISULAD_SUCCESS) ? 0 : -1;
O
overweight 已提交
1492 1493 1494 1495 1496 1497 1498 1499
}

static void pack_stop_response(container_stop_response *response, uint32_t cc, const char *id)
{
    if (response == NULL) {
        return;
    }
    response->cc = cc;
L
LiuHao 已提交
1500 1501
    if (g_isulad_errmsg != NULL) {
        response->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
        DAEMON_CLEAR_ERRMSG();
    }
    if (id != NULL) {
        response->id = util_strdup_s(id);
    }
}

static int container_stop_cb(const container_stop_request *request,
                             container_stop_response **response)
{
    int timeout = 0;
    bool force = false;
    char *name = NULL;
    char *id = NULL;
L
LiuHao 已提交
1516
    uint32_t cc = ISULAD_SUCCESS;
O
overweight 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    container_t *cont = NULL;

    DAEMON_CLEAR_ERRMSG();
    if (request == NULL || response == NULL) {
        ERROR("Invalid NULL input");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(container_stop_response));
    if (*response == NULL) {
        ERROR("Out of memory");
L
LiuHao 已提交
1528
        cc = ISULAD_ERR_MEMOUT;
O
overweight 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537
        goto pack_response;
    }

    name = request->id;
    force = request->force;
    timeout = request->timeout;

    if (name == NULL) {
        ERROR("Stop: receive NULL id");
L
LiuHao 已提交
1538
        cc = ISULAD_ERR_INPUT;
O
overweight 已提交
1539 1540 1541 1542 1543
        goto pack_response;
    }

    if (!util_valid_container_id_or_name(name)) {
        ERROR("Invalid container name %s", name);
L
LiuHao 已提交
1544 1545
        isulad_set_error_message("Invalid container name %s", name);
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1546 1547 1548 1549 1550 1551
        goto pack_response;
    }

    cont = containers_store_get(name);
    if (cont == NULL) {
        ERROR("No such container:%s", name);
L
LiuHao 已提交
1552 1553
        isulad_set_error_message("No such container:%s", name);
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562
        goto pack_response;
    }

    id = cont->common_config->id;
    set_log_prefix(id);

    EVENT("Event: {Object: %s, Type: Stopping}", id);

    if (gc_is_gc_progress(id)) {
L
LiuHao 已提交
1563
        isulad_set_error_message("You cannot stop container %s in garbage collector progress.", id);
O
overweight 已提交
1564
        ERROR("You cannot stop container %s in garbage collector progress.", id);
L
LiuHao 已提交
1565
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1566 1567 1568 1569
        goto pack_response;
    }

    if (stop_container(cont, timeout, force, false)) {
L
LiuHao 已提交
1570 1571
        cc = ISULAD_ERR_EXEC;
        container_state_set_error(cont->state, (const char *)g_isulad_errmsg);
O
overweight 已提交
1572 1573 1574 1575
        goto pack_response;
    }

    INFO("Stoped Container:%s", id);
W
wujing 已提交
1576
    (void)isulad_monitor_send_container_event(id, STOP, -1, 0, NULL, NULL);
O
overweight 已提交
1577 1578 1579 1580 1581

pack_response:
    pack_stop_response(*response, cc, id);
    container_unref(cont);
    free_log_prefix();
L
LiuHao 已提交
1582
    return (cc == ISULAD_SUCCESS) ? 0 : -1;
O
overweight 已提交
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
}

static int kill_container(container_t *cont, uint32_t signal)
{
    int ret = 0;
    char *id = NULL;

    id = cont->common_config->id;

    container_lock(cont);

    if (!is_running(cont->state)) {
        ERROR("Cannot kill container: Container %s is not running", id);
L
LiuHao 已提交
1596
        isulad_set_error_message("Cannot kill container: Container %s is not running", id);
O
overweight 已提交
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
        ret = -1;
        goto out;
    }

    if (signal == 0 || signal == SIGKILL) {
        ret = force_kill(cont);
    } else {
        ret = kill_with_signal(cont, signal);
    }

    if (ret != 0) {
        ret = -1;
        goto out;
    }

out:
    container_unlock(cont);
    return ret;
}

static void pack_kill_response(container_kill_response *response, uint32_t cc, const char *id)
{
    if (response == NULL) {
        return;
    }
    response->cc = cc;
L
LiuHao 已提交
1623 1624
    if (g_isulad_errmsg != NULL) {
        response->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
        DAEMON_CLEAR_ERRMSG();
    }
    if (id != NULL) {
        response->id = util_strdup_s(id);
    }
}

static int container_kill_cb(const container_kill_request *request,
                             container_kill_response **response)
{
    int ret = 0;
    char *name = NULL;
    char *id = NULL;
    uint32_t signal = 0;
L
LiuHao 已提交
1639
    uint32_t cc = ISULAD_SUCCESS;
O
overweight 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
    container_t *cont = NULL;

    DAEMON_CLEAR_ERRMSG();

    if (request == NULL || response == NULL) {
        ERROR("Invalid NULL input");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(container_kill_response));
    if (*response == NULL) {
        ERROR("Out of memory");
L
LiuHao 已提交
1652
        cc = ISULAD_ERR_MEMOUT;
O
overweight 已提交
1653 1654 1655 1656 1657 1658 1659 1660
        goto pack_response;
    }

    name = request->id;
    signal = request->signal;

    if (name == NULL) {
        ERROR("Kill: receive NULL id");
L
LiuHao 已提交
1661
        cc = ISULAD_ERR_INPUT;
O
overweight 已提交
1662 1663 1664 1665
        goto pack_response;
    }

    if (!util_valid_container_id_or_name(name)) {
L
LiuHao 已提交
1666
        isulad_set_error_message("Invalid container name %s", name);
O
overweight 已提交
1667
        ERROR("Invalid container name %s", name);
L
LiuHao 已提交
1668
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1669 1670 1671 1672
        goto pack_response;
    }

    if (!util_check_signal_valid((int)signal)) {
L
LiuHao 已提交
1673
        isulad_set_error_message("Not supported signal %d", signal);
O
overweight 已提交
1674
        ERROR("Not supported signal %d", signal);
L
LiuHao 已提交
1675
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1676 1677 1678 1679 1680
        goto pack_response;
    }

    cont = containers_store_get(name);
    if (cont == NULL) {
L
LiuHao 已提交
1681
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1682
        ERROR("No such container:%s", name);
L
LiuHao 已提交
1683
        isulad_set_error_message("No such container:%s", name);
O
overweight 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
        goto pack_response;
    }

    id = cont->common_config->id;
    set_log_prefix(id);

    EVENT("Event: {Object: %s, Type: Killing, Signal:%u}", id, signal);


    if (gc_is_gc_progress(id)) {
L
LiuHao 已提交
1694
        isulad_set_error_message("You cannot kill container %s in garbage collector progress.", id);
O
overweight 已提交
1695
        ERROR("You cannot kill container %s in garbage collector progress.", id);
L
LiuHao 已提交
1696
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1697 1698 1699 1700 1701
        goto pack_response;
    }

    ret = kill_container(cont, signal);
    if (ret != 0) {
L
LiuHao 已提交
1702 1703
        cc = ISULAD_ERR_EXEC;
        container_state_set_error(cont->state, (const char *)g_isulad_errmsg);
O
overweight 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: Killed, Signal:%u}", id, signal);

pack_response:
    pack_kill_response(*response, cc, id);
    container_unref(cont);
    free_log_prefix();
L
LiuHao 已提交
1713
    return (cc == ISULAD_SUCCESS) ? 0 : -1;
O
overweight 已提交
1714 1715 1716 1717 1718
}

static int do_runtime_rm_helper(const char *id, const char *runtime, const char *rootpath)
{
    int ret = 0;
D
dogsheng 已提交
1719 1720 1721 1722 1723 1724 1725 1726
    rt_rm_params_t params = { 0 };

    params.rootpath = rootpath;

    if (runtime_rm(id, runtime, &params)) {
        ERROR("Runtime remove container failed");
        ret = -1;
        goto out;
O
overweight 已提交
1727
    }
D
dogsheng 已提交
1728

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

static int do_cleanup_container_resources(container_t *cont)
{
    int ret = 0;
    char *id = NULL;
    char *name = NULL;
    char *statepath = NULL;
    char container_state[PATH_MAX] = { 0 };
    const char *runtime = NULL;
    const char *rootpath = NULL;
    container_t *cont_tmp = NULL;

    container_lock(cont);

    id = cont->common_config->id;
    name = cont->common_config->name;
    statepath = cont->state_path;
    runtime = cont->runtime;
    rootpath = cont->root_path;

    /* check if container was deregistered by previous rm already */
    cont_tmp = containers_store_get(id);
    if (cont_tmp == NULL) {
        ret = 0;
        goto out;
    }
    container_unref(cont_tmp);

    (void)container_to_disk(cont);

    if (gc_is_gc_progress(id)) {
L
LiuHao 已提交
1763
        isulad_set_error_message("You cannot remove container %s in garbage collector progress.", id);
O
overweight 已提交
1764 1765 1766 1767 1768
        ERROR("You cannot remove container %s in garbage collector progress.", id);
        ret = -1;
        goto out;
    }

O
openeuler-iSula 已提交
1769 1770
    ret = snprintf(container_state, sizeof(container_state), "%s/%s", statepath, id);
    if (ret < 0 || (size_t)ret >= sizeof(container_state)) {
O
overweight 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
        ERROR("Failed to sprintf container_state");
        ret = -1;
        goto out;
    }
    ret = util_recursive_rmdir(container_state, 0);
    if (ret != 0) {
        ERROR("Failed to delete container's state directory %s: %s", container_state, strerror(errno));
        ret = -1;
        goto out;
    }

    if (im_remove_container_rootfs(cont->common_config->image_type, id)) {
        ERROR("Failed to remove rootfs for container %s", id);
        ret = -1;
        goto out;
    }

L
LiuHao 已提交
1788
    umount_share_shm(cont);
L
LiuHao 已提交
1789

O
overweight 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
    umount_host_channel(cont->hostconfig->host_channel);

    if (do_runtime_rm_helper(id, runtime, rootpath) != 0) {
        ret = -1;
        goto out;
    }

    /* broadcast remove condition */
    container_wait_rm_cond_broadcast(cont);

    if (!containers_store_remove(id)) {
        ERROR("Failed to remove container '%s' from containers store", id);
        ret = -1;
        goto out;
    }

    if (!name_index_remove(name)) {
        ERROR("Failed to remove '%s' from name index", name);
        ret = -1;
    }

out:
    container_unlock(cont);
    return ret;
}

int set_container_to_removal(const container_t *cont)
{
    int ret = 0;
    char *id = NULL;

    if (cont == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    id = cont->common_config->id;

    bool removal_progress = state_set_removal_in_progress(cont->state);
    if (removal_progress) {
L
LiuHao 已提交
1831
        isulad_set_error_message("Container:%s was already in removal progress", id);
O
overweight 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
        ERROR("Container:%s was already in removal progress", id);
        ret = -1;
        goto out;
    }
out:
    return ret;
}

int cleanup_container(container_t *cont, bool force)
{
    int ret = 0;
    char *id = NULL;

    if (cont == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    id = cont->common_config->id;

    if (is_running(cont->state)) {
        if (!force) {
O
openeuler-iSula 已提交
1855
            if (is_paused(cont->state)) {
L
LiuHao 已提交
1856 1857 1858
                isulad_set_error_message("You cannot remove a paused container %s. "
                                         "Unpause and then stop the container before "
                                         "attempting removal or force remove", id);
O
openeuler-iSula 已提交
1859 1860 1861
                ERROR("You cannot remove a paused container %s. Unpause and then stop the container before "
                      "attempting removal or force remove", id);
            } else {
L
LiuHao 已提交
1862 1863
                isulad_set_error_message("You cannot remove a running container %s. "
                                         "Stop the container before attempting removal or use -f", id);
O
openeuler-iSula 已提交
1864 1865 1866
                ERROR("You cannot remove a running container %s."
                      " Stop the container before attempting removal or use -f", id);
            }
O
overweight 已提交
1867 1868 1869 1870 1871
            ret = -1;
            goto reset_removal_progress;
        }
        ret = stop_container(cont, 3, force, false);
        if (ret != 0) {
L
LiuHao 已提交
1872
            isulad_append_error_message("Could not stop running container %s, cannot remove. ", id);
O
overweight 已提交
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
            ERROR("Could not stop running container %s, cannot remove", id);
            ret = -1;
            goto reset_removal_progress;
        }
    }

    plugin_event_container_post_remove(cont);

    ret = do_cleanup_container_resources(cont);
    if (ret != 0) {
        goto reset_removal_progress;
    }

    goto out;

reset_removal_progress:
    state_reset_removal_in_progress(cont->state);
out:
    return ret;
}

static void pack_delete_response(container_delete_response *response, uint32_t cc, const char *id)
{
    if (response == NULL) {
        return;
    }
    response->cc = cc;
L
LiuHao 已提交
1900 1901
    if (g_isulad_errmsg != NULL) {
        response->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
        DAEMON_CLEAR_ERRMSG();
    }
    if (id != NULL) {
        response->id = util_strdup_s(id);
    }
}

static int container_delete_cb(const container_delete_request *request,
                               container_delete_response **response)
{
    bool force = false;
L
LiuHao 已提交
1913
    uint32_t cc = ISULAD_SUCCESS;
O
overweight 已提交
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
    char *name = NULL;
    char *id = NULL;
    container_t *cont = NULL;

    DAEMON_CLEAR_ERRMSG();
    if (request == NULL || response == NULL) {
        ERROR("Invalid NULL input");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(container_delete_response));
    if (*response == NULL) {
        ERROR("Out of memory");
L
LiuHao 已提交
1927
        cc = ISULAD_ERR_MEMOUT;
O
overweight 已提交
1928 1929 1930 1931 1932 1933 1934 1935
        goto pack_response;
    }

    name = request->id;
    force = request->force;

    if (!util_valid_container_id_or_name(name)) {
        ERROR("Invalid container name %s", name);
L
LiuHao 已提交
1936 1937
        isulad_set_error_message("Invalid container name %s", name);
        cc = ISULAD_ERR_INPUT;
O
overweight 已提交
1938 1939 1940 1941 1942 1943
        goto pack_response;
    }

    cont = containers_store_get(name);
    if (cont == NULL) {
        ERROR("No such container:%s", name);
L
LiuHao 已提交
1944 1945
        isulad_set_error_message("No such container:%s", name);
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
        goto pack_response;
    }

    id = cont->common_config->id;
    set_log_prefix(id);

    EVENT("Event: {Object: %s, Type: Deleting}", id);

    container_lock(cont);
    int nret = set_container_to_removal(cont);
    container_unlock(cont);
    if (nret != 0) {
        ERROR("Failed to set container %s state to removal", id);
L
LiuHao 已提交
1959
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1960 1961 1962 1963
        goto pack_response;
    }

    if (cleanup_container(cont, force)) {
L
LiuHao 已提交
1964
        cc = ISULAD_ERR_EXEC;
O
overweight 已提交
1965 1966 1967 1968 1969 1970 1971 1972 1973
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: Deleted}", id);

pack_response:
    pack_delete_response(*response, cc, id);
    container_unref(cont);
    free_log_prefix();
L
LiuHao 已提交
1974
    return (cc == ISULAD_SUCCESS) ? 0 : -1;
O
overweight 已提交
1975 1976 1977 1978 1979
}

void container_callback_init(service_container_callback_t *cb)
{
    cb->get_id = container_get_id_cb;
G
g00501519 已提交
1980
    cb->get_runtime = container_get_runtime_cb;
O
overweight 已提交
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
    cb->create = container_create_cb;
    cb->start = container_start_cb;
    cb->stop = container_stop_cb;
    cb->restart = container_restart_cb;
    cb->kill = container_kill_cb;
    cb->remove = container_delete_cb;

    container_information_callback_init(cb);
    container_stream_callback_init(cb);
    container_extend_callback_init(cb);
}