blob: ddd6223dac36c18e9643a93efb023eaf6cc14a92 [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>
Rom Lemarchandcbcbea22015-02-28 06:39:11 -080034#include <dirent.h>
35
36#include <memory>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038#include <selinux/selinux.h>
39#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040040#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041
Colin Crossf83d0b92010-04-21 12:04:20 -070042#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043
Dima Zavinda04c522011-09-01 17:09:44 -070044#include <cutils/list.h>
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070045#include <cutils/android_reboot.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080047#include <cutils/iosched_policy.h>
Alex Klyubin0d872d82013-08-16 13:19:24 -070048#include <cutils/fs.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070049#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#include <termios.h>
Rom Lemarchandcbcbea22015-02-28 06:39:11 -080051#include <utils/file.h>
52#include <utils/stringprintf.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#include "devices.h"
55#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070056#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080058#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070059#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070060#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070061#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070062#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070063#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070064#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065
Stephen Smalleye46f9d52012-01-13 08:48:47 -050066struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040067struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050068
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069static int property_triggers_enabled = 0;
70
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073static 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);
Yabin Cuie2d63af2015-02-17 19:27:51 -0800258 snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
Colin Cross3294bbb2010-04-19 17:11:33 -0700259 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", },
Rom Lemarchand38b340a2015-02-27 17:20:29 -0800770 { "ro.boot.hardware", "ro.hardware", "unknown", },
771 { "ro.boot.revision", "ro.revision", "0", },
Dima Zavin5511c842011-12-19 11:21:32 -0800772 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700773
Dima Zavin5511c842011-12-19 11:21:32 -0800774 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800775 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700776 if (ret > 0)
777 property_set(prop_map[i].dest_prop, tmp);
778 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800779 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800780 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700781
Colin Cross1a6f4c32013-01-28 17:13:35 -0800782 ret = property_get("ro.boot.console", tmp);
783 if (ret)
784 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800785
786 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800787 property_get("ro.bootmode", tmp);
788 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800789
Dima Zavin5511c842011-12-19 11:21:32 -0800790 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700791 if (!strcmp(bootmode,"factory"))
792 property_set("ro.factorytest", "1");
793 else if (!strcmp(bootmode,"factory2"))
794 property_set("ro.factorytest", "2");
795 else
796 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800797}
Colin Crossebc6ff12010-04-13 19:52:01 -0700798
Rom Lemarchandcbcbea22015-02-28 06:39:11 -0800799static void process_kernel_dt(void)
800{
801 static const char android_dir[] = "/proc/device-tree/firmware/android";
802
803 std::string file_name = android::StringPrintf("%s/compatible", android_dir);
804
805 std::string dt_file;
806 android::ReadFileToString(file_name, &dt_file);
807 if (!dt_file.compare("android,firmware")) {
808 ERROR("firmware/android is not compatible with 'android,firmware'\n");
809 return;
810 }
811
812 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
813 if (!dir)
814 return;
815
816 struct dirent *dp;
817 while ((dp = readdir(dir.get())) != NULL) {
818 if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible"))
819 continue;
820
821 file_name = android::StringPrintf("%s/%s", android_dir, dp->d_name);
822
823 android::ReadFileToString(file_name, &dt_file);
824 std::replace(dt_file.begin(), dt_file.end(), ',', '.');
825
826 std::string property_name = android::StringPrintf("ro.boot.%s", dp->d_name);
827 if (property_set(property_name.c_str(), dt_file.c_str())) {
828 ERROR("Could not set property %s to value %s", property_name.c_str(), dt_file.c_str());
829 }
830 }
831}
832
Dima Zavin5511c842011-12-19 11:21:32 -0800833static void process_kernel_cmdline(void)
834{
835 /* don't expose the raw commandline to nonpriv processes */
836 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700837
Dima Zavin5511c842011-12-19 11:21:32 -0800838 /* first pass does the common stuff, and finds if we are in qemu.
839 * second pass is only necessary for qemu to export all kernel params
840 * as props.
841 */
842 import_kernel_cmdline(0, import_kernel_nv);
843 if (qemu[0])
844 import_kernel_cmdline(1, import_kernel_nv);
Colin Crossebc6ff12010-04-13 19:52:01 -0700845}
846
847static int property_service_init_action(int nargs, char **args)
848{
849 /* read any property files on system or data and
850 * fire up the property service. This must happen
851 * after the ro.foo properties are set above so
852 * that /data/local.prop cannot interfere with them.
853 */
854 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700855 if (get_property_set_fd() < 0) {
856 ERROR("start_property_service() failed\n");
857 exit(1);
858 }
859
Colin Crossebc6ff12010-04-13 19:52:01 -0700860 return 0;
861}
862
863static int signal_init_action(int nargs, char **args)
864{
865 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700866 if (get_signal_fd() < 0) {
867 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700868 exit(1);
869 }
870 return 0;
871}
872
873static int queue_property_triggers_action(int nargs, char **args)
874{
875 queue_all_property_triggers();
876 /* enable property triggers */
877 property_triggers_enabled = 1;
878 return 0;
879}
880
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400881void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500882{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400883 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500884 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400885 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400886}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500887
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700888static bool selinux_is_disabled(void)
889{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800890 if (ALLOW_DISABLE_SELINUX) {
891 if (access("/sys/fs/selinux", F_OK) != 0) {
892 // SELinux is not compiled into the kernel, or has been disabled
893 // via the kernel command line "selinux=0".
894 return true;
895 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700896
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800897 char tmp[PROP_VALUE_MAX];
898 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
899 // SELinux is compiled into the kernel, but we've been told to disable it.
900 return true;
901 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700902 }
903
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700904 return false;
905}
906
907static bool selinux_is_enforcing(void)
908{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800909 if (ALLOW_DISABLE_SELINUX) {
910 char tmp[PROP_VALUE_MAX];
911 if (property_get("ro.boot.selinux", tmp) == 0) {
912 // Property is not set. Assume enforcing.
913 return true;
914 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700915
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800916 if (strcmp(tmp, "permissive") == 0) {
917 // SELinux is in the kernel, but we've been told to go into permissive mode.
918 return false;
919 }
920
921 if (strcmp(tmp, "enforcing") != 0) {
922 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
923 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700924 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700925 return true;
926}
927
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400928int selinux_reload_policy(void)
929{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700930 if (selinux_is_disabled()) {
931 return -1;
932 }
933
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400934 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500935
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400936 if (selinux_android_reload_policy() == -1) {
937 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500938 }
939
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400940 if (sehandle)
941 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500942
rpcraig63207cd2012-08-09 10:05:49 -0400943 if (sehandle_prop)
944 selabel_close(sehandle_prop);
945
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400946 selinux_init_all_handles();
947 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500948}
rpcraig63207cd2012-08-09 10:05:49 -0400949
Elliott Hughesf682b472015-02-06 12:19:48 -0800950static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400951{
952 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
953 return 0;
954}
955
Stephen Smalley439224e2014-06-24 13:45:43 -0400956int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500957{
958 int level;
959 va_list ap;
960 switch (type) {
961 case SELINUX_WARNING:
962 level = KLOG_WARNING_LEVEL;
963 break;
964 case SELINUX_INFO:
965 level = KLOG_INFO_LEVEL;
966 break;
967 default:
968 level = KLOG_ERROR_LEVEL;
969 break;
970 }
971 va_start(ap, fmt);
972 klog_vwrite(level, fmt, ap);
973 va_end(ap);
974 return 0;
975}
976
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700977static void selinux_initialize(void)
978{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700979 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700980 return;
981 }
982
983 INFO("loading selinux policy\n");
984 if (selinux_android_load_policy() < 0) {
985 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
986 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
987 while (1) { pause(); } // never reached
988 }
989
990 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700991 bool is_enforcing = selinux_is_enforcing();
992 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
993 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700994}
995
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700996int main(int argc, char **argv)
997{
Yongqin Liua197ff12014-12-05 13:45:02 +0800998 size_t fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700999 struct pollfd ufds[4];
Colin Crossebc6ff12010-04-13 19:52:01 -07001000 int property_set_fd_init = 0;
1001 int signal_fd_init = 0;
1002 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -08001003 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001004
Colin Crossf83d0b92010-04-21 12:04:20 -07001005 if (!strcmp(basename(argv[0]), "ueventd"))
1006 return ueventd_main(argc, argv);
1007
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001008 if (!strcmp(basename(argv[0]), "watchdogd"))
1009 return watchdogd_main(argc, argv);
1010
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001011 /* clear the umask */
1012 umask(0);
1013
1014 /* Get the basic filesystem setup we need put
1015 * together in the initramdisk on / and then we'll
1016 * let the rc file figure out the rest.
1017 */
1018 mkdir("/dev", 0755);
1019 mkdir("/proc", 0755);
1020 mkdir("/sys", 0755);
1021
Nick Kralevich150f19e2010-06-22 16:35:43 -07001022 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001023 mkdir("/dev/pts", 0755);
1024 mkdir("/dev/socket", 0755);
1025 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1026 mount("proc", "/proc", "proc", 0, NULL);
1027 mount("sysfs", "/sys", "sysfs", 0, NULL);
1028
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001029 /* indicate that booting is in progress to background fw loaders, etc */
Nick Kralevich45a884f2015-02-02 14:37:22 -08001030 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001031
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001032 /* We must have some place other than / to create the
1033 * device nodes for kmsg and null, otherwise we won't
1034 * be able to remount / read-only later on.
1035 * Now that tmpfs is mounted on /dev, we can actually
1036 * talk to the outside world.
1037 */
1038 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001039 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001040 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001041
Rom Lemarchandcbcbea22015-02-28 06:39:11 -08001042 process_kernel_dt();
1043 /* in case one is passing arguments both on the command line and in DT
1044 * Properties set in DT always have priority over the command-line ones
1045 */
Dima Zavin5511c842011-12-19 11:21:32 -08001046 process_kernel_cmdline();
1047
Rom Lemarchandcbcbea22015-02-28 06:39:11 -08001048 /* now propogate the kernel variables to internal variables
1049 * used by init as well as the current required properties
1050 */
1051 export_kernel_boot_props();
1052
rpcraig63207cd2012-08-09 10:05:49 -04001053 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001054 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001055 selinux_set_callback(SELINUX_CB_LOG, cb);
1056
1057 cb.func_audit = audit_callback;
1058 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1059
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001060 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001061 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001062 * and therefore need their security context restored to the proper value.
1063 * This must happen before /dev is populated by ueventd.
1064 */
1065 restorecon("/dev");
1066 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001067 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001068 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001069
Dima Zavind7634c92011-12-16 14:18:06 -08001070 is_charger = !strcmp(bootmode, "charger");
1071
1072 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001073 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001074
Dima Zavind7634c92011-12-16 14:18:06 -08001075 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001076
1077 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001078
Colin Crossf83d0b92010-04-21 12:04:20 -07001079 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001080 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001081 queue_builtin_action(keychord_init_action, "keychord_init");
1082 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001083
Dima Zavinca47cef2011-08-24 15:28:23 -07001084 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001085 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001086
Alex Klyubin0d872d82013-08-16 13:19:24 -07001087 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1088 * wasn't ready immediately after wait_for_coldboot_done
1089 */
1090 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001091 queue_builtin_action(property_service_init_action, "property_service_init");
1092 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001093
Riley Andrewse4b7b292014-06-16 15:06:21 -07001094 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001095 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001096 action_for_each_trigger("charger", action_add_queue_tail);
1097 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001098 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001099 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001100
Riley Andrews9464e5a2014-07-11 15:05:23 -07001101 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001102 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001103
Yongqin Liua197ff12014-12-05 13:45:02 +08001104 for (;;) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001105 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001106 restart_processes();
1107
Colin Crossebc6ff12010-04-13 19:52:01 -07001108 if (!property_set_fd_init && get_property_set_fd() > 0) {
1109 ufds[fd_count].fd = get_property_set_fd();
1110 ufds[fd_count].events = POLLIN;
1111 ufds[fd_count].revents = 0;
1112 fd_count++;
1113 property_set_fd_init = 1;
1114 }
1115 if (!signal_fd_init && get_signal_fd() > 0) {
1116 ufds[fd_count].fd = get_signal_fd();
1117 ufds[fd_count].events = POLLIN;
1118 ufds[fd_count].revents = 0;
1119 fd_count++;
1120 signal_fd_init = 1;
1121 }
1122 if (!keychord_fd_init && get_keychord_fd() > 0) {
1123 ufds[fd_count].fd = get_keychord_fd();
1124 ufds[fd_count].events = POLLIN;
1125 ufds[fd_count].revents = 0;
1126 fd_count++;
1127 keychord_fd_init = 1;
1128 }
1129
Yongqin Liua197ff12014-12-05 13:45:02 +08001130 int timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001131 if (process_needs_restart) {
1132 timeout = (process_needs_restart - gettime()) * 1000;
1133 if (timeout < 0)
1134 timeout = 0;
1135 }
1136
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001137 if (!action_queue_empty() || cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001138 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001139 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001140
Yongqin Liua197ff12014-12-05 13:45:02 +08001141 bootchart_sample(&timeout);
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001142
Yongqin Liua197ff12014-12-05 13:45:02 +08001143 int nr = poll(ufds, fd_count, timeout);
1144 if (nr <= 0) {
1145 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001146 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001147
Yongqin Liua197ff12014-12-05 13:45:02 +08001148 for (size_t i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001149 if (ufds[i].revents & POLLIN) {
Yongqin Liua197ff12014-12-05 13:45:02 +08001150 if (ufds[i].fd == get_property_set_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001151 handle_property_set_fd();
Yongqin Liua197ff12014-12-05 13:45:02 +08001152 } else if (ufds[i].fd == get_keychord_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001153 handle_keychord();
Yongqin Liua197ff12014-12-05 13:45:02 +08001154 } else if (ufds[i].fd == get_signal_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001155 handle_signal();
Yongqin Liua197ff12014-12-05 13:45:02 +08001156 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001157 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001158 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001159 }
1160
1161 return 0;
1162}