Initial implementation of wget, from Chip Rosenthal <chip@laserlink.net>.
[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 /*
27  * Note: This program is only necessary if you are running a 2.0.x (or
28  * earlier) kernel. 2.2.x and higher flush filesystem buffers automatically.
29  */
30
31 #include "internal.h"
32 #include <sys/param.h>
33 #include <sys/syslog.h>
34 #include <unistd.h> /* for getopt() */
35
36
37 #if defined(__GLIBC__)
38 #include <sys/kdaemon.h>
39 #else
40 static _syscall2(int, bdflush, int, func, int, data);
41 #endif /* __GLIBC__ */
42
43 static unsigned int sync_duration = 30;
44 static unsigned int flush_duration = 5;
45 static int use_sync = 0;
46
47 extern int update_main(int argc, char **argv)
48 {
49         int pid;
50         int opt;
51
52         while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
53                 switch (opt) {
54                         case 'S':
55                                 use_sync = 1;
56                                 break;
57                         case 's':
58                                 sync_duration = atoi(optarg);
59                                 break;
60                         case 'f':
61                                 flush_duration = atoi(optarg);
62                                 break;
63                         default:
64                                 usage(update_usage);
65                 }
66         }
67
68         pid = fork();
69         if (pid < 0)
70                 exit(FALSE);
71         else if (pid == 0) {
72                 /* Become a proper daemon */
73                 setsid();
74                 chdir("/");
75                 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
76
77                 /*
78                  * This is no longer necessary since 1.3.5x, but it will harmlessly
79                  * exit if that is the case.
80                  */
81
82                 /* set the program name that will show up in a 'ps' listing */
83                 argv[0] = "bdflush (update)";
84                 argv[1] = NULL;
85                 argv[2] = NULL;
86                 for (;;) {
87                         if (use_sync) {
88                                 sleep(sync_duration);
89                                 sync();
90                         } else {
91                                 sleep(flush_duration);
92                                 if (bdflush(1, 0) < 0) {
93                                         openlog("update", LOG_CONS, LOG_DAEMON);
94                                         syslog(LOG_INFO,
95                                                    "This kernel does not need update(8). Exiting.");
96                                         closelog();
97                                         exit(TRUE);
98                                 }
99                         }
100                 }
101         }
102         return( TRUE);
103 }
104
105 /*
106 Local Variables:
107 c-file-style: "linux"
108 c-basic-offset: 4
109 tab-width: 4
110 End:
111 */