Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3 | * Prints out the previous and the current runlevel. |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5 | * Version: @(#)runlevel 1.20 16-Apr-1997 MvS |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 7 | * This file is part of the sysvinit suite, |
| 8 | * Copyright 1991-1997 Miquel van Smoorenburg. |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 11 | * |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 12 | * initially busyboxified by Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 13 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 14 | //config:config RUNLEVEL |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 15 | //config: bool "runlevel (518 bytes)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 16 | //config: default y |
| 17 | //config: depends on FEATURE_UTMP |
| 18 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: find the current and previous system runlevel. |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 20 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | 73d249e | 2011-10-28 14:07:44 +0200 | [diff] [blame] | 23 | |
Denys Vlasenko | 83d7785 | 2017-08-04 17:59:46 +0200 | [diff] [blame^] | 24 | //applet:IF_RUNLEVEL(APPLET_NOEXEC(runlevel, runlevel, BB_DIR_SBIN, BB_SUID_DROP, runlevel)) |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 25 | |
| 26 | //kbuild:lib-$(CONFIG_RUNLEVEL) += runlevel.o |
| 27 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage:#define runlevel_trivial_usage |
| 29 | //usage: "[FILE]" |
| 30 | //usage:#define runlevel_full_usage "\n\n" |
| 31 | //usage: "Find the current and previous system runlevel\n" |
| 32 | //usage: "\n" |
| 33 | //usage: "If no utmp FILE exists or if no runlevel record can be found,\n" |
| 34 | //usage: "print \"unknown\"" |
| 35 | //usage: |
| 36 | //usage:#define runlevel_example_usage |
| 37 | //usage: "$ runlevel /var/run/utmp\n" |
| 38 | //usage: "N 2" |
| 39 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 40 | #include "libbb.h" |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 42 | int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1e11360 | 2009-11-26 15:26:14 +0100 | [diff] [blame] | 43 | int runlevel_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 44 | { |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 45 | struct utmpx *ut; |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 46 | char prev; |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 47 | |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 48 | if (argv[1]) utmpxname(argv[1]); |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 49 | |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 50 | setutxent(); |
| 51 | while ((ut = getutxent()) != NULL) { |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 52 | if (ut->ut_type == RUN_LVL) { |
| 53 | prev = ut->ut_pid / 256; |
| 54 | if (prev == 0) prev = 'N'; |
| 55 | printf("%c %c\n", prev, ut->ut_pid % 256); |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 56 | if (ENABLE_FEATURE_CLEAN_UP) |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 57 | endutxent(); |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | } |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 60 | } |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 62 | puts("unknown"); |
Denis Vlasenko | 2f6ae43 | 2007-07-19 22:50:47 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 64 | if (ENABLE_FEATURE_CLEAN_UP) |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 65 | endutxent(); |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 66 | return 1; |
Bernhard Reutner-Fischer | 62d7acc | 2005-10-28 20:37:03 +0000 | [diff] [blame] | 67 | } |