The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 1 | /* //device/system/rild/rild.c |
| 2 | ** |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 3 | ** Copyright 2006 The Android Open Source Project |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 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> |
Nick Kralevich | fe4e1ec | 2013-02-28 14:16:41 -0800 | [diff] [blame] | 32 | #include <sys/capability.h> |
Elliott Hughes | 5d0cb9e | 2014-07-18 17:55:52 -0700 | [diff] [blame] | 33 | #include <sys/prctl.h> |
Andreas Gampe | 8ded996 | 2015-04-06 12:20:24 -0700 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 36 | #include <libril/ril_ex.h> |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 37 | |
| 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 Rajkhowa | 40aa126 | 2015-01-14 17:56:25 -0800 | [diff] [blame] | 43 | #define MAX_CAP_NUM (CAP_TO_INDEX(CAP_LAST_CAP) + 1) |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 44 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 45 | static void usage(const char *argv0) { |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 46 | fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 47 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 50 | extern char rild[MAX_SOCKET_NAME_LENGTH]; |
| 51 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 52 | extern void RIL_register (const RIL_RadioFunctions *callbacks); |
| 53 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 54 | extern 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 Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 57 | extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 58 | void *response, size_t responselen); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 59 | |
Sanket Padawe | 6ff9a87 | 2016-01-27 15:09:12 -0800 | [diff] [blame] | 60 | extern void RIL_onRequestAck(RIL_Token t); |
| 61 | |
Elliott Hughes | e102e95 | 2015-02-20 10:52:14 -0800 | [diff] [blame] | 62 | extern void RIL_setRilSocketName(char *); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 63 | |
| 64 | #if defined(ANDROID_MULTI_SIM) |
| 65 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 66 | size_t datalen, RIL_SOCKET_ID socket_id); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 67 | #else |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 68 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 69 | size_t datalen); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 70 | #endif |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 71 | |
Wink Saville | 2932f31 | 2010-10-07 16:35:15 -0700 | [diff] [blame] | 72 | extern void RIL_requestTimedCallback (RIL_TimedCallback callback, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 73 | void *param, const struct timeval *relativeTime); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 74 | |
| 75 | |
| 76 | static struct RIL_Env s_rilEnv = { |
| 77 | RIL_onRequestComplete, |
| 78 | RIL_onUnsolicitedResponse, |
Sanket Padawe | 6ff9a87 | 2016-01-27 15:09:12 -0800 | [diff] [blame] | 79 | RIL_requestTimedCallback, |
| 80 | RIL_onRequestAck |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | extern void RIL_startEventLoop(); |
| 84 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 85 | static int make_argv(char * args, char ** argv) { |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 86 | // 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 | */ |
| 103 | void switchUser() { |
Julien Vuillaumier | faf4676 | 2015-01-22 13:42:58 +0000 | [diff] [blame] | 104 | char debuggable[PROP_VALUE_MAX]; |
| 105 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 106 | prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
Keun Soo Yim | f1e9cf9 | 2016-04-13 17:17:52 -0700 | [diff] [blame] | 107 | 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 Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 111 | |
| 112 | struct __user_cap_header_struct header; |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 113 | memset(&header, 0, sizeof(header)); |
| 114 | header.version = _LINUX_CAPABILITY_VERSION_3; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 115 | header.pid = 0; |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 116 | |
Sukanya Rajkhowa | 40aa126 | 2015-01-14 17:56:25 -0800 | [diff] [blame] | 117 | struct __user_cap_data_struct data[MAX_CAP_NUM]; |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 118 | 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 Rajkhowa | 40aa126 | 2015-01-14 17:56:25 -0800 | [diff] [blame] | 126 | 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 Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 129 | if (capset(&header, &data[0]) == -1) { |
| 130 | RLOGE("capset failed: %s", strerror(errno)); |
| 131 | exit(EXIT_FAILURE); |
| 132 | } |
Julien Vuillaumier | faf4676 | 2015-01-22 13:42:58 +0000 | [diff] [blame] | 133 | |
| 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 Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 144 | int main(int argc, char **argv) { |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 145 | const char * rilLibPath = NULL; |
| 146 | char **rilArgv; |
| 147 | void *dlHandle; |
| 148 | const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 149 | const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **); |
David 'Digit' Turner | 834eca8 | 2016-06-22 12:10:01 +0200 | [diff] [blame] | 150 | const char *err_str = NULL; |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 151 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 152 | const RIL_RadioFunctions *funcs; |
| 153 | char libPath[PROPERTY_VALUE_MAX]; |
| 154 | unsigned char hasLibArgs = 0; |
| 155 | |
| 156 | int i; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 157 | const char *clientId = NULL; |
| 158 | RLOGD("**RIL Daemon Started**"); |
| 159 | RLOGD("**RILd param count=%d**", argc); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 160 | |
Nick Kralevich | aea7d5b | 2010-12-10 15:53:02 -0800 | [diff] [blame] | 161 | umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 162 | 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 Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 170 | } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) { |
| 171 | clientId = argv[i+1]; |
| 172 | i += 2; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 173 | } else { |
| 174 | usage(argv[0]); |
| 175 | } |
| 176 | } |
| 177 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 178 | 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 Yim | 1973685 | 2016-04-13 17:11:20 -0700 | [diff] [blame] | 185 | strlcat(rild, clientId, MAX_SOCKET_NAME_LENGTH); |
Keun Soo Yim | 1067bb6 | 2016-04-13 19:06:26 -0700 | [diff] [blame] | 186 | RIL_setRilSocketName(rild); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 187 | } |
| 188 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 189 | 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 Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 199 | switchUser(); |
| 200 | |
| 201 | dlHandle = dlopen(rilLibPath, RTLD_NOW); |
| 202 | |
| 203 | if (dlHandle == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 204 | RLOGE("dlopen failed: %s", dlerror()); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 205 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | RIL_startEventLoop(); |
| 209 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 210 | rilInit = |
| 211 | (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) |
| 212 | dlsym(dlHandle, "RIL_Init"); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 213 | |
| 214 | if (rilInit == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 215 | RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 216 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 219 | 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 Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 230 | 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 Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 241 | rilArgv[argc++] = "-c"; |
| 242 | rilArgv[argc++] = clientId; |
| 243 | RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); |
| 244 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 245 | // Make sure there's a reasonable argv[0] |
| 246 | rilArgv[0] = argv[0]; |
| 247 | |
| 248 | funcs = rilInit(&s_rilEnv, argc, rilArgv); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 249 | RLOGD("RIL_Init rilInit completed"); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 250 | |
| 251 | RIL_register(funcs); |
| 252 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 253 | RLOGD("RIL_Init RIL_register completed"); |
| 254 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 255 | 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 Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 262 | done: |
| 263 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 264 | RLOGD("RIL_Init starting sleep loop"); |
Elliott Hughes | 164d205 | 2013-11-20 14:53:08 -0800 | [diff] [blame] | 265 | while (true) { |
| 266 | sleep(UINT32_MAX); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 267 | } |
| 268 | } |