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  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <signal.h>
  27 #include <sys/types.h>
  28 #include <sys/time.h>
  29 #include <fcntl.h>
  30 #include <sys/stat.h>
  31 #include <ctype.h>
  32 #include <stdio.h>
  33 #include <errno.h>
  34 #include <stdlib.h>
  35 #include <unistd.h>
  36 #include <locale.h>
  37 #include <sys/nsctl/cfg.h>
  38 
  39 CFGFILE *cfg;
  40 void    cfg_lockd_stat();
  41 int     tty;
  42 
  43 static void
  44 test(int count)
  45 {
  46         struct stat sb;
  47         int i;
  48 
  49         if (count < 1)
  50                 count = 1;
  51         for (i = 0; count-- > 0; i++) {
  52                 if (cfg_lock(cfg, CFG_RDLOCK) < 0)
  53                         (void) printf("CFG_RDLOCK error\n");
  54                 else
  55                         (void) fstat(0, &sb);
  56 
  57                 cfg_unlock(cfg);
  58                 (void) fstat(1, &sb);
  59 
  60                 if (cfg_lock(cfg, CFG_RDLOCK) < 0)
  61                         (void) printf("CFG_RDLOCK error\n");
  62                 else
  63                         (void) fstat(0, &sb);
  64 
  65                 cfg_unlock(cfg);
  66                 (void) fstat(1, &sb);
  67 
  68                 if (cfg_lock(cfg, CFG_WRLOCK) < 0)
  69                         (void) printf("CFG_WRLOCK error\n");
  70                 else
  71                         (void) fstat(0, &sb);
  72 
  73                 cfg_unlock(cfg);
  74                 (void) fstat(1, &sb);
  75 
  76                 if (i > 0) {
  77                         if (i % 100 == 0)
  78                                 (void) write(1, "+", 1);
  79                         if (i % 5000 == 0)
  80                                 (void) write(1, "\n", 1);
  81                 }
  82         }
  83         (void) printf("\nTest complete\n");
  84 }
  85 
  86 static void
  87 cmd_loop()
  88 {
  89         char    host[1024];
  90         char    buffer[1024];
  91         int     i;
  92 
  93         (void) gethostname(host, sizeof (host));
  94         for (;;) {
  95                 if (tty)
  96                         (void) printf(":%s: ", host);
  97                 (void) fgets(buffer, sizeof (buffer), stdin);
  98                 switch (tolower(buffer[0])) {
  99                         case 'p':
 100                                 i = atoi(buffer + 1);
 101                                 (void) sleep(i);
 102                                 break;
 103                         case 'q':
 104                                 exit(0);
 105                                 break;
 106                         case 'r':
 107                                 if (cfg_lock(cfg, CFG_RDLOCK) < 0)
 108                                         (void) printf("CFG_RDLOCK error\n");
 109                                 break;
 110                         case 's':
 111                                 cfg_lockd_stat();
 112                                 break;
 113                         case 't':
 114                                 i = atoi(buffer + 1);
 115                                 test(i);
 116                                 break;
 117                         case 'u':
 118                                 cfg_unlock(cfg);
 119                                 break;
 120                         case 'w':
 121                                 if (cfg_lock(cfg, CFG_WRLOCK) < 0)
 122                                         (void) printf("CFG_WRLOCK error\n");
 123                                 break;
 124                         default:
 125                                 (void) printf("don't understand %s\n", buffer);
 126                                 break;
 127                 }
 128         }
 129 }
 130 
 131 static void
 132 init()
 133 {
 134         tty = isatty(0);
 135         if (tty)
 136                 (void) printf("dscfglockd cli %s\n", "07/06/12");
 137         if ((cfg = cfg_open(NULL)) == NULL) {
 138                 perror("cfg_open");
 139                 exit(1);
 140         }
 141 }
 142 
 143 int
 144 main(void)
 145 {
 146         init();
 147         cmd_loop();
 148         return (0);
 149 }