1 /* //device/system/rild/rild.c
3 ** Copyright 2006 The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
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>
32 #include <sys/capability.h>
33 #include <sys/prctl.h>
35 #include <sys/types.h>
36 #include <libril/ril_ex.h>
38 #include <private/android_filesystem_config.h>
39 #include "hardware/qemu_pipe.h"
41 #define LIB_PATH_PROPERTY "rild.libpath"
42 #define LIB_ARGS_PROPERTY "rild.libargs"
43 #define MAX_LIB_ARGS 16
44 #define MAX_CAP_NUM (CAP_TO_INDEX(CAP_LAST_CAP) + 1)
46 static void usage(const char *argv0) {
47 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
51 extern char rild[MAX_SOCKET_NAME_LENGTH];
53 extern void RIL_register (const RIL_RadioFunctions *callbacks);
55 extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit)
56 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
58 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
59 void *response, size_t responselen);
61 extern void RIL_onRequestAck(RIL_Token t);
63 extern void RIL_setRilSocketName(char *);
65 #if defined(ANDROID_MULTI_SIM)
66 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
67 size_t datalen, RIL_SOCKET_ID socket_id);
69 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
73 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
74 void *param, const struct timeval *relativeTime);
77 static struct RIL_Env s_rilEnv = {
78 RIL_onRequestComplete,
79 RIL_onUnsolicitedResponse,
80 RIL_requestTimedCallback,
84 extern void RIL_startEventLoop();
86 static int make_argv(char * args, char ** argv) {
87 // Note: reserve argv[0]
92 while ((tok = strtok(s, " \0"))) {
101 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
102 * Our group, cache, was set by init.
105 char debuggable[PROP_VALUE_MAX];
107 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
108 if (setresuid(AID_RADIO, AID_RADIO, AID_RADIO) == -1) {
109 RLOGE("setresuid failed: %s", strerror(errno));
113 struct __user_cap_header_struct header;
114 memset(&header, 0, sizeof(header));
115 header.version = _LINUX_CAPABILITY_VERSION_3;
118 struct __user_cap_data_struct data[MAX_CAP_NUM];
119 memset(&data, 0, sizeof(data));
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);
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);
127 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);
130 if (capset(&header, &data[0]) == -1) {
131 RLOGE("capset failed: %s", strerror(errno));
136 * Debuggable build only:
137 * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash
139 property_get("ro.debuggable", debuggable, "0");
140 if (strcmp(debuggable, "1") == 0) {
141 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
145 int main(int argc, char **argv) {
146 const char * rilLibPath = NULL;
149 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
150 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
151 char *err_str = NULL;
153 const RIL_RadioFunctions *funcs;
154 char libPath[PROPERTY_VALUE_MAX];
155 unsigned char hasLibArgs = 0;
158 const char *clientId = NULL;
159 RLOGD("**RIL Daemon Started**");
160 RLOGD("**RILd param count=%d**", argc);
162 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
163 for (i = 1; i < argc ;) {
164 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
165 rilLibPath = argv[i + 1];
167 } else if (0 == strcmp(argv[i], "--")) {
171 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
172 clientId = argv[i+1];
179 if (clientId == NULL) {
181 } else if (atoi(clientId) >= MAX_RILDS) {
182 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
185 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
186 RIL_setRilSocketName(strncat(rild, clientId, MAX_SOCKET_NAME_LENGTH));
189 if (rilLibPath == NULL) {
190 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
191 // No lib sepcified on the command line, and nothing set in props.
192 // Assume "no-ril" case.
195 rilLibPath = libPath;
199 /* special override when in the emulator */
202 static char* arg_overrides[5];
203 static char arg_device[32];
206 #define REFERENCE_RIL_PATH "libreference-ril.so"
208 /* first, read /proc/cmdline into memory */
209 char buffer[1024] = {'\0'}, *p, *q;
211 int fd = open("/proc/cmdline",O_RDONLY);
214 RLOGD("could not open /proc/cmdline:%s", strerror(errno));
219 len = read(fd,buffer,sizeof(buffer)); }
220 while (len == -1 && errno == EINTR);
223 RLOGD("could not read /proc/cmdline:%s", strerror(errno));
229 if (strstr(buffer, "android.qemud=") != NULL)
231 /* the qemud daemon is launched after rild, so
232 * give it some time to create its GSM socket
235 #define QEMUD_SOCKET_NAME "qemud"
242 fd = qemu_pipe_open("qemud:gsm");
244 fd = socket_local_client(
246 ANDROID_SOCKET_NAMESPACE_RESERVED,
251 snprintf( arg_device, sizeof(arg_device), "%s/%s",
252 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
254 arg_overrides[1] = "-s";
255 arg_overrides[2] = arg_device;
259 RLOGD("could not connect to %s socket: %s",
260 QEMUD_SOCKET_NAME, strerror(errno));
265 RLOGE("could not connect to %s socket (giving up): %s",
266 QEMUD_SOCKET_NAME, strerror(errno));
272 /* otherwise, try to see if we passed a device name from the kernel */
274 #define KERNEL_OPTION "android.ril="
275 #define DEV_PREFIX "/dev/"
277 p = strstr( buffer, KERNEL_OPTION );
281 p += sizeof(KERNEL_OPTION)-1;
282 q = strpbrk( p, " \t\n\r" );
286 snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
287 arg_device[sizeof(arg_device)-1] = 0;
288 arg_overrides[1] = "-d";
289 arg_overrides[2] = arg_device;
295 argv = arg_overrides;
299 rilLibPath = REFERENCE_RIL_PATH;
301 RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
308 dlHandle = dlopen(rilLibPath, RTLD_NOW);
310 if (dlHandle == NULL) {
311 RLOGE("dlopen failed: %s", dlerror());
315 RIL_startEventLoop();
318 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
319 dlsym(dlHandle, "RIL_Init");
321 if (rilInit == NULL) {
322 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
326 dlerror(); // Clear any previous dlerror
328 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
329 dlsym(dlHandle, "RIL_SAP_Init");
332 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
333 } else if (!rilUimInit) {
334 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
338 rilArgv = argv + i - 1;
341 static char * newArgv[MAX_LIB_ARGS];
342 static char args[PROPERTY_VALUE_MAX];
344 property_get(LIB_ARGS_PROPERTY, args, "");
345 argc = make_argv(args, rilArgv);
348 rilArgv[argc++] = "-c";
349 rilArgv[argc++] = clientId;
350 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
352 // Make sure there's a reasonable argv[0]
353 rilArgv[0] = argv[0];
355 funcs = rilInit(&s_rilEnv, argc, rilArgv);
356 RLOGD("RIL_Init rilInit completed");
360 RLOGD("RIL_Init RIL_register completed");
363 RLOGD("RIL_register_socket started");
364 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
367 RLOGD("RIL_register_socket completed");
371 RLOGD("RIL_Init starting sleep loop");