OpenCoverage

kill.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/kill.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* kill -- send a signal to a process-
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.-
3-
4 This program is free software: you can redistribute it and/or modify-
5 it under the terms of the GNU General Public License as published by-
6 the Free Software Foundation, either version 3 of the License, or-
7 (at your option) any later version.-
8-
9 This program is distributed in the hope that it will be useful,-
10 but WITHOUT ANY WARRANTY; without even the implied warranty of-
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
12 GNU General Public License for more details.-
13-
14 You should have received a copy of the GNU General Public License-
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
16-
17/* Written by Paul Eggert. */-
18-
19#include <config.h>-
20#include <stdio.h>-
21#include <getopt.h>-
22#include <sys/types.h>-
23#include <signal.h>-
24-
25#include "system.h"-
26#include "error.h"-
27#include "sig2str.h"-
28#include "operand2sig.h"-
29#include "quote.h"-
30-
31/* The official name of this program (e.g., no 'g' prefix). */-
32#define PROGRAM_NAME "kill"-
33-
34#define AUTHORS proper_name ("Paul Eggert")-
35-
36#if ! (HAVE_DECL_STRSIGNAL || defined strsignal)-
37# if ! (HAVE_DECL_SYS_SIGLIST || defined sys_siglist)-
38# if HAVE_DECL__SYS_SIGLIST || defined _sys_siglist-
39# define sys_siglist _sys_siglist-
40# elif HAVE_DECL___SYS_SIGLIST || defined __sys_siglist-
41# define sys_siglist __sys_siglist-
42# endif-
43# endif-
44# if HAVE_DECL_SYS_SIGLIST || defined sys_siglist-
45# define strsignal(signum) (0 <= (signum) && (signum) <= SIGNUM_BOUND \-
46 ? sys_siglist[signum] \-
47 : 0)-
48# endif-
49# ifndef strsignal-
50# define strsignal(signum) 0-
51# endif-
52#endif-
53-
54static char const short_options[] =-
55 "0::1::2::3::4::5::6::7::8::9::"-
56 "A::B::C::D::E::F::G::H::I::J::K::M::"-
57 "N::O::P::Q::R::S::T::U::V::W::X::Y::Z::"-
58 "Lln:s:t";-
59-
60static struct option const long_options[] =-
61{-
62 {"list", no_argument, NULL, 'l'},-
63 {"signal", required_argument, NULL, 's'},-
64 {"table", no_argument, NULL, 't'},-
65 {GETOPT_HELP_OPTION_DECL},-
66 {GETOPT_VERSION_OPTION_DECL},-
67 {NULL, 0, NULL, 0}-
68};-
69-
70void-
71usage (int status)-
72{-
73 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 2 times by 1 test
Evaluated by:
  • kill
2-8
74 emit_try_help ();
executed 8 times by 1 test: end of block
Executed by:
  • kill
8
75 else-
76 {-
77 printf (_("\-
78Usage: %s [-s SIGNAL | -SIGNAL] PID...\n\-
79 or: %s -l [SIGNAL]...\n\-
80 or: %s -t [SIGNAL]...\n\-
81"),-
82 program_name, program_name, program_name);-
83 fputs (_("\-
84Send signals to processes, or list signals.\n\-
85"), stdout);-
86-
87 emit_mandatory_arg_note ();-
88-
89 fputs (_("\-
90 -s, --signal=SIGNAL, -SIGNAL\n\-
91 specify the name or number of the signal to be sent\n\-
92 -l, --list list signal names, or convert signal names to/from numbers\n\-
93 -t, --table print a table of signal information\n\-
94"), stdout);-
95 fputs (HELP_OPTION_DESCRIPTION, stdout);-
96 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
97 fputs (_("\n\-
98SIGNAL may be a signal name like 'HUP', or a signal number like '1',\n\-
99or the exit status of a process terminated by a signal.\n\-
100PID is an integer; if negative it identifies a process group.\n\-
101"), stdout);-
102 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);-
103 emit_ancillary_info (PROGRAM_NAME);-
104 }
executed 2 times by 1 test: end of block
Executed by:
  • kill
2
105 exit (status);
executed 10 times by 1 test: exit (status);
Executed by:
  • kill
10
106}-
107-
108/* Print a row of 'kill -t' output. NUM_WIDTH is the maximum signal-
109 number width, and SIGNUM is the signal number to print. The-
110 maximum name width is NAME_WIDTH, and SIGNAME is the name to print. */-
111-
112static void-
113print_table_row (int num_width, int signum,-
114 int name_width, char const *signame)-
115{-
116 char const *description = strsignal (signum);-
117 printf ("%*d %-*s %s\n", num_width, signum, name_width, signame,-
118 description ? description : "?");-
119}
executed 126 times by 1 test: end of block
Executed by:
  • kill
126
120-
121/* Print a list of signal names. If TABLE, print a table.-
122 Print the names specified by ARGV if nonzero; otherwise,-
123 print all known names. Return a suitable exit status. */-
124-
125static int-
126list_signals (bool table, char *const *argv)-
127{-
128 int signum;-
129 int status = EXIT_SUCCESS;-
130 char signame[SIG2STR_MAX];-
131-
132 if (table)
tableDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 7 times by 1 test
Evaluated by:
  • kill
3-7
133 {-
134 unsigned int name_width = 0;-
135-
136 /* Compute the maximum width of a signal number. */-
137 unsigned int num_width = 1;-
138 for (signum = 1; signum <= SIGNUM_BOUND / 10; signum *= 10)
signum <= ( 65 - 1) / 10Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 3 times by 1 test
Evaluated by:
  • kill
3
139 num_width++;
executed 3 times by 1 test: num_width++;
Executed by:
  • kill
3
140-
141 /* Compute the maximum width of a signal name. */-
142 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
signum <= ( 65 - 1)Description
TRUEevaluated 192 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 3 times by 1 test
Evaluated by:
  • kill
3-192
143 if (sig2str (signum, signame) == 0)
sig2str (signum, signame) == 0Description
TRUEevaluated 186 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 6 times by 1 test
Evaluated by:
  • kill
6-186
144 {-
145 size_t len = strlen (signame);-
146 if (name_width < len)
name_width < lenDescription
TRUEevaluated 15 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 171 times by 1 test
Evaluated by:
  • kill
15-171
147 name_width = len;
executed 15 times by 1 test: name_width = len;
Executed by:
  • kill
15
148 }
executed 186 times by 1 test: end of block
Executed by:
  • kill
186
149-
150 if (argv)
argvDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • kill
FALSEevaluated 2 times by 1 test
Evaluated by:
  • kill
1-2
151 for (; *argv; argv++)
*argvDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 1 time by 1 test
Evaluated by:
  • kill
1-2
152 {-
153 signum = operand2sig (*argv, signame);-
154 if (signum < 0)
signum < 0Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • kill
0-2
155 status = EXIT_FAILURE;
never executed: status = 1 ;
0
156 else-
157 print_table_row (num_width, signum, name_width, signame);
executed 2 times by 1 test: print_table_row (num_width, signum, name_width, signame);
Executed by:
  • kill
2
158 }-
159 else-
160 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
signum <= ( 65 - 1)Description
TRUEevaluated 128 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 2 times by 1 test
Evaluated by:
  • kill
2-128
161 if (sig2str (signum, signame) == 0)
sig2str (signum, signame) == 0Description
TRUEevaluated 124 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 4 times by 1 test
Evaluated by:
  • kill
4-124
162 print_table_row (num_width, signum, name_width, signame);
executed 124 times by 1 test: print_table_row (num_width, signum, name_width, signame);
Executed by:
  • kill
124
163 }
executed 3 times by 1 test: end of block
Executed by:
  • kill
3
164 else-
165 {-
166 if (argv)
argvDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 1 time by 1 test
Evaluated by:
  • kill
1-6
167 for (; *argv; argv++)
*argvDescription
TRUEevaluated 10 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 6 times by 1 test
Evaluated by:
  • kill
6-10
168 {-
169 signum = operand2sig (*argv, signame);-
170 if (signum < 0)
signum < 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 7 times by 1 test
Evaluated by:
  • kill
3-7
171 status = EXIT_FAILURE;
executed 3 times by 1 test: status = 1 ;
Executed by:
  • kill
3
172 else-
173 {-
174 if (ISDIGIT (**argv))
((unsigned int...v) - '0' <= 9)Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 3 times by 1 test
Evaluated by:
  • kill
3-4
175 puts (signame);
executed 4 times by 1 test: puts (signame);
Executed by:
  • kill
4
176 else-
177 printf ("%d\n", signum);
executed 3 times by 1 test: printf ("%d\n", signum);
Executed by:
  • kill
3
178 }-
179 }-
180 else-
181 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
signum <= ( 65 - 1)Description
TRUEevaluated 64 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 1 time by 1 test
Evaluated by:
  • kill
1-64
182 if (sig2str (signum, signame) == 0)
sig2str (signum, signame) == 0Description
TRUEevaluated 62 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 2 times by 1 test
Evaluated by:
  • kill
2-62
183 puts (signame);
executed 62 times by 1 test: puts (signame);
Executed by:
  • kill
62
184 }
executed 7 times by 1 test: end of block
Executed by:
  • kill
7
185-
186 return status;
executed 10 times by 1 test: return status;
Executed by:
  • kill
10
187}-
188-
189/* Send signal SIGNUM to all the processes or process groups specified-
190 by ARGV. Return a suitable exit status. */-
191-
192static int-
193send_signals (int signum, char *const *argv)-
194{-
195 int status = EXIT_SUCCESS;-
196 char const *arg = *argv;-
197-
198 do-
199 {-
200 char *endp;-
201 intmax_t n = (errno = 0, strtoimax (arg, &endp, 10));-
202 pid_t pid = n;-
203-
204 if (errno == ERANGE || pid != n || arg == endp || *endp)
(*__errno_location ()) == 34Description
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • kill
pid != nDescription
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • kill
arg == endpDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • kill
FALSEevaluated 7 times by 1 test
Evaluated by:
  • kill
*endpDescription
TRUEnever evaluated
FALSEevaluated 7 times by 1 test
Evaluated by:
  • kill
0-8
205 {-
206 error (0, 0, _("%s: invalid process id"), quote (arg));-
207 status = EXIT_FAILURE;-
208 }
executed 1 time by 1 test: end of block
Executed by:
  • kill
1
209 else if (kill (pid, signum) != 0)
kill (pid, signum) != 0Description
TRUEnever evaluated
FALSEevaluated 7 times by 1 test
Evaluated by:
  • kill
0-7
210 {-
211 error (0, errno, "%s", quote (arg));-
212 status = EXIT_FAILURE;-
213 }
never executed: end of block
0
214 }
executed 8 times by 1 test: end of block
Executed by:
  • kill
8
215 while ((arg = *++argv));
(arg = *++argv)Description
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • kill
0-8
216-
217 return status;
executed 8 times by 1 test: return status;
Executed by:
  • kill
8
218}-
219-
220int-
221main (int argc, char **argv)-
222{-
223 int optc;-
224 bool list = false;-
225 bool table = false;-
226 int signum = -1;-
227 char signame[SIG2STR_MAX];-
228-
229 initialize_main (&argc, &argv);-
230 set_program_name (argv[0]);-
231 setlocale (LC_ALL, "");-
232 bindtextdomain (PACKAGE, LOCALEDIR);-
233 textdomain (PACKAGE);-
234-
235 atexit (close_stdout);-
236-
237 while ((optc = getopt_long (argc, argv, short_options, long_options, NULL))
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 37 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 19 times by 1 test
Evaluated by:
  • kill
19-37
238 != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 37 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 19 times by 1 test
Evaluated by:
  • kill
19-37
239 switch (optc)-
240 {-
241 case '0': case '1': case '2': case '3': case '4':
executed 3 times by 1 test: case '0':
Executed by:
  • kill
executed 3 times by 1 test: case '1':
Executed by:
  • kill
never executed: case '2':
never executed: case '3':
never executed: case '4':
0-3
242 case '5': case '6': case '7': case '8': case '9':
never executed: case '5':
never executed: case '6':
never executed: case '7':
never executed: case '8':
never executed: case '9':
0
243 if (optind != 2)
optind != 2Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 3 times by 1 test
Evaluated by:
  • kill
3
244 {-
245 /* This option is actually a process-id. */-
246 optind--;-
247 goto no_more_options;
executed 3 times by 1 test: goto no_more_options;
Executed by:
  • kill
3
248 }-
249 FALLTHROUGH;-
250 case 'A': case 'B': case 'C': case 'D': case 'E':
code before this statement executed 3 times by 1 test: case 'A':
Executed by:
  • kill
never executed: case 'A':
never executed: case 'B':
executed 2 times by 1 test: case 'C':
Executed by:
  • kill
never executed: case 'D':
never executed: case 'E':
0-3
251 case 'F': case 'G': case 'H': case 'I': case 'J':
never executed: case 'F':
never executed: case 'G':
never executed: case 'H':
never executed: case 'I':
never executed: case 'J':
0
252 case 'K': /*case 'L':*/ case 'M': case 'N': case 'O':
never executed: case 'K':
never executed: case 'M':
never executed: case 'N':
never executed: case 'O':
0
253 case 'P': case 'Q': case 'R': case 'S': case 'T':
never executed: case 'P':
never executed: case 'Q':
never executed: case 'R':
never executed: case 'S':
executed 1 time by 1 test: case 'T':
Executed by:
  • kill
0-1
254 case 'U': case 'V': case 'W': case 'X': case 'Y':
never executed: case 'U':
never executed: case 'V':
never executed: case 'W':
never executed: case 'X':
never executed: case 'Y':
0
255 case 'Z':
never executed: case 'Z':
0
256 if (! optarg)
! optargDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 3 times by 1 test
Evaluated by:
  • kill
3
257 optarg = argv[optind - 1] + strlen (argv[optind - 1]);
executed 3 times by 1 test: optarg = argv[optind - 1] + strlen (argv[optind - 1]);
Executed by:
  • kill
3
258 if (optarg != argv[optind - 1] + 2)
optarg != argv[optind - 1] + 2Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • kill
0-6
259 {-
260 error (0, 0, _("invalid option -- %c"), optc);-
261 usage (EXIT_FAILURE);-
262 }
never executed: end of block
0
263 optarg--;-
264 FALLTHROUGH;-
265 case 'n': /* -n is not documented, but is for Bash compatibility. */
code before this statement executed 6 times by 1 test: case 'n':
Executed by:
  • kill
executed 1 time by 1 test: case 'n':
Executed by:
  • kill
1-6
266 case 's':
executed 3 times by 1 test: case 's':
Executed by:
  • kill
3
267 if (0 <= signum)
0 <= signumDescription
TRUEnever evaluated
FALSEevaluated 10 times by 1 test
Evaluated by:
  • kill
0-10
268 {-
269 error (0, 0, _("%s: multiple signals specified"), quote (optarg));-
270 usage (EXIT_FAILURE);-
271 }
never executed: end of block
0
272 signum = operand2sig (optarg, signame);-
273 if (signum < 0)
signum < 0Description
TRUEnever evaluated
FALSEevaluated 10 times by 1 test
Evaluated by:
  • kill
0-10
274 usage (EXIT_FAILURE);
never executed: usage ( 1 );
0
275 break;
executed 10 times by 1 test: break;
Executed by:
  • kill
10
276-
277 case 'L': /* -L is not documented, but is for procps compatibility. */
executed 1 time by 1 test: case 'L':
Executed by:
  • kill
1
278 case 't':
executed 4 times by 1 test: case 't':
Executed by:
  • kill
4
279 table = true;-
280 FALLTHROUGH;-
281 case 'l':
code before this statement executed 5 times by 1 test: case 'l':
Executed by:
  • kill
executed 11 times by 1 test: case 'l':
Executed by:
  • kill
5-11
282 if (list)
listDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 14 times by 1 test
Evaluated by:
  • kill
2-14
283 {-
284 error (0, 0, _("multiple -l or -t options specified"));-
285 usage (EXIT_FAILURE);-
286 }
never executed: end of block
0
287 list = true;-
288 break;
executed 14 times by 1 test: break;
Executed by:
  • kill
14
289-
290 case_GETOPT_HELP_CHAR;
never executed: break;
executed 2 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • kill
0-2
291 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 4 times by 1 test: exit ( 0 );
Executed by:
  • kill
never executed: break;
executed 4 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • kill
0-4
292 default:
executed 2 times by 1 test: default:
Executed by:
  • kill
2
293 usage (EXIT_FAILURE);-
294 }
never executed: end of block
0
295 no_more_options:
code before this statement executed 19 times by 1 test: no_more_options:
Executed by:
  • kill
19
296-
297 if (signum < 0)
signum < 0Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 10 times by 1 test
Evaluated by:
  • kill
10-12
298 signum = SIGTERM;
executed 12 times by 1 test: signum = 15 ;
Executed by:
  • kill
12
299 else if (list)
listDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 8 times by 1 test
Evaluated by:
  • kill
2-8
300 {-
301 error (0, 0, _("cannot combine signal with -l or -t"));-
302 usage (EXIT_FAILURE);-
303 }
never executed: end of block
0
304-
305 if ( ! list && argc <= optind)
! listDescription
TRUEevaluated 10 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 10 times by 1 test
Evaluated by:
  • kill
argc <= optindDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • kill
FALSEevaluated 8 times by 1 test
Evaluated by:
  • kill
2-10
306 {-
307 error (0, 0, _("no process ID specified"));-
308 usage (EXIT_FAILURE);-
309 }
never executed: end of block
0
310-
311 return (list
executed 18 times by 1 test: return (list ? list_signals (table, optind < argc ? argv + optind : ((void *)0) ) : send_signals (signum, argv + optind));
Executed by:
  • kill
18
312 ? list_signals (table, optind < argc ? argv + optind : NULL)
executed 18 times by 1 test: return (list ? list_signals (table, optind < argc ? argv + optind : ((void *)0) ) : send_signals (signum, argv + optind));
Executed by:
  • kill
18
313 : send_signals (signum, argv + optind));
executed 18 times by 1 test: return (list ? list_signals (table, optind < argc ? argv + optind : ((void *)0) ) : send_signals (signum, argv + optind));
Executed by:
  • kill
18
314}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2