Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CameraShm: Fix semaphore permission issue #96

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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