ueventd_read_cfg.c 11.8 KB
Newer Older
S
sun_fan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ueventd_read_cfg.h"
S
sun_fan 已提交
17

S
sun_fan 已提交
18 19 20
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
S
sun_fan 已提交
21 22
#include <limits.h>
#include <string.h>
S
sun_fan 已提交
23
#include <sys/stat.h>
S
sun_fan 已提交
24
#include <unistd.h>
S
sun_fan 已提交
25
#include "init_utils.h"
S
sun_fan 已提交
26 27
#include "list.h"
#include "securec.h"
S
sun_fan 已提交
28
#include "ueventd_utils.h"
S
sun_fan 已提交
29 30 31 32 33
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"

// default item count in config files
#define DEFAULTITEMCOUNT (100)
S
sun_fan 已提交
34 35 36 37 38 39 40 41 42 43 44 45

#define SYS_CONFIG_PATH_NUM 0
#define SYS_CONFIG_ATTR_NUM 1
#define SYS_CONFIG_MODE_NUM 2
#define SYS_CONFIG_UID_NUM 3
#define SYS_CONFIG_GID_NUM 4

#define DEVICE_CONFIG_NAME_NUM 0
#define DEVICE_CONFIG_MODE_NUM 1
#define DEVICE_CONFIG_UID_NUM 2
#define DEVICE_CONFIG_GID_NUM 3

S
sun_fan 已提交
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
typedef enum SECTION {
    SECTION_INVALID = -1,
    SECTION_DEVICE = 0,
    SECTION_SYSFS,
    SECTION_FIRMWARE
} SECTION;

typedef int (*ParseConfigFunc)(char *);
typedef struct FunctionMapper {
    char *name;
    ParseConfigFunc func;
} FUNCTIONMAPPER;

struct ListNode g_devices = {
    .next = &g_devices,
    .prev = &g_devices,
};

struct ListNode g_sysDevices = {
    .next = &g_sysDevices,
    .prev = &g_sysDevices,
};

struct ListNode g_firmwares = {
    .next = &g_firmwares,
    .prev = &g_firmwares,
};

static void FreeConfigItems(char **items, int count)
{
    if (items != NULL) {
        for (int i = 0; i < count; i++) {
            if (items[i] != NULL) {
                free(items[i]);
            }
        }
        free(items);
        items = NULL;
    }
}

static char **SplitUeventConfig(char *buffer, const char *del, int *returnCount, int maxItemCount)
{
    char *rest = NULL;
    int count = 0;
S
sun_fan 已提交
91
    const int average = 2;
S
sun_fan 已提交
92
    char *p = strtok_r(buffer, del, &rest);
S
sun_fan 已提交
93 94
    int maxItemCountTmp = maxItemCount;
    if (maxItemCountTmp < 0) {
S
sun_fan 已提交
95 96
        return NULL;
    }
S
sun_fan 已提交
97 98
    if (maxItemCountTmp > DEFAULTITEMCOUNT) {
        maxItemCountTmp = DEFAULTITEMCOUNT;
S
sun_fan 已提交
99
    }
S
sun_fan 已提交
100
    char **items = (char **)malloc(sizeof(char*) * maxItemCountTmp);
S
sun_fan 已提交
101 102 103 104 105
    if (items == NULL) {
        INIT_LOGE("No enough memory to store uevent config");
        return NULL;
    }
    while (p != NULL) {
S
sun_fan 已提交
106 107
        if (count > (maxItemCountTmp - 1)) {
            maxItemCountTmp += (maxItemCountTmp / average) + 1;
S
sun_fan 已提交
108
            INIT_LOGD("Too many items,expand size");
S
sun_fan 已提交
109
            char **expand = (char **)(realloc(items, sizeof(char *) * maxItemCountTmp));
S
sun_fan 已提交
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
            if (expand == NULL) {
                INIT_LOGE("Failed to expand memory for uevent config parser");
                FreeConfigItems(items, count);
                return NULL;
            }
            items = expand;
        }
        size_t len = strlen(p);
        items[count] = (char *)malloc(len + 1);
        if (items[count] == NULL) {
            FreeConfigItems(items, count);
            return NULL;
        }
        if (strncpy_s(items[count], len + 1, p, len) != EOK) {
            INIT_LOGE("Copy string failed");
            FreeConfigItems(items, count);
            return NULL;
        }
        items[count][strlen(p)] = '\0';
        count++;
        p = strtok_r(NULL, del, &rest);
    }
    items[count] = NULL;
    *returnCount = count;
    return items;
}

static int ParseDeviceConfig(char *p)
{
    INIT_LOGD("Parse device config info: %s", p);
    char **items = NULL;
    int count = -1;
    // format: <device node> <mode> <uid> <gid>
S
sun_fan 已提交
143
    const int expectedCount = 4;
S
sun_fan 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

    if (INVALIDSTRING(p)) {
        INIT_LOGE("Invalid argument");
    }
    items = SplitUeventConfig(p, " ", &count, expectedCount);
    if (count != expectedCount) {
        INIT_LOGE("Ignore invalid item: %s", p);
        FreeConfigItems(items, count);
        return 0;
    }

    struct DeviceUdevConf *config = calloc(1, sizeof(struct DeviceUdevConf));
    if (config == NULL) {
        errno = ENOMEM;
        FreeConfigItems(items, count);
        return -1;
    }
S
sun_fan 已提交
161
    config->name = strdup(items[DEVICE_CONFIG_NAME_NUM]); // device node
S
sun_fan 已提交
162
    errno = 0;
S
sun_fan 已提交
163
    config->mode = strtoul(items[DEVICE_CONFIG_MODE_NUM], NULL, OCTAL_BASE);
S
sun_fan 已提交
164 165 166 167
    if (errno != 0) {
        INIT_LOGE("Invalid mode in config file for device node %s. use default mode", config->name);
        config->mode = DEVMODE;
    }
S
sun_fan 已提交
168 169
    config->uid = (uid_t)StringToInt(items[DEVICE_CONFIG_UID_NUM], 0);
    config->gid = (gid_t)StringToInt(items[DEVICE_CONFIG_GID_NUM], 0);
S
sun_fan 已提交
170 171 172 173 174 175 176 177 178 179 180
    ListAddTail(&g_devices, &config->list);
    FreeConfigItems(items, count);
    return 0;
}

static int ParseSysfsConfig(char *p)
{
    INIT_LOGD("Parse sysfs config info: %s", p);
    char **items = NULL;
    int count = -1;
    // format: <syspath> <attribute> <mode> <uid> <gid>
S
sun_fan 已提交
181
    const int expectedCount = 5;
S
sun_fan 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    if (INVALIDSTRING(p)) {
        INIT_LOGE("Invalid argument");
    }
    items = SplitUeventConfig(p, " ", &count, expectedCount);
    if (count != expectedCount) {
        INIT_LOGE("Ignore invalid item: %s", p);
        FreeConfigItems(items, count);
        return 0;
    }
    struct SysUdevConf *config = calloc(1, sizeof(struct SysUdevConf));
    if (config == NULL) {
        errno = ENOMEM;
        FreeConfigItems(items, count);
        return -1;
    }
S
sun_fan 已提交
198 199
    config->sysPath = strdup(items[SYS_CONFIG_PATH_NUM]); // sys path
    config->attr = strdup(items[SYS_CONFIG_ATTR_NUM]);  // attribute
S
sun_fan 已提交
200
    errno = 0;
S
sun_fan 已提交
201
    config->mode = strtoul(items[SYS_CONFIG_MODE_NUM], NULL, OCTAL_BASE);
S
sun_fan 已提交
202 203 204 205
    if (errno != 0) {
        INIT_LOGE("Invalid mode in config file for sys path %s. use default mode", config->sysPath);
        config->mode = DEVMODE;
    }
S
sun_fan 已提交
206 207
    config->uid = (uid_t)StringToInt(items[SYS_CONFIG_UID_NUM], 0);
    config->gid = (gid_t)StringToInt(items[SYS_CONFIG_GID_NUM], 0);
S
sun_fan 已提交
208
    ListAddTail(&g_sysDevices, &config->list);
S
sun_fan 已提交
209
    FreeConfigItems(items, count);
S
sun_fan 已提交
210 211 212
    return 0;
}

S
sun_fan 已提交
213
static int ParseFirmwareConfig(const char *p)
S
sun_fan 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
{
    INIT_LOGD("Parse firmware config info: %s", p);
    if (INVALIDSTRING(p)) {
        INIT_LOGE("Invalid argument");
    }

    // Sanity checks
    struct stat st = {};
    if (stat(p, &st) != 0) {
        INIT_LOGE("Invalid firware file: %s, err = %d", p, errno);
        return -1;
    }

    if (!S_ISDIR(st.st_mode)) {
        INIT_LOGE("Expect directory in firmware config");
        return -1;
    }

S
sun_fan 已提交
232 233 234 235 236 237
    struct FirmwareUdevConf *config = calloc(1, sizeof(struct FirmwareUdevConf));
    if (config == NULL) {
        errno = ENOMEM;
        return -1;
    }

S
sun_fan 已提交
238 239 240 241 242
    config->fmPath = strdup(p);
    ListAddTail(&g_firmwares, &config->list);
    return 0;
}

S
sun_fan 已提交
243
static SECTION GetSection(const char *section)
S
sun_fan 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
{
    if (INVALIDSTRING(section)) {
        return SECTION_INVALID;
    }

    if (STRINGEQUAL(section, "device")) {
        return SECTION_DEVICE;
    } else if (STRINGEQUAL(section, "sysfs")) {
        return SECTION_SYSFS;
    } else if (STRINGEQUAL(section, "firmware")) {
        return SECTION_FIRMWARE;
    } else {
        return SECTION_INVALID;
    }
}

static FUNCTIONMAPPER funcMapper[3] = {
    {"device", ParseDeviceConfig},
    {"sysfs", ParseSysfsConfig},
    {"firmware", ParseFirmwareConfig}
};

ParseConfigFunc callback;
int ParseUeventConfig(char *buffer)
{
    char *section = NULL;
    char *right = NULL;
    char *p = buffer;
    SECTION type;

    if (INVALIDSTRING(buffer)) {
        return -1;
    }
    if (*p == '[') {
        p++;
        if ((right = strchr(p, ']')) == NULL) {
            INIT_LOGE("Invalid line \"%s\", miss ']'", buffer);
            return -1;
        }
        *right = '\0'; // Replace ']' with '\0';
        section = p;
        INIT_LOGD("section is [%s]", section);
        if ((type = GetSection(section)) == SECTION_INVALID) {
            INIT_LOGE("Invalid section \" %s \"", section);
            callback = NULL; // reset callback
            return -1;
        }
        callback = funcMapper[type].func;
        return 0;
    }
S
sun_fan 已提交
294
    return ((callback != NULL) ? callback(p) : -1);
S
sun_fan 已提交
295 296
}

S
sun_fan 已提交
297
static void DoUeventConfigParse(char *buffer, size_t length)
S
sun_fan 已提交
298
{
S
sun_fan 已提交
299 300 301
    if (length < 0) {
        return;
    }
S
sun_fan 已提交
302 303
    char **items = NULL;
    int count = -1;
S
sun_fan 已提交
304
    const int maxItemCount = DEFAULTITEMCOUNT;
S
sun_fan 已提交
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

    items = SplitUeventConfig(buffer, "\n", &count, maxItemCount);
    INIT_LOGD("Dump items count = %d", count);
    for (int i = 0; i < count; i++) {
        char *p = items[i];
        // Skip lead white space
        while (isspace(*p)) {
            p++;
        }

        // Skip comment or empty line
        if (*p == '\0' || *p == '#') {
            continue;
        }
        int rc = ParseUeventConfig(p);
        if (rc < 0) {
            INIT_LOGE("Parse uevent config from %s failed", p);
        }
    }
    // release memory
    FreeConfigItems(items, count);
}

void ParseUeventdConfigFile(const char *file)
{
    if (INVALIDSTRING(file)) {
        return;
    }
S
sun_fan 已提交
333 334
    char config[PATH_MAX] = {0};
    if (Realpath(file, config, sizeof(config)) == NULL) {
S
sun_fan 已提交
335 336 337
        return;
    }
    int fd = open(config, O_RDONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
S
sun_fan 已提交
338 339 340 341 342 343 344 345 346
    if (fd < 0) {
        INIT_LOGE("Read from %s failed", file);
        return;
    }

    struct stat st;
    if (fstat(fd, &st) < 0) {
        INIT_LOGE("Failed to get file stat. err = %d", errno);
        close(fd);
S
sun_fan 已提交
347
        fd = -1;
S
sun_fan 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        return;
    }

    // st_size should never be less than 0
    size_t size = (size_t)st.st_size;
    char *buffer = malloc(size + 1);
    if (buffer == NULL) {
        INIT_LOGE("Failed to malloc memory. err = %d", errno);
        close(fd);
        return;
    }

    if (read(fd, buffer, size) != (ssize_t)size) {
        INIT_LOGE("Read from file %s failed. err = %d", file, errno);
        free(buffer);
        buffer = NULL;
        close(fd);
S
sun_fan 已提交
365
        fd = -1;
S
sun_fan 已提交
366 367 368 369
        return;
    }

    buffer[size] = '\0';
S
sun_fan 已提交
370
    DoUeventConfigParse(buffer, size);
S
sun_fan 已提交
371 372 373
    free(buffer);
    buffer = NULL;
    close(fd);
S
sun_fan 已提交
374
    fd = -1;
S
sun_fan 已提交
375 376 377 378 379 380 381 382 383 384 385 386
}

void GetDeviceNodePermissions(const char *devNode, uid_t *uid, gid_t *gid, mode_t *mode)
{
    if (INVALIDSTRING(devNode)) {
        return;
    }

    struct ListNode *node = NULL;
    if (!ListEmpty(g_devices)) {
        ForEachListEntry(&g_devices, node) {
            struct DeviceUdevConf *config = ListEntry(node, struct DeviceUdevConf, list);
S
sun_fan 已提交
387 388 389 390 391 392 393
            if (config != NULL) {
                if (STRINGEQUAL(config->name, devNode)) {
                    *uid = config->uid;
                    *gid = config->gid;
                    *mode = config->mode;
                    break;
                }
S
sun_fan 已提交
394 395 396
            }
        }
    }
S
sun_fan 已提交
397
    return;
S
sun_fan 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411
}

void ChangeSysAttributePermissions(const char *sysPath)
{
    if (INVALIDSTRING(sysPath)) {
        return;
    }

    struct ListNode *node = NULL;
    struct SysUdevConf *config = NULL;
    if (!ListEmpty(g_sysDevices)) {
        ForEachListEntry(&g_sysDevices, node) {
            config = ListEntry(node, struct SysUdevConf, list);
            if (STRINGEQUAL(config->sysPath, sysPath)) {
S
sun_fan 已提交
412
                break;
S
sun_fan 已提交
413 414 415 416 417 418 419
            }
        }
    }

    if (config == NULL) {
        return;
    }
S
sun_fan 已提交
420
    char sysAttr[SYSPATH_SIZE] = {0};
S
sun_fan 已提交
421 422 423 424 425
    if (config->sysPath != NULL && config->attr != NULL) {
        if (snprintf_s(sysAttr, SYSPATH_SIZE, SYSPATH_SIZE - 1, "/sys%s/%s", config->sysPath, config->attr) == -1) {
            INIT_LOGE("Failed to build sys attribute for sys path %s, attr: %s", config->sysPath, config->attr);
            return;
        }
S
sun_fan 已提交
426 427 428 429 430 431 432 433 434
    }
    if (chown(sysAttr, config->uid, config->gid) < 0) {
        INIT_LOGE("chown for file %s failed, err = %d", sysAttr, errno);
    }

    if (chmod(sysAttr, config->mode) < 0) {
        INIT_LOGE("[uevent][error] chmod for file %s failed, err = %d", sysAttr, errno);
    }
}