blob: 6d344915527db346e5bbe3c15276c4ed380534b9 [file] [log] [blame]
Scott James Remnant12473562006-08-31 04:21:05 +01001/* upstart
2 *
Scott James Remnant75123022009-05-22 13:27:56 +02003 * Copyright © 2009 Canonical Ltd.
Scott James Remnant7d5b2ea2009-05-22 15:20:12 +02004 * Author: Scott James Remnant <scott@netsplit.com>.
Scott James Remnant12473562006-08-31 04:21:05 +01005 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +01006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
Scott James Remnant75123022009-05-22 13:27:56 +02008 * published by the Free Software Foundation.
Scott James Remnant12473562006-08-31 04:21:05 +01009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +010015 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Scott James Remnant12473562006-08-31 04:21:05 +010018 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif /* HAVE_CONFIG_H */
23
24
Scott James Remnant3ef8fac2009-07-16 17:39:04 +010025#include <utmpx.h>
Scott James Remnantff87eaf2009-07-08 18:38:25 +010026#include <errno.h>
Scott James Remnant12473562006-08-31 04:21:05 +010027#include <unistd.h>
28
29#include <nih/macros.h>
30#include <nih/alloc.h>
31#include <nih/main.h>
32#include <nih/option.h>
33#include <nih/logging.h>
34#include <nih/error.h>
35
Scott James Remnantff87eaf2009-07-08 18:38:25 +010036#include "utmp.h"
Scott James Remnant12473562006-08-31 04:21:05 +010037
38
39/**
40 * options:
41 *
42 * Command-line options accepted.
43 **/
44static NihOption options[] = {
Scott James Remnant12473562006-08-31 04:21:05 +010045 NIH_OPTION_LAST
46};
47
48
49int
50main (int argc,
51 char *argv[])
52{
Scott James Remnantff87eaf2009-07-08 18:38:25 +010053 char **args;
54 int runlevel;
55 int prevlevel;
Scott James Remnant12473562006-08-31 04:21:05 +010056
57 nih_main_init (argv[0]);
Scott James Remnant1ee0f482006-10-11 17:40:41 +010058
59 nih_option_set_usage (_("[UTMP]"));
Scott James Remnantf7819622006-10-13 12:54:43 +010060 nih_option_set_synopsis (_("Output previous and current runlevel."));
Scott James Remnant462734c2006-10-13 13:36:00 +010061 nih_option_set_help (
62 _("The system /var/run/utmp file is used unless the alternate "
Scott James Remnantff87eaf2009-07-08 18:38:25 +010063 "file UTMP is given.\n"));
Scott James Remnant12473562006-08-31 04:21:05 +010064
65 args = nih_option_parser (NULL, argc, argv, options, FALSE);
66 if (! args)
67 exit (1);
68
Scott James Remnantff87eaf2009-07-08 18:38:25 +010069 runlevel = NIH_SHOULD (utmp_get_runlevel (args[0], &prevlevel));
70 if (runlevel < 0) {
71 NihError *err;
Scott James Remnant12473562006-08-31 04:21:05 +010072
Scott James Remnantff87eaf2009-07-08 18:38:25 +010073 err = nih_error_get ();
74 if (err->number == ESRCH) {
75 nih_message ("unknown");
76 } else {
Scott James Remnant3ef8fac2009-07-16 17:39:04 +010077 nih_error ("%s: %s", args[0] ?: _PATH_UTMPX,
78 err->message);
Scott James Remnant12473562006-08-31 04:21:05 +010079 }
80
Scott James Remnantff87eaf2009-07-08 18:38:25 +010081 nih_free (err);
Scott James Remnant12473562006-08-31 04:21:05 +010082 exit (1);
Scott James Remnante6de0452009-07-16 17:26:00 +010083 } else if (runlevel == 'N') {
84 nih_message ("unknown");
85 exit (1);
Scott James Remnant12473562006-08-31 04:21:05 +010086 }
87
Scott James Remnantff87eaf2009-07-08 18:38:25 +010088 nih_message ("%c %c", prevlevel, runlevel);
89
Scott James Remnant12473562006-08-31 04:21:05 +010090 return 0;
91}