1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
42 #include "command-line.h"
48 /* -s, --signal: signal to send. */
49 static int sig_nr = SIGTERM;
51 /* -f, --force: ignore errors. */
54 static void cond_error(int err_no, const char *, ...) PRINTF_FORMAT(2, 3);
56 static void parse_options(int argc, char *argv[]);
57 static void usage(void);
60 main(int argc, char *argv[])
65 set_program_name(argv[0]);
68 parse_options(argc, argv);
74 ofp_fatal(0, "need at least one non-option argument; "
75 "use --help for usage");
79 for (i = 0; i < argc; i++) {
83 pidfile = make_pidfile_name(argv[i]);
84 pid = read_pidfile(pidfile);
86 if (kill(pid, sig_nr) < 0) {
87 cond_error(errno, "%s: kill(%ld)", pidfile, (long int) pid);
90 cond_error(-pid, "could not read %s", pidfile);
95 return ok || force ? EXIT_SUCCESS : EXIT_FAILURE;
99 parse_options(int argc, char *argv[])
101 static struct option long_options[] = {
102 {"signal", required_argument, 0, 's'},
103 {"force", no_argument, 0, 'f'},
104 {"help", no_argument, 0, 'h'},
105 {"version", no_argument, 0, 'V'},
108 char *short_options = long_options_to_short_options(long_options);
113 c = getopt_long(argc, argv, short_options, long_options, NULL);
120 if (atoi(optarg) || !strcmp(optarg, "0")) {
121 sig_nr = atoi(optarg);
128 static const struct signal_name signals[] = {
129 #define SIGNAL(NAME) { #NAME, NAME }
164 for (i = 0; i < ARRAY_SIZE(signals); i++) {
165 const struct signal_name *s = &signals[i];
166 if (!strcmp(optarg, s->name)
167 || !strcmp(optarg, s->name + 3)) {
172 ofp_fatal(0, "unknown signal \"%s\"", optarg);
185 printf("%s %s compiled "__DATE__" "__TIME__"\n",
186 program_name, VERSION BUILDNR);
202 printf("%s: kills a program using a pidfile\n"
203 "usage: %s [OPTIONS] PIDFILE [PIDFILE...]\n"
204 "where each PIDFILE is a pidfile created by an OpenFlow daemon.\n"
206 " -s, --signal=NUMBER|NAME signal to send (default: TERM)\n"
207 " -f, --force ignore errors\n"
208 " -h, --help display this help message\n"
209 " -V, --version display version information\n",
210 program_name, program_name);
215 cond_error(int err_no, const char *format, ...)
220 fprintf(stderr, "%s: ", program_name);
221 va_start(args, format);
222 vfprintf(stderr, format, args);
225 fprintf(stderr, " (%s)", strerror(err_no));