update.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini update implementation for busybox; much pasted from update-2.11
  4. *
  5. *
  6. * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  7. * Copyright (c) 1996, 1997, 1999 Torsten Poulin.
  8. * Copyright (c) 2000 by Karl M. Hegbloom <karlheg@debian.org>
  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. /*
  26. * Note: This program is only necessary if you are running a 2.0.x (or
  27. * earlier) kernel. 2.2.x and higher flush filesystem buffers automatically.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/syslog.h>
  31. #include <unistd.h> /* for getopt() */
  32. #include <stdlib.h>
  33. #if __GNU_LIBRARY__ > 5
  34. #include <sys/kdaemon.h>
  35. #else
  36. extern int bdflush (int func, long int data);
  37. #endif
  38. #include "busybox.h"
  39. static unsigned int sync_duration = 30;
  40. static unsigned int flush_duration = 5;
  41. static int use_sync = 0;
  42. extern int update_main(int argc, char **argv)
  43. {
  44. int pid;
  45. int opt;
  46. while ((opt = getopt(argc, argv, "Ss:f:")) > 0) {
  47. switch (opt) {
  48. case 'S':
  49. use_sync = 1;
  50. break;
  51. case 's':
  52. sync_duration = atoi(optarg);
  53. break;
  54. case 'f':
  55. flush_duration = atoi(optarg);
  56. break;
  57. default:
  58. show_usage();
  59. }
  60. }
  61. if (daemon(0, 1) < 0)
  62. perror_msg_and_die("daemon");
  63. #ifdef OPEN_MAX
  64. for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
  65. #else
  66. /* glibc 2.1.92 requires using sysconf(_SC_OPEN_MAX) */
  67. for (pid = 0; pid < sysconf(_SC_OPEN_MAX); pid++) close(pid);
  68. #endif
  69. /* This is no longer necessary since 1.3.5x, but it will harmlessly
  70. * exit if that is the case.
  71. */
  72. /* set the program name that will show up in a 'ps' listing */
  73. argv[0] = "bdflush (update)";
  74. argv[1] = NULL;
  75. argv[2] = NULL;
  76. for (;;) {
  77. if (use_sync) {
  78. sleep(sync_duration);
  79. sync();
  80. } else {
  81. sleep(flush_duration);
  82. if (bdflush(1, 0) < 0) {
  83. openlog("update", LOG_CONS, LOG_DAEMON);
  84. syslog(LOG_INFO,
  85. "This kernel does not need update(8). Exiting.");
  86. closelog();
  87. return EXIT_SUCCESS;
  88. }
  89. }
  90. }
  91. return EXIT_SUCCESS;
  92. }
  93. /*
  94. Local Variables:
  95. c-file-style: "linux"
  96. c-basic-offset: 4
  97. tab-width: 4
  98. End:
  99. */