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 23 #include <sys/socket.h> 24 #include <sys/un.h> 25 #include <linux/netlink.h> 26 27 28 static int fd = -1; 29 30 /* Returns 0 on failure, 1 on success */ 31 int uevent_init() 32 { 33 struct sockaddr_nl addr; 34 int sz = 64*1024; 35 int s; 36 37 memset(&addr, 0, sizeof(addr)); 38 addr.nl_family = AF_NETLINK; 39 addr.nl_pid = getpid(); 40 addr.nl_groups = 0xffffffff; 41 42 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); 43 if(s < 0) 44 return 0; 45 46 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)); 47 48 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 49 close(s); 50 return 0; 51 } 52 53 fd = s; 54 return (fd > 0); 55 } 56 57 int uevent_next_event(char* buffer, int buffer_length) 58 { 59 while (1) { 60 struct pollfd fds; 61 int nr; 62 63 fds.fd = fd; 64 fds.events = POLLIN; 65 fds.revents = 0; 66 nr = poll(&fds, 1, -1); 67 68 if(nr > 0 && fds.revents == POLLIN) { 69 int count = recv(fd, buffer, buffer_length, 0); 70 if (count > 0) { 71 return count; 72 } 73 } 74 } 75 76 // won't get here 77 return 0; 78 } 79