diff options
| author | Lennart Poettering <lennart@poettering.net> | 2009-10-30 03:30:42 +0100 | 
|---|---|---|
| committer | Lennart Poettering <lennart@poettering.net> | 2009-10-30 03:30:42 +0100 | 
| commit | 9c1a98953f25aff7f11af80a073c9c46dee2438c (patch) | |
| tree | bf0fbe07d3c02723808cba0c377e9fc2b545d522 /src/pulsecore/core-util.c | |
| parent | 754644fa6e1ec83dba85bf586a2b09c2283aa096 (diff) | |
core-util: introduce FD_CLOEXEC wrappers for open/socket/pipe/accept
Diffstat (limited to 'src/pulsecore/core-util.c')
| -rw-r--r-- | src/pulsecore/core-util.c | 79 | 
1 files changed, 79 insertions, 0 deletions
| diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 27e09cbc..34516eea 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -2889,3 +2889,82 @@ const char *pa_get_temp_dir(void) {      return "/tmp";  } + +int pa_open_cloexec(const char *fn, int flags, mode_t mode) { +    int fd; + +#ifdef O_NOCTTY +    flags |= O_NOCTTY; +#endif + +#ifdef O_CLOEXEC +    if ((fd = open(fn, flags|O_CLOEXEC, mode)) >= 0) +        return fd; + +    if (errno != EINVAL) +        return fd; +#endif + +    if ((fd = open(fn, flags, mode)) < 0) +        return fd; + +    pa_make_fd_cloexec(fd); +    return fd; +} + +int pa_socket_cloexec(int domain, int type, int protocol) { +    int fd; + +#ifdef SOCK_CLOEXEC +    if ((fd = socket(domain, type | SOCK_CLOEXEC, protocol)) >= 0) +        return fd; + +    if (errno != EINVAL) +        return fd; +#endif + +    if ((fd = socket(domain, type, protocol)) < 0) +        return fd; + +    pa_make_fd_cloexec(fd); +    return fd; +} + +int pa_pipe_cloexec(int pipefd[2]) { +    int r; + +#ifdef HAVE_PIPE2 +    if ((r = pipe2(pipefd, O_CLOEXEC)) >= 0) +        return r; + +    if (errno != EINVAL && errno != ENOSYS) +        return r; +#endif + +    if ((r = pipe(pipefd)) < 0) +        return r; + +    pa_make_fd_cloexec(pipefd[0]); +    pa_make_fd_cloexec(pipefd[1]); + +    return 0; +} + +int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { +    int fd; + +#ifdef HAVE_ACCEPT4 +    if ((fd = accept4(sockfd, addr, addrlen, SOCK_CLOEXEC)) >= 0) +        return fd; + +    if (errno != EINVAL && errno != ENOSYS) +        return fd; +#endif + +    if ((fd = accept(sockfd, addr, addrlen)) < 0) +        return fd; + +    pa_make_fd_cloexec(fd); + +    return 0; +} | 
