Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 3 | * Mini update implementation for busybox; much pasted from update-2.11 |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * |
| 6 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 7 | * Copyright (c) 1996, 1997, 1999 Torsten Poulin. |
| 8 | * Copyright (c) 2000 by Karl M. Hegbloom <karlheg@debian.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 9 | * |
| 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 Whitley | 1ac435c | 2000-07-20 23:06:27 +0000 | [diff] [blame] | 26 | /* |
| 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 Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 31 | #include <sys/param.h> |
| 32 | #include <sys/syslog.h> |
Mark Whitley | 1ac435c | 2000-07-20 23:06:27 +0000 | [diff] [blame] | 33 | #include <unistd.h> /* for getopt() */ |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | #if defined(__GLIBC__) |
| 37 | #include <sys/kdaemon.h> |
| 38 | #else |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 39 | extern int bdflush (int func, long int data); |
| 40 | #endif /* __GLIBC__ */ |
| 41 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 42 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 44 | static unsigned int sync_duration = 30; |
| 45 | static unsigned int flush_duration = 5; |
| 46 | static int use_sync = 0; |
| 47 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | extern int update_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 49 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | int pid; |
Mark Whitley | 1ac435c | 2000-07-20 23:06:27 +0000 | [diff] [blame] | 51 | int opt; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | |
Mark Whitley | 1ac435c | 2000-07-20 23:06:27 +0000 | [diff] [blame] | 53 | while ((opt = getopt(argc, argv, "Ss:f:")) > 0) { |
| 54 | switch (opt) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 55 | case 'S': |
| 56 | use_sync = 1; |
| 57 | break; |
| 58 | case 's': |
Mark Whitley | 1ac435c | 2000-07-20 23:06:27 +0000 | [diff] [blame] | 59 | sync_duration = atoi(optarg); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 60 | break; |
| 61 | case 'f': |
Mark Whitley | 1ac435c | 2000-07-20 23:06:27 +0000 | [diff] [blame] | 62 | flush_duration = atoi(optarg); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 63 | break; |
Erik Andersen | e5b6c7d | 2000-04-17 16:16:10 +0000 | [diff] [blame] | 64 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 65 | show_usage(); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 66 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 69 | pid = fork(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | if (pid < 0) |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 71 | return EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | else if (pid == 0) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 73 | /* Become a proper daemon */ |
| 74 | setsid(); |
| 75 | chdir("/"); |
Pavel Roskin | 43f3e61 | 2000-09-28 20:52:55 +0000 | [diff] [blame] | 76 | #ifdef OPEN_MAX |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 77 | for (pid = 0; pid < OPEN_MAX; pid++) close(pid); |
Pavel Roskin | 43f3e61 | 2000-09-28 20:52:55 +0000 | [diff] [blame] | 78 | #else |
| 79 | /* glibc 2.1.92 requires using sysconf(_SC_OPEN_MAX) */ |
| 80 | for (pid = 0; pid < sysconf(_SC_OPEN_MAX); pid++) close(pid); |
| 81 | #endif |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 82 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 83 | /* |
| 84 | * This is no longer necessary since 1.3.5x, but it will harmlessly |
| 85 | * exit if that is the case. |
| 86 | */ |
Mark Whitley | 1ac435c | 2000-07-20 23:06:27 +0000 | [diff] [blame] | 87 | |
| 88 | /* set the program name that will show up in a 'ps' listing */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 89 | argv[0] = "bdflush (update)"; |
| 90 | argv[1] = NULL; |
| 91 | argv[2] = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | for (;;) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 93 | if (use_sync) { |
| 94 | sleep(sync_duration); |
| 95 | sync(); |
| 96 | } else { |
| 97 | sleep(flush_duration); |
| 98 | if (bdflush(1, 0) < 0) { |
| 99 | openlog("update", LOG_CONS, LOG_DAEMON); |
| 100 | syslog(LOG_INFO, |
| 101 | "This kernel does not need update(8). Exiting."); |
| 102 | closelog(); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 103 | return EXIT_SUCCESS; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 108 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | Local Variables: |
| 113 | c-file-style: "linux" |
| 114 | c-basic-offset: 4 |
| 115 | tab-width: 4 |
| 116 | End: |
| 117 | */ |