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.

main.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (C) 2019 Weber Yann
  3. *
  4. * This file is part of PyFCGI.
  5. *
  6. * PyFCGI is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * PyFCGI is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PyFCGI. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**@defgroup processes Process organisation of PyFCGI
  20. *
  21. * PyFCGI is organised in three layer :
  22. * A @ref main_proc : simple, that keep running and spawn a
  23. * @ref work_master_proc . This process handles @ref worker_process creation
  24. * and try to maintain a pool able to reply efficiently to CGI requests.
  25. */
  26. /**@defgroup main_proc Main process
  27. * @brief The main process in the @ref main() function
  28. * @ingroup processes
  29. */
  30. #include <fcgi_stdio.h> /* fcgi library; put it first*/
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <syslog.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <signal.h>
  37. #include <time.h>
  38. #include <sys/types.h>
  39. #include <sys/wait.h>
  40. #include "conf.h"
  41. #include "logger.h"
  42. #include "responder.h"
  43. #include "ipc.h"
  44. #define IDENT_FMT "pyfcgi[%d]"
  45. #define MAX_REQS 1024
  46. #define EARLY_ERR(err_str) write(2, err_str, strlen(err_str))
  47. extern pyfcgi_conf_t PyFCGI_conf;
  48. pid_t pool_handler_pid;
  49. pid_t monitor_serv_pid;
  50. void sighandler(int signum)
  51. {
  52. int ret;
  53. if(signum == SIGALRM)
  54. {
  55. pyfcgi_log(LOG_WARNING, "Master process received SIGALRM !");
  56. return;
  57. }
  58. else if(signum == SIGINT)
  59. {
  60. pyfcgi_log(LOG_INFO,
  61. "Master process received ctrl+c, exiting...");
  62. }
  63. else
  64. {
  65. pyfcgi_log(LOG_INFO,
  66. "Master process received signal %s(%d), exiting...",
  67. strsignal(signum), signum);
  68. }
  69. if(pool_handler_pid)
  70. {
  71. kill(pool_handler_pid, SIGTERM);
  72. waitpid(pool_handler_pid, &ret, 0);
  73. }
  74. if(monitor_serv_pid)
  75. {
  76. kill(monitor_serv_pid, SIGTERM);
  77. waitpid(monitor_serv_pid, &ret, 0);
  78. }
  79. pyfcgi_log(LOG_INFO,
  80. "Master process exiting.");
  81. exit(0);
  82. }
  83. void debug_sighandler(int signum)
  84. {
  85. pyfcgi_log(LOG_WARNING, "Master process received signal %s(%d)",
  86. strsignal(signum), signum);
  87. return;
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. int emerg_sleep = 3;
  92. int child_ret;
  93. pid_t rpid;
  94. struct sigaction act;
  95. short fails, need_wait;
  96. //Sleeping on waitpid WNOHANG
  97. struct timespec tsleep;
  98. char *child_name;
  99. act.sa_handler = sighandler;
  100. sigemptyset(&act.sa_mask);
  101. sigaddset(&act.sa_mask, SIGTERM);
  102. act.sa_flags = 0;
  103. act.sa_restorer = NULL;
  104. if(sigaction(SIGINT, &act, NULL))
  105. {
  106. perror("Sigaction error");
  107. exit(4);
  108. }
  109. sigfillset(&act.sa_mask);
  110. sigdelset(&act.sa_mask, SIGINT);
  111. sigdelset(&act.sa_mask, SIGTERM);
  112. act.sa_handler = debug_sighandler;
  113. if(sigaction(SIGALRM, &act, NULL))
  114. {
  115. perror("Sigaction2 error");
  116. exit(4);
  117. }
  118. pool_handler_pid = 0;
  119. monitor_serv_pid = 0;
  120. default_conf();
  121. pyfcgi_name_sems(getpid()); //name semaphore using master proc PID
  122. pyfcgi_logger_init();
  123. pyfcgi_logger_set_ident("MainProc");
  124. if(parse_args(argc, argv))
  125. {
  126. return 1;
  127. }
  128. pyfcgi_log(LOG_INFO, "New server started");
  129. while(1)
  130. {
  131. if(!pool_handler_pid)
  132. {
  133. pool_handler_pid = spawn_pool_handler();
  134. if(pool_handler_pid < 0)
  135. {
  136. fails = 1;
  137. pool_handler_pid = 0;
  138. }
  139. }
  140. if(PyFCGI_conf.mon_socket && !monitor_serv_pid)
  141. {
  142. monitor_serv_pid = pyfcgi_spawn_monitor();
  143. if(monitor_serv_pid < 0)
  144. {
  145. fails = 1;
  146. monitor_serv_pid = 0;
  147. }
  148. }
  149. if(fails)
  150. {
  151. if(emerg_sleep > 600)
  152. {
  153. pyfcgi_log(LOG_EMERG, "Abording...");
  154. exit(PYFCGI_FATAL);
  155. }
  156. fails = 0;
  157. pyfcgi_log(LOG_WARNING, "Sleeping %ds",
  158. emerg_sleep);
  159. emerg_sleep *= 2;
  160. continue;
  161. }
  162. else
  163. {
  164. emerg_sleep = 3;
  165. }
  166. need_wait = ((!PyFCGI_conf.mon_socket || monitor_serv_pid)
  167. && pool_handler_pid);
  168. rpid = waitpid(0, &child_ret, need_wait?0:WNOHANG);
  169. if(rpid == pool_handler_pid)
  170. {
  171. child_name = "Pool handler";
  172. pool_handler_pid = 0;
  173. if(!child_ret)
  174. {
  175. pyfcgi_log(LOG_NOTICE,
  176. "Restarting main process after %d requests",
  177. MAX_REQS);
  178. continue;
  179. }
  180. }
  181. else if(rpid == monitor_serv_pid)
  182. {
  183. child_name = "Monitor server";
  184. monitor_serv_pid = 0;
  185. }
  186. else if(rpid == 0 && !need_wait)
  187. {
  188. tsleep.tv_sec = 0;
  189. tsleep.tv_nsec = 100000000;
  190. nanosleep(&tsleep, NULL);
  191. continue;
  192. }
  193. else if(rpid < 0)
  194. {
  195. pyfcgi_log(LOG_EMERG, "Unable to waitpid : %s",
  196. strerror(errno));
  197. exit(PYFCGI_FATAL);
  198. }
  199. else
  200. {
  201. child_name = "Unknown child";
  202. pyfcgi_log(LOG_WARNING,
  203. "Unknown child (PID %d) exited...", rpid);
  204. }
  205. if(child_ret)
  206. {
  207. pyfcgi_log(LOG_ERR, "%s exits with error code '%d'",
  208. child_name, WEXITSTATUS(child_ret));
  209. if(WIFSIGNALED(child_ret))
  210. {
  211. pyfcgi_log(LOG_ERR, "%s terminated by sig %s(%d)",
  212. child_name,
  213. strsignal(WTERMSIG(child_ret)),
  214. WTERMSIG(child_ret));
  215. }
  216. }
  217. else
  218. {
  219. pyfcgi_log(LOG_WARNING,
  220. "%s exits with no error status 0",
  221. child_name);
  222. }
  223. }
  224. closelog();
  225. }
  226. /**@mainpage PyFCGI
  227. * @section main_what What is PyFCGI ?
  228. * PyFCGI is a simple python3 fastcgi runner.
  229. *
  230. * Usage : Usage : spawn-fcgi [OPTIONS] -- pyfcgi -e PYMODULE [-E PYFUN] [OPTIONS]
  231. *
  232. * To run foo.entrypoint() python function.
  233. *
  234. * @subsection main_how_use How to use it ?
  235. *
  236. * @warning For the moment PyFCGI is under heavy developpement and in early
  237. * stage. Everything will change ;o)
  238. *
  239. * PyFCGI should be runned with spawn-fcgi (or something similar), allowing
  240. * to configure & forward environnement variables to libFCGI.
  241. *
  242. * For the moment no configuration files exists. You have to pass arguments
  243. * to pyfcgi using -- argument of spawnn-fcgi
  244. *
  245. * When called this function will have to send valid CGI data to the webserver
  246. * using sys.stdin (the print() function for exemple) like :
  247. *<code>Content-type: text/html\\r\\n\\r\\nHello world !\\n</code>
  248. *
  249. * The function will have access to updated CGI os.environ containing
  250. * all informations about a request
  251. *
  252. * @subsubsection main_how_use_syslog Logging, using syslog
  253. *
  254. * Right now PyFCGI uses pyfcgi_log() to log stuff using a pyfcgi ident.
  255. * PyFCGI logs can be filtered using /etc/rsyslog.d/pyfcgi.conf :
  256. *
  257. <pre>
  258. if ($programname contains 'pyfcgi') then {
  259. -/var/log/pyfcgi/pyfcgi.log
  260. stop
  261. }
  262. </pre>
  263. *
  264. * @subsubsection main_how_use_hardcode Hardcoded stuffs
  265. *
  266. * Right now there is a lot of hardcodd stuff. Fortunatly a vast majority
  267. * is in @ref main.c like the minimum and maximum number of workers, or
  268. * the number of requests before a worker restart. Some timers and stuff
  269. * are harcoded but should not in @ref pyworker.c & @ref responder.c
  270. *
  271. * @subsection main_how_works How it works ?
  272. *
  273. * - @ref processes
  274. * - @ref main_proc
  275. * - @ref work_master_proc
  276. * - @ref worker_process
  277. *
  278. */