コード例 #1
0
ファイル: netns_par_chld_ipv6.c プロジェクト: krytarowski/ltp
int main(void)
{
	int pid, status = 0, ret;
	long int flags = 0;

	setup();

	flags |= CLONE_NEWNS;
	flags |= CLONE_NEWNET;

	if (tst_kvercmp(2, 6, 19) < 0)
		tst_brkm(TCONF, NULL, "CLONE_NEWPID not supported");

	if ((pid = fork()) == 0) {

		// Child.
#if HAVE_UNSHARE
		ret = unshare(flags);
		if (ret < 0) {
			perror("unshare");
			tst_brkm(TFAIL,
				 NULL,
				 "Error:Unshare syscall failed for network namespace");
		}
#else
		tst_resm(TCONF, "System doesn't have unshare support");
#endif
		if (crtchild(CHILD_SCRIPT, NULL) != 0) {
			tst_brkm(TFAIL, NULL, "Failed running child script");
		}
	} else {

		//parent
		ret = system(PARENT_SCRIPT);
		status = WEXITSTATUS(ret);
		if (ret == -1 || status != 0) {
			tst_resm(TFAIL,
				 "Error: While running the IPv6 tests between \
parent & child NS");
			fflush(stdout);
			tst_exit();
		}
		fflush(stdout);

		ret = waitpid(pid, &status, __WALL);
		status = WEXITSTATUS(status);
		if (status != 0 || ret == -1) {
			tst_brkm(TFAIL, NULL,
				 "waitpid() returns %d, errno %d", ret,
				 errno);
		}
		tst_resm(TPASS, "par child ipv6");
		tst_exit();
	}
コード例 #2
0
ファイル: common.c プロジェクト: Altiscale/sig-core-t_ltp
/* The function to be executed in the child namespace */
int child_fn(void *c1)
{
	char *ltproot, *child;
	unsigned long flags = 0;
#if HAVE_UNSHARE
	int ret;
#endif

	/* Flags to unshare different Namespaces */
	flags |= CLONE_NEWNS;
	flags |= CLONE_NEWNET;
	flags |= CLONE_NEWUTS;
	flags |= CLONE_FS;

	ltproot = getenv("LTPROOT");

	if (!ltproot) {
		printf("LTPROOT env variable is not set\n");
		printf("Please set LTPROOT and re-run the test..\n");
		return -1;
	}

	child = malloc(FILENAME_MAX);
	if (child == NULL) {
		printf("FAIL: error while allocating memory");
		exit(1);
	}

	sprintf(child, "%s/testcases/bin/childns.sh", ltproot);

	/* Unshare the network namespace in the child */
#if HAVE_UNSHARE
	ret = unshare(flags);
	if (ret < 0) {
		perror("Failed to unshare for netns...");
		return 1;
	}
	return crtchild(child, c1);
#else
	printf("System doesn't support unshare.\n");
	return -1;
#endif
}