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 <linux/capability.h>
33 #include <linux/prctl.h>
35 #include <private/android_filesystem_config.h>
37 #define LIB_PATH_PROPERTY "rild.libpath"
38 #define LIB_ARGS_PROPERTY "rild.libargs"
39 #define MAX_LIB_ARGS 16
41 static void usage(const char *argv0)
43 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
47 extern void RIL_register (const RIL_RadioFunctions *callbacks);
49 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
50 void *response, size_t responselen);
52 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
55 extern void* RIL_requestTimedCallback (RIL_TimedCallback callback,
56 void *param, const struct timeval *relativeTime);
58 extern void RIL_removeTimedCallback(void *callbackInfo);
60 static struct RIL_Env s_rilEnv = {
61 RIL_onRequestComplete,
62 RIL_onUnsolicitedResponse,
63 RIL_requestTimedCallback,
64 RIL_removeTimedCallback
67 extern void RIL_startEventLoop();
69 static int make_argv(char * args, char ** argv)
71 // Note: reserve argv[0]
76 while ((tok = strtok(s, " \0"))) {
85 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
86 * Our group, cache, was set by init.
89 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
92 struct __user_cap_header_struct header;
93 struct __user_cap_data_struct cap;
94 header.version = _LINUX_CAPABILITY_VERSION;
96 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
98 capset(&header, &cap);
101 int main(int argc, char **argv)
103 const char * rilLibPath = NULL;
106 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
107 const RIL_RadioFunctions *funcs;
108 char libPath[PROPERTY_VALUE_MAX];
109 unsigned char hasLibArgs = 0;
113 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
114 for (i = 1; i < argc ;) {
115 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
116 rilLibPath = argv[i + 1];
118 } else if (0 == strcmp(argv[i], "--")) {
127 if (rilLibPath == NULL) {
128 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
129 // No lib sepcified on the command line, and nothing set in props.
130 // Assume "no-ril" case.
133 rilLibPath = libPath;
137 /* special override when in the emulator */
140 static char* arg_overrides[3];
141 static char arg_device[32];
144 #define REFERENCE_RIL_PATH "/system/lib/libreference-ril.so"
146 /* first, read /proc/cmdline into memory */
147 char buffer[1024], *p, *q;
149 int fd = open("/proc/cmdline",O_RDONLY);
152 LOGD("could not open /proc/cmdline:%s", strerror(errno));
157 len = read(fd,buffer,sizeof(buffer)); }
158 while (len == -1 && errno == EINTR);
161 LOGD("could not read /proc/cmdline:%s", strerror(errno));
167 if (strstr(buffer, "android.qemud=") != NULL)
169 /* the qemud daemon is launched after rild, so
170 * give it some time to create its GSM socket
173 #define QEMUD_SOCKET_NAME "qemud"
180 fd = socket_local_client(
182 ANDROID_SOCKET_NAMESPACE_RESERVED,
187 snprintf( arg_device, sizeof(arg_device), "%s/%s",
188 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
190 arg_overrides[1] = "-s";
191 arg_overrides[2] = arg_device;
195 LOGD("could not connect to %s socket: %s",
196 QEMUD_SOCKET_NAME, strerror(errno));
201 LOGE("could not connect to %s socket (giving up): %s",
202 QEMUD_SOCKET_NAME, strerror(errno));
208 /* otherwise, try to see if we passed a device name from the kernel */
210 #define KERNEL_OPTION "android.ril="
211 #define DEV_PREFIX "/dev/"
213 p = strstr( buffer, KERNEL_OPTION );
217 p += sizeof(KERNEL_OPTION)-1;
218 q = strpbrk( p, " \t\n\r" );
222 snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
223 arg_device[sizeof(arg_device)-1] = 0;
224 arg_overrides[1] = "-d";
225 arg_overrides[2] = arg_device;
231 argv = arg_overrides;
235 rilLibPath = REFERENCE_RIL_PATH;
237 LOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
244 dlHandle = dlopen(rilLibPath, RTLD_NOW);
246 if (dlHandle == NULL) {
247 fprintf(stderr, "dlopen failed: %s\n", dlerror());
251 RIL_startEventLoop();
253 rilInit = (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))dlsym(dlHandle, "RIL_Init");
255 if (rilInit == NULL) {
256 fprintf(stderr, "RIL_Init not defined or exported in %s\n", rilLibPath);
261 rilArgv = argv + i - 1;
264 static char * newArgv[MAX_LIB_ARGS];
265 static char args[PROPERTY_VALUE_MAX];
267 property_get(LIB_ARGS_PROPERTY, args, "");
268 argc = make_argv(args, rilArgv);
271 // Make sure there's a reasonable argv[0]
272 rilArgv[0] = argv[0];
274 funcs = rilInit(&s_rilEnv, argc, rilArgv);
281 // sleep(UINT32_MAX) seems to return immediately on bionic