Пример #1
0
int
pipe_cloexec_nonblock(int fd[2])
{
#ifdef WIN32
    return _pipe(fd, 512, _O_BINARY);
#else
    int ret;

#ifdef HAVE_PIPE2
    ret = pipe2(fd, O_CLOEXEC|O_NONBLOCK);
    if (ret >= 0 || errno != ENOSYS)
        return ret;
#endif

    ret = pipe(fd);
    if (ret >= 0) {
        fd_set_cloexec(fd[0], true);
        fd_set_cloexec(fd[1], true);

        fd_set_nonblock(fd[0]);
        fd_set_nonblock(fd[1]);
    }

    return ret;
#endif
}
Пример #2
0
int
accept_cloexec_nonblock(int fd, struct sockaddr *address,
            size_t *address_length_r)
{
    int ret;
    socklen_t address_length = *address_length_r;

#ifdef HAVE_ACCEPT4
    ret = accept4(fd, address, &address_length,
              SOCK_CLOEXEC|SOCK_NONBLOCK);
    if (ret >= 0 || errno != ENOSYS) {
        if (ret >= 0)
            *address_length_r = address_length;

        return ret;
    }
#endif

    ret = accept(fd, address, &address_length);
    if (ret >= 0) {
        fd_set_cloexec(ret, true);
        fd_set_nonblock(ret);
        *address_length_r = address_length;
    }

    return ret;
}
Пример #3
0
int
socketpair_cloexec(int domain, int type, int protocol, int sv[2])
{
	int ret;

#ifdef SOCK_CLOEXEC
	ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv);
	if (ret >= 0 || errno != EINVAL)
		return ret;
#endif

	ret = socketpair(domain, type, protocol, sv);
	if (ret >= 0) {
		fd_set_cloexec(sv[0], true);
		fd_set_cloexec(sv[1], true);
	}

	return ret;
}
Пример #4
0
int
socketpair_cloexec_nonblock(int domain, int type, int protocol, int sv[2])
{
	int ret;

#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
	ret = socketpair(domain, type | SOCK_CLOEXEC | SOCK_NONBLOCK, protocol,
			 sv);
	if (ret >= 0 || errno != EINVAL)
		return ret;
#endif

	ret = socketpair(domain, type, protocol, sv);
	if (ret >= 0) {
		fd_set_cloexec(sv[0], true);
		fd_set_nonblock(sv[0]);
		fd_set_cloexec(sv[1], true);
		fd_set_nonblock(sv[1]);
	}

	return ret;
}
Пример #5
0
int
inotify_init_cloexec(void)
{
    int fd;

#ifdef HAVE_INOTIFY_INIT1
    fd = inotify_init1(IN_CLOEXEC);
    if (fd >= 0 || errno != ENOSYS)
        return fd;
#endif

    fd = inotify_init();
    if (fd >= 0)
        fd_set_cloexec(fd, true);

    return fd;
}
Пример #6
0
int
socket_cloexec_nonblock(int domain, int type, int protocol)
{
    int fd;

#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
    fd = socket(domain, type | SOCK_CLOEXEC | SOCK_NONBLOCK, protocol);
    if (fd >= 0 || errno != EINVAL)
        return fd;
#endif

    fd = socket(domain, type, protocol);
    if (fd >= 0) {
        fd_set_cloexec(fd, true);
        fd_set_nonblock(fd);
    }

    return fd;
}
Пример #7
0
int
open_cloexec(const char *path_fs, int flags, int mode)
{
    int fd;

#ifdef O_CLOEXEC
    flags |= O_CLOEXEC;
#endif

#ifdef O_NOCTTY
    flags |= O_NOCTTY;
#endif

    fd = open(path_fs, flags, mode);
    if (fd >= 0)
        fd_set_cloexec(fd, true);

    return fd;
}
Пример #8
0
ssize_t
recvmsg_cloexec(int sockfd, struct msghdr *msg, int flags)
{
#ifdef MSG_CMSG_CLOEXEC
	flags |= MSG_CMSG_CLOEXEC;
#endif

	ssize_t result = recvmsg(sockfd, msg, flags);
	if (result >= 0) {
		struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
		while (cmsg != NULL) {
			if (cmsg->cmsg_type == SCM_RIGHTS) {
				const int *fd_p = (const int *)CMSG_DATA(cmsg);
				fd_set_cloexec(*fd_p, true);
			}

			cmsg = CMSG_NXTHDR(msg, cmsg);
		}
	}

	return result;
}