blob: 080cb99c31dff71ea57ee13717b0f9371363801e [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
Mark Whitley1ac435c2000-07-20 23:06:27 +000026/*
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
Eric Andersen3570a342000-09-25 21:45:58 +000031#include "busybox.h"
Erik Andersen029011b2000-03-04 21:19:32 +000032#include <sys/param.h>
33#include <sys/syslog.h>
Mark Whitley1ac435c2000-07-20 23:06:27 +000034#include <unistd.h> /* for getopt() */
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Eric Andersena15cd0b2000-06-19 18:14:20 +000036
Eric Andersencc8ed391999-10-05 16:24:54 +000037#if defined(__GLIBC__)
38#include <sys/kdaemon.h>
39#else
Eric Andersena15cd0b2000-06-19 18:14:20 +000040static _syscall2(int, bdflush, int, func, int, data);
Mark Whitley1ac435c2000-07-20 23:06:27 +000041#endif /* __GLIBC__ */
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Erik Andersen029011b2000-03-04 21:19:32 +000043static 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;
Mark Whitley1ac435c2000-07-20 23:06:27 +000050 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Mark Whitley1ac435c2000-07-20 23:06:27 +000052 while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
53 switch (opt) {
Erik Andersen029011b2000-03-04 21:19:32 +000054 case 'S':
55 use_sync = 1;
56 break;
57 case 's':
Mark Whitley1ac435c2000-07-20 23:06:27 +000058 sync_duration = atoi(optarg);
Erik Andersen029011b2000-03-04 21:19:32 +000059 break;
60 case 'f':
Mark Whitley1ac435c2000-07-20 23:06:27 +000061 flush_duration = atoi(optarg);
Erik Andersen029011b2000-03-04 21:19:32 +000062 break;
Erik Andersene5b6c7d2000-04-17 16:16:10 +000063 default:
64 usage(update_usage);
Erik Andersen029011b2000-03-04 21:19:32 +000065 }
Erik Andersen029011b2000-03-04 21:19:32 +000066 }
67
Eric Andersencc8ed391999-10-05 16:24:54 +000068 pid = fork();
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 if (pid < 0)
Matt Kraai3e856ce2000-12-01 02:55:13 +000070 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 else if (pid == 0) {
Erik Andersen029011b2000-03-04 21:19:32 +000072 /* Become a proper daemon */
73 setsid();
74 chdir("/");
Pavel Roskin43f3e612000-09-28 20:52:55 +000075#ifdef OPEN_MAX
Erik Andersen029011b2000-03-04 21:19:32 +000076 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
Pavel Roskin43f3e612000-09-28 20:52:55 +000077#else
78 /* glibc 2.1.92 requires using sysconf(_SC_OPEN_MAX) */
79 for (pid = 0; pid < sysconf(_SC_OPEN_MAX); pid++) close(pid);
80#endif
Erik Andersen029011b2000-03-04 21:19:32 +000081
Eric Andersencc8ed391999-10-05 16:24:54 +000082 /*
83 * This is no longer necessary since 1.3.5x, but it will harmlessly
84 * exit if that is the case.
85 */
Mark Whitley1ac435c2000-07-20 23:06:27 +000086
87 /* set the program name that will show up in a 'ps' listing */
Erik Andersen029011b2000-03-04 21:19:32 +000088 argv[0] = "bdflush (update)";
89 argv[1] = NULL;
90 argv[2] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 for (;;) {
Erik Andersen029011b2000-03-04 21:19:32 +000092 if (use_sync) {
93 sleep(sync_duration);
94 sync();
95 } else {
96 sleep(flush_duration);
97 if (bdflush(1, 0) < 0) {
98 openlog("update", LOG_CONS, LOG_DAEMON);
99 syslog(LOG_INFO,
100 "This kernel does not need update(8). Exiting.");
101 closelog();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000102 return EXIT_SUCCESS;
Erik Andersen029011b2000-03-04 21:19:32 +0000103 }
104 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000105 }
106 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000107 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000108}
Erik Andersen029011b2000-03-04 21:19:32 +0000109
110/*
111Local Variables:
112c-file-style: "linux"
113c-basic-offset: 4
114tab-width: 4
115End:
116*/