OSDN Git Service

df7853997e0ac93850a60c67af823bb80203af27
[android-x86/hardware-ril.git] / rild / rild.c
1 /* //device/system/rild/rild.c
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
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>
32 #include <linux/capability.h>
33 #include <linux/prctl.h>
34
35 #include <private/android_filesystem_config.h>
36
37 #define LIB_PATH_PROPERTY   "rild.libpath"
38 #define LIB_ARGS_PROPERTY   "rild.libargs"
39 #define MAX_LIB_ARGS        16
40
41 static void usage(const char *argv0)
42 {
43     fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
44     exit(-1);
45 }
46
47 extern void RIL_register (const RIL_RadioFunctions *callbacks);
48
49 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
50                            void *response, size_t responselen);
51
52 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
53                                 size_t datalen);
54
55 extern void* RIL_requestTimedCallback (RIL_TimedCallback callback,
56                                void *param, const struct timeval *relativeTime);
57
58 extern void RIL_removeTimedCallback(void *callbackInfo);
59
60 static struct RIL_Env s_rilEnv = {
61     RIL_onRequestComplete,
62     RIL_onUnsolicitedResponse,
63     RIL_requestTimedCallback,
64     RIL_removeTimedCallback
65 };
66
67 extern void RIL_startEventLoop();
68
69 static int make_argv(char * args, char ** argv)
70 {
71     // Note: reserve argv[0]
72     int count = 1;
73     char * tok;
74     char * s = args;
75
76     while ((tok = strtok(s, " \0"))) {
77         argv[count] = tok;
78         s = NULL;
79         count++;
80     }
81     return count;
82 }
83
84 /*
85  * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
86  * Our group, cache, was set by init.
87  */
88 void switchUser() {
89     prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
90     setuid(AID_RADIO);
91
92     struct __user_cap_header_struct header;
93     struct __user_cap_data_struct cap;
94     header.version = _LINUX_CAPABILITY_VERSION;
95     header.pid = 0;
96     cap.effective = cap.permitted = 1 << CAP_NET_ADMIN;
97     cap.inheritable = 0;
98     capset(&header, &cap);
99 }
100
101 int main(int argc, char **argv)
102 {
103     const char * rilLibPath = NULL;
104     char **rilArgv;
105     void *dlHandle;
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;
110
111     int i;
112
113     for (i = 1; i < argc ;) {
114         if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
115             rilLibPath = argv[i + 1];
116             i += 2;
117         } else if (0 == strcmp(argv[i], "--")) {
118             i++;
119             hasLibArgs = 1;
120             break;
121         } else {
122             usage(argv[0]);
123         }
124     }
125
126     if (rilLibPath == NULL) {
127         if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
128             // No lib sepcified on the command line, and nothing set in props.
129             // Assume "no-ril" case.
130             goto done;
131         } else {
132             rilLibPath = libPath;
133         }
134     }
135
136     /* special override when in the emulator */
137 #if 1
138     {
139         static char*  arg_overrides[3];
140         static char   arg_device[32];
141         int           done = 0;
142
143 #define  REFERENCE_RIL_PATH  "/system/lib/libreference-ril.so"
144
145         /* first, read /proc/cmdline into memory */
146         char          buffer[1024], *p, *q;
147         int           len;
148         int           fd = open("/proc/cmdline",O_RDONLY);
149
150         if (fd < 0) {
151             LOGD("could not open /proc/cmdline:%s", strerror(errno));
152             goto OpenLib;
153         }
154
155         do {
156             len = read(fd,buffer,sizeof(buffer)); }
157         while (len == -1 && errno == EINTR);
158
159         if (len < 0) {
160             LOGD("could not read /proc/cmdline:%s", strerror(errno));
161             close(fd);
162             goto OpenLib;
163         }
164         close(fd);
165
166         if (strstr(buffer, "android.qemud=") != NULL)
167         {
168             /* the qemud daemon is launched after rild, so
169             * give it some time to create its GSM socket
170             */
171             int  tries = 5;
172 #define  QEMUD_SOCKET_NAME    "qemud"
173
174             while (1) {
175                 int  fd;
176
177                 sleep(1);
178
179                 fd = socket_local_client(
180                             QEMUD_SOCKET_NAME,
181                             ANDROID_SOCKET_NAMESPACE_RESERVED,
182                             SOCK_STREAM );
183
184                 if (fd >= 0) {
185                     close(fd);
186                     snprintf( arg_device, sizeof(arg_device), "%s/%s",
187                                 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
188
189                     arg_overrides[1] = "-s";
190                     arg_overrides[2] = arg_device;
191                     done = 1;
192                     break;
193                 }
194                 LOGD("could not connect to %s socket: %s",
195                     QEMUD_SOCKET_NAME, strerror(errno));
196                 if (--tries == 0)
197                     break;
198             }
199             if (!done) {
200                 LOGE("could not connect to %s socket (giving up): %s",
201                     QEMUD_SOCKET_NAME, strerror(errno));
202                 while(1)
203                     sleep(0x00ffffff);
204             }
205         }
206
207         /* otherwise, try to see if we passed a device name from the kernel */
208         if (!done) do {
209 #define  KERNEL_OPTION  "android.ril="
210 #define  DEV_PREFIX     "/dev/"
211
212             p = strstr( buffer, KERNEL_OPTION );
213             if (p == NULL)
214                 break;
215
216             p += sizeof(KERNEL_OPTION)-1;
217             q  = strpbrk( p, " \t\n\r" );
218             if (q != NULL)
219                 *q = 0;
220
221             snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
222             arg_device[sizeof(arg_device)-1] = 0;
223             arg_overrides[1] = "-d";
224             arg_overrides[2] = arg_device;
225             done = 1;
226
227         } while (0);
228
229         if (done) {
230             argv = arg_overrides;
231             argc = 3;
232             i    = 1;
233             hasLibArgs = 1;
234             rilLibPath = REFERENCE_RIL_PATH;
235
236             LOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
237         }
238     }
239 OpenLib:
240 #endif
241     switchUser();
242
243     dlHandle = dlopen(rilLibPath, RTLD_NOW);
244
245     if (dlHandle == NULL) {
246         fprintf(stderr, "dlopen failed: %s\n", dlerror());
247         exit(-1);
248     }
249
250     RIL_startEventLoop();
251
252     rilInit = (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))dlsym(dlHandle, "RIL_Init");
253
254     if (rilInit == NULL) {
255         fprintf(stderr, "RIL_Init not defined or exported in %s\n", rilLibPath);
256         exit(-1);
257     }
258
259     if (hasLibArgs) {
260         rilArgv = argv + i - 1;
261         argc = argc -i + 1;
262     } else {
263         static char * newArgv[MAX_LIB_ARGS];
264         static char args[PROPERTY_VALUE_MAX];
265         rilArgv = newArgv;
266         property_get(LIB_ARGS_PROPERTY, args, "");
267         argc = make_argv(args, rilArgv);
268     }
269
270     // Make sure there's a reasonable argv[0]
271     rilArgv[0] = argv[0];
272
273     funcs = rilInit(&s_rilEnv, argc, rilArgv);
274
275     RIL_register(funcs);
276
277 done:
278
279     while(1) {
280         // sleep(UINT32_MAX) seems to return immediately on bionic
281         sleep(0x00ffffff);
282     }
283 }
284