xref: /openbsd/usr.bin/make/cmd_exec.c (revision 905646f0)
1 /*	$OpenBSD: cmd_exec.c,v 1.11 2020/01/16 16:07:18 espie Exp $ */
2 /*
3  * Copyright (c) 2001 Marc Espie.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
18  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include "config.h"
33 #include "defines.h"
34 #include "cmd_exec.h"
35 #include "buf.h"
36 #include "memory.h"
37 #include "pathnames.h"
38 #include "job.h"
39 
40 char *
41 Cmd_Exec(const char *cmd, char **err)
42 {
43 	char	*args[4];	/* Args for invoking the shell */
44 	int 	fds[2]; 	/* Pipe streams */
45 	pid_t 	cpid;		/* Child PID */
46 	char	*result;	/* Result */
47 	int 	status; 	/* Command exit status */
48 	BUFFER	buf;		/* Buffer to store the result. */
49 	char	*cp;		/* Pointer into result. */
50 	ssize_t	cc;		/* Characters read from pipe. */
51 	size_t	length;		/* Total length of result. */
52 
53 
54 	*err = NULL;
55 
56 	/* Set up arguments for the shell. */
57 	args[0] = "sh";
58 	args[1] = "-c";
59 	args[2] = (char *)cmd;
60 	args[3] = NULL;
61 
62 	/* Open a pipe for retrieving shell's output. */
63 	if (pipe(fds) == -1) {
64 		*err = "Couldn't create pipe for \"%s\"";
65 		goto bad;
66 	}
67 
68 	/* Fork */
69 	switch (cpid = fork()) {
70 	case 0:
71 		reset_signal_mask();
72 		/* Close input side of pipe */
73 		(void)close(fds[0]);
74 
75 		/* Duplicate the output stream to the shell's output, then
76 		 * shut the extra thing down. Note we don't fetch the error
77 		 * stream: user can use redirection to grab it as this goes
78 		 * through /bin/sh.
79 		 */
80 		if (fds[1] != 1) {
81 			(void)dup2(fds[1], 1);
82 			(void)close(fds[1]);
83 		}
84 
85 		(void)execv(_PATH_BSHELL, args);
86 		_exit(1);
87 		/*NOTREACHED*/
88 
89 	case -1:
90 		*err = "Couldn't exec \"%s\"";
91 		goto bad;
92 
93 	default:
94 		/* No need for the writing half. */
95 		(void)close(fds[1]);
96 
97 		Buf_Init(&buf, MAKE_BSIZE);
98 
99 		do {
100 			char   grab[BUFSIZ];
101 
102 			cc = read(fds[0], grab, sizeof(grab));
103 			if (cc > 0)
104 				Buf_AddChars(&buf, cc, grab);
105 		} while (cc > 0 || (cc == -1 && errno == EINTR));
106 
107 		/* Close the input side of the pipe.  */
108 		(void)close(fds[0]);
109 
110 		/* Wait for the child to exit.  */
111 		while (waitpid(cpid, &status, 0) == -1 && errno == EINTR)
112 			continue;
113 
114 		if (cc == -1)
115 			*err = "Couldn't read shell's output for \"%s\"";
116 
117 		if (status)
118 			*err = "\"%s\" returned non-zero status";
119 
120 		length = Buf_Size(&buf);
121 		result = Buf_Retrieve(&buf);
122 
123 		/* The result is null terminated, Convert newlines to spaces. */
124 		cp = result + length - 1;
125 
126 		if (cp >= result && *cp == '\n')
127 			/* A final newline is just stripped.  */
128 			*cp-- = '\0';
129 
130 		while (cp >= result) {
131 			if (*cp == '\n')
132 				*cp = ' ';
133 			cp--;
134 		}
135 		break;
136 	}
137 	return result;
138     bad:
139 	return estrdup("");
140 }
141 
142