OSDN Git Service

am 14e36261: am 831ad524: Fix issue 2641884: Bluetooth volume is dependent on in...
[android-x86/hardware-libhardware_legacy.git] / uevent / uevent.c
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_next_event(char* buffer, int buffer_length)
69 {
70     while (1) {
71         struct pollfd fds;
72         int nr;
73     
74         fds.fd = fd;
75         fds.events = POLLIN;
76         fds.revents = 0;
77         nr = poll(&fds, 1, -1);
78      
79         if(nr > 0 && fds.revents == POLLIN) {
80             int count = recv(fd, buffer, buffer_length, 0);
81             if (count > 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);
87
88                 return count;
89             } 
90         }
91     }
92     
93     // won't get here
94     return 0;
95 }
96
97 int uevent_add_native_handler(void (*handler)(void *data, const char *msg, int msg_len),
98                              void *handler_data)
99 {
100     struct uevent_handler *h;
101
102     h = malloc(sizeof(struct uevent_handler));
103     if (h == NULL)
104         return -1;
105     h->handler = handler;
106     h->handler_data = handler_data;
107
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);
111
112     return 0;
113 }
114
115 int uevent_remove_native_handler(void (*handler)(void *data, const char *msg, int msg_len))
116 {
117     struct uevent_handler *h;
118     int err = -1;
119
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);
124             err = 0;
125             break;
126        }
127     }
128     pthread_mutex_unlock(&uevent_handler_list_lock);
129
130     return err;
131 }