blob: 14f1d3c2081dcfaa3268b167a55f72fe821665f3 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Erik Andersen029011b2000-03-04 21:19:32 +00003 * Mini update implementation for busybox; much pasted from update-2.11
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 *
6 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Erik Andersen029011b2000-03-04 21:19:32 +00007 * Copyright (c) 1996, 1997, 1999 Torsten Poulin.
8 * Copyright (c) 2000 by Karl M. Hegbloom <karlheg@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +00009 *
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
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include "internal.h"
27#include <linux/unistd.h>
Erik Andersen029011b2000-03-04 21:19:32 +000028#include <sys/param.h>
29#include <sys/syslog.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000030
Eric Andersencc8ed391999-10-05 16:24:54 +000031#if defined(__GLIBC__)
32#include <sys/kdaemon.h>
33#else
34_syscall2(int, bdflush, int, func, int, data);
Erik Andersene49d5ec2000-02-08 19:58:47 +000035#endif /* __GLIBC__ */
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Erik Andersen029011b2000-03-04 21:19:32 +000037static char update_usage[] =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000038 "update [options]\n"
39#ifndef BB_FEATURE_TRIVIAL_HELP
40 "\nPeriodically flushes filesystem buffers.\n\n"
Erik Andersene5b6c7d2000-04-17 16:16:10 +000041 "Options:\n"
42 "\t-S\tforce use of sync(2) instead of flushing\n"
43 "\t-s SECS\tcall sync this often (default 30)\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000044 "\t-f SECS\tflush some buffers this often (default 5)\n"
45#endif
46 ;
Erik Andersen029011b2000-03-04 21:19:32 +000047
48static unsigned int sync_duration = 30;
49static unsigned int flush_duration = 5;
50static int use_sync = 0;
51
Erik Andersene49d5ec2000-02-08 19:58:47 +000052extern int update_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000053{
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 int pid;
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Erik Andersene5b6c7d2000-04-17 16:16:10 +000056 argc--;
57 argv++;
Erik Andersen6ed02a02000-04-28 22:10:34 +000058 while (argc>0 && **argv == '-') {
Erik Andersen029011b2000-03-04 21:19:32 +000059 while (*++(*argv)) {
60 switch (**argv) {
61 case 'S':
62 use_sync = 1;
63 break;
64 case 's':
65 if (--argc < 1) usage(update_usage);
66 sync_duration = atoi(*(++argv));
67 break;
68 case 'f':
69 if (--argc < 1) usage(update_usage);
70 flush_duration = atoi(*(++argv));
71 break;
Erik Andersene5b6c7d2000-04-17 16:16:10 +000072 default:
73 usage(update_usage);
Erik Andersen029011b2000-03-04 21:19:32 +000074 }
75 }
76 argc--;
77 argv++;
78 }
79
Eric Andersencc8ed391999-10-05 16:24:54 +000080 pid = fork();
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 if (pid < 0)
Erik Andersen029011b2000-03-04 21:19:32 +000082 exit(FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 else if (pid == 0) {
Erik Andersen029011b2000-03-04 21:19:32 +000084 /* Become a proper daemon */
85 setsid();
86 chdir("/");
87 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
88
Eric Andersencc8ed391999-10-05 16:24:54 +000089 /*
90 * This is no longer necessary since 1.3.5x, but it will harmlessly
91 * exit if that is the case.
92 */
Erik Andersen029011b2000-03-04 21:19:32 +000093 argv[0] = "bdflush (update)";
94 argv[1] = NULL;
95 argv[2] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 for (;;) {
Erik Andersen029011b2000-03-04 21:19:32 +000097 if (use_sync) {
98 sleep(sync_duration);
99 sync();
100 } else {
101 sleep(flush_duration);
102 if (bdflush(1, 0) < 0) {
103 openlog("update", LOG_CONS, LOG_DAEMON);
104 syslog(LOG_INFO,
105 "This kernel does not need update(8). Exiting.");
106 closelog();
107 exit(TRUE);
108 }
109 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000110 }
111 }
Erik Andersene5b6c7d2000-04-17 16:16:10 +0000112 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000113}
Erik Andersen029011b2000-03-04 21:19:32 +0000114
115/*
116Local Variables:
117c-file-style: "linux"
118c-basic-offset: 4
119tab-width: 4
120End:
121*/