summaryrefslogtreecommitdiff
path: root/lib/util
diff options
context:
space:
mode:
authorRalph Boehme <slow@samba.org>2017-04-11 17:32:01 +0200
committerRalph Boehme <slow@samba.org>2017-04-20 16:53:16 +0200
commit3ce1060f781ff5b97582542861f83129b16a8bbf (patch)
treeb87898ccb0696cc1b8ce7109b123960126ded625 /lib/util
parent6b950ae37e80eccea7ed0a831497f6c7923f6d03 (diff)
downloadsamba-3ce1060f781ff5b97582542861f83129b16a8bbf.tar.gz
lib/util: add a test for tfork()
Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/tests/tfork.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/lib/util/tests/tfork.c b/lib/util/tests/tfork.c
new file mode 100644
index 00000000000..7963aafaae1
--- /dev/null
+++ b/lib/util/tests/tfork.c
@@ -0,0 +1,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;
+}