2 * Copyright (c) 2008, 2009, 2010 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]);
49 parse_options(argc, argv);
55 ovs_fatal(0, "need at least one non-option argument; "
56 "use --help for usage");
60 for (i = 0; i < argc; i++) {
64 pidfile = make_pidfile_name(argv[i]);
65 pid = read_pidfile(pidfile);
67 if (kill(pid, sig_nr) < 0) {
68 cond_error(errno, "%s: kill(%ld)", pidfile, (long int) pid);
71 cond_error(-pid, "could not read %s", pidfile);
76 return ok || force ? EXIT_SUCCESS : EXIT_FAILURE;
80 parse_options(int argc, char *argv[])
82 static struct option long_options[] = {
83 {"signal", required_argument, 0, 's'},
84 {"force", no_argument, 0, 'f'},
85 {"help", no_argument, 0, 'h'},
86 {"version", no_argument, 0, 'V'},
89 char *short_options = long_options_to_short_options(long_options);
94 c = getopt_long(argc, argv, short_options, long_options, NULL);
101 if (atoi(optarg) || !strcmp(optarg, "0")) {
102 sig_nr = atoi(optarg);
109 static const struct signal_name signals[] = {
110 #define SIGNAL(NAME) { #NAME, NAME }
145 for (i = 0; i < ARRAY_SIZE(signals); i++) {
146 const struct signal_name *s = &signals[i];
147 if (!strcmp(optarg, s->name)
148 || !strcmp(optarg, s->name + 3)) {
153 ovs_fatal(0, "unknown signal \"%s\"", optarg);
166 OVS_PRINT_VERSION(0, 0);
182 printf("%s: kills a program using a pidfile\n"
183 "usage: %s [OPTIONS] PIDFILE [PIDFILE...]\n"
184 "where PIDFILE is a pidfile created by an Open vSwitch daemon.\n"
186 " -s, --signal=NUMBER|NAME signal to send (default: TERM)\n"
187 " -f, --force ignore errors\n"
188 " -h, --help display this help message\n"
189 " -V, --version display version information\n",
190 program_name, program_name);
195 cond_error(int err_no, const char *format, ...)
200 fprintf(stderr, "%s: ", program_name);
201 va_start(args, format);
202 vfprintf(stderr, format, args);
205 fprintf(stderr, " (%s)", strerror(err_no));