author | viric@llimona |
Fri, 16 Jun 2006 18:45:24 +0200 | |
changeset 62 | 39bf7ecd7b21 |
parent 51 | a01abd65856a |
permissions | -rw-r--r-- |
49 | 1 |
#include <sys/types.h> |
2 |
#include <unistd.h> |
|
3 |
#include <errno.h> |
|
4 |
#include <signal.h> |
|
5 |
#include <string.h> |
|
6 |
#include <stdio.h> |
|
7 |
#include <sys/wait.h> // Per wait |
|
8 |
#include <assert.h> // Pels assert |
|
9 |
||
10 |
#include "syslog.h" |
|
11 |
||
12 |
/* Variables */ |
|
50
6958ce45aa83
Changing to static an internal module variable.
viric@llimona
parents:
49
diff
changeset
|
13 |
static int childs_alive = 0; |
49 | 14 |
|
15 |
static int child[MAX_CHILDS]; |
|
16 |
||
17 |
||
18 |
/* Prototypes */ |
|
19 |
static void remove_child(const int pid); |
|
20 |
||
21 |
/* Code */ |
|
22 |
void term_childs() |
|
23 |
{ |
|
24 |
int res; |
|
25 |
int i; |
|
26 |
||
27 |
sigset_t tmp, normal; |
|
28 |
||
29 |
/* block the child signals */ |
|
30 |
sigemptyset(&tmp); |
|
31 |
sigaddset(&tmp, SIGCHLD); |
|
32 |
sigprocmask(SIG_BLOCK, &tmp, &normal); |
|
33 |
||
34 |
/* Send SIGTERM */ |
|
35 |
for(i=0;i<childs_alive;i++) |
|
36 |
{ |
|
37 |
res = kill(child[i], SIGTERM); |
|
51
a01abd65856a
The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
50
diff
changeset
|
38 |
fprintf(stderr, "Sent SIGTERM to %i\n", child[i]); |
49 | 39 |
/* Accepting the res == -1 && errno == ESRCH saves us |
40 |
* from the race condition possible kill error */ |
|
41 |
if (res == -1) |
|
42 |
assert( errno == ESRCH ); |
|
43 |
} |
|
44 |
||
45 |
/* restore the mask */ |
|
46 |
sigprocmask(SIG_SETMASK, &normal, NULL); |
|
47 |
||
48 |
} |
|
49 |
||
50 |
void wait_childs_die() |
|
51 |
{ |
|
52 |
/* Wait for the end */ |
|
53 |
while(childs_alive > 0) |
|
54 |
{ |
|
55 |
sleep(1); |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
void child_handler(int parameter) |
|
60 |
{ |
|
61 |
pid_t pid; |
|
62 |
int status; |
|
63 |
||
64 |
do |
|
65 |
{ |
|
66 |
pid = waitpid(-1, &status, WNOHANG); |
|
67 |
if (pid == -1) |
|
68 |
{ |
|
69 |
if(errno == ECHILD) |
|
70 |
{ |
|
71 |
/* We don't have more childs! */ |
|
72 |
fprintf(stderr, "We don't have more childs.\n"); |
|
73 |
childs_alive = 0; |
|
74 |
break; |
|
75 |
} else |
|
76 |
{ |
|
77 |
fprintf(stderr, "waitpid failed. %s\n", |
|
78 |
strerror(errno)); |
|
79 |
break; |
|
80 |
} |
|
81 |
} |
|
82 |
/* Debug */ |
|
83 |
if (pid > 0) |
|
84 |
{ |
|
85 |
fprintf(stderr, "Child %i died: 0x%08x\n", |
|
86 |
pid, status); |
|
87 |
remove_child(pid); |
|
88 |
} |
|
89 |
} while(pid > 0); |
|
90 |
} |
|
91 |
||
92 |
int child_fork() |
|
93 |
{ |
|
94 |
int pid; |
|
95 |
sigset_t tmp, normal; |
|
96 |
||
97 |
/* block the child signals */ |
|
98 |
sigemptyset(&tmp); |
|
99 |
sigaddset(&tmp, SIGCHLD); |
|
100 |
sigprocmask(SIG_BLOCK, &tmp, &normal); |
|
101 |
||
102 |
||
103 |
if (childs_alive < MAX_CHILDS) |
|
104 |
{ |
|
105 |
pid = fork(); |
|
106 |
} |
|
107 |
else |
|
108 |
{ |
|
109 |
pid = -1; |
|
110 |
} |
|
111 |
||
112 |
if (pid > 0) |
|
113 |
{ |
|
114 |
/* Parent */ |
|
115 |
||
116 |
/* MUTEX-ed actions with the SIGCHLD handler */ |
|
117 |
child[childs_alive++] = pid; |
|
118 |
} |
|
119 |
||
120 |
/* restore the mask */ |
|
121 |
sigprocmask(SIG_SETMASK, &normal, NULL); |
|
122 |
||
123 |
return pid; |
|
124 |
} |
|
125 |
||
126 |
/* This will be called only from the SIGCHLD handler */ |
|
127 |
static void remove_child(const int pid) |
|
128 |
{ |
|
129 |
int i; |
|
130 |
||
131 |
/* We update the child status */ |
|
132 |
for(i=0;i<childs_alive;i++) |
|
133 |
{ |
|
134 |
if(child[i] == pid) |
|
135 |
break; |
|
136 |
} |
|
137 |
||
138 |
for(i = i; i < (childs_alive-1); i++) |
|
139 |
{ |
|
140 |
child[i] = child[i+1]; |
|
141 |
} |
|
142 |
||
143 |
childs_alive--; |
|
144 |
} |
|
145 |
||
146 |
int get_childs_alive() |
|
147 |
{ |
|
148 |
int num; |
|
149 |
sigset_t tmp, normal; |
|
150 |
||
151 |
/* block the child signals */ |
|
152 |
sigemptyset(&tmp); |
|
153 |
sigaddset(&tmp, SIGCHLD); |
|
154 |
sigprocmask(SIG_BLOCK, &tmp, &normal); |
|
155 |
||
156 |
num = childs_alive; |
|
157 |
||
158 |
/* restore the mask */ |
|
159 |
sigprocmask(SIG_SETMASK, &normal, NULL); |
|
160 |
||
161 |
return num; |
|
162 |
} |