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.

responder.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include "responder.h"
  20. int pyfcgi_semid = 0;
  21. void init_context()
  22. {
  23. pyfcgi_semid = 0;
  24. }
  25. int responder_loop()
  26. {
  27. unsigned int n_wrk, wanted_n, n;
  28. pid_t *wrk_pids;
  29. int semid, err;
  30. int status;
  31. pid_t ret;
  32. struct sembuf sop;
  33. struct timespec timeout;
  34. short idle;
  35. sop.sem_num = 0;
  36. sop.sem_op = 0;
  37. sop.sem_flg = 0;
  38. timeout.tv_sec = 0;
  39. timeout.tv_nsec = 100000000;
  40. idle = 0;
  41. pyfcgi_log(LOG_INFO, "Preparing workers");
  42. init_context();
  43. wrk_pids = malloc(sizeof(int) * PyFCGI_conf.max_wrk);
  44. if(!wrk_pids)
  45. {
  46. err = errno;
  47. pyfcgi_log( LOG_ALERT,
  48. "Unable to allocate memory for childs PID : %s",
  49. strerror(err));
  50. clean_exit(err);
  51. }
  52. bzero(wrk_pids, sizeof(int) * PyFCGI_conf.max_wrk);
  53. semid = new_semaphore();
  54. wanted_n = PyFCGI_conf.min_wrk;
  55. n_wrk = 0;
  56. // prespawning minimum worker count
  57. for(n_wrk=0; n_wrk < wanted_n; n_wrk++)
  58. {
  59. wrk_pids[n_wrk] = spawn(n_wrk, semid);
  60. }
  61. //Wait at least for a process to be ready
  62. while(!semtimedop(semid, &sop, 1, &timeout));
  63. // main loop, taking care to restart terminated workers,
  64. // spawn new one if needed, etc.
  65. while(1)
  66. {
  67. if( (ret = waitpid(0, &status, WNOHANG)) )
  68. {
  69. if(ret < 0)
  70. {
  71. //TODO : error
  72. }
  73. if(!ret)
  74. {
  75. continue;
  76. }
  77. for(n=0; n<n_wrk; n++)
  78. {
  79. if(wrk_pids[n] == ret)
  80. {
  81. break;
  82. }
  83. }
  84. if(n == n_wrk)
  85. {
  86. pyfcgi_log(LOG_WARNING,
  87. "Child %d stopped but was notregistered",
  88. ret);
  89. continue;
  90. }
  91. idle=0;
  92. sop.sem_op = -1;
  93. ret = semop(semid, &sop, 1);
  94. sop.sem_op = 0;
  95. if(ret < 0)
  96. {
  97. err = errno;
  98. pyfcgi_log(LOG_ALERT,
  99. "Unable to dec sem after child exit : %s",
  100. strerror(err));
  101. clean_exit(err);
  102. }
  103. if(status)
  104. {
  105. if(WIFSIGNALED(status))
  106. {
  107. if(WTERMSIG(status) == 11)
  108. {
  109. pyfcgi_log(LOG_ALERT,
  110. "Worker[%d] segfault !",
  111. n);
  112. }
  113. else
  114. {
  115. pyfcgi_log(LOG_ALERT,
  116. "Worker[%d] terminated by signal %d",
  117. n, WTERMSIG(status));
  118. }
  119. }
  120. if(WEXITSTATUS(status) & PYFCGI_FATAL)
  121. {
  122. pyfcgi_log(LOG_ALERT,
  123. "Worker[%d] exited with status FATAL",
  124. n);
  125. //TODO : restart ?
  126. }
  127. else
  128. {
  129. pyfcgi_log(LOG_WARNING,
  130. "Worker[%d] exited with status %d",
  131. n, WEXITSTATUS(status));
  132. }
  133. }
  134. else
  135. {
  136. pyfcgi_log(LOG_INFO,
  137. "Worker[%d] PID %d exited normally",
  138. n, wrk_pids[n]);
  139. }
  140. // child stopped, looking for it
  141. if(wanted_n < n_wrk)
  142. { // need to shift the list and dec n_wrk
  143. pyfcgi_log(LOG_DEBUG, "GC Workers");
  144. pyfcgi_log( LOG_DEBUG, "GC want %d have %d", wanted_n, n_wrk);
  145. memmove(wrk_pids+n, wrk_pids+n+1,
  146. sizeof(pid_t) * (n_wrk - n));
  147. n_wrk--;
  148. }
  149. else
  150. { // respawn on same slot
  151. pyfcgi_log(LOG_INFO, "respawn #%d", n);
  152. wrk_pids[n] = spawn(n, semid);
  153. continue;
  154. }
  155. }
  156. ret = semtimedop(semid, &sop, 1, &timeout);
  157. //pyfcgi_log( LOG_DEBUG, "semtimeop ret=%d want %d have %d", ret, wanted_n, n_wrk);
  158. if(ret < 0)
  159. {
  160. err = errno;
  161. if(err == EAGAIN)
  162. {
  163. //pyfcgi_log(LOG_DEBUG, "IDLE want %d have %d\t min=%d", wanted_n, n_wrk, min_wrk);
  164. // workers idle
  165. if(!idle)
  166. {
  167. idle = 1;
  168. }
  169. else if(wanted_n > PyFCGI_conf.min_wrk
  170. && n_wrk - wanted_n < 2)
  171. {
  172. wanted_n--;
  173. }
  174. continue;
  175. }
  176. pyfcgi_log(LOG_ERR, "Unable to read semaphore : %s",
  177. strerror(err));
  178. }
  179. if(!ret && n_wrk < PyFCGI_conf.max_wrk)
  180. {
  181. idle=0;
  182. pyfcgi_log( LOG_DEBUG,
  183. "All workers busy, spawning a new one");
  184. n = n_wrk;
  185. n_wrk++;
  186. wanted_n = n_wrk;
  187. wrk_pids[n] = spawn(n, semid);
  188. }
  189. }
  190. //Debug wait & exit
  191. for(n_wrk=0; n_wrk != PyFCGI_conf.min_wrk; n_wrk++)
  192. {
  193. waitpid(wrk_pids[n_wrk], &status, 0);
  194. pyfcgi_log(LOG_DEBUG, "Child %d stopped with status %d",
  195. wrk_pids[n_wrk], status);
  196. }
  197. //printf("Content-Type: text/html\r\n\r\nHello world !\n");
  198. pyfcgi_log(LOG_INFO,"Child workers stoped, stopping responder");
  199. exit(0);
  200. }
  201. pid_t spawn(int wrk_id, int semid)
  202. {
  203. pid_t res;
  204. struct timespec timeout;
  205. timeout.tv_sec = 0;
  206. timeout.tv_nsec = 100000000;
  207. res = fork();
  208. if(res == -1)
  209. {
  210. pyfcgi_log(LOG_ERR, "Fork fails for worker #%d : %s",
  211. wrk_id, strerror(errno));
  212. return -1;
  213. }
  214. else if(!res)
  215. {
  216. // Child process
  217. if(PyFCGI_conf.pep333)
  218. {
  219. exit(work333(wrk_id, semid));
  220. }
  221. else
  222. {
  223. exit(work(wrk_id, semid));
  224. }
  225. }
  226. // Sleep to avoid spawning like hell thinking all workers are
  227. // busy. Let some time to this one to go up...
  228. // TODO: find a better way to avoid spawning to max_wrk
  229. nanosleep(&timeout, NULL);
  230. pyfcgi_log( LOG_INFO,
  231. "Worker #%d spawned with PID %d", wrk_id, res);
  232. return res;
  233. }
  234. int new_semaphore(key_t semkey)
  235. {
  236. int semid, err;
  237. semid = semget(IPC_PRIVATE, 2, 0770);
  238. if(semid == -1)
  239. {
  240. err = errno;
  241. pyfcgi_log( LOG_ALERT,
  242. "Unable to create semaphore : %s",
  243. strerror(err));
  244. clean_exit(err);
  245. }
  246. if(pyfcgi_semid)
  247. {
  248. pyfcgi_log( LOG_WARNING,
  249. "The semid context was not zero when calling new_semaphore, attempt to closeing it.");
  250. semctl(pyfcgi_semid, 0, IPC_RMID);
  251. }
  252. pyfcgi_semid = semid;
  253. return semid;
  254. }
  255. void clean_exit(int status)
  256. {
  257. if(pyfcgi_semid && semctl(pyfcgi_semid, 0, IPC_RMID) == -1)
  258. {
  259. pyfcgi_log( LOG_CRIT,
  260. "Unable to delete semaphore before exiting.");
  261. }
  262. exit(status);
  263. }