Home | History | Annotate | Line # | Download | only in ftp
progressbar.c revision 1.3
      1  1.3  lukem /*	$NetBSD: progressbar.c,v 1.3 2003/02/28 09:53:49 lukem 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 #include <sys/cdefs.h>
     44  1.1  jhawk #ifndef lint
     45  1.3  lukem __RCSID("$NetBSD: progressbar.c,v 1.3 2003/02/28 09:53:49 lukem Exp $");
     46  1.1  jhawk #endif /* not lint */
     47  1.1  jhawk 
     48  1.1  jhawk /*
     49  1.1  jhawk  * FTP User Program -- Misc support routines
     50  1.1  jhawk  */
     51  1.1  jhawk #include <sys/types.h>
     52  1.1  jhawk #include <sys/param.h>
     53  1.1  jhawk 
     54  1.1  jhawk #include <err.h>
     55  1.1  jhawk #include <errno.h>
     56  1.1  jhawk #include <signal.h>
     57  1.1  jhawk #include <stdio.h>
     58  1.1  jhawk #include <stdlib.h>
     59  1.1  jhawk #include <time.h>
     60  1.1  jhawk #include <tzfile.h>
     61  1.1  jhawk #include <unistd.h>
     62  1.1  jhawk 
     63  1.1  jhawk #include "progressbar.h"
     64  1.1  jhawk 
     65  1.2  grant #if !defined(NO_PROGRESS)
     66  1.1  jhawk /*
     67  1.1  jhawk  * return non-zero if we're the current foreground process
     68  1.1  jhawk  */
     69  1.1  jhawk int
     70  1.1  jhawk foregroundproc(void)
     71  1.1  jhawk {
     72  1.1  jhawk 	static pid_t pgrp = -1;
     73  1.1  jhawk 
     74  1.1  jhawk 	if (pgrp == -1)
     75  1.1  jhawk 		pgrp = getpgrp();
     76  1.1  jhawk 
     77  1.1  jhawk 	return (tcgetpgrp(fileno(ttyout)) == pgrp);
     78  1.1  jhawk }
     79  1.2  grant #endif	/* !defined(NO_PROGRESS) */
     80  1.1  jhawk 
     81  1.1  jhawk 
     82  1.1  jhawk #ifndef	NO_PROGRESS
     83  1.1  jhawk static void updateprogressmeter(int);
     84  1.1  jhawk 
     85  1.1  jhawk /*
     86  1.1  jhawk  * SIGALRM handler to update the progress meter
     87  1.1  jhawk  */
     88  1.1  jhawk static void
     89  1.1  jhawk updateprogressmeter(int dummy)
     90  1.1  jhawk {
     91  1.1  jhawk 	int oerrno = errno;
     92  1.1  jhawk 
     93  1.1  jhawk 	progressmeter(0);
     94  1.1  jhawk 	errno = oerrno;
     95  1.1  jhawk }
     96  1.1  jhawk #endif	/* NO_PROGRESS */
     97  1.1  jhawk 
     98  1.1  jhawk 
     99  1.1  jhawk /*
    100  1.1  jhawk  * List of order of magnitude prefixes.
    101  1.1  jhawk  * The last is `P', as 2^64 = 16384 Petabytes
    102  1.1  jhawk  */
    103  1.1  jhawk static const char prefixes[] = " KMGTP";
    104  1.1  jhawk 
    105  1.1  jhawk /*
    106  1.1  jhawk  * Display a transfer progress bar if progress is non-zero.
    107  1.1  jhawk  * SIGALRM is hijacked for use by this function.
    108  1.1  jhawk  * - Before the transfer, set filesize to size of file (or -1 if unknown),
    109  1.1  jhawk  *   and call with flag = -1. This starts the once per second timer,
    110  1.1  jhawk  *   and a call to updateprogressmeter() upon SIGALRM.
    111  1.1  jhawk  * - During the transfer, updateprogressmeter will call progressmeter
    112  1.1  jhawk  *   with flag = 0
    113  1.1  jhawk  * - After the transfer, call with flag = 1
    114  1.1  jhawk  */
    115  1.1  jhawk static struct timeval start;
    116  1.1  jhawk static struct timeval lastupdate;
    117  1.1  jhawk 
    118  1.1  jhawk #define	BUFLEFT	(sizeof(buf) - len)
    119  1.1  jhawk 
    120  1.1  jhawk void
    121  1.1  jhawk progressmeter(int flag)
    122  1.1  jhawk {
    123  1.1  jhawk 	static off_t lastsize;
    124  1.1  jhawk 	off_t cursize;
    125  1.1  jhawk 	struct timeval now, wait;
    126  1.1  jhawk #ifndef NO_PROGRESS
    127  1.1  jhawk 	struct timeval td;
    128  1.1  jhawk 	off_t abbrevsize, bytespersec;
    129  1.1  jhawk 	double elapsed;
    130  1.1  jhawk 	int ratio, barlength, i, len, remaining;
    131  1.1  jhawk 
    132  1.1  jhawk 			/*
    133  1.1  jhawk 			 * Work variables for progress bar.
    134  1.1  jhawk 			 *
    135  1.1  jhawk 			 * XXX:	if the format of the progress bar changes
    136  1.1  jhawk 			 *	(especially the number of characters in the
    137  1.1  jhawk 			 *	`static' portion of it), be sure to update
    138  1.1  jhawk 			 *	these appropriately.
    139  1.1  jhawk 			 */
    140  1.1  jhawk 	char		buf[256];	/* workspace for progress bar */
    141  1.1  jhawk #define	BAROVERHEAD	43		/* non `*' portion of progress bar */
    142  1.1  jhawk 					/*
    143  1.1  jhawk 					 * stars should contain at least
    144  1.1  jhawk 					 * sizeof(buf) - BAROVERHEAD entries
    145  1.1  jhawk 					 */
    146  1.1  jhawk 	static const char	stars[] =
    147  1.1  jhawk "*****************************************************************************"
    148  1.1  jhawk "*****************************************************************************"
    149  1.1  jhawk "*****************************************************************************";
    150  1.1  jhawk 
    151  1.1  jhawk #endif
    152  1.1  jhawk 
    153  1.1  jhawk 	if (flag == -1) {
    154  1.1  jhawk 		(void)gettimeofday(&start, NULL);
    155  1.1  jhawk 		lastupdate = start;
    156  1.1  jhawk 		lastsize = restart_point;
    157  1.1  jhawk 	}
    158  1.1  jhawk 
    159  1.1  jhawk 	(void)gettimeofday(&now, NULL);
    160  1.1  jhawk 	cursize = bytes + restart_point;
    161  1.1  jhawk 	timersub(&now, &lastupdate, &wait);
    162  1.1  jhawk 	if (cursize > lastsize) {
    163  1.1  jhawk 		lastupdate = now;
    164  1.1  jhawk 		lastsize = cursize;
    165  1.1  jhawk 		wait.tv_sec = 0;
    166  1.1  jhawk 	} else {
    167  1.1  jhawk #ifndef STANDALONE_PROGRESS
    168  1.1  jhawk 		if (quit_time > 0 && wait.tv_sec > quit_time) {
    169  1.1  jhawk 			len = snprintf(buf, sizeof(buf), "\r\n%s: "
    170  1.1  jhawk 			    "transfer aborted because stalled for %lu sec.\r\n",
    171  1.1  jhawk 			    getprogname(), (unsigned long)wait.tv_sec);
    172  1.1  jhawk 			(void)write(fileno(ttyout), buf, len);
    173  1.1  jhawk 			(void)xsignal(SIGALRM, SIG_DFL);
    174  1.1  jhawk 			alarmtimer(0);
    175  1.1  jhawk 			siglongjmp(toplevel, 1);
    176  1.1  jhawk 		}
    177  1.1  jhawk #endif	/* !STANDALONE_PROGRESS */
    178  1.1  jhawk 	}
    179  1.1  jhawk 	/*
    180  1.1  jhawk 	 * Always set the handler even if we are not the foreground process.
    181  1.1  jhawk 	 */
    182  1.1  jhawk #ifdef STANDALONE_PROGRESS
    183  1.1  jhawk 	if (progress) {
    184  1.1  jhawk #else
    185  1.1  jhawk 	if (quit_time > 0 || progress) {
    186  1.1  jhawk #endif /* !STANDALONE_PROGRESS */
    187  1.1  jhawk 		if (flag == -1) {
    188  1.1  jhawk 			(void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
    189  1.1  jhawk 			alarmtimer(1);		/* set alarm timer for 1 Hz */
    190  1.1  jhawk 		} else if (flag == 1) {
    191  1.1  jhawk 			(void)xsignal(SIGALRM, SIG_DFL);
    192  1.1  jhawk 			alarmtimer(0);
    193  1.1  jhawk 		}
    194  1.1  jhawk 	}
    195  1.1  jhawk #ifndef NO_PROGRESS
    196  1.1  jhawk 	if (!progress)
    197  1.1  jhawk 		return;
    198  1.1  jhawk 	len = 0;
    199  1.1  jhawk 
    200  1.1  jhawk 	/*
    201  1.1  jhawk 	 * print progress bar only if we are foreground process.
    202  1.1  jhawk 	 */
    203  1.1  jhawk 	if (! foregroundproc())
    204  1.1  jhawk 		return;
    205  1.1  jhawk 
    206  1.1  jhawk 	len += snprintf(buf + len, BUFLEFT, "\r");
    207  1.1  jhawk 	if (filesize > 0) {
    208  1.1  jhawk 		ratio = (int)((double)cursize * 100.0 / (double)filesize);
    209  1.1  jhawk 		ratio = MAX(ratio, 0);
    210  1.1  jhawk 		ratio = MIN(ratio, 100);
    211  1.1  jhawk 		len += snprintf(buf + len, BUFLEFT, "%3d%% ", ratio);
    212  1.1  jhawk 
    213  1.1  jhawk 			/*
    214  1.1  jhawk 			 * calculate the length of the `*' bar, ensuring that
    215  1.1  jhawk 			 * the number of stars won't exceed the buffer size
    216  1.1  jhawk 			 */
    217  1.1  jhawk 		barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
    218  1.1  jhawk 		if (barlength > 0) {
    219  1.1  jhawk 			i = barlength * ratio / 100;
    220  1.1  jhawk 			len += snprintf(buf + len, BUFLEFT,
    221  1.1  jhawk 			    "|%.*s%*s|", i, stars, barlength - i, "");
    222  1.1  jhawk 		}
    223  1.1  jhawk 	}
    224  1.1  jhawk 
    225  1.1  jhawk 	abbrevsize = cursize;
    226  1.1  jhawk 	for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
    227  1.1  jhawk 		abbrevsize >>= 10;
    228  1.1  jhawk 	len += snprintf(buf + len, BUFLEFT, " " LLFP("5") " %c%c ",
    229  1.1  jhawk 	    (LLT)abbrevsize,
    230  1.1  jhawk 	    prefixes[i],
    231  1.1  jhawk 	    i == 0 ? ' ' : 'B');
    232  1.1  jhawk 
    233  1.1  jhawk 	timersub(&now, &start, &td);
    234  1.1  jhawk 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    235  1.1  jhawk 
    236  1.1  jhawk 	bytespersec = 0;
    237  1.1  jhawk 	if (bytes > 0) {
    238  1.1  jhawk 		bytespersec = bytes;
    239  1.1  jhawk 		if (elapsed > 0.0)
    240  1.1  jhawk 			bytespersec /= elapsed;
    241  1.1  jhawk 	}
    242  1.1  jhawk 	for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
    243  1.1  jhawk 		bytespersec >>= 10;
    244  1.1  jhawk 	len += snprintf(buf + len, BUFLEFT,
    245  1.1  jhawk 	    " " LLFP("3") ".%02d %cB/s ",
    246  1.1  jhawk 	    (LLT)(bytespersec / 1024),
    247  1.1  jhawk 	    (int)((bytespersec % 1024) * 100 / 1024),
    248  1.1  jhawk 	    prefixes[i]);
    249  1.1  jhawk 
    250  1.1  jhawk 	if (filesize > 0) {
    251  1.1  jhawk 		if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
    252  1.1  jhawk 			len += snprintf(buf + len, BUFLEFT, "   --:-- ETA");
    253  1.1  jhawk 		} else if (wait.tv_sec >= STALLTIME) {
    254  1.1  jhawk 			len += snprintf(buf + len, BUFLEFT, " - stalled -");
    255  1.1  jhawk 		} else {
    256  1.1  jhawk 			remaining = (int)
    257  1.1  jhawk 			    ((filesize - restart_point) / (bytes / elapsed) -
    258  1.1  jhawk 			    elapsed);
    259  1.1  jhawk 			if (remaining >= 100 * SECSPERHOUR)
    260  1.1  jhawk 				len += snprintf(buf + len, BUFLEFT,
    261  1.1  jhawk 				    "   --:-- ETA");
    262  1.1  jhawk 			else {
    263  1.1  jhawk 				i = remaining / SECSPERHOUR;
    264  1.1  jhawk 				if (i)
    265  1.1  jhawk 					len += snprintf(buf + len, BUFLEFT,
    266  1.1  jhawk 					    "%2d:", i);
    267  1.1  jhawk 				else
    268  1.1  jhawk 					len += snprintf(buf + len, BUFLEFT,
    269  1.1  jhawk 					    "   ");
    270  1.1  jhawk 				i = remaining % SECSPERHOUR;
    271  1.1  jhawk 				len += snprintf(buf + len, BUFLEFT,
    272  1.1  jhawk 				    "%02d:%02d ETA", i / 60, i % 60);
    273  1.1  jhawk 			}
    274  1.1  jhawk 		}
    275  1.1  jhawk 	}
    276  1.1  jhawk 	if (flag == 1)
    277  1.1  jhawk 		len += snprintf(buf + len, BUFLEFT, "\n");
    278  1.1  jhawk 	(void)write(fileno(ttyout), buf, len);
    279  1.1  jhawk 
    280  1.1  jhawk #endif	/* !NO_PROGRESS */
    281  1.1  jhawk }
    282  1.1  jhawk 
    283  1.1  jhawk #ifndef STANDALONE_PROGRESS
    284  1.1  jhawk /*
    285  1.1  jhawk  * Display transfer statistics.
    286  1.1  jhawk  * Requires start to be initialised by progressmeter(-1),
    287  1.1  jhawk  * direction to be defined by xfer routines, and filesize and bytes
    288  1.1  jhawk  * to be updated by xfer routines
    289  1.1  jhawk  * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
    290  1.1  jhawk  * instead of ttyout.
    291  1.1  jhawk  */
    292  1.1  jhawk void
    293  1.1  jhawk ptransfer(int siginfo)
    294  1.1  jhawk {
    295  1.1  jhawk 	struct timeval now, td, wait;
    296  1.1  jhawk 	double elapsed;
    297  1.1  jhawk 	off_t bytespersec;
    298  1.1  jhawk 	int remaining, hh, i, len;
    299  1.1  jhawk 
    300  1.1  jhawk 	char buf[256];		/* Work variable for transfer status. */
    301  1.1  jhawk 
    302  1.1  jhawk 	if (!verbose && !progress && !siginfo)
    303  1.1  jhawk 		return;
    304  1.1  jhawk 
    305  1.1  jhawk 	(void)gettimeofday(&now, NULL);
    306  1.1  jhawk 	timersub(&now, &start, &td);
    307  1.1  jhawk 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    308  1.1  jhawk 	bytespersec = 0;
    309  1.1  jhawk 	if (bytes > 0) {
    310  1.1  jhawk 		bytespersec = bytes;
    311  1.1  jhawk 		if (elapsed > 0.0)
    312  1.1  jhawk 			bytespersec /= elapsed;
    313  1.1  jhawk 	}
    314  1.1  jhawk 	len = 0;
    315  1.1  jhawk 	len += snprintf(buf + len, BUFLEFT, LLF " byte%s %s in ",
    316  1.1  jhawk 	    (LLT)bytes, bytes == 1 ? "" : "s", direction);
    317  1.1  jhawk 	remaining = (int)elapsed;
    318  1.1  jhawk 	if (remaining > SECSPERDAY) {
    319  1.1  jhawk 		int days;
    320  1.1  jhawk 
    321  1.1  jhawk 		days = remaining / SECSPERDAY;
    322  1.1  jhawk 		remaining %= SECSPERDAY;
    323  1.1  jhawk 		len += snprintf(buf + len, BUFLEFT,
    324  1.1  jhawk 		    "%d day%s ", days, days == 1 ? "" : "s");
    325  1.1  jhawk 	}
    326  1.1  jhawk 	hh = remaining / SECSPERHOUR;
    327  1.1  jhawk 	remaining %= SECSPERHOUR;
    328  1.1  jhawk 	if (hh)
    329  1.1  jhawk 		len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
    330  1.1  jhawk 	len += snprintf(buf + len, BUFLEFT,
    331  1.1  jhawk 	    "%02d:%02d ", remaining / 60, remaining % 60);
    332  1.1  jhawk 
    333  1.1  jhawk 	for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
    334  1.1  jhawk 		bytespersec >>= 10;
    335  1.1  jhawk 	len += snprintf(buf + len, BUFLEFT, "(" LLF ".%02d %cB/s)",
    336  1.1  jhawk 	    (LLT)(bytespersec / 1024),
    337  1.1  jhawk 	    (int)((bytespersec % 1024) * 100 / 1024),
    338  1.1  jhawk 	    prefixes[i]);
    339  1.1  jhawk 
    340  1.1  jhawk 	if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
    341  1.1  jhawk 	    && bytes + restart_point <= filesize) {
    342  1.1  jhawk 		remaining = (int)((filesize - restart_point) /
    343  1.1  jhawk 				  (bytes / elapsed) - elapsed);
    344  1.1  jhawk 		hh = remaining / SECSPERHOUR;
    345  1.1  jhawk 		remaining %= SECSPERHOUR;
    346  1.1  jhawk 		len += snprintf(buf + len, BUFLEFT, "  ETA: ");
    347  1.1  jhawk 		if (hh)
    348  1.1  jhawk 			len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
    349  1.1  jhawk 		len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
    350  1.1  jhawk 		    remaining / 60, remaining % 60);
    351  1.1  jhawk 		timersub(&now, &lastupdate, &wait);
    352  1.1  jhawk 		if (wait.tv_sec >= STALLTIME)
    353  1.1  jhawk 			len += snprintf(buf + len, BUFLEFT, "  (stalled)");
    354  1.1  jhawk 	}
    355  1.1  jhawk 	len += snprintf(buf + len, BUFLEFT, "\n");
    356  1.1  jhawk 	(void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
    357  1.1  jhawk }
    358  1.1  jhawk 
    359  1.1  jhawk /*
    360  1.1  jhawk  * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
    361  1.1  jhawk  */
    362  1.1  jhawk void
    363  1.1  jhawk psummary(int notused)
    364  1.1  jhawk {
    365  1.1  jhawk 	int oerrno = errno;
    366  1.1  jhawk 
    367  1.1  jhawk 	if (bytes > 0) {
    368  1.1  jhawk 		if (fromatty)
    369  1.1  jhawk 			write(fileno(ttyout), "\n", 1);
    370  1.1  jhawk 		ptransfer(1);
    371  1.1  jhawk 	}
    372  1.1  jhawk 	errno = oerrno;
    373  1.1  jhawk }
    374  1.1  jhawk #endif	/* !STANDALONE_PROGRESS */
    375  1.1  jhawk 
    376  1.1  jhawk 
    377  1.1  jhawk /*
    378  1.1  jhawk  * Set the SIGALRM interval timer for wait seconds, 0 to disable.
    379  1.1  jhawk  */
    380  1.1  jhawk void
    381  1.1  jhawk alarmtimer(int wait)
    382  1.1  jhawk {
    383  1.1  jhawk 	struct itimerval itv;
    384  1.1  jhawk 
    385  1.1  jhawk 	itv.it_value.tv_sec = wait;
    386  1.1  jhawk 	itv.it_value.tv_usec = 0;
    387  1.1  jhawk 	itv.it_interval = itv.it_value;
    388  1.1  jhawk 	setitimer(ITIMER_REAL, &itv, NULL);
    389  1.1  jhawk }
    390  1.1  jhawk 
    391  1.1  jhawk 
    392  1.1  jhawk /*
    393  1.1  jhawk  * Install a POSIX signal handler, allowing the invoker to set whether
    394  1.1  jhawk  * the signal should be restartable or not
    395  1.1  jhawk  */
    396  1.1  jhawk sigfunc
    397  1.1  jhawk xsignal_restart(int sig, sigfunc func, int restartable)
    398  1.1  jhawk {
    399  1.1  jhawk 	struct sigaction act, oact;
    400  1.1  jhawk 	act.sa_handler = func;
    401  1.1  jhawk 
    402  1.1  jhawk 	sigemptyset(&act.sa_mask);
    403  1.1  jhawk #if defined(SA_RESTART)			/* 4.4BSD, Posix(?), SVR4 */
    404  1.1  jhawk 	act.sa_flags = restartable ? SA_RESTART : 0;
    405  1.1  jhawk #elif defined(SA_INTERRUPT)		/* SunOS 4.x */
    406  1.1  jhawk 	act.sa_flags = restartable ? 0 : SA_INTERRUPT;
    407  1.1  jhawk #else
    408  1.1  jhawk #error "system must have SA_RESTART or SA_INTERRUPT"
    409  1.1  jhawk #endif
    410  1.1  jhawk 	if (sigaction(sig, &act, &oact) < 0)
    411  1.1  jhawk 		return (SIG_ERR);
    412  1.1  jhawk 	return (oact.sa_handler);
    413  1.1  jhawk }
    414  1.1  jhawk 
    415  1.1  jhawk /*
    416  1.1  jhawk  * Install a signal handler with the `restartable' flag set dependent upon
    417  1.1  jhawk  * which signal is being set. (This is a wrapper to xsignal_restart())
    418  1.1  jhawk  */
    419  1.1  jhawk sigfunc
    420  1.1  jhawk xsignal(int sig, sigfunc func)
    421  1.1  jhawk {
    422  1.1  jhawk 	int restartable;
    423  1.1  jhawk 
    424  1.1  jhawk 	/*
    425  1.1  jhawk 	 * Some signals print output or change the state of the process.
    426  1.1  jhawk 	 * There should be restartable, so that reads and writes are
    427  1.1  jhawk 	 * not affected.  Some signals should cause program flow to change;
    428  1.1  jhawk 	 * these signals should not be restartable, so that the system call
    429  1.1  jhawk 	 * will return with EINTR, and the program will go do something
    430  1.1  jhawk 	 * different.  If the signal handler calls longjmp() or siglongjmp(),
    431  1.1  jhawk 	 * it doesn't matter if it's restartable.
    432  1.1  jhawk 	 */
    433  1.1  jhawk 
    434  1.1  jhawk 	switch(sig) {
    435  1.1  jhawk #ifdef SIGINFO
    436  1.1  jhawk 	case SIGINFO:
    437  1.1  jhawk #endif
    438  1.1  jhawk 	case SIGQUIT:
    439  1.1  jhawk 	case SIGUSR1:
    440  1.1  jhawk 	case SIGUSR2:
    441  1.1  jhawk 	case SIGWINCH:
    442  1.1  jhawk 		restartable = 1;
    443  1.1  jhawk 		break;
    444  1.1  jhawk 
    445  1.1  jhawk 	case SIGALRM:
    446  1.1  jhawk 	case SIGINT:
    447  1.1  jhawk 	case SIGPIPE:
    448  1.1  jhawk 		restartable = 0;
    449  1.1  jhawk 		break;
    450  1.1  jhawk 
    451  1.1  jhawk 	default:
    452  1.1  jhawk 		/*
    453  1.1  jhawk 		 * This is unpleasant, but I don't know what would be better.
    454  1.1  jhawk 		 * Right now, this "can't happen"
    455  1.1  jhawk 		 */
    456  1.1  jhawk 		errx(1, "xsignal_restart called with signal %d", sig);
    457  1.1  jhawk 	}
    458  1.1  jhawk 
    459  1.1  jhawk 	return(xsignal_restart(sig, func, restartable));
    460  1.1  jhawk }
    461