blob: 28640e9206d3cee93097746e76a1c9a15c5e38c1 [file] [log] [blame]
Tom Marshall55220ba2019-01-04 14:37:31 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (C) 2019 The LineageOS Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <errno.h>
19#include <stdio.h>
20#include <string.h>
21
22#include <sys/select.h>
23#include <sys/socket.h>
24#include <sys/time.h>
25#include <sys/types.h>
26#include <sys/un.h>
27
28#include <linux/netlink.h>
29
30#define LOG_TAG "Vold"
31
32#include <cutils/log.h>
33
34#include "NetlinkHandler.h"
35#include "NetlinkManager.h"
36
37NetlinkManager* NetlinkManager::sInstance = NULL;
38
39NetlinkManager* NetlinkManager::Instance() {
40 if (!sInstance) sInstance = new NetlinkManager();
41 return sInstance;
42}
43
44NetlinkManager::NetlinkManager() {
45 // Empty
46}
47
48NetlinkManager::~NetlinkManager() {}
49
50bool NetlinkManager::start() {
51 struct sockaddr_nl nladdr;
52 int sz = 64 * 1024;
53 int on = 1;
54
55 memset(&nladdr, 0, sizeof(nladdr));
56 nladdr.nl_family = AF_NETLINK;
57 nladdr.nl_pid = getpid();
58 nladdr.nl_groups = 0xffffffff;
59
60 if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) < 0) {
61 SLOGE("Unable to create uevent socket: %s", strerror(errno));
62 return false;
63 }
64
65 // When running in a net/user namespace, SO_RCVBUFFORCE is not available.
66 // Try using SO_RCVBUF first.
67 if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0) &&
68 (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0)) {
69 SLOGE("Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option: %s", strerror(errno));
70 goto out;
71 }
72
73 if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
74 SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno));
75 goto out;
76 }
77
78 if (bind(mSock, (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
79 SLOGE("Unable to bind uevent socket: %s", strerror(errno));
80 goto out;
81 }
82
83 mHandler = new NetlinkHandler(mSock);
84 if (!mHandler->start()) {
85 SLOGE("Unable to start NetlinkHandler: %s", strerror(errno));
86 goto out;
87 }
88
89 return true;
90
91out:
92 close(mSock);
93 return false;
94}
95
96void NetlinkManager::stop() {
97 mHandler->stop();
98 delete mHandler;
99 mHandler = NULL;
100
101 close(mSock);
102 mSock = -1;
103}