blob: 90e3c6cf9cde0aa1eee792ccfa6b73b261f388a4 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Hughes38a73c62015-01-28 11:47:21 -080019#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020
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
36NetlinkManager *NetlinkManager::sInstance = NULL;
37
38NetlinkManager *NetlinkManager::Instance() {
39 if (!sInstance)
40 sInstance = new NetlinkManager();
41 return sInstance;
42}
43
44NetlinkManager::NetlinkManager() {
45 mBroadcaster = NULL;
46}
47
48NetlinkManager::~NetlinkManager() {
49}
50
51int NetlinkManager::start() {
52 struct sockaddr_nl nladdr;
53 int sz = 64 * 1024;
Nick Kralevichc51920c2011-04-18 15:51:19 -070054 int on = 1;
San Mehatf1b736b2009-10-10 17:22:08 -070055
56 memset(&nladdr, 0, sizeof(nladdr));
57 nladdr.nl_family = AF_NETLINK;
58 nladdr.nl_pid = getpid();
59 nladdr.nl_groups = 0xffffffff;
60
Jeff Sharkey36801cc2015-03-13 16:09:20 -070061 if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC,
62 NETLINK_KOBJECT_UEVENT)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070063 SLOGE("Unable to create uevent socket: %s", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070064 return -1;
65 }
66
Luis Hector Chavezd1b00de2017-08-29 10:29:48 -070067 // When running in a net/user namespace, SO_RCVBUFFORCE will fail because
68 // it will check for the CAP_NET_ADMIN capability in the root namespace.
69 // Try using SO_RCVBUF if that fails.
70 if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) &&
71 (setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0)) {
Junichi Uekawac865ada2015-11-05 06:05:54 +090072 SLOGE("Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080073 goto out;
Nick Kralevichc51920c2011-04-18 15:51:19 -070074 }
75
76 if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
77 SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080078 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070079 }
80
81 if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070082 SLOGE("Unable to bind uevent socket: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080083 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070084 }
85
86 mHandler = new NetlinkHandler(mSock);
87 if (mHandler->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070088 SLOGE("Unable to start NetlinkHandler: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080089 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070090 }
Kaspter Ju15bd71f2012-04-14 10:48:45 +080091
San Mehatf1b736b2009-10-10 17:22:08 -070092 return 0;
Kaspter Ju15bd71f2012-04-14 10:48:45 +080093
94out:
95 close(mSock);
96 return -1;
San Mehatf1b736b2009-10-10 17:22:08 -070097}
98
99int NetlinkManager::stop() {
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800100 int status = 0;
101
San Mehatf1b736b2009-10-10 17:22:08 -0700102 if (mHandler->stop()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700103 SLOGE("Unable to stop NetlinkHandler: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800104 status = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700105 }
106 delete mHandler;
107 mHandler = NULL;
108
109 close(mSock);
110 mSock = -1;
111
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800112 return status;
San Mehatf1b736b2009-10-10 17:22:08 -0700113}