rild.c revision 7955c43727b86f559ade21de682035b9f9c4cc4b
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 40#define LIB_PATH_PROPERTY "rild.libpath" 41#define LIB_ARGS_PROPERTY "rild.libargs" 42#define MAX_LIB_ARGS 16 43#define MAX_CAP_NUM (CAP_TO_INDEX(CAP_LAST_CAP) + 1) 44 45static void usage(const char *argv0) { 46 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0); 47 exit(EXIT_FAILURE); 48} 49 50extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH]; 51extern char ril_service_name[MAX_SERVICE_NAME_LENGTH]; 52 53extern void RIL_register (const RIL_RadioFunctions *callbacks); 54extern void rilc_thread_pool (); 55 56extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit) 57 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv); 58 59extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e, 60 void *response, size_t responselen); 61 62extern void RIL_onRequestAck(RIL_Token t); 63 64extern void RIL_setServiceName(char *); 65 66#if defined(ANDROID_MULTI_SIM) 67extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, 68 size_t datalen, RIL_SOCKET_ID socket_id); 69#else 70extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, 71 size_t datalen); 72#endif 73 74extern void RIL_requestTimedCallback (RIL_TimedCallback callback, 75 void *param, const struct timeval *relativeTime); 76 77 78static struct RIL_Env s_rilEnv = { 79 RIL_onRequestComplete, 80 RIL_onUnsolicitedResponse, 81 RIL_requestTimedCallback, 82 RIL_onRequestAck 83}; 84 85extern void RIL_startEventLoop(); 86 87static int make_argv(char * args, char ** argv) { 88 // Note: reserve argv[0] 89 int count = 1; 90 char * tok; 91 char * s = args; 92 93 while ((tok = strtok(s, " \0"))) { 94 argv[count] = tok; 95 s = NULL; 96 count++; 97 } 98 return count; 99} 100 101/* 102 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities. 103 * Our group, cache, was set by init. 104 */ 105void switchUser() { 106 char debuggable[PROP_VALUE_MAX]; 107 108 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); 109 if (setresuid(AID_RADIO, AID_RADIO, AID_RADIO) == -1) { 110 RLOGE("setresuid failed: %s", strerror(errno)); 111 exit(EXIT_FAILURE); 112 } 113 114 struct __user_cap_header_struct header; 115 memset(&header, 0, sizeof(header)); 116 header.version = _LINUX_CAPABILITY_VERSION_3; 117 header.pid = 0; 118 119 struct __user_cap_data_struct data[MAX_CAP_NUM]; 120 memset(&data, 0, sizeof(data)); 121 122 data[CAP_TO_INDEX(CAP_NET_ADMIN)].effective |= CAP_TO_MASK(CAP_NET_ADMIN); 123 data[CAP_TO_INDEX(CAP_NET_ADMIN)].permitted |= CAP_TO_MASK(CAP_NET_ADMIN); 124 125 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW); 126 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW); 127 128 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].effective |= CAP_TO_MASK(CAP_BLOCK_SUSPEND); 129 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].permitted |= CAP_TO_MASK(CAP_BLOCK_SUSPEND); 130 131 if (capset(&header, &data[0]) == -1) { 132 RLOGE("capset failed: %s", strerror(errno)); 133 exit(EXIT_FAILURE); 134 } 135 136 /* 137 * Debuggable build only: 138 * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash 139 */ 140 property_get("ro.debuggable", debuggable, "0"); 141 if (strcmp(debuggable, "1") == 0) { 142 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); 143 } 144} 145 146int main(int argc, char **argv) { 147 // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property 148 const char * rilLibPath = NULL; 149 // ril arguments either passed in as -- parameter, or read from rild.libargs property 150 char **rilArgv; 151 // handle for vendor ril lib 152 void *dlHandle; 153 // Pointer to ril init function in vendor ril 154 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); 155 // Pointer to sap init function in vendor ril 156 RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **); 157 const char *err_str = NULL; 158 159 // functions returned by ril init function in vendor ril 160 const RIL_RadioFunctions *funcs; 161 // lib path from rild.libpath property (if it's read) 162 char libPath[PROPERTY_VALUE_MAX]; 163 // flat to indicate if -- parameters are present 164 unsigned char hasLibArgs = 0; 165 166 int i; 167 // ril/socket id received as -c parameter, otherwise set to 0 168 const char *clientId = NULL; 169 170 RLOGD("**RIL Daemon Started**"); 171 RLOGD("**RILd param count=%d**", argc); 172 173 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); 174 for (i = 1; i < argc ;) { 175 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { 176 rilLibPath = argv[i + 1]; 177 i += 2; 178 } else if (0 == strcmp(argv[i], "--")) { 179 i++; 180 hasLibArgs = 1; 181 break; 182 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) { 183 clientId = argv[i+1]; 184 i += 2; 185 } else { 186 usage(argv[0]); 187 } 188 } 189 190 if (clientId == NULL) { 191 clientId = "0"; 192 } else if (atoi(clientId) >= MAX_RILDS) { 193 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS); 194 exit(0); 195 } 196 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) { 197 strncpy(ril_service_name, ril_service_name_base, MAX_SERVICE_NAME_LENGTH); 198 strncat(ril_service_name, clientId, MAX_SERVICE_NAME_LENGTH); 199 RIL_setServiceName(ril_service_name); 200 } 201 202 if (rilLibPath == NULL) { 203 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { 204 // No lib sepcified on the command line, and nothing set in props. 205 // Assume "no-ril" case. 206 goto done; 207 } else { 208 rilLibPath = libPath; 209 } 210 } 211 212 switchUser(); 213 214 dlHandle = dlopen(rilLibPath, RTLD_NOW); 215 216 if (dlHandle == NULL) { 217 RLOGE("dlopen failed: %s", dlerror()); 218 exit(EXIT_FAILURE); 219 } 220 221 RIL_startEventLoop(); 222 223 rilInit = 224 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) 225 dlsym(dlHandle, "RIL_Init"); 226 227 if (rilInit == NULL) { 228 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); 229 exit(EXIT_FAILURE); 230 } 231 232 dlerror(); // Clear any previous dlerror 233 rilUimInit = 234 (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) 235 dlsym(dlHandle, "RIL_SAP_Init"); 236 err_str = dlerror(); 237 if (err_str) { 238 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str); 239 } else if (!rilUimInit) { 240 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath); 241 } 242 243 if (hasLibArgs) { 244 rilArgv = argv + i - 1; 245 argc = argc -i + 1; 246 } else { 247 static char * newArgv[MAX_LIB_ARGS]; 248 static char args[PROPERTY_VALUE_MAX]; 249 rilArgv = newArgv; 250 property_get(LIB_ARGS_PROPERTY, args, ""); 251 argc = make_argv(args, rilArgv); 252 } 253 254 rilArgv[argc++] = "-c"; 255 rilArgv[argc++] = clientId; 256 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); 257 258 // Make sure there's a reasonable argv[0] 259 rilArgv[0] = argv[0]; 260 261 funcs = rilInit(&s_rilEnv, argc, rilArgv); 262 RLOGD("RIL_Init rilInit completed"); 263 264 RIL_register(funcs); 265 266 RLOGD("RIL_Init RIL_register completed"); 267 268 if (rilUimInit) { 269 RLOGD("RIL_register_socket started"); 270 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv); 271 } 272 273 RLOGD("RIL_register_socket completed"); 274 275done: 276 277 rilc_thread_pool(); 278 279 RLOGD("RIL_Init starting sleep loop"); 280 while (true) { 281 sleep(UINT32_MAX); 282 } 283} 284