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