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>
25 #include <sys/socket.h>
27 #include <sys/queue.h>
28 #include <linux/netlink.h>
31 LIST_HEAD(uevent_handler_head, uevent_handler) uevent_handler_list;
32 pthread_mutex_t uevent_handler_list_lock = PTHREAD_MUTEX_INITIALIZER;
34 struct uevent_handler {
35 void (*handler)(void *data, const char *msg, int msg_len);
37 LIST_ENTRY(uevent_handler) list;
42 /* Returns 0 on failure, 1 on success */
45 struct sockaddr_nl addr;
49 memset(&addr, 0, sizeof(addr));
50 addr.nl_family = AF_NETLINK;
51 addr.nl_pid = getpid();
52 addr.nl_groups = 0xffffffff;
54 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
58 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
60 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
74 int uevent_next_event(char* buffer, int buffer_length)
83 nr = poll(&fds, 1, -1);
85 if(nr > 0 && (fds.revents & POLLIN)) {
86 int count = recv(fd, buffer, buffer_length, 0);
88 struct uevent_handler *h;
89 pthread_mutex_lock(&uevent_handler_list_lock);
90 LIST_FOREACH(h, &uevent_handler_list, list)
91 h->handler(h->handler_data, buffer, buffer_length);
92 pthread_mutex_unlock(&uevent_handler_list_lock);
103 int uevent_add_native_handler(void (*handler)(void *data, const char *msg, int msg_len),
106 struct uevent_handler *h;
108 h = malloc(sizeof(struct uevent_handler));
111 h->handler = handler;
112 h->handler_data = handler_data;
114 pthread_mutex_lock(&uevent_handler_list_lock);
115 LIST_INSERT_HEAD(&uevent_handler_list, h, list);
116 pthread_mutex_unlock(&uevent_handler_list_lock);
121 int uevent_remove_native_handler(void (*handler)(void *data, const char *msg, int msg_len))
123 struct uevent_handler *h;
126 pthread_mutex_lock(&uevent_handler_list_lock);
127 LIST_FOREACH(h, &uevent_handler_list, list) {
128 if (h->handler == handler) {
129 LIST_REMOVE(h, list);
134 pthread_mutex_unlock(&uevent_handler_list_lock);