master xplshn/aruu / cmd / posix / sh / kill.c
  1/*-
  2 * SPDX-License-Identifier: BSD-3-Clause
  3 *
  4 * Copyright (c) 1988, 1993, 1994
  5 *	The Regents of the University of California.  All rights reserved.
  6 *
  7 * Redistribution and use in source and binary forms, with or without
  8 * modification, are permitted provided that the following conditions
  9 * are met:
 10 * 1. Redistributions of source code must retain the above copyright
 11 *    notice, this list of conditions and the following disclaimer.
 12 * 2. Redistributions in binary form must reproduce the above copyright
 13 *    notice, this list of conditions and the following disclaimer in the
 14 *    documentation and/or other materials provided with the distribution.
 15 * 3. Neither the name of the University nor the names of its contributors
 16 *    may be used to endorse or promote products derived from this software
 17 *    without specific prior written permission.
 18 *
 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 29 * SUCH DAMAGE.
 30 */
 31/*
 32 * Important: This file is used both as a standalone program /bin/kill and
 33 * as a builtin for /bin/sh (#define SHELL).
 34 */
 35
 36#include "sig.h"
 37
 38#ifdef SHELL
 39#define main killcmd
 40#include "bltin.h"
 41#endif
 42
 43#ifndef __dead2
 44#define __dead2 __attribute__((__noreturn__))
 45#endif
 46
 47#include <ctype.h>
 48#ifndef SHELL
 49#include <err.h>
 50#endif
 51#include <errno.h>
 52#include <signal.h>
 53#include <stdio.h>
 54#include <stdlib.h>
 55#include <string.h>
 56
 57static void nosig(const char *);
 58static void printsignals(FILE *);
 59static void usage(void) __dead2;
 60
 61int
 62main(int argc, char *argv[])
 63{
 64  char  signame[SIG2STR_MAX];
 65  long  pidl;
 66  pid_t pid;
 67  int   errors, numsig, ret;
 68  char *ep;
 69
 70  if (argc < 2)
 71    usage();
 72
 73  numsig = SIGTERM;
 74
 75  argc--, argv++;
 76  if (!strcmp(*argv, "-l")) {
 77    argc--, argv++;
 78    if (argc > 1)
 79      usage();
 80    if (argc == 1) {
 81      if (!isdigit(**argv))
 82        usage();
 83      numsig = strtol(*argv, &ep, 10);
 84      if (!**argv || *ep)
 85        errx(2, "invalid signal number: %s", *argv);
 86      if (numsig >= 128)
 87        numsig -= 128;
 88      if (sig2str(numsig, signame) < 0)
 89        nosig(*argv);
 90      printf("%s\n", signame);
 91      return (0);
 92    }
 93    printsignals(stdout);
 94    return (0);
 95  }
 96
 97  if (!strcmp(*argv, "-s")) {
 98    argc--, argv++;
 99    if (argc < 1) {
100      warnx("option requires an argument -- s");
101      usage();
102    }
103    if (strcmp(*argv, "0") == 0)
104      numsig = 0;
105    else if (str2sig(*argv, &numsig) < 0)
106      nosig(*argv);
107    argc--, argv++;
108  } else if (**argv == '-' && *(*argv + 1) != '-') {
109    ++*argv;
110    if (strcmp(*argv, "0") == 0)
111      numsig = 0;
112    else if (str2sig(*argv, &numsig) < 0)
113      nosig(*argv);
114    argc--, argv++;
115  }
116
117  if (argc > 0 && strncmp(*argv, "--", 2) == 0)
118    argc--, argv++;
119
120  if (argc == 0)
121    usage();
122
123  for (errors = 0; argc; argc--, argv++) {
124#ifdef SHELL
125    if (**argv == '%')
126      ret = killjob(*argv, numsig);
127    else
128#endif
129    {
130      pidl = strtol(*argv, &ep, 10);
131      /* Check for overflow of pid_t. */
132      pid = (pid_t)pidl;
133      if (!**argv || *ep || pid != pidl)
134        errx(2, "illegal process id: %s", *argv);
135      ret = kill(pid, numsig);
136    }
137    if (ret == -1) {
138      warn("%s", *argv);
139      errors = 1;
140    }
141  }
142
143  return (errors);
144}
145
146static void
147nosig(const char *name)
148{
149  warnx("unknown signal %s; valid signals:", name);
150  printsignals(stderr);
151#ifdef SHELL
152  error(NULL);
153#else
154  exit(2);
155#endif
156}
157
158static void
159printsignals(FILE *fp)
160{
161  int n;
162
163  for (n = 1; n < sys_nsig; n++) {
164    (void)fprintf(fp, "%s", sys_signame[n]);
165    if (n == (sys_nsig / 2) || n == (sys_nsig - 1))
166      (void)fprintf(fp, "\n");
167    else
168      (void)fprintf(fp, " ");
169  }
170}
171
172static void
173usage(void)
174{
175  (void)fprintf(
176      stderr,
177      "%s\n%s\n%s\n%s\n",
178      "usage: kill [-s signal_name] pid ...",
179      "       kill -l [exit_status]",
180      "       kill -signal_name pid ...",
181      "       kill -signal_number pid ..."
182  );
183#ifdef SHELL
184  error(NULL);
185#else
186  exit(2);
187#endif
188}