Print this page
NEX-19665 Several door servers don't properly handle exiting threads
Review by: Gordon Ross <gordon.ross@nexenta.com>
Review by: Evan Layton <evan.layton@nexenta.com>


   4  * The contents of this file are subject to the terms of the
   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  */
  25 
  26 /*
  27  * Simple doors ldap cache daemon
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <signal.h>
  33 #include <door.h>
  34 #include <time.h>
  35 #include <string.h>
  36 #include <strings.h>
  37 #include <libintl.h>
  38 #include <sys/stat.h>
  39 #include <sys/time.h>
  40 #include <sys/wait.h>
  41 #include <stdlib.h>
  42 #include <errno.h>
  43 #include <pthread.h>


 208 
 209         return (0);
 210 }
 211 
 212 
 213 static mutex_t          create_lock;
 214 static int              num_servers = 0;
 215 static thread_key_t     server_key;
 216 
 217 
 218 /*
 219  * Bind a TSD value to a server thread. This enables the destructor to
 220  * be called if/when this thread exits.  This would be a programming error,
 221  * but better safe than sorry.
 222  */
 223 
 224 /*ARGSUSED*/
 225 static void *
 226 server_tsd_bind(void *arg)
 227 {
 228         static void     *value = 0;
 229 
 230         /*
 231          * disable cancellation to prevent hangs when server
 232          * threads disappear
 233          */
 234 
 235         (void) thr_setspecific(server_key, value);
 236         (void) door_return(NULL, 0, NULL, 0);
 237 
 238         return (value);
 239 }
 240 
 241 /*
 242  * Server threads are created here.
 243  */
 244 
 245 /*ARGSUSED*/
 246 static void
 247 server_create(door_info_t *dip)
 248 {
 249         (void) mutex_lock(&create_lock);
 250         if (++num_servers > MAX_SERVER_THREADS) {
 251                 num_servers--;
 252                 (void) mutex_unlock(&create_lock);
 253                 return;
 254         }
 255         (void) mutex_unlock(&create_lock);
 256         (void) thr_create(NULL, 0, server_tsd_bind, NULL,
 257             THR_BOUND|THR_DETACHED, NULL);
 258 }
 259 
 260 /*
 261  * Server thread are destroyed here
 262  */
 263 
 264 /*ARGSUSED*/
 265 static void
 266 server_destroy(void *arg)
 267 {
 268         (void) mutex_lock(&create_lock);
 269         num_servers--;
 270         (void) mutex_unlock(&create_lock);

 271 }
 272 
 273 static void             client_killserver();
 274 
 275 int
 276 main(int argc, char ** argv)
 277 {
 278         int                     did;
 279         int                     opt;
 280         int                     errflg = 0;
 281         int                     showstats = 0;
 282         int                     doset = 0;
 283         int                     dofg = 0;
 284         struct stat             buf;
 285         sigset_t                myset;
 286         struct sigaction        sighupaction;
 287         int                     debug_level = 0;
 288 
 289         /* setup for localization */
 290         (void) setlocale(LC_ALL, "");




   4  * The contents of this file are subject to the terms of the
   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 2019 Nexenta Systems, Inc.
  25  */
  26 
  27 /*
  28  * Simple doors ldap cache daemon
  29  */
  30 
  31 #include <stdio.h>
  32 #include <stdlib.h>
  33 #include <signal.h>
  34 #include <door.h>
  35 #include <time.h>
  36 #include <string.h>
  37 #include <strings.h>
  38 #include <libintl.h>
  39 #include <sys/stat.h>
  40 #include <sys/time.h>
  41 #include <sys/wait.h>
  42 #include <stdlib.h>
  43 #include <errno.h>
  44 #include <pthread.h>


 209 
 210         return (0);
 211 }
 212 
 213 
 214 static mutex_t          create_lock;
 215 static int              num_servers = 0;
 216 static thread_key_t     server_key;
 217 
 218 
 219 /*
 220  * Bind a TSD value to a server thread. This enables the destructor to
 221  * be called if/when this thread exits.  This would be a programming error,
 222  * but better safe than sorry.
 223  */
 224 
 225 /*ARGSUSED*/
 226 static void *
 227 server_tsd_bind(void *arg)
 228 {
 229         static void *value = "NON-NULL TSD";
 230 
 231         /*
 232          * disable cancellation to prevent hangs when server
 233          * threads disappear
 234          */
 235         (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
 236         (void) thr_setspecific(server_key, value);
 237         (void) door_return(NULL, 0, NULL, 0);
 238 
 239         return (value);
 240 }
 241 
 242 /*
 243  * Server threads are created here.
 244  */
 245 
 246 /*ARGSUSED*/
 247 static void
 248 server_create(door_info_t *dip)
 249 {
 250         (void) mutex_lock(&create_lock);
 251         if (++num_servers > MAX_SERVER_THREADS) {
 252                 num_servers--;
 253                 (void) mutex_unlock(&create_lock);
 254                 return;
 255         }
 256         (void) mutex_unlock(&create_lock);
 257         (void) thr_create(NULL, 0, server_tsd_bind, NULL,
 258             THR_BOUND|THR_DETACHED, NULL);
 259 }
 260 
 261 /*
 262  * Server thread are destroyed here
 263  */
 264 
 265 /*ARGSUSED*/
 266 static void
 267 server_destroy(void *arg)
 268 {
 269         (void) mutex_lock(&create_lock);
 270         num_servers--;
 271         (void) mutex_unlock(&create_lock);
 272         (void) thr_setspecific(server_key, NULL);
 273 }
 274 
 275 static void             client_killserver();
 276 
 277 int
 278 main(int argc, char ** argv)
 279 {
 280         int                     did;
 281         int                     opt;
 282         int                     errflg = 0;
 283         int                     showstats = 0;
 284         int                     doset = 0;
 285         int                     dofg = 0;
 286         struct stat             buf;
 287         sigset_t                myset;
 288         struct sigaction        sighupaction;
 289         int                     debug_level = 0;
 290 
 291         /* setup for localization */
 292         (void) setlocale(LC_ALL, "");