1 | #include "globals.h"
|
---|
2 | #include <syslog.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <pthread.h>
|
---|
5 |
|
---|
6 | int number_of_chars_printed = 0;
|
---|
7 |
|
---|
8 | static FILE *fp=(FILE *)0;
|
---|
9 | static FILE *fps=(FILE *)0;
|
---|
10 | static int use_syslog=0;
|
---|
11 | static int use_stdout=0;
|
---|
12 | static pthread_mutex_t log_lock;
|
---|
13 | static char *log_txt;
|
---|
14 |
|
---|
15 | #ifdef CS_ANTICASC
|
---|
16 | FILE *fpa=(FILE *)0;
|
---|
17 | int use_ac_log=0;
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | static void switch_log(char* file, FILE **f, int (*pfinit)(char*))
|
---|
21 | {
|
---|
22 | if( cfg->max_log_size && mcl)
|
---|
23 | {
|
---|
24 | struct stat stlog;
|
---|
25 | if( stat(file, &stlog)!=0 )
|
---|
26 | {
|
---|
27 | fprintf(stderr, "stat('%s',..) failed (errno=%d)\n", file, errno);
|
---|
28 | return;
|
---|
29 | }
|
---|
30 |
|
---|
31 | if( stlog.st_size >= cfg->max_log_size*1024 ) {
|
---|
32 | int rc;
|
---|
33 | char prev_log[128];
|
---|
34 | sprintf(prev_log, "%s-prev", file);
|
---|
35 | fprintf(*f, "switch log file\n");
|
---|
36 | fflush(*f);
|
---|
37 | fclose(*f);
|
---|
38 | *f = (FILE *)0;
|
---|
39 | rc = rename(file, prev_log);
|
---|
40 | if( rc!=0 ) {
|
---|
41 | fprintf(stderr, "rename(%s, %s) failed (errno=%d)\n",
|
---|
42 | file, prev_log, errno);
|
---|
43 | }
|
---|
44 | else if( pfinit(file))
|
---|
45 | cs_exit(0);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | void cs_write_log(char *txt)
|
---|
51 | {
|
---|
52 | #ifdef CS_ANTICASC
|
---|
53 | if( use_ac_log && fpa ) {
|
---|
54 | switch_log(cfg->ac_logfile, &fpa, ac_init_log);
|
---|
55 | fprintf(fpa, "%s", txt);
|
---|
56 | fflush(fpa);
|
---|
57 | }
|
---|
58 | else
|
---|
59 | #endif
|
---|
60 | // filter out entries with leading 's' and forward to statistics
|
---|
61 | if(txt[0] == 's') {
|
---|
62 | if (fps) {
|
---|
63 | switch_log(cfg->usrfile, &fps, cs_init_statistics);
|
---|
64 | fprintf(fps, "%s", txt + 1); // remove the leading 's' and write to file
|
---|
65 | fflush(fps);
|
---|
66 | }
|
---|
67 | } else {
|
---|
68 | if (fp || use_stdout) {
|
---|
69 | if( !use_stdout && !use_syslog)
|
---|
70 | switch_log(cfg->logfile, &fp, cs_init_log);
|
---|
71 | if (!cfg->disablelog){
|
---|
72 | fprintf(fp, "%s", txt);
|
---|
73 | fflush(fp);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | int cs_init_log(char *file)
|
---|
80 | {
|
---|
81 | static char *head = ">> OSCam << cardserver started version " CS_VERSION ", build #" CS_SVN_VERSION " (" CS_OSTYPE ")";
|
---|
82 |
|
---|
83 | pthread_mutex_init(&log_lock, NULL);
|
---|
84 | log_txt = malloc(1024);
|
---|
85 |
|
---|
86 | if (!strcmp(file, "stdout")) {
|
---|
87 | use_stdout = 1;
|
---|
88 | fp = stdout;
|
---|
89 | cs_log(head);
|
---|
90 | cs_log_config();
|
---|
91 | return(0);
|
---|
92 | }
|
---|
93 | if (strcmp(file, "syslog")) {
|
---|
94 | if (!fp) {
|
---|
95 | if ((fp = fopen(file, "a+")) <= (FILE *)0) {
|
---|
96 | fp = (FILE *)0;
|
---|
97 | fprintf(stderr, "couldn't open logfile: %s (errno %d)\n", file, errno);
|
---|
98 | } else {
|
---|
99 | time_t t;
|
---|
100 | char line[80];
|
---|
101 | memset(line, '-', sizeof(line));
|
---|
102 | line[(sizeof(line)/sizeof(char)) - 1] = '\0';
|
---|
103 | time(&t);
|
---|
104 | if (!cfg->disablelog)
|
---|
105 | fprintf(fp, "\n%s\n>> OSCam << cardserver started at %s%s\n", line, ctime(&t), line);
|
---|
106 | cs_log_config();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | return(fp <= (FILE *)0);
|
---|
110 | } else {
|
---|
111 | openlog("oscam", LOG_NDELAY, LOG_DAEMON);
|
---|
112 | use_syslog = 1;
|
---|
113 | cs_log(head);
|
---|
114 | cs_log_config();
|
---|
115 | return(0);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | static char *get_log_header(int m, char *txt)
|
---|
120 | {
|
---|
121 | if(m) {
|
---|
122 | sprintf(txt, "%6d ", getpid());
|
---|
123 | if (cs_idx) {
|
---|
124 | switch (client[cs_idx].typ) {
|
---|
125 | case 'r':
|
---|
126 | case 'p': sprintf(txt+7, "%c%02d ", client[cs_idx].typ, cs_idx - 1);
|
---|
127 | break;
|
---|
128 | case 'm':
|
---|
129 | case 'c': sprintf(txt+7, "%c%02d ", client[cs_idx].typ, cs_idx - cdiff);
|
---|
130 | break;
|
---|
131 | #ifdef CS_ANTICASC
|
---|
132 | case 'a':
|
---|
133 | #endif
|
---|
134 | case 'l':
|
---|
135 | #ifdef WEBIF
|
---|
136 | case 'h':
|
---|
137 | #endif
|
---|
138 | case 'n': sprintf(txt+7, "%c " , client[cs_idx].typ);
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | } else {
|
---|
142 | strcpy(txt+7, "s ");
|
---|
143 | }
|
---|
144 | } else {
|
---|
145 | sprintf(txt, "%-11.11s", "");
|
---|
146 | }
|
---|
147 | return(txt);
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void write_to_log(int flag, char *txt)
|
---|
151 | {
|
---|
152 | //flag = -1 is old behaviour, before implementation of debug_nolf (=debug no line feed)
|
---|
153 | //
|
---|
154 | int i;
|
---|
155 | time_t t;
|
---|
156 | struct tm *lt;
|
---|
157 | char buf[512], sbuf[16];
|
---|
158 |
|
---|
159 | // get_log_header(flag, sbuf);
|
---|
160 | // memcpy(txt, sbuf, 11);
|
---|
161 |
|
---|
162 | #ifdef CS_ANTICASC
|
---|
163 | if (use_syslog && !use_ac_log) // system-logfile
|
---|
164 | #else
|
---|
165 | if (use_syslog) // system-logfile
|
---|
166 | #endif
|
---|
167 | syslog(LOG_INFO, "%s", txt);
|
---|
168 |
|
---|
169 | time(&t);
|
---|
170 | lt=localtime(&t);
|
---|
171 |
|
---|
172 | switch(flag) {
|
---|
173 | case -1:
|
---|
174 | sprintf(buf, "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s\n",
|
---|
175 | lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
|
---|
176 | lt->tm_hour, lt->tm_min, lt->tm_sec, txt);
|
---|
177 | break;
|
---|
178 | case 1:
|
---|
179 | sprintf(buf, "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s",
|
---|
180 | lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
|
---|
181 | lt->tm_hour, lt->tm_min, lt->tm_sec, txt);
|
---|
182 | break;
|
---|
183 | case 16:
|
---|
184 | number_of_chars_printed = 0;
|
---|
185 | sprintf(buf, "[LOG000]%s\n", txt);
|
---|
186 | break;
|
---|
187 | default:
|
---|
188 | sprintf(buf, "[LOG000]%s", txt);
|
---|
189 | }
|
---|
190 |
|
---|
191 | if ((*log_fd) && (client[cs_idx].typ != 'l') && (client[cs_idx].typ != 'a'))
|
---|
192 | write_to_pipe(*log_fd, PIP_ID_LOG, (uchar *) buf+8, strlen(buf+8));
|
---|
193 | else
|
---|
194 | cs_write_log(buf + 8);
|
---|
195 |
|
---|
196 | store_logentry(buf);
|
---|
197 |
|
---|
198 | for (i = 0; i < CS_MAXPID; i++) // monitor-clients
|
---|
199 | {
|
---|
200 | if ((client[i].pid) && (client[i].log))
|
---|
201 | {
|
---|
202 | if (client[i].monlvl<2)
|
---|
203 | {
|
---|
204 | if ((client[cs_idx].typ != 'c') && (client[cs_idx].typ != 'm'))
|
---|
205 | continue;
|
---|
206 | if (strcmp(client[cs_idx].usr, client[i].usr))
|
---|
207 | continue;
|
---|
208 | }
|
---|
209 | sprintf(sbuf, "%03d", client[i].logcounter);
|
---|
210 | client[i].logcounter = (client[i].logcounter+1) % 1000;
|
---|
211 | memcpy(buf + 4, sbuf, 3);
|
---|
212 | monitor_send_idx(i, buf);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | void cs_log(char *fmt,...)
|
---|
218 | {
|
---|
219 | if (!log_txt)
|
---|
220 | return;
|
---|
221 | pthread_mutex_lock(&log_lock);
|
---|
222 | get_log_header(1, log_txt);
|
---|
223 | va_list params;
|
---|
224 | va_start(params, fmt);
|
---|
225 | vsprintf(log_txt+11, fmt, params);
|
---|
226 | va_end(params);
|
---|
227 | write_to_log(-1, log_txt);
|
---|
228 | pthread_mutex_unlock(&log_lock);
|
---|
229 | }
|
---|
230 |
|
---|
231 | void cs_close_log(void)
|
---|
232 | {
|
---|
233 | if (log_txt) {
|
---|
234 | cs_log("LOG CLOSED");
|
---|
235 | pthread_mutex_destroy(&log_lock);
|
---|
236 | free(log_txt);
|
---|
237 | log_txt = NULL;
|
---|
238 | }
|
---|
239 | if (use_stdout || use_syslog || !fp) return;
|
---|
240 | fclose(fp);
|
---|
241 | fp=(FILE *)0;
|
---|
242 | }
|
---|
243 |
|
---|
244 | void cs_debug(char *fmt,...)
|
---|
245 | {
|
---|
246 | // cs_log("cs_debug called, cs_ptyp=%d, cs_dblevel=%d, %d", cs_ptyp, client[cs_idx].dbglvl ,cs_ptyp & client[cs_idx].dbglvl);
|
---|
247 | if (log_txt && client[cs_idx].dbglvl & cs_ptyp)
|
---|
248 | {
|
---|
249 | pthread_mutex_lock(&log_lock);
|
---|
250 | get_log_header(1, log_txt);
|
---|
251 | va_list params;
|
---|
252 | va_start(params, fmt);
|
---|
253 | vsprintf(log_txt+11, fmt, params);
|
---|
254 | va_end(params);
|
---|
255 | write_to_log(-1, log_txt);
|
---|
256 | pthread_mutex_unlock(&log_lock);
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | void cs_debug_mask(unsigned short mask, char *fmt,...)
|
---|
261 | {
|
---|
262 | if (log_txt && client[cs_idx].dbglvl & mask)
|
---|
263 | {
|
---|
264 | pthread_mutex_lock(&log_lock);
|
---|
265 | get_log_header(1, log_txt);
|
---|
266 | va_list params;
|
---|
267 | va_start(params, fmt);
|
---|
268 | vsprintf(log_txt+11, fmt, params);
|
---|
269 | va_end(params);
|
---|
270 | write_to_log(-1, log_txt);
|
---|
271 | pthread_mutex_unlock(&log_lock);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | void cs_debug_nolf(char *fmt,...)
|
---|
276 | {
|
---|
277 | if (log_txt && client[cs_idx].dbglvl & cs_ptyp)
|
---|
278 | {
|
---|
279 | pthread_mutex_lock(&log_lock);
|
---|
280 | va_list params;
|
---|
281 | va_start(params, fmt);
|
---|
282 | vsprintf(log_txt, fmt, params);
|
---|
283 | va_end(params);
|
---|
284 | if(!memcmp(log_txt,"\n", 1)) {
|
---|
285 | number_of_chars_printed = 0;
|
---|
286 | }
|
---|
287 | else
|
---|
288 | number_of_chars_printed++;
|
---|
289 | write_to_log(number_of_chars_printed, log_txt);
|
---|
290 | pthread_mutex_unlock(&log_lock);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | void cs_dump(uchar *buf, int n, char *fmt, ...)
|
---|
295 | {
|
---|
296 | if (!log_txt)
|
---|
297 | return;
|
---|
298 | pthread_mutex_lock(&log_lock);
|
---|
299 | int i;
|
---|
300 |
|
---|
301 | if( fmt )
|
---|
302 | {
|
---|
303 | get_log_header(1, log_txt);
|
---|
304 | va_list params;
|
---|
305 | va_start(params, fmt);
|
---|
306 | vsprintf(log_txt+11, fmt, params);
|
---|
307 | va_end(params);
|
---|
308 | write_to_log(-1, log_txt);
|
---|
309 | //printf("LOG: %s\n", txt); fflush(stdout);
|
---|
310 | }
|
---|
311 |
|
---|
312 | for( i=0; i<n; i+=16 )
|
---|
313 | {
|
---|
314 | get_log_header(0, log_txt);
|
---|
315 | sprintf(log_txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
316 | write_to_log(-1, log_txt);
|
---|
317 | }
|
---|
318 | pthread_mutex_unlock(&log_lock);
|
---|
319 | }
|
---|
320 |
|
---|
321 | void cs_ddump(uchar *buf, int n, char *fmt, ...)
|
---|
322 | {
|
---|
323 | if (!log_txt)
|
---|
324 | return;
|
---|
325 | pthread_mutex_lock(&log_lock);
|
---|
326 | int i;
|
---|
327 |
|
---|
328 | //if (((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp) && (fmt))
|
---|
329 | if ((cs_ptyp & client[cs_idx].dbglvl) && (fmt))
|
---|
330 | {
|
---|
331 | get_log_header(1, log_txt);
|
---|
332 | va_list params;
|
---|
333 | va_start(params, fmt);
|
---|
334 | vsprintf(log_txt+11, fmt, params);
|
---|
335 | va_end(params);
|
---|
336 | write_to_log(-1, log_txt);
|
---|
337 | //printf("LOG: %s\n", txt); fflush(stdout);
|
---|
338 | }
|
---|
339 | //if (((cs_ptyp | D_DUMP) & client[cs_idx].dbglvl)==(cs_ptyp | D_DUMP))
|
---|
340 | if (cs_ptyp & client[cs_idx].dbglvl)
|
---|
341 | {
|
---|
342 | for (i=0; i<n; i+=16)
|
---|
343 | {
|
---|
344 | get_log_header(0, log_txt);
|
---|
345 | sprintf(log_txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
346 | write_to_log(-1, log_txt);
|
---|
347 | }
|
---|
348 | }
|
---|
349 | pthread_mutex_unlock(&log_lock);
|
---|
350 | }
|
---|
351 |
|
---|
352 | void cs_ddump_mask(unsigned short mask, uchar *buf, int n, char *fmt, ...)
|
---|
353 | {
|
---|
354 | if(!log_txt)
|
---|
355 | return;
|
---|
356 | pthread_mutex_lock(&log_lock);
|
---|
357 | int i;
|
---|
358 |
|
---|
359 | //if (((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp) && (fmt))
|
---|
360 | if ((mask & client[cs_idx].dbglvl) && (fmt))
|
---|
361 | {
|
---|
362 | get_log_header(1, log_txt);
|
---|
363 | va_list params;
|
---|
364 | va_start(params, fmt);
|
---|
365 | vsprintf(log_txt+11, fmt, params);
|
---|
366 | va_end(params);
|
---|
367 | write_to_log(-1, log_txt);
|
---|
368 | //printf("LOG: %s\n", txt); fflush(stdout);
|
---|
369 | }
|
---|
370 | //if (((cs_ptyp | D_DUMP) & client[cs_idx].dbglvl)==(cs_ptyp | D_DUMP))
|
---|
371 | if (mask & client[cs_idx].dbglvl)
|
---|
372 | {
|
---|
373 | for (i=0; i<n; i+=16)
|
---|
374 | {
|
---|
375 | get_log_header(0, log_txt);
|
---|
376 | sprintf(log_txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
377 | write_to_log(-1, log_txt);
|
---|
378 | }
|
---|
379 | }
|
---|
380 | pthread_mutex_unlock(&log_lock);
|
---|
381 | }
|
---|
382 |
|
---|
383 | int cs_init_statistics(char *file)
|
---|
384 | {
|
---|
385 | if ((!fps) && (file != NULL))
|
---|
386 | {
|
---|
387 | if ((fps=fopen(file, "a+"))<=(FILE *)0)
|
---|
388 | {
|
---|
389 | fps=(FILE *)0;
|
---|
390 | cs_log("couldn't open statistics file: %s", file);
|
---|
391 | }
|
---|
392 | }
|
---|
393 | return(fps<=(FILE *)0);
|
---|
394 | }
|
---|
395 |
|
---|
396 | void cs_statistics(int idx)
|
---|
397 | {
|
---|
398 | if (!cfg->disableuserfile){
|
---|
399 | time_t t;
|
---|
400 | struct tm *lt;
|
---|
401 | char buf[512];
|
---|
402 |
|
---|
403 | float cwps;
|
---|
404 |
|
---|
405 | time(&t);
|
---|
406 | lt=localtime(&t);
|
---|
407 | if (client[idx].cwfound+client[idx].cwnot>0)
|
---|
408 | {
|
---|
409 | cwps=client[idx].last-client[idx].login;
|
---|
410 | cwps/=client[idx].cwfound+client[idx].cwnot;
|
---|
411 | }
|
---|
412 | else
|
---|
413 | cwps=0;
|
---|
414 |
|
---|
415 | char *channel ="";
|
---|
416 | if(cfg->mon_appendchaninfo)
|
---|
417 | channel = get_servicename(client[idx].last_srvid,client[idx].last_caid);
|
---|
418 |
|
---|
419 | int lsec;
|
---|
420 | if ((client[idx].last_caid == 0xFFFF) && (client[idx].last_srvid == 0xFFFF))
|
---|
421 | lsec = client[idx].last - client[idx].login; //client leave calc total duration
|
---|
422 | else
|
---|
423 | lsec = client[idx].last - client[idx].lastswitch;
|
---|
424 |
|
---|
425 | int secs = 0, fullmins = 0, mins = 0, fullhours = 0;
|
---|
426 |
|
---|
427 | if((lsec > 0) && (lsec < 1000000)) {
|
---|
428 | secs = lsec % 60;
|
---|
429 | if (lsec > 60) {
|
---|
430 | fullmins = lsec / 60;
|
---|
431 | mins = fullmins % 60;
|
---|
432 | if(fullmins > 60) {
|
---|
433 | fullhours = fullmins / 60;
|
---|
434 | }
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | /* statistics entry start with 's' to filter it out on other end of pipe
|
---|
439 | * so we can use the same Pipe as Log
|
---|
440 | */
|
---|
441 | sprintf(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",
|
---|
442 | lt->tm_mday, lt->tm_mon+1, lt->tm_year%100,
|
---|
443 | lt->tm_hour, lt->tm_min, lt->tm_sec, cwps,
|
---|
444 | client[idx].usr[0] ? client[idx].usr : "-",
|
---|
445 | cs_inet_ntoa(client[idx].ip),
|
---|
446 | client[idx].port,
|
---|
447 | client[idx].cwfound,
|
---|
448 | client[idx].cwcache,
|
---|
449 | client[idx].cwnot,
|
---|
450 | client[idx].cwignored,
|
---|
451 | client[idx].cwtout,
|
---|
452 | client[idx].cwtun,
|
---|
453 | client[idx].login,
|
---|
454 | client[idx].last,
|
---|
455 | fullhours, mins, secs,
|
---|
456 | ph[client[idx].ctyp].desc,
|
---|
457 | client[idx].last_caid,
|
---|
458 | client[idx].last_srvid,
|
---|
459 | channel);
|
---|
460 |
|
---|
461 | if ((*log_fd) && (client[cs_idx].typ != 'l') && (client[cs_idx].typ != 'a'))
|
---|
462 | write_to_pipe(*log_fd, PIP_ID_LOG, (uchar *) buf, strlen(buf));
|
---|
463 | else
|
---|
464 | cs_write_log(buf);
|
---|
465 | }
|
---|
466 | }
|
---|