blob: 07477176e5f07ad5383786d14d8363f9efdb2ebb [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>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080036
37#include <private/android_filesystem_config.h>
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -070038#include "hardware/qemu_pipe.h"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080039
40#define LIB_PATH_PROPERTY "rild.libpath"
41#define LIB_ARGS_PROPERTY "rild.libargs"
42#define MAX_LIB_ARGS 16
43
44static void usage(const char *argv0)
45{
46 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
Elliott Hughese2a70cf2013-11-20 13:51:45 -080047 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080048}
49
Etan Cohend3652192014-06-20 08:28:44 -070050extern char rild[MAX_SOCKET_NAME_LENGTH];
51
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080052extern void RIL_register (const RIL_RadioFunctions *callbacks);
53
54extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
55 void *response, size_t responselen);
56
Elliott Hughese102e952015-02-20 10:52:14 -080057extern void RIL_setRilSocketName(char *);
Etan Cohend3652192014-06-20 08:28:44 -070058
59#if defined(ANDROID_MULTI_SIM)
60extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
61 size_t datalen, RIL_SOCKET_ID socket_id);
62#else
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080063extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
64 size_t datalen);
Etan Cohend3652192014-06-20 08:28:44 -070065#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080066
Wink Saville2932f312010-10-07 16:35:15 -070067extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080068 void *param, const struct timeval *relativeTime);
69
70
71static struct RIL_Env s_rilEnv = {
72 RIL_onRequestComplete,
73 RIL_onUnsolicitedResponse,
Wink Saville2932f312010-10-07 16:35:15 -070074 RIL_requestTimedCallback
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080075};
76
77extern void RIL_startEventLoop();
78
79static 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 */
98void switchUser() {
Julien Vuillaumierfaf46762015-01-22 13:42:58 +000099 char debuggable[PROP_VALUE_MAX];
100
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800101 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
102 setuid(AID_RADIO);
103
104 struct __user_cap_header_struct header;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800105 memset(&header, 0, sizeof(header));
106 header.version = _LINUX_CAPABILITY_VERSION_3;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800107 header.pid = 0;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800108
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 Vuillaumierfaf46762015-01-22 13:42:58 +0000122
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 Project00f06fc2009-03-03 19:32:15 -0800131}
132
133int 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 Cohend3652192014-06-20 08:28:44 -0700144 const char *clientId = NULL;
145 RLOGD("**RIL Daemon Started**");
146 RLOGD("**RILd param count=%d**", argc);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800147
Nick Kralevichaea7d5b2010-12-10 15:53:02 -0800148 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800149 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 Cohend3652192014-06-20 08:28:44 -0700157 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
158 clientId = argv[i+1];
159 i += 2;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800160 } else {
161 usage(argv[0]);
162 }
163 }
164
Etan Cohend3652192014-06-20 08:28:44 -0700165 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 Project00f06fc2009-03-03 19:32:15 -0800175 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 Cohend3652192014-06-20 08:28:44 -0700188 static char* arg_overrides[5];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800189 static char arg_device[32];
190 int done = 0;
191
Vince Harron60c1f5b2014-08-13 23:12:37 -0700192#define REFERENCE_RIL_PATH "libreference-ril.so"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800193
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 Saville8eb2a122012-11-19 16:05:13 -0800200 RLOGD("could not open /proc/cmdline:%s", strerror(errno));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800201 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 Saville8eb2a122012-11-19 16:05:13 -0800209 RLOGD("could not read /proc/cmdline:%s", strerror(errno));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800210 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 Projecte6e6fb22009-03-18 17:39:47 -0700221#define QEMUD_SOCKET_NAME "qemud"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800222
223 while (1) {
224 int fd;
225
226 sleep(1);
227
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -0700228 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 Project00f06fc2009-03-03 19:32:15 -0800235 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 Saville8eb2a122012-11-19 16:05:13 -0800245 RLOGD("could not connect to %s socket: %s",
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800246 QEMUD_SOCKET_NAME, strerror(errno));
247 if (--tries == 0)
248 break;
249 }
250 if (!done) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800251 RLOGE("could not connect to %s socket (giving up): %s",
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800252 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 Saville8eb2a122012-11-19 16:05:13 -0800287 RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800288 }
289 }
290OpenLib:
291#endif
292 switchUser();
293
294 dlHandle = dlopen(rilLibPath, RTLD_NOW);
295
296 if (dlHandle == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800297 RLOGE("dlopen failed: %s", dlerror());
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800298 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800299 }
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 Saville8eb2a122012-11-19 16:05:13 -0800306 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800307 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800308 }
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 Cohend3652192014-06-20 08:28:44 -0700321 rilArgv[argc++] = "-c";
322 rilArgv[argc++] = clientId;
323 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
324
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800325 // Make sure there's a reasonable argv[0]
326 rilArgv[0] = argv[0];
327
328 funcs = rilInit(&s_rilEnv, argc, rilArgv);
Etan Cohend3652192014-06-20 08:28:44 -0700329 RLOGD("RIL_Init rilInit completed");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800330
331 RIL_register(funcs);
332
Etan Cohend3652192014-06-20 08:28:44 -0700333 RLOGD("RIL_Init RIL_register completed");
334
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800335done:
336
Etan Cohend3652192014-06-20 08:28:44 -0700337 RLOGD("RIL_Init starting sleep loop");
Elliott Hughes164d2052013-11-20 14:53:08 -0800338 while (true) {
339 sleep(UINT32_MAX);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800340 }
341}