blob: 9c447ca76f85c78d069895454370a4e6e284118f [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -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#include <errno.h>
17
18#include <sys/types.h>
19#include <sys/socket.h>
Nick Kralevich6bc08282011-04-18 15:55:59 -070020#include <linux/netlink.h>
San Mehat3d407292009-05-07 08:49:30 -070021#include <string.h>
San Mehat168415b2009-05-06 11:14:21 -070022
23#define LOG_TAG "NetlinkListener"
24#include <cutils/log.h>
Nick Kralevich531312e2011-04-29 16:49:22 -070025#include <cutils/uevent.h>
San Mehat168415b2009-05-06 11:14:21 -070026
San Mehat168415b2009-05-06 11:14:21 -070027#include <sysutils/NetlinkEvent.h>
28
Mike J. Chen2a566882011-06-23 15:04:32 -070029#if 1
30/* temporary version until we can get Motorola to update their
31 * ril.so. Their prebuilt ril.so is using this private class
32 * so changing the NetlinkListener() constructor breaks their ril.
33 */
34NetlinkListener::NetlinkListener(int socket) :
35 SocketListener(socket, false) {
36 mFormat = NETLINK_FORMAT_ASCII;
37}
38#endif
39
Mike J. Chenec16b9d2011-06-23 14:55:28 -070040NetlinkListener::NetlinkListener(int socket, int format) :
Mike J. Chen17260b12011-06-23 15:00:30 -070041 SocketListener(socket, false), mFormat(format) {
San Mehat168415b2009-05-06 11:14:21 -070042}
43
San Mehatfa644ff2009-05-08 11:15:53 -070044bool NetlinkListener::onDataAvailable(SocketClient *cli)
San Mehat168415b2009-05-06 11:14:21 -070045{
San Mehatfa644ff2009-05-08 11:15:53 -070046 int socket = cli->getSocket();
Nick Kralevich6bc08282011-04-18 15:55:59 -070047 ssize_t count;
Geremy Condrad98533a2012-03-29 15:44:06 -070048 uid_t uid = -1;
San Mehat168415b2009-05-06 11:14:21 -070049
Geremy Condrad98533a2012-03-29 15:44:06 -070050 count = TEMP_FAILURE_RETRY(uevent_kernel_multicast_uid_recv(
51 socket, mBuffer, sizeof(mBuffer), &uid));
David 'Digit' Turnerb59539d2011-01-17 02:04:37 +010052 if (count < 0) {
Geremy Condrad98533a2012-03-29 15:44:06 -070053 if (uid > 0)
54 LOG_EVENT_INT(65537, uid);
Nick Kralevich6bc08282011-04-18 15:55:59 -070055 SLOGE("recvmsg failed (%s)", strerror(errno));
56 return false;
57 }
58
San Mehat168415b2009-05-06 11:14:21 -070059 NetlinkEvent *evt = new NetlinkEvent();
Mike J. Chen17260b12011-06-23 15:00:30 -070060 if (!evt->decode(mBuffer, count, mFormat)) {
San Mehat7e8529a2010-03-25 09:31:42 -070061 SLOGE("Error decoding NetlinkEvent");
Mike J. Chenec16b9d2011-06-23 14:55:28 -070062 } else {
63 onEvent(evt);
San Mehat168415b2009-05-06 11:14:21 -070064 }
65
San Mehat168415b2009-05-06 11:14:21 -070066 delete evt;
67 return true;
68}