Print this page
8158 Want named threads API
9857 proc manpages should have LIBRARY section


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2012 Milan Jurik. All rights reserved.
  24  * Copyright (c) 2016 by Delphix. All rights reserved.

  25  */
  26 
  27 /*
  28  * Cache routines for nscd
  29  */
  30 #include <assert.h>
  31 #include <errno.h>
  32 #include <memory.h>
  33 #include <signal.h>
  34 #include <stdlib.h>
  35 #include <stddef.h>
  36 #include <stdio.h>
  37 #include <string.h>
  38 #include <sys/stat.h>
  39 #include <sys/time.h>
  40 #include <sys/types.h>
  41 #include <sys/wait.h>
  42 #include <unistd.h>
  43 #include <ucred.h>
  44 #include <nss_common.h>


1800 
1801         ctx = calloc(1, sizeof (nsc_ctx_t));
1802         if (ctx == NULL)
1803                 return (NULL);
1804 
1805         /* init locks and semaphores */
1806         (void) mutex_init(&ctx->file_mutex, USYNC_THREAD, NULL);
1807         (void) rwlock_init(&ctx->cfg_rwlp, USYNC_THREAD, NULL);
1808         (void) mutex_init(&ctx->stats_mutex, USYNC_THREAD, NULL);
1809         (void) _nscd_init_cache_sema(&ctx->throttle_sema, cache_name[i]);
1810         cache_init_ctx[i](ctx);
1811         cache_ctx_p[i] = ctx;
1812 
1813         return (ctx);
1814 }
1815 
1816 
1817 static void
1818 revalidate(nsc_ctx_t *ctx)
1819 {


1820         for (;;) {
1821                 int             i, slp, interval, count;
1822 
1823                 (void) rw_rdlock(&ctx->cfg_rwlp);
1824                 slp = ctx->cfg.pos_ttl;
1825                 count = ctx->cfg.keephot;
1826                 (void) rw_unlock(&ctx->cfg_rwlp);
1827 
1828                 if (slp < 60)
1829                         slp = 60;
1830                 if (count != 0) {
1831                         interval = (slp/2)/count;
1832                         if (interval == 0)
1833                                 interval = 1;
1834                         (void) sleep(slp*2/3);
1835                         for (i = 0; i < ctx->db_count; i++) {
1836                                 getxy_keepalive(ctx, ctx->nsc_db[i],
1837                                     count, interval);
1838                         }
1839                 } else {


1944 {
1945         char    *me = "launch_update";
1946         int     errnum;
1947 
1948         errnum = thr_create(NULL, NULL, (void *(*)(void*))do_update,
1949             in, 0|THR_DETACHED, NULL);
1950         if (errnum != 0) {
1951                 _NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1952                 (me, "%s: thread creation failure (%d)\n",
1953                     in->nscdb->name, errnum);
1954                 return (-1);
1955         }
1956         return (0);
1957 }
1958 
1959 
1960 static void
1961 do_update(nsc_lookup_args_t *in) {
1962         nss_pheader_t   *phdr = (nss_pheader_t *)in->buffer;
1963 


1964         /* update the length of the data buffer */
1965         phdr->data_len = phdr->pbufsiz - phdr->data_off;
1966 
1967         (void) lookup_int(in, UPDATEBIT);
1968         if (in->buffer)
1969                 free(in->buffer);
1970         free(in);
1971 }
1972 
1973 
1974 /*
1975  * Invalidate cache
1976  */
1977 void
1978 nsc_invalidate(nsc_ctx_t *ctx, char *dbname, nsc_ctx_t **ctxs)
1979 {
1980         int     i;
1981         char    *me = "nsc_invalidate";
1982 
1983         if (ctx) {


2173                 }
2174 
2175                 /*
2176                  * It's okay if we were not able to find one to delete.
2177                  * The reaper (when invoked) will return the cache to a
2178                  * safe level.
2179                  */
2180         }
2181 
2182         return (NSCD_SUCCESS);
2183 }
2184 
2185 static void
2186 reaper(nsc_ctx_t *ctx)
2187 {
2188         uint_t          ttl, extra_sleep, total_sleep, intervals;
2189         uint_t          nodes_per_interval, seconds_per_interval;
2190         ulong_t         nsc_entries;
2191         char            *me = "reaper";
2192 


2193         for (;;) {
2194                 (void) mutex_lock(&ctx->stats_mutex);
2195                 nsc_entries = ctx->stats.entries;
2196                 (void) mutex_unlock(&ctx->stats_mutex);
2197 
2198                 (void) rw_rdlock(&ctx->cfg_rwlp);
2199                 ttl = ctx->cfg.pos_ttl;
2200                 (void) rw_unlock(&ctx->cfg_rwlp);
2201 
2202                 if (nsc_entries == 0) {
2203                         _NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2204                                 (me, "%s: nothing to reap\n", ctx->dbname);
2205 
2206                         /* sleep for atleast 60 seconds */
2207                         if (ttl < 60)
2208                                 ttl = 60;
2209                         _NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2210                         (me, "%s: sleep %d\n", ctx->dbname, ttl);
2211                         (void) sleep(ttl);
2212                         continue;




   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2012 Milan Jurik. All rights reserved.
  24  * Copyright (c) 2016 by Delphix. All rights reserved.
  25  * Copyright 2018 Joyent, Inc.
  26  */
  27 
  28 /*
  29  * Cache routines for nscd
  30  */
  31 #include <assert.h>
  32 #include <errno.h>
  33 #include <memory.h>
  34 #include <signal.h>
  35 #include <stdlib.h>
  36 #include <stddef.h>
  37 #include <stdio.h>
  38 #include <string.h>
  39 #include <sys/stat.h>
  40 #include <sys/time.h>
  41 #include <sys/types.h>
  42 #include <sys/wait.h>
  43 #include <unistd.h>
  44 #include <ucred.h>
  45 #include <nss_common.h>


1801 
1802         ctx = calloc(1, sizeof (nsc_ctx_t));
1803         if (ctx == NULL)
1804                 return (NULL);
1805 
1806         /* init locks and semaphores */
1807         (void) mutex_init(&ctx->file_mutex, USYNC_THREAD, NULL);
1808         (void) rwlock_init(&ctx->cfg_rwlp, USYNC_THREAD, NULL);
1809         (void) mutex_init(&ctx->stats_mutex, USYNC_THREAD, NULL);
1810         (void) _nscd_init_cache_sema(&ctx->throttle_sema, cache_name[i]);
1811         cache_init_ctx[i](ctx);
1812         cache_ctx_p[i] = ctx;
1813 
1814         return (ctx);
1815 }
1816 
1817 
1818 static void
1819 revalidate(nsc_ctx_t *ctx)
1820 {
1821         (void) thr_setname(thr_self(), "revalidate");
1822 
1823         for (;;) {
1824                 int i, slp, interval, count;
1825 
1826                 (void) rw_rdlock(&ctx->cfg_rwlp);
1827                 slp = ctx->cfg.pos_ttl;
1828                 count = ctx->cfg.keephot;
1829                 (void) rw_unlock(&ctx->cfg_rwlp);
1830 
1831                 if (slp < 60)
1832                         slp = 60;
1833                 if (count != 0) {
1834                         interval = (slp/2)/count;
1835                         if (interval == 0)
1836                                 interval = 1;
1837                         (void) sleep(slp*2/3);
1838                         for (i = 0; i < ctx->db_count; i++) {
1839                                 getxy_keepalive(ctx, ctx->nsc_db[i],
1840                                     count, interval);
1841                         }
1842                 } else {


1947 {
1948         char    *me = "launch_update";
1949         int     errnum;
1950 
1951         errnum = thr_create(NULL, NULL, (void *(*)(void*))do_update,
1952             in, 0|THR_DETACHED, NULL);
1953         if (errnum != 0) {
1954                 _NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1955                 (me, "%s: thread creation failure (%d)\n",
1956                     in->nscdb->name, errnum);
1957                 return (-1);
1958         }
1959         return (0);
1960 }
1961 
1962 
1963 static void
1964 do_update(nsc_lookup_args_t *in) {
1965         nss_pheader_t   *phdr = (nss_pheader_t *)in->buffer;
1966 
1967         (void) thr_setname(thr_self(), "do_update");
1968 
1969         /* update the length of the data buffer */
1970         phdr->data_len = phdr->pbufsiz - phdr->data_off;
1971 
1972         (void) lookup_int(in, UPDATEBIT);
1973         if (in->buffer)
1974                 free(in->buffer);
1975         free(in);
1976 }
1977 
1978 
1979 /*
1980  * Invalidate cache
1981  */
1982 void
1983 nsc_invalidate(nsc_ctx_t *ctx, char *dbname, nsc_ctx_t **ctxs)
1984 {
1985         int     i;
1986         char    *me = "nsc_invalidate";
1987 
1988         if (ctx) {


2178                 }
2179 
2180                 /*
2181                  * It's okay if we were not able to find one to delete.
2182                  * The reaper (when invoked) will return the cache to a
2183                  * safe level.
2184                  */
2185         }
2186 
2187         return (NSCD_SUCCESS);
2188 }
2189 
2190 static void
2191 reaper(nsc_ctx_t *ctx)
2192 {
2193         uint_t          ttl, extra_sleep, total_sleep, intervals;
2194         uint_t          nodes_per_interval, seconds_per_interval;
2195         ulong_t         nsc_entries;
2196         char            *me = "reaper";
2197 
2198         (void) thr_setname(thr_self(), me);
2199 
2200         for (;;) {
2201                 (void) mutex_lock(&ctx->stats_mutex);
2202                 nsc_entries = ctx->stats.entries;
2203                 (void) mutex_unlock(&ctx->stats_mutex);
2204 
2205                 (void) rw_rdlock(&ctx->cfg_rwlp);
2206                 ttl = ctx->cfg.pos_ttl;
2207                 (void) rw_unlock(&ctx->cfg_rwlp);
2208 
2209                 if (nsc_entries == 0) {
2210                         _NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2211                                 (me, "%s: nothing to reap\n", ctx->dbname);
2212 
2213                         /* sleep for atleast 60 seconds */
2214                         if (ttl < 60)
2215                                 ttl = 60;
2216                         _NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2217                         (me, "%s: sleep %d\n", ctx->dbname, ttl);
2218                         (void) sleep(ttl);
2219                         continue;