masqmail

annotate src/log.c @ 421:f37384470855

Changed lockdir to /var/lock/masqmail; Create lockdir and piddir on startup. Moved the lockdir out of the spool dir. (When /var/lock is a ramdisk we do well to have the lock files there.) Added the new configure option --with-lockdir to change that location. Nontheless, if we run_as_user, then lock files are always stored in the spool dir directly. Instead of installing the lockdir and piddir at installation time, we create them on startup time now if they are missing. This is necessary if lockdir or piddir are a tmpfs.
author markus schnalke <meillo@marmaro.de>
date Wed, 30 May 2012 09:38:38 +0200
parents 9bc3e47b0222
children 19be3b27df6f
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 1999-2001 Oliver Kurth
meillo@367 4 **
meillo@367 5 ** This program is free software; you can redistribute it and/or modify
meillo@367 6 ** it under the terms of the GNU General Public License as published by
meillo@367 7 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 8 ** (at your option) any later version.
meillo@367 9 **
meillo@367 10 ** This program is distributed in the hope that it will be useful,
meillo@367 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 13 ** GNU General Public License for more details.
meillo@367 14 **
meillo@367 15 ** You should have received a copy of the GNU General Public License
meillo@367 16 ** along with this program; if not, write to the Free Software
meillo@367 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 18 */
meillo@0 19
meillo@377 20 #include <sys/stat.h>
meillo@15 21 #include <sysexits.h>
meillo@15 22
meillo@0 23 #include "masqmail.h"
meillo@0 24
meillo@0 25 static char *_sysexit_strings[] = {
meillo@10 26 "command line usage error",
meillo@10 27 "data format error",
meillo@10 28 "cannot open input",
meillo@10 29 "addressee unknown",
meillo@10 30 "host name unknown",
meillo@10 31 "service unavailable",
meillo@10 32 "internal software error",
meillo@10 33 "system error (e.g., can't fork)",
meillo@10 34 "critical OS file missing",
meillo@10 35 "can't create (user) output file",
meillo@10 36 "input/output error",
meillo@10 37 "temp failure; user is invited to retry",
meillo@10 38 "remote error in protocol",
meillo@10 39 "permission denied",
meillo@10 40 "configuration error"
meillo@0 41 };
meillo@0 42
meillo@10 43 gchar*
meillo@10 44 ext_strerror(int err)
meillo@0 45 {
meillo@10 46 if (err < 1024)
meillo@10 47 return strerror(err);
meillo@10 48 else if (err > 1024 + EX__BASE
meillo@10 49 && (err - 1024 - EX__BASE < sizeof(_sysexit_strings) / sizeof(_sysexit_strings[0])))
meillo@10 50 return _sysexit_strings[err - 1024 - EX__BASE];
meillo@0 51
meillo@10 52 return "unknown error";
meillo@0 53 }
meillo@0 54
meillo@0 55 static FILE *logfile = NULL;
meillo@0 56 static FILE *debugfile = NULL;
meillo@0 57
meillo@10 58 gboolean
meillo@10 59 logopen()
meillo@0 60 {
meillo@10 61 gchar *filename;
meillo@10 62 mode_t saved_mode = umask(066);
meillo@0 63
meillo@10 64 if (conf.use_syslog) {
meillo@10 65 openlog(PACKAGE, LOG_PID, LOG_MAIL);
meillo@10 66 } else {
meillo@10 67 uid_t saved_uid;
meillo@10 68 gid_t saved_gid;
meillo@0 69
meillo@331 70 if (!conf.run_as_user) {
meillo@331 71 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
meillo@331 72 }
meillo@0 73
meillo@10 74 filename = g_strdup_printf("%s/masqmail.log", conf.log_dir);
meillo@10 75 logfile = fopen(filename, "a");
meillo@10 76 if (!logfile) {
meillo@10 77 fprintf(stderr, "could not open log '%s': %s\n", filename, strerror(errno));
meillo@10 78 return FALSE;
meillo@10 79 }
meillo@10 80 g_free(filename);
meillo@10 81
meillo@331 82 if (!conf.run_as_user) {
meillo@331 83 set_euidgid(saved_uid, saved_gid, NULL, NULL);
meillo@331 84 }
meillo@10 85 }
meillo@0 86
meillo@0 87 #ifdef ENABLE_DEBUG
meillo@10 88 if (conf.debug_level > 0) {
meillo@10 89 filename = g_strdup_printf("%s/debug.log", conf.log_dir);
meillo@10 90 debugfile = fopen(filename, "a");
meillo@10 91 if (!debugfile) {
meillo@10 92 fprintf(stderr, "could not open debug log '%s'\n", filename);
meillo@10 93 return FALSE;
meillo@10 94 }
meillo@10 95 g_free(filename);
meillo@10 96 }
meillo@0 97 #endif
meillo@10 98 umask(saved_mode);
meillo@10 99 return TRUE;
meillo@0 100 }
meillo@0 101
meillo@10 102 void
meillo@10 103 logclose()
meillo@0 104 {
meillo@10 105 if (conf.use_syslog)
meillo@10 106 closelog();
meillo@10 107 else if (logfile)
meillo@10 108 fclose(logfile);
meillo@10 109 if (debugfile)
meillo@10 110 fclose(debugfile);
meillo@0 111 }
meillo@0 112
meillo@10 113 void
meillo@10 114 vlogwrite(int pri, const char *fmt, va_list args)
meillo@0 115 {
meillo@15 116 if ((conf.do_verbose && (pri & LOG_VERBOSE)) || (pri == LOG_ALERT) || (pri == LOG_WARNING)) {
meillo@10 117 va_list args_copy;
meillo@10 118 va_copy(args_copy, args);
meillo@10 119 vfprintf(stdout, fmt, args_copy);
meillo@10 120 va_end(args_copy);
meillo@331 121 fflush(stdout); /* in case output ends not with newline */
meillo@10 122 }
meillo@0 123
meillo@10 124 pri &= ~LOG_VERBOSE;
meillo@331 125 if (!pri) {
meillo@331 126 return;
meillo@331 127 }
meillo@395 128 if (conf.use_syslog) {
meillo@331 129 vsyslog(pri, fmt, args);
meillo@395 130 return;
meillo@395 131 }
meillo@395 132 FILE *file = logfile ? logfile : stderr;
meillo@395 133 time_t now = time(NULL);
meillo@395 134 struct tm *t = localtime(&now);
meillo@395 135 gchar buf[24];
meillo@10 136
meillo@395 137 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
meillo@395 138 fprintf(file, "%s [%d] ", buf, getpid());
meillo@10 139
meillo@395 140 vfprintf(file, fmt, args);
meillo@395 141 fflush(file);
meillo@10 142 }
meillo@10 143
meillo@10 144 #ifdef ENABLE_DEBUG
meillo@10 145 void
meillo@10 146 vdebugwrite(int pri, const char *fmt, va_list args)
meillo@10 147 {
meillo@0 148 time_t now = time(NULL);
meillo@0 149 struct tm *t = localtime(&now);
meillo@0 150 gchar buf[24];
meillo@10 151 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
meillo@0 152
meillo@10 153 if (debugfile) {
meillo@10 154 fprintf(debugfile, "%s [%d] ", buf, getpid());
meillo@10 155 vfprintf(debugfile, fmt, args);
meillo@10 156 fflush(debugfile);
meillo@10 157 } else {
meillo@10 158 fprintf(stderr, "no debug file, msg was:\n");
meillo@10 159 vfprintf(stderr, fmt, args);
meillo@10 160 }
meillo@0 161 }
meillo@0 162 #endif
meillo@0 163
meillo@10 164 void
meillo@10 165 logwrite(int pri, const char *fmt, ...)
meillo@0 166 {
meillo@10 167 va_list args, args_copy;
meillo@10 168 int saved_errno = errno; /* somewhere this is changed to EBADF */
meillo@0 169
meillo@10 170 va_start(args, fmt);
meillo@0 171 #ifdef ENABLE_DEBUG
meillo@10 172 va_copy(args_copy, args);
meillo@0 173 #endif
meillo@10 174 vlogwrite(pri, fmt, args);
meillo@0 175 #ifdef ENABLE_DEBUG
meillo@10 176 if (debugfile)
meillo@10 177 vdebugwrite(pri, fmt, args_copy);
meillo@10 178 va_end(args_copy);
meillo@0 179 #endif
meillo@10 180 va_end(args);
meillo@0 181
meillo@10 182 errno = saved_errno;
meillo@0 183 }
meillo@0 184
meillo@0 185 #ifdef ENABLE_DEBUG
meillo@10 186 void
meillo@10 187 debugf(const char *fmt, ...)
meillo@0 188 {
meillo@10 189 va_list args;
meillo@10 190 va_start(args, fmt);
meillo@0 191
meillo@10 192 vdebugwrite(LOG_DEBUG, fmt, args);
meillo@0 193
meillo@10 194 va_end(args);
meillo@0 195 }
meillo@0 196
meillo@10 197 void
meillo@10 198 vdebugf(const char *fmt, va_list args)
meillo@0 199 {
meillo@10 200 vdebugwrite(LOG_DEBUG, fmt, args);
meillo@0 201 }
meillo@0 202 #endif
meillo@0 203
meillo@10 204 void
meillo@10 205 maillog(const char *fmt, ...)
meillo@0 206 {
meillo@10 207 va_list args;
meillo@10 208 va_start(args, fmt);
meillo@0 209
meillo@10 210 vlogwrite(LOG_NOTICE, fmt, args);
meillo@0 211
meillo@10 212 va_end(args);
meillo@0 213 }