2009-10-11 02:22:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <stdio.h>
|
2015-01-28 20:47:21 +01:00
|
|
|
#include <string.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
|
|
|
|
#include <sys/select.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <sys/socket.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
|
2017-10-07 02:02:53 +02:00
|
|
|
#include <android-base/logging.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
|
|
|
|
#include "NetlinkHandler.h"
|
2018-09-18 22:30:21 +02:00
|
|
|
#include "NetlinkManager.h"
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
NetlinkManager* NetlinkManager::sInstance = NULL;
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
NetlinkManager* NetlinkManager::Instance() {
|
|
|
|
if (!sInstance) sInstance = new NetlinkManager();
|
2009-10-11 02:22:08 +02:00
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetlinkManager::NetlinkManager() {
|
|
|
|
mBroadcaster = NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
NetlinkManager::~NetlinkManager() {}
|
2009-10-11 02:22:08 +02:00
|
|
|
|
|
|
|
int NetlinkManager::start() {
|
|
|
|
struct sockaddr_nl nladdr;
|
|
|
|
int sz = 64 * 1024;
|
2011-04-19 00:51:19 +02:00
|
|
|
int on = 1;
|
2009-10-11 02:22:08 +02:00
|
|
|
|
|
|
|
memset(&nladdr, 0, sizeof(nladdr));
|
|
|
|
nladdr.nl_family = AF_NETLINK;
|
|
|
|
nladdr.nl_pid = getpid();
|
|
|
|
nladdr.nl_groups = 0xffffffff;
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Unable to create uevent socket";
|
2009-10-11 02:22:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-29 19:29:48 +02:00
|
|
|
// When running in a net/user namespace, SO_RCVBUFFORCE will fail because
|
|
|
|
// it will check for the CAP_NET_ADMIN capability in the root namespace.
|
|
|
|
// Try using SO_RCVBUF if that fails.
|
|
|
|
if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) &&
|
|
|
|
(setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0)) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option";
|
2012-04-14 04:48:45 +02:00
|
|
|
goto out;
|
2011-04-19 00:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Unable to set uevent socket SO_PASSCRED option";
|
2012-04-14 04:48:45 +02:00
|
|
|
goto out;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
if (bind(mSock, (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Unable to bind uevent socket";
|
2012-04-14 04:48:45 +02:00
|
|
|
goto out;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mHandler = new NetlinkHandler(mSock);
|
|
|
|
if (mHandler->start()) {
|
2017-10-07 02:02:53 +02:00
|
|
|
PLOG(ERROR) << "Unable to start NetlinkHandler";
|
2012-04-14 04:48:45 +02:00
|
|
|
goto out;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
2012-04-14 04:48:45 +02:00
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
return 0;
|
2012-04-14 04:48:45 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
close(mSock);
|
|
|
|
return -1;
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|