1 | #include "globals.h"
|
---|
2 | #include <syslog.h>
|
---|
3 |
|
---|
4 | char logfile[256]=CS_LOGFILE;
|
---|
5 |
|
---|
6 | static FILE *fp=(FILE *)0;
|
---|
7 | static FILE *fps=(FILE *)0;
|
---|
8 | static int use_syslog=0;
|
---|
9 | static int use_stdout=0;
|
---|
10 |
|
---|
11 | #ifdef CS_ANTICASC
|
---|
12 | FILE *fpa=(FILE *)0;
|
---|
13 | int use_ac_log=0;
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | static void switch_log(char* file, FILE **f, int (*pfinit)(char*))
|
---|
17 | {
|
---|
18 | if( cfg->max_log_size && mcl)
|
---|
19 | {
|
---|
20 | struct stat stlog;
|
---|
21 | if( stat(file, &stlog)!=0 )
|
---|
22 | {
|
---|
23 | fprintf(stderr, "stat('%s',..) failed (errno=%d)\n", file, errno);
|
---|
24 | return;
|
---|
25 | }
|
---|
26 |
|
---|
27 | if( stlog.st_size >= cfg->max_log_size*1024 )
|
---|
28 | {
|
---|
29 | int rc;
|
---|
30 | char prev_log[128];
|
---|
31 | sprintf(prev_log, "%s-prev", file);
|
---|
32 | fprintf(*f, "switch log file\n");
|
---|
33 | fflush(*f);
|
---|
34 | fclose(*f);
|
---|
35 | *f=(FILE *)0;
|
---|
36 | rc=rename(file, prev_log);
|
---|
37 | if( rc!=0 ) {
|
---|
38 | fprintf(stderr, "rename(%s, %s) failed (errno=%d)\n",
|
---|
39 | file, prev_log, errno);
|
---|
40 | }
|
---|
41 | else if( pfinit(file))
|
---|
42 | cs_exit(0);
|
---|
43 |
|
---|
44 | int i;
|
---|
45 | for( i=1; i<CS_MAXPID; i++ ) {
|
---|
46 | if( client[i].pid && client[i].typ=='c' && client[i].usr[0] ) {
|
---|
47 | kill(client[i].pid, SIGUSR2);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | void cs_write_log(char *txt)
|
---|
56 | {
|
---|
57 | #ifdef CS_ANTICASC
|
---|
58 | if( use_ac_log && fpa )
|
---|
59 | {
|
---|
60 | switch_log(cfg->ac_logfile, &fpa, ac_init_log);
|
---|
61 | fprintf(fpa, "%s", txt);
|
---|
62 | fflush(fpa);
|
---|
63 | }
|
---|
64 | else
|
---|
65 | #endif
|
---|
66 | if (fp || use_stdout)
|
---|
67 | {
|
---|
68 | if( !use_stdout && !use_syslog) switch_log(logfile, &fp, cs_init_log);
|
---|
69 | fprintf(fp, "%s", txt);
|
---|
70 | fflush(fp);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | int cs_init_log(char *file)
|
---|
75 | {
|
---|
76 | static char *head = ">> OSCam << cardserver started";
|
---|
77 |
|
---|
78 | if (!strcmp(file, "stdout"))
|
---|
79 | {
|
---|
80 | use_stdout=1;
|
---|
81 | fp=stdout;
|
---|
82 | cs_log(head);
|
---|
83 | return(0);
|
---|
84 | }
|
---|
85 | if (strcmp(file, "syslog"))
|
---|
86 | {
|
---|
87 | if (!fp)
|
---|
88 | {
|
---|
89 | if ((fp=fopen(file, "a+"))<=(FILE *)0)
|
---|
90 | {
|
---|
91 | fp=(FILE *)0;
|
---|
92 | fprintf(stderr, "couldn't open logfile: %s (errno %d)\n", file, errno);
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | time_t t;
|
---|
97 | char line[80];
|
---|
98 | memset(line, '-', sizeof(line));
|
---|
99 | line[(sizeof(line)/sizeof(char))-1]='\0';
|
---|
100 | time(&t);
|
---|
101 | fprintf(fp, "\n%s\n>> OSCam << cardserver started at %s%s\n", line, ctime(&t), line);
|
---|
102 | cs_log_config();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return(fp<=(FILE *)0);
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | openlog("oscam", LOG_NDELAY, LOG_DAEMON);
|
---|
110 | use_syslog=1;
|
---|
111 | cs_log(head);
|
---|
112 | return(0);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | static char *get_log_header(int m, char *txt)
|
---|
117 | {
|
---|
118 | if (m)
|
---|
119 | {
|
---|
120 | sprintf(txt, "%6d ", getpid());
|
---|
121 | if (cs_idx)
|
---|
122 | switch (client[cs_idx].typ)
|
---|
123 | {
|
---|
124 | case 'r':
|
---|
125 | case 'p': sprintf(txt+7, "%c%02d ", client[cs_idx].typ, cs_idx-1);
|
---|
126 | break;
|
---|
127 | case 'm':
|
---|
128 | case 'c': sprintf(txt+7, "%c%02d ", client[cs_idx].typ, cs_idx-cdiff);
|
---|
129 | break;
|
---|
130 | #ifdef CS_ANTICASC
|
---|
131 | case 'a':
|
---|
132 | #endif
|
---|
133 | case 'l':
|
---|
134 | case 'n': sprintf(txt+7, "%c " , client[cs_idx].typ);
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | else
|
---|
138 | strcpy(txt+7, "s ");
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | sprintf(txt, "%-11.11s", "");
|
---|
143 | }
|
---|
144 | return(txt);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static void write_to_log(int flag, char *txt)
|
---|
148 | {
|
---|
149 | //static int logcounter=0;
|
---|
150 | int i;
|
---|
151 | time_t t;
|
---|
152 | struct tm *lt;
|
---|
153 | char buf[512], sbuf[16];
|
---|
154 |
|
---|
155 | get_log_header(flag, sbuf);
|
---|
156 | memcpy(txt, sbuf, 11);
|
---|
157 |
|
---|
158 | if (use_syslog && !use_ac_log) // system-logfile
|
---|
159 | syslog(LOG_INFO, "%s", txt);
|
---|
160 | else {
|
---|
161 | time(&t);
|
---|
162 | lt=localtime(&t);
|
---|
163 | sprintf(buf, "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s\n",
|
---|
164 | lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
|
---|
165 | lt->tm_hour, lt->tm_min, lt->tm_sec, txt);
|
---|
166 |
|
---|
167 | /*
|
---|
168 | #ifdef CS_ANTICASC
|
---|
169 | if (fp || fpa || use_stdout) // logfile
|
---|
170 | #else
|
---|
171 | if (fp || use_stdout) // logfile
|
---|
172 | #endif
|
---|
173 | {
|
---|
174 | */
|
---|
175 | if ((*log_fd) && (client[cs_idx].typ!='l') && (client[cs_idx].typ!='a'))
|
---|
176 | write_to_pipe(*log_fd, PIP_ID_LOG, (uchar *) buf+8, strlen(buf+8));
|
---|
177 | else
|
---|
178 | cs_write_log(buf+8);
|
---|
179 | // }
|
---|
180 | }
|
---|
181 | store_logentry(buf);
|
---|
182 |
|
---|
183 | for (i=0; i<CS_MAXPID; i++) // monitor-clients
|
---|
184 | {
|
---|
185 | if ((client[i].pid) && (client[i].log))
|
---|
186 | {
|
---|
187 | if (client[i].monlvl<2)
|
---|
188 | {
|
---|
189 | if ((client[cs_idx].typ!='c') && (client[cs_idx].typ!='m'))
|
---|
190 | continue;
|
---|
191 | if (strcmp(client[cs_idx].usr, client[i].usr))
|
---|
192 | continue;
|
---|
193 | }
|
---|
194 | sprintf(sbuf, "%03d", client[i].logcounter);
|
---|
195 | client[i].logcounter=(client[i].logcounter+1) % 1000;
|
---|
196 | memcpy(buf+4, sbuf, 3);
|
---|
197 | monitor_send_idx(i, buf);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | void cs_log(char *fmt,...)
|
---|
203 | {
|
---|
204 | char txt[256+11];
|
---|
205 |
|
---|
206 | va_list params;
|
---|
207 | va_start(params, fmt);
|
---|
208 | vsprintf(txt+11, fmt, params);
|
---|
209 | va_end(params);
|
---|
210 | write_to_log(1, txt);
|
---|
211 | }
|
---|
212 |
|
---|
213 | void cs_close_log(void)
|
---|
214 | {
|
---|
215 | if (use_stdout || use_syslog || !fp) return;
|
---|
216 | fclose(fp);
|
---|
217 | fp=(FILE *)0;
|
---|
218 | }
|
---|
219 |
|
---|
220 | void cs_debug(char *fmt,...)
|
---|
221 | {
|
---|
222 | char txt[256];
|
---|
223 |
|
---|
224 | // cs_log("cs_debug called, cs_ptyp=%d, cs_dblevel=%d, %d", cs_ptyp, client[cs_idx].dbglvl ,cs_ptyp & client[cs_idx].dbglvl);
|
---|
225 |
|
---|
226 | // if ((cs_ptyp & cs_dblevel)==cs_ptyp)
|
---|
227 | if ((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp)
|
---|
228 | {
|
---|
229 | va_list params;
|
---|
230 | va_start(params, fmt);
|
---|
231 | vsprintf(txt+11, fmt, params);
|
---|
232 | va_end(params);
|
---|
233 | write_to_log(1, txt);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | void cs_dump(uchar *buf, int n, char *fmt, ...)
|
---|
238 | {
|
---|
239 | int i;
|
---|
240 | char txt[512];
|
---|
241 |
|
---|
242 | if( fmt )
|
---|
243 | cs_log(fmt);
|
---|
244 |
|
---|
245 | for( i=0; i<n; i+=16 )
|
---|
246 | {
|
---|
247 | sprintf(txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
248 | write_to_log(i==0, txt);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | void cs_ddump(uchar *buf, int n, char *fmt, ...)
|
---|
253 | {
|
---|
254 | int i;
|
---|
255 | char txt[512];
|
---|
256 |
|
---|
257 | // if (((cs_ptyp & cs_dblevel)==cs_ptyp) && (fmt))
|
---|
258 | if (((cs_ptyp & client[cs_idx].dbglvl)==cs_ptyp) && (fmt))
|
---|
259 | {
|
---|
260 | va_list params;
|
---|
261 | va_start(params, fmt);
|
---|
262 | vsprintf(txt+11, fmt, params);
|
---|
263 | va_end(params);
|
---|
264 | write_to_log(1, txt);
|
---|
265 | //printf("LOG: %s\n", txt); fflush(stdout);
|
---|
266 | }
|
---|
267 | // if (((cs_ptyp | D_DUMP) & cs_dblevel)==(cs_ptyp | D_DUMP))
|
---|
268 | if (((cs_ptyp | D_DUMP) & client[cs_idx].dbglvl)==(cs_ptyp | D_DUMP))
|
---|
269 | {
|
---|
270 | for (i=0; i<n; i+=16)
|
---|
271 | {
|
---|
272 | sprintf(txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));
|
---|
273 | write_to_log(i==0, txt);
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | int cs_init_statistics(char *file)
|
---|
279 | {
|
---|
280 | if ((!fps) && (file[0]))
|
---|
281 | {
|
---|
282 | if ((fps=fopen(file, "a"))<=(FILE *)0)
|
---|
283 | {
|
---|
284 | fps=(FILE *)0;
|
---|
285 | cs_log("couldn't open statistics file: %s", file);
|
---|
286 | }
|
---|
287 | }
|
---|
288 | return(fps<=(FILE *)0);
|
---|
289 | }
|
---|
290 |
|
---|
291 | void cs_statistics(int idx)
|
---|
292 | {
|
---|
293 | time_t t;
|
---|
294 | struct tm *lt;
|
---|
295 |
|
---|
296 | if (fps)
|
---|
297 | {
|
---|
298 | float cwps;
|
---|
299 |
|
---|
300 | switch_log(cfg->usrfile, &fps, cs_init_statistics);
|
---|
301 | time(&t);
|
---|
302 | lt=localtime(&t);
|
---|
303 | if (client[idx].cwfound+client[idx].cwnot>0)
|
---|
304 | {
|
---|
305 | cwps=client[idx].last-client[idx].login;
|
---|
306 | cwps/=client[idx].cwfound+client[idx].cwnot;
|
---|
307 | }
|
---|
308 | else
|
---|
309 | cwps=0;
|
---|
310 |
|
---|
311 | fprintf(fps, "%02d.%02d.%02d %02d:%02d:%02d %3.1f %s %s %d %d %d %d %ld %ld %s\n",
|
---|
312 | lt->tm_mday, lt->tm_mon+1, lt->tm_year%100,
|
---|
313 | lt->tm_hour, lt->tm_min, lt->tm_sec, cwps,
|
---|
314 | client[idx].usr[0] ? client[idx].usr : "-",
|
---|
315 | cs_inet_ntoa(client[idx].ip), client[idx].port,
|
---|
316 | client[idx].cwfound, client[idx].cwcache, client[idx].cwnot,
|
---|
317 | client[idx].login, client[idx].last,
|
---|
318 | ph[client[idx].ctyp].desc);
|
---|
319 | fflush(fps);
|
---|
320 | }
|
---|
321 | }
|
---|