| Line | Source | Count |
| 1 | This file is kill.def, from which is created kill.c. | - |
| 2 | It implements the builtin "kill" in Bash. | - |
| 3 | | - |
| 4 | Copyright (C) 1987-2015 Free Software Foundation, Inc. | - |
| 5 | | - |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. | - |
| 7 | | - |
| 8 | Bash is free software: you can redistribute it and/or modify | - |
| 9 | it under the terms of the GNU General Public License as published by | - |
| 10 | the Free Software Foundation, either version 3 of the License, or | - |
| 11 | (at your option) any later version. | - |
| 12 | | - |
| 13 | Bash is distributed in the hope that it will be useful, | - |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - |
| 16 | GNU General Public License for more details. | - |
| 17 | | - |
| 18 | You should have received a copy of the GNU General Public License | - |
| 19 | along with Bash. If not, see <http: | - |
| 20 | | - |
| 21 | $PRODUCES kill.c | - |
| 22 | | - |
| 23 | $BUILTIN kill | - |
| 24 | $FUNCTION kill_builtin | - |
| 25 | $SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] | - |
| 26 | Send a signal to a job. | - |
| 27 | | - |
| 28 | Send the processes identified by PID or JOBSPEC the signal named by | - |
| 29 | SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then | - |
| 30 | SIGTERM is assumed. | - |
| 31 | | - |
| 32 | Options: | - |
| 33 | -s sig SIG is a signal name | - |
| 34 | -n sig SIG is a signal number | - |
| 35 | -l list the signal names; if arguments follow `-l' they are | - |
| 36 | assumed to be signal numbers for which names should be listed | - |
| 37 | -L synonym for -l | - |
| 38 | | - |
| 39 | Kill is a shell builtin for two reasons: it allows job IDs to be used | - |
| 40 | instead of process IDs, and allows processes to be killed if the limit | - |
| 41 | on processes that you can create is reached. | - |
| 42 | | - |
| 43 | Exit Status: | - |
| 44 | Returns success unless an invalid option is given or an error occurs. | - |
| 45 | $END | - |
| 46 | | - |
| 47 | #include <config.h> | - |
| 48 | | - |
| 49 | #include <stdio.h> | - |
| 50 | #include <errno.h> | - |
| 51 | #if defined (HAVE_UNISTD_H) | - |
| 52 | # ifdef _MINIX | - |
| 53 | # include <sys/types.h> | - |
| 54 | # endif | - |
| 55 | # include <unistd.h> | - |
| 56 | #endif | - |
| 57 | | - |
| 58 | #include "../bashansi.h" | - |
| 59 | #include "../bashintl.h" | - |
| 60 | | - |
| 61 | #include <signal.h> | - |
| 62 | | - |
| 63 | #include "../shell.h" | - |
| 64 | #include "../trap.h" | - |
| 65 | #include "../jobs.h" | - |
| 66 | #include "common.h" | - |
| 67 | | - |
| 68 | | - |
| 69 | #if !defined (errno) | - |
| 70 | extern int errno; | - |
| 71 | #endif /* !errno */ | - |
| 72 | | - |
| 73 | static void kill_error __P((pid_t, int)); | - |
| 74 | | - |
| 75 | #if !defined (CONTINUE_AFTER_KILL_ERROR) | - |
| 76 | # define CONTINUE_OR_FAIL return (EXECUTION_FAILURE) | - |
| 77 | #else | - |
| 78 | # define CONTINUE_OR_FAIL goto continue_killing | - |
| 79 | #endif /* CONTINUE_AFTER_KILL_ERROR */ | - |
| 80 | | - |
| 81 | | - |
| 82 | | - |
| 83 | | - |
| 84 | int | - |
| 85 | kill_builtin (list) | - |
| 86 | WORD_LIST *list; | - |
| 87 | { | - |
| 88 | int sig, any_succeeded, listing, saw_signal, dflags; | - |
| 89 | char *sigspec, *word; | - |
| 90 | pid_t pid; | - |
| 91 | intmax_t pid_value; | - |
| 92 | | - |
| 93 | if (list == 0)| TRUE | never evaluated | | FALSE | evaluated 26 times by 1 test |
| 0-26 |
| 94 | { | - |
| 95 | builtin_usage (); | - |
| 96 | return (EX_USAGE); never executed: return (258); | 0 |
| 97 | } | - |
| 98 | CHECK_HELPOPT (list); never executed: __result = (((const unsigned char *) (const char *) ( ((list)->word->word) ))[3] - __s2[3]); never executed: end of block never executed: end of block never executed: __result = (((const unsigned char *) (const char *) ( "--help" ))[3] - __s2[3]); never executed: end of block never executed: end of block never executed: return (258); | TRUE | evaluated 26 times by 1 test | | FALSE | never evaluated |
| TRUE | evaluated 26 times by 1 test | | FALSE | never evaluated |
| TRUE | evaluated 24 times by 1 test | | FALSE | evaluated 2 times by 1 test |
| TRUE | never evaluated | | FALSE | evaluated 24 times by 1 test |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0-26 |
| 99 | | - |
| 100 | any_succeeded = listing = saw_signal = 0; | - |
| 101 | sig = SIGTERM; | - |
| 102 | sigspec = "TERM"; | - |
| 103 | | - |
| 104 | dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0); | - |
| 105 | | - |
| 106 | while (list)| TRUE | evaluated 46 times by 1 test | | FALSE | evaluated 3 times by 1 test |
| 3-46 |
| 107 | { | - |
| 108 | word = list->word->word; | - |
| 109 | | - |
| 110 | if (ISOPTION (word, 'l') || ISOPTION (word, 'L'))| TRUE | evaluated 24 times by 1 test | | FALSE | evaluated 22 times by 1 test |
| TRUE | evaluated 5 times by 1 test | | FALSE | evaluated 19 times by 1 test |
| TRUE | evaluated 5 times by 1 test | | FALSE | never evaluated |
| TRUE | evaluated 19 times by 1 test | | FALSE | evaluated 22 times by 1 test |
| TRUE | never evaluated | | FALSE | evaluated 19 times by 1 test |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0-24 |
| 111 | { | - |
| 112 | listing++; | - |
| 113 | list = list->next; | - |
| 114 | }executed 5 times by 1 test: end of block | 5 |
| 115 | else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))| TRUE | evaluated 19 times by 1 test | | FALSE | evaluated 22 times by 1 test |
| TRUE | evaluated 2 times by 1 test | | FALSE | evaluated 17 times by 1 test |
| TRUE | evaluated 2 times by 1 test | | FALSE | never evaluated |
| TRUE | evaluated 17 times by 1 test | | FALSE | evaluated 22 times by 1 test |
| TRUE | evaluated 6 times by 1 test | | FALSE | evaluated 11 times by 1 test |
| TRUE | evaluated 6 times by 1 test | | FALSE | never evaluated |
| 0-22 |
| 116 | { | - |
| 117 | list = list->next; | - |
| 118 | if (list)| TRUE | evaluated 7 times by 1 test | | FALSE | evaluated 1 time by 1 test |
| 1-7 |
| 119 | { | - |
| 120 | sigspec = list->word->word; | - |
| 121 | if (sigspec[0] == '0' && sigspec[1] == '\0')| TRUE | never evaluated | | FALSE | evaluated 7 times by 1 test |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0-7 |
| 122 | sig = 0; never executed: sig = 0; | 0 |
| 123 | else | - |
| 124 | sig = decode_signal (sigspec, dflags);executed 7 times by 1 test: sig = decode_signal (sigspec, dflags); | 7 |
| 125 | list = list->next; | - |
| 126 | saw_signal++; | - |
| 127 | }executed 7 times by 1 test: end of block | 7 |
| 128 | else | - |
| 129 | { | - |
| 130 | sh_needarg (word); | - |
| 131 | return (EXECUTION_FAILURE);executed 1 time by 1 test: return (1); | 1 |
| 132 | } | - |
| 133 | } | - |
| 134 | else if (ISOPTION (word, '-'))| TRUE | evaluated 11 times by 1 test | | FALSE | evaluated 22 times by 1 test |
| TRUE | never evaluated | | FALSE | evaluated 11 times by 1 test |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0-22 |
| 135 | { | - |
| 136 | list = list->next; | - |
| 137 | break; never executed: break; | 0 |
| 138 | } | - |
| 139 | else if (ISOPTION (word, '?'))| TRUE | evaluated 11 times by 1 test | | FALSE | evaluated 22 times by 1 test |
| TRUE | never evaluated | | FALSE | evaluated 11 times by 1 test |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0-22 |
| 140 | { | - |
| 141 | builtin_usage (); | - |
| 142 | return (EX_USAGE); never executed: return (258); | 0 |
| 143 | } | - |
| 144 | | - |
| 145 | | - |
| 146 | | - |
| 147 | else if (*word == '-' && saw_signal == 0)| TRUE | evaluated 11 times by 1 test | | FALSE | evaluated 22 times by 1 test |
| TRUE | evaluated 11 times by 1 test | | FALSE | never evaluated |
| 0-22 |
| 148 | { | - |
| 149 | sigspec = word + 1; | - |
| 150 | sig = decode_signal (sigspec, dflags); | - |
| 151 | saw_signal++; | - |
| 152 | list = list->next; | - |
| 153 | }executed 11 times by 1 test: end of block | 11 |
| 154 | else | - |
| 155 | break;executed 22 times by 1 test: break; | 22 |
| 156 | } | - |
| 157 | | - |
| 158 | if (listing)| TRUE | evaluated 5 times by 1 test | | FALSE | evaluated 20 times by 1 test |
| 5-20 |
| 159 | return (display_signal_list (list, 0));executed 5 times by 1 test: return (display_signal_list (list, 0)); | 5 |
| 160 | | - |
| 161 | | - |
| 162 | if (sig == NO_SIG)| TRUE | evaluated 1 time by 1 test | | FALSE | evaluated 19 times by 1 test |
| 1-19 |
| 163 | { | - |
| 164 | sh_invalidsig (sigspec); | - |
| 165 | return (EXECUTION_FAILURE);executed 1 time by 1 test: return (1); | 1 |
| 166 | } | - |
| 167 | | - |
| 168 | if (list == 0)| TRUE | evaluated 1 time by 1 test | | FALSE | evaluated 18 times by 1 test |
| 1-18 |
| 169 | { | - |
| 170 | builtin_usage (); | - |
| 171 | return (EX_USAGE);executed 1 time by 1 test: return (258); | 1 |
| 172 | } | - |
| 173 | | - |
| 174 | while (list)| TRUE | evaluated 19 times by 1 test | | FALSE | evaluated 18 times by 1 test |
| 18-19 |
| 175 | { | - |
| 176 | word = list->word->word; | - |
| 177 | | - |
| 178 | if (*word == '-')| TRUE | never evaluated | | FALSE | evaluated 19 times by 1 test |
| 0-19 |
| 179 | word++; never executed: word++; | 0 |
| 180 | | - |
| 181 | | - |
| 182 | if (*word && legal_number (list->word->word, &pid_value) && (pid_value == (pid_t)pid_value))| TRUE | evaluated 18 times by 1 test | | FALSE | evaluated 1 time by 1 test |
| TRUE | evaluated 10 times by 1 test | | FALSE | evaluated 8 times by 1 test |
| TRUE | evaluated 10 times by 1 test | | FALSE | never evaluated |
| 0-18 |
| 183 | { | - |
| 184 | pid = (pid_t) pid_value; | - |
| 185 | | - |
| 186 | if (kill_pid (pid, sig, pid < -1) < 0)| TRUE | never evaluated | | FALSE | evaluated 10 times by 1 test |
| 0-10 |
| 187 | { | - |
| 188 | if (errno == EINVAL)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 189 | sh_invalidsig (sigspec); never executed: sh_invalidsig (sigspec); | 0 |
| 190 | else | - |
| 191 | kill_error (pid, errno); never executed: kill_error (pid, (*__errno_location ()) ); | 0 |
| 192 | CONTINUE_OR_FAIL; never executed: goto continue_killing; | 0 |
| 193 | } | - |
| 194 | else | - |
| 195 | any_succeeded++;executed 10 times by 1 test: any_succeeded++; | 10 |
| 196 | } | - |
| 197 | #if defined (JOB_CONTROL) | - |
| 198 | else if (*list->word->word && *list->word->word != '%)| TRUE | evaluated 8 times by 1 test | | FALSE | evaluated 1 time by 1 test |
| TRUE | never evaluated | | FALSE | evaluated 8 times by 1 test |
| 0-8 |
| 199 | { | - |
| 200 | builtin_error (_("%s: arguments must be process or job IDs"), list->word->word); | - |
| 201 | CONTINUE_OR_FAIL; never executed: goto continue_killing; | 0 |
| 202 | } | - |
| 203 | else if (*word)| TRUE | evaluated 8 times by 1 test | | FALSE | evaluated 1 time by 1 test |
| 1-8 |
| 204 | | - |
| 205 | { | - |
| 206 | int job; | - |
| 207 | sigset_t set, oset; | - |
| 208 | JOB *j; | - |
| 209 | | - |
| 210 | BLOCK_CHILD (set, oset); | - |
| 211 | job = get_job_spec (list); | - |
| 212 | | - |
| 213 | if (INVALID_JOB (job))| TRUE | never evaluated | | FALSE | evaluated 8 times by 1 test |
| TRUE | never evaluated | | FALSE | evaluated 8 times by 1 test |
| TRUE | evaluated 1 time by 1 test | | FALSE | evaluated 7 times by 1 test |
| 0-8 |
| 214 | { | - |
| 215 | if (job != DUP_JOB)| TRUE | evaluated 1 time by 1 test | | FALSE | never evaluated |
| 0-1 |
| 216 | sh_badjob (list->word->word);executed 1 time by 1 test: sh_badjob (list->word->word); | 1 |
| 217 | UNBLOCK_CHILD (oset); | - |
| 218 | CONTINUE_OR_FAIL;executed 1 time by 1 test: goto continue_killing; | 1 |
| 219 | } | - |
| 220 | | - |
| 221 | j = get_job_by_jid (job); | - |
| 222 | | - |
| 223 | | - |
| 224 | | - |
| 225 | | - |
| 226 | pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid;| TRUE | evaluated 6 times by 1 test | | FALSE | evaluated 1 time by 1 test |
| 1-6 |
| 227 | | - |
| 228 | UNBLOCK_CHILD (oset); | - |
| 229 | | - |
| 230 | if (kill_pid (pid, sig, 1) < 0)| TRUE | never evaluated | | FALSE | evaluated 7 times by 1 test |
| 0-7 |
| 231 | { | - |
| 232 | if (errno == EINVAL)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 233 | sh_invalidsig (sigspec); never executed: sh_invalidsig (sigspec); | 0 |
| 234 | else | - |
| 235 | kill_error (pid, errno); never executed: kill_error (pid, (*__errno_location ()) ); | 0 |
| 236 | CONTINUE_OR_FAIL; never executed: goto continue_killing; | 0 |
| 237 | } | - |
| 238 | else | - |
| 239 | any_succeeded++;executed 7 times by 1 test: any_succeeded++; | 7 |
| 240 | } | - |
| 241 | #endif /* !JOB_CONTROL */ | - |
| 242 | else | - |
| 243 | { | - |
| 244 | sh_badpid (list->word->word); | - |
| 245 | CONTINUE_OR_FAIL;executed 1 time by 1 test: goto continue_killing; | 1 |
| 246 | } | - |
| 247 | continue_killing:code before this statement executed 17 times by 1 test: continue_killing: | 17 |
| 248 | list = list->next; | - |
| 249 | }executed 19 times by 1 test: end of block | 19 |
| 250 | | - |
| 251 | return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);executed 18 times by 1 test: return (any_succeeded ? 0 : 1); | 18 |
| 252 | } | - |
| 253 | | - |
| 254 | static void | - |
| 255 | kill_error (pid, e) | - |
| 256 | pid_t pid; | - |
| 257 | int e; | - |
| 258 | { | - |
| 259 | char *x; | - |
| 260 | | - |
| 261 | x = strerror (e); | - |
| 262 | if (x == 0)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 263 | x = _("Unknown error"); never executed: x = dcgettext (((void *)0), "Unknown error" , 5) ; | 0 |
| 264 | builtin_error ("(%ld) - %s", (long)pid, x); | - |
| 265 | } never executed: end of block | 0 |
| | |