2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <hardware_legacy/uevent.h>
24 #include <sys/socket.h>
26 #include <sys/queue.h>
27 #include <linux/netlink.h>
30 LIST_HEAD(uevent_handler_head, uevent_handler) uevent_handler_list;
31 pthread_mutex_t uevent_handler_list_lock = PTHREAD_MUTEX_INITIALIZER;
33 struct uevent_handler {
34 void (*handler)(void *data, const char *msg, int msg_len);
36 LIST_ENTRY(uevent_handler) list;
41 /* Returns 0 on failure, 1 on success */
44 struct sockaddr_nl addr;
48 memset(&addr, 0, sizeof(addr));
49 addr.nl_family = AF_NETLINK;
50 addr.nl_pid = getpid();
51 addr.nl_groups = 0xffffffff;
53 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
57 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
59 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
73 int uevent_next_event(char* buffer, int buffer_length)
82 nr = poll(&fds, 1, -1);
84 if(nr > 0 && fds.revents == POLLIN) {
85 int count = recv(fd, buffer, buffer_length, 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);
102 int uevent_add_native_handler(void (*handler)(void *data, const char *msg, int msg_len),
105 struct uevent_handler *h;
107 h = malloc(sizeof(struct uevent_handler));
110 h->handler = handler;
111 h->handler_data = handler_data;
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);
120 int uevent_remove_native_handler(void (*handler)(void *data, const char *msg, int msg_len))
122 struct uevent_handler *h;
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);
133 pthread_mutex_unlock(&uevent_handler_list_lock);