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