blob: bf9c4219ad705c6b3e4e0e8e5ee8055e6c889346 [file] [log] [blame]
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001/* //device/system/rild/rild.c
2**
Etan Cohend3652192014-06-20 08:28:44 -07003** Copyright 2006 The Android Open Source Project
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08004**
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 Kralevichfe4e1ec2013-02-28 14:16:41 -080032#include <sys/capability.h>
Elliott Hughes5d0cb9e2014-07-18 17:55:52 -070033#include <sys/prctl.h>
Andreas Gampe8ded9962015-04-06 12:20:24 -070034#include <sys/stat.h>
35#include <sys/types.h>
Dheeraj Shetty27976c42014-07-02 21:27:57 +020036#include <libril/ril_ex.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080037
38#include <private/android_filesystem_config.h>
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -070039#include "hardware/qemu_pipe.h"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080040
41#define LIB_PATH_PROPERTY "rild.libpath"
42#define LIB_ARGS_PROPERTY "rild.libargs"
43#define MAX_LIB_ARGS 16
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -080044#define MAX_CAP_NUM (CAP_TO_INDEX(CAP_LAST_CAP) + 1)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080045
Dheeraj Shetty27976c42014-07-02 21:27:57 +020046static void usage(const char *argv0) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080047 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
Elliott Hughese2a70cf2013-11-20 13:51:45 -080048 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080049}
50
Etan Cohend3652192014-06-20 08:28:44 -070051extern char rild[MAX_SOCKET_NAME_LENGTH];
52
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080053extern void RIL_register (const RIL_RadioFunctions *callbacks);
54
Dheeraj Shetty27976c42014-07-02 21:27:57 +020055extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit)
56 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
57
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080058extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020059 void *response, size_t responselen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080060
Elliott Hughese102e952015-02-20 10:52:14 -080061extern void RIL_setRilSocketName(char *);
Etan Cohend3652192014-06-20 08:28:44 -070062
63#if defined(ANDROID_MULTI_SIM)
64extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020065 size_t datalen, RIL_SOCKET_ID socket_id);
Etan Cohend3652192014-06-20 08:28:44 -070066#else
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080067extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020068 size_t datalen);
Etan Cohend3652192014-06-20 08:28:44 -070069#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080070
Wink Saville2932f312010-10-07 16:35:15 -070071extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020072 void *param, const struct timeval *relativeTime);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080073
74
75static struct RIL_Env s_rilEnv = {
76 RIL_onRequestComplete,
77 RIL_onUnsolicitedResponse,
Wink Saville2932f312010-10-07 16:35:15 -070078 RIL_requestTimedCallback
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080079};
80
81extern void RIL_startEventLoop();
82
Dheeraj Shetty27976c42014-07-02 21:27:57 +020083static int make_argv(char * args, char ** argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080084 // Note: reserve argv[0]
85 int count = 1;
86 char * tok;
87 char * s = args;
88
89 while ((tok = strtok(s, " \0"))) {
90 argv[count] = tok;
91 s = NULL;
92 count++;
93 }
94 return count;
95}
96
97/*
98 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
99 * Our group, cache, was set by init.
100 */
101void switchUser() {
Julien Vuillaumierfaf46762015-01-22 13:42:58 +0000102 char debuggable[PROP_VALUE_MAX];
103
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800104 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
105 setuid(AID_RADIO);
106
107 struct __user_cap_header_struct header;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800108 memset(&header, 0, sizeof(header));
109 header.version = _LINUX_CAPABILITY_VERSION_3;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800110 header.pid = 0;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800111
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -0800112 struct __user_cap_data_struct data[MAX_CAP_NUM];
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800113 memset(&data, 0, sizeof(data));
114
115 data[CAP_TO_INDEX(CAP_NET_ADMIN)].effective |= CAP_TO_MASK(CAP_NET_ADMIN);
116 data[CAP_TO_INDEX(CAP_NET_ADMIN)].permitted |= CAP_TO_MASK(CAP_NET_ADMIN);
117
118 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
119 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
120
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -0800121 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].effective |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
122 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].permitted |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
123
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800124 if (capset(&header, &data[0]) == -1) {
125 RLOGE("capset failed: %s", strerror(errno));
126 exit(EXIT_FAILURE);
127 }
Julien Vuillaumierfaf46762015-01-22 13:42:58 +0000128
129 /*
130 * Debuggable build only:
131 * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash
132 */
133 property_get("ro.debuggable", debuggable, "0");
134 if (strcmp(debuggable, "1") == 0) {
135 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
136 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800137}
138
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200139int main(int argc, char **argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800140 const char * rilLibPath = NULL;
141 char **rilArgv;
142 void *dlHandle;
143 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200144 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
145 char *err_str = NULL;
146
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800147 const RIL_RadioFunctions *funcs;
148 char libPath[PROPERTY_VALUE_MAX];
149 unsigned char hasLibArgs = 0;
150
151 int i;
Etan Cohend3652192014-06-20 08:28:44 -0700152 const char *clientId = NULL;
153 RLOGD("**RIL Daemon Started**");
154 RLOGD("**RILd param count=%d**", argc);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800155
Nick Kralevichaea7d5b2010-12-10 15:53:02 -0800156 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800157 for (i = 1; i < argc ;) {
158 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
159 rilLibPath = argv[i + 1];
160 i += 2;
161 } else if (0 == strcmp(argv[i], "--")) {
162 i++;
163 hasLibArgs = 1;
164 break;
Etan Cohend3652192014-06-20 08:28:44 -0700165 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
166 clientId = argv[i+1];
167 i += 2;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800168 } else {
169 usage(argv[0]);
170 }
171 }
172
Etan Cohend3652192014-06-20 08:28:44 -0700173 if (clientId == NULL) {
174 clientId = "0";
175 } else if (atoi(clientId) >= MAX_RILDS) {
176 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
177 exit(0);
178 }
179 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
180 RIL_setRilSocketName(strncat(rild, clientId, MAX_SOCKET_NAME_LENGTH));
181 }
182
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800183 if (rilLibPath == NULL) {
184 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
185 // No lib sepcified on the command line, and nothing set in props.
186 // Assume "no-ril" case.
187 goto done;
188 } else {
189 rilLibPath = libPath;
190 }
191 }
192
193 /* special override when in the emulator */
194#if 1
195 {
Etan Cohend3652192014-06-20 08:28:44 -0700196 static char* arg_overrides[5];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800197 static char arg_device[32];
198 int done = 0;
199
Vince Harron60c1f5b2014-08-13 23:12:37 -0700200#define REFERENCE_RIL_PATH "libreference-ril.so"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800201
202 /* first, read /proc/cmdline into memory */
Sindhu Kanathurea024182013-12-10 15:03:17 +0530203 char buffer[1024] = {'\0'}, *p, *q;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800204 int len;
205 int fd = open("/proc/cmdline",O_RDONLY);
206
207 if (fd < 0) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800208 RLOGD("could not open /proc/cmdline:%s", strerror(errno));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800209 goto OpenLib;
210 }
211
212 do {
213 len = read(fd,buffer,sizeof(buffer)); }
214 while (len == -1 && errno == EINTR);
215
216 if (len < 0) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800217 RLOGD("could not read /proc/cmdline:%s", strerror(errno));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800218 close(fd);
219 goto OpenLib;
220 }
221 close(fd);
222
223 if (strstr(buffer, "android.qemud=") != NULL)
224 {
225 /* the qemud daemon is launched after rild, so
226 * give it some time to create its GSM socket
227 */
228 int tries = 5;
The Android Open Source Projecte6e6fb22009-03-18 17:39:47 -0700229#define QEMUD_SOCKET_NAME "qemud"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800230
231 while (1) {
232 int fd;
233
234 sleep(1);
235
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -0700236 fd = qemu_pipe_open("qemud:gsm");
237 if (fd < 0) {
238 fd = socket_local_client(
239 QEMUD_SOCKET_NAME,
240 ANDROID_SOCKET_NAMESPACE_RESERVED,
241 SOCK_STREAM );
242 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800243 if (fd >= 0) {
244 close(fd);
245 snprintf( arg_device, sizeof(arg_device), "%s/%s",
246 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
247
248 arg_overrides[1] = "-s";
249 arg_overrides[2] = arg_device;
250 done = 1;
251 break;
252 }
Wink Saville8eb2a122012-11-19 16:05:13 -0800253 RLOGD("could not connect to %s socket: %s",
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800254 QEMUD_SOCKET_NAME, strerror(errno));
255 if (--tries == 0)
256 break;
257 }
258 if (!done) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800259 RLOGE("could not connect to %s socket (giving up): %s",
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800260 QEMUD_SOCKET_NAME, strerror(errno));
261 while(1)
262 sleep(0x00ffffff);
263 }
264 }
265
266 /* otherwise, try to see if we passed a device name from the kernel */
267 if (!done) do {
268#define KERNEL_OPTION "android.ril="
269#define DEV_PREFIX "/dev/"
270
271 p = strstr( buffer, KERNEL_OPTION );
272 if (p == NULL)
273 break;
274
275 p += sizeof(KERNEL_OPTION)-1;
276 q = strpbrk( p, " \t\n\r" );
277 if (q != NULL)
278 *q = 0;
279
280 snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
281 arg_device[sizeof(arg_device)-1] = 0;
282 arg_overrides[1] = "-d";
283 arg_overrides[2] = arg_device;
284 done = 1;
285
286 } while (0);
287
288 if (done) {
289 argv = arg_overrides;
290 argc = 3;
291 i = 1;
292 hasLibArgs = 1;
293 rilLibPath = REFERENCE_RIL_PATH;
294
Wink Saville8eb2a122012-11-19 16:05:13 -0800295 RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800296 }
297 }
298OpenLib:
299#endif
300 switchUser();
301
302 dlHandle = dlopen(rilLibPath, RTLD_NOW);
303
304 if (dlHandle == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800305 RLOGE("dlopen failed: %s", dlerror());
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800306 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800307 }
308
309 RIL_startEventLoop();
310
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200311 rilInit =
312 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
313 dlsym(dlHandle, "RIL_Init");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800314
315 if (rilInit == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800316 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800317 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800318 }
319
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200320 dlerror(); // Clear any previous dlerror
321 rilUimInit =
322 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
323 dlsym(dlHandle, "RIL_SAP_Init");
324 err_str = dlerror();
325 if (err_str) {
326 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
327 } else if (!rilUimInit) {
328 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
329 }
330
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800331 if (hasLibArgs) {
332 rilArgv = argv + i - 1;
333 argc = argc -i + 1;
334 } else {
335 static char * newArgv[MAX_LIB_ARGS];
336 static char args[PROPERTY_VALUE_MAX];
337 rilArgv = newArgv;
338 property_get(LIB_ARGS_PROPERTY, args, "");
339 argc = make_argv(args, rilArgv);
340 }
341
Etan Cohend3652192014-06-20 08:28:44 -0700342 rilArgv[argc++] = "-c";
343 rilArgv[argc++] = clientId;
344 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
345
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800346 // Make sure there's a reasonable argv[0]
347 rilArgv[0] = argv[0];
348
349 funcs = rilInit(&s_rilEnv, argc, rilArgv);
Etan Cohend3652192014-06-20 08:28:44 -0700350 RLOGD("RIL_Init rilInit completed");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800351
352 RIL_register(funcs);
353
Etan Cohend3652192014-06-20 08:28:44 -0700354 RLOGD("RIL_Init RIL_register completed");
355
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200356 if (rilUimInit) {
357 RLOGD("RIL_register_socket started");
358 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
359 }
360
361 RLOGD("RIL_register_socket completed");
362
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800363done:
364
Etan Cohend3652192014-06-20 08:28:44 -0700365 RLOGD("RIL_Init starting sleep loop");
Elliott Hughes164d2052013-11-20 14:53:08 -0800366 while (true) {
367 sleep(UINT32_MAX);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800368 }
369}