123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- #include "ipc.h"
-
- static const char *semnames[] = PYFCGI_SEMNAMES;
- static const pyfcgi_ipc_flag_t semflg[PYFCGI_NSEM] = {
- IPC_WSTATE, IPC_WREQS, IPC_SEMST};
-
- void pyfcgi_name_IPC(pid_t master_pid)
- {
- short i;
- for(i=0; i<PYFCGI_NSEM; i++)
- {
- if(snprintf(PyFCGI_conf.sems[i].name, NAME_MAX - 4,
- PYFCGI_IPCNAME_FMT, master_pid, semnames[i]) < 0)
- {
- perror("Error allocating semaphore names");
- exit(PYFCGI_FATAL);
- }
- }
- if(snprintf(PyFCGI_conf.shm.name, NAME_MAX, PYFCGI_IPCNAME_FMT,
- master_pid, "SHMstats") < 0)
- {
- perror("Error setting shm name");
- exit(PYFCGI_FATAL);
- }
- PyFCGI_conf.shm.len = sizeof(pyfcgi_stats_shm_t);
- }
-
- int pyfcgi_IPC_create(pyfcgi_ipc_flag_t flag)
- {
- short i;
- int res, err;
-
- res = 0;
- for(i=0; i<PYFCGI_NSEM; i++)
- {
- if(flag & semflg[i])
- {
- flag ^= semflg[i];
- PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name,
- O_CREAT | O_EXCL, 0770, 0);
- if(PyFCGI_SEM(i).sem == SEM_FAILED)
- {
- err = errno;
- pyfcgi_log(LOG_ALERT,
- "Unable to create semaphore %s(named '%s') : %s",
- semnames[i], PyFCGI_SEM(i).name,
- strerror(err));
- PyFCGI_SEM(i).sem = NULL;
- res = -1;
- }
- else
- {
- PyFCGI_conf.context.ipc_flag |= semflg[i];
- }
- }
- }
- if(flag & IPC_SHMST)
- {
- flag ^= IPC_SHMST;
- PyFCGI_conf.shm.fd = shm_open(PyFCGI_conf.shm.name,
- O_RDWR | O_CREAT | O_EXCL, 0770);
- if(PyFCGI_conf.shm.fd == -1)
- {
- err = errno;
- pyfcgi_log(LOG_ALERT,
- "Unable to create SHM '%s' : %s",
- PyFCGI_conf.shm.name, strerror(errno));
- PyFCGI_conf.shm.fd = 0;
- return -1;
- }
- PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
- if(ftruncate(PyFCGI_conf.shm.fd,
- sizeof(pyfcgi_stats_shm_t)) < 0)
- {
- pyfcgi_log(LOG_ALERT,
- "Unable to truncate SHM to wanted size %ld : %s",
- sizeof(pyfcgi_stats_shm_t),
- strerror(errno));
- pyfcgi_IPC_destroy(IPC_SHMST);
- return -1;
- }
- pyfcgi_log(LOG_INFO, "MMAP LENGHt : %ld", PyFCGI_conf.shm.len);
- PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
- PROT_READ | PROT_WRITE, MAP_SHARED, PyFCGI_conf.shm.fd,
- 0);
- if(PyFCGI_conf.shm.ptr == (void*)-1)
- {
- pyfcgi_log(LOG_ALERT,
- "Unable to mmap SHM : %s", strerror(errno));
- pyfcgi_IPC_destroy(IPC_SHMST);
- return -1;
- }
- }
- return res;
- }
-
- int pyfcgi_IPC_init(pyfcgi_ipc_flag_t flag)
- {
- short i;
- int res, err;
-
- res = 0;
- for(i=0; i<PYFCGI_NSEM; i++)
- {
- if(flag & semflg[i])
- {
- flag ^= semflg[i];
- PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name, 0);
- if(PyFCGI_SEM(i).sem == SEM_FAILED)
- {
- err = errno;
- pyfcgi_log(LOG_ALERT,
- "Unable to open semaphore %s(named '%s') : %s",
- semnames[i], PyFCGI_SEM(i).name,
- strerror(err));
- PyFCGI_SEM(i).sem = NULL;
- res = -1;
- }
- else
- {
- PyFCGI_conf.context.ipc_flag |= semflg[i];
- }
- }
- }
- if(flag & IPC_SHMST)
- {
- flag ^= IPC_SHMST;
- PyFCGI_conf.shm.fd = shm_open(PyFCGI_conf.shm.name, O_RDONLY, 0);
- if(PyFCGI_conf.shm.fd == -1)
- {
- err = errno;
- pyfcgi_log(LOG_ALERT,
- "Unable to open SHM '%s' : %s",
- PyFCGI_conf.shm.name, strerror(errno));
- PyFCGI_conf.shm.fd = 0;
- return -1;
- }
-
- PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
- PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
- PROT_READ, MAP_SHARED, PyFCGI_conf.shm.fd, 0);
- if(PyFCGI_conf.shm.ptr == (void*)-1)
- {
- pyfcgi_log(LOG_ALERT,
- "Unable to mmap SHM : %s", strerror(errno));
- pyfcgi_IPC_destroy(IPC_SHMST);
- return -1;
- }
- }
- return res;
- }
-
- int pyfcgi_IPC_close()
- {
- short i;
- int res, err;
- pyfcgi_ipc_flag_t flag;
-
- flag = PyFCGI_conf.context.ipc_flag;
- res = 0;
- for(i=0; i<PYFCGI_NSEM; i++)
- {
- if(flag & semflg[i])
- {
- if(sem_close(PyFCGI_SEM(i).sem) < 0)
- {
- err = errno;
- pyfcgi_log(LOG_ALERT,
- "Unable to close semaphore %s('%s') : %s",
- semnames[i], PyFCGI_SEM(i).name,
- strerror(err));
- PyFCGI_SEM(i).sem = NULL;
- }
- PyFCGI_SEM(i).sem = NULL;
- }
- }
- if(flag & IPC_SHMST)
- {
- if(PyFCGI_conf.shm.ptr &&
- munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
- {
- pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
- strerror(errno));
- }
- if(close(PyFCGI_conf.shm.fd) < 0)
- {
- pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
- strerror(errno));
- }
- PyFCGI_conf.shm.fd = 0;
- PyFCGI_conf.shm.ptr = NULL;
- PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
- }
-
- return res;
- }
-
- int pyfcgi_IPC_destroy(pyfcgi_ipc_flag_t flag)
- {
- short i;
- int err, res;
-
- res = 0;
- for(i=0; i<PYFCGI_NSEM; i++)
- {
- if(flag & semflg[i])
- {
- if(sem_unlink(PyFCGI_SEM(i).name) < 0)
- {
- err = errno;
- pyfcgi_log(LOG_ALERT,
- "Unable to destroy %s semaphore (%s) : %s",
- semnames[i], PyFCGI_SEM(i).name,
- strerror(err));
- res = -1;
- }
- PyFCGI_SEM(i).sem = NULL;
- }
- }
- if(flag & IPC_SHMST)
- {
-
- if(PyFCGI_conf.shm.ptr &&
- munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
- {
- pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
- strerror(errno));
- }
- if(close(PyFCGI_conf.shm.fd) < 0)
- {
- pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
- strerror(errno));
- }
- if(shm_unlink(PyFCGI_conf.shm.name) < 0)
- {
- pyfcgi_log(LOG_WARNING, "Unabe to unlink SHM : %s",
- strerror(errno));
- }
- PyFCGI_conf.shm.fd = 0;
- PyFCGI_conf.shm.ptr = NULL;
- PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
- }
- return res;
- }
|