File: fd.c

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (107 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download | duplicates (3)
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
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/wait.h>

#include "zdtmtst.h"
#include "lock.h"

const char *test_doc = "Check that criu closes up all its descriptors";
const char *test_author = "Andrew Vagin <avagin@parallels.com>";

int main(int argc, char **argv)
{
	struct dirent *de;
	char pfd[PATH_MAX];
	mutex_t *lock;
	int status;
	pid_t pid;
	DIR *d;

	test_init(argc, argv);

	lock = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	if (lock == MAP_FAILED)
		return 1;

	mutex_init(lock);
	mutex_lock(lock);

	pid = fork();
	if (pid < 0) {
		pr_perror("fork()");
		return 1;
	}

	if (pid == 0) {
		d = opendir("/proc/self/fd");
		if (d == NULL)
			return 1;

		while ((de = readdir(d))) {
			int fd;

			if (de->d_name[0] == '.')
				continue;

			fd = atoi(de->d_name);
			if (dirfd(d) == fd)
				continue;
			close(fd);
		}

		closedir(d);
		mutex_unlock(lock);

		test_waitsig();

		return 0;
	}

	mutex_lock(lock);

	test_daemon();
	test_waitsig();

	snprintf(pfd, sizeof(pfd), "/proc/%d/fd", pid);
	d = opendir(pfd);
	if (d == NULL)
		return 2;

	while ((de = readdir(d))) {
		int ret;

		if (de->d_name[0] == '.')
			continue;

		ret = readlinkat(dirfd(d), de->d_name, pfd, sizeof(pfd) - 1);
		if (ret < 0) {
			pr_perror("readlink");
			ret = 0;
		}
		pfd[ret] = '\0';
		fail("Unexpected fd: %s -> %s", de->d_name, pfd);
		return 1;
	}

	closedir(d);
	kill(pid, SIGTERM);

	if (waitpid(pid, &status, 0) != pid) {
		pr_perror("waitpid()");
		return 1;
	}

	if (status != 0) {
		fail("%d:%d:%d:%d", WIFEXITED(status), WEXITSTATUS(status), WIFSIGNALED(status), WTERMSIG(status));
		return 1;
	}

	pass();

	return 0;
}