~lattis/muon

ref: 49a50d56c57dfdf470b4bcbffc16bc5ec92dcb4e muon/src/platform/run_cmd.c -rw-r--r-- 3.7 KiB
49a50d56Stone Tickle support comma seperated option array 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "posix.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

#include "buf_size.h"
#include "log.h"
#include "platform/filesystem.h"
#include "platform/mem.h"
#include "platform/run_cmd.h"

extern char *const *environ;

static bool
copy_pipe(int pipe, char **dest, uint32_t *len)
{
	uint32_t block_size = 256;
	uint32_t size = block_size + 1, i = 0;
	ssize_t b;

	char *buf = z_calloc(1, size);

	while (true) {
		b = read(pipe, &buf[i], block_size);

		if (b == -1) {
			return false;
		} else if (b < block_size) {
			i += b;
			break;
		}

		i += block_size;
		block_size *= 2;
		size += block_size;
		buf = z_realloc(buf, size);
		memset(&buf[i], 0, size - i);
	}

	*dest = buf;
	*len = i;
	return true;
}

bool
run_cmd(struct run_cmd_ctx *ctx, const char *_cmd, const char *const argv[], char *const envp[])
{
	pid_t pid;
	bool res = false, pipefd_out_open[2] = { 0 }, pipefd_err_open[2] = { 0 };
	int pipefd_out[2] = { 0 }, pipefd_err[2] = { 0 };

	const char *cmd;
	if (!fs_find_cmd(_cmd, &cmd)) {
		ctx->err_msg = "command not found";
		return false;
	}

	if (log_should_print(log_debug)) {
		LL("executing %s:", cmd);
		const char *const *ap;

		for (ap = argv; *ap; ++ap) {
			log_plain(" '%s'", *ap);
		}
		log_plain("\n");

		if (envp) {
			char *const *ap;

			LL("env:");
			for (ap = envp; *ap; ++ap) {
				log_plain(" '%s'", *ap);
			}
			log_plain("\n");
		}
	}

	if (pipe(pipefd_out) == -1) {
		goto err;
	}
	pipefd_out_open[0] = true;
	pipefd_out_open[1] = true;

	if (pipe(pipefd_err) == -1) {
		goto err;
	}
	pipefd_err_open[0] = true;
	pipefd_err_open[1] = true;

	if ((pid = fork()) == -1) {
		goto err;
	} else if (pid == 0 /* child */) {
		if (dup2(pipefd_out[1], 1) == -1) {
			log_plain("failed to dup stdout: %s", strerror(errno));
			exit(1);
		}
		if (dup2(pipefd_err[1], 2) == -1) {
			log_plain("failed to dup stderr: %s", strerror(errno));
			exit(1);
		}

		if (envp) {
			char *const *ap;
			for (ap = envp; *ap; ++ap) {
				char *const k = *ap;
				char *v = strchr(k, '=');
				assert(v);
				*v = 0;
				++v;

				int err;
				if ((err = setenv(k, v, 1)) != 0) {
					log_plain("failed to set environment: %s", strerror(err));
					exit(1);
				}
			}
		}

		if (execve(cmd, (char *const *)argv, environ) == -1) {
			log_plain("%s: %s", cmd, strerror(errno));
			exit(1);
		}

		abort();
	}

	/* parent */
	if (pipefd_err_open[1] && close(pipefd_err[1]) == -1) {
		LOG_E("failed to close: %s", strerror(errno));
	}
	pipefd_err_open[1] = false;

	if (pipefd_out_open[1] && close(pipefd_out[1]) == -1) {
		LOG_E("failed to close: %s", strerror(errno));
	}
	pipefd_out_open[1] = false;

	int status;
	if (waitpid(pid, &status, 0) != pid) {
		goto err;
	}

	if (WIFEXITED(status)) {
		ctx->status = WEXITSTATUS(status);

		if (!copy_pipe(pipefd_out[0], &ctx->out, &ctx->out_len)) {
			goto err;
		}
		if (!copy_pipe(pipefd_err[0], &ctx->err, &ctx->err_len)) {
			goto err;
		}

		res = true;
	} else {
		ctx->err_msg = "child exited abnormally";
	}
err:
	if (!res && !ctx->err_msg) {
		ctx->err_msg = errno ? strerror(errno) : "unknown";
	}

	if (pipefd_err_open[0] && close(pipefd_err[0]) == -1) {
		LOG_E("failed to close: %s", strerror(errno));
	}
	if (pipefd_err_open[1] && close(pipefd_err[1]) == -1) {
		LOG_E("failed to close: %s", strerror(errno));
	}
	if (pipefd_out_open[0] && close(pipefd_out[0]) == -1) {
		LOG_E("failed to close: %s", strerror(errno));
	}
	if (pipefd_out_open[1] && close(pipefd_out[1]) == -1) {
		LOG_E("failed to close: %s", strerror(errno));
	}

	return res;
}

void
run_cmd_ctx_destroy(struct run_cmd_ctx *ctx)
{
	if (ctx->out) {
		z_free(ctx->out);
	}

	if (ctx->err) {
		z_free(ctx->err);
	}
}