More cleanups.
[oweals/busybox.git] / update.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini update implementation for busybox; much pasted from update-2.11
4  *
5  *
6  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
7  * Copyright (c) 1996, 1997, 1999 Torsten Poulin.
8  * Copyright (c) 2000 by Karl M. Hegbloom <karlheg@debian.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */
25
26 #include "internal.h"
27 #include <sys/param.h>
28 #include <sys/syslog.h>
29
30
31 #if defined(__GLIBC__)
32 #include <sys/kdaemon.h>
33 #else
34 static _syscall2(int, bdflush, int, func, int, data);
35 #endif                                                  /* __GLIBC__ */
36
37
38 static char update_usage[] =
39         "update [options]\n"
40 #ifndef BB_FEATURE_TRIVIAL_HELP
41         "\nPeriodically flushes filesystem buffers.\n\n"
42         "Options:\n"
43         "\t-S\tforce use of sync(2) instead of flushing\n"
44         "\t-s SECS\tcall sync this often (default 30)\n"
45         "\t-f SECS\tflush some buffers this often (default 5)\n"
46 #endif
47         ;
48
49 static unsigned int sync_duration = 30;
50 static unsigned int flush_duration = 5;
51 static int use_sync = 0;
52
53 extern int update_main(int argc, char **argv)
54 {
55         int pid;
56
57         argc--;
58         argv++;
59         while (argc>0 && **argv == '-') {
60                 while (*++(*argv)) {
61                         switch (**argv) {
62                         case 'S':
63                                 use_sync = 1;
64                                 break;
65                         case 's':
66                                 if (--argc < 1) usage(update_usage);
67                                 sync_duration = atoi(*(++argv));
68                                 break;
69                         case 'f':
70                                 if (--argc < 1) usage(update_usage);
71                                 flush_duration = atoi(*(++argv));
72                                 break;
73                         default:
74                                 usage(update_usage);
75                         }
76                 }
77                 argc--;
78                 argv++;
79         }
80
81         pid = fork();
82         if (pid < 0)
83                 exit(FALSE);
84         else if (pid == 0) {
85                 /* Become a proper daemon */
86                 setsid();
87                 chdir("/");
88                 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
89
90                 /*
91                  * This is no longer necessary since 1.3.5x, but it will harmlessly
92                  * exit if that is the case.
93                  */
94                 argv[0] = "bdflush (update)";
95                 argv[1] = NULL;
96                 argv[2] = NULL;
97                 for (;;) {
98                         if (use_sync) {
99                                 sleep(sync_duration);
100                                 sync();
101                         } else {
102                                 sleep(flush_duration);
103                                 if (bdflush(1, 0) < 0) {
104                                         openlog("update", LOG_CONS, LOG_DAEMON);
105                                         syslog(LOG_INFO,
106                                                    "This kernel does not need update(8). Exiting.");
107                                         closelog();
108                                         exit(TRUE);
109                                 }
110                         }
111                 }
112         }
113         return( TRUE);
114 }
115
116 /*
117 Local Variables:
118 c-file-style: "linux"
119 c-basic-offset: 4
120 tab-width: 4
121 End:
122 */