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#include <errno.h>
17
18#include <sys/types.h>
19#include <sys/socket.h>
20#include <linux/netlink.h>
21#include <string.h>
22
23#define LOG_TAG "NetlinkListener"
24#include <cutils/log.h>
25#include <cutils/uevent.h>
26
27#include <sysutils/NetlinkEvent.h>
28
29#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
40NetlinkListener::NetlinkListener(int socket, int format) :
41                            SocketListener(socket, false), mFormat(format) {
42}
43
44bool NetlinkListener::onDataAvailable(SocketClient *cli)
45{
46    int socket = cli->getSocket();
47    ssize_t count;
48    uid_t uid = -1;
49
50    count = TEMP_FAILURE_RETRY(uevent_kernel_multicast_uid_recv(
51                                       socket, mBuffer, sizeof(mBuffer), &uid));
52    if (count < 0) {
53        if (uid > 0)
54            LOG_EVENT_INT(65537, uid);
55        SLOGE("recvmsg failed (%s)", strerror(errno));
56        return false;
57    }
58
59    NetlinkEvent *evt = new NetlinkEvent();
60    if (!evt->decode(mBuffer, count, mFormat)) {
61        SLOGE("Error decoding NetlinkEvent");
62    } else {
63        onEvent(evt);
64    }
65
66    delete evt;
67    return true;
68}
69