Imported from ../bash-3.1.tar.gz.
[platform/upstream/bash.git] / builtins / kill.def
index 4392717..bedbb1a 100644 (file)
@@ -1,7 +1,7 @@
 This file is kill.def, from which is created kill.c.
 It implements the builtin "kill" in Bash.
 
-Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+Copyright (C) 1987-2005 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -23,9 +23,8 @@ $PRODUCES kill.c
 
 $BUILTIN kill
 $FUNCTION kill_builtin
-$DEPENDS_ON JOB_CONTROL
-$SHORT_DOC kill [-s sigspec | -n signum | -sigspec] [pid | job]... or kill -l [sigspec]
-Send the processes named by PID (or JOB) the signal SIGSPEC.  If
+$SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
+Send the processes named by PID (or JOBSPEC) the signal SIGSPEC.  If
 SIGSPEC is not present, then SIGTERM is assumed.  An argument of `-l'
 lists the signal names; if arguments follow `-l' they are assumed to
 be signal numbers for which names should be listed.  Kill is a shell
@@ -46,6 +45,7 @@ $END
 #endif
 
 #include "../bashansi.h"
+#include "../bashintl.h"
 
 #include "../shell.h"
 #include "../trap.h"
@@ -57,11 +57,10 @@ $END
 extern int errno;
 #endif /* !errno */
 
-#if defined (JOB_CONTROL)
-extern int interactive;
-extern int job_control;
 extern int posixly_correct;
 
+static void kill_error __P((pid_t, int));
+
 #if !defined (CONTINUE_AFTER_KILL_ERROR)
 #  define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
 #else
@@ -75,9 +74,10 @@ int
 kill_builtin (list)
      WORD_LIST *list;
 {
-  int signal, any_succeeded, listing, saw_signal;
+  int sig, any_succeeded, listing, saw_signal, dflags;
   char *sigspec, *word;
   pid_t pid;
+  intmax_t pid_value;
 
   if (list == 0)
     {
@@ -86,9 +86,10 @@ kill_builtin (list)
     }
 
   any_succeeded = listing = saw_signal = 0;
-  signal = SIGTERM;
+  sig = SIGTERM;
   sigspec = "TERM";
 
+  dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0);
   /* Process options. */
   while (list)
     {
@@ -106,14 +107,14 @@ kill_builtin (list)
            {
              sigspec = list->word->word;
              if (sigspec[0] == '0' && sigspec[1] == '\0')
-               signal = 0;
+               sig = 0;
              else
-               signal = decode_signal (sigspec);
+               sig = decode_signal (sigspec, dflags);
              list = list->next;
            }
          else
            {
-             builtin_error ("%s requires an argument", word);
+             sh_needarg (word);
              return (EXECUTION_FAILURE);
            }
        }
@@ -133,7 +134,7 @@ kill_builtin (list)
       else if ((*word == '-') && !saw_signal)
        {
          sigspec = word + 1;
-         signal = decode_signal (sigspec);
+         sig = decode_signal (sigspec, dflags);
          saw_signal++;
          list = list->next;
        }
@@ -145,9 +146,9 @@ kill_builtin (list)
     return (display_signal_list (list, 0));
 
   /* OK, we are killing processes. */
-  if (signal == NO_SIG)
+  if (sig == NO_SIG)
     {
-      builtin_error ("bad signal spec `%s'", sigspec);
+      sh_invalidsig (sigspec);
       return (EXECUTION_FAILURE);
     }
 
@@ -164,63 +165,70 @@ kill_builtin (list)
       if (*word == '-')
        word++;
 
-      if (*word && all_digits (word))
+      /* Use the entire argument in case of minus sign presence. */
+      if (*word && legal_number (list->word->word, &pid_value) && (pid_value == (pid_t)pid_value))
        {
-         /* Use the entire argument in case of minus sign presence. */
-         pid = (pid_t) atoi (list->word->word);
+         pid = (pid_t) pid_value;
 
-         if (kill_pid (pid, signal, 0) < 0)
-           goto signal_error;
+         if (kill_pid (pid, sig, pid < -1) < 0)
+           {
+             if (errno == EINVAL)
+               sh_invalidsig (sigspec);
+             else
+               kill_error (pid, errno);
+             CONTINUE_OR_FAIL;
+           }
          else
            any_succeeded++;
        }
+#if defined (JOB_CONTROL)
       else if (*list->word->word && *list->word->word != '%')
        {
-         builtin_error ("%s: no such pid", list->word->word);
+         builtin_error (_("%s: arguments must be process or job IDs"), list->word->word);
          CONTINUE_OR_FAIL;
        }
-      else if (*word && (interactive || job_control))
+      else if (*word)
        /* Posix.2 says you can kill without job control active (4.32.4) */
        {                       /* Must be a job spec.  Check it out. */
          int job;
          sigset_t set, oset;
+         JOB *j;
 
          BLOCK_CHILD (set, oset);
          job = get_job_spec (list);
 
-         if (job < 0 || job >= job_slots || !jobs[job])
+         if (INVALID_JOB (job))
            {
              if (job != DUP_JOB)
-               builtin_error ("%s: no such job", list->word->word);
+               sh_badjob (list->word->word);
              UNBLOCK_CHILD (oset);
              CONTINUE_OR_FAIL;
            }
 
+         j = get_job_by_jid (job);
          /* Job spec used.  Kill the process group. If the job was started
             without job control, then its pgrp == shell_pgrp, so we have
             to be careful.  We take the pid of the first job in the pipeline
             in that case. */
-         pid = IS_JOBCONTROL (job) ? jobs[job]->pgrp : jobs[job]->pipe->pid;
+         pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid;
 
          UNBLOCK_CHILD (oset);
 
-         if (kill_pid (pid, signal, 1) < 0)
+         if (kill_pid (pid, sig, 1) < 0)
            {
-           signal_error:
-             if (errno == EPERM)
-               builtin_error ("(%d) - Not owner", (int)pid);
-             else if (errno == ESRCH)
-               builtin_error ("(%d) - No such pid", (int)pid);
+             if (errno == EINVAL)
+               sh_invalidsig (sigspec);
              else
-               builtin_error ("Invalid signal %d", signal);
+               kill_error (pid, errno);
              CONTINUE_OR_FAIL;
            }
          else
            any_succeeded++;
        }
+#endif /* !JOB_CONTROL */
       else
        {
-         builtin_error ("`%s': not a pid or valid job spec", list->word->word);
+         sh_badpid (list->word->word);
          CONTINUE_OR_FAIL;
        }
     continue_killing:
@@ -229,4 +237,16 @@ kill_builtin (list)
 
   return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
 }
-#endif /* JOB_CONTROL */
+
+static void
+kill_error (pid, e)
+     pid_t pid;
+     int e;
+{
+  char *x;
+
+  x = strerror (e);
+  if (x == 0)
+    x = _("Unknown error");
+  builtin_error ("(%ld) - %s", (long)pid, x);
+}