blob: 3c6e8a45fe35ea845d2b893f99117c16fd7a555f [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes8d82ea02015-02-06 20:15:18 -080017#include <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <libgen.h>
Elliott Hughescc86fb22015-03-17 20:01:13 -070021#include <paths.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080022#include <signal.h>
23#include <stdarg.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <sys/mount.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/poll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070029#include <sys/socket.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080030#include <sys/stat.h>
31#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070032#include <sys/un.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080033#include <sys/wait.h>
34#include <termios.h>
35#include <unistd.h>
36
37#include <mtd/mtd-user.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#include <selinux/selinux.h>
40#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040041#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070043#include <cutils/android_reboot.h>
Alex Klyubin0d872d82013-08-16 13:19:24 -070044#include <cutils/fs.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080045#include <cutils/iosched_policy.h>
46#include <cutils/list.h>
47#include <cutils/sockets.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070048#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#include "devices.h"
51#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070052#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080054#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070055#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070056#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070057#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070058#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070059#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070060#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061
Stephen Smalleye46f9d52012-01-13 08:48:47 -050062struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040063struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050064
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065static int property_triggers_enabled = 0;
66
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069static char hardware[32];
70static unsigned revision = 0;
71static char qemu[32];
72
Colin Crossebc6ff12010-04-13 19:52:01 -070073static struct action *cur_action = NULL;
74static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070075
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080077static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078static time_t process_needs_restart;
79
80static const char *ENV[32];
81
Elliott Hughes8d82ea02015-02-06 20:15:18 -080082bool waiting_for_exec = false;
83
84void service::NotifyStateChange(const char* new_state) {
85 if (!properties_inited()) {
86 // If properties aren't available yet, we can't set them.
87 return;
88 }
89
90 if ((flags & SVC_EXEC) != 0) {
91 // 'exec' commands don't have properties tracking their state.
92 return;
93 }
94
95 char prop_name[PROP_NAME_MAX];
96 if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) {
97 // If the property name would be too long, we can't set it.
98 ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state);
99 return;
100 }
101
102 property_set(prop_name, new_state);
103}
104
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105/* add_environment - add "key=value" to the current environment */
106int add_environment(const char *key, const char *val)
107{
James Morrissey381341f2014-05-16 11:36:36 +0100108 size_t n;
109 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700110
James Morrissey381341f2014-05-16 11:36:36 +0100111 /* The last environment entry is reserved to terminate the list */
112 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
113
114 /* Delete any existing entry for this key */
115 if (ENV[n] != NULL) {
116 size_t entry_key_len = strcspn(ENV[n], "=");
117 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
118 free((char*)ENV[n]);
119 ENV[n] = NULL;
120 }
121 }
122
123 /* Add entry if a free slot is available */
124 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800125 char* entry;
126 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127 ENV[n] = entry;
128 return 0;
129 }
130 }
131
James Morrissey381341f2014-05-16 11:36:36 +0100132 ERROR("No env. room to store: '%s':'%s'\n", key, val);
133
134 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135}
136
San Mehat429721c2014-09-23 07:48:47 -0700137void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138{
139 int fd;
140 fd = open("/dev/null", O_RDWR);
141 dup2(fd, 0);
142 dup2(fd, 1);
143 dup2(fd, 2);
144 close(fd);
145}
146
147static void open_console()
148{
149 int fd;
150 if ((fd = open(console_name, O_RDWR)) < 0) {
151 fd = open("/dev/null", O_RDWR);
152 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700153 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154 dup2(fd, 0);
155 dup2(fd, 1);
156 dup2(fd, 2);
157 close(fd);
158}
159
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160static void publish_socket(const char *name, int fd)
161{
162 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
163 char val[64];
164
165 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
166 name,
167 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
168 snprintf(val, sizeof(val), "%d", fd);
169 add_environment(key, val);
170
171 /* make sure we don't close-on-exec */
172 fcntl(fd, F_SETFD, 0);
173}
174
San Mehatf24e2522009-05-19 13:30:46 -0700175void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700176{
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800177 // Starting a service removes it from the disabled or reset state and
178 // immediately takes it out of the restarting state if it was in there.
JP Abgrall3beec7e2014-05-02 21:14:29 -0700179 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700181
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800182 // Running processes require no additional work --- if they're in the
183 // process of exiting, we've ensured that they will immediately restart
184 // on exit, unless they are ONESHOT.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185 if (svc->flags & SVC_RUNNING) {
186 return;
187 }
188
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800189 bool needs_console = (svc->flags & SVC_CONSOLE);
190 if (needs_console && !have_console) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191 ERROR("service '%s' requires console\n", svc->name);
192 svc->flags |= SVC_DISABLED;
193 return;
194 }
195
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800196 struct stat s;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197 if (stat(svc->args[0], &s) != 0) {
198 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
199 svc->flags |= SVC_DISABLED;
200 return;
201 }
202
San Mehatf24e2522009-05-19 13:30:46 -0700203 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700204 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
205 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700206 svc->flags |= SVC_DISABLED;
207 return;
208 }
209
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800210 char* scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500211 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500212 if (svc->seclabel) {
213 scon = strdup(svc->seclabel);
214 if (!scon) {
215 ERROR("Out of memory while starting '%s'\n", svc->name);
216 return;
217 }
218 } else {
219 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500220
Stephen Smalley30f30332012-11-16 14:34:27 -0500221 INFO("computing context for service '%s'\n", svc->args[0]);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800222 int rc = getcon(&mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500223 if (rc < 0) {
224 ERROR("could not get context while starting '%s'\n", svc->name);
225 return;
226 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500227
Stephen Smalley30f30332012-11-16 14:34:27 -0500228 rc = getfilecon(svc->args[0], &fcon);
229 if (rc < 0) {
230 ERROR("could not get context while starting '%s'\n", svc->name);
231 freecon(mycon);
232 return;
233 }
234
235 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500236 if (rc == 0 && !strcmp(scon, mycon)) {
237 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
238 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500239 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500240 freecon(fcon);
241 if (rc < 0) {
242 ERROR("could not get context while starting '%s'\n", svc->name);
243 return;
244 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500245 }
246 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500247
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248 NOTICE("starting '%s'\n", svc->name);
249
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800250 pid_t pid = fork();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251 if (pid == 0) {
252 struct socketinfo *si;
253 struct svcenvinfo *ei;
254 char tmp[32];
255 int fd, sz;
256
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700257 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700258 if (properties_inited()) {
259 get_property_workspace(&fd, &sz);
Yabin Cuie2d63af2015-02-17 19:27:51 -0800260 snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
Colin Cross3294bbb2010-04-19 17:11:33 -0700261 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
262 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263
264 for (ei = svc->envvars; ei; ei = ei->next)
265 add_environment(ei->name, ei->value);
266
267 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400268 int socket_type = (
269 !strcmp(si->type, "stream") ? SOCK_STREAM :
270 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
271 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400272 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273 if (s >= 0) {
274 publish_socket(si->name, s);
275 }
276 }
277
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500278 freecon(scon);
279 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500280
San Mehat4e221f02010-02-25 14:19:50 -0800281 if (svc->ioprio_class != IoSchedClass_NONE) {
282 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
283 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
284 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
285 }
286 }
287
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 if (needs_console) {
289 setsid();
290 open_console();
291 } else {
292 zap_stdio();
293 }
294
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800295 if (false) {
296 for (size_t n = 0; svc->args[n]; n++) {
297 INFO("args[%zu] = '%s'\n", n, svc->args[n]);
298 }
299 for (size_t n = 0; ENV[n]; n++) {
300 INFO("env[%zu] = '%s'\n", n, ENV[n]);
301 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303
304 setpgid(0, getpid());
305
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800306 // As requested, set our gid, supplemental gids, and uid.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800308 if (setgid(svc->gid) != 0) {
309 ERROR("setgid failed: %s\n", strerror(errno));
310 _exit(127);
311 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
313 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800314 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
315 ERROR("setgroups failed: %s\n", strerror(errno));
316 _exit(127);
317 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 }
319 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800320 if (setuid(svc->uid) != 0) {
321 ERROR("setuid failed: %s\n", strerror(errno));
322 _exit(127);
323 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500325 if (svc->seclabel) {
326 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
327 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
328 _exit(127);
329 }
330 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500331
San Mehat8ad15682009-05-20 08:50:40 -0700332 if (!dynamic_args) {
333 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
334 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
335 }
336 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700337 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700338 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700339 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700340 char *next = tmp;
341 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700342
343 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700344 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700345
San Mehatd4cdd132009-05-20 09:52:16 -0700346 while((bword = strsep(&next, " "))) {
347 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700348 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700349 break;
San Mehatf24e2522009-05-19 13:30:46 -0700350 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800351 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700352 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100353 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 _exit(127);
355 }
356
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500357 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500358
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 if (pid < 0) {
360 ERROR("failed to start '%s'\n", svc->name);
361 svc->pid = 0;
362 return;
363 }
364
365 svc->time_started = gettime();
366 svc->pid = pid;
367 svc->flags |= SVC_RUNNING;
368
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800369 if ((svc->flags & SVC_EXEC) != 0) {
Elliott Hughes58c3bac2015-03-11 12:31:53 -0700370 INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800371 svc->pid, svc->uid, svc->gid, svc->nr_supp_gids, svc->seclabel);
372 waiting_for_exec = true;
373 }
374
375 svc->NotifyStateChange("running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376}
377
Mike Kasickb54f39f2012-01-25 23:48:46 -0500378/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800379static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500381 /* The service is still SVC_RUNNING until its process exits, but if it has
382 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700383 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384
Mike Kasickb54f39f2012-01-25 23:48:46 -0500385 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800386 /* Hrm, an illegal flag. Default to SVC_DISABLED */
387 how = SVC_DISABLED;
388 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389 /* if the service has not yet started, prevent
390 * it from auto-starting with its class
391 */
Ken Sumralla2864802011-10-26 16:56:00 -0700392 if (how == SVC_RESET) {
393 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
394 } else {
395 svc->flags |= how;
396 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397
398 if (svc->pid) {
399 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800400 kill(-svc->pid, SIGKILL);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800401 svc->NotifyStateChange("stopping");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 } else {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800403 svc->NotifyStateChange("stopped");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700404 }
405}
406
Ken Sumrall752923c2010-12-03 16:33:31 -0800407void service_reset(struct service *svc)
408{
409 service_stop_or_reset(svc, SVC_RESET);
410}
411
412void service_stop(struct service *svc)
413{
414 service_stop_or_reset(svc, SVC_DISABLED);
415}
416
Mike Kasickb54f39f2012-01-25 23:48:46 -0500417void service_restart(struct service *svc)
418{
419 if (svc->flags & SVC_RUNNING) {
420 /* Stop, wait, then start the service. */
421 service_stop_or_reset(svc, SVC_RESTART);
422 } else if (!(svc->flags & SVC_RESTARTING)) {
423 /* Just start the service since it's not running. */
424 service_start(svc, NULL);
425 } /* else: Service is restarting anyways. */
426}
427
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428void property_changed(const char *name, const char *value)
429{
Colin Crossebc6ff12010-04-13 19:52:01 -0700430 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700432}
433
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700434static void restart_service_if_needed(struct service *svc)
435{
436 time_t next_start_time = svc->time_started + 5;
437
438 if (next_start_time <= gettime()) {
439 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700440 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700441 return;
442 }
443
444 if ((next_start_time < process_needs_restart) ||
445 (process_needs_restart == 0)) {
446 process_needs_restart = next_start_time;
447 }
448}
449
450static void restart_processes()
451{
452 process_needs_restart = 0;
453 service_for_each_flags(SVC_RESTARTING,
454 restart_service_if_needed);
455}
456
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457static void msg_start(const char *name)
458{
Hong-Mei Li11467182013-04-01 11:17:51 +0800459 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700460 char *tmp = NULL;
461 char *args = NULL;
462
463 if (!strchr(name, ':'))
464 svc = service_find_by_name(name);
465 else {
466 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800467 if (tmp) {
468 args = strchr(tmp, ':');
469 *args = '\0';
470 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700471
Hong-Mei Li11467182013-04-01 11:17:51 +0800472 svc = service_find_by_name(tmp);
473 }
San Mehatf24e2522009-05-19 13:30:46 -0700474 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700475
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700477 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700478 } else {
479 ERROR("no such service '%s'\n", name);
480 }
San Mehatf24e2522009-05-19 13:30:46 -0700481 if (tmp)
482 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483}
484
485static void msg_stop(const char *name)
486{
487 struct service *svc = service_find_by_name(name);
488
489 if (svc) {
490 service_stop(svc);
491 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700492 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493 }
494}
495
Mike Kasickb54f39f2012-01-25 23:48:46 -0500496static void msg_restart(const char *name)
497{
498 struct service *svc = service_find_by_name(name);
499
500 if (svc) {
501 service_restart(svc);
502 } else {
503 ERROR("no such service '%s'\n", name);
504 }
505}
506
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700507void handle_control_message(const char *msg, const char *arg)
508{
509 if (!strcmp(msg,"start")) {
510 msg_start(arg);
511 } else if (!strcmp(msg,"stop")) {
512 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700513 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500514 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700515 } else {
516 ERROR("unknown control msg '%s'\n", msg);
517 }
518}
519
Colin Crossebc6ff12010-04-13 19:52:01 -0700520static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700521{
522 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700523 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700524 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700525 return NULL;
526
527 return node_to_item(node, struct command, clist);
528}
529
530static struct command *get_next_command(struct action *act, struct command *cmd)
531{
532 struct listnode *node;
533 node = cmd->clist.next;
534 if (!node)
535 return NULL;
536 if (node == &act->commands)
537 return NULL;
538
539 return node_to_item(node, struct command, clist);
540}
541
542static int is_last_command(struct action *act, struct command *cmd)
543{
544 return (list_tail(&act->commands) == &cmd->clist);
545}
546
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700547
548void build_triggers_string(char *name_str, int length, struct action *cur_action) {
549 struct listnode *node;
550 struct trigger *cur_trigger;
551
552 list_for_each(node, &cur_action->triggers) {
553 cur_trigger = node_to_item(node, struct trigger, nlist);
554 if (node != cur_action->triggers.next) {
555 strlcat(name_str, " " , length);
556 }
557 strlcat(name_str, cur_trigger->name , length);
558 }
559}
560
Colin Crossebc6ff12010-04-13 19:52:01 -0700561void execute_one_command(void)
562{
Riley Andrews24a3b782014-06-26 13:56:01 -0700563 int ret, i;
564 char cmd_str[256] = "";
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700565 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700566
Colin Crossebc6ff12010-04-13 19:52:01 -0700567 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
568 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700569 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700570 if (!cur_action)
571 return;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700572
573 build_triggers_string(name_str, sizeof(name_str), cur_action);
574
575 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700576 cur_command = get_first_command(cur_action);
577 } else {
578 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700580
581 if (!cur_command)
582 return;
583
584 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700585 if (klog_get_level() >= KLOG_INFO_LEVEL) {
586 for (i = 0; i < cur_command->nargs; i++) {
587 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
588 if (i < cur_command->nargs - 1) {
589 strlcat(cmd_str, " ", sizeof(cmd_str));
590 }
591 }
592 INFO("command '%s' action=%s status=%d (%s:%d)\n",
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700593 cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
Riley Andrews24a3b782014-06-26 13:56:01 -0700594 cur_command->line);
595 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700596}
597
Colin Crossf83d0b92010-04-21 12:04:20 -0700598static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700599{
Colin Crossf83d0b92010-04-21 12:04:20 -0700600 int ret;
Andreas Gampea016c422014-11-24 19:52:41 -0800601 INFO("wait for %s\n", COLDBOOT_DONE);
602 ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
Colin Crossf83d0b92010-04-21 12:04:20 -0700603 if (ret)
Andreas Gampea016c422014-11-24 19:52:41 -0800604 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Colin Crossf83d0b92010-04-21 12:04:20 -0700605 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700606}
607
Alex Klyubin0d872d82013-08-16 13:19:24 -0700608/*
609 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
610 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
611 * Does nothing if Hardware RNG is not present.
612 *
613 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
614 * mixed into the primary pool of Linux RNG and the entropy estimate is left
615 * unmodified.
616 *
617 * If the HW RNG device /dev/hw_random is present, we require that at least
618 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
619 * devices/configurations where these I/O operations are blocking for a long
620 * time. We do not reboot or halt on failures, as this is a best-effort
621 * attempt.
622 */
623static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
624{
625 int result = -1;
626 int hwrandom_fd = -1;
627 int urandom_fd = -1;
628 char buf[512];
629 ssize_t chunk_size;
630 size_t total_bytes_written = 0;
631
632 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800633 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700634 if (hwrandom_fd == -1) {
635 if (errno == ENOENT) {
636 ERROR("/dev/hw_random not found\n");
637 /* It's not an error to not have a Hardware RNG. */
638 result = 0;
639 } else {
640 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
641 }
642 goto ret;
643 }
644
645 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800646 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700647 if (urandom_fd == -1) {
648 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
649 goto ret;
650 }
651
652 while (total_bytes_written < sizeof(buf)) {
653 chunk_size = TEMP_FAILURE_RETRY(
654 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
655 if (chunk_size == -1) {
656 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
657 goto ret;
658 } else if (chunk_size == 0) {
659 ERROR("Failed to read from /dev/hw_random: EOF\n");
660 goto ret;
661 }
662
663 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
664 if (chunk_size == -1) {
665 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
666 goto ret;
667 }
668 total_bytes_written += chunk_size;
669 }
670
Elliott Hughesccecf142014-01-16 10:53:11 -0800671 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700672 total_bytes_written);
673 result = 0;
674
675ret:
676 if (hwrandom_fd != -1) {
677 close(hwrandom_fd);
678 }
679 if (urandom_fd != -1) {
680 close(urandom_fd);
681 }
682 memset(buf, 0, sizeof(buf));
683 return result;
684}
685
Colin Crossebc6ff12010-04-13 19:52:01 -0700686static int keychord_init_action(int nargs, char **args)
687{
688 keychord_init();
689 return 0;
690}
691
692static int console_init_action(int nargs, char **args)
693{
694 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700695
696 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800697 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700698 }
699
Nick Kralevich45a884f2015-02-02 14:37:22 -0800700 fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700701 if (fd >= 0)
702 have_console = 1;
703 close(fd);
704
Nick Kralevich45a884f2015-02-02 14:37:22 -0800705 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200706 if (fd >= 0) {
707 const char *msg;
708 msg = "\n"
709 "\n"
710 "\n"
711 "\n"
712 "\n"
713 "\n"
714 "\n" // console is 40 cols x 30 lines
715 "\n"
716 "\n"
717 "\n"
718 "\n"
719 "\n"
720 "\n"
721 "\n"
722 " A N D R O I D ";
723 write(fd, msg, strlen(msg));
724 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700725 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200726
Colin Crossebc6ff12010-04-13 19:52:01 -0700727 return 0;
728}
729
Dima Zavin5511c842011-12-19 11:21:32 -0800730static void import_kernel_nv(char *name, int for_emulator)
731{
732 char *value = strchr(name, '=');
733 int name_len = strlen(name);
734
735 if (value == 0) return;
736 *value++ = 0;
737 if (name_len == 0) return;
738
739 if (for_emulator) {
740 /* in the emulator, export any kernel option with the
741 * ro.kernel. prefix */
742 char buff[PROP_NAME_MAX];
743 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
744
745 if (len < (int)sizeof(buff))
746 property_set( buff, value );
747 return;
748 }
749
750 if (!strcmp(name,"qemu")) {
751 strlcpy(qemu, value, sizeof(qemu));
752 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
753 const char *boot_prop_name = name + 12;
754 char prop[PROP_NAME_MAX];
755 int cnt;
756
757 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
758 if (cnt < PROP_NAME_MAX)
759 property_set(prop, value);
760 }
761}
762
763static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700764{
765 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800766 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800767 unsigned i;
768 struct {
769 const char *src_prop;
770 const char *dest_prop;
771 const char *def_val;
772 } prop_map[] = {
773 { "ro.boot.serialno", "ro.serialno", "", },
774 { "ro.boot.mode", "ro.bootmode", "unknown", },
775 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800776 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
777 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700778
Dima Zavin5511c842011-12-19 11:21:32 -0800779 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800780 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700781 if (ret > 0)
782 property_set(prop_map[i].dest_prop, tmp);
783 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800784 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800785 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700786
Colin Cross1a6f4c32013-01-28 17:13:35 -0800787 ret = property_get("ro.boot.console", tmp);
788 if (ret)
789 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800790
791 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800792 property_get("ro.bootmode", tmp);
793 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800794
795 /* if this was given on kernel command line, override what we read
796 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800797 ret = property_get("ro.boot.hardware", tmp);
798 if (ret)
799 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800800 property_set("ro.hardware", hardware);
801
802 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
803 property_set("ro.revision", tmp);
804
805 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700806 if (!strcmp(bootmode,"factory"))
807 property_set("ro.factorytest", "1");
808 else if (!strcmp(bootmode,"factory2"))
809 property_set("ro.factorytest", "2");
810 else
811 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800812}
Colin Crossebc6ff12010-04-13 19:52:01 -0700813
Dima Zavin5511c842011-12-19 11:21:32 -0800814static void process_kernel_cmdline(void)
815{
816 /* don't expose the raw commandline to nonpriv processes */
817 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700818
Dima Zavin5511c842011-12-19 11:21:32 -0800819 /* first pass does the common stuff, and finds if we are in qemu.
820 * second pass is only necessary for qemu to export all kernel params
821 * as props.
822 */
823 import_kernel_cmdline(0, import_kernel_nv);
824 if (qemu[0])
825 import_kernel_cmdline(1, import_kernel_nv);
826
827 /* now propogate the info given on command line to internal variables
828 * used by init as well as the current required properties
829 */
830 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700831}
832
833static int property_service_init_action(int nargs, char **args)
834{
835 /* read any property files on system or data and
836 * fire up the property service. This must happen
837 * after the ro.foo properties are set above so
838 * that /data/local.prop cannot interfere with them.
839 */
840 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700841 if (get_property_set_fd() < 0) {
842 ERROR("start_property_service() failed\n");
843 exit(1);
844 }
845
Colin Crossebc6ff12010-04-13 19:52:01 -0700846 return 0;
847}
848
849static int signal_init_action(int nargs, char **args)
850{
851 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700852 if (get_signal_fd() < 0) {
853 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700854 exit(1);
855 }
856 return 0;
857}
858
859static int queue_property_triggers_action(int nargs, char **args)
860{
861 queue_all_property_triggers();
862 /* enable property triggers */
863 property_triggers_enabled = 1;
864 return 0;
865}
866
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400867void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500868{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400869 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500870 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400871 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400872}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500873
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700874static bool selinux_is_disabled(void)
875{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800876 if (ALLOW_DISABLE_SELINUX) {
877 if (access("/sys/fs/selinux", F_OK) != 0) {
878 // SELinux is not compiled into the kernel, or has been disabled
879 // via the kernel command line "selinux=0".
880 return true;
881 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700882
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800883 char tmp[PROP_VALUE_MAX];
884 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
885 // SELinux is compiled into the kernel, but we've been told to disable it.
886 return true;
887 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700888 }
889
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700890 return false;
891}
892
893static bool selinux_is_enforcing(void)
894{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800895 if (ALLOW_DISABLE_SELINUX) {
896 char tmp[PROP_VALUE_MAX];
897 if (property_get("ro.boot.selinux", tmp) == 0) {
898 // Property is not set. Assume enforcing.
899 return true;
900 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700901
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800902 if (strcmp(tmp, "permissive") == 0) {
903 // SELinux is in the kernel, but we've been told to go into permissive mode.
904 return false;
905 }
906
907 if (strcmp(tmp, "enforcing") != 0) {
908 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
909 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700910 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700911 return true;
912}
913
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400914int selinux_reload_policy(void)
915{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700916 if (selinux_is_disabled()) {
917 return -1;
918 }
919
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400920 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500921
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400922 if (selinux_android_reload_policy() == -1) {
923 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500924 }
925
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400926 if (sehandle)
927 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500928
rpcraig63207cd2012-08-09 10:05:49 -0400929 if (sehandle_prop)
930 selabel_close(sehandle_prop);
931
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400932 selinux_init_all_handles();
933 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500934}
rpcraig63207cd2012-08-09 10:05:49 -0400935
Elliott Hughesf682b472015-02-06 12:19:48 -0800936static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400937{
938 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
939 return 0;
940}
941
Stephen Smalley439224e2014-06-24 13:45:43 -0400942int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500943{
944 int level;
945 va_list ap;
946 switch (type) {
947 case SELINUX_WARNING:
948 level = KLOG_WARNING_LEVEL;
949 break;
950 case SELINUX_INFO:
951 level = KLOG_INFO_LEVEL;
952 break;
953 default:
954 level = KLOG_ERROR_LEVEL;
955 break;
956 }
957 va_start(ap, fmt);
958 klog_vwrite(level, fmt, ap);
959 va_end(ap);
960 return 0;
961}
962
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700963static void selinux_initialize(void)
964{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700965 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700966 return;
967 }
968
969 INFO("loading selinux policy\n");
970 if (selinux_android_load_policy() < 0) {
971 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
972 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
973 while (1) { pause(); } // never reached
974 }
975
976 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700977 bool is_enforcing = selinux_is_enforcing();
978 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
979 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700980}
981
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800982int main(int argc, char** argv) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700983 if (!strcmp(basename(argv[0]), "ueventd"))
984 return ueventd_main(argc, argv);
985
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700986 if (!strcmp(basename(argv[0]), "watchdogd"))
987 return watchdogd_main(argc, argv);
988
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800989 // Clear the umask.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700990 umask(0);
991
Elliott Hughescc86fb22015-03-17 20:01:13 -0700992 add_environment("PATH", _PATH_DEFPATH);
993
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800994 // Get the basic filesystem setup we need put together in the initramdisk
995 // on / and then we'll let the rc file figure out the rest.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700996 mkdir("/dev", 0755);
997 mkdir("/proc", 0755);
998 mkdir("/sys", 0755);
999
Nick Kralevich150f19e2010-06-22 16:35:43 -07001000 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001001 mkdir("/dev/pts", 0755);
1002 mkdir("/dev/socket", 0755);
1003 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1004 mount("proc", "/proc", "proc", 0, NULL);
1005 mount("sysfs", "/sys", "sysfs", 0, NULL);
1006
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001007 // Indicate that booting is in progress to background fw loaders, etc.
Nick Kralevich45a884f2015-02-02 14:37:22 -08001008 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001009
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001010 // We must have some place other than / to create the device nodes for
1011 // kmsg and null, otherwise we won't be able to remount / read-only
1012 // later on. Now that tmpfs is mounted on /dev, we can actually talk
1013 // to the outside world.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001014 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001015 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001016 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001017
Colin Crossf83d0b92010-04-21 12:04:20 -07001018 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001019
Dima Zavin5511c842011-12-19 11:21:32 -08001020 process_kernel_cmdline();
1021
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001022 selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001023 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001024 selinux_set_callback(SELINUX_CB_LOG, cb);
rpcraig63207cd2012-08-09 10:05:49 -04001025 cb.func_audit = audit_callback;
1026 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1027
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001028 selinux_initialize();
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001029
1030 // These directories were necessarily created before initial policy load
1031 // and therefore need their security context restored to the proper value.
1032 // This must happen before /dev is populated by ueventd.
Stephen Smalleye096e362012-06-11 13:37:39 -04001033 restorecon("/dev");
1034 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001035 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001036 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001037
Dima Zavind7634c92011-12-16 14:18:06 -08001038 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001039 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001040
Dima Zavind7634c92011-12-16 14:18:06 -08001041 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001042
1043 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001044
Colin Crossf83d0b92010-04-21 12:04:20 -07001045 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001046 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001047 queue_builtin_action(keychord_init_action, "keychord_init");
1048 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001049
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001050 // Execute all the boot actions to get us started.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001051 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001052
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001053 // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1054 // wasn't ready immediately after wait_for_coldboot_done
Alex Klyubin0d872d82013-08-16 13:19:24 -07001055 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001056 queue_builtin_action(property_service_init_action, "property_service_init");
1057 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001058
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001059 // Don't mount filesystems or start core system services in charger mode.
1060 if (strcmp(bootmode, "charger") == 0) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001061 action_for_each_trigger("charger", action_add_queue_tail);
1062 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001063 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001064 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001065
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001066 // Run all property triggers based on current state of the properties.
Chris Dearman469b7b22012-03-01 15:29:20 -08001067 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001068
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001069 // TODO: why do we only initialize ufds after execute_one_command and restart_processes?
1070 size_t fd_count = 0;
1071 struct pollfd ufds[3];
1072 bool property_set_fd_init = false;
1073 bool signal_fd_init = false;
1074 bool keychord_fd_init = false;
1075
Yongqin Liua197ff12014-12-05 13:45:02 +08001076 for (;;) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001077 if (!waiting_for_exec) {
1078 execute_one_command();
1079 restart_processes();
1080 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001081
Colin Crossebc6ff12010-04-13 19:52:01 -07001082 if (!property_set_fd_init && get_property_set_fd() > 0) {
1083 ufds[fd_count].fd = get_property_set_fd();
1084 ufds[fd_count].events = POLLIN;
1085 ufds[fd_count].revents = 0;
1086 fd_count++;
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001087 property_set_fd_init = true;
Colin Crossebc6ff12010-04-13 19:52:01 -07001088 }
1089 if (!signal_fd_init && get_signal_fd() > 0) {
1090 ufds[fd_count].fd = get_signal_fd();
1091 ufds[fd_count].events = POLLIN;
1092 ufds[fd_count].revents = 0;
1093 fd_count++;
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001094 signal_fd_init = true;
Colin Crossebc6ff12010-04-13 19:52:01 -07001095 }
1096 if (!keychord_fd_init && get_keychord_fd() > 0) {
1097 ufds[fd_count].fd = get_keychord_fd();
1098 ufds[fd_count].events = POLLIN;
1099 ufds[fd_count].revents = 0;
1100 fd_count++;
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001101 keychord_fd_init = true;
Colin Crossebc6ff12010-04-13 19:52:01 -07001102 }
1103
Yongqin Liua197ff12014-12-05 13:45:02 +08001104 int timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001105 if (process_needs_restart) {
1106 timeout = (process_needs_restart - gettime()) * 1000;
1107 if (timeout < 0)
1108 timeout = 0;
1109 }
1110
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001111 if (!action_queue_empty() || cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001112 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001113 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001114
Yongqin Liua197ff12014-12-05 13:45:02 +08001115 bootchart_sample(&timeout);
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001116
Yongqin Liua197ff12014-12-05 13:45:02 +08001117 int nr = poll(ufds, fd_count, timeout);
1118 if (nr <= 0) {
1119 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001120 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001121
Yongqin Liua197ff12014-12-05 13:45:02 +08001122 for (size_t i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001123 if (ufds[i].revents & POLLIN) {
Yongqin Liua197ff12014-12-05 13:45:02 +08001124 if (ufds[i].fd == get_property_set_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001125 handle_property_set_fd();
Yongqin Liua197ff12014-12-05 13:45:02 +08001126 } else if (ufds[i].fd == get_keychord_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001127 handle_keychord();
Yongqin Liua197ff12014-12-05 13:45:02 +08001128 } else if (ufds[i].fd == get_signal_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001129 handle_signal();
Yongqin Liua197ff12014-12-05 13:45:02 +08001130 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001131 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001132 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001133 }
1134
1135 return 0;
1136}