NetlinkListener.cpp revision 17260b14682d4fe59dad3de2de8c9370e6ba9a71
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
29NetlinkListener::NetlinkListener(int socket, int format) :
30                            SocketListener(socket, false), mFormat(format) {
31}
32
33bool NetlinkListener::onDataAvailable(SocketClient *cli)
34{
35    int socket = cli->getSocket();
36    ssize_t count;
37
38    count = TEMP_FAILURE_RETRY(uevent_kernel_multicast_recv(socket, mBuffer, sizeof(mBuffer)));
39    if (count < 0) {
40        SLOGE("recvmsg failed (%s)", strerror(errno));
41        return false;
42    }
43
44    NetlinkEvent *evt = new NetlinkEvent();
45    if (!evt->decode(mBuffer, count, mFormat)) {
46        SLOGE("Error decoding NetlinkEvent");
47    } else {
48        onEvent(evt);
49    }
50
51    delete evt;
52    return true;
53}
54