- --- /dev/null 2022-04-19 22:28:32.062891741 +0200
- +++ pthread_mutex_init_log.c 2022-07-06 12:57:16.111555681 +0200
- @@ -0,0 +1,64 @@
- +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
- +
- +#include <pthread.h>
- +#include <unistd.h>
- +#include <errno.h>
- +#include <stdio.h>
- +#include <string.h>
- +#include <stdbool.h>
- +#include <dlfcn.h>
- +
- +static int once = 0;
- +
- +#define STDERR_MSG(msg) \
- + (void)write(2, (msg), strlen(msg))
- +
- +int pthread_mutex_init(pthread_mutex_t *restrict mutex,
- + const pthread_mutexattr_t *restrict attr) {
- + pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
- + int pmi_res;
- + int pmi_errno;
- +
- + const char *mymessage = "pthread_mutex_init_log.so: In our own pthread_mutex_init()\n";
- + /* just print message ONCE */
- + if (once++ == 0)
- + STDERR_MSG(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");
- +
- + pthread_mutexattr_t myattr;
- + bool myattr_used = false;
- +
- + if (!xattr) {
- + pthread_mutexattr_init(&myattr);
- + myattr_used = true;
- + xattr = &myattr;
- + }
- +
- + int mutex_protocol = -666;
- + (void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
- + if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
- + char msgbuff[1024];
- +
- + pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
- + snprintf(msgbuff,
- + sizeof(msgbuff),
- + "pthread_mutex_init_log.so: "
- + "PTHREAD_PRIO_INHERIT enforced for %lx, was %d\n",
- + (void *)mutex,
- + mutex_protocol);
- + STDERR_MSG(msgbuff);
- + }
- +
- + pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
- + pmi_errno = errno;
- +
- + if (myattr_used) {
- + pthread_mutexattr_destroy(&myattr);
- + }
- +
- + errno = pmi_errno;
- + return pmi_res;
- +}
- --- /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 12:55:59.122798683 +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 -lpthread -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 11:58
raw | new post
view followups (newest first): Intercepting pthread_mutext_init() via LD_PRELOAD by Anonymous
modification of post by Anonymous (view diff)
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.