Extract usage information into a separate file.
[oweals/busybox.git] / miscutils / 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 static unsigned int sync_duration = 30;
38 static unsigned int flush_duration = 5;
39 static int use_sync = 0;
40
41 extern int update_main(int argc, char **argv)
42 {
43         int pid;
44
45         argc--;
46         argv++;
47         while (argc>0 && **argv == '-') {
48                 while (*++(*argv)) {
49                         switch (**argv) {
50                         case 'S':
51                                 use_sync = 1;
52                                 break;
53                         case 's':
54                                 if (--argc < 1) usage(update_usage);
55                                 sync_duration = atoi(*(++argv));
56                                 break;
57                         case 'f':
58                                 if (--argc < 1) usage(update_usage);
59                                 flush_duration = atoi(*(++argv));
60                                 break;
61                         default:
62                                 usage(update_usage);
63                         }
64                 }
65                 argc--;
66                 argv++;
67         }
68
69         pid = fork();
70         if (pid < 0)
71                 exit(FALSE);
72         else if (pid == 0) {
73                 /* Become a proper daemon */
74                 setsid();
75                 chdir("/");
76                 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
77
78                 /*
79                  * This is no longer necessary since 1.3.5x, but it will harmlessly
80                  * exit if that is the case.
81                  */
82                 argv[0] = "bdflush (update)";
83                 argv[1] = NULL;
84                 argv[2] = NULL;
85                 for (;;) {
86                         if (use_sync) {
87                                 sleep(sync_duration);
88                                 sync();
89                         } else {
90                                 sleep(flush_duration);
91                                 if (bdflush(1, 0) < 0) {
92                                         openlog("update", LOG_CONS, LOG_DAEMON);
93                                         syslog(LOG_INFO,
94                                                    "This kernel does not need update(8). Exiting.");
95                                         closelog();
96                                         exit(TRUE);
97                                 }
98                         }
99                 }
100         }
101         return( TRUE);
102 }
103
104 /*
105 Local Variables:
106 c-file-style: "linux"
107 c-basic-offset: 4
108 tab-width: 4
109 End:
110 */