当前位置: 首页>>代码示例>>C++>>正文


C++ closefrom函数代码示例

本文整理汇总了C++中closefrom函数的典型用法代码示例。如果您正苦于以下问题:C++ closefrom函数的具体用法?C++ closefrom怎么用?C++ closefrom使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了closefrom函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ATF_TC_BODY

ATF_TC_BODY(closefrom_buffer, tc)
{
	int buf[16], cur, half;
	size_t i;

	/*
	 * Open a buffer of descriptors, close the half of
	 * these and verify that the result is consistent.
	 */
	ATF_REQUIRE(closefrom(STDERR_FILENO + 1) == 0);

	cur = fcntl(0, F_MAXFD);
	ATF_REQUIRE(cur == STDERR_FILENO);

	for (i = 0; i < __arraycount(buf); i++) {
		buf[i] = open(path, O_RDWR | O_CREAT, 0600);
		ATF_REQUIRE(buf[i] >= 0);
	}

	cur = fcntl(0, F_MAXFD);
	ATF_REQUIRE(cur == __arraycount(buf) + STDERR_FILENO);

	half = STDERR_FILENO + __arraycount(buf) / 2;
	ATF_REQUIRE(closefrom(half) == 0);

	cur = fcntl(0, F_MAXFD);
	ATF_REQUIRE(cur == half - 1);

	for (i = 0; i < __arraycount(buf); i++)
		(void)close(buf[i]);
}
开发者ID:2asoft,项目名称:freebsd,代码行数:31,代码来源:t_closefrom.c

示例2: main

/*
 * Test that closefrom does the right thing in a threaded programs,
 * specifically that it doesn't kill the thread kernel signal pipe.
 */
int
main(int argc, char *argv[])
{
	pthread_t thread;
	void *status;
	int fd;
	int result;

	/* close files above stderr.   The kernel pipes shouldn't be touched */
	fd = STDERR_FILENO + 1;
	result = closefrom(fd);
	printf("closefrom(%d) == %d/%d\n", fd, result, errno);

	/* do it again: make sure that the result is -1/EBADF */
	result = closefrom(fd);
	printf("closefrom(%d) == %d/%d\n", fd, result, errno);
	ASSERT(result == -1 && errno == EBADF);

	/* start a thread to verify the thread kernel is working */
	CHECKr(pthread_create(&thread, NULL, dummy_thread, NULL));
	CHECKr(pthread_join(thread, &status));
	printf("dummy thread exited with status %p\n", status);

	SUCCEED;
	return 0;
}
开发者ID:repos-holder,项目名称:openbsd-patches,代码行数:30,代码来源:closefrom.c

示例3: main

int
main(void)
{
	int i, max, fds[NUM_OPENS];
	char buf[512];

	for (i = 0; i < NUM_OPENS; i++)
		if ((fds[i] = open("/dev/null", O_RDONLY)) == -1)
			exit(0);	/* can't test */
	max = i - 1;

	/* should close last fd only */
	closefrom(fds[max]);
	if (close(fds[max]) != -1)
		fail("failed to close highest fd");

	/* make sure we can still use remaining descriptors */
	for (i = 0; i < max; i++)
		if (read(fds[i], buf, sizeof(buf)) == -1)
			fail("closed descriptors it should not have");

	/* should close all fds */
	closefrom(fds[0]);
	for (i = 0; i < NUM_OPENS; i++)
		if (close(fds[i]) != -1)
			fail("failed to close from lowest fd");
}
开发者ID:cryptography-training,项目名称:debian-rng,代码行数:27,代码来源:closefromtest.c

示例4: detachfromtty

/*
 * detach from tty
 */
static void
detachfromtty(void)
{
	nscd_rc_t	rc;
	char		*me = "detachfromtty";

	if (_logfd > 0) {
		int i;
		for (i = 0; i < _logfd; i++)
			(void) close(i);
		closefrom(_logfd + 1);
	} else
		closefrom(0);

	(void) chdir("/");

	switch (fork1()) {
	case (pid_t)-1:

		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to fork: pid = %d, %s\n",
		    getpid(), strerror(errno));

		exit(1);
		break;
	case 0:
		/* start the forker nscd if so configured */
		_nscd_start_forker(saved_execname, saved_argc, saved_argv);
		break;
	default:
		exit(0);
	}

	(void) setsid();
	(void) open("/dev/null", O_RDWR, 0);
	(void) dup(0);
	if (_logfd != 2)
		(void) dup(0);

	/*
	 * start monitoring the states of the name service clients
	 */
	rc = _nscd_init_smf_monitor();
	if (rc != NSCD_SUCCESS) {
		_NSCD_LOG(NSCD_LOG_FRONT_END, NSCD_LOG_LEVEL_ERROR)
	(me, "unable to start the SMF monitor (rc = %d)\n", rc);

		exit(-1);
	}
}
开发者ID:AlainODea,项目名称:illumos-gate,代码行数:53,代码来源:server.c

示例5: ctl_openconsole

__dead void
ctl_openconsole(const char *name)
{
	closefrom(STDERR_FILENO + 1);
	execl(VMCTL_CU, VMCTL_CU, "-l", name, "-s", "9600", NULL);
	err(1, "failed to open the console");
}
开发者ID:darksoul42,项目名称:bitrig,代码行数:7,代码来源:main.c

示例6: shell_exec

__dead void
shell_exec(const char *shell, const char *shellcmd)
{
	const char	*shellname, *ptr;
	char		*argv0;

	ptr = strrchr(shell, '/');
	if (ptr != NULL && *(ptr + 1) != '\0')
		shellname = ptr + 1;
	else
		shellname = shell;
	if (login_shell)
		xasprintf(&argv0, "-%s", shellname);
	else
		xasprintf(&argv0, "%s", shellname);
	setenv("SHELL", shell, 1);

	setblocking(STDIN_FILENO, 1);
	setblocking(STDOUT_FILENO, 1);
	setblocking(STDERR_FILENO, 1);
	closefrom(STDERR_FILENO + 1);

	execl(shell, argv0, "-c", shellcmd, (char *) NULL);
	fatal("execl failed");
}
开发者ID:MarvinZhuang,项目名称:tmate,代码行数:25,代码来源:tmux.c

示例7: shell_exec

__dead void
shell_exec(const char *shell, const char *shellcmd)
{
	const char	*shellname, *ptr;
	char		*argv0;
	int		 mode;

	ptr = strrchr(shell, '/');
	if (ptr != NULL && *(ptr + 1) != '\0')
		shellname = ptr + 1;
	else
		shellname = shell;
	if (login_shell)
		xasprintf(&argv0, "-%s", shellname);
	else
		xasprintf(&argv0, "%s", shellname);
	setenv("SHELL", shell, 1);

	if ((mode = fcntl(STDIN_FILENO, F_GETFL)) != -1)
		fcntl(STDIN_FILENO, F_SETFL, mode & ~O_NONBLOCK);
	if ((mode = fcntl(STDOUT_FILENO, F_GETFL)) != -1)
		fcntl(STDOUT_FILENO, F_SETFL, mode & ~O_NONBLOCK);
	if ((mode = fcntl(STDERR_FILENO, F_GETFL)) != -1)
		fcntl(STDERR_FILENO, F_SETFL, mode & ~O_NONBLOCK);
	closefrom(STDERR_FILENO + 1);

	execl(shell, argv0, "-c", shellcmd, (char *) NULL);
	fatal("execl failed");
}
开发者ID:ddollar,项目名称:tmux,代码行数:29,代码来源:tmux.c

示例8: daemonize_self

static int
daemonize_self(void)
{
	pid_t pid;
	int fd;

	(void) close(STDIN_FILENO);

	if ((fd = open(DEV_NULL, O_RDONLY)) == -1) {
		(void) printf("Could not open /dev/null: %s\n",
		    strerror(errno));
	} else if (fd != STDIN_FILENO) {
		(void) dup2(fd, STDIN_FILENO);
		(void) close(fd);
	}
	(void) dup2(STDERR_FILENO, STDOUT_FILENO);
	closefrom(3);

	if ((pid = fork1()) < 0) {
		(void) printf("fork() failed: %s\n", strerror(errno));
		return (1);
	}

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

	(void) setsid();
	(void) chdir("/");

	return (0);
}
开发者ID:AlainODea,项目名称:illumos-gate,代码行数:31,代码来源:ipfd.c

示例9: has_fs

static int
has_fs(char *prog, char *slice)
{
	pid_t	pid;
	int	loc;
	mode_t	mode = S_IRUSR | S_IWUSR;

	switch ((pid = fork1())) {
	case 0:
	    /* child process */

	    closefrom(1);
	    (void) open("/dev/null", O_WRONLY, mode);
	    (void) open("/dev/null", O_WRONLY, mode);
	    (void) execl(prog, "fstyp", slice, NULL);
	    _exit(1);
	    break;

	case -1:
	    return (0);

	default:
	    /* parent process */
	    break;
	}

	(void) waitpid(pid, &loc, 0);

	if (WIFEXITED(loc) && WEXITSTATUS(loc) == 0) {
	    return (1);
	}

	return (0);
}
开发者ID:apprisi,项目名称:illumos-gate,代码行数:34,代码来源:inuse_fs.c

示例10: close_all_beyond

/*
 * close_all_beyond()
 *
 * Close all file descriptors after the given keep_fd, which is the
 * highest fd to keep open.  See
 *
 * http://stackoverflow.com/questions/899038/getting-the-highest-allocated-file-descriptor
 */
void
close_all_beyond(
	int keep_fd
	)
{
# ifdef HAVE_CLOSEFROM
	closefrom(keep_fd + 1);
# elif defined(F_CLOSEM)
	/*
	 * From 'Writing Reliable AIX Daemons,' SG24-4946-00,
	 * by Eric Agar (saves us from doing 32767 system
	 * calls)
	 */
	if (fcntl(keep_fd + 1, F_CLOSEM, 0) == -1)
		msyslog(LOG_ERR, "F_CLOSEM(%d): %m", keep_fd + 1);
# else	/* !HAVE_CLOSEFROM && !F_CLOSEM follows */
	int fd;
	int max_fd;

	/* includes POSIX case */
	max_fd = GETDTABLESIZE();
	for (fd = keep_fd + 1; fd < max_fd; fd++)
		close(fd);
# endif	/* !HAVE_CLOSEFROM && !F_CLOSEM */
}
开发者ID:ntpsec,项目名称:ntpsec,代码行数:33,代码来源:ntp_worker.c

示例11: mproc_fork

int
mproc_fork(struct mproc *p, const char *path, char *argv[])
{
	int sp[2];

	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0)
		return (-1);

	io_set_nonblocking(sp[0]);
	io_set_nonblocking(sp[1]);

	if ((p->pid = fork()) == -1)
		goto err;

	if (p->pid == 0) {
		/* child process */
		dup2(sp[0], STDIN_FILENO);
		if (closefrom(STDERR_FILENO + 1) < 0)
			exit(1);

		execv(path, argv);
		err(1, "execv: %s", path);
	}

	/* parent process */
	close(sp[0]);
	mproc_init(p, sp[1]);
	return (0);

err:
	log_warn("warn: Failed to start process %s, instance of %s", argv[0], path);
	close(sp[0]);
	close(sp[1]);
	return (-1);
}
开发者ID:OpenSMTPD,项目名称:OpenSMTPD,代码行数:35,代码来源:mproc.c

示例12: closer

static void *
closer(void *arg)
{

	pthread_mutex_lock(&closermtx);
	while (!quit) {
		while (!riseandwhine)
			pthread_cond_wait(&closercv, &closermtx);
		riseandwhine = 0;
		pthread_mutex_unlock(&closermtx);

		/* try to catch a random slot */
		usleep(random() % 100000);

		/*
		 * wide-angle disintegration beam, but takes care
		 * of the client rumpkernel communication socket.
		 */
		closefrom(3);

		pthread_mutex_lock(&closermtx);
	}
	pthread_mutex_unlock(&closermtx);

	return NULL;
}
开发者ID:2asoft,项目名称:freebsd,代码行数:26,代码来源:h_reconcli.c

示例13: pipeline

static int
pipeline(char **ppargv, char **asargv)
{
	int pipedes[4];
	int active = 0;
	int rval = 0;
	pid_t pid_pp, pid_f, pid_as;

	if (pipe(pipedes) == -1 || pipe(pipedes + 2) == -1) {
		perror("pipe");
		return (4);
	}

	if ((pid_pp = invoke(ppargv, -1, pipedes[0])) > 0)
		active++;

	if (verbose)
		(void) fprintf(stderr, "| ");

	if ((pid_f = filter(pipedes[1], pipedes[2])) > 0)
		active++;

	if (verbose)
		(void) fprintf(stderr, "| ");

	if ((pid_as = invoke(asargv, pipedes[3], -1)) > 0)
		active++;

	if (verbose) {
		(void) fprintf(stderr, "\n");
		(void) fflush(stderr);
	}

	closefrom(3);

	if (active != 3)
		return (5);

	while (active != 0) {
		pid_t pid;
		int stat;

		if ((pid = wait(&stat)) == -1) {
			rval++;
			break;
		}

		if (!WIFEXITED(stat))
			continue;

		if (pid == pid_pp || pid == pid_f || pid == pid_as) {
			active--;
			if (WEXITSTATUS(stat) != 0)
				rval++;
		}
	}

	return (rval);
}
开发者ID:pcd1193182,项目名称:openzfs,代码行数:59,代码来源:aw.c

示例14: startChild

struct sandbox *
startChild(void *data)
{
	int procd, pid, sv[2];
	struct sandbox *newsandbox;

	if ((newsandbox = malloc(sizeof (struct sandbox))) == NULL)
		err(1, "Couldn't allocate memory for sandbox");

	sv[0] = sv[1] = 0;
	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) < 0 )
		err(1, "zcaplib: socketpair()");

	pid = pdfork(&procd, 0);
	if (pid == 0 ) {
		cap_rights_t stdin_cap;
		cap_rights_t stdout_cap;
		cap_rights_t stderr_cap;
		cap_rights_t socket_cap;

		cap_rights_init(&stdin_cap, CAP_READ);
		cap_rights_init(&stderr_cap, CAP_WRITE, CAP_FSTAT);
		cap_rights_init(&stdout_cap, CAP_WRITE);

		if (dup2(sv[0], 3) != 3)
			err(1, "Couldn't duplicate fd");
		closefrom(4);

		cap_rights_init(&socket_cap, CAP_WRITE, CAP_READ, CAP_POLL_EVENT);

		if (cap_rights_limit(STDIN_FILENO, &stdin_cap) < 0)
			err(1, "Couldn't limit stdin");
		if (cap_rights_limit(STDOUT_FILENO, &stdout_cap) < 0)
			err(1, "Couldn't limit stdout");
		if (cap_rights_limit(STDERR_FILENO, &stderr_cap) < 0)
			err(1, "Couldn't limit stderr");
		if (cap_rights_limit(3, &socket_cap) < 0)
			err(1, "Couldn't limit sandbox socket");

		/* execl() zlibworker */
		if ( execl("/usr/libexec/zlibworker", "zlibworker", NULL) < 0)
			err(1, "Couldn't find zlibworker.");

		exit(0);
	} else if (pid == -1) {
		err(1, "Couldn't fork");
	} else {
		signal(SIGCHLD, suicide);
		atexit(killChild);
		newsandbox->dataptr = data;
		newsandbox->pd = procd;
		newsandbox->socket = sv[1];
		debug("DEBUG: We have started a new sandbox.\n");
		debug("\tdata: %p pd: %d, socket: %d\n", data, newsandbox->pd, newsandbox->socket);
	}
	return (newsandbox);
}
开发者ID:dpl0,项目名称:soc2013,代码行数:57,代码来源:capsicum.c

示例15: queue_proc_init

static int
queue_proc_init(struct passwd *pw, int server)
{
	int		sp[2];
	uint32_t	version;

	errno = 0;

	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) {
		log_warn("warn: queue-proc: socketpair");
		return (0);
	}

	if ((pid = fork()) == -1) {
		log_warn("warn: queue-proc: fork");
		goto err;
	}

	if (pid == 0) {
		/* child process */
		dup2(sp[0], STDIN_FILENO);
		if (closefrom(STDERR_FILENO + 1) < 0)
			exit(1);

		execl(execpath, "queue_ramproc", NULL);
		err(1, "execl");
	}

	/* parent process */
	close(sp[0]);
	imsg_init(&ibuf, sp[1]);

	version = PROC_QUEUE_API_VERSION;
	imsg_compose(&ibuf, PROC_QUEUE_INIT, 0, 0, -1,
	    &version, sizeof(version));

	queue_api_on_message_create(queue_proc_message_create);
	queue_api_on_message_commit(queue_proc_message_commit);
	queue_api_on_message_delete(queue_proc_message_delete);
	queue_api_on_message_fd_r(queue_proc_message_fd_r);
	queue_api_on_message_corrupt(queue_proc_message_corrupt);
	queue_api_on_envelope_create(queue_proc_envelope_create);
	queue_api_on_envelope_delete(queue_proc_envelope_delete);
	queue_api_on_envelope_update(queue_proc_envelope_update);
	queue_api_on_envelope_load(queue_proc_envelope_load);
	queue_api_on_envelope_walk(queue_proc_envelope_walk);

	queue_proc_call();
	queue_proc_end();

	return (1);

err:
	close(sp[0]);
	close(sp[1]);
	return (0);
}
开发者ID:appleorange1,项目名称:bitrig,代码行数:57,代码来源:queue_proc.c


注:本文中的closefrom函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。