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 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Milan Jurik. All rights reserved.
  25  */
  26 
  27 #include <stdlib.h>
  28 #include <alloca.h>
  29 #include <signal.h>
  30 #include <sys/stat.h>
  31 #include <unistd.h>
  32 #include <pthread.h>
  33 #include <time.h>
  34 #include <errno.h>
  35 #include <door.h>
  36 #include <zone.h>
  37 #include <resolv.h>
  38 #include <sys/socket.h>
  39 #include <net/route.h>
  40 #include <string.h>
  41 #include <net/if.h>
  42 #include <sys/stat.h>
  43 #include <fcntl.h>
  44 #include "nscd_common.h"
 
 
  70 extern int      _whoami;
  71 extern long     activity;
  72 extern mutex_t  activity_lock;
  73 
  74 static sema_t   common_sema;
  75 
  76 static thread_key_t     lookup_state_key;
  77 static mutex_t          create_lock = DEFAULTMUTEX;
  78 static int              num_servers = 0;
  79 static thread_key_t     server_key;
  80 
  81 /*
  82  * Bind a TSD value to a server thread. This enables the destructor to
  83  * be called if/when this thread exits.  This would be a programming
  84  * error, but better safe than sorry.
  85  */
  86 /*ARGSUSED*/
  87 static void *
  88 server_tsd_bind(void *arg)
  89 {
  90         static void *value = 0;
  91 
  92         /* disable cancellation to avoid hangs if server threads disappear */
  93         (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  94         (void) thr_setspecific(server_key, value);
  95         (void) door_return(NULL, 0, NULL, 0);
  96 
  97         /* make lint happy */
  98         return (NULL);
  99 }
 100 
 101 /*
 102  * Server threads are created here.
 103  */
 104 /*ARGSUSED*/
 105 static void
 106 server_create(door_info_t *dip)
 107 {
 108         (void) mutex_lock(&create_lock);
 109         if (++num_servers > max_servers) {
 110                 num_servers--;
 111                 (void) mutex_unlock(&create_lock);
 112                 return;
 113         }
 114         (void) mutex_unlock(&create_lock);
 115         (void) thr_create(NULL, 0, server_tsd_bind, NULL,
 116             THR_BOUND|THR_DETACHED, NULL);
 117 }
 118 
 119 /*
 120  * Server thread are destroyed here
 121  */
 122 /*ARGSUSED*/
 123 static void
 124 server_destroy(void *arg)
 125 {
 126         (void) mutex_lock(&create_lock);
 127         num_servers--;
 128         (void) mutex_unlock(&create_lock);
 129 }
 130 
 131 /*
 132  * get clearance
 133  */
 134 int
 135 _nscd_get_clearance(sema_t *sema) {
 136         if (sema_trywait(&common_sema) == 0) {
 137                 (void) thr_setspecific(lookup_state_key, NULL);
 138                 return (0);
 139         }
 140 
 141         if (sema_trywait(sema) == 0) {
 142                 (void) thr_setspecific(lookup_state_key, (void*)1);
 143                 return (0);
 144         }
 145 
 146         return (1);
 147 }
 148 
 
 | 
 
 
   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 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Milan Jurik. All rights reserved.
  25  * Copyright 2019 Nexenta Systems, Inc.
  26  */
  27 
  28 #include <stdlib.h>
  29 #include <alloca.h>
  30 #include <signal.h>
  31 #include <sys/stat.h>
  32 #include <unistd.h>
  33 #include <pthread.h>
  34 #include <time.h>
  35 #include <errno.h>
  36 #include <door.h>
  37 #include <zone.h>
  38 #include <resolv.h>
  39 #include <sys/socket.h>
  40 #include <net/route.h>
  41 #include <string.h>
  42 #include <net/if.h>
  43 #include <sys/stat.h>
  44 #include <fcntl.h>
  45 #include "nscd_common.h"
 
 
  71 extern int      _whoami;
  72 extern long     activity;
  73 extern mutex_t  activity_lock;
  74 
  75 static sema_t   common_sema;
  76 
  77 static thread_key_t     lookup_state_key;
  78 static mutex_t          create_lock = DEFAULTMUTEX;
  79 static int              num_servers = 0;
  80 static thread_key_t     server_key;
  81 
  82 /*
  83  * Bind a TSD value to a server thread. This enables the destructor to
  84  * be called if/when this thread exits.  This would be a programming
  85  * error, but better safe than sorry.
  86  */
  87 /*ARGSUSED*/
  88 static void *
  89 server_tsd_bind(void *arg)
  90 {
  91         static void *value = "NON-NULL TSD";
  92 
  93         /* disable cancellation to avoid hangs if server threads disappear */
  94         (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  95         (void) thr_setspecific(server_key, value);
  96         (void) door_return(NULL, 0, NULL, 0);
  97 
  98         /* make lint happy */
  99         return (NULL);
 100 }
 101 
 102 /*
 103  * Server threads are created here.
 104  */
 105 /*ARGSUSED*/
 106 static void
 107 server_create(door_info_t *dip)
 108 {
 109         (void) mutex_lock(&create_lock);
 110         if (++num_servers > max_servers) {
 111                 num_servers--;
 112                 (void) mutex_unlock(&create_lock);
 113                 return;
 114         }
 115         (void) mutex_unlock(&create_lock);
 116         (void) thr_create(NULL, 0, server_tsd_bind, NULL,
 117             THR_BOUND|THR_DETACHED, NULL);
 118 }
 119 
 120 /*
 121  * Server thread are destroyed here
 122  */
 123 /*ARGSUSED*/
 124 static void
 125 server_destroy(void *arg)
 126 {
 127         (void) mutex_lock(&create_lock);
 128         num_servers--;
 129         (void) mutex_unlock(&create_lock);
 130         (void) thr_setspecific(server_key, NULL);
 131 }
 132 
 133 /*
 134  * get clearance
 135  */
 136 int
 137 _nscd_get_clearance(sema_t *sema) {
 138         if (sema_trywait(&common_sema) == 0) {
 139                 (void) thr_setspecific(lookup_state_key, NULL);
 140                 return (0);
 141         }
 142 
 143         if (sema_trywait(sema) == 0) {
 144                 (void) thr_setspecific(lookup_state_key, (void*)1);
 145                 return (0);
 146         }
 147 
 148         return (1);
 149 }
 150 
 
 |