xref: /minix3/minix/lib/libc/sys/setpgid.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include <sys/cdefs.h>
2*433d6423SLionel Sambuc #include <lib.h>
3*433d6423SLionel Sambuc #include "namespace.h"
4*433d6423SLionel Sambuc 
5*433d6423SLionel Sambuc #include <string.h>
6*433d6423SLionel Sambuc 
7*433d6423SLionel Sambuc #include <unistd.h>
8*433d6423SLionel Sambuc 
9*433d6423SLionel Sambuc /*
10*433d6423SLionel Sambuc  * "Smart" stub for now. This requires job control to be properly implemented.
11*433d6423SLionel Sambuc  */
12*433d6423SLionel Sambuc int setpgid(pid_t pid, pid_t pgid)
13*433d6423SLionel Sambuc {
14*433d6423SLionel Sambuc 	pid_t _pid, _pgid, sid, cpid;
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc 	_pid = pid;
17*433d6423SLionel Sambuc 	_pgid = pgid;
18*433d6423SLionel Sambuc 
19*433d6423SLionel Sambuc 	/* Who are we? */
20*433d6423SLionel Sambuc 	cpid = getpid();
21*433d6423SLionel Sambuc 
22*433d6423SLionel Sambuc 	/* if zero, means current process. */
23*433d6423SLionel Sambuc 	if (_pid == 0) {
24*433d6423SLionel Sambuc 		_pid = cpid;
25*433d6423SLionel Sambuc 	}
26*433d6423SLionel Sambuc 
27*433d6423SLionel Sambuc 	/* if zero, means given pid. */
28*433d6423SLionel Sambuc 	if (_pgid == 0) {
29*433d6423SLionel Sambuc 		_pgid = _pid;
30*433d6423SLionel Sambuc 	}
31*433d6423SLionel Sambuc 
32*433d6423SLionel Sambuc 	/* right now we only support the equivalent of setsid(), which is
33*433d6423SLionel Sambuc 	 * setpgid(0,0) */
34*433d6423SLionel Sambuc 	if ((_pid != cpid) || (_pgid != cpid)) {
35*433d6423SLionel Sambuc 	    errno = EINVAL;
36*433d6423SLionel Sambuc 	    return -1;
37*433d6423SLionel Sambuc 	}
38*433d6423SLionel Sambuc 
39*433d6423SLionel Sambuc 	if (setsid() == cpid) {
40*433d6423SLionel Sambuc 		return 0;
41*433d6423SLionel Sambuc 	} else {
42*433d6423SLionel Sambuc 		return -1;
43*433d6423SLionel Sambuc 	}
44*433d6423SLionel Sambuc }
45