1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "alloc-util.h"
6 #include "pretty-print.h"
7 #include "systemctl-compat-runlevel.h"
8 #include "systemctl.h"
9 #include "terminal-util.h"
10 #include "utmp-wtmp.h"
11
runlevel_help(void)12 static int runlevel_help(void) {
13 _cleanup_free_ char *link = NULL;
14 int r;
15
16 r = terminal_urlify_man("runlevel", "8", &link);
17 if (r < 0)
18 return log_oom();
19
20 printf("%s [OPTIONS...]\n"
21 "\n%sPrints the previous and current runlevel of the init system.%s\n"
22 "\nOptions:\n"
23 " --help Show this help\n"
24 "\nSee the %s for details.\n",
25 program_invocation_short_name,
26 ansi_highlight(),
27 ansi_normal(),
28 link);
29
30 return 0;
31 }
32
runlevel_parse_argv(int argc,char * argv[])33 int runlevel_parse_argv(int argc, char *argv[]) {
34 enum {
35 ARG_HELP = 0x100,
36 };
37
38 static const struct option options[] = {
39 { "help", no_argument, NULL, ARG_HELP },
40 {}
41 };
42
43 int c;
44
45 assert(argc >= 0);
46 assert(argv);
47
48 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0)
49 switch (c) {
50
51 case ARG_HELP:
52 return runlevel_help();
53
54 case '?':
55 return -EINVAL;
56
57 default:
58 assert_not_reached();
59 }
60
61 if (optind < argc)
62 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
63 "Too many arguments.");
64
65 return 1;
66 }
67
runlevel_main(void)68 int runlevel_main(void) {
69 int r, runlevel, previous;
70
71 r = utmp_get_runlevel(&runlevel, &previous);
72 if (r < 0) {
73 puts("unknown");
74 return r;
75 }
76
77 printf("%c %c\n",
78 previous <= 0 ? 'N' : previous,
79 runlevel <= 0 ? 'N' : runlevel);
80
81 return 0;
82 }
83