NetlinkManager.cpp revision e6ab884a82964831ec075fa48d6935cc847e31b6
1/* 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> 19 20#include <sys/socket.h> 21#include <sys/select.h> 22#include <sys/time.h> 23#include <sys/types.h> 24#include <sys/un.h> 25 26#include <linux/netlink.h> 27 28#define LOG_TAG "Netd" 29 30#include <cutils/log.h> 31 32#include "NetlinkManager.h" 33#include "NetlinkHandler.h" 34 35NetlinkManager *NetlinkManager::sInstance = NULL; 36 37NetlinkManager *NetlinkManager::Instance() { 38 if (!sInstance) 39 sInstance = new NetlinkManager(); 40 return sInstance; 41} 42 43NetlinkManager::NetlinkManager() { 44 mBroadcaster = NULL; 45} 46 47NetlinkManager::~NetlinkManager() { 48} 49 50int 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, 61 SOCK_DGRAM,NETLINK_KOBJECT_UEVENT)) < 0) { 62 LOGE("Unable to create uevent socket: %s", strerror(errno)); 63 return -1; 64 } 65 66 if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0 || 67 setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { 68 LOGE("Unable to set uevent socket options: %s", strerror(errno)); 69 return -1; 70 } 71 72 if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) { 73 LOGE("Unable to bind uevent socket: %s", strerror(errno)); 74 return -1; 75 } 76 77 mHandler = new NetlinkHandler(this, mSock); 78 if (mHandler->start()) { 79 LOGE("Unable to start NetlinkHandler: %s", strerror(errno)); 80 return -1; 81 } 82 return 0; 83} 84 85int NetlinkManager::stop() { 86 if (mHandler->stop()) { 87 LOGE("Unable to stop NetlinkHandler: %s", strerror(errno)); 88 return -1; 89 } 90 delete mHandler; 91 mHandler = NULL; 92 93 close(mSock); 94 mSock = -1; 95 96 return 0; 97} 98