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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <sys/types.h>
  37. #include <sys/wait.h>
  38. #include "responder.h"
  39. #define IDENT_FMT "pyfcgi[%d]"
  40. #define MAX_REQS 50
  41. #define EARLY_ERR(err_str) write(2, err_str, strlen(err_str))
  42. int main(int argc, char **argv)
  43. {
  44. char *ident, *py_entrypoint;
  45. size_t ident_len;
  46. pid_t child;
  47. int child_ret;
  48. unsigned int emerg_sleep = 3;
  49. py_entrypoint = getenv("PY_ENTRYPOINT");
  50. if(argc > 1 || !py_entrypoint)
  51. {
  52. EARLY_ERR("Usage : PY_ENTRYPOINT='PATH.py' spawn-fcgi [OPTIONS] ");
  53. EARLY_ERR(argv[0]);
  54. EARLY_ERR("\n");
  55. return 1;
  56. }
  57. ident_len = snprintf(NULL, 0, IDENT_FMT, getpid());
  58. ident_len++;
  59. ident = malloc(sizeof(char)*ident_len);
  60. if(ident == NULL)
  61. {
  62. EARLY_ERR("Unable to allocate memory for ident string...\n");
  63. return -1;
  64. }
  65. snprintf(ident, ident_len, IDENT_FMT, getpid());
  66. openlog(ident, LOG_CONS | LOG_PERROR, LOG_DAEMON | LOG_USER);
  67. syslog(LOG_INFO, "New server started");
  68. while(1)
  69. {
  70. child = fork();
  71. if(child == -1)
  72. {
  73. // TODO : Error
  74. syslog(LOG_EMERG, "Failed to fork : '%s' sleeping %ds", strerror(errno), emerg_sleep);
  75. sleep(emerg_sleep);
  76. continue;
  77. }
  78. else if(!child)
  79. {
  80. responder_loop(py_entrypoint, MAX_REQS, 1, 30);
  81. exit((unsigned char)-1);
  82. }
  83. waitpid(child, &child_ret, 0);
  84. if(child_ret)
  85. {
  86. syslog(LOG_ERR, "Responder loop function exits with error code '%d'",
  87. WEXITSTATUS(child_ret));
  88. }
  89. else
  90. {
  91. syslog(LOG_NOTICE, "Restarting main process after %d requests", MAX_REQS);
  92. }
  93. }
  94. closelog();
  95. free(ident);
  96. }
  97. /**@mainpage PyFCGI
  98. * @section main_what What is PyFCGI ?
  99. * PyFCGI is a simple python3 fastcgi runner.
  100. *
  101. * Usage : *PY_ENTRYPOINT='foo' spawn-fcgi -d . -n src/pyfcgi -p 9000 -a 127.0.0.1*
  102. *
  103. * To run foo.entrypoint() python function.
  104. *
  105. * @subsection main_how_use How to use it ?
  106. *
  107. * @warning For the moment PyFCGI is under heavy developpement and in early
  108. * stage. Everything will change ;o)
  109. *
  110. * PyFCGI should be runned with spawn-fcgi (or something similar), allowing
  111. * to configure & forward environnement variables to libFCGI.
  112. *
  113. * For the moment no configuration files exists. You have to pass a
  114. * PY_ENTRYPOINT containing an importable python module containing an
  115. * entrypoint() (no arguments for the moment) function.
  116. *
  117. * When called this function will have to send valid CGI data to the webserver
  118. * using sys.stdin (the print() function for exemple) like :
  119. *<code>Content-type: text/html\\r\\n\\r\\nHello world !\\n</code>
  120. *
  121. * The function will have access to updated CGI os.environ containing
  122. * all informations about a request
  123. *
  124. * @subsubsection main_how_use_syslog Logging, using syslog
  125. *
  126. * Right now PyFCGI uses syslog() to log stuff using a pyfcgi ident.
  127. * PyFCGI logs can be filtered using /etc/rsyslog.d/pyfcgi.conf :
  128. *
  129. <pre>
  130. if ($programname contains 'pyfcgi') then {
  131. -/var/log/pyfcgi/pyfcgi.log
  132. stop
  133. }
  134. </pre>
  135. *
  136. * @subsubsection main_how_use_hardcode Hardcoded stuffs
  137. *
  138. * Right now there is a lot of hardcodd stuff. Fortunatly a vast majority
  139. * is in @ref main.c like the minimum and maximum number of workers, or
  140. * the number of requests before a worker restart. Some timers and stuff
  141. * are harcoded but should not in @ref pyworker.c & @ref responder.c
  142. *
  143. * @subsection main_how_works How it works ?
  144. *
  145. * - @ref processes
  146. * - @ref main_proc
  147. * - @ref work_master_proc
  148. * - @ref worker_process
  149. *
  150. * @subsection main_how_todo TODO
  151. * @todo Add another logging facility
  152. * @todo Add a configuration mecanism
  153. * @todo Implement a watchdog (request too long, or too much memory ?)
  154. */