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.
20 #include <sys/socket.h>
21 #include <sys/select.h>
23 #include <sys/types.h>
26 #include <linux/netlink.h>
28 #define LOG_TAG "Netd"
30 #include <cutils/log.h>
32 #include "NetlinkManager.h"
33 #include "NetlinkHandler.h"
35 NetlinkManager *NetlinkManager::sInstance = NULL;
37 NetlinkManager *NetlinkManager::Instance() {
39 sInstance = new NetlinkManager();
43 NetlinkManager::NetlinkManager() {
47 NetlinkManager::~NetlinkManager() {
50 int NetlinkManager::start() {
51 struct sockaddr_nl nladdr;
54 memset(&nladdr, 0, sizeof(nladdr));
55 nladdr.nl_family = AF_NETLINK;
56 nladdr.nl_pid = getpid();
57 nladdr.nl_groups = 0xffffffff;
59 if ((mSock = socket(PF_NETLINK,
60 SOCK_DGRAM,NETLINK_KOBJECT_UEVENT)) < 0) {
61 LOGE("Unable to create uevent socket: %s", strerror(errno));
65 if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
66 LOGE("Unable to set uevent socket options: %s", strerror(errno));
70 if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
71 LOGE("Unable to bind uevent socket: %s", strerror(errno));
75 mHandler = new NetlinkHandler(mSock);
76 if (mHandler->start()) {
77 LOGE("Unable to start NetlinkHandler: %s", strerror(errno));
83 int NetlinkManager::stop() {
84 if (mHandler->stop()) {
85 LOGE("Unable to stop NetlinkHandler: %s", strerror(errno));