summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/src/run_proc_test.c
blob: 7cfb8703e994876d54bbc23518a530f426a6f9cd (plain)
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
/*
   run_proc test wrapper

   Copyright (C) Amitay Isaacs  2016

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"

#include <talloc.h>
#include <tevent.h>

#include "common/db_hash.c"
#include "common/run_proc.c"

int main(int argc, const char **argv)
{
	TALLOC_CTX *mem_ctx;
	struct tevent_context *ev;
	struct tevent_req *req;
	struct run_proc_context *run_ctx;
	struct timeval tv;
	char *output;
	struct run_proc_result result;
	pid_t pid;
	int timeout, ret, fd;
	bool status;

	if (argc < 4) {
		fprintf(stderr,
			"Usage: %s <timeout> <stdin-fd> <program> <args>\n",
			argv[0]);
		exit(1);
	}

	mem_ctx = talloc_new(NULL);
	if (mem_ctx == NULL) {
		fprintf(stderr, "talloc_new() failed\n");
		exit(1);
	}

	ev = tevent_context_init(mem_ctx);
	if (ev == NULL) {
		fprintf(stderr, "tevent_context_init() failed\n");
		exit(1);
	}

	timeout = atoi(argv[1]);
	if (timeout <= 0) {
		tv = tevent_timeval_zero();
	} else {
		tv = tevent_timeval_current_ofs(timeout, 0);
	}

	fd = atoi(argv[2]);
	if (fd < 0) {
		fd = -1;
	}

	ret = run_proc_init(mem_ctx, ev, &run_ctx);
	if (ret != 0) {
		fprintf(stderr, "run_proc_init() failed, ret=%d\n", ret);
		exit(1);
	}

	req = run_proc_send(mem_ctx, ev, run_ctx, argv[3], &argv[3], fd, tv);
	if (req == NULL) {
		fprintf(stderr, "run_proc_send() failed\n");
		exit(1);
	}

	tevent_req_poll(req, ev);

	status = run_proc_recv(req, &ret, &result, &pid, mem_ctx, &output);
	if (! status) {
		fprintf(stderr, "run_proc_recv() failed, ret=%d\n", ret);
		exit(1);
	}

	if (result.sig > 0) {
		printf("Process exited with signal %d\n", result.sig);
	} else if (result.err > 0) {
		printf("Process exited with error %d\n", result.err);
	} else {
		printf("Process exited with status %d\n", result.status);
	}

	if (pid != -1) {
		printf("Child = %d\n", pid);
	}

	if (output != NULL) {
		printf("Output = (%s)\n", output);
	}

	talloc_free(mem_ctx);

	exit(0);
}