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