/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1988, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/30/*31* Important: This file is used both as a standalone program /bin/kill and32* as a builtin for /bin/sh (#define SHELL).33*/3435#include <ctype.h>36#include <err.h>37#include <errno.h>38#include <signal.h>39#include <stdio.h>40#include <stdlib.h>41#include <string.h>4243#ifdef SHELL44#define main killcmd45#include "bltin/bltin.h"46#endif4748static void nosig(const char *);49static void printsignals(FILE *);50static void usage(void) __dead2;5152int53main(int argc, char *argv[])54{55char signame[SIG2STR_MAX];56long pidl;57pid_t pid;58int errors, numsig, ret;59char *ep;6061if (argc < 2)62usage();6364numsig = SIGTERM;6566argc--, argv++;67if (!strcmp(*argv, "-l")) {68argc--, argv++;69if (argc > 1)70usage();71if (argc == 1) {72if (!isdigit(**argv))73usage();74numsig = strtol(*argv, &ep, 10);75if (!**argv || *ep)76errx(2, "invalid signal number: %s", *argv);77if (numsig >= 128)78numsig -= 128;79if (sig2str(numsig, signame) < 0)80nosig(*argv);81printf("%s\n", signame);82return (0);83}84printsignals(stdout);85return (0);86}8788if (!strcmp(*argv, "-s")) {89argc--, argv++;90if (argc < 1) {91warnx("option requires an argument -- s");92usage();93}94if (strcmp(*argv, "0") == 0)95numsig = 0;96else if (str2sig(*argv, &numsig) < 0)97nosig(*argv);98argc--, argv++;99} else if (**argv == '-' && *(*argv + 1) != '-') {100++*argv;101if (strcmp(*argv, "0") == 0)102numsig = 0;103else if (str2sig(*argv, &numsig) < 0)104nosig(*argv);105argc--, argv++;106}107108if (argc > 0 && strncmp(*argv, "--", 2) == 0)109argc--, argv++;110111if (argc == 0)112usage();113114for (errors = 0; argc; argc--, argv++) {115#ifdef SHELL116if (**argv == '%')117ret = killjob(*argv, numsig);118else119#endif120{121pidl = strtol(*argv, &ep, 10);122/* Check for overflow of pid_t. */123pid = (pid_t)pidl;124if (!**argv || *ep || pid != pidl)125errx(2, "illegal process id: %s", *argv);126ret = kill(pid, numsig);127}128if (ret == -1) {129warn("%s", *argv);130errors = 1;131}132}133134return (errors);135}136137static void138nosig(const char *name)139{140141warnx("unknown signal %s; valid signals:", name);142printsignals(stderr);143#ifdef SHELL144error(NULL);145#else146exit(2);147#endif148}149150static void151printsignals(FILE *fp)152{153int n;154155for (n = 1; n < sys_nsig; n++) {156(void)fprintf(fp, "%s", sys_signame[n]);157if (n == (sys_nsig / 2) || n == (sys_nsig - 1))158(void)fprintf(fp, "\n");159else160(void)fprintf(fp, " ");161}162}163164static void165usage(void)166{167168(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",169"usage: kill [-s signal_name] pid ...",170" kill -l [exit_status]",171" kill -signal_name pid ...",172" kill -signal_number pid ...");173#ifdef SHELL174error(NULL);175#else176exit(2);177#endif178}179180181