#include #include #include #include "config.h" #include "misc.h" static int nMutexFD; /* for file locking stuff*/ static int MutexFD; /* for file locking stuff*/ static int criticalMutexFD; /* for file locking stuff*/ static void open_critical_lock() { if((criticalMutexFD =(int) open(CRITICAL_LOCK,O_RDWR|O_CREAT,0600 )) < 0) { (void) fprintf(stderr,"child could not open lockFile %s error is %d\n",CRITICAL_LOCK, errno); exit(1); } return; } static int critical_lock_on(void) { int rc = -1; struct flock lock_it; lock_it.l_whence = SEEK_SET; /* from current point */ lock_it.l_start = 0; /* -"- */ lock_it.l_len = 0; /* until end of file */ lock_it.l_type = F_WRLCK; /* set exclusive/write lock */ lock_it.l_pid = 0; /* pid not actually interesting */ while (((rc = fcntl(criticalMutexFD, F_SETLKW, &lock_it)) < 0) && (errno == EINTR) ) ; (void) fcntl(criticalMutexFD,F_GETLK,&lock_it); if (lock_it.l_type != F_UNLCK) return 0; else return 1; } static int critical_lock_off(void) { int rc = -1; struct flock unlock_it; unlock_it.l_whence = SEEK_SET; /* from current point */ unlock_it.l_start = 0; /* -"- */ unlock_it.l_len = 0; /* until end of file */ unlock_it.l_type = F_UNLCK; /* unlock */ unlock_it.l_pid = 0; /* pid not actually interesting */ while (((rc = fcntl(criticalMutexFD, F_SETLKW, &unlock_it)) < 0) && (errno == EINTR) ) ; (void) fcntl(criticalMutexFD,F_GETLK,&unlock_it); if(unlock_it.l_type == F_UNLCK) return 1; else return 0; } void ssl_mutex_sem_on(int sem_id) { struct sembuf lock_it; lock_it.sem_num = 0; lock_it.sem_op = -1; lock_it.sem_flg = 0; semop(sem_id, &lock_it, 1); return; } void ssl_mutex_sem_off(int sem_id) { struct sembuf unlock_it; unlock_it.sem_num = 0; unlock_it.sem_op = 1; unlock_it.sem_flg = 0; semop(sem_id, &unlock_it, 1); return; } void ssl_mutex_file_open(char *sess_file) { char *file; file = mystrdup(sess_file); strcat(file,".db"); if((MutexFD =(int) open(file,O_WRONLY|O_CREAT,SSL_MUTEX_LOCK_MODE)) < 0) { (void) fprintf(stderr,"child could not open SSLMutex lockFile %s errno is %d \n",file); exit(1); } free(file); return; } void ssl_mutex_file_close(void) { close (MutexFD); return; } unsigned int ssl_mutex_file_acquire(void) { int rc = -1; struct flock lock_it; lock_it.l_whence = SEEK_SET; /* from current point */ lock_it.l_start = 0; /* -"- */ lock_it.l_len = 0; /* until end of file */ lock_it.l_type = F_WRLCK; /* set exclusive/write lock */ lock_it.l_pid = 0; /* pid not actually interesting */ while (((rc = fcntl(MutexFD, F_SETLKW, &lock_it)) < 0) && (errno == EINTR) ) ; (void) fcntl(MutexFD,F_GETLK,&lock_it); if (lock_it.l_type != F_UNLCK) return 0; else return 1; } unsigned int ssl_mutex_file_release(void) { int rc = -1; struct flock unlock_it; unlock_it.l_whence = SEEK_SET; /* from current point */ unlock_it.l_start = 0; /* -"- */ unlock_it.l_len = 0; /* until end of file */ unlock_it.l_type = F_UNLCK; /* unlock */ unlock_it.l_pid = 0; /* pid not actually interesting */ while (((rc = fcntl(MutexFD, F_SETLKW, &unlock_it)) < 0) && (errno == EINTR) ) ; (void) fcntl(MutexFD,F_GETLK,&unlock_it); if(unlock_it.l_type == F_UNLCK) return 1; else return 0; } void ssl_mutex_on(void) { unsigned int ok = 1; ok = ssl_mutex_file_acquire(); if(!ok) (void) fprintf(stderr, "Failed to acquire mutex lock\n"); return; } void ssl_mutex_off(void) { unsigned int ok = 1; ok = ssl_mutex_file_release(); if (!ok) (void) fprintf(stderr, "Failed to release mutex lock\n"); return; }