It turns out that DODMALLOC was broken when I reorganized busybox.h
[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 <sys/param.h>
32 #include <sys/syslog.h>
33 #include <unistd.h> /* for getopt() */
34 #include <stdlib.h>
35
36
37 #if defined(__GLIBC__)
38 #include <sys/kdaemon.h>
39 #else
40 #include <sys/syscall.h>
41 #include <linux/unistd.h>
42 static _syscall2(int, bdflush, int, func, int, data);
43 #endif /* __GLIBC__ */
44 #include "busybox.h"
45
46 static unsigned int sync_duration = 30;
47 static unsigned int flush_duration = 5;
48 static int use_sync = 0;
49
50 extern int update_main(int argc, char **argv)
51 {
52         int pid;
53         int opt;
54
55         while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
56                 switch (opt) {
57                         case 'S':
58                                 use_sync = 1;
59                                 break;
60                         case 's':
61                                 sync_duration = atoi(optarg);
62                                 break;
63                         case 'f':
64                                 flush_duration = atoi(optarg);
65                                 break;
66                         default:
67                                 show_usage();
68                 }
69         }
70
71         pid = fork();
72         if (pid < 0)
73                 return EXIT_FAILURE;
74         else if (pid == 0) {
75                 /* Become a proper daemon */
76                 setsid();
77                 chdir("/");
78 #ifdef OPEN_MAX
79                 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
80 #else
81                 /* glibc 2.1.92 requires using sysconf(_SC_OPEN_MAX) */
82                 for (pid = 0; pid < sysconf(_SC_OPEN_MAX); pid++) close(pid);
83 #endif
84
85                 /*
86                  * This is no longer necessary since 1.3.5x, but it will harmlessly
87                  * exit if that is the case.
88                  */
89
90                 /* set the program name that will show up in a 'ps' listing */
91                 argv[0] = "bdflush (update)";
92                 argv[1] = NULL;
93                 argv[2] = NULL;
94                 for (;;) {
95                         if (use_sync) {
96                                 sleep(sync_duration);
97                                 sync();
98                         } else {
99                                 sleep(flush_duration);
100                                 if (bdflush(1, 0) < 0) {
101                                         openlog("update", LOG_CONS, LOG_DAEMON);
102                                         syslog(LOG_INFO,
103                                                    "This kernel does not need update(8). Exiting.");
104                                         closelog();
105                                         return EXIT_SUCCESS;
106                                 }
107                         }
108                 }
109         }
110         return EXIT_SUCCESS;
111 }
112
113 /*
114 Local Variables:
115 c-file-style: "linux"
116 c-basic-offset: 4
117 tab-width: 4
118 End:
119 */