usr/src/cmd/rexd/sharetab.c
changeset 3957 86c9dda5df37
parent 0 68f95e015346
equal deleted inserted replaced
3956:ea75466401e7 3957:86c9dda5df37
     1 /*
     1 /*
     2  * CDDL HEADER START
     2  * CDDL HEADER START
     3  *
     3  *
     4  * The contents of this file are subject to the terms of the
     4  * The contents of this file are subject to the terms of the
     5  * Common Development and Distribution License, Version 1.0 only
     5  * Common Development and Distribution License (the "License").
     6  * (the "License").  You may not use this file except in compliance
     6  * You may not use this file except in compliance with the License.
     7  * with the License.
       
     8  *
     7  *
     9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
    10  * or http://www.opensolaris.org/os/licensing.
     9  * or http://www.opensolaris.org/os/licensing.
    11  * See the License for the specific language governing permissions
    10  * See the License for the specific language governing permissions
    12  * and limitations under the License.
    11  * and limitations under the License.
    17  * fields enclosed by brackets "[]" replaced with your own identifying
    16  * fields enclosed by brackets "[]" replaced with your own identifying
    18  * information: Portions Copyright [yyyy] [name of copyright owner]
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
    19  *
    18  *
    20  * CDDL HEADER END
    19  * CDDL HEADER END
    21  */
    20  */
       
    21 
    22 /*
    22 /*
    23  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
    23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
    24  * Use is subject to license terms.
    24  * Use is subject to license terms.
    25  */
    25  */
    26 
    26 
    27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
    27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
    28 /*	  All Rights Reserved  	*/
    28 /*	  All Rights Reserved  	*/
    30 /*
    30 /*
    31  * Portions of this source code were derived from Berkeley 4.3 BSD
    31  * Portions of this source code were derived from Berkeley 4.3 BSD
    32  * under license from the Regents of the University of California.
    32  * under license from the Regents of the University of California.
    33  */
    33  */
    34 
    34 
    35 #ident	"%Z%%M%	%I%	%E% SMI"
    35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
    36 
    36 
    37 #include <stdio.h>
    37 #include <stdio.h>
    38 #include <string.h>
    38 #include <string.h>
       
    39 #include <sys/types.h>
       
    40 #include <sys/types32.h>
       
    41 #include <sharefs/share.h>
    39 #include "sharetab.h"
    42 #include "sharetab.h"
    40 
    43 
    41 static struct share *	sharedup();
    44 static share_t	*sharedup();
    42 static void		sharefree();
    45 static void	sharefree();
    43 
    46 
    44 /*
    47 /*
    45  * Get an entry from the share table.
    48  * Get an entry from the share table.
    46  * There should be at least 4 fields:
    49  * There should be at least 4 fields:
    47  *
    50  *
    53  *	> 1  valid entry
    56  *	> 1  valid entry
    54  *	= 0  end of file
    57  *	= 0  end of file
    55  *	< 0  error
    58  *	< 0  error
    56  */
    59  */
    57 int
    60 int
    58 getshare(fd, shp)
    61 getshare(FILE *fd, share_t **shp)
    59 	FILE *fd;
       
    60 	struct share **shp;
       
    61 {
    62 {
    62 	static char *line = NULL;
    63 	static char *line = NULL;
    63 	static struct share *sh = NULL;
    64 	static share_t *sh = NULL;
    64 	register char *p;
    65 	register char *p;
    65 	char *w = " \t";
    66 	char *w = " \t";
    66 
    67 
    67 	if (line == NULL) {
    68 	if (line == NULL) {
    68 		line = (char *) malloc(BUFSIZ+1);
    69 		line = (char *)malloc(BUFSIZ+1);
    69 		if (line == NULL)
    70 		if (line == NULL)
    70 			return (-1);
    71 			return (-1);
    71 	}
    72 	}
    72 	if (sh == NULL) {
    73 	if (sh == NULL) {
    73 		sh = (struct share *) malloc(sizeof(*sh));
    74 		sh = (share_t *)malloc(sizeof (*sh));
    74 		if (sh == NULL)
    75 		if (sh == NULL)
    75 			return (-1);
    76 			return (-1);
    76 	}
    77 	}
    77 
    78 
    78 	p = fgets(line, BUFSIZ, fd);
    79 	p = fgets(line, BUFSIZ, fd);
    98 
    99 
    99 	*shp = sh;
   100 	*shp = sh;
   100 	return (1);
   101 	return (1);
   101 }
   102 }
   102 
   103 
   103 /*
   104 static share_t *
   104  * Append an entry to the sharetab file.
   105 sharedup(share_t *sh)
   105  */
       
   106 int
       
   107 putshare(fd, sh)
       
   108 	FILE *fd;
       
   109 	struct share *sh;
       
   110 {
   106 {
   111 	int r;
   107 	share_t *nsh;
   112 
   108 
   113 	if (fseek(fd, 0L, 2) < 0)
   109 	nsh = (share_t *)calloc(1, sizeof (*nsh));
   114 		return (-1);
       
   115 
       
   116 	r = fprintf(fd, "%s\t%s\t%s\t%s\t%s\n",
       
   117 		sh->sh_path,
       
   118 		sh->sh_res,
       
   119 		sh->sh_fstype,
       
   120 		sh->sh_opts,
       
   121 		sh->sh_descr);
       
   122 	return (r);
       
   123 }
       
   124 
       
   125 /*
       
   126  * The entry corresponding to path is removed from the
       
   127  * sharetab file.  The file is assumed to be locked.
       
   128  * Read the entries into a linked list of share structures
       
   129  * minus the entry to be removed.  Then truncate the sharetab
       
   130  * file and write almost all of it back to the file from the
       
   131  * linked list.
       
   132  * Note: The file is assumed to be locked.
       
   133  */
       
   134 int
       
   135 remshare(fd, path)
       
   136 	FILE *fd;
       
   137 	char *path;
       
   138 {
       
   139 	struct share *sh_tmp;
       
   140 	struct shl {			/* the linked list */
       
   141 		struct shl   *shl_next;
       
   142 		struct share *shl_sh;
       
   143 	};
       
   144 	struct shl *shl_head = NULL;
       
   145 	struct shl *shl, *prev, *next;
       
   146 	int res, remcnt;
       
   147 
       
   148 	rewind(fd);
       
   149 	remcnt = 0;
       
   150 	while ((res = getshare(fd, &sh_tmp)) > 0) {
       
   151 		if (strcmp(path, sh_tmp->sh_path) == 0 ||
       
   152 		    strcmp(path, sh_tmp->sh_res)  == 0) {
       
   153 			remcnt++;
       
   154 		} else {
       
   155 			prev = shl;
       
   156 			shl = (struct shl *) malloc(sizeof(*shl));
       
   157 			if (shl == NULL) {
       
   158 				res = -1;
       
   159 				goto dealloc;
       
   160 			}
       
   161 			if (shl_head == NULL)
       
   162 				shl_head = shl;
       
   163 			else
       
   164 				prev->shl_next = shl;
       
   165 			shl->shl_next = NULL;
       
   166 			shl->shl_sh = sharedup(sh_tmp);
       
   167 			if (shl->shl_sh == NULL) {
       
   168 				res = -1;
       
   169 				goto dealloc;
       
   170 			}
       
   171 		}
       
   172 	}
       
   173 	if (res < 0)
       
   174 		goto dealloc;
       
   175 	if (remcnt == 0) {
       
   176 		res = 1;	/* nothing removed */
       
   177 		goto dealloc;
       
   178 	}
       
   179 
       
   180 	if (ftruncate(fileno(fd), 0) < 0) {
       
   181 		res = -1;
       
   182 		goto dealloc;
       
   183 	}
       
   184 
       
   185 	for (shl = shl_head ; shl ; shl = shl->shl_next)
       
   186 		putshare(fd, shl->shl_sh);
       
   187 	res = 1;
       
   188 
       
   189 dealloc:
       
   190 	for (shl = shl_head ; shl ; shl = next) {
       
   191 		sharefree(shl->shl_sh);
       
   192 		next = shl->shl_next;
       
   193 		free(shl);
       
   194 	}
       
   195 	return (res);
       
   196 }
       
   197 
       
   198 static struct share *
       
   199 sharedup(sh)
       
   200 	struct share *sh;
       
   201 {
       
   202 	struct share *nsh;
       
   203 	
       
   204 	nsh = (struct share *) malloc(sizeof(*nsh));
       
   205 	if (nsh == NULL)
   110 	if (nsh == NULL)
   206 		return (NULL);
   111 		return (NULL);
       
   112 
   207 	nsh->sh_path = strdup(sh->sh_path);
   113 	nsh->sh_path = strdup(sh->sh_path);
   208 	if (nsh->sh_path == NULL)
   114 	if (nsh->sh_path == NULL)
   209 		goto alloc_failed;
   115 		goto alloc_failed;
   210 	nsh->sh_res = strdup(sh->sh_res);
   116 	nsh->sh_res = strdup(sh->sh_res);
   211 	if (nsh->sh_res == NULL)
   117 	if (nsh->sh_res == NULL)
   225 	sharefree(nsh);
   131 	sharefree(nsh);
   226 	return (NULL);
   132 	return (NULL);
   227 }
   133 }
   228 
   134 
   229 static void
   135 static void
   230 sharefree(sh)
   136 sharefree(share_t *sh)
   231 	struct share *sh;
       
   232 {
   137 {
   233 	if (sh == NULL)
   138 	if (sh == NULL)
   234 		return;
   139 		return;
   235 	if (sh->sh_path != NULL)
   140 	if (sh->sh_path != NULL)
   236 		free(sh->sh_path);
   141 		free(sh->sh_path);
   248 /*
   153 /*
   249  * Return the value after "=" for option "opt"
   154  * Return the value after "=" for option "opt"
   250  * in option string "optlist".
   155  * in option string "optlist".
   251  */
   156  */
   252 char *
   157 char *
   253 getshareopt(optlist, opt)
   158 getshareopt(char *optlist, char *opt)
   254 	char *optlist, *opt;
       
   255 {
   159 {
   256 	char *p, *pe;
   160 	char *p, *pe;
   257 	char *b;
   161 	char *b;
   258 	static char *bb;
   162 	static char *bb;
   259 
   163