San Mehat | f1b736b | 2009-10-10 17:22:08 -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 | |
| 17 | #include <stdio.h> |
| 18 | #include <errno.h> |
Elliott Hughes | 38a73c6 | 2015-01-28 11:47:21 -0800 | [diff] [blame] | 19 | #include <string.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 20 | |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/select.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/un.h> |
| 26 | |
| 27 | #include <linux/netlink.h> |
| 28 | |
| 29 | #define LOG_TAG "Vold" |
| 30 | |
| 31 | #include <cutils/log.h> |
| 32 | |
| 33 | #include "NetlinkManager.h" |
| 34 | #include "NetlinkHandler.h" |
| 35 | |
| 36 | NetlinkManager *NetlinkManager::sInstance = NULL; |
| 37 | |
| 38 | NetlinkManager *NetlinkManager::Instance() { |
| 39 | if (!sInstance) |
| 40 | sInstance = new NetlinkManager(); |
| 41 | return sInstance; |
| 42 | } |
| 43 | |
| 44 | NetlinkManager::NetlinkManager() { |
| 45 | mBroadcaster = NULL; |
| 46 | } |
| 47 | |
| 48 | NetlinkManager::~NetlinkManager() { |
| 49 | } |
| 50 | |
| 51 | int NetlinkManager::start() { |
| 52 | struct sockaddr_nl nladdr; |
| 53 | int sz = 64 * 1024; |
Nick Kralevich | c51920c | 2011-04-18 15:51:19 -0700 | [diff] [blame] | 54 | int on = 1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 55 | |
| 56 | memset(&nladdr, 0, sizeof(nladdr)); |
| 57 | nladdr.nl_family = AF_NETLINK; |
| 58 | nladdr.nl_pid = getpid(); |
| 59 | nladdr.nl_groups = 0xffffffff; |
| 60 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 61 | if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, |
| 62 | NETLINK_KOBJECT_UEVENT)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 63 | SLOGE("Unable to create uevent socket: %s", strerror(errno)); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) { |
Nick Kralevich | 3b20c39 | 2013-06-28 19:08:35 -0700 | [diff] [blame] | 68 | SLOGE("Unable to set uevent socket SO_RCVBUFFORCE option: %s", strerror(errno)); |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 69 | goto out; |
Nick Kralevich | c51920c | 2011-04-18 15:51:19 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { |
| 73 | SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno)); |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 74 | goto out; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 78 | SLOGE("Unable to bind uevent socket: %s", strerror(errno)); |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 79 | goto out; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | mHandler = new NetlinkHandler(mSock); |
| 83 | if (mHandler->start()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 84 | SLOGE("Unable to start NetlinkHandler: %s", strerror(errno)); |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 85 | goto out; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 86 | } |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 87 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 88 | return 0; |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 89 | |
| 90 | out: |
| 91 | close(mSock); |
| 92 | return -1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int NetlinkManager::stop() { |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 96 | int status = 0; |
| 97 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 98 | if (mHandler->stop()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 99 | SLOGE("Unable to stop NetlinkHandler: %s", strerror(errno)); |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 100 | status = -1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 101 | } |
| 102 | delete mHandler; |
| 103 | mHandler = NULL; |
| 104 | |
| 105 | close(mSock); |
| 106 | mSock = -1; |
| 107 | |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 108 | return status; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 109 | } |