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