blob: 8558db8625e0f6308deec54a3da51b06c5350363 [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 Vlasenko73d249e2011-10-28 14:07:44 +020014
Pere Orga5bc8c002011-04-11 03:29:49 +020015//usage:#define runlevel_trivial_usage
16//usage: "[FILE]"
17//usage:#define runlevel_full_usage "\n\n"
18//usage: "Find the current and previous system runlevel\n"
19//usage: "\n"
20//usage: "If no utmp FILE exists or if no runlevel record can be found,\n"
21//usage: "print \"unknown\""
22//usage:
23//usage:#define runlevel_example_usage
24//usage: "$ runlevel /var/run/utmp\n"
25//usage: "N 2"
26
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000027#include "libbb.h"
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000028
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000029int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1e113602009-11-26 15:26:14 +010030int runlevel_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000031{
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020032 struct utmpx *ut;
Denis Vlasenkoc2905632006-09-23 16:01:09 +000033 char prev;
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000034
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020035 if (argv[1]) utmpxname(argv[1]);
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000036
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020037 setutxent();
38 while ((ut = getutxent()) != NULL) {
Denis Vlasenkoc2905632006-09-23 16:01:09 +000039 if (ut->ut_type == RUN_LVL) {
40 prev = ut->ut_pid / 256;
41 if (prev == 0) prev = 'N';
42 printf("%c %c\n", prev, ut->ut_pid % 256);
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000043 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020044 endutxent();
Denis Vlasenkoc2905632006-09-23 16:01:09 +000045 return 0;
46 }
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000047 }
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000048
Denis Vlasenkoc2905632006-09-23 16:01:09 +000049 puts("unknown");
Denis Vlasenko2f6ae432007-07-19 22:50:47 +000050
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000051 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020052 endutxent();
Denis Vlasenkoc2905632006-09-23 16:01:09 +000053 return 1;
Bernhard Reutner-Fischer62d7acc2005-10-28 20:37:03 +000054}