Commit af021625 authored by Jay Satiro's avatar Jay Satiro
Browse files

curl_setup_once: Remove ERRNO/SET_ERRNO macros

Prior to this change (SET_)ERRNO mapped to GetLastError/SetLastError
for Win32 and regular errno otherwise.

I reviewed the code and found no justifiable reason for conflating errno
on WIN32 with GetLastError/SetLastError. All Win32 CRTs support errno,
and any Win32 multithreaded CRT supports thread-local errno.

Fixes https://github.com/curl/curl/issues/895
Closes https://github.com/curl/curl/pull/1589
parent 17da6750
Loading
Loading
Loading
Loading
+2 −5
Original line number Diff line number Diff line
@@ -407,9 +407,7 @@ static bool init_resolve_thread(struct connectdata *conn,
#endif

  if(!td->thread_hnd) {
#ifndef _WIN32_WCE
    err = errno;
#endif
    goto err_exit;
  }

@@ -418,8 +416,7 @@ static bool init_resolve_thread(struct connectdata *conn,
 err_exit:
  destroy_async_data(&conn->async);

  SET_ERRNO(err);

  errno = err;
  return FALSE;
}

@@ -655,7 +652,7 @@ Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,

  /* fall-back to blocking version */
  infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
        hostname, Curl_strerror(conn, ERRNO));
        hostname, Curl_strerror(conn, errno));

  error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
  if(error) {
+8 −12
Original line number Diff line number Diff line
@@ -607,7 +607,8 @@ void Curl_persistconninfo(struct connectdata *conn)
  conn->data->info.conn_local_port = conn->local_port;
}

/* retrieves ip address and port from a sockaddr structure */
/* retrieves ip address and port from a sockaddr structure.
   note it calls Curl_inet_ntop which sets errno on fail, not SOCKERRNO. */
static bool getaddressinfo(struct sockaddr *sa, char *addr,
                           long *port)
{
@@ -654,7 +655,7 @@ static bool getaddressinfo(struct sockaddr *sa, char *addr,

  addr[0] = '\0';
  *port = 0;

  errno = EAFNOSUPPORT;
  return FALSE;
}

@@ -672,11 +673,9 @@ void Curl_updateconninfo(struct connectdata *conn, curl_socket_t sockfd)
    return;

  if(!conn->bits.reuse && !conn->bits.tcp_fastopen) {
    int error;

    len = sizeof(struct Curl_sockaddr_storage);
    if(getpeername(sockfd, (struct sockaddr*) &ssrem, &len)) {
      error = SOCKERRNO;
      int error = SOCKERRNO;
      failf(data, "getpeername() failed with errno %d: %s",
            error, Curl_strerror(conn, error));
      return;
@@ -685,7 +684,7 @@ void Curl_updateconninfo(struct connectdata *conn, curl_socket_t sockfd)
    len = sizeof(struct Curl_sockaddr_storage);
    memset(&ssloc, 0, sizeof(ssloc));
    if(getsockname(sockfd, (struct sockaddr*) &ssloc, &len)) {
      error = SOCKERRNO;
      int error = SOCKERRNO;
      failf(data, "getsockname() failed with errno %d: %s",
            error, Curl_strerror(conn, error));
      return;
@@ -693,18 +692,16 @@ void Curl_updateconninfo(struct connectdata *conn, curl_socket_t sockfd)

    if(!getaddressinfo((struct sockaddr*)&ssrem,
                        conn->primary_ip, &conn->primary_port)) {
      error = ERRNO;
      failf(data, "ssrem inet_ntop() failed with errno %d: %s",
            error, Curl_strerror(conn, error));
            errno, Curl_strerror(conn, errno));
      return;
    }
    memcpy(conn->ip_addr_str, conn->primary_ip, MAX_IPADR_LEN);

    if(!getaddressinfo((struct sockaddr*)&ssloc,
                       conn->local_ip, &conn->local_port)) {
      error = ERRNO;
      failf(data, "ssloc inet_ntop() failed with errno %d: %s",
            error, Curl_strerror(conn, error));
            errno, Curl_strerror(conn, errno));
      return;
    }

@@ -995,9 +992,8 @@ static CURLcode singleipconnect(struct connectdata *conn,
  if(!getaddressinfo((struct sockaddr*)&addr.sa_addr,
                     ipaddress, &port)) {
    /* malformed address or bug in inet_ntop, try next address */
    error = ERRNO;
    failf(data, "sa_addr inet_ntop() failed with errno %d: %s",
          error, Curl_strerror(conn, error));
          errno, Curl_strerror(conn, errno));
    Curl_closesocket(conn, sockfd);
    return CURLE_OK;
  }
+6 −13
Original line number Diff line number Diff line
@@ -123,7 +123,6 @@ static CURLcode ntlm_wb_init(struct connectdata *conn, const char *userp)
  struct passwd pw, *pw_res;
  char pwbuf[1024];
#endif
  int error;

  /* Return if communication with ntlm_auth already set up */
  if(conn->ntlm_auth_hlpr_socket != CURL_SOCKET_BAD ||
@@ -178,26 +177,23 @@ static CURLcode ntlm_wb_init(struct connectdata *conn, const char *userp)
    ntlm_auth = NTLM_WB_FILE;

  if(access(ntlm_auth, X_OK) != 0) {
    error = ERRNO;
    failf(conn->data, "Could not access ntlm_auth: %s errno %d: %s",
          ntlm_auth, error, Curl_strerror(conn, error));
          ntlm_auth, errno, Curl_strerror(conn, errno));
    goto done;
  }

  if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds)) {
    error = ERRNO;
    failf(conn->data, "Could not open socket pair. errno %d: %s",
          error, Curl_strerror(conn, error));
          errno, Curl_strerror(conn, errno));
    goto done;
  }

  child_pid = fork();
  if(child_pid == -1) {
    error = ERRNO;
    sclose(sockfds[0]);
    sclose(sockfds[1]);
    failf(conn->data, "Could not fork. errno %d: %s",
          error, Curl_strerror(conn, error));
          errno, Curl_strerror(conn, errno));
    goto done;
  }
  else if(!child_pid) {
@@ -208,16 +204,14 @@ static CURLcode ntlm_wb_init(struct connectdata *conn, const char *userp)
    /* Don't use sclose in the child since it fools the socket leak detector */
    sclose_nolog(sockfds[0]);
    if(dup2(sockfds[1], STDIN_FILENO) == -1) {
      error = ERRNO;
      failf(conn->data, "Could not redirect child stdin. errno %d: %s",
            error, Curl_strerror(conn, error));
            errno, Curl_strerror(conn, errno));
      exit(1);
    }

    if(dup2(sockfds[1], STDOUT_FILENO) == -1) {
      error = ERRNO;
      failf(conn->data, "Could not redirect child stdout. errno %d: %s",
            error, Curl_strerror(conn, error));
            errno, Curl_strerror(conn, errno));
      exit(1);
    }

@@ -235,10 +229,9 @@ static CURLcode ntlm_wb_init(struct connectdata *conn, const char *userp)
            "--username", username,
            NULL);

    error = ERRNO;
    sclose_nolog(sockfds[1]);
    failf(conn->data, "Could not execl(). errno %d: %s",
          error, Curl_strerror(conn, error));
          errno, Curl_strerror(conn, errno));
    exit(1);
  }

+0 −14
Original line number Diff line number Diff line
@@ -435,20 +435,6 @@ typedef int sig_atomic_t;
#endif


/*
 * Macro ERRNO / SET_ERRNO() returns / sets the NOT *socket-related* errno
 * (or equivalent) on this platform to hide platform details to code using it.
 */

#if defined(WIN32) && !defined(USE_LWIPSOCK)
#define ERRNO         ((int)GetLastError())
#define SET_ERRNO(x)  (SetLastError((DWORD)(x)))
#else
#define ERRNO         (errno)
#define SET_ERRNO(x)  (errno = (x))
#endif


/*
 * Portable error number symbolic names defined to Winsock error codes.
 */
+11 −4
Original line number Diff line number Diff line
@@ -104,15 +104,22 @@ int Curl_thread_join(curl_thread_t *hnd)
curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void *),
                                 void *arg)
{
  curl_thread_t t;
#ifdef _WIN32_WCE
  return CreateThread(NULL, 0, func, arg, 0, NULL);
  t = CreateThread(NULL, 0, func, arg, 0, NULL);
#else
  curl_thread_t t;
  t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL);
  if((t == 0) || (t == (curl_thread_t)-1L))
#endif
  if((t == 0) || (t == (curl_thread_t)-1L)) {
#ifdef _WIN32_WCE
    DWORD gle = GetLastError();
    errno = ((gle == ERROR_ACCESS_DENIED ||
              gle == ERROR_NOT_ENOUGH_MEMORY) ?
             EACCES : EINVAL);
#endif
    return curl_thread_t_null;
  }
  return t;
#endif
}

void Curl_thread_destroy(curl_thread_t hnd)
Loading