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.h 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifndef __CONF_H___
  20. #define __CONF_H___
  21. #include <fcgiapp.h>
  22. #include <fcgi_stdio.h> /* fcgi library; put it first*/
  23. #define PY_SSIZE_T_CLEAN
  24. #include <Python.h>
  25. #include "structmember.h"
  26. #include <unistd.h>
  27. #include <getopt.h>
  28. #include <limits.h>
  29. #include <semaphore.h>
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include "config.h"
  33. /**@defgroup conf_internal PYFCGI configuration handling
  34. */
  35. /**@defgroup conf_glob PYFCGI global (for all process) configurations
  36. * @see struct_pyfcgi_conf_s
  37. * @ingroup conf_internal */
  38. /**@brief Friendly name for @ref struct pyfcgi_conf_s
  39. * @see struct pyfcgi_conf_s */
  40. typedef struct pyfcgi_conf_s pyfcgi_conf_t;
  41. typedef struct pyfcgi_conf_logger_s pyfcgi_conf_logger_t;
  42. typedef struct pyfcgi_context_s pyfcgi_context_t;
  43. typedef struct pyfcgi_semdata_s pyfcgi_semdata_t;
  44. /**@brief Configuration globals, inherited from parent to childs
  45. * @ingroup conf_internal
  46. */
  47. pyfcgi_conf_t PyFCGI_conf;
  48. /* Include other PyFCGI stuff */
  49. #include "logger.h"
  50. #include "pyutils.h"
  51. #include "ipc.h"
  52. #include "monitor.h"
  53. /**@defgroup ret_status Function & process return status
  54. */
  55. /**@ingroup ret_status */
  56. #define EXIT_PYERR 4
  57. /**@ingroup ret_status */
  58. #define PYFCGI_TIMEOUT 8
  59. /**@ingroup ret_status */
  60. #define PYFCGI_ERR 16
  61. /**@ingroup ret_status */
  62. #define PYFCGI_WORKER_FAIL 32
  63. /**@ingroup ret_status */
  64. #define PYFCGI_MASTER_FAIL 64
  65. /**@ingroup ret_status */
  66. #define PYFCGI_FATAL 128
  67. #define PYFCGI_NAME "spawn-fcgi [OPTIONS] -- pyfcgi"
  68. #define PYFCGI_SHORT_OPT "Ce:E:Aw:W:m:ft:L:SPs:vVh"
  69. #define PYFCGI_LONG_OPT { \
  70. {"config", required_argument, 0, 'C'},\
  71. {"pymodule", required_argument, 0, 'e'},\
  72. {"pyapp", required_argument, 0, 'E'},\
  73. {"alt-io", no_argument, 0, 'A'},\
  74. {"min-worker", required_argument, 0, 'w'},\
  75. {"max-worker", required_argument, 0, 'W'},\
  76. {"max-request", required_argument, 0, 'm'},\
  77. {"fast-spawn", no_argument, 0, 'f'},\
  78. {"timeout", required_argument, 0, 't'},\
  79. {"log", required_argument, 0, 'L'},\
  80. {"syslog", no_argument, 0, 'S'},\
  81. {"pid-file", required_argument, 0, 'P'},\
  82. {"socket-server", required_argument, 0, 's'},\
  83. {"verbose", no_argument, 0, 'v'},\
  84. {"version", no_argument, 0, 'V'},\
  85. {"help", no_argument, 0, 'h' },\
  86. {0, 0, 0, 0}\
  87. }
  88. #define PYFCGI_OPT_HELP {\
  89. {"Load options from configuration file", "CONFIG"},\
  90. {"Search application function in given python module", "MODULE_NAME"},\
  91. {"Python application entrypoint function name", "FUNC_NAME"},\
  92. {"Use stdout to communicate with web server instead of entrypoint return as specified in PEP 333", NULL},\
  93. {"Minimum worker in the pool", "INT"},\
  94. {"Maximum worker in the pool", "INT"},\
  95. {"Request count after wich the worker is restarted (if 0 never restart)", "INT"},\
  96. {"If not given there is at least 1s between 2 child creation, else childs may be spawned in small burst", NULL},\
  97. {"Request timeout (before worker restarts)", "SECONDS"},\
  98. {"Add a logfile using syntax : 'LOGFILE[;FILT][;FMT]'", "LOGGER_SPEC"},\
  99. {"Use syslog for logging", NULL},\
  100. {"Create a PID file", "FILENAME"},\
  101. {"Indicate a socket (for example 'udp://localhost:8765' ) to listen on, replying stats & status", "SOCKET"},\
  102. {"Send all loglines on stderr", NULL},\
  103. {"Print PyFCGI and Python version and exit", NULL},\
  104. {"Display this help and exit", NULL},\
  105. }
  106. #define PYFCGI_HELP_TEXT "\n\
  107. Logger specification format 'LOGFILE[;FILT][;FMT]' with :\n\
  108. \t- LOGFILE the log file name\n\
  109. \t- FILT a number (in decimal or hexadicimal 0xXX) indicating wich\n\
  110. \t facility/level to log\n\
  111. \t- FMT the logline format in a special markup format using fields between { }\n\
  112. \t supported fields are :\n\
  113. \t\t- {datetime} {datetime:SIZE} {datetime:SIZE:FMT} defines a format and a \n\
  114. \t\t constant length for a datetime field. Default : {datetime:25:%F %T%z}\n\
  115. \t\t- {level} the loglevel \n\
  116. \t\t- {facility} the log facility\n\
  117. \t\t- {pid} the process PID\n\
  118. \t\t- {ident} the defined ident (set by process)\n\
  119. \t\t- {msg} the log message (can only appear once)\n\
  120. You can escape { and } by using {{ and }} and all field names can by\n\
  121. abbreviated to one character.\n\n\
  122. Socket URL specification format : PROT://HOST[:PORT] with PROT on of \n\
  123. \t- tcp for tcp sockets and HOST a valid INET address\n\
  124. \t- udp for udp sockets and HOST a valid INET address\n\
  125. \t- unix for file sockets and HOST a valid path\n\
  126. "
  127. #define PYENTRY_DEFAULT_FUN "application"
  128. /**@brief Stores a semaphore name and pointer */
  129. struct pyfcgi_semdata_s {
  130. /**@brief Semaphore names. Set by master process */
  131. char name[NAME_MAX - 4];
  132. /**@brief Semaphore pointer. */
  133. sem_t *sem;
  134. };
  135. /**@brief Stores contextual informations about current process */
  136. struct pyfcgi_context_s {
  137. /**@brief Stores current process PID */
  138. pid_t pid;
  139. /**@brief Stores parent process PID */
  140. pid_t ppid;
  141. /**@brief Stores the watchdog timer */
  142. timer_t wd_timer;
  143. /**@brief Security timer sending a sigkill */
  144. timer_t wd_timerkill;
  145. /**@brief Watchdog delay */
  146. struct timespec wd_delay;
  147. /**@brief Watchdog sig restorer */
  148. struct sigaction wd_oldsig;
  149. /**@brief watchdog flag */
  150. short wd;
  151. /**@brief array of worker pids (pool handler context) */
  152. pid_t **wrk_pids;
  153. /**@brief workers count */
  154. unsigned int n_wrk;
  155. /**@brief Stores IPC flag used in init */
  156. pyfcgi_ipc_flag_t ipc_flag;
  157. /**@brief Stores python_path (not dupped by python) */
  158. wchar_t python_path[PATH_MAX];
  159. /**@brief Stores venv_path for python home (not dupped by python) */
  160. wchar_t venv_path[PATH_MAX];
  161. /**@brief Stores a part of the environ (containing wsgi.* keys) */
  162. PyObject *wsgi_dict;
  163. };
  164. /**@brief Structure containing configuration
  165. * @ingroup conf_internal
  166. * The structure is used for the global @ref pyfcgi_conf variable.
  167. * @see pyfcgi_conf_t
  168. */
  169. struct pyfcgi_conf_s
  170. {
  171. /** @brief Stores pidfile path */
  172. char *pidfile;
  173. /**@brief Entrypoint module name
  174. * @ingroup conf_glob */
  175. char *py_entrymod;
  176. /**@brief Entrypoint function name
  177. * @ingroup conf_glob */
  178. char *py_entryfun;
  179. /**@brief If 0 use stdout to communicate with webserver, else
  180. * PyFCGI will expect PEP333 compliant entrypoint */
  181. short pep333;
  182. /**@brief Minimum count worker in pool
  183. * @ingroup conf_glob */
  184. int min_wrk;
  185. /**@brief Maximum count workers in pool
  186. * @ingroup conf_glob */
  187. int max_wrk;
  188. /**@brief Maximum request before a worker restarts (0 for no restart)
  189. * @ingroup conf_glob */
  190. int max_reqs;
  191. //Pool handling conf
  192. /**@brief Idle timeout : starts GC workers after Xs idle */
  193. time_t worker_gc_timeout;
  194. /**@brief Fast spawn : if 1 spawn more childs faster
  195. *
  196. * When not activated there is at least 1s between 2 child creation,
  197. * else child are created in small burst ( ~2 childs burst)
  198. */
  199. short worker_fast_spawn;
  200. //watchdogs config
  201. /**@brief Worker timeout in seconds (if 0 no timeout)*/
  202. time_t worker_timeout;
  203. /**@brief Pool timeout in seconds (if 0 no timeout)*/
  204. time_t pool_timeout;
  205. // logger config
  206. /**@brief 0 is silent 1 means logs to stderr */
  207. short verbosity;
  208. /**@brief Logger configuration
  209. * @ingroupe conf_glob */
  210. pyfcgi_conf_logger_t logs;
  211. /**@brief Context informations */
  212. pyfcgi_context_t context;
  213. /**@brief Semaphores informations
  214. *
  215. * - sems[0] is the worker state indicator : when a worker is idle the
  216. * sem is incremented
  217. * - sems[1] is the request counter sem : when a request is handled
  218. * this sem is incremented
  219. * - sems[2] is the stats/status SHM semaphore
  220. */
  221. pyfcgi_semdata_t sems[PYFCGI_NSEM];
  222. /**@brief Stores a copy of the specified socket URL to listen to. */
  223. char *mon_socket;
  224. /**@brief If 1 force ipv4 */
  225. short ipv4;
  226. /**@brief If 1 force ipv6 */
  227. short ipv6;
  228. };
  229. /**@brief Print usage on FD 2 (stdout) */
  230. void usage();
  231. /**@brief Print pyfcgi & python version on given fd */
  232. void print_version(int);
  233. /**@brief Init conf with default values */
  234. void default_conf();
  235. /**@brief Parse arguments and store them in conf
  236. * @return 0 if no error */
  237. int parse_args(int argc, char *argv[]);
  238. int check_entrypoint_import();
  239. int parse_optlog(const char*);
  240. /**@brief Initialize the watchdog
  241. * @param void (*wd_sig_cleaner)(int) pointer on a signal handler (or NULL to
  242. * use @ref pyfcgi_default_sighandler
  243. * @param const struct timespec* delay a pointer on the watchdog timeout
  244. * @return 0 if no error else -1
  245. */
  246. int pyfcgi_wd_init(void (*wd_sig_cleaner)(int), const struct timespec *delay);
  247. int pyfcgi_wd_arm();
  248. int pyfcgi_wd_pause();
  249. int pyfcgi_wd_stop();
  250. /**@brief Watchdog default signal handler */
  251. void pyfcgi_wd_default_sighandler(int signum);
  252. /**@brief Return a string representing a status
  253. * @param int status
  254. * @return a char* that should be freed
  255. */
  256. char *status2str(int);
  257. #endif