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