San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 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 | #include <errno.h> |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/socket.h> |
Nick Kralevich | 6bc0828 | 2011-04-18 15:55:59 -0700 | [diff] [blame] | 20 | #include <linux/netlink.h> |
San Mehat | 3d40729 | 2009-05-07 08:49:30 -0700 | [diff] [blame] | 21 | #include <string.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 22 | |
| 23 | #define LOG_TAG "NetlinkListener" |
| 24 | #include <cutils/log.h> |
Nick Kralevich | 531312e | 2011-04-29 16:49:22 -0700 | [diff] [blame] | 25 | #include <cutils/uevent.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 26 | |
| 27 | #include <sysutils/NetlinkListener.h> |
| 28 | #include <sysutils/NetlinkEvent.h> |
| 29 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame^] | 30 | NetlinkListener::NetlinkListener(int socket, int format) : |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 31 | SocketListener(socket, false) { |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame^] | 32 | mFormat = format; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 33 | } |
| 34 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 35 | bool NetlinkListener::onDataAvailable(SocketClient *cli) |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 36 | { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 37 | int socket = cli->getSocket(); |
Nick Kralevich | 6bc0828 | 2011-04-18 15:55:59 -0700 | [diff] [blame] | 38 | ssize_t count; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 39 | |
Nick Kralevich | 57de8b8 | 2011-05-11 14:58:24 -0700 | [diff] [blame] | 40 | count = TEMP_FAILURE_RETRY(uevent_kernel_multicast_recv(socket, mBuffer, sizeof(mBuffer))); |
David 'Digit' Turner | b59539d | 2011-01-17 02:04:37 +0100 | [diff] [blame] | 41 | if (count < 0) { |
Nick Kralevich | 6bc0828 | 2011-04-18 15:55:59 -0700 | [diff] [blame] | 42 | SLOGE("recvmsg failed (%s)", strerror(errno)); |
| 43 | return false; |
| 44 | } |
| 45 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 46 | NetlinkEvent *evt = new NetlinkEvent(); |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame^] | 47 | int err = evt->decode(mBuffer, count, mFormat); |
| 48 | |
| 49 | if (!err) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 50 | SLOGE("Error decoding NetlinkEvent"); |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame^] | 51 | } else { |
| 52 | onEvent(evt); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 53 | } |
| 54 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 55 | delete evt; |
| 56 | return true; |
| 57 | } |