blob: b5069a6c2074f427e77f7e701a17dab08710e519 [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
67 if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
Nick Kralevich3b20c392013-06-28 19:08:35 -070068 SLOGE("Unable to set uevent socket SO_RCVBUFFORCE option: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080069 goto out;
Nick Kralevichc51920c2011-04-18 15:51:19 -070070 }
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 Ju15bd71f2012-04-14 10:48:45 +080074 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070075 }
76
77 if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070078 SLOGE("Unable to bind uevent socket: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080079 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070080 }
81
82 mHandler = new NetlinkHandler(mSock);
83 if (mHandler->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070084 SLOGE("Unable to start NetlinkHandler: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080085 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070086 }
Kaspter Ju15bd71f2012-04-14 10:48:45 +080087
San Mehatf1b736b2009-10-10 17:22:08 -070088 return 0;
Kaspter Ju15bd71f2012-04-14 10:48:45 +080089
90out:
91 close(mSock);
92 return -1;
San Mehatf1b736b2009-10-10 17:22:08 -070093}
94
95int NetlinkManager::stop() {
Kaspter Ju15bd71f2012-04-14 10:48:45 +080096 int status = 0;
97
San Mehatf1b736b2009-10-10 17:22:08 -070098 if (mHandler->stop()) {
San Mehat97ac40e2010-03-24 10:24:19 -070099 SLOGE("Unable to stop NetlinkHandler: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800100 status = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700101 }
102 delete mHandler;
103 mHandler = NULL;
104
105 close(mSock);
106 mSock = -1;
107
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800108 return status;
San Mehatf1b736b2009-10-10 17:22:08 -0700109}