nsswitch: make wb_global_ctx private add add get/put functions to access global context
authorRalph Wuerthner <ralph.wuerthner@de.ibm.com>
Tue, 2 Oct 2018 11:35:16 +0000 (13:35 +0200)
committerJeremy Allison <jra@samba.org>
Thu, 1 Nov 2018 00:59:10 +0000 (01:59 +0100)
Signed-off-by: Ralph Wuerthner <ralph.wuerthner@de.ibm.com>
Reviewed-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
nsswitch/wb_common.c

index c2f67d7..9ba74c3 100644 (file)
@@ -35,11 +35,22 @@ struct winbindd_context {
        pid_t our_pid;          /* calling process pid */
 };
 
-static struct winbindd_context wb_global_ctx = {
-       .winbindd_fd = -1,
-       .is_privileged = false,
-       .our_pid = 0
-};
+static struct winbindd_context *get_wb_global_ctx(void)
+{
+       static struct winbindd_context wb_global_ctx = {
+               .winbindd_fd = -1,
+               .is_privileged = false,
+               .our_pid = 0
+       };
+
+       return &wb_global_ctx;
+}
+
+static void put_wb_global_ctx(void)
+{
+       /* noop for now */
+       return;
+}
 
 /* Free a response structure */
 
@@ -93,7 +104,11 @@ __attribute__((destructor))
 #endif
 static void winbind_destructor(void)
 {
-       winbind_close_sock(&wb_global_ctx);
+       struct winbindd_context *ctx;
+
+       ctx = get_wb_global_ctx();
+       winbind_close_sock(ctx);
+       put_wb_global_ctx();
 }
 
 #define CONNECT_TIMEOUT 30
@@ -725,9 +740,11 @@ NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
                                     struct winbindd_response *response)
 {
        NSS_STATUS status = NSS_STATUS_UNAVAIL;
+       bool release_global_ctx = false;
 
        if (ctx == NULL) {
-               ctx = &wb_global_ctx;
+               ctx = get_wb_global_ctx();
+               release_global_ctx = true;
        }
 
        status = winbindd_send_request(ctx, req_type, 0, request);
@@ -737,6 +754,9 @@ NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
        status = winbindd_get_response(ctx, response);
 
 out:
+       if (release_global_ctx) {
+               put_wb_global_ctx();
+       }
        return status;
 }
 
@@ -746,9 +766,11 @@ NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
                                          struct winbindd_response *response)
 {
        NSS_STATUS status = NSS_STATUS_UNAVAIL;
+       bool release_global_ctx = false;
 
        if (ctx == NULL) {
-               ctx = &wb_global_ctx;
+               ctx = get_wb_global_ctx();
+               release_global_ctx = true;
        }
 
        status = winbindd_send_request(ctx, req_type, 1, request);
@@ -758,6 +780,9 @@ NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
        status = winbindd_get_response(ctx, response);
 
 out:
+       if (release_global_ctx) {
+               put_wb_global_ctx();
+       }
        return status;
 }