1 | #include "globals.h"
|
---|
2 | #include <getopt.h>
|
---|
3 |
|
---|
4 | #include "csctapi/cardreaders.h"
|
---|
5 | #include "modules.h"
|
---|
6 | #include "readers.h"
|
---|
7 |
|
---|
8 | #include "extapi/coolapi.h"
|
---|
9 | #include "module-anticasc.h"
|
---|
10 | #include "module-cacheex.h"
|
---|
11 | #include "module-cccam.h"
|
---|
12 | #include "module-dvbapi-azbox.h"
|
---|
13 | #include "module-dvbapi-mca.h"
|
---|
14 | #include "module-ird-guess.h"
|
---|
15 | #include "module-lcd.h"
|
---|
16 | #include "module-led.h"
|
---|
17 | #include "module-stat.h"
|
---|
18 | #include "module-webif.h"
|
---|
19 | #include "module-cw-cycle-check.h"
|
---|
20 | #include "oscam-chk.h"
|
---|
21 | #include "oscam-client.h"
|
---|
22 | #include "oscam-config.h"
|
---|
23 | #include "oscam-ecm.h"
|
---|
24 | #include "oscam-emm.h"
|
---|
25 | #include "oscam-files.h"
|
---|
26 | #include "oscam-garbage.h"
|
---|
27 | #include "oscam-lock.h"
|
---|
28 | #include "oscam-net.h"
|
---|
29 | #include "oscam-reader.h"
|
---|
30 | #include "oscam-string.h"
|
---|
31 | #include "oscam-time.h"
|
---|
32 | #include "oscam-work.h"
|
---|
33 | #include "reader-common.h"
|
---|
34 |
|
---|
35 | /*****************************************************************************
|
---|
36 | Globals
|
---|
37 | *****************************************************************************/
|
---|
38 | const char *syslog_ident = "oscam";
|
---|
39 | char *oscam_pidfile = NULL;
|
---|
40 | char default_pidfile[64];
|
---|
41 |
|
---|
42 | int32_t exit_oscam=0;
|
---|
43 | static struct s_module modules[CS_MAX_MOD];
|
---|
44 | struct s_cardsystem cardsystems[CS_MAX_MOD];
|
---|
45 | struct s_cardreader cardreaders[CS_MAX_MOD];
|
---|
46 |
|
---|
47 | struct s_client * first_client = NULL; //Pointer to clients list, first client is master
|
---|
48 | struct s_reader * first_active_reader = NULL; //list of active readers (enable=1 deleted = 0)
|
---|
49 | LLIST * configured_readers = NULL; //list of all (configured) readers
|
---|
50 |
|
---|
51 | uint16_t len4caid[256]; // table for guessing caid (by len)
|
---|
52 | char cs_confdir[128]=CS_CONFDIR;
|
---|
53 | uint16_t cs_dblevel=0; // Debug Level
|
---|
54 | int32_t thread_pipe[2] = {0, 0};
|
---|
55 | int8_t cs_restart_mode=1; //Restartmode: 0=off, no restart fork, 1=(default)restart fork, restart by webif, 2=like=1, but also restart on segfaults
|
---|
56 | uint8_t cs_http_use_utf8 = 0;
|
---|
57 | int8_t cs_capture_SEGV=0;
|
---|
58 | int8_t cs_dump_stack=0;
|
---|
59 | uint16_t cs_waittime = 60;
|
---|
60 | char cs_tmpdir[200]={0x00};
|
---|
61 | CS_MUTEX_LOCK system_lock;
|
---|
62 | CS_MUTEX_LOCK config_lock;
|
---|
63 | CS_MUTEX_LOCK gethostbyname_lock;
|
---|
64 | CS_MUTEX_LOCK clientlist_lock;
|
---|
65 | CS_MUTEX_LOCK readerlist_lock;
|
---|
66 | CS_MUTEX_LOCK fakeuser_lock;
|
---|
67 | CS_MUTEX_LOCK ecmcache_lock;
|
---|
68 | CS_MUTEX_LOCK readdir_lock;
|
---|
69 | CS_MUTEX_LOCK cwcycle_lock;
|
---|
70 | CS_MUTEX_LOCK hitcache_lock;
|
---|
71 | pthread_key_t getclient;
|
---|
72 | static int32_t bg;
|
---|
73 | static int32_t gbdb;
|
---|
74 | static int32_t max_pending = 32;
|
---|
75 |
|
---|
76 | struct s_client *timecheck_client;
|
---|
77 |
|
---|
78 | //Cache for ecms, cws and rcs:
|
---|
79 | struct ecm_request_t *ecmcwcache = NULL;
|
---|
80 | uint32_t ecmcwcache_size = 0;
|
---|
81 |
|
---|
82 | struct s_config cfg;
|
---|
83 |
|
---|
84 | int log_remove_sensitive = 1;
|
---|
85 |
|
---|
86 | char *prog_name = NULL;
|
---|
87 | char *processUsername = NULL;
|
---|
88 |
|
---|
89 | /*****************************************************************************
|
---|
90 | Statics
|
---|
91 | *****************************************************************************/
|
---|
92 | #define _check(CONFIG_VAR, text) \
|
---|
93 | do { \
|
---|
94 | if (config_enabled(CONFIG_VAR)) \
|
---|
95 | printf(" %s", text); \
|
---|
96 | } while(0)
|
---|
97 |
|
---|
98 | /* Prints usage information and information about the built-in modules. */
|
---|
99 | static void show_usage(void)
|
---|
100 | {
|
---|
101 | printf("%s",
|
---|
102 | " ___ ____ ___\n"
|
---|
103 | " / _ \\/ ___| / __|__ _ _ __ ___\n"
|
---|
104 | "| | | \\___ \\| | / _` | '_ ` _ \\\n"
|
---|
105 | "| |_| |___) | |_| (_| | | | | | |\n"
|
---|
106 | " \\___/|____/ \\___\\__,_|_| |_| |_|\n\n");
|
---|
107 | printf("OSCam cardserver v%s, build r%s (%s)\n", CS_VERSION, CS_SVN_VERSION, CS_TARGET);
|
---|
108 | printf("Copyright (C) 2009-2013 OSCam developers.\n");
|
---|
109 | printf("This program is distributed under GPLv3.\n");
|
---|
110 | printf("OSCam is based on Streamboard mp-cardserver v0.9d written by dukat\n");
|
---|
111 | printf("Visit http://www.streamboard.tv/oscam/ for more details.\n\n");
|
---|
112 |
|
---|
113 | printf(" Features :");
|
---|
114 | _check(WEBIF, "webif");
|
---|
115 | _check(TOUCH, "touch");
|
---|
116 | _check(MODULE_MONITOR, "monitor");
|
---|
117 | _check(WITH_SSL, "ssl");
|
---|
118 | if (!config_enabled(WITH_STAPI))
|
---|
119 | _check(HAVE_DVBAPI, "dvbapi");
|
---|
120 | else
|
---|
121 | _check(WITH_STAPI, "dvbapi_stapi");
|
---|
122 | _check(IRDETO_GUESSING, "irdeto-guessing");
|
---|
123 | _check(CS_ANTICASC, "anticascading");
|
---|
124 | _check(WITH_DEBUG, "debug");
|
---|
125 | _check(WITH_LB, "loadbalancing");
|
---|
126 | _check(CW_CYCLE_CHECK, "cw-cycle-check");
|
---|
127 | _check(LCDSUPPORT, "lcd");
|
---|
128 | _check(LEDSUPPORT, "led");
|
---|
129 | printf("\n");
|
---|
130 |
|
---|
131 | printf(" Protocols :");
|
---|
132 | _check(MODULE_CAMD33, "camd33");
|
---|
133 | _check(MODULE_CAMD35, "camd35_udp");
|
---|
134 | _check(MODULE_CAMD35_TCP, "camd35_tcp");
|
---|
135 | _check(MODULE_NEWCAMD, "newcamd");
|
---|
136 | _check(MODULE_CCCAM, "cccam");
|
---|
137 | _check(MODULE_CCCSHARE, "cccam_share");
|
---|
138 | _check(MODULE_PANDORA, "pandora");
|
---|
139 | _check(MODULE_GHTTP, "ghttp");
|
---|
140 | _check(CS_CACHEEX, "cache-exchange");
|
---|
141 | _check(MODULE_GBOX, "gbox");
|
---|
142 | _check(MODULE_RADEGAST, "radegast");
|
---|
143 | _check(MODULE_SERIAL, "serial");
|
---|
144 | _check(MODULE_CONSTCW, "constcw");
|
---|
145 | printf("\n");
|
---|
146 |
|
---|
147 | printf(" Readers :");
|
---|
148 | _check(READER_NAGRA, "nagra");
|
---|
149 | _check(READER_IRDETO, "irdeto");
|
---|
150 | _check(READER_CONAX, "conax");
|
---|
151 | _check(READER_CRYPTOWORKS, "cryptoworks");
|
---|
152 | _check(READER_SECA, "seca");
|
---|
153 | _check(READER_VIACCESS, "viaccess");
|
---|
154 | _check(READER_VIDEOGUARD, "videoguard");
|
---|
155 | _check(READER_DRE, "dre");
|
---|
156 | _check(READER_TONGFANG, "tongfang");
|
---|
157 | _check(READER_BULCRYPT, "bulcrypt");
|
---|
158 | _check(READER_GRIFFIN, "griffin");
|
---|
159 | _check(READER_DGCRYPT, "dgcrypt");
|
---|
160 | printf("\n");
|
---|
161 |
|
---|
162 | printf(" CardReaders:");
|
---|
163 | _check(CARDREADER_PHOENIX, "phoenix");
|
---|
164 | _check(CARDREADER_INTERNAL_AZBOX, "internal_azbox");
|
---|
165 | _check(CARDREADER_INTERNAL_COOLAPI, "internal_coolapi");
|
---|
166 | _check(CARDREADER_INTERNAL_SCI, "internal_sci");
|
---|
167 | _check(CARDREADER_SC8IN1, "sc8in1");
|
---|
168 | _check(CARDREADER_MP35, "mp35");
|
---|
169 | _check(CARDREADER_SMARGO, "smargo");
|
---|
170 | _check(CARDREADER_PCSC, "pcsc");
|
---|
171 | _check(CARDREADER_SMART, "smartreader");
|
---|
172 | _check(CARDREADER_DB2COM, "db2com");
|
---|
173 | _check(CARDREADER_STAPI, "stapi");
|
---|
174 | printf("\n");
|
---|
175 | printf(" ConfigDir : %s\n", CS_CONFDIR);
|
---|
176 | printf("\n");
|
---|
177 | printf(" Usage: oscam [parameters]\n");
|
---|
178 | printf("\n Directories:\n");
|
---|
179 | printf(" -c, --config-dir <dir> | Read configuration files from <dir>.\n");
|
---|
180 | printf(" . Default: %s\n", CS_CONFDIR);
|
---|
181 | printf(" -t, --temp-dir <dir> | Set temporary directory to <dir>.\n");
|
---|
182 | #if defined(__CYGWIN__)
|
---|
183 | printf(" . Default: (OS-TMP)\n");
|
---|
184 | #else
|
---|
185 | printf(" . Default: /tmp/.oscam\n");
|
---|
186 | #endif
|
---|
187 | printf("\n Startup:\n");
|
---|
188 | printf(" -b, --daemon | Start in the background as daemon.\n");
|
---|
189 | printf(" -B, --pidfile <pidfile> | Create pidfile when starting.\n");
|
---|
190 | if (config_enabled(WEBIF)) {
|
---|
191 | printf(" -r, --restart <level> | Set restart level:\n");
|
---|
192 | printf(" . 0 - Restart disabled (exit on restart request).\n");
|
---|
193 | printf(" . 1 - WebIf restart is active (default).\n");
|
---|
194 | printf(" . 2 - Like 1, but also restart on segfaults.\n");
|
---|
195 | }
|
---|
196 | printf(" -w, --wait <secs> | Set how much seconds to wait at startup for the\n");
|
---|
197 | printf(" . system clock to be set correctly. Default: 60\n");
|
---|
198 | printf("\n Logging:\n");
|
---|
199 | printf(" -I, --syslog-ident <ident> | Set syslog ident. Default: oscam\n");
|
---|
200 | printf(" -S, --show-sensitive | Do not filter sensitive info (card serials, boxids)\n");
|
---|
201 | printf(" . from the logs.\n");
|
---|
202 | printf(" -d, --debug <level> | Set debug level mask used for logging:\n");
|
---|
203 | printf(" . 0 - No extra debugging (default).\n");
|
---|
204 | printf(" . 1 - Detailed error messages.\n");
|
---|
205 | printf(" . 2 - ATR parsing info, ECM, EMM and CW dumps.\n");
|
---|
206 | printf(" . 4 - Traffic from/to the reader.\n");
|
---|
207 | printf(" . 8 - Traffic from/to the clients.\n");
|
---|
208 | printf(" . 16 - Traffic to the reader-device on IFD layer.\n");
|
---|
209 | printf(" . 32 - Traffic to the reader-device on I/O layer.\n");
|
---|
210 | printf(" . 64 - EMM logging.\n");
|
---|
211 | printf(" . 128 - DVBAPI logging.\n");
|
---|
212 | printf(" . 256 - Loadbalancer logging.\n");
|
---|
213 | printf(" . 512 - CACHEEX logging.\n");
|
---|
214 | printf(" . 1024 - Client ECM logging.\n");
|
---|
215 | printf(" . 65535 - Debug all.\n");
|
---|
216 | printf("\n Settings:\n");
|
---|
217 | printf(" -p, --pending-ecm <num> | Set the maximum number of pending ECM packets.\n");
|
---|
218 | printf(" . Default: 32 Max: 255\n");
|
---|
219 | if (config_enabled(WEBIF)) {
|
---|
220 | printf(" -u, --utf8 | Enable WebIf support for UTF-8 charset.\n");
|
---|
221 | }
|
---|
222 | printf("\n Debug parameters:\n");
|
---|
223 | printf(" -a, --crash-dump | Write oscam.crash file on segfault. This option\n");
|
---|
224 | printf(" . needs GDB to be installed and OSCam executable to\n");
|
---|
225 | printf(" . contain the debug information (run oscam-XXXX.debug)\n");
|
---|
226 | printf(" -s, --capture-segfaults | Capture segmentation faults.\n");
|
---|
227 | printf(" -g, --gcollect <mode> | Garbage collector debug mode:\n");
|
---|
228 | printf(" . 1 - Immediate free.\n");
|
---|
229 | printf(" . 2 - Check for double frees.\n");
|
---|
230 | printf("\n Information:\n");
|
---|
231 | printf(" -h, --help | Show command line help text.\n");
|
---|
232 | printf(" -V, --build-info | Show OSCam binary configuration and version.\n");
|
---|
233 | }
|
---|
234 | #undef _check
|
---|
235 |
|
---|
236 | /* Keep the options sorted */
|
---|
237 | static const char short_options[] = "aB:bc:d:g:hI:p:r:Sst:uVw:";
|
---|
238 |
|
---|
239 | /* Keep the options sorted by short option */
|
---|
240 | static const struct option long_options[] = {
|
---|
241 | { "crash-dump", no_argument, NULL, 'a' },
|
---|
242 | { "pidfile", required_argument, NULL, 'B' },
|
---|
243 | { "daemon", no_argument, NULL, 'b' },
|
---|
244 | { "config-dir", required_argument, NULL, 'c' },
|
---|
245 | { "debug", required_argument, NULL, 'd' },
|
---|
246 | { "gcollect", required_argument, NULL, 'g' },
|
---|
247 | { "help", no_argument, NULL, 'h' },
|
---|
248 | { "syslog-ident", required_argument, NULL, 'I' },
|
---|
249 | { "pending-ecm", required_argument, NULL, 'p' },
|
---|
250 | { "restart", required_argument, NULL, 'r' },
|
---|
251 | { "show-sensitive", no_argument, NULL, 'S' },
|
---|
252 | { "capture-segfaults", no_argument, NULL, 's' },
|
---|
253 | { "temp-dir", required_argument, NULL, 't' },
|
---|
254 | { "utf8", no_argument, NULL, 'u' },
|
---|
255 | { "build-info", no_argument, NULL, 'V' },
|
---|
256 | { "wait", required_argument, NULL, 'w' },
|
---|
257 | { 0, 0, 0, 0 }
|
---|
258 | };
|
---|
259 |
|
---|
260 | static void write_versionfile(bool use_stdout);
|
---|
261 |
|
---|
262 | static void parse_cmdline_params(int argc, char **argv) {
|
---|
263 | int i;
|
---|
264 | while ((i = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF) {
|
---|
265 | if (i == '?')
|
---|
266 | fprintf(stderr, "ERROR: Unknown command line parameter: %s\n", argv[optind - 1]);
|
---|
267 | switch(i) {
|
---|
268 | case 'a': // --crash-dump
|
---|
269 | cs_dump_stack = 1;
|
---|
270 | break;
|
---|
271 | case 'B': // --pidfile
|
---|
272 | oscam_pidfile = optarg;
|
---|
273 | break;
|
---|
274 | case 'b': // --daemon
|
---|
275 | bg = 1;
|
---|
276 | break;
|
---|
277 | case 'c': // --config-dir
|
---|
278 | cs_strncpy(cs_confdir, optarg, sizeof(cs_confdir));
|
---|
279 | break;
|
---|
280 | case 'd': // --debug
|
---|
281 | cs_dblevel = atoi(optarg);
|
---|
282 | break;
|
---|
283 | case 'g': // --gcollect
|
---|
284 | gbdb = atoi(optarg);
|
---|
285 | break;
|
---|
286 | case 'h': // --help
|
---|
287 | show_usage();
|
---|
288 | exit(EXIT_SUCCESS);
|
---|
289 | break;
|
---|
290 | case 'I': // --syslog-ident
|
---|
291 | syslog_ident = optarg;
|
---|
292 | break;
|
---|
293 | case 'p': // --pending-ecm
|
---|
294 | max_pending = atoi(optarg) <= 0 ? 32 : MIN(atoi(optarg), 255);
|
---|
295 | break;
|
---|
296 | case 'r': // --restart
|
---|
297 | if (config_enabled(WEBIF)) {
|
---|
298 | cs_restart_mode = atoi(optarg);
|
---|
299 | }
|
---|
300 | break;
|
---|
301 | case 'S': // --show-sensitive
|
---|
302 | log_remove_sensitive = !log_remove_sensitive;
|
---|
303 | break;
|
---|
304 | case 's': // --capture-segfaults
|
---|
305 | cs_capture_SEGV = 1;
|
---|
306 | break;
|
---|
307 | case 't': { // --temp-dir
|
---|
308 | mkdir(optarg, S_IRWXU);
|
---|
309 | int j = open(optarg, O_RDONLY);
|
---|
310 | if (j >= 0) {
|
---|
311 | close(j);
|
---|
312 | cs_strncpy(cs_tmpdir, optarg, sizeof(cs_tmpdir));
|
---|
313 | } else {
|
---|
314 | printf("WARNING: Temp dir does not exist. Using default value.\n");
|
---|
315 | }
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | case 'u': // --utf8
|
---|
319 | if (config_enabled(WEBIF)) {
|
---|
320 | cs_http_use_utf8 = 1;
|
---|
321 | printf("WARNING: Web interface UTF-8 mode enabled. Carefully read documentation as bugs may arise.\n");
|
---|
322 | }
|
---|
323 | break;
|
---|
324 | case 'V': // --build-info
|
---|
325 | write_versionfile(true);
|
---|
326 | exit(EXIT_SUCCESS);
|
---|
327 | break;
|
---|
328 | case 'w': // --wait
|
---|
329 | cs_waittime = strtoul(optarg, NULL, 10);
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | #define write_conf(CONFIG_VAR, text) \
|
---|
336 | fprintf(fp, "%-30s %s\n", text ":", config_enabled(CONFIG_VAR) ? "yes" : "no")
|
---|
337 |
|
---|
338 | #define write_readerconf(CONFIG_VAR, text) \
|
---|
339 | fprintf(fp, "%-30s %s\n", text ":", config_enabled(CONFIG_VAR) ? "yes" : "no - no EMM support!")
|
---|
340 |
|
---|
341 | #define write_cardreaderconf(CONFIG_VAR, text) \
|
---|
342 | fprintf(fp, "%s%-19s %s\n", "cardreader_", text ":", config_enabled(CONFIG_VAR) ? "yes" : "no")
|
---|
343 |
|
---|
344 | static void write_versionfile(bool use_stdout) {
|
---|
345 | FILE *fp = stdout;
|
---|
346 | if (!use_stdout) {
|
---|
347 | char targetfile[256];
|
---|
348 | fp = fopen(get_tmp_dir_filename(targetfile, sizeof(targetfile), "oscam.version"), "w");
|
---|
349 | if (!fp) {
|
---|
350 | cs_log("Cannot open %s (errno=%d %s)", targetfile, errno, strerror(errno));
|
---|
351 | return;
|
---|
352 | }
|
---|
353 | struct tm st;
|
---|
354 | time_t now = time(NULL);
|
---|
355 | localtime_r(&now, &st);
|
---|
356 |
|
---|
357 | fprintf(fp, "Unix starttime: %ld\n", (long)now);
|
---|
358 | fprintf(fp, "Starttime: %02d.%02d.%04d %02d:%02d:%02d\n",
|
---|
359 | st.tm_mday, st.tm_mon + 1, st.tm_year + 1900,
|
---|
360 | st.tm_hour, st.tm_min, st.tm_sec);
|
---|
361 | }
|
---|
362 |
|
---|
363 | fprintf(fp, "Version: oscam-%s-r%s\n", CS_VERSION, CS_SVN_VERSION);
|
---|
364 |
|
---|
365 | fprintf(fp, "\n");
|
---|
366 | write_conf(WEBIF, "Web interface support");
|
---|
367 | write_conf(TOUCH, "Touch interface support");
|
---|
368 | write_conf(WITH_SSL, "SSL support");
|
---|
369 | write_conf(HAVE_DVBAPI, "DVB API support");
|
---|
370 | if (config_enabled(HAVE_DVBAPI)) {
|
---|
371 | write_conf(WITH_AZBOX, "DVB API with AZBOX support");
|
---|
372 | write_conf(WITH_MCA, "DVB API with MCA support");
|
---|
373 | write_conf(WITH_COOLAPI, "DVB API with COOLAPI support");
|
---|
374 | write_conf(WITH_STAPI, "DVB API with STAPI support");
|
---|
375 | }
|
---|
376 | write_conf(CS_ANTICASC, "Anti-cascading support");
|
---|
377 | write_conf(IRDETO_GUESSING, "Irdeto guessing");
|
---|
378 | write_conf(WITH_DEBUG, "Debug mode");
|
---|
379 | write_conf(MODULE_MONITOR, "Monitor");
|
---|
380 | write_conf(WITH_LB, "Loadbalancing support");
|
---|
381 | write_conf(CW_CYCLE_CHECK, "CW Cycle Check support");
|
---|
382 | write_conf(LCDSUPPORT, "LCD support");
|
---|
383 | write_conf(LEDSUPPORT, "LED support");
|
---|
384 | write_conf(IPV6SUPPORT, "IPv6 support");
|
---|
385 | write_conf(CS_CACHEEX, "Cache exchange support");
|
---|
386 |
|
---|
387 | fprintf(fp, "\n");
|
---|
388 | write_conf(MODULE_CAMD33, "camd 3.3x");
|
---|
389 | write_conf(MODULE_CAMD35, "camd 3.5 UDP");
|
---|
390 | write_conf(MODULE_CAMD35_TCP, "camd 3.5 TCP");
|
---|
391 | write_conf(MODULE_NEWCAMD, "newcamd");
|
---|
392 | write_conf(MODULE_CCCAM, "CCcam");
|
---|
393 | write_conf(MODULE_CCCSHARE, "CCcam share");
|
---|
394 | write_conf(MODULE_PANDORA, "Pandora");
|
---|
395 | write_conf(MODULE_GHTTP, "ghttp");
|
---|
396 | write_conf(MODULE_GBOX, "gbox");
|
---|
397 | write_conf(MODULE_RADEGAST, "radegast");
|
---|
398 | write_conf(MODULE_SERIAL, "serial");
|
---|
399 | write_conf(MODULE_CONSTCW, "constant CW");
|
---|
400 |
|
---|
401 | fprintf(fp, "\n");
|
---|
402 | write_conf(WITH_CARDREADER, "Reader support");
|
---|
403 | if (config_enabled(WITH_CARDREADER)) {
|
---|
404 | fprintf(fp, "\n");
|
---|
405 | write_readerconf(READER_NAGRA, "Nagra");
|
---|
406 | write_readerconf(READER_IRDETO, "Irdeto");
|
---|
407 | write_readerconf(READER_CONAX, "Conax");
|
---|
408 | write_readerconf(READER_CRYPTOWORKS, "Cryptoworks");
|
---|
409 | write_readerconf(READER_SECA, "Seca");
|
---|
410 | write_readerconf(READER_VIACCESS, "Viaccess");
|
---|
411 | write_readerconf(READER_VIDEOGUARD, "NDS Videoguard");
|
---|
412 | write_readerconf(READER_DRE, "DRE Crypt");
|
---|
413 | write_readerconf(READER_TONGFANG, "TONGFANG");
|
---|
414 | write_readerconf(READER_BULCRYPT, "Bulcrypt");
|
---|
415 | write_readerconf(READER_GRIFFIN, "Griffin");
|
---|
416 | write_readerconf(READER_DGCRYPT, "DGCrypt");
|
---|
417 | fprintf(fp, "\n");
|
---|
418 | write_cardreaderconf(CARDREADER_PHOENIX, "phoenix");
|
---|
419 | write_cardreaderconf(CARDREADER_INTERNAL_AZBOX, "internal_azbox");
|
---|
420 | write_cardreaderconf(CARDREADER_INTERNAL_COOLAPI, "internal_coolapi");
|
---|
421 | write_cardreaderconf(CARDREADER_INTERNAL_SCI, "internal_sci");
|
---|
422 | write_cardreaderconf(CARDREADER_SC8IN1, "sc8in1");
|
---|
423 | write_cardreaderconf(CARDREADER_MP35, "mp35");
|
---|
424 | write_cardreaderconf(CARDREADER_SMARGO, "smargo");
|
---|
425 | write_cardreaderconf(CARDREADER_PCSC, "pcsc");
|
---|
426 | write_cardreaderconf(CARDREADER_SMART, "smartreader");
|
---|
427 | write_cardreaderconf(CARDREADER_DB2COM, "db2com");
|
---|
428 | write_cardreaderconf(CARDREADER_STAPI, "stapi");
|
---|
429 | } else {
|
---|
430 | write_readerconf(WITH_CARDREADER, "Reader Support");
|
---|
431 | }
|
---|
432 | if (!use_stdout)
|
---|
433 | fclose(fp);
|
---|
434 | }
|
---|
435 | #undef write_conf
|
---|
436 | #undef write_readerconf
|
---|
437 | #undef write_cardreaderconf
|
---|
438 |
|
---|
439 | static void remove_versionfile(void) {
|
---|
440 | char targetfile[256];
|
---|
441 | unlink(get_tmp_dir_filename(targetfile, sizeof(targetfile), "oscam.version"));
|
---|
442 | }
|
---|
443 |
|
---|
444 | #define report_emm_support(CONFIG_VAR, text) \
|
---|
445 | do { \
|
---|
446 | if (!config_enabled(CONFIG_VAR)) \
|
---|
447 | cs_log("Binary without %s module - no EMM processing for %s possible!", text, text); \
|
---|
448 | } while(0)
|
---|
449 |
|
---|
450 | static void do_report_emm_support(void) {
|
---|
451 | if (!config_enabled(WITH_CARDREADER)) {
|
---|
452 | cs_log("Binary without Cardreader Support! No EMM processing possible!");
|
---|
453 | } else {
|
---|
454 | report_emm_support(READER_NAGRA, "Nagra");
|
---|
455 | report_emm_support(READER_IRDETO, "Irdeto");
|
---|
456 | report_emm_support(READER_CONAX, "Conax");
|
---|
457 | report_emm_support(READER_CRYPTOWORKS, "Cryptoworks");
|
---|
458 | report_emm_support(READER_SECA, "Seca");
|
---|
459 | report_emm_support(READER_VIACCESS, "Viaccess");
|
---|
460 | report_emm_support(READER_VIDEOGUARD, "NDS Videoguard");
|
---|
461 | report_emm_support(READER_DRE, "DRE Crypt");
|
---|
462 | report_emm_support(READER_TONGFANG, "TONGFANG");
|
---|
463 | report_emm_support(READER_BULCRYPT, "Bulcrypt");
|
---|
464 | report_emm_support(READER_GRIFFIN, "Griffin");
|
---|
465 | report_emm_support(READER_DGCRYPT, "DGCrypt");
|
---|
466 | }
|
---|
467 | }
|
---|
468 | #undef report_emm_support
|
---|
469 |
|
---|
470 | #ifdef NEED_DAEMON
|
---|
471 | // The compat function is not called daemon() because this may cause problems.
|
---|
472 | static int32_t do_daemon(int32_t nochdir, int32_t noclose)
|
---|
473 | {
|
---|
474 | int32_t fd;
|
---|
475 |
|
---|
476 | switch (fork())
|
---|
477 | {
|
---|
478 | case -1: return (-1);
|
---|
479 | case 0: break;
|
---|
480 | default: _exit(0);
|
---|
481 | }
|
---|
482 |
|
---|
483 | if (setsid()==(-1))
|
---|
484 | return(-1);
|
---|
485 |
|
---|
486 | if (!nochdir)
|
---|
487 | (void)chdir("/");
|
---|
488 |
|
---|
489 | if (!noclose && (fd=open("/dev/null", O_RDWR, 0)) != -1)
|
---|
490 | {
|
---|
491 | (void)dup2(fd, STDIN_FILENO);
|
---|
492 | (void)dup2(fd, STDOUT_FILENO);
|
---|
493 | (void)dup2(fd, STDERR_FILENO);
|
---|
494 | if (fd>2)
|
---|
495 | (void)close(fd);
|
---|
496 | }
|
---|
497 | return(0);
|
---|
498 | }
|
---|
499 | #else
|
---|
500 | #define do_daemon daemon
|
---|
501 | #endif
|
---|
502 |
|
---|
503 | /*
|
---|
504 | * flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
|
---|
505 | */
|
---|
506 | static void set_signal_handler(int32_t sig, int32_t flags, void (*sighandler))
|
---|
507 | {
|
---|
508 | struct sigaction sa;
|
---|
509 | sigaction(sig, (struct sigaction *) 0, &sa);
|
---|
510 | if (!((flags & 2) && (sa.sa_handler==SIG_IGN)))
|
---|
511 | {
|
---|
512 | sigemptyset(&sa.sa_mask);
|
---|
513 | sa.sa_flags=(flags & 1) ? SA_RESTART : 0;
|
---|
514 | sa.sa_handler=sighandler;
|
---|
515 | sigaction(sig, &sa, (struct sigaction *) 0);
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | static void cs_master_alarm(void)
|
---|
520 | {
|
---|
521 | cs_log("PANIC: master deadlock!");
|
---|
522 | fprintf(stderr, "PANIC: master deadlock!");
|
---|
523 | fflush(stderr);
|
---|
524 | }
|
---|
525 |
|
---|
526 | static void cs_sigpipe(void)
|
---|
527 | {
|
---|
528 | if (cs_dblevel & D_ALL_DUMP)
|
---|
529 | cs_log("Got sigpipe signal -> captured");
|
---|
530 | }
|
---|
531 |
|
---|
532 | static void cs_dummy(void) {
|
---|
533 | return;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /* Switch debuglevel forward one step (called when receiving SIGUSR1). */
|
---|
537 | static void cs_debug_level(void) {
|
---|
538 | switch (cs_dblevel) {
|
---|
539 | case 0:
|
---|
540 | cs_dblevel = 1;
|
---|
541 | break;
|
---|
542 | case 128:
|
---|
543 | cs_dblevel = 255;
|
---|
544 | break;
|
---|
545 | case 255:
|
---|
546 | cs_dblevel = 0;
|
---|
547 | break;
|
---|
548 | default:
|
---|
549 | cs_dblevel <<= 1;
|
---|
550 | }
|
---|
551 |
|
---|
552 | cs_log("debug_level=%d", cs_dblevel);
|
---|
553 | }
|
---|
554 |
|
---|
555 | /**
|
---|
556 | * write stacktrace to oscam.crash. file is always appended
|
---|
557 | * Usage:
|
---|
558 | * 1. compile oscam with debug parameters (Makefile: DS_OPTS="-ggdb")
|
---|
559 | * 2. you need gdb installed and working on the local machine
|
---|
560 | * 3. start oscam with parameter: -a
|
---|
561 | */
|
---|
562 | static void cs_dumpstack(int32_t sig)
|
---|
563 | {
|
---|
564 | FILE *fp = fopen("oscam.crash", "a+");
|
---|
565 |
|
---|
566 | time_t timep;
|
---|
567 | char buf[200];
|
---|
568 |
|
---|
569 | time(&timep);
|
---|
570 | cs_ctime_r(&timep, buf);
|
---|
571 |
|
---|
572 | fprintf(stderr, "crashed with signal %d on %swriting oscam.crash\n", sig, buf);
|
---|
573 |
|
---|
574 | fprintf(fp, "%sOSCam cardserver v%s, build r%s (%s)\n", buf, CS_VERSION, CS_SVN_VERSION, CS_TARGET);
|
---|
575 | fprintf(fp, "FATAL: Signal %d: %s Fault. Logged StackTrace:\n\n", sig, (sig == SIGSEGV) ? "Segmentation" : ((sig == SIGBUS) ? "Bus" : "Unknown"));
|
---|
576 | fclose(fp);
|
---|
577 |
|
---|
578 | FILE *cmd = fopen("/tmp/gdbcmd", "w");
|
---|
579 | fputs("bt\n", cmd);
|
---|
580 | fputs("thread apply all bt\n", cmd);
|
---|
581 | fclose(cmd);
|
---|
582 |
|
---|
583 | snprintf(buf, sizeof(buf)-1, "gdb %s %d -batch -x /tmp/gdbcmd >> oscam.crash", prog_name, getpid());
|
---|
584 | if(system(buf) == -1)
|
---|
585 | fprintf(stderr, "Fatal error on trying to start gdb process.");
|
---|
586 |
|
---|
587 | exit(-1);
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * called by signal SIGHUP
|
---|
593 | *
|
---|
594 | * reloads configs:
|
---|
595 | * - useraccounts (oscam.user)
|
---|
596 | * - services ids (oscam.srvid)
|
---|
597 | * - tier ids (oscam.tiers)
|
---|
598 | * Also clears anticascading stats.
|
---|
599 | **/
|
---|
600 | static void cs_reload_config(void)
|
---|
601 | {
|
---|
602 | cs_accounts_chk();
|
---|
603 | init_srvid();
|
---|
604 | init_tierid();
|
---|
605 | ac_init_stat();
|
---|
606 | }
|
---|
607 |
|
---|
608 | /* Sets signal handlers to ignore for early startup of OSCam because for example log
|
---|
609 | could cause SIGPIPE errors and the normal signal handlers can't be used at this point. */
|
---|
610 | static void init_signal_pre(void)
|
---|
611 | {
|
---|
612 | set_signal_handler(SIGPIPE , 1, SIG_IGN);
|
---|
613 | set_signal_handler(SIGWINCH, 1, SIG_IGN);
|
---|
614 | set_signal_handler(SIGALRM , 1, SIG_IGN);
|
---|
615 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
616 | }
|
---|
617 |
|
---|
618 | /* Sets the signal handlers.*/
|
---|
619 | static void init_signal(void)
|
---|
620 | {
|
---|
621 | set_signal_handler(SIGINT, 3, cs_exit);
|
---|
622 | #if defined(__APPLE__)
|
---|
623 | set_signal_handler(SIGEMT, 3, cs_exit);
|
---|
624 | #endif
|
---|
625 | set_signal_handler(SIGTERM, 3, cs_exit);
|
---|
626 |
|
---|
627 | set_signal_handler(SIGWINCH, 1, SIG_IGN);
|
---|
628 | set_signal_handler(SIGPIPE , 0, cs_sigpipe);
|
---|
629 | set_signal_handler(SIGALRM , 0, cs_master_alarm);
|
---|
630 | set_signal_handler(SIGHUP , 1, cs_reload_config);
|
---|
631 | set_signal_handler(SIGUSR1, 1, cs_debug_level);
|
---|
632 | set_signal_handler(SIGUSR2, 1, cs_card_info);
|
---|
633 | set_signal_handler(OSCAM_SIGNAL_WAKEUP, 0, cs_dummy);
|
---|
634 |
|
---|
635 | if (cs_capture_SEGV) {
|
---|
636 | set_signal_handler(SIGSEGV, 1, cs_exit);
|
---|
637 | set_signal_handler(SIGBUS, 1, cs_exit);
|
---|
638 | }
|
---|
639 | else if (cs_dump_stack) {
|
---|
640 | set_signal_handler(SIGSEGV, 1, cs_dumpstack);
|
---|
641 | set_signal_handler(SIGBUS, 1, cs_dumpstack);
|
---|
642 | }
|
---|
643 |
|
---|
644 | cs_log("signal handling initialized");
|
---|
645 | return;
|
---|
646 | }
|
---|
647 |
|
---|
648 | void cs_exit(int32_t sig)
|
---|
649 | {
|
---|
650 | if (cs_dump_stack && (sig == SIGSEGV || sig == SIGBUS || sig == SIGQUIT))
|
---|
651 | cs_dumpstack(sig);
|
---|
652 |
|
---|
653 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
654 | set_signal_handler(SIGPIPE, 1, SIG_IGN);
|
---|
655 |
|
---|
656 | struct s_client *cl = cur_client();
|
---|
657 | if (!cl)
|
---|
658 | return;
|
---|
659 |
|
---|
660 | // this is very important - do not remove
|
---|
661 | if (cl->typ != 's') {
|
---|
662 | cs_debug_mask(D_TRACE, "thread %8lX ended!", (unsigned long)pthread_self());
|
---|
663 |
|
---|
664 | free_client(cl);
|
---|
665 |
|
---|
666 | //Restore signals before exiting thread
|
---|
667 | set_signal_handler(SIGPIPE , 0, cs_sigpipe);
|
---|
668 | set_signal_handler(SIGHUP , 1, cs_reload_config);
|
---|
669 |
|
---|
670 | pthread_exit(NULL);
|
---|
671 | return;
|
---|
672 | }
|
---|
673 |
|
---|
674 | if (!exit_oscam)
|
---|
675 | exit_oscam = sig?sig:1;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /* Checks if the date of the system is correct and waits if necessary. */
|
---|
679 | static void init_check(void){
|
---|
680 | char *ptr = __DATE__;
|
---|
681 | int32_t month, year = atoi(ptr + strlen(ptr) - 4), day = atoi(ptr + 4);
|
---|
682 | if(day > 0 && day < 32 && year > 2010 && year < 9999){
|
---|
683 | struct tm timeinfo;
|
---|
684 | char months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
---|
685 | for(month = 0; month < 12; ++month){
|
---|
686 | if(!strncmp(ptr, months[month], 3)) break;
|
---|
687 | }
|
---|
688 | if(month > 11) month = 0;
|
---|
689 | memset(&timeinfo, 0, sizeof(timeinfo));
|
---|
690 | timeinfo.tm_mday = day;
|
---|
691 | timeinfo.tm_mon = month;
|
---|
692 | timeinfo.tm_year = year - 1900;
|
---|
693 | time_t builddate = mktime(&timeinfo) - 86400;
|
---|
694 | int32_t i = 0;
|
---|
695 | while(time((time_t*)0) < builddate){
|
---|
696 | if(i == 0) cs_log("The current system time is smaller than the build date (%s). Waiting up to %d seconds for time to correct", ptr, cs_waittime);
|
---|
697 | cs_sleepms(1000);
|
---|
698 | ++i;
|
---|
699 | if(i > cs_waittime){
|
---|
700 | cs_log("Waiting was not successful. OSCam will be started but is UNSUPPORTED this way. Do not report any errors with this version.");
|
---|
701 | break;
|
---|
702 | }
|
---|
703 | }
|
---|
704 | // adjust login time of first client
|
---|
705 | if(i > 0) first_client->login=time((time_t *)0);
|
---|
706 | }
|
---|
707 | }
|
---|
708 |
|
---|
709 | #ifdef __linux__
|
---|
710 | #include <sys/prctl.h>
|
---|
711 | // PR_SET_NAME is introduced in 2.6.9 (which is ancient, released 18 Oct 2004)
|
---|
712 | // but apparantly we can't count on having at least that version :(
|
---|
713 | #ifndef PR_SET_NAME
|
---|
714 | #define PR_SET_NAME 15
|
---|
715 | #endif
|
---|
716 | // Set the thread name (comm) under linux (the limit is 16 chars)
|
---|
717 | void set_thread_name(const char *thread_name) {
|
---|
718 | prctl(PR_SET_NAME, thread_name, NULL, NULL, NULL);
|
---|
719 | }
|
---|
720 | #else
|
---|
721 | void set_thread_name(const char *UNUSED(thread_name)) { }
|
---|
722 | #endif
|
---|
723 |
|
---|
724 | /* Starts a thread named nameroutine with the start function startroutine. */
|
---|
725 | void start_thread(void * startroutine, char * nameroutine) {
|
---|
726 | pthread_t temp;
|
---|
727 | pthread_attr_t attr;
|
---|
728 | pthread_attr_init(&attr);
|
---|
729 | cs_debug_mask(D_TRACE, "starting thread %s", nameroutine);
|
---|
730 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
731 | cs_writelock(&system_lock);
|
---|
732 | int32_t ret = pthread_create(&temp, &attr, startroutine, NULL);
|
---|
733 | if (ret)
|
---|
734 | cs_log("ERROR: can't create %s thread (errno=%d %s)", nameroutine, ret, strerror(ret));
|
---|
735 | else {
|
---|
736 | cs_debug_mask(D_TRACE, "%s thread started", nameroutine);
|
---|
737 | pthread_detach(temp);
|
---|
738 | }
|
---|
739 | pthread_attr_destroy(&attr);
|
---|
740 | cs_writeunlock(&system_lock);
|
---|
741 | }
|
---|
742 |
|
---|
743 | /* Allows to kill another thread specified through the client cl with locking.
|
---|
744 | If the own thread has to be cancelled, cs_exit or cs_disconnect_client has to be used. */
|
---|
745 | void kill_thread(struct s_client *cl) {
|
---|
746 | if (!cl || cl->kill) return;
|
---|
747 | if (cl == cur_client()) {
|
---|
748 | cs_log("Trying to kill myself, exiting.");
|
---|
749 | cs_exit(0);
|
---|
750 | }
|
---|
751 | add_job(cl, ACTION_CLIENT_KILL, NULL, 0); //add kill job, ...
|
---|
752 | cl->kill=1; //then set kill flag!
|
---|
753 | }
|
---|
754 |
|
---|
755 | struct s_module *get_module(struct s_client *cl) {
|
---|
756 | return &modules[cl->module_idx];
|
---|
757 | }
|
---|
758 |
|
---|
759 | void module_reader_set(struct s_reader *rdr) {
|
---|
760 | int i;
|
---|
761 | if (!is_cascading_reader(rdr))
|
---|
762 | return;
|
---|
763 | for (i = 0; i < CS_MAX_MOD; i++) {
|
---|
764 | struct s_module *module = &modules[i];
|
---|
765 | if (module->num && module->num == rdr->typ) {
|
---|
766 | rdr->ph = *module;
|
---|
767 | if (rdr->device[0])
|
---|
768 | rdr->ph.active = 1;
|
---|
769 | }
|
---|
770 | }
|
---|
771 | }
|
---|
772 |
|
---|
773 | static void cs_waitforcardinit(void)
|
---|
774 | {
|
---|
775 | if (cfg.waitforcards)
|
---|
776 | {
|
---|
777 | cs_log("waiting for local card init");
|
---|
778 | int32_t card_init_done;
|
---|
779 | do {
|
---|
780 | card_init_done = 1;
|
---|
781 | struct s_reader *rdr;
|
---|
782 | LL_ITER itr = ll_iter_create(configured_readers);
|
---|
783 | while((rdr = ll_iter_next(&itr))) {
|
---|
784 | if (rdr->enable && !is_cascading_reader(rdr) && (rdr->card_status == CARD_NEED_INIT || rdr->card_status == UNKNOWN)) {
|
---|
785 | card_init_done = 0;
|
---|
786 | break;
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | if (!card_init_done)
|
---|
791 | cs_sleepms(300); // wait a little bit
|
---|
792 | //alarm(cfg.cmaxidle + cfg.ctimeout / 1000 + 1);
|
---|
793 | } while (!card_init_done && !exit_oscam);
|
---|
794 | if (cfg.waitforcards_extra_delay>0 && !exit_oscam)
|
---|
795 | cs_sleepms(cfg.waitforcards_extra_delay);
|
---|
796 | cs_log("init for all local cards done");
|
---|
797 | }
|
---|
798 | }
|
---|
799 |
|
---|
800 | static uint32_t auto_timeout(ECM_REQUEST *er, uint32_t timeout) {
|
---|
801 | (void)er; // Prevent warning about unused er, when WITH_LB is disabled
|
---|
802 | #ifdef WITH_LB
|
---|
803 | if (cfg.lb_auto_timeout)
|
---|
804 | return lb_auto_timeout(er, timeout);
|
---|
805 | #endif
|
---|
806 | return timeout;
|
---|
807 | }
|
---|
808 |
|
---|
809 |
|
---|
810 | static void * check_thread(void) {
|
---|
811 | int32_t time_to_check, next_check, ecmc_next, msec_wait = 3000;
|
---|
812 | struct timeb t_now, tbc, ecmc_time;
|
---|
813 | ECM_REQUEST *er = NULL;
|
---|
814 | time_t ecm_timeout;
|
---|
815 | time_t ecm_mintimeout;
|
---|
816 | struct timespec ts;
|
---|
817 | struct s_client *cl = create_client(first_client->ip);
|
---|
818 | cl->typ = 's';
|
---|
819 | #ifdef WEBIF
|
---|
820 | cl->wihidden = 1;
|
---|
821 | #endif
|
---|
822 | cl->thread = pthread_self();
|
---|
823 |
|
---|
824 | timecheck_client = cl;
|
---|
825 |
|
---|
826 | #ifdef CS_ANTICASC
|
---|
827 | int32_t ac_next;
|
---|
828 | struct timeb ac_time;
|
---|
829 | cs_ftime(&ac_time);
|
---|
830 | add_ms_to_timeb(&ac_time, cfg.ac_stime*60*1000);
|
---|
831 | #endif
|
---|
832 |
|
---|
833 | cs_ftime(&ecmc_time);
|
---|
834 | add_ms_to_timeb(&ecmc_time, 1000);
|
---|
835 |
|
---|
836 | while(1) {
|
---|
837 | ts.tv_sec = msec_wait/1000;
|
---|
838 | ts.tv_nsec = (msec_wait % 1000) * 1000000L;
|
---|
839 | pthread_mutex_lock(&cl->thread_lock);
|
---|
840 | cl->thread_active = 2;
|
---|
841 | pthread_mutex_unlock(&cl->thread_lock);
|
---|
842 | nanosleep(&ts, NULL);
|
---|
843 | pthread_mutex_lock(&cl->thread_lock);
|
---|
844 | cl->thread_active = 1;
|
---|
845 | pthread_mutex_unlock(&cl->thread_lock);
|
---|
846 |
|
---|
847 | next_check = 0;
|
---|
848 | #ifdef CS_ANTICASC
|
---|
849 | ac_next = 0;
|
---|
850 | #endif
|
---|
851 | ecmc_next = 0;
|
---|
852 | msec_wait = 0;
|
---|
853 |
|
---|
854 | cs_ftime(&t_now);
|
---|
855 | cs_readlock(&ecmcache_lock);
|
---|
856 |
|
---|
857 | for (er = ecmcwcache; er; er = er->next) {
|
---|
858 | if (er->rc < E_99 || !er->ecmlen || !er->matching_rdr) //ignore CACHEEX pending ECMs
|
---|
859 | continue;
|
---|
860 |
|
---|
861 | tbc = er->tps;
|
---|
862 | #if defined CS_CACHEEX && defined CW_CYCLE_CHECK
|
---|
863 | time_to_check = add_ms_to_timeb(&tbc, (er->stage < 2) ? er->cacheex_wait_time:((er->stage < 4) ? auto_timeout(er, cfg.ftimeout) : auto_timeout(er, cfg.ctimeout)));
|
---|
864 | #else
|
---|
865 | time_to_check = add_ms_to_timeb(&tbc, ((er->stage < 4) ? auto_timeout(er, cfg.ftimeout) : auto_timeout(er, cfg.ctimeout)));
|
---|
866 | #endif
|
---|
867 | if (comp_timeb(&t_now, &tbc) >= 0) {
|
---|
868 | if (er->stage < 4) {
|
---|
869 | #if defined CS_CACHEEX && defined CW_CYCLE_CHECK
|
---|
870 | if (er->stage < 2 && er->cacheex_wait_time)
|
---|
871 | debug_ecm(D_TRACE, "request for %s %s", username(er->client), buf);
|
---|
872 | else
|
---|
873 | #endif
|
---|
874 | debug_ecm(D_TRACE, "fallback for %s %s", username(er->client), buf);
|
---|
875 |
|
---|
876 | if (er->rc >= E_UNHANDLED) //do not request rc=99
|
---|
877 | request_cw_from_readers(er);
|
---|
878 |
|
---|
879 | tbc = er->tps;
|
---|
880 | time_to_check = add_ms_to_timeb(&tbc, auto_timeout(er, cfg.ctimeout));
|
---|
881 | } else {
|
---|
882 | if (er->client) {
|
---|
883 | er->selected_reader = NULL;
|
---|
884 | debug_ecm(D_TRACE, "timeout for %s %s", username(er->client), buf);
|
---|
885 | write_ecm_answer(NULL, er, E_TIMEOUT, 0, NULL, NULL);
|
---|
886 | }
|
---|
887 | #ifdef WITH_LB
|
---|
888 | if (!er->ecmcacheptr) { //do not add stat for cache entries:
|
---|
889 | //because of lb, send E_TIMEOUT for all readers:
|
---|
890 | struct s_ecm_answer *ea_list;
|
---|
891 |
|
---|
892 | for(ea_list = er->matching_rdr; ea_list; ea_list = ea_list->next) {
|
---|
893 | if ((ea_list->status & (REQUEST_SENT|REQUEST_ANSWERED)) == REQUEST_SENT) //Request send, but no answer!
|
---|
894 | send_reader_stat(ea_list->reader, er, NULL, E_TIMEOUT);
|
---|
895 | }
|
---|
896 | }
|
---|
897 | #endif
|
---|
898 |
|
---|
899 | time_to_check = 0;
|
---|
900 | }
|
---|
901 | }
|
---|
902 | if (!next_check || (time_to_check > 0 && time_to_check < next_check))
|
---|
903 | next_check = time_to_check;
|
---|
904 | }
|
---|
905 | cs_readunlock(&ecmcache_lock);
|
---|
906 |
|
---|
907 | #ifdef CS_ANTICASC
|
---|
908 | if ((ac_next = comp_timeb(&ac_time, &t_now)) <= 10) {
|
---|
909 | if (cfg.ac_enabled)
|
---|
910 | ac_do_stat();
|
---|
911 | cs_ftime(&ac_time);
|
---|
912 | ac_next = add_ms_to_timeb(&ac_time, cfg.ac_stime*60*1000);
|
---|
913 | }
|
---|
914 | #endif
|
---|
915 |
|
---|
916 | if ((ecmc_next = comp_timeb(&ecmc_time, &t_now)) <= 10) {
|
---|
917 | ecm_timeout = t_now.time-cfg.max_cache_time;
|
---|
918 | ecm_mintimeout = t_now.time-(cfg.ctimeout/1000+2);
|
---|
919 | uint32_t count = 0;
|
---|
920 |
|
---|
921 | struct ecm_request_t *ecm, *ecmt=NULL, *prv;
|
---|
922 | cs_readlock(&ecmcache_lock);
|
---|
923 | for (ecm = ecmcwcache, prv = NULL; ecm; prv = ecm, ecm = ecm->next, count++) {
|
---|
924 | if (ecm->tps.time < ecm_timeout || (ecm->tps.time<ecm_mintimeout && count>cfg.max_cache_count)) {
|
---|
925 | cs_readunlock(&ecmcache_lock);
|
---|
926 | cs_writelock(&ecmcache_lock);
|
---|
927 | ecmt = ecm;
|
---|
928 | if (prv)
|
---|
929 | prv->next = NULL;
|
---|
930 | else
|
---|
931 | ecmcwcache = NULL;
|
---|
932 | cs_writeunlock(&ecmcache_lock);
|
---|
933 | break;
|
---|
934 | }
|
---|
935 | }
|
---|
936 | if (!ecmt)
|
---|
937 | cs_readunlock(&ecmcache_lock);
|
---|
938 | ecmcwcache_size = count;
|
---|
939 |
|
---|
940 | while (ecmt) {
|
---|
941 | ecm = ecmt->next;
|
---|
942 | free_ecm(ecmt);
|
---|
943 | ecmt = ecm;
|
---|
944 | }
|
---|
945 |
|
---|
946 | cs_ftime(&ecmc_time);
|
---|
947 | ecmc_next = add_ms_to_timeb(&ecmc_time, 1000);
|
---|
948 | }
|
---|
949 |
|
---|
950 | msec_wait = next_check;
|
---|
951 |
|
---|
952 | #ifdef CS_ANTICASC
|
---|
953 | if (!msec_wait || (ac_next > 0 && ac_next < msec_wait))
|
---|
954 | msec_wait = ac_next;
|
---|
955 | #endif
|
---|
956 |
|
---|
957 | if (!msec_wait || (ecmc_next > 0 && ecmc_next < msec_wait))
|
---|
958 | msec_wait = ecmc_next;
|
---|
959 |
|
---|
960 | if (!msec_wait)
|
---|
961 | msec_wait = 3000;
|
---|
962 | cleanupcwcycle();
|
---|
963 | cleanup_hitcache();
|
---|
964 | }
|
---|
965 | add_garbage(cl);
|
---|
966 | timecheck_client = NULL;
|
---|
967 | return NULL;
|
---|
968 | }
|
---|
969 |
|
---|
970 | static uint32_t resize_pfd_cllist(struct pollfd **pfd, struct s_client ***cl_list, uint32_t old_size, uint32_t new_size) {
|
---|
971 | if (old_size != new_size) {
|
---|
972 | struct pollfd *pfd_new;
|
---|
973 | if (!cs_malloc(&pfd_new, new_size * sizeof(struct pollfd))) {
|
---|
974 | return old_size;
|
---|
975 | }
|
---|
976 | struct s_client **cl_list_new;
|
---|
977 | if (!cs_malloc(&cl_list_new, new_size * sizeof(cl_list))) {
|
---|
978 | free(pfd_new);
|
---|
979 | return old_size;
|
---|
980 | }
|
---|
981 | if (old_size > 0) {
|
---|
982 | memcpy(pfd_new, *pfd, old_size*sizeof(struct pollfd));
|
---|
983 | memcpy(cl_list_new, *cl_list, old_size*sizeof(cl_list));
|
---|
984 | free(*pfd);
|
---|
985 | free(*cl_list);
|
---|
986 | }
|
---|
987 | *pfd = pfd_new;
|
---|
988 | *cl_list = cl_list_new;
|
---|
989 | }
|
---|
990 | return new_size;
|
---|
991 | }
|
---|
992 |
|
---|
993 | static uint32_t chk_resize_cllist(struct pollfd **pfd, struct s_client ***cl_list, uint32_t cur_size, uint32_t chk_size) {
|
---|
994 | chk_size++;
|
---|
995 | if (chk_size > cur_size) {
|
---|
996 | uint32_t new_size = ((chk_size % 100)+1) * 100; //increase 100 step
|
---|
997 | cur_size = resize_pfd_cllist(pfd, cl_list, cur_size, new_size);
|
---|
998 | }
|
---|
999 | return cur_size;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | static void process_clients(void) {
|
---|
1003 | int32_t i, k, j, rc, pfdcount = 0;
|
---|
1004 | struct s_client *cl;
|
---|
1005 | struct s_reader *rdr;
|
---|
1006 | struct pollfd *pfd;
|
---|
1007 | struct s_client **cl_list;
|
---|
1008 | uint32_t cl_size = 0;
|
---|
1009 |
|
---|
1010 | char buf[10];
|
---|
1011 |
|
---|
1012 | if (pipe(thread_pipe) == -1) {
|
---|
1013 | printf("cannot create pipe, errno=%d\n", errno);
|
---|
1014 | exit(1);
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | cl_size = chk_resize_cllist(&pfd, &cl_list, 0, 100);
|
---|
1018 |
|
---|
1019 | pfd[pfdcount].fd = thread_pipe[0];
|
---|
1020 | pfd[pfdcount].events = POLLIN | POLLPRI | POLLHUP;
|
---|
1021 | cl_list[pfdcount] = NULL;
|
---|
1022 |
|
---|
1023 | while (!exit_oscam) {
|
---|
1024 | pfdcount = 1;
|
---|
1025 |
|
---|
1026 | //connected tcp clients
|
---|
1027 | for (cl=first_client->next; cl; cl=cl->next) {
|
---|
1028 | if (cl->init_done && !cl->kill && cl->pfd && cl->typ=='c' && !cl->is_udp) {
|
---|
1029 | if (cl->pfd && !cl->thread_active) {
|
---|
1030 | cl_size = chk_resize_cllist(&pfd, &cl_list, cl_size, pfdcount);
|
---|
1031 | cl_list[pfdcount] = cl;
|
---|
1032 | pfd[pfdcount].fd = cl->pfd;
|
---|
1033 | pfd[pfdcount++].events = POLLIN | POLLPRI | POLLHUP;
|
---|
1034 | }
|
---|
1035 | }
|
---|
1036 | //reader:
|
---|
1037 | //TCP:
|
---|
1038 | // - TCP socket must be connected
|
---|
1039 | // - no active init thread
|
---|
1040 | //UDP:
|
---|
1041 | // - connection status ignored
|
---|
1042 | // - no active init thread
|
---|
1043 | rdr = cl->reader;
|
---|
1044 | if (rdr && cl->typ=='p' && cl->init_done) {
|
---|
1045 | if (cl->pfd && !cl->thread_active && ((rdr->tcp_connected && rdr->ph.type==MOD_CONN_TCP)||(rdr->ph.type==MOD_CONN_UDP))) {
|
---|
1046 | cl_size = chk_resize_cllist(&pfd, &cl_list, cl_size, pfdcount);
|
---|
1047 | cl_list[pfdcount] = cl;
|
---|
1048 | pfd[pfdcount].fd = cl->pfd;
|
---|
1049 | pfd[pfdcount++].events = POLLIN | POLLPRI | POLLHUP;
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | //server (new tcp connections or udp messages)
|
---|
1055 | for (k = 0; k < CS_MAX_MOD; k++) {
|
---|
1056 | struct s_module *module = &modules[k];
|
---|
1057 | if ((module->type & MOD_CONN_NET) && module->ptab) {
|
---|
1058 | for (j = 0; j < module->ptab->nports; j++) {
|
---|
1059 | if (module->ptab->ports[j].fd) {
|
---|
1060 | cl_size = chk_resize_cllist(&pfd, &cl_list, cl_size, pfdcount);
|
---|
1061 | cl_list[pfdcount] = NULL;
|
---|
1062 | pfd[pfdcount].fd = module->ptab->ports[j].fd;
|
---|
1063 | pfd[pfdcount++].events = POLLIN | POLLPRI | POLLHUP;
|
---|
1064 | }
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | if (pfdcount >= 1024)
|
---|
1070 | cs_log("WARNING: too many users!");
|
---|
1071 |
|
---|
1072 | rc = poll(pfd, pfdcount, 5000);
|
---|
1073 |
|
---|
1074 | if (rc<1)
|
---|
1075 | continue;
|
---|
1076 |
|
---|
1077 | for (i=0; i<pfdcount; i++) {
|
---|
1078 | //clients
|
---|
1079 | cl = cl_list[i];
|
---|
1080 | if (cl && !is_valid_client(cl))
|
---|
1081 | continue;
|
---|
1082 |
|
---|
1083 | if (pfd[i].fd == thread_pipe[0] && (pfd[i].revents & (POLLIN | POLLPRI))) {
|
---|
1084 | // a thread ended and cl->pfd should be added to pollfd list again (thread_active==0)
|
---|
1085 | if(read(thread_pipe[0], buf, sizeof(buf)) == -1){
|
---|
1086 | cs_debug_mask(D_TRACE, "Reading from pipe failed (errno=%d %s)", errno, strerror(errno));
|
---|
1087 | }
|
---|
1088 | continue;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | //clients
|
---|
1092 | // message on an open tcp connection
|
---|
1093 | if (cl && cl->init_done && cl->pfd && (cl->typ == 'c' || cl->typ == 'm')) {
|
---|
1094 | if (pfd[i].fd == cl->pfd && (pfd[i].revents & (POLLHUP | POLLNVAL))) {
|
---|
1095 | //client disconnects
|
---|
1096 | kill_thread(cl);
|
---|
1097 | continue;
|
---|
1098 | }
|
---|
1099 | if (pfd[i].fd == cl->pfd && (pfd[i].revents & (POLLIN | POLLPRI))) {
|
---|
1100 | add_job(cl, ACTION_CLIENT_TCP, NULL, 0);
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 |
|
---|
1105 | //reader
|
---|
1106 | // either an ecm answer, a keepalive or connection closed from a proxy
|
---|
1107 | // physical reader ('r') should never send data without request
|
---|
1108 | rdr = NULL;
|
---|
1109 | struct s_client *cl2 = NULL;
|
---|
1110 | if (cl && cl->typ == 'p'){
|
---|
1111 | rdr = cl->reader;
|
---|
1112 | if(rdr)
|
---|
1113 | cl2 = rdr->client;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | if (rdr && cl2 && cl2->init_done) {
|
---|
1117 | if (cl2->pfd && pfd[i].fd == cl2->pfd && (pfd[i].revents & (POLLHUP | POLLNVAL))) {
|
---|
1118 | //connection to remote proxy was closed
|
---|
1119 | //oscam should check for rdr->tcp_connected and reconnect on next ecm request sent to the proxy
|
---|
1120 | network_tcp_connection_close(rdr, "closed");
|
---|
1121 | rdr_debug_mask(rdr, D_READER, "connection closed");
|
---|
1122 | }
|
---|
1123 | if (cl2->pfd && pfd[i].fd == cl2->pfd && (pfd[i].revents & (POLLIN | POLLPRI))) {
|
---|
1124 | add_job(cl2, ACTION_READER_REMOTE, NULL, 0);
|
---|
1125 | }
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 |
|
---|
1129 | //server sockets
|
---|
1130 | // new connection on a tcp listen socket or new message on udp listen socket
|
---|
1131 | if (!cl && (pfd[i].revents & (POLLIN | POLLPRI))) {
|
---|
1132 | for (k = 0; k < CS_MAX_MOD; k++) {
|
---|
1133 | struct s_module *module = &modules[k];
|
---|
1134 | if ((module->type & MOD_CONN_NET) && module->ptab) {
|
---|
1135 | for (j = 0; j < module->ptab->nports; j++) {
|
---|
1136 | if (module->ptab->ports[j].fd && module->ptab->ports[j].fd == pfd[i].fd) {
|
---|
1137 | accept_connection(module, k, j);
|
---|
1138 | }
|
---|
1139 | }
|
---|
1140 | }
|
---|
1141 | }
|
---|
1142 | }
|
---|
1143 | }
|
---|
1144 | first_client->last=time((time_t *)0);
|
---|
1145 | }
|
---|
1146 | free(pfd);
|
---|
1147 | free(cl_list);
|
---|
1148 | return;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | static void * reader_check(void) {
|
---|
1152 | struct s_client *cl;
|
---|
1153 | struct s_reader *rdr;
|
---|
1154 | set_thread_name(__func__);
|
---|
1155 | while (1) {
|
---|
1156 | for (cl=first_client->next; cl ; cl=cl->next) {
|
---|
1157 | if (!cl->thread_active)
|
---|
1158 | client_check_status(cl);
|
---|
1159 | }
|
---|
1160 | cs_readlock(&readerlist_lock);
|
---|
1161 | for (rdr=first_active_reader; rdr; rdr=rdr->next) {
|
---|
1162 | if (rdr->enable) {
|
---|
1163 | cl = rdr->client;
|
---|
1164 | if (!cl || cl->kill)
|
---|
1165 | restart_cardreader(rdr, 0);
|
---|
1166 | else if (!cl->thread_active)
|
---|
1167 | client_check_status(cl);
|
---|
1168 | }
|
---|
1169 | }
|
---|
1170 | cs_readunlock(&readerlist_lock);
|
---|
1171 | cs_sleepms(1000);
|
---|
1172 | }
|
---|
1173 | return NULL;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | #ifdef WEBIF
|
---|
1177 | pid_t pid;
|
---|
1178 |
|
---|
1179 |
|
---|
1180 | static void fwd_sig(int32_t sig)
|
---|
1181 | {
|
---|
1182 | kill(pid, sig);
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | static void restart_daemon(void)
|
---|
1186 | {
|
---|
1187 | while (1) {
|
---|
1188 |
|
---|
1189 | //start client process:
|
---|
1190 | pid = fork();
|
---|
1191 | if (!pid)
|
---|
1192 | return; //client process=oscam process
|
---|
1193 | if (pid < 0)
|
---|
1194 | exit(1);
|
---|
1195 |
|
---|
1196 | //set signal handler for the restart daemon:
|
---|
1197 | set_signal_handler(SIGINT, 3, fwd_sig);
|
---|
1198 | #if defined(__APPLE__)
|
---|
1199 | set_signal_handler(SIGEMT, 3, fwd_sig);
|
---|
1200 | #endif
|
---|
1201 | set_signal_handler(SIGTERM, 3, fwd_sig);
|
---|
1202 | set_signal_handler(SIGQUIT, 0, fwd_sig);
|
---|
1203 | set_signal_handler(SIGHUP , 0, fwd_sig);
|
---|
1204 | set_signal_handler(SIGUSR1, 0, fwd_sig);
|
---|
1205 | set_signal_handler(SIGUSR2, 0, fwd_sig);
|
---|
1206 | set_signal_handler(SIGALRM , 0, fwd_sig);
|
---|
1207 | set_signal_handler(SIGWINCH, 1, SIG_IGN);
|
---|
1208 | set_signal_handler(SIGPIPE , 0, SIG_IGN);
|
---|
1209 | set_signal_handler(OSCAM_SIGNAL_WAKEUP, 0, SIG_IGN);
|
---|
1210 |
|
---|
1211 | //restart control process:
|
---|
1212 | int32_t res=0;
|
---|
1213 | int32_t status=0;
|
---|
1214 | do {
|
---|
1215 | res = waitpid(pid, &status, 0);
|
---|
1216 | if (res==-1) {
|
---|
1217 | if (errno!=EINTR)
|
---|
1218 | exit(1);
|
---|
1219 | }
|
---|
1220 | } while (res!=pid);
|
---|
1221 |
|
---|
1222 | if (cs_restart_mode==2 && WIFSIGNALED(status) && WTERMSIG(status)==SIGSEGV)
|
---|
1223 | status=99; //restart on segfault!
|
---|
1224 | else
|
---|
1225 | status = WEXITSTATUS(status);
|
---|
1226 |
|
---|
1227 | //status=99 restart oscam, all other->terminate
|
---|
1228 | if (status!=99) {
|
---|
1229 | exit(status);
|
---|
1230 | }
|
---|
1231 | }
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | void cs_restart_oscam(void) {
|
---|
1235 | exit_oscam=99;
|
---|
1236 | cs_log("restart oscam requested");
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | int32_t cs_get_restartmode(void) {
|
---|
1240 | return cs_restart_mode;
|
---|
1241 | }
|
---|
1242 | #endif
|
---|
1243 |
|
---|
1244 | void cs_exit_oscam(void) {
|
---|
1245 | exit_oscam = 1;
|
---|
1246 | cs_log("exit oscam requested");
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | static void pidfile_create(char *pidfile) {
|
---|
1250 | FILE *f = fopen(pidfile, "w");
|
---|
1251 | if (f) {
|
---|
1252 | pid_t my_pid = getpid();
|
---|
1253 | cs_log("creating pidfile %s with pid %d", pidfile, my_pid);
|
---|
1254 | fprintf(f, "%d\n", my_pid);
|
---|
1255 | fclose(f);
|
---|
1256 | }
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | int32_t main (int32_t argc, char *argv[])
|
---|
1260 | {
|
---|
1261 | int32_t i, j;
|
---|
1262 | prog_name = argv[0];
|
---|
1263 | if (pthread_key_create(&getclient, NULL)) {
|
---|
1264 | fprintf(stderr, "Could not create getclient, exiting...");
|
---|
1265 | exit(1);
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | void (*mod_def[])(struct s_module *)=
|
---|
1269 | {
|
---|
1270 | #ifdef MODULE_MONITOR
|
---|
1271 | module_monitor,
|
---|
1272 | #endif
|
---|
1273 | #ifdef MODULE_CAMD33
|
---|
1274 | module_camd33,
|
---|
1275 | #endif
|
---|
1276 | #ifdef MODULE_CAMD35
|
---|
1277 | module_camd35,
|
---|
1278 | #endif
|
---|
1279 | #ifdef MODULE_CAMD35_TCP
|
---|
1280 | module_camd35_tcp,
|
---|
1281 | #endif
|
---|
1282 | #ifdef MODULE_NEWCAMD
|
---|
1283 | module_newcamd,
|
---|
1284 | #endif
|
---|
1285 | #ifdef MODULE_CCCAM
|
---|
1286 | module_cccam,
|
---|
1287 | #endif
|
---|
1288 | #ifdef MODULE_PANDORA
|
---|
1289 | module_pandora,
|
---|
1290 | #endif
|
---|
1291 | #ifdef MODULE_GHTTP
|
---|
1292 | module_ghttp,
|
---|
1293 | #endif
|
---|
1294 | #ifdef CS_CACHEEX
|
---|
1295 | module_csp,
|
---|
1296 | #endif
|
---|
1297 | #ifdef MODULE_GBOX
|
---|
1298 | module_gbox,
|
---|
1299 | #endif
|
---|
1300 | #ifdef MODULE_CONSTCW
|
---|
1301 | module_constcw,
|
---|
1302 | #endif
|
---|
1303 | #ifdef MODULE_RADEGAST
|
---|
1304 | module_radegast,
|
---|
1305 | #endif
|
---|
1306 | #ifdef MODULE_SERIAL
|
---|
1307 | module_serial,
|
---|
1308 | #endif
|
---|
1309 | #ifdef HAVE_DVBAPI
|
---|
1310 | module_dvbapi,
|
---|
1311 | #endif
|
---|
1312 | 0
|
---|
1313 | };
|
---|
1314 |
|
---|
1315 | void (*cardsystem_def[])(struct s_cardsystem *)=
|
---|
1316 | {
|
---|
1317 | #ifdef READER_NAGRA
|
---|
1318 | reader_nagra,
|
---|
1319 | #endif
|
---|
1320 | #ifdef READER_IRDETO
|
---|
1321 | reader_irdeto,
|
---|
1322 | #endif
|
---|
1323 | #ifdef READER_CONAX
|
---|
1324 | reader_conax,
|
---|
1325 | #endif
|
---|
1326 | #ifdef READER_CRYPTOWORKS
|
---|
1327 | reader_cryptoworks,
|
---|
1328 | #endif
|
---|
1329 | #ifdef READER_SECA
|
---|
1330 | reader_seca,
|
---|
1331 | #endif
|
---|
1332 | #ifdef READER_VIACCESS
|
---|
1333 | reader_viaccess,
|
---|
1334 | #endif
|
---|
1335 | #ifdef READER_VIDEOGUARD
|
---|
1336 | reader_videoguard1,
|
---|
1337 | reader_videoguard2,
|
---|
1338 | reader_videoguard12,
|
---|
1339 | #endif
|
---|
1340 | #ifdef READER_DRE
|
---|
1341 | reader_dre,
|
---|
1342 | #endif
|
---|
1343 | #ifdef READER_TONGFANG
|
---|
1344 | reader_tongfang,
|
---|
1345 | #endif
|
---|
1346 | #ifdef READER_BULCRYPT
|
---|
1347 | reader_bulcrypt,
|
---|
1348 | #endif
|
---|
1349 | #ifdef READER_GRIFFIN
|
---|
1350 | reader_griffin,
|
---|
1351 | #endif
|
---|
1352 | #ifdef READER_DGCRYPT
|
---|
1353 | reader_dgcrypt,
|
---|
1354 | #endif
|
---|
1355 | 0
|
---|
1356 | };
|
---|
1357 |
|
---|
1358 | void (*cardreader_def[])(struct s_cardreader *)=
|
---|
1359 | {
|
---|
1360 | #ifdef CARDREADER_DB2COM
|
---|
1361 | cardreader_db2com,
|
---|
1362 | #endif
|
---|
1363 | #if defined(CARDREADER_INTERNAL_AZBOX)
|
---|
1364 | cardreader_internal_azbox,
|
---|
1365 | #elif defined(CARDREADER_INTERNAL_COOLAPI)
|
---|
1366 | cardreader_internal_cool,
|
---|
1367 | #elif defined(CARDREADER_INTERNAL_SCI)
|
---|
1368 | cardreader_internal_sci,
|
---|
1369 | #endif
|
---|
1370 | #ifdef CARDREADER_PHOENIX
|
---|
1371 | cardreader_mouse,
|
---|
1372 | #endif
|
---|
1373 | #ifdef CARDREADER_MP35
|
---|
1374 | cardreader_mp35,
|
---|
1375 | #endif
|
---|
1376 | #ifdef CARDREADER_PCSC
|
---|
1377 | cardreader_pcsc,
|
---|
1378 | #endif
|
---|
1379 | #ifdef CARDREADER_SC8IN1
|
---|
1380 | cardreader_sc8in1,
|
---|
1381 | #endif
|
---|
1382 | #ifdef CARDREADER_SMARGO
|
---|
1383 | cardreader_smargo,
|
---|
1384 | #endif
|
---|
1385 | #ifdef CARDREADER_SMART
|
---|
1386 | cardreader_smartreader,
|
---|
1387 | #endif
|
---|
1388 | #ifdef CARDREADER_STAPI
|
---|
1389 | cardreader_stapi,
|
---|
1390 | #endif
|
---|
1391 | 0
|
---|
1392 | };
|
---|
1393 |
|
---|
1394 | parse_cmdline_params(argc, argv);
|
---|
1395 |
|
---|
1396 | if (bg && do_daemon(1,0))
|
---|
1397 | {
|
---|
1398 | printf("Error starting in background (errno=%d: %s)", errno, strerror(errno));
|
---|
1399 | cs_exit(1);
|
---|
1400 | }
|
---|
1401 |
|
---|
1402 | get_random_bytes_init();
|
---|
1403 |
|
---|
1404 | #ifdef WEBIF
|
---|
1405 | if (cs_restart_mode)
|
---|
1406 | restart_daemon();
|
---|
1407 | #endif
|
---|
1408 |
|
---|
1409 | memset(&cfg, 0, sizeof(struct s_config));
|
---|
1410 | cfg.max_pending = max_pending;
|
---|
1411 |
|
---|
1412 | if (cs_confdir[strlen(cs_confdir) - 1] != '/') strcat(cs_confdir, "/");
|
---|
1413 | init_signal_pre(); // because log could cause SIGPIPE errors, init a signal handler first
|
---|
1414 | init_first_client();
|
---|
1415 | cs_lock_create(&system_lock, 5, "system_lock");
|
---|
1416 | cs_lock_create(&config_lock, 10, "config_lock");
|
---|
1417 | cs_lock_create(&gethostbyname_lock, 10, "gethostbyname_lock");
|
---|
1418 | cs_lock_create(&clientlist_lock, 5, "clientlist_lock");
|
---|
1419 | cs_lock_create(&readerlist_lock, 5, "readerlist_lock");
|
---|
1420 | cs_lock_create(&fakeuser_lock, 5, "fakeuser_lock");
|
---|
1421 | cs_lock_create(&ecmcache_lock, 5, "ecmcache_lock");
|
---|
1422 | cs_lock_create(&readdir_lock, 5, "readdir_lock");
|
---|
1423 | cs_lock_create(&cwcycle_lock, 5, "cwcycle_lock");
|
---|
1424 | cs_lock_create(&hitcache_lock, 5, "hitcache_lock");
|
---|
1425 | coolapi_open_all();
|
---|
1426 | init_config();
|
---|
1427 | cs_init_log();
|
---|
1428 | if (!oscam_pidfile && cfg.pidfile)
|
---|
1429 | oscam_pidfile = cfg.pidfile;
|
---|
1430 | if (!oscam_pidfile) {
|
---|
1431 | oscam_pidfile = get_tmp_dir_filename(default_pidfile, sizeof(default_pidfile), "oscam.pid");
|
---|
1432 | }
|
---|
1433 | if (oscam_pidfile)
|
---|
1434 | pidfile_create(oscam_pidfile);
|
---|
1435 | cs_init_statistics();
|
---|
1436 | init_check();
|
---|
1437 | init_stat();
|
---|
1438 |
|
---|
1439 | // These initializations *MUST* be called after init_config()
|
---|
1440 | // because modules depend on config values.
|
---|
1441 | for (i=0; mod_def[i]; i++)
|
---|
1442 | {
|
---|
1443 | struct s_module *module = &modules[i];
|
---|
1444 | mod_def[i](module);
|
---|
1445 | }
|
---|
1446 | for (i=0; cardsystem_def[i]; i++)
|
---|
1447 | {
|
---|
1448 | memset(&cardsystems[i], 0, sizeof(struct s_cardsystem));
|
---|
1449 | cardsystem_def[i](&cardsystems[i]);
|
---|
1450 | }
|
---|
1451 | for (i=0; cardreader_def[i]; i++)
|
---|
1452 | {
|
---|
1453 | memset(&cardreaders[i], 0, sizeof(struct s_cardreader));
|
---|
1454 | cardreader_def[i](&cardreaders[i]);
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | init_sidtab();
|
---|
1458 | init_readerdb();
|
---|
1459 | cfg.account = init_userdb();
|
---|
1460 | init_signal();
|
---|
1461 | init_srvid();
|
---|
1462 | init_tierid();
|
---|
1463 | init_provid();
|
---|
1464 |
|
---|
1465 | start_garbage_collector(gbdb);
|
---|
1466 |
|
---|
1467 | cacheex_init();
|
---|
1468 |
|
---|
1469 | init_len4caid();
|
---|
1470 | init_irdeto_guess_tab();
|
---|
1471 |
|
---|
1472 | write_versionfile(false);
|
---|
1473 |
|
---|
1474 | led_init();
|
---|
1475 | led_status_default();
|
---|
1476 |
|
---|
1477 | azbox_init();
|
---|
1478 |
|
---|
1479 | mca_init();
|
---|
1480 |
|
---|
1481 | global_whitelist_read();
|
---|
1482 | cacheex_load_config_file();
|
---|
1483 |
|
---|
1484 | for (i = 0; i < CS_MAX_MOD; i++) {
|
---|
1485 | struct s_module *module = &modules[i];
|
---|
1486 | if ((module->type & MOD_CONN_NET) && module->ptab) {
|
---|
1487 | for (j = 0; j < module->ptab->nports; j++) {
|
---|
1488 | start_listener(module, &module->ptab->ports[j]);
|
---|
1489 | }
|
---|
1490 | }
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | //set time for server to now to avoid 0 in monitor/webif
|
---|
1494 | first_client->last=time((time_t *)0);
|
---|
1495 |
|
---|
1496 | webif_init();
|
---|
1497 |
|
---|
1498 | start_thread((void *) &reader_check, "reader check");
|
---|
1499 | start_thread((void *) &check_thread, "check");
|
---|
1500 |
|
---|
1501 | lcd_thread_start();
|
---|
1502 |
|
---|
1503 | do_report_emm_support();
|
---|
1504 |
|
---|
1505 | init_cardreader();
|
---|
1506 |
|
---|
1507 | cs_waitforcardinit();
|
---|
1508 |
|
---|
1509 | led_status_starting();
|
---|
1510 |
|
---|
1511 | ac_init();
|
---|
1512 |
|
---|
1513 | for (i = 0; i < CS_MAX_MOD; i++) {
|
---|
1514 | struct s_module *module = &modules[i];
|
---|
1515 | if ((module->type & MOD_CONN_SERIAL) && module->s_handler)
|
---|
1516 | module->s_handler(NULL, NULL, i);
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | // main loop function
|
---|
1520 | process_clients();
|
---|
1521 |
|
---|
1522 | // Cleanup
|
---|
1523 | azbox_close();
|
---|
1524 | coolapi_close_all();
|
---|
1525 | mca_close();
|
---|
1526 |
|
---|
1527 | led_status_stopping();
|
---|
1528 | led_stop();
|
---|
1529 | lcd_thread_stop();
|
---|
1530 |
|
---|
1531 | remove_versionfile();
|
---|
1532 |
|
---|
1533 | stat_finish();
|
---|
1534 | cccam_done_share();
|
---|
1535 |
|
---|
1536 | kill_all_clients();
|
---|
1537 | kill_all_readers();
|
---|
1538 |
|
---|
1539 | if (oscam_pidfile)
|
---|
1540 | unlink(oscam_pidfile);
|
---|
1541 |
|
---|
1542 | init_free_userdb(cfg.account);
|
---|
1543 | cfg.account = NULL;
|
---|
1544 | init_free_sidtab();
|
---|
1545 | config_free();
|
---|
1546 |
|
---|
1547 | cs_log("cardserver down");
|
---|
1548 | cs_close_log();
|
---|
1549 |
|
---|
1550 | stop_garbage_collector();
|
---|
1551 |
|
---|
1552 | return exit_oscam;
|
---|
1553 | }
|
---|