summaryrefslogtreecommitdiff
path: root/lib/util/tests/tfork.c
blob: 7963aafaae108eb1e81c6b2b292947a5a8811db8 (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
/*
 * Tests for tfork
 *
 * Copyright Ralph Boehme <slow@samba.org> 2017
 *
 * 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 "system/filesys.h"
#include "libcli/util/ntstatus.h"
#include "torture/torture.h"
#include "lib/util/data_blob.h"
#include "torture/local/proto.h"
#include "lib/util/tfork.h"
#include "lib/util/samba_util.h"
#include "lib/util/sys_rw.h"

static bool test_tfork_simple(struct torture_context *tctx)
{
	pid_t pid;
	pid_t parent = getpid();
	pid_t parent_arg;

	pid = tfork(NULL, &parent_arg);
	if (pid == 0) {
		torture_comment(tctx, "my parent pid is %d\n", parent);
		torture_assert(tctx, parent == parent_arg, "tfork failed\n");
		_exit(0);
	}
	if (pid == -1) {
		torture_fail(tctx, "tfork failed\n");
		return false;
	}

	return true;
}

static bool test_tfork_status(struct torture_context *tctx)
{
	pid_t child;
	int status;
	ssize_t nread;
	int status_fd = -1;
	bool ok = true;

	child = tfork(&status_fd, NULL);
	if (child == 0) {
		_exit(123);
	}
	if (child == -1) {
		torture_fail(tctx, "tfork failed\n");
		return false;
	}

	nread = sys_read(status_fd, &status, sizeof(status));
	if (nread != sizeof(status)) {
		torture_fail(tctx, "sys_read failed\n");
	}

	torture_assert_goto(tctx, WIFEXITED(status) == true, ok, done,
			    "tfork failed\n");
	torture_assert_goto(tctx, WEXITSTATUS(status) == 123, ok, done,
			    "tfork failed\n");

	torture_comment(tctx, "exit status [%d]\n", WEXITSTATUS(status));

done:
	if (status_fd != -1) {
		close(status_fd);
	}

	return ok;
}

struct torture_suite *torture_local_tfork(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite =
		torture_suite_create(mem_ctx, "tfork");

	torture_suite_add_simple_test(suite,
				      "tfork_simple",
				      test_tfork_simple);

	torture_suite_add_simple_test(suite,
				      "tfork_status",
				      test_tfork_status);

	return suite;
}