Simplified version checking.
[oweals/busybox.git] / update.c
index bb77c5f1f060d1b00d8d9d0cc4b25ce5a152d8f3..27a04ddee35848e5e151d362bc3d797b441feb16 100644 (file)
--- a/update.c
+++ b/update.c
  *
  */
 
-#include "internal.h"
-#include <linux/unistd.h>
+/*
+ * Note: This program is only necessary if you are running a 2.0.x (or
+ * earlier) kernel. 2.2.x and higher flush filesystem buffers automatically.
+ */
+
 #include <sys/param.h>
 #include <sys/syslog.h>
+#include <unistd.h> /* for getopt() */
+#include <stdlib.h>
 
-#if defined(__GLIBC__)
-#include <sys/kdaemon.h>
+#if __GNU_LIBRARY__ > 5
+       #include <sys/kdaemon.h>
 #else
-_syscall2(int, bdflush, int, func, int, data);
-#endif                                                 /* __GLIBC__ */
+       extern int bdflush (int func, long int data);
+#endif
 
-static char update_usage[] =
-       "update [options]\n"
-       "  -S\tforce use of sync(2) instead of flushing\n"
-       "  -s SECS\tcall sync this often (default 30)\n"
-       "  -f SECS\tflush some buffers this often (default 5)\n";
+#include "busybox.h"
 
 static unsigned int sync_duration = 30;
 static unsigned int flush_duration = 5;
@@ -47,60 +48,59 @@ static int use_sync = 0;
 extern int update_main(int argc, char **argv)
 {
        int pid;
+       int opt;
 
-       while (**argv == '-') {
-               while (*++(*argv)) {
-                       switch (**argv) {
+       while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
+               switch (opt) {
                        case 'S':
                                use_sync = 1;
                                break;
                        case 's':
-                               if (--argc < 1) usage(update_usage);
-                               sync_duration = atoi(*(++argv));
+                               sync_duration = atoi(optarg);
                                break;
                        case 'f':
-                               if (--argc < 1) usage(update_usage);
-                               flush_duration = atoi(*(++argv));
+                               flush_duration = atoi(optarg);
                                break;
-                       }
+                       default:
+                               show_usage();
                }
-               argc--;
-               argv++;
        }
+       
+       if (daemon(0, 1) < 0)
+               perror_msg_and_die("daemon");
 
-       pid = fork();
-       if (pid < 0)
-               exit(FALSE);
-       else if (pid == 0) {
-               /* Become a proper daemon */
-               setsid();
-               chdir("/");
-               for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
+#ifdef OPEN_MAX
+       for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
+#else
+       /* glibc 2.1.92 requires using sysconf(_SC_OPEN_MAX) */
+       for (pid = 0; pid < sysconf(_SC_OPEN_MAX); pid++) close(pid);
+#endif
+
+       /* This is no longer necessary since 1.3.5x, but it will harmlessly
+        * exit if that is the case.
+        */
 
-               /*
-                * This is no longer necessary since 1.3.5x, but it will harmlessly
-                * exit if that is the case.
-                */
-               argv[0] = "bdflush (update)";
-               argv[1] = NULL;
-               argv[2] = NULL;
-               for (;;) {
-                       if (use_sync) {
-                               sleep(sync_duration);
-                               sync();
-                       } else {
-                               sleep(flush_duration);
-                               if (bdflush(1, 0) < 0) {
-                                       openlog("update", LOG_CONS, LOG_DAEMON);
-                                       syslog(LOG_INFO,
-                                                  "This kernel does not need update(8). Exiting.");
-                                       closelog();
-                                       exit(TRUE);
-                               }
+       /* set the program name that will show up in a 'ps' listing */
+       argv[0] = "bdflush (update)";
+       argv[1] = NULL;
+       argv[2] = NULL;
+       for (;;) {
+               if (use_sync) {
+                       sleep(sync_duration);
+                       sync();
+               } else {
+                       sleep(flush_duration);
+                       if (bdflush(1, 0) < 0) {
+                               openlog("update", LOG_CONS, LOG_DAEMON);
+                               syslog(LOG_INFO,
+                                               "This kernel does not need update(8). Exiting.");
+                               closelog();
+                               return EXIT_SUCCESS;
                        }
                }
        }
-       return TRUE;
+
+       return EXIT_SUCCESS;
 }
 
 /*