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