From 75eb5cc043d0b327ea0219d4af74a8560e2cbcc1 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 25 Sep 2012 15:12:26 +0200 Subject: [PATCH] Move fork() of console to run_getty() and remove startx conf option New function helpers.c:run_getty() refactored from main(). Used to start a default console getty. Removed startx finit.conf option, superseded by service statement, see examples/finit.conf for a working use case. Signed-off-by: Joachim Nilsson --- conf.c | 5 --- examples/finit.conf | 2 +- finit.c | 75 ++++++--------------------------------------- finit.h | 1 - helpers.c | 59 +++++++++++++++++++++++++++++++++++ helpers.h | 1 + 6 files changed, 70 insertions(+), 73 deletions(-) diff --git a/conf.c b/conf.c index 15f8aa69..00358e6c 100644 --- a/conf.c +++ b/conf.c @@ -122,11 +122,6 @@ void parse_finit_conf(char *file) network = build_cmd(NULL, x, CMD_SIZE); continue; } - if (MATCH_CMD(line, "startx ", x)) { - if (startx) free(startx); - startx = build_cmd(NULL, x, CMD_SIZE); - continue; - } if (MATCH_CMD(line, "service ", x)) { if (svc_register(x)) _e("Failed, too many services to monitor.\n"); diff --git a/examples/finit.conf b/examples/finit.conf index 735506df..8bcb74cb 100644 --- a/examples/finit.conf +++ b/examples/finit.conf @@ -10,4 +10,4 @@ network service networking start service /sbin/klogd -n -- Kernel logging server service /sbin/syslogd -n -- Syslog server service /usr/sbin/sshd -D -- OpenSSH server -startx startx +service /usr/bin/startx -- X Window System diff --git a/finit.c b/finit.c index 5a6914d6..a7af778b 100644 --- a/finit.c +++ b/finit.c @@ -38,7 +38,6 @@ int debug = 0; int verbose = 1; char *sdown = NULL; char *network = NULL; -char *startx = NULL; char *username = NULL; char *hostname = NULL; @@ -165,81 +164,25 @@ int main(int UNUSED(args), char *argv[]) _d("Starting all static services from %s", FINIT_CONF); svc_start_all(); - /* Run startup scripts in /etc/finit.d/, if any. */ + /* + * Run startup scripts in /etc/finit.d/, if any. + */ + _d("Running startup scripts in /etc/finit.d/ ..."); run_parts(FINIT_RCSD); - if (!fork()) { - /* child process */ - int i; - char c; - sigset_t nmask; - struct sigaction sa; - - vhangup(); - - close(2); - close(1); - close(0); - - if (open(CONSOLE, O_RDWR) != 0) - exit(1); - - sigemptyset(&sa.sa_mask); - sa.sa_handler = SIG_DFL; - - sigemptyset(&nmask); - sigaddset(&nmask, SIGCHLD); - sigprocmask(SIG_UNBLOCK, &nmask, NULL); - - for (i = 1; i < NSIG; i++) - sigaction(i, &sa, NULL); - - dup2(0, STDIN_FILENO); - dup2(0, STDOUT_FILENO); - dup2(0, STDERR_FILENO); - - set_procname(argv, "console"); - - /* ConsoleKit needs this */ - setenv("DISPLAY", ":0", 1); - - while (!fexist(SYNC_SHUTDOWN)) { - char line[LINE_SIZE]; - - if (fexist(SYNC_STOPPED)) { - sleep(1); - continue; - } - - if (startx && !debug) { - echo("Starting X ..."); - snprintf(line, sizeof(line), "su -c '%s' -l %s", startx, username); - system(line); - } else { - static const char msg[] = "\nPlease press Enter to activate this console. "; - - i = write(STDERR_FILENO, msg, sizeof(msg) - 1); - while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n') - continue; - - if (fexist(SYNC_STOPPED)) - continue; - - run(GETTY); - } - } - - exit(0); - } - /* * Hooks that should run at the very end */ run_hooks(HOOK_SYSTEM_UP); + /* Start GETTY on console */ + _d("Starting getty on console ..."); + run_getty(GETTY, argv); + /* * Enter main loop to monior /dev/initctl and services */ + _d("Entering main loop ..."); ev_run(loop, 0); return 0; diff --git a/finit.h b/finit.h index 816641f8..a61f60c8 100644 --- a/finit.h +++ b/finit.h @@ -103,7 +103,6 @@ extern int debug; extern int verbose; extern char *sdown; extern char *network; -extern char *startx; extern char *hostname; extern char *username; diff --git a/helpers.c b/helpers.c index 7047a155..79a76da0 100644 --- a/helpers.c +++ b/helpers.c @@ -343,6 +343,65 @@ int run_interactive(char *cmd, char *fmt, ...) return status; } +pid_t run_getty(char *cmd, char *argv[]) +{ + pid_t pid = fork(); + + if (!pid) { + int i; + char c; + sigset_t nmask; + struct sigaction sa; + + vhangup(); + + close(2); + close(1); + close(0); + + if (open(CONSOLE, O_RDWR) != 0) + exit(1); + + sigemptyset(&sa.sa_mask); + sa.sa_handler = SIG_DFL; + + sigemptyset(&nmask); + sigaddset(&nmask, SIGCHLD); + sigprocmask(SIG_UNBLOCK, &nmask, NULL); + + for (i = 1; i < NSIG; i++) + sigaction(i, &sa, NULL); + + dup2(0, STDIN_FILENO); + dup2(0, STDOUT_FILENO); + dup2(0, STDERR_FILENO); + + set_procname(argv, "console"); + + while (!fexist(SYNC_SHUTDOWN)) { + static const char msg[] = "\nPlease press Enter to activate this console. "; + + if (fexist(SYNC_STOPPED)) { + sleep(1); + continue; + } + + i = write(STDERR_FILENO, msg, sizeof(msg) - 1); + while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n') + continue; + + if (fexist(SYNC_STOPPED)) + continue; + + run(cmd); + } + + exit(0); + } + + return pid; +} + static int cmp(const void *s1, const void *s2) { return strcmp(*(char **)s1, *(char **)s2); diff --git a/helpers.h b/helpers.h index b9a1838b..ef398fb4 100644 --- a/helpers.h +++ b/helpers.h @@ -42,6 +42,7 @@ void set_hostname (char *hostname); int run (char *cmd); int run_interactive (char *cmd, char *fmt, ...); +pid_t run_getty (char *cmd, char *argv[]); int run_parts (char *dir, ...); #endif /* FINIT_HELPERS_H_ */