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
 * 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;
    }
H
haozi007 已提交
74
    entries = clibcni_util_common_calloc_s(sizeof(char *) * (len + 1));
O
overweight 已提交
75 76 77 78 79 80
    if (entries == NULL) {
        ERROR("Out of memory");
        return NULL;
    }
    for (i = 0; i < len; i++) {
        work = (const char **)pargs[i];
H
haozi007 已提交
81
        entries[i] = clibcni_util_string_join("=", work, 2);
O
overweight 已提交
82 83 84 85 86 87
        if (entries[i] == NULL) {
            ERROR("Join args failed");
            goto free_out;
        }
    }

H
haozi007 已提交
88
    result = clibcni_util_string_join(";", (const char **)entries, len);
O
overweight 已提交
89
free_out:
H
haozi007 已提交
90
    clibcni_util_free_array(entries);
O
overweight 已提交
91 92 93 94 95 96 97 98 99 100 101
    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;

H
haozi007 已提交
102 103
    plugin_args_str = cniargs->plugin_args_str ? clibcni_util_strdup_s(cniargs->plugin_args_str) : NULL;
    if (clibcni_is_null_or_empty(plugin_args_str)) {
O
overweight 已提交
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
        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;
    }

H
haozi007 已提交
171
    len = clibcni_util_array_len((const char * const *)envir);
O
overweight 已提交
172 173 174 175 176 177 178

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

    len += (CNI_ENVS_LEN + 1);
H
haozi007 已提交
179
    result = clibcni_util_common_calloc_s(len * sizeof(char *));
O
overweight 已提交
180 181 182 183 184 185 186 187 188 189 190
    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++) {
H
haozi007 已提交
191
        result[i] = clibcni_util_strdup_s(*pos);
O
overweight 已提交
192 193 194 195 196 197 198 199 200 201 202
        i++;
    }

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