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_setRilSocketName(char *);
63 #if defined(ANDROID_MULTI_SIM)
64 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
65 size_t datalen, RIL_SOCKET_ID socket_id);
67 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
71 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
72 void *param, const struct timeval *relativeTime);
75 static struct RIL_Env s_rilEnv = {
76 RIL_onRequestComplete,
77 RIL_onUnsolicitedResponse,
78 RIL_requestTimedCallback
81 extern void RIL_startEventLoop();
83 static int make_argv(char * args, char ** argv) {
84 // Note: reserve argv[0]
89 while ((tok = strtok(s, " \0"))) {
98 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
99 * Our group, cache, was set by init.
102 char debuggable[PROP_VALUE_MAX];
104 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
107 struct __user_cap_header_struct header;
108 memset(&header, 0, sizeof(header));
109 header.version = _LINUX_CAPABILITY_VERSION_3;
112 struct __user_cap_data_struct data[MAX_CAP_NUM];
113 memset(&data, 0, sizeof(data));
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);
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);
121 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);
124 if (capset(&header, &data[0]) == -1) {
125 RLOGE("capset failed: %s", strerror(errno));
130 * Debuggable build only:
131 * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash
133 property_get("ro.debuggable", debuggable, "0");
134 if (strcmp(debuggable, "1") == 0) {
135 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
139 int main(int argc, char **argv) {
140 const char * rilLibPath = NULL;
143 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
144 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
145 char *err_str = NULL;
147 const RIL_RadioFunctions *funcs;
148 char libPath[PROPERTY_VALUE_MAX];
149 unsigned char hasLibArgs = 0;
152 const char *clientId = NULL;
153 RLOGD("**RIL Daemon Started**");
154 RLOGD("**RILd param count=%d**", argc);
156 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
157 for (i = 1; i < argc ;) {
158 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
159 rilLibPath = argv[i + 1];
161 } else if (0 == strcmp(argv[i], "--")) {
165 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
166 clientId = argv[i+1];
173 if (clientId == NULL) {
175 } else if (atoi(clientId) >= MAX_RILDS) {
176 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
179 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
180 RIL_setRilSocketName(strncat(rild, clientId, MAX_SOCKET_NAME_LENGTH));
183 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.
189 rilLibPath = libPath;
193 /* special override when in the emulator */
196 static char* arg_overrides[5];
197 static char arg_device[32];
200 #define REFERENCE_RIL_PATH "libreference-ril.so"
202 /* first, read /proc/cmdline into memory */
203 char buffer[1024], *p, *q;
205 int fd = open("/proc/cmdline",O_RDONLY);
208 RLOGD("could not open /proc/cmdline:%s", strerror(errno));
213 len = read(fd,buffer,sizeof(buffer)); }
214 while (len == -1 && errno == EINTR);
217 RLOGD("could not read /proc/cmdline:%s", strerror(errno));
223 if (strstr(buffer, "android.qemud=") != NULL)
225 /* the qemud daemon is launched after rild, so
226 * give it some time to create its GSM socket
229 #define QEMUD_SOCKET_NAME "qemud"
236 fd = qemu_pipe_open("qemud:gsm");
238 fd = socket_local_client(
240 ANDROID_SOCKET_NAMESPACE_RESERVED,
245 snprintf( arg_device, sizeof(arg_device), "%s/%s",
246 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
248 arg_overrides[1] = "-s";
249 arg_overrides[2] = arg_device;
253 RLOGD("could not connect to %s socket: %s",
254 QEMUD_SOCKET_NAME, strerror(errno));
259 RLOGE("could not connect to %s socket (giving up): %s",
260 QEMUD_SOCKET_NAME, strerror(errno));
266 /* otherwise, try to see if we passed a device name from the kernel */
268 #define KERNEL_OPTION "android.ril="
269 #define DEV_PREFIX "/dev/"
271 p = strstr( buffer, KERNEL_OPTION );
275 p += sizeof(KERNEL_OPTION)-1;
276 q = strpbrk( p, " \t\n\r" );
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;
289 argv = arg_overrides;
293 rilLibPath = REFERENCE_RIL_PATH;
295 RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
302 dlHandle = dlopen(rilLibPath, RTLD_NOW);
304 if (dlHandle == NULL) {
305 RLOGE("dlopen failed: %s", dlerror());
309 RIL_startEventLoop();
312 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
313 dlsym(dlHandle, "RIL_Init");
315 if (rilInit == NULL) {
316 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
320 dlerror(); // Clear any previous dlerror
322 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
323 dlsym(dlHandle, "RIL_SAP_Init");
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);
332 rilArgv = argv + i - 1;
335 static char * newArgv[MAX_LIB_ARGS];
336 static char args[PROPERTY_VALUE_MAX];
338 property_get(LIB_ARGS_PROPERTY, args, "");
339 argc = make_argv(args, rilArgv);
342 rilArgv[argc++] = "-c";
343 rilArgv[argc++] = clientId;
344 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
346 // Make sure there's a reasonable argv[0]
347 rilArgv[0] = argv[0];
349 funcs = rilInit(&s_rilEnv, argc, rilArgv);
350 RLOGD("RIL_Init rilInit completed");
354 RLOGD("RIL_Init RIL_register completed");
357 RLOGD("RIL_register_socket started");
358 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
361 RLOGD("RIL_register_socket completed");
365 RLOGD("RIL_Init starting sleep loop");