The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 1 | /* //device/system/rild/rild.c |
| 2 | ** |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 3 | ** Copyright 2006 The Android Open Source Project |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 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> |
Nick Kralevich | fe4e1ec | 2013-02-28 14:16:41 -0800 | [diff] [blame] | 32 | #include <sys/capability.h> |
Elliott Hughes | 5d0cb9e | 2014-07-18 17:55:52 -0700 | [diff] [blame] | 33 | #include <sys/prctl.h> |
Andreas Gampe | 8ded996 | 2015-04-06 12:20:24 -0700 | [diff] [blame^] | 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 36 | |
| 37 | #include <private/android_filesystem_config.h> |
Vladimir Chtchetkine | 385a739 | 2011-08-04 14:03:07 -0700 | [diff] [blame] | 38 | #include "hardware/qemu_pipe.h" |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 39 | |
| 40 | #define LIB_PATH_PROPERTY "rild.libpath" |
| 41 | #define LIB_ARGS_PROPERTY "rild.libargs" |
| 42 | #define MAX_LIB_ARGS 16 |
| 43 | |
| 44 | static void usage(const char *argv0) |
| 45 | { |
| 46 | fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 47 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 50 | extern char rild[MAX_SOCKET_NAME_LENGTH]; |
| 51 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 52 | extern void RIL_register (const RIL_RadioFunctions *callbacks); |
| 53 | |
| 54 | extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e, |
| 55 | void *response, size_t responselen); |
| 56 | |
Elliott Hughes | e102e95 | 2015-02-20 10:52:14 -0800 | [diff] [blame] | 57 | extern void RIL_setRilSocketName(char *); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 58 | |
| 59 | #if defined(ANDROID_MULTI_SIM) |
| 60 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
| 61 | size_t datalen, RIL_SOCKET_ID socket_id); |
| 62 | #else |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 63 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
| 64 | size_t datalen); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 65 | #endif |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 66 | |
Wink Saville | 2932f31 | 2010-10-07 16:35:15 -0700 | [diff] [blame] | 67 | extern void RIL_requestTimedCallback (RIL_TimedCallback callback, |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 68 | void *param, const struct timeval *relativeTime); |
| 69 | |
| 70 | |
| 71 | static struct RIL_Env s_rilEnv = { |
| 72 | RIL_onRequestComplete, |
| 73 | RIL_onUnsolicitedResponse, |
Wink Saville | 2932f31 | 2010-10-07 16:35:15 -0700 | [diff] [blame] | 74 | RIL_requestTimedCallback |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | extern void RIL_startEventLoop(); |
| 78 | |
| 79 | static int make_argv(char * args, char ** argv) |
| 80 | { |
| 81 | // Note: reserve argv[0] |
| 82 | int count = 1; |
| 83 | char * tok; |
| 84 | char * s = args; |
| 85 | |
| 86 | while ((tok = strtok(s, " \0"))) { |
| 87 | argv[count] = tok; |
| 88 | s = NULL; |
| 89 | count++; |
| 90 | } |
| 91 | return count; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities. |
| 96 | * Our group, cache, was set by init. |
| 97 | */ |
| 98 | void switchUser() { |
Julien Vuillaumier | faf4676 | 2015-01-22 13:42:58 +0000 | [diff] [blame] | 99 | char debuggable[PROP_VALUE_MAX]; |
| 100 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 101 | prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 102 | setuid(AID_RADIO); |
| 103 | |
| 104 | struct __user_cap_header_struct header; |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 105 | memset(&header, 0, sizeof(header)); |
| 106 | header.version = _LINUX_CAPABILITY_VERSION_3; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 107 | header.pid = 0; |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 108 | |
| 109 | struct __user_cap_data_struct data[2]; |
| 110 | memset(&data, 0, sizeof(data)); |
| 111 | |
| 112 | data[CAP_TO_INDEX(CAP_NET_ADMIN)].effective |= CAP_TO_MASK(CAP_NET_ADMIN); |
| 113 | data[CAP_TO_INDEX(CAP_NET_ADMIN)].permitted |= CAP_TO_MASK(CAP_NET_ADMIN); |
| 114 | |
| 115 | data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW); |
| 116 | data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW); |
| 117 | |
| 118 | if (capset(&header, &data[0]) == -1) { |
| 119 | RLOGE("capset failed: %s", strerror(errno)); |
| 120 | exit(EXIT_FAILURE); |
| 121 | } |
Julien Vuillaumier | faf4676 | 2015-01-22 13:42:58 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Debuggable build only: |
| 125 | * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash |
| 126 | */ |
| 127 | property_get("ro.debuggable", debuggable, "0"); |
| 128 | if (strcmp(debuggable, "1") == 0) { |
| 129 | prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
| 130 | } |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int main(int argc, char **argv) |
| 134 | { |
| 135 | const char * rilLibPath = NULL; |
| 136 | char **rilArgv; |
| 137 | void *dlHandle; |
| 138 | const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); |
| 139 | const RIL_RadioFunctions *funcs; |
| 140 | char libPath[PROPERTY_VALUE_MAX]; |
| 141 | unsigned char hasLibArgs = 0; |
| 142 | |
| 143 | int i; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 144 | const char *clientId = NULL; |
| 145 | RLOGD("**RIL Daemon Started**"); |
| 146 | RLOGD("**RILd param count=%d**", argc); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 147 | |
Nick Kralevich | aea7d5b | 2010-12-10 15:53:02 -0800 | [diff] [blame] | 148 | umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 149 | for (i = 1; i < argc ;) { |
| 150 | if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { |
| 151 | rilLibPath = argv[i + 1]; |
| 152 | i += 2; |
| 153 | } else if (0 == strcmp(argv[i], "--")) { |
| 154 | i++; |
| 155 | hasLibArgs = 1; |
| 156 | break; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 157 | } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) { |
| 158 | clientId = argv[i+1]; |
| 159 | i += 2; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 160 | } else { |
| 161 | usage(argv[0]); |
| 162 | } |
| 163 | } |
| 164 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 165 | if (clientId == NULL) { |
| 166 | clientId = "0"; |
| 167 | } else if (atoi(clientId) >= MAX_RILDS) { |
| 168 | RLOGE("Max Number of rild's supported is: %d", MAX_RILDS); |
| 169 | exit(0); |
| 170 | } |
| 171 | if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) { |
| 172 | RIL_setRilSocketName(strncat(rild, clientId, MAX_SOCKET_NAME_LENGTH)); |
| 173 | } |
| 174 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 175 | if (rilLibPath == NULL) { |
| 176 | if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { |
| 177 | // No lib sepcified on the command line, and nothing set in props. |
| 178 | // Assume "no-ril" case. |
| 179 | goto done; |
| 180 | } else { |
| 181 | rilLibPath = libPath; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* special override when in the emulator */ |
| 186 | #if 1 |
| 187 | { |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 188 | static char* arg_overrides[5]; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 189 | static char arg_device[32]; |
| 190 | int done = 0; |
| 191 | |
Vince Harron | 60c1f5b | 2014-08-13 23:12:37 -0700 | [diff] [blame] | 192 | #define REFERENCE_RIL_PATH "libreference-ril.so" |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 193 | |
| 194 | /* first, read /proc/cmdline into memory */ |
| 195 | char buffer[1024], *p, *q; |
| 196 | int len; |
| 197 | int fd = open("/proc/cmdline",O_RDONLY); |
| 198 | |
| 199 | if (fd < 0) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 200 | RLOGD("could not open /proc/cmdline:%s", strerror(errno)); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 201 | goto OpenLib; |
| 202 | } |
| 203 | |
| 204 | do { |
| 205 | len = read(fd,buffer,sizeof(buffer)); } |
| 206 | while (len == -1 && errno == EINTR); |
| 207 | |
| 208 | if (len < 0) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 209 | RLOGD("could not read /proc/cmdline:%s", strerror(errno)); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 210 | close(fd); |
| 211 | goto OpenLib; |
| 212 | } |
| 213 | close(fd); |
| 214 | |
| 215 | if (strstr(buffer, "android.qemud=") != NULL) |
| 216 | { |
| 217 | /* the qemud daemon is launched after rild, so |
| 218 | * give it some time to create its GSM socket |
| 219 | */ |
| 220 | int tries = 5; |
The Android Open Source Project | e6e6fb2 | 2009-03-18 17:39:47 -0700 | [diff] [blame] | 221 | #define QEMUD_SOCKET_NAME "qemud" |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 222 | |
| 223 | while (1) { |
| 224 | int fd; |
| 225 | |
| 226 | sleep(1); |
| 227 | |
Vladimir Chtchetkine | 385a739 | 2011-08-04 14:03:07 -0700 | [diff] [blame] | 228 | fd = qemu_pipe_open("qemud:gsm"); |
| 229 | if (fd < 0) { |
| 230 | fd = socket_local_client( |
| 231 | QEMUD_SOCKET_NAME, |
| 232 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 233 | SOCK_STREAM ); |
| 234 | } |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 235 | if (fd >= 0) { |
| 236 | close(fd); |
| 237 | snprintf( arg_device, sizeof(arg_device), "%s/%s", |
| 238 | ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME ); |
| 239 | |
| 240 | arg_overrides[1] = "-s"; |
| 241 | arg_overrides[2] = arg_device; |
| 242 | done = 1; |
| 243 | break; |
| 244 | } |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 245 | RLOGD("could not connect to %s socket: %s", |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 246 | QEMUD_SOCKET_NAME, strerror(errno)); |
| 247 | if (--tries == 0) |
| 248 | break; |
| 249 | } |
| 250 | if (!done) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 251 | RLOGE("could not connect to %s socket (giving up): %s", |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 252 | QEMUD_SOCKET_NAME, strerror(errno)); |
| 253 | while(1) |
| 254 | sleep(0x00ffffff); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* otherwise, try to see if we passed a device name from the kernel */ |
| 259 | if (!done) do { |
| 260 | #define KERNEL_OPTION "android.ril=" |
| 261 | #define DEV_PREFIX "/dev/" |
| 262 | |
| 263 | p = strstr( buffer, KERNEL_OPTION ); |
| 264 | if (p == NULL) |
| 265 | break; |
| 266 | |
| 267 | p += sizeof(KERNEL_OPTION)-1; |
| 268 | q = strpbrk( p, " \t\n\r" ); |
| 269 | if (q != NULL) |
| 270 | *q = 0; |
| 271 | |
| 272 | snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p ); |
| 273 | arg_device[sizeof(arg_device)-1] = 0; |
| 274 | arg_overrides[1] = "-d"; |
| 275 | arg_overrides[2] = arg_device; |
| 276 | done = 1; |
| 277 | |
| 278 | } while (0); |
| 279 | |
| 280 | if (done) { |
| 281 | argv = arg_overrides; |
| 282 | argc = 3; |
| 283 | i = 1; |
| 284 | hasLibArgs = 1; |
| 285 | rilLibPath = REFERENCE_RIL_PATH; |
| 286 | |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 287 | RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | OpenLib: |
| 291 | #endif |
| 292 | switchUser(); |
| 293 | |
| 294 | dlHandle = dlopen(rilLibPath, RTLD_NOW); |
| 295 | |
| 296 | if (dlHandle == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 297 | RLOGE("dlopen failed: %s", dlerror()); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 298 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | RIL_startEventLoop(); |
| 302 | |
| 303 | rilInit = (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))dlsym(dlHandle, "RIL_Init"); |
| 304 | |
| 305 | if (rilInit == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 306 | RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 307 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (hasLibArgs) { |
| 311 | rilArgv = argv + i - 1; |
| 312 | argc = argc -i + 1; |
| 313 | } else { |
| 314 | static char * newArgv[MAX_LIB_ARGS]; |
| 315 | static char args[PROPERTY_VALUE_MAX]; |
| 316 | rilArgv = newArgv; |
| 317 | property_get(LIB_ARGS_PROPERTY, args, ""); |
| 318 | argc = make_argv(args, rilArgv); |
| 319 | } |
| 320 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 321 | rilArgv[argc++] = "-c"; |
| 322 | rilArgv[argc++] = clientId; |
| 323 | RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); |
| 324 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 325 | // Make sure there's a reasonable argv[0] |
| 326 | rilArgv[0] = argv[0]; |
| 327 | |
| 328 | funcs = rilInit(&s_rilEnv, argc, rilArgv); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 329 | RLOGD("RIL_Init rilInit completed"); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 330 | |
| 331 | RIL_register(funcs); |
| 332 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 333 | RLOGD("RIL_Init RIL_register completed"); |
| 334 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 335 | done: |
| 336 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 337 | RLOGD("RIL_Init starting sleep loop"); |
Elliott Hughes | 164d205 | 2013-11-20 14:53:08 -0800 | [diff] [blame] | 338 | while (true) { |
| 339 | sleep(UINT32_MAX); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 340 | } |
| 341 | } |