aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/process.c
blob: 732ca50eff19ba5566d909985ca39159ffc03cde (plain) (blame)
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
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "process.h"
#include "file.h"
#include "log.h"

#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

static int exec_child(const char *args[], const char *envp[])
{
	static const char *default_envp[] = {NULL};

	if (!envp)
		envp = default_envp;

	int ret = execvpe(args[0], (char *const *)args, (char *const *)envp);
	if (ret < 0) {
		log_errno("execvpe");
		return ret;
	}

	return ret;
}

static int wait_for_child(pid_t pid, int *ec)
{
	int status;

	pid_t ret = waitpid(pid, &status, 0);
	if (ret < 0) {
		log_errno("waitpid");
		return ret;
	}

	if (WIFEXITED(status))
		*ec = WEXITSTATUS(status);
	else
		*ec = -1;

	return 0;
}

int proc_spawn(const char *args[], const char *envp[], int *ec)
{
	pid_t child_pid = fork();
	if (child_pid < 0) {
		log_errno("fork");
		return child_pid;
	}

	if (!child_pid)
		exit(exec_child(args, envp));

	return wait_for_child(child_pid, ec);
}

static int redirect_and_exec_child(int pipe_fds[2], const char *args[], const char *envp[])
{
	int ret = 0;

	file_close(pipe_fds[0]);

	ret = dup2(pipe_fds[1], STDOUT_FILENO);
	if (ret < 0) {
		log_errno("dup2");
		return ret;
	}

	ret = dup2(pipe_fds[1], STDERR_FILENO);
	if (ret < 0) {
		log_errno("dup2");
		return ret;
	}

	return exec_child(args, envp);
}

int proc_capture(const char *args[], const char *envp[], struct proc_output *result)
{
	static const int flags = O_CLOEXEC;
	int pipe_fds[2];
	int ret = 0;

	ret = pipe2(pipe_fds, flags);
	if (ret < 0) {
		log_errno("pipe2");
		return -1;
	}

	pid_t child_pid = fork();
	if (child_pid < 0) {
		log_errno("fork");
		goto close_pipe;
	}

	if (!child_pid)
		exit(redirect_and_exec_child(pipe_fds, args, envp));

	file_close(pipe_fds[1]);

	ret = file_read(pipe_fds[0], &result->output, &result->output_len);
	if (ret < 0)
		goto close_pipe;

	ret = wait_for_child(child_pid, &result->ec);
	if (ret < 0)
		goto free_output;

	goto close_pipe;

free_output:
	free(result->output);

close_pipe:
	file_close(pipe_fds[0]);
	/* No errno checking here, we might've already closed the write end. */
	close(pipe_fds[1]);

	return ret;
}

void proc_output_init(struct proc_output *output)
{
	output->ec = 0;
	output->output = NULL;
	output->output_len = 0;
}

void proc_output_free(const struct proc_output *output)
{
	free(output->output);
}

void proc_output_dump(const struct proc_output *output)
{
	log("Process exit code: %d\n", output->ec);
	log("Process output:\n%s", output->output);
	if (!output->output || !output->output_len ||
	    output->output[output->output_len - 1] != '\n')
		log("\n");
}