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 <log/log.h>
30 #include <cutils/properties.h>
31 #include <cutils/sockets.h>
32 #include <sys/capability.h>
33 #include <sys/prctl.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 
37 #if defined(PRODUCT_COMPATIBLE_PROPERTY)
38 #define LIB_PATH_PROPERTY   "vendor.rild.libpath"
39 #define LIB_ARGS_PROPERTY   "vendor.rild.libargs"
40 #else
41 #define LIB_PATH_PROPERTY   "rild.libpath"
42 #define LIB_ARGS_PROPERTY   "rild.libargs"
43 #endif
44 #define MAX_LIB_ARGS        16
45 
usage(const char * argv0)46 static void usage(const char *argv0) {
47     fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
48     exit(EXIT_FAILURE);
49 }
50 
51 typedef enum {
52     RIL_TELEPHONY_SOCKET,
53     RIL_SAP_SOCKET
54 } RIL_SOCKET_TYPE;
55 
56 extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH];
57 extern char ril_service_name[MAX_SERVICE_NAME_LENGTH];
58 
59 extern void RIL_register (const RIL_RadioFunctions *callbacks);
60 extern void rilc_thread_pool ();
61 
62 extern void RIL_register_socket (const RIL_RadioFunctions *(*rilUimInit)
63         (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
64 
65 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
66         void *response, size_t responselen);
67 
68 extern void RIL_onRequestAck(RIL_Token t);
69 
70 #if defined(ANDROID_MULTI_SIM)
71 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
72         size_t datalen, RIL_SOCKET_ID socket_id);
73 #else
74 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
75         size_t datalen);
76 #endif
77 
78 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
79         void *param, const struct timeval *relativeTime);
80 
81 
82 static struct RIL_Env s_rilEnv = {
83     RIL_onRequestComplete,
84     RIL_onUnsolicitedResponse,
85     RIL_requestTimedCallback,
86     RIL_onRequestAck
87 };
88 
89 extern void RIL_startEventLoop();
90 
make_argv(char * args,char ** argv)91 static int make_argv(char * args, char ** argv) {
92     // Note: reserve argv[0]
93     int count = 1;
94     char * tok;
95     char * s = args;
96 
97     while ((tok = strtok(s, " \0"))) {
98         argv[count] = tok;
99         s = NULL;
100         count++;
101     }
102     return count;
103 }
104 
main(int argc,char ** argv)105 int main(int argc, char **argv) {
106     // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property
107     const char *rilLibPath = NULL;
108     // ril arguments either passed in as -- parameter, or read from rild.libargs property
109     char **rilArgv;
110     // handle for vendor ril lib
111     void *dlHandle;
112     // Pointer to ril init function in vendor ril
113     const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
114     // Pointer to sap init function in vendor ril
115     const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
116     const char *err_str = NULL;
117 
118     // functions returned by ril init function in vendor ril
119     const RIL_RadioFunctions *funcs;
120     // lib path from rild.libpath property (if it's read)
121     char libPath[PROPERTY_VALUE_MAX];
122     // flat to indicate if -- parameters are present
123     unsigned char hasLibArgs = 0;
124 
125     int i;
126     // ril/socket id received as -c parameter, otherwise set to 0
127     const char *clientId = NULL;
128 
129     RLOGD("**RIL Daemon Started**");
130     RLOGD("**RILd param count=%d**", argc);
131 
132     umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
133     for (i = 1; i < argc ;) {
134         if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
135             rilLibPath = argv[i + 1];
136             i += 2;
137         } else if (0 == strcmp(argv[i], "--")) {
138             i++;
139             hasLibArgs = 1;
140             break;
141         } else if (0 == strcmp(argv[i], "-c") &&  (argc - i > 1)) {
142             clientId = argv[i+1];
143             i += 2;
144         } else {
145             usage(argv[0]);
146         }
147     }
148 
149     if (clientId == NULL) {
150         clientId = "0";
151     } else if (atoi(clientId) >= MAX_RILDS) {
152         RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
153         exit(0);
154     }
155     if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
156         snprintf(ril_service_name, sizeof(ril_service_name), "%s%s", ril_service_name_base,
157                  clientId);
158     }
159 
160     if (rilLibPath == NULL) {
161         if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
162             // No lib sepcified on the command line, and nothing set in props.
163             // Assume "no-ril" case.
164             goto done;
165         } else {
166             rilLibPath = libPath;
167         }
168     }
169 
170     // force to use libcuttlefish-ril-2.so
171     rilLibPath = "libcuttlefish-ril-2.so";
172 
173     dlHandle = dlopen(rilLibPath, RTLD_NOW);
174 
175     if (dlHandle == NULL) {
176         RLOGE("dlopen failed: %s", dlerror());
177         exit(EXIT_FAILURE);
178     }
179 
180     RLOGI("dlopen good: %s", rilLibPath);
181 
182     RIL_startEventLoop();
183 
184     rilInit =
185         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
186         dlsym(dlHandle, "RIL_Init");
187 
188     if (rilInit == NULL) {
189         RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
190         exit(EXIT_FAILURE);
191     }
192 
193     dlerror(); // Clear any previous dlerror
194     rilUimInit =
195         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
196         dlsym(dlHandle, "RIL_SAP_Init");
197     err_str = dlerror();
198     if (err_str) {
199         RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
200     } else if (!rilUimInit) {
201         RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
202     }
203 
204     if (hasLibArgs) {
205         rilArgv = argv + i - 1;
206         argc = argc -i + 1;
207     } else {
208         static char * newArgv[MAX_LIB_ARGS];
209         static char args[PROPERTY_VALUE_MAX];
210         rilArgv = newArgv;
211         property_get(LIB_ARGS_PROPERTY, args, "");
212         argc = make_argv(args, rilArgv);
213     }
214 
215     rilArgv[argc++] = "-c";
216     rilArgv[argc++] = (char*)clientId;
217     RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
218 
219     // Make sure there's a reasonable argv[0]
220     rilArgv[0] = argv[0];
221 
222     funcs = rilInit(&s_rilEnv, argc, rilArgv);
223     RLOGD("RIL_Init rilInit completed");
224 
225     RIL_register(funcs);
226 
227     RLOGD("RIL_Init RIL_register completed");
228 
229     if (rilUimInit) {
230         RLOGD("RIL_register_socket started");
231         RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
232     }
233 
234     RLOGD("RIL_register_socket completed");
235 
236     rilc_thread_pool();
237 
238 done:
239     RLOGD("RIL_Init starting sleep loop");
240     while (true) {
241         sleep(UINT32_MAX);
242     }
243 }
244