blob: dbba303ae56070df83a2b01307d943b815c69911 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 "Vold"
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;
Nick Kralevichc51920c2011-04-18 15:51:19 -070053 int on = 1;
San Mehatf1b736b2009-10-10 17:22:08 -070054
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) {
San Mehat97ac40e2010-03-24 10:24:19 -070062 SLOGE("Unable to create uevent socket: %s", strerror(errno));
San Mehatf1b736b2009-10-10 17:22:08 -070063 return -1;
64 }
65
66 if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
Nick Kralevich3b20c392013-06-28 19:08:35 -070067 SLOGE("Unable to set uevent socket SO_RCVBUFFORCE option: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080068 goto out;
Nick Kralevichc51920c2011-04-18 15:51:19 -070069 }
70
71 if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
72 SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080073 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070074 }
75
76 if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070077 SLOGE("Unable to bind uevent socket: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080078 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070079 }
80
81 mHandler = new NetlinkHandler(mSock);
82 if (mHandler->start()) {
San Mehat97ac40e2010-03-24 10:24:19 -070083 SLOGE("Unable to start NetlinkHandler: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080084 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070085 }
Kaspter Ju15bd71f2012-04-14 10:48:45 +080086
San Mehatf1b736b2009-10-10 17:22:08 -070087 return 0;
Kaspter Ju15bd71f2012-04-14 10:48:45 +080088
89out:
90 close(mSock);
91 return -1;
San Mehatf1b736b2009-10-10 17:22:08 -070092}
93
94int NetlinkManager::stop() {
Kaspter Ju15bd71f2012-04-14 10:48:45 +080095 int status = 0;
96
San Mehatf1b736b2009-10-10 17:22:08 -070097 if (mHandler->stop()) {
San Mehat97ac40e2010-03-24 10:24:19 -070098 SLOGE("Unable to stop NetlinkHandler: %s", strerror(errno));
Kaspter Ju15bd71f2012-04-14 10:48:45 +080099 status = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700100 }
101 delete mHandler;
102 mHandler = NULL;
103
104 close(mSock);
105 mSock = -1;
106
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800107 return status;
San Mehatf1b736b2009-10-10 17:22:08 -0700108}