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.

ipc.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "ipc.h"
  2. static const char *semnames[] = PYFCGI_SEMNAMES;
  3. static const pyfcgi_ipc_flag_t semflg[PYFCGI_NSEM] = {
  4. IPC_WSTATE, IPC_WREQS, IPC_SEMST};
  5. void pyfcgi_name_IPC(pid_t master_pid)
  6. {
  7. short i;
  8. for(i=0; i<PYFCGI_NSEM; i++)
  9. {
  10. if(snprintf(PyFCGI_conf.sems[i].name, NAME_MAX - 4,
  11. PYFCGI_IPCNAME_FMT, master_pid, semnames[i]) < 0)
  12. {
  13. perror("Error allocating semaphore names");
  14. exit(PYFCGI_FATAL);
  15. }
  16. }
  17. if(snprintf(PyFCGI_conf.shm.name, NAME_MAX, PYFCGI_IPCNAME_FMT,
  18. master_pid, "SHMstats") < 0)
  19. {
  20. perror("Error setting shm name");
  21. exit(PYFCGI_FATAL);
  22. }
  23. PyFCGI_conf.shm.len = sizeof(pyfcgi_stats_shm_t);
  24. }
  25. int pyfcgi_IPC_create(pyfcgi_ipc_flag_t flag)
  26. {
  27. short i;
  28. int res, err;
  29. res = 0;
  30. for(i=0; i<PYFCGI_NSEM; i++)
  31. {
  32. if(flag & semflg[i])
  33. {
  34. flag ^= semflg[i];
  35. PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name,
  36. O_CREAT | O_EXCL, 0770, 0);
  37. if(PyFCGI_SEM(i).sem == SEM_FAILED)
  38. {
  39. err = errno;
  40. pyfcgi_log(LOG_ALERT,
  41. "Unable to create semaphore %s(named '%s') : %s",
  42. semnames[i], PyFCGI_SEM(i).name,
  43. strerror(err));
  44. PyFCGI_SEM(i).sem = NULL;
  45. res = -1;
  46. }
  47. else
  48. {
  49. PyFCGI_conf.context.ipc_flag |= semflg[i];
  50. }
  51. }
  52. }
  53. if(flag & IPC_SHMST)
  54. {
  55. flag ^= IPC_SHMST;
  56. PyFCGI_conf.shm.fd = shm_open(PyFCGI_conf.shm.name,
  57. O_RDWR | O_CREAT | O_EXCL, 0770);
  58. if(PyFCGI_conf.shm.fd == -1)
  59. {
  60. err = errno;
  61. pyfcgi_log(LOG_ALERT,
  62. "Unable to create SHM '%s' : %s",
  63. PyFCGI_conf.shm.name, strerror(errno));
  64. PyFCGI_conf.shm.fd = 0;
  65. return -1;
  66. }
  67. PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
  68. if(ftruncate(PyFCGI_conf.shm.fd,
  69. sizeof(pyfcgi_stats_shm_t)) < 0)
  70. {
  71. pyfcgi_log(LOG_ALERT,
  72. "Unable to truncate SHM to wanted size %ld : %s",
  73. sizeof(pyfcgi_stats_shm_t),
  74. strerror(errno));
  75. pyfcgi_IPC_destroy(IPC_SHMST);
  76. return -1;
  77. }
  78. pyfcgi_log(LOG_INFO, "MMAP LENGHt : %ld", PyFCGI_conf.shm.len);
  79. PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
  80. PROT_READ | PROT_WRITE, MAP_SHARED, PyFCGI_conf.shm.fd,
  81. 0);
  82. if(PyFCGI_conf.shm.ptr == (void*)-1)
  83. {
  84. pyfcgi_log(LOG_ALERT,
  85. "Unable to mmap SHM : %s", strerror(errno));
  86. pyfcgi_IPC_destroy(IPC_SHMST);
  87. return -1;
  88. }
  89. }
  90. return res;
  91. }
  92. int pyfcgi_IPC_init(pyfcgi_ipc_flag_t flag)
  93. {
  94. short i;
  95. int res, err;
  96. res = 0;
  97. for(i=0; i<PYFCGI_NSEM; i++)
  98. {
  99. if(flag & semflg[i])
  100. {
  101. flag ^= semflg[i];
  102. PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name, 0);
  103. if(PyFCGI_SEM(i).sem == SEM_FAILED)
  104. {
  105. err = errno;
  106. pyfcgi_log(LOG_ALERT,
  107. "Unable to open semaphore %s(named '%s') : %s",
  108. semnames[i], PyFCGI_SEM(i).name,
  109. strerror(err));
  110. PyFCGI_SEM(i).sem = NULL;
  111. res = -1;
  112. }
  113. else
  114. {
  115. PyFCGI_conf.context.ipc_flag |= semflg[i];
  116. }
  117. }
  118. }
  119. if(flag & IPC_SHMST)
  120. {
  121. flag ^= IPC_SHMST;
  122. PyFCGI_conf.shm.fd = shm_open(PyFCGI_conf.shm.name, O_RDONLY, 0);
  123. if(PyFCGI_conf.shm.fd == -1)
  124. {
  125. err = errno;
  126. pyfcgi_log(LOG_ALERT,
  127. "Unable to open SHM '%s' : %s",
  128. PyFCGI_conf.shm.name, strerror(errno));
  129. PyFCGI_conf.shm.fd = 0;
  130. return -1;
  131. }
  132. PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
  133. PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
  134. PROT_READ, MAP_SHARED, PyFCGI_conf.shm.fd, 0);
  135. if(PyFCGI_conf.shm.ptr == (void*)-1)
  136. {
  137. pyfcgi_log(LOG_ALERT,
  138. "Unable to mmap SHM : %s", strerror(errno));
  139. pyfcgi_IPC_destroy(IPC_SHMST);
  140. return -1;
  141. }
  142. }
  143. return res;
  144. }
  145. int pyfcgi_IPC_close()
  146. {
  147. short i;
  148. int res, err;
  149. pyfcgi_ipc_flag_t flag;
  150. flag = PyFCGI_conf.context.ipc_flag;
  151. res = 0;
  152. for(i=0; i<PYFCGI_NSEM; i++)
  153. {
  154. if(flag & semflg[i])
  155. {
  156. if(sem_close(PyFCGI_SEM(i).sem) < 0)
  157. {
  158. err = errno;
  159. pyfcgi_log(LOG_ALERT,
  160. "Unable to close semaphore %s('%s') : %s",
  161. semnames[i], PyFCGI_SEM(i).name,
  162. strerror(err));
  163. PyFCGI_SEM(i).sem = NULL;
  164. }
  165. PyFCGI_SEM(i).sem = NULL;
  166. }
  167. }
  168. if(flag & IPC_SHMST)
  169. {
  170. if(PyFCGI_conf.shm.ptr &&
  171. munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
  172. {
  173. pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
  174. strerror(errno));
  175. }
  176. if(close(PyFCGI_conf.shm.fd) < 0)
  177. {
  178. pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
  179. strerror(errno));
  180. }
  181. PyFCGI_conf.shm.fd = 0;
  182. PyFCGI_conf.shm.ptr = NULL;
  183. PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  184. }
  185. return res;
  186. }
  187. int pyfcgi_IPC_destroy(pyfcgi_ipc_flag_t flag)
  188. {
  189. short i;
  190. int err, res;
  191. res = 0;
  192. for(i=0; i<PYFCGI_NSEM; i++)
  193. {
  194. if(flag & semflg[i])
  195. {
  196. if(sem_unlink(PyFCGI_SEM(i).name) < 0)
  197. {
  198. err = errno;
  199. pyfcgi_log(LOG_ALERT,
  200. "Unable to destroy %s semaphore (%s) : %s",
  201. semnames[i], PyFCGI_SEM(i).name,
  202. strerror(err));
  203. res = -1;
  204. }
  205. PyFCGI_SEM(i).sem = NULL;
  206. }
  207. }
  208. if(flag & IPC_SHMST)
  209. {
  210. if(PyFCGI_conf.shm.ptr &&
  211. munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
  212. {
  213. pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
  214. strerror(errno));
  215. }
  216. if(close(PyFCGI_conf.shm.fd) < 0)
  217. {
  218. pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
  219. strerror(errno));
  220. }
  221. if(shm_unlink(PyFCGI_conf.shm.name) < 0)
  222. {
  223. pyfcgi_log(LOG_WARNING, "Unabe to unlink SHM : %s",
  224. strerror(errno));
  225. }
  226. PyFCGI_conf.shm.fd = 0;
  227. PyFCGI_conf.shm.ptr = NULL;
  228. PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  229. }
  230. return res;
  231. }