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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_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. memset(PyFCGI_conf.shm.ptr, 0, PyFCGI_conf.shm.len);
  90. }
  91. return res;
  92. }
  93. int pyfcgi_IPC_init(pyfcgi_ipc_flag_t flag)
  94. {
  95. short i;
  96. int res, err;
  97. res = 0;
  98. for(i=0; i<PYFCGI_NSEM; i++)
  99. {
  100. if(flag & semflg[i])
  101. {
  102. flag ^= semflg[i];
  103. PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name, 0);
  104. if(PyFCGI_SEM(i).sem == SEM_FAILED)
  105. {
  106. err = errno;
  107. pyfcgi_log(LOG_ALERT,
  108. "Unable to open semaphore %s(named '%s') : %s",
  109. semnames[i], PyFCGI_SEM(i).name,
  110. strerror(err));
  111. PyFCGI_SEM(i).sem = NULL;
  112. res = -1;
  113. }
  114. else
  115. {
  116. PyFCGI_conf.context.ipc_flag |= semflg[i];
  117. }
  118. }
  119. }
  120. if(flag & IPC_SHMST)
  121. {
  122. flag ^= IPC_SHMST;
  123. PyFCGI_conf.shm.fd = shm_open(PyFCGI_conf.shm.name, O_RDONLY,
  124. S_IRUSR | S_IWUSR);
  125. if(PyFCGI_conf.shm.fd == -1)
  126. {
  127. err = errno;
  128. pyfcgi_log(LOG_ALERT,
  129. "Unable to open SHM '%s' : %s",
  130. PyFCGI_conf.shm.name, strerror(errno));
  131. PyFCGI_conf.shm.fd = 0;
  132. return -1;
  133. }
  134. PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
  135. PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
  136. PROT_READ, MAP_SHARED, PyFCGI_conf.shm.fd, 0);
  137. if(PyFCGI_conf.shm.ptr == (void*)-1)
  138. {
  139. pyfcgi_log(LOG_ALERT,
  140. "Unable to mmap SHM : %s", strerror(errno));
  141. pyfcgi_IPC_destroy(IPC_SHMST);
  142. return -1;
  143. }
  144. }
  145. return res;
  146. }
  147. int pyfcgi_IPC_close()
  148. {
  149. short i;
  150. int res, err;
  151. pyfcgi_ipc_flag_t flag;
  152. flag = PyFCGI_conf.context.ipc_flag;
  153. res = 0;
  154. for(i=0; i<PYFCGI_NSEM; i++)
  155. {
  156. if(flag & semflg[i])
  157. {
  158. if(sem_close(PyFCGI_SEM(i).sem) < 0)
  159. {
  160. err = errno;
  161. pyfcgi_log(LOG_ALERT,
  162. "Unable to close semaphore %s('%s') : %s",
  163. semnames[i], PyFCGI_SEM(i).name,
  164. strerror(err));
  165. PyFCGI_SEM(i).sem = NULL;
  166. }
  167. PyFCGI_SEM(i).sem = NULL;
  168. }
  169. }
  170. if(flag & IPC_SHMST)
  171. {
  172. if(PyFCGI_conf.shm.ptr &&
  173. munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
  174. {
  175. pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
  176. strerror(errno));
  177. }
  178. if(close(PyFCGI_conf.shm.fd) < 0)
  179. {
  180. pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
  181. strerror(errno));
  182. }
  183. PyFCGI_conf.shm.fd = 0;
  184. PyFCGI_conf.shm.ptr = NULL;
  185. PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  186. }
  187. return res;
  188. }
  189. int pyfcgi_IPC_destroy(pyfcgi_ipc_flag_t flag)
  190. {
  191. short i;
  192. int err, res;
  193. res = 0;
  194. for(i=0; i<PYFCGI_NSEM; i++)
  195. {
  196. if(flag & semflg[i])
  197. {
  198. if(sem_unlink(PyFCGI_SEM(i).name) < 0)
  199. {
  200. err = errno;
  201. pyfcgi_log(LOG_ALERT,
  202. "Unable to destroy %s semaphore (%s) : %s",
  203. semnames[i], PyFCGI_SEM(i).name,
  204. strerror(err));
  205. res = -1;
  206. }
  207. PyFCGI_SEM(i).sem = NULL;
  208. }
  209. }
  210. if(flag & IPC_SHMST)
  211. {
  212. if(PyFCGI_conf.shm.ptr &&
  213. munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
  214. {
  215. pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
  216. strerror(errno));
  217. }
  218. if(close(PyFCGI_conf.shm.fd) < 0)
  219. {
  220. pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
  221. strerror(errno));
  222. }
  223. if(shm_unlink(PyFCGI_conf.shm.name) < 0)
  224. {
  225. pyfcgi_log(LOG_WARNING, "Unabe to unlink SHM : %s",
  226. strerror(errno));
  227. }
  228. PyFCGI_conf.shm.fd = 0;
  229. PyFCGI_conf.shm.ptr = NULL;
  230. PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  231. }
  232. return res;
  233. }