ueventd_device_handler.c 16.7 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_device_handler.h"
S
sun_fan 已提交
17

S
sun_fan 已提交
18 19
#include <errno.h>
#include <libgen.h>
S
sun_fan 已提交
20
#include <limits.h>
S
sun_fan 已提交
21 22 23 24 25
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
S
sun_fan 已提交
26
#include "init_utils.h"
S
sun_fan 已提交
27 28
#include "list.h"
#include "ueventd.h"
X
xionglei6 已提交
29
#ifndef __RAMDISK__
X
xionglei6 已提交
30
#include "ueventd_parameter.h"
X
xionglei6 已提交
31
#endif
S
sun_fan 已提交
32 33 34 35 36
#include "ueventd_read_cfg.h"
#include "ueventd_utils.h"
#include "securec.h"
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"
R
renwei 已提交
37 38 39
#ifdef WITH_SELINUX
#include <policycoreutils.h>
#endif
S
sun_fan 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

static void CreateSymbolLinks(const char *deviceNode, char **symLinks)
{
    if (INVALIDSTRING(deviceNode) || symLinks == NULL) {
        return;
    }

    for (int i = 0; symLinks[i] != NULL; i++) {
        const char *linkName = symLinks[i];
        char linkBuf[DEVICE_FILE_SIZE] = {};

        if (strncpy_s(linkBuf, DEVICE_FILE_SIZE - 1, linkName, strlen(linkName)) != EOK) {
            INIT_LOGE("Failed to copy link name");
            return;
        }
        const char *linkDir = dirname(linkBuf);
        if (MakeDirRecursive(linkDir, DIRMODE) < 0) {
X
xionglei6 已提交
57
            INIT_LOGE("[uevent] Failed to create dir \" %s \", err = %d", linkDir, errno);
S
sun_fan 已提交
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
        }
        errno = 0;
        int rc = symlink(deviceNode, linkName);
        if (rc != 0) {
            if (errno == EEXIST) {
                INIT_LOGW("Link \" %s \" already linked to other target", linkName);
            } else {
                INIT_LOGE("Failed to link \" %s \" to \" %s \", err = %d", deviceNode, linkName, errno);
            }
        }
    }
}

static inline void AdjustDeviceNodePermissions(const char *deviceNode, uid_t uid, gid_t gid, mode_t mode)
{
    if (INVALIDSTRING(deviceNode)) {
        return;
    }
    if (chown(deviceNode, uid, gid) != 0) {
        INIT_LOGW("Failed to change \" %s \" owner", deviceNode);
    }

    if (chmod(deviceNode, mode) != 0) {
        INIT_LOGW("Failed to change \" %s \" mode", deviceNode);
    }
}

R
renwei 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static void SetDeviceLable(const char *dir, const char *path)
{
#ifdef WITH_SELINUX
    int rc = 0;
    if (!STRINGEQUAL(dir, "/dev")) {
        rc = RestoreconRecurse(dir);
    }

    rc += Restorecon(path);
    if (rc != 0) {
        INIT_LOGI("restorecon device node[%s] failed. %d", path, errno);
    }
#endif
}

S
sun_fan 已提交
100 101 102
static int CreateDeviceNode(const struct Uevent *uevent, const char *deviceNode, char **symLinks, bool isBlock)
{
    int rc = -1;
103 104
    int major = uevent->major;
    int minor = uevent->minor;
S
sun_fan 已提交
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
    uid_t uid = uevent->ug.uid;
    gid_t gid = uevent->ug.gid;
    mode_t mode = DEVMODE;

    if (deviceNode == NULL || *deviceNode == '\0') {
        INIT_LOGE("Invalid device file");
        return rc;
    }

    char deviceNodeBuffer[DEVICE_FILE_SIZE] = {};
    if (strncpy_s(deviceNodeBuffer, DEVICE_FILE_SIZE - 1, deviceNode, strlen(deviceNode)) != EOK) {
        INIT_LOGE("Failed to copy device node");
        return rc;
    }
    const char *devicePath = dirname(deviceNodeBuffer);
    // device node always installed in /dev, should not be other locations.
    if (STRINGEQUAL(devicePath, ".") || STRINGEQUAL(devicePath, "/")) {
        INIT_LOGE("device path is not valid. should be starts with /dev");
        return rc;
    }

    rc = MakeDirRecursive(devicePath, DIRMODE);
    if (rc < 0) {
        INIT_LOGE("Create path \" %s \" failed", devicePath);
        return rc;
    }
S
sun_fan 已提交
131

S
sun_fan 已提交
132 133 134 135 136 137 138
    GetDeviceNodePermissions(deviceNode, &uid, &gid, &mode);
    mode |= isBlock ? S_IFBLK : S_IFCHR;
    dev_t dev = makedev(major, minor);
    setegid(0);
    rc = mknod(deviceNode, mode, dev);
    if (rc < 0) {
        if (errno != EEXIST) {
S
sun_fan 已提交
139
            INIT_LOGE("Create device node[%s %d, %d] failed. %d", deviceNode, major, minor, errno);
S
sun_fan 已提交
140 141 142 143
            return rc;
        }
    }
    AdjustDeviceNodePermissions(deviceNode, uid, gid, mode);
S
sun_fan 已提交
144
    if (symLinks != NULL) {
S
sun_fan 已提交
145 146
        CreateSymbolLinks(deviceNode, symLinks);
    }
R
renwei 已提交
147
    SetDeviceLable(devicePath, deviceNode);
S
sun_fan 已提交
148
    // No matter what result the symbol links returns,
S
sun_fan 已提交
149 150 151 152 153 154 155 156 157
    // as long as create device node done, just returns success.
    rc = 0;
    return rc;
}

static int RemoveDeviceNode(const char *deviceNode, char **symLinks)
{
    if (INVALIDSTRING(deviceNode)) {
        INIT_LOGE("Invalid device node");
S
sun_fan 已提交
158
        return -1;
S
sun_fan 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }
    if (symLinks != NULL) {
        for (int i = 0; symLinks[i] != NULL; i++) {
            char realPath[DEVICE_FILE_SIZE] = {};
            const char *linkName = symLinks[i];
            ssize_t ret = readlink(linkName, realPath, DEVICE_FILE_SIZE - 1);
            if (ret < 0) {
                continue;
            }
            if (STRINGEQUAL(deviceNode, realPath)) {
                unlink(linkName);
            }
        }
    }
    return unlink(deviceNode);
}

S
sun_fan 已提交
176 177 178 179 180 181 182 183 184 185
static char *FindPlatformDeviceName(char *path)
{
    if (INVALIDSTRING(path)) {
        return NULL;
    }

    if (STARTSWITH(path, "/sys/devices/platform/")) {
        path += strlen("/sys/devices/platform/");
        return path;
    }
186 187 188 189 190 191

    // Some platform devices may not be registered under platform device.
    if (STARTSWITH(path, "/sys/devices/")) {
        path += strlen("/sys/devices/");
        return path;
    }
S
sun_fan 已提交
192 193 194
    return NULL;
}

X
xionglei6 已提交
195 196
static void BuildBootDeviceSymbolLink(char **links, int linkNum, const char *partitionName)
{
197 198 199 200
    if (links == NULL) {
        INIT_LOGE("Function parameter error.");
        return;
    }
X
xionglei6 已提交
201
    if (linkNum > BLOCKDEVICE_LINKS - 1) {
202 203 204 205 206
        INIT_LOGW("Too many links, ignore.");
        return;
    }
    if (partitionName == NULL) {
        INIT_LOGW("Partition name is null, skip creating links");
X
xionglei6 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220
        return;
    }
    links[linkNum] = calloc(sizeof(char), DEVICE_FILE_SIZE);
    if (links[linkNum] == NULL) {
        INIT_LOGE("Failed to allocate memory for link, err = %d", errno);
        return;
    }

    if (snprintf_s(links[linkNum], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
        "/dev/block/by-name/%s", partitionName) == -1) {
        INIT_LOGE("Failed to build link");
    }
}

S
sun_fan 已提交
221 222
static void BuildDeviceSymbolLinks(char **links, int linkNum, const char *parent,
    const char *partitionName, const char *deviceName)
S
sun_fan 已提交
223
{
224 225
    if (linkNum > BLOCKDEVICE_LINKS - 1) {
        INIT_LOGW("Too many links, ignore");
S
sun_fan 已提交
226 227 228 229 230
        return;
    }
    links[linkNum] = calloc(sizeof(char), DEVICE_FILE_SIZE);
    if (links[linkNum] == NULL) {
        INIT_LOGE("Failed to allocate memory for link, err = %d", errno);
S
sun_fan 已提交
231 232 233 234 235 236
        return;
    }

    // If a block device without partition name.
    // For now, we will not create symbol link for it.
    if (!INVALIDSTRING(partitionName)) {
S
sun_fan 已提交
237
        if (snprintf_s(links[linkNum], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
S
sun_fan 已提交
238
            "/dev/block/platform/%s/by-name/%s", parent, partitionName) == -1) {
S
sun_fan 已提交
239
            INIT_LOGE("Failed to build link");
S
sun_fan 已提交
240
        }
S
sun_fan 已提交
241
    } else if (!INVALIDSTRING(deviceName)) {
S
sun_fan 已提交
242
        if (snprintf_s(links[linkNum], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
X
add ut  
xionglei6 已提交
243
            "/dev/block/platform/%s/%s", parent, deviceName) == -1) {
S
sun_fan 已提交
244 245 246 247 248
            INIT_LOGE("Failed to build link");
        }
    }
}

S
sun_fan 已提交
249 250
static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent)
{
251
    if (uevent == NULL || uevent->subsystem == NULL || STRINGEQUAL(uevent->subsystem, "block") == 0) {
S
sun_fan 已提交
252 253 254 255 256 257 258 259 260 261 262
        INIT_LOGW("Invalid arguments, Skip to get device symbol links.");
        return NULL;
    }

    // Only if current uevent is for real device.
    if (!STARTSWITH(uevent->syspath, "/devices")) {
        return NULL;
    }
    // For block device under one platform device.
    // check subsystem file under directory, see if it links to bus/platform.
    // For now, only support platform device.
S
sun_fan 已提交
263
    char sysPath[SYSPATH_SIZE] = {};
S
sun_fan 已提交
264 265 266 267 268 269 270 271
    if (snprintf_s(sysPath, SYSPATH_SIZE, SYSPATH_SIZE - 1, "/sys%s", uevent->syspath) == -1) {
        INIT_LOGE("Failed to build sys path for device %s", uevent->syspath);
        return NULL;
    }
    char **links = calloc(sizeof(char *), BLOCKDEVICE_LINKS);
    int linkNum = 0;
    if (links == NULL) {
        INIT_LOGE("Failed to allocate memory for links, err = %d", errno);
S
sun_fan 已提交
272
        return NULL;
S
sun_fan 已提交
273 274 275 276 277
    }

    // Reverse walk through sysPath, and check subystem file under each directory.
    char *parent = dirname(sysPath);
    while (parent != NULL && !STRINGEQUAL(parent, "/") && !STRINGEQUAL(parent, ".")) {
278
        char subsystem[SYSPATH_SIZE];
S
sun_fan 已提交
279 280 281 282
        if (snprintf_s(subsystem, SYSPATH_SIZE, SYSPATH_SIZE - 1, "%s/subsystem", parent) == -1) {
            INIT_LOGE("Failed to build subsystem path for device \" %s \"", uevent->syspath);
            return NULL;
        }
283 284
        char *bus = GetRealPath(subsystem);
        if (bus == NULL) {
S
sun_fan 已提交
285 286 287 288
            parent = dirname(parent);
            continue;
        }
        if (STRINGEQUAL(bus, "/sys/bus/platform")) {
X
xionglei6 已提交
289
            INIT_LOGV("Find a platform device: %s", parent);
S
sun_fan 已提交
290 291 292
            parent = FindPlatformDeviceName(parent);
            if (parent != NULL) {
                BuildDeviceSymbolLinks(links, linkNum, parent, uevent->partitionName, uevent->deviceName);
S
sun_fan 已提交
293
            }
S
sun_fan 已提交
294
            linkNum++;
X
xionglei6 已提交
295
            if ((parent != NULL) && STRINGEQUAL(parent, bootDevice)) {
X
xionglei6 已提交
296 297 298
                BuildBootDeviceSymbolLink(links, linkNum, uevent->partitionName);
                linkNum++;
            }
S
sun_fan 已提交
299
        }
300
        free(bus);
S
sun_fan 已提交
301 302
        parent = dirname(parent);
    }
S
sun_fan 已提交
303

S
sun_fan 已提交
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
    links[linkNum] = NULL;
    return links;
}

static void FreeSymbolLinks(char **links)
{
    if (links != NULL) {
        for (int i = 0; links[i] != NULL; i++) {
            free(links[i]);
            links[i] = NULL;
        }
        free(links);
        links = NULL;
    }
}

static void HandleDeviceNode(const struct Uevent *uevent, const char *deviceNode, bool isBlock)
{
    ACTION action = uevent->action;
    char **symLinks = NULL;

    // Block device path and name maybe not human readable.
    // Consider to create symbol links for them.
    // Make block device more readable.
    if (isBlock) {
        symLinks = GetBlockDeviceSymbolLinks(uevent);
    }

    if (action == ACTION_ADD) {
        if (CreateDeviceNode(uevent, deviceNode, symLinks, isBlock) < 0) {
            INIT_LOGE("Create device \" %s \" failed", deviceNode);
X
xionglei6 已提交
335
        } else {
X
xionglei6 已提交
336
#ifndef __RAMDISK__
X
xionglei6 已提交
337
            if (SetUeventDeviceParameter(deviceNode, action) != 0) {
X
xionglei6 已提交
338 339
                INIT_LOGE("Set device parameter added failed");
            }
X
xionglei6 已提交
340
#endif
S
sun_fan 已提交
341 342 343 344
        }
    } else if (action == ACTION_REMOVE) {
        if (RemoveDeviceNode(deviceNode, symLinks) < 0) {
            INIT_LOGE("Remove device \" %s \" failed", deviceNode);
X
xionglei6 已提交
345
        } else {
X
xionglei6 已提交
346
#ifndef __RAMDISK__
X
xionglei6 已提交
347
            if (SetUeventDeviceParameter(deviceNode, action) != 0) {
X
xionglei6 已提交
348 349
                INIT_LOGE("Set device parameter removed failed");
            }
X
xionglei6 已提交
350
#endif
S
sun_fan 已提交
351 352
        }
    } else if (action == ACTION_CHANGE) {
353
        INIT_LOGV("Device %s changed", uevent->syspath);
S
sun_fan 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
    }
    // Ignore other actions
    FreeSymbolLinks(symLinks);
}

static const char *GetDeviceName(char *sysPath, const char *deviceName)
{
    const char *devName = NULL;
    if (INVALIDSTRING(sysPath)) {
        INIT_LOGE("Invalid sys path");
        return NULL;
    }
    if (deviceName != NULL && deviceName[0] != '\0') {
        // if device name reported by kernel includes '/', skip it.
S
sun_fan 已提交
368
        // use entire device name reported by kernel
S
sun_fan 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381
        devName = basename((char *)deviceName);
        char *p = strrchr(deviceName, '/');
        if (p != NULL) { // device name includes slash
            p++;
            if (p == NULL || *p == '\0') {
                // device name ends with '/', which should never happen.
                // Get name from sys path.
                devName = basename(sysPath);
            } else {
                devName = p;
            }
        }
    } else {
Z
zhong_ning 已提交
382
        // kernel does not report DEVNAME, which is possible. use base name of syspath instead.
S
sun_fan 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
        devName = basename(sysPath);
    }
    return devName;
}

static const char *GetDeviceBasePath(const char *subsystem)
{
    char *devPath = NULL;
    if (INVALIDSTRING(subsystem)) {
        return devPath;
    }

    if (STRINGEQUAL(subsystem, "block")) {
        devPath = "/dev/block";
    } else if (STRINGEQUAL(subsystem, "input")) {
        devPath = "/dev/input";
    } else if (STRINGEQUAL(subsystem, "drm")) {
        devPath = "/dev/dri";
    } else if (STRINGEQUAL(subsystem, "graphics")) {
        devPath = "/dev/graphics";
    } else if (STRINGEQUAL(subsystem, "sound")) {
        devPath = "/dev/snd";
B
bigA2021 已提交
405 406
    } else if (STRINGEQUAL(subsystem, "functionfs")) {
        devPath = "/dev/functionfs";
407 408
    } else if (STRINGEQUAL(subsystem, "dma_heap")) {
        devPath = "/dev/dma_heap";
S
sun_fan 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    } else {
        devPath = "/dev";
    }
    return devPath;
}

void HandleBlockDeviceEvent(const struct Uevent *uevent)
{
    // Sanity checks
    if (uevent == NULL || uevent->subsystem == NULL) {
        INIT_LOGE("Invalid uevent message received");
        return;
    }

    if (strcmp(uevent->subsystem, "block") != 0) {
        INIT_LOGE("Unexpceted uevent subsystem \" %s \" received in block device handler", uevent->subsystem);
        return;
    }

    if (uevent->major < 0 || uevent->minor < 0) {
        return;
    }

    bool isBlock = true;
    // Block device always installed into /dev/block
    const char *devPath = GetDeviceBasePath(uevent->subsystem);
    char deviceNode[DEVICE_FILE_SIZE] = {};
    char sysPath[SYSPATH_SIZE] = {};

S
sun_fan 已提交
438 439 440
    if (uevent->syspath == NULL) {
        return;
    }
S
sun_fan 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    if (strncpy_s(sysPath, SYSPATH_SIZE - 1, uevent->syspath, strlen(uevent->syspath) != EOK)) {
        INIT_LOGE("Failed to copy sys path");
        return;
    }
    const char *devName = GetDeviceName(sysPath, uevent->deviceName);

    if (devPath == NULL || devName == NULL) {
        INIT_LOGE("Cannot get device path or device name");
        return;
    }
    if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "%s/%s", devPath, devName) == -1) {
        INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor);
        return;
    }
    HandleDeviceNode(uevent, deviceNode, isBlock);
}

void HandleOtherDeviceEvent(const struct Uevent *uevent)
{
S
sun_fan 已提交
460
    if (uevent == NULL || uevent->subsystem == NULL || uevent->syspath == NULL) {
S
sun_fan 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        INIT_LOGE("Invalid uevent received");
        return;
    }

    if (uevent->major < 0 || uevent->minor < 0) {
        return;
    }

    char deviceNode[DEVICE_FILE_SIZE] = {};
    char sysPath[SYSPATH_SIZE] = {};
    if (strncpy_s(sysPath, SYSPATH_SIZE - 1, uevent->syspath, strlen(uevent->syspath) != EOK)) {
        INIT_LOGE("Failed to copy sys path");
        return;
    }
    const char *devName = GetDeviceName(sysPath, uevent->deviceName);
    const char *devPath = GetDeviceBasePath(uevent->subsystem);

    if (devPath == NULL || devName == NULL) {
        INIT_LOGE("Cannot get device path or device name");
        return;
    }
X
xionglei6 已提交
482
    INIT_LOGV("HandleOtherDeviceEvent, devPath = %s, devName = %s", devPath, devName);
S
sun_fan 已提交
483

S
sun_fan 已提交
484 485 486 487
    // For usb devices, should take care of it specially.
    // if usb devices report DEVNAME, just create device node.
    // otherwise, create deviceNode with bus number and device number.
    if (STRINGEQUAL(uevent->subsystem, "usb")) {
S
sun_fan 已提交
488 489 490 491 492 493 494 495 496 497 498
        if (uevent->deviceName != NULL) {
            if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "/dev/%s", uevent->deviceName) == -1) {
                INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor);
                return;
            }
        } else {
            if (uevent->busNum < 0 || uevent->devNum < 0) {
                // usb device should always report bus number and device number.
                INIT_LOGE("usb device with invalid bus number or device number");
                return;
            }
S
sun_fan 已提交
499
            if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
S
sun_fan 已提交
500
                "/dev/bus/usb/%03d/%03d", uevent->busNum, uevent->devNum) == -1) {
S
sun_fan 已提交
501
                INIT_LOGE("Make usb device node for device [%d : %d]", uevent->busNum, uevent->devNum);
S
sun_fan 已提交
502 503
            }
        }
S
sun_fan 已提交
504 505 506 507
    } else if (STARTSWITH(uevent->subsystem, "usb")) {
        // Other usb devies, do not handle it.
        return;
    } else {
S
sun_fan 已提交
508 509 510 511
        if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "%s/%s", devPath, devName) == -1) {
            INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor);
            return;
        }
S
sun_fan 已提交
512 513
    }
    HandleDeviceNode(uevent, deviceNode, false);
S
sun_fan 已提交
514
}