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