blob: 3e3be2e0c784aff2108e5067e5d8b2b33448f148 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <signal.h>
24#include <sys/wait.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/poll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <errno.h>
29#include <stdarg.h>
30#include <mtd/mtd-user.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/un.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035#include <selinux/selinux.h>
36#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040037#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
Colin Crossf83d0b92010-04-21 12:04:20 -070039#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Dima Zavinda04c522011-09-01 17:09:44 -070041#include <cutils/list.h>
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070042#include <cutils/android_reboot.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080044#include <cutils/iosched_policy.h>
Alex Klyubin0d872d82013-08-16 13:19:24 -070045#include <cutils/fs.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070046#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049#include "devices.h"
50#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070051#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080053#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070054#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070055#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070056#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070057#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070058#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070059#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040062struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064static int property_triggers_enabled = 0;
65
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066static int bootchart_count;
Bo (Andover) Zhang37003732014-07-24 13:11:35 -040067static long long bootchart_time = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068
69static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071static char hardware[32];
72static unsigned revision = 0;
73static char qemu[32];
74
Colin Crossebc6ff12010-04-13 19:52:01 -070075static struct action *cur_action = NULL;
76static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070077
Colin Cross9c5366b2010-04-13 19:48:59 -070078void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079{
80 char pname[PROP_NAME_MAX];
81 int len = strlen(name);
82 if ((len + 10) > PROP_NAME_MAX)
83 return;
84 snprintf(pname, sizeof(pname), "init.svc.%s", name);
85 property_set(pname, state);
86}
87
88static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080089static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070090static time_t process_needs_restart;
91
92static const char *ENV[32];
93
94/* add_environment - add "key=value" to the current environment */
95int add_environment(const char *key, const char *val)
96{
James Morrissey381341f2014-05-16 11:36:36 +010097 size_t n;
98 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070099
James Morrissey381341f2014-05-16 11:36:36 +0100100 /* The last environment entry is reserved to terminate the list */
101 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
102
103 /* Delete any existing entry for this key */
104 if (ENV[n] != NULL) {
105 size_t entry_key_len = strcspn(ENV[n], "=");
106 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
107 free((char*)ENV[n]);
108 ENV[n] = NULL;
109 }
110 }
111
112 /* Add entry if a free slot is available */
113 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800114 char* entry;
115 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 ENV[n] = entry;
117 return 0;
118 }
119 }
120
James Morrissey381341f2014-05-16 11:36:36 +0100121 ERROR("No env. room to store: '%s':'%s'\n", key, val);
122
123 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124}
125
San Mehat429721c2014-09-23 07:48:47 -0700126void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127{
128 int fd;
129 fd = open("/dev/null", O_RDWR);
130 dup2(fd, 0);
131 dup2(fd, 1);
132 dup2(fd, 2);
133 close(fd);
134}
135
136static void open_console()
137{
138 int fd;
139 if ((fd = open(console_name, O_RDWR)) < 0) {
140 fd = open("/dev/null", O_RDWR);
141 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700142 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143 dup2(fd, 0);
144 dup2(fd, 1);
145 dup2(fd, 2);
146 close(fd);
147}
148
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700149static void publish_socket(const char *name, int fd)
150{
151 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
152 char val[64];
153
154 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
155 name,
156 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
157 snprintf(val, sizeof(val), "%d", fd);
158 add_environment(key, val);
159
160 /* make sure we don't close-on-exec */
161 fcntl(fd, F_SETFD, 0);
162}
163
San Mehatf24e2522009-05-19 13:30:46 -0700164void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700165{
166 struct stat s;
167 pid_t pid;
168 int needs_console;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500169 char *scon = NULL;
170 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700171
Ken Sumrall752923c2010-12-03 16:33:31 -0800172 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173 * state and immediately takes it out of the restarting
174 * state if it was in there
175 */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700176 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700178
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179 /* running processes require no additional work -- if
180 * they're in the process of exiting, we've ensured
181 * that they will immediately restart on exit, unless
182 * they are ONESHOT
183 */
184 if (svc->flags & SVC_RUNNING) {
185 return;
186 }
187
188 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
189 if (needs_console && (!have_console)) {
190 ERROR("service '%s' requires console\n", svc->name);
191 svc->flags |= SVC_DISABLED;
192 return;
193 }
194
195 if (stat(svc->args[0], &s) != 0) {
196 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
197 svc->flags |= SVC_DISABLED;
198 return;
199 }
200
San Mehatf24e2522009-05-19 13:30:46 -0700201 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700202 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
203 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700204 svc->flags |= SVC_DISABLED;
205 return;
206 }
207
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500208 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500209 if (svc->seclabel) {
210 scon = strdup(svc->seclabel);
211 if (!scon) {
212 ERROR("Out of memory while starting '%s'\n", svc->name);
213 return;
214 }
215 } else {
216 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500217
Stephen Smalley30f30332012-11-16 14:34:27 -0500218 INFO("computing context for service '%s'\n", svc->args[0]);
219 rc = getcon(&mycon);
220 if (rc < 0) {
221 ERROR("could not get context while starting '%s'\n", svc->name);
222 return;
223 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500224
Stephen Smalley30f30332012-11-16 14:34:27 -0500225 rc = getfilecon(svc->args[0], &fcon);
226 if (rc < 0) {
227 ERROR("could not get context while starting '%s'\n", svc->name);
228 freecon(mycon);
229 return;
230 }
231
232 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500233 if (rc == 0 && !strcmp(scon, mycon)) {
234 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
235 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500236 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500237 freecon(fcon);
238 if (rc < 0) {
239 ERROR("could not get context while starting '%s'\n", svc->name);
240 return;
241 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500242 }
243 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500244
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700245 NOTICE("starting '%s'\n", svc->name);
246
247 pid = fork();
248
249 if (pid == 0) {
250 struct socketinfo *si;
251 struct svcenvinfo *ei;
252 char tmp[32];
253 int fd, sz;
254
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700255 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700256 if (properties_inited()) {
257 get_property_workspace(&fd, &sz);
258 sprintf(tmp, "%d,%d", dup(fd), sz);
259 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
260 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261
262 for (ei = svc->envvars; ei; ei = ei->next)
263 add_environment(ei->name, ei->value);
264
265 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400266 int socket_type = (
267 !strcmp(si->type, "stream") ? SOCK_STREAM :
268 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
269 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400270 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271 if (s >= 0) {
272 publish_socket(si->name, s);
273 }
274 }
275
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500276 freecon(scon);
277 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500278
San Mehat4e221f02010-02-25 14:19:50 -0800279 if (svc->ioprio_class != IoSchedClass_NONE) {
280 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
281 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
282 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
283 }
284 }
285
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286 if (needs_console) {
287 setsid();
288 open_console();
289 } else {
290 zap_stdio();
291 }
292
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800293 if (false) {
294 for (size_t n = 0; svc->args[n]; n++) {
295 INFO("args[%zu] = '%s'\n", n, svc->args[n]);
296 }
297 for (size_t n = 0; ENV[n]; n++) {
298 INFO("env[%zu] = '%s'\n", n, ENV[n]);
299 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301
302 setpgid(0, getpid());
303
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800304 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800306 if (setgid(svc->gid) != 0) {
307 ERROR("setgid failed: %s\n", strerror(errno));
308 _exit(127);
309 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 }
311 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800312 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
313 ERROR("setgroups failed: %s\n", strerror(errno));
314 _exit(127);
315 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 }
317 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800318 if (setuid(svc->uid) != 0) {
319 ERROR("setuid failed: %s\n", strerror(errno));
320 _exit(127);
321 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500323 if (svc->seclabel) {
324 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
325 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
326 _exit(127);
327 }
328 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500329
San Mehat8ad15682009-05-20 08:50:40 -0700330 if (!dynamic_args) {
331 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
332 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
333 }
334 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700335 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700336 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700337 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700338 char *next = tmp;
339 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700340
341 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700342 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700343
San Mehatd4cdd132009-05-20 09:52:16 -0700344 while((bword = strsep(&next, " "))) {
345 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700346 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700347 break;
San Mehatf24e2522009-05-19 13:30:46 -0700348 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800349 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700350 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100351 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352 _exit(127);
353 }
354
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500355 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500356
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 if (pid < 0) {
358 ERROR("failed to start '%s'\n", svc->name);
359 svc->pid = 0;
360 return;
361 }
362
363 svc->time_started = gettime();
364 svc->pid = pid;
365 svc->flags |= SVC_RUNNING;
366
Colin Cross3294bbb2010-04-19 17:11:33 -0700367 if (properties_inited())
368 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369}
370
Mike Kasickb54f39f2012-01-25 23:48:46 -0500371/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800372static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500374 /* The service is still SVC_RUNNING until its process exits, but if it has
375 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700376 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377
Mike Kasickb54f39f2012-01-25 23:48:46 -0500378 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800379 /* Hrm, an illegal flag. Default to SVC_DISABLED */
380 how = SVC_DISABLED;
381 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382 /* if the service has not yet started, prevent
383 * it from auto-starting with its class
384 */
Ken Sumralla2864802011-10-26 16:56:00 -0700385 if (how == SVC_RESET) {
386 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
387 } else {
388 svc->flags |= how;
389 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390
391 if (svc->pid) {
392 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800393 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700394 notify_service_state(svc->name, "stopping");
395 } else {
396 notify_service_state(svc->name, "stopped");
397 }
398}
399
Ken Sumrall752923c2010-12-03 16:33:31 -0800400void service_reset(struct service *svc)
401{
402 service_stop_or_reset(svc, SVC_RESET);
403}
404
405void service_stop(struct service *svc)
406{
407 service_stop_or_reset(svc, SVC_DISABLED);
408}
409
Mike Kasickb54f39f2012-01-25 23:48:46 -0500410void service_restart(struct service *svc)
411{
412 if (svc->flags & SVC_RUNNING) {
413 /* Stop, wait, then start the service. */
414 service_stop_or_reset(svc, SVC_RESTART);
415 } else if (!(svc->flags & SVC_RESTARTING)) {
416 /* Just start the service since it's not running. */
417 service_start(svc, NULL);
418 } /* else: Service is restarting anyways. */
419}
420
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421void property_changed(const char *name, const char *value)
422{
Colin Crossebc6ff12010-04-13 19:52:01 -0700423 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700424 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425}
426
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427static void restart_service_if_needed(struct service *svc)
428{
429 time_t next_start_time = svc->time_started + 5;
430
431 if (next_start_time <= gettime()) {
432 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700433 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700434 return;
435 }
436
437 if ((next_start_time < process_needs_restart) ||
438 (process_needs_restart == 0)) {
439 process_needs_restart = next_start_time;
440 }
441}
442
443static void restart_processes()
444{
445 process_needs_restart = 0;
446 service_for_each_flags(SVC_RESTARTING,
447 restart_service_if_needed);
448}
449
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700450static void msg_start(const char *name)
451{
Hong-Mei Li11467182013-04-01 11:17:51 +0800452 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700453 char *tmp = NULL;
454 char *args = NULL;
455
456 if (!strchr(name, ':'))
457 svc = service_find_by_name(name);
458 else {
459 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800460 if (tmp) {
461 args = strchr(tmp, ':');
462 *args = '\0';
463 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700464
Hong-Mei Li11467182013-04-01 11:17:51 +0800465 svc = service_find_by_name(tmp);
466 }
San Mehatf24e2522009-05-19 13:30:46 -0700467 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700468
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700469 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700470 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471 } else {
472 ERROR("no such service '%s'\n", name);
473 }
San Mehatf24e2522009-05-19 13:30:46 -0700474 if (tmp)
475 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476}
477
478static void msg_stop(const char *name)
479{
480 struct service *svc = service_find_by_name(name);
481
482 if (svc) {
483 service_stop(svc);
484 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700485 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700486 }
487}
488
Mike Kasickb54f39f2012-01-25 23:48:46 -0500489static void msg_restart(const char *name)
490{
491 struct service *svc = service_find_by_name(name);
492
493 if (svc) {
494 service_restart(svc);
495 } else {
496 ERROR("no such service '%s'\n", name);
497 }
498}
499
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700500void handle_control_message(const char *msg, const char *arg)
501{
502 if (!strcmp(msg,"start")) {
503 msg_start(arg);
504 } else if (!strcmp(msg,"stop")) {
505 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700506 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500507 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700508 } else {
509 ERROR("unknown control msg '%s'\n", msg);
510 }
511}
512
Colin Crossebc6ff12010-04-13 19:52:01 -0700513static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700514{
515 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700516 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700517 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700518 return NULL;
519
520 return node_to_item(node, struct command, clist);
521}
522
523static struct command *get_next_command(struct action *act, struct command *cmd)
524{
525 struct listnode *node;
526 node = cmd->clist.next;
527 if (!node)
528 return NULL;
529 if (node == &act->commands)
530 return NULL;
531
532 return node_to_item(node, struct command, clist);
533}
534
535static int is_last_command(struct action *act, struct command *cmd)
536{
537 return (list_tail(&act->commands) == &cmd->clist);
538}
539
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700540
541void build_triggers_string(char *name_str, int length, struct action *cur_action) {
542 struct listnode *node;
543 struct trigger *cur_trigger;
544
545 list_for_each(node, &cur_action->triggers) {
546 cur_trigger = node_to_item(node, struct trigger, nlist);
547 if (node != cur_action->triggers.next) {
548 strlcat(name_str, " " , length);
549 }
550 strlcat(name_str, cur_trigger->name , length);
551 }
552}
553
Colin Crossebc6ff12010-04-13 19:52:01 -0700554void execute_one_command(void)
555{
Riley Andrews24a3b782014-06-26 13:56:01 -0700556 int ret, i;
557 char cmd_str[256] = "";
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700558 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559
Colin Crossebc6ff12010-04-13 19:52:01 -0700560 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
561 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700562 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700563 if (!cur_action)
564 return;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700565
566 build_triggers_string(name_str, sizeof(name_str), cur_action);
567
568 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700569 cur_command = get_first_command(cur_action);
570 } else {
571 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700573
574 if (!cur_command)
575 return;
576
577 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700578 if (klog_get_level() >= KLOG_INFO_LEVEL) {
579 for (i = 0; i < cur_command->nargs; i++) {
580 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
581 if (i < cur_command->nargs - 1) {
582 strlcat(cmd_str, " ", sizeof(cmd_str));
583 }
584 }
585 INFO("command '%s' action=%s status=%d (%s:%d)\n",
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700586 cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
Riley Andrews24a3b782014-06-26 13:56:01 -0700587 cur_command->line);
588 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589}
590
Colin Crossf83d0b92010-04-21 12:04:20 -0700591static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592{
Colin Crossf83d0b92010-04-21 12:04:20 -0700593 int ret;
Andreas Gampea016c422014-11-24 19:52:41 -0800594 INFO("wait for %s\n", COLDBOOT_DONE);
595 ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
Colin Crossf83d0b92010-04-21 12:04:20 -0700596 if (ret)
Andreas Gampea016c422014-11-24 19:52:41 -0800597 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Colin Crossf83d0b92010-04-21 12:04:20 -0700598 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700599}
600
Alex Klyubin0d872d82013-08-16 13:19:24 -0700601/*
602 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
603 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
604 * Does nothing if Hardware RNG is not present.
605 *
606 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
607 * mixed into the primary pool of Linux RNG and the entropy estimate is left
608 * unmodified.
609 *
610 * If the HW RNG device /dev/hw_random is present, we require that at least
611 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
612 * devices/configurations where these I/O operations are blocking for a long
613 * time. We do not reboot or halt on failures, as this is a best-effort
614 * attempt.
615 */
616static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
617{
618 int result = -1;
619 int hwrandom_fd = -1;
620 int urandom_fd = -1;
621 char buf[512];
622 ssize_t chunk_size;
623 size_t total_bytes_written = 0;
624
625 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800626 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700627 if (hwrandom_fd == -1) {
628 if (errno == ENOENT) {
629 ERROR("/dev/hw_random not found\n");
630 /* It's not an error to not have a Hardware RNG. */
631 result = 0;
632 } else {
633 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
634 }
635 goto ret;
636 }
637
638 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800639 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700640 if (urandom_fd == -1) {
641 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
642 goto ret;
643 }
644
645 while (total_bytes_written < sizeof(buf)) {
646 chunk_size = TEMP_FAILURE_RETRY(
647 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
648 if (chunk_size == -1) {
649 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
650 goto ret;
651 } else if (chunk_size == 0) {
652 ERROR("Failed to read from /dev/hw_random: EOF\n");
653 goto ret;
654 }
655
656 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
657 if (chunk_size == -1) {
658 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
659 goto ret;
660 }
661 total_bytes_written += chunk_size;
662 }
663
Elliott Hughesccecf142014-01-16 10:53:11 -0800664 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700665 total_bytes_written);
666 result = 0;
667
668ret:
669 if (hwrandom_fd != -1) {
670 close(hwrandom_fd);
671 }
672 if (urandom_fd != -1) {
673 close(urandom_fd);
674 }
675 memset(buf, 0, sizeof(buf));
676 return result;
677}
678
Colin Crossebc6ff12010-04-13 19:52:01 -0700679static int keychord_init_action(int nargs, char **args)
680{
681 keychord_init();
682 return 0;
683}
684
685static int console_init_action(int nargs, char **args)
686{
687 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700688
689 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800690 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700691 }
692
Nick Kralevich45a884f2015-02-02 14:37:22 -0800693 fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700694 if (fd >= 0)
695 have_console = 1;
696 close(fd);
697
Nick Kralevich45a884f2015-02-02 14:37:22 -0800698 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200699 if (fd >= 0) {
700 const char *msg;
701 msg = "\n"
702 "\n"
703 "\n"
704 "\n"
705 "\n"
706 "\n"
707 "\n" // console is 40 cols x 30 lines
708 "\n"
709 "\n"
710 "\n"
711 "\n"
712 "\n"
713 "\n"
714 "\n"
715 " A N D R O I D ";
716 write(fd, msg, strlen(msg));
717 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700718 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200719
Colin Crossebc6ff12010-04-13 19:52:01 -0700720 return 0;
721}
722
Dima Zavin5511c842011-12-19 11:21:32 -0800723static void import_kernel_nv(char *name, int for_emulator)
724{
725 char *value = strchr(name, '=');
726 int name_len = strlen(name);
727
728 if (value == 0) return;
729 *value++ = 0;
730 if (name_len == 0) return;
731
732 if (for_emulator) {
733 /* in the emulator, export any kernel option with the
734 * ro.kernel. prefix */
735 char buff[PROP_NAME_MAX];
736 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
737
738 if (len < (int)sizeof(buff))
739 property_set( buff, value );
740 return;
741 }
742
743 if (!strcmp(name,"qemu")) {
744 strlcpy(qemu, value, sizeof(qemu));
745 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
746 const char *boot_prop_name = name + 12;
747 char prop[PROP_NAME_MAX];
748 int cnt;
749
750 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
751 if (cnt < PROP_NAME_MAX)
752 property_set(prop, value);
753 }
754}
755
756static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700757{
758 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800759 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800760 unsigned i;
761 struct {
762 const char *src_prop;
763 const char *dest_prop;
764 const char *def_val;
765 } prop_map[] = {
766 { "ro.boot.serialno", "ro.serialno", "", },
767 { "ro.boot.mode", "ro.bootmode", "unknown", },
768 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800769 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
770 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700771
Dima Zavin5511c842011-12-19 11:21:32 -0800772 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800773 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700774 if (ret > 0)
775 property_set(prop_map[i].dest_prop, tmp);
776 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800777 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800778 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700779
Colin Cross1a6f4c32013-01-28 17:13:35 -0800780 ret = property_get("ro.boot.console", tmp);
781 if (ret)
782 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800783
784 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800785 property_get("ro.bootmode", tmp);
786 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800787
788 /* if this was given on kernel command line, override what we read
789 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800790 ret = property_get("ro.boot.hardware", tmp);
791 if (ret)
792 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800793 property_set("ro.hardware", hardware);
794
795 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
796 property_set("ro.revision", tmp);
797
798 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700799 if (!strcmp(bootmode,"factory"))
800 property_set("ro.factorytest", "1");
801 else if (!strcmp(bootmode,"factory2"))
802 property_set("ro.factorytest", "2");
803 else
804 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800805}
Colin Crossebc6ff12010-04-13 19:52:01 -0700806
Dima Zavin5511c842011-12-19 11:21:32 -0800807static void process_kernel_cmdline(void)
808{
809 /* don't expose the raw commandline to nonpriv processes */
810 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700811
Dima Zavin5511c842011-12-19 11:21:32 -0800812 /* first pass does the common stuff, and finds if we are in qemu.
813 * second pass is only necessary for qemu to export all kernel params
814 * as props.
815 */
816 import_kernel_cmdline(0, import_kernel_nv);
817 if (qemu[0])
818 import_kernel_cmdline(1, import_kernel_nv);
819
820 /* now propogate the info given on command line to internal variables
821 * used by init as well as the current required properties
822 */
823 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700824}
825
826static int property_service_init_action(int nargs, char **args)
827{
828 /* read any property files on system or data and
829 * fire up the property service. This must happen
830 * after the ro.foo properties are set above so
831 * that /data/local.prop cannot interfere with them.
832 */
833 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700834 if (get_property_set_fd() < 0) {
835 ERROR("start_property_service() failed\n");
836 exit(1);
837 }
838
Colin Crossebc6ff12010-04-13 19:52:01 -0700839 return 0;
840}
841
842static int signal_init_action(int nargs, char **args)
843{
844 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700845 if (get_signal_fd() < 0) {
846 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700847 exit(1);
848 }
849 return 0;
850}
851
852static int queue_property_triggers_action(int nargs, char **args)
853{
854 queue_all_property_triggers();
855 /* enable property triggers */
856 property_triggers_enabled = 1;
857 return 0;
858}
859
Colin Crossebc6ff12010-04-13 19:52:01 -0700860static int bootchart_init_action(int nargs, char **args)
861{
862 bootchart_count = bootchart_init();
863 if (bootchart_count < 0) {
864 ERROR("bootcharting init failure\n");
865 } else if (bootchart_count > 0) {
866 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
867 } else {
868 NOTICE("bootcharting ignored\n");
869 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100870
871 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700872}
Colin Crossebc6ff12010-04-13 19:52:01 -0700873
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400874void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500875{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400876 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500877 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400878 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400879}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500880
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700881static bool selinux_is_disabled(void)
882{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800883 if (ALLOW_DISABLE_SELINUX) {
884 if (access("/sys/fs/selinux", F_OK) != 0) {
885 // SELinux is not compiled into the kernel, or has been disabled
886 // via the kernel command line "selinux=0".
887 return true;
888 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700889
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800890 char tmp[PROP_VALUE_MAX];
891 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
892 // SELinux is compiled into the kernel, but we've been told to disable it.
893 return true;
894 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700895 }
896
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700897 return false;
898}
899
900static bool selinux_is_enforcing(void)
901{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800902 if (ALLOW_DISABLE_SELINUX) {
903 char tmp[PROP_VALUE_MAX];
904 if (property_get("ro.boot.selinux", tmp) == 0) {
905 // Property is not set. Assume enforcing.
906 return true;
907 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700908
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800909 if (strcmp(tmp, "permissive") == 0) {
910 // SELinux is in the kernel, but we've been told to go into permissive mode.
911 return false;
912 }
913
914 if (strcmp(tmp, "enforcing") != 0) {
915 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
916 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700917 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700918 return true;
919}
920
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400921int selinux_reload_policy(void)
922{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700923 if (selinux_is_disabled()) {
924 return -1;
925 }
926
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400927 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500928
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400929 if (selinux_android_reload_policy() == -1) {
930 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500931 }
932
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400933 if (sehandle)
934 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500935
rpcraig63207cd2012-08-09 10:05:49 -0400936 if (sehandle_prop)
937 selabel_close(sehandle_prop);
938
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400939 selinux_init_all_handles();
940 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500941}
rpcraig63207cd2012-08-09 10:05:49 -0400942
Elliott Hughesf682b472015-02-06 12:19:48 -0800943static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400944{
945 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
946 return 0;
947}
948
Stephen Smalley439224e2014-06-24 13:45:43 -0400949int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500950{
951 int level;
952 va_list ap;
953 switch (type) {
954 case SELINUX_WARNING:
955 level = KLOG_WARNING_LEVEL;
956 break;
957 case SELINUX_INFO:
958 level = KLOG_INFO_LEVEL;
959 break;
960 default:
961 level = KLOG_ERROR_LEVEL;
962 break;
963 }
964 va_start(ap, fmt);
965 klog_vwrite(level, fmt, ap);
966 va_end(ap);
967 return 0;
968}
969
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700970static void selinux_initialize(void)
971{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700972 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700973 return;
974 }
975
976 INFO("loading selinux policy\n");
977 if (selinux_android_load_policy() < 0) {
978 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
979 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
980 while (1) { pause(); } // never reached
981 }
982
983 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700984 bool is_enforcing = selinux_is_enforcing();
985 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
986 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700987}
988
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989int main(int argc, char **argv)
990{
Colin Crossebc6ff12010-04-13 19:52:01 -0700991 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700992 struct pollfd ufds[4];
Colin Crossebc6ff12010-04-13 19:52:01 -0700993 int property_set_fd_init = 0;
994 int signal_fd_init = 0;
995 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800996 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997
Colin Crossf83d0b92010-04-21 12:04:20 -0700998 if (!strcmp(basename(argv[0]), "ueventd"))
999 return ueventd_main(argc, argv);
1000
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001001 if (!strcmp(basename(argv[0]), "watchdogd"))
1002 return watchdogd_main(argc, argv);
1003
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001004 /* clear the umask */
1005 umask(0);
1006
1007 /* Get the basic filesystem setup we need put
1008 * together in the initramdisk on / and then we'll
1009 * let the rc file figure out the rest.
1010 */
1011 mkdir("/dev", 0755);
1012 mkdir("/proc", 0755);
1013 mkdir("/sys", 0755);
1014
Nick Kralevich150f19e2010-06-22 16:35:43 -07001015 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001016 mkdir("/dev/pts", 0755);
1017 mkdir("/dev/socket", 0755);
1018 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1019 mount("proc", "/proc", "proc", 0, NULL);
1020 mount("sysfs", "/sys", "sysfs", 0, NULL);
1021
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001022 /* indicate that booting is in progress to background fw loaders, etc */
Nick Kralevich45a884f2015-02-02 14:37:22 -08001023 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001024
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001025 /* We must have some place other than / to create the
1026 * device nodes for kmsg and null, otherwise we won't
1027 * be able to remount / read-only later on.
1028 * Now that tmpfs is mounted on /dev, we can actually
1029 * talk to the outside world.
1030 */
1031 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001032 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001033 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001034
Colin Crossf83d0b92010-04-21 12:04:20 -07001035 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001036
Dima Zavin5511c842011-12-19 11:21:32 -08001037 process_kernel_cmdline();
1038
rpcraig63207cd2012-08-09 10:05:49 -04001039 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001040 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001041 selinux_set_callback(SELINUX_CB_LOG, cb);
1042
1043 cb.func_audit = audit_callback;
1044 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1045
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001046 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001047 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001048 * and therefore need their security context restored to the proper value.
1049 * This must happen before /dev is populated by ueventd.
1050 */
1051 restorecon("/dev");
1052 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001053 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001054 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001055
Dima Zavind7634c92011-12-16 14:18:06 -08001056 is_charger = !strcmp(bootmode, "charger");
1057
1058 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001059 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001060
Dima Zavind7634c92011-12-16 14:18:06 -08001061 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001062
1063 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001064
Colin Crossf83d0b92010-04-21 12:04:20 -07001065 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001066 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001067 queue_builtin_action(keychord_init_action, "keychord_init");
1068 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001069
Dima Zavinca47cef2011-08-24 15:28:23 -07001070 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001071 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001072
Alex Klyubin0d872d82013-08-16 13:19:24 -07001073 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1074 * wasn't ready immediately after wait_for_coldboot_done
1075 */
1076 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001077 queue_builtin_action(property_service_init_action, "property_service_init");
1078 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001079
Riley Andrewse4b7b292014-06-16 15:06:21 -07001080 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001081 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001082 action_for_each_trigger("charger", action_add_queue_tail);
1083 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001084 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001085 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001086
Riley Andrews9464e5a2014-07-11 15:05:23 -07001087 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001088 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001089
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001090 if (BOOTCHART) {
1091 queue_builtin_action(bootchart_init_action, "bootchart_init");
1092 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001093
1094 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001095 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001096
Colin Crossebc6ff12010-04-13 19:52:01 -07001097 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001098 restart_processes();
1099
Colin Crossebc6ff12010-04-13 19:52:01 -07001100 if (!property_set_fd_init && get_property_set_fd() > 0) {
1101 ufds[fd_count].fd = get_property_set_fd();
1102 ufds[fd_count].events = POLLIN;
1103 ufds[fd_count].revents = 0;
1104 fd_count++;
1105 property_set_fd_init = 1;
1106 }
1107 if (!signal_fd_init && get_signal_fd() > 0) {
1108 ufds[fd_count].fd = get_signal_fd();
1109 ufds[fd_count].events = POLLIN;
1110 ufds[fd_count].revents = 0;
1111 fd_count++;
1112 signal_fd_init = 1;
1113 }
1114 if (!keychord_fd_init && get_keychord_fd() > 0) {
1115 ufds[fd_count].fd = get_keychord_fd();
1116 ufds[fd_count].events = POLLIN;
1117 ufds[fd_count].revents = 0;
1118 fd_count++;
1119 keychord_fd_init = 1;
1120 }
1121
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001122 if (process_needs_restart) {
1123 timeout = (process_needs_restart - gettime()) * 1000;
1124 if (timeout < 0)
1125 timeout = 0;
1126 }
1127
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001128 if (!action_queue_empty() || cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001129 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001130 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001131
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001132 if (BOOTCHART) {
Bo (Andover) Zhang37003732014-07-24 13:11:35 -04001133 if (bootchart_count > 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001134 long long current_time;
1135 int elapsed_time, remaining_time;
1136
1137 current_time = bootchart_gettime();
1138 elapsed_time = current_time - bootchart_time;
1139
1140 if (elapsed_time >= BOOTCHART_POLLING_MS) {
1141 /* count missed samples */
1142 while (elapsed_time >= BOOTCHART_POLLING_MS) {
1143 elapsed_time -= BOOTCHART_POLLING_MS;
1144 bootchart_count--;
1145 }
1146 /* count may be negative, take a sample anyway */
1147 bootchart_time = current_time;
1148 if (bootchart_step() < 0 || bootchart_count <= 0) {
1149 bootchart_finish();
1150 bootchart_count = 0;
1151 }
1152 }
1153 if (bootchart_count > 0) {
1154 remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
1155 if (timeout < 0 || timeout > remaining_time) {
1156 timeout = remaining_time;
1157 }
1158 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001159 }
1160 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001161
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001162 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001163 if (nr <= 0)
1164 continue;
1165
Colin Crossebc6ff12010-04-13 19:52:01 -07001166 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001167 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001168 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001169 handle_property_set_fd();
1170 else if (ufds[i].fd == get_keychord_fd())
1171 handle_keychord();
1172 else if (ufds[i].fd == get_signal_fd())
1173 handle_signal();
1174 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001175 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001176 }
1177
1178 return 0;
1179}