[alt.sources] hup

maart@cs.vu.nl (Maarten Litmaath) (03/12/90)

This utility seems to do the job on SunOS 4.0.3c and 4.3BSD.  However,
started by csh under rlogind the command somehow doesn't receive a SIGHUP
on logout!  :-(  Work-around: end your .logout with `exec true'.
Furthermore note the close-on-exec trick.  Can you figure it out?
--------------------cut here--------------------
/*
 * hup.c
 * Run a background job in the process group of its parent (the shell),
 * so that it will receive a SIGHUP on logout.
 * Usage: hup command
 * Note: `hup' will put `command' in the background itself, so there's no
 * need for a trailing `&'.
 * Compile with `-DNO_FCNTL' if your UNIX variant doesn't have fcntl(2).
 * If your shell is csh, you might want to end your `.logout' file with an
 * `exec' command (e.g. `exec true') to get `hup' to work on an `rlogin'.  :-(
 * Author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
 */
#include	<stdio.h>
#include	<signal.h>
#ifdef	NO_FCNTL
#include	<sys/ioctl.h>
#else
#include	<fcntl.h>
#endif	/* NO_FCNTL */

char	Id[] = "@(#)hup 1.0 90/03/09 Maarten Litmaath";

int	Keyboard_signals[] = {
	SIGINT,
	SIGQUIT,
#ifdef	SIGTSTP
	SIGTSTP,
#endif	/* SIGTSTP */
	0
};

main(argc, argv)
int	argc;
char	**argv;
{
	int	pgrp, sig, i, pp[2];
	char	buf[1];

	if (argc == 1) {
		fprintf(stderr, "Usage: %s command\n", argv[0]);
		exit(1);
	}

	pgrp = getpgrp(getppid());

	if (pipe(pp) < 0) {
		perror("pipe");
		exit(1);
	}

	switch (fork()) {
	case -1:
		perror("fork");
		exit(1);
	case 0:
		for (i = 0; sig = Keyboard_signals[i]; i++)
			signal(sig, SIG_IGN);

		if (setpgrp(0, pgrp) < 0) {
			perror("setpgrp");
			exit(1);
		}

		close(pp[0]);

		/* set close-on-exec flag */
#ifdef	NO_FCNTL
		ioctl(pp[1], FIOCLEX, (int *) 0);
#else
		fcntl(pp[1], F_SETFD, 1);
#endif	/* NO_FCNTL */

		execvp(argv[1], &argv[1]);
		perror(argv[1]);

		/* send a message to indicate failure */
		write(pp[1], buf, 1);
		exit(1);
	}
	close(pp[1]);
	exit(read(pp[0], buf, 1));
}
--------------------cut here--------------------
--
 1) Will 4.5BSD have wait5()?         |Maarten Litmaath @ VU Amsterdam:
 2) Sleep(3) should be sleep(2) again.|maart@cs.vu.nl, uunet!mcsun!botter!maart