File:  [MW Coherent from dump] / coherent / d / etc / SYSVcron / src / cronpipe.c.1.2
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed May 29 04:56:37 2019 UTC (7 years, 2 months ago) by root
Branches: MarkWilliams, MAIN
CVS tags: relic, HEAD
coherent

/*
 * cronpipe.c 
 * Special version of popen, that allows to crond keep track about
 * children.
 */
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include "cron.h"

#define	R	0	/* Pipe read descriptor index */
#define	W	1	/* Pipe write descriptor index */

extern child_id	*add_entry();	/* Add an new entry to a link list */
extern child_id *current;	/* Pointer to the current child id */

extern char	acRealUser[MAX_UNAME];
extern time_t	clock;
extern int	flag320;

static	int	poppid[_NFILE];

FILE *
cronpipe(command, type)
char *command, *type;
{
	register mode;
	register fd;
	int pd[2];

	if (pipe(pd) < 0)
		return (NULL);
	if (*type == 'w') {
		mode = W;
		fd = pd[W];
	} else {
		mode = R;
		fd = pd[R];
	}
	if ((poppid[fd] = fork()) < 0) {
		close(pd[R]);
		close(pd[W]);
		return (NULL);
	}
	if (poppid[fd] == 0) {		/* Child */
		if (mode == W) {
			close(pd[W]);
			close(fileno(stdin));
			dup(pd[R]);
			close(pd[R]);
		} else {
			close(pd[R]);
			close(fileno(stdout));
			dup(pd[W]);
			close(pd[W]);
		}
		Dprint("cronpipe: before execl %s\n", command);
		execl("/bin/sh", "sh", "-c", command, NULL);
		exit(1);
	} 
	/* Parent */
	/* We will keep track about keeds only in SV mode */
	if (flag320 == FALSE) {
		Dprint("cronpipe before fork: Name %s\n", acRealUser);
		Dprint("                      Pid %d\n", poppid[fd]);
		Dprint("                      Name %s\n", acRealUser);
		Dprint("                      Time %s\n", ctime(&clock));
		Dprint("                      Command %s\n", command);
		current = add_entry(poppid[fd], acRealUser, clock, command);
	}
	if (mode == W) {
		close(pd[R]);
		return (fdopen(pd[W], "w"));
	} else {
		close(pd[W]);
		return (fdopen(pd[R], "r"));
	}
}

pclose(stream)
FILE *stream;
{
	register fd;
	register pid, wpid;
	int status;
	int (*hupfun)(), (*intfun)(), (*quitfun)();

	fd = fileno(stream);
	pid = poppid[fd];
	poppid[fd] = 0;
	if (pid==0 || fclose(stream)==EOF)
		return (-1);
	hupfun = signal(SIGHUP, SIG_IGN);
	intfun = signal(SIGINT, SIG_IGN);
	quitfun = signal(SIGQUIT, SIG_IGN);
	while ((wpid = wait(&status))!=pid && wpid>=0)
		;
	if (wpid < 0)
		status = -1;
	signal(SIGHUP, hupfun);
	signal(SIGINT, intfun);
	signal(SIGQUIT, quitfun);
	return (status);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.