xref: /illumos-gate/usr/src/cmd/fs.d/nfs/lib/nfslogtab.c (revision 2a8bcb4e)
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 (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * Manipulates the nfslogtab
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #ifndef _REENTRANT
32*7c478bd9Sstevel@tonic-gate #define	_REENTRANT
33*7c478bd9Sstevel@tonic-gate #endif
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <utmpx.h>
39*7c478bd9Sstevel@tonic-gate #include <assert.h>
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <unistd.h>
43*7c478bd9Sstevel@tonic-gate #include <strings.h>
44*7c478bd9Sstevel@tonic-gate #include <string.h>
45*7c478bd9Sstevel@tonic-gate #include "nfslogtab.h"
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #ifndef	LINTHAPPY
48*7c478bd9Sstevel@tonic-gate #define	LINTHAPPY
49*7c478bd9Sstevel@tonic-gate #endif
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static void logtab_ent_list_free(struct logtab_ent_list *);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate  * Retrieves the next entry from nfslogtab.
55*7c478bd9Sstevel@tonic-gate  * Assumes the file is locked.
56*7c478bd9Sstevel@tonic-gate  * '*lepp' points to the new entry if successful.
57*7c478bd9Sstevel@tonic-gate  * Returns:
58*7c478bd9Sstevel@tonic-gate  *      > 0  valid entry
59*7c478bd9Sstevel@tonic-gate  *      = 0  end of file
60*7c478bd9Sstevel@tonic-gate  *      < 0  error
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate int
logtab_getent(FILE * fd,struct logtab_ent ** lepp)63*7c478bd9Sstevel@tonic-gate logtab_getent(FILE *fd, struct logtab_ent **lepp)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	char line[MAXBUFSIZE + 1];
66*7c478bd9Sstevel@tonic-gate 	char *p;
67*7c478bd9Sstevel@tonic-gate 	char *lasts, *tmp;
68*7c478bd9Sstevel@tonic-gate 	char *w = " \t";
69*7c478bd9Sstevel@tonic-gate 	struct logtab_ent *lep = NULL;
70*7c478bd9Sstevel@tonic-gate 	int error = 0;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	if ((lep = (struct logtab_ent *)malloc(sizeof (*lep))) == NULL) {
73*7c478bd9Sstevel@tonic-gate 		return (-1);
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 	(void) memset((char *)lep, 0, sizeof (*lep));
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if ((p = fgets(line, MAXBUFSIZE, fd)) == NULL)  {
78*7c478bd9Sstevel@tonic-gate 		error = 0;
79*7c478bd9Sstevel@tonic-gate 		goto errout;
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	line[strlen(line) - 1] = '\0';
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	tmp = (char *)strtok_r(p, w, &lasts);
85*7c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
86*7c478bd9Sstevel@tonic-gate 		error = -1;
87*7c478bd9Sstevel@tonic-gate 		goto errout;
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 	if ((lep->le_buffer = strdup(tmp)) == NULL) {
90*7c478bd9Sstevel@tonic-gate 		error = -1;
91*7c478bd9Sstevel@tonic-gate 		goto errout;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	tmp = (char *)strtok_r(NULL, w, &lasts);
95*7c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
96*7c478bd9Sstevel@tonic-gate 		error = -1;
97*7c478bd9Sstevel@tonic-gate 		goto errout;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 	if ((lep->le_path = strdup(tmp)) == NULL) {
100*7c478bd9Sstevel@tonic-gate 		error = -1;
101*7c478bd9Sstevel@tonic-gate 		goto errout;
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	tmp = (char *)strtok_r(NULL, w, &lasts);
105*7c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
106*7c478bd9Sstevel@tonic-gate 		error = -1;
107*7c478bd9Sstevel@tonic-gate 		goto errout;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	if ((lep->le_tag = strdup(tmp)) == NULL) {
110*7c478bd9Sstevel@tonic-gate 		error = -1;
111*7c478bd9Sstevel@tonic-gate 		goto errout;
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	tmp = (char *)strtok_r(NULL, w, &lasts);
115*7c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
116*7c478bd9Sstevel@tonic-gate 		error = -1;
117*7c478bd9Sstevel@tonic-gate 		goto errout;
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	lep->le_state = atoi(tmp);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	*lepp = lep;
122*7c478bd9Sstevel@tonic-gate 	return (1);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate errout:
125*7c478bd9Sstevel@tonic-gate 	logtab_ent_free(lep);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	return (error);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate /*
131*7c478bd9Sstevel@tonic-gate  * Append an entry to the logtab file.
132*7c478bd9Sstevel@tonic-gate  */
133*7c478bd9Sstevel@tonic-gate int
logtab_putent(FILE * fd,struct logtab_ent * lep)134*7c478bd9Sstevel@tonic-gate logtab_putent(FILE *fd, struct logtab_ent *lep)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	int r;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	if (fseek(fd, 0L, SEEK_END) < 0)
139*7c478bd9Sstevel@tonic-gate 		return (errno);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	r = fprintf(fd, "%s\t%s\t%s\t%d\n",
142*7c478bd9Sstevel@tonic-gate 		lep->le_buffer,
143*7c478bd9Sstevel@tonic-gate 		lep->le_path,
144*7c478bd9Sstevel@tonic-gate 		lep->le_tag,
145*7c478bd9Sstevel@tonic-gate 		lep->le_state);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	return (r);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate #ifndef	LINTHAPPY
151*7c478bd9Sstevel@tonic-gate /*
152*7c478bd9Sstevel@tonic-gate  * Searches the nfslogtab file looking for the next entry which matches
153*7c478bd9Sstevel@tonic-gate  * the search criteria. The search is continued at the current position
154*7c478bd9Sstevel@tonic-gate  * in the nfslogtab file.
155*7c478bd9Sstevel@tonic-gate  * If 'buffer' != NULL, then buffer is matched.
156*7c478bd9Sstevel@tonic-gate  * If 'path' != NULL, then path is matched.
157*7c478bd9Sstevel@tonic-gate  * If 'tag' != NULL, then tag is matched.
158*7c478bd9Sstevel@tonic-gate  * If 'state' != -1, then state is matched.
159*7c478bd9Sstevel@tonic-gate  * 'buffer', 'path' and 'tag' can all be non-NULL, which means the entry must
160*7c478bd9Sstevel@tonic-gate  * satisfy all requirements.
161*7c478bd9Sstevel@tonic-gate  *
162*7c478bd9Sstevel@tonic-gate  * Returns 0 on success, ENOENT otherwise.
163*7c478bd9Sstevel@tonic-gate  * If found, '*lepp' points to the matching entry, otherwise '*lepp' is
164*7c478bd9Sstevel@tonic-gate  * undefined.
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate static int
logtab_findent(FILE * fd,char * buffer,char * path,char * tag,int state,struct logtab_ent ** lepp)167*7c478bd9Sstevel@tonic-gate logtab_findent(FILE *fd, char *buffer, char *path, char *tag, int state,
168*7c478bd9Sstevel@tonic-gate 		struct logtab_ent **lepp)
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	while (!found && (logtab_getent(fd, lepp) > 0)) {
173*7c478bd9Sstevel@tonic-gate 		found = B_TRUE;
174*7c478bd9Sstevel@tonic-gate 		if (buffer != NULL)
175*7c478bd9Sstevel@tonic-gate 			found = strcmp(buffer, (*lepp)->le_buffer) == 0;
176*7c478bd9Sstevel@tonic-gate 		if (path != NULL)
177*7c478bd9Sstevel@tonic-gate 			found = found && (strcmp(path, (*lepp)->le_path) == 0);
178*7c478bd9Sstevel@tonic-gate 		if (tag != NULL)
179*7c478bd9Sstevel@tonic-gate 			found = found && (strcmp(tag, (*lepp)->le_tag) == 0);
180*7c478bd9Sstevel@tonic-gate 		if (state != -1)
181*7c478bd9Sstevel@tonic-gate 			found = found && (state == (*lepp)->le_state);
182*7c478bd9Sstevel@tonic-gate 		if (!found)
183*7c478bd9Sstevel@tonic-gate 			logtab_ent_free(*lepp);
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return (found ? 0 : ENOENT);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate #endif
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate /*
191*7c478bd9Sstevel@tonic-gate  * Remove all entries which match the search criteria.
192*7c478bd9Sstevel@tonic-gate  * If 'buffer' != NULL, then buffer is matched.
193*7c478bd9Sstevel@tonic-gate  * If 'path' != NULL, then path is matched.
194*7c478bd9Sstevel@tonic-gate  * If 'tag' != NULL, then tag is matched.
195*7c478bd9Sstevel@tonic-gate  * If 'state' != -1, then state is matched.
196*7c478bd9Sstevel@tonic-gate  * 'buffer', 'path' and 'tag' can all be non-NULL, which means the entry must
197*7c478bd9Sstevel@tonic-gate  * satisfy all requirements.
198*7c478bd9Sstevel@tonic-gate  * The file is assumed to be locked.
199*7c478bd9Sstevel@tonic-gate  * Read the entries into a linked list of logtab_ent structures
200*7c478bd9Sstevel@tonic-gate  * minus the entries to be removed, then truncate the nfslogtab
201*7c478bd9Sstevel@tonic-gate  * file and write it back to the file from the linked list.
202*7c478bd9Sstevel@tonic-gate  *
203*7c478bd9Sstevel@tonic-gate  * On success returns 0, -1 otherwise.
204*7c478bd9Sstevel@tonic-gate  * Entry not found is treated as success since it was going to be removed
205*7c478bd9Sstevel@tonic-gate  * anyway.
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate int
logtab_rement(FILE * fd,char * buffer,char * path,char * tag,int state)208*7c478bd9Sstevel@tonic-gate logtab_rement(FILE *fd, char *buffer, char *path, char *tag, int state)
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	struct logtab_ent_list *head = NULL, *tail = NULL, *tmpl;
211*7c478bd9Sstevel@tonic-gate 	struct logtab_ent *lep;
212*7c478bd9Sstevel@tonic-gate 	int remcnt = 0;		/* remove count */
213*7c478bd9Sstevel@tonic-gate 	int error = 0;
214*7c478bd9Sstevel@tonic-gate 	boolean_t found;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	rewind(fd);
217*7c478bd9Sstevel@tonic-gate 	while ((error = logtab_getent(fd, &lep)) > 0) {
218*7c478bd9Sstevel@tonic-gate 		found = B_TRUE;
219*7c478bd9Sstevel@tonic-gate 		if (buffer != NULL)
220*7c478bd9Sstevel@tonic-gate 			found = strcmp(buffer, lep->le_buffer) == 0;
221*7c478bd9Sstevel@tonic-gate 		if (path != NULL)
222*7c478bd9Sstevel@tonic-gate 			found = found && (strcmp(path, lep->le_path) == 0);
223*7c478bd9Sstevel@tonic-gate 		if (tag != NULL)
224*7c478bd9Sstevel@tonic-gate 			found = found && (strcmp(tag, lep->le_tag) == 0);
225*7c478bd9Sstevel@tonic-gate 		if (state != -1)
226*7c478bd9Sstevel@tonic-gate 			found = found && (state == lep->le_state);
227*7c478bd9Sstevel@tonic-gate 		if (found) {
228*7c478bd9Sstevel@tonic-gate 			remcnt++;
229*7c478bd9Sstevel@tonic-gate 			logtab_ent_free(lep);
230*7c478bd9Sstevel@tonic-gate 		} else {
231*7c478bd9Sstevel@tonic-gate 			tmpl = (struct logtab_ent_list *)
232*7c478bd9Sstevel@tonic-gate 				malloc(sizeof (struct logtab_ent));
233*7c478bd9Sstevel@tonic-gate 			if (tmpl == NULL) {
234*7c478bd9Sstevel@tonic-gate 				error = ENOENT;
235*7c478bd9Sstevel@tonic-gate 				break;
236*7c478bd9Sstevel@tonic-gate 			}
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 			tmpl->lel_le = lep;
239*7c478bd9Sstevel@tonic-gate 			tmpl->lel_next = NULL;
240*7c478bd9Sstevel@tonic-gate 			if (head == NULL) {
241*7c478bd9Sstevel@tonic-gate 				/*
242*7c478bd9Sstevel@tonic-gate 				 * empty list
243*7c478bd9Sstevel@tonic-gate 				 */
244*7c478bd9Sstevel@tonic-gate 				head = tail = tmpl;
245*7c478bd9Sstevel@tonic-gate 			} else {
246*7c478bd9Sstevel@tonic-gate 				/*
247*7c478bd9Sstevel@tonic-gate 				 * Add to the end of the list and remember
248*7c478bd9Sstevel@tonic-gate 				 * the new last element.
249*7c478bd9Sstevel@tonic-gate 				 */
250*7c478bd9Sstevel@tonic-gate 				tail->lel_next = tmpl;
251*7c478bd9Sstevel@tonic-gate 				tail = tmpl;	/* remember the last element */
252*7c478bd9Sstevel@tonic-gate 			}
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	if (error)
257*7c478bd9Sstevel@tonic-gate 		goto deallocate;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	if (remcnt == 0) {
260*7c478bd9Sstevel@tonic-gate 		/*
261*7c478bd9Sstevel@tonic-gate 		 * Entry not found, nothing to do
262*7c478bd9Sstevel@tonic-gate 		 */
263*7c478bd9Sstevel@tonic-gate 		goto deallocate;
264*7c478bd9Sstevel@tonic-gate 	}
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	if (ftruncate(fileno(fd), 0) < 0) {
267*7c478bd9Sstevel@tonic-gate 		error = -1;
268*7c478bd9Sstevel@tonic-gate 		goto deallocate;
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	for (tmpl = head; tmpl != NULL; tmpl = tmpl->lel_next)
272*7c478bd9Sstevel@tonic-gate 		(void) logtab_putent(fd, tmpl->lel_le);
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate deallocate:
275*7c478bd9Sstevel@tonic-gate 	logtab_ent_list_free(head);
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	return (error);
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate /*
281*7c478bd9Sstevel@tonic-gate  * Deactivate all entries matching search criteria.
282*7c478bd9Sstevel@tonic-gate  * If 'buffer' != NULL then match buffer.
283*7c478bd9Sstevel@tonic-gate  * If 'path' != NULL then match path.
284*7c478bd9Sstevel@tonic-gate  * If 'tag' != NULL then match tag.
285*7c478bd9Sstevel@tonic-gate  * Note that 'buffer', 'path' and 'tag' can al be non-null at the same time.
286*7c478bd9Sstevel@tonic-gate  *
287*7c478bd9Sstevel@tonic-gate  * Rewrites the nfslogtab file with the updated state for each entry.
288*7c478bd9Sstevel@tonic-gate  * Assumes the nfslogtab file has been locked for writing.
289*7c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
290*7c478bd9Sstevel@tonic-gate  */
291*7c478bd9Sstevel@tonic-gate int
logtab_deactivate(FILE * fd,char * buffer,char * path,char * tag)292*7c478bd9Sstevel@tonic-gate logtab_deactivate(FILE *fd, char *buffer, char *path, char *tag)
293*7c478bd9Sstevel@tonic-gate {
294*7c478bd9Sstevel@tonic-gate 	struct logtab_ent_list *lelp, *head = NULL, *tail = NULL;
295*7c478bd9Sstevel@tonic-gate 	struct logtab_ent *lep;
296*7c478bd9Sstevel@tonic-gate 	boolean_t found;
297*7c478bd9Sstevel@tonic-gate 	int error = 0;
298*7c478bd9Sstevel@tonic-gate 	int count = 0;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	rewind(fd);
301*7c478bd9Sstevel@tonic-gate 	while ((error = logtab_getent(fd, &lep)) > 0) {
302*7c478bd9Sstevel@tonic-gate 		found = B_TRUE;
303*7c478bd9Sstevel@tonic-gate 		if (buffer != NULL)
304*7c478bd9Sstevel@tonic-gate 			found = strcmp(buffer, lep->le_buffer) == 0;
305*7c478bd9Sstevel@tonic-gate 		if (path != NULL)
306*7c478bd9Sstevel@tonic-gate 			found = found && (strcmp(path, lep->le_path) == 0);
307*7c478bd9Sstevel@tonic-gate 		if (tag != NULL)
308*7c478bd9Sstevel@tonic-gate 			found = found && (strcmp(tag, lep->le_tag) == 0);
309*7c478bd9Sstevel@tonic-gate 		if (found && (lep->le_state == LES_ACTIVE)) {
310*7c478bd9Sstevel@tonic-gate 			count++;
311*7c478bd9Sstevel@tonic-gate 			lep->le_state = LES_INACTIVE;
312*7c478bd9Sstevel@tonic-gate 		}
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 		lelp = (struct logtab_ent_list *)
315*7c478bd9Sstevel@tonic-gate 			malloc(sizeof (struct logtab_ent));
316*7c478bd9Sstevel@tonic-gate 		if (lelp == NULL) {
317*7c478bd9Sstevel@tonic-gate 			error = ENOENT;
318*7c478bd9Sstevel@tonic-gate 			break;
319*7c478bd9Sstevel@tonic-gate 		}
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 		lelp->lel_le = lep;
322*7c478bd9Sstevel@tonic-gate 		lelp->lel_next = NULL;
323*7c478bd9Sstevel@tonic-gate 		if (head == NULL) {
324*7c478bd9Sstevel@tonic-gate 			/*
325*7c478bd9Sstevel@tonic-gate 			 * empty list
326*7c478bd9Sstevel@tonic-gate 			 */
327*7c478bd9Sstevel@tonic-gate 			head = tail = lelp;
328*7c478bd9Sstevel@tonic-gate 		} else {
329*7c478bd9Sstevel@tonic-gate 			/*
330*7c478bd9Sstevel@tonic-gate 			 * Add to the end of the list and remember
331*7c478bd9Sstevel@tonic-gate 			 * the new last element.
332*7c478bd9Sstevel@tonic-gate 			 */
333*7c478bd9Sstevel@tonic-gate 			tail->lel_next = lelp;
334*7c478bd9Sstevel@tonic-gate 			tail = lelp;	/* remember the last element */
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 	}
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	if (error)
339*7c478bd9Sstevel@tonic-gate 		goto deallocate;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if (count == 0) {
342*7c478bd9Sstevel@tonic-gate 		/*
343*7c478bd9Sstevel@tonic-gate 		 * done
344*7c478bd9Sstevel@tonic-gate 		 */
345*7c478bd9Sstevel@tonic-gate 		error = 0;
346*7c478bd9Sstevel@tonic-gate 		goto deallocate;
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	if (ftruncate(fileno(fd), 0) < 0) {
350*7c478bd9Sstevel@tonic-gate 		error = -1;
351*7c478bd9Sstevel@tonic-gate 		goto deallocate;
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	for (lelp = head; lelp != NULL; lelp = lelp->lel_next)
355*7c478bd9Sstevel@tonic-gate 		(void) logtab_putent(fd, lelp->lel_le);
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate deallocate:
358*7c478bd9Sstevel@tonic-gate 	logtab_ent_list_free(head);
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	return (error);
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate /*
364*7c478bd9Sstevel@tonic-gate  * Deactivates all entries if nfslogtab exists and is older than boot time
365*7c478bd9Sstevel@tonic-gate  * This will only happen the first time it is called.
366*7c478bd9Sstevel@tonic-gate  * Assumes 'fd' has been locked by the caller.
367*7c478bd9Sstevel@tonic-gate  * Returns 0 on success, otherwise -1.
368*7c478bd9Sstevel@tonic-gate  */
369*7c478bd9Sstevel@tonic-gate int
logtab_deactivate_after_boot(FILE * fd)370*7c478bd9Sstevel@tonic-gate logtab_deactivate_after_boot(FILE *fd)
371*7c478bd9Sstevel@tonic-gate {
372*7c478bd9Sstevel@tonic-gate 	struct stat st;
373*7c478bd9Sstevel@tonic-gate 	struct utmpx *utmpxp;
374*7c478bd9Sstevel@tonic-gate 	int error = 0;
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 	if ((fstat(fileno(fd), &st) == 0) &&
377*7c478bd9Sstevel@tonic-gate 	    ((utmpxp = getutxent()) != NULL) &&
378*7c478bd9Sstevel@tonic-gate 	    (utmpxp->ut_xtime > st.st_mtime)) {
379*7c478bd9Sstevel@tonic-gate 		if (logtab_deactivate(fd, NULL, NULL, NULL))
380*7c478bd9Sstevel@tonic-gate 			error = -1;
381*7c478bd9Sstevel@tonic-gate 	}
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	return (error);
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate void
logtab_ent_free(struct logtab_ent * lep)387*7c478bd9Sstevel@tonic-gate logtab_ent_free(struct logtab_ent *lep)
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate 	if (lep->le_buffer)
390*7c478bd9Sstevel@tonic-gate 		free(lep->le_buffer);
391*7c478bd9Sstevel@tonic-gate 	if (lep->le_path)
392*7c478bd9Sstevel@tonic-gate 		free(lep->le_path);
393*7c478bd9Sstevel@tonic-gate 	if (lep->le_tag)
394*7c478bd9Sstevel@tonic-gate 		free(lep->le_tag);
395*7c478bd9Sstevel@tonic-gate 	free(lep);
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate static void
logtab_ent_list_free(struct logtab_ent_list * head)399*7c478bd9Sstevel@tonic-gate logtab_ent_list_free(struct logtab_ent_list *head)
400*7c478bd9Sstevel@tonic-gate {
401*7c478bd9Sstevel@tonic-gate 	struct logtab_ent_list *lelp, *next;
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	if (head == NULL)
404*7c478bd9Sstevel@tonic-gate 		return;
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 	for (lelp = head; lelp != NULL; lelp = next) {
407*7c478bd9Sstevel@tonic-gate 		if (lelp->lel_le != NULL)
408*7c478bd9Sstevel@tonic-gate 			logtab_ent_free(lelp->lel_le);
409*7c478bd9Sstevel@tonic-gate 		next = lelp->lel_next;
410*7c478bd9Sstevel@tonic-gate 		free(lelp);
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate }
413