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 |
| 43 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 44 | static void usage(const char *argv0) { |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 45 | 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] | 46 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 49 | extern char rild[MAX_SOCKET_NAME_LENGTH]; |
| 50 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 51 | extern void RIL_register (const RIL_RadioFunctions *callbacks); |
| 52 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 53 | extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit) |
| 54 | (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv); |
| 55 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 56 | extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 57 | void *response, size_t responselen); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 58 | |
Sanket Padawe | 6ff9a87 | 2016-01-27 15:09:12 -0800 | [diff] [blame] | 59 | extern void RIL_onRequestAck(RIL_Token t); |
| 60 | |
Elliott Hughes | e102e95 | 2015-02-20 10:52:14 -0800 | [diff] [blame] | 61 | extern void RIL_setRilSocketName(char *); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 62 | |
| 63 | #if defined(ANDROID_MULTI_SIM) |
| 64 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 65 | size_t datalen, RIL_SOCKET_ID socket_id); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 66 | #else |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 67 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 68 | size_t datalen); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 69 | #endif |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 70 | |
Wink Saville | 2932f31 | 2010-10-07 16:35:15 -0700 | [diff] [blame] | 71 | extern void RIL_requestTimedCallback (RIL_TimedCallback callback, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 72 | void *param, const struct timeval *relativeTime); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 73 | |
| 74 | |
| 75 | static struct RIL_Env s_rilEnv = { |
| 76 | RIL_onRequestComplete, |
| 77 | RIL_onUnsolicitedResponse, |
Sanket Padawe | 6ff9a87 | 2016-01-27 15:09:12 -0800 | [diff] [blame] | 78 | RIL_requestTimedCallback, |
| 79 | RIL_onRequestAck |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | extern void RIL_startEventLoop(); |
| 83 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 84 | static int make_argv(char * args, char ** argv) { |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 85 | // Note: reserve argv[0] |
| 86 | int count = 1; |
| 87 | char * tok; |
| 88 | char * s = args; |
| 89 | |
| 90 | while ((tok = strtok(s, " \0"))) { |
| 91 | argv[count] = tok; |
| 92 | s = NULL; |
| 93 | count++; |
| 94 | } |
| 95 | return count; |
| 96 | } |
| 97 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 98 | int main(int argc, char **argv) { |
Jorge Lucangeli Obes | eaa7b3c | 2016-04-18 17:58:04 -0700 | [diff] [blame] | 99 | const char *rilLibPath = NULL; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 100 | char **rilArgv; |
| 101 | void *dlHandle; |
| 102 | const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); |
Sanket Padawe | f0c8ca7 | 2016-06-30 15:01:08 -0700 | [diff] [blame] | 103 | RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **); |
Jorge Lucangeli Obes | eaa7b3c | 2016-04-18 17:58:04 -0700 | [diff] [blame] | 104 | const char *err_str = NULL; |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 105 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 106 | const RIL_RadioFunctions *funcs; |
| 107 | char libPath[PROPERTY_VALUE_MAX]; |
| 108 | unsigned char hasLibArgs = 0; |
| 109 | |
| 110 | int i; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 111 | const char *clientId = NULL; |
| 112 | RLOGD("**RIL Daemon Started**"); |
| 113 | RLOGD("**RILd param count=%d**", argc); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 114 | |
Nick Kralevich | aea7d5b | 2010-12-10 15:53:02 -0800 | [diff] [blame] | 115 | 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] | 116 | for (i = 1; i < argc ;) { |
| 117 | if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { |
| 118 | rilLibPath = argv[i + 1]; |
| 119 | i += 2; |
| 120 | } else if (0 == strcmp(argv[i], "--")) { |
| 121 | i++; |
| 122 | hasLibArgs = 1; |
| 123 | break; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 124 | } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) { |
| 125 | clientId = argv[i+1]; |
| 126 | i += 2; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 127 | } else { |
| 128 | usage(argv[0]); |
| 129 | } |
| 130 | } |
| 131 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 132 | if (clientId == NULL) { |
| 133 | clientId = "0"; |
| 134 | } else if (atoi(clientId) >= MAX_RILDS) { |
| 135 | RLOGE("Max Number of rild's supported is: %d", MAX_RILDS); |
| 136 | exit(0); |
| 137 | } |
| 138 | if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) { |
Keun Soo Yim | 1973685 | 2016-04-13 17:11:20 -0700 | [diff] [blame] | 139 | strlcat(rild, clientId, MAX_SOCKET_NAME_LENGTH); |
Keun Soo Yim | 1067bb6 | 2016-04-13 19:06:26 -0700 | [diff] [blame] | 140 | RIL_setRilSocketName(rild); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 141 | } |
| 142 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 143 | if (rilLibPath == NULL) { |
| 144 | if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { |
| 145 | // No lib sepcified on the command line, and nothing set in props. |
| 146 | // Assume "no-ril" case. |
| 147 | goto done; |
| 148 | } else { |
| 149 | rilLibPath = libPath; |
| 150 | } |
| 151 | } |
| 152 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 153 | dlHandle = dlopen(rilLibPath, RTLD_NOW); |
| 154 | |
| 155 | if (dlHandle == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 156 | RLOGE("dlopen failed: %s", dlerror()); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 157 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | RIL_startEventLoop(); |
| 161 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 162 | rilInit = |
| 163 | (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) |
| 164 | dlsym(dlHandle, "RIL_Init"); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 165 | |
| 166 | if (rilInit == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 167 | RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 168 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 171 | dlerror(); // Clear any previous dlerror |
| 172 | rilUimInit = |
Sanket Padawe | f0c8ca7 | 2016-06-30 15:01:08 -0700 | [diff] [blame] | 173 | (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 174 | dlsym(dlHandle, "RIL_SAP_Init"); |
| 175 | err_str = dlerror(); |
| 176 | if (err_str) { |
| 177 | RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str); |
| 178 | } else if (!rilUimInit) { |
| 179 | RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath); |
| 180 | } |
| 181 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 182 | if (hasLibArgs) { |
| 183 | rilArgv = argv + i - 1; |
| 184 | argc = argc -i + 1; |
| 185 | } else { |
| 186 | static char * newArgv[MAX_LIB_ARGS]; |
| 187 | static char args[PROPERTY_VALUE_MAX]; |
| 188 | rilArgv = newArgv; |
| 189 | property_get(LIB_ARGS_PROPERTY, args, ""); |
| 190 | argc = make_argv(args, rilArgv); |
| 191 | } |
| 192 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 193 | rilArgv[argc++] = "-c"; |
Jorge Lucangeli Obes | eaa7b3c | 2016-04-18 17:58:04 -0700 | [diff] [blame] | 194 | rilArgv[argc++] = (char*)clientId; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 195 | RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); |
| 196 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 197 | // Make sure there's a reasonable argv[0] |
| 198 | rilArgv[0] = argv[0]; |
| 199 | |
| 200 | funcs = rilInit(&s_rilEnv, argc, rilArgv); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 201 | RLOGD("RIL_Init rilInit completed"); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 202 | |
| 203 | RIL_register(funcs); |
| 204 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 205 | RLOGD("RIL_Init RIL_register completed"); |
| 206 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 207 | if (rilUimInit) { |
| 208 | RLOGD("RIL_register_socket started"); |
| 209 | RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv); |
| 210 | } |
| 211 | |
| 212 | RLOGD("RIL_register_socket completed"); |
| 213 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 214 | done: |
| 215 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 216 | RLOGD("RIL_Init starting sleep loop"); |
Elliott Hughes | 164d205 | 2013-11-20 14:53:08 -0800 | [diff] [blame] | 217 | while (true) { |
| 218 | sleep(UINT32_MAX); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 219 | } |
| 220 | } |