args.c 5.3 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
H
haozi007 已提交
3 4 5 6
 * clibcni licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
H
haozi007 已提交
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
 * Author: tanyifeng
 * Create: 2019-04-25
 * Description: provide args functions
 *********************************************************************************/
#define _GNU_SOURCE
#define __USE_GNU
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

#include "utils.h"
#include "args.h"
#include "log.h"

void free_cni_args(struct cni_args *cargs)
{
    size_t i = 0;

    if (cargs == NULL) {
        return;
    }

    free(cargs->command);
    cargs->command = NULL;
    free(cargs->container_id);
    cargs->container_id = NULL;
    free(cargs->netns);
    cargs->netns = NULL;
    free(cargs->plugin_args_str);
    cargs->plugin_args_str = NULL;
    free(cargs->ifname);
    cargs->ifname = NULL;
    free(cargs->path);
    cargs->path = NULL;
    for (i = 0; i < cargs->plugin_args_len; i++) {
        free(cargs->plugin_args[i][0]);
        cargs->plugin_args[i][0] = NULL;
        free(cargs->plugin_args[i][1]);
        cargs->plugin_args[i][1] = NULL;
    }
    free(cargs->plugin_args);
    cargs->plugin_args = NULL;
    free(cargs);
}

static char *env_stringify(char *(*pargs)[2], size_t len)
{
    char **entries = NULL;
    const char **work = NULL;
    char *result = NULL;
    size_t i = 0;
    bool invalid_arg = (pargs == NULL || len == 0);

    if (invalid_arg) {
        ERROR("Invalid arguments");
        return NULL;
    }

    if (len > (INT_MAX / sizeof(char *)) - 1) {
        ERROR("Too large arguments");
        return NULL;
    }
    entries = util_common_calloc_s(sizeof(char *) * (len + 1));
    if (entries == NULL) {
        ERROR("Out of memory");
        return NULL;
    }
    for (i = 0; i < len; i++) {
        work = (const char **)pargs[i];
        entries[i] = cni_util_string_join("=", work, 2);
        if (entries[i] == NULL) {
            ERROR("Join args failed");
            goto free_out;
        }
    }

    result = cni_util_string_join(";", (const char **)entries, len);
free_out:
    util_free_array(entries);
    return result;
}

static int add_cni_envs(const struct cni_args *cniargs, size_t *pos, char **result)
{
    char *plugin_args_str = NULL;
    char *buffer = NULL;
    size_t i = *pos;
    int nret = 0;
    int ret = -1;

    plugin_args_str = cniargs->plugin_args_str ? util_strdup_s(cniargs->plugin_args_str) : NULL;
    if (is_null_or_empty(plugin_args_str)) {
        free(plugin_args_str);
        plugin_args_str = env_stringify(cniargs->plugin_args, cniargs->plugin_args_len);
    }

    nret = asprintf(&buffer, "%s=%s", ENV_CNI_COMMAND, cniargs->command);
    if (nret < 0) {
        ERROR("Sprintf failed");
        goto free_out;
    }
    result[i++] = buffer;
    buffer = NULL;
    nret = asprintf(&buffer, "%s=%s", ENV_CNI_CONTAINERID, cniargs->container_id);
    if (nret < 0) {
        ERROR("Sprintf failed");
        goto free_out;
    }
    result[i++] = buffer;
    buffer = NULL;
    nret = asprintf(&buffer, "%s=%s", ENV_CNI_NETNS, cniargs->netns);
    if (nret < 0) {
        ERROR("Sprintf failed");
        goto free_out;
    }
    result[i++] = buffer;
    buffer = NULL;
    nret = asprintf(&buffer, "%s=%s", ENV_CNI_ARGS, plugin_args_str);
    if (nret < 0) {
        ERROR("Sprintf failed");
        goto free_out;
    }
    result[i++] = buffer;
    buffer = NULL;
    nret = asprintf(&buffer, "%s=%s", ENV_CNI_IFNAME, cniargs->ifname);
    if (nret < 0) {
        ERROR("Sprintf failed");
        goto free_out;
    }
    result[i++] = buffer;
    buffer = NULL;
    nret = asprintf(&buffer, "%s=%s", ENV_CNI_PATH, cniargs->path);
    if (nret < 0) {
        ERROR("Sprintf failed");
        goto free_out;
    }
    result[i++] = buffer;

    ret = 0;
free_out:
    free(plugin_args_str);
    *pos = i;
    return ret;
}

char **as_env(const struct cni_args *cniargs)
{
    char **result = NULL;
    char **pos = NULL;
    size_t len = 0;
    size_t i = 0;
    size_t j = 0;
    char **envir = environ;

    if (cniargs == NULL) {
        ERROR("Invlaid cni args");
        return NULL;
    }

    len = util_array_len((const char * const *)envir);

    if (len > ((SIZE_MAX / sizeof(char *)) - (CNI_ENVS_LEN + 1))) {
        ERROR("Too large arguments");
        return NULL;
    }

    len += (CNI_ENVS_LEN + 1);
    result = util_common_calloc_s(len * sizeof(char *));
    if (result == NULL) {
        ERROR("Out of memory");
        return NULL;
    }

    if (add_cni_envs(cniargs, &i, result) != 0) {
        goto free_out;
    }

    /* inherit environs of parent */
    for (pos = envir; pos != NULL && *pos != NULL && i < len; pos++) {
        result[i] = util_strdup_s(*pos);
        i++;
    }

    return result;
free_out:
    for (j = 0; j < i; j++) {
        free(result[j]);
    }
    free(result);
    return NULL;
}