17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*458cf4d6Sek  * Common Development and Distribution License (the "License").
6*458cf4d6Sek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*458cf4d6Sek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*458cf4d6Sek  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <strings.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <syslog.h>
297c478bd9Sstevel@tonic-gate #include <locale.h>
307c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
317c478bd9Sstevel@tonic-gate #include <nfs/export.h>
327c478bd9Sstevel@tonic-gate #include <nfs/nfssys.h>
337c478bd9Sstevel@tonic-gate #include <nfs/nfs_log.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <assert.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <nfs/nfs_log.h>
427c478bd9Sstevel@tonic-gate #include "../lib/nfslog_config.h"
437c478bd9Sstevel@tonic-gate #include "buffer_list.h"
447c478bd9Sstevel@tonic-gate #include "nfslogd.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate extern int _nfssys(int, void *);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * simple list used to keep track of bad tag messages syslogged.
507c478bd9Sstevel@tonic-gate  */
51*458cf4d6Sek struct nfs_log_list {
527c478bd9Sstevel@tonic-gate 	char *l_name;
53*458cf4d6Sek 	struct nfs_log_list *l_next;
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static void badtag_notify(char *tag);
57*458cf4d6Sek static struct nfs_log_list *badtag_list = NULL;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static void cleanup_elf_state(nfsl_config_t *);
607c478bd9Sstevel@tonic-gate static void cleanup_trans_state(nfsl_config_t *);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Read the contents of the 'bufferpath', process them and store the
647c478bd9Sstevel@tonic-gate  * user-readable log in 'elfpath', updating the 'fhpath' filehandle
657c478bd9Sstevel@tonic-gate  * table.
667c478bd9Sstevel@tonic-gate  * The contents of the configuration list (*config_list) may be
677c478bd9Sstevel@tonic-gate  * modified if the configuration file has been updated and we can not
687c478bd9Sstevel@tonic-gate  * find the configuration entry in the currently loaded list.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * Returns 0 on success and sets *buffer_processed to 1.
717c478bd9Sstevel@tonic-gate  *	   non zero error on failure and *buffer_processed set to 0.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate int
process_buffer(struct buffer_ent * bep,nfsl_config_t ** config_list,int min_size,int idle_time,int * buffer_processed)747c478bd9Sstevel@tonic-gate process_buffer(
757c478bd9Sstevel@tonic-gate 	struct buffer_ent *bep,
767c478bd9Sstevel@tonic-gate 	nfsl_config_t **config_list,
777c478bd9Sstevel@tonic-gate 	int min_size,
787c478bd9Sstevel@tonic-gate 	int idle_time,
797c478bd9Sstevel@tonic-gate 	int *buffer_processed)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	struct stat st;
827c478bd9Sstevel@tonic-gate 	struct nfsl_flush_args nfa;
837c478bd9Sstevel@tonic-gate 	struct nfslog_buf *lbp = NULL;
847c478bd9Sstevel@tonic-gate 	struct nfslog_lr *lrp;
857c478bd9Sstevel@tonic-gate 	char *path1 = NULL;
867c478bd9Sstevel@tonic-gate 	char *path2 = NULL;
877c478bd9Sstevel@tonic-gate 	char *buffer_inprog = NULL;
887c478bd9Sstevel@tonic-gate 	int buffer_inprog_len;
897c478bd9Sstevel@tonic-gate 	int error = 0;
907c478bd9Sstevel@tonic-gate 	nfsl_config_t *ncp = NULL, *last_good_ncp;
917c478bd9Sstevel@tonic-gate 	char *bufferpath = bep->be_name;
927c478bd9Sstevel@tonic-gate 	char *tag;
937c478bd9Sstevel@tonic-gate 	boolean_t elf_checked = B_FALSE;
947c478bd9Sstevel@tonic-gate 	boolean_t trans_checked = B_FALSE;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	assert(buffer_processed != NULL);
977c478bd9Sstevel@tonic-gate 	assert(bufferpath != NULL);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (stat(bufferpath, &st) == -1) {
1007c478bd9Sstevel@tonic-gate 		error = errno;
1017c478bd9Sstevel@tonic-gate 		if (error == ENOENT) {
1027c478bd9Sstevel@tonic-gate 			error = 0;
1037c478bd9Sstevel@tonic-gate 			buffer_inprog_len = strlen(bufferpath) +
104*458cf4d6Sek 			    strlen(LOG_INPROG_STRING) + 1;
1057c478bd9Sstevel@tonic-gate 			buffer_inprog = (char *)malloc(buffer_inprog_len);
1067c478bd9Sstevel@tonic-gate 			if (buffer_inprog == NULL) {
1077c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
108*458cf4d6Sek 				    "process_buffer: malloc failed"));
1097c478bd9Sstevel@tonic-gate 				return (ENOMEM);
1107c478bd9Sstevel@tonic-gate 			}
1117c478bd9Sstevel@tonic-gate 			(void) sprintf(buffer_inprog, "%s%s", bufferpath,
112*458cf4d6Sek 			    LOG_INPROG_STRING);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 			if (stat(buffer_inprog, &st) == -1) {
1157c478bd9Sstevel@tonic-gate 				error = errno;
1167c478bd9Sstevel@tonic-gate 				if (bep->be_error != error) {
1177c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, gettext(
118*458cf4d6Sek 					    "Can not stat %s: %s"),
119*458cf4d6Sek 					    buffer_inprog, strerror(error));
1207c478bd9Sstevel@tonic-gate 				}
1217c478bd9Sstevel@tonic-gate 				free(buffer_inprog);
1227c478bd9Sstevel@tonic-gate 				return (error);
1237c478bd9Sstevel@tonic-gate 			}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 			free(buffer_inprog);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 			/*
1287c478bd9Sstevel@tonic-gate 			 * Does the buffer in progress meet our minimum
1297c478bd9Sstevel@tonic-gate 			 * processing requirements? or has it been around
1307c478bd9Sstevel@tonic-gate 			 * longer than we're willing to wait for more
1317c478bd9Sstevel@tonic-gate 			 * data to be logged?
1327c478bd9Sstevel@tonic-gate 			 */
1337c478bd9Sstevel@tonic-gate 			if ((st.st_size < min_size) &&
1347c478bd9Sstevel@tonic-gate 			    ((time(0) - bep->be_lastprocessed) < idle_time)) {
1357c478bd9Sstevel@tonic-gate 				/*
1367c478bd9Sstevel@tonic-gate 				 * The buffer does not meet the minimum
1377c478bd9Sstevel@tonic-gate 				 * size processing requirements, and it has not
1387c478bd9Sstevel@tonic-gate 				 * been around longer than we're willing to
1397c478bd9Sstevel@tonic-gate 				 * wait for more data collection.
1407c478bd9Sstevel@tonic-gate 				 * We return now without processing it.
1417c478bd9Sstevel@tonic-gate 				 */
1427c478bd9Sstevel@tonic-gate 				return (0);
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 			/*
1467c478bd9Sstevel@tonic-gate 			 * Issue the LOG_FLUSH system call to flush the
1477c478bd9Sstevel@tonic-gate 			 * buffer and process it.
1487c478bd9Sstevel@tonic-gate 			 */
1497c478bd9Sstevel@tonic-gate 			(void) memset((void *)&nfa, 0, sizeof (nfa));
1507c478bd9Sstevel@tonic-gate 			nfa.version = NFSL_FLUSH_ARGS_VERS;
1517c478bd9Sstevel@tonic-gate 			nfa.directive = NFSL_RENAME | NFSL_SYNC;
1527c478bd9Sstevel@tonic-gate 			nfa.buff = bufferpath;
1537c478bd9Sstevel@tonic-gate 			nfa.buff_len = strlen(bufferpath) + 1;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 			if (_nfssys(LOG_FLUSH, &nfa) < 0) {
1567c478bd9Sstevel@tonic-gate 				error = errno;
1577c478bd9Sstevel@tonic-gate 				if (bep->be_error != error) {
1587c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, gettext(
159*458cf4d6Sek 					    "_nfssys(%s) failed: %s"),
160*458cf4d6Sek 					    nfa.buff, strerror(error));
1617c478bd9Sstevel@tonic-gate 				}
1627c478bd9Sstevel@tonic-gate 				return (error);
1637c478bd9Sstevel@tonic-gate 			}
1647c478bd9Sstevel@tonic-gate 		} else {
1657c478bd9Sstevel@tonic-gate 			if (bep->be_error != error) {
1667c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("Can not stat %s: %s"),
167*458cf4d6Sek 				    bufferpath, strerror(error));
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			return (error);
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Open and lock input buffer.
1757c478bd9Sstevel@tonic-gate 	 * Passes in the value of the last error so that it will not
1767c478bd9Sstevel@tonic-gate 	 * print it again if it is still hitting the same error condition.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	error = bep->be_error;
1797c478bd9Sstevel@tonic-gate 	if ((lbp = nfslog_open_buf(bufferpath, &error)) == NULL)
1807c478bd9Sstevel@tonic-gate 		goto done;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if ((ncp = last_good_ncp =
1837c478bd9Sstevel@tonic-gate 	    nfsl_findconfig(*config_list, "global", &error)) == NULL) {
1847c478bd9Sstevel@tonic-gate 		assert(error != 0);
1857c478bd9Sstevel@tonic-gate 		nfsl_freeconfig_list(config_list);
1867c478bd9Sstevel@tonic-gate 		if (error != bep->be_error) {
1877c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
188*458cf4d6Sek 			    "Could not search config list: %s"),
189*458cf4d6Sek 			    strerror(error));
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		goto done;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	assert(error == 0);
1957c478bd9Sstevel@tonic-gate 	while ((lrp = nfslog_get_logrecord(lbp)) != NULL && keep_running) {
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if (*buffer_processed == 0)
1987c478bd9Sstevel@tonic-gate 			(*buffer_processed)++;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * Get the matching config entry.
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		tag = lrp->log_record.re_tag;
2047c478bd9Sstevel@tonic-gate 		if (strcmp(tag, last_good_ncp->nc_name) != 0) {
2057c478bd9Sstevel@tonic-gate 			ncp = nfsl_findconfig(*config_list, tag, &error);
2067c478bd9Sstevel@tonic-gate 			if (error) {
2077c478bd9Sstevel@tonic-gate 				if (error != bep->be_error) {
2087c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, gettext(
209*458cf4d6Sek 					    "Could not search config list: %s"),
210*458cf4d6Sek 					    strerror(error));
2117c478bd9Sstevel@tonic-gate 				}
2127c478bd9Sstevel@tonic-gate 				nfsl_freeconfig_list(config_list);
2137c478bd9Sstevel@tonic-gate 				goto done;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			if (ncp == NULL) {
2167c478bd9Sstevel@tonic-gate 				badtag_notify(tag);
2177c478bd9Sstevel@tonic-gate 				ncp = last_good_ncp;
2187c478bd9Sstevel@tonic-gate 				goto skip;
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 			last_good_ncp = ncp;
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		if (ncp->nc_flags & NC_UPDATED) {
2247c478bd9Sstevel@tonic-gate 			/*
2257c478bd9Sstevel@tonic-gate 			 * The location of the log files may have changed,
2267c478bd9Sstevel@tonic-gate 			 * we need to close transactions and invalidate
2277c478bd9Sstevel@tonic-gate 			 * cookies so that the log files can be reopened
2287c478bd9Sstevel@tonic-gate 			 * further down.
2297c478bd9Sstevel@tonic-gate 			 */
2307c478bd9Sstevel@tonic-gate 			cleanup_elf_state(ncp);
2317c478bd9Sstevel@tonic-gate 			cleanup_trans_state(ncp);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 			ncp->nc_flags &= ~NC_UPDATED;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 			/*
2367c478bd9Sstevel@tonic-gate 			 * Force cookies to be recreated if necessary.
2377c478bd9Sstevel@tonic-gate 			 */
2387c478bd9Sstevel@tonic-gate 			elf_checked = trans_checked = B_FALSE;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		/*
2427c478bd9Sstevel@tonic-gate 		 * Open output files.
2437c478bd9Sstevel@tonic-gate 		 */
2447c478bd9Sstevel@tonic-gate 		if (ncp->nc_rpclogpath != NULL) {
2457c478bd9Sstevel@tonic-gate 			/*
2467c478bd9Sstevel@tonic-gate 			 * Log rpc requests in W3C-ELF format.
2477c478bd9Sstevel@tonic-gate 			 */
2487c478bd9Sstevel@tonic-gate 			if (!elf_checked && ncp->nc_elfcookie != NULL) {
2497c478bd9Sstevel@tonic-gate 				/*
2507c478bd9Sstevel@tonic-gate 				 * Make sure file still exists.
2517c478bd9Sstevel@tonic-gate 				 * Do this once per buffer.
2527c478bd9Sstevel@tonic-gate 				 */
2537c478bd9Sstevel@tonic-gate 				if (stat(ncp->nc_rpclogpath, &st) == -1 &&
2547c478bd9Sstevel@tonic-gate 				    errno == ENOENT) {
2557c478bd9Sstevel@tonic-gate 					/*
2567c478bd9Sstevel@tonic-gate 					 * The open rpclogfile has been
2577c478bd9Sstevel@tonic-gate 					 * deleted.  Get new one below.
2587c478bd9Sstevel@tonic-gate 					 */
2597c478bd9Sstevel@tonic-gate 					cleanup_elf_state(ncp);
2607c478bd9Sstevel@tonic-gate 				}
2617c478bd9Sstevel@tonic-gate 				elf_checked = B_TRUE;
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 			if (ncp->nc_elfcookie == NULL) {
2647c478bd9Sstevel@tonic-gate 				error = bep->be_error;
2657c478bd9Sstevel@tonic-gate 				ncp->nc_elfcookie = nfslog_open_elf_file(
266*458cf4d6Sek 				    ncp->nc_rpclogpath, &lbp->bh, &error);
2677c478bd9Sstevel@tonic-gate 				if (ncp->nc_elfcookie == NULL) {
2687c478bd9Sstevel@tonic-gate 					bep->be_error = error;
2697c478bd9Sstevel@tonic-gate 					goto done;
2707c478bd9Sstevel@tonic-gate 				}
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		if (ncp->nc_logpath != NULL) {
2757c478bd9Sstevel@tonic-gate 			/*
2767c478bd9Sstevel@tonic-gate 			 * Log rpc reqs in trans/ftp format.
2777c478bd9Sstevel@tonic-gate 			 */
2787c478bd9Sstevel@tonic-gate 			if (!trans_checked && ncp->nc_transcookie != NULL) {
2797c478bd9Sstevel@tonic-gate 				/*
2807c478bd9Sstevel@tonic-gate 				 * Do this once per buffer.
2817c478bd9Sstevel@tonic-gate 				 */
2827c478bd9Sstevel@tonic-gate 				if (stat(ncp->nc_logpath, &st) == -1 &&
2837c478bd9Sstevel@tonic-gate 				    errno == ENOENT) {
2847c478bd9Sstevel@tonic-gate 					/*
2857c478bd9Sstevel@tonic-gate 					 * The open transaction file has been
2867c478bd9Sstevel@tonic-gate 					 * deleted. Close pending transaction
2877c478bd9Sstevel@tonic-gate 					 * work. A new transaction log will be
2887c478bd9Sstevel@tonic-gate 					 * opened by nfslog_open_trans_file()
2897c478bd9Sstevel@tonic-gate 					 * below.
2907c478bd9Sstevel@tonic-gate 					 */
2917c478bd9Sstevel@tonic-gate 					cleanup_trans_state(ncp);
2927c478bd9Sstevel@tonic-gate 				}
2937c478bd9Sstevel@tonic-gate 				trans_checked = B_TRUE;
2947c478bd9Sstevel@tonic-gate 			}
2957c478bd9Sstevel@tonic-gate 			if (ncp->nc_transcookie == NULL) {
2967c478bd9Sstevel@tonic-gate 				int transtolog;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 				transtolog =
299*458cf4d6Sek 				    (ncp->nc_logformat == TRANSLOG_BASIC) ?
300*458cf4d6Sek 				    TRANSTOLOG_OPER_READWRITE : TRANSTOLOG_ALL;
3017c478bd9Sstevel@tonic-gate 				error = bep->be_error;
3027c478bd9Sstevel@tonic-gate 				ncp->nc_transcookie = nfslog_open_trans_file(
303*458cf4d6Sek 				    ncp->nc_logpath, ncp->nc_logformat,
304*458cf4d6Sek 				    transtolog, &error);
3057c478bd9Sstevel@tonic-gate 				if (ncp->nc_transcookie == NULL) {
3067c478bd9Sstevel@tonic-gate 					bep->be_error = error;
3077c478bd9Sstevel@tonic-gate 					goto done;
3087c478bd9Sstevel@tonic-gate 				}
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		assert(ncp->nc_fhpath != NULL);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		if (nfslog_process_fh_rec(lrp, ncp->nc_fhpath, &path1, &path2,
3157c478bd9Sstevel@tonic-gate 		    ncp->nc_elfcookie != NULL)) {
3167c478bd9Sstevel@tonic-gate 			/*
3177c478bd9Sstevel@tonic-gate 			 * Make sure there is room.
3187c478bd9Sstevel@tonic-gate 			 */
3197c478bd9Sstevel@tonic-gate 			if (ncp->nc_elfcookie != NULL) {
3207c478bd9Sstevel@tonic-gate 				(void) nfslog_process_elf_rec(ncp->nc_elfcookie,
321*458cf4d6Sek 				    &lrp->log_record, path1, path2);
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 			if (ncp->nc_transcookie != NULL) {
3257c478bd9Sstevel@tonic-gate 				(void) nfslog_process_trans_rec(
326*458cf4d6Sek 				    ncp->nc_transcookie,
327*458cf4d6Sek 				    &lrp->log_record, ncp->nc_fhpath,
328*458cf4d6Sek 				    path1, path2);
3297c478bd9Sstevel@tonic-gate 			}
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate skip:		if (path1 != NULL)
3337c478bd9Sstevel@tonic-gate 			free(path1);
3347c478bd9Sstevel@tonic-gate 		if (path2 != NULL)
3357c478bd9Sstevel@tonic-gate 			free(path2);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		path1 = path2 = NULL;
3387c478bd9Sstevel@tonic-gate 		nfslog_free_logrecord(lrp, TRUE);
3397c478bd9Sstevel@tonic-gate 	} /* while */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (!error && keep_running) {
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * Keep track of when this buffer was last processed.
3447c478bd9Sstevel@tonic-gate 		 */
3457c478bd9Sstevel@tonic-gate 		bep->be_lastprocessed = time(0);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		if (test && *buffer_processed != 0) {
3487c478bd9Sstevel@tonic-gate 			/*
3497c478bd9Sstevel@tonic-gate 			 * Save the buffer for future debugging. We do this
3507c478bd9Sstevel@tonic-gate 			 * by following the log cycling policy, with a maximum
3517c478bd9Sstevel@tonic-gate 			 * of 'max_logs_preserve' to save.
3527c478bd9Sstevel@tonic-gate 			 */
3537c478bd9Sstevel@tonic-gate 			if (cycle_log(bufferpath, max_logs_preserve)) {
3547c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
355*458cf4d6Sek 				    "could not save copy of buffer \"%s\""),
356*458cf4d6Sek 				    bufferpath);
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 		} else {
3597c478bd9Sstevel@tonic-gate 			/*
3607c478bd9Sstevel@tonic-gate 			 * Remove buffer since it has been processed.
3617c478bd9Sstevel@tonic-gate 			 */
3627c478bd9Sstevel@tonic-gate 			if (unlink(bufferpath)) {
3637c478bd9Sstevel@tonic-gate 				error = errno;
3647c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
365*458cf4d6Sek 				    "could not unlink %s: %s"),
366*458cf4d6Sek 				    bufferpath, strerror(error));
3677c478bd9Sstevel@tonic-gate 				/*
3687c478bd9Sstevel@tonic-gate 				 * Buffer was processed correctly.
3697c478bd9Sstevel@tonic-gate 				 */
3707c478bd9Sstevel@tonic-gate 				error = 0;
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate done:
3767c478bd9Sstevel@tonic-gate 	if (lbp != NULL)
3777c478bd9Sstevel@tonic-gate 		nfslog_close_buf(lbp, quick_cleaning);
3787c478bd9Sstevel@tonic-gate 	if (ncp && !quick_cleaning)
3797c478bd9Sstevel@tonic-gate 		cleanup_elf_state(ncp);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	return (error);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate static void
cleanup_elf_state(nfsl_config_t * ncp)3857c478bd9Sstevel@tonic-gate cleanup_elf_state(nfsl_config_t *ncp)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	if (ncp->nc_elfcookie != NULL) {
3887c478bd9Sstevel@tonic-gate 		nfslog_close_elf_file(&ncp->nc_elfcookie);
3897c478bd9Sstevel@tonic-gate 		assert(ncp->nc_elfcookie == NULL);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static void
cleanup_trans_state(nfsl_config_t * ncp)3947c478bd9Sstevel@tonic-gate cleanup_trans_state(nfsl_config_t *ncp)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	if (ncp->nc_transcookie != NULL) {
3977c478bd9Sstevel@tonic-gate 		nfslog_close_transactions(&ncp->nc_transcookie);
3987c478bd9Sstevel@tonic-gate 		assert(ncp->nc_transcookie == NULL);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Searches the list of previously seen bad tags. Note that this
4047c478bd9Sstevel@tonic-gate  * list is never pruned. This should not be a problem since the
4057c478bd9Sstevel@tonic-gate  * list of bad tags should be fairl small. New entries are inserted
4067c478bd9Sstevel@tonic-gate  * at the beginning of the list assuming it will be accessed more
4077c478bd9Sstevel@tonic-gate  * frequently since we have just seen it.
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate static void
badtag_notify(char * tag)4107c478bd9Sstevel@tonic-gate badtag_notify(char *tag)
4117c478bd9Sstevel@tonic-gate {
412*458cf4d6Sek 	struct nfs_log_list *lp, *p;
4137c478bd9Sstevel@tonic-gate 	int error;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	for (p = badtag_list; p != NULL; p = p->l_next) {
4167c478bd9Sstevel@tonic-gate 		if (strcmp(tag, p->l_name) == 0) {
4177c478bd9Sstevel@tonic-gate 			/*
4187c478bd9Sstevel@tonic-gate 			 * We've seen this before, nothing to do.
4197c478bd9Sstevel@tonic-gate 			 */
4207c478bd9Sstevel@tonic-gate 			return;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * Not on the list, add it.
4267c478bd9Sstevel@tonic-gate 	 */
427*458cf4d6Sek 	syslog(LOG_ERR, gettext("tag \"%s\" not found in %s - "
428*458cf4d6Sek 	    "ignoring records referencing such tag."),
429*458cf4d6Sek 	    tag, NFSL_CONFIG_FILE_PATH);
4307c478bd9Sstevel@tonic-gate 
431*458cf4d6Sek 	if ((lp = (struct nfs_log_list *)malloc(sizeof (*lp))) != NULL) {
4327c478bd9Sstevel@tonic-gate 		if ((lp->l_name = strdup(tag)) != NULL) {
4337c478bd9Sstevel@tonic-gate 			lp->l_next = badtag_list;
4347c478bd9Sstevel@tonic-gate 			badtag_list = lp;
4357c478bd9Sstevel@tonic-gate 			return;		/* done */
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (lp->l_name != NULL)
4407c478bd9Sstevel@tonic-gate 		free(lp->l_name);
4417c478bd9Sstevel@tonic-gate 	if (lp)
4427c478bd9Sstevel@tonic-gate 		free(lp);
4437c478bd9Sstevel@tonic-gate 	error = errno;
4447c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, gettext(
4457c478bd9Sstevel@tonic-gate 	    "Cannot add \"%s\" to bad tag list: %s"), tag, strerror(error));
4467c478bd9Sstevel@tonic-gate }
447