Skip to content

Commit

Permalink
csexec-preload: eliminate unncecessary uses of the init mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudka committed Oct 19, 2020
1 parent 8d8730d commit dc4cfd8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion csexec-preload.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
// the thread that triggered the initialization
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;

// optimization to eliminate unncecessary calls to pthread_mutex_lock()
static volatile bool init_done;

// address of the original canonicalize_file_name()
static char* (*orig_cfn)(const char *path);
static void init_orig_cfn(void)
Expand Down Expand Up @@ -143,16 +146,23 @@ static void init_real_exe(void)

// initialize global variables on first call
static void init_once(void) {
if (init_done)
// already initialized
return;

int rv = pthread_mutex_lock(&init_mutex);
if (rv != 0)
error(1, rv, "csexec-prealod: failed to lock init mutex");

if (!orig_cfn) {
if (!init_done) {
// first call -> initialize global state
init_orig_cfn();
init_orig_readlink();
init_ld_so_real();
init_real_exe();

// no need to call pthread_mutex_lock() from now on
init_done = true;
}

rv = pthread_mutex_unlock(&init_mutex);
Expand Down

0 comments on commit dc4cfd8

Please sign in to comment.