xref: /onnv-gate/usr/src/lib/libpkg/common/handlelocalfs.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  *
3*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
4*9781SMoriah.Waterland@Sun.COM  *
5*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
6*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
7*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
8*9781SMoriah.Waterland@Sun.COM  *
9*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
11*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
12*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
13*9781SMoriah.Waterland@Sun.COM  *
14*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
15*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
17*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
18*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
19*9781SMoriah.Waterland@Sun.COM  *
20*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
21*9781SMoriah.Waterland@Sun.COM  *
22*9781SMoriah.Waterland@Sun.COM  */
23*9781SMoriah.Waterland@Sun.COM 
24*9781SMoriah.Waterland@Sun.COM /*
25*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
27*9781SMoriah.Waterland@Sun.COM  *
28*9781SMoriah.Waterland@Sun.COM */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
32*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
33*9781SMoriah.Waterland@Sun.COM #include <strings.h>
34*9781SMoriah.Waterland@Sun.COM #include <libscf.h>
35*9781SMoriah.Waterland@Sun.COM 
36*9781SMoriah.Waterland@Sun.COM #define	MAX_TRY	15
37*9781SMoriah.Waterland@Sun.COM static boolean_t fs_temporarily_enabled = B_FALSE;
38*9781SMoriah.Waterland@Sun.COM char svm_core_svcs[] = "system/filesystem/local:default";
39*9781SMoriah.Waterland@Sun.COM 
40*9781SMoriah.Waterland@Sun.COM /*
41*9781SMoriah.Waterland@Sun.COM  * Name:	enable_local_fs
42*9781SMoriah.Waterland@Sun.COM  * Description: If the SMF service system/filesystem/local:default is not
43*9781SMoriah.Waterland@Sun.COM  *		enabled, then this function enables the service, so that,
44*9781SMoriah.Waterland@Sun.COM  *		all the local filesystems are mounted.
45*9781SMoriah.Waterland@Sun.COM  * Arguments:	None
46*9781SMoriah.Waterland@Sun.COM  * Returns:	B_TRUE on success; B_FALSE on error.
47*9781SMoriah.Waterland@Sun.COM  */
48*9781SMoriah.Waterland@Sun.COM boolean_t
enable_local_fs(void)49*9781SMoriah.Waterland@Sun.COM enable_local_fs(void)
50*9781SMoriah.Waterland@Sun.COM {
51*9781SMoriah.Waterland@Sun.COM 	char *cur_smf_state;
52*9781SMoriah.Waterland@Sun.COM 	int i;
53*9781SMoriah.Waterland@Sun.COM 	boolean_t fs_enabled_here = B_FALSE;
54*9781SMoriah.Waterland@Sun.COM 
55*9781SMoriah.Waterland@Sun.COM 	if (fs_temporarily_enabled)  {
56*9781SMoriah.Waterland@Sun.COM 		return (B_TRUE);
57*9781SMoriah.Waterland@Sun.COM 	}
58*9781SMoriah.Waterland@Sun.COM 
59*9781SMoriah.Waterland@Sun.COM 	if ((cur_smf_state = smf_get_state(svm_core_svcs)) != NULL) {
60*9781SMoriah.Waterland@Sun.COM 		if (strcmp(cur_smf_state, SCF_STATE_STRING_DISABLED) == 0) {
61*9781SMoriah.Waterland@Sun.COM 			if (smf_enable_instance(svm_core_svcs, SMF_TEMPORARY)
62*9781SMoriah.Waterland@Sun.COM 				!= 0) {
63*9781SMoriah.Waterland@Sun.COM 				free(cur_smf_state);
64*9781SMoriah.Waterland@Sun.COM 				return (B_FALSE);
65*9781SMoriah.Waterland@Sun.COM 			}
66*9781SMoriah.Waterland@Sun.COM 
67*9781SMoriah.Waterland@Sun.COM 			fs_enabled_here = B_TRUE;
68*9781SMoriah.Waterland@Sun.COM 
69*9781SMoriah.Waterland@Sun.COM 		} else if (strcmp(cur_smf_state, SCF_STATE_STRING_ONLINE)
70*9781SMoriah.Waterland@Sun.COM 				== 0) {
71*9781SMoriah.Waterland@Sun.COM 			free(cur_smf_state);
72*9781SMoriah.Waterland@Sun.COM 			return (B_TRUE);
73*9781SMoriah.Waterland@Sun.COM 		} else if (strcmp(cur_smf_state, SCF_STATE_STRING_OFFLINE)
74*9781SMoriah.Waterland@Sun.COM 				!= 0) {
75*9781SMoriah.Waterland@Sun.COM 			free(cur_smf_state);
76*9781SMoriah.Waterland@Sun.COM 			return (B_FALSE);
77*9781SMoriah.Waterland@Sun.COM 		}
78*9781SMoriah.Waterland@Sun.COM 
79*9781SMoriah.Waterland@Sun.COM 		free(cur_smf_state);
80*9781SMoriah.Waterland@Sun.COM 
81*9781SMoriah.Waterland@Sun.COM 	} else {
82*9781SMoriah.Waterland@Sun.COM 		return (B_FALSE);
83*9781SMoriah.Waterland@Sun.COM 	}
84*9781SMoriah.Waterland@Sun.COM 
85*9781SMoriah.Waterland@Sun.COM 	for (i = 0; i < MAX_TRY; i++) {
86*9781SMoriah.Waterland@Sun.COM 		if ((cur_smf_state = smf_get_state(svm_core_svcs)) != NULL) {
87*9781SMoriah.Waterland@Sun.COM 			if (strcmp(cur_smf_state, SCF_STATE_STRING_ONLINE)
88*9781SMoriah.Waterland@Sun.COM 				== 0) {
89*9781SMoriah.Waterland@Sun.COM 				free(cur_smf_state);
90*9781SMoriah.Waterland@Sun.COM 				if (fs_enabled_here) {
91*9781SMoriah.Waterland@Sun.COM 					fs_temporarily_enabled = B_TRUE;
92*9781SMoriah.Waterland@Sun.COM 				}
93*9781SMoriah.Waterland@Sun.COM 				return (B_TRUE);
94*9781SMoriah.Waterland@Sun.COM 			} else if ((strcmp(cur_smf_state,
95*9781SMoriah.Waterland@Sun.COM 				SCF_STATE_STRING_OFFLINE) == 0) ||
96*9781SMoriah.Waterland@Sun.COM 		(strcmp(cur_smf_state, SCF_STATE_STRING_DISABLED) == 0)) {
97*9781SMoriah.Waterland@Sun.COM 				(void) sleep(1);
98*9781SMoriah.Waterland@Sun.COM 				free(cur_smf_state);
99*9781SMoriah.Waterland@Sun.COM 			} else {
100*9781SMoriah.Waterland@Sun.COM 				free(cur_smf_state);
101*9781SMoriah.Waterland@Sun.COM 				return (B_FALSE);
102*9781SMoriah.Waterland@Sun.COM 			}
103*9781SMoriah.Waterland@Sun.COM 		} else {
104*9781SMoriah.Waterland@Sun.COM 			return (B_FALSE);
105*9781SMoriah.Waterland@Sun.COM 		}
106*9781SMoriah.Waterland@Sun.COM 	}
107*9781SMoriah.Waterland@Sun.COM 
108*9781SMoriah.Waterland@Sun.COM 	return (B_FALSE);
109*9781SMoriah.Waterland@Sun.COM }
110*9781SMoriah.Waterland@Sun.COM 
111*9781SMoriah.Waterland@Sun.COM /*
112*9781SMoriah.Waterland@Sun.COM  * Name:	restore_local_fs
113*9781SMoriah.Waterland@Sun.COM  * Description: If the SMF service system/filesystem/local:default was
114*9781SMoriah.Waterland@Sun.COM  *		enabled using enable_local_fs(), then this function disables
115*9781SMoriah.Waterland@Sun.COM  *		the service.
116*9781SMoriah.Waterland@Sun.COM  * Arguments:	None
117*9781SMoriah.Waterland@Sun.COM  * Returns:	B_TRUE on success; B_FALSE on error.
118*9781SMoriah.Waterland@Sun.COM  */
119*9781SMoriah.Waterland@Sun.COM boolean_t
restore_local_fs(void)120*9781SMoriah.Waterland@Sun.COM restore_local_fs(void)
121*9781SMoriah.Waterland@Sun.COM {
122*9781SMoriah.Waterland@Sun.COM 	int i;
123*9781SMoriah.Waterland@Sun.COM 	char *cur_smf_state;
124*9781SMoriah.Waterland@Sun.COM 
125*9781SMoriah.Waterland@Sun.COM 	if (!fs_temporarily_enabled) {
126*9781SMoriah.Waterland@Sun.COM 		return (B_TRUE);
127*9781SMoriah.Waterland@Sun.COM 	}
128*9781SMoriah.Waterland@Sun.COM 
129*9781SMoriah.Waterland@Sun.COM 	if (smf_disable_instance(svm_core_svcs, SMF_TEMPORARY) != 0) {
130*9781SMoriah.Waterland@Sun.COM 		return (B_FALSE);
131*9781SMoriah.Waterland@Sun.COM 	}
132*9781SMoriah.Waterland@Sun.COM 
133*9781SMoriah.Waterland@Sun.COM 	for (i = 0; i < MAX_TRY; i++) {
134*9781SMoriah.Waterland@Sun.COM 		if ((cur_smf_state = smf_get_state(svm_core_svcs)) != NULL) {
135*9781SMoriah.Waterland@Sun.COM 			if (strcmp(cur_smf_state, SCF_STATE_STRING_DISABLED)
136*9781SMoriah.Waterland@Sun.COM 				== 0) {
137*9781SMoriah.Waterland@Sun.COM 				fs_temporarily_enabled = B_FALSE;
138*9781SMoriah.Waterland@Sun.COM 				free(cur_smf_state);
139*9781SMoriah.Waterland@Sun.COM 				break;
140*9781SMoriah.Waterland@Sun.COM 			}
141*9781SMoriah.Waterland@Sun.COM 			(void) sleep(1);
142*9781SMoriah.Waterland@Sun.COM 
143*9781SMoriah.Waterland@Sun.COM 			free(cur_smf_state);
144*9781SMoriah.Waterland@Sun.COM 		} else {
145*9781SMoriah.Waterland@Sun.COM 			return (B_FALSE);
146*9781SMoriah.Waterland@Sun.COM 		}
147*9781SMoriah.Waterland@Sun.COM 	}
148*9781SMoriah.Waterland@Sun.COM 
149*9781SMoriah.Waterland@Sun.COM 	return (!fs_temporarily_enabled);
150*9781SMoriah.Waterland@Sun.COM }
151