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

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