Add in some (theoretical) uClinux support. Some init cleanups

This commit is contained in:
Eric Andersen 2001-10-28 05:12:20 +00:00
parent 9af363fe20
commit 72f9a4277f
7 changed files with 82 additions and 51 deletions

View File

@ -75,6 +75,12 @@ struct serial_struct {
}; };
#if (__GNU_LIBRARY__ > 5) || defined(__dietlibc__)
#include <sys/reboot.h>
#define init_reboot(magic) reboot(magic)
#else
#define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
#endif
#ifndef RB_HALT_SYSTEM #ifndef RB_HALT_SYSTEM
static const int RB_HALT_SYSTEM = 0xcdef0123; static const int RB_HALT_SYSTEM = 0xcdef0123;
@ -84,13 +90,6 @@ static const int RB_DISABLE_CAD = 0;
static const int RB_AUTOBOOT = 0x01234567; static const int RB_AUTOBOOT = 0x01234567;
#endif #endif
#if (__GNU_LIBRARY__ > 5) || defined(__dietlibc__)
#include <sys/reboot.h>
#define init_reboot(magic) reboot(magic)
#else
#define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
#endif
#ifndef _PATH_STDPATH #ifndef _PATH_STDPATH
#define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin" #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
#endif #endif
@ -218,7 +217,7 @@ static void message(int device, char *fmt, ...)
device = CONSOLE; device = CONSOLE;
} else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) { } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
log_fd = -2; log_fd = -2;
fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log); fprintf(stderr, "Bummer, can't write to log on %s!\n", log);
log = NULL; log = NULL;
device = CONSOLE; device = CONSOLE;
} }
@ -382,7 +381,7 @@ static void console_init(void)
if (strcmp( termType, "TERM=linux" ) == 0) if (strcmp( termType, "TERM=linux" ) == 0)
safe_strncpy(termType, "TERM=vt102", sizeof(termType)); safe_strncpy(termType, "TERM=vt102", sizeof(termType));
message(LOG | CONSOLE, message(LOG | CONSOLE,
"serial console detected. Disabling virtual terminals.\r\n"); "serial console detected. Disabling virtual terminals.\n");
} }
close(fd); close(fd);
} }
@ -395,7 +394,7 @@ static void fixup_argv(int argc, char **argv, char *new_argv0)
/* Fix up argv[0] to be certain we claim to be init */ /* Fix up argv[0] to be certain we claim to be init */
len = strlen(argv[0]); len = strlen(argv[0]);
memset(argv[0], 0, len); memset(argv[0], 0, len);
strncpy(argv[0], new_argv0, len); safe_strncpy(argv[0], new_argv0, len + 1);
/* Wipe argv[1]-argv[N] so they don't clutter the ps listing */ /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
len = 1; len = 1;
@ -425,7 +424,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
char *environment[MAXENV+1] = { char *environment[MAXENV+1] = {
termType, termType,
"HOME=/", "HOME=/",
"PATH=/usr/bin:/bin:/usr/sbin:/sbin", "PATH=" _PATH_STDPATH,
"SHELL=" SHELL, "SHELL=" SHELL,
"USER=root", "USER=root",
NULL NULL
@ -444,7 +443,12 @@ static pid_t run(char *command, char *terminal, int get_enter)
} }
} }
if ((pid = fork()) == 0) { #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
if ((pid = fork()) == 0)
#else
if ((pid = vfork()) == 0)
#endif
{
/* Clean up */ /* Clean up */
ioctl(0, TIOCNOTTY, 0); ioctl(0, TIOCNOTTY, 0);
close(0); close(0);
@ -465,7 +469,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
terminal); terminal);
exit(1); exit(1);
} }
message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal); message(LOG | CONSOLE, "Bummer, can't open %s\n", terminal);
exit(1); exit(1);
} }
dup2(fd, 0); dup2(fd, 0);
@ -480,13 +484,16 @@ static pid_t run(char *command, char *terminal, int get_enter)
cmd[0] = SHELL; cmd[0] = SHELL;
cmd[1] = "-c"; cmd[1] = "-c";
strcpy(buf, "exec "); strcpy(buf, "exec ");
strncat(buf, command, sizeof(buf) - strlen(buf) - 1); safe_strncpy(buf + sizeof("exec "), command,
sizeof(buf) - sizeof("exec "));
cmd[2] = buf; cmd[2] = buf;
cmd[3] = NULL; cmd[3] = NULL;
} else { } else {
/* Convert command (char*) into cmd (char**, one word per string) */ /* Convert command (char*) into cmd (char**, one word per string) */
for (tmpCmd = command, i = 0; safe_strncpy(buf, command, sizeof(buf));
(tmpCmd = strsep(&command, " \t")) != NULL;) { s = buf;
for (tmpCmd = buf, i = 0;
(tmpCmd = strsep(&s, " \t")) != NULL;) {
if (*tmpCmd != '\0') { if (*tmpCmd != '\0') {
cmd[i] = tmpCmd; cmd[i] = tmpCmd;
tmpCmd++; tmpCmd++;
@ -533,7 +540,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
* specifies. * specifies.
*/ */
#ifdef DEBUG_INIT #ifdef DEBUG_INIT
message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n", message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\n",
cmd[0], getpid(), terminal); cmd[0], getpid(), terminal);
#endif #endif
write(fileno(stdout), press_enter, sizeof(press_enter) - 1); write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
@ -542,7 +549,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
#ifdef DEBUG_INIT #ifdef DEBUG_INIT
/* Log the process name and args */ /* Log the process name and args */
message(LOG, "Starting pid %d, console %s: '%s'\r\n", message(LOG, "Starting pid %d, console %s: '%s'\n",
getpid(), terminal, command); getpid(), terminal, command);
#endif #endif
@ -592,6 +599,7 @@ static void check_memory(void)
if (check_free_memory() > 1000) if (check_free_memory() > 1000)
return; return;
#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
if (stat("/etc/fstab", &statBuf) == 0) { if (stat("/etc/fstab", &statBuf) == 0) {
/* swapon -a requires /proc typically */ /* swapon -a requires /proc typically */
waitfor("mount proc /proc -t proc", console, FALSE); waitfor("mount proc /proc -t proc", console, FALSE);
@ -602,10 +610,11 @@ static void check_memory(void)
} else } else
goto goodnight; goto goodnight;
return; return;
#endif
goodnight: goodnight:
message(CONSOLE, message(CONSOLE,
"Sorry, your computer does not have enough memory.\r\n"); "Sorry, your computer does not have enough memory.\n");
loop_forever(); loop_forever();
} }
@ -627,22 +636,29 @@ static void run_actions(initActionEnum action)
static void shutdown_system(void) static void shutdown_system(void)
{ {
/* first disable our SIGHUP signal */ /* first disable all our signals */
signal(SIGHUP, SIG_DFL); sigemptyset(&block_signals);
sigaddset(&block_signals, SIGHUP);
sigaddset(&block_signals, SIGCHLD);
sigaddset(&block_signals, SIGUSR1);
sigaddset(&block_signals, SIGUSR2);
sigaddset(&block_signals, SIGINT);
sigaddset(&block_signals, SIGTERM);
sigprocmask(SIG_BLOCK, &block_signals, NULL);
/* Allow Ctrl-Alt-Del to reboot system. */ /* Allow Ctrl-Alt-Del to reboot system. */
init_reboot(RB_ENABLE_CAD); init_reboot(RB_ENABLE_CAD);
message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n"); message(CONSOLE|LOG, "\nThe system is going down NOW !!\n");
sync(); sync();
/* Send signals to every process _except_ pid 1 */ /* Send signals to every process _except_ pid 1 */
message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n"); message(CONSOLE|LOG, "Sending SIGTERM to all processes.\n");
kill(-1, SIGTERM); kill(-1, SIGTERM);
sleep(1); sleep(1);
sync(); sync();
message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n"); message(CONSOLE|LOG, "Sending SIGKILL to all processes.\n");
kill(-1, SIGKILL); kill(-1, SIGKILL);
sleep(1); sleep(1);
@ -661,7 +677,7 @@ static void halt_signal(int sig)
{ {
shutdown_system(); shutdown_system();
message(CONSOLE|LOG, message(CONSOLE|LOG,
"The system is halted. Press %s or turn off power\r\n", "The system is halted. Press %s or turn off power\n",
(secondConsole == NULL) /* serial console */ (secondConsole == NULL) /* serial console */
? "Reset" : "CTRL-ALT-DEL"); ? "Reset" : "CTRL-ALT-DEL");
sync(); sync();
@ -680,7 +696,7 @@ static void halt_signal(int sig)
static void reboot_signal(int sig) static void reboot_signal(int sig)
{ {
shutdown_system(); shutdown_system();
message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n"); message(CONSOLE|LOG, "Please stand by while rebooting the system.\n");
sync(); sync();
/* allow time for last message to reach serial console */ /* allow time for last message to reach serial console */
@ -701,9 +717,7 @@ static void ctrlaltdel_signal(int sig)
static void new_initAction(initActionEnum action, char *process, char *cons) static void new_initAction(initActionEnum action, char *process, char *cons)
{ {
initAction *newAction; initAction *newAction;
#ifdef CONFIG_FEATURE_INIT_NORMAL_ORDER
initAction *a; initAction *a;
#endif
if (*cons == '\0') if (*cons == '\0')
cons = console; cons = console;
@ -723,20 +737,15 @@ static void new_initAction(initActionEnum action, char *process, char *cons)
message(LOG | CONSOLE, "Memory allocation failure\n"); message(LOG | CONSOLE, "Memory allocation failure\n");
loop_forever(); loop_forever();
} }
#ifdef CONFIG_FEATURE_INIT_NORMAL_ORDER
for (a = initActionList; a && a->nextPtr; a = a->nextPtr) ; for (a = initActionList; a && a->nextPtr; a = a->nextPtr) ;
if (a) { if (a) {
a->nextPtr = newAction; a->nextPtr = newAction;
} else { } else {
initActionList = newAction; initActionList = newAction;
} }
#else safe_strncpy(newAction->process, process, 255);
newAction->nextPtr = initActionList;
initActionList = newAction;
#endif
strncpy(newAction->process, process, 255);
newAction->action = action; newAction->action = action;
strncpy(newAction->console, cons, 255); safe_strncpy(newAction->console, cons, 255);
newAction->pid = 0; newAction->pid = 0;
// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n", // message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
// newAction->process, newAction->action, newAction->console); // newAction->process, newAction->action, newAction->console);
@ -782,16 +791,11 @@ static void parse_inittab(void)
#endif #endif
/* Reboot on Ctrl-Alt-Del */ /* Reboot on Ctrl-Alt-Del */
new_initAction(CTRLALTDEL, "/sbin/reboot", console); new_initAction(CTRLALTDEL, "/sbin/reboot", console);
#ifdef CONFIG_FEATURE_INIT_NORMAL_ORDER
/* Umount all filesystems on halt/reboot */ /* Umount all filesystems on halt/reboot */
new_initAction(SHUTDOWN, "/bin/umount -a -r", console); new_initAction(SHUTDOWN, "/bin/umount -a -r", console);
#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
/* Swapoff on halt/reboot */ /* Swapoff on halt/reboot */
new_initAction(SHUTDOWN, "/sbin/swapoff -a", console); new_initAction(SHUTDOWN, "/sbin/swapoff -a", console);
#else
/* Swapoff on halt/reboot */
new_initAction(SHUTDOWN, "/sbin/swapoff -a", console);
/* Umount all filesystems on halt/reboot */
new_initAction(SHUTDOWN, "/bin/umount -a -r", console);
#endif #endif
/* Askfirst shell on tty1 */ /* Askfirst shell on tty1 */
new_initAction(ASKFIRST, LOGIN_SHELL, console); new_initAction(ASKFIRST, LOGIN_SHELL, console);
@ -916,13 +920,14 @@ extern int init_main(int argc, char **argv)
/* Figure out what kernel this is running */ /* Figure out what kernel this is running */
kernelVersion = get_kernel_revision(); kernelVersion = get_kernel_revision();
/* Figure out where the default console should be */
console_init();
/* Close whatever files are open, and reset the console. */ /* Close whatever files are open, and reset the console. */
close(0); close(0);
close(1); close(1);
close(2); close(2);
/* Figure out where the default console should be */
console_init();
set_term(0); set_term(0);
chdir("/"); chdir("/");
setsid(); setsid();
@ -937,14 +942,14 @@ extern int init_main(int argc, char **argv)
CONSOLE| CONSOLE|
#endif #endif
LOG, LOG,
"init started: %s\r\n", full_version); "init started: %s\n", full_version);
#else #else
message( message(
#if ! defined CONFIG_FEATURE_EXTRA_QUIET #if ! defined CONFIG_FEATURE_EXTRA_QUIET
CONSOLE| CONSOLE|
#endif #endif
LOG, LOG,
"init(%d) started: %s\r\n", getpid(), full_version); "init(%d) started: %s\n", getpid(), full_version);
#endif #endif

View File

@ -15,7 +15,7 @@
* Foundation; either version 2 of the License, or (at * Foundation; either version 2 of the License, or (at
* your option) any later version. * your option) any later version.
* *
* $Id: ifconfig.c,v 1.13 2001/10/24 04:59:56 andersen Exp $ * $Id: ifconfig.c,v 1.14 2001/10/28 05:12:18 andersen Exp $
* *
*/ */
@ -135,7 +135,7 @@
#define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/) #define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
#define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/) #define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
#define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG) #define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
#define ARG_IO_ADDR (A_ARG_REQ | A_MAP_USHORT) #define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
#define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR) #define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
#define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE) #define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
#define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK) #define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)

View File

@ -6756,7 +6756,11 @@ forkshell(struct job *jp, const union node *n, int mode)
TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
mode)); mode));
INTOFF; INTOFF;
#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
pid = fork(); pid = fork();
#else
pid = vfork();
#endif
if (pid == -1) { if (pid == -1) {
TRACE(("Fork failed, errno=%d\n", errno)); TRACE(("Fork failed, errno=%d\n", errno));
INTON; INTON;
@ -12627,7 +12631,7 @@ findvar(struct var **vpp, const char *name)
/* /*
* Copyright (c) 1999 Herbert Xu <herbert@debian.org> * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
* This file contains code for the times builtin. * This file contains code for the times builtin.
* $Id: ash.c,v 1.31 2001/10/24 17:19:35 andersen Exp $ * $Id: ash.c,v 1.32 2001/10/28 05:12:18 andersen Exp $
*/ */
static int timescmd (int argc, char **argv) static int timescmd (int argc, char **argv)
{ {

View File

@ -1403,7 +1403,12 @@ static int run_pipe_real(struct pipe *pi)
} }
/* XXX test for failed fork()? */ /* XXX test for failed fork()? */
if (!(child->pid = fork())) { #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
if (!(child->pid = fork()))
#else
if (!(child->pid = vfork()))
#endif
{
/* Set the handling for job control signals back to the default. */ /* Set the handling for job control signals back to the default. */
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL); signal(SIGQUIT, SIG_DFL);
@ -2118,7 +2123,11 @@ FILE *generate_stream_from_list(struct pipe *head)
#if 1 #if 1
int pid, channel[2]; int pid, channel[2];
if (pipe(channel)<0) perror_msg_and_die("pipe"); if (pipe(channel)<0) perror_msg_and_die("pipe");
#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
pid=fork(); pid=fork();
#else
pid=vfork();
#endif
if (pid<0) { if (pid<0) {
perror_msg_and_die("fork"); perror_msg_and_die("fork");
} else if (pid==0) { } else if (pid==0) {

View File

@ -1347,7 +1347,12 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
} }
} }
if (!(child->pid = fork())) { #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
if (!(child->pid = fork()))
#else
if (!(child->pid = vfork()))
#endif
{
/* Set the handling for job control signals back to the default. */ /* Set the handling for job control signals back to the default. */
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL); signal(SIGQUIT, SIG_DFL);

View File

@ -136,8 +136,12 @@ extern int klogd_main(int argc, char **argv)
} }
if (doFork == TRUE) { if (doFork == TRUE) {
#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
if (daemon(0, 1) < 0) if (daemon(0, 1) < 0)
perror_msg_and_die("daemon"); perror_msg_and_die("daemon");
#else
error_msg_and_die("daemon not supported");
#endif
} }
doKlogd(); doKlogd();

View File

@ -630,8 +630,12 @@ extern int syslogd_main(int argc, char **argv)
umask(0); umask(0);
if (doFork == TRUE) { if (doFork == TRUE) {
#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
if (daemon(0, 1) < 0) if (daemon(0, 1) < 0)
perror_msg_and_die("daemon"); perror_msg_and_die("daemon");
#else
error_msg_and_die("daemon not supported");
#endif
} }
doSyslogd(); doSyslogd();