1 | /* |
---|
2 | * sigd - print names of signals we get |
---|
3 | * [it's not a real deamon-- so sue me. =)] |
---|
4 | */ |
---|
5 | |
---|
6 | #include <signal.h> |
---|
7 | #include <malloc.h> |
---|
8 | #include "signames.h" |
---|
9 | |
---|
10 | int restart_sig; |
---|
11 | int exit_sig; |
---|
12 | |
---|
13 | FILE **logfiles = NULL; |
---|
14 | char **names = NULL; |
---|
15 | |
---|
16 | FILE *def_lf[2]; |
---|
17 | |
---|
18 | void sig_handler(int signum) |
---|
19 | { |
---|
20 | char *name = signal_name(signum); |
---|
21 | char **n; |
---|
22 | FILE **f; |
---|
23 | int exiting = (signum == exit_sig); |
---|
24 | |
---|
25 | signal(signum, sig_handler); |
---|
26 | |
---|
27 | /* using printf in signal handlers is bad, but I really don't want to deal |
---|
28 | with raw write()'s right now, and this is only a test anyway. */ |
---|
29 | |
---|
30 | for (f = logfiles; f && *f; f++) { |
---|
31 | if (name) |
---|
32 | fprintf(*f, "%s got a SIG%s\n", |
---|
33 | exiting? "[exiting] " : "[pre-restart]", |
---|
34 | name); |
---|
35 | else |
---|
36 | fprintf(*f, "%s got signal %d\n", |
---|
37 | exiting? "[exiting] " : "[pre-restart]", |
---|
38 | signum); |
---|
39 | } |
---|
40 | |
---|
41 | if (exiting) |
---|
42 | exit(0); |
---|
43 | |
---|
44 | if (names) { |
---|
45 | /* restart */ |
---|
46 | for (f = logfiles, n = names; n && *n; f++, n++) { |
---|
47 | if (**n) |
---|
48 | *f = freopen( *n, "a", *f ); |
---|
49 | if (! *f) abort(); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | for (f = logfiles; f && *f; f++) { |
---|
54 | if (name) |
---|
55 | fprintf(*f, "[post-restart] got a SIG%s\n", name); |
---|
56 | else |
---|
57 | fprintf(*f, "[post-restart] got signal %d\n", signum); |
---|
58 | } |
---|
59 | |
---|
60 | for (f = logfiles; f && *f; f++) |
---|
61 | fflush(*f); |
---|
62 | } |
---|
63 | |
---|
64 | void open_files (char **names) |
---|
65 | { |
---|
66 | int count, i; |
---|
67 | char **n; |
---|
68 | |
---|
69 | /* assume logfiles==NULL */ |
---|
70 | |
---|
71 | for (n = names, count = 0; n && *n; n++, count++) ; |
---|
72 | logfiles = (FILE **) malloc((count+1)*sizeof(FILE*)); |
---|
73 | |
---|
74 | for (i=0; i<count; i++) { |
---|
75 | logfiles[i] = fopen( names[i], "a" ); |
---|
76 | if (! logfiles[i]) abort(); |
---|
77 | } |
---|
78 | logfiles[count] = NULL; |
---|
79 | } |
---|
80 | |
---|
81 | void main(int argc, char **argv) |
---|
82 | { |
---|
83 | if (argc < 3) exit(100); |
---|
84 | |
---|
85 | if ((restart_sig = signal_number( argv[1] )) <= 0) |
---|
86 | exit(101); |
---|
87 | |
---|
88 | if ((exit_sig = signal_number( argv[2] )) <= 0) |
---|
89 | exit(102); |
---|
90 | |
---|
91 | if (argc == 3) { |
---|
92 | names = NULL; |
---|
93 | def_lf[0] = stdout; |
---|
94 | def_lf[1] = NULL; |
---|
95 | logfiles = def_lf; |
---|
96 | } else { |
---|
97 | names = (argv + 3); |
---|
98 | open_files( names ); |
---|
99 | } |
---|
100 | |
---|
101 | signal(restart_sig, sig_handler); |
---|
102 | signal(exit_sig, sig_handler); |
---|
103 | |
---|
104 | while(1) { |
---|
105 | sleep(3600); |
---|
106 | } |
---|
107 | } |
---|