/* * (POP3Lite) FileLog - 3lite POP3 Daemon (logging to a file) * Copyright (C) 2000, 2001 Gergely Nagy <8@free.bsd.hu> * * This file is part of POP3Lite. * * POP3Lite is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * POP3Lite is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include #include #include #include #ifndef MAXHOSTNAMELEN # define MAXHOSTNAMELEN 64 #endif static const char rcsid[]="$Id: filelog.c,v 1.7.2.1 2001/04/18 10:15:33 algernon Exp $"; int filelog_LTX_module_init ( P3LControl *control ); int filelog_LTX_module_done ( P3LControl *control ); static void filelog_sys_openlog ( P3LControl *control ); static void filelog_sys_closelog ( P3LControl *control ); static void filelog_sys_log ( P3LControl *control, int priority, const char *format, ... ); static P3LSysCon_openlog B_filelog_sys_openlog; static P3LSysCon_closelog B_filelog_sys_closelog; static P3LSysCon_log B_filelog_sys_log; /** * filelog_sys_openlog: opens the logfile * @control: the usual control struct * * Opens a file for logging. * * Returns: nothing. **/ static void filelog_sys_openlog ( P3LControl *control ) { int logfd; if ( ( logfd = open ( (char *) P3L_GET_FIRST_OPTION ( "FILELOG.LOGFILE" ), O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR ) ) < 0 ) return; g_hash_table_insert ( control->data, "FILELOG.LOGFILEFD", (gpointer) logfd ); } /** * filelog_sys_closelog: * @control: the usual control struct * * Closes a logfile * * Returns: nothing. **/ static void filelog_sys_closelog ( P3LControl *control ) { int logfd = -1; logfd = (int) g_hash_table_lookup ( control->data, "FILELOG.LOGFILEFD" ); if ( logfd > 0 ) close ( logfd ); } /** * filelog_sys_log: send a message to the logfile * @control: the usual control struct * @priority: log priority * @format: printf-style format string * * Sends a message to the logfile * * Returns: nothing. **/ static void filelog_sys_log ( P3LControl *control, int priority, const char *format, ... ) { char *tmp, *tmp2; va_list lst; time_t logtime; int logfd; pid_t pid = getpid (); char hostname[MAXHOSTNAMELEN]; #ifdef WITH_LOGLEVEL_CHECK char *loglevel; loglevel = P3L_GET_FIRST_OPTION ( "MINIMUM_LOG_LEVEL" ); if ( priority > LOG_DEBUG - atoi ( ( loglevel != NULL ) ? loglevel : "5" ) ) return; #endif va_start ( lst, format ); tmp = g_strdup_vprintf ( format, lst ); va_end ( lst ); logfd = (int) g_hash_table_lookup ( control->data, "FILELOG.LOGFILEFD" ); if ( logfd <= 0 ) return; #if defined(HAVE_GETHOSTNAME) gethostname ( hostname, sizeof ( hostname ) ); #else strcpy ( (char *) hostname, "unknown" ); #endif logtime = time ( NULL ); tmp2 = ctime ( &logtime ); tmp2[strlen(tmp2)-1]=0; tmp2 = g_strdup_printf ("%s %s pop3lite[%u]: %s\n", tmp2, hostname, (unsigned int) pid, tmp ); p3l_lock_fd ( logfd, P3L_GET_FIRST_OPTION ( "FILELOG.LOGFILE" ) ); lseek ( logfd, 0, SEEK_END ); write ( logfd, tmp2, strlen ( tmp2 ) ); p3l_unlock_fd ( logfd, P3L_GET_FIRST_OPTION ( "FILELOG.LOGFILE" ) ); g_free ( tmp2 ); g_free ( tmp ); } int filelog_LTX_module_init ( P3LControl *control ) { control->system->closelog ( control ); B_filelog_sys_openlog = control->system->openlog; control->system->openlog = filelog_sys_openlog; B_filelog_sys_closelog = control->system->closelog; control->system->closelog = filelog_sys_closelog; B_filelog_sys_log = control->system->log; control->system->log = filelog_sys_log; control->system->openlog ( control ); return 0; } int filelog_LTX_module_done ( P3LControl *control ) { control->system->closelog ( control ); control->system->openlog = B_filelog_sys_openlog; control->system->closelog = B_filelog_sys_closelog; control->system->log = B_filelog_sys_log; control->system->openlog ( control ); return 0; }