summaryrefslogtreecommitdiff
path: root/src/kill.c
blob: d0c8ea84f3ea67b46fd8c9ba47527db40865463d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/* kill -- send a signal to a process
   Copyright (C) 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* Written by Marcus Brinkmann.  */

#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>

#include <signal.h>

#include "system.h"
#include "closeout.h"
#include "human.h"
#include "error.h"
#include "xstrtol.h"

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "kill"

#define AUTHORS "Marcus Brinkmann"


/* An invalid signal number.  */
#define NO_SIG -1

/* A structure holding the number and the name of a signal.  */
struct sigspec
{
  int signum;
  char *signame;
};

/* The list of signals was taken from bash 2.05.  The best name for a
   signal comes after any possible alias, so it is read it from back
   to front.  This is why the terminating null entry comes first.  */
static struct sigspec sigspecs[] =
{
  { NO_SIG, NULL },

  /* Null is used to test for the existance and ownership of a PID.  */
  { 0, "0" },

/* AIX */
#if defined (SIGLOST)	/* resource lost (eg, record-lock lost) */
  { SIGLOST, "LOST" },
#endif

#if defined (SIGMSG)	/* HFT input data pending */
  { SIGMSG, "MSG" },
#endif

#if defined (SIGDANGER)	/* system crash imminent */
  { SIGDANGER, "DANGER" },
#endif

#if defined (SIGMIGRATE) /* migrate process to another CPU */
  { SIGMIGRATE, "MIGRATE" },
#endif

#if defined (SIGPRE)	/* programming error */
  { SIGPRE, "PRE" },
#endif

#if defined (SIGVIRT)	/* AIX virtual time alarm */
  { SIGVIRT, "VIRT" },
#endif

#if defined (SIGALRM1)	/* m:n condition variables */
  { SIGALRM1, "ALRM1" },
#endif

#if defined (SIGWAITING)	/* m:n scheduling */
  { SIGWAITING, "WAITING" },
#endif

#if defined (SIGGRANT)	/* HFT monitor mode granted */
  { SIGGRANT, "GRANT" },
#endif

#if defined (SIGKAP)	/* keep alive poll from native keyboard */
  { SIGKAP, "KAP" },
#endif

#if defined (SIGRETRACT) /* HFT monitor mode retracted */
  { SIGRETRACT, "RETRACT" },
#endif

#if defined (SIGSOUND)	/* HFT sound sequence has completed */
  { SIGSOUND, "SOUND" },
#endif

#if defined (SIGSAK)	/* Secure Attention Key */
  { SIGSAK, "SAK" },
#endif

/* SunOS5 */
#if defined (SIGLWP)	/* special signal used by thread library */
  { SIGLWP, "LWP" },
#endif

#if defined (SIGFREEZE)	/* special signal used by CPR */
  { SIGFREEZE, "FREEZE" },
#endif

#if defined (SIGTHAW)	/* special signal used by CPR */
  { SIGTHAW, "THAW" },
#endif

#if defined (SIGCANCEL)	/* thread cancellation signal used by libthread */
  { SIGCANCEL, "CANCEL" },
#endif

/* HP-UX */
#if defined (SIGDIL)	/* DIL signal (?) */
  { SIGDIL, "DIL" },
#endif

/* System V */
#if defined (SIGCLD)	/* Like SIGCHLD.  */
  { SIGCLD, "CLD" },
#endif

#if defined (SIGPWR)	/* power state indication */
  { SIGPWR, "PWR" },
#endif

#if defined (SIGPOLL)	/* Pollable event (for streams)  */
  { SIGPOLL, "POLL" },
#endif

/* Unknown */
#if defined (SIGWINDOW)
  { SIGWINDOW, "WINDOW" },
#endif

/* Common */
#if defined (SIGHUP)	/* hangup */
  { SIGHUP, "HUP" },
#endif

#if defined (SIGINT)	/* interrupt */
  { SIGINT, "INT" },
#endif

#if defined (SIGQUIT)	/* quit */
  { SIGQUIT, "QUIT" },
#endif

#if defined (SIGILL)	/* illegal instruction (not reset when caught) */
  { SIGILL, "ILL" },
#endif

#if defined (SIGTRAP)	/* trace trap (not reset when caught) */
  { SIGTRAP, "TRAP" },
#endif

#if defined (SIGIOT)	/* IOT instruction */
  { SIGIOT, "IOT" },
#endif

#if defined (SIGABRT)	/* Cause current process to dump core. */
  { SIGABRT, "ABRT" },
#endif

#if defined (SIGEMT)	/* EMT instruction */
  { SIGEMT, "EMT" },
#endif

#if defined (SIGFPE)	/* floating point exception */
  { SIGFPE, "FPE" },
#endif

#if defined (SIGKILL)	/* kill (cannot be caught or ignored) */
  { SIGKILL, "KILL" },
#endif

#if defined (SIGBUS)	/* bus error */
  { SIGBUS, "BUS" },
#endif

#if defined (SIGSEGV)	/* segmentation violation */
  { SIGSEGV, "SEGV" },
#endif

#if defined (SIGSYS)	/* bad argument to system call */
  { SIGSYS, "SYS" },
#endif

#if defined (SIGPIPE)	/* write on a pipe with no one to read it */
  { SIGPIPE, "PIPE" },
#endif

#if defined (SIGALRM)	/* alarm clock */
  { SIGALRM, "ALRM" },
#endif

#if defined (SIGTERM)	/* software termination signal from kill */
  { SIGTERM, "TERM" },
#endif

#if defined (SIGURG)	/* urgent condition on IO channel */
  { SIGURG, "URG" },
#endif

#if defined (SIGSTOP)	/* sendable stop signal not from tty */
  { SIGSTOP, "STOP" },
#endif

#if defined (SIGTSTP)	/* stop signal from tty */
  { SIGTSTP, "TSTP" },
#endif

#if defined (SIGCONT)	/* continue a stopped process */
  { SIGCONT, "CONT" },
#endif

#if defined (SIGCHLD)	/* to parent on child stop or exit */
  { SIGCHLD, "CHLD" },
#endif

#if defined (SIGTTIN)	/* to readers pgrp upon background tty read */
  { SIGTTIN, "TTIN" },
#endif

#if defined (SIGTTOU)	/* like TTIN for output if (tp->t_local&LTOSTOP) */
  { SIGTTOU, "TTOU" },
#endif

#if defined (SIGIO)	/* input/output possible signal */
  { SIGIO, "IO" },
#endif

#if defined (SIGXCPU)	/* exceeded CPU time limit */
  { SIGXCPU, "XCPU" },
#endif

#if defined (SIGXFSZ)	/* exceeded file size limit */
  { SIGXFSZ, "XFSZ" },
#endif

#if defined (SIGVTALRM)	/* virtual time alarm */
  { SIGVTALRM, "VTALRM" },
#endif

#if defined (SIGPROF)	/* profiling time alarm */
  { SIGPROF, "PROF" },
#endif

#if defined (SIGWINCH)	/* window changed */
  { SIGWINCH, "WINCH" },
#endif

/* 4.4 BSD */
#if defined (SIGINFO) && !defined (_SEQUENT_)	/* information request */
  { SIGINFO, "INFO" },
#endif

#if defined (SIGUSR1)	/* user defined signal 1 */
  { SIGUSR1, "USR1" },
#endif

#if defined (SIGUSR2)	/* user defined signal 2 */
  { SIGUSR2, "USR2" },
#endif

#if defined (SIGKILLTHR)	/* BeOS: Kill Thread */
  { SIGKILLTHR, "KILLTHR" }
#endif
};

/* The last entry of the complete signal list, including real time
   signals.  */
struct sigspec *sigspecs_last;

/* The number of sigspecs in the list.  */
int sigspecs_size;

/* The number of digits in NSIG (approx.).  */
int nsig_digits;


/* The name this program was run with, for error messages.  */
char *program_name;

/* All options which require an argument are known to the
   pre-scan loop in main().  */
#define OPT_SIGSPEC_LONG "sigspec"
#define OPT_SIGNUM_LONG "signum"

static struct option const long_options[] =
{
  {"list", no_argument, NULL, 'l'},
  {"long-list", no_argument, NULL, 'L'},
  {OPT_SIGSPEC_LONG, required_argument, NULL, 's'},
  {OPT_SIGNUM_LONG, required_argument, NULL, 'n'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {NULL, 0, NULL, 0}
};

void
usage (int status)
{
  if (status != 0)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
	     program_name);
  else
    {
      printf (_("\
Usage: %s [-s SIGSPEC | -n SIGNUM | -SIGSPEC] PID ...\n\
  or:  %s -l [SIGSPEC] ...\n\
"),
	      program_name, program_name);
      fputs (_("\
Send the signal named by SIGSPEC or SIGNUM to processes named by PID.\n\
\n\
  -s, --" OPT_SIGSPEC_LONG " SIGSPEC     name or number of signal to be sent\n\
  -n, --" OPT_SIGNUM_LONG " SIGNUM       number of signal to be sent\n\
\n\
  -l, --list                list the signal names\n\
  -L, --long-list           list the signal names with their numbers\n\
\n\
"), stdout);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      fputs (_("\
\n\
kill returns true if at least one signal was successfully sent, or\n\
false if an error occurs or an invalid option is encountered.\n\
"), stdout);
      puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
    }
  exit (status);
}


/* The function is called once before option parsing.  It calculates
   the maximum number of digits in a signum, adds the realtime signal
   specs to the signal list, and adds all signals of the form -SIGTERM
   and -TERM as possible options.  */
static void
initialize (void)
{
  char number[LONGEST_HUMAN_READABLE + 1];
  struct sigspec *newspecs;

  nsig_digits = strlen (human_readable ((uintmax_t) NSIG, number, 1, 1));

  sigspecs_size = sizeof (sigspecs) / sizeof (struct sigspec);

#if defined (SIGRTMIN) || defined (SIGRTMAX)
  /* POSIX 1003.1b-1993 defines real time signals.  The following code
     is so convoluted because it also takes care of incomplete
     implementations.  */
# ifndef SIGRTMIN
#  define SIGRTMIN SIGRTMAX
# endif
# ifndef SIGRTMAX
#  define SIGRTMAX SIGRTMIN
# endif

  /* Sanity check.  */
  if (SIGRTMAX >= SIGRTMIN)
    {
      int rtsigc = SIGRTMAX - SIGRTMIN + 1;
      int i;
      /* Account for "RTMIN+" resp "RTMAX-", the number and '\0'.  */
      int maxlength = nsig_digits + 6 + 1;

      newspecs = xmalloc (sizeof (struct sigspec)
			  * (sigspecs_size + rtsigc));

      /* After this, newspecs will always point to the last element of
	 the array.  */
      newspecs->signum = NO_SIG;
      newspecs->signame = NULL;

      (++newspecs)->signum = SIGRTMAX;
      newspecs->signame = "RTMAX";
      if (rtsigc > 1)
	{
	  (++newspecs)->signum = SIGRTMIN;
	  newspecs->signame = "RTMIN";
	}

      /* Create new elements for all missing realtime signals.  */
      for (i = 0; i < rtsigc - 2; i++)
	{
	  (++newspecs)->signum = SIGRTMIN + 1 + i;
	  newspecs->signame = xmalloc (maxlength);

	  snprintf (newspecs->signame, maxlength, "%s%d",
		    (i < (rtsigc - 2)/2
		     ? "RTMIN+" : "RTMAX-"),
		    (i < (rtsigc - 2)/2
		     ? i + 1 : (rtsigc - 2) - i));
	}

      /* Copy the existing elements in the following space.  */
      for (i = 1; i < sigspecs_size; i++)
	*(++newspecs) = sigspecs[i];

      sigspecs_last = newspecs;
      sigspecs_size += rtsigc;
    }
#else
  sigspecs_last = sigspecs + (sigspecs_size - 1);
#endif
}


typedef enum { LIST_NONE, LIST_FLAT, LIST_PRETTY } list_t;

/* Print out a table listing all signals specifications with their
   preferred name.  */
static void
list_signals (list_t type)
{
  int i = 0;
  int entrylen = 0;
  int unsorted;
  int entries;
  int last_signum = NO_SIG;
  int column = 0;
  struct sigspec *spec = sigspecs_last;
  struct sigspec **specs = xmalloc (sizeof (struct sigspec *)
				    * (sigspecs_size - 1));


  /* Gather maximum name length and prepare sort array.  Note that the
     list is reversed in the array.  This is taken into account by the
     output routine below.  */
  while (spec->signum != NO_SIG)
    {
      specs[i++] = spec;
      if (spec->signame && strlen (spec->signame) > entrylen)
	entrylen = strlen (spec->signame);
      spec--;
    }

  /* Sort the array by signal number.  This is a simple bubble sort,
     but the point is that the order of entries with the same signum
     is presevered (otherwise the preferred alias is lost).  */
  if (sigspecs_size > 2)
    do
      {
	unsorted = 0;

	for (i = 0; i < sigspecs_size - 2; i++)
	  {
	    if (specs[i]->signum > specs[i+1]->signum)
	      {
		struct sigspec *saved = specs[i];
		specs[i] = specs[i+1];
		specs[i+1] = saved;
		unsorted = 1;
	      }
	  }
      }
    while (unsorted);

  /* Account for "NR) NAME ".  Calculate for 79 columns, the 80 takes
     into account that the last entry is not followed by a space.  */
  entrylen += nsig_digits + 2 + 1;
  entries = 80 / entrylen;
  if (entries < 1)
    entries = 1;

  for (i = 0; i < sigspecs_size - 1; i++)
    {
      /* Skip duplicated signal numbers, signums without name and the
	 special signal number `0'.  */
      if (specs[i]->signame && specs[i]->signum
	  && (last_signum == NO_SIG || last_signum != specs[i]->signum))
	{
	  switch (type)
	    {
	    case LIST_PRETTY:
	      column++;
	      printf ("%*i) %-*s", nsig_digits, specs[i]->signum,
		      entrylen - nsig_digits - 2 + (column != entries ? 1 : 0),
		      specs[i]->signame);
	      if (column == entries - 1)
		{
		  column = 0;
		  putchar ('\n');
		}
	      break;
	    case LIST_FLAT:
	      column += printf ("%s%s",
				(column == 0 ? ""
				 : (column + strlen (specs[i]->signame) > 78
				    ? "\n" : " ")),
				specs[i]->signame);
	      if (column > 79)
		column = strlen (specs[i]->signame);
	      break;
	    default:
	      break;
	    }
	  last_signum = specs[i]->signum;
	}
      if (i == sigspecs_size - 2)
	putchar ('\n');
    }
}


/* Turn a string into a signal number, or a number into a signal
   number.  If STRING is "2", or "INT", then return the integer 2.
   Return NO_SIG if STRING doesn't contain a valid signal
   descriptor.  */
static int
decode_signal (char const *sigspec)
{
  struct sigspec const *spec = sigspecs_last;
  long l;

  if (xstrtol (sigspec, NULL, 0, &l, "") == LONGINT_OK)
    {
      int sig = l;

      if (sig != l)
	return NO_SIG;

      while (spec->signum != NO_SIG && spec->signum != sig)
	spec--;
      return spec->signum;
    }

  /* A leading `SIG' may be omitted. */
  while (spec->signum != NO_SIG)
    {
      if (spec->signame
	  && (strcasecmp (sigspec, spec->signame) == 0
	      || (strncasecmp (sigspec, "SIG", 3) == 0
		  && strcasecmp (sigspec + 3, spec->signame) == 0)))
	return (spec->signum);
      spec--;
    }
  return NO_SIG;
}

/* Find the preferred name for the signal with the number SIGNUM.  */
static char const *const
name_signal (int signum)
{
  struct sigspec const *spec = sigspecs_last;

  while (spec->signum != NO_SIG && spec->signum != signum)
    spec--;
  return spec->signame;
}

/* Send the signal SIGNUM to process PID, using kill ().  If an error
   occurs, it is reported and passed through to the caller.  */
static int
send_signal (pid_t pid, int signum)
{
  int err;

  err = kill (pid, signum);
  if (err)
    {
      uintmax_t nr = (uintmax_t) (pid < 0 ? -pid : pid);
      char number[LONGEST_HUMAN_READABLE + 1];

      error (0, errno, "(%s%s)", (pid < 0 ? "-" : ""),
	     human_readable (nr, number, 1, 1));
    }
  return err;
}


int
main (int argc, char **argv)
{
  int optc;
  int err = 0;
  int success = 0;
  list_t list = LIST_NONE;
  int sig_num = NO_SIG;
  int i;
  char **extra_opt;
  int extra_opt_size = 0;

  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdout);

  initialize ();

  extra_opt = xmalloc ((argc - 1) * sizeof (char *));
  for (i = 1; i < argc; i++)
    {
      intmax_t dummy;

      /* getopt will ignore everything following `--', so we do as
         well.  */
      if (!strcmp ("--", argv[i]))
	break;

      /* Skip this argument if it doesn't look like an option.  */
      if (argv[i][0] != '-')
	continue;

      /* Skip it if it follows an option requiring an argument.  */
      if (i > 1 && argv[i-1][0] == '-')
	{
	  /* A short option that doesn't look like -nTERM.  */
	  if ((argv[i-1][1] == 'n' || argv[i-1][1] == 's')
	      && argv[i-1][2] == '\0')
	    continue;

	  /* A long option (not `--', which was excluded above).  */
	  if (argv[i-1][1] == '-'
	      && (!strncmp (OPT_SIGNUM_LONG, &(argv[i-1][2]),
			    strlen (&argv[i-1][2]))
		  || !strncmp (OPT_SIGSPEC_LONG, &(argv[i-1][2]),
			       strlen (&argv[i-1][2]))))
	    continue;
	}

      /* At this point we know that this argument is not argument to
	 another option, and that it starts with `-' but is not `--'.  */

      if (decode_signal (&(argv[i][1])) != NO_SIG
	  || xstrtoimax (argv[i], NULL, 10, &dummy, "") == LONGINT_OK)
	{
	  /* It is either a valid signal specifier or potentially a
	     valid process group.  So remember it and make getopt not
	     care about it.  */
	  extra_opt[extra_opt_size++] = argv[i];
	  argv[i][0] = 'X';
	}
    }

  while ((optc = getopt_long (argc, argv, "s:n:lL", long_options, NULL))
	 != -1)
    switch (optc)
      {
      case 's':
      case 'n':
	if (sig_num != NO_SIG)
	  {
	    error (0, 0, _("%s: only one signal specififier allowed"), optarg);
	    usage (1);
	  }
	sig_num = decode_signal (optarg);
	if (sig_num == NO_SIG)
	  error (1, 0, _("%s: invalid signal specifier"), optarg);
	break;
      case 'l':
	list = LIST_FLAT;
	break;
      case 'L':
	list = LIST_PRETTY;
	break;
      case_GETOPT_HELP_CHAR;
      case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
      default:
	usage (1);
      }

  argc -= optind;
  argv += optind;

  for (i = 0; i < extra_opt_size; i++)
    extra_opt[i][0] = '-';

  if (extra_opt_size > 0 && sig_num == NO_SIG)
    {
      char **arg = argv;

      /* Find the first extra option collected in the remaining
	 argument list and if necessary, replace it with the first
	 remaining argument.  This is a precaution in case getopt()
	 mangles the order of non-option arguments.  */

      while (*arg && *arg != extra_opt[0])
	arg++;
      if (*arg && *arg != argv[0])
	*arg = argv[0];

      argc--;
      argv++;

      /* Interpret the first one as a signal specifier.  */
      sig_num = decode_signal (&(extra_opt[0][1]));
      if (sig_num == NO_SIG)
	error (1, 0, _("%s: invalid signal specifier"), extra_opt[0]);
    }

  if (!list && sig_num == NO_SIG)
    sig_num = SIGTERM;

  if (argc == 0)
    {
      if (list)
	list_signals (list);
      else
	{
	  error (0, 0, _("too few arguments"));
	  usage (1);
	}
    }
  else
    {
      int j;
      for (j = 0; j < argc; j++)
	if (list)
	  {
	    int signum = decode_signal (argv[j]);
	    if (signum == NO_SIG)
	      {
		error (0, 0, _("%s: invalid signal specifier"), argv[j]);
		err = 1;
	      }
	    else
	      {
		char const *const name = name_signal (signum);
		printf ("%s\n", name ? name : "(unknown)");
		success = 1;
	      }
	  }
	else
	  {
	    intmax_t nr;
	    pid_t pid;
	    int inval = 0;

	    if (xstrtoimax (argv[j], NULL, 10, &nr, "") != LONGINT_OK)
	      inval = 1;
	    pid = (pid_t) nr;
	    if (inval || pid != nr)
	      {
		error (0, 0, _("%s: invalid process id"), argv[j]);
		err = 1;
		continue;
	      }
	    if (send_signal (pid, sig_num))
	      err = 1;
	    else
	      success = 1;
	  }
    }

  exit ((success || !err) ? 0 : 1);
}