masqmail

view src/log.c @ 424:19be3b27df6f

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