diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-20 17:27:40 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-20 17:27:40 +0000 |
commit | 45946f8b513d9c292613ac08c3ddf4a89b915752 (patch) | |
tree | d6ea51887431e4261a114b95989950d1973d3227 /runit/runsvdir.c | |
parent | 63db27f9f4c0ec8b9abe3c32c8d699be0781ffd6 (diff) | |
download | busybox-w32-45946f8b513d9c292613ac08c3ddf4a89b915752.tar.gz busybox-w32-45946f8b513d9c292613ac08c3ddf4a89b915752.tar.bz2 busybox-w32-45946f8b513d9c292613ac08c3ddf4a89b915752.zip |
runit/*: get rid of tai[a] time abstraction, it's too bloaty.
text data bss dec hex filename
772537 1058 11092 784687 bf92f busybox.t0/busybox
772459 1058 11060 784577 bf8c1 busybox.t1/busybox
772326 1058 11028 784412 bf81c busybox.t2/busybox
772158 1058 10980 784196 bf744 busybox.t3/busybox
771490 1055 10988 783533 bf4ad busybox.t4/busybox
Diffstat (limited to 'runit/runsvdir.c')
-rw-r--r-- | runit/runsvdir.c | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/runit/runsvdir.c b/runit/runsvdir.c index 8db0fc189..38da7f8ae 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c | |||
@@ -35,26 +35,25 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
35 | 35 | ||
36 | #define MAXSERVICES 1000 | 36 | #define MAXSERVICES 1000 |
37 | 37 | ||
38 | struct service { | ||
39 | dev_t dev; | ||
40 | ino_t ino; | ||
41 | pid_t pid; | ||
42 | smallint isgone; | ||
43 | }; | ||
44 | |||
45 | struct service *sv; | ||
38 | static char *svdir; | 46 | static char *svdir; |
39 | static unsigned long dev; | ||
40 | static unsigned long ino; | ||
41 | static struct service { | ||
42 | unsigned long dev; | ||
43 | unsigned long ino; | ||
44 | int pid; | ||
45 | int isgone; | ||
46 | } *sv; | ||
47 | static int svnum; | 47 | static int svnum; |
48 | static int check = 1; | ||
49 | static char *rplog; | 48 | static char *rplog; |
50 | static int rploglen; | 49 | static int rploglen; |
51 | static int logpipe[2]; | 50 | static int logpipe[2]; |
52 | static iopause_fd io[1]; | 51 | static struct pollfd pfd[1]; |
53 | static struct taia stamplog; | 52 | static unsigned stamplog; |
54 | static int exitsoon; | 53 | static smallint check = 1; |
55 | static int pgrp; | 54 | static smallint exitsoon; |
55 | static smallint set_pgrp; | ||
56 | 56 | ||
57 | #define usage() bb_show_usage() | ||
58 | static void fatal2_cannot(const char *m1, const char *m2) | 57 | static void fatal2_cannot(const char *m1, const char *m2) |
59 | { | 58 | { |
60 | bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2); | 59 | bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2); |
@@ -84,25 +83,26 @@ static void s_hangup(int sig_no) | |||
84 | 83 | ||
85 | static void runsv(int no, const char *name) | 84 | static void runsv(int no, const char *name) |
86 | { | 85 | { |
87 | int pid = fork(); | 86 | pid_t pid; |
87 | char *prog[3]; | ||
88 | |||
89 | prog[0] = (char*)"runsv"; | ||
90 | prog[1] = (char*)name; | ||
91 | prog[2] = NULL; | ||
92 | |||
93 | pid = vfork(); | ||
88 | 94 | ||
89 | if (pid == -1) { | 95 | if (pid == -1) { |
90 | warn2_cannot("fork for ", name); | 96 | warn2_cannot("vfork", ""); |
91 | return; | 97 | return; |
92 | } | 98 | } |
93 | if (pid == 0) { | 99 | if (pid == 0) { |
94 | /* child */ | 100 | /* child */ |
95 | char *prog[3]; | 101 | if (set_pgrp) |
96 | |||
97 | prog[0] = (char*)"runsv"; | ||
98 | prog[1] = (char*)name; | ||
99 | prog[2] = NULL; | ||
100 | if (pgrp) | ||
101 | setsid(); | 102 | setsid(); |
102 | signal(SIGHUP, SIG_DFL); | 103 | signal(SIGHUP, SIG_DFL); |
103 | signal(SIGTERM, SIG_DFL); | 104 | signal(SIGTERM, SIG_DFL); |
104 | BB_EXECVP(prog[0], prog); | 105 | execvp(prog[0], prog); |
105 | //pathexec_run(*prog, prog, (char* const*)environ); | ||
106 | fatal2_cannot("start runsv ", name); | 106 | fatal2_cannot("start runsv ", name); |
107 | } | 107 | } |
108 | sv[no].pid = pid; | 108 | sv[no].pid = pid; |
@@ -124,13 +124,15 @@ static void runsvdir(void) | |||
124 | sv[i].isgone = 1; | 124 | sv[i].isgone = 1; |
125 | errno = 0; | 125 | errno = 0; |
126 | while ((d = readdir(dir))) { | 126 | while ((d = readdir(dir))) { |
127 | if (d->d_name[0] == '.') continue; | 127 | if (d->d_name[0] == '.') |
128 | continue; | ||
128 | if (stat(d->d_name, &s) == -1) { | 129 | if (stat(d->d_name, &s) == -1) { |
129 | warn2_cannot("stat ", d->d_name); | 130 | warn2_cannot("stat ", d->d_name); |
130 | errno = 0; | 131 | errno = 0; |
131 | continue; | 132 | continue; |
132 | } | 133 | } |
133 | if (!S_ISDIR(s.st_mode)) continue; | 134 | if (!S_ISDIR(s.st_mode)) |
135 | continue; | ||
134 | for (i = 0; i < svnum; i++) { | 136 | for (i = 0; i < svnum; i++) { |
135 | if ((sv[i].ino == s.st_ino) && (sv[i].dev == s.st_dev)) { | 137 | if ((sv[i].ino == s.st_ino) && (sv[i].dev == s.st_dev)) { |
136 | sv[i].isgone = 0; | 138 | sv[i].isgone = 0; |
@@ -152,8 +154,8 @@ static void runsvdir(void) | |||
152 | memset(&sv[i], 0, sizeof(sv[i])); | 154 | memset(&sv[i], 0, sizeof(sv[i])); |
153 | sv[i].ino = s.st_ino; | 155 | sv[i].ino = s.st_ino; |
154 | sv[i].dev = s.st_dev; | 156 | sv[i].dev = s.st_dev; |
155 | //sv[i].pid = 0; | 157 | /*sv[i].pid = 0;*/ |
156 | //sv[i].isgone = 0; | 158 | /*sv[i].isgone = 0;*/ |
157 | runsv(i, d->d_name); | 159 | runsv(i, d->d_name); |
158 | check = 1; | 160 | check = 1; |
159 | } | 161 | } |
@@ -196,9 +198,9 @@ static int setup_log(void) | |||
196 | warnx("cannot set filedescriptor for log"); | 198 | warnx("cannot set filedescriptor for log"); |
197 | return -1; | 199 | return -1; |
198 | } | 200 | } |
199 | io[0].fd = logpipe[0]; | 201 | pfd[0].fd = logpipe[0]; |
200 | io[0].events = IOPAUSE_READ; | 202 | pfd[0].events = POLLIN; |
201 | taia_now(&stamplog); | 203 | stamplog = monotonic_sec(); |
202 | return 1; | 204 | return 1; |
203 | } | 205 | } |
204 | 206 | ||
@@ -206,24 +208,28 @@ int runsvdir_main(int argc, char **argv); | |||
206 | int runsvdir_main(int argc, char **argv) | 208 | int runsvdir_main(int argc, char **argv) |
207 | { | 209 | { |
208 | struct stat s; | 210 | struct stat s; |
209 | time_t mtime = 0; | 211 | dev_t last_dev = last_dev; /* for gcc */ |
212 | ino_t last_ino = last_ino; /* for gcc */ | ||
213 | time_t last_mtime = 0; | ||
210 | int wstat; | 214 | int wstat; |
211 | int curdir; | 215 | int curdir; |
212 | int pid; | 216 | int pid; |
213 | struct taia deadline; | 217 | unsigned deadline; |
214 | struct taia now; | 218 | unsigned now; |
215 | struct taia stampcheck; | 219 | unsigned stampcheck; |
216 | char ch; | 220 | char ch; |
217 | int i; | 221 | int i; |
218 | 222 | ||
219 | argv++; | 223 | argv++; |
220 | if (!argv || !*argv) usage(); | 224 | if (!*argv) |
221 | if (**argv == '-') { | 225 | bb_show_usage(); |
222 | switch (*(*argv + 1)) { | 226 | if (argv[0][0] == '-') { |
223 | case 'P': pgrp = 1; | 227 | switch (argv[0][1]) { |
228 | case 'P': set_pgrp = 1; | ||
224 | case '-': ++argv; | 229 | case '-': ++argv; |
225 | } | 230 | } |
226 | if (!argv || !*argv) usage(); | 231 | if (!*argv) |
232 | bb_show_usage(); | ||
227 | } | 233 | } |
228 | 234 | ||
229 | sig_catch(SIGTERM, s_term); | 235 | sig_catch(SIGTERM, s_term); |
@@ -241,13 +247,14 @@ int runsvdir_main(int argc, char **argv) | |||
241 | fatal2_cannot("open current directory", ""); | 247 | fatal2_cannot("open current directory", ""); |
242 | coe(curdir); | 248 | coe(curdir); |
243 | 249 | ||
244 | taia_now(&stampcheck); | 250 | stampcheck = monotonic_sec(); |
245 | 251 | ||
246 | for (;;) { | 252 | for (;;) { |
247 | /* collect children */ | 253 | /* collect children */ |
248 | for (;;) { | 254 | for (;;) { |
249 | pid = wait_nohang(&wstat); | 255 | pid = wait_nohang(&wstat); |
250 | if (pid <= 0) break; | 256 | if (pid <= 0) |
257 | break; | ||
251 | for (i = 0; i < svnum; i++) { | 258 | for (i = 0; i < svnum; i++) { |
252 | if (pid == sv[i].pid) { | 259 | if (pid == sv[i].pid) { |
253 | /* runsv has gone */ | 260 | /* runsv has gone */ |
@@ -258,31 +265,23 @@ int runsvdir_main(int argc, char **argv) | |||
258 | } | 265 | } |
259 | } | 266 | } |
260 | 267 | ||
261 | taia_now(&now); | 268 | now = monotonic_sec(); |
262 | if (now.sec.x < (stampcheck.sec.x - 3)) { | 269 | if ((int)(now - stampcheck) >= 0) { |
263 | /* time warp */ | ||
264 | warnx("time warp: resetting time stamp"); | ||
265 | taia_now(&stampcheck); | ||
266 | taia_now(&now); | ||
267 | if (rplog) taia_now(&stamplog); | ||
268 | } | ||
269 | if (taia_less(&now, &stampcheck) == 0) { | ||
270 | /* wait at least a second */ | 270 | /* wait at least a second */ |
271 | taia_uint(&deadline, 1); | 271 | stampcheck = now + 1; |
272 | taia_add(&stampcheck, &now, &deadline); | ||
273 | 272 | ||
274 | if (stat(svdir, &s) != -1) { | 273 | if (stat(svdir, &s) != -1) { |
275 | if (check || s.st_mtime != mtime | 274 | if (check || s.st_mtime != last_mtime |
276 | || s.st_ino != ino || s.st_dev != dev | 275 | || s.st_ino != last_ino || s.st_dev != last_dev |
277 | ) { | 276 | ) { |
278 | /* svdir modified */ | 277 | /* svdir modified */ |
279 | if (chdir(svdir) != -1) { | 278 | if (chdir(svdir) != -1) { |
280 | mtime = s.st_mtime; | 279 | last_mtime = s.st_mtime; |
281 | dev = s.st_dev; | 280 | last_dev = s.st_dev; |
282 | ino = s.st_ino; | 281 | last_ino = s.st_ino; |
283 | check = 0; | 282 | check = 0; |
284 | if (now.sec.x <= (4611686018427387914ULL + (uint64_t)mtime)) | 283 | //if (now <= mtime) |
285 | sleep(1); | 284 | // sleep(1); |
286 | runsvdir(); | 285 | runsvdir(); |
287 | while (fchdir(curdir) == -1) { | 286 | while (fchdir(curdir) == -1) { |
288 | warn2_cannot("change directory, pausing", ""); | 287 | warn2_cannot("change directory, pausing", ""); |
@@ -296,29 +295,30 @@ int runsvdir_main(int argc, char **argv) | |||
296 | } | 295 | } |
297 | 296 | ||
298 | if (rplog) { | 297 | if (rplog) { |
299 | if (taia_less(&now, &stamplog) == 0) { | 298 | if ((int)(now - stamplog) >= 0) { |
300 | write(logpipe[1], ".", 1); | 299 | write(logpipe[1], ".", 1); |
301 | taia_uint(&deadline, 900); | 300 | stamplog = now + 900; |
302 | taia_add(&stamplog, &now, &deadline); | ||
303 | } | 301 | } |
304 | } | 302 | } |
305 | taia_uint(&deadline, check ? 1 : 5); | 303 | deadline = now + (check ? 1 : 5); |
306 | taia_add(&deadline, &now, &deadline); | ||
307 | 304 | ||
305 | pfd[0].revents = 0; | ||
308 | sig_block(SIGCHLD); | 306 | sig_block(SIGCHLD); |
309 | if (rplog) | 307 | if (rplog) |
310 | iopause(io, 1, &deadline, &now); | 308 | poll(pfd, 1, deadline*1000); |
311 | else | 309 | else |
312 | iopause(0, 0, &deadline, &now); | 310 | sleep(deadline); |
313 | sig_unblock(SIGCHLD); | 311 | sig_unblock(SIGCHLD); |
314 | 312 | ||
315 | if (rplog && (io[0].revents | IOPAUSE_READ)) | 313 | if (pfd[0].revents & POLLIN) { |
316 | while (read(logpipe[0], &ch, 1) > 0) | 314 | while (read(logpipe[0], &ch, 1) > 0) { |
317 | if (ch) { | 315 | if (ch) { |
318 | for (i = 6; i < rploglen; i++) | 316 | for (i = 6; i < rploglen; i++) |
319 | rplog[i-1] = rplog[i]; | 317 | rplog[i-1] = rplog[i]; |
320 | rplog[rploglen-1] = ch; | 318 | rplog[rploglen-1] = ch; |
321 | } | 319 | } |
320 | } | ||
321 | } | ||
322 | 322 | ||
323 | switch (exitsoon) { | 323 | switch (exitsoon) { |
324 | case 1: | 324 | case 1: |