This patch from Adam Heath <doogie@debian.org>, makes print_file
[oweals/busybox.git] / update.c
index f3b7fc0c89e496f62aac2c94ffa7d7df71815d69..a6550b05cab853c8ec7ccd8e136c661dc764b265 100644 (file)
--- a/update.c
+++ b/update.c
-#include "internal.h"
-#include <linux/unistd.h>
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini update implementation for busybox; much pasted from update-2.11
+ *
+ *
+ * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
+ * Copyright (c) 1996, 1997, 1999 Torsten Poulin.
+ * Copyright (c) 2000 by Karl M. Hegbloom <karlheg@debian.org>
+ *
+ * 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
+ *
+ */
 
-const char     update_usage[] = "update\n"
-"\n"
-"\tFlush buffered data to the disk devices every 30 seconds.\n";
+/*
+ * 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.
+ */
 
-#if defined(__GLIBC__)
-#include <sys/kdaemon.h>
+#include <sys/param.h>
+#include <sys/syslog.h>
+#include <unistd.h> /* for getopt() */
+#include <stdlib.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
+
+#include "busybox.h"
 
-extern int
-update_main(struct FileInfo * i, int argc, char * * argv)
+static unsigned int sync_duration = 30;
+static unsigned int flush_duration = 5;
+static int use_sync = 0;
+
+extern int update_main(int argc, char **argv)
 {
-       /*
-        * Update is actually two daemons, bdflush and update.
-        */
-       int     pid;
+       int pid;
+       int opt;
+
+       while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
+               switch (opt) {
+                       case 'S':
+                               use_sync = 1;
+                               break;
+                       case 's':
+                               sync_duration = atoi(optarg);
+                               break;
+                       case 'f':
+                               flush_duration = atoi(optarg);
+                               break;
+                       default:
+                               show_usage();
+               }
+       }
 
        pid = fork();
-       if ( pid < 0 )
-               return pid;
-       else if ( pid == 0 ) {
+       if (pid < 0)
+               return EXIT_FAILURE;
+       else if (pid == 0) {
+               /* Become a proper daemon */
+               setsid();
+               chdir("/");
+#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.
                 */
-               strcpy(argv[0], "bdflush (update)");
-               argv[1] = 0;
-               argv[2] = 0;
-               bdflush(1, 0);
-               _exit(0);
-       }
-       pid = fork();
-       if ( pid < 0 )
-               return pid;
-       else if ( pid == 0 ) {
-               argv[0] = "update";
-               for ( ; ; ) {
-                       sync();
-                       sleep(30);
+
+               /* 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 0;
+       return EXIT_SUCCESS;
 }
+
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/