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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_sems(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. }
  18. int pyfcgi_IPC_create(pyfcgi_ipc_flag_t flag)
  19. {
  20. short i;
  21. int res, err;
  22. res = 0;
  23. for(i=0; i<PYFCGI_NSEM; i++)
  24. {
  25. if(flag & semflg[i])
  26. {
  27. flag ^= semflg[i];
  28. PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name, O_CREAT | O_EXCL,
  29. 0770, 0);
  30. if(PyFCGI_SEM(i).sem == SEM_FAILED)
  31. {
  32. err = errno;
  33. pyfcgi_log(LOG_ALERT,
  34. "Unable to open semaphore %s(named '%s') : %s",
  35. semnames[i], PyFCGI_SEM(i).name,
  36. strerror(err));
  37. PyFCGI_SEM(i).sem = NULL;
  38. res = -1;
  39. }
  40. else
  41. {
  42. PyFCGI_conf.context.ipc_flag |= semflg[i];
  43. }
  44. }
  45. }
  46. if(flag & IPC_SHMST)
  47. {
  48. flag ^= IPC_SHMST;
  49. dprintf(2, "SHM not yet implemented...\n");
  50. res = -1;
  51. //PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
  52. }
  53. return res;
  54. }
  55. int pyfcgi_IPC_init(pyfcgi_ipc_flag_t flag)
  56. {
  57. short i;
  58. int res, err;
  59. res = 0;
  60. for(i=0; i<PYFCGI_NSEM; i++)
  61. {
  62. if(flag & semflg[i])
  63. {
  64. flag ^= semflg[i];
  65. PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name, 0);
  66. if(PyFCGI_SEM(i).sem == SEM_FAILED)
  67. {
  68. err = errno;
  69. pyfcgi_log(LOG_ALERT,
  70. "Unable to open semaphore %s(named '%s') : %s",
  71. semnames[i], PyFCGI_SEM(i).name,
  72. strerror(err));
  73. PyFCGI_SEM(i).sem = NULL;
  74. res = -1;
  75. }
  76. else
  77. {
  78. PyFCGI_conf.context.ipc_flag |= semflg[i];
  79. }
  80. }
  81. }
  82. if(flag & IPC_SHMST)
  83. {
  84. flag ^= IPC_SHMST;
  85. dprintf(2, "SHM not yet implemented...\n");
  86. res = -1;
  87. //PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
  88. }
  89. return res;
  90. }
  91. int pyfcgi_IPC_close()
  92. {
  93. short i;
  94. int res, err;
  95. pyfcgi_ipc_flag_t flag;
  96. flag = PyFCGI_conf.context.ipc_flag;
  97. res = 0;
  98. for(i=0; i<PYFCGI_NSEM; i++)
  99. {
  100. if(flag & semflg[i])
  101. {
  102. if(sem_close(PyFCGI_SEM(i).sem) < 0)
  103. {
  104. err = errno;
  105. pyfcgi_log(LOG_ALERT,
  106. "Unable to close semaphore %s('%s') : %s",
  107. semnames[i], PyFCGI_SEM(i).name,
  108. strerror(err));
  109. PyFCGI_SEM(i).sem = NULL;
  110. }
  111. PyFCGI_SEM(i).sem = NULL;
  112. }
  113. }
  114. if(flag & IPC_SHMST)
  115. {
  116. dprintf(2, "SHM not yet implemented...\n");
  117. res = -1;
  118. //PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  119. }
  120. return res;
  121. }
  122. int pyfcgi_IPC_destroy(pyfcgi_ipc_flag_t flag)
  123. {
  124. short i;
  125. int err, res;
  126. res = 0;
  127. for(i=0; i<PYFCGI_NSEM; i++)
  128. {
  129. if(flag & semflg[i])
  130. {
  131. if(sem_unlink(PyFCGI_SEM(i).name) < 0)
  132. {
  133. err = errno;
  134. pyfcgi_log(LOG_ALERT,
  135. "Unable to destroy %s semaphore (%s) : %s",
  136. semnames[i], PyFCGI_SEM(i).name,
  137. strerror(err));
  138. res = -1;
  139. }
  140. PyFCGI_SEM(i).sem = NULL;
  141. }
  142. }
  143. if(flag & IPC_SHMST)
  144. {
  145. dprintf(2, "SHM not yet implemented...\n");
  146. res = -1;
  147. //PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
  148. }
  149. return res;
  150. }