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 <linux/capability.h> 33 #include <linux/prctl.h> 34 35 #include <private/android_filesystem_config.h> 36 37 #define LIB_PATH_PROPERTY "rild.libpath" 38 #define LIB_ARGS_PROPERTY "rild.libargs" 39 #define MAX_LIB_ARGS 16 40 41 static void usage(const char *argv0) 42 { 43 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0); 44 exit(-1); 45 } 46 47 extern void RIL_register (const RIL_RadioFunctions *callbacks); 48 49 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e, 50 void *response, size_t responselen); 51 52 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, 53 size_t datalen); 54 55 extern void RIL_requestTimedCallback (RIL_TimedCallback callback, 56 void *param, const struct timeval *relativeTime); 57 58 59 static struct RIL_Env s_rilEnv = { 60 RIL_onRequestComplete, 61 RIL_onUnsolicitedResponse, 62 RIL_requestTimedCallback 63 }; 64 65 extern void RIL_startEventLoop(); 66 67 static int make_argv(char * args, char ** argv) 68 { 69 // Note: reserve argv[0] 70 int count = 1; 71 char * tok; 72 char * s = args; 73 74 while ((tok = strtok(s, " \0"))) { 75 argv[count] = tok; 76 s = NULL; 77 count++; 78 } 79 return count; 80 } 81 82 /* 83 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities. 84 * Our group, cache, was set by init. 85 */ 86 void switchUser() { 87 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); 88 setuid(AID_RADIO); 89 90 struct __user_cap_header_struct header; 91 struct __user_cap_data_struct cap; 92 header.version = _LINUX_CAPABILITY_VERSION; 93 header.pid = 0; 94 cap.effective = cap.permitted = 1 << CAP_NET_ADMIN; 95 cap.inheritable = 0; 96 capset(&header, &cap); 97 } 98 99 int main(int argc, char **argv) 100 { 101 const char * rilLibPath = NULL; 102 char **rilArgv; 103 void *dlHandle; 104 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); 105 const RIL_RadioFunctions *funcs; 106 char libPath[PROPERTY_VALUE_MAX]; 107 unsigned char hasLibArgs = 0; 108 109 int i; 110 111 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); 112 for (i = 1; i < argc ;) { 113 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { 114 rilLibPath = argv[i + 1]; 115 i += 2; 116 } else if (0 == strcmp(argv[i], "--")) { 117 i++; 118 hasLibArgs = 1; 119 break; 120 } else { 121 usage(argv[0]); 122 } 123 } 124 125 if (rilLibPath == NULL) { 126 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { 127 // No lib sepcified on the command line, and nothing set in props. 128 // Assume "no-ril" case. 129 goto done; 130 } else { 131 rilLibPath = libPath; 132 } 133 } 134 135 /* special override when in the emulator */ 136 #if 1 137 { 138 static char* arg_overrides[3]; 139 static char arg_device[32]; 140 int done = 0; 141 142 #define REFERENCE_RIL_PATH "/system/lib/libreference-ril.so" 143 144 /* first, read /proc/cmdline into memory */ 145 char buffer[1024], *p, *q; 146 int len; 147 int fd = open("/proc/cmdline",O_RDONLY); 148 149 if (fd < 0) { 150 LOGD("could not open /proc/cmdline:%s", strerror(errno)); 151 goto OpenLib; 152 } 153 154 do { 155 len = read(fd,buffer,sizeof(buffer)); } 156 while (len == -1 && errno == EINTR); 157 158 if (len < 0) { 159 LOGD("could not read /proc/cmdline:%s", strerror(errno)); 160 close(fd); 161 goto OpenLib; 162 } 163 close(fd); 164 165 if (strstr(buffer, "android.qemud=") != NULL) 166 { 167 /* the qemud daemon is launched after rild, so 168 * give it some time to create its GSM socket 169 */ 170 int tries = 5; 171 #define QEMUD_SOCKET_NAME "qemud" 172 173 while (1) { 174 int fd; 175 176 sleep(1); 177 178 fd = socket_local_client( 179 QEMUD_SOCKET_NAME, 180 ANDROID_SOCKET_NAMESPACE_RESERVED, 181 SOCK_STREAM ); 182 183 if (fd >= 0) { 184 close(fd); 185 snprintf( arg_device, sizeof(arg_device), "%s/%s", 186 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME ); 187 188 arg_overrides[1] = "-s"; 189 arg_overrides[2] = arg_device; 190 done = 1; 191 break; 192 } 193 LOGD("could not connect to %s socket: %s", 194 QEMUD_SOCKET_NAME, strerror(errno)); 195 if (--tries == 0) 196 break; 197 } 198 if (!done) { 199 LOGE("could not connect to %s socket (giving up): %s", 200 QEMUD_SOCKET_NAME, strerror(errno)); 201 while(1) 202 sleep(0x00ffffff); 203 } 204 } 205 206 /* otherwise, try to see if we passed a device name from the kernel */ 207 if (!done) do { 208 #define KERNEL_OPTION "android.ril=" 209 #define DEV_PREFIX "/dev/" 210 211 p = strstr( buffer, KERNEL_OPTION ); 212 if (p == NULL) 213 break; 214 215 p += sizeof(KERNEL_OPTION)-1; 216 q = strpbrk( p, " \t\n\r" ); 217 if (q != NULL) 218 *q = 0; 219 220 snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p ); 221 arg_device[sizeof(arg_device)-1] = 0; 222 arg_overrides[1] = "-d"; 223 arg_overrides[2] = arg_device; 224 done = 1; 225 226 } while (0); 227 228 if (done) { 229 argv = arg_overrides; 230 argc = 3; 231 i = 1; 232 hasLibArgs = 1; 233 rilLibPath = REFERENCE_RIL_PATH; 234 235 LOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]); 236 } 237 } 238 OpenLib: 239 #endif 240 switchUser(); 241 242 dlHandle = dlopen(rilLibPath, RTLD_NOW); 243 244 if (dlHandle == NULL) { 245 fprintf(stderr, "dlopen failed: %s\n", dlerror()); 246 exit(-1); 247 } 248 249 RIL_startEventLoop(); 250 251 rilInit = (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))dlsym(dlHandle, "RIL_Init"); 252 253 if (rilInit == NULL) { 254 fprintf(stderr, "RIL_Init not defined or exported in %s\n", rilLibPath); 255 exit(-1); 256 } 257 258 if (hasLibArgs) { 259 rilArgv = argv + i - 1; 260 argc = argc -i + 1; 261 } else { 262 static char * newArgv[MAX_LIB_ARGS]; 263 static char args[PROPERTY_VALUE_MAX]; 264 rilArgv = newArgv; 265 property_get(LIB_ARGS_PROPERTY, args, ""); 266 argc = make_argv(args, rilArgv); 267 } 268 269 // Make sure there's a reasonable argv[0] 270 rilArgv[0] = argv[0]; 271 272 funcs = rilInit(&s_rilEnv, argc, rilArgv); 273 274 RIL_register(funcs); 275 276 done: 277 278 while(1) { 279 // sleep(UINT32_MAX) seems to return immediately on bionic 280 sleep(0x00ffffff); 281 } 282 } 283 284