blob: 309ce0cbddc049fc21f3f3ad83cf1be7264f412d [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 Andersened3ef502001-01-27 08:24:39 +000035#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Eric Andersena15cd0b2000-06-19 18:14:20 +000037
Eric Andersencc8ed391999-10-05 16:24:54 +000038#if defined(__GLIBC__)
39#include <sys/kdaemon.h>
40#else
Eric Andersened3ef502001-01-27 08:24:39 +000041#include <sys/syscall.h>
42#include <linux/unistd.h>
Eric Andersena15cd0b2000-06-19 18:14:20 +000043static _syscall2(int, bdflush, int, func, int, data);
Mark Whitley1ac435c2000-07-20 23:06:27 +000044#endif /* __GLIBC__ */
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Erik Andersen029011b2000-03-04 21:19:32 +000046static unsigned int sync_duration = 30;
47static unsigned int flush_duration = 5;
48static int use_sync = 0;
49
Erik Andersene49d5ec2000-02-08 19:58:47 +000050extern int update_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000051{
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 int pid;
Mark Whitley1ac435c2000-07-20 23:06:27 +000053 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +000054
Mark Whitley1ac435c2000-07-20 23:06:27 +000055 while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
56 switch (opt) {
Erik Andersen029011b2000-03-04 21:19:32 +000057 case 'S':
58 use_sync = 1;
59 break;
60 case 's':
Mark Whitley1ac435c2000-07-20 23:06:27 +000061 sync_duration = atoi(optarg);
Erik Andersen029011b2000-03-04 21:19:32 +000062 break;
63 case 'f':
Mark Whitley1ac435c2000-07-20 23:06:27 +000064 flush_duration = atoi(optarg);
Erik Andersen029011b2000-03-04 21:19:32 +000065 break;
Erik Andersene5b6c7d2000-04-17 16:16:10 +000066 default:
67 usage(update_usage);
Erik Andersen029011b2000-03-04 21:19:32 +000068 }
Erik Andersen029011b2000-03-04 21:19:32 +000069 }
70
Eric Andersencc8ed391999-10-05 16:24:54 +000071 pid = fork();
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 if (pid < 0)
Matt Kraai3e856ce2000-12-01 02:55:13 +000073 return EXIT_FAILURE;
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("/");
Pavel Roskin43f3e612000-09-28 20:52:55 +000078#ifdef OPEN_MAX
Erik Andersen029011b2000-03-04 21:19:32 +000079 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
Pavel Roskin43f3e612000-09-28 20:52:55 +000080#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
Erik Andersen029011b2000-03-04 21:19:32 +000084
Eric Andersencc8ed391999-10-05 16:24:54 +000085 /*
86 * This is no longer necessary since 1.3.5x, but it will harmlessly
87 * exit if that is the case.
88 */
Mark Whitley1ac435c2000-07-20 23:06:27 +000089
90 /* set the program name that will show up in a 'ps' listing */
Erik Andersen029011b2000-03-04 21:19:32 +000091 argv[0] = "bdflush (update)";
92 argv[1] = NULL;
93 argv[2] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 for (;;) {
Erik Andersen029011b2000-03-04 21:19:32 +000095 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();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000105 return EXIT_SUCCESS;
Erik Andersen029011b2000-03-04 21:19:32 +0000106 }
107 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000108 }
109 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000110 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000111}
Erik Andersen029011b2000-03-04 21:19:32 +0000112
113/*
114Local Variables:
115c-file-style: "linux"
116c-basic-offset: 4
117tab-width: 4
118End:
119*/