]> granicus.if.org Git - strace/blob - tests/attach-p-cmd-cmd.c
Update copyright headers
[strace] / tests / attach-p-cmd-cmd.c
1 /*
2  * This file is part of attach-p-cmd strace test.
3  *
4  * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <errno.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include "attach-p-cmd.h"
16
17 static void
18 write_pidfile(const pid_t pid)
19 {
20         FILE *fp = fopen(pidfile, "w");
21         if (!fp)
22                 perror_msg_and_fail("fopen: %s", pidfile);
23
24         if (fprintf(fp, "%d", pid) < 0)
25                 perror_msg_and_fail("fprintf: %s", pidfile);
26
27         if (fclose(fp))
28                 perror_msg_and_fail("fclose: %s", pidfile);
29 }
30
31 static void
32 wait_for_peer_invocation(void)
33 {
34         /* wait for the lock directory to be created by peer */
35         while (rmdir(lockdir)) {
36                 if (ENOENT != errno)
37                         perror_msg_and_fail("rmdir: %s", lockdir);
38         }
39 }
40
41 int
42 main(void)
43 {
44         const pid_t pid = getpid();
45         write_pidfile(pid);
46
47         wait_for_peer_invocation();
48
49         static const char dir[] = "attach-p-cmd.test cmd";
50         int rc = chdir(dir);
51
52         printf("%-5d chdir(\"%s\") = %s\n"
53                "%-5d +++ exited with 0 +++\n",
54                pid, dir, sprintrc(rc), pid);
55
56         return 0;
57 }