Changeset: d0720566e7f8 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=d0720566e7f8
Modified Files:
        NT/monetdb_config.h.in
        clients/mapiclient/mnc.c
        clients/mapilib/mapi.c
        common/stream/stream.c
        common/utils/mutils.c
        gdk/gdk_posix.c
        gdk/gdk_storage.c
        monetdb5/mal/mal_linker.c
        monetdb5/modules/mal/mal_mapi.c
        testing/Mtest.py.in
        tools/merovingian/ChangeLog.Jul2017
        tools/merovingian/daemon/client.c
        tools/merovingian/daemon/connections.c
        tools/merovingian/daemon/controlrunner.c
        tools/merovingian/daemon/handlers.c
        tools/merovingian/daemon/merovingian.c
        tools/merovingian/daemon/multiplex-funnel.c
        tools/merovingian/daemon/proxy.c
        tools/merovingian/utils/control.c
        tools/merovingian/utils/utils.c
Branch: Jul2017
Log Message:

Don't leak open file descriptors from monetdtd to mserver5.
Also, just in case, mark most other open files as close-on-exec.


diffs (truncated from 572 to 300 lines):

diff --git a/NT/monetdb_config.h.in b/NT/monetdb_config.h.in
--- a/NT/monetdb_config.h.in
+++ b/NT/monetdb_config.h.in
@@ -1028,6 +1028,9 @@ typedef unsigned __int64 uint64_t;
 #define false 0
 #define __bool_true_false_are_defined 1
 
+/* normally defined in fcntl.h, but not on Windows */
+#define O_CLOEXEC 0
+
 
 #ifdef HAVE_LONG_LONG
 typedef long long lng;
diff --git a/clients/mapiclient/mnc.c b/clients/mapiclient/mnc.c
--- a/clients/mapiclient/mnc.c
+++ b/clients/mapiclient/mnc.c
@@ -172,6 +172,9 @@ main(int argc, char **argv)
                                break;  /* success */
                        closesocket(s);
                }
+#ifdef HAVE_FCNTL
+               fcntl(s, F_SETFD, FD_CLOEXEC);
+#endif
                freeaddrinfo(res);
                if (rp == NULL) {
                        fprintf(stderr, "could not connect to %s:%s: %s\n",
@@ -197,6 +200,9 @@ main(int argc, char **argv)
                        fprintf(stderr, "opening socket failed: %s\n", 
strerror(errno));
                        exit(1);
                }
+#ifdef HAVE_FCNTL
+               fcntl(s, F_SETFD, FD_CLOEXEC);
+#endif
 
                if (connect(s, serv, sizeof(server)) == SOCKET_ERROR) {
                        fprintf(stderr,
@@ -221,6 +227,9 @@ main(int argc, char **argv)
                        fprintf(stderr, "failed to create socket: %s\n", 
strerror(errno));
                        exit(1);
                }
+#ifdef HAVE_FCNTL
+               fcntl(sock, F_SETFD, FD_CLOEXEC);
+#endif
 
                setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof 
on);
 
@@ -243,6 +252,9 @@ main(int argc, char **argv)
                                        strerror(errno));
                        exit(1);
                }
+#ifdef HAVE_FCNTL
+               fcntl(s, F_SETFD, FD_CLOEXEC);
+#endif
        }
 
        out = socket_wastream(s, "ascii write stream");
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -766,6 +766,10 @@
 # endif
 #endif
 
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
 #ifndef INVALID_SOCKET
 #define INVALID_SOCKET (-1)
 #endif
@@ -2354,6 +2358,9 @@ mapi_reconnect(Mapi mid)
                                );
                        return mapi_setError(mid, errbuf, "mapi_reconnect", 
MERROR);
                }
+#ifdef HAVE_FCNTL
+               fcntl(s, F_SETFD, FD_CLOEXEC);
+#endif
                memset(&userver, 0, sizeof(struct sockaddr_un));
                userver.sun_family = AF_UNIX;
                strncpy(userver.sun_path, mid->hostname, 
sizeof(userver.sun_path) - 1);
@@ -2420,6 +2427,9 @@ mapi_reconnect(Mapi mid)
                        s = socket(rp->ai_family, rp->ai_socktype, 
rp->ai_protocol);
                        if (s == INVALID_SOCKET)
                                continue;
+#ifdef HAVE_FCNTL
+                       fcntl(s, F_SETFD, FD_CLOEXEC);
+#endif
                        if (connect(s, rp->ai_addr, (socklen_t) rp->ai_addrlen) 
!= SOCKET_ERROR)
                                break;  /* success */
                        closesocket(s);
@@ -2470,6 +2480,9 @@ mapi_reconnect(Mapi mid)
                                );
                        return mapi_setError(mid, errbuf, "mapi_reconnect", 
MERROR);
                }
+#ifdef HAVE_FCNTL
+               fcntl(s, F_SETFD, FD_CLOEXEC);
+#endif
 
                if (connect(s, serv, sizeof(server)) == SOCKET_ERROR) {
                        snprintf(errbuf, sizeof(errbuf),
diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -2597,6 +2597,9 @@ udp_socket(udp_stream *udp, const char *
                udp->s = socket(rp->ai_family, rp->ai_socktype, 
rp->ai_protocol);
                if (udp->s == INVALID_SOCKET)
                        continue;
+#ifdef HAVE_FCNTL
+               fcntl(udp->s, F_SETFD, FD_CLOEXEC);
+#endif
                if (!write &&
                    bind(udp->s, rp->ai_addr,
 #ifdef _MSC_VER
@@ -2633,6 +2636,9 @@ udp_socket(udp_stream *udp, const char *
        udp->s = socket(serv->sa_family, SOCK_DGRAM, IPPROTO_UDP);
        if (udp->s == INVALID_SOCKET)
                return -1;
+#ifdef HAVE_FCNTL
+       fcntl(udp->s, F_SETFD, FD_CLOEXEC);
+#endif
        if (!write && bind(udp->s, serv, servsize) == SOCKET_ERROR)
                return -1;
        return 0;
diff --git a/common/utils/mutils.c b/common/utils/mutils.c
--- a/common/utils/mutils.c
+++ b/common/utils/mutils.c
@@ -343,7 +343,7 @@ MT_lockf(char *filename, int mode, off_t
                return 0;
        }
 
-       fd = open(filename, O_CREAT | O_RDWR | O_TEXT, MONETDB_MODE);
+       fd = open(filename, O_CREAT | O_RDWR | O_TEXT | O_CLOEXEC, 
MONETDB_MODE);
        if (fd < 0)
                return -2;
        fh = (HANDLE) _get_osfhandle(fd);
@@ -421,7 +421,7 @@ lockf(int fd, int cmd, off_t len)
 int
 MT_lockf(char *filename, int mode, off_t off, off_t len)
 {
-       int fd = open(filename, O_CREAT | O_RDWR | O_TEXT, MONETDB_MODE);
+       int fd = open(filename, O_CREAT | O_RDWR | O_TEXT | O_CLOEXEC, 
MONETDB_MODE);
 
        if (fd < 0)
                return -2;
diff --git a/gdk/gdk_posix.c b/gdk/gdk_posix.c
--- a/gdk/gdk_posix.c
+++ b/gdk/gdk_posix.c
@@ -278,7 +278,7 @@ MT_getrss(void)
        int fd;
        psinfo_t psbuff;
 
-       fd = open("/proc/self/psinfo", O_RDONLY);
+       fd = open("/proc/self/psinfo", O_RDONLY | O_CLOEXEC);
        if (fd >= 0) {
                if (read(fd, &psbuff, sizeof(psbuff)) == sizeof(psbuff)) {
                        close(fd);
@@ -325,7 +325,7 @@ MT_getrss(void)
        /* get RSS on Linux */
        int fd;
 
-       fd = open("/proc/self/stat", O_RDONLY);
+       fd = open("/proc/self/stat", O_RDONLY | O_CLOEXEC);
        if (fd >= 0) {
                char buf[1024], *r = buf;
                ssize_t i, sz = read(fd, buf, 1024);
@@ -354,7 +354,7 @@ MT_mmap(const char *path, int mode, size
        int fd;
        void *ret;
 
-       fd = open(path, O_CREAT | ((mode & MMAP_WRITE) ? O_RDWR : O_RDONLY), 
MONETDB_MODE);
+       fd = open(path, O_CREAT | ((mode & MMAP_WRITE) ? O_RDWR : O_RDONLY) | 
O_CLOEXEC, MONETDB_MODE);
        if (fd < 0) {
                GDKsyserror("MT_mmap: open %s failed\n", path);
                return MAP_FAILED;
@@ -442,7 +442,7 @@ MT_mremap(const char *path, int mode, vo
        if (!(mode & MMAP_COPY) && path != NULL) {
                /* "normal" memory map */
 
-               if ((fd = open(path, O_RDWR)) < 0) {
+               if ((fd = open(path, O_RDWR | O_CLOEXEC)) < 0) {
                        GDKsyserror("MT_mremap: open(%s) failed\n", path);
                        fprintf(stderr, "= %s:%d: 
MT_mremap(%s,"PTRFMT","SZFMT","SZFMT"): open() failed\n", __FILE__, __LINE__, 
path, PTRFMTCAST old_address, old_size, *new_size);
                        return NULL;
@@ -506,7 +506,7 @@ MT_mremap(const char *path, int mode, vo
 #ifdef MAP_ANONYMOUS
                flags |= MAP_ANONYMOUS;
 #else
-               if ((fd = open("/dev/zero", O_RDWR)) < 0) {
+               if ((fd = open("/dev/zero", O_RDWR | O_CLOEXEC)) < 0) {
                        GDKsyserror("MT_mremap: open(/dev/zero) failed\n");
                        fprintf(stderr, "= %s:%d: 
MT_mremap(%s,"PTRFMT","SZFMT","SZFMT"): open('/dev/zero') failed\n", __FILE__, 
__LINE__, path?path:"NULL", PTRFMTCAST old_address, old_size, *new_size);
                        return NULL;
@@ -589,7 +589,7 @@ MT_mremap(const char *path, int mode, vo
                                        }
 
                                        strcat(strcpy(p, path), ".tmp");
-                                       fd = open(p, O_RDWR | O_CREAT,
+                                       fd = open(p, O_RDWR | O_CREAT | 
O_CLOEXEC,
                                                  MONETDB_MODE);
                                        if (fd < 0) {
                                                GDKsyserror("MT_mremap: 
open(%s) failed\n", (char *) p);
diff --git a/gdk/gdk_storage.c b/gdk/gdk_storage.c
--- a/gdk/gdk_storage.c
+++ b/gdk/gdk_storage.c
@@ -208,11 +208,11 @@ GDKfdlocate(int farmid, const char *nme,
 #ifdef WIN32
        flags |= strchr(mode, 'b') ? O_BINARY : O_TEXT;
 #endif
-       fd = open(path, flags, MONETDB_MODE);
+       fd = open(path, flags | O_CLOEXEC, MONETDB_MODE);
        if (fd < 0 && *mode == 'w') {
                /* try to create the directory, in case that was the problem */
                if (GDKcreatedir(path) == GDK_SUCCEED) {
-                       fd = open(path, flags, MONETDB_MODE);
+                       fd = open(path, flags | O_CLOEXEC, MONETDB_MODE);
                        if (fd < 0)
                                GDKsyserror("GDKfdlocate: cannot open file 
%s\n", path);
                }
@@ -379,7 +379,7 @@ GDKextend(const char *fn, size_t size)
         * bytes without O_BINARY. */
        flags |= O_BINARY;
 #endif
-       if ((fd = open(fn, flags)) >= 0) {
+       if ((fd = open(fn, flags | O_CLOEXEC)) >= 0) {
                rt = GDKextendf(fd, size, fn);
                close(fd);
        } else {
diff --git a/monetdb5/mal/mal_linker.c b/monetdb5/mal/mal_linker.c
--- a/monetdb5/mal/mal_linker.c
+++ b/monetdb5/mal/mal_linker.c
@@ -362,7 +362,7 @@ locate_file(const char *basename, const 
                        (void)closedir(rdir);
                } else {
                        strcat(fullname + i + 1, ext);
-                       if ((fd = open(fullname, O_RDONLY)) >= 0) {
+                       if ((fd = open(fullname, O_RDONLY | O_CLOEXEC)) >= 0) {
                                char *tmp;
                                close(fd);
                                tmp = GDKrealloc(fullname, strlen(fullname) + 
1);
diff --git a/monetdb5/modules/mal/mal_mapi.c b/monetdb5/modules/mal/mal_mapi.c
--- a/monetdb5/modules/mal/mal_mapi.c
+++ b/monetdb5/modules/mal/mal_mapi.c
@@ -351,6 +351,9 @@ SERVERlistenThread(SOCKET *Sock)
                                }
                                continue;
                        }
+#ifdef HAVE_FCNTL
+                       fcntl(msgsock, F_SETFD, FD_CLOEXEC);
+#endif
 #ifdef HAVE_SYS_UN_H
                } else if (usock != INVALID_SOCKET && FD_ISSET(usock, &fds)) {
                        struct msghdr msgh;
@@ -373,6 +376,9 @@ SERVERlistenThread(SOCKET *Sock)
                                }
                                continue;
                        }
+#ifdef HAVE_FCNTL
+                       fcntl(msgsock, F_SETFD, FD_CLOEXEC);
+#endif
 
                        /* BEWARE: unix domain sockets have a slightly different
                         * behaviour initialy than normal sockets, because we 
can
@@ -610,6 +616,9 @@ SERVERlisten(int *Port, str *Usockfile, 
 #endif
                                );
                }
+#ifdef HAVE_FCNTL
+               fcntl(sock, F_SETFD, FD_CLOEXEC);
+#endif
 
                if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, 
sizeof on) == SOCKET_ERROR) {
 #ifdef _MSC_VER
@@ -699,6 +708,9 @@ SERVERlisten(int *Port, str *Usockfile, 
 #endif
                                );
                }
+#ifdef HAVE_FCNTL
+               fcntl(usock, F_SETFD, FD_CLOEXEC);
+#endif
 
                /* prevent silent truncation, sun_path is typically around 108
                 * chars long :/ */
diff --git a/testing/Mtest.py.in b/testing/Mtest.py.in
--- a/testing/Mtest.py.in
+++ b/testing/Mtest.py.in
@@ -1280,6 +1280,7 @@ def PerformDir(env, testdir, testlist, B
                 if FtOut in (F_OK, F_WARN) and FtErr in (F_OK, F_WARN):
                     oktests.append(TST)
                 if global_timeout and start_time + global_timeout < 
time.time():
+                    print('\nGlobal testing timeout reached\n')
                     break
         except KeyboardInterrupt:
             print('\nInterrupted')
@@ -4267,6 +4268,7 @@ def main(argv) :
                     if interrupted:
                         break
                     if global_timeout and start_time + global_timeout < 
time.time():
+                        print('\nGlobal testing timeout reached\n')
                         break
                     if not testweb:
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to