xref: /titanic_52/usr/src/cmd/avs/dscfglockd/dscfgcli.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte /*
22*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*fcf3ce44SJohn Forte  * Use is subject to license terms.
24*fcf3ce44SJohn Forte  */
25*fcf3ce44SJohn Forte 
26*fcf3ce44SJohn Forte #include <signal.h>
27*fcf3ce44SJohn Forte #include <sys/types.h>
28*fcf3ce44SJohn Forte #include <sys/time.h>
29*fcf3ce44SJohn Forte #include <fcntl.h>
30*fcf3ce44SJohn Forte #include <sys/stat.h>
31*fcf3ce44SJohn Forte #include <ctype.h>
32*fcf3ce44SJohn Forte #include <stdio.h>
33*fcf3ce44SJohn Forte #include <errno.h>
34*fcf3ce44SJohn Forte #include <stdlib.h>
35*fcf3ce44SJohn Forte #include <unistd.h>
36*fcf3ce44SJohn Forte #include <locale.h>
37*fcf3ce44SJohn Forte #include <sys/nsctl/cfg.h>
38*fcf3ce44SJohn Forte 
39*fcf3ce44SJohn Forte CFGFILE *cfg;
40*fcf3ce44SJohn Forte void	cfg_lockd_stat();
41*fcf3ce44SJohn Forte int	tty;
42*fcf3ce44SJohn Forte 
43*fcf3ce44SJohn Forte static void
44*fcf3ce44SJohn Forte test(int count)
45*fcf3ce44SJohn Forte {
46*fcf3ce44SJohn Forte 	struct stat sb;
47*fcf3ce44SJohn Forte 	int i;
48*fcf3ce44SJohn Forte 
49*fcf3ce44SJohn Forte 	if (count < 1)
50*fcf3ce44SJohn Forte 		count = 1;
51*fcf3ce44SJohn Forte 	for (i = 0; count-- > 0; i++) {
52*fcf3ce44SJohn Forte 		if (cfg_lock(cfg, CFG_RDLOCK) < 0)
53*fcf3ce44SJohn Forte 			(void) printf("CFG_RDLOCK error\n");
54*fcf3ce44SJohn Forte 		else
55*fcf3ce44SJohn Forte 			fstat(0, &sb);
56*fcf3ce44SJohn Forte 
57*fcf3ce44SJohn Forte 		cfg_unlock(cfg);
58*fcf3ce44SJohn Forte 		fstat(1, &sb);
59*fcf3ce44SJohn Forte 
60*fcf3ce44SJohn Forte 		if (cfg_lock(cfg, CFG_RDLOCK) < 0)
61*fcf3ce44SJohn Forte 			(void) printf("CFG_RDLOCK error\n");
62*fcf3ce44SJohn Forte 		else
63*fcf3ce44SJohn Forte 			fstat(0, &sb);
64*fcf3ce44SJohn Forte 
65*fcf3ce44SJohn Forte 		cfg_unlock(cfg);
66*fcf3ce44SJohn Forte 		fstat(1, &sb);
67*fcf3ce44SJohn Forte 
68*fcf3ce44SJohn Forte 		if (cfg_lock(cfg, CFG_WRLOCK) < 0)
69*fcf3ce44SJohn Forte 			(void) printf("CFG_WRLOCK error\n");
70*fcf3ce44SJohn Forte 		else
71*fcf3ce44SJohn Forte 			fstat(0, &sb);
72*fcf3ce44SJohn Forte 
73*fcf3ce44SJohn Forte 		cfg_unlock(cfg);
74*fcf3ce44SJohn Forte 		fstat(1, &sb);
75*fcf3ce44SJohn Forte 
76*fcf3ce44SJohn Forte 		if (i > 0) {
77*fcf3ce44SJohn Forte 			if (i % 100 == 0)
78*fcf3ce44SJohn Forte 				(void) write(1, "+", 1);
79*fcf3ce44SJohn Forte 			if (i % 5000 == 0)
80*fcf3ce44SJohn Forte 				(void) write(1, "\n", 1);
81*fcf3ce44SJohn Forte 		}
82*fcf3ce44SJohn Forte 	}
83*fcf3ce44SJohn Forte 	(void) printf("\nTest complete\n");
84*fcf3ce44SJohn Forte }
85*fcf3ce44SJohn Forte 
86*fcf3ce44SJohn Forte static void
87*fcf3ce44SJohn Forte cmd_loop()
88*fcf3ce44SJohn Forte {
89*fcf3ce44SJohn Forte 	char	host[1024];
90*fcf3ce44SJohn Forte 	char	buffer[1024];
91*fcf3ce44SJohn Forte 	int	i;
92*fcf3ce44SJohn Forte 
93*fcf3ce44SJohn Forte 	(void) gethostname(host, sizeof (host));
94*fcf3ce44SJohn Forte 	for (;;) {
95*fcf3ce44SJohn Forte 		if (tty)
96*fcf3ce44SJohn Forte 			(void) printf(":%s: ", host);
97*fcf3ce44SJohn Forte 		(void) fgets(buffer, sizeof (buffer), stdin);
98*fcf3ce44SJohn Forte 		switch (tolower(buffer[0])) {
99*fcf3ce44SJohn Forte 			case 'p':
100*fcf3ce44SJohn Forte 				i = atoi(buffer + 1);
101*fcf3ce44SJohn Forte 				(void) sleep(i);
102*fcf3ce44SJohn Forte 				break;
103*fcf3ce44SJohn Forte 			case 'q':
104*fcf3ce44SJohn Forte 				exit(0);
105*fcf3ce44SJohn Forte 				break;
106*fcf3ce44SJohn Forte 			case 'r':
107*fcf3ce44SJohn Forte 				if (cfg_lock(cfg, CFG_RDLOCK) < 0)
108*fcf3ce44SJohn Forte 					(void) printf("CFG_RDLOCK error\n");
109*fcf3ce44SJohn Forte 				break;
110*fcf3ce44SJohn Forte 			case 's':
111*fcf3ce44SJohn Forte 				cfg_lockd_stat();
112*fcf3ce44SJohn Forte 				break;
113*fcf3ce44SJohn Forte 			case 't':
114*fcf3ce44SJohn Forte 				i = atoi(buffer + 1);
115*fcf3ce44SJohn Forte 				test(i);
116*fcf3ce44SJohn Forte 				break;
117*fcf3ce44SJohn Forte 			case 'u':
118*fcf3ce44SJohn Forte 				cfg_unlock(cfg);
119*fcf3ce44SJohn Forte 				break;
120*fcf3ce44SJohn Forte 			case 'w':
121*fcf3ce44SJohn Forte 				if (cfg_lock(cfg, CFG_WRLOCK) < 0)
122*fcf3ce44SJohn Forte 					(void) printf("CFG_WRLOCK error\n");
123*fcf3ce44SJohn Forte 				break;
124*fcf3ce44SJohn Forte 			default:
125*fcf3ce44SJohn Forte 				(void) printf("don't understand %s\n", buffer);
126*fcf3ce44SJohn Forte 				break;
127*fcf3ce44SJohn Forte 		}
128*fcf3ce44SJohn Forte 	}
129*fcf3ce44SJohn Forte }
130*fcf3ce44SJohn Forte 
131*fcf3ce44SJohn Forte static void
132*fcf3ce44SJohn Forte init()
133*fcf3ce44SJohn Forte {
134*fcf3ce44SJohn Forte 	tty = isatty(0);
135*fcf3ce44SJohn Forte 	if (tty)
136*fcf3ce44SJohn Forte 		(void) printf("dscfglockd cli %s\n", "07/06/12");
137*fcf3ce44SJohn Forte 	if ((cfg = cfg_open(NULL)) == NULL) {
138*fcf3ce44SJohn Forte 		perror("cfg_open");
139*fcf3ce44SJohn Forte 		exit(1);
140*fcf3ce44SJohn Forte 	}
141*fcf3ce44SJohn Forte }
142*fcf3ce44SJohn Forte 
143*fcf3ce44SJohn Forte int
144*fcf3ce44SJohn Forte main(void)
145*fcf3ce44SJohn Forte {
146*fcf3ce44SJohn Forte 	init();
147*fcf3ce44SJohn Forte 	cmd_loop();
148*fcf3ce44SJohn Forte 	return (0);
149*fcf3ce44SJohn Forte }
150