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