-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowsig.c
61 lines (51 loc) · 1.64 KB
/
showsig.c
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
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
// http://web.theurbanpenguin.com/adding-color-to-your-output-from-c/
char* color = "\033[0m";
char* nocolor = "\33[0m";
pid_t pid;
void sig_handler(int signo)
{
char * str = strsignal(signo);
printf("%sPID: %i %2d -> SIG%s%s\n", color, pid, signo, str, nocolor);
};
char *setColor (char* color_str)
{
int i = 0;
while(color_str[i]) {
color_str[i] = tolower(color_str[i]);
i++;
}
if (strcmp(color_str, "red") == 0) return ("\33[0;31m");
if (strcmp(color_str, "green") == 0) return ("\33[0;32m");
if (strcmp(color_str, "yellow") == 0) return ("\33[0;33m");
if (strcmp(color_str, "blue") == 0) return ("\33[0;34m");
if (strcmp(color_str, "magenta") == 0) return ("\33[0;35m");
if (strcmp(color_str, "cyan") == 0) return ("\33[0;36m");
printf ("Available Colors for Output: red, green yello, blue, magenta, cyan\n\n");
return ("\033[0m");
};
int main(int argc, char *argv[])
{
if (argc > 1)
{
color = setColor (argv[1]);
}
int sig;
pid = getpid();
for (sig = 1; sig < NSIG; sig++)
{
if (signal(sig, sig_handler) == SIG_ERR)
printf("%sPID: %i Can't catch %i -> %s%s\n", color, pid, sig, strsignal(sig), nocolor);
}
//printf ("%sPID: %i Get a list of available signals with \"kill -l\"%s\n", color, pid, nocolor);
printf ("%sPID: %i Send signals to PID = %d%s\n", color, pid, pid, nocolor) ;
printf ("%sPID: %i Starting listener%s\n", color, pid, nocolor);
while(1)
sleep(1);
return 0;
}