Home | History | Annotate | Download | only in src
      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 
     17 #define LOG_TAG "NetlinkListener"
     18 
     19 #include <errno.h>
     20 #include <string.h>
     21 #include <sys/socket.h>
     22 #include <sys/types.h>
     23 #include <unistd.h>
     24 
     25 #include <linux/netlink.h> /* out of order because must follow sys/socket.h */
     26 
     27 #include <cutils/uevent.h>
     28 #include <log/log.h>
     29 #include <sysutils/NetlinkEvent.h>
     30 
     31 #if 1
     32 /* temporary version until we can get Motorola to update their
     33  * ril.so.  Their prebuilt ril.so is using this private class
     34  * so changing the NetlinkListener() constructor breaks their ril.
     35  */
     36 NetlinkListener::NetlinkListener(int socket) :
     37                             SocketListener(socket, false) {
     38     mFormat = NETLINK_FORMAT_ASCII;
     39 }
     40 #endif
     41 
     42 NetlinkListener::NetlinkListener(int socket, int format) :
     43                             SocketListener(socket, false), mFormat(format) {
     44 }
     45 
     46 bool NetlinkListener::onDataAvailable(SocketClient *cli)
     47 {
     48     int socket = cli->getSocket();
     49     ssize_t count;
     50     uid_t uid = -1;
     51 
     52     bool require_group = true;
     53     if (mFormat == NETLINK_FORMAT_BINARY_UNICAST) {
     54         require_group = false;
     55     }
     56 
     57     count = TEMP_FAILURE_RETRY(uevent_kernel_recv(socket,
     58             mBuffer, sizeof(mBuffer), require_group, &uid));
     59     if (count < 0) {
     60         SLOGE("recvmsg failed (%s)", strerror(errno));
     61         return false;
     62     }
     63 
     64     NetlinkEvent *evt = new NetlinkEvent();
     65     if (evt->decode(mBuffer, count, mFormat)) {
     66         onEvent(evt);
     67     } else if (mFormat != NETLINK_FORMAT_BINARY) {
     68         // Don't complain if parseBinaryNetlinkMessage returns false. That can
     69         // just mean that the buffer contained no messages we're interested in.
     70         SLOGE("Error decoding NetlinkEvent");
     71     }
     72 
     73     delete evt;
     74     return true;
     75 }
     76