OSDN Git Service

Added poweroff (and adjusted init to use it). Inlined function
authorEric Andersen <andersen@codepoet.org>
Fri, 10 Dec 1999 08:25:07 +0000 (08:25 -0000)
committerEric Andersen <andersen@codepoet.org>
Fri, 10 Dec 1999 08:25:07 +0000 (08:25 -0000)
calls to code only called once in tee.  Made BB_KLOGD and option.
 -Erik

16 files changed:
Changelog
TODO
applets/busybox.c
busybox.c
busybox.def.h
coreutils/tee.c
init.c
init/init.c
init/poweroff.c [new file with mode: 0644]
init/reboot.c
internal.h
poweroff.c [new file with mode: 0644]
reboot.c
sysklogd/syslogd.c
syslogd.c
tee.c

index 3f2efba..4f8fb09 100644 (file)
--- a/Changelog
+++ b/Changelog
        * kill now behaves itself properly, added 'kill -l' to list signals
        * 'ls -l' was failing on long directories, since my_getid was leaking 
            one file descriptor per file.  Oops.
-       * Fixed rebooting from init.  I'd left some debugging code in
+       * Fixed rebooting from init.  I'd accidently left some debugging code in
            which blocked reboots.
+       * Fixed reboot, halt (and added poweroff) such that they handle it when
+           init is not at PID 1 (like when running in an initrd).
        * Added a prelinary du implementation.  Some parameter parsing
            stuff still needs to be added. -beppu  (John Beppu <beppu@lineo.com>)
        * Implemented tee.  -beppu
diff --git a/TODO b/TODO
index 5daa67a..4cc82d5 100644 (file)
--- a/TODO
+++ b/TODO
@@ -9,9 +9,6 @@ around to it some time. If you have any good ideas, please let me know.
 
 * Allow tar to create archives with sockets, devices, and other special files
 * Add in a mini insmod, rmmod, lsmod
-* Change init so halt, reboot (and poweroff) work with an initrd
-    when init is not PID 1
-* poweroff
 * mkfifo
 * dnsdomainname
 * traceroute/nslookup/netstat
@@ -22,7 +19,7 @@ around to it some time. If you have any good ideas, please let me know.
 * sort/uniq
 * wc
 * tr
-* expr (maybe)?  (ash builtin?)
+* expr (maybe?)  (ash builtin?)
 * login/sulogin/passwd/getty  (These are actully now part of tinylogin, which 
                                 I've just started to maintain).
 
index f457301..d1eb38a 100644 (file)
@@ -138,6 +138,9 @@ static const struct Applet applets[] = {
 #ifdef BB_PING                  //bin
     {"ping", ping_main},
 #endif
+#ifdef BB_POWEROFF              //sbin
+    {"poweroff", poweroff_main},
+#endif
 #ifdef BB_PRINTF               //usr/bin
     {"printf", printf_main},
 #endif
index f457301..d1eb38a 100644 (file)
--- a/busybox.c
+++ b/busybox.c
@@ -138,6 +138,9 @@ static const struct Applet applets[] = {
 #ifdef BB_PING                  //bin
     {"ping", ping_main},
 #endif
+#ifdef BB_POWEROFF              //sbin
+    {"poweroff", poweroff_main},
+#endif
 #ifdef BB_PRINTF               //usr/bin
     {"printf", printf_main},
 #endif
index 1bdb9a4..3053361 100644 (file)
@@ -28,6 +28,7 @@
 #define BB_HOSTNAME
 #define BB_INIT
 #define BB_KILL
+#define BB_KLOGD
 //#define BB_LENGTH
 #define BB_LN
 #define BB_LOADFONT
@@ -47,6 +48,7 @@
 //#define BB_MTAB
 #define BB_MV
 #define BB_PING
+#define BB_POWEROFF
 //#define BB_PRINTF
 #define BB_PS
 #define BB_PWD
index 45128b5..8d1ca6e 100644 (file)
 #include <stdio.h>
 
 static const char tee_usage[] =
-"Usage: tee [OPTION]... [FILE]...\n"
-"Copy standard input to each FILE, and also to standard output.\n\n"
-"  -a,    append to the given FILEs, do not overwrite\n"
-"  -i,    ignore interrupt signals\n"
-"  -h,    this help message\n";
+    "tee [OPTION]... [FILE]...\n\n"
+    "Copy standard input to each FILE, and also to standard output.\n\n"
+    "Options:\n"
+    "\t-a\tappend to the given FILEs, do not overwrite\n"
+#if 0
+    "\t-i\tignore interrupt signals\n"
+#endif
+    ;
+
 
 /* FileList _______________________________________________________________ */
 
@@ -39,27 +43,6 @@ static int  FL_end;
 
 typedef void (FL_Function)(FILE *file, char c);
     
-/* initialize FileList */
-static void
-FL_init()
-{
-    FL_end = 0;
-    FileList[0] = stdout;
-}
-
-/* add a file to FileList */
-static int
-FL_add(const char *filename, char *opt_open)
-{
-    FILE    *file;
-
-    file = fopen(filename, opt_open);
-    if (!file) { return 0; };
-    if (FL_end < FL_MAX) {
-       FileList[++FL_end] = file;
-    }
-    return 1;
-}
 
 /* apply a function to everything in FileList */
 static void
@@ -71,8 +54,6 @@ FL_apply(FL_Function *f, char c)
     }
 }
 
-/* ________________________________________________________________________ */
-
 /* FL_Function for writing to files*/
 static void
 tee_fwrite(FILE *file, char c)
@@ -87,6 +68,8 @@ tee_fclose(FILE *file, char c)
     fclose(file);
 }
 
+/* ________________________________________________________________________ */
+
 /* BusyBoxed tee(1) */
 int
 tee_main(int argc, char **argv)
@@ -95,6 +78,7 @@ tee_main(int argc, char **argv)
     char    c;
     char    opt;
     char    opt_fopen[2] = "w";
+    FILE    *file;
 
     /* parse argv[] */
     for (i = 1; i < argc; i++) {
@@ -104,14 +88,12 @@ tee_main(int argc, char **argv)
                case 'a':
                    opt_fopen[0] = 'a';
                    break;
+#if 0
                case 'i':
-                   fprintf(stderr, "ingore interrupt not implemented\n");
-                   break;
-               case 'h':
-                   usage(tee_usage);
+                   fprintf(stderr, "ignore interrupt not implemented\n");
                    break;
+#endif
                default:
-                   fprintf(stderr, "tee: invalid option -- %c\n", opt);
                    usage(tee_usage);
            }
        } else {
@@ -120,9 +102,15 @@ tee_main(int argc, char **argv)
     }
 
     /* init FILE pointers */
-    FL_init();
+    FL_end = 0;
+    FileList[0] = stdout;
     for ( ; i < argc; i++) {
-       FL_add(argv[i], opt_fopen);
+       /* add a file to FileList */
+       file = fopen(argv[i], opt_fopen);
+       if (!file) { continue; }
+       if (FL_end < FL_MAX) {
+           FileList[++FL_end] = file;
+       }
     }
 
     /* read and redirect */
@@ -135,4 +123,4 @@ tee_main(int argc, char **argv)
     exit(0);
 }
 
-/* $Id: tee.c,v 1.3 1999/12/10 07:41:03 beppu Exp $ */
+/* $Id: tee.c,v 1.4 1999/12/10 08:25:07 andersen Exp $ */
diff --git a/init.c b/init.c
index d2e9a7e..3c800b9 100644 (file)
--- a/init.c
+++ b/init.c
@@ -336,9 +336,9 @@ static pid_t run(const char * const* command,
        }
 
        /* Log the process name and args */
-       message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
-       while ( *cmd) message(LOG, "%s ", *cmd++);
-       message(LOG, "'\r\n");
+       message(LOG|CONSOLE, "Starting pid %d, console %s: '", getpid(), terminal);
+       while ( *cmd) message(LOG|CONSOLE, "%s ", *cmd++);
+       message(LOG|CONSOLE, "'\r\n");
        
        /* Now run it.  The new program will take over this PID, 
         * so nothing further in init.c should be run. */
@@ -418,8 +418,10 @@ static void halt_signal(int sig)
            "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
     sync();
 #ifndef DEBUG_INIT
-    reboot(RB_HALT_SYSTEM);
-    //reboot(RB_POWER_OFF);
+    if (sig == SIGUSR2)
+       reboot(RB_POWER_OFF);
+    else
+       reboot(RB_HALT_SYSTEM);
 #endif
     exit(0);
 }
@@ -514,8 +516,11 @@ extern int init_main(int argc, char **argv)
     } else
        message(CONSOLE|LOG, "Mounting /proc: failed!\n");
 
+fprintf(stderr, "got proc\n");
+
     /* Make sure there is enough memory to do something useful. */
     check_memory();
+fprintf(stderr, "got check_memory\n");
 
     /* Check if we are supposed to be in single user mode */
     if ( argc > 1 && (!strcmp(argv[1], "single") || 
@@ -524,6 +529,7 @@ extern int init_main(int argc, char **argv)
        tty1_command = shell_command;
        tty2_command = shell_command;
     }
+fprintf(stderr, "got single\n");
 
     /* Make sure an init script exists before trying to run it */
     if (single==FALSE && stat(INITSCRIPT, &statbuf)==0) {
@@ -535,6 +541,7 @@ extern int init_main(int argc, char **argv)
     /* Make sure /sbin/getty exists before trying to run it */
     if (stat(GETTY, &statbuf)==0) {
        char* where;
+fprintf(stderr, "\n");
        wait_for_enter_tty2 = FALSE;
        where = strrchr( console, '/');
        if ( where != NULL) {
index d2e9a7e..3c800b9 100644 (file)
@@ -336,9 +336,9 @@ static pid_t run(const char * const* command,
        }
 
        /* Log the process name and args */
-       message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
-       while ( *cmd) message(LOG, "%s ", *cmd++);
-       message(LOG, "'\r\n");
+       message(LOG|CONSOLE, "Starting pid %d, console %s: '", getpid(), terminal);
+       while ( *cmd) message(LOG|CONSOLE, "%s ", *cmd++);
+       message(LOG|CONSOLE, "'\r\n");
        
        /* Now run it.  The new program will take over this PID, 
         * so nothing further in init.c should be run. */
@@ -418,8 +418,10 @@ static void halt_signal(int sig)
            "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
     sync();
 #ifndef DEBUG_INIT
-    reboot(RB_HALT_SYSTEM);
-    //reboot(RB_POWER_OFF);
+    if (sig == SIGUSR2)
+       reboot(RB_POWER_OFF);
+    else
+       reboot(RB_HALT_SYSTEM);
 #endif
     exit(0);
 }
@@ -514,8 +516,11 @@ extern int init_main(int argc, char **argv)
     } else
        message(CONSOLE|LOG, "Mounting /proc: failed!\n");
 
+fprintf(stderr, "got proc\n");
+
     /* Make sure there is enough memory to do something useful. */
     check_memory();
+fprintf(stderr, "got check_memory\n");
 
     /* Check if we are supposed to be in single user mode */
     if ( argc > 1 && (!strcmp(argv[1], "single") || 
@@ -524,6 +529,7 @@ extern int init_main(int argc, char **argv)
        tty1_command = shell_command;
        tty2_command = shell_command;
     }
+fprintf(stderr, "got single\n");
 
     /* Make sure an init script exists before trying to run it */
     if (single==FALSE && stat(INITSCRIPT, &statbuf)==0) {
@@ -535,6 +541,7 @@ extern int init_main(int argc, char **argv)
     /* Make sure /sbin/getty exists before trying to run it */
     if (stat(GETTY, &statbuf)==0) {
        char* where;
+fprintf(stderr, "\n");
        wait_for_enter_tty2 = FALSE;
        where = strrchr( console, '/');
        if ( where != NULL) {
diff --git a/init/poweroff.c b/init/poweroff.c
new file mode 100644 (file)
index 0000000..405ca3f
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Mini poweroff implementation for busybox
+ *
+ *
+ * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "internal.h"
+#include <signal.h>
+
+extern int
+poweroff_main(int argc, char ** argv)
+{
+    /* don't assume init's pid == 1 */
+    exit( kill(findInitPid(), SIGUSR2));
+}
index ff2c6ad..1339a60 100644 (file)
@@ -27,5 +27,5 @@ extern int
 reboot_main(int argc, char ** argv)
 {
     /* don't assume init's pid == 1 */
-    exit( kill(findInitPid(), SIGUSR2));
+    exit( kill(findInitPid(), SIGINT));
 }
index 39b07b9..95df64c 100644 (file)
@@ -96,6 +96,7 @@ extern int mount_main(int argc, char** argv);
 extern int mt_main(int argc, char** argv);
 extern int mv_main(int argc, char** argv);
 extern int ping_main(int argc, char **argv);
+extern int poweroff_main(int argc, char **argv);
 extern int printf_main(int argc, char** argv);
 extern int ps_main(int argc, char** argv);
 extern int pwd_main(int argc, char** argv);
diff --git a/poweroff.c b/poweroff.c
new file mode 100644 (file)
index 0000000..405ca3f
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Mini poweroff implementation for busybox
+ *
+ *
+ * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "internal.h"
+#include <signal.h>
+
+extern int
+poweroff_main(int argc, char ** argv)
+{
+    /* don't assume init's pid == 1 */
+    exit( kill(findInitPid(), SIGUSR2));
+}
index ff2c6ad..1339a60 100644 (file)
--- a/reboot.c
+++ b/reboot.c
@@ -27,5 +27,5 @@ extern int
 reboot_main(int argc, char ** argv)
 {
     /* don't assume init's pid == 1 */
-    exit( kill(findInitPid(), SIGUSR2));
+    exit( kill(findInitPid(), SIGINT));
 }
index 1f3e312..24c721f 100644 (file)
@@ -62,6 +62,9 @@ static const char syslogd_usage[] =
     "Options:\n"
     "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
     "\t-n\tDo not fork into the background (for when run by init)\n"
+#ifdef BB_KLOGD
+    "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
+#endif
     "\t-O\tSpecify an alternate log file.  default=/var/log/messages\n";
 
 
@@ -251,6 +254,8 @@ static void doSyslogd(void)
     close(fd);
 }
 
+#ifdef BB_KLOGD
+
 static void klogd_signal(int sig)
 {
     ksyslog(7, NULL, 0);
@@ -259,7 +264,6 @@ static void klogd_signal(int sig)
     exit( TRUE);
 }
 
-
 static void doKlogd(void)
 {
     int priority=LOG_INFO;
@@ -325,11 +329,15 @@ static void doKlogd(void)
 
 }
 
+#endif
 
 extern int syslogd_main(int argc, char **argv)
 {
     int        pid, klogd_pid;
     int doFork = TRUE;
+#ifdef BB_KLOGD
+    int startKlogd = TRUE;
+#endif
     char *p;
     char **argv1=argv;
     
@@ -345,6 +353,11 @@ extern int syslogd_main(int argc, char **argv)
            case 'n':
                doFork = FALSE;
                break;
+#ifdef BB_KLOGD
+           case 'K':
+               startKlogd = FALSE;
+               break;
+#endif
            case 'O':
                if (--argc == 0) {
                    usage(syslogd_usage);
@@ -375,12 +388,16 @@ extern int syslogd_main(int argc, char **argv)
        doSyslogd();
     }
 
+#ifdef BB_KLOGD
     /* Start up the klogd process */
-    klogd_pid = fork();
-    if (klogd_pid == 0 ) {
-           strncpy(argv[0], "klogd", strlen(argv[0]));
-           doKlogd();
+    if (startKlogd == TRUE) {
+       klogd_pid = fork();
+       if (klogd_pid == 0 ) {
+               strncpy(argv[0], "klogd", strlen(argv[0]));
+               doKlogd();
+       }
     }
+#endif
 
     exit( TRUE);
 }
index 1f3e312..24c721f 100644 (file)
--- a/syslogd.c
+++ b/syslogd.c
@@ -62,6 +62,9 @@ static const char syslogd_usage[] =
     "Options:\n"
     "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
     "\t-n\tDo not fork into the background (for when run by init)\n"
+#ifdef BB_KLOGD
+    "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
+#endif
     "\t-O\tSpecify an alternate log file.  default=/var/log/messages\n";
 
 
@@ -251,6 +254,8 @@ static void doSyslogd(void)
     close(fd);
 }
 
+#ifdef BB_KLOGD
+
 static void klogd_signal(int sig)
 {
     ksyslog(7, NULL, 0);
@@ -259,7 +264,6 @@ static void klogd_signal(int sig)
     exit( TRUE);
 }
 
-
 static void doKlogd(void)
 {
     int priority=LOG_INFO;
@@ -325,11 +329,15 @@ static void doKlogd(void)
 
 }
 
+#endif
 
 extern int syslogd_main(int argc, char **argv)
 {
     int        pid, klogd_pid;
     int doFork = TRUE;
+#ifdef BB_KLOGD
+    int startKlogd = TRUE;
+#endif
     char *p;
     char **argv1=argv;
     
@@ -345,6 +353,11 @@ extern int syslogd_main(int argc, char **argv)
            case 'n':
                doFork = FALSE;
                break;
+#ifdef BB_KLOGD
+           case 'K':
+               startKlogd = FALSE;
+               break;
+#endif
            case 'O':
                if (--argc == 0) {
                    usage(syslogd_usage);
@@ -375,12 +388,16 @@ extern int syslogd_main(int argc, char **argv)
        doSyslogd();
     }
 
+#ifdef BB_KLOGD
     /* Start up the klogd process */
-    klogd_pid = fork();
-    if (klogd_pid == 0 ) {
-           strncpy(argv[0], "klogd", strlen(argv[0]));
-           doKlogd();
+    if (startKlogd == TRUE) {
+       klogd_pid = fork();
+       if (klogd_pid == 0 ) {
+               strncpy(argv[0], "klogd", strlen(argv[0]));
+               doKlogd();
+       }
     }
+#endif
 
     exit( TRUE);
 }
diff --git a/tee.c b/tee.c
index 45128b5..8d1ca6e 100644 (file)
--- a/tee.c
+++ b/tee.c
 #include <stdio.h>
 
 static const char tee_usage[] =
-"Usage: tee [OPTION]... [FILE]...\n"
-"Copy standard input to each FILE, and also to standard output.\n\n"
-"  -a,    append to the given FILEs, do not overwrite\n"
-"  -i,    ignore interrupt signals\n"
-"  -h,    this help message\n";
+    "tee [OPTION]... [FILE]...\n\n"
+    "Copy standard input to each FILE, and also to standard output.\n\n"
+    "Options:\n"
+    "\t-a\tappend to the given FILEs, do not overwrite\n"
+#if 0
+    "\t-i\tignore interrupt signals\n"
+#endif
+    ;
+
 
 /* FileList _______________________________________________________________ */
 
@@ -39,27 +43,6 @@ static int  FL_end;
 
 typedef void (FL_Function)(FILE *file, char c);
     
-/* initialize FileList */
-static void
-FL_init()
-{
-    FL_end = 0;
-    FileList[0] = stdout;
-}
-
-/* add a file to FileList */
-static int
-FL_add(const char *filename, char *opt_open)
-{
-    FILE    *file;
-
-    file = fopen(filename, opt_open);
-    if (!file) { return 0; };
-    if (FL_end < FL_MAX) {
-       FileList[++FL_end] = file;
-    }
-    return 1;
-}
 
 /* apply a function to everything in FileList */
 static void
@@ -71,8 +54,6 @@ FL_apply(FL_Function *f, char c)
     }
 }
 
-/* ________________________________________________________________________ */
-
 /* FL_Function for writing to files*/
 static void
 tee_fwrite(FILE *file, char c)
@@ -87,6 +68,8 @@ tee_fclose(FILE *file, char c)
     fclose(file);
 }
 
+/* ________________________________________________________________________ */
+
 /* BusyBoxed tee(1) */
 int
 tee_main(int argc, char **argv)
@@ -95,6 +78,7 @@ tee_main(int argc, char **argv)
     char    c;
     char    opt;
     char    opt_fopen[2] = "w";
+    FILE    *file;
 
     /* parse argv[] */
     for (i = 1; i < argc; i++) {
@@ -104,14 +88,12 @@ tee_main(int argc, char **argv)
                case 'a':
                    opt_fopen[0] = 'a';
                    break;
+#if 0
                case 'i':
-                   fprintf(stderr, "ingore interrupt not implemented\n");
-                   break;
-               case 'h':
-                   usage(tee_usage);
+                   fprintf(stderr, "ignore interrupt not implemented\n");
                    break;
+#endif
                default:
-                   fprintf(stderr, "tee: invalid option -- %c\n", opt);
                    usage(tee_usage);
            }
        } else {
@@ -120,9 +102,15 @@ tee_main(int argc, char **argv)
     }
 
     /* init FILE pointers */
-    FL_init();
+    FL_end = 0;
+    FileList[0] = stdout;
     for ( ; i < argc; i++) {
-       FL_add(argv[i], opt_fopen);
+       /* add a file to FileList */
+       file = fopen(argv[i], opt_fopen);
+       if (!file) { continue; }
+       if (FL_end < FL_MAX) {
+           FileList[++FL_end] = file;
+       }
     }
 
     /* read and redirect */
@@ -135,4 +123,4 @@ tee_main(int argc, char **argv)
     exit(0);
 }
 
-/* $Id: tee.c,v 1.3 1999/12/10 07:41:03 beppu Exp $ */
+/* $Id: tee.c,v 1.4 1999/12/10 08:25:07 andersen Exp $ */