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