rild.c revision fee63df3d23280da60243e94f3ab084eeca1d38e
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 <libminijail.h> 37#include <libril/ril_ex.h> 38 39#include <private/android_filesystem_config.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 46static 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 51extern char rild[MAX_SOCKET_NAME_LENGTH]; 52 53extern void RIL_register (const RIL_RadioFunctions *callbacks); 54 55extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit) 56 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv); 57 58extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e, 59 void *response, size_t responselen); 60 61extern void RIL_onRequestAck(RIL_Token t); 62 63extern void RIL_setRilSocketName(char *); 64 65#if defined(ANDROID_MULTI_SIM) 66extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, 67 size_t datalen, RIL_SOCKET_ID socket_id); 68#else 69extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, 70 size_t datalen); 71#endif 72 73extern void RIL_requestTimedCallback (RIL_TimedCallback callback, 74 void *param, const struct timeval *relativeTime); 75 76 77static struct RIL_Env s_rilEnv = { 78 RIL_onRequestComplete, 79 RIL_onUnsolicitedResponse, 80 RIL_requestTimedCallback, 81 RIL_onRequestAck 82}; 83 84extern void RIL_startEventLoop(); 85 86static 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 */ 104void switchUser() { 105 char debuggable[PROP_VALUE_MAX]; 106 struct minijail *j = minijail_new(); 107 minijail_change_uid(j, AID_RADIO); 108 minijail_use_caps(j, CAP_MASK_LONG(CAP_BLOCK_SUSPEND) | 109 CAP_MASK_LONG(CAP_NET_ADMIN) | 110 CAP_MASK_LONG(CAP_NET_RAW)); 111 112 minijail_enter(j); 113 114 /* 115 * Debuggable build only: 116 * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash 117 */ 118 property_get("ro.debuggable", debuggable, "0"); 119 if (strcmp(debuggable, "1") == 0) { 120 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); 121 } 122} 123 124int main(int argc, char **argv) { 125 const char *rilLibPath = NULL; 126 char **rilArgv; 127 void *dlHandle; 128 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); 129 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **); 130 const char *err_str = NULL; 131 132 const RIL_RadioFunctions *funcs; 133 char libPath[PROPERTY_VALUE_MAX]; 134 unsigned char hasLibArgs = 0; 135 136 int i; 137 const char *clientId = NULL; 138 RLOGD("**RIL Daemon Started**"); 139 RLOGD("**RILd param count=%d**", argc); 140 141 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); 142 for (i = 1; i < argc ;) { 143 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { 144 rilLibPath = argv[i + 1]; 145 i += 2; 146 } else if (0 == strcmp(argv[i], "--")) { 147 i++; 148 hasLibArgs = 1; 149 break; 150 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) { 151 clientId = argv[i+1]; 152 i += 2; 153 } else { 154 usage(argv[0]); 155 } 156 } 157 158 if (clientId == NULL) { 159 clientId = "0"; 160 } else if (atoi(clientId) >= MAX_RILDS) { 161 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS); 162 exit(0); 163 } 164 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) { 165 strlcat(rild, clientId, MAX_SOCKET_NAME_LENGTH); 166 RIL_setRilSocketName(rild); 167 } 168 169 if (rilLibPath == NULL) { 170 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { 171 // No lib sepcified on the command line, and nothing set in props. 172 // Assume "no-ril" case. 173 goto done; 174 } else { 175 rilLibPath = libPath; 176 } 177 } 178 179 switchUser(); 180 181 dlHandle = dlopen(rilLibPath, RTLD_NOW); 182 183 if (dlHandle == NULL) { 184 RLOGE("dlopen failed: %s", dlerror()); 185 exit(EXIT_FAILURE); 186 } 187 188 RIL_startEventLoop(); 189 190 rilInit = 191 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) 192 dlsym(dlHandle, "RIL_Init"); 193 194 if (rilInit == NULL) { 195 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); 196 exit(EXIT_FAILURE); 197 } 198 199 dlerror(); // Clear any previous dlerror 200 rilUimInit = 201 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) 202 dlsym(dlHandle, "RIL_SAP_Init"); 203 err_str = dlerror(); 204 if (err_str) { 205 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str); 206 } else if (!rilUimInit) { 207 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath); 208 } 209 210 if (hasLibArgs) { 211 rilArgv = argv + i - 1; 212 argc = argc -i + 1; 213 } else { 214 static char * newArgv[MAX_LIB_ARGS]; 215 static char args[PROPERTY_VALUE_MAX]; 216 rilArgv = newArgv; 217 property_get(LIB_ARGS_PROPERTY, args, ""); 218 argc = make_argv(args, rilArgv); 219 } 220 221 rilArgv[argc++] = "-c"; 222 rilArgv[argc++] = (char*)clientId; 223 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); 224 225 // Make sure there's a reasonable argv[0] 226 rilArgv[0] = argv[0]; 227 228 funcs = rilInit(&s_rilEnv, argc, rilArgv); 229 RLOGD("RIL_Init rilInit completed"); 230 231 RIL_register(funcs); 232 233 RLOGD("RIL_Init RIL_register completed"); 234 235 if (rilUimInit) { 236 RLOGD("RIL_register_socket started"); 237 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv); 238 } 239 240 RLOGD("RIL_register_socket completed"); 241 242done: 243 244 RLOGD("RIL_Init starting sleep loop"); 245 while (true) { 246 sleep(UINT32_MAX); 247 } 248} 249