blob: 603740e3815e85fc4ab328857e70e936e8687d52 [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
Erik Andersen029011b2000-03-04 21:19:32 +000031#include <sys/param.h>
32#include <sys/syslog.h>
Mark Whitley1ac435c2000-07-20 23:06:27 +000033#include <unistd.h> /* for getopt() */
Eric Andersened3ef502001-01-27 08:24:39 +000034#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Eric Andersenb6b519b2001-04-09 23:52:18 +000036#if __GNU_LIBRARY__ > 5
37 #include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000038#else
Eric Andersenb6b519b2001-04-09 23:52:18 +000039 extern int bdflush (int func, long int data);
40#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000041
Eric Andersencbe31da2001-02-20 06:14:08 +000042#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Erik Andersen029011b2000-03-04 21:19:32 +000044static unsigned int sync_duration = 30;
45static unsigned int flush_duration = 5;
46static int use_sync = 0;
47
Erik Andersene49d5ec2000-02-08 19:58:47 +000048extern int update_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000049{
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 int pid;
Mark Whitley1ac435c2000-07-20 23:06:27 +000051 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Mark Whitley1ac435c2000-07-20 23:06:27 +000053 while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
54 switch (opt) {
Erik Andersen029011b2000-03-04 21:19:32 +000055 case 'S':
56 use_sync = 1;
57 break;
58 case 's':
Mark Whitley1ac435c2000-07-20 23:06:27 +000059 sync_duration = atoi(optarg);
Erik Andersen029011b2000-03-04 21:19:32 +000060 break;
61 case 'f':
Mark Whitley1ac435c2000-07-20 23:06:27 +000062 flush_duration = atoi(optarg);
Erik Andersen029011b2000-03-04 21:19:32 +000063 break;
Erik Andersene5b6c7d2000-04-17 16:16:10 +000064 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000065 show_usage();
Erik Andersen029011b2000-03-04 21:19:32 +000066 }
Erik Andersen029011b2000-03-04 21:19:32 +000067 }
Eric Andersen20aab262001-07-19 22:28:02 +000068
69 if (daemon(0, 1) < 0)
70 perror_msg_and_die("daemon");
Erik Andersen029011b2000-03-04 21:19:32 +000071
Eric Andersen20aab262001-07-19 22:28:02 +000072 /* Become a proper daemon */
73 setsid();
74 chdir("/");
Pavel Roskin43f3e612000-09-28 20:52:55 +000075#ifdef OPEN_MAX
Eric Andersen20aab262001-07-19 22:28:02 +000076 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
Pavel Roskin43f3e612000-09-28 20:52:55 +000077#else
Eric Andersen20aab262001-07-19 22:28:02 +000078 /* glibc 2.1.92 requires using sysconf(_SC_OPEN_MAX) */
79 for (pid = 0; pid < sysconf(_SC_OPEN_MAX); pid++) close(pid);
Pavel Roskin43f3e612000-09-28 20:52:55 +000080#endif
Erik Andersen029011b2000-03-04 21:19:32 +000081
Eric Andersen20aab262001-07-19 22:28:02 +000082 /* This is no longer necessary since 1.3.5x, but it will harmlessly
83 * exit if that is the case.
84 */
Mark Whitley1ac435c2000-07-20 23:06:27 +000085
Eric Andersen20aab262001-07-19 22:28:02 +000086 /* set the program name that will show up in a 'ps' listing */
87 argv[0] = "bdflush (update)";
88 argv[1] = NULL;
89 argv[2] = NULL;
90 for (;;) {
91 if (use_sync) {
92 sleep(sync_duration);
93 sync();
94 } else {
95 sleep(flush_duration);
96 if (bdflush(1, 0) < 0) {
97 openlog("update", LOG_CONS, LOG_DAEMON);
98 syslog(LOG_INFO,
99 "This kernel does not need update(8). Exiting.");
100 closelog();
101 return EXIT_SUCCESS;
Erik Andersen029011b2000-03-04 21:19:32 +0000102 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000103 }
104 }
Eric Andersen20aab262001-07-19 22:28:02 +0000105
Matt Kraai3e856ce2000-12-01 02:55:13 +0000106 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000107}
Erik Andersen029011b2000-03-04 21:19:32 +0000108
109/*
110Local Variables:
111c-file-style: "linux"
112c-basic-offset: 4
113tab-width: 4
114End:
115*/