2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include "command-line.h"
31 /* -s, --signal: signal to send. */
32 static int sig_nr = SIGTERM;
34 /* -f, --force: ignore errors. */
37 static void cond_error(int err_no, const char *, ...) PRINTF_FORMAT(2, 3);
39 static void parse_options(int argc, char *argv[]);
40 static void usage(void);
43 main(int argc, char *argv[])
48 set_program_name(argv[0]);
51 parse_options(argc, argv);
57 ovs_fatal(0, "need at least one non-option argument; "
58 "use --help for usage");
62 for (i = 0; i < argc; i++) {
66 pidfile = make_pidfile_name(argv[i]);
67 pid = read_pidfile(pidfile);
69 if (kill(pid, sig_nr) < 0) {
70 cond_error(errno, "%s: kill(%ld)", pidfile, (long int) pid);
73 cond_error(-pid, "could not read %s", pidfile);
78 return ok || force ? EXIT_SUCCESS : EXIT_FAILURE;
82 parse_options(int argc, char *argv[])
84 static struct option long_options[] = {
85 {"signal", required_argument, 0, 's'},
86 {"force", no_argument, 0, 'f'},
87 {"help", no_argument, 0, 'h'},
88 {"version", no_argument, 0, 'V'},
91 char *short_options = long_options_to_short_options(long_options);
96 c = getopt_long(argc, argv, short_options, long_options, NULL);
103 if (atoi(optarg) || !strcmp(optarg, "0")) {
104 sig_nr = atoi(optarg);
111 static const struct signal_name signals[] = {
112 #define SIGNAL(NAME) { #NAME, NAME }
147 for (i = 0; i < ARRAY_SIZE(signals); i++) {
148 const struct signal_name *s = &signals[i];
149 if (!strcmp(optarg, s->name)
150 || !strcmp(optarg, s->name + 3)) {
155 ovs_fatal(0, "unknown signal \"%s\"", optarg);
168 OVS_PRINT_VERSION(0, 0);
184 printf("%s: kills a program using a pidfile\n"
185 "usage: %s [OPTIONS] PIDFILE [PIDFILE...]\n"
186 "where PIDFILE is a pidfile created by an Open vSwitch daemon.\n"
188 " -s, --signal=NUMBER|NAME signal to send (default: TERM)\n"
189 " -f, --force ignore errors\n"
190 " -h, --help display this help message\n"
191 " -V, --version display version information\n",
192 program_name, program_name);
197 cond_error(int err_no, const char *format, ...)
202 fprintf(stderr, "%s: ", program_name);
203 va_start(args, format);
204 vfprintf(stderr, format, args);
207 fprintf(stderr, " (%s)", strerror(err_no));