/[imapfilter]/imapfilter/log.c
ViewVC logotype

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (hide annotations)
Fri Nov 9 18:01:29 2001 UTC (22 years, 4 months ago) by lefcha
Branch: MAIN
Changes since 1.17: +20 -0 lines
File MIME type: text/plain
Added signal handling.

1 lefcha 1.1 #include <stdio.h>
2     #include <stdlib.h>
3 lefcha 1.9 #include <unistd.h>
4 lefcha 1.1 #include <errno.h>
5     #include <string.h>
6     #include <stdarg.h>
7 lefcha 1.18 #include <signal.h>
8 lefcha 1.1 #include <limits.h>
9 lefcha 1.9 #include <sys/types.h>
10     #include <sys/stat.h>
11     #include <fcntl.h>
12 lefcha 1.12 #include <time.h>
13 lefcha 1.1
14     #include "config.h"
15     #include "imapfilter.h"
16    
17    
18 lefcha 1.5 extern char logfile[PATH_MAX];
19 lefcha 1.8 extern unsigned int options;
20 lefcha 1.6
21 lefcha 1.10 static FILE *logfp = NULL; /* Pointer to logfile. */
22 lefcha 1.1
23    
24     /*
25 lefcha 1.17 * Print message if not in OPTION_DETAILS_QUIET mode.
26 lefcha 1.7 */
27 lefcha 1.14 void info(const char *info,...)
28 lefcha 1.7 {
29     va_list args;
30 lefcha 1.14
31 lefcha 1.16 if (options & OPTION_DETAILS_QUIET)
32 lefcha 1.13 return;
33 lefcha 1.14
34 lefcha 1.12 va_start(args, info);
35 lefcha 1.13 vprintf(info, args);
36 lefcha 1.12 va_end(args);
37     }
38    
39    
40     /*
41 lefcha 1.10 * Print message if in OPTION_DETAILS_VERBOSE mode.
42 lefcha 1.1 */
43 lefcha 1.14 void verbose(const char *info,...)
44 lefcha 1.1 {
45     va_list args;
46    
47 lefcha 1.10 if (options & OPTION_DETAILS_VERBOSE) {
48 lefcha 1.1 va_start(args, info);
49     vprintf(info, args);
50     va_end(args);
51     }
52     }
53    
54 lefcha 1.17
55 lefcha 1.1 /*
56 lefcha 1.10 * Print error message and write it into logfile.
57 lefcha 1.1 */
58 lefcha 1.14 void error(const char *errmsg,...)
59 lefcha 1.1 {
60     va_list args;
61    
62     va_start(args, errmsg);
63    
64 lefcha 1.14 vfprintf(stderr, errmsg, args);
65    
66 lefcha 1.8 if (logfp) {
67 lefcha 1.1 vfprintf(logfp, errmsg, args);
68     }
69     va_end(args);
70     }
71    
72    
73     /*
74 lefcha 1.12 * Print error message and exit program.
75     */
76 lefcha 1.14 void fatal(unsigned int errnum, const char *fatal,...)
77 lefcha 1.12 {
78     va_list args;
79 lefcha 1.14
80 lefcha 1.12 va_start(args, fatal);
81     vfprintf(stderr, fatal, args);
82     va_end(args);
83 lefcha 1.14
84 lefcha 1.12 close_connection();
85     close_logfile();
86 lefcha 1.14
87 lefcha 1.12 exit(errnum);
88     }
89 lefcha 1.14
90 lefcha 1.18
91     /*
92     * Catch signals that cause program's termination.
93     */
94     void catch_signals(void)
95     {
96     signal(SIGINT, signal_handler);
97     signal(SIGQUIT, signal_handler);
98     signal(SIGTERM, signal_handler);
99     }
100    
101    
102     /*
103     * Signal handler for signals that cause termination of program.
104     */
105     void signal_handler(int sig)
106     {
107     fatal(ERROR_SIGNAL, "imapfilter: killed by signal %d\n", sig);
108     }
109 lefcha 1.12
110    
111     /*
112 lefcha 1.17 * Open the file for saving of logging information.
113 lefcha 1.1 */
114     int open_logfile(void)
115     {
116 lefcha 1.10 if (!*logfile)
117 lefcha 1.6 return 0; /* Logging not enabled. */
118 lefcha 1.4
119 lefcha 1.1 #ifdef DEBUG
120 lefcha 1.8 printf("debug: logfile: '%s'\n", logfile);
121 lefcha 1.1 #endif
122    
123 lefcha 1.9 if (create_logfile())
124 lefcha 1.12 return ERROR_TRIVIAL;
125 lefcha 1.9
126 lefcha 1.5 logfp = fopen(logfile, "a");
127 lefcha 1.1
128     if (!logfp) {
129 lefcha 1.12 error("imapfilter: opening logfile %s: %s\n",
130 lefcha 1.14 logfile, strerror(errno));
131 lefcha 1.12 return ERROR_TRIVIAL;
132 lefcha 1.9 }
133     return 0;
134     }
135    
136    
137     /*
138 lefcha 1.12 * If logfile does not exist, create it with proper permissions.
139 lefcha 1.9 */
140     int create_logfile(void)
141     {
142     int fd;
143     struct stat fs;
144    
145     if (stat(logfile, &fs) && errno == ENOENT) {
146     fd = creat(logfile, S_IRUSR | S_IWUSR);
147     close(fd);
148     return 0;
149     } else if (!S_ISREG(fs.st_mode)) {
150 lefcha 1.12 error("imapfilter: file %s already exists and not a regular file\n",
151 lefcha 1.14 logfile);
152 lefcha 1.12 return ERROR_TRIVIAL;
153 lefcha 1.1 }
154 lefcha 1.6 return 0;
155 lefcha 1.1 }
156    
157    
158     /*
159 lefcha 1.10 * Close the logfile.
160 lefcha 1.1 */
161     int close_logfile(void)
162     {
163 lefcha 1.8 if (!logfp)
164 lefcha 1.6 return 0;
165 lefcha 1.1 else
166     return fclose(logfp);
167     }
168    
169 lefcha 1.13
170     /*
171     * Prepares the log entry to be saved through continues calls, and writes it
172     * to logfile.
173     */
174     void log_info(int flag, void *ptr)
175     {
176     static struct {
177     char *server;
178     char *username;
179     char *mbox;
180     char *filter;
181     unsigned int *action;
182     char *destmbox;
183     char *hdrs;
184     } inf = {
185     NULL, NULL, NULL, NULL, NULL, NULL, NULL
186     };
187 lefcha 1.14
188 lefcha 1.13 if (!logfp)
189     return;
190 lefcha 1.14
191     switch (flag) {
192 lefcha 1.13 case LOG_WRITE:
193     fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(),
194     inf.server, inf.username, inf.mbox, inf.filter,
195     (*inf.action == FILTER_ACTION_DELETE ? "delete" :
196     *inf.action == FILTER_ACTION_COPY ? "copy " :
197     *inf.action == FILTER_ACTION_MOVE ? "move " : "list"),
198 lefcha 1.15 (!*inf.destmbox ? "" : inf.destmbox));
199 lefcha 1.14
200 lefcha 1.13 if (ptr) {
201     inf.hdrs = (char *) ptr;
202     fputc('\t', logfp);
203     while (*inf.hdrs) {
204     if (*inf.hdrs == '\n') {
205     fputc('\n', logfp);
206     if (*(inf.hdrs + 1))
207     fputc('\t', logfp);
208     inf.hdrs++;
209     } else
210     fputc(*(inf.hdrs++), logfp);
211     }
212     }
213     break;
214     case LOG_SERVER:
215     inf.server = (char *) ptr;
216     break;
217     case LOG_USERNAME:
218     inf.username = (char *) ptr;
219 lefcha 1.14 break;
220 lefcha 1.13 case LOG_MAILBOX:
221     inf.mbox = (char *) ptr;
222     break;
223     case LOG_FILTER:
224     inf.filter = (char *) ptr;
225 lefcha 1.14 break;
226 lefcha 1.13 case LOG_ACTION:
227     inf.action = (unsigned int *) ptr;
228     break;
229     case LOG_DESTINATION_MAILBOX:
230     inf.destmbox = (char *) ptr;
231     break;
232     }
233     }
234 lefcha 1.14
235 lefcha 1.1
236 lefcha 1.2 /*
237 lefcha 1.12 * Return current local time and date.
238 lefcha 1.2 */
239 lefcha 1.12 char *get_time(void)
240 lefcha 1.1 {
241 lefcha 1.12 char *ct;
242     time_t t;
243 lefcha 1.14
244 lefcha 1.12 t = time(NULL);
245 lefcha 1.14
246 lefcha 1.12 ct = ctime(&t);
247     *(strchr(ct, '\n')) = 0;
248 lefcha 1.1
249 lefcha 1.12 return ct;
250 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26