uevent.c 21.0 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Copyright (c) 2020 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 <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <poll.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <dirent.h>
#include <linux/netlink.h>
#include "list.h"
#include "securec.h"

L
leon 已提交
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
#define LINK_NUMBER 4
#define DEFAULT_DIR_MODE 0755
#define DEV_DRM 3
#define DEV_ONCRPC 6
#define DEV_ADSP 4
#define DEV_INPUT 5
#define DEV_MTD 3
#define DEV_SOUND 5
#define DEV_MISC 4
#define DEV_DEFAULT 4
#define DEV_PLAT_FORM 9
#define DEV_USB 4
#define DEV_GRAPHICS 8
#define EVENT_ACTION 7
#define EVENT_DEVPATH 8
#define EVENT_SYSTEM 10
#define EVENT_FIRMWARE 9
#define EVENT_MAJOR 6
#define EVENT_MINOR 6
#define EVENT_PARTN 6
#define EVENT_PART_NAME 9
#define EVENT_DEV_NAME 8
#define EVENT_BLOCK 5
#define EVENT_PLAT_FORM 8
#define TRIGGER_ADDR_SIZE 4
#define BASE_BUFFER_SIZE 1024
#define MAX_BUFFER 256
#define EVENT_MAX_BUFFER 1026
#define MAX_DEV_PATH 96
#define MINORS_GROUPS 128
#define SYS_LINK_NUMBER 2
#define MAX_DEVICE_LEN 64
#define DEFAULT_MODE 0000
#define DEVICE_SKIP 5
#define HANDLE_DEVICE_USB 3
#define DEFAULT_NO_AUTHORITY_MODE 0600

Z
zhong_ning 已提交
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
int g_ueventFD = -1;

struct Uevent {
    const char *action;
    const char *path;
    const char *subsystem;
    const char *firmware;
    const char *partitionName;
    const char *deviceName;
    int partitionNum;
    int major;
    int minor;
};

struct PlatformNode {
    char *name;
    char *path;
    size_t pathLen;
    struct ListNode list;
};

static struct ListNode g_platformNames = {
    .next = &g_platformNames,
    .prev = &g_platformNames,
};

L
leon 已提交
97
const char *g_trigger = "/dev/.trigger_uevent";
Z
zhong_ning 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
static void HandleUevent();

static int UeventFD()
{
    return g_ueventFD;
}

static void DoTrigger(DIR *dir)
{
    struct dirent *de = NULL;
    int dfd = dirfd(dir);
    int fd = openat(dfd, "uevent", O_WRONLY);
    if (fd >= 0) {
L
leon 已提交
111
        write(fd, "add\n", TRIGGER_ADDR_SIZE);
Z
zhong_ning 已提交
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
        close(fd);
        HandleUevent();
    }

    while ((de = readdir(dir)) != NULL) {
        DIR *dir2 = NULL;
        if (de->d_type != DT_DIR || de->d_name[0] == '.') {
            continue;
        }
        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
        if (fd < 0) {
            continue;
        }

        dir2 = fdopendir(fd);
        if (dir2 == NULL) {
            close(fd);
        } else {
            DoTrigger(dir2);
            closedir(dir2);
        }
    }
}

void Trigger(const char *sysPath)
{
    DIR *dir = opendir(sysPath);
    if (dir) {
        DoTrigger(dir);
        closedir(dir);
    }
}

static void RetriggerUevent()
{
L
leon 已提交
147
    if (access(g_trigger, F_OK) == 0) {
Z
zhong_ning 已提交
148 149 150 151 152 153
        printf("Skip trigger uevent, alread done\n");
        return;
    }
    Trigger("/sys/class");
    Trigger("/sys/block");
    Trigger("/sys/devices");
L
leon 已提交
154
    int fd = open(g_trigger, O_WRONLY | O_CREAT | O_CLOEXEC, DEFAULT_MODE);
Z
zhong_ning 已提交
155 156 157 158 159 160 161 162 163
    if (fd > 0) {
        close(fd);
    }
    printf("Re-trigger uevent done\n");
}

static void UeventSockInit()
{
    struct sockaddr_nl addr;
L
leon 已提交
164
    int buffSize = MAX_BUFFER * BASE_BUFFER_SIZE;
Z
zhong_ning 已提交
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    int on = 1;

    if (memset_s(&addr, sizeof(addr), 0, sizeof(addr)) != 0) {
        return;
    }
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = getpid();
    addr.nl_groups = 0xffffffff;

    int sockfd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
    if (sockfd < 0) {
        printf("Create socket failed. %d\n", errno);
        return;
    }

    setsockopt(sockfd, SOL_SOCKET, SO_RCVBUFFORCE, &buffSize, sizeof(buffSize));
    setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));

    if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        printf("Bind socket failed. %d\n", errno);
        close(sockfd);
        return;
    }
    g_ueventFD = sockfd;
    fcntl(g_ueventFD, F_SETFD, FD_CLOEXEC);
    fcntl(g_ueventFD, F_SETFL, O_NONBLOCK);
    RetriggerUevent();
    return;
}

ssize_t ReadUevent(int fd, void *buf, size_t len)
{
    struct iovec iov = { buf, len };
    struct sockaddr_nl addr;
    char control[CMSG_SPACE(sizeof(struct ucred))];
    uid_t uid = -1;
    struct msghdr hdr = {
        &addr,
        sizeof(addr),
        &iov,
        1,
        control,
        sizeof(control),
        0,
    };
    ssize_t n = recvmsg(fd, &hdr, 0);
    if (n <= 0) {
        return n;
    }
    struct cmsghdr *cMsg = CMSG_FIRSTHDR(&hdr);
    if (cMsg == NULL || cMsg->cmsg_type != SCM_CREDENTIALS) {
        goto out;
    }
    struct ucred *cRed = (struct ucred *)CMSG_DATA(cMsg);
    uid = cRed->uid;
    if (uid != 0) {
        goto out;
    }
 
    if (addr.nl_groups == 0 || addr.nl_pid != 0) {
    /* ignoring non-kernel or unicast netlink message */
        goto out;
    }

    return n;
out:
    bzero(buf, len);
    errno = -EIO;
    return n;
}

static void InitUevent(struct Uevent *event)
{
    event->action = "";
    event->path = "";
    event->subsystem = "";
    event->firmware = "";
    event->partitionName = "";
    event->deviceName = "";
    event->partitionNum = -1;
}

static void ParseUevent(const char *buf, struct Uevent *event)
{
    InitUevent(event);
    while (*buf) {
L
leon 已提交
251 252
        if (strncmp(buf, "ACTION=", EVENT_ACTION) == 0) {
            buf += EVENT_ACTION;
Z
zhong_ning 已提交
253
            event->action = buf;
L
leon 已提交
254 255
        } else if (strncmp(buf, "DEVPATH=", EVENT_DEVPATH) == 0) {
            buf += EVENT_DEVPATH;
Z
zhong_ning 已提交
256
            event->path = buf;
L
leon 已提交
257 258
        } else if (strncmp(buf, "SUBSYSTEM=", EVENT_SYSTEM) == 0) {
            buf += EVENT_SYSTEM;
Z
zhong_ning 已提交
259
            event->subsystem = buf;
L
leon 已提交
260 261
        } else if (strncmp(buf, "FIRMWARE=", EVENT_FIRMWARE) == 0) {
            buf += EVENT_FIRMWARE;
Z
zhong_ning 已提交
262
            event->firmware = buf;
L
leon 已提交
263 264
        } else if (strncmp(buf, "MAJOR=", EVENT_MAJOR) == 0) {
            buf += EVENT_MAJOR;
Z
zhong_ning 已提交
265
            event->major = atoi(buf);
L
leon 已提交
266 267
        } else if (strncmp(buf, "MINOR=", EVENT_MINOR) == 0) {
            buf += EVENT_MINOR;
Z
zhong_ning 已提交
268
            event->minor = atoi(buf);
L
leon 已提交
269 270
        } else if (strncmp(buf, "PARTN=", EVENT_PARTN) == 0) {
            buf += EVENT_PARTN;
Z
zhong_ning 已提交
271
            event->partitionNum = atoi(buf);
L
leon 已提交
272 273
        } else if (strncmp(buf, "PARTNAME=", EVENT_PART_NAME) == 0) {
            buf += EVENT_PART_NAME;
Z
zhong_ning 已提交
274
            event->partitionName = buf;
L
leon 已提交
275 276
        } else if (strncmp(buf, "DEVNAME=", EVENT_DEV_NAME) == 0) {
            buf += EVENT_DEV_NAME;
Z
zhong_ning 已提交
277 278 279 280 281 282 283 284 285 286 287 288 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
            event->deviceName = buf;
        }
        // Drop reset.
        while (*buf++) {}
    }
}

static int MakeDir(const char *path, mode_t mode)
{
    int rc = mkdir(path, mode);
    if (rc < 0 && errno != EEXIST) {
        printf("Create %s failed. %d\n", path, errno);
    }
    return rc;
}

static struct PlatformNode *FindPlatformDevice(const char *path)
{
    size_t pathLen = strlen(path);
    struct ListNode *node = NULL;
    struct PlatformNode *bus = NULL;

    for (node = (&g_platformNames)->prev; node != &g_platformNames; node = node->prev) {
        bus = (struct PlatformNode *)(((char*)(node)) - offsetof(struct PlatformNode, list));
        if ((bus->pathLen < pathLen) && (path[bus->pathLen] == '/') && !strncmp(path, bus->path, bus->pathLen)) {
            return bus;
        }
    }
    return NULL;
}

static void Sanitize(char *s)
{
    const char* accept =
        "abcdefghijklmnopqrstuvwxyz"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "0123456789"
        "_-.";

    if (!s) {
        return;
    }

    for (; *s; s++) {
        s += strspn(s, accept);
        if (*s) {
            *s = '_';
        }
    }
}

static char **ParsePlatformBlockDevice(const struct Uevent *uevent)
{
    const char *device;
    char *slash = NULL;
    const char *type;
L
leon 已提交
333
    char linkPath[MAX_BUFFER];
Z
zhong_ning 已提交
334 335 336 337
    int linkNum = 0;
    char *p = NULL;

    struct PlatformNode *pDev = FindPlatformDevice(uevent->path);
L
leon 已提交
338
    if (!pDev) {
Z
zhong_ning 已提交
339 340
        return NULL;
    }
L
leon 已提交
341 342 343 344
    device = pDev->name;
    type = "platform";
    char **links = calloc(sizeof(char *), LINK_NUMBER);
    if (!links) {
Z
zhong_ning 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
        return NULL;
    }
    if (snprintf_s(linkPath, sizeof(linkPath), sizeof(linkPath), "/dev/block/%s/%s", type, device) == -1) {
        return NULL;
    }
    if (uevent->partitionName) {
        p = strdup(uevent->partitionName);
        Sanitize(p);
        if (strcmp(uevent->partitionName, p)) {
            printf("Linking partition '%s' as '%s'\n", uevent->partitionName, p);
        }
        if (asprintf(&links[linkNum], "%s/by-name/%s", linkPath, p) > 0) {
            linkNum++;
        } else {
            links[linkNum] = NULL;
        }
        free(p);
    }
    if (uevent->partitionNum >= 0) {
        if (asprintf(&links[linkNum], "%s/by-num/p%d", linkPath, uevent->partitionNum) > 0) {
            linkNum++;
        } else {
            links[linkNum] = NULL;
        }
    }
    slash = strrchr(uevent->path, '/');
    if (asprintf(&links[linkNum], "%s/%s", linkPath, slash + 1) > 0) {
        linkNum++;
    } else {
        links[linkNum] = NULL;
    }
    return links;
}

static void MakeDevice(const char *devpath, const char *path, int block, int major, int minor)
{
    /* Only for super user */
    gid_t gid = 0;
    dev_t dev;
L
leon 已提交
384
    mode_t mode = DEFAULT_NO_AUTHORITY_MODE;
Z
zhong_ning 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 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
    mode |= (block ? S_IFBLK : S_IFCHR);
    dev = makedev(major, minor);
    setegid(gid);
    if (mknod(devpath, mode, dev) != 0) {
        if (errno != EEXIST) {
            printf("Make device node[%d, %d] failed. %d\n", major, minor, errno);
        }
    }
}

int MkdirRecursive(const char *pathName, mode_t mode)
{
    char buf[128];
    const char *slash;
    const char *p = pathName;
    struct stat info;

    while ((slash = strchr(p, '/')) != NULL) {
        int width = slash - pathName;
        p = slash + 1;
        if (width < 0) {
            break;
        }
        if (width == 0) {
            continue;
        }
        if ((unsigned int)width > sizeof(buf) - 1) {
            printf("path too long for MkdirRecursive\n");
            return -1;
        }
        if (memcpy_s(buf, width, pathName, width) != 0) {
            return -1;
        }
        buf[width] = 0;
        if (stat(buf, &info) != 0) {
            int ret = MakeDir(buf, mode);
            if (ret && errno != EEXIST) {
                return ret;
            }
        }
    }
    int ret = MakeDir(pathName, mode);
    if (ret && errno != EEXIST) {
        return ret;
    }
    return 0;
}

void RemoveLink(const char *oldpath, const char *newpath)
{
L
leon 已提交
435
    char path[MAX_BUFFER];
Z
zhong_ning 已提交
436 437 438 439 440 441 442 443 444 445 446 447
    ssize_t ret = readlink(newpath, path, sizeof(path) - 1);
    if (ret <= 0) {
        return;
    }
    path[ret] = 0;
    if (!strcmp(path, oldpath)) {
        unlink(newpath);
    }
}

static void MakeLink(const char *oldPath, const char *newPath)
{
L
leon 已提交
448
    char buf[MAX_BUFFER];
Z
zhong_ning 已提交
449 450 451 452 453 454 455 456 457 458 459 460
    char *slash = strrchr(newPath, '/');
    if (!slash) {
        return;
    }
    int width = slash - newPath;
    if (width <= 0 || width > (int)sizeof(buf) - 1) {
        return;
    }
    if (memcpy_s(buf, sizeof(buf), newPath, width) != 0) {
        return;
    }
    buf[width] = 0;
L
leon 已提交
461
    int ret = MkdirRecursive(buf, DEFAULT_DIR_MODE);
Z
zhong_ning 已提交
462 463 464 465 466 467 468 469 470
    if (ret) {
        printf("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
    }
    ret = symlink(oldPath, newPath);
    if (ret && errno != EEXIST) {
        printf("Failed to symlink %s to %s: %s (%d)\n", oldPath, newPath, strerror(errno), errno);
    }
}

L
leon 已提交
471
static void HandleDevice(struct Uevent *event, const char *devpath, int block, char **links)
Z
zhong_ning 已提交
472 473
{
    int i;
L
leon 已提交
474 475
    if (!strcmp(event->action, "add")) {
        MakeDevice(devpath, event->path, block, event->major, event->minor);
Z
zhong_ning 已提交
476 477 478 479 480 481 482
        if (links) {
            for (i = 0; links[i]; i++) {
                MakeLink(devpath, links[i]);
            }
        }
    }

L
leon 已提交
483
    if (!strcmp(event->action, "remove")) {
Z
zhong_ning 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        if (links) {
            for (i = 0; links[i]; i++) {
                RemoveLink(devpath, links[i]);
            }
        }
        unlink(devpath);
    }

    if (links) {
        for (i = 0; links[i]; i++) {
            free(links[i]);
        }
        free(links);
    }
}

static void HandleBlockDevice(struct Uevent *event)
{
    const char *base = "/dev/block";
L
leon 已提交
503
    char devpath[MAX_DEV_PATH];
Z
zhong_ning 已提交
504 505 506 507 508 509 510 511 512 513
    char **links = NULL;

    if (event->major < 0 || event->minor < 0) {
        return;
    }
    const char *name = strrchr(event->path, '/');
    if (name == NULL) {
        return;
    }
    name++;
L
leon 已提交
514
    if (strlen(name) > MAX_DEVICE_LEN) { // too long
Z
zhong_ning 已提交
515 516 517 518 519
        return;
    }
    if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "%s/%s", base, name) == -1) {
        return;
    }
L
leon 已提交
520 521
    MakeDir(base, DEFAULT_DIR_MODE);
    if (!strncmp(event->path, "/devices/", DEV_PLAT_FORM)) {
Z
zhong_ning 已提交
522 523
        links = ParsePlatformBlockDevice(event);
    }
L
leon 已提交
524
    HandleDevice(event, devpath, 1, links);
Z
zhong_ning 已提交
525 526 527 528 529 530 531
}

static void AddPlatformDevice(const char *path)
{
    size_t pathLen = strlen(path);
    const char *name = path;

L
leon 已提交
532 533 534 535
    if (!strncmp(path, "/devices/", DEV_PLAT_FORM)) {
        name += DEV_PLAT_FORM;
        if (!strncmp(name, "platform/", DEV_PLAT_FORM)) {
            name += DEV_PLAT_FORM;
Z
zhong_ning 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
        }
    }
    printf("adding platform device %s (%s)\n", name, path);
    struct PlatformNode *bus = calloc(1, sizeof(struct PlatformNode));
    bus->path = strdup(path);
    bus->pathLen = pathLen;
    bus->name = bus->path + (name - path);
    ListAddTail(&g_platformNames, &bus->list);
}

static void RemovePlatformDevice(const char *path)
{
    struct ListNode *node = NULL;
    struct PlatformNode *bus = NULL;

    for (node = (&g_platformNames)->prev; node != &g_platformNames; node = node->prev) {
        bus = (struct PlatformNode *)(((char*)(node)) - offsetof(struct PlatformNode, list));
        if (!strcmp(path, bus->path)) {
            printf("removing platform device %s\n", bus->name);
            free(bus->path);
            ListRemove(node);
            free(bus);
            return;
        }
    }
}

static void HandlePlatformDevice(const struct Uevent *event)
{
    const char *path = event->path;
    if (strcmp(event->action, "add") == 0) {
        AddPlatformDevice(path);
    } else if (strcmp(event->action, "remove") == 0) {
        RemovePlatformDevice(path);
    }
}

static const char *ParseDeviceName(const struct Uevent *uevent, unsigned int len)
{
    /* if it's not a /dev device, nothing else to do */
    if ((uevent->major < 0) || (uevent->minor < 0)) {
        return NULL;
    }
    /* do we have a name? */
    const char *name = strrchr(uevent->path, '/');
    if (!name) {
        return NULL;
    }
    name++;
    /* too-long names would overrun our buffer */
    if (strlen(name) > len) {
        return NULL;
    }
    return name;
}

L
leon 已提交
592 593 594 595 596 597 598 599 600 601
static void FindCharEnd(const char *parent)
{
    while (*++parent) {
        if (*parent == '/') {
            return;
        }
    }
    return;
}

Z
zhong_ning 已提交
602 603 604 605 606 607 608
static char **GetCharacterDeviceSymlinks(const struct Uevent *uevent)
{
    char *slash = NULL;
    int linkNum = 0;
    int width;

    struct PlatformNode *pDev = FindPlatformDevice(uevent->path);
L
leon 已提交
609 610 611 612 613
    if (!pDev) {
        return NULL;
    }
    char **links = calloc(sizeof(char *), SYS_LINK_NUMBER);
    if (!links) {
Z
zhong_ning 已提交
614 615 616 617 618 619 620 621 622
        return NULL;
    }

    /* skip "/devices/platform/<driver>" */
    const char *parent = strchr(uevent->path + pDev->pathLen, '/');
    if (!*parent) {
        goto err;
    }

L
leon 已提交
623
    if (!strncmp(parent, "/usb", DEV_USB)) {
Z
zhong_ning 已提交
624
       /* skip root hub name and device. use device interface */
L
leon 已提交
625
        FindCharEnd(parent);
Z
zhong_ning 已提交
626
        if (*parent) {
L
leon 已提交
627
            FindCharEnd(parent);
Z
zhong_ning 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
        }
        if (!*parent) {
            goto err;
        }
        slash = strchr(++parent, '/');
        if (!slash) {
            goto err;
        }
        width = slash - parent;
        if (width <= 0) {
            goto err;
        }

        if (asprintf(&links[linkNum], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) {
            linkNum++;
        } else {
            links[linkNum] = NULL;
        }
L
leon 已提交
646
        mkdir("/dev/usb", DEFAULT_DIR_MODE);
Z
zhong_ning 已提交
647 648 649 650 651 652 653 654 655
    } else {
        goto err;
    }
    return links;
err:
    free(links);
    return NULL;
}

L
leon 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
static int HandleUsbDevice(const struct Uevent *event, char *devpath, int len)
{
    if (event->deviceName) {
        /*
         * create device node provided by kernel if present
         * see drivers/base/core.c
         */
        char *p = devpath;
        if (snprintf_s(devpath, len, len, "/dev/%s", event->deviceName) == -1) {
            return -1;
        }
        /* skip leading /dev/ */
        p += DEVICE_SKIP;
        /* build directories */
        while (*p) {
            if (*p == '/') {
                *p = 0;
                MakeDir(devpath, DEFAULT_DIR_MODE);
                *p = '/';
            }
            p++;
        }
    } else {
        /* This imitates the file system that would be created
         * if we were using devfs instead.
         * Minors are broken up into groups of 128, starting at "001"
         */
        int busId  = event->minor / MINORS_GROUPS + 1;
        int deviceId = event->minor % MINORS_GROUPS + 1;
        /* build directories */
        MakeDir("/dev/bus", DEFAULT_DIR_MODE);
        MakeDir("/dev/bus/usb", DEFAULT_DIR_MODE);
        if (snprintf_s(devpath, len, len, "/dev/bus/usb/%03d", busId) == -1) {
            return -1;
        }
        MakeDir(devpath, DEFAULT_DIR_MODE);
        if (snprintf_s(devpath, len, len, "/dev/bus/usb/%03d/%03d", busId,
            deviceId) == -1) {
            return -1;
        }
    }
    return 0;
}

static void HandleDeviceEvent(struct Uevent *event, char *devpath, int len, const char *base, const char *name)
{
    char **links = NULL;
    links = GetCharacterDeviceSymlinks(event);
    if (!devpath[0]) {
        if (snprintf_s(devpath, len, len, "%s%s", base, name) == -1) {
            printf("[Init] snprintf_s err \n");
            return;
        }
    }
    HandleDevice(event, devpath, 0, links);
    return;
}
Z
zhong_ning 已提交
713 714 715
static void HandleGenericDevice(struct Uevent *event)
{
    char *base = NULL;
L
leon 已提交
716 717
    char devpath[MAX_DEV_PATH] = {0};
    const char *name = ParseDeviceName(event, MAX_DEVICE_LEN);
Z
zhong_ning 已提交
718 719 720
    if (!name) {
        return;
    }
L
leon 已提交
721
    if (!strncmp(event->subsystem, "usb", HANDLE_DEVICE_USB)) {
Z
zhong_ning 已提交
722
        if (!strcmp(event->subsystem, "usb")) {
L
leon 已提交
723 724
            if (HandleUsbDevice(event, devpath, MAX_DEV_PATH) == -1) {
                return;
Z
zhong_ning 已提交
725 726 727 728 729
            }
        } else {
            /* ignore other USB events */
            return;
        }
L
leon 已提交
730
    } else if (!strncmp(event->subsystem, "graphics", DEV_GRAPHICS)) {
Z
zhong_ning 已提交
731
        base = "/dev/graphics/";
L
leon 已提交
732 733
        MakeDir(base, DEFAULT_DIR_MODE);
    } else if (!strncmp(event->subsystem, "drm", DEV_DRM)) {
Z
zhong_ning 已提交
734
        base = "/dev/dri/";
L
leon 已提交
735 736
        MakeDir(base, DEFAULT_DIR_MODE);
    } else if (!strncmp(event->subsystem, "oncrpc", DEV_ONCRPC)) {
Z
zhong_ning 已提交
737
        base = "/dev/oncrpc/";
L
leon 已提交
738 739
        MakeDir(base, DEFAULT_DIR_MODE);
    } else if (!strncmp(event->subsystem, "adsp", DEV_ADSP)) {
Z
zhong_ning 已提交
740
        base = "/dev/adsp/";
L
leon 已提交
741 742
        MakeDir(base, DEFAULT_DIR_MODE);
    } else if (!strncmp(event->subsystem, "input", DEV_INPUT)) {
Z
zhong_ning 已提交
743
        base = "/dev/input/";
L
leon 已提交
744 745
        MakeDir(base, DEFAULT_DIR_MODE);
    } else if (!strncmp(event->subsystem, "mtd", DEV_MTD)) {
Z
zhong_ning 已提交
746
        base = "/dev/mtd/";
L
leon 已提交
747 748
        MakeDir(base, DEFAULT_DIR_MODE);
    } else if (!strncmp(event->subsystem, "sound", DEV_SOUND)) {
Z
zhong_ning 已提交
749
        base = "/dev/snd/";
L
leon 已提交
750
        MakeDir(base, DEFAULT_DIR_MODE);
Z
zhong_ning 已提交
751 752 753
    } else {
        base = "/dev/";
    }
L
leon 已提交
754 755
    HandleDeviceEvent(event, devpath, MAX_DEV_PATH, base, name);
    return;
Z
zhong_ning 已提交
756 757 758 759 760 761 762
}

static void HandleDeviceUevent(struct Uevent *event)
{
    if (strcmp(event->action, "add") == 0 || strcmp(event->action, "change") == 0) {
        /* Do nothing for now */
    }
L
leon 已提交
763
    if (strncmp(event->subsystem, "block", EVENT_BLOCK) == 0) {
Z
zhong_ning 已提交
764
        HandleBlockDevice(event);
L
leon 已提交
765
    } else if (strncmp(event->subsystem, "platform", EVENT_PLAT_FORM) == 0) {
Z
zhong_ning 已提交
766 767 768 769 770 771 772 773
        HandlePlatformDevice(event);
    } else {
        HandleGenericDevice(event);
    }
}

static void HandleUevent()
{
L
leon 已提交
774
    char buf[EVENT_MAX_BUFFER];
Z
zhong_ning 已提交
775 776
    int ret;
    struct Uevent event;
L
leon 已提交
777 778
    while ((ret = ReadUevent(g_ueventFD, buf, BASE_BUFFER_SIZE)) > 0) {
        if (ret >= BASE_BUFFER_SIZE) {
Z
zhong_ning 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
            continue;
        }
        buf[ret] = '\0';
        buf[ret + 1] = '\0';
        ParseUevent(buf, &event);
        HandleDeviceUevent(&event);
    }
}

void UeventInit()
{
    struct pollfd ufd;
    UeventSockInit();
    ufd.events = POLLIN;
    ufd.fd = UeventFD();
    while (1) {
        ufd.revents = 0;
        int ret = poll(&ufd, 1, -1);
        if (ret <= 0) {
            continue;
        }
        if (ufd.revents & POLLIN) {
            HandleUevent();
        }
    }
    return;
}

L
leon 已提交
807
int main(const int argc, const char **argv)
Z
zhong_ning 已提交
808 809 810 811 812
{
    printf("Uevent demo starting...\n");
    UeventInit();
    return 0;
}