xref: /titanic_50/usr/src/lib/libldap5/sources/ldap/util/log.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
2*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3*004388ebScasper  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate #include <stdio.h>
97c478bd9Sstevel@tonic-gate #include <stdlib.h>
107c478bd9Sstevel@tonic-gate #include <sys/types.h>
117c478bd9Sstevel@tonic-gate #include <sys/stat.h>
127c478bd9Sstevel@tonic-gate #include <nl_types.h>
137c478bd9Sstevel@tonic-gate #include <limits.h>
147c478bd9Sstevel@tonic-gate #include <stdarg.h>
157c478bd9Sstevel@tonic-gate #include <string.h>
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <syslog.h>
187c478bd9Sstevel@tonic-gate #include <portable.h>
197c478bd9Sstevel@tonic-gate /* #include <lthread.h> */
207c478bd9Sstevel@tonic-gate #include <pthread.h>
217c478bd9Sstevel@tonic-gate #include <thread.h>
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include "log.h"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #define	LDAP_DEBUG_ANY	0xffff
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate static pthread_mutex_t	log_mutex;
287c478bd9Sstevel@tonic-gate static char		logfile[PATH_MAX] =
297c478bd9Sstevel@tonic-gate 					"/var/opt/SUNWconn/ldap/log/slapd.log";
307c478bd9Sstevel@tonic-gate static int		logsize = 512000;
317c478bd9Sstevel@tonic-gate static int		logtime = 1;
327c478bd9Sstevel@tonic-gate static FILE		*logfd = NULL;
337c478bd9Sstevel@tonic-gate static int		syslogopen = 0;
347c478bd9Sstevel@tonic-gate pthread_mutex_t		systime_mutex;
357c478bd9Sstevel@tonic-gate nl_catd			sundscat;
367c478bd9Sstevel@tonic-gate static int		log_debug = LDAP_DEBUG_STATS;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate typedef struct _logctx {
397c478bd9Sstevel@tonic-gate 	char		*logfile;
407c478bd9Sstevel@tonic-gate 	int		syslogopen;
417c478bd9Sstevel@tonic-gate 	int		logsize;
427c478bd9Sstevel@tonic-gate 	pthread_mutex_t	log_mutex;
437c478bd9Sstevel@tonic-gate 	int		log_debug;
447c478bd9Sstevel@tonic-gate 	int		log_syslog;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate } LogCtx;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate void
ldaplogconfig(char * logf,int size)497c478bd9Sstevel@tonic-gate ldaplogconfig(char *logf, int size)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	strcpy(logfile, logf);
527c478bd9Sstevel@tonic-gate 	logsize = size * 1024;
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate void
ldaplogconfigf(FILE * fd)567c478bd9Sstevel@tonic-gate ldaplogconfigf(FILE *fd)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	logfd = fd;
597c478bd9Sstevel@tonic-gate 	logsize = 0;
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate void
ldaploginit(char * name,int facility)637c478bd9Sstevel@tonic-gate ldaploginit(char *name, int facility)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	openlog(name, OPENLOG_OPTIONS, facility);
667c478bd9Sstevel@tonic-gate 	syslogopen = 1;
677c478bd9Sstevel@tonic-gate 	pthread_mutex_init(&log_mutex, NULL);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate void
ldaploginitlevel(char * name,int facility,int level)717c478bd9Sstevel@tonic-gate ldaploginitlevel(char *name, int facility, int level)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	ldaploginit(name, facility);
747c478bd9Sstevel@tonic-gate 	log_debug = level;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate LogCtx *
sundsloginit(char * name,int facility,int debug_level,int syslog_level)787c478bd9Sstevel@tonic-gate sundsloginit(char *name, int facility, int debug_level, int syslog_level)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	LogCtx *returnCtx = NULL;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if ((returnCtx = (LogCtx *)malloc(sizeof (LogCtx))) == NULL)
837c478bd9Sstevel@tonic-gate 		return (NULL);
847c478bd9Sstevel@tonic-gate 	if ((returnCtx->logfile = strdup(name)) == NULL) {
857c478bd9Sstevel@tonic-gate 		free(returnCtx);
867c478bd9Sstevel@tonic-gate 		return (NULL);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	openlog(returnCtx->logfile, OPENLOG_OPTIONS, facility);
897c478bd9Sstevel@tonic-gate 	returnCtx->syslogopen = 1;
907c478bd9Sstevel@tonic-gate 	pthread_mutex_init(&(returnCtx->log_mutex), NULL);
917c478bd9Sstevel@tonic-gate 	returnCtx->log_debug = debug_level;
927c478bd9Sstevel@tonic-gate 	returnCtx->log_syslog = syslog_level;
937c478bd9Sstevel@tonic-gate 	return (returnCtx);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static char timestr[128];
977c478bd9Sstevel@tonic-gate static time_t timelast = 0;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*VARARGS*/
1007c478bd9Sstevel@tonic-gate void
ldaplog(int level,char * fmt,...)1017c478bd9Sstevel@tonic-gate ldaplog(int level, char *fmt, ...)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	va_list ap;
1047c478bd9Sstevel@tonic-gate 	struct stat statbuf = {0};
1057c478bd9Sstevel@tonic-gate 	char newlog1[PATH_MAX];
1067c478bd9Sstevel@tonic-gate 	char newlog2[PATH_MAX];
1077c478bd9Sstevel@tonic-gate 	time_t now;
1087c478bd9Sstevel@tonic-gate 	int i;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (!(log_debug & level))
1117c478bd9Sstevel@tonic-gate 		return;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (level == LDAP_DEBUG_ANY) {
1167c478bd9Sstevel@tonic-gate 		/*
1177c478bd9Sstevel@tonic-gate 		 * this message is probably an error message, send it to syslog
1187c478bd9Sstevel@tonic-gate 		 */
1197c478bd9Sstevel@tonic-gate 		if (syslogopen) {
1207c478bd9Sstevel@tonic-gate 			vsyslog(LOG_ERR, fmt, ap);
1217c478bd9Sstevel@tonic-gate 		} /* end if */
1227c478bd9Sstevel@tonic-gate 		/* and sent it also on stderr */
1237c478bd9Sstevel@tonic-gate 		vfprintf(stderr, fmt, ap);
1247c478bd9Sstevel@tonic-gate 	} /* end if */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/*
1277c478bd9Sstevel@tonic-gate 	 * check that the log file is not already too big
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	pthread_mutex_lock(&log_mutex);
1307c478bd9Sstevel@tonic-gate 	if ((logsize > 0) && (stat(logfile, &statbuf) == 0 &&
1317c478bd9Sstevel@tonic-gate 					statbuf.st_size > logsize)) {
1327c478bd9Sstevel@tonic-gate 		for (i = 9; i > 1; i--) {
1337c478bd9Sstevel@tonic-gate 			(void) sprintf(newlog1, "%s.%d", logfile, i-1);
1347c478bd9Sstevel@tonic-gate 			(void) sprintf(newlog2, "%s.%d", logfile, i);
1357c478bd9Sstevel@tonic-gate 			(void) rename(newlog1, newlog2);
1367c478bd9Sstevel@tonic-gate 		} /* end for */
1377c478bd9Sstevel@tonic-gate 		if (logfd) {
1387c478bd9Sstevel@tonic-gate 			fclose(logfd);
1397c478bd9Sstevel@tonic-gate 			logfd = NULL;
1407c478bd9Sstevel@tonic-gate 		} /* end if */
1417c478bd9Sstevel@tonic-gate 		(void) rename(logfile, newlog1);
1427c478bd9Sstevel@tonic-gate 	} /* end if */
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * send the message into a regular log file
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	if (!logfd) {
147*004388ebScasper 		logfd = fopen(logfile, "aF");
1487c478bd9Sstevel@tonic-gate 	} /* end if */
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * finally write the message into the log file
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	if (logfd) {
1537c478bd9Sstevel@tonic-gate 		if (logtime) {
1547c478bd9Sstevel@tonic-gate 			time(&now);
1557c478bd9Sstevel@tonic-gate 			if (now-timelast > 60) {
1567c478bd9Sstevel@tonic-gate 				pthread_mutex_lock(&systime_mutex);
1577c478bd9Sstevel@tonic-gate 				timelast = now;
1587c478bd9Sstevel@tonic-gate 				ctime_r(&now, timestr, 128);
1597c478bd9Sstevel@tonic-gate 				pthread_mutex_unlock(&systime_mutex);
1607c478bd9Sstevel@tonic-gate 			} /* end if */
1617c478bd9Sstevel@tonic-gate 			fprintf(logfd, "%.16s : ", timestr);
1627c478bd9Sstevel@tonic-gate 		} /* end if */
1637c478bd9Sstevel@tonic-gate 		vfprintf(logfd, fmt, ap);
1647c478bd9Sstevel@tonic-gate 		fflush(logfd);
1657c478bd9Sstevel@tonic-gate 	} /* end if */
1667c478bd9Sstevel@tonic-gate 	pthread_mutex_unlock(&log_mutex);
1677c478bd9Sstevel@tonic-gate 	va_end(ap);
1687c478bd9Sstevel@tonic-gate }
169