xref: /inferno-os/emu/NetBSD/cmd.c (revision b43c1ca5eb5fc65b93ae935a568432712797b049)
1*b43c1ca5SCharles.Forsyth #include	<sys/types.h>
2*b43c1ca5SCharles.Forsyth #include	<signal.h>
3*b43c1ca5SCharles.Forsyth #include 	<pwd.h>
4*b43c1ca5SCharles.Forsyth #include	<sys/resource.h>
5*b43c1ca5SCharles.Forsyth #include	<sys/wait.h>
6*b43c1ca5SCharles.Forsyth #include	<fcntl.h>
7*b43c1ca5SCharles.Forsyth 
8*b43c1ca5SCharles.Forsyth #include	"dat.h"
9*b43c1ca5SCharles.Forsyth #include	"fns.h"
10*b43c1ca5SCharles.Forsyth #include	"error.h"
11*b43c1ca5SCharles.Forsyth 
12*b43c1ca5SCharles.Forsyth enum
13*b43c1ca5SCharles.Forsyth {
14*b43c1ca5SCharles.Forsyth 	Debug = 0
15*b43c1ca5SCharles.Forsyth };
16*b43c1ca5SCharles.Forsyth 
17*b43c1ca5SCharles.Forsyth /*
18*b43c1ca5SCharles.Forsyth  * os-specific devcmd support.
19*b43c1ca5SCharles.Forsyth  * this version should be reasonably portable across Unix systems.
20*b43c1ca5SCharles.Forsyth  */
21*b43c1ca5SCharles.Forsyth typedef struct Targ Targ;
22*b43c1ca5SCharles.Forsyth struct Targ
23*b43c1ca5SCharles.Forsyth {
24*b43c1ca5SCharles.Forsyth 	int	fd[3];	/* fd[0] is standard input, fd[1] is standard output, fd[2] is standard error */
25*b43c1ca5SCharles.Forsyth 	char**	args;
26*b43c1ca5SCharles.Forsyth 	char*	dir;
27*b43c1ca5SCharles.Forsyth 	int	pid;
28*b43c1ca5SCharles.Forsyth 	int	wfd;	/* child writes errors that occur after the fork or on exec */
29*b43c1ca5SCharles.Forsyth 	int	uid;
30*b43c1ca5SCharles.Forsyth 	int	gid;
31*b43c1ca5SCharles.Forsyth };
32*b43c1ca5SCharles.Forsyth 
33*b43c1ca5SCharles.Forsyth extern int gidnobody;
34*b43c1ca5SCharles.Forsyth extern int uidnobody;
35*b43c1ca5SCharles.Forsyth 
36*b43c1ca5SCharles.Forsyth static int
childproc(Targ * t)37*b43c1ca5SCharles.Forsyth childproc(Targ *t)
38*b43c1ca5SCharles.Forsyth {
39*b43c1ca5SCharles.Forsyth 	int i, nfd;
40*b43c1ca5SCharles.Forsyth 
41*b43c1ca5SCharles.Forsyth 	if(Debug)
42*b43c1ca5SCharles.Forsyth 		print("devcmd: '%s'", t->args[0]);
43*b43c1ca5SCharles.Forsyth 
44*b43c1ca5SCharles.Forsyth 	nfd = getdtablesize();
45*b43c1ca5SCharles.Forsyth 	for(i = 0; i < nfd; i++)
46*b43c1ca5SCharles.Forsyth 		if(i != t->fd[0] && i != t->fd[1] && i != t->fd[2] && i != t->wfd)
47*b43c1ca5SCharles.Forsyth 			close(i);
48*b43c1ca5SCharles.Forsyth 
49*b43c1ca5SCharles.Forsyth 	dup2(t->fd[0], 0);
50*b43c1ca5SCharles.Forsyth 	dup2(t->fd[1], 1);
51*b43c1ca5SCharles.Forsyth 	dup2(t->fd[2], 2);
52*b43c1ca5SCharles.Forsyth 	close(t->fd[0]);
53*b43c1ca5SCharles.Forsyth 	close(t->fd[1]);
54*b43c1ca5SCharles.Forsyth 	close(t->fd[2]);
55*b43c1ca5SCharles.Forsyth 
56*b43c1ca5SCharles.Forsyth 	/* should have an auth file to do host-specific authorisation? */
57*b43c1ca5SCharles.Forsyth 	if(t->gid != -1){
58*b43c1ca5SCharles.Forsyth 		if(setgid(t->gid) < 0 && getegid() == 0){
59*b43c1ca5SCharles.Forsyth 			fprint(t->wfd, "can't set gid %d: %s", t->gid, strerror(errno));
60*b43c1ca5SCharles.Forsyth 			_exit(1);
61*b43c1ca5SCharles.Forsyth 		}
62*b43c1ca5SCharles.Forsyth 	}
63*b43c1ca5SCharles.Forsyth 
64*b43c1ca5SCharles.Forsyth 	if(t->uid != -1){
65*b43c1ca5SCharles.Forsyth 		if(setuid(t->uid) < 0 && geteuid() == 0){
66*b43c1ca5SCharles.Forsyth 			fprint(t->wfd, "can't set uid %d: %s", t->uid, strerror(errno));
67*b43c1ca5SCharles.Forsyth 			_exit(1);
68*b43c1ca5SCharles.Forsyth 		}
69*b43c1ca5SCharles.Forsyth 	}
70*b43c1ca5SCharles.Forsyth 
71*b43c1ca5SCharles.Forsyth 	if(t->dir != nil && chdir(t->dir) < 0){
72*b43c1ca5SCharles.Forsyth 		fprint(t->wfd, "can't chdir to %s: %s", t->dir, strerror(errno));
73*b43c1ca5SCharles.Forsyth 		_exit(1);
74*b43c1ca5SCharles.Forsyth 	}
75*b43c1ca5SCharles.Forsyth 
76*b43c1ca5SCharles.Forsyth 	signal(SIGPIPE, SIG_DFL);
77*b43c1ca5SCharles.Forsyth 
78*b43c1ca5SCharles.Forsyth 	execvp(t->args[0], t->args);
79*b43c1ca5SCharles.Forsyth 	if(Debug)
80*b43c1ca5SCharles.Forsyth 		print("execvp: %s\n",strerror(errno));
81*b43c1ca5SCharles.Forsyth 	fprint(t->wfd, "exec failed: %s", strerror(errno));
82*b43c1ca5SCharles.Forsyth 
83*b43c1ca5SCharles.Forsyth 	_exit(1);
84*b43c1ca5SCharles.Forsyth }
85*b43c1ca5SCharles.Forsyth 
86*b43c1ca5SCharles.Forsyth void*
oscmd(char ** args,int nice,char * dir,int * fd)87*b43c1ca5SCharles.Forsyth oscmd(char **args, int nice, char *dir, int *fd)
88*b43c1ca5SCharles.Forsyth {
89*b43c1ca5SCharles.Forsyth 	Targ *t;
90*b43c1ca5SCharles.Forsyth 	int r, fd0[2], fd1[2], fd2[2], wfd[2], n, pid;
91*b43c1ca5SCharles.Forsyth 
92*b43c1ca5SCharles.Forsyth 	t = mallocz(sizeof(*t), 1);
93*b43c1ca5SCharles.Forsyth 	if(t == nil)
94*b43c1ca5SCharles.Forsyth 		return nil;
95*b43c1ca5SCharles.Forsyth 
96*b43c1ca5SCharles.Forsyth 	fd0[0] = fd0[1] = -1;
97*b43c1ca5SCharles.Forsyth 	fd1[0] = fd1[1] = -1;
98*b43c1ca5SCharles.Forsyth 	fd2[0] = fd2[1] = -1;
99*b43c1ca5SCharles.Forsyth 	wfd[0] = wfd[1] = -1;
100*b43c1ca5SCharles.Forsyth 	if(pipe(fd0) < 0 || pipe(fd1) < 0 || pipe(fd2) < 0 || pipe(wfd) < 0)
101*b43c1ca5SCharles.Forsyth 		goto Error;
102*b43c1ca5SCharles.Forsyth 	if(fcntl(wfd[1], F_SETFD, FD_CLOEXEC) < 0)	/* close on exec to give end of file on success */
103*b43c1ca5SCharles.Forsyth 		goto Error;
104*b43c1ca5SCharles.Forsyth 
105*b43c1ca5SCharles.Forsyth 	t->fd[0] = fd0[0];
106*b43c1ca5SCharles.Forsyth 	t->fd[1] = fd1[1];
107*b43c1ca5SCharles.Forsyth 	t->fd[2] = fd2[1];
108*b43c1ca5SCharles.Forsyth 	t->wfd = wfd[1];
109*b43c1ca5SCharles.Forsyth 	t->args = args;
110*b43c1ca5SCharles.Forsyth 	t->dir = dir;
111*b43c1ca5SCharles.Forsyth 	t->gid = up->env->gid;
112*b43c1ca5SCharles.Forsyth 	if(t->gid == -1)
113*b43c1ca5SCharles.Forsyth 		t->gid = gidnobody;
114*b43c1ca5SCharles.Forsyth 	t->uid = up->env->uid;
115*b43c1ca5SCharles.Forsyth 	if(t->uid == -1)
116*b43c1ca5SCharles.Forsyth 		t->uid = uidnobody;
117*b43c1ca5SCharles.Forsyth 
118*b43c1ca5SCharles.Forsyth 	signal(SIGCHLD, SIG_DFL);
119*b43c1ca5SCharles.Forsyth 	switch(pid = fork()) {
120*b43c1ca5SCharles.Forsyth 	case -1:
121*b43c1ca5SCharles.Forsyth 		goto Error;
122*b43c1ca5SCharles.Forsyth 	case 0:
123*b43c1ca5SCharles.Forsyth 		setpgid(0, getpid());
124*b43c1ca5SCharles.Forsyth 		if(nice)
125*b43c1ca5SCharles.Forsyth 			oslopri();
126*b43c1ca5SCharles.Forsyth 		childproc(t);
127*b43c1ca5SCharles.Forsyth 		_exit(1);
128*b43c1ca5SCharles.Forsyth 	default:
129*b43c1ca5SCharles.Forsyth 		t->pid = pid;
130*b43c1ca5SCharles.Forsyth 		if(Debug)
131*b43c1ca5SCharles.Forsyth 			print("cmd pid %d\n", t->pid);
132*b43c1ca5SCharles.Forsyth 		break;
133*b43c1ca5SCharles.Forsyth 	}
134*b43c1ca5SCharles.Forsyth 
135*b43c1ca5SCharles.Forsyth 	close(fd0[0]);
136*b43c1ca5SCharles.Forsyth 	close(fd1[1]);
137*b43c1ca5SCharles.Forsyth 	close(fd2[1]);
138*b43c1ca5SCharles.Forsyth 	close(wfd[1]);
139*b43c1ca5SCharles.Forsyth 
140*b43c1ca5SCharles.Forsyth 	n = read(wfd[0], up->genbuf, sizeof(up->genbuf)-1);
141*b43c1ca5SCharles.Forsyth 	close(wfd[0]);
142*b43c1ca5SCharles.Forsyth 	if(n > 0){
143*b43c1ca5SCharles.Forsyth 		close(fd0[1]);
144*b43c1ca5SCharles.Forsyth 		close(fd1[0]);
145*b43c1ca5SCharles.Forsyth 		close(fd2[0]);
146*b43c1ca5SCharles.Forsyth 		free(t);
147*b43c1ca5SCharles.Forsyth 		up->genbuf[n] = 0;
148*b43c1ca5SCharles.Forsyth 		if(Debug)
149*b43c1ca5SCharles.Forsyth 			print("oscmd: bad exec: %q\n", up->genbuf);
150*b43c1ca5SCharles.Forsyth 		error(up->genbuf);
151*b43c1ca5SCharles.Forsyth 		return nil;
152*b43c1ca5SCharles.Forsyth 	}
153*b43c1ca5SCharles.Forsyth 
154*b43c1ca5SCharles.Forsyth 	fd[0] = fd0[1];
155*b43c1ca5SCharles.Forsyth 	fd[1] = fd1[0];
156*b43c1ca5SCharles.Forsyth 	fd[2] = fd2[0];
157*b43c1ca5SCharles.Forsyth 	return t;
158*b43c1ca5SCharles.Forsyth 
159*b43c1ca5SCharles.Forsyth Error:
160*b43c1ca5SCharles.Forsyth 	r = errno;
161*b43c1ca5SCharles.Forsyth 	if(Debug)
162*b43c1ca5SCharles.Forsyth 		print("oscmd: %q\n",strerror(r));
163*b43c1ca5SCharles.Forsyth 	close(fd0[0]);
164*b43c1ca5SCharles.Forsyth 	close(fd0[1]);
165*b43c1ca5SCharles.Forsyth 	close(fd1[0]);
166*b43c1ca5SCharles.Forsyth 	close(fd1[1]);
167*b43c1ca5SCharles.Forsyth 	close(fd2[0]);
168*b43c1ca5SCharles.Forsyth 	close(fd2[1]);
169*b43c1ca5SCharles.Forsyth 	close(wfd[0]);
170*b43c1ca5SCharles.Forsyth 	close(wfd[1]);
171*b43c1ca5SCharles.Forsyth 	error(strerror(r));
172*b43c1ca5SCharles.Forsyth 	return nil;
173*b43c1ca5SCharles.Forsyth }
174*b43c1ca5SCharles.Forsyth 
175*b43c1ca5SCharles.Forsyth int
oscmdkill(void * a)176*b43c1ca5SCharles.Forsyth oscmdkill(void *a)
177*b43c1ca5SCharles.Forsyth {
178*b43c1ca5SCharles.Forsyth 	Targ *t = a;
179*b43c1ca5SCharles.Forsyth 
180*b43c1ca5SCharles.Forsyth 	if(Debug)
181*b43c1ca5SCharles.Forsyth 		print("kill: %d\n", t->pid);
182*b43c1ca5SCharles.Forsyth 	return kill(-t->pid, SIGTERM);
183*b43c1ca5SCharles.Forsyth }
184*b43c1ca5SCharles.Forsyth 
185*b43c1ca5SCharles.Forsyth int
oscmdwait(void * a,char * buf,int n)186*b43c1ca5SCharles.Forsyth oscmdwait(void *a, char *buf, int n)
187*b43c1ca5SCharles.Forsyth {
188*b43c1ca5SCharles.Forsyth 	Targ *t = a;
189*b43c1ca5SCharles.Forsyth 	int s;
190*b43c1ca5SCharles.Forsyth 
191*b43c1ca5SCharles.Forsyth 	if(waitpid(t->pid, &s, 0) == -1){
192*b43c1ca5SCharles.Forsyth 		if(Debug)
193*b43c1ca5SCharles.Forsyth 			print("wait error: %d [in %d] %q\n", t->pid, getpid(), strerror(errno));
194*b43c1ca5SCharles.Forsyth 		return -1;
195*b43c1ca5SCharles.Forsyth 	}
196*b43c1ca5SCharles.Forsyth 	if(WIFEXITED(s)){
197*b43c1ca5SCharles.Forsyth 		if(WEXITSTATUS(s) == 0)
198*b43c1ca5SCharles.Forsyth 			return snprint(buf, n, "%d 0 0 0 ''", t->pid);
199*b43c1ca5SCharles.Forsyth 		return snprint(buf, n, "%d 0 0 0 'exit: %d'", t->pid, WEXITSTATUS(s));
200*b43c1ca5SCharles.Forsyth 	}
201*b43c1ca5SCharles.Forsyth 	if(WIFSIGNALED(s)){
202*b43c1ca5SCharles.Forsyth 		if(WTERMSIG(s) == SIGTERM || WTERMSIG(s) == SIGKILL)
203*b43c1ca5SCharles.Forsyth 			return snprint(buf, n, "%d 0 0 0 killed", t->pid);
204*b43c1ca5SCharles.Forsyth 		return snprint(buf, n, "%d 0 0 0 'signal: %d'", t->pid, WTERMSIG(s));
205*b43c1ca5SCharles.Forsyth 	}
206*b43c1ca5SCharles.Forsyth 	return snprint(buf, n, "%d 0 0 0 'odd status: 0x%x'", t->pid, s);
207*b43c1ca5SCharles.Forsyth }
208*b43c1ca5SCharles.Forsyth 
209*b43c1ca5SCharles.Forsyth void
oscmdfree(void * a)210*b43c1ca5SCharles.Forsyth oscmdfree(void *a)
211*b43c1ca5SCharles.Forsyth {
212*b43c1ca5SCharles.Forsyth 	free(a);
213*b43c1ca5SCharles.Forsyth }
214