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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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, S_IRUSR | S_IWUSR);
  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_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
  79. PROT_READ | PROT_WRITE, MAP_SHARED, PyFCGI_conf.shm.fd,
  80. 0);
  81. if(PyFCGI_conf.shm.ptr == (void*)-1)
  82. {
  83. pyfcgi_log(LOG_ALERT,
  84. "Unable to mmap SHM : %s", strerror(errno));
  85. pyfcgi_IPC_destroy(IPC_SHMST);
  86. return -1;
  87. }
  88. memset(PyFCGI_conf.shm.ptr, 0, PyFCGI_conf.shm.len);
  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,
  123. S_IRUSR | S_IWUSR);
  124. if(PyFCGI_conf.shm.fd == -1)
  125. {
  126. err = errno;
  127. pyfcgi_log(LOG_ALERT,
  128. "Unable to open SHM '%s' : %s",
  129. PyFCGI_conf.shm.name, strerror(errno));
  130. PyFCGI_conf.shm.fd = 0;
  131. return -1;
  132. }
  133. PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
  134. PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
  135. PROT_READ, MAP_SHARED, PyFCGI_conf.shm.fd, 0);
  136. if(PyFCGI_conf.shm.ptr == (void*)-1)
  137. {
  138. pyfcgi_log(LOG_ALERT,
  139. "Unable to mmap SHM : %s", strerror(errno));
  140. pyfcgi_IPC_destroy(IPC_SHMST);
  141. return -1;
  142. }
  143. }
  144. return res;
  145. }
  146. int pyfcgi_IPC_close()
  147. {
  148. short i;
  149. int res, err;
  150. pyfcgi_ipc_flag_t flag;
  151. flag = PyFCGI_conf.context.ipc_flag;
  152. res = 0;
  153. for(i=0; i<PYFCGI_NSEM; i++)
  154. {
  155. if(flag & semflg[i])
  156. {
  157. if(sem_close(PyFCGI_SEM(i).sem) < 0)
  158. {
  159. err = errno;
  160. pyfcgi_log(LOG_ALERT,
  161. "Unable to close semaphore %s('%s') : %s",
  162. semnames[i], PyFCGI_SEM(i).name,
  163. strerror(err));
  164. PyFCGI_SEM(i).sem = NULL;
  165. }
  166. PyFCGI_SEM(i).sem = NULL;
  167. }
  168. }
  169. if(flag & IPC_SHMST)
  170. {
  171. if(PyFCGI_conf.shm.ptr &&
  172. munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
  173. {
  174. pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
  175. strerror(errno));
  176. }
  177. if(close(PyFCGI_conf.shm.fd) < 0)
  178. {
  179. pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
  180. strerror(errno));
  181. }
  182. PyFCGI_conf.shm.fd = 0;
  183. PyFCGI_conf.shm.ptr = NULL;
  184. PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  185. }
  186. return res;
  187. }
  188. int pyfcgi_IPC_destroy(pyfcgi_ipc_flag_t flag)
  189. {
  190. short i;
  191. int err, res;
  192. res = 0;
  193. for(i=0; i<PYFCGI_NSEM; i++)
  194. {
  195. if(flag & semflg[i])
  196. {
  197. if(sem_unlink(PyFCGI_SEM(i).name) < 0)
  198. {
  199. err = errno;
  200. pyfcgi_log(LOG_ALERT,
  201. "Unable to unlink %s semaphore (%s) : %s",
  202. semnames[i], PyFCGI_SEM(i).name,
  203. strerror(err));
  204. res = -1;
  205. }
  206. PyFCGI_SEM(i).sem = NULL;
  207. }
  208. }
  209. if(flag & IPC_SHMST)
  210. {
  211. if(PyFCGI_conf.shm.ptr &&
  212. munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
  213. {
  214. pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
  215. strerror(errno));
  216. }
  217. if(close(PyFCGI_conf.shm.fd) < 0)
  218. {
  219. pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
  220. strerror(errno));
  221. }
  222. if(shm_unlink(PyFCGI_conf.shm.name) < 0)
  223. {
  224. pyfcgi_log(LOG_WARNING, "Unabe to unlink SHM : %s",
  225. strerror(errno));
  226. }
  227. PyFCGI_conf.shm.fd = 0;
  228. PyFCGI_conf.shm.ptr = NULL;
  229. PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  230. }
  231. return res;
  232. }