本文整理汇总了C++中set_cloexec函数的典型用法代码示例。如果您正苦于以下问题:C++ set_cloexec函数的具体用法?C++ set_cloexec怎么用?C++ set_cloexec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了set_cloexec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: serve
void serve(int sockfd)
{
int clfd;
FILE* fp;
char buf[BUFLEN];
set_cloexec(sockfd);
for(;;){
if((clfd = accept(sockfd,NULL,NULL)) < 0){
syslog(LOG_ERR,"ruptime: accept error: %s",strerror(errno));
exit(1);
}
set_cloexec(clfd);
if((fp = popen("/usr/bin/uptime","r")) == NULL){
sprintf(buf,"error: %s\n",strerror(errno));
send(clfd,buf,strlen(buf),0);
}else {
while(fgets(buf,BUFLEN,fp) != NULL){
send(clfd,buf,strlen(buf),0);
}
pclose(fp);
}
close(clfd);
}
}
示例2: cloexec_pipe
int cloexec_pipe(int fds[2])
{
#ifdef __linux__
return pipe2(fds, O_CLOEXEC);
#else
int ret = -1;
#ifndef _MSC_VER
pthread_mutex_lock(&cloexec_mutex);
if (pipe(fds))
#else
uv_mutex_lock(&cloexec_mutex);
if (_pipe(fds, 4096, O_BINARY) != 0)
#endif
goto Exit;
if (set_cloexec(fds[0]) != 0 || set_cloexec(fds[1]) != 0)
goto Exit;
ret = 0;
Exit:
#ifndef _MSC_VER
pthread_mutex_unlock(&cloexec_mutex);
#else
uv_mutex_unlock(&cloexec_mutex);
#endif
return ret;
#endif
}
示例3: open_sockets
int
open_sockets(void)
{
if ((socket_afnet = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return -1;
set_cloexec(socket_afnet);
sock_fd = _open_link_socket(&sock_nl);
set_cloexec(sock_fd);
return sock_fd;
}
示例4: init_sockets
int
init_sockets(void)
{
if ((socket_afnet = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return -1;
set_cloexec(socket_afnet);
if ((r_fd = socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
return -1;
set_cloexec(r_fd);
return 0;
}
示例5: iothread_init
static void iothread_init(void) {
static bool inited = false;
if (!inited) {
inited = true;
// Initialize the completion pipes.
int pipes[2] = {0, 0};
assert_with_errno(pipe(pipes) != -1);
s_read_pipe = pipes[0];
s_write_pipe = pipes[1];
set_cloexec(s_read_pipe);
set_cloexec(s_write_pipe);
}
}
示例6: server2
void server2(int sockfd)
{
int clfd, status;
pid_t pid;
set_cloexec(sockfd);
for(;;)
{
if ((clfd = accept(sockfd, NULL, NULL)) < 0){
syslog(LOG_ERR, "ruptimed: accept error: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if ((pid = fork()) < 0)
{
syslog(LOG_ERR, "ruptimed: fork error %s", strerror(errno));
exit(EXIT_FAILURE);
}
else if (pid == 0){
if (dup2(clfd, STDOUT_FILENO) != STDOUT_FILENO ||
dup2(clfd,STDERR_FILENO) != STDERR_FILENO)
{
syslog(LOG_ERR,"ruptimed: unexcepted error");
exit(EXIT_FAILURE);
}
close(clfd);
execl("/usr/bin/uptime", "uptime", (char *)0);
syslog(LOG_ERR, "ruptimed: unexcepted return from exec: %s", strerror(errno));
}
else
{
close(clfd);
waitpid(pid, &status, 0);
}
}
}
示例7: cloexec_socket
int cloexec_socket(int domain, int type, int protocol)
{
#ifdef __linux__
return socket(domain, type | SOCK_CLOEXEC, protocol);
#else
int fd = -1;
#ifndef _MSC_VER
pthread_mutex_lock(&cloexec_mutex);
#else
uv_mutex_lock(&cloexec_mutex);
#endif
if ((fd = socket(domain, type, protocol)) == -1)
goto Exit;
if (set_cloexec(fd) != 0) {
#ifndef _MSC_VER
close(fd);
#else
_close(fd);
#endif
fd = -1;
goto Exit;
}
Exit:
#ifndef _MSC_VER
pthread_mutex_unlock(&cloexec_mutex);
#else
uv_mutex_unlock(&cloexec_mutex);
#endif
return fd;
#endif
}
示例8: move_fd_to_unused
int move_fd_to_unused(int fd, const io_chain_t &io_chain, bool cloexec) {
if (fd < 0 || io_chain.get_io_for_fd(fd).get() == NULL) {
return fd;
}
// We have fd >= 0, and it's a conflict. dup it and recurse. Note that we recurse before
// anything is closed; this forces the kernel to give us a new one (or report fd exhaustion).
int new_fd = fd;
int tmp_fd;
do {
tmp_fd = dup(fd);
} while (tmp_fd < 0 && errno == EINTR);
assert(tmp_fd != fd);
if (tmp_fd < 0) {
// Likely fd exhaustion.
new_fd = -1;
} else {
// Ok, we have a new candidate fd. Recurse. If we get a valid fd, either it's the same as
// what we gave it, or it's a new fd and what we gave it has been closed. If we get a
// negative value, the fd also has been closed.
if (cloexec) set_cloexec(tmp_fd);
new_fd = move_fd_to_unused(tmp_fd, io_chain);
}
// We're either returning a new fd or an error. In both cases, we promise to close the old one.
assert(new_fd != fd);
int saved_errno = errno;
exec_close(fd);
errno = saved_errno;
return new_fd;
}
示例9: cloexec_accept
/*
* note: the socket must be in non-blocking mode, or the call might block while the mutex is being locked
*/
int cloexec_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
{
int fd = -1;
#ifndef _MSC_VER
pthread_mutex_lock(&cloexec_mutex);
#else
uv_mutex_lock(&cloexec_mutex);
#endif
if ((fd = accept(socket, addr, addrlen)) == -1)
goto Exit;
if (set_cloexec(fd) != 0) {
closesocket(fd);
fd = -1;
goto Exit;
}
Exit:
#ifndef _MSC_VER
pthread_mutex_unlock(&cloexec_mutex);
#else
uv_mutex_unlock(&cloexec_mutex);
#endif
return fd;
}
示例10: set_cloexec
static FILE *open_socket(void) {
int fd = -1;
struct sockaddr_un sa;
FILE *f = NULL;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
goto fail;
set_cloexec(fd);
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
sa.sun_path[sizeof(sa.sun_path)-1] = 0;
if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0)
goto fail;
if (!(f = fdopen(fd, "r+")))
goto fail;
return f;
fail:
if (fd >= 0)
close(fd);
return NULL;
}
示例11: binsh_in_filelist
static int binsh_in_filelist(const char *package)
{
const char * const cmd[] = {"dpkg-query", "-L", package, NULL};
pid_t child;
int sink;
FILE *in;
int found;
/*
* dpkg -L $package 2>/dev/null | ...
*
* Redirection of stderr is for quieter output
* when $package is not installed. If opening /dev/null
* fails, no problem; leave stderr alone in that case.
*/
sink = open("/dev/null", O_WRONLY);
if (sink >= 0)
set_cloexec(sink);
in = spawn_pipe(&child, cmd, sink);
/* ... | grep "^/bin/sh\$" */
found = has_binsh_line(in);
if (fclose(in))
die_errno("cannot close read end of pipe");
/*
* dpkg -L will error out if $package is not already installed.
*
* We stopped reading early if we found a match, so
* tolerate SIGPIPE in that case.
*/
wait_or_die(child, "dpkg-query -L", ERROR_OK |
(found ? SIGPIPE_OK : 0));
return found;
}
示例12: serve
void
serve(int sockfd)
{
int n;
socklen_t alen;
FILE *fp;
char buf[BUFLEN];
char abuf[MAXADDRLEN];
struct sockaddr *addr = (struct sockaddr *)abuf;
set_cloexec(sockfd);
for (;;) {
alen = MAXADDRLEN;
if ((n = recvfrom(sockfd, buf, BUFLEN, 0, addr, &alen)) < 0) {
syslog(LOG_ERR, "ruptimed: recvfrom error: %s",
strerror(errno));
exit(1);
}
if ((fp = popen("/usr/bin/uptime", "r")) == NULL) {
sprintf(buf, "error: %s\n", strerror(errno));
sendto(sockfd, buf, strlen(buf), 0, addr, alen);
} else {
if (fgets(buf, BUFLEN, fp) != NULL)
sendto(sockfd, buf, strlen(buf), 0, addr, alen); // 从recvfrom获得的发送者地址addr
pclose(fp);
}
}
}
示例13: open_socket
static int open_socket(void) {
int fd = -1;
struct sockaddr_un sa;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
goto fail;
}
if (set_cloexec(fd) < 0) {
daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
goto fail;
}
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
sa.sun_path[sizeof(sa.sun_path)-1] = 0;
if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
daemon_log(LOG_INFO, "Failed to connect to the daemon. This probably means that you");
daemon_log(LOG_INFO, "didn't start avahi-daemon before avahi-dnsconfd.");
goto fail;
}
return fd;
fail:
if (fd >= 0)
close(fd);
return -1;
}
示例14: malloc
u_link_origin *u_link_origin_create_from_fd(mowgli_eventloop_t *ev, int fd)
{
const char *operation;
u_link_origin *origin = NULL;
/* Set the fd to be close-on-exec. All listening sockets will be closed when
* we upgrade. This may cause some slight disturbance for users currently
* connecting, but this is acceptable.
*/
operation = "set close-on-exec";
if (set_cloexec(fd) < 0)
goto error;
u_log(LG_DEBUG, "u_link_origin_create_from_fd: %d", fd);
origin = malloc(sizeof(*origin));
operation = "create pollable";
if (!(origin->poll = mowgli_pollable_create(ev, fd, origin))) {
errno = -EINVAL; /* XXX */
goto error;
}
mowgli_node_add(origin, &origin->n, &all_origins);
mowgli_pollable_setselect(ev, origin->poll, MOWGLI_EVENTLOOP_IO_READ,
accept_ready);
return origin;
error:
u_perror(operation);
free(origin);
return NULL;
}
示例15: exec_pipe
int exec_pipe(int fd[2]) {
ASSERT_IS_MAIN_THREAD();
int res;
while ((res = pipe(fd))) {
if (errno != EINTR) {
return res; // caller will call wperror
}
}
debug(4, L"Created pipe using fds %d and %d", fd[0], fd[1]);
// Pipes ought to be cloexec. Pipes are dup2'd the corresponding fds; the resulting fds are not
// cloexec.
set_cloexec(fd[0]);
set_cloexec(fd[1]);
return res;
}