1 | #include "globals.h"
|
---|
2 | #include <syslog.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 |
|
---|
5 | static FILE *fp=(FILE *)0;
|
---|
6 | static FILE *fps=(FILE *)0;
|
---|
7 | static int16_t logStarted = 0;
|
---|
8 |
|
---|
9 | pthread_mutex_t switching_log;
|
---|
10 | #ifdef CS_LOGHISTORY
|
---|
11 | pthread_mutex_t loghistory_lock;
|
---|
12 | #endif
|
---|
13 |
|
---|
14 | #ifdef CS_ANTICASC
|
---|
15 | FILE *fpa=(FILE *)0;
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #define LOG_BUF_SIZE (520+11) // should be aligned with s_client.dump from globals.h
|
---|
19 |
|
---|
20 | static void switch_log(char* file, FILE **f, int32_t (*pfinit)(void))
|
---|
21 | {
|
---|
22 | if( cfg.max_log_size) //only 1 thread needs to switch the log; even if anticasc, statistics and normal log are running
|
---|
23 | //at the same time, it is ok to have the other logs switching 1 entry later
|
---|
24 | {
|
---|
25 | struct stat stlog;
|
---|
26 |
|
---|
27 | if( stat(file, &stlog)!=0 )
|
---|
28 | {
|
---|
29 | fprintf(stderr, "stat('%s',..) failed (errno=%d %s)\n", file, errno, strerror(errno));
|
---|
30 | return;
|
---|
31 | }
|
---|
32 |
|
---|
33 | if( stlog.st_size >= cfg.max_log_size*1024 && *f != NULL) {
|
---|
34 | int32_t rc;
|
---|
35 | char prev_log[strlen(file) + 6];
|
---|
36 | snprintf(prev_log, sizeof(prev_log), "%s-prev", file);
|
---|
37 | if ( pthread_mutex_trylock(&switching_log) == 0) { //I got the lock so I am the first thread detecting a switchlog is needed
|
---|
38 | fprintf(*f, "switch log file\n");
|
---|
39 | fflush(*f);
|
---|
40 | fclose(*f);
|
---|
41 | *f = (FILE *)0;
|
---|
42 | rc = rename(file, prev_log);
|
---|
43 | if( rc!=0 ) {
|
---|
44 | fprintf(stderr, "rename(%s, %s) failed (errno=%d %s)\n", file, prev_log, errno, strerror(errno));
|
---|
45 | }
|
---|
46 | else
|
---|
47 | if( pfinit()){
|
---|
48 | fprintf(stderr, "Initialisation of log file failed, continuing without logging thread %8X. Log will be output to stdout!", (unsigned int)pthread_self());
|
---|
49 | cfg.logtostdout = 1;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | else //I am not the first to detect a switchlog is needed, so I need to wait for the first thread to complete
|
---|
53 | pthread_mutex_lock(&switching_log); //wait on 1st thread
|
---|
54 | pthread_mutex_unlock(&switching_log); //release after processing or after waiting
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | void cs_write_log(char *txt)
|
---|
60 | {
|
---|
61 | #ifdef CS_ANTICASC
|
---|
62 | struct s_client *cl = cur_client();
|
---|
63 | if( cl && cl->typ == 'a' && fpa ) {
|
---|
64 | switch_log(cfg.ac_logfile, &fpa, ac_init_log);
|
---|
65 | if (fpa) {
|
---|
66 | fputs(txt, fpa);
|
---|
67 | fflush(fpa);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | else
|
---|
71 | #endif
|
---|
72 | // filter out entries with leading 's' and forward to statistics
|
---|
73 | if(txt[0] == 's') {
|
---|
74 | if (fps) {
|
---|
75 | switch_log(cfg.usrfile, &fps, cs_init_statistics);
|
---|
76 | if (fps) {
|
---|
77 | fputs(txt + 1, fps); // remove the leading 's' and write to file
|
---|
78 | fflush(fps);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | } else {
|
---|
82 | if(!cfg.disablelog){
|
---|
83 | if (fp){
|
---|
84 | switch_log(cfg.logfile, &fp, cs_open_logfiles);
|
---|
85 | if (fp) {
|
---|
86 | fputs(txt, fp);
|
---|
87 | fflush(fp);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | if(cfg.logtostdout){
|
---|
91 | fputs(txt, stdout);
|
---|
92 | fflush(stdout);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | int32_t cs_open_logfiles()
|
---|
99 | {
|
---|
100 | if (!fp && cfg.logfile != NULL) { //log to file
|
---|
101 | if ((fp = fopen(cfg.logfile, "a+")) <= (FILE *)0) {
|
---|
102 | fp = (FILE *)0;
|
---|
103 | fprintf(stderr, "couldn't open logfile: %s (errno %d %s)\n", cfg.logfile, errno, strerror(errno));
|
---|
104 | } else {
|
---|
105 | time_t t;
|
---|
106 | char line[80];
|
---|
107 | memset(line, '-', sizeof(line));
|
---|
108 | line[(sizeof(line)/sizeof(char)) - 1] = '\0';
|
---|
109 | time(&t);
|
---|
110 | if (!cfg.disablelog)
|
---|
111 | fprintf(fp, "\n%s\n>> OSCam << cardserver started at %s%s\n", line, ctime(&t), line);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | // according to syslog docu: calling closelog is not necessary and calling openlog multiple times is safe
|
---|
115 | // We use openlog to set the default syslog settings so that it's possible to allow switching syslog on and off
|
---|
116 | openlog("oscam", LOG_NDELAY, LOG_DAEMON);
|
---|
117 |
|
---|
118 | cs_log(">> OSCam << cardserver started version " CS_VERSION ", build #" CS_SVN_VERSION " (" CS_OSTYPE ")");
|
---|
119 | cs_log_config();
|
---|
120 | return(fp <= (FILE *)0);
|
---|
121 | }
|
---|
122 |
|
---|
123 | int32_t cs_init_log(void)
|
---|
124 | {
|
---|
125 | if(logStarted == 0){
|
---|
126 | pthread_mutex_init(&switching_log, NULL);
|
---|
127 | #ifdef CS_LOGHISTORY
|
---|
128 | pthread_mutex_init(&loghistory_lock, NULL);
|
---|
129 | #endif
|
---|
130 | }
|
---|
131 | int32_t rc = cs_open_logfiles();
|
---|
132 | logStarted = 1;
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | static void get_log_header(int32_t m, char *txt)
|
---|
137 | {
|
---|
138 | struct s_client *cl = cur_client();
|
---|
139 | pthread_t thread = cl?cl->thread:0;
|
---|
140 | if(m)
|
---|
141 | snprintf(txt, LOG_BUF_SIZE, "%8X %c ",(unsigned int) thread, cl?cl->typ:' ');
|
---|
142 | else
|
---|
143 | snprintf(txt, LOG_BUF_SIZE, "%8X%-3.3s",(unsigned int) thread, "");
|
---|
144 | }
|
---|
145 |
|
---|
146 | static void write_to_log(int32_t flag, char *txt)
|
---|
147 | {
|
---|
148 | //flag = -1 is old behaviour, before implementation of debug_nolf (=debug no line feed)
|
---|
149 | //
|
---|
150 | time_t t;
|
---|
151 | struct tm lt;
|
---|
152 | char sbuf[16];
|
---|
153 | char log_buf[700];
|
---|
154 | struct s_client *cur_cl = cur_client();
|
---|
155 |
|
---|
156 | // get_log_header(flag, sbuf);
|
---|
157 | // memcpy(txt, sbuf, 11);
|
---|
158 |
|
---|
159 | #ifdef CS_ANTICASC
|
---|
160 | if (cfg.logtosyslog && cur_cl && cur_cl->typ != 'a') // system-logfile
|
---|
161 | #else
|
---|
162 | if (cfg.logtosyslog) // system-logfile
|
---|
163 | #endif
|
---|
164 | syslog(LOG_INFO, "%s", txt);
|
---|
165 |
|
---|
166 | time(&t);
|
---|
167 | localtime_r(&t, <);
|
---|
168 |
|
---|
169 | switch(flag) {
|
---|
170 | case -1:
|
---|
171 | snprintf(log_buf, sizeof(log_buf), "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s\n",
|
---|
172 | lt.tm_year+1900, lt.tm_mon+1, lt.tm_mday,
|
---|
173 | lt.tm_hour, lt.tm_min, lt.tm_sec, txt);
|
---|
174 | break;
|
---|
175 | case 1:
|
---|
176 | snprintf(log_buf, sizeof(log_buf), "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s",
|
---|
177 | lt.tm_year+1900, lt.tm_mon+1, lt.tm_mday,
|
---|
178 | lt.tm_hour, lt.tm_min, lt.tm_sec, txt);
|
---|
179 | break;
|
---|
180 | default:
|
---|
181 | snprintf(log_buf, sizeof(log_buf), "[LOG000]%s", txt);
|
---|
182 | }
|
---|
183 |
|
---|
184 | cs_write_log(log_buf + 8);
|
---|
185 | #ifdef CS_LOGHISTORY
|
---|
186 | pthread_mutex_lock(&loghistory_lock);
|
---|
187 | char *ptr=(char *)(loghist+(loghistidx*CS_LOGHISTSIZE));
|
---|
188 | loghistidx=(++loghistidx < CS_MAXLOGHIST)?loghistidx:0;
|
---|
189 | pthread_mutex_unlock(&loghistory_lock);
|
---|
190 |
|
---|
191 | ptr[0]='\1'; // make username unusable
|
---|
192 | ptr[1]='\0';
|
---|
193 |
|
---|
194 | if (!cur_cl)
|
---|
195 | cs_strncpy(ptr, "undef", 63);
|
---|
196 | else if ((cur_cl->typ=='c') || (cur_cl->typ=='m'))
|
---|
197 | cs_strncpy(ptr, cur_cl->account?cur_cl->account->usr:"", 63);
|
---|
198 | else if ((cur_cl->typ=='p') || (cur_cl->typ=='r'))
|
---|
199 | cs_strncpy(ptr, cur_cl->reader?cur_cl->reader->label:"", 63);
|
---|
200 | else
|
---|
201 | cs_strncpy(ptr, "server", 63);
|
---|
202 |
|
---|
203 | cs_strncpy(ptr+32, log_buf, CS_LOGHISTSIZE-33);
|
---|
204 |
|
---|
205 | #endif
|
---|
206 | struct s_client *cl;
|
---|
207 | for (cl=first_client; cl ; cl=cl->next)
|
---|
208 | {
|
---|
209 | if ((cl->typ == 'm') && (cl->monlvl>0))
|
---|
210 | if (cl->log) //this variable is only initialized for cl->typ = 'm'
|
---|
211 | {
|
---|
212 | if (cl->monlvl<2) {
|
---|
213 | if (cur_cl && (cur_cl->typ != 'c') && (cur_cl->typ != 'm'))
|
---|
214 | continue;
|
---|
215 | if (cur_cl && cur_cl->account && cl->account && strcmp(cur_cl->account->usr, cl->account->usr))
|
---|
216 | continue;
|
---|
217 | }
|
---|
218 | snprintf(sbuf, sizeof(sbuf), "%03d", cl->logcounter);
|
---|
219 | cl->logcounter = (cl->logcounter+1) % 1000;
|
---|
220 | memcpy(log_buf + 4, sbuf, 3);
|
---|
221 | monitor_send_idx(cl, log_buf);
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | void cs_log(const char *fmt,...)
|
---|
227 | {
|
---|
228 | char log_txt[LOG_BUF_SIZE];
|
---|
229 | get_log_header(1, log_txt);
|
---|
230 | va_list params;
|
---|
231 | va_start(params, fmt);
|
---|
232 | vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params);
|
---|
233 | va_end(params);
|
---|
234 | write_to_log(-1, log_txt);
|
---|
235 | }
|
---|
236 |
|
---|
237 | void cs_close_log(void)
|
---|
238 | {
|
---|
239 | if (!fp) return;
|
---|
240 | fclose(fp);
|
---|
241 | fp=(FILE *)0;
|
---|
242 | }
|
---|
243 | #ifdef WITH_DEBUG
|
---|
244 | void cs_debug_mask(uint16_t mask, const char *fmt,...)
|
---|
245 | {
|
---|
246 | char log_txt[LOG_BUF_SIZE];
|
---|
247 | if (cs_dblevel & mask)
|
---|
248 | {
|
---|
249 | get_log_header(1, log_txt);
|
---|
250 | va_list params;
|
---|
251 | va_start(params, fmt);
|
---|
252 | vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params);
|
---|
253 | va_end(params);
|
---|
254 | write_to_log(-1, log_txt);
|
---|
255 | }
|
---|
256 | }
|
---|
257 | #endif
|
---|
258 | void cs_dump(const uchar *buf, int32_t n, char *fmt, ...)
|
---|
259 | {
|
---|
260 | char log_txt[LOG_BUF_SIZE];
|
---|
261 | int32_t i;
|
---|
262 |
|
---|
263 | if( fmt )
|
---|
264 | {
|
---|
265 | get_log_header(1, log_txt);
|
---|
266 | va_list params;
|
---|
267 | va_start(params, fmt);
|
---|
268 | vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params);
|
---|
269 | va_end(params);
|
---|
270 | write_to_log(-1, log_txt);
|
---|
271 | //printf("LOG: %s\n", txt); fflush(stdout);
|
---|
272 | }
|
---|
273 |
|
---|
274 | for( i=0; i<n; i+=16 )
|
---|
275 | {
|
---|
276 | get_log_header(0, log_txt);
|
---|
277 | snprintf(log_txt+11, sizeof(log_txt) - 11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
278 | write_to_log(-1, log_txt);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | #ifdef WITH_DEBUG
|
---|
282 | void cs_ddump_mask(uint16_t mask, const uchar *buf, int32_t n, char *fmt, ...)
|
---|
283 | {
|
---|
284 |
|
---|
285 | char log_txt[LOG_BUF_SIZE];
|
---|
286 | int32_t i;
|
---|
287 | if ((mask & cs_dblevel) && (fmt))
|
---|
288 | {
|
---|
289 | get_log_header(1, log_txt);
|
---|
290 | va_list params;
|
---|
291 | va_start(params, fmt);
|
---|
292 | vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params);
|
---|
293 | va_end(params);
|
---|
294 | write_to_log(-1, log_txt);
|
---|
295 | //printf("LOG: %s\n", txt); fflush(stdout);
|
---|
296 | }
|
---|
297 | if (mask & cs_dblevel)
|
---|
298 | {
|
---|
299 | for (i=0; i<n; i+=16)
|
---|
300 | {
|
---|
301 | get_log_header(0, log_txt);
|
---|
302 | snprintf(log_txt+11, sizeof(log_txt) - 11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
303 | write_to_log(-1, log_txt);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 | #endif
|
---|
308 | int32_t cs_init_statistics(void)
|
---|
309 | {
|
---|
310 | if ((!fps) && (cfg.usrfile != NULL))
|
---|
311 | {
|
---|
312 | if ((fps=fopen(cfg.usrfile, "a+"))<=(FILE *)0)
|
---|
313 | {
|
---|
314 | fps=(FILE *)0;
|
---|
315 | cs_log("couldn't open statistics file: %s", cfg.usrfile);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | return(fps<=(FILE *)0);
|
---|
319 | }
|
---|
320 |
|
---|
321 | void cs_statistics(struct s_client * client)
|
---|
322 | {
|
---|
323 | if (!cfg.disableuserfile){
|
---|
324 | time_t t;
|
---|
325 | struct tm lt;
|
---|
326 | char buf[LOG_BUF_SIZE];
|
---|
327 |
|
---|
328 | float cwps;
|
---|
329 |
|
---|
330 | time(&t);
|
---|
331 | localtime_r(&t, <);
|
---|
332 | if (client->cwfound+client->cwnot>0)
|
---|
333 | {
|
---|
334 | cwps=client->last-client->login;
|
---|
335 | cwps/=client->cwfound+client->cwnot;
|
---|
336 | }
|
---|
337 | else
|
---|
338 | cwps=0;
|
---|
339 |
|
---|
340 | char *channel ="";
|
---|
341 | if(cfg.mon_appendchaninfo)
|
---|
342 | channel = get_servicename(client, client->last_srvid,client->last_caid);
|
---|
343 |
|
---|
344 | int32_t lsec;
|
---|
345 | if ((client->last_caid == 0xFFFF) && (client->last_srvid == 0xFFFF))
|
---|
346 | lsec = client->last - client->login; //client leave calc total duration
|
---|
347 | else
|
---|
348 | lsec = client->last - client->lastswitch;
|
---|
349 |
|
---|
350 | int32_t secs = 0, fullmins = 0, mins = 0, fullhours = 0;
|
---|
351 |
|
---|
352 | if((lsec > 0) && (lsec < 1000000)) {
|
---|
353 | secs = lsec % 60;
|
---|
354 | if (lsec > 60) {
|
---|
355 | fullmins = lsec / 60;
|
---|
356 | mins = fullmins % 60;
|
---|
357 | if(fullmins > 60) {
|
---|
358 | fullhours = fullmins / 60;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | /* statistics entry start with 's' to filter it out on other end of pipe
|
---|
364 | * so we can use the same Pipe as Log
|
---|
365 | */
|
---|
366 | snprintf(buf, sizeof(buf), "s%02d.%02d.%02d %02d:%02d:%02d %3.1f %s %s %d %d %d %d %d %d %d %ld %ld %02d:%02d:%02d %s %04X:%04X %s\n",
|
---|
367 | lt.tm_mday, lt.tm_mon+1, lt.tm_year%100,
|
---|
368 | lt.tm_hour, lt.tm_min, lt.tm_sec, cwps,
|
---|
369 | client->account->usr,
|
---|
370 | cs_inet_ntoa(client->ip),
|
---|
371 | client->port,
|
---|
372 | client->cwfound,
|
---|
373 | client->cwcache,
|
---|
374 | client->cwnot,
|
---|
375 | client->cwignored,
|
---|
376 | client->cwtout,
|
---|
377 | client->cwtun,
|
---|
378 | client->login,
|
---|
379 | client->last,
|
---|
380 | fullhours, mins, secs,
|
---|
381 | ph[client->ctyp].desc,
|
---|
382 | client->last_caid,
|
---|
383 | client->last_srvid,
|
---|
384 | channel);
|
---|
385 |
|
---|
386 | cs_write_log(buf);
|
---|
387 | }
|
---|
388 | }
|
---|