aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_add_output_document.c
blob: 9eb1f8d26c0f30d9d453205e301f7a9dd771b487 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libexec_add_output_document(struct libexec_command *cmd, int fd, struct libexec_document *doc, int flags)
{
	int fd_flags, fl_flags, fds[2];

	if (!cmd || fd < 0 || !doc) {
		errno = EINVAL;
		return -1;
	}

	fd_flags = (flags & O_CLOEXEC);
	fl_flags = (flags ^ fd_flags);
	if (fd_flags)
		fd_flags = FD_CLOEXEC;

	if (pipe(fds))
		return -1;
	if (fd_flags && fcntl(fds[0], F_SETFD, fd_flags))
		goto fail;
	if (fl_flags && fcntl(fds[0], F_SETFL, fl_flags))
		goto fail;

	doc->fd = fds[0];

	if (libexec_dup(cmd, fd, fds[1]))
		goto fail;
	cmd->plumings[cmd->nplumings - 1].type = LIBEXEC_PLUMING_PIPE;

	return 0;

fail:
	close(fds[0]);
	close(fds[1]);
	return -1;
}


#else
LIBEXEC_CONST__ int main(void) {return 0;} /* TODO test */
#endif