39 extern void pedigree_reboot();
41 static int g_Running = 1;
44 static void sigterm(
int sig)
50 static pid_t start(
const char *proc)
55 klog(LOG_ALERT,
"init: fork failed %s", strerror(errno));
60 klog(LOG_INFO,
"init: starting %s...", proc);
62 klog(LOG_INFO,
"init: loading %s failed: %s", proc, strerror(errno));
65 klog(LOG_INFO,
"init: %s running with pid %d", proc, f);
69 char basename_buf[PATH_MAX];
70 strncpy(basename_buf, proc, PATH_MAX);
76 memset(&init, 0,
sizeof(init));
77 gettimeofday(&tv, NULL);
78 init.ut_type = INIT_PROCESS;
81 strncpy(init.ut_id, basename(basename_buf), UT_LINESIZE);
88 static void startAndWait(
const char *proc)
90 pid_t f = start(proc);
94 static void runScripts()
96 struct dirent **namelist;
98 int count = scandir(
"/system/initscripts", &namelist, 0, alphasort);
102 LOG_CRIT,
"could not scan /system/initscripts: %s",
107 for (
int i = 0; i < count; ++i)
109 char script[PATH_MAX];
111 script, PATH_MAX,
"/system/initscripts/%s",
112 namelist[i]->d_name);
114 if (!strcmp(namelist[i]->d_name,
".") ||
115 !strcmp(namelist[i]->d_name,
".."))
124 int r = stat(script, &st);
127 if (S_ISREG(st.st_mode) &&
128 (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
131 klog(LOG_INFO,
"init: running %s", script);
132 startAndWait(script);
138 "init: not running %s (not a file, or not executable)",
145 LOG_INFO,
"init: cannot stat %s (broken symlink?)", script);
153 int main(
int argc,
char **argv)
155 klog(LOG_INFO,
"init: starting...");
158 int fd = open(UTMP_FILE, O_CREAT | O_RDWR, 0664);
170 gettimeofday(&tv, NULL);
173 memset(&boot, 0,
sizeof(boot));
174 boot.ut_type = BOOT_TIME;
182 signal(SIGTERM, sigterm);
186 klog(LOG_INFO,
"init: hosted build, triggering a reboot");
193 klog(LOG_INFO,
"init: complete!");
199 pid_t changer = waitpid(-1, &status, g_Running == 0 ? WNOHANG : 0);
203 LOG_INFO,
"init: child %d exited with status %d", changer,
204 WEXITSTATUS(status));
209 LOG_INFO,
"init: no more children and have been asked to " 210 "terminate, terminating...");
224 if (p && (p->ut_type == INIT_PROCESS && p->ut_pid == changer))
236 memset(&dead, 0,
sizeof(dead));
237 gettimeofday(&tv, NULL);
238 dead.ut_type = DEAD_PROCESS;
239 dead.ut_pid = changer;
241 strncpy(dead.ut_id, p->ut_id, UT_LINESIZE);