xref: /illumos-gate/usr/src/cmd/mail/pipletr.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include "mail.h"
31 
32 int
33 dowait(pid_t pidval)
34 {
35 	pid_t w;
36 	int status;
37 	void (*istat)(), (*qstat)();
38 
39 	/*
40 		Parent temporarily ignores signals so it will remain
41 		around for command to finish
42 	*/
43 	istat = signal(SIGINT, SIG_IGN);
44 	qstat = signal(SIGQUIT, SIG_IGN);
45 
46 	while ((w = wait(&status)) != pidval && w != CERROR);
47 	if (w == CERROR) {
48 		status = -errno;
49 		signal(SIGINT, istat);
50 		signal(SIGQUIT, qstat);
51 		return (status);
52 	}
53 
54 	signal(SIGINT, istat);
55 	signal(SIGQUIT, qstat);
56 	status = ((status>>8)&0xFF);  		/* extract 8 high order bits */
57 	return (status);
58 }
59 
60 /*
61 	invoke shell to execute command waiting for command to terminate
62 		s	-> command string
63 	return:
64 		status	-> command exit status
65 */
66 int
67 systm(char *s)
68 {
69 	pid_t	pid;
70 
71 	/*
72 		Spawn the shell to execute command, however, since the
73 		mail command runs setgid mode reset the effective group
74 		id to the real group id so that the command does not
75 		acquire any special privileges
76 	*/
77 	if ((pid = fork()) == CHILD) {
78 		setuid(my_uid);
79 		setgid(my_gid);
80 #ifdef SVR3
81 		execl("/bin/sh", "sh", "-c", s, (char*)NULL);
82 #else
83 		execl("/usr/bin/sh", "sh", "-c", s, (char*)NULL);
84 #endif
85 		exit(127);
86 	}
87 	return (dowait(pid));
88 }
89