Skip to content

Commit 54251d9

Browse files
committed
src/bindings: prevent duplicate entries in the pid hash table from appear
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
1 parent bf914cc commit 54251d9

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/bindings.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,29 @@ pid_t lookup_initpid_in_store(pid_t pid)
566566

567567
hashed_pid = lookup_verify_initpid(st.st_ino);
568568
if (hashed_pid < 0) {
569+
pid_t already_hashed_pid;
570+
569571
/* release the mutex as the following call is expensive */
570572
store_unlock();
571573

572574
hashed_pid = scm_init_pid(pid);
573575

574576
store_lock();
575577

576-
if (hashed_pid > 0)
577-
save_initpid(st.st_ino, hashed_pid);
578+
/* recheck that entry wasn't added while lock was released */
579+
already_hashed_pid = lookup_verify_initpid(st.st_ino);
580+
581+
/* no existing entry found. Just add a new one. */
582+
if (already_hashed_pid < 0) {
583+
if (hashed_pid > 0)
584+
save_initpid(st.st_ino, hashed_pid);
585+
586+
/* entry found it must have the same pid */
587+
} else if (already_hashed_pid != hashed_pid) {
588+
lxcfs_error("Different init pids (%d, %d) for the same cache entry %lu\n",
589+
already_hashed_pid, hashed_pid, HASH(st.st_ino));
590+
hashed_pid = -1;
591+
}
578592
}
579593

580594
/*

0 commit comments

Comments
 (0)