1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * 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  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "cmdgen_include.h"
30 
31 /*
32  * Public data type declaration
33  */
34 #define	UMNTALL_CMD "/usr/sbin/umountall"
35 #define	FSTYPE_FLAG "-F"
36 #define	HOST_FLAG "-h"
37 #define	KILL_FLAG "-k"
38 #define	LOCAL_FLAG "-l"
39 #define	NO_PARALLEL_FLAG "-s"
40 #define	REMOTE_FLAG "-r"
41 #define	SPACE " "
42 
43 
44 /*
45  * Public methods
46  */
47 /*
48  * Method: cmdgen_umountall
49  *
50  * Description: Forms the umountall command with the options given.
51  *
52  * Parameters:
53  *	- CCIMPropertyList *paramList - The parameter list containing the
54  *	options for the umountall command.
55  *	- int *errp - The error indicator.  Upon error, this will get set to a
56  *	value != 0.
57  *
58  * Returns:
59  *	- char * - The formed umounall command.
60  *	- NULL if an error occurred.
61  */
62 char *
cmdgen_umountall(CCIMPropertyList * paramList,int * errp)63 cmdgen_umountall(CCIMPropertyList *paramList, int *errp) {
64 	CCIMPropertyList	*currentParam;
65 	CCIMProperty		*fstypeProp = NULL;
66 	CCIMProperty		*hostProp = NULL;
67 	CCIMProperty		*onlyLocalProp = NULL;
68 	CCIMProperty		*onlyRemoteProp = NULL;
69 	CCIMProperty		*killProcessesProp = NULL;
70 	CCIMProperty		*inParallelProp = NULL;
71 	char			*cmd = NULL;
72 	int			cmdLen;
73 
74 	cmd = strdup(UMNTALL_CMD);
75 	if (cmd == NULL) {
76 		*errp = errno;
77 		return (NULL);
78 	}
79 
80 	/*
81 	 * In parameters in the paramList are as follows:
82 	 * 1. String fstype,
83 	 * 2. String host,
84 	 * 3. Boolean onlyLocalFileSystems,
85 	 * 4. Boolean onlyRemoteFileSystems,
86 	 * 5. Boolean killProcesses,
87 	 * 6. Boolean umountInParallel
88 	 *
89 	 * They are expected to always be in this order in the property list.
90 	 */
91 	/*
92 	 * Check if a file system type was passed in.  If one was we will
93 	 * use this in forming the command.
94 	 */
95 	currentParam = paramList;
96 	fstypeProp = currentParam->mDataObject;
97 	if (fstypeProp != NULL && fstypeProp->mValue != NULL &&
98 		strlen(fstypeProp->mValue) != 0) {
99 
100 		cim_logDebug("cmdgen_umountall", "Adding the -F flag");
101 		/*
102 		 * Add -F <fstype> to the command
103 		 */
104 		cmdLen = strlen(cmd) + strlen(SPACE) + strlen(FSTYPE_FLAG) +
105 			strlen(SPACE) + strlen(fstypeProp->mValue) + 1;
106 		cmd = realloc(cmd, (size_t)(cmdLen * sizeof (char)));
107 		if (cmd == NULL) {
108 			*errp = errno;
109 			return (NULL);
110 		}
111 		(void) snprintf(cmd, cmdLen, "%s%s%s%s%s", cmd, SPACE,
112 		    FSTYPE_FLAG, SPACE, fstypeProp->mValue);
113 	}
114 
115 	/*
116 	 * Check if a host was passed in.
117 	 */
118 	currentParam = currentParam->mNext;
119 	hostProp = currentParam->mDataObject;
120 	if (hostProp != NULL && hostProp->mValue != NULL &&
121 		strlen(hostProp->mValue) != 0) {
122 
123 		cim_logDebug("cmdgen_umountall", "Adding the -h flag");
124 		/*
125 		 * Add -h <host> to the command.
126 		 */
127 		cmdLen = strlen(cmd) + strlen(SPACE) + strlen(HOST_FLAG) +
128 			strlen(SPACE) + strlen(hostProp->mValue) + 1;
129 		cmd = realloc(cmd, (size_t)(cmdLen * sizeof (char)));
130 		if (cmd == NULL) {
131 			*errp = errno;
132 			return (NULL);
133 		}
134 		(void) snprintf(cmd, cmdLen, "%s%s%s%s%s", cmd, SPACE,
135 		    HOST_FLAG, SPACE, hostProp->mValue);
136 	}
137 
138 	currentParam = currentParam->mNext;
139 	onlyLocalProp = currentParam->mDataObject;
140 	if (onlyLocalProp != NULL && onlyLocalProp->mValue != NULL) {
141 		if (strcmp(onlyLocalProp->mValue, "1") == 0 ||
142 			strcasecmp(onlyLocalProp->mValue, "true") == 0) {
143 
144 			cim_logDebug("cmdgen_umountall",
145 				"Adding the -l flag");
146 			/*
147 			 * Add the -l flag to the command.
148 			 */
149 			cmdLen = strlen(cmd) + strlen(SPACE) +
150 				strlen(LOCAL_FLAG) + 1;
151 
152 			cmd = realloc(cmd, (size_t)(cmdLen * sizeof (char)));
153 			if (cmd == NULL) {
154 				*errp = errno;
155 				return (NULL);
156 			}
157 			(void) snprintf(cmd, cmdLen, "%s%s%s", cmd, SPACE,
158 				LOCAL_FLAG);
159 		}
160 	}
161 
162 	currentParam = currentParam->mNext;
163 	onlyRemoteProp = currentParam->mDataObject;
164 	if (onlyRemoteProp != NULL && onlyRemoteProp->mValue != NULL) {
165 		if (strcmp(onlyRemoteProp->mValue, "1") == 0 ||
166 			strcasecmp(onlyRemoteProp->mValue, "true") == 0) {
167 
168 			/*
169 			 * Add the -r flag to the command.
170 			 */
171 			cim_logDebug("cmdgen_umountall",
172 				"Adding the -r flag");
173 
174 			cmdLen = strlen(cmd) + strlen(SPACE) +
175 				strlen(REMOTE_FLAG) + 1;
176 			cmd = realloc(cmd, (size_t)(cmdLen * sizeof (char)));
177 			if (cmd == NULL) {
178 				*errp = errno;
179 				return (NULL);
180 			}
181 			(void) snprintf(cmd, cmdLen, "%s%s%s", cmd, SPACE,
182 				REMOTE_FLAG);
183 		}
184 	}
185 
186 	currentParam = currentParam->mNext;
187 	killProcessesProp = currentParam->mDataObject;
188 	if (killProcessesProp != NULL && killProcessesProp->mValue != NULL) {
189 		if (strcmp(killProcessesProp->mValue, "1") == 0 ||
190 			strcasecmp(killProcessesProp->mValue, "true") == 0) {
191 
192 			/*
193 			 * Add the -k flag to the command.
194 			 */
195 			cim_logDebug("cmdgen_umountall",
196 				"Adding the -k flag");
197 			cmdLen = strlen(cmd) + strlen(SPACE) +
198 				strlen(KILL_FLAG) + 1;
199 			cmd = realloc(cmd, (size_t)(cmdLen * sizeof (char)));
200 			if (cmd == NULL) {
201 				*errp = errno;
202 				return (NULL);
203 			}
204 			(void) snprintf(cmd, cmdLen, "%s%s%s", cmd, SPACE,
205 			    KILL_FLAG);
206 		}
207 	}
208 
209 	currentParam = currentParam->mNext;
210 	inParallelProp = currentParam->mDataObject;
211 	if (inParallelProp != NULL && inParallelProp->mValue != NULL) {
212 		if (strcmp(inParallelProp->mValue, "0") == 0 ||
213 			strcasecmp(inParallelProp->mValue, "false") == 0) {
214 
215 			/*
216 			 * Add the -s flag (do not perform umount operation in
217 			 * parallel) to the command.
218 			 */
219 			cim_logDebug("cmdgen_umountall",
220 				"Adding the -s flag");
221 			cmdLen = strlen(cmd) + strlen(SPACE) +
222 				strlen(NO_PARALLEL_FLAG) + 1;
223 			cmd = realloc(cmd, (size_t)(cmdLen * sizeof (char)));
224 			if (cmd == NULL) {
225 				*errp = errno;
226 				return (NULL);
227 			}
228 			(void) snprintf(cmd, cmdLen, "%s%s%s", cmd, SPACE,
229 				NO_PARALLEL_FLAG);
230 		}
231 	}
232 
233 	/*
234 	 * The caller must free the memory allocated to the return value
235 	 * using free().
236 	 */
237 	cim_logDebug("cmdgen_umountall", "Returning command: %s", cmd);
238 	*errp = 0;
239 	return (cmd);
240 } /* cmdgen_umountall */
241