xref: /onnv-gate/usr/src/cmd/avs/dscfglockd/dscfgcli.c (revision 11576:b23c42c0c9d6)
17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM  * CDDL HEADER START
37836SJohn.Forte@Sun.COM  *
47836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM  *
87836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM  * and limitations under the License.
127836SJohn.Forte@Sun.COM  *
137836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM  *
197836SJohn.Forte@Sun.COM  * CDDL HEADER END
207836SJohn.Forte@Sun.COM  */
217836SJohn.Forte@Sun.COM /*
22*11576SSurya.Prakki@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237836SJohn.Forte@Sun.COM  * Use is subject to license terms.
247836SJohn.Forte@Sun.COM  */
257836SJohn.Forte@Sun.COM 
267836SJohn.Forte@Sun.COM #include <signal.h>
277836SJohn.Forte@Sun.COM #include <sys/types.h>
287836SJohn.Forte@Sun.COM #include <sys/time.h>
297836SJohn.Forte@Sun.COM #include <fcntl.h>
307836SJohn.Forte@Sun.COM #include <sys/stat.h>
317836SJohn.Forte@Sun.COM #include <ctype.h>
327836SJohn.Forte@Sun.COM #include <stdio.h>
337836SJohn.Forte@Sun.COM #include <errno.h>
347836SJohn.Forte@Sun.COM #include <stdlib.h>
357836SJohn.Forte@Sun.COM #include <unistd.h>
367836SJohn.Forte@Sun.COM #include <locale.h>
377836SJohn.Forte@Sun.COM #include <sys/nsctl/cfg.h>
387836SJohn.Forte@Sun.COM 
397836SJohn.Forte@Sun.COM CFGFILE *cfg;
407836SJohn.Forte@Sun.COM void	cfg_lockd_stat();
417836SJohn.Forte@Sun.COM int	tty;
427836SJohn.Forte@Sun.COM 
437836SJohn.Forte@Sun.COM static void
test(int count)447836SJohn.Forte@Sun.COM test(int count)
457836SJohn.Forte@Sun.COM {
467836SJohn.Forte@Sun.COM 	struct stat sb;
477836SJohn.Forte@Sun.COM 	int i;
487836SJohn.Forte@Sun.COM 
497836SJohn.Forte@Sun.COM 	if (count < 1)
507836SJohn.Forte@Sun.COM 		count = 1;
517836SJohn.Forte@Sun.COM 	for (i = 0; count-- > 0; i++) {
527836SJohn.Forte@Sun.COM 		if (cfg_lock(cfg, CFG_RDLOCK) < 0)
537836SJohn.Forte@Sun.COM 			(void) printf("CFG_RDLOCK error\n");
547836SJohn.Forte@Sun.COM 		else
55*11576SSurya.Prakki@Sun.COM 			(void) fstat(0, &sb);
567836SJohn.Forte@Sun.COM 
577836SJohn.Forte@Sun.COM 		cfg_unlock(cfg);
58*11576SSurya.Prakki@Sun.COM 		(void) fstat(1, &sb);
597836SJohn.Forte@Sun.COM 
607836SJohn.Forte@Sun.COM 		if (cfg_lock(cfg, CFG_RDLOCK) < 0)
617836SJohn.Forte@Sun.COM 			(void) printf("CFG_RDLOCK error\n");
627836SJohn.Forte@Sun.COM 		else
63*11576SSurya.Prakki@Sun.COM 			(void) fstat(0, &sb);
647836SJohn.Forte@Sun.COM 
657836SJohn.Forte@Sun.COM 		cfg_unlock(cfg);
66*11576SSurya.Prakki@Sun.COM 		(void) fstat(1, &sb);
677836SJohn.Forte@Sun.COM 
687836SJohn.Forte@Sun.COM 		if (cfg_lock(cfg, CFG_WRLOCK) < 0)
697836SJohn.Forte@Sun.COM 			(void) printf("CFG_WRLOCK error\n");
707836SJohn.Forte@Sun.COM 		else
71*11576SSurya.Prakki@Sun.COM 			(void) fstat(0, &sb);
727836SJohn.Forte@Sun.COM 
737836SJohn.Forte@Sun.COM 		cfg_unlock(cfg);
74*11576SSurya.Prakki@Sun.COM 		(void) fstat(1, &sb);
757836SJohn.Forte@Sun.COM 
767836SJohn.Forte@Sun.COM 		if (i > 0) {
777836SJohn.Forte@Sun.COM 			if (i % 100 == 0)
787836SJohn.Forte@Sun.COM 				(void) write(1, "+", 1);
797836SJohn.Forte@Sun.COM 			if (i % 5000 == 0)
807836SJohn.Forte@Sun.COM 				(void) write(1, "\n", 1);
817836SJohn.Forte@Sun.COM 		}
827836SJohn.Forte@Sun.COM 	}
837836SJohn.Forte@Sun.COM 	(void) printf("\nTest complete\n");
847836SJohn.Forte@Sun.COM }
857836SJohn.Forte@Sun.COM 
867836SJohn.Forte@Sun.COM static void
cmd_loop()877836SJohn.Forte@Sun.COM cmd_loop()
887836SJohn.Forte@Sun.COM {
897836SJohn.Forte@Sun.COM 	char	host[1024];
907836SJohn.Forte@Sun.COM 	char	buffer[1024];
917836SJohn.Forte@Sun.COM 	int	i;
927836SJohn.Forte@Sun.COM 
937836SJohn.Forte@Sun.COM 	(void) gethostname(host, sizeof (host));
947836SJohn.Forte@Sun.COM 	for (;;) {
957836SJohn.Forte@Sun.COM 		if (tty)
967836SJohn.Forte@Sun.COM 			(void) printf(":%s: ", host);
977836SJohn.Forte@Sun.COM 		(void) fgets(buffer, sizeof (buffer), stdin);
987836SJohn.Forte@Sun.COM 		switch (tolower(buffer[0])) {
997836SJohn.Forte@Sun.COM 			case 'p':
1007836SJohn.Forte@Sun.COM 				i = atoi(buffer + 1);
1017836SJohn.Forte@Sun.COM 				(void) sleep(i);
1027836SJohn.Forte@Sun.COM 				break;
1037836SJohn.Forte@Sun.COM 			case 'q':
1047836SJohn.Forte@Sun.COM 				exit(0);
1057836SJohn.Forte@Sun.COM 				break;
1067836SJohn.Forte@Sun.COM 			case 'r':
1077836SJohn.Forte@Sun.COM 				if (cfg_lock(cfg, CFG_RDLOCK) < 0)
1087836SJohn.Forte@Sun.COM 					(void) printf("CFG_RDLOCK error\n");
1097836SJohn.Forte@Sun.COM 				break;
1107836SJohn.Forte@Sun.COM 			case 's':
1117836SJohn.Forte@Sun.COM 				cfg_lockd_stat();
1127836SJohn.Forte@Sun.COM 				break;
1137836SJohn.Forte@Sun.COM 			case 't':
1147836SJohn.Forte@Sun.COM 				i = atoi(buffer + 1);
1157836SJohn.Forte@Sun.COM 				test(i);
1167836SJohn.Forte@Sun.COM 				break;
1177836SJohn.Forte@Sun.COM 			case 'u':
1187836SJohn.Forte@Sun.COM 				cfg_unlock(cfg);
1197836SJohn.Forte@Sun.COM 				break;
1207836SJohn.Forte@Sun.COM 			case 'w':
1217836SJohn.Forte@Sun.COM 				if (cfg_lock(cfg, CFG_WRLOCK) < 0)
1227836SJohn.Forte@Sun.COM 					(void) printf("CFG_WRLOCK error\n");
1237836SJohn.Forte@Sun.COM 				break;
1247836SJohn.Forte@Sun.COM 			default:
1257836SJohn.Forte@Sun.COM 				(void) printf("don't understand %s\n", buffer);
1267836SJohn.Forte@Sun.COM 				break;
1277836SJohn.Forte@Sun.COM 		}
1287836SJohn.Forte@Sun.COM 	}
1297836SJohn.Forte@Sun.COM }
1307836SJohn.Forte@Sun.COM 
1317836SJohn.Forte@Sun.COM static void
init()1327836SJohn.Forte@Sun.COM init()
1337836SJohn.Forte@Sun.COM {
1347836SJohn.Forte@Sun.COM 	tty = isatty(0);
1357836SJohn.Forte@Sun.COM 	if (tty)
1367836SJohn.Forte@Sun.COM 		(void) printf("dscfglockd cli %s\n", "07/06/12");
1377836SJohn.Forte@Sun.COM 	if ((cfg = cfg_open(NULL)) == NULL) {
1387836SJohn.Forte@Sun.COM 		perror("cfg_open");
1397836SJohn.Forte@Sun.COM 		exit(1);
1407836SJohn.Forte@Sun.COM 	}
1417836SJohn.Forte@Sun.COM }
1427836SJohn.Forte@Sun.COM 
1437836SJohn.Forte@Sun.COM int
main(void)1447836SJohn.Forte@Sun.COM main(void)
1457836SJohn.Forte@Sun.COM {
1467836SJohn.Forte@Sun.COM 	init();
1477836SJohn.Forte@Sun.COM 	cmd_loop();
1487836SJohn.Forte@Sun.COM 	return (0);
1497836SJohn.Forte@Sun.COM }
150