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.6KB

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