blob: 6b4742255e2e9b39d7d0cf6172df1613898400e9 [file] [log] [blame]
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +00002/*
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02003 * Prints out the previous and the current runlevel.
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +00004 *
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02005 * Version: @(#)runlevel 1.20 16-Apr-1997 MvS
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +00006 *
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02007 * This file is part of the sysvinit suite,
8 * Copyright 1991-1997 Miquel van Smoorenburg.
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +00009 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000011 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +000012 * initially busyboxified by Bernhard Reutner-Fischer
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000013 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010014//config:config RUNLEVEL
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020015//config: bool "runlevel (518 bytes)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010016//config: default y
17//config: depends on FEATURE_UTMP
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: find the current and previous system runlevel.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010020//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//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 Vlasenko73d249e2011-10-28 14:07:44 +020023
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010024//applet:IF_RUNLEVEL(APPLET(runlevel, BB_DIR_SBIN, BB_SUID_DROP))
25
26//kbuild:lib-$(CONFIG_RUNLEVEL) += runlevel.o
27
Pere Orga5bc8c002011-04-11 03:29:49 +020028//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 Vlasenko9a7d38f2007-05-31 22:42:12 +000040#include "libbb.h"
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000041
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000042int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1e113602009-11-26 15:26:14 +010043int runlevel_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000044{
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020045 struct utmpx *ut;
Denis Vlasenkoc2905632006-09-23 16:01:09 +000046 char prev;
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000047
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020048 if (argv[1]) utmpxname(argv[1]);
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000049
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020050 setutxent();
51 while ((ut = getutxent()) != NULL) {
Denis Vlasenkoc2905632006-09-23 16:01:09 +000052 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 Vlasenko9a7d38f2007-05-31 22:42:12 +000056 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020057 endutxent();
Denis Vlasenkoc2905632006-09-23 16:01:09 +000058 return 0;
59 }
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000060 }
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000061
Denis Vlasenkoc2905632006-09-23 16:01:09 +000062 puts("unknown");
Denis Vlasenko2f6ae432007-07-19 22:50:47 +000063
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000064 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020065 endutxent();
Denis Vlasenkoc2905632006-09-23 16:01:09 +000066 return 1;
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000067}