1 /*
   2  * CDDL HEADER START
   3  *
   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 /*
  23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * get_domain_mt.c
  29  *    This program starts multiple threads to call mapid_get_domain()
  30  *    and test its thread-safety.
  31  *
  32  * Return value:
  33  *    On success, it returns 0; on error, it returns 1.
  34  *
  35  * Usage:
  36  *    ./get_domain_mt
  37  */
  38 
  39 #include <stdio.h>
  40 #include <pthread.h>
  41 #include <nfs/mapid.h>
  42 
  43 #define NTHREADS        100
  44 
  45 char domain[256];
  46 
  47 void *get_domain(void *arg);
  48 
  49 int
  50 main()
  51 {
  52         pthread_t threads[NTHREADS+1];
  53         int ids[NTHREADS+1];
  54         char *p;
  55         int i;
  56 
  57         mapid_reeval_domain((cb_t *)0);
  58         p = mapid_get_domain();
  59         if (!p) {
  60                 fprintf(stderr, "mapid_get_domain() returned NULL\n");
  61                 perror("mapid_get_domain");
  62                 exit(1);
  63         }
  64         strcpy(domain, p);
  65 
  66         /* start threads */
  67         for (i = 1; i <= NTHREADS; i++) {
  68                 ids[i] = i;
  69                 if (pthread_create(&threads[i], NULL, get_domain, &ids[i])) {
  70                         fprintf(stderr, "failed to start thread %d\n", i);
  71                         perror("pthread_create");
  72                         exit(1);
  73                 }
  74         }
  75 
  76         /* check threads' exit statuses */
  77         for (i = 1; i <= NTHREADS; i++) {
  78                 if (pthread_join(threads[i], (void **)&ids[i])) {
  79                         fprintf(stderr, "failed to get thread %d status\n", i);
  80                         perror("pthread_join");
  81                         exit(1);
  82                 }
  83 
  84                 if (ids[i]) {
  85                         fprintf(stderr, "thread %d terminated abnormally\n");
  86                         exit(1);
  87                 }
  88         }
  89 
  90         exit(0);
  91 }
  92 
  93 void *
  94 get_domain(void *arg) {
  95         int id = *((int *)arg);
  96         char *p;
  97 
  98         p = mapid_get_domain();
  99         if (!p) {
 100                 fprintf(stderr, "mapid_get_domain() returned NULL"
 101                 "in thread %d\n", id);
 102                 perror("mapid_get_domain");
 103                 pthread_exit((void *)1);
 104         }
 105 
 106         if (strcmp(domain, p)) {
 107                 fprintf(stderr, "domain values don't match:\n");
 108                 fprintf(stderr, "Main Thread\t\tThread %d\n", id);
 109                 fprintf(stderr, "===========\t\t=========\n", id);
 110                 fprintf(stderr, "%s\t\t%s\n", domain, p);
 111                 pthread_exit((void *)1);
 112         }
 113 
 114         pthread_exit((void *)0);
 115 }