Home | History | Annotate | Download | only in uevent
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <hardware_legacy/uevent.h>
     18 
     19 #include <string.h>
     20 #include <unistd.h>
     21 #include <poll.h>
     22 #include <pthread.h>
     23 
     24 #include <sys/socket.h>
     25 #include <sys/un.h>
     26 #include <sys/queue.h>
     27 #include <linux/netlink.h>
     28 
     29 
     30 LIST_HEAD(uevent_handler_head, uevent_handler) uevent_handler_list;
     31 pthread_mutex_t uevent_handler_list_lock = PTHREAD_MUTEX_INITIALIZER;
     32 
     33 struct uevent_handler {
     34     void (*handler)(void *data, const char *msg, int msg_len);
     35     void *handler_data;
     36     LIST_ENTRY(uevent_handler) list;
     37 };
     38 
     39 static int fd = -1;
     40 
     41 /* Returns 0 on failure, 1 on success */
     42 int uevent_init()
     43 {
     44     struct sockaddr_nl addr;
     45     int sz = 64*1024;
     46     int s;
     47 
     48     memset(&addr, 0, sizeof(addr));
     49     addr.nl_family = AF_NETLINK;
     50     addr.nl_pid = getpid();
     51     addr.nl_groups = 0xffffffff;
     52 
     53     s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
     54     if(s < 0)
     55         return 0;
     56 
     57     setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
     58 
     59     if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
     60         close(s);
     61         return 0;
     62     }
     63 
     64     fd = s;
     65     return (fd > 0);
     66 }
     67 
     68 int uevent_get_fd()
     69 {
     70     return fd;
     71 }
     72 
     73 int uevent_next_event(char* buffer, int buffer_length)
     74 {
     75     while (1) {
     76         struct pollfd fds;
     77         int nr;
     78 
     79         fds.fd = fd;
     80         fds.events = POLLIN;
     81         fds.revents = 0;
     82         nr = poll(&fds, 1, -1);
     83 
     84         if(nr > 0 && (fds.revents & POLLIN)) {
     85             int count = recv(fd, buffer, buffer_length, 0);
     86             if (count > 0) {
     87                 struct uevent_handler *h;
     88                 pthread_mutex_lock(&uevent_handler_list_lock);
     89                 LIST_FOREACH(h, &uevent_handler_list, list)
     90                     h->handler(h->handler_data, buffer, buffer_length);
     91                 pthread_mutex_unlock(&uevent_handler_list_lock);
     92 
     93                 return count;
     94             }
     95         }
     96     }
     97 
     98     // won't get here
     99     return 0;
    100 }
    101 
    102 int uevent_add_native_handler(void (*handler)(void *data, const char *msg, int msg_len),
    103                              void *handler_data)
    104 {
    105     struct uevent_handler *h;
    106 
    107     h = malloc(sizeof(struct uevent_handler));
    108     if (h == NULL)
    109         return -1;
    110     h->handler = handler;
    111     h->handler_data = handler_data;
    112 
    113     pthread_mutex_lock(&uevent_handler_list_lock);
    114     LIST_INSERT_HEAD(&uevent_handler_list, h, list);
    115     pthread_mutex_unlock(&uevent_handler_list_lock);
    116 
    117     return 0;
    118 }
    119 
    120 int uevent_remove_native_handler(void (*handler)(void *data, const char *msg, int msg_len))
    121 {
    122     struct uevent_handler *h;
    123     int err = -1;
    124 
    125     pthread_mutex_lock(&uevent_handler_list_lock);
    126     LIST_FOREACH(h, &uevent_handler_list, list) {
    127         if (h->handler == handler) {
    128             LIST_REMOVE(h, list);
    129             err = 0;
    130             break;
    131        }
    132     }
    133     pthread_mutex_unlock(&uevent_handler_list_lock);
    134 
    135     return err;
    136 }
    137