123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #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_sems(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);
- }
- }
- }
-
- 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 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;
- dprintf(2, "SHM not yet implemented...\n");
- res = -1;
- //PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
- }
- 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;
- dprintf(2, "SHM not yet implemented...\n");
- res = -1;
- //PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
- }
- 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)
- {
- dprintf(2, "SHM not yet implemented...\n");
- res = -1;
- //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)
- {
- dprintf(2, "SHM not yet implemented...\n");
- res = -1;
- //PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
- }
- return res;
- }
|