blob: b32afe4d7bec6542376e5ed49cd98acc4bc200be [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
Sanket Padawe6ff9a872016-01-27 15:09:12 -080061extern void RIL_onRequestAck(RIL_Token t);
62
Elliott Hughese102e952015-02-20 10:52:14 -080063extern void RIL_setRilSocketName(char *);
Etan Cohend3652192014-06-20 08:28:44 -070064
65#if defined(ANDROID_MULTI_SIM)
66extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020067 size_t datalen, RIL_SOCKET_ID socket_id);
Etan Cohend3652192014-06-20 08:28:44 -070068#else
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080069extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020070 size_t datalen);
Etan Cohend3652192014-06-20 08:28:44 -070071#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080072
Wink Saville2932f312010-10-07 16:35:15 -070073extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020074 void *param, const struct timeval *relativeTime);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080075
76
77static struct RIL_Env s_rilEnv = {
78 RIL_onRequestComplete,
79 RIL_onUnsolicitedResponse,
Sanket Padawe6ff9a872016-01-27 15:09:12 -080080 RIL_requestTimedCallback,
81 RIL_onRequestAck
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080082};
83
84extern void RIL_startEventLoop();
85
Dheeraj Shetty27976c42014-07-02 21:27:57 +020086static int make_argv(char * args, char ** argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080087 // Note: reserve argv[0]
88 int count = 1;
89 char * tok;
90 char * s = args;
91
92 while ((tok = strtok(s, " \0"))) {
93 argv[count] = tok;
94 s = NULL;
95 count++;
96 }
97 return count;
98}
99
100/*
101 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
102 * Our group, cache, was set by init.
103 */
104void switchUser() {
Julien Vuillaumierfaf46762015-01-22 13:42:58 +0000105 char debuggable[PROP_VALUE_MAX];
106
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800107 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
Keun Soo Yimf1e9cf92016-04-13 17:17:52 -0700108 if (setresuid(AID_RADIO, AID_RADIO, AID_RADIO) == -1) {
109 RLOGE("setresuid failed: %s", strerror(errno));
110 exit(EXIT_FAILURE);
111 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800112
113 struct __user_cap_header_struct header;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800114 memset(&header, 0, sizeof(header));
115 header.version = _LINUX_CAPABILITY_VERSION_3;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800116 header.pid = 0;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800117
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -0800118 struct __user_cap_data_struct data[MAX_CAP_NUM];
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800119 memset(&data, 0, sizeof(data));
120
121 data[CAP_TO_INDEX(CAP_NET_ADMIN)].effective |= CAP_TO_MASK(CAP_NET_ADMIN);
122 data[CAP_TO_INDEX(CAP_NET_ADMIN)].permitted |= CAP_TO_MASK(CAP_NET_ADMIN);
123
124 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
125 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
126
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -0800127 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].effective |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
128 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].permitted |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
129
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800130 if (capset(&header, &data[0]) == -1) {
131 RLOGE("capset failed: %s", strerror(errno));
132 exit(EXIT_FAILURE);
133 }
Julien Vuillaumierfaf46762015-01-22 13:42:58 +0000134
135 /*
136 * Debuggable build only:
137 * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash
138 */
139 property_get("ro.debuggable", debuggable, "0");
140 if (strcmp(debuggable, "1") == 0) {
141 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
142 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800143}
144
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200145int main(int argc, char **argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800146 const char * rilLibPath = NULL;
147 char **rilArgv;
148 void *dlHandle;
149 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200150 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
151 char *err_str = NULL;
152
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800153 const RIL_RadioFunctions *funcs;
154 char libPath[PROPERTY_VALUE_MAX];
155 unsigned char hasLibArgs = 0;
156
157 int i;
Etan Cohend3652192014-06-20 08:28:44 -0700158 const char *clientId = NULL;
159 RLOGD("**RIL Daemon Started**");
160 RLOGD("**RILd param count=%d**", argc);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800161
Nick Kralevichaea7d5b2010-12-10 15:53:02 -0800162 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800163 for (i = 1; i < argc ;) {
164 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
165 rilLibPath = argv[i + 1];
166 i += 2;
167 } else if (0 == strcmp(argv[i], "--")) {
168 i++;
169 hasLibArgs = 1;
170 break;
Etan Cohend3652192014-06-20 08:28:44 -0700171 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
172 clientId = argv[i+1];
173 i += 2;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800174 } else {
175 usage(argv[0]);
176 }
177 }
178
Etan Cohend3652192014-06-20 08:28:44 -0700179 if (clientId == NULL) {
180 clientId = "0";
181 } else if (atoi(clientId) >= MAX_RILDS) {
182 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
183 exit(0);
184 }
185 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
Keun Soo Yim19736852016-04-13 17:11:20 -0700186 strlcat(rild, clientId, MAX_SOCKET_NAME_LENGTH);
Keun Soo Yim1067bb62016-04-13 19:06:26 -0700187 RIL_setRilSocketName(rild);
Etan Cohend3652192014-06-20 08:28:44 -0700188 }
189
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800190 if (rilLibPath == NULL) {
191 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
192 // No lib sepcified on the command line, and nothing set in props.
193 // Assume "no-ril" case.
194 goto done;
195 } else {
196 rilLibPath = libPath;
197 }
198 }
199
200 /* special override when in the emulator */
201#if 1
202 {
Etan Cohend3652192014-06-20 08:28:44 -0700203 static char* arg_overrides[5];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800204 static char arg_device[32];
205 int done = 0;
206
Vince Harron60c1f5b2014-08-13 23:12:37 -0700207#define REFERENCE_RIL_PATH "libreference-ril.so"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800208
209 /* first, read /proc/cmdline into memory */
Ajay Nambif9808632016-01-15 10:57:52 -0800210 char buffer[1024] = {'\0'}, *p, *q;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800211 int len;
212 int fd = open("/proc/cmdline",O_RDONLY);
213
214 if (fd < 0) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800215 RLOGD("could not open /proc/cmdline:%s", strerror(errno));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800216 goto OpenLib;
217 }
218
219 do {
220 len = read(fd,buffer,sizeof(buffer)); }
221 while (len == -1 && errno == EINTR);
222
223 if (len < 0) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800224 RLOGD("could not read /proc/cmdline:%s", strerror(errno));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800225 close(fd);
226 goto OpenLib;
227 }
228 close(fd);
229
230 if (strstr(buffer, "android.qemud=") != NULL)
231 {
232 /* the qemud daemon is launched after rild, so
233 * give it some time to create its GSM socket
234 */
235 int tries = 5;
The Android Open Source Projecte6e6fb22009-03-18 17:39:47 -0700236#define QEMUD_SOCKET_NAME "qemud"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800237
238 while (1) {
239 int fd;
240
241 sleep(1);
242
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -0700243 fd = qemu_pipe_open("qemud:gsm");
244 if (fd < 0) {
245 fd = socket_local_client(
246 QEMUD_SOCKET_NAME,
247 ANDROID_SOCKET_NAMESPACE_RESERVED,
248 SOCK_STREAM );
249 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800250 if (fd >= 0) {
251 close(fd);
252 snprintf( arg_device, sizeof(arg_device), "%s/%s",
253 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
254
255 arg_overrides[1] = "-s";
256 arg_overrides[2] = arg_device;
257 done = 1;
258 break;
259 }
Wink Saville8eb2a122012-11-19 16:05:13 -0800260 RLOGD("could not connect to %s socket: %s",
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800261 QEMUD_SOCKET_NAME, strerror(errno));
262 if (--tries == 0)
263 break;
264 }
265 if (!done) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800266 RLOGE("could not connect to %s socket (giving up): %s",
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800267 QEMUD_SOCKET_NAME, strerror(errno));
268 while(1)
269 sleep(0x00ffffff);
270 }
271 }
272
273 /* otherwise, try to see if we passed a device name from the kernel */
274 if (!done) do {
275#define KERNEL_OPTION "android.ril="
276#define DEV_PREFIX "/dev/"
277
278 p = strstr( buffer, KERNEL_OPTION );
279 if (p == NULL)
280 break;
281
282 p += sizeof(KERNEL_OPTION)-1;
283 q = strpbrk( p, " \t\n\r" );
284 if (q != NULL)
285 *q = 0;
286
287 snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
288 arg_device[sizeof(arg_device)-1] = 0;
289 arg_overrides[1] = "-d";
290 arg_overrides[2] = arg_device;
291 done = 1;
292
293 } while (0);
294
295 if (done) {
296 argv = arg_overrides;
297 argc = 3;
298 i = 1;
299 hasLibArgs = 1;
300 rilLibPath = REFERENCE_RIL_PATH;
301
Wink Saville8eb2a122012-11-19 16:05:13 -0800302 RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800303 }
304 }
305OpenLib:
306#endif
307 switchUser();
308
309 dlHandle = dlopen(rilLibPath, RTLD_NOW);
310
311 if (dlHandle == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800312 RLOGE("dlopen failed: %s", dlerror());
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800313 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800314 }
315
316 RIL_startEventLoop();
317
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200318 rilInit =
319 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
320 dlsym(dlHandle, "RIL_Init");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800321
322 if (rilInit == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800323 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800324 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800325 }
326
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200327 dlerror(); // Clear any previous dlerror
328 rilUimInit =
329 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
330 dlsym(dlHandle, "RIL_SAP_Init");
331 err_str = dlerror();
332 if (err_str) {
333 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
334 } else if (!rilUimInit) {
335 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
336 }
337
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800338 if (hasLibArgs) {
339 rilArgv = argv + i - 1;
340 argc = argc -i + 1;
341 } else {
342 static char * newArgv[MAX_LIB_ARGS];
343 static char args[PROPERTY_VALUE_MAX];
344 rilArgv = newArgv;
345 property_get(LIB_ARGS_PROPERTY, args, "");
346 argc = make_argv(args, rilArgv);
347 }
348
Etan Cohend3652192014-06-20 08:28:44 -0700349 rilArgv[argc++] = "-c";
350 rilArgv[argc++] = clientId;
351 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
352
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800353 // Make sure there's a reasonable argv[0]
354 rilArgv[0] = argv[0];
355
356 funcs = rilInit(&s_rilEnv, argc, rilArgv);
Etan Cohend3652192014-06-20 08:28:44 -0700357 RLOGD("RIL_Init rilInit completed");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800358
359 RIL_register(funcs);
360
Etan Cohend3652192014-06-20 08:28:44 -0700361 RLOGD("RIL_Init RIL_register completed");
362
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200363 if (rilUimInit) {
364 RLOGD("RIL_register_socket started");
365 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
366 }
367
368 RLOGD("RIL_register_socket completed");
369
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800370done:
371
Etan Cohend3652192014-06-20 08:28:44 -0700372 RLOGD("RIL_Init starting sleep loop");
Elliott Hughes164d2052013-11-20 14:53:08 -0800373 while (true) {
374 sleep(UINT32_MAX);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800375 }
376}