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