runlevel.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Prints out the previous and the current runlevel.
  4. *
  5. * Version: @(#)runlevel 1.20 16-Apr-1997 MvS
  6. *
  7. * This file is part of the sysvinit suite,
  8. * Copyright 1991-1997 Miquel van Smoorenburg.
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. *
  12. * initially busyboxified by Bernhard Reutner-Fischer
  13. */
  14. //config:config RUNLEVEL
  15. //config: bool "runlevel (559 bytes)"
  16. //config: default y
  17. //config: depends on FEATURE_UTMP
  18. //config: help
  19. //config: Find the current and previous system runlevel.
  20. //config:
  21. //config: This applet uses utmp but does not rely on busybox supporing
  22. //config: utmp on purpose. It is used by e.g. emdebian via /etc/init.d/rc.
  23. //applet:IF_RUNLEVEL(APPLET_NOEXEC(runlevel, runlevel, BB_DIR_SBIN, BB_SUID_DROP, runlevel))
  24. //kbuild:lib-$(CONFIG_RUNLEVEL) += runlevel.o
  25. //usage:#define runlevel_trivial_usage
  26. //usage: "[FILE]"
  27. //usage:#define runlevel_full_usage "\n\n"
  28. //usage: "Find the current and previous system runlevel\n"
  29. //usage: "\n"
  30. //usage: "If no utmp FILE exists or if no runlevel record can be found,\n"
  31. //usage: "print \"unknown\""
  32. //usage:
  33. //usage:#define runlevel_example_usage
  34. //usage: "$ runlevel /var/run/utmp\n"
  35. //usage: "N 2"
  36. #include "libbb.h"
  37. int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int runlevel_main(int argc UNUSED_PARAM, char **argv)
  39. {
  40. struct utmpx *ut;
  41. char prev;
  42. if (argv[1]) utmpxname(argv[1]);
  43. setutxent();
  44. while ((ut = getutxent()) != NULL) {
  45. if (ut->ut_type == RUN_LVL) {
  46. prev = ut->ut_pid / 256;
  47. if (prev == 0) prev = 'N';
  48. printf("%c %c\n", prev, ut->ut_pid % 256);
  49. if (ENABLE_FEATURE_CLEAN_UP)
  50. endutxent();
  51. return 0;
  52. }
  53. }
  54. puts("unknown");
  55. if (ENABLE_FEATURE_CLEAN_UP)
  56. endutxent();
  57. return 1;
  58. }