usr/src/cmd/fs.d/nfs/lib/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 1999 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  	*/
    36 
    36 
    37 #include <stdio.h>
    37 #include <stdio.h>
    38 #include <stdlib.h>
    38 #include <stdlib.h>
    39 #include <string.h>
    39 #include <string.h>
    40 #include <unistd.h>
    40 #include <unistd.h>
       
    41 #include <sharefs/share.h>
    41 #include "sharetab.h"
    42 #include "sharetab.h"
    42 
       
    43 static int logging_specified(char *);
       
    44 
    43 
    45 /*
    44 /*
    46  * Get an entry from the share table.
    45  * Get an entry from the share table.
    47  * There should be at least 4 fields:
    46  * There should be at least 4 fields:
    48  *
    47  *
    54  *	> 1  valid entry
    53  *	> 1  valid entry
    55  *	= 0  end of file
    54  *	= 0  end of file
    56  *	< 0  error
    55  *	< 0  error
    57  */
    56  */
    58 int
    57 int
    59 getshare(fd, shp)
    58 getshare(FILE *fd, share_t **shp)
    60 	FILE *fd;
       
    61 	struct share **shp;
       
    62 {
    59 {
    63 	static char *line = NULL;
    60 	static char *line = NULL;
    64 	static struct share *sh = NULL;
    61 	static share_t *sh = NULL;
    65 	char *p;
    62 	char *p;
    66 	char *lasts;
    63 	char *lasts;
    67 	char *w = " \t";
    64 	char *w = " \t";
    68 
    65 
    69 	if (line == NULL) {
    66 	if (line == NULL) {
    70 		line = (char *)malloc(MAXBUFSIZE+1);
    67 		line = (char *)malloc(MAXBUFSIZE+1);
    71 		if (line == NULL)
    68 		if (line == NULL)
    72 			return (-1);
    69 			return (-1);
    73 	}
    70 	}
    74 	if (sh == NULL) {
    71 	if (sh == NULL) {
    75 		sh = (struct share *)malloc(sizeof (*sh));
    72 		sh = (share_t *)malloc(sizeof (*sh));
    76 		if (sh == NULL)
    73 		if (sh == NULL)
    77 			return (-1);
    74 			return (-1);
    78 	}
    75 	}
    79 
    76 
    80 	p = fgets(line, MAXBUFSIZE, fd);
    77 	p = fgets(line, MAXBUFSIZE, fd);
   100 
    97 
   101 	*shp = sh;
    98 	*shp = sh;
   102 	return (1);
    99 	return (1);
   103 }
   100 }
   104 
   101 
   105 /*
   102 share_t *
   106  * Append an entry to the sharetab file.
   103 sharedup(share_t *sh)
   107  */
       
   108 int
       
   109 putshare(fd, sh)
       
   110 	FILE *fd;
       
   111 	struct share *sh;
       
   112 {
   104 {
   113 	int r;
   105 	share_t *nsh;
   114 
   106 
   115 	if (fseek(fd, 0L, 2) < 0)
   107 	nsh = (share_t *)calloc(1, sizeof (*nsh));
   116 		return (-1);
       
   117 
       
   118 	r = fprintf(fd, "%s\t%s\t%s\t%s\t%s\n",
       
   119 		sh->sh_path,
       
   120 		sh->sh_res,
       
   121 		sh->sh_fstype,
       
   122 		sh->sh_opts,
       
   123 		sh->sh_descr);
       
   124 	return (r);
       
   125 }
       
   126 
       
   127 /*
       
   128  * The entry corresponding to path is removed from the
       
   129  * sharetab file.  The file is assumed to be locked.
       
   130  * Read the entries into a linked list of share structures
       
   131  * minus the entry to be removed.  Then truncate the sharetab
       
   132  * file and write almost all of it back to the file from the
       
   133  * linked list.
       
   134  *
       
   135  * If logging information is requested then 'logging' is set
       
   136  * to non-zero if the entry is shared with logging enabled.
       
   137  *
       
   138  * Note: The file is assumed to be locked.
       
   139  */
       
   140 int
       
   141 remshare(fd, path, logging)
       
   142 	FILE *fd;
       
   143 	char *path;
       
   144 	int *logging;
       
   145 {
       
   146 	struct share *sh_tmp;
       
   147 	struct shl {			/* the linked list */
       
   148 		struct shl   *shl_next;
       
   149 		struct share *shl_sh;
       
   150 	};
       
   151 	struct shl *shl_head = NULL;
       
   152 	struct shl *shl, *prev, *next;
       
   153 	int res, remcnt;
       
   154 
       
   155 	rewind(fd);
       
   156 	remcnt = 0;
       
   157 	shl = NULL;
       
   158 	while ((res = getshare(fd, &sh_tmp)) > 0) {
       
   159 		if (strcmp(path, sh_tmp->sh_path) == 0 ||
       
   160 		    strcmp(path, sh_tmp->sh_res)  == 0) {
       
   161 			remcnt++;
       
   162 			if (logging != NULL)
       
   163 				*logging = logging_specified(sh_tmp->sh_opts);
       
   164 		} else {
       
   165 			prev = shl;
       
   166 			shl = (struct shl *)malloc(sizeof (*shl));
       
   167 			if (shl == NULL) {
       
   168 				res = -1;
       
   169 				goto dealloc;
       
   170 			}
       
   171 			if (shl_head == NULL)
       
   172 				shl_head = shl;
       
   173 			else
       
   174 				prev->shl_next = shl;
       
   175 			shl->shl_next = NULL;
       
   176 			shl->shl_sh = sharedup(sh_tmp);
       
   177 			if (shl->shl_sh == NULL) {
       
   178 				res = -3;
       
   179 				goto dealloc;
       
   180 			}
       
   181 		}
       
   182 	}
       
   183 	if (res < 0)
       
   184 		goto dealloc;
       
   185 	if (remcnt == 0) {
       
   186 		res = 1;	/* nothing removed */
       
   187 		goto dealloc;
       
   188 	}
       
   189 
       
   190 	if (ftruncate(fileno(fd), 0) < 0) {
       
   191 		res = -2;
       
   192 		goto dealloc;
       
   193 	}
       
   194 
       
   195 	for (shl = shl_head; shl; shl = shl->shl_next)
       
   196 		putshare(fd, shl->shl_sh);
       
   197 	res = 1;
       
   198 
       
   199 dealloc:
       
   200 	for (shl = shl_head; shl; shl = next) {
       
   201 		/*
       
   202 		 * make sure we don't reference sharefree with NULL shl->shl_sh
       
   203 		 */
       
   204 		if (shl->shl_sh != NULL)
       
   205 			sharefree(shl->shl_sh);
       
   206 		next = shl->shl_next;
       
   207 		free(shl);
       
   208 	}
       
   209 	return (res);
       
   210 }
       
   211 
       
   212 struct share *
       
   213 sharedup(sh)
       
   214 	struct share *sh;
       
   215 {
       
   216 	struct share *nsh;
       
   217 
       
   218 	nsh = (struct share *)malloc(sizeof (*nsh));
       
   219 	if (nsh == NULL)
   108 	if (nsh == NULL)
   220 		return (NULL);
   109 		return (NULL);
   221 
   110 
   222 	(void) memset((char *)nsh, 0, sizeof (*nsh));
       
   223 	if (sh->sh_path) {
   111 	if (sh->sh_path) {
   224 		nsh->sh_path = strdup(sh->sh_path);
   112 		nsh->sh_path = strdup(sh->sh_path);
   225 		if (nsh->sh_path == NULL)
   113 		if (nsh->sh_path == NULL)
   226 			goto alloc_failed;
   114 			goto alloc_failed;
   227 	}
   115 	}
   252 	sharefree(nsh);
   140 	sharefree(nsh);
   253 	return (NULL);
   141 	return (NULL);
   254 }
   142 }
   255 
   143 
   256 void
   144 void
   257 sharefree(sh)
   145 sharefree(share_t *sh)
   258 	struct share *sh;
       
   259 {
   146 {
   260 	if (sh->sh_path != NULL)
   147 	if (sh->sh_path != NULL)
   261 		free(sh->sh_path);
   148 		free(sh->sh_path);
   262 	if (sh->sh_res != NULL)
   149 	if (sh->sh_res != NULL)
   263 		free(sh->sh_res);
   150 		free(sh->sh_res);
   274  * Return the value after "=" for option "opt"
   161  * Return the value after "=" for option "opt"
   275  * in option string "optlist". Caller must
   162  * in option string "optlist". Caller must
   276  * free returned value.
   163  * free returned value.
   277  */
   164  */
   278 char *
   165 char *
   279 getshareopt(optlist, opt)
   166 getshareopt(char *optlist, char *opt)
   280 	char *optlist, *opt;
       
   281 {
   167 {
   282 	char *p, *pe;
   168 	char *p, *pe;
   283 	char *b;
   169 	char *b;
   284 	char *bb;
   170 	char *bb;
   285 	char *lasts;
   171 	char *lasts;
   305 	}
   191 	}
   306 done:
   192 done:
   307 	free(bb);
   193 	free(bb);
   308 	return (val);
   194 	return (val);
   309 }
   195 }
   310 
       
   311 /*
       
   312  * Return 1 if the "log" option was specified in the optlist.
       
   313  * Return 0 otherwise.
       
   314  */
       
   315 static int
       
   316 logging_specified(optlist)
       
   317 	char *optlist;
       
   318 {
       
   319 	char *p;
       
   320 	char *b, *bb, *lasts;
       
   321 	int specified = 0;
       
   322 
       
   323 	b = bb = strdup(optlist);
       
   324 	if (b == NULL)
       
   325 		return (0);
       
   326 
       
   327 	while (p = (char *)strtok_r(b, ",", &lasts)) {
       
   328 		b = NULL;
       
   329 		if (strncmp(p, "log", 3) == 0)
       
   330 			specified++;
       
   331 	}
       
   332 
       
   333 	free(bb);
       
   334 	return (specified ? 1 : 0);
       
   335 }