Skip to content

Commit

Permalink
Merge pull request #96 from hao-yao/dfsg
Browse files Browse the repository at this point in the history
CameraShm: Fix semaphore permission issue
  • Loading branch information
hao-yao authored Mar 5, 2024
2 parents 8af98c0 + 0cc4d67 commit 6718c25
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/iutils/CameraShm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static const int CAMERA_IPCKEY = 0x43414D;
static const int CAMERA_SHM_LOCK_TIME = 2;

#define SEM_NAME "/camlock"
#define SEM_FD_NAME "/dev/shm/sem.camlock"

CameraSharedMemory::CameraSharedMemory()
: mSemLock(nullptr),
Expand Down Expand Up @@ -211,7 +212,7 @@ bool CameraSharedMemory::processExist(pid_t pid, const char* storedName) {
}

void CameraSharedMemory::openSemLock() {
mSemLock = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 1);
mSemLock = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0644, 1);
if (mSemLock == SEM_FAILED) {
mSemLock = sem_open(SEM_NAME, O_RDWR);
if (mSemLock == SEM_FAILED) {
Expand All @@ -221,6 +222,7 @@ void CameraSharedMemory::openSemLock() {
LOG1("Open the sem lock");
}
} else {
chmod(SEM_FD_NAME, 0666);
LOG1("Create the sem lock");
return;
}
Expand All @@ -244,7 +246,12 @@ void CameraSharedMemory::openSemLock() {
LOG1("Lock timed out, process holding it may have crashed. Re-create the semaphore.");
sem_close(mSemLock);
sem_unlink(SEM_NAME);
mSemLock = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 1);
mSemLock = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0644, 1);
if (mSemLock == SEM_FAILED) {
LOGE("failed to re-create sem lock, errno: %s\n", strerror(errno));
} else {
chmod(SEM_FD_NAME, 0666);
}
}
}

Expand All @@ -255,9 +262,10 @@ void CameraSharedMemory::closeSemLock() {
int CameraSharedMemory::lock() {
int ret = OK;
struct timespec ts;
CLEAR(ts);
CheckAndLogError(mSemLock == SEM_FAILED, BAD_VALUE, "invalid sem lock");

// Wait the semaphore lock for 2 seconds
CLEAR(ts);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += CAMERA_SHM_LOCK_TIME;
while (((ret = sem_timedwait(mSemLock, &ts)) == -1) && errno == EINTR) {
Expand Down

0 comments on commit 6718c25

Please sign in to comment.