progressbar.c revision 1.2 1 1.2 grant /* $NetBSD: progressbar.c,v 1.2 2003/02/12 15:18:28 grant Exp $ */
2 1.1 jhawk
3 1.1 jhawk /*-
4 1.1 jhawk * Copyright (c) 1997-2003 The NetBSD Foundation, Inc.
5 1.1 jhawk * All rights reserved.
6 1.1 jhawk *
7 1.1 jhawk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jhawk * by Luke Mewburn.
9 1.1 jhawk *
10 1.1 jhawk * This code is derived from software contributed to The NetBSD Foundation
11 1.1 jhawk * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12 1.1 jhawk * NASA Ames Research Center.
13 1.1 jhawk *
14 1.1 jhawk * Redistribution and use in source and binary forms, with or without
15 1.1 jhawk * modification, are permitted provided that the following conditions
16 1.1 jhawk * are met:
17 1.1 jhawk * 1. Redistributions of source code must retain the above copyright
18 1.1 jhawk * notice, this list of conditions and the following disclaimer.
19 1.1 jhawk * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 jhawk * notice, this list of conditions and the following disclaimer in the
21 1.1 jhawk * documentation and/or other materials provided with the distribution.
22 1.1 jhawk * 3. All advertising materials mentioning features or use of this software
23 1.1 jhawk * must display the following acknowledgement:
24 1.1 jhawk * This product includes software developed by the NetBSD
25 1.1 jhawk * Foundation, Inc. and its contributors.
26 1.1 jhawk * 4. Neither the name of The NetBSD Foundation nor the names of its
27 1.1 jhawk * contributors may be used to endorse or promote products derived
28 1.1 jhawk * from this software without specific prior written permission.
29 1.1 jhawk *
30 1.1 jhawk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 1.1 jhawk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 1.1 jhawk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 1.1 jhawk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 1.1 jhawk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 1.1 jhawk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 1.1 jhawk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 1.1 jhawk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 1.1 jhawk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.1 jhawk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.1 jhawk * POSSIBILITY OF SUCH DAMAGE.
41 1.1 jhawk */
42 1.1 jhawk
43 1.1 jhawk /*
44 1.1 jhawk * Copyright (c) 1985, 1989, 1993, 1994
45 1.1 jhawk * The Regents of the University of California. All rights reserved.
46 1.1 jhawk *
47 1.1 jhawk * Redistribution and use in source and binary forms, with or without
48 1.1 jhawk * modification, are permitted provided that the following conditions
49 1.1 jhawk * are met:
50 1.1 jhawk * 1. Redistributions of source code must retain the above copyright
51 1.1 jhawk * notice, this list of conditions and the following disclaimer.
52 1.1 jhawk * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 jhawk * notice, this list of conditions and the following disclaimer in the
54 1.1 jhawk * documentation and/or other materials provided with the distribution.
55 1.1 jhawk * 3. All advertising materials mentioning features or use of this software
56 1.1 jhawk * must display the following acknowledgement:
57 1.1 jhawk * This product includes software developed by the University of
58 1.1 jhawk * California, Berkeley and its contributors.
59 1.1 jhawk * 4. Neither the name of the University nor the names of its contributors
60 1.1 jhawk * may be used to endorse or promote products derived from this software
61 1.1 jhawk * without specific prior written permission.
62 1.1 jhawk *
63 1.1 jhawk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 jhawk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 jhawk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 jhawk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 jhawk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 jhawk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 jhawk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 jhawk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 jhawk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 jhawk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 jhawk * SUCH DAMAGE.
74 1.1 jhawk */
75 1.1 jhawk
76 1.1 jhawk #include <sys/cdefs.h>
77 1.1 jhawk #ifndef lint
78 1.2 grant __RCSID("$NetBSD: progressbar.c,v 1.2 2003/02/12 15:18:28 grant Exp $");
79 1.1 jhawk #endif /* not lint */
80 1.1 jhawk
81 1.1 jhawk /*
82 1.1 jhawk * FTP User Program -- Misc support routines
83 1.1 jhawk */
84 1.1 jhawk #include <sys/types.h>
85 1.1 jhawk #include <sys/param.h>
86 1.1 jhawk
87 1.1 jhawk #include <err.h>
88 1.1 jhawk #include <errno.h>
89 1.1 jhawk #include <signal.h>
90 1.1 jhawk #include <stdio.h>
91 1.1 jhawk #include <stdlib.h>
92 1.1 jhawk #include <time.h>
93 1.1 jhawk #include <tzfile.h>
94 1.1 jhawk #include <unistd.h>
95 1.1 jhawk
96 1.1 jhawk #include "progressbar.h"
97 1.1 jhawk
98 1.2 grant #if !defined(NO_PROGRESS)
99 1.1 jhawk /*
100 1.1 jhawk * return non-zero if we're the current foreground process
101 1.1 jhawk */
102 1.1 jhawk int
103 1.1 jhawk foregroundproc(void)
104 1.1 jhawk {
105 1.1 jhawk static pid_t pgrp = -1;
106 1.1 jhawk
107 1.1 jhawk if (pgrp == -1)
108 1.1 jhawk pgrp = getpgrp();
109 1.1 jhawk
110 1.1 jhawk return (tcgetpgrp(fileno(ttyout)) == pgrp);
111 1.1 jhawk }
112 1.2 grant #endif /* !defined(NO_PROGRESS) */
113 1.1 jhawk
114 1.1 jhawk
115 1.1 jhawk #ifndef NO_PROGRESS
116 1.1 jhawk static void updateprogressmeter(int);
117 1.1 jhawk
118 1.1 jhawk /*
119 1.1 jhawk * SIGALRM handler to update the progress meter
120 1.1 jhawk */
121 1.1 jhawk static void
122 1.1 jhawk updateprogressmeter(int dummy)
123 1.1 jhawk {
124 1.1 jhawk int oerrno = errno;
125 1.1 jhawk
126 1.1 jhawk progressmeter(0);
127 1.1 jhawk errno = oerrno;
128 1.1 jhawk }
129 1.1 jhawk #endif /* NO_PROGRESS */
130 1.1 jhawk
131 1.1 jhawk
132 1.1 jhawk /*
133 1.1 jhawk * List of order of magnitude prefixes.
134 1.1 jhawk * The last is `P', as 2^64 = 16384 Petabytes
135 1.1 jhawk */
136 1.1 jhawk static const char prefixes[] = " KMGTP";
137 1.1 jhawk
138 1.1 jhawk /*
139 1.1 jhawk * Display a transfer progress bar if progress is non-zero.
140 1.1 jhawk * SIGALRM is hijacked for use by this function.
141 1.1 jhawk * - Before the transfer, set filesize to size of file (or -1 if unknown),
142 1.1 jhawk * and call with flag = -1. This starts the once per second timer,
143 1.1 jhawk * and a call to updateprogressmeter() upon SIGALRM.
144 1.1 jhawk * - During the transfer, updateprogressmeter will call progressmeter
145 1.1 jhawk * with flag = 0
146 1.1 jhawk * - After the transfer, call with flag = 1
147 1.1 jhawk */
148 1.1 jhawk static struct timeval start;
149 1.1 jhawk static struct timeval lastupdate;
150 1.1 jhawk
151 1.1 jhawk #define BUFLEFT (sizeof(buf) - len)
152 1.1 jhawk
153 1.1 jhawk void
154 1.1 jhawk progressmeter(int flag)
155 1.1 jhawk {
156 1.1 jhawk static off_t lastsize;
157 1.1 jhawk off_t cursize;
158 1.1 jhawk struct timeval now, wait;
159 1.1 jhawk #ifndef NO_PROGRESS
160 1.1 jhawk struct timeval td;
161 1.1 jhawk off_t abbrevsize, bytespersec;
162 1.1 jhawk double elapsed;
163 1.1 jhawk int ratio, barlength, i, len, remaining;
164 1.1 jhawk
165 1.1 jhawk /*
166 1.1 jhawk * Work variables for progress bar.
167 1.1 jhawk *
168 1.1 jhawk * XXX: if the format of the progress bar changes
169 1.1 jhawk * (especially the number of characters in the
170 1.1 jhawk * `static' portion of it), be sure to update
171 1.1 jhawk * these appropriately.
172 1.1 jhawk */
173 1.1 jhawk char buf[256]; /* workspace for progress bar */
174 1.1 jhawk #define BAROVERHEAD 43 /* non `*' portion of progress bar */
175 1.1 jhawk /*
176 1.1 jhawk * stars should contain at least
177 1.1 jhawk * sizeof(buf) - BAROVERHEAD entries
178 1.1 jhawk */
179 1.1 jhawk static const char stars[] =
180 1.1 jhawk "*****************************************************************************"
181 1.1 jhawk "*****************************************************************************"
182 1.1 jhawk "*****************************************************************************";
183 1.1 jhawk
184 1.1 jhawk #endif
185 1.1 jhawk
186 1.1 jhawk if (flag == -1) {
187 1.1 jhawk (void)gettimeofday(&start, NULL);
188 1.1 jhawk lastupdate = start;
189 1.1 jhawk lastsize = restart_point;
190 1.1 jhawk }
191 1.1 jhawk
192 1.1 jhawk (void)gettimeofday(&now, NULL);
193 1.1 jhawk cursize = bytes + restart_point;
194 1.1 jhawk timersub(&now, &lastupdate, &wait);
195 1.1 jhawk if (cursize > lastsize) {
196 1.1 jhawk lastupdate = now;
197 1.1 jhawk lastsize = cursize;
198 1.1 jhawk wait.tv_sec = 0;
199 1.1 jhawk } else {
200 1.1 jhawk #ifndef STANDALONE_PROGRESS
201 1.1 jhawk if (quit_time > 0 && wait.tv_sec > quit_time) {
202 1.1 jhawk len = snprintf(buf, sizeof(buf), "\r\n%s: "
203 1.1 jhawk "transfer aborted because stalled for %lu sec.\r\n",
204 1.1 jhawk getprogname(), (unsigned long)wait.tv_sec);
205 1.1 jhawk (void)write(fileno(ttyout), buf, len);
206 1.1 jhawk (void)xsignal(SIGALRM, SIG_DFL);
207 1.1 jhawk alarmtimer(0);
208 1.1 jhawk siglongjmp(toplevel, 1);
209 1.1 jhawk }
210 1.1 jhawk #endif /* !STANDALONE_PROGRESS */
211 1.1 jhawk }
212 1.1 jhawk /*
213 1.1 jhawk * Always set the handler even if we are not the foreground process.
214 1.1 jhawk */
215 1.1 jhawk #ifdef STANDALONE_PROGRESS
216 1.1 jhawk if (progress) {
217 1.1 jhawk #else
218 1.1 jhawk if (quit_time > 0 || progress) {
219 1.1 jhawk #endif /* !STANDALONE_PROGRESS */
220 1.1 jhawk if (flag == -1) {
221 1.1 jhawk (void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
222 1.1 jhawk alarmtimer(1); /* set alarm timer for 1 Hz */
223 1.1 jhawk } else if (flag == 1) {
224 1.1 jhawk (void)xsignal(SIGALRM, SIG_DFL);
225 1.1 jhawk alarmtimer(0);
226 1.1 jhawk }
227 1.1 jhawk }
228 1.1 jhawk #ifndef NO_PROGRESS
229 1.1 jhawk if (!progress)
230 1.1 jhawk return;
231 1.1 jhawk len = 0;
232 1.1 jhawk
233 1.1 jhawk /*
234 1.1 jhawk * print progress bar only if we are foreground process.
235 1.1 jhawk */
236 1.1 jhawk if (! foregroundproc())
237 1.1 jhawk return;
238 1.1 jhawk
239 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "\r");
240 1.1 jhawk if (filesize > 0) {
241 1.1 jhawk ratio = (int)((double)cursize * 100.0 / (double)filesize);
242 1.1 jhawk ratio = MAX(ratio, 0);
243 1.1 jhawk ratio = MIN(ratio, 100);
244 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
245 1.1 jhawk
246 1.1 jhawk /*
247 1.1 jhawk * calculate the length of the `*' bar, ensuring that
248 1.1 jhawk * the number of stars won't exceed the buffer size
249 1.1 jhawk */
250 1.1 jhawk barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
251 1.1 jhawk if (barlength > 0) {
252 1.1 jhawk i = barlength * ratio / 100;
253 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
254 1.1 jhawk "|%.*s%*s|", i, stars, barlength - i, "");
255 1.1 jhawk }
256 1.1 jhawk }
257 1.1 jhawk
258 1.1 jhawk abbrevsize = cursize;
259 1.1 jhawk for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
260 1.1 jhawk abbrevsize >>= 10;
261 1.1 jhawk len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %c%c ",
262 1.1 jhawk (LLT)abbrevsize,
263 1.1 jhawk prefixes[i],
264 1.1 jhawk i == 0 ? ' ' : 'B');
265 1.1 jhawk
266 1.1 jhawk timersub(&now, &start, &td);
267 1.1 jhawk elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
268 1.1 jhawk
269 1.1 jhawk bytespersec = 0;
270 1.1 jhawk if (bytes > 0) {
271 1.1 jhawk bytespersec = bytes;
272 1.1 jhawk if (elapsed > 0.0)
273 1.1 jhawk bytespersec /= elapsed;
274 1.1 jhawk }
275 1.1 jhawk for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
276 1.1 jhawk bytespersec >>= 10;
277 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
278 1.1 jhawk " " LLFP("3") ".%02d %cB/s ",
279 1.1 jhawk (LLT)(bytespersec / 1024),
280 1.1 jhawk (int)((bytespersec % 1024) * 100 / 1024),
281 1.1 jhawk prefixes[i]);
282 1.1 jhawk
283 1.1 jhawk if (filesize > 0) {
284 1.1 jhawk if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
285 1.1 jhawk len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
286 1.1 jhawk } else if (wait.tv_sec >= STALLTIME) {
287 1.1 jhawk len += snprintf(buf + len, BUFLEFT, " - stalled -");
288 1.1 jhawk } else {
289 1.1 jhawk remaining = (int)
290 1.1 jhawk ((filesize - restart_point) / (bytes / elapsed) -
291 1.1 jhawk elapsed);
292 1.1 jhawk if (remaining >= 100 * SECSPERHOUR)
293 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
294 1.1 jhawk " --:-- ETA");
295 1.1 jhawk else {
296 1.1 jhawk i = remaining / SECSPERHOUR;
297 1.1 jhawk if (i)
298 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
299 1.1 jhawk "%2d:", i);
300 1.1 jhawk else
301 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
302 1.1 jhawk " ");
303 1.1 jhawk i = remaining % SECSPERHOUR;
304 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
305 1.1 jhawk "%02d:%02d ETA", i / 60, i % 60);
306 1.1 jhawk }
307 1.1 jhawk }
308 1.1 jhawk }
309 1.1 jhawk if (flag == 1)
310 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "\n");
311 1.1 jhawk (void)write(fileno(ttyout), buf, len);
312 1.1 jhawk
313 1.1 jhawk #endif /* !NO_PROGRESS */
314 1.1 jhawk }
315 1.1 jhawk
316 1.1 jhawk #ifndef STANDALONE_PROGRESS
317 1.1 jhawk /*
318 1.1 jhawk * Display transfer statistics.
319 1.1 jhawk * Requires start to be initialised by progressmeter(-1),
320 1.1 jhawk * direction to be defined by xfer routines, and filesize and bytes
321 1.1 jhawk * to be updated by xfer routines
322 1.1 jhawk * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
323 1.1 jhawk * instead of ttyout.
324 1.1 jhawk */
325 1.1 jhawk void
326 1.1 jhawk ptransfer(int siginfo)
327 1.1 jhawk {
328 1.1 jhawk struct timeval now, td, wait;
329 1.1 jhawk double elapsed;
330 1.1 jhawk off_t bytespersec;
331 1.1 jhawk int remaining, hh, i, len;
332 1.1 jhawk
333 1.1 jhawk char buf[256]; /* Work variable for transfer status. */
334 1.1 jhawk
335 1.1 jhawk if (!verbose && !progress && !siginfo)
336 1.1 jhawk return;
337 1.1 jhawk
338 1.1 jhawk (void)gettimeofday(&now, NULL);
339 1.1 jhawk timersub(&now, &start, &td);
340 1.1 jhawk elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
341 1.1 jhawk bytespersec = 0;
342 1.1 jhawk if (bytes > 0) {
343 1.1 jhawk bytespersec = bytes;
344 1.1 jhawk if (elapsed > 0.0)
345 1.1 jhawk bytespersec /= elapsed;
346 1.1 jhawk }
347 1.1 jhawk len = 0;
348 1.1 jhawk len += snprintf(buf + len, BUFLEFT, LLF " byte%s %s in ",
349 1.1 jhawk (LLT)bytes, bytes == 1 ? "" : "s", direction);
350 1.1 jhawk remaining = (int)elapsed;
351 1.1 jhawk if (remaining > SECSPERDAY) {
352 1.1 jhawk int days;
353 1.1 jhawk
354 1.1 jhawk days = remaining / SECSPERDAY;
355 1.1 jhawk remaining %= SECSPERDAY;
356 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
357 1.1 jhawk "%d day%s ", days, days == 1 ? "" : "s");
358 1.1 jhawk }
359 1.1 jhawk hh = remaining / SECSPERHOUR;
360 1.1 jhawk remaining %= SECSPERHOUR;
361 1.1 jhawk if (hh)
362 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
363 1.1 jhawk len += snprintf(buf + len, BUFLEFT,
364 1.1 jhawk "%02d:%02d ", remaining / 60, remaining % 60);
365 1.1 jhawk
366 1.1 jhawk for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
367 1.1 jhawk bytespersec >>= 10;
368 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %cB/s)",
369 1.1 jhawk (LLT)(bytespersec / 1024),
370 1.1 jhawk (int)((bytespersec % 1024) * 100 / 1024),
371 1.1 jhawk prefixes[i]);
372 1.1 jhawk
373 1.1 jhawk if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
374 1.1 jhawk && bytes + restart_point <= filesize) {
375 1.1 jhawk remaining = (int)((filesize - restart_point) /
376 1.1 jhawk (bytes / elapsed) - elapsed);
377 1.1 jhawk hh = remaining / SECSPERHOUR;
378 1.1 jhawk remaining %= SECSPERHOUR;
379 1.1 jhawk len += snprintf(buf + len, BUFLEFT, " ETA: ");
380 1.1 jhawk if (hh)
381 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
382 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
383 1.1 jhawk remaining / 60, remaining % 60);
384 1.1 jhawk timersub(&now, &lastupdate, &wait);
385 1.1 jhawk if (wait.tv_sec >= STALLTIME)
386 1.1 jhawk len += snprintf(buf + len, BUFLEFT, " (stalled)");
387 1.1 jhawk }
388 1.1 jhawk len += snprintf(buf + len, BUFLEFT, "\n");
389 1.1 jhawk (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
390 1.1 jhawk }
391 1.1 jhawk
392 1.1 jhawk /*
393 1.1 jhawk * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
394 1.1 jhawk */
395 1.1 jhawk void
396 1.1 jhawk psummary(int notused)
397 1.1 jhawk {
398 1.1 jhawk int oerrno = errno;
399 1.1 jhawk
400 1.1 jhawk if (bytes > 0) {
401 1.1 jhawk if (fromatty)
402 1.1 jhawk write(fileno(ttyout), "\n", 1);
403 1.1 jhawk ptransfer(1);
404 1.1 jhawk }
405 1.1 jhawk errno = oerrno;
406 1.1 jhawk }
407 1.1 jhawk #endif /* !STANDALONE_PROGRESS */
408 1.1 jhawk
409 1.1 jhawk
410 1.1 jhawk /*
411 1.1 jhawk * Set the SIGALRM interval timer for wait seconds, 0 to disable.
412 1.1 jhawk */
413 1.1 jhawk void
414 1.1 jhawk alarmtimer(int wait)
415 1.1 jhawk {
416 1.1 jhawk struct itimerval itv;
417 1.1 jhawk
418 1.1 jhawk itv.it_value.tv_sec = wait;
419 1.1 jhawk itv.it_value.tv_usec = 0;
420 1.1 jhawk itv.it_interval = itv.it_value;
421 1.1 jhawk setitimer(ITIMER_REAL, &itv, NULL);
422 1.1 jhawk }
423 1.1 jhawk
424 1.1 jhawk
425 1.1 jhawk /*
426 1.1 jhawk * Install a POSIX signal handler, allowing the invoker to set whether
427 1.1 jhawk * the signal should be restartable or not
428 1.1 jhawk */
429 1.1 jhawk sigfunc
430 1.1 jhawk xsignal_restart(int sig, sigfunc func, int restartable)
431 1.1 jhawk {
432 1.1 jhawk struct sigaction act, oact;
433 1.1 jhawk act.sa_handler = func;
434 1.1 jhawk
435 1.1 jhawk sigemptyset(&act.sa_mask);
436 1.1 jhawk #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
437 1.1 jhawk act.sa_flags = restartable ? SA_RESTART : 0;
438 1.1 jhawk #elif defined(SA_INTERRUPT) /* SunOS 4.x */
439 1.1 jhawk act.sa_flags = restartable ? 0 : SA_INTERRUPT;
440 1.1 jhawk #else
441 1.1 jhawk #error "system must have SA_RESTART or SA_INTERRUPT"
442 1.1 jhawk #endif
443 1.1 jhawk if (sigaction(sig, &act, &oact) < 0)
444 1.1 jhawk return (SIG_ERR);
445 1.1 jhawk return (oact.sa_handler);
446 1.1 jhawk }
447 1.1 jhawk
448 1.1 jhawk /*
449 1.1 jhawk * Install a signal handler with the `restartable' flag set dependent upon
450 1.1 jhawk * which signal is being set. (This is a wrapper to xsignal_restart())
451 1.1 jhawk */
452 1.1 jhawk sigfunc
453 1.1 jhawk xsignal(int sig, sigfunc func)
454 1.1 jhawk {
455 1.1 jhawk int restartable;
456 1.1 jhawk
457 1.1 jhawk /*
458 1.1 jhawk * Some signals print output or change the state of the process.
459 1.1 jhawk * There should be restartable, so that reads and writes are
460 1.1 jhawk * not affected. Some signals should cause program flow to change;
461 1.1 jhawk * these signals should not be restartable, so that the system call
462 1.1 jhawk * will return with EINTR, and the program will go do something
463 1.1 jhawk * different. If the signal handler calls longjmp() or siglongjmp(),
464 1.1 jhawk * it doesn't matter if it's restartable.
465 1.1 jhawk */
466 1.1 jhawk
467 1.1 jhawk switch(sig) {
468 1.1 jhawk #ifdef SIGINFO
469 1.1 jhawk case SIGINFO:
470 1.1 jhawk #endif
471 1.1 jhawk case SIGQUIT:
472 1.1 jhawk case SIGUSR1:
473 1.1 jhawk case SIGUSR2:
474 1.1 jhawk case SIGWINCH:
475 1.1 jhawk restartable = 1;
476 1.1 jhawk break;
477 1.1 jhawk
478 1.1 jhawk case SIGALRM:
479 1.1 jhawk case SIGINT:
480 1.1 jhawk case SIGPIPE:
481 1.1 jhawk restartable = 0;
482 1.1 jhawk break;
483 1.1 jhawk
484 1.1 jhawk default:
485 1.1 jhawk /*
486 1.1 jhawk * This is unpleasant, but I don't know what would be better.
487 1.1 jhawk * Right now, this "can't happen"
488 1.1 jhawk */
489 1.1 jhawk errx(1, "xsignal_restart called with signal %d", sig);
490 1.1 jhawk }
491 1.1 jhawk
492 1.1 jhawk return(xsignal_restart(sig, func, restartable));
493 1.1 jhawk }
494