static pid_t read_sandbox_pidfile(pid_t parent) {
	char *pidfile;
	if (asprintf(&pidfile, "%s/%d", RUN_FIREJAIL_SANDBOX_DIR, parent) == -1)
		errExit("asprintf");

	// open the pidfile
	EUID_ROOT();
	int pidfile_fd = open(pidfile, O_RDWR|O_CLOEXEC);
	free(pidfile);
	EUID_USER();
	if (pidfile_fd < 0)
		goto errexit;

	// assume pidfile is outdated if parent doesn't hold a lock
	struct flock pidfile_lock = {
		.l_type = F_WRLCK,
		.l_whence = SEEK_SET,
		.l_start = 0,
		.l_len = 0,
		.l_pid = 0,
	};
	if (fcntl(pidfile_fd, F_GETLK, &pidfile_lock) < 0)
		errExit("fcntl");
	if (pidfile_lock.l_type == F_UNLCK)
		goto errexit;
	if (pidfile_lock.l_pid != parent)
		goto errexit;

	// read pidfile
	pid_t sandbox;
	FILE *fp = fdopen(pidfile_fd, "r");
	if (!fp)
		errExit("fdopen");
	if (fscanf(fp, "%d", &sandbox) != 1)
		goto errexit;
	fclose(fp);

	return sandbox;

errexit:
	fprintf(stderr, "Error: no valid sandbox\n");
	exit(1);
}
static pid_t read_sandbox_pidfile(pid_t parent) {
	char *pidfile;
	if (asprintf(&pidfile, "%s/%d", RUN_FIREJAIL_SANDBOX_DIR, parent) == -1)
		errExit("asprintf");

	 
	EUID_ROOT();
	int pidfile_fd = open(pidfile, O_RDWR|O_CLOEXEC);
	free(pidfile);
	EUID_USER();
	if (pidfile_fd < 0)
		goto errexit;

	 
	struct flock pidfile_lock = {
		.l_type = F_WRLCK,
		.l_whence = SEEK_SET,
		.l_start = 0,
		.l_len = 0,
		.l_pid = 0,
	};
	if (fcntl(pidfile_fd, F_GETLK, &pidfile_lock) < 0)
		errExit("fcntl");
	if (pidfile_lock.l_type == F_UNLCK)
		goto errexit;
	if (pidfile_lock.l_pid != parent)
		goto errexit;

	 
	pid_t sandbox;
	FILE *fp = fdopen(pidfile_fd, "r");
	if (!fp)
		errExit("fdopen");
	if (fscanf(fp, "%d", &sandbox) != 1)
		goto errexit;
	fclose(fp);

	return sandbox;

errexit:
	fprintf(stderr, "Error: no valid sandbox\n");
	exit(1);
}