xref: /original-bsd/usr.bin/uucp/shio.c (revision 7e9b2efe)
199a5b95dSsam #ifndef lint
2*7e9b2efeSralph static char sccsid[] = "@(#)shio.c	5.2 (Berkeley) 01/22/85";
399a5b95dSsam #endif
499a5b95dSsam 
599a5b95dSsam #include "uucp.h"
699a5b95dSsam #include <signal.h>
799a5b95dSsam 
899a5b95dSsam /*******
999a5b95dSsam  *	shio(cmd, fi, fo, user)	execute shell of command with
1099a5b95dSsam  *	char *cmd, *fi, *fo;	fi and fo as standard input/output
1199a5b95dSsam  *	char *user;		user name
1299a5b95dSsam  *
1399a5b95dSsam  *	return codes:
1499a5b95dSsam  *		0  - ok
1599a5b95dSsam  *		non zero -  failed  -  status from child
1699a5b95dSsam  */
1799a5b95dSsam 
shio(cmd,fi,fo,user)1899a5b95dSsam shio(cmd, fi, fo, user)
1999a5b95dSsam char *cmd, *fi, *fo, *user;
2099a5b95dSsam {
2199a5b95dSsam 	int status, f;
2299a5b95dSsam 	int uid, pid, ret;
2399a5b95dSsam 	char path[MAXFULLNAME];
24*7e9b2efeSralph 	extern int errno;
2599a5b95dSsam 
2699a5b95dSsam 	if (fi == NULL)
27*7e9b2efeSralph 		fi = DEVNULL;
2899a5b95dSsam 	if (fo == NULL)
29*7e9b2efeSralph 		fo = DEVNULL;
3099a5b95dSsam 
3199a5b95dSsam 	DEBUG(3, "shio - %s\n", cmd);
32*7e9b2efeSralph #ifdef SIGCHLD
33*7e9b2efeSralph 	signal(SIGCHLD, SIG_IGN);
34*7e9b2efeSralph #endif SIGCHLD
3599a5b95dSsam 	if ((pid = fork()) == 0) {
3699a5b95dSsam 		signal(SIGINT, SIG_IGN);
3799a5b95dSsam 		signal(SIGHUP, SIG_IGN);
3899a5b95dSsam 		signal(SIGQUIT, SIG_IGN);
3999a5b95dSsam 		signal(SIGKILL, SIG_IGN);
4099a5b95dSsam 		close(Ifn);
4199a5b95dSsam 		close(Ofn);
4299a5b95dSsam 		close(0);
43*7e9b2efeSralph 		if (user == NULL || (gninfo(user, &uid, path) != 0)
4499a5b95dSsam 			|| setuid(uid))
4599a5b95dSsam 			setuid(getuid());
4699a5b95dSsam 		f = open(subfile(fi), 0);
47*7e9b2efeSralph 		if (f != 0) {
48*7e9b2efeSralph 			logent(fi, "CAN'T READ");
49*7e9b2efeSralph 			exit(-errno);
50*7e9b2efeSralph 		}
5199a5b95dSsam 		close(1);
5299a5b95dSsam 		f = creat(subfile(fo), 0666);
53*7e9b2efeSralph 		if (f != 1) {
54*7e9b2efeSralph 			logent(fo, "CAN'T WRITE");
55*7e9b2efeSralph 			exit(-errno);
5699a5b95dSsam 		}
57*7e9b2efeSralph 		execl(SHELL, "sh", "-c", cmd, (char *)0);
58*7e9b2efeSralph 		exit(100+errno);
59*7e9b2efeSralph 	}
60*7e9b2efeSralph 	while ((ret = wait(&status)) != pid && ret != -1)
61*7e9b2efeSralph 		;
6299a5b95dSsam 	DEBUG(3, "status %d\n", status);
63*7e9b2efeSralph 	return status;
6499a5b95dSsam }
65