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"
47 /* -s, --signal: signal to send. */
48 static int sig_nr = SIGTERM;
50 /* -f, --force: ignore errors. */
53 static void cond_error(int err_no, const char *, ...) PRINTF_FORMAT(2, 3);
54 static bool kill_pidfile(const char *pidfile, FILE *);
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]);
67 parse_options(argc, argv);
73 ofp_fatal(0, "need at least one non-option argument; "
74 "use --help for usage");
78 for (i = 0; i < argc; i++) {
82 pidfile = make_pidfile_name(argv[i]);
83 file = fopen(pidfile, "r");
86 cond_error(errno, "%s: open", pidfile);
88 ok = kill_pidfile(argv[i], file) && ok;
94 return ok || force ? EXIT_SUCCESS : EXIT_FAILURE;
98 kill_pidfile(const char *pidfile, FILE *file)
103 lck.l_type = F_WRLCK;
104 lck.l_whence = SEEK_SET;
107 if (fcntl(fileno(file), F_GETLK, &lck)) {
108 cond_error(errno, "%s: fcntl", pidfile);
112 if (!fgets(line, sizeof line, file)) {
113 cond_error(errno, "%s: read", pidfile);
117 if (lck.l_pid != strtoul(line, NULL, 10)) {
118 cond_error(errno, "l_pid (%ld) != %s pid (%s)",
119 (long int) lck.l_pid, pidfile, line);
123 if (kill(lck.l_pid, sig_nr) < 0) {
124 cond_error(errno, "%s: kill(%ld)", pidfile, (long int) lck.l_pid);
132 parse_options(int argc, char *argv[])
134 static struct option long_options[] = {
135 {"signal", required_argument, 0, 's'},
136 {"force", no_argument, 0, 'f'},
137 {"help", no_argument, 0, 'h'},
138 {"version", no_argument, 0, 'V'},
141 char *short_options = long_options_to_short_options(long_options);
146 c = getopt_long(argc, argv, short_options, long_options, NULL);
153 if (atoi(optarg) || !strcmp(optarg, "0")) {
154 sig_nr = atoi(optarg);
161 static const struct signal_name signals[] = {
162 #define SIGNAL(NAME) { #NAME, NAME }
197 for (i = 0; i < ARRAY_SIZE(signals); i++) {
198 const struct signal_name *s = &signals[i];
199 if (!strcmp(optarg, s->name)
200 || !strcmp(optarg, s->name + 3)) {
205 ofp_fatal(0, "unknown signal \"%s\"", optarg);
218 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
234 printf("%s: kills a program using a pidfile\n"
235 "usage: %s [OPTIONS] PIDFILE [PIDFILE...]\n"
236 "where each PIDFILE is a pidfile created by an OpenFlow daemon.\n"
238 " -s, --signal=NUMBER|NAME signal to send (default: TERM)\n"
239 " -f, --force ignore errors\n"
240 " -h, --help display this help message\n"
241 " -V, --version display version information\n",
242 program_name, program_name);
247 cond_error(int err_no, const char *format, ...)
252 fprintf(stderr, "%s: ", program_name);
253 va_start(args, format);
254 vfprintf(stderr, format, args);
257 fprintf(stderr, " (%s)", strerror(err_no));