Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conf.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include "conf.h"
  2. void usage()
  3. {
  4. static const struct option opts[] = PYFCGI_LONG_OPT;
  5. static const char *help[][2] = PYFCGI_OPT_HELP;
  6. size_t i;
  7. dprintf(2, "Usage : %s -e PYMODULE [-E PYFUN] [OPTIONS]\n", PYFCGI_NAME);
  8. dprintf(2, "\nOptions list :\n");
  9. i=0;
  10. while(opts[i].name)
  11. {
  12. dprintf(2, "\t-%c, --%s", opts[i].val, opts[i].name);
  13. switch(opts[i].has_arg)
  14. {
  15. case required_argument:
  16. dprintf(2, "=%s\n",
  17. help[i][1]?help[i][1]:"ARG");
  18. break;
  19. case optional_argument:
  20. dprintf(2, "[=%s]\n",
  21. help[i][1]?help[i][1]:"ARG");
  22. break;
  23. default: //no_argument
  24. dprintf(2, "\n");
  25. }
  26. dprintf(2, "\t\t%s\n", help[i][0]);
  27. i++;
  28. }
  29. dprintf(2, "%s", PYFCGI_HELP_TEXT);
  30. }
  31. void print_version(int fd)
  32. {
  33. char pyversion[16];
  34. pyfcgi_python_version(pyversion);
  35. dprintf(fd, "%s\nPython %s\n", PACKAGE_STRING, pyversion);
  36. }
  37. void default_conf()
  38. {
  39. memset(&PyFCGI_conf, 0, sizeof(pyfcgi_conf_t));
  40. PyFCGI_conf.context.pid = getpid();
  41. PyFCGI_conf.min_wrk = 1;
  42. PyFCGI_conf.max_wrk = 5;
  43. PyFCGI_conf.max_reqs = 1000;
  44. PyFCGI_conf.pep333 = 1;
  45. PyFCGI_conf.verbosity = 0;
  46. PyFCGI_conf.pool_timeout = 5;
  47. PyFCGI_conf.worker_timeout = 3;
  48. PyFCGI_conf.worker_gc_timeout = 7;
  49. PyFCGI_conf.context.uptime = time(NULL);
  50. }
  51. int parse_args(int argc, char *argv[])
  52. {
  53. static const struct option long_options[] = PYFCGI_LONG_OPT;
  54. char ident[] = "pyfcgi[XXXXXXXX]";
  55. int c, opt_i;
  56. while(1)
  57. {
  58. c = getopt_long(argc, argv, PYFCGI_SHORT_OPT, long_options,
  59. &opt_i);
  60. if(c == -1) { break; }
  61. switch(c)
  62. {
  63. case 'V':
  64. print_version(1);
  65. exit(0);
  66. case 'C':
  67. dprintf(2, "Config parser not yet implemented :'(\n");
  68. exit(1);
  69. case 'e':
  70. PyFCGI_conf.py_entrymod = strdup(optarg);
  71. break;
  72. case 'E':
  73. PyFCGI_conf.py_entryfun = strdup(optarg);
  74. break;
  75. case 'A':
  76. PyFCGI_conf.pep333 = 0;
  77. break;
  78. case 'w':
  79. PyFCGI_conf.min_wrk = atoi(optarg);
  80. break;
  81. case 'W':
  82. PyFCGI_conf.max_wrk = atoi(optarg);
  83. break;
  84. case 'v':
  85. PyFCGI_conf.verbosity = 1;
  86. pyfcgi_logger_add(NULL, 0xFF, 0xFF,
  87. PYFCGI_LOGGER_FMT_DEFAULT);
  88. break;
  89. case 'm':
  90. PyFCGI_conf.max_reqs = atoi(optarg);
  91. if(PyFCGI_conf.max_reqs < 0)
  92. {
  93. PyFCGI_conf.max_reqs = 0;
  94. }
  95. break;
  96. case 'f':
  97. PyFCGI_conf.worker_fast_spawn = 1;
  98. break;
  99. case 't':
  100. PyFCGI_conf.worker_timeout = atoi(optarg);
  101. if(PyFCGI_conf.worker_timeout < 0)
  102. {
  103. PyFCGI_conf.worker_timeout = 0;
  104. }
  105. break;
  106. case 'L':
  107. if(parse_optlog(optarg))
  108. {
  109. exit(1);
  110. }
  111. break;
  112. case 'S':
  113. snprintf(ident+7, 8, "%4d]", PyFCGI_conf.context.pid);
  114. pyfcgi_logger_enable_syslog(ident);
  115. break;
  116. case 'P':
  117. PyFCGI_conf.pidfile = strdup(optarg);
  118. /**@todo create pidfile and put master pid in it */
  119. break;
  120. case 's':
  121. if(pyfcgi_monitor_check_sock(optarg) < 0)
  122. {
  123. exit(1);
  124. }
  125. PyFCGI_conf.mon_socket = strdup(optarg);
  126. /**@todo check strdup returned value */
  127. break;
  128. case 'h':
  129. usage();
  130. exit(0);
  131. default:
  132. usage();
  133. exit(1);
  134. }
  135. }
  136. if(optind < argc)
  137. {
  138. for(opt_i=optind; opt_i<argc; opt_i++)
  139. {
  140. dprintf(2, "Unkown argument '%s'\n", argv[opt_i]);
  141. }
  142. usage();
  143. exit(1);
  144. }
  145. if(!PyFCGI_conf.py_entrymod)
  146. {
  147. dprintf(2, "No python entry module given... exiting\n");
  148. usage();
  149. exit(2);
  150. }
  151. if(!PyFCGI_conf.py_entryfun)
  152. {
  153. PyFCGI_conf.py_entryfun = PYENTRY_DEFAULT_FUN;
  154. }
  155. if(check_entrypoint_import())
  156. {
  157. usage();
  158. exit(3);
  159. }
  160. //Alloc workers PID array
  161. PyFCGI_conf.context.wrk_pids = malloc(sizeof(pid_t)*(PyFCGI_conf.max_wrk+1));
  162. if(!PyFCGI_conf.context.wrk_pids)
  163. {
  164. perror("Unable to allocate worker PID array");
  165. exit(PYFCGI_FATAL);
  166. }
  167. return 0;
  168. }
  169. int check_entrypoint_import()
  170. {
  171. pid_t pid;
  172. int status;
  173. void *ret;
  174. pid = fork();
  175. if(!pid)
  176. {
  177. pyinit();
  178. ret = (void*)import_entrypoint();
  179. if(!ret)
  180. {
  181. dprintf(2, "Unable to import entrypoint...\n");
  182. exit(1);
  183. }
  184. pyfcgi_log(LOG_DEBUG, "Entrypoint import [OK]");
  185. Py_Exit(0);
  186. }
  187. waitpid(pid, &status, 0);
  188. return WEXITSTATUS(status);
  189. }
  190. int parse_optlog(const char* logspec)
  191. {
  192. char *filename, *filter, *fmt;
  193. int filt;
  194. filename = strdup(logspec); /**@todo check error */
  195. filter = filename;
  196. while(*filter && *filter != ';') { filter++; }
  197. if(*filter)
  198. {
  199. *filter = '\0';
  200. filter++;
  201. }
  202. fmt = filter;
  203. while(*fmt && *fmt != ';') { fmt++; }
  204. if(*fmt)
  205. {
  206. *fmt = '\0';
  207. fmt++;
  208. }
  209. if(!strlen(filter))
  210. {
  211. filt = 0xFF;
  212. }
  213. else
  214. {
  215. filt = strtol(filter, NULL, !strncmp(filter, "0x", 2)?16:10);
  216. }
  217. fmt = strlen(fmt)?fmt:NULL;
  218. if(pyfcgi_logger_add(filename, filt, filt, fmt))
  219. {
  220. return 1;
  221. }
  222. return 0;
  223. }
  224. int pyfcgi_wd_init(void (*wd_sig_cleaner)(int), const struct timespec *delay)
  225. {
  226. struct sigaction act;
  227. struct sigevent sev;
  228. pyfcgi_context_t *context;
  229. struct timespec timeout;
  230. double part, tmp;
  231. int err;
  232. context = &(PyFCGI_conf.context);
  233. timeout = PyFCGI_conf.context.wd_delay = *delay;
  234. //kill delay timeout
  235. timeout.tv_nsec *= 1.5;
  236. part = modf(((double)timeout.tv_sec/2.0), &tmp);
  237. timeout.tv_nsec += part * 1000000000;
  238. timeout.tv_sec = (time_t)(timeout.tv_sec * 1.5);
  239. timeout.tv_sec += timeout.tv_nsec / 1000000000;
  240. if(part != 0.0)
  241. {
  242. timeout.tv_nsec /= (long)(part * 1000000000);
  243. }
  244. PyFCGI_conf.context.wd_killdelay = timeout;
  245. pyfcgi_log(LOG_DEBUG,
  246. "Set KILL watchdog with %d.%09ds timeout (%d.%09ds)",
  247. PyFCGI_conf.context.wd_delay.tv_sec,
  248. PyFCGI_conf.context.wd_delay.tv_nsec,
  249. PyFCGI_conf.context.wd_killdelay.tv_sec,
  250. PyFCGI_conf.context.wd_killdelay.tv_nsec);
  251. // Creating new timer with default sigevent (SIGEV_SIGNAL with SIGALRM)
  252. if(timer_create(CLOCK_REALTIME, NULL, &(context->wd_timer)) < 0)
  253. {
  254. err = errno;
  255. pyfcgi_log(LOG_ALERT, "Unable to create watchdog timer : %s",
  256. strerror(err));
  257. errno = err;
  258. return -1;
  259. }
  260. // Creating a new timer sending SIGKILL after 1.5 delay
  261. sev.sigev_notify = SIGEV_SIGNAL;
  262. sev.sigev_signo = SIGKILL;
  263. if(timer_create(CLOCK_REALTIME, &sev, &(context->wd_timerkill)) < 0)
  264. {
  265. err = errno;
  266. pyfcgi_log(LOG_ALERT, "Unable to create kill watchdog timer : %s",
  267. strerror(err));
  268. errno = err;
  269. return -1;
  270. }
  271. // Registering SIGALRM signal handler
  272. act.sa_handler = wd_sig_cleaner?wd_sig_cleaner:pyfcgi_wd_default_sighandler;
  273. sigemptyset(&act.sa_mask);
  274. act.sa_flags = 0;
  275. act.sa_restorer = NULL;
  276. if(sigaction(SIGALRM, &act, &(context->wd_oldsig)) < 0)
  277. {
  278. err = errno;
  279. pyfcgi_log(LOG_ALERT, "Unable to set the signal handler for watchdog timer : %s",
  280. strerror(err));
  281. errno = err;
  282. return -1;
  283. }
  284. context->wd = 1;
  285. return 0;
  286. }
  287. int pyfcgi_wd_arm()
  288. {
  289. pyfcgi_context_t *context;
  290. struct itimerspec timeout;
  291. int err, res;
  292. context = &(PyFCGI_conf.context);
  293. if(!context->wd) { return 0; }
  294. timeout.it_value = context->wd_delay;
  295. timeout.it_interval.tv_sec = 0;
  296. timeout.it_interval.tv_nsec = 0;
  297. context = &(PyFCGI_conf.context);
  298. res = 0;
  299. if(timer_settime(context->wd_timer, 0, &timeout, NULL) < 0)
  300. {
  301. err = errno;
  302. pyfcgi_log(LOG_ALERT, "Unable to arm watchdog : %s",
  303. strerror(err));
  304. res = -1;
  305. }
  306. timeout.it_value = context->wd_killdelay;
  307. timeout.it_interval = timeout.it_value;
  308. if(timer_settime(context->wd_timerkill, 0, &timeout, NULL) < 0)
  309. {
  310. err = errno;
  311. pyfcgi_log(LOG_ALERT, "Unable to pause killer watchdog : %s",
  312. strerror(err));
  313. res = -1;
  314. }
  315. return res;
  316. }
  317. int pyfcgi_wd_pause()
  318. {
  319. pyfcgi_context_t *context;
  320. struct itimerspec zero;
  321. int err, res;
  322. context = &(PyFCGI_conf.context);
  323. if(!context->wd) { return 0; }
  324. memset(&zero, 0, sizeof(struct itimerspec));
  325. res = 0;
  326. if(timer_settime(context->wd_timer, TIMER_ABSTIME, &zero, NULL) < 0)
  327. {
  328. err = errno;
  329. pyfcgi_log(LOG_ALERT, "Unable to pause watchdog : %s",
  330. strerror(err));
  331. res = -1;
  332. }
  333. if(timer_settime(context->wd_timerkill, TIMER_ABSTIME, &zero, NULL) < 0)
  334. {
  335. err = errno;
  336. pyfcgi_log(LOG_ALERT, "Unable to pause killer watchdog : %s",
  337. strerror(err));
  338. res = -1;
  339. }
  340. return res;
  341. }
  342. int pyfcgi_wd_stop()
  343. {
  344. int err, res;
  345. pyfcgi_context_t *context;
  346. context = &(PyFCGI_conf.context);
  347. if(!context->wd) { return 0; }
  348. res = 0;
  349. if(timer_delete(context->wd_timer) < 0)
  350. {
  351. err = errno;
  352. pyfcgi_log(LOG_WARNING, "Unable to delete watchdog timer : %s",
  353. strerror(err));
  354. res = -1;
  355. }
  356. if(timer_delete(context->wd_timerkill) < 0)
  357. {
  358. err = errno;
  359. pyfcgi_log(LOG_WARNING, "Unable to delete killer watchdog timer : %s",
  360. strerror(err));
  361. res = -1;
  362. }
  363. if(sigaction(SIGALRM, &(context->wd_oldsig), NULL) < 0)
  364. {
  365. err = errno;
  366. pyfcgi_log(LOG_WARNING, "Unable to restore signal handler : %s",
  367. strerror(err));
  368. res = -1;
  369. }
  370. context->wd = 0;
  371. pyfcgi_log(LOG_DEBUG, "Watchdog stopped");
  372. return res;
  373. }
  374. void pyfcgi_wd_default_sighandler(int signum)
  375. {
  376. pyfcgi_log(LOG_ALERT, "Timeout after %d.%09ds",
  377. PyFCGI_conf.context.wd_delay.tv_sec,
  378. PyFCGI_conf.context.wd_delay.tv_nsec);
  379. pyfcgi_wd_stop();
  380. exit(PYFCGI_TIMEOUT);
  381. }
  382. char *status2str(int status)
  383. {
  384. char buff[1024], *buffptr;
  385. size_t left = 1024, ret;
  386. short reason;
  387. reason = 0;
  388. buffptr = buff;
  389. if(status & (PYFCGI_WORKER_FAIL | PYFCGI_MASTER_FAIL))
  390. {
  391. buffptr += ret = snprintf(buffptr, left, "%s ",
  392. (status & PYFCGI_WORKER_FAIL)?"Worker":"Master");
  393. left -= ret;
  394. status &= ((0xFFFF) ^ (PYFCGI_WORKER_FAIL | PYFCGI_MASTER_FAIL));
  395. }
  396. if(status & PYFCGI_FATAL)
  397. {
  398. buffptr += ret = snprintf(buffptr, left, "fatal ");
  399. left -= ret;
  400. status ^= PYFCGI_FATAL;
  401. }
  402. if(status & EXIT_PYERR)
  403. {
  404. buffptr += ret = snprintf(buffptr, left, "Python");
  405. left -= ret;
  406. status ^= EXIT_PYERR;
  407. }
  408. if( status & PYFCGI_TIMEOUT)
  409. {
  410. buffptr += ret = snprintf(buffptr, left, "Timeout");
  411. left -= ret;
  412. reason = 1;
  413. status ^= PYFCGI_TIMEOUT;
  414. }
  415. if(status & PYFCGI_ERR)
  416. {
  417. buffptr += ret = snprintf(buffptr, left, "Error");
  418. left -= ret;
  419. reason = 1;
  420. status ^= PYFCGI_ERR;
  421. }
  422. if(!reason)
  423. {
  424. buffptr += ret = snprintf(buffptr, left, "Failure");
  425. left -= ret;
  426. }
  427. if(status)
  428. {
  429. buffptr += ret = snprintf(buffptr, left, "%d", status);
  430. left -= ret;
  431. }
  432. return strndup(buff, 1024);
  433. }
  434. void pyfcgi_sighandler_drop(int signum)
  435. {
  436. pyfcgi_log(LOG_DEBUG, "Catching signal %s", strsignal(signum));
  437. return;
  438. }