Move wbc global variables into global context instead
authorMatthew Newton <matthew-git@newtoncomputing.co.uk>
Sun, 22 Feb 2015 23:31:48 +0000 (23:31 +0000)
committerJeremy Allison <jra@samba.org>
Mon, 9 Mar 2015 23:50:10 +0000 (00:50 +0100)
There are some global variables in use in the libwbclient
library. Now that we have a context, move these into it so that
they are thread-safe when the wbcCtx* functions are used.

Signed-off-by: Matthew Newton <matthew-git@newtoncomputing.co.uk>
Reviewed-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
nsswitch/libwbclient/wbc_pwd.c
nsswitch/libwbclient/wbclient.c
nsswitch/libwbclient/wbclient_internal.h

index 0b05133a4310a30cabdcc57d7c58281f8d81ae81..805ab63e42c8b91f3a045411a8b769a27e11b3ad 100644 (file)
@@ -4,6 +4,7 @@
    Winbind client API
 
    Copyright (C) Gerald (Jerry) Carter 2007
+   Copyright (C) Matthew Newton 2015
 
 
    This library is free software; you can redistribute it and/or
@@ -359,16 +360,6 @@ wbcErr wbcGetgrgid(gid_t gid, struct group **grp)
        return wbcCtxGetgrgid(NULL, gid, grp);
 }
 
-/** @brief Number of cached passwd structs
- *
- */
-static uint32_t pw_cache_size;
-
-/** @brief Position of the pwent context
- *
- */
-static uint32_t pw_cache_idx;
-
 /** @brief Winbindd response containing the passwd structs
  *
  */
@@ -379,8 +370,12 @@ wbcErr wbcCtxSetpwent(struct wbcContext *ctx)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
 
-       if (pw_cache_size > 0) {
-               pw_cache_idx = pw_cache_size = 0;
+       if (!ctx) {
+               ctx = wbcGetGlobalCtx();
+       }
+
+       if (ctx->pw_cache_size > 0) {
+               ctx->pw_cache_idx = ctx->pw_cache_size = 0;
                winbindd_free_response(&pw_response);
        }
 
@@ -404,8 +399,12 @@ wbcErr wbcCtxEndpwent(struct wbcContext *ctx)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
 
-       if (pw_cache_size > 0) {
-               pw_cache_idx = pw_cache_size = 0;
+       if (!ctx) {
+               ctx = wbcGetGlobalCtx();
+       }
+
+       if (ctx->pw_cache_size > 0) {
+               ctx->pw_cache_idx = ctx->pw_cache_size = 0;
                winbindd_free_response(&pw_response);
        }
 
@@ -429,14 +428,18 @@ wbcErr wbcCtxGetpwent(struct wbcContext *ctx, struct passwd **pwd)
        struct winbindd_request request;
        struct winbindd_pw *wb_pw;
 
+       if (!ctx) {
+               ctx = wbcGetGlobalCtx();
+       }
+
        /* If there's a cached result, return that. */
-       if (pw_cache_idx < pw_cache_size) {
+       if (ctx->pw_cache_idx < ctx->pw_cache_size) {
                goto return_result;
        }
 
        /* Otherwise, query winbindd for some entries. */
 
-       pw_cache_idx = 0;
+       ctx->pw_cache_idx = 0;
 
        winbindd_free_response(&pw_response);
 
@@ -448,17 +451,17 @@ wbcErr wbcCtxGetpwent(struct wbcContext *ctx, struct passwd **pwd)
 
        BAIL_ON_WBC_ERROR(wbc_status);
 
-       pw_cache_size = pw_response.data.num_entries;
+       ctx->pw_cache_size = pw_response.data.num_entries;
 
 return_result:
 
        wb_pw = (struct winbindd_pw *) pw_response.extra_data.data;
 
-       *pwd = copy_passwd_entry(&wb_pw[pw_cache_idx]);
+       *pwd = copy_passwd_entry(&wb_pw[ctx->pw_cache_idx]);
 
        BAIL_ON_PTR_ERROR(*pwd, wbc_status);
 
-       pw_cache_idx++;
+       ctx->pw_cache_idx++;
 
 done:
        return wbc_status;
@@ -469,16 +472,6 @@ wbcErr wbcGetpwent(struct passwd **pwd)
        return wbcCtxGetpwent(NULL, pwd);
 }
 
-/** @brief Number of cached group structs
- *
- */
-static uint32_t gr_cache_size;
-
-/** @brief Position of the grent context
- *
- */
-static uint32_t gr_cache_idx;
-
 /** @brief Winbindd response containing the group structs
  *
  */
@@ -489,8 +482,12 @@ wbcErr wbcCtxSetgrent(struct wbcContext *ctx)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
 
-       if (gr_cache_size > 0) {
-               gr_cache_idx = gr_cache_size = 0;
+       if (!ctx) {
+               ctx = wbcGetGlobalCtx();
+       }
+
+       if (ctx->gr_cache_size > 0) {
+               ctx->gr_cache_idx = ctx->gr_cache_size = 0;
                winbindd_free_response(&gr_response);
        }
 
@@ -514,8 +511,12 @@ wbcErr wbcCtxEndgrent(struct wbcContext *ctx)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
 
-       if (gr_cache_size > 0) {
-               gr_cache_idx = gr_cache_size = 0;
+       if (!ctx) {
+               ctx = wbcGetGlobalCtx();
+       }
+
+       if (ctx->gr_cache_size > 0) {
+               ctx->gr_cache_idx = ctx->gr_cache_size = 0;
                winbindd_free_response(&gr_response);
        }
 
@@ -540,14 +541,18 @@ wbcErr wbcCtxGetgrent(struct wbcContext *ctx, struct group **grp)
        struct winbindd_gr *wb_gr;
        uint32_t mem_ofs;
 
+       if (!ctx) {
+               ctx = wbcGetGlobalCtx();
+       }
+
        /* If there's a cached result, return that. */
-       if (gr_cache_idx < gr_cache_size) {
+       if (ctx->gr_cache_idx < ctx->gr_cache_size) {
                goto return_result;
        }
 
        /* Otherwise, query winbindd for some entries. */
 
-       gr_cache_idx = 0;
+       ctx->gr_cache_idx = 0;
 
        winbindd_free_response(&gr_response);
 
@@ -559,21 +564,21 @@ wbcErr wbcCtxGetgrent(struct wbcContext *ctx, struct group **grp)
 
        BAIL_ON_WBC_ERROR(wbc_status);
 
-       gr_cache_size = gr_response.data.num_entries;
+       ctx->gr_cache_size = gr_response.data.num_entries;
 
 return_result:
 
        wb_gr = (struct winbindd_gr *) gr_response.extra_data.data;
 
-       mem_ofs = wb_gr[gr_cache_idx].gr_mem_ofs +
-                 gr_cache_size * sizeof(struct winbindd_gr);
+       mem_ofs = wb_gr[ctx->gr_cache_idx].gr_mem_ofs +
+                 ctx->gr_cache_size * sizeof(struct winbindd_gr);
 
-       *grp = copy_group_entry(&wb_gr[gr_cache_idx],
+       *grp = copy_group_entry(&wb_gr[ctx->gr_cache_idx],
                                ((char *)gr_response.extra_data.data)+mem_ofs);
 
        BAIL_ON_PTR_ERROR(*grp, wbc_status);
 
-       gr_cache_idx++;
+       ctx->gr_cache_idx++;
 
 done:
        return wbc_status;
@@ -591,14 +596,18 @@ wbcErr wbcCtxGetgrlist(struct wbcContext *ctx, struct group **grp)
        struct winbindd_request request;
        struct winbindd_gr *wb_gr;
 
+       if (!ctx) {
+               ctx = wbcGetGlobalCtx();
+       }
+
        /* If there's a cached result, return that. */
-       if (gr_cache_idx < gr_cache_size) {
+       if (ctx->gr_cache_idx < ctx->gr_cache_size) {
                goto return_result;
        }
 
        /* Otherwise, query winbindd for some entries. */
 
-       gr_cache_idx = 0;
+       ctx->gr_cache_idx = 0;
 
        winbindd_free_response(&gr_response);
        ZERO_STRUCT(gr_response);
@@ -611,17 +620,17 @@ wbcErr wbcCtxGetgrlist(struct wbcContext *ctx, struct group **grp)
 
        BAIL_ON_WBC_ERROR(wbc_status);
 
-       gr_cache_size = gr_response.data.num_entries;
+       ctx->gr_cache_size = gr_response.data.num_entries;
 
 return_result:
 
        wb_gr = (struct winbindd_gr *) gr_response.extra_data.data;
 
-       *grp = copy_group_entry(&wb_gr[gr_cache_idx], NULL);
+       *grp = copy_group_entry(&wb_gr[ctx->gr_cache_idx], NULL);
 
        BAIL_ON_PTR_ERROR(*grp, wbc_status);
 
-       gr_cache_idx++;
+       ctx->gr_cache_idx++;
 
 done:
        return wbc_status;
index ab1159ad9377ee166b04c6d613586caca7eff28c..5444e823ddfdbfc3b97a3e79811e1fc704aaed10 100644 (file)
@@ -41,6 +41,15 @@ NSS_STATUS winbindd_priv_request_response(struct winbindd_context *wbctx,
 struct winbindd_context *winbindd_ctx_create(void);
 void winbindd_ctx_free(struct winbindd_context *ctx);
 
+/* Global context used for non-Ctx functions */
+
+static struct wbcContext wbcGlobalCtx = {
+       .winbindd_ctx = NULL,
+       .pw_cache_size = 0,
+       .pw_cache_idx = 0,
+       .gr_cache_size = 0,
+       .gr_cache_idx = 0
+};
 
 /*
  result == NSS_STATUS_UNAVAIL: winbind not around
@@ -317,3 +326,8 @@ void wbcCtxFree(struct wbcContext *ctx)
 {
        wbcFreeMemory(ctx);
 }
+
+struct wbcContext *wbcGetGlobalCtx(void)
+{
+       return &wbcGlobalCtx;
+}
index becddacdf82654b9ef42386dc9c5045ee888ed43..6d815c033dd1053a55ce0de566cf61c60facbd5e 100644 (file)
 
 struct wbcContext {
        struct winbindd_context *winbindd_ctx;
+       uint32_t pw_cache_size; /* Number of cached passwd structs */
+       uint32_t pw_cache_idx;  /* Position of the pwent context */
+       uint32_t gr_cache_size; /* Number of cached group structs */
+       uint32_t gr_cache_idx;  /* Position of the grent context */
 };
 
 /* Private functions */
@@ -41,5 +45,6 @@ void *wbcAllocateMemory(size_t nelem, size_t elsize,
 
 char *wbcStrDup(const char *str);
 const char **wbcAllocateStringArray(int num_strings);
+struct wbcContext *wbcGetGlobalCtx(void);
 
 #endif      /* _WBCLIENT_INTERNAL_H */