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