>From a8826248b309133f7c0af41dd405c03da41ad0e9 Mon Sep 17 00:00:00 2001 From: Matthew Newton Date: Sat, 17 Jan 2015 00:30:36 +0000 Subject: [PATCH 2/3] Get pointers to winbind fd global from function Access the global variable struct via pointers returned from a function. This permits thread local storage to be easily added. --- nsswitch/wb_common.c | 59 +++++++++++++++++++++++++++++++------------ nsswitch/winbind_nss_irix.c | 17 ++++++++++--- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/nsswitch/wb_common.c b/nsswitch/wb_common.c index e86dad6..5e499d5 100644 --- a/nsswitch/wb_common.c +++ b/nsswitch/wb_common.c @@ -28,7 +28,15 @@ /* Global variables. These are effectively the client state information */ -struct fd_info_s fd_info = { .winbindd_fd = -1, .is_privileged = 0 }; +struct fd_info_s fd_info_global = { .winbindd_fd = -1, .is_privileged = 0 }; + +/* Return pointer to fd_info struct */ + +struct fd_info_s *winbindd_fd_info(void) +{ + return &fd_info_global; +} + /* Free a response structure */ @@ -68,9 +76,17 @@ __attribute__((destructor)) #endif static void winbind_close_sock(void) { - if (fd_info.winbindd_fd != -1) { - close(fd_info.winbindd_fd); - fd_info.winbindd_fd = -1; + struct fd_info_s *fd_info; + + fd_info = winbindd_fd_info(); + + if (fd_info == NULL) { + return; + } + + if (fd_info->winbindd_fd != -1) { + close(fd_info->winbindd_fd); + fd_info->winbindd_fd = -1; } } @@ -338,33 +354,41 @@ static int winbind_open_pipe_sock(int recursing, int need_priv) static pid_t our_pid; struct winbindd_request request; struct winbindd_response response; + struct fd_info_s *fd_info; + ZERO_STRUCT(request); ZERO_STRUCT(response); + fd_info = winbindd_fd_info(); + + if (fd_info == NULL) { + return -1; + } + if (our_pid != getpid()) { winbind_close_sock(); our_pid = getpid(); } - if ((need_priv != 0) && (fd_info.is_privileged == 0)) { + if ((need_priv != 0) && (fd_info->is_privileged == 0)) { winbind_close_sock(); } - if (fd_info.winbindd_fd != -1) { - return fd_info.winbindd_fd; + if (fd_info->winbindd_fd != -1) { + return fd_info->winbindd_fd; } if (recursing) { return -1; } - fd_info.winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir()); + fd_info->winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir()); - if (fd_info.winbindd_fd == -1) { + if (fd_info->winbindd_fd == -1) { return -1; } - fd_info.is_privileged = 0; + fd_info->is_privileged = 0; /* version-check the socket */ @@ -386,20 +410,23 @@ static int winbind_open_pipe_sock(int recursing, int need_priv) ZERO_STRUCT(response); if (winbindd_request_response(WINBINDD_PRIV_PIPE_DIR, &request, &response) == NSS_STATUS_SUCCESS) { int fd; - if ((fd = winbind_named_pipe_sock((char *)response.extra_data.data)) != -1) { - close(fd_info.winbindd_fd); - fd_info.winbindd_fd = fd; - fd_info.is_privileged = 1; + + fd = winbind_named_pipe_sock((char *)response.extra_data.data); + + if (fd != -1) { + close(fd_info->winbindd_fd); + fd_info->winbindd_fd = fd; + fd_info->is_privileged = 1; } } - if ((need_priv != 0) && (fd_info.is_privileged == 0)) { + if ((need_priv != 0) && (fd_info->is_privileged == 0)) { return -1; } SAFE_FREE(response.extra_data.data); - return fd_info.winbindd_fd; + return fd_info->winbindd_fd; #else return -1; #endif /* HAVE_UNIXSOCKET */ diff --git a/nsswitch/winbind_nss_irix.c b/nsswitch/winbind_nss_irix.c index 6ed9d20..7bbfd29 100644 --- a/nsswitch/winbind_nss_irix.c +++ b/nsswitch/winbind_nss_irix.c @@ -46,7 +46,7 @@ int asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3); /* Prototypes from wb_common.c */ -extern struct fd_info_s fd_info; +struct fd_info_s *winbindd_fd_info(void); #ifdef HAVE_NS_API_H @@ -409,6 +409,7 @@ static int winbind_timeout(nsd_file_t **rqp, nsd_times_t *to) { nsd_file_t *rq; + struct fd_info_s *fd_info; dequeue_request(); @@ -418,7 +419,11 @@ winbind_timeout(nsd_file_t **rqp, nsd_times_t *to) *rqp = rq; /* Remove the callback and timeout */ - nsd_callback_remove(fd_info.winbindd_fd); + fd_info = winbindd_fd_info(); + if (fd_info == NULL) { + return NSD_ERROR; + } + nsd_callback_remove(fd_info->winbindd_fd); nsd_timeout_remove(rq); rq->f_status = NS_NOTFOUND; @@ -467,10 +472,14 @@ send_next_request(nsd_file_t *rq, struct winbindd_request *request) /* * Set up callback and timeouts */ + fd_info = winbindd_fd_info(); + if (fd_info == NULL) { + return NSD_ERROR; + } nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) fd = %d\n", - fd_info.winbindd_fd); + fd_info->winbindd_fd); - nsd_callback_new(fd_info.winbindd_fd, winbind_callback, NSD_READ); + nsd_callback_new(fd_info->winbindd_fd, winbind_callback, NSD_READ); nsd_timeout_new(rq, timeout * 1000, winbind_timeout, NULL); return NSD_CONTINUE; } -- 1.7.10.4