1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1992-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * mediator status utility.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <meta.h>
34*0Sstevel@tonic-gate #include <sdssc.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate static void
37*0Sstevel@tonic-gate usage(
38*0Sstevel@tonic-gate 	mdsetname_t	*sp,
39*0Sstevel@tonic-gate 	char		*string)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	if ((string != NULL) && (*string != '\0'))
42*0Sstevel@tonic-gate 		md_eprintf("%s\n", string);
43*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
44*0Sstevel@tonic-gate 	    "usage:	%s [-q] -s setname\n"),
45*0Sstevel@tonic-gate 	myname);
46*0Sstevel@tonic-gate 	md_exit(sp, (string == NULL) ? 0 : 1);
47*0Sstevel@tonic-gate }
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * parse args and do it
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate int
53*0Sstevel@tonic-gate main(
54*0Sstevel@tonic-gate 	int			argc,
55*0Sstevel@tonic-gate 	char			*argv[]
56*0Sstevel@tonic-gate )
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	int			c;
59*0Sstevel@tonic-gate 	char			*sname = MD_LOCAL_NAME;
60*0Sstevel@tonic-gate 	md_error_t		status = mdnullerror;
61*0Sstevel@tonic-gate 	md_error_t		*ep = &status;
62*0Sstevel@tonic-gate 	mdsetname_t		*sp = NULL;
63*0Sstevel@tonic-gate 	md_set_desc		*sd;
64*0Sstevel@tonic-gate 	int			i;
65*0Sstevel@tonic-gate 	int			max_meds;
66*0Sstevel@tonic-gate 	md_h_t			mdh;
67*0Sstevel@tonic-gate 	med_data_t		medd;
68*0Sstevel@tonic-gate 	int			medok = 0;
69*0Sstevel@tonic-gate 	int			golden = 0;
70*0Sstevel@tonic-gate 	int			verbose = 1;
71*0Sstevel@tonic-gate 	int			error;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	/*
74*0Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
75*0Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
76*0Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
77*0Sstevel@tonic-gate 	 * something.
78*0Sstevel@tonic-gate 	 */
79*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
80*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
81*0Sstevel@tonic-gate #endif
82*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
83*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	if ((sdssc_bind_library() == SDSSC_OKAY) &&
86*0Sstevel@tonic-gate 		(sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
87*0Sstevel@tonic-gate 		    &error) == SDSSC_PROXY_DONE))
88*0Sstevel@tonic-gate 			exit(error);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	/* initialize */
91*0Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0) {
92*0Sstevel@tonic-gate 		mde_perror(ep, "");
93*0Sstevel@tonic-gate 		md_exit(sp, 1);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	optind = 1;
97*0Sstevel@tonic-gate 	opterr = 1;
98*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "qs:?")) != -1) {
99*0Sstevel@tonic-gate 		switch (c) {
100*0Sstevel@tonic-gate 		    case 'q':
101*0Sstevel@tonic-gate 			    verbose = 0;
102*0Sstevel@tonic-gate 			    break;
103*0Sstevel@tonic-gate 		    case 's':
104*0Sstevel@tonic-gate 			    sname = optarg;
105*0Sstevel@tonic-gate 			    break;
106*0Sstevel@tonic-gate 		    case '?':
107*0Sstevel@tonic-gate 			    if (optopt == '?')
108*0Sstevel@tonic-gate 				    usage(sp, NULL);
109*0Sstevel@tonic-gate 			    /*FALLTHROUGH*/
110*0Sstevel@tonic-gate 		    default:
111*0Sstevel@tonic-gate 			usage(sp, gettext("unknown command"));
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/* must have set for everything else */
116*0Sstevel@tonic-gate 	if (strcmp(sname, MD_LOCAL_NAME) == 0)
117*0Sstevel@tonic-gate 		usage(sp, gettext("setname must be specified"));
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/* snarf MDDB */
120*0Sstevel@tonic-gate 	if (meta_setup_db_locations(ep) != 0) {
121*0Sstevel@tonic-gate 		mde_perror(ep, "");
122*0Sstevel@tonic-gate 		md_exit(sp, 1);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
126*0Sstevel@tonic-gate 		mde_perror(ep, "");
127*0Sstevel@tonic-gate 		md_exit(sp, 1);
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if ((sd = metaget_setdesc(sp, ep)) == NULL) {
131*0Sstevel@tonic-gate 		mde_perror(ep, "");
132*0Sstevel@tonic-gate 		md_exit(sp, 1);
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if (sd->sd_med.n_cnt == 0) {
136*0Sstevel@tonic-gate 		if (verbose)
137*0Sstevel@tonic-gate 			(void) printf(gettext(
138*0Sstevel@tonic-gate 			    "No mediator hosts configured for set \"%s\".\n"),
139*0Sstevel@tonic-gate 			    sname);
140*0Sstevel@tonic-gate 		md_exit(sp, 2);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if ((max_meds = get_max_meds(ep)) == 0)
144*0Sstevel@tonic-gate 		return (-1);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if (verbose)
147*0Sstevel@tonic-gate 		(void) printf("%8.8s\t\t%6.6s\t%6.6s\n",
148*0Sstevel@tonic-gate 		    gettext("Mediator"), gettext("Status"),
149*0Sstevel@tonic-gate 		    gettext("Golden"));
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	for (i = 0; i < max_meds; i++) {
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		if (sd->sd_med.n_lst[i].a_cnt == 0)
154*0Sstevel@tonic-gate 			continue;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 		(void) memset(&medd, '\0', sizeof (medd));
157*0Sstevel@tonic-gate 		(void) memset(&mdh, '\0', sizeof (mdh));
158*0Sstevel@tonic-gate 		mdh = sd->sd_med.n_lst[i];	/* structure assignment */
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 		if (verbose)
161*0Sstevel@tonic-gate 			(void) printf("%-17.17s\t",
162*0Sstevel@tonic-gate 			    sd->sd_med.n_lst[i].a_nm[0]);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		if (clnt_med_get_data(&mdh, sp, &medd, ep) == -1) {
165*0Sstevel@tonic-gate 			if (mdanyrpcerror(ep)) {
166*0Sstevel@tonic-gate 				if (verbose)
167*0Sstevel@tonic-gate 					(void) printf("%s\n",
168*0Sstevel@tonic-gate 					    gettext("Unreachable"));
169*0Sstevel@tonic-gate 				continue;
170*0Sstevel@tonic-gate 			} else if (mdiserror(ep, MDE_MED_ERROR)) {
171*0Sstevel@tonic-gate 				if (verbose)
172*0Sstevel@tonic-gate 					(void) printf("%s\n",
173*0Sstevel@tonic-gate 					    gettext("Bad"));
174*0Sstevel@tonic-gate 			} else {
175*0Sstevel@tonic-gate 				if (verbose)
176*0Sstevel@tonic-gate 					(void) printf("%s\n",
177*0Sstevel@tonic-gate 					    gettext("Fatal"));
178*0Sstevel@tonic-gate 			}
179*0Sstevel@tonic-gate 			mde_perror(ep, "");
180*0Sstevel@tonic-gate 			if (mdiserror(ep, MDE_MED_ERROR))
181*0Sstevel@tonic-gate 				continue;
182*0Sstevel@tonic-gate 			md_exit(sp, 1);
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 		if (verbose)
186*0Sstevel@tonic-gate 			(void) printf("%s", gettext("Ok"));
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		if (medd.med_dat_fl & MED_DFL_GOLDEN) {
189*0Sstevel@tonic-gate 			if (verbose)
190*0Sstevel@tonic-gate 				(void) printf("\t%s",
191*0Sstevel@tonic-gate 				    gettext("Yes"));
192*0Sstevel@tonic-gate 			golden++;
193*0Sstevel@tonic-gate 		} else {
194*0Sstevel@tonic-gate 			if (verbose)
195*0Sstevel@tonic-gate 				(void) printf("\t%s",
196*0Sstevel@tonic-gate 				    gettext("No"));
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		if (verbose)
200*0Sstevel@tonic-gate 			(void) printf("\n");
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 		medok++;
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if (golden)
206*0Sstevel@tonic-gate 		md_exit(sp, 0);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	if (medok < ((sd->sd_med.n_cnt / 2) + 1))
209*0Sstevel@tonic-gate 		md_exit(sp, 1);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	md_exit(sp, 0);
212*0Sstevel@tonic-gate 	/*NOTREACHED*/
213*0Sstevel@tonic-gate 	return (0);
214*0Sstevel@tonic-gate }
215