tools/testing/selftests/pid_namespace/pid_max.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/pid_namespace/pid_max.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/pid_namespace/pid_max.c
Extension
.c
Size
9152 bytes
Lines
452
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (pid > inner_limit) {
			fprintf(stderr, "Managed to create pid number beyond limit\n");
			return -1;
		}
	}

	return 0;
}

static int pid_max_nested_inner(void *data)
{
	int fret = -1;
	pid_t pids[2];
	int fd, i, ret;

	ret = mount("", "/", NULL, MS_PRIVATE | MS_REC, 0);
	if (ret) {
		fprintf(stderr, "%m - Failed to make rootfs private mount\n");
		return fret;
	}

	umount2("/proc", MNT_DETACH);

	ret = mount("proc", "/proc", "proc", 0, NULL);
	if (ret) {
		fprintf(stderr, "%m - Failed to mount proc\n");
		return fret;
	}

	fd = open("/proc/sys/kernel/pid_max", O_RDWR | O_CLOEXEC | O_NOCTTY);
	if (fd < 0) {
		fprintf(stderr, "%m - Failed to open pid_max\n");
		return fret;
	}

	ret = write_int_to_fd(fd, inner_limit);
	close(fd);
	if (ret < 0) {
		fprintf(stderr, "%m - Failed to write pid_max\n");
		return fret;
	}

	pids[0] = fork();
	if (pids[0] < 0) {
		fprintf(stderr, "Failed to create first new process\n");
		return fret;
	}

	if (pids[0] == 0)
		exit(EXIT_SUCCESS);

	pids[1] = fork();
	wait_for_pid(pids[0]);
	if (pids[1] >= 0) {
		if (pids[1] == 0)
			exit(EXIT_SUCCESS);
		wait_for_pid(pids[1]);

		fprintf(stderr, "Managed to create process even though ancestor pid namespace had a limit\n");
		return fret;
	}

	/* Now make sure that we wrap pids at outer_limit. */
	for (i = 0; i < inner_limit + 10; i++) {
		pid_t pid;

		pid = fork();
		if (pid < 0)
			return fret;

		if (pid == 0)
			exit(EXIT_SUCCESS);

		wait_for_pid(pid);
		if (pid >= inner_limit) {
			fprintf(stderr, "Managed to create process with pid %d beyond configured limit\n", pid);
			return fret;
		}
	}

	return 0;
}

static int pid_max_nested_outer(void *data)
{
	int fret = -1, nr_procs = 0;
	pid_t *pids;
	int fd, ret;
	pid_t pid;

Annotation

Implementation Notes