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 2004 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  * Just in case we're not in a build environment, make sure that
31*0Sstevel@tonic-gate  * TEXT_DOMAIN gets set to something.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
34*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
35*0Sstevel@tonic-gate #endif
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * Return the values of runtime parameters stored in
39*0Sstevel@tonic-gate  * /etc/lvm/runtime.cf, converting them to data
40*0Sstevel@tonic-gate  * types appropriate for use by functions whose behavior
41*0Sstevel@tonic-gate  * is affected by those values.
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * system include files
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <libintl.h>
49*0Sstevel@tonic-gate #include <stdio.h>
50*0Sstevel@tonic-gate #include <stdlib.h>
51*0Sstevel@tonic-gate #include <string.h>
52*0Sstevel@tonic-gate #include <syslog.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * SUNWmd include files
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #include <meta.h>		/* for MDD_DOMAIN */
59*0Sstevel@tonic-gate #include <meta_runtime.h>	/* external interface definition */
60*0Sstevel@tonic-gate #include <sdssc.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * The following lines define the runtime parameter configuration file.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static const char *param_file_namep = "/etc/lvm/runtime.cf";
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * The runtime parameter configuration file is an ascii text file.
70*0Sstevel@tonic-gate  * Each text line in the file has a maximum length of 80 four-byte
71*0Sstevel@tonic-gate  * wide characters.  The line buffer size defined below accomodates
72*0Sstevel@tonic-gate  * the maximum line length plus the newline character at the end of
73*0Sstevel@tonic-gate  * the line and the null character that fgets() adds at the end of
74*0Sstevel@tonic-gate  * the line when it writes the line to the buffer.
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static const int line_buffer_size = 325;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * The format for parameter entries in the file is "name=value".
81*0Sstevel@tonic-gate  * Each "name=value" string must begin a line of the file.
82*0Sstevel@tonic-gate  * The "name" and "value" tokens may be preceded or followed by
83*0Sstevel@tonic-gate  * spaces.  Lines beginning with "#" are comment lines.
84*0Sstevel@tonic-gate  */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static const char *token_separator_listp = " =";
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * If a runtime parameter that can be set in the file is not set,
90*0Sstevel@tonic-gate  * or is set to an invalid value, or if the file can't be opened,
91*0Sstevel@tonic-gate  * the parameter takes on the default value given in the comments
92*0Sstevel@tonic-gate  * below.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * The following string constant declarations name the runtime
97*0Sstevel@tonic-gate  * configuration parameters that can be set in the runtime parameter
98*0Sstevel@tonic-gate  * configuration file.  The allowed values of parameters that
99*0Sstevel@tonic-gate  * range over small sets of discrete values are also declared below
100*0Sstevel@tonic-gate  * as string constants.
101*0Sstevel@tonic-gate  *
102*0Sstevel@tonic-gate  * CAUTION: When adding new runtime parameters to the runtime
103*0Sstevel@tonic-gate  *          parameter configuration file, declare their names
104*0Sstevel@tonic-gate  *          as string constants below, and check for conflicts
105*0Sstevel@tonic-gate  *          with the names of existing parameters.
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate static const char *ownerioctls_namep = "ownerioctls";
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate  * allowed values:
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static const char *ownerioctls_onp = "on"; /* default value */
115*0Sstevel@tonic-gate static const char *ownerioctls_offp = "off";
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /*
118*0Sstevel@tonic-gate  * The "ownerioctls" parameter controls whether the metaset -t and
119*0Sstevel@tonic-gate  * metaset -r commands issue the MHIOCTKOWN, MHIOCRELEASE, and
120*0Sstevel@tonic-gate  * MHIOCENFAILFAST ioctls when taking or releasing ownership of disksets.
121*0Sstevel@tonic-gate  * The allowed parameter values are "on" and "off".
122*0Sstevel@tonic-gate  *
123*0Sstevel@tonic-gate  * If the line "ownerioctls=off" appears in the runtime configuration file,
124*0Sstevel@tonic-gate  * the metaset -t command doesn't issue the MHIOCTKOWN ioctl when taking
125*0Sstevel@tonic-gate  * ownership of disksets, and the metaset -r command doesn't issue the
126*0Sstevel@tonic-gate  * MHIOCRELEASE and MHIOCENFAILFAST ioctls when releasing ownership of
127*0Sstevel@tonic-gate  * disksets.
128*0Sstevel@tonic-gate  *
129*0Sstevel@tonic-gate  * If the line "ownerioctls=on" appears in the file, the metaset -t
130*0Sstevel@tonic-gate  * command issues the MHIOCTKOWN ioctl when taking ownership of disksets,
131*0Sstevel@tonic-gate  * and the metaset -r command issues the MHIOCRELEASE AND MHIOCENFAILFAST
132*0Sstevel@tonic-gate  * icotls when releasing ownership of disksets.
133*0Sstevel@tonic-gate  *
134*0Sstevel@tonic-gate  * The default value of "ownerioctls" is "on".
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate /*
138*0Sstevel@tonic-gate  * The following lines make forward declarations of private functions.
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static
142*0Sstevel@tonic-gate char *
143*0Sstevel@tonic-gate meta_get_rt_param(const char *param_namep, boolean_t warn_if_not_found);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate /*
146*0Sstevel@tonic-gate  * The following lines define public functions.
147*0Sstevel@tonic-gate  */
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate boolean_t
150*0Sstevel@tonic-gate do_owner_ioctls(void)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate 	const char	*function_namep = "do_owner_ioctls()";
153*0Sstevel@tonic-gate 	char		*param_valuep;
154*0Sstevel@tonic-gate 	boolean_t	return_value = B_TRUE; /* default behavior */
155*0Sstevel@tonic-gate 	sdssc_version_t	version;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if ((sdssc_version(&version) == SDSSC_OKAY) && (version.major >= 3)) {
158*0Sstevel@tonic-gate 		/*
159*0Sstevel@tonic-gate 		 * If we're bound to a cluster machine never do ioctls.
160*0Sstevel@tonic-gate 		 * The SC3.0 cluster code will always deal with disk
161*0Sstevel@tonic-gate 		 * reservation.
162*0Sstevel@tonic-gate 		 */
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		return_value = B_FALSE;
165*0Sstevel@tonic-gate 	} else {
166*0Sstevel@tonic-gate 		param_valuep = meta_get_rt_param(ownerioctls_namep, B_TRUE);
167*0Sstevel@tonic-gate 		if (param_valuep != NULL) {
168*0Sstevel@tonic-gate 			if (strcmp(param_valuep, ownerioctls_offp) == 0) {
169*0Sstevel@tonic-gate 				return_value = B_FALSE;
170*0Sstevel@tonic-gate 			} else if (strcmp(param_valuep,
171*0Sstevel@tonic-gate 			    ownerioctls_onp) != 0) {
172*0Sstevel@tonic-gate 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
173*0Sstevel@tonic-gate 				    "%s: illegal value for %s: %s.\n"),
174*0Sstevel@tonic-gate 					function_namep,
175*0Sstevel@tonic-gate 					ownerioctls_namep,
176*0Sstevel@tonic-gate 					param_valuep);
177*0Sstevel@tonic-gate 				syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
178*0Sstevel@tonic-gate 				    "%s: illegal value for %s: %s.\n"),
179*0Sstevel@tonic-gate 				    function_namep,
180*0Sstevel@tonic-gate 				    ownerioctls_namep,
181*0Sstevel@tonic-gate 				    param_valuep);
182*0Sstevel@tonic-gate 			}
183*0Sstevel@tonic-gate 			free(param_valuep);
184*0Sstevel@tonic-gate 		}
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 	return (return_value);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate  * Retrieve the verbosity level for rpc.mdcommd from the config file.
191*0Sstevel@tonic-gate  * If none is specified, don't print a warning and return 0
192*0Sstevel@tonic-gate  */
193*0Sstevel@tonic-gate uint_t
194*0Sstevel@tonic-gate commd_get_verbosity(void)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	char		*param_valuep;
197*0Sstevel@tonic-gate 	uint_t retval	= 0;
198*0Sstevel@tonic-gate 	param_valuep = meta_get_rt_param("commd_verbosity", B_FALSE);
199*0Sstevel@tonic-gate 	if (param_valuep != NULL) {
200*0Sstevel@tonic-gate 		retval = (uint_t)strtol(param_valuep, NULL, 16);
201*0Sstevel@tonic-gate 		free(param_valuep);
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 	return (retval);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * Retrieve the debug output file for rpc.mdcommd from the config file.
208*0Sstevel@tonic-gate  * If none is specified, don't print a warning.
209*0Sstevel@tonic-gate  * Note that if returning non-NULL, the caller is responsible for freeing
210*0Sstevel@tonic-gate  * the result pointer.
211*0Sstevel@tonic-gate  */
212*0Sstevel@tonic-gate char *
213*0Sstevel@tonic-gate commd_get_outfile(void)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	return (meta_get_rt_param("commd_out_file", B_FALSE));
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate /*
219*0Sstevel@tonic-gate  * The following lines define private functions
220*0Sstevel@tonic-gate  */
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate static char *
223*0Sstevel@tonic-gate meta_get_rt_param(const char *param_namep, boolean_t warn_if_not_found)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	const char *function_namep = "meta_get_rt_param()";
226*0Sstevel@tonic-gate 	char *line_bufferp = NULL;
227*0Sstevel@tonic-gate 	char *newlinep = NULL;
228*0Sstevel@tonic-gate 	FILE *param_filep = NULL;
229*0Sstevel@tonic-gate 	char *param_name_tokenp = NULL;
230*0Sstevel@tonic-gate 	char *param_valuep = NULL;
231*0Sstevel@tonic-gate 	char *param_value_tokenp = NULL;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	line_bufferp = (char *)malloc(line_buffer_size);
234*0Sstevel@tonic-gate 	if (line_bufferp == NULL) {
235*0Sstevel@tonic-gate 		(void) fprintf(stderr,
236*0Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN, "%s: malloc failed\n"),
237*0Sstevel@tonic-gate 			function_namep);
238*0Sstevel@tonic-gate 		syslog(LOG_ERR,
239*0Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN, "%s: malloc failed\n"),
240*0Sstevel@tonic-gate 			function_namep);
241*0Sstevel@tonic-gate 		return (param_valuep);
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 	param_filep = fopen(param_file_namep, "r");
244*0Sstevel@tonic-gate 	if (param_filep == NULL) {
245*0Sstevel@tonic-gate 		(void) fprintf(stderr,
246*0Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN, "%s: can't open %s\n"),
247*0Sstevel@tonic-gate 			function_namep, param_file_namep);
248*0Sstevel@tonic-gate 		syslog(LOG_ERR,
249*0Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN, "%s: can't open %s\n"),
250*0Sstevel@tonic-gate 			function_namep, param_file_namep);
251*0Sstevel@tonic-gate 		free(line_bufferp);
252*0Sstevel@tonic-gate 		return (param_valuep);
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 	while ((fgets(line_bufferp, line_buffer_size, param_filep) != NULL) &&
255*0Sstevel@tonic-gate 		(param_valuep == NULL)) {
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 		newlinep = strchr(line_bufferp, '\n');
258*0Sstevel@tonic-gate 		if (newlinep != NULL) {
259*0Sstevel@tonic-gate 			*newlinep = '\0';
260*0Sstevel@tonic-gate 			newlinep = NULL;
261*0Sstevel@tonic-gate 		}
262*0Sstevel@tonic-gate 		param_name_tokenp = strtok(line_bufferp, token_separator_listp);
263*0Sstevel@tonic-gate 		if ((param_name_tokenp != NULL) &&
264*0Sstevel@tonic-gate 			(strcmp(param_namep, param_name_tokenp) == 0)) {
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 			param_value_tokenp = strtok(NULL,
267*0Sstevel@tonic-gate 						token_separator_listp);
268*0Sstevel@tonic-gate 		}
269*0Sstevel@tonic-gate 		if (param_value_tokenp != NULL) {
270*0Sstevel@tonic-gate 			param_valuep = strdup(param_value_tokenp);
271*0Sstevel@tonic-gate 			if (param_valuep == NULL) {
272*0Sstevel@tonic-gate 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
273*0Sstevel@tonic-gate 				    "%s: strdup failed\n"),
274*0Sstevel@tonic-gate 				    function_namep);
275*0Sstevel@tonic-gate 				syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
276*0Sstevel@tonic-gate 				    "%s: strdup failed\n"),
277*0Sstevel@tonic-gate 				    function_namep);
278*0Sstevel@tonic-gate 				free(line_bufferp);
279*0Sstevel@tonic-gate 				(void) fclose(param_filep);
280*0Sstevel@tonic-gate 				return (param_valuep);
281*0Sstevel@tonic-gate 			}
282*0Sstevel@tonic-gate 		}
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 	if ((param_valuep == NULL) && (warn_if_not_found == B_TRUE)) {
285*0Sstevel@tonic-gate 		(void) fprintf(stderr,
286*0Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN,
287*0Sstevel@tonic-gate 			    "%s: value of %s not set or error in %s\n"),
288*0Sstevel@tonic-gate 			function_namep,
289*0Sstevel@tonic-gate 			param_namep,
290*0Sstevel@tonic-gate 			param_file_namep);
291*0Sstevel@tonic-gate 		syslog(LOG_ERR,
292*0Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN,
293*0Sstevel@tonic-gate 			    "%s: value of %s not set or error in %s\n"),
294*0Sstevel@tonic-gate 			function_namep,
295*0Sstevel@tonic-gate 			param_namep,
296*0Sstevel@tonic-gate 			param_file_namep);
297*0Sstevel@tonic-gate 	}
298*0Sstevel@tonic-gate 	free(line_bufferp);
299*0Sstevel@tonic-gate 	(void) fclose(param_filep);
300*0Sstevel@tonic-gate 	return (param_valuep);
301*0Sstevel@tonic-gate }
302