Properly implemented "kill -s"
This commit is contained in:
parent
e80a1ce6c7
commit
28dacb966b
40
src/kill.c
40
src/kill.c
|
@ -20,6 +20,9 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
/* kill */
|
/* kill */
|
||||||
|
@ -93,6 +96,9 @@ _kill_names[] =
|
||||||
static int _kill(int sig, int argc, char * argv[]);
|
static int _kill(int sig, int argc, char * argv[]);
|
||||||
static int _kill_list(int argc, char * argv[]);
|
static int _kill_list(int argc, char * argv[]);
|
||||||
|
|
||||||
|
static int _error(char const * message, int ret);
|
||||||
|
static int _usage(void);
|
||||||
|
|
||||||
|
|
||||||
/* functions */
|
/* functions */
|
||||||
/* kill */
|
/* kill */
|
||||||
|
@ -110,14 +116,11 @@ static int _kill(int sig, int argc, char * argv[])
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s%s%s", "kill: ", argv[i],
|
fprintf(stderr, "%s%s%s", "kill: ", argv[i],
|
||||||
": Invalid process number\n");
|
": Invalid process number\n");
|
||||||
|
ret |= 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(kill(pid, sig) != 0)
|
if(kill(pid, sig) != 0)
|
||||||
{
|
ret |= _error(strerror(errno), 1);
|
||||||
fputs("kill: ", stderr);
|
|
||||||
perror("kill");
|
|
||||||
ret |= 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -134,6 +137,14 @@ static int _kill_list(int argc, char * argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* error */
|
||||||
|
static int _error(char const * message, int ret)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: %s\n", "kill", message);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* usage */
|
/* usage */
|
||||||
static int _usage(void)
|
static int _usage(void)
|
||||||
{
|
{
|
||||||
|
@ -151,7 +162,6 @@ int main(int argc, char * argv[])
|
||||||
int sig = SIGTERM;
|
int sig = SIGTERM;
|
||||||
int o;
|
int o;
|
||||||
int list = 0;
|
int list = 0;
|
||||||
char * p;
|
|
||||||
|
|
||||||
while((o = getopt(argc, argv, "ls:")) != -1)
|
while((o = getopt(argc, argv, "ls:")) != -1)
|
||||||
{
|
{
|
||||||
|
@ -161,10 +171,20 @@ int main(int argc, char * argv[])
|
||||||
list = 1;
|
list = 1;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
/* FIXME signal_name expected, NaN... */
|
if(strcmp(optarg, "0") == 0)
|
||||||
sig = strtol(optarg, &p, 10);
|
{
|
||||||
if(*optarg == '\0' || *p != '\0')
|
sig = 0;
|
||||||
return _usage();
|
break;
|
||||||
|
}
|
||||||
|
for(o = 0; _kill_names[o].name != NULL; o++)
|
||||||
|
if(strcasecmp(&_kill_names[o].name[3],
|
||||||
|
optarg) == 0)
|
||||||
|
{
|
||||||
|
sig = _kill_names[o].signal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(_kill_names[o].name == NULL)
|
||||||
|
return _error("Unknown signal", 2);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return _usage();
|
return _usage();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user