Home | History | Annotate | Download | only in rild
      1 /* //device/system/rild/rild.c
      2 **
      3 ** Copyright 2006 The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <dlfcn.h>
     21 #include <string.h>
     22 #include <stdint.h>
     23 #include <unistd.h>
     24 #include <fcntl.h>
     25 #include <errno.h>
     26 
     27 #include <telephony/ril.h>
     28 #define LOG_TAG "RILD"
     29 #include <utils/Log.h>
     30 #include <cutils/properties.h>
     31 #include <cutils/sockets.h>
     32 #include <sys/capability.h>
     33 #include <sys/prctl.h>
     34 #include <sys/stat.h>
     35 #include <sys/types.h>
     36 #include <libril/ril_ex.h>
     37 
     38 #include <private/android_filesystem_config.h>
     39 #include "hardware/qemu_pipe.h"
     40 
     41 #define LIB_PATH_PROPERTY   "rild.libpath"
     42 #define LIB_ARGS_PROPERTY   "rild.libargs"
     43 #define MAX_LIB_ARGS        16
     44 #define MAX_CAP_NUM         (CAP_TO_INDEX(CAP_LAST_CAP) + 1)
     45 
     46 static void usage(const char *argv0) {
     47     fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
     48     exit(EXIT_FAILURE);
     49 }
     50 
     51 extern char rild[MAX_SOCKET_NAME_LENGTH];
     52 
     53 extern void RIL_register (const RIL_RadioFunctions *callbacks);
     54 
     55 extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit)
     56         (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
     57 
     58 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
     59         void *response, size_t responselen);
     60 
     61 extern void RIL_onRequestAck(RIL_Token t);
     62 
     63 extern void RIL_setRilSocketName(char *);
     64 
     65 #if defined(ANDROID_MULTI_SIM)
     66 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
     67         size_t datalen, RIL_SOCKET_ID socket_id);
     68 #else
     69 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
     70         size_t datalen);
     71 #endif
     72 
     73 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
     74         void *param, const struct timeval *relativeTime);
     75 
     76 
     77 static struct RIL_Env s_rilEnv = {
     78     RIL_onRequestComplete,
     79     RIL_onUnsolicitedResponse,
     80     RIL_requestTimedCallback,
     81     RIL_onRequestAck
     82 };
     83 
     84 extern void RIL_startEventLoop();
     85 
     86 static int make_argv(char * args, char ** argv) {
     87     // Note: reserve argv[0]
     88     int count = 1;
     89     char * tok;
     90     char * s = args;
     91 
     92     while ((tok = strtok(s, " \0"))) {
     93         argv[count] = tok;
     94         s = NULL;
     95         count++;
     96     }
     97     return count;
     98 }
     99 
    100 /*
    101  * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
    102  * Our group, cache, was set by init.
    103  */
    104 void switchUser() {
    105     char debuggable[PROP_VALUE_MAX];
    106 
    107     prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
    108     if (setresuid(AID_RADIO, AID_RADIO, AID_RADIO) == -1) {
    109         RLOGE("setresuid failed: %s", strerror(errno));
    110         exit(EXIT_FAILURE);
    111     }
    112 
    113     struct __user_cap_header_struct header;
    114     memset(&header, 0, sizeof(header));
    115     header.version = _LINUX_CAPABILITY_VERSION_3;
    116     header.pid = 0;
    117 
    118     struct __user_cap_data_struct data[MAX_CAP_NUM];
    119     memset(&data, 0, sizeof(data));
    120 
    121     data[CAP_TO_INDEX(CAP_NET_ADMIN)].effective |= CAP_TO_MASK(CAP_NET_ADMIN);
    122     data[CAP_TO_INDEX(CAP_NET_ADMIN)].permitted |= CAP_TO_MASK(CAP_NET_ADMIN);
    123 
    124     data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
    125     data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
    126 
    127     data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].effective |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
    128     data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].permitted |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
    129 
    130     if (capset(&header, &data[0]) == -1) {
    131         RLOGE("capset failed: %s", strerror(errno));
    132         exit(EXIT_FAILURE);
    133     }
    134 
    135     /*
    136      * Debuggable build only:
    137      * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash
    138      */
    139     property_get("ro.debuggable", debuggable, "0");
    140     if (strcmp(debuggable, "1") == 0) {
    141         prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
    142     }
    143 }
    144 
    145 int main(int argc, char **argv) {
    146     const char * rilLibPath = NULL;
    147     char **rilArgv;
    148     void *dlHandle;
    149     const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
    150     RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
    151     const char *err_str = NULL;
    152 
    153     const RIL_RadioFunctions *funcs;
    154     char libPath[PROPERTY_VALUE_MAX];
    155     unsigned char hasLibArgs = 0;
    156 
    157     int i;
    158     const char *clientId = NULL;
    159     RLOGD("**RIL Daemon Started**");
    160     RLOGD("**RILd param count=%d**", argc);
    161 
    162     umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
    163     for (i = 1; i < argc ;) {
    164         if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
    165             rilLibPath = argv[i + 1];
    166             i += 2;
    167         } else if (0 == strcmp(argv[i], "--")) {
    168             i++;
    169             hasLibArgs = 1;
    170             break;
    171         } else if (0 == strcmp(argv[i], "-c") &&  (argc - i > 1)) {
    172             clientId = argv[i+1];
    173             i += 2;
    174         } else {
    175             usage(argv[0]);
    176         }
    177     }
    178 
    179     if (clientId == NULL) {
    180         clientId = "0";
    181     } else if (atoi(clientId) >= MAX_RILDS) {
    182         RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
    183         exit(0);
    184     }
    185     if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
    186         strlcat(rild, clientId, MAX_SOCKET_NAME_LENGTH);
    187         RIL_setRilSocketName(rild);
    188     }
    189 
    190     if (rilLibPath == NULL) {
    191         if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
    192             // No lib sepcified on the command line, and nothing set in props.
    193             // Assume "no-ril" case.
    194             goto done;
    195         } else {
    196             rilLibPath = libPath;
    197         }
    198     }
    199 
    200     /* special override when in the emulator */
    201 #if 1
    202     {
    203         static char*  arg_overrides[5];
    204         static char   arg_device[32];
    205         int           done = 0;
    206 
    207 #define  REFERENCE_RIL_PATH  "libreference-ril.so"
    208 
    209         /* first, read /proc/cmdline into memory */
    210         char          buffer[2048] = {'\0'}, *p, *q;
    211         int           len;
    212         struct stat   st;
    213         int           fd = open("/proc/cmdline",O_RDONLY);
    214 
    215         if (fd < 0) {
    216             RLOGE("could not open /proc/cmdline:%s", strerror(errno));
    217             goto OpenLib;
    218         }
    219 
    220         if (fstat(fd, &st)) {
    221             RLOGE("fstat error: %s", strerror(errno));
    222             close(fd);
    223             goto OpenLib;
    224         }
    225 
    226         if ((unsigned long)st.st_size > sizeof(buffer) - 1) {
    227             RLOGE("Size of /proc/cmdline exceeds buffer");
    228             close(fd);
    229             goto OpenLib;
    230         }
    231 
    232         do {
    233             len = read(fd,buffer,sizeof(buffer) - 1); }
    234         while (len == -1 && errno == EINTR);
    235 
    236         if (len < 0) {
    237             RLOGE("could not read /proc/cmdline:%s", strerror(errno));
    238             close(fd);
    239             goto OpenLib;
    240         }
    241         close(fd);
    242 
    243         if (strstr(buffer, "android.qemud=") != NULL)
    244         {
    245             /* the qemud daemon is launched after rild, so
    246             * give it some time to create its GSM socket
    247             */
    248             int  tries = 5;
    249 #define  QEMUD_SOCKET_NAME    "qemud"
    250 
    251             while (1) {
    252                 int  fd;
    253 
    254                 sleep(1);
    255 
    256                 fd = qemu_pipe_open("qemud:gsm");
    257                 if (fd < 0) {
    258                     fd = socket_local_client(
    259                                 QEMUD_SOCKET_NAME,
    260                                 ANDROID_SOCKET_NAMESPACE_RESERVED,
    261                                 SOCK_STREAM );
    262                 }
    263                 if (fd >= 0) {
    264                     close(fd);
    265                     snprintf( arg_device, sizeof(arg_device), "%s/%s",
    266                                 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
    267 
    268                     arg_overrides[1] = "-s";
    269                     arg_overrides[2] = arg_device;
    270                     done = 1;
    271                     break;
    272                 }
    273                 RLOGD("could not connect to %s socket: %s",
    274                     QEMUD_SOCKET_NAME, strerror(errno));
    275                 if (--tries == 0)
    276                     break;
    277             }
    278             if (!done) {
    279                 RLOGE("could not connect to %s socket (giving up): %s",
    280                     QEMUD_SOCKET_NAME, strerror(errno));
    281                 while(1)
    282                     sleep(0x00ffffff);
    283             }
    284         }
    285 
    286         /* otherwise, try to see if we passed a device name from the kernel */
    287         if (!done) do {
    288 #define  KERNEL_OPTION  "android.ril="
    289 #define  DEV_PREFIX     "/dev/"
    290 
    291             p = strstr( buffer, KERNEL_OPTION );
    292             if (p == NULL)
    293                 break;
    294 
    295             p += sizeof(KERNEL_OPTION)-1;
    296             q  = strpbrk( p, " \t\n\r" );
    297             if (q != NULL)
    298                 *q = 0;
    299 
    300             snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
    301             arg_device[sizeof(arg_device)-1] = 0;
    302             arg_overrides[1] = "-d";
    303             arg_overrides[2] = arg_device;
    304             done = 1;
    305 
    306         } while (0);
    307 
    308         if (done) {
    309             argv = arg_overrides;
    310             argc = 3;
    311             i    = 1;
    312             hasLibArgs = 1;
    313             rilLibPath = REFERENCE_RIL_PATH;
    314 
    315             RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
    316         }
    317     }
    318 OpenLib:
    319 #endif
    320     switchUser();
    321 
    322     dlHandle = dlopen(rilLibPath, RTLD_NOW);
    323 
    324     if (dlHandle == NULL) {
    325         RLOGE("dlopen failed: %s", dlerror());
    326         exit(EXIT_FAILURE);
    327     }
    328 
    329     RIL_startEventLoop();
    330 
    331     rilInit =
    332         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
    333         dlsym(dlHandle, "RIL_Init");
    334 
    335     if (rilInit == NULL) {
    336         RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
    337         exit(EXIT_FAILURE);
    338     }
    339 
    340     dlerror(); // Clear any previous dlerror
    341     rilUimInit =
    342         (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
    343         dlsym(dlHandle, "RIL_SAP_Init");
    344     err_str = dlerror();
    345     if (err_str) {
    346         RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
    347     } else if (!rilUimInit) {
    348         RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
    349     }
    350 
    351     if (hasLibArgs) {
    352         rilArgv = argv + i - 1;
    353         argc = argc -i + 1;
    354     } else {
    355         static char * newArgv[MAX_LIB_ARGS];
    356         static char args[PROPERTY_VALUE_MAX];
    357         rilArgv = newArgv;
    358         property_get(LIB_ARGS_PROPERTY, args, "");
    359         argc = make_argv(args, rilArgv);
    360     }
    361 
    362     rilArgv[argc++] = "-c";
    363     rilArgv[argc++] = clientId;
    364     RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
    365 
    366     // Make sure there's a reasonable argv[0]
    367     rilArgv[0] = argv[0];
    368 
    369     funcs = rilInit(&s_rilEnv, argc, rilArgv);
    370     RLOGD("RIL_Init rilInit completed");
    371 
    372     RIL_register(funcs);
    373 
    374     RLOGD("RIL_Init RIL_register completed");
    375 
    376     if (rilUimInit) {
    377         RLOGD("RIL_register_socket started");
    378         RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
    379     }
    380 
    381     RLOGD("RIL_register_socket completed");
    382 
    383 done:
    384 
    385     RLOGD("RIL_Init starting sleep loop");
    386     while (true) {
    387         sleep(UINT32_MAX);
    388     }
    389 }
    390