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