ueventd_read_cfg.c 11.6 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
#include <ctype.h>
S
sun_fan 已提交
19 20
#include <limits.h>
#include <string.h>
X
add ut  
xionglei6 已提交
21
#include <stdbool.h>
22
#include <stdlib.h>
S
sun_fan 已提交
23
#include <unistd.h>
24 25 26
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
S
sun_fan 已提交
27
#include "init_utils.h"
S
sun_fan 已提交
28
#include "list.h"
S
sun_fan 已提交
29
#include "ueventd_utils.h"
30
#include "securec.h"
S
sun_fan 已提交
31 32 33 34
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"

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

#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
X
xionglei6 已提交
47
#define DEVICE_CONFIG_PARAM_NUM 4
S
sun_fan 已提交
48

S
sun_fan 已提交
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
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 int ParseDeviceConfig(char *p)
{
X
xionglei6 已提交
79
    INIT_LOGV("Parse device config info: %s", p);
S
sun_fan 已提交
80 81
    char **items = NULL;
    int count = -1;
X
xionglei6 已提交
82 83
    // format: <device node> <mode> <uid> <gid> <parameter>
    const int expectedCount = 5;
S
sun_fan 已提交
84

X
add ut  
xionglei6 已提交
85
    INIT_CHECK_ONLY_ELOG(!INVALIDSTRING(p), "Invalid argument");
86
    items = SplitStringExt(p, " ", &count, expectedCount);
X
xionglei6 已提交
87
    if ((count != expectedCount) && (count != expectedCount - 1)) {
S
sun_fan 已提交
88
        INIT_LOGE("Ignore invalid item: %s", p);
89
        FreeStringVector(items, count);
S
sun_fan 已提交
90 91 92 93 94 95
        return 0;
    }

    struct DeviceUdevConf *config = calloc(1, sizeof(struct DeviceUdevConf));
    if (config == NULL) {
        errno = ENOMEM;
96
        FreeStringVector(items, count);
S
sun_fan 已提交
97 98
        return -1;
    }
S
sun_fan 已提交
99
    config->name = strdup(items[DEVICE_CONFIG_NAME_NUM]); // device node
S
sun_fan 已提交
100
    errno = 0;
S
sun_fan 已提交
101
    config->mode = strtoul(items[DEVICE_CONFIG_MODE_NUM], NULL, OCTAL_BASE);
X
add ut  
xionglei6 已提交
102 103
    INIT_ERROR_CHECK(errno == 0, config->mode = DEVMODE,
        "Invalid mode in config file for device node %s. use default mode", config->name);
S
sun_fan 已提交
104 105
    config->uid = (uid_t)DecodeUid(items[DEVICE_CONFIG_UID_NUM]);
    config->gid = (gid_t)DecodeUid(items[DEVICE_CONFIG_GID_NUM]);
X
xionglei6 已提交
106 107 108 109 110
    if (count == expectedCount) {
        config->parameter = strdup(items[DEVICE_CONFIG_PARAM_NUM]); // device parameter
    } else {
        config->parameter = NULL;
    }
X
xionglei6 已提交
111
    ListInit(&config->paramNode);
S
sun_fan 已提交
112
    ListAddTail(&g_devices, &config->list);
113
    FreeStringVector(items, count);
S
sun_fan 已提交
114 115 116 117 118
    return 0;
}

static int ParseSysfsConfig(char *p)
{
X
xionglei6 已提交
119
    INIT_LOGV("Parse sysfs config info: %s", p);
S
sun_fan 已提交
120 121 122
    char **items = NULL;
    int count = -1;
    // format: <syspath> <attribute> <mode> <uid> <gid>
S
sun_fan 已提交
123
    const int expectedCount = 5;
S
sun_fan 已提交
124

X
add ut  
xionglei6 已提交
125
    INIT_CHECK_ONLY_ELOG(!INVALIDSTRING(p), "Invalid argument");
126
    items = SplitStringExt(p, " ", &count, expectedCount);
S
sun_fan 已提交
127 128
    if (count != expectedCount) {
        INIT_LOGE("Ignore invalid item: %s", p);
129
        FreeStringVector(items, count);
S
sun_fan 已提交
130 131 132 133 134
        return 0;
    }
    struct SysUdevConf *config = calloc(1, sizeof(struct SysUdevConf));
    if (config == NULL) {
        errno = ENOMEM;
135
        FreeStringVector(items, count);
S
sun_fan 已提交
136 137
        return -1;
    }
S
sun_fan 已提交
138 139
    config->sysPath = strdup(items[SYS_CONFIG_PATH_NUM]); // sys path
    config->attr = strdup(items[SYS_CONFIG_ATTR_NUM]);  // attribute
S
sun_fan 已提交
140
    errno = 0;
S
sun_fan 已提交
141
    config->mode = strtoul(items[SYS_CONFIG_MODE_NUM], NULL, OCTAL_BASE);
X
add ut  
xionglei6 已提交
142 143
    INIT_ERROR_CHECK(errno == 0, config->mode = DEVMODE,
        "Invalid mode in config file for sys path %s. use default mode", config->sysPath);
S
sun_fan 已提交
144 145
    config->uid = (uid_t)DecodeUid(items[SYS_CONFIG_UID_NUM]);
    config->gid = (gid_t)DecodeUid(items[SYS_CONFIG_GID_NUM]);
S
sun_fan 已提交
146
    ListAddTail(&g_sysDevices, &config->list);
147
    FreeStringVector(items, count);
S
sun_fan 已提交
148 149 150
    return 0;
}

S
sun_fan 已提交
151
static int ParseFirmwareConfig(char *p)
S
sun_fan 已提交
152
{
X
xionglei6 已提交
153
    INIT_LOGV("Parse firmware config info: %s", p);
X
add ut  
xionglei6 已提交
154
    INIT_CHECK_ONLY_ELOG(!INVALIDSTRING(p), "Invalid argument");
S
sun_fan 已提交
155 156 157

    // Sanity checks
    struct stat st = {};
X
add ut  
xionglei6 已提交
158 159
    INIT_ERROR_CHECK(stat(p, &st) == 0, return -1, "Invalid firware file: %s, err = %d", p, errno);
    INIT_ERROR_CHECK(S_ISDIR(st.st_mode), return -1, "Expect directory in firmware config");
S
sun_fan 已提交
160
    struct FirmwareUdevConf *config = calloc(1, sizeof(struct FirmwareUdevConf));
X
add ut  
xionglei6 已提交
161 162
    INIT_CHECK(config != NULL, errno = ENOMEM;
        return -1);
S
sun_fan 已提交
163 164 165 166 167
    config->fmPath = strdup(p);
    ListAddTail(&g_firmwares, &config->list);
    return 0;
}

S
sun_fan 已提交
168
static SECTION GetSection(const char *section)
S
sun_fan 已提交
169
{
X
add ut  
xionglei6 已提交
170
    INIT_CHECK_RETURN_VALUE(!INVALIDSTRING(section), SECTION_INVALID);
S
sun_fan 已提交
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
    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;

X
add ut  
xionglei6 已提交
196
    INIT_CHECK_RETURN_VALUE(!INVALIDSTRING(buffer), -1);
S
sun_fan 已提交
197 198 199 200 201 202 203 204
    if (*p == '[') {
        p++;
        if ((right = strchr(p, ']')) == NULL) {
            INIT_LOGE("Invalid line \"%s\", miss ']'", buffer);
            return -1;
        }
        *right = '\0'; // Replace ']' with '\0';
        section = p;
X
xionglei6 已提交
205
        INIT_LOGV("section is [%s]", section);
S
sun_fan 已提交
206 207 208 209 210 211 212 213
        if ((type = GetSection(section)) == SECTION_INVALID) {
            INIT_LOGE("Invalid section \" %s \"", section);
            callback = NULL; // reset callback
            return -1;
        }
        callback = funcMapper[type].func;
        return 0;
    }
X
xionglei6 已提交
214
    return (callback != NULL) ? callback(p) : -1;
S
sun_fan 已提交
215 216
}

S
sun_fan 已提交
217
static void DoUeventConfigParse(char *buffer, size_t length)
S
sun_fan 已提交
218 219 220
{
    char **items = NULL;
    int count = -1;
S
sun_fan 已提交
221
    const int maxItemCount = DEFAULTITEMCOUNT;
S
sun_fan 已提交
222

223
    items = SplitStringExt(buffer, "\n", &count, maxItemCount);
X
xionglei6 已提交
224
    INIT_LOGV("Dump items count = %d", count);
S
sun_fan 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    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
242
    FreeStringVector(items, count);
S
sun_fan 已提交
243 244 245 246
}

void ParseUeventdConfigFile(const char *file)
{
X
add ut  
xionglei6 已提交
247
    INIT_CHECK_ONLY_RETURN(!INVALIDSTRING(file));
248
    char *config = GetRealPath(file);
X
add ut  
xionglei6 已提交
249
    INIT_CHECK_ONLY_RETURN(config != NULL);
S
sun_fan 已提交
250
    int fd = open(config, O_RDONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
251
    free(config);
X
add ut  
xionglei6 已提交
252
    INIT_ERROR_CHECK(fd >= 0, return, "Read from %s failed", file);
S
sun_fan 已提交
253 254 255 256 257

    struct stat st;
    if (fstat(fd, &st) < 0) {
        INIT_LOGE("Failed to get file stat. err = %d", errno);
        close(fd);
S
sun_fan 已提交
258
        fd = -1;
S
sun_fan 已提交
259 260 261 262 263 264 265 266 267
        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);
X
add ut  
xionglei6 已提交
268
        fd = -1;
S
sun_fan 已提交
269 270 271 272 273 274 275 276
        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 已提交
277
        fd = -1;
S
sun_fan 已提交
278 279 280 281
        return;
    }

    buffer[size] = '\0';
S
sun_fan 已提交
282
    DoUeventConfigParse(buffer, size);
S
sun_fan 已提交
283 284 285
    free(buffer);
    buffer = NULL;
    close(fd);
S
sun_fan 已提交
286
    fd = -1;
S
sun_fan 已提交
287 288
}

X
add ut  
xionglei6 已提交
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
// support '*' to match all characters
// '?' match one character.
bool IsMatch(const char *target, const char *pattern)
{
    INIT_CHECK_RETURN_VALUE(target != NULL, false);
    INIT_CHECK_RETURN_VALUE(pattern != NULL, true);

    const char *t = target;
    const char *p = pattern;
    const char *plast = NULL;
    bool reMatch = false;
    while (*t != '\0') {
        if (*t == *p) {
            t++;
            p++;
            continue;
        }

        // Match one character.
        if (*p == '?') {
            p++;
            t++;
            continue;
        }

        if (*p == '\0') {
            return !reMatch ? false : true;
        }

        if (*p == '*') {
            reMatch = true;
            // Met '*', record where we will start over.
            // plast point to next character that we will compare it again.
            plast = ++p;
            t++;
            continue;
        }

        if (reMatch) {
            // Start over.
            p = plast;
            t++;
        } else {
            return false;
        }
    }

    while (*p == '*') {
        p++;
    }
X
xionglei6 已提交
339
    return (*p == '\0') ? true : false;
X
add ut  
xionglei6 已提交
340 341
}

X
xionglei6 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
struct DeviceUdevConf *GetDeviceUdevConfByDevNode(const char *devNode)
{
    if (INVALIDSTRING(devNode)) {
        return NULL;
    }

    struct ListNode *node = NULL;
    if (!ListEmpty(g_devices)) {
        ForEachListEntry(&g_devices, node) {
            struct DeviceUdevConf *config = ListEntry(node, struct DeviceUdevConf, list);
            if (IsMatch(devNode, config->name)) {
                return config;
            }
        }
    }
    return NULL;
}

S
sun_fan 已提交
360 361 362 363 364 365 366 367 368 369
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);
X
add ut  
xionglei6 已提交
370
            if (IsMatch(devNode, config->name)) {
371 372 373 374
                *uid = config->uid;
                *gid = config->gid;
                *mode = config->mode;
                break;
S
sun_fan 已提交
375 376 377
            }
        }
    }
S
sun_fan 已提交
378
    return;
S
sun_fan 已提交
379 380 381 382 383 384 385 386 387 388
}

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

    struct ListNode *node = NULL;
    struct SysUdevConf *config = NULL;
S
sun_fan 已提交
389
    int matched = 0;
S
sun_fan 已提交
390 391 392 393
    if (!ListEmpty(g_sysDevices)) {
        ForEachListEntry(&g_sysDevices, node) {
            config = ListEntry(node, struct SysUdevConf, list);
            if (STRINGEQUAL(config->sysPath, sysPath)) {
S
sun_fan 已提交
394
                matched = 1;
S
sun_fan 已提交
395
                break;
S
sun_fan 已提交
396 397 398 399
            }
        }
    }

S
sun_fan 已提交
400
    if (matched == 0) {
S
sun_fan 已提交
401 402
        return;
    }
403 404 405 406
    char sysAttr[SYSPATH_SIZE] = {};
    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 已提交
407 408 409 410 411 412 413 414 415
    }
    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);
    }
}