• Daniel Black's avatar
    MDEV-8743: mysqld port/socket - FD_CLOEXEC if no SOCK_CLOEXEC · ccd566af
    Daniel Black authored
    In MDEV-8743, the port/socket of mysqld was changed to set FD_CLOEXEC.
    The existing mysql_socket_socket function already set that with
    SOCK_CLOEXEC when the socket was created. So here we move the fcntl
    functionality to the mysql_socket_socket as port/socket are the only
    callers.
    
    Preprocessor checks of SOCK_CLOEXEC cannot be done as its a 0 if not
    there and SOCK_CLOEXEC (being the value of the enum in bits/socket_type.h)
    Preprocesssor logic for arithmetic and non-arithmetic defines are
    hard/nonportable/ugly to read. As such we just check in my_global.h
    and define HAVE_SOCK_CLOEXEC if we have it.
    
    There was a disparity in behaviour between defined(WITH_WSREP) and
    not depending on the OS, so the WITH_WSREP condition was removed
    from setting calling fcntl.
    
    All sockets are now maked SOCK_CLOEXEC/FD_CLOEXEC.
    
    strace of mysqld with SOCK_CLOEXEC:
    
    socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_TCP) = 10
    write(2, "180419 14:52:40 [Note] Server socket created on IP: '127.0.0.1'.\n", 65180419 14:52:40 [Note] Server socket created on IP: '127.0.0.1'.
    ) = 65
    setsockopt(10, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
    bind(10, {sa_family=AF_INET, sin_port=htons(16020), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
    listen(10, 150)                         = 0
    socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_TCP) = 11
    write(2, "180419 14:52:40 [Note] Server socket created on IP: '127.0.0.1'.\n", 65180419 14:52:40 [Note] Server socket created on IP: '127.0.0.1'.
    ) = 65
    setsockopt(11, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
    bind(11, {sa_family=AF_INET, sin_port=htons(16021), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
    listen(11, 150)                         = 0
    socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0) = 12
    unlink("/home/dan/repos/build-mariadb-server-10.0/mysql-test/var/tmp/mysqld.1.sock") = -1 ENOENT (No such file or directory)
    setsockopt(12, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
    umask(000)                              = 006
    bind(12, {sa_family=AF_UNIX, sun_path="/home/dan/repos/build-mariadb-server-10.0/mysql-test/var/tmp/mysqld.1.sock"}, 110) = 0
    umask(006)                              = 000
    listen(12, 150)                         = 0
    ccd566af
mysql_socket.h 31.3 KB