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"
46 /* -s, --signal: signal to send. */
47 static int sig_nr = SIGTERM;
49 /* -f, --force: ignore errors. */
52 static void cond_error(int err_no, const char *, ...) PRINTF_FORMAT(2, 3);
53 static bool kill_pidfile(const char *pidfile, FILE *);
55 static void parse_options(int argc, char *argv[]);
56 static void usage(void);
59 main(int argc, char *argv[])
64 set_program_name(argv[0]);
65 parse_options(argc, argv);
71 fatal(0, "need at least one non-option argument; "
72 "use --help for usage");
76 for (i = 0; i < argc; i++) {
80 pidfile = make_pidfile_name(argv[i]);
81 file = fopen(pidfile, "r");
84 cond_error(errno, "%s: open", pidfile);
86 ok = kill_pidfile(argv[i], file) && ok;
92 return ok || force ? EXIT_SUCCESS : EXIT_FAILURE;
96 kill_pidfile(const char *pidfile, FILE *file)
101 lck.l_type = F_WRLCK;
102 lck.l_whence = SEEK_SET;
105 if (fcntl(fileno(file), F_GETLK, &lck)) {
106 cond_error(errno, "%s: fcntl", pidfile);
110 if (!fgets(line, sizeof line, file)) {
111 cond_error(errno, "%s: read", pidfile);
115 if (lck.l_pid != strtoul(line, NULL, 10)) {
116 cond_error(errno, "l_pid (%ld) != %s pid (%s)",
117 (long int) lck.l_pid, pidfile, line);
121 if (kill(lck.l_pid, sig_nr) < 0) {
122 cond_error(errno, "%s: kill(%ld)", pidfile, (long int) lck.l_pid);
130 parse_options(int argc, char *argv[])
132 static struct option long_options[] = {
133 {"signal", required_argument, 0, 's'},
134 {"force", no_argument, 0, 'f'},
135 {"help", no_argument, 0, 'h'},
136 {"version", no_argument, 0, 'V'},
139 char *short_options = long_options_to_short_options(long_options);
144 c = getopt_long(argc, argv, short_options, long_options, NULL);
151 if (atoi(optarg) || !strcmp(optarg, "0")) {
152 sig_nr = atoi(optarg);
159 static const struct signal_name signals[] = {
160 #define SIGNAL(NAME) { #NAME, NAME }
195 for (i = 0; i < ARRAY_SIZE(signals); i++) {
196 const struct signal_name *s = &signals[i];
197 if (!strcmp(optarg, s->name)
198 || !strcmp(optarg, s->name + 3)) {
203 fatal(0, "unknown signal \"%s\"", optarg);
216 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
232 printf("%s: kills a program using a pidfile\n"
233 "usage: %s [OPTIONS] PIDFILE [PIDFILE...]\n"
234 "where each PIDFILE is a pidfile created by an OpenFlow daemon.\n"
236 " -s, --signal=NUMBER|NAME signal to send (default: TERM)\n"
237 " -f, --force ignore errors\n"
238 " -h, --help display this help message\n"
239 " -V, --version display version information\n",
240 program_name, program_name);
245 cond_error(int err_no, const char *format, ...)
250 fprintf(stderr, "%s: ", program_name);
251 va_start(args, format);
252 vfprintf(stderr, format, args);
255 fprintf(stderr, " (%s)", strerror(err_no));