Changeset: d8bf59530f25 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=d8bf59530f25
Modified Files:
        clients/mapilib/mapi.c
        common/utils/msabaoth.c
        monetdb5/modules/atoms/streams.c
        monetdb5/modules/mal/mal_mapi.c
        sql/backends/monet5/sql.c
        tools/merovingian/daemon/client.c
        tools/merovingian/daemon/forkmserver.c
Branch: Apr2019
Log Message:

Try to consume errno right after error occurs.
Also note that mnstr_errnr() does *not* return a value that is
compatible with errno.


diffs (truncated from 363 to 300 lines):

diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -2328,7 +2328,6 @@ mapi_reconnect(Mapi mid)
                userver.sun_path[sizeof(userver.sun_path) - 1] = 0;
 
                if (connect(s, serv, sizeof(struct sockaddr_un)) == 
SOCKET_ERROR) {
-                       closesocket(s);
                        snprintf(errbuf, sizeof(errbuf),
                                 "initiating connection on socket failed: %s",
 #ifdef _MSC_VER
@@ -2337,6 +2336,7 @@ mapi_reconnect(Mapi mid)
                                 strerror(errno)
 #endif
                                );
+                       closesocket(s);
                        return mapi_setError(mid, errbuf, "mapi_reconnect", 
MERROR);
                }
 
@@ -2353,7 +2353,6 @@ mapi_reconnect(Mapi mid)
                msg.msg_flags = 0;
 
                if (sendmsg(s, &msg, 0) < 0) {
-                       closesocket(s);
                        snprintf(errbuf, sizeof(errbuf), "could not send 
initial byte: %s",
 #ifdef _MSC_VER
                                 wsaerror(WSAGetLastError())
@@ -2361,6 +2360,7 @@ mapi_reconnect(Mapi mid)
                                 strerror(errno)
 #endif
                                );
+                       closesocket(s);
                        return mapi_setError(mid, errbuf, "mapi_reconnect", 
MERROR);
                }
        } else
@@ -2385,14 +2385,25 @@ mapi_reconnect(Mapi mid)
                        snprintf(errbuf, sizeof(errbuf), "getaddrinfo failed: 
%s", gai_strerror(ret));
                        return mapi_setError(mid, errbuf, "mapi_reconnect", 
MERROR);
                }
+               errbuf[0] = 0;
                for (rp = res; rp; rp = rp->ai_next) {
                        s = socket(rp->ai_family, rp->ai_socktype
 #ifdef SOCK_CLOEXEC
                                   | SOCK_CLOEXEC
 #endif
                                   , rp->ai_protocol);
-                       if (s == INVALID_SOCKET)
+                       if (s == INVALID_SOCKET) {
+                               snprintf(errbuf, sizeof(errbuf),
+                                        "could not connect to %s:%s: %s",
+                                        mid->hostname, port,
+#ifdef _MSC_VER
+                                        wsaerror(WSAGetLastError())
+#else
+                                        strerror(errno)
+#endif
+                                       );
                                continue;
+                       }
 #if !defined(SOCK_CLOEXEC) && defined(HAVE_FCNTL)
                        (void) fcntl(s, F_SETFD, FD_CLOEXEC);
 #endif
@@ -2402,14 +2413,11 @@ mapi_reconnect(Mapi mid)
                }
                freeaddrinfo(res);
                if (rp == NULL) {
-                       snprintf(errbuf, sizeof(errbuf), "could not connect to 
%s:%s: %s",
-                                mid->hostname, port,
-#ifdef _MSC_VER
-                                wsaerror(WSAGetLastError())
-#else
-                                strerror(errno)
-#endif
-                               );
+                       if (errbuf[0] == 0) {
+                               /* should not happen */
+                               snprintf(errbuf, sizeof(errbuf),
+                                        "getaddrinfo succeeded but did not 
return a result");
+                       }
                        return mapi_setError(mid, errbuf, "mapi_reconnect", 
MERROR);
                }
 #else
diff --git a/common/utils/msabaoth.c b/common/utils/msabaoth.c
--- a/common/utils/msabaoth.c
+++ b/common/utils/msabaoth.c
@@ -327,9 +327,9 @@ msab_retreatScenario(const char *lang)
                                rewind(f);
                                len = strlen(buf) + 1;
                                if (fwrite(buf, 1, len, f) < len) {
-                                       (void)fclose(f);
                                        snprintf(buf, sizeof(buf), "failed to 
write: %s (%s)",
                                                        strerror(errno), 
pathbuf);
+                                       (void)fclose(f);
                                        return(strdup(buf));
                                }
                                fflush(f);
diff --git a/monetdb5/modules/atoms/streams.c b/monetdb5/modules/atoms/streams.c
--- a/monetdb5/modules/atoms/streams.c
+++ b/monetdb5/modules/atoms/streams.c
@@ -24,7 +24,7 @@ str mnstr_open_rstreamwrap(Stream *S, st
        stream *s;
 
        if ((s = open_rstream(*filename)) == NULL || mnstr_errnr(s)) {
-               int errnr = mnstr_errnr(s);
+               int errnr = errno;
                if (s)
                        close_stream(s);
                throw(IO, "streams.open", "could not open file '%s': %s",
@@ -40,7 +40,7 @@ str mnstr_open_wstreamwrap(Stream *S, st
        stream *s;
 
        if ((s = open_wstream(*filename)) == NULL || mnstr_errnr(s)) {
-               int errnr = mnstr_errnr(s);
+               int errnr = errno;
                if (s)
                        close_stream(s);
                throw(IO, "streams.open", "could not open file '%s': %s",
@@ -57,7 +57,7 @@ str mnstr_open_rastreamwrap(Stream *S, s
        stream *s;
 
        if ((s = open_rastream(*filename)) == NULL || mnstr_errnr(s)) {
-               int errnr = mnstr_errnr(s);
+               int errnr = errno;
                if (s)
                        close_stream(s);
                throw(IO, "streams.open", "could not open file '%s': %s",
@@ -74,7 +74,7 @@ str mnstr_open_wastreamwrap(Stream *S, s
        stream *s;
 
        if ((s = open_wastream(*filename)) == NULL || mnstr_errnr(s)) {
-               int errnr = mnstr_errnr(s);
+               int errnr = errno;
                if (s)
                        close_stream(s);
                throw(IO, "streams.open", "could not open file '%s': %s",
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
@@ -613,8 +613,10 @@ SERVERlisten(int *Port, str *Usockfile, 
 #endif
                                          , 0);
                if (sock == INVALID_SOCKET) {
+                       int e = errno;
                        GDKfree(psock);
                        GDKfree(usockfile);
+                       errno = e;
                        throw(IO, "mal_mapi.listen",
                                  OPERATION_FAILED ": creation of stream socket 
failed: %s",
 #ifdef _MSC_VER
@@ -652,6 +654,7 @@ SERVERlisten(int *Port, str *Usockfile, 
                do {
                        server.sin_port = htons((unsigned short) ((port) & 
0xFFFF));
                        if (bind(sock, (SOCKPTR) &server, length) == 
SOCKET_ERROR) {
+                               int e = errno;
                                if (
 #ifdef _MSC_VER
                                        WSAGetLastError() == WSAEADDRINUSE &&
@@ -669,6 +672,7 @@ SERVERlisten(int *Port, str *Usockfile, 
                                closesocket(sock);
                                GDKfree(psock);
                                GDKfree(usockfile);
+                               errno = e;
                                throw(IO, "mal_mapi.listen",
                                          OPERATION_FAILED ": bind to stream 
socket port %d "
                                          "failed: %s", port,
@@ -684,9 +688,11 @@ SERVERlisten(int *Port, str *Usockfile, 
                } while (1);
 
                if (getsockname(sock, (SOCKPTR) &server, &length) == 
SOCKET_ERROR) {
+                       int e = errno;
                        closesocket(sock);
                        GDKfree(psock);
                        GDKfree(usockfile);
+                       errno = e;
                        throw(IO, "mal_mapi.listen",
                                  OPERATION_FAILED ": failed getting socket 
name: %s",
 #ifdef _MSC_VER
@@ -697,9 +703,11 @@ SERVERlisten(int *Port, str *Usockfile, 
                                );
                }
                if(listen(sock, maxusers) == SOCKET_ERROR) {
+                       int e = errno;
                        closesocket(sock);
                        GDKfree(psock);
                        GDKfree(usockfile);
+                       errno = e;
                        throw(IO, "mal_mapi.listen",
                                  OPERATION_FAILED ": failed to set socket to 
listen %s",
 #ifdef _MSC_VER
@@ -718,8 +726,10 @@ SERVERlisten(int *Port, str *Usockfile, 
 #endif
                                           , 0);
                if (usock == INVALID_SOCKET ) {
+                       int e = errno;
                        GDKfree(psock);
                        GDKfree(usockfile);
+                       errno = e;
                        throw(IO, "mal_mapi.listen",
                                  OPERATION_FAILED ": creation of UNIX socket 
failed: %s",
 #ifdef _MSC_VER
@@ -760,9 +770,11 @@ SERVERlisten(int *Port, str *Usockfile, 
                }
                if (bind(usock, (SOCKPTR) &userver, length) == SOCKET_ERROR) {
                        char *e;
+                       int err = errno;
                        closesocket(usock);
                        (void) remove(usockfile);
                        GDKfree(psock);
+                       errno = err;
                        e = createException(IO, "mal_mapi.listen",
                                                                OPERATION_FAILED
                                                                ": binding to 
UNIX socket file %s failed: %s",
@@ -778,9 +790,11 @@ SERVERlisten(int *Port, str *Usockfile, 
                }
                if(listen(usock, maxusers) == SOCKET_ERROR) {
                        char *e;
+                       int err = errno;
                        closesocket(usock);
                        (void) remove(usockfile);
                        GDKfree(psock);
+                       errno = err;
                        e = createException(IO, "mal_mapi.listen",
                                                                OPERATION_FAILED
                                                                ": setting UNIX 
socket file %s to listen failed: %s",
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -2151,10 +2151,9 @@ mvc_export_table_wrap( Client cntxt, Mal
                s = cntxt->fdout;
        } else if (!onclient) {
                if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s)) {
-                       int errnr = mnstr_errnr(s);
+                       msg=  createException(IO, "streams.open", 
SQLSTATE(42000) "could not open file '%s': %s",
+                                             filename?filename:"stdout", 
strerror(errno));
                        close_stream(s);
-                       msg=  createException(IO, "streams.open", 
SQLSTATE(42000) "could not open file '%s': %s",
-                                             filename?filename:"stdout", 
strerror(errnr));
                        goto wrapup_result_set1;
                }
        } else {
@@ -2351,10 +2350,9 @@ mvc_export_row_wrap( Client cntxt, MalBl
                s = cntxt->fdout;
        } else if (!onclient) {
                if ((s = open_wastream(filename)) == NULL || mnstr_errnr(s)) {
-                       int errnr = mnstr_errnr(s);
+                       msg=  createException(IO, "streams.open", 
SQLSTATE(42000) "could not open file '%s': %s",
+                                             filename?filename:"stdout", 
strerror(errno));
                        close_stream(s);
-                       msg=  createException(IO, "streams.open", 
SQLSTATE(42000) "could not open file '%s': %s",
-                                             filename?filename:"stdout", 
strerror(errnr));
                        goto wrapup_result_set;
                }
        } else {
@@ -2683,9 +2681,8 @@ mvc_import_table_wrap(Client cntxt, MalB
                } else {
                        ss = open_rastream(fname);
                        if (ss == NULL || mnstr_errnr(ss)) {
-                               int errnr = mnstr_errnr(ss);
+                               msg = createException(IO, "sql.copy_from", 
SQLSTATE(42000) "Cannot open file '%s': %s", fname, strerror(errno));
                                close_stream(ss);
-                               msg = createException(IO, "sql.copy_from", 
SQLSTATE(42000) "Cannot open file '%s': %s", fname, strerror(errnr));
                                return msg;
                        }
                }
diff --git a/tools/merovingian/daemon/client.c 
b/tools/merovingian/daemon/client.c
--- a/tools/merovingian/daemon/client.c
+++ b/tools/merovingian/daemon/client.c
@@ -479,7 +479,7 @@ acceptConnections(int sock, int usock)
                        if ((msgsock = accept4(sock, (SOCKPTR)0, (socklen_t *) 
0, SOCK_CLOEXEC)) == -1) {
                                if (_mero_keep_listening == 0)
                                        break;
-                               switch (errnr) {
+                               switch (errno) {
                                case EINTR:
                                        /* interrupted */
                                        break;
@@ -493,7 +493,7 @@ acceptConnections(int sock, int usock)
                                        /* connection aborted before we began */
                                        break;
                                default:
-                                       msg = strerror(errnr);
+                                       msg = strerror(errno);
                                        goto error;
                                }
                                continue;
@@ -511,7 +511,7 @@ acceptConnections(int sock, int usock)
                        if ((msgsock = accept4(usock, (SOCKPTR)0, (socklen_t 
*)0, SOCK_CLOEXEC)) == -1) {
                                if (_mero_keep_listening == 0)
                                        break;
-                               switch (errnr) {
+                               switch (errno) {
                                case EINTR:
                                        /* interrupted */
                                        break;
@@ -525,7 +525,7 @@ acceptConnections(int sock, int usock)
                                        /* connection aborted before we began */
                                        break;
                                default:
-                                       msg = strerror(errnr);
+                                       msg = strerror(errno);
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to