Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 1 | /* upstart |
| 2 | * |
Scott James Remnant | 7512302 | 2009-05-22 13:27:56 +0200 | [diff] [blame] | 3 | * Copyright © 2009 Canonical Ltd. |
Scott James Remnant | 7d5b2ea | 2009-05-22 15:20:12 +0200 | [diff] [blame] | 4 | * Author: Scott James Remnant <scott@netsplit.com>. |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 5 | * |
Scott James Remnant | 0c0c5a5 | 2009-06-23 10:29:35 +0100 | [diff] [blame] | 6 | * 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 Remnant | 7512302 | 2009-05-22 13:27:56 +0200 | [diff] [blame] | 8 | * published by the Free Software Foundation. |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 9 | * |
| 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 Remnant | 0c0c5a5 | 2009-06-23 10:29:35 +0100 | [diff] [blame] | 15 | * 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 Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | # include <config.h> |
| 22 | #endif /* HAVE_CONFIG_H */ |
| 23 | |
| 24 | |
Scott James Remnant | 3ef8fac | 2009-07-16 17:39:04 +0100 | [diff] [blame] | 25 | #include <utmpx.h> |
Scott James Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 26 | #include <errno.h> |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 27 | #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 Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 36 | #include "utmp.h" |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 37 | |
| 38 | |
| 39 | /** |
| 40 | * options: |
| 41 | * |
| 42 | * Command-line options accepted. |
| 43 | **/ |
| 44 | static NihOption options[] = { |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 45 | NIH_OPTION_LAST |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | int |
| 50 | main (int argc, |
| 51 | char *argv[]) |
| 52 | { |
Scott James Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 53 | char **args; |
| 54 | int runlevel; |
| 55 | int prevlevel; |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 56 | |
| 57 | nih_main_init (argv[0]); |
Scott James Remnant | 1ee0f48 | 2006-10-11 17:40:41 +0100 | [diff] [blame] | 58 | |
| 59 | nih_option_set_usage (_("[UTMP]")); |
Scott James Remnant | f781962 | 2006-10-13 12:54:43 +0100 | [diff] [blame] | 60 | nih_option_set_synopsis (_("Output previous and current runlevel.")); |
Scott James Remnant | 462734c | 2006-10-13 13:36:00 +0100 | [diff] [blame] | 61 | nih_option_set_help ( |
| 62 | _("The system /var/run/utmp file is used unless the alternate " |
Scott James Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 63 | "file UTMP is given.\n")); |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 64 | |
| 65 | args = nih_option_parser (NULL, argc, argv, options, FALSE); |
| 66 | if (! args) |
| 67 | exit (1); |
| 68 | |
Scott James Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 69 | runlevel = NIH_SHOULD (utmp_get_runlevel (args[0], &prevlevel)); |
| 70 | if (runlevel < 0) { |
| 71 | NihError *err; |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 72 | |
Scott James Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 73 | err = nih_error_get (); |
| 74 | if (err->number == ESRCH) { |
| 75 | nih_message ("unknown"); |
| 76 | } else { |
Scott James Remnant | 3ef8fac | 2009-07-16 17:39:04 +0100 | [diff] [blame] | 77 | nih_error ("%s: %s", args[0] ?: _PATH_UTMPX, |
| 78 | err->message); |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Scott James Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 81 | nih_free (err); |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 82 | exit (1); |
Scott James Remnant | e6de045 | 2009-07-16 17:26:00 +0100 | [diff] [blame] | 83 | } else if (runlevel == 'N') { |
| 84 | nih_message ("unknown"); |
| 85 | exit (1); |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Scott James Remnant | ff87eaf | 2009-07-08 18:38:25 +0100 | [diff] [blame] | 88 | nih_message ("%c %c", prevlevel, runlevel); |
| 89 | |
Scott James Remnant | 1247356 | 2006-08-31 04:21:05 +0100 | [diff] [blame] | 90 | return 0; |
| 91 | } |