summaryrefslogtreecommitdiff
path: root/tests/attach-p-cmd-cmd.c
blob: aa0f3f588b46b2e50b6fb7dc5e2ffb0f32316a43 (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
/*
 * This file is part of attach-p-cmd strace test.
 *
 * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@altlinux.org>
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "attach-p-cmd.h"

static void
write_pidfile(const pid_t pid)
{
	FILE *fp = fopen(pidfile, "w");
	if (!fp)
		perror_msg_and_fail("fopen: %s", pidfile);

	if (fprintf(fp, "%d", pid) < 0)
		perror_msg_and_fail("fprintf: %s", pidfile);

	if (fclose(fp))
		perror_msg_and_fail("fclose: %s", pidfile);
}

static void
wait_for_peer_invocation(void)
{
	/* wait for the lock directory to be created by peer */
	while (rmdir(lockdir)) {
		if (ENOENT != errno)
			perror_msg_and_fail("rmdir: %s", lockdir);
	}
}

int
main(void)
{
	const pid_t pid = getpid();
	write_pidfile(pid);

	wait_for_peer_invocation();

	static const char dir[] = "attach-p-cmd.test cmd";
	int rc = chdir(dir);

	printf("%-5d chdir(\"%s\") = %s\n"
	       "%-5d +++ exited with 0 +++\n",
	       pid, dir, sprintrc(rc), pid);

	return 0;
}