2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/select.h>
22 #include <sys/socket.h>
24 #include <sys/types.h>
27 #include <linux/netlink.h>
29 #include <android-base/logging.h>
31 #include "NetlinkHandler.h"
32 #include "NetlinkManager.h"
34 NetlinkManager* NetlinkManager::sInstance = NULL;
36 NetlinkManager* NetlinkManager::Instance() {
37 if (!sInstance) sInstance = new NetlinkManager();
41 NetlinkManager::NetlinkManager() {
45 NetlinkManager::~NetlinkManager() {}
47 int NetlinkManager::start() {
48 struct sockaddr_nl nladdr;
52 memset(&nladdr, 0, sizeof(nladdr));
53 nladdr.nl_family = AF_NETLINK;
54 nladdr.nl_pid = getpid();
55 nladdr.nl_groups = 0xffffffff;
57 if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) < 0) {
58 PLOG(ERROR) << "Unable to create uevent socket";
62 // When running in a net/user namespace, SO_RCVBUFFORCE will fail because
63 // it will check for the CAP_NET_ADMIN capability in the root namespace.
64 // Try using SO_RCVBUF if that fails.
65 if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) &&
66 (setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0)) {
67 PLOG(ERROR) << "Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option";
71 if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
72 PLOG(ERROR) << "Unable to set uevent socket SO_PASSCRED option";
76 if (bind(mSock, (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
77 PLOG(ERROR) << "Unable to bind uevent socket";
81 mHandler = new NetlinkHandler(mSock);
82 if (mHandler->start()) {
83 PLOG(ERROR) << "Unable to start NetlinkHandler";
94 int NetlinkManager::stop() {
97 if (mHandler->stop()) {
98 PLOG(ERROR) << "Unable to stop NetlinkHandler";