xref: /titanic_51/usr/src/cmd/rexd/sharetab.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include "sharetab.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static struct share *	sharedup();
42*7c478bd9Sstevel@tonic-gate static void		sharefree();
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * Get an entry from the share table.
46*7c478bd9Sstevel@tonic-gate  * There should be at least 4 fields:
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  * 	pathname  resource  fstype  options  [ description ]
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * A fifth field (description) is optional.
51*7c478bd9Sstevel@tonic-gate  *
52*7c478bd9Sstevel@tonic-gate  * Returns:
53*7c478bd9Sstevel@tonic-gate  *	> 1  valid entry
54*7c478bd9Sstevel@tonic-gate  *	= 0  end of file
55*7c478bd9Sstevel@tonic-gate  *	< 0  error
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate int
58*7c478bd9Sstevel@tonic-gate getshare(fd, shp)
59*7c478bd9Sstevel@tonic-gate 	FILE *fd;
60*7c478bd9Sstevel@tonic-gate 	struct share **shp;
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	static char *line = NULL;
63*7c478bd9Sstevel@tonic-gate 	static struct share *sh = NULL;
64*7c478bd9Sstevel@tonic-gate 	register char *p;
65*7c478bd9Sstevel@tonic-gate 	char *w = " \t";
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (line == NULL) {
68*7c478bd9Sstevel@tonic-gate 		line = (char *) malloc(BUFSIZ+1);
69*7c478bd9Sstevel@tonic-gate 		if (line == NULL)
70*7c478bd9Sstevel@tonic-gate 			return (-1);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	if (sh == NULL) {
73*7c478bd9Sstevel@tonic-gate 		sh = (struct share *) malloc(sizeof(*sh));
74*7c478bd9Sstevel@tonic-gate 		if (sh == NULL)
75*7c478bd9Sstevel@tonic-gate 			return (-1);
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	p = fgets(line, BUFSIZ, fd);
79*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
80*7c478bd9Sstevel@tonic-gate 		return (0);
81*7c478bd9Sstevel@tonic-gate 	line[strlen(line) - 1] = '\0';
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	sh->sh_path = strtok(p, w);
84*7c478bd9Sstevel@tonic-gate 	if (sh->sh_path == NULL)
85*7c478bd9Sstevel@tonic-gate 		return (-1);
86*7c478bd9Sstevel@tonic-gate 	sh->sh_res = strtok(NULL, w);
87*7c478bd9Sstevel@tonic-gate 	if (sh->sh_res == NULL)
88*7c478bd9Sstevel@tonic-gate 		return (-1);
89*7c478bd9Sstevel@tonic-gate 	sh->sh_fstype = strtok(NULL, w);
90*7c478bd9Sstevel@tonic-gate 	if (sh->sh_fstype == NULL)
91*7c478bd9Sstevel@tonic-gate 		return (-1);
92*7c478bd9Sstevel@tonic-gate 	sh->sh_opts = strtok(NULL, w);
93*7c478bd9Sstevel@tonic-gate 	if (sh->sh_opts == NULL)
94*7c478bd9Sstevel@tonic-gate 		return (-1);
95*7c478bd9Sstevel@tonic-gate 	sh->sh_descr = strtok(NULL, "");
96*7c478bd9Sstevel@tonic-gate 	if (sh->sh_descr == NULL)
97*7c478bd9Sstevel@tonic-gate 		sh->sh_descr = "";
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	*shp = sh;
100*7c478bd9Sstevel@tonic-gate 	return (1);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate  * Append an entry to the sharetab file.
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate int
107*7c478bd9Sstevel@tonic-gate putshare(fd, sh)
108*7c478bd9Sstevel@tonic-gate 	FILE *fd;
109*7c478bd9Sstevel@tonic-gate 	struct share *sh;
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	int r;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	if (fseek(fd, 0L, 2) < 0)
114*7c478bd9Sstevel@tonic-gate 		return (-1);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	r = fprintf(fd, "%s\t%s\t%s\t%s\t%s\n",
117*7c478bd9Sstevel@tonic-gate 		sh->sh_path,
118*7c478bd9Sstevel@tonic-gate 		sh->sh_res,
119*7c478bd9Sstevel@tonic-gate 		sh->sh_fstype,
120*7c478bd9Sstevel@tonic-gate 		sh->sh_opts,
121*7c478bd9Sstevel@tonic-gate 		sh->sh_descr);
122*7c478bd9Sstevel@tonic-gate 	return (r);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  * The entry corresponding to path is removed from the
127*7c478bd9Sstevel@tonic-gate  * sharetab file.  The file is assumed to be locked.
128*7c478bd9Sstevel@tonic-gate  * Read the entries into a linked list of share structures
129*7c478bd9Sstevel@tonic-gate  * minus the entry to be removed.  Then truncate the sharetab
130*7c478bd9Sstevel@tonic-gate  * file and write almost all of it back to the file from the
131*7c478bd9Sstevel@tonic-gate  * linked list.
132*7c478bd9Sstevel@tonic-gate  * Note: The file is assumed to be locked.
133*7c478bd9Sstevel@tonic-gate  */
134*7c478bd9Sstevel@tonic-gate int
135*7c478bd9Sstevel@tonic-gate remshare(fd, path)
136*7c478bd9Sstevel@tonic-gate 	FILE *fd;
137*7c478bd9Sstevel@tonic-gate 	char *path;
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate 	struct share *sh_tmp;
140*7c478bd9Sstevel@tonic-gate 	struct shl {			/* the linked list */
141*7c478bd9Sstevel@tonic-gate 		struct shl   *shl_next;
142*7c478bd9Sstevel@tonic-gate 		struct share *shl_sh;
143*7c478bd9Sstevel@tonic-gate 	};
144*7c478bd9Sstevel@tonic-gate 	struct shl *shl_head = NULL;
145*7c478bd9Sstevel@tonic-gate 	struct shl *shl, *prev, *next;
146*7c478bd9Sstevel@tonic-gate 	int res, remcnt;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	rewind(fd);
149*7c478bd9Sstevel@tonic-gate 	remcnt = 0;
150*7c478bd9Sstevel@tonic-gate 	while ((res = getshare(fd, &sh_tmp)) > 0) {
151*7c478bd9Sstevel@tonic-gate 		if (strcmp(path, sh_tmp->sh_path) == 0 ||
152*7c478bd9Sstevel@tonic-gate 		    strcmp(path, sh_tmp->sh_res)  == 0) {
153*7c478bd9Sstevel@tonic-gate 			remcnt++;
154*7c478bd9Sstevel@tonic-gate 		} else {
155*7c478bd9Sstevel@tonic-gate 			prev = shl;
156*7c478bd9Sstevel@tonic-gate 			shl = (struct shl *) malloc(sizeof(*shl));
157*7c478bd9Sstevel@tonic-gate 			if (shl == NULL) {
158*7c478bd9Sstevel@tonic-gate 				res = -1;
159*7c478bd9Sstevel@tonic-gate 				goto dealloc;
160*7c478bd9Sstevel@tonic-gate 			}
161*7c478bd9Sstevel@tonic-gate 			if (shl_head == NULL)
162*7c478bd9Sstevel@tonic-gate 				shl_head = shl;
163*7c478bd9Sstevel@tonic-gate 			else
164*7c478bd9Sstevel@tonic-gate 				prev->shl_next = shl;
165*7c478bd9Sstevel@tonic-gate 			shl->shl_next = NULL;
166*7c478bd9Sstevel@tonic-gate 			shl->shl_sh = sharedup(sh_tmp);
167*7c478bd9Sstevel@tonic-gate 			if (shl->shl_sh == NULL) {
168*7c478bd9Sstevel@tonic-gate 				res = -1;
169*7c478bd9Sstevel@tonic-gate 				goto dealloc;
170*7c478bd9Sstevel@tonic-gate 			}
171*7c478bd9Sstevel@tonic-gate 		}
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 	if (res < 0)
174*7c478bd9Sstevel@tonic-gate 		goto dealloc;
175*7c478bd9Sstevel@tonic-gate 	if (remcnt == 0) {
176*7c478bd9Sstevel@tonic-gate 		res = 1;	/* nothing removed */
177*7c478bd9Sstevel@tonic-gate 		goto dealloc;
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (ftruncate(fileno(fd), 0) < 0) {
181*7c478bd9Sstevel@tonic-gate 		res = -1;
182*7c478bd9Sstevel@tonic-gate 		goto dealloc;
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	for (shl = shl_head ; shl ; shl = shl->shl_next)
186*7c478bd9Sstevel@tonic-gate 		putshare(fd, shl->shl_sh);
187*7c478bd9Sstevel@tonic-gate 	res = 1;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate dealloc:
190*7c478bd9Sstevel@tonic-gate 	for (shl = shl_head ; shl ; shl = next) {
191*7c478bd9Sstevel@tonic-gate 		sharefree(shl->shl_sh);
192*7c478bd9Sstevel@tonic-gate 		next = shl->shl_next;
193*7c478bd9Sstevel@tonic-gate 		free(shl);
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 	return (res);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate static struct share *
199*7c478bd9Sstevel@tonic-gate sharedup(sh)
200*7c478bd9Sstevel@tonic-gate 	struct share *sh;
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	struct share *nsh;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	nsh = (struct share *) malloc(sizeof(*nsh));
205*7c478bd9Sstevel@tonic-gate 	if (nsh == NULL)
206*7c478bd9Sstevel@tonic-gate 		return (NULL);
207*7c478bd9Sstevel@tonic-gate 	nsh->sh_path = strdup(sh->sh_path);
208*7c478bd9Sstevel@tonic-gate 	if (nsh->sh_path == NULL)
209*7c478bd9Sstevel@tonic-gate 		goto alloc_failed;
210*7c478bd9Sstevel@tonic-gate 	nsh->sh_res = strdup(sh->sh_res);
211*7c478bd9Sstevel@tonic-gate 	if (nsh->sh_res == NULL)
212*7c478bd9Sstevel@tonic-gate 		goto alloc_failed;
213*7c478bd9Sstevel@tonic-gate 	nsh->sh_fstype = strdup(sh->sh_fstype);
214*7c478bd9Sstevel@tonic-gate 	if (nsh->sh_fstype == NULL)
215*7c478bd9Sstevel@tonic-gate 		goto alloc_failed;
216*7c478bd9Sstevel@tonic-gate 	nsh->sh_opts = strdup(sh->sh_opts);
217*7c478bd9Sstevel@tonic-gate 	if (nsh->sh_opts == NULL)
218*7c478bd9Sstevel@tonic-gate 		goto alloc_failed;
219*7c478bd9Sstevel@tonic-gate 	nsh->sh_descr = strdup(sh->sh_descr);
220*7c478bd9Sstevel@tonic-gate 	if (nsh->sh_descr == NULL)
221*7c478bd9Sstevel@tonic-gate 		goto alloc_failed;
222*7c478bd9Sstevel@tonic-gate 	return (nsh);
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate alloc_failed:
225*7c478bd9Sstevel@tonic-gate 	sharefree(nsh);
226*7c478bd9Sstevel@tonic-gate 	return (NULL);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate static void
230*7c478bd9Sstevel@tonic-gate sharefree(sh)
231*7c478bd9Sstevel@tonic-gate 	struct share *sh;
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	if (sh == NULL)
234*7c478bd9Sstevel@tonic-gate 		return;
235*7c478bd9Sstevel@tonic-gate 	if (sh->sh_path != NULL)
236*7c478bd9Sstevel@tonic-gate 		free(sh->sh_path);
237*7c478bd9Sstevel@tonic-gate 	if (sh->sh_res != NULL)
238*7c478bd9Sstevel@tonic-gate 		free(sh->sh_res);
239*7c478bd9Sstevel@tonic-gate 	if (sh->sh_fstype != NULL)
240*7c478bd9Sstevel@tonic-gate 		free(sh->sh_fstype);
241*7c478bd9Sstevel@tonic-gate 	if (sh->sh_opts != NULL)
242*7c478bd9Sstevel@tonic-gate 		free(sh->sh_opts);
243*7c478bd9Sstevel@tonic-gate 	if (sh->sh_descr != NULL)
244*7c478bd9Sstevel@tonic-gate 		free(sh->sh_descr);
245*7c478bd9Sstevel@tonic-gate 	free(sh);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate /*
249*7c478bd9Sstevel@tonic-gate  * Return the value after "=" for option "opt"
250*7c478bd9Sstevel@tonic-gate  * in option string "optlist".
251*7c478bd9Sstevel@tonic-gate  */
252*7c478bd9Sstevel@tonic-gate char *
253*7c478bd9Sstevel@tonic-gate getshareopt(optlist, opt)
254*7c478bd9Sstevel@tonic-gate 	char *optlist, *opt;
255*7c478bd9Sstevel@tonic-gate {
256*7c478bd9Sstevel@tonic-gate 	char *p, *pe;
257*7c478bd9Sstevel@tonic-gate 	char *b;
258*7c478bd9Sstevel@tonic-gate 	static char *bb;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	if (bb)
261*7c478bd9Sstevel@tonic-gate 		free(bb);
262*7c478bd9Sstevel@tonic-gate 	b = bb = strdup(optlist);
263*7c478bd9Sstevel@tonic-gate 	if (b == NULL)
264*7c478bd9Sstevel@tonic-gate 		return (NULL);
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	while (p = strtok(b, ",")) {
267*7c478bd9Sstevel@tonic-gate 		b = NULL;
268*7c478bd9Sstevel@tonic-gate 		if (pe = strchr(p, '=')) {
269*7c478bd9Sstevel@tonic-gate 			*pe = '\0';
270*7c478bd9Sstevel@tonic-gate 			if (strcmp(opt, p) == 0)
271*7c478bd9Sstevel@tonic-gate 				return (pe + 1);
272*7c478bd9Sstevel@tonic-gate 		}
273*7c478bd9Sstevel@tonic-gate 		if (strcmp(opt, p) == 0)
274*7c478bd9Sstevel@tonic-gate 			return ("");
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	return (NULL);
278*7c478bd9Sstevel@tonic-gate }
279