8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libfsmgt / common / fs_shares.c
blob72d1cb7be8cf2ff7fe47929f92a83f37f2d0f880
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Traverses /etc/dfs/sharetab in order to find shared file systems
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 #include <errno.h>
37 #include <thread.h>
38 #include <synch.h>
39 #include "libfsmgt.h"
40 #include <sharefs/share.h>
41 #include "sharetab.h"
43 #define SECMODES 5
46 * Private variables
48 static mutex_t sharetab_lock = DEFAULTMUTEX;
51 * Private method declarations
53 fs_sharelist_t *create_sharelist_entry(struct share *sharetab_entry,
54 int *errp);
57 * Public methods
60 void
61 fs_free_share_list(fs_sharelist_t *headp)
63 fs_sharelist_t *tmp;
65 while (headp != NULL) {
66 tmp = headp->next;
67 free(headp->path);
68 free(headp->resource);
69 free(headp->fstype);
70 free(headp->options);
71 free(headp->description);
72 headp->next = NULL;
73 free(headp);
75 headp = tmp;
80 * Get a linked list of all the shares on the system from /etc/dfs/dfstab
82 fs_sharelist_t *
83 fs_get_share_list(int *errp)
85 fs_sharelist_t *newp;
86 fs_sharelist_t *headp;
87 fs_sharelist_t *tailp;
88 FILE *fp;
90 headp = NULL;
91 tailp = NULL;
93 if ((fp = fopen(SHARETAB, "r")) != NULL) {
94 struct share *sharetab_entry;
96 (void) mutex_lock(&sharetab_lock);
97 while (getshare(fp, &sharetab_entry) > 0) {
99 newp = create_sharelist_entry(sharetab_entry, errp);
100 if (newp == NULL) {
102 * Out of memory
104 fs_free_share_list(headp);
105 (void) mutex_unlock(&sharetab_lock);
106 (void) fclose(fp);
107 return (NULL);
110 if (headp == NULL) {
111 headp = newp;
112 tailp = newp;
113 } else {
114 tailp->next = newp;
115 tailp = newp;
118 } /* while (getshare(fp, &sharetab_entry) != 0) */
119 (void) mutex_unlock(&sharetab_lock);
120 (void) fclose(fp);
121 } else {
122 *errp = errno;
123 } /* if ((fp = fopen(SHARETAB, "r")) != NULL) */
126 * Caller must free the mount list
128 return (headp);
129 } /* fs_get_share_list */
133 * fs_parse_opts_for_sec_modes
134 * Get an array of strings of all the security modes of the option string.
136 * char *cmd - The option string from the share command.
137 * int *count - pointer to the number of elements in the returned array.
138 * int *error - error pointer for returning any errors.
140 char **
141 fs_parse_opts_for_sec_modes(char *cmd, int *count, int *error)
143 char *temp_str;
144 char **secstringarray;
145 char *strptr;
147 *count = 0;
148 strptr = strdup(cmd);
149 if (strptr == NULL) {
150 *error = ENOMEM;
151 return (NULL);
154 temp_str = strptr;
156 secstringarray =
157 (char **)calloc((size_t)SECMODES, (size_t)(sizeof (char *)));
158 if (secstringarray == NULL) {
159 *error = ENOMEM;
160 return (NULL);
163 if (strstr(strptr, "sec=") != NULL) {
164 char *next_str;
165 next_str = strptr;
167 while (next_str != NULL) {
168 next_str = strstr(strptr, "sec=");
169 if (next_str != NULL) {
170 if (strncmp(strptr, "sec=", 4) != 0) {
171 *(next_str - 1) = '\0';
173 strptr = next_str;
174 next_str = strstr(strptr + 4, "sec=");
175 if (next_str != NULL) {
176 *(next_str - 1) = '\0';
178 secstringarray[*count] = strdup(strptr);
179 if (secstringarray[*count] == NULL) {
180 *error = ENOMEM;
181 if (*count > 0) {
182 fileutil_free_string_array(
183 secstringarray, *count);
184 } else {
185 free(secstringarray);
187 free(temp_str);
188 return (NULL);
190 strptr = next_str;
191 (*count)++;
194 } else {
195 secstringarray[*count] = strdup(temp_str);
196 if (secstringarray[*count] == NULL) {
197 *error = ENOMEM;
198 if (*count > 0) {
199 fileutil_free_string_array(
200 secstringarray, *count);
201 } else {
202 free(secstringarray);
204 free(temp_str);
205 return (NULL);
207 (*count)++;
209 free(temp_str);
210 return (secstringarray);
214 * fs_create_array_from_accesslist
215 * Takes the colon seperated access list parses the list into an array
216 * containing all the elements of the list. The array created is returned
217 * and count is set to the number of elements in the array.
219 * char *access_list - The string containing the colon sperated access list.
220 * int *count - Will contain the number of elements in the array.
221 * int *err - any errors encountered.
223 char **
224 fs_create_array_from_accesslist(char *access_list, int *count, int *err)
226 char *delimiter = ":";
227 char *server_string;
228 char **list_array = NULL;
229 char *list_copy;
231 *count = 0;
232 if (access_list != NULL) {
233 list_copy = strdup(access_list);
234 if (list_copy != NULL) {
235 server_string = strtok(list_copy, delimiter);
236 if (server_string != NULL) {
237 while (server_string != NULL) {
238 if (!fileutil_add_string_to_array(
239 &list_array, server_string, count,
240 err)) {
241 fileutil_free_string_array(
242 list_array, *count);
243 free(list_copy);
244 goto return_err;
246 server_string =
247 strtok(NULL, delimiter);
249 } else {
250 list_array =
251 (char **)calloc(((*count) + 1),
252 sizeof (char *));
253 if (list_array == NULL) {
254 *err = ENOMEM;
255 free(list_copy);
256 goto return_err;
258 list_array[*count] = strdup(access_list);
259 if (list_array[*count] == NULL) {
260 *err = ENOMEM;
261 free(list_array);
262 list_array = NULL;
263 goto return_err;
265 (*count)++;
267 free(list_copy);
268 } else {
269 *err = ENOMEM;
272 return_err:
273 return (list_array);
274 } /* fs_create_array_from_accesslist */
278 * Private Methods
281 fs_sharelist_t *
282 create_sharelist_entry(struct share *sharetab_entry, int *errp)
285 fs_sharelist_t *newp;
287 newp = (fs_sharelist_t *)calloc((size_t)1,
288 (size_t)sizeof (fs_sharelist_t));
290 if (newp == NULL) {
292 * Out of memory
294 *errp = errno;
295 return (NULL);
298 newp->path = strdup(sharetab_entry->sh_path);
299 if (newp->path == NULL) {
301 * Out of memory
303 *errp = errno;
304 fs_free_share_list(newp);
305 return (NULL);
308 newp->resource = strdup(sharetab_entry->sh_res);
309 if (newp->path == NULL) {
311 * Out of memory
313 *errp = errno;
314 fs_free_share_list(newp);
315 return (NULL);
318 newp->fstype = strdup(sharetab_entry->sh_fstype);
319 if (newp->fstype == NULL) {
321 * Out of memory
323 *errp = errno;
324 fs_free_share_list(newp);
325 return (NULL);
328 newp->options = strdup(sharetab_entry->sh_opts);
329 if (newp->options == NULL) {
331 * Out of memory
333 *errp = errno;
334 fs_free_share_list(newp);
335 return (NULL);
338 newp->description = strdup(sharetab_entry->sh_descr);
339 if (newp->description == NULL) {
341 * Out of memory
343 *errp = errno;
344 fs_free_share_list(newp);
345 return (NULL);
347 newp->next = NULL;
349 return (newp);
350 } /* create_sharelist_entry */