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) {
68 int uevent_next_event(char* buffer, int buffer_length)
77 nr = poll(&fds, 1, -1);
79 if(nr > 0 && fds.revents == POLLIN) {
80 int count = recv(fd, buffer, buffer_length, 0);
82 struct uevent_handler *h;
83 pthread_mutex_lock(&uevent_handler_list_lock);
84 LIST_FOREACH(h, &uevent_handler_list, list)
85 h->handler(h->handler_data, buffer, buffer_length);
86 pthread_mutex_unlock(&uevent_handler_list_lock);
97 int uevent_add_native_handler(void (*handler)(void *data, const char *msg, int msg_len),
100 struct uevent_handler *h;
102 h = malloc(sizeof(struct uevent_handler));
105 h->handler = handler;
106 h->handler_data = handler_data;
108 pthread_mutex_lock(&uevent_handler_list_lock);
109 LIST_INSERT_HEAD(&uevent_handler_list, h, list);
110 pthread_mutex_unlock(&uevent_handler_list_lock);
115 int uevent_remove_native_handler(void (*handler)(void *data, const char *msg, int msg_len))
117 struct uevent_handler *h;
120 pthread_mutex_lock(&uevent_handler_list_lock);
121 LIST_FOREACH(h, &uevent_handler_list, list) {
122 if (h->handler == handler) {
123 LIST_REMOVE(h, list);
128 pthread_mutex_unlock(&uevent_handler_list_lock);