lcr_rt_ops.c 21.1 KB
Newer Older
D
dogsheng 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
D
dogsheng 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
D
dogsheng 已提交
11 12 13 14 15 16 17 18 19 20
 * Author: lifeng
 * Create: 2019-11-22
 * Description: provide container list callback function definition
 ********************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <limits.h>

#include "lcr_rt_ops.h"
H
haozi007 已提交
21
#include "isula_libutils/log.h"
D
dogsheng 已提交
22 23
#include "engine.h"
#include "error.h"
L
LiuHao 已提交
24
#include "isulad_config.h"
25
#include "libisulad.h"
26
#include "runtime_api.h"
D
dogsheng 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

bool rt_lcr_detect(const char *runtime)
{
    /* now we just support lcr engine */
    if (runtime != NULL && strcasecmp(runtime, "lcr") == 0) {
        return true;
    }

    return false;
}

int rt_lcr_create(const char *name, const char *runtime, const rt_create_params_t *params)
{
    int ret = 0;
    char *runtime_root = NULL;
    struct engine_operation *engine_ops = NULL;

    runtime_root = conf_get_routine_rootdir(runtime);
    if (runtime_root == NULL) {
        ERROR("Root path is NULL");
        ret = -1;
        goto out;
    }

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_create_op == NULL) {
        ERROR("Failed to get engine create operations");
        ret = -1;
        goto out;
    }

58
    if (!engine_ops->engine_create_op(name, runtime_root, params->oci_config_data)) {
D
dogsheng 已提交
59 60
        ERROR("Failed to create container");
        const char *tmpmsg = NULL;
L
LiFeng 已提交
61 62 63
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
64
        isulad_set_error_message("Create container error: %s",
65
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
D
dogsheng 已提交
66 67 68 69 70
        ret = -1;
        goto out;
    }

out:
L
LiFeng 已提交
71
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
72 73
        engine_ops->engine_clear_errmsg_op();
    }
D
dogsheng 已提交
74 75 76 77
    free(runtime_root);
    return ret;
}

O
openeuler-iSula 已提交
78
static int parse_container_pid(const char *S, container_pid_t *pid_info)
D
dogsheng 已提交
79 80 81
{
    int num;

O
openeuler-iSula 已提交
82
    num = sscanf(S, "%d %Lu %d %Lu", &pid_info->pid, &pid_info->start_time, &pid_info->ppid, &pid_info->pstart_time);
D
dogsheng 已提交
83 84 85 86 87 88 89 90
    if (num != 4) { // args num to read is 4
        ERROR("Call sscanf error: %s", errno ? strerror(errno) : "");
        return -1;
    }

    return 0;
}

O
openeuler-iSula 已提交
91
static int lcr_rt_read_pidfile(const char *pidfile, container_pid_t *pid_info)
D
dogsheng 已提交
92 93 94 95 96 97
{
    if (pidfile == NULL || pid_info == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

98
    char sbuf[1024] = { 0 }; /* bufs for stat */
D
dogsheng 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    if ((util_file2str(pidfile, sbuf, sizeof(sbuf))) == -1) {
        return -1;
    }

    return parse_container_pid(sbuf, pid_info);
}

int rt_lcr_start(const char *name, const char *runtime, const rt_start_params_t *params, container_pid_t *pid_info)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;
    engine_start_request_t request = { 0 };

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_start_op == NULL) {
        ERROR("Failed to get engine start operations");
        ret = -1;
        goto out;
    }

    request.name = name;
    request.lcrpath = params->rootpath;
    request.logpath = params->logpath;
    request.loglevel = params->loglevel;
    request.daemonize = true;
    request.tty = params->tty;
    request.open_stdin = params->open_stdin;
    request.console_fifos = params->console_fifos;
    request.start_timeout = params->start_timeout;
    request.container_pidfile = params->container_pidfile;
    request.exit_fifo = params->exit_fifo;

    if (!engine_ops->engine_start_op(&request)) {
        const char *tmpmsg = NULL;
L
LiFeng 已提交
134 135 136
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
137
        isulad_set_error_message("Start container error: %s",
138 139
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
        ERROR("Start container error: %s", (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
D
dogsheng 已提交
140 141 142 143 144 145 146 147 148 149
        ret = -1;
        goto out;
    }
    ret = lcr_rt_read_pidfile(params->container_pidfile, pid_info);
    if (ret != 0) {
        ERROR("Failed to get started container's pid info, start container fail");
        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
150
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
151 152
        engine_ops->engine_clear_errmsg_op();
    }
D
dogsheng 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    return ret;
}

int rt_lcr_restart(const char *name, const char *runtime, const rt_restart_params_t *params)
{
    return RUNTIME_NOT_IMPLEMENT_RESET;
}

int rt_lcr_clean_resource(const char *name, const char *runtime, const rt_clean_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_clean_op == NULL) {
        ERROR("Failed to get engine clean operations");
        ret = -1;
        goto out;
    }

    if (!engine_ops->engine_clean_op(name, params->rootpath, params->logpath, params->loglevel, params->pid)) {
        const char *tmpmsg = NULL;
L
LiFeng 已提交
175 176 177
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
178
        isulad_try_set_error_message("Clean resource container error;%s",
179
                                     (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
D
dogsheng 已提交
180 181 182 183 184
        ret = -1;
        goto out;
    }

out:
L
LiFeng 已提交
185
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
D
dogsheng 已提交
186 187 188 189 190 191 192 193 194 195
        engine_ops->engine_clear_errmsg_op();
    }
    return ret;
}

static int remove_container_rootpath(const char *id, const char *root_path)
{
    int ret = 0;
    char cont_root_path[PATH_MAX] = { 0 };

O
openeuler-iSula 已提交
196 197
    ret = snprintf(cont_root_path, sizeof(cont_root_path), "%s/%s", root_path, id);
    if (ret < 0 || (size_t)ret >= sizeof(cont_root_path)) {
D
dogsheng 已提交
198 199 200 201 202 203 204 205
        ERROR("Failed to sprintf container_state");
        ret = -1;
        goto out;
    }
    ret = util_recursive_rmdir(cont_root_path, 0);
    if (ret != 0) {
        const char *tmp_err = (errno != 0) ? strerror(errno) : "error";
        ERROR("Failed to delete container's root directory %s: %s", cont_root_path, tmp_err);
L
LiuHao 已提交
206
        isulad_set_error_message("Failed to delete container's root directory %s: %s", cont_root_path, tmp_err);
D
dogsheng 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        ret = -1;
        goto out;
    }

out:
    return ret;
}

int rt_lcr_rm(const char *name, const char *runtime, const rt_rm_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_delete_op == NULL) {
        ERROR("Failed to get engine delete operations");
        ret = -1;
        goto out;
    }

    if (!engine_ops->engine_delete_op(name, params->rootpath)) {
L
LiFeng 已提交
228
        const char *tmpmsg = NULL;
O
openeuler-iSula 已提交
229
        ret = -1;
L
LiFeng 已提交
230 231 232
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
233 234
        isulad_set_error_message("Runtime delete container error: %s",
                                 (tmpmsg != NULL && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
O
openeuler-iSula 已提交
235 236
        ERROR("Runtime delete container error: %s",
              (tmpmsg != NULL && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
D
dogsheng 已提交
237 238 239 240
        if (tmpmsg != NULL && strstr(tmpmsg, "No such container") != NULL) {
            // container root path may been corrupted, try to remove by daemon
            WARN("container %s root path may been corrupted, try to remove by daemon", name);
            if (remove_container_rootpath(name, params->rootpath) == 0) {
O
openeuler-iSula 已提交
241
                ret = 0;
D
dogsheng 已提交
242 243 244 245 246 247 248
                goto out;
            }
        }
        goto out;
    }

out:
L
LiFeng 已提交
249
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
D
dogsheng 已提交
250 251 252 253 254
        engine_ops->engine_clear_errmsg_op();
    }
    return ret;
}

L
LiFeng 已提交
255
int rt_lcr_status(const char *name, const char *runtime, const rt_status_params_t *params,
256
                  struct runtime_container_status_info *status)
D
dogsheng 已提交
257 258
{
    int ret = 0;
L
LiFeng 已提交
259
    int nret = 0;
D
dogsheng 已提交
260 261 262
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
L
LiFeng 已提交
263 264
    if (engine_ops == NULL || engine_ops->engine_get_container_status_op == NULL) {
        ERROR("Failed to get engine status operations");
D
dogsheng 已提交
265 266 267 268
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
269 270
    nret = engine_ops->engine_get_container_status_op(name, params->rootpath, status);
    if (nret != 0) {
D
dogsheng 已提交
271 272 273 274 275
        ret = -1;
        goto out;
    }

out:
L
LiFeng 已提交
276
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
D
dogsheng 已提交
277 278 279 280 281
        engine_ops->engine_clear_errmsg_op();
    }
    return ret;
}

L
LiFeng 已提交
282
int rt_lcr_resources_stats(const char *name, const char *runtime, const rt_stats_params_t *params,
283
                           struct runtime_container_resources_stats_info *rs_stats)
D
dogsheng 已提交
284 285 286 287 288 289
{
    int ret = 0;
    int nret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
L
LiFeng 已提交
290 291
    if (engine_ops == NULL || engine_ops->engine_get_container_resources_stats_op == NULL) {
        ERROR("Failed to get engine stats operations");
D
dogsheng 已提交
292 293 294 295
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
296
    nret = engine_ops->engine_get_container_resources_stats_op(name, params->rootpath, rs_stats);
D
dogsheng 已提交
297 298 299 300 301 302
    if (nret != 0) {
        ret = -1;
        goto out;
    }

out:
L
LiFeng 已提交
303
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
304 305
        engine_ops->engine_clear_errmsg_op();
    }
D
dogsheng 已提交
306 307
    return ret;
}
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

// user string(UID:GID)
static int generate_user_string_by_uid_gid(const defs_process_user *puser, char **user)
{
    char uid_str[ISULAD_NUMSTRLEN32] = { 0 };
    char gid_str[ISULAD_NUMSTRLEN32] = { 0 };
    size_t len;
    int nret = 0;

    nret = snprintf(uid_str, ISULAD_NUMSTRLEN32, "%u", (unsigned int)puser->uid);
    if (nret < 0 || nret >= ISULAD_NUMSTRLEN32) {
        ERROR("Invalid UID:%u", (unsigned int)puser->uid);
        return -1;
    }

    nret = snprintf(gid_str, ISULAD_NUMSTRLEN32, "%u", (unsigned int)puser->gid);
    if (nret < 0 || nret >= ISULAD_NUMSTRLEN32) {
        ERROR("Invalid attach uid value :%u", (unsigned int)puser->gid);
        return -1;
    }

    len = strlen(uid_str) + 1 + strlen(gid_str) + 1;
    *user = (char *)util_common_calloc_s(len * sizeof(char));
    if (*user == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    nret = snprintf(*user, len, "%u:%u", (unsigned int)puser->uid, (unsigned int)puser->gid);
    if (nret < 0 || (size_t)nret >= len) {
        ERROR("Invalid UID:GID (%u:%u)", (unsigned int)puser->uid, (unsigned int)puser->gid);
        free(*user);
        *user = NULL;
        return -1;
    }

    return 0;
}

D
dogsheng 已提交
347 348 349 350 351
int rt_lcr_exec(const char *id, const char *runtime, const rt_exec_params_t *params, int *exit_code)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;
    engine_exec_request_t request = { 0 };
352
    char *user = NULL;
D
dogsheng 已提交
353 354 355 356 357 358 359 360 361 362 363 364

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_exec_op == NULL) {
        DEBUG("Failed to get engine exec operations");
        ret = -1;
        goto out;
    }

    request.name = id;
    request.lcrpath = params->rootpath;
    request.logpath = params->logpath;
    request.loglevel = params->loglevel;
L
LiFeng 已提交
365 366 367 368 369 370
    if (params->spec != NULL) {
        request.args = (const char **)params->spec->args;
        request.args_len = params->spec->args_len;
        request.env = (const char **)params->spec->env;
        request.env_len = params->spec->env_len;
    }
D
dogsheng 已提交
371 372
    request.console_fifos = params->console_fifos;
    request.timeout = params->timeout;
L
LiFeng 已提交
373
    request.suffix = params->suffix;
L
LiFeng 已提交
374
    if (params->spec != NULL && params->spec->user != NULL) {
375 376 377 378 379 380
        if (generate_user_string_by_uid_gid(params->spec->user, &user) != 0) {
            ret = -1;
            goto out;
        }
        request.user = user;
    }
D
dogsheng 已提交
381

382 383 384 385 386
    request.open_stdin = params->attach_stdin;
    if (params->spec != NULL) {
        request.tty = params->spec->terminal;
    }

D
dogsheng 已提交
387 388
    if (!engine_ops->engine_exec_op(&request, exit_code)) {
        const char *tmpmsg = NULL;
L
LiFeng 已提交
389 390 391
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
392 393
        isulad_set_error_message("Exec container error;%s",
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
L
LiuHao 已提交
394
        util_contain_errmsg(g_isulad_errmsg, exit_code);
D
dogsheng 已提交
395 396 397 398 399
        ret = -1;
        goto out;
    }

out:
L
LiFeng 已提交
400
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
401 402
        engine_ops->engine_clear_errmsg_op();
    }
403
    free(user);
D
dogsheng 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    return ret;
}

int rt_lcr_pause(const char *name, const char *runtime, const rt_pause_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_pause_op == NULL) {
        DEBUG("Failed to get engine pause operations");
        ret = -1;
        goto out;
    }

    if (!engine_ops->engine_pause_op(name, params->rootpath)) {
        DEBUG("Pause container %s failed", name);
        const char *tmpmsg = NULL;
L
LiFeng 已提交
422 423 424
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
425 426
        isulad_set_error_message("Pause container error;%s",
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
D
dogsheng 已提交
427 428 429 430
        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
431
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
432 433
        engine_ops->engine_clear_errmsg_op();
    }
D
dogsheng 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    return ret;
}

int rt_lcr_resume(const char *name, const char *runtime, const rt_resume_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_resume_op == NULL) {
        DEBUG("Failed to get engine resume operations");
        ret = -1;
        goto out;
    }

    if (!engine_ops->engine_resume_op(name, params->rootpath)) {
        DEBUG("Resume container %s failed", name);
        const char *tmpmsg = NULL;
L
LiFeng 已提交
452 453 454
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
455
        isulad_set_error_message("Resume container error;%s",
456
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
L
LiFeng 已提交
457 458 459 460
        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
461
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        engine_ops->engine_clear_errmsg_op();
    }
    return ret;
}

int rt_lcr_attach(const char *name, const char *runtime, const rt_attach_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_console_op == NULL) {
        DEBUG("Failed to get engine attach operations");
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
478

L
LiFeng 已提交
479 480 481 482
    if (!engine_ops->engine_console_op(name, params->rootpath, (char *)params->stdin, (char *)params->stdout,
                                       (char *)params->stderr)) {
        ERROR("attach failed");
        const char *tmpmsg = NULL;
L
LiFeng 已提交
483 484 485
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
486 487
        isulad_set_error_message("Attach container error;%s",
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
L
LiFeng 已提交
488 489 490 491
        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
492
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
D
dogsheng 已提交
493
        engine_ops->engine_clear_errmsg_op();
L
LiFeng 已提交
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
    }
    return ret;
}

static void to_engine_resources(const host_config *hostconfig, struct engine_cgroup_resources *cr)
{
    if (hostconfig == NULL || cr == NULL) {
        return;
    }

    cr->blkio_weight = hostconfig->blkio_weight;
    cr->cpu_shares = (uint64_t)hostconfig->cpu_shares;
    cr->cpu_period = (uint64_t)hostconfig->cpu_period;
    cr->cpu_quota = (uint64_t)hostconfig->cpu_quota;
    cr->cpuset_cpus = hostconfig->cpuset_cpus;
    cr->cpuset_mems = hostconfig->cpuset_mems;
    cr->memory_limit = (uint64_t)hostconfig->memory;
    cr->memory_swap = (uint64_t)hostconfig->memory_swap;
    cr->memory_reservation = (uint64_t)hostconfig->memory_reservation;
    cr->kernel_memory_limit = (uint64_t)hostconfig->kernel_memory;
}

int rt_lcr_update(const char *id, const char *runtime, const rt_update_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;
    struct engine_cgroup_resources cr = { 0 };

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_update_op == NULL) {
        DEBUG("Failed to get engine update operations");
        ret = -1;
        goto out;
    }

    to_engine_resources(params->hostconfig, &cr);

    if (!engine_ops->engine_update_op(id, params->rootpath, &cr)) {
        DEBUG("Update container %s failed", id);
        const char *tmpmsg = NULL;
L
LiFeng 已提交
534 535 536
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
537 538
        isulad_set_error_message("Cannot update container %s: %s", id,
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
D
dogsheng 已提交
539 540 541 542
        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
543
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
544 545 546 547 548 549 550 551 552 553
        engine_ops->engine_clear_errmsg_op();
    }
    return ret;
}

int rt_lcr_listpids(const char *name, const char *runtime, const rt_listpids_params_t *params, rt_listpids_out_t *out)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

L
LiFeng 已提交
554 555 556 557 558 559
    if (out == NULL) {
        ERROR("Invalid arguments");
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
560 561
    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_get_container_pids_op == NULL) {
L
LiFeng 已提交
562
        ERROR("Failed to get engine top operations");
L
LiFeng 已提交
563 564 565 566 567
        ret = -1;
        goto out;
    }

    if (!engine_ops->engine_get_container_pids_op(name, params->rootpath, &(out->pids), &(out->pids_len))) {
L
LiFeng 已提交
568
        ERROR("Top container %s failed", name);
L
LiFeng 已提交
569
        const char *tmpmsg = NULL;
L
LiFeng 已提交
570 571 572
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
573 574
        isulad_set_error_message("Runtime top container error: %s",
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
L
LiFeng 已提交
575 576 577 578
        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
579
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
        engine_ops->engine_clear_errmsg_op();
    }
    return ret;
}

int rt_lcr_resize(const char *id, const char *runtime, const rt_resize_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_resize_op == NULL) {
        DEBUG("Failed to get engine resume operations");
        ret = -1;
        goto out;
    }

    if (!engine_ops->engine_resize_op(id, params->rootpath, params->height, params->width)) {
        DEBUG("resize container %s failed", id);
        const char *tmpmsg = NULL;
L
LiFeng 已提交
600 601 602
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
603
        isulad_set_error_message("Resize container error;%s",
604
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
L
LiFeng 已提交
605 606 607 608 609

        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
610
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
        engine_ops->engine_clear_errmsg_op();
    }
    return ret;
}

int rt_lcr_exec_resize(const char *id, const char *runtime, const rt_exec_resize_params_t *params)
{
    int ret = 0;
    struct engine_operation *engine_ops = NULL;

    engine_ops = engines_get_handler(runtime);
    if (engine_ops == NULL || engine_ops->engine_resize_op == NULL) {
        DEBUG("Failed to get engine resume operations");
        ret = -1;
        goto out;
    }

    if (!engine_ops->engine_exec_resize_op(id, params->rootpath, params->suffix, params->height, params->width)) {
        DEBUG("exec resize container %s failed", id);
        const char *tmpmsg = NULL;
L
LiFeng 已提交
631 632 633
        if (engine_ops->engine_get_errmsg_op != NULL) {
            tmpmsg = engine_ops->engine_get_errmsg_op();
        }
L
LiuHao 已提交
634
        isulad_set_error_message("Resize container error;%s",
635
                                 (tmpmsg && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR);
L
LiFeng 已提交
636 637 638 639
        ret = -1;
        goto out;
    }
out:
L
LiFeng 已提交
640
    if (engine_ops != NULL && engine_ops->engine_clear_errmsg_op != NULL) {
L
LiFeng 已提交
641 642
        engine_ops->engine_clear_errmsg_op();
    }
D
dogsheng 已提交
643 644
    return ret;
}