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 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | #include "internal.h" |
| 27 | #include <linux/unistd.h> |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 28 | #include <sys/param.h> |
| 29 | #include <sys/syslog.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | |
Eric Andersen | a15cd0b | 2000-06-19 18:14:20 +0000 | [diff] [blame^] | 31 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #if defined(__GLIBC__) |
| 33 | #include <sys/kdaemon.h> |
| 34 | #else |
Eric Andersen | a15cd0b | 2000-06-19 18:14:20 +0000 | [diff] [blame^] | 35 | static _syscall2(int, bdflush, int, func, int, data); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | #endif /* __GLIBC__ */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | |
Eric Andersen | a15cd0b | 2000-06-19 18:14:20 +0000 | [diff] [blame^] | 38 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 39 | static char update_usage[] = |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 40 | "update [options]\n" |
| 41 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 42 | "\nPeriodically flushes filesystem buffers.\n\n" |
Erik Andersen | e5b6c7d | 2000-04-17 16:16:10 +0000 | [diff] [blame] | 43 | "Options:\n" |
| 44 | "\t-S\tforce use of sync(2) instead of flushing\n" |
| 45 | "\t-s SECS\tcall sync this often (default 30)\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 46 | "\t-f SECS\tflush some buffers this often (default 5)\n" |
| 47 | #endif |
| 48 | ; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 49 | |
| 50 | static unsigned int sync_duration = 30; |
| 51 | static unsigned int flush_duration = 5; |
| 52 | static int use_sync = 0; |
| 53 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | extern int update_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | int pid; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | |
Erik Andersen | e5b6c7d | 2000-04-17 16:16:10 +0000 | [diff] [blame] | 58 | argc--; |
| 59 | argv++; |
Erik Andersen | 6ed02a0 | 2000-04-28 22:10:34 +0000 | [diff] [blame] | 60 | while (argc>0 && **argv == '-') { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 61 | while (*++(*argv)) { |
| 62 | switch (**argv) { |
| 63 | case 'S': |
| 64 | use_sync = 1; |
| 65 | break; |
| 66 | case 's': |
| 67 | if (--argc < 1) usage(update_usage); |
| 68 | sync_duration = atoi(*(++argv)); |
| 69 | break; |
| 70 | case 'f': |
| 71 | if (--argc < 1) usage(update_usage); |
| 72 | flush_duration = atoi(*(++argv)); |
| 73 | break; |
Erik Andersen | e5b6c7d | 2000-04-17 16:16:10 +0000 | [diff] [blame] | 74 | default: |
| 75 | usage(update_usage); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | argc--; |
| 79 | argv++; |
| 80 | } |
| 81 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 82 | pid = fork(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | if (pid < 0) |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 84 | exit(FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | else if (pid == 0) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 86 | /* Become a proper daemon */ |
| 87 | setsid(); |
| 88 | chdir("/"); |
| 89 | for (pid = 0; pid < OPEN_MAX; pid++) close(pid); |
| 90 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 91 | /* |
| 92 | * This is no longer necessary since 1.3.5x, but it will harmlessly |
| 93 | * exit if that is the case. |
| 94 | */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 95 | argv[0] = "bdflush (update)"; |
| 96 | argv[1] = NULL; |
| 97 | argv[2] = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | for (;;) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 99 | if (use_sync) { |
| 100 | sleep(sync_duration); |
| 101 | sync(); |
| 102 | } else { |
| 103 | sleep(flush_duration); |
| 104 | if (bdflush(1, 0) < 0) { |
| 105 | openlog("update", LOG_CONS, LOG_DAEMON); |
| 106 | syslog(LOG_INFO, |
| 107 | "This kernel does not need update(8). Exiting."); |
| 108 | closelog(); |
| 109 | exit(TRUE); |
| 110 | } |
| 111 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Eric Andersen | a15cd0b | 2000-06-19 18:14:20 +0000 | [diff] [blame^] | 114 | return( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | Local Variables: |
| 119 | c-file-style: "linux" |
| 120 | c-basic-offset: 4 |
| 121 | tab-width: 4 |
| 122 | End: |
| 123 | */ |