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
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.
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]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/types.h>
41 * Private datastructures.
43 typedef struct dfstab_entry
{
44 struct dfstab_entry
*next
;
52 static const char *whitespace
= " \t";
53 static mutex_t dfstab_lock
= DEFAULTMUTEX
;
58 static dfstab_entry_t
*get_dfstab_ents(int *);
59 static void free_dfstab_list(dfstab_entry_t
*);
60 static dfstab_entry_t
*dfstab_line_to_dfstab_entry(char *, int *);
61 static char *create_share_cmd(dfstab_entry_t
*, char *, int *);
62 static dfstab_entry_t
*change_dfstab_ent(dfstab_entry_t
*,
63 dfstab_entry_t
*, int *);
64 static void add_entry_to_dfstab(dfstab_entry_t
*, int *);
67 static dfstab_entry_t
*
68 get_dfstab_ents(int *err
)
70 dfstab_entry_t
*dfstablist
, *headptr
, *tailptr
= NULL
;
71 FILE *dfp
; /* fp for dfs list */
72 static char cmd
[BUFSIZE
];
75 if ((dfp
= fopen(DFSTAB
, "r")) != NULL
) {
77 (void) mutex_lock(&dfstab_lock
);
79 fileutil_getline(dfp
, cmd
, BUFSIZE
)) != NULL
) {
81 dfstab_line_to_dfstab_entry(share_cmd
, err
)) !=
83 if (tailptr
== NULL
) {
87 tailptr
->next
= dfstablist
;
90 dfstablist
= dfstablist
->next
;
97 if (tailptr
== NULL
) {
100 (void) mutex_unlock(&dfstab_lock
);
104 (void) fprintf(stderr
, "%s: cannot open %s\n", cmd
, DFSTAB
);
108 } /* get_dfstab_ents */
111 add_entry_to_dfstab(dfstab_entry_t
*list
, int *err
)
113 FILE *dfp
; /* fp for dfs list */
115 if ((dfp
= fopen(DFSTAB
, "a")) != NULL
) {
117 if ((share_cmd
= create_share_cmd(list
, NULL
, err
)) != NULL
) {
118 (void) mutex_lock(&dfstab_lock
);
119 fprintf(dfp
, "%s", share_cmd
);
121 (void) mutex_unlock(&dfstab_lock
);
130 } /* add_entry_to_dfstab */
133 free_dfstab_list(dfstab_entry_t
*headp
)
135 dfstab_entry_t
*tmp
= headp
;
137 while (headp
!= NULL
) {
139 if (headp
->path
!= NULL
) {
142 if (headp
->resource
!= NULL
) {
143 free(headp
->resource
);
145 if (headp
->fstype
!= NULL
) {
148 if (headp
->options
!= NULL
) {
149 free(headp
->options
);
151 if (headp
->description
!= NULL
) {
152 free(headp
->description
);
158 } /* free_dfstab_list */
161 create_share_cmd(dfstab_entry_t
*new_entry
, char *temp_line
, int *err
)
163 char tempstr
[BUFSIZE
];
166 cmd
= (char *)calloc((size_t)1, BUFSIZE
);
171 sprintf(cmd
, "share ");
172 if (new_entry
->fstype
) {
173 sprintf(tempstr
, "-F %s ", new_entry
->fstype
);
174 strlcat(cmd
, tempstr
, BUFSIZE
);
176 if (new_entry
->options
) {
177 sprintf(tempstr
, "-o %s ", new_entry
->options
);
178 strlcat(cmd
, tempstr
, BUFSIZE
);
180 if (new_entry
->description
) {
181 sprintf(tempstr
, "-d %s ",
182 new_entry
->description
);
183 strlcat(cmd
, tempstr
, BUFSIZE
);
185 sprintf(tempstr
, "%s\n", new_entry
->path
);
186 strlcat(cmd
, tempstr
, BUFSIZE
);
187 if (temp_line
!= NULL
&& strchr(temp_line
, '#')) {
188 sprintf(tempstr
, " %s", strchr(temp_line
, '#'));
189 strlcat(cmd
, tempstr
, BUFSIZE
);
191 ret_val
= strdup(cmd
);
194 } /* create_share_cmd */
197 * dfstab_line_to_dfstab_entry - parses a line from dfstab and fills in
198 * the fields of a dfstab_entry_t structure
200 * char *cmd - the share command or dfstab line to be parsed
201 * int *err - a pointer for returning any error codes encountered
203 static dfstab_entry_t
*
204 dfstab_line_to_dfstab_entry(char *cmd
, int *err
)
207 dfstab_entry_t
*dfstablist
;
212 char *arglist
[LINESZ
];
217 temp_str
= strdup(cmd
);
218 if (temp_str
== NULL
) {
223 for (arglist
[argcount
] = strtok(temp_str
, whitespace
);
224 arglist
[argcount
] != NULL
; /* CSTYLED */) {
225 arglist
[++argcount
] = strtok(NULL
, whitespace
);
229 (dfstab_entry_t
*)calloc((size_t)1,
230 sizeof (dfstab_entry_t
));
231 if (dfstablist
== NULL
) {
236 while ((c
= getopt(argcount
, arglist
, "F:d:o:")) != -1) {
239 /* file system type */
241 *err
|= (dfstablist
->fstype
!= NULL
);
242 dfstablist
->fstype
= strdup(optarg
);
243 if (dfstablist
->fstype
== NULL
) {
245 free_dfstab_list(dfstablist
);
250 case 'd': /* description */
252 *err
|= (dfstablist
->description
!= NULL
);
253 dfstablist
->description
= strdup(optarg
);
254 if (dfstablist
->description
== NULL
) {
256 free_dfstab_list(dfstablist
);
261 case 'o': /* fs specific options */
262 /* at most one - o */
263 *err
|= (dfstablist
->options
!= NULL
);
264 dfstablist
->options
= strdup(optarg
);
265 if (dfstablist
->options
== NULL
) {
267 free_dfstab_list(dfstablist
);
277 if (dfstablist
->fstype
== NULL
) {
280 if ((fp
= fopen(DFSTYPES
, "r")) == NULL
) {
281 (void) fprintf(stderr
, "%s: cannot open %s\n",
283 free_dfstab_list(dfstablist
);
287 (void) mutex_lock(&dfstab_lock
);
288 dfstablist
->fstype
= strdup(fileutil_getfs(fp
));
289 (void) mutex_unlock(&dfstab_lock
);
292 dfstablist
->path
= strdup(arglist
[argcount
]);
293 if (dfstablist
->path
== NULL
) {
295 free_dfstab_list(dfstablist
);
301 } /* dfstab_line_to_dfstab_entry */
303 static dfstab_entry_t
*
305 dfstab_entry_t
*old_entry
,
306 dfstab_entry_t
*new_entry
,
311 dfstab_entry_t
*temp_list
, *ret_val
;
313 char **temp_dfstab
= NULL
;
316 if ((fp
= fopen(DFSTAB
, "r")) != NULL
) {
319 (void) mutex_lock(&dfstab_lock
);
320 while (fgets(cmd
, BUFSIZE
, fp
) != NULL
) {
322 fileutil_get_cmd_from_string(cmd
)) == NULL
) {
323 if (!fileutil_add_string_to_array(
324 &temp_dfstab
, cmd
, &count
, err
)) {
332 dfstab_line_to_dfstab_entry(share_cmd
, err
)) ==
338 if (strcmp(old_entry
->path
,
339 temp_list
->path
) == 0) {
340 char *new_cmd
= NULL
;
342 if (new_entry
!= NULL
&& (new_cmd
=
343 create_share_cmd(new_entry
, cmd
,
345 if (!fileutil_add_string_to_array(
346 &temp_dfstab
, new_cmd
, &count
,
357 if (!fileutil_add_string_to_array(
358 &temp_dfstab
, cmd
, &count
, err
)) {
365 free_dfstab_list(temp_list
);
370 if (line_found
&& temp_dfstab
!= NULL
) {
371 if ((fp
= fopen(DFSTAB
, "w")) != NULL
) {
373 for (i
= 0; i
< count
; i
++) {
374 fprintf(fp
, "%s", temp_dfstab
[i
]);
377 (void) mutex_unlock(&dfstab_lock
);
378 ret_val
= get_dfstab_ents(err
);
379 fileutil_free_string_array(temp_dfstab
, count
);
382 (void) mutex_unlock(&dfstab_lock
);
383 fileutil_free_string_array(temp_dfstab
, count
);
387 (void) mutex_unlock(&dfstab_lock
);
388 if (temp_dfstab
!= NULL
) {
389 fileutil_free_string_array(temp_dfstab
, count
);
398 } /* change_dfstab_ent */
401 * Public accessor functions.
405 * fs_add_DFStab_ent - adds an entry to dfstab and to the list of dfstab
406 * entries. Returns a pointer to the head of the dfstab entry list.
408 * char *cmd - the same command to be added to dstab
409 * int *err - an error pointer for retruning any errors
412 fs_add_DFStab_ent(char *cmd
, int *err
)
414 dfstab_entry_t
*dfstab_ent
;
416 dfstab_ent
= dfstab_line_to_dfstab_entry(cmd
, err
);
417 if (dfstab_ent
== NULL
) {
421 add_entry_to_dfstab(dfstab_ent
, err
);
423 free_dfstab_list(dfstab_ent
);
426 free_dfstab_list(dfstab_ent
);
427 return (get_dfstab_ents(err
));
431 * set_DFStab_ent - adds an entry to dfstab and to the list of dfstab entries.
432 * returns a pointer to the head of the dfstab entry list.
443 dfstab_entry_t
*new_entry
;
444 new_entry
= (dfstab_entry_t
*)calloc((size_t)1,
445 sizeof (dfstab_entry_t
));
446 if (new_entry
== NULL
) {
451 new_entry
->path
= strdup(path
);
454 free_dfstab_list(new_entry
);
457 if (fstype
!= NULL
) {
458 new_entry
->fstype
= strdup(fstype
);
462 if ((fp
= fopen(DFSTYPES
, "r")) == NULL
) {
463 /* change this to error handler */
464 (void) fprintf(stderr
, "cannot open %s\n",
466 free_dfstab_list(new_entry
);
469 (void) mutex_lock(&dfstab_lock
);
470 new_entry
->fstype
= strdup(fileutil_getfs(fp
));
471 (void) mutex_unlock(&dfstab_lock
);
474 if (options
!= NULL
) {
475 new_entry
->options
= strdup(options
);
477 if (description
!= NULL
) {
478 new_entry
->description
= strdup(description
);
480 add_entry_to_dfstab(new_entry
, err
);
482 free_dfstab_list(new_entry
);
485 free_dfstab_list(new_entry
);
486 return (get_dfstab_ents(err
));
487 } /* set_DFStab_ent */
490 * Accessor function for path element of dfstab entry.
493 fs_get_DFStab_ent_Path(void *entry
)
495 dfstab_entry_t
*entryptr
= (dfstab_entry_t
*)entry
;
496 if (entryptr
== NULL
) {
499 return (entryptr
->path
);
500 } /* get_DFStab_ent_Path */
503 * Accessor function for fstype element of dfstab entry.
506 fs_get_DFStab_ent_Fstype(void *entry
)
508 dfstab_entry_t
*entryptr
= (dfstab_entry_t
*)entry
;
509 if (entryptr
== NULL
) {
512 return (entryptr
->fstype
);
516 * Accessor function for options element of dfstab entry.
519 fs_get_DFStab_ent_Options(void *entry
)
521 dfstab_entry_t
*entryptr
= (dfstab_entry_t
*)entry
;
522 if (entryptr
== NULL
) {
525 return (entryptr
->options
);
529 * Accessor function for description element of dfstab entry.
532 fs_get_DFStab_ent_Desc(void *entry
)
534 dfstab_entry_t
*entryptr
= (dfstab_entry_t
*)entry
;
535 if (entryptr
== NULL
) {
538 return (entryptr
->description
);
542 * Accessor function for resource element of dfstab entry.
545 fs_get_DFStab_ent_Res(void *entry
)
547 dfstab_entry_t
*entryptr
= (dfstab_entry_t
*)entry
;
548 if (entryptr
== NULL
) {
551 return (entryptr
->resource
);
556 * Calls get_dfstab_ents to create the list of dfstab
557 * entries and returns that list.
560 fs_get_DFStab_ents(int *err
)
562 dfstab_entry_t
*list
;
563 list
= get_dfstab_ents(err
);
568 * Retrives and returns the next entry in the list.
571 fs_get_DFStab_ent_Next(void *list
)
573 dfstab_entry_t
*listptr
= (dfstab_entry_t
*)list
;
574 if (listptr
== NULL
) {
577 return (listptr
->next
);
581 * Retrives and returns a share command based on the dfstab entry passed in.
584 fs_get_Dfstab_share_cmd(fs_dfstab_entry_t dfstab_ent
, int *err
)
587 if (dfstab_ent
== NULL
) {
590 share_cmd
= create_share_cmd((dfstab_entry_t
*)dfstab_ent
, NULL
, err
);
592 } /* fs_get_Dfstab_share_cmd */
595 * edit_DFStab_ent - changes an entry in dfstab.
598 fs_edit_DFStab_ent(char *old_cmd
, char *new_cmd
, int *err
)
600 dfstab_entry_t
*old_dfstabent
, *new_dfstabent
, *ret_val
;
603 dfstab_line_to_dfstab_entry(old_cmd
, err
)) == NULL
) {
607 dfstab_line_to_dfstab_entry(new_cmd
, err
)) == NULL
) {
611 change_dfstab_ent(old_dfstabent
, new_dfstabent
, err
)) == NULL
) {
614 free_dfstab_list(old_dfstabent
);
615 free_dfstab_list(new_dfstabent
);
620 * del_DFStab_ent - deletes an entry in dfstab.
623 fs_del_DFStab_ent(char *del_cmd
, int *err
)
625 dfstab_entry_t
*del_dfstabent
, *ret_val
;
628 dfstab_line_to_dfstab_entry(del_cmd
, err
)) == NULL
) {
632 change_dfstab_ent(del_dfstabent
, NULL
, err
)) == NULL
) {
635 free_dfstab_list(del_dfstabent
);
640 * del_All_DFStab_ents_with_Path - deletes all duplicate entries with
641 * the specified path.
644 fs_del_All_DFStab_ents_with_Path(char *path
, int *err
)
646 dfstab_entry_t del_dfstabent
, *ret_val
;
649 if ((del_dfstabent
.path
= strdup(path
)) != NULL
) {
650 if ((ret_val
= change_dfstab_ent(&del_dfstabent
,
651 NULL
, err
)) == NULL
) {
654 free(del_dfstabent
.path
);
668 fs_check_for_duplicate_DFStab_paths(char *path
, int *err
)
670 dfstab_entry_t
*dfstablist
;
677 dfstablist
= get_dfstab_ents(err
);
678 if (dfstablist
!= NULL
) {
679 while (dfstablist
!= NULL
) {
680 if (strcmp(dfstablist
->path
, path
) == 0) {
683 dfstablist
= dfstablist
->next
;
686 free_dfstab_list(dfstablist
);
697 fs_free_DFStab_ents(void *list
)
699 dfstab_entry_t
*headp
= (dfstab_entry_t
*)list
;
700 free_dfstab_list(headp
);
704 * used for debugging only
707 fs_print_dfstab_entries(void *list
)
709 while (list
!= NULL
) {
711 if (fs_get_DFStab_ent_Fstype(list
) != NULL
)
712 printf("fstype: %s", fs_get_DFStab_ent_Fstype(list
));
713 if (fs_get_DFStab_ent_Desc(list
) != NULL
)
714 printf(" description: %s",
715 fs_get_DFStab_ent_Desc(list
));
716 if (fs_get_DFStab_ent_Options(list
) != NULL
)
717 printf(" options: %s",
718 fs_get_DFStab_ent_Options(list
));
719 if (fs_get_DFStab_ent_Path(list
) != NULL
)
720 printf(" shared path is: %s\n",
721 fs_get_DFStab_ent_Path(list
));
722 list
= (void *)fs_get_DFStab_ent_Next(list
);