Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 1 | /* upstart |
| 2 | * |
Scott James Remnant | c5d3e1b | 2009-02-20 22:22:48 +0000 | [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 | 13ffffe | 2006-08-31 04:33:39 +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 | 13ffffe | 2006-08-31 04:33:39 +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 | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | # include <config.h> |
| 22 | #endif /* HAVE_CONFIG_H */ |
| 23 | |
| 24 | |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 28 | #include <signal.h> |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <nih/macros.h> |
Scott James Remnant | da10027 | 2007-01-06 20:24:18 +0000 | [diff] [blame] | 34 | #include <nih/alloc.h> |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 35 | #include <nih/string.h> |
| 36 | #include <nih/main.h> |
| 37 | #include <nih/option.h> |
| 38 | #include <nih/logging.h> |
| 39 | #include <nih/error.h> |
| 40 | |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 41 | #include "sysv.h" |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 42 | |
| 43 | |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 44 | /* Prototypes for option functions */ |
| 45 | int env_option (NihOption *option, const char *arg); |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * extra_env: |
| 50 | * |
| 51 | * Extra environment variables to append to the runlevel event. |
| 52 | **/ |
| 53 | char **extra_env = NULL; |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * env_option: |
| 58 | * @option: NihOption invoked, |
| 59 | * @arg: argument to parse. |
| 60 | * |
| 61 | * This option setter is used to append @arg to the list of environment |
| 62 | * variables pointed to by the value member of option, which must be a |
| 63 | * pointer to a char **. |
| 64 | * |
| 65 | * The arg_name member of @option must not be NULL. |
| 66 | * |
| 67 | * Returns: zero on success, non-zero on error. |
| 68 | **/ |
| 69 | int |
| 70 | env_option (NihOption *option, |
| 71 | const char *arg) |
| 72 | { |
| 73 | char ***value; |
| 74 | |
| 75 | nih_assert (option != NULL); |
| 76 | nih_assert (option->value != NULL); |
| 77 | nih_assert (arg != NULL); |
| 78 | |
| 79 | value = (char ***)option->value; |
| 80 | |
| 81 | NIH_MUST (nih_str_array_add (value, NULL, NULL, arg)); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | #ifndef TEST |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 88 | /** |
| 89 | * options: |
| 90 | * |
| 91 | * Command-line options accepted. |
| 92 | **/ |
| 93 | static NihOption options[] = { |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 94 | { 'e', NULL, N_("set environment variable in the runlevel event"), |
| 95 | NULL, "KEY=VALUE", &extra_env, env_option }, |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 96 | |
Scott James Remnant | c2ebba9 | 2009-07-08 22:43:49 +0100 | [diff] [blame] | 97 | /* Compatibility options, all ignored */ |
| 98 | { 't', NULL, NULL, NULL, "SECONDS", NULL, NULL }, |
| 99 | |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 100 | NIH_OPTION_LAST |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | int |
| 105 | main (int argc, |
| 106 | char *argv[]) |
| 107 | { |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 108 | char **args; |
| 109 | int runlevel; |
| 110 | int ret; |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 111 | |
| 112 | nih_main_init (argv[0]); |
Scott James Remnant | 1ee0f48 | 2006-10-11 17:40:41 +0100 | [diff] [blame] | 113 | |
Scott James Remnant | 17fec6e | 2006-09-09 05:05:42 +0100 | [diff] [blame] | 114 | nih_option_set_usage ("RUNLEVEL"); |
Scott James Remnant | 4a5cd21 | 2006-10-13 12:59:15 +0100 | [diff] [blame] | 115 | nih_option_set_synopsis (_("Change runlevel.")); |
Scott James Remnant | 462734c | 2006-10-13 13:36:00 +0100 | [diff] [blame] | 116 | nih_option_set_help ( |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 117 | _("RUNLEVEL should be one of 0123456sS, where s and S are " |
| 118 | "considered identical.\n" |
| 119 | "\n" |
| 120 | "RUNLEVEL may also be Q or q to instruct the init daemon " |
| 121 | "to reload its configuration, this is rarely necessary " |
| 122 | "since the daemon watches its configuration for changes.\n" |
| 123 | "\n" |
| 124 | "RUNLEVEL may be U or u to instruct the init daemon to " |
| 125 | "re-execute itself, this is not recommended since Upstart " |
| 126 | "does not currently preserve its state.\n")); |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 127 | |
| 128 | args = nih_option_parser (NULL, argc, argv, options, FALSE); |
| 129 | if (! args) |
| 130 | exit (1); |
| 131 | |
| 132 | /* First argument must be a single character we know */ |
Scott James Remnant | 98af00b | 2009-07-09 00:22:25 +0100 | [diff] [blame] | 133 | if (! args[0]) { |
| 134 | fprintf (stderr, _("%s: missing runlevel\n"), program_name); |
| 135 | nih_main_suggest_help (); |
| 136 | exit (1); |
| 137 | } |
| 138 | if ((! strchr ("0123456SsQqUu", args[0][0])) || args[0][1]) { |
Scott James Remnant | ff63cf7 | 2006-08-31 04:39:34 +0100 | [diff] [blame] | 139 | fprintf (stderr, _("%s: illegal runlevel: %s\n"), |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 140 | program_name, args[0]); |
| 141 | nih_main_suggest_help (); |
| 142 | exit (1); |
| 143 | } |
| 144 | |
| 145 | /* Check we're root */ |
| 146 | setuid (geteuid ()); |
| 147 | if (getuid ()) { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 148 | nih_fatal (_("Need to be root")); |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 149 | exit (1); |
| 150 | } |
| 151 | |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 152 | /* Send the appropriate message */ |
| 153 | runlevel = args[0][0]; |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 154 | |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 155 | switch (runlevel) { |
Scott James Remnant | 52360ea | 2007-02-08 03:06:40 +0000 | [diff] [blame] | 156 | case '0': |
| 157 | case '1': |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 158 | case '2': |
| 159 | case '3': |
| 160 | case '4': |
| 161 | case '5': |
Scott James Remnant | 1889aa6 | 2006-09-09 03:41:53 +0100 | [diff] [blame] | 162 | case '6': |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 163 | ret = sysv_change_runlevel (runlevel, extra_env, NULL, NULL); |
Scott James Remnant | 1889aa6 | 2006-09-09 03:41:53 +0100 | [diff] [blame] | 164 | break; |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 165 | case 'S': |
| 166 | case 's': |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 167 | ret = sysv_change_runlevel ('S', extra_env, NULL, NULL); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 168 | break; |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 169 | case 'Q': |
| 170 | case 'q': |
| 171 | ret = kill (1, SIGHUP); |
| 172 | if (ret < 0) |
| 173 | nih_error_raise_system (); |
| 174 | break; |
| 175 | case 'U': |
| 176 | case 'u': |
Scott James Remnant | c80b09f | 2009-07-08 22:48:03 +0100 | [diff] [blame] | 177 | ret = kill (1, SIGTERM); |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 178 | if (ret < 0) |
| 179 | nih_error_raise_system (); |
| 180 | break; |
Scott James Remnant | b175de8 | 2009-07-09 12:50:19 +0100 | [diff] [blame] | 181 | default: |
| 182 | nih_assert_not_reached (); |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 185 | if (ret < 0) { |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 186 | NihError *err; |
| 187 | |
| 188 | err = nih_error_get (); |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 189 | nih_error ("%s", err->message); |
| 190 | nih_free (err); |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 191 | |
Scott James Remnant | 13ffffe | 2006-08-31 04:33:39 +0100 | [diff] [blame] | 192 | exit (1); |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
Scott James Remnant | 3aac713 | 2009-07-08 19:02:56 +0100 | [diff] [blame] | 197 | #endif |