blob: 12fda884a19b3818926d9106bdf00172451c980b [file] [log] [blame]
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001/* //device/system/rild/rild.c
2**
Etan Cohend3652192014-06-20 08:28:44 -07003** Copyright 2006 The Android Open Source Project
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08004**
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>
Nick Kralevichfe4e1ec2013-02-28 14:16:41 -080032#include <sys/capability.h>
Elliott Hughes5d0cb9e2014-07-18 17:55:52 -070033#include <sys/prctl.h>
Andreas Gampe8ded9962015-04-06 12:20:24 -070034#include <sys/stat.h>
35#include <sys/types.h>
Dheeraj Shetty27976c42014-07-02 21:27:57 +020036#include <libril/ril_ex.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080037
38#include <private/android_filesystem_config.h>
39
40#define LIB_PATH_PROPERTY "rild.libpath"
41#define LIB_ARGS_PROPERTY "rild.libargs"
42#define MAX_LIB_ARGS 16
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -080043#define MAX_CAP_NUM (CAP_TO_INDEX(CAP_LAST_CAP) + 1)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080044
Dheeraj Shetty27976c42014-07-02 21:27:57 +020045static void usage(const char *argv0) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080046 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
Elliott Hughese2a70cf2013-11-20 13:51:45 -080047 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080048}
49
Etan Cohend3652192014-06-20 08:28:44 -070050extern char rild[MAX_SOCKET_NAME_LENGTH];
51
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080052extern void RIL_register (const RIL_RadioFunctions *callbacks);
53
Dheeraj Shetty27976c42014-07-02 21:27:57 +020054extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit)
55 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
56
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080057extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020058 void *response, size_t responselen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080059
Sanket Padawe6ff9a872016-01-27 15:09:12 -080060extern void RIL_onRequestAck(RIL_Token t);
61
Elliott Hughese102e952015-02-20 10:52:14 -080062extern void RIL_setRilSocketName(char *);
Etan Cohend3652192014-06-20 08:28:44 -070063
64#if defined(ANDROID_MULTI_SIM)
65extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020066 size_t datalen, RIL_SOCKET_ID socket_id);
Etan Cohend3652192014-06-20 08:28:44 -070067#else
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080068extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020069 size_t datalen);
Etan Cohend3652192014-06-20 08:28:44 -070070#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080071
Wink Saville2932f312010-10-07 16:35:15 -070072extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020073 void *param, const struct timeval *relativeTime);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080074
75
76static struct RIL_Env s_rilEnv = {
77 RIL_onRequestComplete,
78 RIL_onUnsolicitedResponse,
Sanket Padawe6ff9a872016-01-27 15:09:12 -080079 RIL_requestTimedCallback,
80 RIL_onRequestAck
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080081};
82
83extern void RIL_startEventLoop();
84
Dheeraj Shetty27976c42014-07-02 21:27:57 +020085static int make_argv(char * args, char ** argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080086 // Note: reserve argv[0]
87 int count = 1;
88 char * tok;
89 char * s = args;
90
91 while ((tok = strtok(s, " \0"))) {
92 argv[count] = tok;
93 s = NULL;
94 count++;
95 }
96 return count;
97}
98
99/*
100 * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
101 * Our group, cache, was set by init.
102 */
103void switchUser() {
Julien Vuillaumierfaf46762015-01-22 13:42:58 +0000104 char debuggable[PROP_VALUE_MAX];
105
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800106 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
Keun Soo Yimf1e9cf92016-04-13 17:17:52 -0700107 if (setresuid(AID_RADIO, AID_RADIO, AID_RADIO) == -1) {
108 RLOGE("setresuid failed: %s", strerror(errno));
109 exit(EXIT_FAILURE);
110 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800111
112 struct __user_cap_header_struct header;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800113 memset(&header, 0, sizeof(header));
114 header.version = _LINUX_CAPABILITY_VERSION_3;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800115 header.pid = 0;
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800116
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -0800117 struct __user_cap_data_struct data[MAX_CAP_NUM];
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800118 memset(&data, 0, sizeof(data));
119
120 data[CAP_TO_INDEX(CAP_NET_ADMIN)].effective |= CAP_TO_MASK(CAP_NET_ADMIN);
121 data[CAP_TO_INDEX(CAP_NET_ADMIN)].permitted |= CAP_TO_MASK(CAP_NET_ADMIN);
122
123 data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
124 data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
125
Sukanya Rajkhowa40aa1262015-01-14 17:56:25 -0800126 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].effective |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
127 data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].permitted |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
128
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800129 if (capset(&header, &data[0]) == -1) {
130 RLOGE("capset failed: %s", strerror(errno));
131 exit(EXIT_FAILURE);
132 }
Julien Vuillaumierfaf46762015-01-22 13:42:58 +0000133
134 /*
135 * Debuggable build only:
136 * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash
137 */
138 property_get("ro.debuggable", debuggable, "0");
139 if (strcmp(debuggable, "1") == 0) {
140 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
141 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800142}
143
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200144int main(int argc, char **argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800145 const char * rilLibPath = NULL;
146 char **rilArgv;
147 void *dlHandle;
148 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200149 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200150 const char *err_str = NULL;
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200151
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800152 const RIL_RadioFunctions *funcs;
153 char libPath[PROPERTY_VALUE_MAX];
154 unsigned char hasLibArgs = 0;
155
156 int i;
Etan Cohend3652192014-06-20 08:28:44 -0700157 const char *clientId = NULL;
158 RLOGD("**RIL Daemon Started**");
159 RLOGD("**RILd param count=%d**", argc);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800160
Nick Kralevichaea7d5b2010-12-10 15:53:02 -0800161 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800162 for (i = 1; i < argc ;) {
163 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
164 rilLibPath = argv[i + 1];
165 i += 2;
166 } else if (0 == strcmp(argv[i], "--")) {
167 i++;
168 hasLibArgs = 1;
169 break;
Etan Cohend3652192014-06-20 08:28:44 -0700170 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
171 clientId = argv[i+1];
172 i += 2;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800173 } else {
174 usage(argv[0]);
175 }
176 }
177
Etan Cohend3652192014-06-20 08:28:44 -0700178 if (clientId == NULL) {
179 clientId = "0";
180 } else if (atoi(clientId) >= MAX_RILDS) {
181 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
182 exit(0);
183 }
184 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
Keun Soo Yim19736852016-04-13 17:11:20 -0700185 strlcat(rild, clientId, MAX_SOCKET_NAME_LENGTH);
Keun Soo Yim1067bb62016-04-13 19:06:26 -0700186 RIL_setRilSocketName(rild);
Etan Cohend3652192014-06-20 08:28:44 -0700187 }
188
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800189 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.
193 goto done;
194 } else {
195 rilLibPath = libPath;
196 }
197 }
198
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800199 switchUser();
200
201 dlHandle = dlopen(rilLibPath, RTLD_NOW);
202
203 if (dlHandle == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800204 RLOGE("dlopen failed: %s", dlerror());
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800205 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800206 }
207
208 RIL_startEventLoop();
209
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200210 rilInit =
211 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
212 dlsym(dlHandle, "RIL_Init");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800213
214 if (rilInit == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800215 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800216 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800217 }
218
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200219 dlerror(); // Clear any previous dlerror
220 rilUimInit =
221 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
222 dlsym(dlHandle, "RIL_SAP_Init");
223 err_str = dlerror();
224 if (err_str) {
225 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
226 } else if (!rilUimInit) {
227 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
228 }
229
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800230 if (hasLibArgs) {
231 rilArgv = argv + i - 1;
232 argc = argc -i + 1;
233 } else {
234 static char * newArgv[MAX_LIB_ARGS];
235 static char args[PROPERTY_VALUE_MAX];
236 rilArgv = newArgv;
237 property_get(LIB_ARGS_PROPERTY, args, "");
238 argc = make_argv(args, rilArgv);
239 }
240
Etan Cohend3652192014-06-20 08:28:44 -0700241 rilArgv[argc++] = "-c";
242 rilArgv[argc++] = clientId;
243 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
244
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800245 // Make sure there's a reasonable argv[0]
246 rilArgv[0] = argv[0];
247
248 funcs = rilInit(&s_rilEnv, argc, rilArgv);
Etan Cohend3652192014-06-20 08:28:44 -0700249 RLOGD("RIL_Init rilInit completed");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800250
251 RIL_register(funcs);
252
Etan Cohend3652192014-06-20 08:28:44 -0700253 RLOGD("RIL_Init RIL_register completed");
254
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200255 if (rilUimInit) {
256 RLOGD("RIL_register_socket started");
257 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
258 }
259
260 RLOGD("RIL_register_socket completed");
261
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800262done:
263
Etan Cohend3652192014-06-20 08:28:44 -0700264 RLOGD("RIL_Init starting sleep loop");
Elliott Hughes164d2052013-11-20 14:53:08 -0800265 while (true) {
266 sleep(UINT32_MAX);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800267 }
268}