Changeset 138935 for trunk/base


Ignore:
Timestamp:
Jul 23, 2015, 8:49:56 PM (5 years ago)
Author:
cal@…
Message:

base: darwintrace: set close-on-exec on darwintrace socket

While there is currently no indication that this has been a significant problem
so far, it could certainly happen. Let's do the safe thing and set
close-on-exec (our socket cannot be used after exec anyway).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/base/src/darwintracelib1.0/darwintrace.c

    r138558 r138935  
    4545
    4646#include <errno.h>
     47#include <fcntl.h>
    4748#include <inttypes.h>
    4849#include <pthread.h>
     
    378379        if (__darwintrace_sock() == NULL) {
    379380                int sock;
     381                int sockflags;
    380382                FILE *stream;
    381383                struct sockaddr_un sun;
     
    390392                if (-1 == (sock = socket(PF_LOCAL, SOCK_STREAM, 0))) {
    391393                        perror("darwintrace: socket");
     394                        abort();
     395                }
     396
     397                /* Set the close-on-exec flag as early as possible after the socket
     398                 * creation. On OS X, there is no way to do this race-condition free
     399                 * unless you synchronize around creation and fork(2) -- however,
     400                 * blocking in this function is not acceptable for darwintrace, because
     401                 * it could possibly run in a signal handler, leading to a deadlock.
     402                 *
     403                 * The close-on-exec flag is needed because we're using a thread-local
     404                 * variable to hold a reference to this socket, but multi-threaded
     405                 * programs that fork will only clone the thread that calls fork(2),
     406                 * which leaves us with no reference to the other sockets (which are
     407                 * inherited, because FDs are process-wide). Consequently, this can
     408                 * lead to a resource leak.
     409                 */
     410                if (-1 == (sockflags = fcntl(sock, F_GETFD))) {
     411                        perror("darwintrace: fcntl(F_GETFD)");
     412                        abort();
     413                }
     414                sockflags |= FD_CLOEXEC;
     415                if (-1 == fcntl(sock, F_SETFD, sockflags)) {
     416                        perror("darwintrace: fcntl(F_SETFD, flags | FD_CLOEXEC)");
    392417                        abort();
    393418                }
Note: See TracChangeset for help on using the changeset viewer.