xref: /csrg-svn/usr.bin/uucp/shio.c (revision 17838)
113669Ssam #ifndef lint
2*17838Sralph static char sccsid[] = "@(#)shio.c	5.2 (Berkeley) 01/22/85";
313669Ssam #endif
413669Ssam 
513669Ssam #include "uucp.h"
613669Ssam #include <signal.h>
713669Ssam 
813669Ssam /*******
913669Ssam  *	shio(cmd, fi, fo, user)	execute shell of command with
1013669Ssam  *	char *cmd, *fi, *fo;	fi and fo as standard input/output
1113669Ssam  *	char *user;		user name
1213669Ssam  *
1313669Ssam  *	return codes:
1413669Ssam  *		0  - ok
1513669Ssam  *		non zero -  failed  -  status from child
1613669Ssam  */
1713669Ssam 
shio(cmd,fi,fo,user)1813669Ssam shio(cmd, fi, fo, user)
1913669Ssam char *cmd, *fi, *fo, *user;
2013669Ssam {
2113669Ssam 	int status, f;
2213669Ssam 	int uid, pid, ret;
2313669Ssam 	char path[MAXFULLNAME];
24*17838Sralph 	extern int errno;
2513669Ssam 
2613669Ssam 	if (fi == NULL)
27*17838Sralph 		fi = DEVNULL;
2813669Ssam 	if (fo == NULL)
29*17838Sralph 		fo = DEVNULL;
3013669Ssam 
3113669Ssam 	DEBUG(3, "shio - %s\n", cmd);
32*17838Sralph #ifdef SIGCHLD
33*17838Sralph 	signal(SIGCHLD, SIG_IGN);
34*17838Sralph #endif SIGCHLD
3513669Ssam 	if ((pid = fork()) == 0) {
3613669Ssam 		signal(SIGINT, SIG_IGN);
3713669Ssam 		signal(SIGHUP, SIG_IGN);
3813669Ssam 		signal(SIGQUIT, SIG_IGN);
3913669Ssam 		signal(SIGKILL, SIG_IGN);
4013669Ssam 		close(Ifn);
4113669Ssam 		close(Ofn);
4213669Ssam 		close(0);
43*17838Sralph 		if (user == NULL || (gninfo(user, &uid, path) != 0)
44*17838Sralph 			|| setuid(uid))
4513669Ssam 			setuid(getuid());
4613669Ssam 		f = open(subfile(fi), 0);
47*17838Sralph 		if (f != 0) {
48*17838Sralph 			logent(fi, "CAN'T READ");
49*17838Sralph 			exit(-errno);
50*17838Sralph 		}
5113669Ssam 		close(1);
5213669Ssam 		f = creat(subfile(fo), 0666);
53*17838Sralph 		if (f != 1) {
54*17838Sralph 			logent(fo, "CAN'T WRITE");
55*17838Sralph 			exit(-errno);
56*17838Sralph 		}
5713669Ssam 		execl(SHELL, "sh", "-c", cmd, (char *)0);
58*17838Sralph 		exit(100+errno);
5913669Ssam 	}
60*17838Sralph 	while ((ret = wait(&status)) != pid && ret != -1)
61*17838Sralph 		;
6213669Ssam 	DEBUG(3, "status %d\n", status);
63*17838Sralph 	return status;
6413669Ssam }
65