xref: /onnv-gate/usr/src/cmd/fs.d/nfs/nfslog/nfslogd.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-2000 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  * Postprocessor for NFS server logging.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include <arpa/inet.h>
33*0Sstevel@tonic-gate #include <assert.h>
34*0Sstevel@tonic-gate #include <deflt.h>
35*0Sstevel@tonic-gate #include <errno.h>
36*0Sstevel@tonic-gate #include <fcntl.h>
37*0Sstevel@tonic-gate #include <netinet/in.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <strings.h>
42*0Sstevel@tonic-gate #include <signal.h>
43*0Sstevel@tonic-gate #include <syslog.h>
44*0Sstevel@tonic-gate #include <limits.h>
45*0Sstevel@tonic-gate #include <libintl.h>
46*0Sstevel@tonic-gate #include <locale.h>
47*0Sstevel@tonic-gate #include <unistd.h>
48*0Sstevel@tonic-gate #include <sys/types.h>
49*0Sstevel@tonic-gate #include <sys/utsname.h>
50*0Sstevel@tonic-gate #include <sys/stat.h>
51*0Sstevel@tonic-gate #include <sys/resource.h>
52*0Sstevel@tonic-gate #include <rpc/clnt_stat.h>
53*0Sstevel@tonic-gate #include <nfs/nfs.h>
54*0Sstevel@tonic-gate #include <nfs/export.h>
55*0Sstevel@tonic-gate #include <nfs/nfs_log.h>
56*0Sstevel@tonic-gate #include "fhtab.h"
57*0Sstevel@tonic-gate #include "nfslogd.h"
58*0Sstevel@tonic-gate #include "buffer_list.h"
59*0Sstevel@tonic-gate #include "../lib/nfslog_config.h"
60*0Sstevel@tonic-gate #include "../lib/nfslogtab.h"
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate enum pidfile_operation {
63*0Sstevel@tonic-gate 	PID_STARTUP, PID_SHUTDOWN
64*0Sstevel@tonic-gate };
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static int nfslogtab_deactivate_after_boot(void);
67*0Sstevel@tonic-gate static int
68*0Sstevel@tonic-gate 	nfslogtab_remove(struct buffer_ent **, struct buffer_ent **, boolean_t);
69*0Sstevel@tonic-gate static int cycle_logs(nfsl_config_t *, int);
70*0Sstevel@tonic-gate static void enable_logcycling(void);
71*0Sstevel@tonic-gate static int process_pidfile(enum pidfile_operation);
72*0Sstevel@tonic-gate static void short_cleanup(void);
73*0Sstevel@tonic-gate static void full_cleanup(void);
74*0Sstevel@tonic-gate static void transactions_timeout(nfsl_config_t *);
75*0Sstevel@tonic-gate static void close_all_translogs(nfsl_config_t *);
76*0Sstevel@tonic-gate int cycle_log(char *, int);
77*0Sstevel@tonic-gate static boolean_t is_cycle_needed(char *, void **, boolean_t, int *);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * Configuration information.
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate int debug = 0;
84*0Sstevel@tonic-gate boolean_t test = B_FALSE;
85*0Sstevel@tonic-gate time_t mapping_update_interval = MAPPING_UPDATE_INTERVAL;
86*0Sstevel@tonic-gate /* prune_timeout measures how old a database entry must be to be pruned */
87*0Sstevel@tonic-gate time_t prune_timeout = (SECSPERHOUR * 7 * 24);
88*0Sstevel@tonic-gate int max_logs_preserve = MAX_LOGS_PRESERVE;
89*0Sstevel@tonic-gate uint_t idle_time = IDLE_TIME;
90*0Sstevel@tonic-gate static mode_t Umask = NFSLOG_UMASK;
91*0Sstevel@tonic-gate static long cycle_frequency = CYCLE_FREQUENCY;
92*0Sstevel@tonic-gate /* prune_frequency measures how often should prune_dbs be called */
93*0Sstevel@tonic-gate static long prune_frequency = (SECSPERHOUR * 24);
94*0Sstevel@tonic-gate static int min_size = MIN_PROCESSING_SIZE;
95*0Sstevel@tonic-gate static volatile bool_t need2cycle = FALSE;
96*0Sstevel@tonic-gate static volatile bool_t need2prune = FALSE;
97*0Sstevel@tonic-gate boolean_t keep_running = B_TRUE;
98*0Sstevel@tonic-gate boolean_t quick_cleaning = B_FALSE;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*ARGSUSED*/
101*0Sstevel@tonic-gate int
main(int argc,char ** argv)102*0Sstevel@tonic-gate main(int argc, char **argv)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	struct rlimit rl;
105*0Sstevel@tonic-gate 	int error = 0;
106*0Sstevel@tonic-gate 	char *defp;
107*0Sstevel@tonic-gate 	pid_t pid;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	timestruc_t logtab_update;
110*0Sstevel@tonic-gate 	time_t process_start, last_prune = time(0);
111*0Sstevel@tonic-gate 	time_t last_cycle = time(0);	/* last time logs were cycled */
112*0Sstevel@tonic-gate 	int processed, buffers_processed;
113*0Sstevel@tonic-gate 	struct buffer_ent *buffer_list = NULL, *bep, *next;
114*0Sstevel@tonic-gate 	nfsl_config_t *config_list = NULL;
115*0Sstevel@tonic-gate 	char	*fhtable_to_prune = NULL;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/*
118*0Sstevel@tonic-gate 	 * Check to make sure user is root.
119*0Sstevel@tonic-gate 	 */
120*0Sstevel@tonic-gate 	if (geteuid() != 0) {
121*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s must be run as root\n"),
122*0Sstevel@tonic-gate 			argv[0]);
123*0Sstevel@tonic-gate 		exit(1);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * Read defaults file.
128*0Sstevel@tonic-gate 	 */
129*0Sstevel@tonic-gate 	if (defopen(NFSLOG_OPTIONS_FILE) == 0) {
130*0Sstevel@tonic-gate 		if ((defp = defread("DEBUG=")) != NULL) {
131*0Sstevel@tonic-gate 			debug = atoi(defp);
132*0Sstevel@tonic-gate 			if (debug > 0)
133*0Sstevel@tonic-gate 				(void) printf("debug=%d\n", debug);
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 		if ((defp = defread("TEST=")) != NULL) {
136*0Sstevel@tonic-gate 			if (strcmp(defp, "TRUE") == 0)
137*0Sstevel@tonic-gate 				test = B_TRUE;
138*0Sstevel@tonic-gate 			if (debug > 0) {
139*0Sstevel@tonic-gate 				if (test)
140*0Sstevel@tonic-gate 					(void) printf("test=TRUE\n");
141*0Sstevel@tonic-gate 				else
142*0Sstevel@tonic-gate 					(void) printf("test=FALSE\n");
143*0Sstevel@tonic-gate 			}
144*0Sstevel@tonic-gate 		}
145*0Sstevel@tonic-gate 		/*
146*0Sstevel@tonic-gate 		 * Set Umask for log and fhtable creation.
147*0Sstevel@tonic-gate 		 */
148*0Sstevel@tonic-gate 		if ((defp = defread("UMASK=")) != NULL) {
149*0Sstevel@tonic-gate 			if (sscanf(defp, "%lo", &Umask) != 1)
150*0Sstevel@tonic-gate 				Umask = NFSLOG_UMASK;
151*0Sstevel@tonic-gate 		}
152*0Sstevel@tonic-gate 		/*
153*0Sstevel@tonic-gate 		 * Minimum size buffer should reach before processing.
154*0Sstevel@tonic-gate 		 */
155*0Sstevel@tonic-gate 		if ((defp = defread("MIN_PROCESSING_SIZE=")) != NULL) {
156*0Sstevel@tonic-gate 			min_size = atoi(defp);
157*0Sstevel@tonic-gate 			if (debug > 0)
158*0Sstevel@tonic-gate 				(void) printf("min_size=%d\n", min_size);
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 		/*
161*0Sstevel@tonic-gate 		 * Number of seconds the daemon should
162*0Sstevel@tonic-gate 		 * sleep waiting for more work.
163*0Sstevel@tonic-gate 		 */
164*0Sstevel@tonic-gate 		if ((defp = defread("IDLE_TIME=")) != NULL) {
165*0Sstevel@tonic-gate 			idle_time = (uint_t)atoi(defp);
166*0Sstevel@tonic-gate 			if (debug > 0)
167*0Sstevel@tonic-gate 				(void) printf("idle_time=%d\n", idle_time);
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 		/*
170*0Sstevel@tonic-gate 		 * Maximum number of logs to preserve.
171*0Sstevel@tonic-gate 		 */
172*0Sstevel@tonic-gate 		if ((defp = defread("MAX_LOGS_PRESERVE=")) != NULL) {
173*0Sstevel@tonic-gate 			max_logs_preserve = atoi(defp);
174*0Sstevel@tonic-gate 			if (debug > 0) {
175*0Sstevel@tonic-gate 				(void) printf("max_logs_preserve=%d\n",
176*0Sstevel@tonic-gate 					max_logs_preserve);
177*0Sstevel@tonic-gate 			}
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 		/*
180*0Sstevel@tonic-gate 		 * Frequency of atime updates.
181*0Sstevel@tonic-gate 		 */
182*0Sstevel@tonic-gate 		if ((defp = defread("MAPPING_UPDATE_INTERVAL=")) != NULL) {
183*0Sstevel@tonic-gate 			mapping_update_interval = atoi(defp);
184*0Sstevel@tonic-gate 			if (debug > 0) {
185*0Sstevel@tonic-gate 				(void) printf("mapping_update_interval=%ld\n",
186*0Sstevel@tonic-gate 					mapping_update_interval);
187*0Sstevel@tonic-gate 			}
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 		/*
190*0Sstevel@tonic-gate 		 * Time to remove entries
191*0Sstevel@tonic-gate 		 */
192*0Sstevel@tonic-gate 		if ((defp = defread("PRUNE_TIMEOUT=")) != NULL) {
193*0Sstevel@tonic-gate 			/*
194*0Sstevel@tonic-gate 			 * Prune timeout is in hours but we want
195*0Sstevel@tonic-gate 			 * deal with the time in seconds internally.
196*0Sstevel@tonic-gate 			 */
197*0Sstevel@tonic-gate 			prune_timeout = atoi(defp);
198*0Sstevel@tonic-gate 			prune_timeout *= SECSPERHOUR;
199*0Sstevel@tonic-gate 			if (prune_timeout < prune_frequency)
200*0Sstevel@tonic-gate 				prune_frequency = prune_timeout;
201*0Sstevel@tonic-gate 			if (debug > 0) {
202*0Sstevel@tonic-gate 				(void) printf("prune_timeout=%ld\n",
203*0Sstevel@tonic-gate 					prune_timeout);
204*0Sstevel@tonic-gate 			}
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 		/*
207*0Sstevel@tonic-gate 		 * fhtable to prune when start (for debug/test purposes)
208*0Sstevel@tonic-gate 		 */
209*0Sstevel@tonic-gate 		if ((defp = defread("PRUNE_FHTABLE=")) != NULL) {
210*0Sstevel@tonic-gate 			/*
211*0Sstevel@tonic-gate 			 * Specify full pathname of fhtable to prune before
212*0Sstevel@tonic-gate 			 * any processing is to be done
213*0Sstevel@tonic-gate 			 */
214*0Sstevel@tonic-gate 			if (fhtable_to_prune = malloc(strlen(defp) + 1)) {
215*0Sstevel@tonic-gate 				(void) strcpy(fhtable_to_prune, defp);
216*0Sstevel@tonic-gate 				if (debug > 0) {
217*0Sstevel@tonic-gate 					(void) printf("fhtable to prune=%s\n",
218*0Sstevel@tonic-gate 							fhtable_to_prune);
219*0Sstevel@tonic-gate 				}
220*0Sstevel@tonic-gate 			} else {
221*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
222*0Sstevel@tonic-gate 					"malloc fhtable_to_prune error %s\n"),
223*0Sstevel@tonic-gate 					strerror(errno));
224*0Sstevel@tonic-gate 			}
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 		/*
227*0Sstevel@tonic-gate 		 * Log cycle frequency.
228*0Sstevel@tonic-gate 		 */
229*0Sstevel@tonic-gate 		if ((defp = defread("CYCLE_FREQUENCY=")) != NULL) {
230*0Sstevel@tonic-gate 			cycle_frequency = atol(defp);
231*0Sstevel@tonic-gate 			if (debug > 0) {
232*0Sstevel@tonic-gate 				(void) printf("cycle_frequency=%ld\n",
233*0Sstevel@tonic-gate 					cycle_frequency);
234*0Sstevel@tonic-gate 			}
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 		/*
237*0Sstevel@tonic-gate 		 * defopen of NULL closes the open defaults file.
238*0Sstevel@tonic-gate 		 */
239*0Sstevel@tonic-gate 		(void) defopen((char *)NULL);
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	if (Umask > ((mode_t)0777))
243*0Sstevel@tonic-gate 		Umask = NFSLOG_UMASK;
244*0Sstevel@tonic-gate 	(void) umask(Umask);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if (getrlimit(RLIMIT_FSIZE, &rl) < 0) {
247*0Sstevel@tonic-gate 		error = errno;
248*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
249*0Sstevel@tonic-gate 			"getrlimit failed error is %d - %s\n"),
250*0Sstevel@tonic-gate 			error, strerror(error));
251*0Sstevel@tonic-gate 		exit(1);
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 	if (min_size < 0 || min_size > rl.rlim_cur) {
254*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
255*0Sstevel@tonic-gate 			"MIN_PROCESSING_SIZE out of range, should be >= 0 and "
256*0Sstevel@tonic-gate 			"< %d. Check %s.\n"), rl.rlim_cur, NFSLOG_OPTIONS_FILE);
257*0Sstevel@tonic-gate 		exit(1);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 	if (idle_time > INT_MAX) {
260*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
261*0Sstevel@tonic-gate 			"IDLE_TIME out of range, should be >= 0 and "
262*0Sstevel@tonic-gate 			"< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE);
263*0Sstevel@tonic-gate 		exit(1);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 	if (max_logs_preserve < 0 || max_logs_preserve > INT_MAX) {
266*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
267*0Sstevel@tonic-gate 			"MAX_LOGS_PRESERVE out of range, should be >= 0 and "
268*0Sstevel@tonic-gate 			"< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE);
269*0Sstevel@tonic-gate 		exit(1);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 	if (mapping_update_interval < 0|| mapping_update_interval > INT_MAX) {
272*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
273*0Sstevel@tonic-gate 			"MAPPING_UPDATE_INTERVAL out of range, "
274*0Sstevel@tonic-gate 			"should be >= 0 and "
275*0Sstevel@tonic-gate 			"< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE);
276*0Sstevel@tonic-gate 		exit(1);
277*0Sstevel@tonic-gate 	}
278*0Sstevel@tonic-gate 	if (cycle_frequency < 0 || cycle_frequency > INT_MAX) {
279*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
280*0Sstevel@tonic-gate 			"CYCLE_FREQUENCY out of range, should be >= 0 and "
281*0Sstevel@tonic-gate 			"< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE);
282*0Sstevel@tonic-gate 		exit(1);
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 	/* get value in seconds */
285*0Sstevel@tonic-gate 	cycle_frequency = cycle_frequency * 60 * 60;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	/*
288*0Sstevel@tonic-gate 	 * If we dump core, it will be /core
289*0Sstevel@tonic-gate 	 */
290*0Sstevel@tonic-gate 	if (chdir("/") < 0)
291*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("chdir /: %s"), strerror(errno));
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	/*
294*0Sstevel@tonic-gate 	 * Config errors to stderr
295*0Sstevel@tonic-gate 	 */
296*0Sstevel@tonic-gate 	nfsl_errs_to_syslog = B_FALSE;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate #ifndef DEBUG
299*0Sstevel@tonic-gate 	pid = fork();
300*0Sstevel@tonic-gate 	if (pid == -1) {
301*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: fork failure\n"),
302*0Sstevel@tonic-gate 			argv[0]);
303*0Sstevel@tonic-gate 		exit(1);
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 	if (pid != 0)
306*0Sstevel@tonic-gate 		exit(0);
307*0Sstevel@tonic-gate 	/*
308*0Sstevel@tonic-gate 	 * Config errors to syslog
309*0Sstevel@tonic-gate 	 */
310*0Sstevel@tonic-gate 	nfsl_errs_to_syslog = B_TRUE;
311*0Sstevel@tonic-gate #endif /* DEBUG */
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
314*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
315*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
316*0Sstevel@tonic-gate #endif
317*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	/*
320*0Sstevel@tonic-gate 	 * Check to see if nfslogd is already running.
321*0Sstevel@tonic-gate 	 */
322*0Sstevel@tonic-gate 	if (process_pidfile(PID_STARTUP) != 0) {
323*0Sstevel@tonic-gate 		exit(1);
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	(void) sigset(SIGUSR1, (void (*)(int))enable_logcycling);
327*0Sstevel@tonic-gate 	(void) sigset(SIGHUP, (void (*)(int))full_cleanup);
328*0Sstevel@tonic-gate 	(void) sigset(SIGTERM, (void(*)(int))short_cleanup);
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate #ifndef DEBUG
331*0Sstevel@tonic-gate 	/*
332*0Sstevel@tonic-gate 	 * Close existing file descriptors, open "/dev/null" as
333*0Sstevel@tonic-gate 	 * standard input, output, and error, and detach from
334*0Sstevel@tonic-gate 	 * controlling terminal.
335*0Sstevel@tonic-gate 	 */
336*0Sstevel@tonic-gate 	if (!debug && !test) {
337*0Sstevel@tonic-gate 		closefrom(0);
338*0Sstevel@tonic-gate 		(void) open("/dev/null", O_RDONLY);
339*0Sstevel@tonic-gate 		(void) open("/dev/null", O_WRONLY);
340*0Sstevel@tonic-gate 		(void) dup(1);
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 	(void) setsid();
343*0Sstevel@tonic-gate #endif /* DEBUG */
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	openlog(argv[0], LOG_PID, LOG_DAEMON);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	public_fh.fh_len = NFS_FHMAXDATA;
348*0Sstevel@tonic-gate 	public_fh.fh_xlen = NFS_FHMAXDATA;
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	/*
351*0Sstevel@tonic-gate 	 * Call once at startup to handle the nfslogtab
352*0Sstevel@tonic-gate 	 */
353*0Sstevel@tonic-gate 	if (nfslogtab_deactivate_after_boot() == -1)
354*0Sstevel@tonic-gate 		exit(1);
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	/*
357*0Sstevel@tonic-gate 	 * Get a list of buffers that need to be processed.
358*0Sstevel@tonic-gate 	 */
359*0Sstevel@tonic-gate 	if (error = getbuffer_list(&buffer_list, &logtab_update)) {
360*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("Could not read %s: %s"),
361*0Sstevel@tonic-gate 			NFSLOGTAB, strerror(error));
362*0Sstevel@tonic-gate 		goto done;
363*0Sstevel@tonic-gate 	}
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	/*
366*0Sstevel@tonic-gate 	 * Get the configuration list.
367*0Sstevel@tonic-gate 	 */
368*0Sstevel@tonic-gate 	if (error = nfsl_getconfig_list(&config_list)) {
369*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
370*0Sstevel@tonic-gate 			"Could not obtain configuration list: %s"),
371*0Sstevel@tonic-gate 			strerror(error));
372*0Sstevel@tonic-gate 		goto done;
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	/*
376*0Sstevel@tonic-gate 	 * loop to process the work being generated by the NFS server
377*0Sstevel@tonic-gate 	 */
378*0Sstevel@tonic-gate 	while (keep_running) {
379*0Sstevel@tonic-gate 		buffers_processed = 0;
380*0Sstevel@tonic-gate 		(void) checkbuffer_list(&buffer_list, &logtab_update);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 		while (buffer_list == NULL) {
383*0Sstevel@tonic-gate 			/*
384*0Sstevel@tonic-gate 			 * Nothing to do
385*0Sstevel@tonic-gate 			 */
386*0Sstevel@tonic-gate 			(void) sleep(idle_time);
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 			if (!keep_running) {
389*0Sstevel@tonic-gate 				/*
390*0Sstevel@tonic-gate 				 * We have been interrupted and asked to
391*0Sstevel@tonic-gate 				 * flush our transactions and exit.
392*0Sstevel@tonic-gate 				 */
393*0Sstevel@tonic-gate 				close_all_translogs(config_list);
394*0Sstevel@tonic-gate 				goto done;
395*0Sstevel@tonic-gate 			}
396*0Sstevel@tonic-gate 			(void) checkbuffer_list(&buffer_list, &logtab_update);
397*0Sstevel@tonic-gate 		}
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 		process_start = time(0);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		if (error = nfsl_checkconfig_list(&config_list, NULL)) {
402*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
403*0Sstevel@tonic-gate 				"Could not update configuration list: %s"),
404*0Sstevel@tonic-gate 				strerror(error));
405*0Sstevel@tonic-gate 			nfsl_freeconfig_list(&config_list);
406*0Sstevel@tonic-gate 			goto done;
407*0Sstevel@tonic-gate 		}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 		if (difftime(time(0), last_cycle) > cycle_frequency)
410*0Sstevel@tonic-gate 			need2cycle = TRUE;
411*0Sstevel@tonic-gate 		if (need2cycle) {
412*0Sstevel@tonic-gate 			error = cycle_logs(config_list, max_logs_preserve);
413*0Sstevel@tonic-gate 			if (error) {
414*0Sstevel@tonic-gate 				syslog(LOG_WARNING, gettext(
415*0Sstevel@tonic-gate 				    "One or more logfiles couldn't be cycled, "
416*0Sstevel@tonic-gate 				    "continuing regular processing"));
417*0Sstevel@tonic-gate 			}
418*0Sstevel@tonic-gate 			need2cycle = FALSE;
419*0Sstevel@tonic-gate 			last_cycle = time(0);
420*0Sstevel@tonic-gate 		}
421*0Sstevel@tonic-gate 		if (difftime(time(0), last_prune) > prune_frequency)
422*0Sstevel@tonic-gate 			need2prune = TRUE;
423*0Sstevel@tonic-gate 		if (need2prune || fhtable_to_prune) {
424*0Sstevel@tonic-gate 			error = prune_dbs(fhtable_to_prune);
425*0Sstevel@tonic-gate 			if (error) {
426*0Sstevel@tonic-gate 				syslog(LOG_WARNING, gettext(
427*0Sstevel@tonic-gate 				    "Error in cleaning database files"));
428*0Sstevel@tonic-gate 			}
429*0Sstevel@tonic-gate 			need2prune = FALSE;
430*0Sstevel@tonic-gate 			last_prune = time(0);
431*0Sstevel@tonic-gate 			/* After the first time, use the normal procedure */
432*0Sstevel@tonic-gate 			free(fhtable_to_prune);
433*0Sstevel@tonic-gate 			fhtable_to_prune = NULL;
434*0Sstevel@tonic-gate 		}
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 		for (bep = buffer_list; bep != NULL; bep = next) {
437*0Sstevel@tonic-gate 			next = bep->be_next;
438*0Sstevel@tonic-gate 			processed = 0;
439*0Sstevel@tonic-gate 			error = process_buffer(bep, &config_list,
440*0Sstevel@tonic-gate 				min_size, idle_time, &processed);
441*0Sstevel@tonic-gate 			if (error == 0 && processed) {
442*0Sstevel@tonic-gate 				if (bep->be_error) {
443*0Sstevel@tonic-gate 					syslog(LOG_ERR, gettext(
444*0Sstevel@tonic-gate 						"Buffer file '%s' "
445*0Sstevel@tonic-gate 						"processed successfully."),
446*0Sstevel@tonic-gate 						bep->be_name);
447*0Sstevel@tonic-gate 				}
448*0Sstevel@tonic-gate 				error =
449*0Sstevel@tonic-gate 				nfslogtab_remove(&buffer_list, &bep, B_FALSE);
450*0Sstevel@tonic-gate 			} else if (error == ENOENT) {
451*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("Removed entry"
452*0Sstevel@tonic-gate 					"\t\"%s\t%s\t%d\" from %s"),
453*0Sstevel@tonic-gate 					bep->be_name,
454*0Sstevel@tonic-gate 					bep->be_sharepnt->se_name,
455*0Sstevel@tonic-gate 					bep->be_sharepnt->se_state,
456*0Sstevel@tonic-gate 					NFSLOGTAB);
457*0Sstevel@tonic-gate 				error =
458*0Sstevel@tonic-gate 				nfslogtab_remove(&buffer_list, &bep, B_TRUE);
459*0Sstevel@tonic-gate 			} else if (error && error != bep->be_error) {
460*0Sstevel@tonic-gate 				/*
461*0Sstevel@tonic-gate 				 * An error different from what we've reported
462*0Sstevel@tonic-gate 				 * before occured.
463*0Sstevel@tonic-gate 				 */
464*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
465*0Sstevel@tonic-gate 					"Cannot process buffer file '%s' - "
466*0Sstevel@tonic-gate 					"will retry on every iteration."),
467*0Sstevel@tonic-gate 					bep->be_name);
468*0Sstevel@tonic-gate 			}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 			if (bep != NULL)
471*0Sstevel@tonic-gate 				bep->be_error = error;
472*0Sstevel@tonic-gate 			buffers_processed += processed;
473*0Sstevel@tonic-gate 		}
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 		transactions_timeout(config_list);
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 		if (keep_running) {
478*0Sstevel@tonic-gate 			uint_t process_time;
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 			/*
481*0Sstevel@tonic-gate 			 * Sleep idle_time minus however long it took us
482*0Sstevel@tonic-gate 			 * to process the buffers.
483*0Sstevel@tonic-gate 			 */
484*0Sstevel@tonic-gate 			process_time =
485*0Sstevel@tonic-gate 				(uint_t)(difftime(time(0), process_start));
486*0Sstevel@tonic-gate 			if (process_time < idle_time)
487*0Sstevel@tonic-gate 				(void) sleep(idle_time - process_time);
488*0Sstevel@tonic-gate 		}
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate done:
492*0Sstevel@tonic-gate 	/*
493*0Sstevel@tonic-gate 	 * Make sure to clean house before we exit
494*0Sstevel@tonic-gate 	 */
495*0Sstevel@tonic-gate 	close_all_translogs(config_list);
496*0Sstevel@tonic-gate 	free_buffer_list(&buffer_list);
497*0Sstevel@tonic-gate 	nfsl_freeconfig_list(&config_list);
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	(void) process_pidfile(PID_SHUTDOWN);
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	return (error);
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate static void
short_cleanup(void)505*0Sstevel@tonic-gate short_cleanup(void)
506*0Sstevel@tonic-gate {
507*0Sstevel@tonic-gate 	if (debug) {
508*0Sstevel@tonic-gate 		(void) fprintf(stderr,
509*0Sstevel@tonic-gate 			"SIGTERM received, setting state to terminate...\n");
510*0Sstevel@tonic-gate 	}
511*0Sstevel@tonic-gate 	quick_cleaning = B_TRUE;
512*0Sstevel@tonic-gate 	keep_running = B_FALSE;
513*0Sstevel@tonic-gate }
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate static void
full_cleanup(void)516*0Sstevel@tonic-gate full_cleanup(void)
517*0Sstevel@tonic-gate {
518*0Sstevel@tonic-gate 	if (debug) {
519*0Sstevel@tonic-gate 		(void) fprintf(stderr,
520*0Sstevel@tonic-gate 			"SIGHUP received, setting state to shutdown...\n");
521*0Sstevel@tonic-gate 	}
522*0Sstevel@tonic-gate 	quick_cleaning = keep_running = B_FALSE;
523*0Sstevel@tonic-gate }
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate /*
526*0Sstevel@tonic-gate  * Removes nfslogtab entries matching the specified buffer_ent,
527*0Sstevel@tonic-gate  * if 'inactive_only' is set, then only inactive entries are removed.
528*0Sstevel@tonic-gate  * The buffer_list and sharepoint list entries are removed appropriately.
529*0Sstevel@tonic-gate  * Returns 0 on success, error otherwise.
530*0Sstevel@tonic-gate  */
531*0Sstevel@tonic-gate static int
nfslogtab_remove(struct buffer_ent ** buffer_list,struct buffer_ent ** bep,boolean_t allstates)532*0Sstevel@tonic-gate nfslogtab_remove(
533*0Sstevel@tonic-gate 	struct buffer_ent **buffer_list,
534*0Sstevel@tonic-gate 	struct buffer_ent **bep,
535*0Sstevel@tonic-gate 	boolean_t allstates)
536*0Sstevel@tonic-gate {
537*0Sstevel@tonic-gate 	FILE *fd;
538*0Sstevel@tonic-gate 	int error = 0;
539*0Sstevel@tonic-gate 	struct sharepnt_ent *sep, *next;
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	fd = fopen(NFSLOGTAB, "r+");
542*0Sstevel@tonic-gate 	rewind(fd);
543*0Sstevel@tonic-gate 	if (fd == NULL) {
544*0Sstevel@tonic-gate 		error = errno;
545*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("%s - %s\n"), NFSLOGTAB,
546*0Sstevel@tonic-gate 			strerror(error));
547*0Sstevel@tonic-gate 		return (error);
548*0Sstevel@tonic-gate 	}
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	if (lockf(fileno(fd), F_LOCK, 0L) < 0) {
551*0Sstevel@tonic-gate 		error = errno;
552*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("cannot lock %s - %s\n"), NFSLOGTAB,
553*0Sstevel@tonic-gate 			strerror(error));
554*0Sstevel@tonic-gate 		(void) fclose(fd);
555*0Sstevel@tonic-gate 		return (error);
556*0Sstevel@tonic-gate 	}
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	for (sep = (*bep)->be_sharepnt; sep != NULL; sep = next) {
559*0Sstevel@tonic-gate 		next = sep->se_next;
560*0Sstevel@tonic-gate 		if (!allstates && sep->se_state == LES_ACTIVE)
561*0Sstevel@tonic-gate 			continue;
562*0Sstevel@tonic-gate 		if (error = logtab_rement(fd, (*bep)->be_name, sep->se_name,
563*0Sstevel@tonic-gate 					NULL, sep->se_state)) {
564*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("cannot update %s\n"),
565*0Sstevel@tonic-gate 				NFSLOGTAB);
566*0Sstevel@tonic-gate 			error = EIO;
567*0Sstevel@tonic-gate 			goto errout;
568*0Sstevel@tonic-gate 		}
569*0Sstevel@tonic-gate 		remove_sharepnt_ent(&((*bep)->be_sharepnt), sep);
570*0Sstevel@tonic-gate 	}
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	if ((*bep)->be_sharepnt == NULL) {
573*0Sstevel@tonic-gate 		/*
574*0Sstevel@tonic-gate 		 * All sharepoints were removed from NFSLOGTAB.
575*0Sstevel@tonic-gate 		 * Remove this buffer from our list.
576*0Sstevel@tonic-gate 		 */
577*0Sstevel@tonic-gate 		remove_buffer_ent(buffer_list, *bep);
578*0Sstevel@tonic-gate 		*bep = NULL;
579*0Sstevel@tonic-gate 	}
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate errout: (void) fclose(fd);
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	return (error);
584*0Sstevel@tonic-gate }
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate /*
587*0Sstevel@tonic-gate  * Deactivates entries if nfslogtab is older than the boot time.
588*0Sstevel@tonic-gate  */
589*0Sstevel@tonic-gate static int
nfslogtab_deactivate_after_boot(void)590*0Sstevel@tonic-gate nfslogtab_deactivate_after_boot(void)
591*0Sstevel@tonic-gate {
592*0Sstevel@tonic-gate 	FILE *fd;
593*0Sstevel@tonic-gate 	int error = 0;
594*0Sstevel@tonic-gate 
595*0Sstevel@tonic-gate 	fd = fopen(NFSLOGTAB, "r+");
596*0Sstevel@tonic-gate 	if (fd == NULL) {
597*0Sstevel@tonic-gate 		error = errno;
598*0Sstevel@tonic-gate 		if (error != ENOENT) {
599*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("%s: %s\n"), NFSLOGTAB,
600*0Sstevel@tonic-gate 				strerror(error));
601*0Sstevel@tonic-gate 			return (-1);
602*0Sstevel@tonic-gate 		}
603*0Sstevel@tonic-gate 		return (0);
604*0Sstevel@tonic-gate 	}
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	if (lockf(fileno(fd), F_LOCK, 0L) < 0) {
607*0Sstevel@tonic-gate 		error = errno;
608*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("cannot lock %s: %s\n"),
609*0Sstevel@tonic-gate 			NFSLOGTAB, strerror(error));
610*0Sstevel@tonic-gate 		(void) fclose(fd);
611*0Sstevel@tonic-gate 		return (-1);
612*0Sstevel@tonic-gate 	}
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	if (logtab_deactivate_after_boot(fd) == -1) {
615*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
616*0Sstevel@tonic-gate 			"Cannot deactivate all entries in %s\n"), NFSLOGTAB);
617*0Sstevel@tonic-gate 		(void) fclose(fd);
618*0Sstevel@tonic-gate 		return (-1);
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	(void) fclose(fd);
622*0Sstevel@tonic-gate 	return (0);
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate /*
626*0Sstevel@tonic-gate  * Enables the log file cycling flag.
627*0Sstevel@tonic-gate  */
628*0Sstevel@tonic-gate static void
enable_logcycling(void)629*0Sstevel@tonic-gate enable_logcycling(void)
630*0Sstevel@tonic-gate {
631*0Sstevel@tonic-gate 	need2cycle = TRUE;
632*0Sstevel@tonic-gate }
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate /*
635*0Sstevel@tonic-gate  * Cycle all log files that have been active since the last cycling.
636*0Sstevel@tonic-gate  * This means it's not simply listed in the configuration file, but
637*0Sstevel@tonic-gate  * there's information associated with it.
638*0Sstevel@tonic-gate  */
639*0Sstevel@tonic-gate static int
cycle_logs(nfsl_config_t * listp,int max_logs_preserve)640*0Sstevel@tonic-gate cycle_logs(nfsl_config_t *listp, int max_logs_preserve)
641*0Sstevel@tonic-gate {
642*0Sstevel@tonic-gate 	nfsl_config_t *clp;
643*0Sstevel@tonic-gate 	void *processed_list = NULL;
644*0Sstevel@tonic-gate 	int error = 0, total_errors = 0;
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	for (clp = listp; clp != NULL; clp = clp->nc_next) {
647*0Sstevel@tonic-gate 		error = 0;
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 		/*
650*0Sstevel@tonic-gate 		 * Process transpath log.
651*0Sstevel@tonic-gate 		 */
652*0Sstevel@tonic-gate 		if (clp->nc_logpath) {
653*0Sstevel@tonic-gate 			if (is_cycle_needed(clp->nc_logpath, &processed_list,
654*0Sstevel@tonic-gate 			    B_FALSE, &error)) {
655*0Sstevel@tonic-gate 				if (clp->nc_transcookie != NULL) {
656*0Sstevel@tonic-gate 					nfslog_close_transactions(
657*0Sstevel@tonic-gate 						&clp->nc_transcookie);
658*0Sstevel@tonic-gate 					assert(clp->nc_transcookie == NULL);
659*0Sstevel@tonic-gate 				}
660*0Sstevel@tonic-gate 				error = cycle_log(clp->nc_logpath,
661*0Sstevel@tonic-gate 					max_logs_preserve);
662*0Sstevel@tonic-gate 			} else if (error)
663*0Sstevel@tonic-gate 				goto errout;
664*0Sstevel@tonic-gate 		}
665*0Sstevel@tonic-gate 		total_errors += error;
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 		/*
668*0Sstevel@tonic-gate 		 * Process elfpath log.
669*0Sstevel@tonic-gate 		 */
670*0Sstevel@tonic-gate 		if (clp->nc_rpclogpath) {
671*0Sstevel@tonic-gate 			if (is_cycle_needed(clp->nc_rpclogpath, &processed_list,
672*0Sstevel@tonic-gate 			    B_FALSE, &error)) {
673*0Sstevel@tonic-gate 				error = cycle_log(clp->nc_rpclogpath,
674*0Sstevel@tonic-gate 					max_logs_preserve);
675*0Sstevel@tonic-gate 			} else if (error)
676*0Sstevel@tonic-gate 				goto errout;
677*0Sstevel@tonic-gate 		}
678*0Sstevel@tonic-gate 		total_errors += error;
679*0Sstevel@tonic-gate 	}
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate errout:
682*0Sstevel@tonic-gate 	/*
683*0Sstevel@tonic-gate 	 * Free the list of processed entries.
684*0Sstevel@tonic-gate 	 */
685*0Sstevel@tonic-gate 	(void) is_cycle_needed(NULL, &processed_list, B_TRUE, &error);
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	return (total_errors);
688*0Sstevel@tonic-gate }
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate /*
691*0Sstevel@tonic-gate  * Returns TRUE if this log has not yet been cycled, FALSE otherwise.
692*0Sstevel@tonic-gate  * '*head' points to the list of entries that have been processed.
693*0Sstevel@tonic-gate  * If this is a new entry, it gets inserted at the beginning of the
694*0Sstevel@tonic-gate  * list, and returns TRUE.
695*0Sstevel@tonic-gate  *
696*0Sstevel@tonic-gate  * The list is freed if 'need2free' is set, and returns FALSE.
697*0Sstevel@tonic-gate  * Sets 'error' on failure, and returns FALSE.
698*0Sstevel@tonic-gate  */
699*0Sstevel@tonic-gate static boolean_t
is_cycle_needed(char * path,void ** list,boolean_t need2free,int * error)700*0Sstevel@tonic-gate is_cycle_needed(char *path, void **list, boolean_t need2free, int *error)
701*0Sstevel@tonic-gate {
702*0Sstevel@tonic-gate 	struct list {
703*0Sstevel@tonic-gate 		char *log;
704*0Sstevel@tonic-gate 		struct list *next;
705*0Sstevel@tonic-gate 	} *head, *next, *p;
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	head = (struct list *)(*list);
708*0Sstevel@tonic-gate 	if (need2free) {
709*0Sstevel@tonic-gate 		/*
710*0Sstevel@tonic-gate 		 * Free the list and return
711*0Sstevel@tonic-gate 		 */
712*0Sstevel@tonic-gate 		for (p = head; p != NULL; p = next) {
713*0Sstevel@tonic-gate 			next = p->next;
714*0Sstevel@tonic-gate 			free(p);
715*0Sstevel@tonic-gate 		}
716*0Sstevel@tonic-gate 		head = NULL;
717*0Sstevel@tonic-gate 		return (B_FALSE);
718*0Sstevel@tonic-gate 	}
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	assert(path != NULL);
721*0Sstevel@tonic-gate 	*error = 0;
722*0Sstevel@tonic-gate 	for (p = head; p != NULL; p = p->next) {
723*0Sstevel@tonic-gate 		/*
724*0Sstevel@tonic-gate 		 * Have we seen this before?
725*0Sstevel@tonic-gate 		 */
726*0Sstevel@tonic-gate 		if (strcmp(p->log, path) == 0)
727*0Sstevel@tonic-gate 			return (B_FALSE);
728*0Sstevel@tonic-gate 	}
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	/*
731*0Sstevel@tonic-gate 	 * Add it to the list
732*0Sstevel@tonic-gate 	 */
733*0Sstevel@tonic-gate 	if ((p = (struct list *)malloc(sizeof (*p))) == NULL) {
734*0Sstevel@tonic-gate 		*error = ENOMEM;
735*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("Cannot allocate memory."));
736*0Sstevel@tonic-gate 		return (B_FALSE);
737*0Sstevel@tonic-gate 	}
738*0Sstevel@tonic-gate 	p->log = path;
739*0Sstevel@tonic-gate 	p->next = head;
740*0Sstevel@tonic-gate 	head = p;
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	return (B_TRUE);
743*0Sstevel@tonic-gate }
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate /*
746*0Sstevel@tonic-gate  * cycle given log file.
747*0Sstevel@tonic-gate  */
748*0Sstevel@tonic-gate int
cycle_log(char * filename,int max_logs_preserve)749*0Sstevel@tonic-gate cycle_log(char *filename, int max_logs_preserve)
750*0Sstevel@tonic-gate {
751*0Sstevel@tonic-gate 	int i;
752*0Sstevel@tonic-gate 	char *file_1;
753*0Sstevel@tonic-gate 	char *file_2;
754*0Sstevel@tonic-gate 	int error = 0;
755*0Sstevel@tonic-gate 	struct stat st;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	if (max_logs_preserve == 0)
758*0Sstevel@tonic-gate 		return (0);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	if (stat(filename, &st) == -1) {
761*0Sstevel@tonic-gate 		if (errno == ENOENT) {
762*0Sstevel@tonic-gate 			/*
763*0Sstevel@tonic-gate 			 * Nothing to cycle.
764*0Sstevel@tonic-gate 			 */
765*0Sstevel@tonic-gate 			return (0);
766*0Sstevel@tonic-gate 		}
767*0Sstevel@tonic-gate 		return (errno);
768*0Sstevel@tonic-gate 	}
769*0Sstevel@tonic-gate 	file_1 = (char *)malloc(PATH_MAX);
770*0Sstevel@tonic-gate 	file_2 = (char *)malloc(PATH_MAX);
771*0Sstevel@tonic-gate 	for (i = max_logs_preserve - 2; i >= 0; i--) {
772*0Sstevel@tonic-gate 		(void) sprintf(file_1, "%s.%d", filename, i);
773*0Sstevel@tonic-gate 		(void) sprintf(file_2, "%s.%d", filename, (i + 1));
774*0Sstevel@tonic-gate 		if (rename(file_1, file_2) == -1) {
775*0Sstevel@tonic-gate 			error = errno;
776*0Sstevel@tonic-gate 			if (error != ENOENT) {
777*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
778*0Sstevel@tonic-gate 				    "cycle_log: can not rename %s to %s: %s"),
779*0Sstevel@tonic-gate 				    file_1, file_2, strerror(error));
780*0Sstevel@tonic-gate 				goto out;
781*0Sstevel@tonic-gate 			}
782*0Sstevel@tonic-gate 		}
783*0Sstevel@tonic-gate 	}
784*0Sstevel@tonic-gate 	(void) sprintf(file_1, "%s.0", filename);
785*0Sstevel@tonic-gate 	if (rename(filename, file_1) == -1) {
786*0Sstevel@tonic-gate 		error = errno;
787*0Sstevel@tonic-gate 		if (error != ENOENT) {
788*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
789*0Sstevel@tonic-gate 				"cycle_log: can not rename %s to %s: %s"),
790*0Sstevel@tonic-gate 				filename, file_1, strerror(error));
791*0Sstevel@tonic-gate 			goto out;
792*0Sstevel@tonic-gate 		}
793*0Sstevel@tonic-gate 	}
794*0Sstevel@tonic-gate 	error = 0;
795*0Sstevel@tonic-gate 
796*0Sstevel@tonic-gate out:	free(file_1);
797*0Sstevel@tonic-gate 	free(file_2);
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 	return (error);
800*0Sstevel@tonic-gate }
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate /*
803*0Sstevel@tonic-gate  * If operation = PID_STARTUP then checks the nfslogd.pid file, it is opened
804*0Sstevel@tonic-gate  * if it exists, read and the pid is checked for an active process. If no
805*0Sstevel@tonic-gate  * active process is found, the pid of this process is written to the file,
806*0Sstevel@tonic-gate  * and 0 is returned, otherwise non-zero error is returned.
807*0Sstevel@tonic-gate  *
808*0Sstevel@tonic-gate  * If operation = PID_SHUTDOWN then removes the nfslogd.pid file and 0 is
809*0Sstevel@tonic-gate  * returned.
810*0Sstevel@tonic-gate  */
811*0Sstevel@tonic-gate static int
process_pidfile(enum pidfile_operation op)812*0Sstevel@tonic-gate process_pidfile(enum pidfile_operation op)
813*0Sstevel@tonic-gate {
814*0Sstevel@tonic-gate 	int fd, read_count;
815*0Sstevel@tonic-gate 	int error = 0;
816*0Sstevel@tonic-gate 	pid_t pid, mypid;
817*0Sstevel@tonic-gate 	char *PidFile = NFSLOGD_PIDFILE;
818*0Sstevel@tonic-gate 	int open_flags;
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	if (op == PID_STARTUP)
821*0Sstevel@tonic-gate 		open_flags = O_RDWR | O_CREAT;
822*0Sstevel@tonic-gate 	else {
823*0Sstevel@tonic-gate 		assert(op == PID_SHUTDOWN);
824*0Sstevel@tonic-gate 		open_flags = O_RDWR;
825*0Sstevel@tonic-gate 	}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 	if ((fd = open(PidFile, open_flags, 0600)) < 0) {
828*0Sstevel@tonic-gate 		error = errno;
829*0Sstevel@tonic-gate 		if (error == ENOENT && op == PID_SHUTDOWN) {
830*0Sstevel@tonic-gate 			/*
831*0Sstevel@tonic-gate 			 * We were going to remove it anyway
832*0Sstevel@tonic-gate 			 */
833*0Sstevel@tonic-gate 			error = 0;
834*0Sstevel@tonic-gate 			goto out;
835*0Sstevel@tonic-gate 		}
836*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
837*0Sstevel@tonic-gate 			"cannot open or create pid file %s\n"), PidFile);
838*0Sstevel@tonic-gate 		goto out;
839*0Sstevel@tonic-gate 	}
840*0Sstevel@tonic-gate 	if (lockf(fd, F_LOCK, 0) < 0) {
841*0Sstevel@tonic-gate 		error = errno;
842*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
843*0Sstevel@tonic-gate 			"Cannot lock %s - %s\n"), PidFile,
844*0Sstevel@tonic-gate 			strerror(error));
845*0Sstevel@tonic-gate 		goto out;
846*0Sstevel@tonic-gate 	}
847*0Sstevel@tonic-gate 	if ((read_count = read(fd, &pid, sizeof (pid))) < 0) {
848*0Sstevel@tonic-gate 		error = errno;
849*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
850*0Sstevel@tonic-gate 			"Can not read from file %s - %s\n"), PidFile,
851*0Sstevel@tonic-gate 			strerror(error));
852*0Sstevel@tonic-gate 	}
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate 	mypid = getpid();
855*0Sstevel@tonic-gate 	if (op == PID_STARTUP) {
856*0Sstevel@tonic-gate 		if (read_count > 0) {
857*0Sstevel@tonic-gate 			if (kill(pid, 0) == 0) {
858*0Sstevel@tonic-gate 				error = EEXIST;
859*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
860*0Sstevel@tonic-gate 					"Terminated - nfslogd(%ld) already "
861*0Sstevel@tonic-gate 					"running.\n"), pid);
862*0Sstevel@tonic-gate 				goto out;
863*0Sstevel@tonic-gate 			} else if (errno != ESRCH) {
864*0Sstevel@tonic-gate 				error = errno;
865*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
866*0Sstevel@tonic-gate 					"Unexpected error returned %s\n"),
867*0Sstevel@tonic-gate 					strerror(error));
868*0Sstevel@tonic-gate 				goto out;
869*0Sstevel@tonic-gate 			}
870*0Sstevel@tonic-gate 		}
871*0Sstevel@tonic-gate 		pid = mypid;
872*0Sstevel@tonic-gate 		/*
873*0Sstevel@tonic-gate 		 * rewind the file to overwrite old pid
874*0Sstevel@tonic-gate 		 */
875*0Sstevel@tonic-gate 		(void) lseek(fd, 0, SEEK_SET);
876*0Sstevel@tonic-gate 		if (write(fd, &mypid, sizeof (mypid)) < 0) {
877*0Sstevel@tonic-gate 			error = errno;
878*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
879*0Sstevel@tonic-gate 				"Cannot update %s: %s\n"),
880*0Sstevel@tonic-gate 				PidFile, strerror(error));
881*0Sstevel@tonic-gate 		}
882*0Sstevel@tonic-gate 	} else {
883*0Sstevel@tonic-gate 		assert(pid == mypid);
884*0Sstevel@tonic-gate 		if (unlink(PidFile)) {
885*0Sstevel@tonic-gate 			error = errno;
886*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("Cannot remove %s: %s"),
887*0Sstevel@tonic-gate 				strerror(error));
888*0Sstevel@tonic-gate 		}
889*0Sstevel@tonic-gate 	}
890*0Sstevel@tonic-gate out:
891*0Sstevel@tonic-gate 	if (fd >= 0)
892*0Sstevel@tonic-gate 		(void) close(fd);
893*0Sstevel@tonic-gate 	return (error);
894*0Sstevel@tonic-gate }
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate /*
897*0Sstevel@tonic-gate  * Forces a timeout on all open transactions.
898*0Sstevel@tonic-gate  */
899*0Sstevel@tonic-gate static void
transactions_timeout(nfsl_config_t * clp)900*0Sstevel@tonic-gate transactions_timeout(nfsl_config_t *clp)
901*0Sstevel@tonic-gate {
902*0Sstevel@tonic-gate 	for (; clp != NULL; clp = clp->nc_next) {
903*0Sstevel@tonic-gate 		if (clp->nc_transcookie != NULL) {
904*0Sstevel@tonic-gate 			nfslog_process_trans_timeout(
905*0Sstevel@tonic-gate 			    (struct nfslog_trans_file *)clp->nc_transcookie,
906*0Sstevel@tonic-gate 			    FALSE);
907*0Sstevel@tonic-gate 		}
908*0Sstevel@tonic-gate 	}
909*0Sstevel@tonic-gate }
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate /*
912*0Sstevel@tonic-gate  * Closes all transaction logs causing outstanding transactions
913*0Sstevel@tonic-gate  * to be flushed to their respective log.
914*0Sstevel@tonic-gate  */
915*0Sstevel@tonic-gate static void
close_all_translogs(nfsl_config_t * clp)916*0Sstevel@tonic-gate close_all_translogs(nfsl_config_t *clp)
917*0Sstevel@tonic-gate {
918*0Sstevel@tonic-gate 	for (; clp != NULL; clp = clp->nc_next) {
919*0Sstevel@tonic-gate 		if (clp->nc_transcookie != NULL) {
920*0Sstevel@tonic-gate 			nfslog_close_transactions(&clp->nc_transcookie);
921*0Sstevel@tonic-gate 			assert(clp->nc_transcookie == NULL);
922*0Sstevel@tonic-gate 		}
923*0Sstevel@tonic-gate 	}
924*0Sstevel@tonic-gate }
925