blob: bb77c5f1f060d1b00d8d9d0cc4b25ce5a152d8f3 [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[] =
38 "update [options]\n"
39 " -S\tforce use of sync(2) instead of flushing\n"
40 " -s SECS\tcall sync this often (default 30)\n"
41 " -f SECS\tflush some buffers this often (default 5)\n";
42
43static unsigned int sync_duration = 30;
44static unsigned int flush_duration = 5;
45static int use_sync = 0;
46
Erik Andersene49d5ec2000-02-08 19:58:47 +000047extern int update_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000048{
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 int pid;
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Erik Andersen029011b2000-03-04 21:19:32 +000051 while (**argv == '-') {
52 while (*++(*argv)) {
53 switch (**argv) {
54 case 'S':
55 use_sync = 1;
56 break;
57 case 's':
58 if (--argc < 1) usage(update_usage);
59 sync_duration = atoi(*(++argv));
60 break;
61 case 'f':
62 if (--argc < 1) usage(update_usage);
63 flush_duration = atoi(*(++argv));
64 break;
65 }
66 }
67 argc--;
68 argv++;
69 }
70
Eric Andersencc8ed391999-10-05 16:24:54 +000071 pid = fork();
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 if (pid < 0)
Erik Andersen029011b2000-03-04 21:19:32 +000073 exit(FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 else if (pid == 0) {
Erik Andersen029011b2000-03-04 21:19:32 +000075 /* Become a proper daemon */
76 setsid();
77 chdir("/");
78 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
79
Eric Andersencc8ed391999-10-05 16:24:54 +000080 /*
81 * This is no longer necessary since 1.3.5x, but it will harmlessly
82 * exit if that is the case.
83 */
Erik Andersen029011b2000-03-04 21:19:32 +000084 argv[0] = "bdflush (update)";
85 argv[1] = NULL;
86 argv[2] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 for (;;) {
Erik Andersen029011b2000-03-04 21:19:32 +000088 if (use_sync) {
89 sleep(sync_duration);
90 sync();
91 } else {
92 sleep(flush_duration);
93 if (bdflush(1, 0) < 0) {
94 openlog("update", LOG_CONS, LOG_DAEMON);
95 syslog(LOG_INFO,
96 "This kernel does not need update(8). Exiting.");
97 closelog();
98 exit(TRUE);
99 }
100 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000101 }
102 }
Erik Andersen029011b2000-03-04 21:19:32 +0000103 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000104}
Erik Andersen029011b2000-03-04 21:19:32 +0000105
106/*
107Local Variables:
108c-file-style: "linux"
109c-basic-offset: 4
110tab-width: 4
111End:
112*/