Skip to content

Commit 2f46ce8

Browse files
markovamariardementi
authored andcommitted
SDL330: Add ELOOP check for /dev/cpu/*/msr and define retry constant
1 parent 110ccba commit 2f46ce8

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/daemon/daemon.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ namespace PCMDaemon {
318318
// SDL330: Atomic file creation with symlink protection
319319
// Try O_EXCL first, unlink and retry only if needed (avoids TOCTOU race)
320320
int fd = -1;
321-
for (int attempt = 0; attempt < 3 && fd < 0; ++attempt) {
321+
constexpr int MAX_FILE_CREATION_RETRIES = 3;
322+
for (int attempt = 0; attempt < MAX_FILE_CREATION_RETRIES && fd < 0; ++attempt) {
322323
fd = open(shmIdLocation_.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW, 0660);
323324
if (fd >= 0) break;
324325

@@ -361,7 +362,7 @@ namespace PCMDaemon {
361362
shmData.shm_perm.gid = gid;
362363
shmData.shm_perm.mode = mode;
363364

364-
success = shmctl(sharedMemoryId_, IPC_SET, &shmData);
365+
int success = shmctl(sharedMemoryId_, IPC_SET, &shmData);
365366
if (success < 0)
366367
{
367368
std::cerr << "Failed to IPC_SET (errno=" << errno << ")\n";
@@ -773,7 +774,7 @@ namespace PCMDaemon {
773774
else
774775
{
775776
// Delete segment
776-
success = shmctl(sharedMemoryId_, IPC_RMID, NULL);
777+
int success = shmctl(sharedMemoryId_, IPC_RMID, NULL);
777778
if (success != 0)
778779
{
779780
std::cerr << "Failed to delete the shared memory segment (errno=" << errno << ")\n";

src/msr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ MsrHandle::MsrHandle(uint32 cpu) : fd(-1), cpu_id(cpu)
235235
if (!path) throw std::runtime_error("Allocation of 200 bytes failed.");
236236
snprintf(path, 200, "/dev/cpu/%u/msr", cpu);
237237
int handle = ::open(path, O_RDWR | O_NOFOLLOW);
238+
if (handle < 0 && errno == ELOOP) {
239+
std::cerr << "SDL330 ERROR: Symlink detected at MSR device path " << path << "\n";
240+
}
238241
if (handle < 0)
239242
{ // try Android msr device path
240243
snprintf(path, 200, "/dev/msr%u", cpu);

0 commit comments

Comments
 (0)