- --- /dev/null 2022-04-19 22:28:32.062891741 +0200
- +++ pthread_mutex_init_log.c 2022-07-06 11:16:54.162382585 +0200
- @@ -0,0 +1,22 @@
- +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
- +
- +#include <pthread.h>
- +#include <unistd.h>
- +#include <string.h>
- +#include <dlfcn.h>
- +
- +static int once = 0;
- +
- +int pthread_mutex_init(pthread_mutex_t *restrict mutex,
- + const pthread_mutexattr_t *restrict attr) {
- + const char *mymessage = "Interceptor: In our own pthread_mutex_init()\n";
- +
- + /* just print message ONCE */
- + if (once++ == 0)
- + (void)write(2, mymessage, strlen(mymessage));
- +
- + int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
- + const pthread_mutexattr_t *restrict attr);
- + original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
- + return (*original_pthread_mutex_init)(mutex, attr);
- +}
- --- /dev/null 2022-04-19 22:28:32.062891741 +0200
- +++ test1.c 2022-07-06 11:16:22.874096153 +0200
- @@ -0,0 +1,16 @@
- +#include <pthread.h>
- +#include <errno.h>
- +#include <stdio.h>
- +
- +int main(void) {
- + (void)puts("# start.");
- +
- + pthread_mutex_t mutex;
- +
- + pthread_mutex_init(&mutex, NULL);
- + perror("pthread_mutex_init() result");
- +
- + (void)puts("# end.");
- +
- + return 0;
- +}
- --- /dev/null 2022-04-19 22:28:32.062891741 +0200
- +++ Makefile 2022-07-06 11:17:24.038655927 +0200
- @@ -0,0 +1,14 @@
- +
- +pthread_mutex_init_log.o: pthread_mutex_init_log.c
- + gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o
- +pthread_mutex_init_log.so: pthread_mutex_init_log.o
- + gcc -std=gnu11 -g pthread_mutex_init_log.o -fPIC -shared -ldl -o pthread_mutex_init_log.so
- +
- +test1: test1.c
- + gcc -std=gnu11 -g test1.c -o test1
- +
- +run_test: test1 pthread_mutex_init_log.so
- + ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1'
- +
- +clean:
- + rm -f *.o *.so test1
Intercepting pthread_mutext_init() via LD_PRELOAD
Posted by Anonymous on Wed 6th Jul 2022 10:20
raw | new post
view followups (newest first): Intercepting pthread_mutext_init() via LD_PRELOAD by Anonymous
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.