2008-10-21 16:00:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <mtd/mtd-user.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2010-04-21 21:04:20 +02:00
|
|
|
#include <libgen.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2011-09-02 02:09:44 +02:00
|
|
|
#include <cutils/list.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <cutils/sockets.h>
|
2010-02-25 23:19:50 +01:00
|
|
|
#include <cutils/iosched_policy.h>
|
2010-04-21 21:04:20 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <termios.h>
|
|
|
|
|
|
|
|
#include <sys/system_properties.h>
|
|
|
|
|
|
|
|
#include "devices.h"
|
|
|
|
#include "init.h"
|
2010-04-20 02:05:34 +02:00
|
|
|
#include "log.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
#include "property_service.h"
|
2008-12-18 03:08:08 +01:00
|
|
|
#include "bootchart.h"
|
2010-04-14 04:48:59 +02:00
|
|
|
#include "signal_handler.h"
|
2010-04-14 04:20:44 +02:00
|
|
|
#include "keychords.h"
|
2010-04-20 23:29:05 +02:00
|
|
|
#include "init_parser.h"
|
2010-04-14 05:35:46 +02:00
|
|
|
#include "util.h"
|
2010-04-21 21:04:20 +02:00
|
|
|
#include "ueventd.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
static int property_triggers_enabled = 0;
|
|
|
|
|
|
|
|
#if BOOTCHART
|
|
|
|
static int bootchart_count;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char console[32];
|
|
|
|
static char serialno[32];
|
|
|
|
static char bootmode[32];
|
|
|
|
static char baseband[32];
|
|
|
|
static char carrier[32];
|
|
|
|
static char bootloader[32];
|
|
|
|
static char hardware[32];
|
|
|
|
static unsigned revision = 0;
|
|
|
|
static char qemu[32];
|
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
static struct action *cur_action = NULL;
|
|
|
|
static struct command *cur_command = NULL;
|
|
|
|
static struct listnode *command_queue = NULL;
|
|
|
|
|
2010-04-14 04:48:59 +02:00
|
|
|
void notify_service_state(const char *name, const char *state)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
char pname[PROP_NAME_MAX];
|
|
|
|
int len = strlen(name);
|
|
|
|
if ((len + 10) > PROP_NAME_MAX)
|
|
|
|
return;
|
|
|
|
snprintf(pname, sizeof(pname), "init.svc.%s", name);
|
|
|
|
property_set(pname, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int have_console;
|
|
|
|
static char *console_name = "/dev/console";
|
|
|
|
static time_t process_needs_restart;
|
|
|
|
|
|
|
|
static const char *ENV[32];
|
|
|
|
|
|
|
|
/* add_environment - add "key=value" to the current environment */
|
|
|
|
int add_environment(const char *key, const char *val)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < 31; n++) {
|
|
|
|
if (!ENV[n]) {
|
|
|
|
size_t len = strlen(key) + strlen(val) + 2;
|
|
|
|
char *entry = malloc(len);
|
|
|
|
snprintf(entry, len, "%s=%s", key, val);
|
|
|
|
ENV[n] = entry;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zap_stdio(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void open_console()
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
if ((fd = open(console_name, O_RDWR)) < 0) {
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
}
|
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void publish_socket(const char *name, int fd)
|
|
|
|
{
|
|
|
|
char key[64] = ANDROID_SOCKET_ENV_PREFIX;
|
|
|
|
char val[64];
|
|
|
|
|
|
|
|
strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
|
|
|
|
name,
|
|
|
|
sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
|
|
|
|
snprintf(val, sizeof(val), "%d", fd);
|
|
|
|
add_environment(key, val);
|
|
|
|
|
|
|
|
/* make sure we don't close-on-exec */
|
|
|
|
fcntl(fd, F_SETFD, 0);
|
|
|
|
}
|
|
|
|
|
2009-05-19 22:30:46 +02:00
|
|
|
void service_start(struct service *svc, const char *dynamic_args)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
pid_t pid;
|
|
|
|
int needs_console;
|
|
|
|
int n;
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
/* starting a service removes it from the disabled or reset
|
2008-10-21 16:00:00 +02:00
|
|
|
* state and immediately takes it out of the restarting
|
|
|
|
* state if it was in there
|
|
|
|
*/
|
2010-12-04 01:33:31 +01:00
|
|
|
svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
|
2008-10-21 16:00:00 +02:00
|
|
|
svc->time_started = 0;
|
|
|
|
|
|
|
|
/* running processes require no additional work -- if
|
|
|
|
* they're in the process of exiting, we've ensured
|
|
|
|
* that they will immediately restart on exit, unless
|
|
|
|
* they are ONESHOT
|
|
|
|
*/
|
|
|
|
if (svc->flags & SVC_RUNNING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
|
|
|
|
if (needs_console && (!have_console)) {
|
|
|
|
ERROR("service '%s' requires console\n", svc->name);
|
|
|
|
svc->flags |= SVC_DISABLED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(svc->args[0], &s) != 0) {
|
|
|
|
ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
|
|
|
|
svc->flags |= SVC_DISABLED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-19 22:30:46 +02:00
|
|
|
if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
|
2009-05-20 18:52:16 +02:00
|
|
|
ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
|
|
|
|
svc->args[0]);
|
2009-05-19 22:30:46 +02:00
|
|
|
svc->flags |= SVC_DISABLED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
NOTICE("starting '%s'\n", svc->name);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
struct socketinfo *si;
|
|
|
|
struct svcenvinfo *ei;
|
|
|
|
char tmp[32];
|
|
|
|
int fd, sz;
|
|
|
|
|
2010-04-20 02:11:33 +02:00
|
|
|
if (properties_inited()) {
|
|
|
|
get_property_workspace(&fd, &sz);
|
|
|
|
sprintf(tmp, "%d,%d", dup(fd), sz);
|
|
|
|
add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
for (ei = svc->envvars; ei; ei = ei->next)
|
|
|
|
add_environment(ei->name, ei->value);
|
|
|
|
|
|
|
|
for (si = svc->sockets; si; si = si->next) {
|
2010-10-01 14:20:36 +02:00
|
|
|
int socket_type = (
|
|
|
|
!strcmp(si->type, "stream") ? SOCK_STREAM :
|
|
|
|
(!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
|
|
|
|
int s = create_socket(si->name, socket_type,
|
2008-10-21 16:00:00 +02:00
|
|
|
si->perm, si->uid, si->gid);
|
|
|
|
if (s >= 0) {
|
|
|
|
publish_socket(si->name, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 23:19:50 +01:00
|
|
|
if (svc->ioprio_class != IoSchedClass_NONE) {
|
|
|
|
if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
|
|
|
|
ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
|
|
|
|
getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
if (needs_console) {
|
|
|
|
setsid();
|
|
|
|
open_console();
|
|
|
|
} else {
|
|
|
|
zap_stdio();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
for (n = 0; svc->args[n]; n++) {
|
|
|
|
INFO("args[%d] = '%s'\n", n, svc->args[n]);
|
|
|
|
}
|
|
|
|
for (n = 0; ENV[n]; n++) {
|
|
|
|
INFO("env[%d] = '%s'\n", n, ENV[n]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
setpgid(0, getpid());
|
|
|
|
|
2009-01-10 02:51:25 +01:00
|
|
|
/* as requested, set our gid, supplemental gids, and uid */
|
2008-10-21 16:00:00 +02:00
|
|
|
if (svc->gid) {
|
2010-11-18 01:55:42 +01:00
|
|
|
if (setgid(svc->gid) != 0) {
|
|
|
|
ERROR("setgid failed: %s\n", strerror(errno));
|
|
|
|
_exit(127);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
if (svc->nr_supp_gids) {
|
2010-11-18 01:55:42 +01:00
|
|
|
if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
|
|
|
|
ERROR("setgroups failed: %s\n", strerror(errno));
|
|
|
|
_exit(127);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
if (svc->uid) {
|
2010-11-18 01:55:42 +01:00
|
|
|
if (setuid(svc->uid) != 0) {
|
|
|
|
ERROR("setuid failed: %s\n", strerror(errno));
|
|
|
|
_exit(127);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 17:50:40 +02:00
|
|
|
if (!dynamic_args) {
|
|
|
|
if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
|
|
|
|
ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
|
|
|
|
}
|
|
|
|
} else {
|
2010-04-20 23:29:05 +02:00
|
|
|
char *arg_ptrs[INIT_PARSER_MAXARGS+1];
|
2009-05-20 18:52:16 +02:00
|
|
|
int arg_idx = svc->nargs;
|
2009-05-19 22:30:46 +02:00
|
|
|
char *tmp = strdup(dynamic_args);
|
2009-05-20 18:52:16 +02:00
|
|
|
char *next = tmp;
|
|
|
|
char *bword;
|
2009-05-19 22:30:46 +02:00
|
|
|
|
|
|
|
/* Copy the static arguments */
|
2009-05-20 18:52:16 +02:00
|
|
|
memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
|
2009-05-19 22:30:46 +02:00
|
|
|
|
2009-05-20 18:52:16 +02:00
|
|
|
while((bword = strsep(&next, " "))) {
|
|
|
|
arg_ptrs[arg_idx++] = bword;
|
2010-04-20 23:29:05 +02:00
|
|
|
if (arg_idx == INIT_PARSER_MAXARGS)
|
2009-05-19 22:30:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
arg_ptrs[arg_idx] = '\0';
|
|
|
|
execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
|
2008-11-23 22:26:39 +01:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
_exit(127);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
ERROR("failed to start '%s'\n", svc->name);
|
|
|
|
svc->pid = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
svc->time_started = gettime();
|
|
|
|
svc->pid = pid;
|
|
|
|
svc->flags |= SVC_RUNNING;
|
|
|
|
|
2010-04-20 02:11:33 +02:00
|
|
|
if (properties_inited())
|
|
|
|
notify_service_state(svc->name, "running");
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
/* The how field should be either SVC_DISABLED or SVC_RESET */
|
|
|
|
static void service_stop_or_reset(struct service *svc, int how)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
/* we are no longer running, nor should we
|
|
|
|
* attempt to restart
|
|
|
|
*/
|
|
|
|
svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
|
|
|
|
/* Hrm, an illegal flag. Default to SVC_DISABLED */
|
|
|
|
how = SVC_DISABLED;
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
/* if the service has not yet started, prevent
|
|
|
|
* it from auto-starting with its class
|
|
|
|
*/
|
2010-12-04 01:33:31 +01:00
|
|
|
svc->flags |= how;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
if (svc->pid) {
|
|
|
|
NOTICE("service '%s' is being killed\n", svc->name);
|
2010-12-04 01:33:31 +01:00
|
|
|
kill(-svc->pid, SIGKILL);
|
2008-10-21 16:00:00 +02:00
|
|
|
notify_service_state(svc->name, "stopping");
|
|
|
|
} else {
|
|
|
|
notify_service_state(svc->name, "stopped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
void service_reset(struct service *svc)
|
|
|
|
{
|
|
|
|
service_stop_or_reset(svc, SVC_RESET);
|
|
|
|
}
|
|
|
|
|
|
|
|
void service_stop(struct service *svc)
|
|
|
|
{
|
|
|
|
service_stop_or_reset(svc, SVC_DISABLED);
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
void property_changed(const char *name, const char *value)
|
|
|
|
{
|
2010-04-14 04:52:01 +02:00
|
|
|
if (property_triggers_enabled)
|
2008-10-21 16:00:00 +02:00
|
|
|
queue_property_triggers(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restart_service_if_needed(struct service *svc)
|
|
|
|
{
|
|
|
|
time_t next_start_time = svc->time_started + 5;
|
|
|
|
|
|
|
|
if (next_start_time <= gettime()) {
|
|
|
|
svc->flags &= (~SVC_RESTARTING);
|
2009-05-19 22:30:46 +02:00
|
|
|
service_start(svc, NULL);
|
2008-10-21 16:00:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((next_start_time < process_needs_restart) ||
|
|
|
|
(process_needs_restart == 0)) {
|
|
|
|
process_needs_restart = next_start_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restart_processes()
|
|
|
|
{
|
|
|
|
process_needs_restart = 0;
|
|
|
|
service_for_each_flags(SVC_RESTARTING,
|
|
|
|
restart_service_if_needed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msg_start(const char *name)
|
|
|
|
{
|
2009-05-19 22:30:46 +02:00
|
|
|
struct service *svc;
|
|
|
|
char *tmp = NULL;
|
|
|
|
char *args = NULL;
|
|
|
|
|
|
|
|
if (!strchr(name, ':'))
|
|
|
|
svc = service_find_by_name(name);
|
|
|
|
else {
|
|
|
|
tmp = strdup(name);
|
|
|
|
args = strchr(tmp, ':');
|
|
|
|
*args = '\0';
|
|
|
|
args++;
|
|
|
|
|
|
|
|
svc = service_find_by_name(tmp);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
if (svc) {
|
2009-05-19 22:30:46 +02:00
|
|
|
service_start(svc, args);
|
2008-10-21 16:00:00 +02:00
|
|
|
} else {
|
|
|
|
ERROR("no such service '%s'\n", name);
|
|
|
|
}
|
2009-05-19 22:30:46 +02:00
|
|
|
if (tmp)
|
|
|
|
free(tmp);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void msg_stop(const char *name)
|
|
|
|
{
|
|
|
|
struct service *svc = service_find_by_name(name);
|
|
|
|
|
|
|
|
if (svc) {
|
|
|
|
service_stop(svc);
|
|
|
|
} else {
|
2009-05-06 03:33:07 +02:00
|
|
|
ERROR("no such service '%s'\n", name);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_control_message(const char *msg, const char *arg)
|
|
|
|
{
|
|
|
|
if (!strcmp(msg,"start")) {
|
|
|
|
msg_start(arg);
|
|
|
|
} else if (!strcmp(msg,"stop")) {
|
|
|
|
msg_stop(arg);
|
2010-10-03 22:30:11 +02:00
|
|
|
} else if (!strcmp(msg,"restart")) {
|
|
|
|
msg_stop(arg);
|
|
|
|
msg_start(arg);
|
2008-10-21 16:00:00 +02:00
|
|
|
} else {
|
|
|
|
ERROR("unknown control msg '%s'\n", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void import_kernel_nv(char *name, int in_qemu)
|
|
|
|
{
|
|
|
|
char *value = strchr(name, '=');
|
|
|
|
|
|
|
|
if (value == 0) return;
|
|
|
|
*value++ = 0;
|
|
|
|
if (*name == 0) return;
|
|
|
|
|
|
|
|
if (!in_qemu)
|
|
|
|
{
|
|
|
|
/* on a real device, white-list the kernel options */
|
|
|
|
if (!strcmp(name,"qemu")) {
|
|
|
|
strlcpy(qemu, value, sizeof(qemu));
|
|
|
|
} else if (!strcmp(name,"androidboot.console")) {
|
|
|
|
strlcpy(console, value, sizeof(console));
|
|
|
|
} else if (!strcmp(name,"androidboot.mode")) {
|
|
|
|
strlcpy(bootmode, value, sizeof(bootmode));
|
|
|
|
} else if (!strcmp(name,"androidboot.serialno")) {
|
|
|
|
strlcpy(serialno, value, sizeof(serialno));
|
|
|
|
} else if (!strcmp(name,"androidboot.baseband")) {
|
|
|
|
strlcpy(baseband, value, sizeof(baseband));
|
|
|
|
} else if (!strcmp(name,"androidboot.carrier")) {
|
|
|
|
strlcpy(carrier, value, sizeof(carrier));
|
|
|
|
} else if (!strcmp(name,"androidboot.bootloader")) {
|
|
|
|
strlcpy(bootloader, value, sizeof(bootloader));
|
|
|
|
} else if (!strcmp(name,"androidboot.hardware")) {
|
|
|
|
strlcpy(hardware, value, sizeof(hardware));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* in the emulator, export any kernel option with the
|
|
|
|
* ro.kernel. prefix */
|
|
|
|
char buff[32];
|
|
|
|
int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
|
|
|
|
if (len < (int)sizeof(buff)) {
|
|
|
|
property_set( buff, value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void import_kernel_cmdline(int in_qemu)
|
|
|
|
{
|
|
|
|
char cmdline[1024];
|
|
|
|
char *ptr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open("/proc/cmdline", O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
int n = read(fd, cmdline, 1023);
|
|
|
|
if (n < 0) n = 0;
|
|
|
|
|
|
|
|
/* get rid of trailing newline, it happens */
|
|
|
|
if (n > 0 && cmdline[n-1] == '\n') n--;
|
|
|
|
|
|
|
|
cmdline[n] = 0;
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
cmdline[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = cmdline;
|
|
|
|
while (ptr && *ptr) {
|
|
|
|
char *x = strchr(ptr, ' ');
|
|
|
|
if (x != 0) *x++ = 0;
|
|
|
|
import_kernel_nv(ptr, in_qemu);
|
|
|
|
ptr = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't expose the raw commandline to nonpriv processes */
|
|
|
|
chmod("/proc/cmdline", 0440);
|
|
|
|
}
|
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
static struct command *get_first_command(struct action *act)
|
|
|
|
{
|
|
|
|
struct listnode *node;
|
|
|
|
node = list_head(&act->commands);
|
2011-08-26 22:59:18 +02:00
|
|
|
if (!node || list_empty(&act->commands))
|
2010-04-14 04:52:01 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return node_to_item(node, struct command, clist);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command *get_next_command(struct action *act, struct command *cmd)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
struct listnode *node;
|
2010-04-14 04:52:01 +02:00
|
|
|
node = cmd->clist.next;
|
|
|
|
if (!node)
|
|
|
|
return NULL;
|
|
|
|
if (node == &act->commands)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return node_to_item(node, struct command, clist);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_last_command(struct action *act, struct command *cmd)
|
|
|
|
{
|
|
|
|
return (list_tail(&act->commands) == &cmd->clist);
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute_one_command(void)
|
|
|
|
{
|
2008-10-21 16:00:00 +02:00
|
|
|
int ret;
|
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
|
|
|
|
cur_action = action_remove_queue_head();
|
2010-04-22 20:52:23 +02:00
|
|
|
cur_command = NULL;
|
2010-04-14 04:52:01 +02:00
|
|
|
if (!cur_action)
|
|
|
|
return;
|
|
|
|
INFO("processing action %p (%s)\n", cur_action, cur_action->name);
|
|
|
|
cur_command = get_first_command(cur_action);
|
|
|
|
} else {
|
|
|
|
cur_command = get_next_command(cur_action, cur_command);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2010-04-14 04:52:01 +02:00
|
|
|
|
|
|
|
if (!cur_command)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ret = cur_command->func(cur_command->nargs, cur_command->args);
|
|
|
|
INFO("command '%s' r=%d\n", cur_command->args[0], ret);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2010-04-21 21:04:20 +02:00
|
|
|
static int wait_for_coldboot_done_action(int nargs, char **args)
|
2010-04-14 04:52:01 +02:00
|
|
|
{
|
2010-04-21 21:04:20 +02:00
|
|
|
int ret;
|
|
|
|
INFO("wait for %s\n", coldboot_done);
|
|
|
|
ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
|
|
|
|
if (ret)
|
|
|
|
ERROR("Timed out waiting for %s\n", coldboot_done);
|
|
|
|
return ret;
|
2010-04-14 04:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int property_init_action(int nargs, char **args)
|
|
|
|
{
|
2011-09-06 19:12:21 +02:00
|
|
|
bool load_defaults = true;
|
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
INFO("property init\n");
|
2011-09-06 19:12:21 +02:00
|
|
|
if (!strcmp(bootmode, "charger"))
|
|
|
|
load_defaults = false;
|
|
|
|
property_init(load_defaults);
|
2010-04-14 04:52:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int keychord_init_action(int nargs, char **args)
|
|
|
|
{
|
|
|
|
keychord_init();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int console_init_action(int nargs, char **args)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char tmp[PROP_VALUE_MAX];
|
2010-04-14 04:52:01 +02:00
|
|
|
|
|
|
|
if (console[0]) {
|
|
|
|
snprintf(tmp, sizeof(tmp), "/dev/%s", console);
|
|
|
|
console_name = strdup(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(console_name, O_RDWR);
|
|
|
|
if (fd >= 0)
|
|
|
|
have_console = 1;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if( load_565rle_image(INIT_IMAGE_FILE) ) {
|
|
|
|
fd = open("/dev/tty0", O_WRONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
const char *msg;
|
|
|
|
msg = "\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n" // console is 40 cols x 30 lines
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
" A N D R O I D ";
|
|
|
|
write(fd, msg, strlen(msg));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_init_properties_action(int nargs, char **args)
|
|
|
|
{
|
|
|
|
char tmp[PROP_VALUE_MAX];
|
|
|
|
|
|
|
|
if (qemu[0])
|
|
|
|
import_kernel_cmdline(1);
|
|
|
|
|
|
|
|
if (!strcmp(bootmode,"factory"))
|
|
|
|
property_set("ro.factorytest", "1");
|
|
|
|
else if (!strcmp(bootmode,"factory2"))
|
|
|
|
property_set("ro.factorytest", "2");
|
|
|
|
else
|
|
|
|
property_set("ro.factorytest", "0");
|
|
|
|
|
|
|
|
property_set("ro.serialno", serialno[0] ? serialno : "");
|
|
|
|
property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
|
|
|
|
property_set("ro.baseband", baseband[0] ? baseband : "unknown");
|
|
|
|
property_set("ro.carrier", carrier[0] ? carrier : "unknown");
|
|
|
|
property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
|
|
|
|
|
|
|
|
property_set("ro.hardware", hardware);
|
|
|
|
snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
|
|
|
|
property_set("ro.revision", tmp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int property_service_init_action(int nargs, char **args)
|
|
|
|
{
|
|
|
|
/* read any property files on system or data and
|
|
|
|
* fire up the property service. This must happen
|
|
|
|
* after the ro.foo properties are set above so
|
|
|
|
* that /data/local.prop cannot interfere with them.
|
|
|
|
*/
|
|
|
|
start_property_service();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int signal_init_action(int nargs, char **args)
|
|
|
|
{
|
|
|
|
signal_init();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_startup_action(int nargs, char **args)
|
|
|
|
{
|
|
|
|
/* make sure we actually have all the pieces we need */
|
2010-04-21 21:04:20 +02:00
|
|
|
if ((get_property_set_fd() < 0) ||
|
2010-04-14 04:52:01 +02:00
|
|
|
(get_signal_fd() < 0)) {
|
|
|
|
ERROR("init startup failure\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-24 23:45:30 +01:00
|
|
|
|
|
|
|
/* signal that we hit this point */
|
|
|
|
unlink("/dev/.booting");
|
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int queue_property_triggers_action(int nargs, char **args)
|
|
|
|
{
|
|
|
|
queue_all_property_triggers();
|
|
|
|
/* enable property triggers */
|
|
|
|
property_triggers_enabled = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if BOOTCHART
|
|
|
|
static int bootchart_init_action(int nargs, char **args)
|
|
|
|
{
|
|
|
|
bootchart_count = bootchart_init();
|
|
|
|
if (bootchart_count < 0) {
|
|
|
|
ERROR("bootcharting init failure\n");
|
|
|
|
} else if (bootchart_count > 0) {
|
|
|
|
NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
|
|
|
|
} else {
|
|
|
|
NOTICE("bootcharting ignored\n");
|
|
|
|
}
|
2011-01-14 09:35:30 +01:00
|
|
|
|
|
|
|
return 0;
|
2010-04-14 04:52:01 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int fd_count = 0;
|
2008-10-21 16:00:00 +02:00
|
|
|
struct pollfd ufds[4];
|
|
|
|
char *tmpdev;
|
2009-01-10 02:51:25 +01:00
|
|
|
char* debuggable;
|
2010-04-14 04:52:01 +02:00
|
|
|
char tmp[32];
|
|
|
|
int property_set_fd_init = 0;
|
|
|
|
int signal_fd_init = 0;
|
|
|
|
int keychord_fd_init = 0;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-04-21 21:04:20 +02:00
|
|
|
if (!strcmp(basename(argv[0]), "ueventd"))
|
|
|
|
return ueventd_main(argc, argv);
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
/* clear the umask */
|
|
|
|
umask(0);
|
|
|
|
|
|
|
|
/* Get the basic filesystem setup we need put
|
|
|
|
* together in the initramdisk on / and then we'll
|
|
|
|
* let the rc file figure out the rest.
|
|
|
|
*/
|
|
|
|
mkdir("/dev", 0755);
|
|
|
|
mkdir("/proc", 0755);
|
|
|
|
mkdir("/sys", 0755);
|
|
|
|
|
2010-06-23 01:35:43 +02:00
|
|
|
mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
|
2008-10-21 16:00:00 +02:00
|
|
|
mkdir("/dev/pts", 0755);
|
|
|
|
mkdir("/dev/socket", 0755);
|
|
|
|
mount("devpts", "/dev/pts", "devpts", 0, NULL);
|
|
|
|
mount("proc", "/proc", "proc", 0, NULL);
|
|
|
|
mount("sysfs", "/sys", "sysfs", 0, NULL);
|
|
|
|
|
2011-03-24 23:45:30 +01:00
|
|
|
/* indicate that booting is in progress to background fw loaders, etc */
|
|
|
|
close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
/* We must have some place other than / to create the
|
|
|
|
* device nodes for kmsg and null, otherwise we won't
|
|
|
|
* be able to remount / read-only later on.
|
|
|
|
* Now that tmpfs is mounted on /dev, we can actually
|
|
|
|
* talk to the outside world.
|
|
|
|
*/
|
|
|
|
open_devnull_stdio();
|
2011-09-01 03:26:17 +02:00
|
|
|
klog_init();
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
INFO("reading config file\n");
|
2010-04-20 23:29:05 +02:00
|
|
|
init_parse_config_file("/init.rc");
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
/* pull the kernel commandline and ramdisk properties file in */
|
|
|
|
import_kernel_cmdline(0);
|
|
|
|
|
2010-04-21 21:04:20 +02:00
|
|
|
get_hardware_name(hardware, &revision);
|
2008-10-21 16:00:00 +02:00
|
|
|
snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
|
2010-04-20 23:29:05 +02:00
|
|
|
init_parse_config_file(tmp);
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
action_for_each_trigger("early-init", action_add_queue_tail);
|
|
|
|
|
2010-04-21 21:04:20 +02:00
|
|
|
queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
|
2010-04-14 04:52:01 +02:00
|
|
|
queue_builtin_action(property_init_action, "property_init");
|
|
|
|
queue_builtin_action(keychord_init_action, "keychord_init");
|
|
|
|
queue_builtin_action(console_init_action, "console_init");
|
|
|
|
queue_builtin_action(set_init_properties_action, "set_init_properties");
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2011-08-25 00:28:23 +02:00
|
|
|
/* execute all the boot actions to get us started */
|
2008-10-21 16:00:00 +02:00
|
|
|
action_for_each_trigger("init", action_add_queue_tail);
|
2011-08-25 00:28:23 +02:00
|
|
|
|
|
|
|
/* skip mounting filesystems in charger mode */
|
|
|
|
if (strcmp(bootmode, "charger") != 0) {
|
|
|
|
action_for_each_trigger("early-fs", action_add_queue_tail);
|
|
|
|
action_for_each_trigger("fs", action_add_queue_tail);
|
|
|
|
action_for_each_trigger("post-fs", action_add_queue_tail);
|
|
|
|
action_for_each_trigger("post-fs-data", action_add_queue_tail);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
queue_builtin_action(property_service_init_action, "property_service_init");
|
|
|
|
queue_builtin_action(signal_init_action, "signal_init");
|
|
|
|
queue_builtin_action(check_startup_action, "check_startup");
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2011-08-25 00:28:23 +02:00
|
|
|
if (!strcmp(bootmode, "charger")) {
|
|
|
|
action_for_each_trigger("charger", action_add_queue_tail);
|
|
|
|
} else {
|
|
|
|
action_for_each_trigger("early-boot", action_add_queue_tail);
|
|
|
|
action_for_each_trigger("boot", action_add_queue_tail);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
/* run all property triggers based on current state of the properties */
|
2010-04-14 04:52:01 +02:00
|
|
|
queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
#if BOOTCHART
|
2010-04-14 04:52:01 +02:00
|
|
|
queue_builtin_action(bootchart_init_action, "bootchart_init");
|
2008-10-21 16:00:00 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for(;;) {
|
2009-01-10 02:51:25 +01:00
|
|
|
int nr, i, timeout = -1;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
execute_one_command();
|
2008-10-21 16:00:00 +02:00
|
|
|
restart_processes();
|
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
if (!property_set_fd_init && get_property_set_fd() > 0) {
|
|
|
|
ufds[fd_count].fd = get_property_set_fd();
|
|
|
|
ufds[fd_count].events = POLLIN;
|
|
|
|
ufds[fd_count].revents = 0;
|
|
|
|
fd_count++;
|
|
|
|
property_set_fd_init = 1;
|
|
|
|
}
|
|
|
|
if (!signal_fd_init && get_signal_fd() > 0) {
|
|
|
|
ufds[fd_count].fd = get_signal_fd();
|
|
|
|
ufds[fd_count].events = POLLIN;
|
|
|
|
ufds[fd_count].revents = 0;
|
|
|
|
fd_count++;
|
|
|
|
signal_fd_init = 1;
|
|
|
|
}
|
|
|
|
if (!keychord_fd_init && get_keychord_fd() > 0) {
|
|
|
|
ufds[fd_count].fd = get_keychord_fd();
|
|
|
|
ufds[fd_count].events = POLLIN;
|
|
|
|
ufds[fd_count].revents = 0;
|
|
|
|
fd_count++;
|
|
|
|
keychord_fd_init = 1;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
if (process_needs_restart) {
|
|
|
|
timeout = (process_needs_restart - gettime()) * 1000;
|
|
|
|
if (timeout < 0)
|
|
|
|
timeout = 0;
|
|
|
|
}
|
|
|
|
|
2010-04-22 20:52:23 +02:00
|
|
|
if (!action_queue_empty() || cur_action)
|
2010-04-14 04:52:01 +02:00
|
|
|
timeout = 0;
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
#if BOOTCHART
|
|
|
|
if (bootchart_count > 0) {
|
|
|
|
if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
|
|
|
|
timeout = BOOTCHART_POLLING_MS;
|
|
|
|
if (bootchart_step() < 0 || --bootchart_count == 0) {
|
|
|
|
bootchart_finish();
|
|
|
|
bootchart_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-14 04:52:01 +02:00
|
|
|
|
2009-01-10 02:51:25 +01:00
|
|
|
nr = poll(ufds, fd_count, timeout);
|
2008-10-21 16:00:00 +02:00
|
|
|
if (nr <= 0)
|
|
|
|
continue;
|
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
for (i = 0; i < fd_count; i++) {
|
|
|
|
if (ufds[i].revents == POLLIN) {
|
2010-04-21 21:04:20 +02:00
|
|
|
if (ufds[i].fd == get_property_set_fd())
|
2010-04-14 04:52:01 +02:00
|
|
|
handle_property_set_fd();
|
|
|
|
else if (ufds[i].fd == get_keychord_fd())
|
|
|
|
handle_keychord();
|
|
|
|
else if (ufds[i].fd == get_signal_fd())
|
|
|
|
handle_signal();
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|