Difference between
modified post 0ae238f15 by Anonymous on Wed 6th Jul 2022 11:58
original post e21dfbc03a by Anonymous on Wed 6th Jul 2022 10:20
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | --- /dev/null 2022-04-19 22:28:32.062891741 +0200 | |
| 2 | - | +++ pthread_mutex_init_log.c 2022-07-06 11:16:54.162382585 +0200 | |
| 2 | + | +++ pthread_mutex_init_log.c 2022-07-06 12:57:16.111555681 +0200 | |
| 3 | - | @@ -0,0 +1,22 @@ | |
| 3 | + | @@ -0,0 +1,64 @@ | |
| 4 | 4 | +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */ | |
| 5 | 5 | + | |
| 6 | 6 | +#include <pthread.h> | |
| 7 | 7 | +#include <unistd.h> | |
| 8 | + | +#include <errno.h> | |
| 8 | 10 | +#include <string.h> | |
| 11 | + | +#include <stdbool.h> | |
| 9 | 12 | +#include <dlfcn.h> | |
| 10 | 13 | + | |
| 11 | 14 | +static int once = 0; | |
| 12 | 15 | + | |
| 16 | + | +#define STDERR_MSG(msg) \ | |
| 13 | 19 | +int pthread_mutex_init(pthread_mutex_t *restrict mutex, | |
| 14 | 20 | + const pthread_mutexattr_t *restrict attr) { | |
| 15 | - | + const char *mymessage = "Interceptor: In our own pthread_mutex_init()\n"; | |
| 21 | + | + pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr; | |
| 22 | + | + int pmi_res; | |
| 16 | 24 | + | |
| 25 | + | + const char *mymessage = "pthread_mutex_init_log.so: In our own pthread_mutex_init()\n"; | |
| 17 | 26 | + /* just print message ONCE */ | |
| 18 | 27 | + if (once++ == 0) | |
| 19 | - | + (void)write(2, mymessage, strlen(mymessage)); | |
| 28 | + | + STDERR_MSG(mymessage); | |
| 20 | 29 | + | |
| 21 | 30 | + int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex, | |
| 22 | 31 | + const pthread_mutexattr_t *restrict attr); | |
| 23 | 32 | + original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init"); | |
| 29 | - | +#include <pthread.h> | |
| 30 | - | +#include <errno.h> | |
| 31 | - | +#include <stdio.h> | |
| 32 | 33 | + | |
| 33 | - | +int main(void) { | |
| 34 | + | + pthread_mutexattr_t myattr; | |
| 34 | - | + (void)puts("# start."); | |
| 35 | + | + bool myattr_used = false; | |
| 35 | 36 | + | |
| 36 | - | + pthread_mutex_t mutex; | |
| 37 | + | + if (!xattr) { | |
| 38 | + | + pthread_mutexattr_init(&myattr); | |
| 37 | 42 | + | |
| 38 | - | + pthread_mutex_init(&mutex, NULL); | |
| 43 | + | + int mutex_protocol = -666; | |
| 39 | - | + perror("pthread_mutex_init() result"); | |
| 44 | + | + (void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol); | |
| 45 | + | + if (mutex_protocol != PTHREAD_PRIO_INHERIT) { | |
| 40 | 47 | + | |
| 41 | - | + (void)puts("# end."); | |
| 48 | + | + pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT); | |
| 49 | + | + snprintf(msgbuff, | |
| 42 | 57 | + | |
| 43 | - | + return 0; | |
| 58 | + | + pmi_res = (*original_pthread_mutex_init)(mutex, xattr); | |
| 59 | + | + pmi_errno = errno; | |
| 48 | 60 | + | |
| 49 | - | +pthread_mutex_init_log.o: pthread_mutex_init_log.c | |
| 61 | + | + if (myattr_used) { | |
| 50 | - | + gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o | |
| 62 | + | + pthread_mutexattr_destroy(&myattr); | |
| 51 | - | +pthread_mutex_init_log.so: pthread_mutex_init_log.o | |
| 63 | + | + } | |
| 53 | 64 | + | |
| 54 | - | +test1: test1.c | |
| 65 | + | + errno = pmi_errno; | |
| 55 | - | + gcc -std=gnu11 -g test1.c -o test1 | |
| 66 | + | + return pmi_res; | |
| 67 | + | +} | |
| 56 | 74 | + | |
| 57 | - | +run_test: test1 pthread_mutex_init_log.so | |
| 75 | + | +int main(void) { | |
| 58 | - | + ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1' | |
| 76 | + | + (void)puts("# start."); | |
| 59 | 77 | + | |
| 60 | - | +clean: | |
| 78 | + | + pthread_mutex_t mutex; | |
| 61 | - | + rm -f *.o *.so test1 | |
| 79 | + | + | |
| 62 | - | ||
| 80 | + | + pthread_mutex_init(&mutex, NULL); | |
| 63 | - | ||
| 81 | + | + perror("pthread_mutex_init() result"); | |
| 64 | - | ||
| 82 | + | + | |
| 65 | - | ||
| 83 | + | + (void)puts("# end."); | |
| 66 | - | ||
| 84 | + | + | |
| 67 | - | ||
| 85 | + | + return 0; | |
| 68 | - | ||
| 86 | + | +} | |
| 69 | - | ||
| 87 | + | --- /dev/null 2022-04-19 22:28:32.062891741 +0200 | |
| 70 | - | ||
| 88 | + | +++ Makefile 2022-07-06 12:55:59.122798683 +0200 | |
| 71 | - | ||
| 89 | + | @@ -0,0 +1,14 @@ | |
| 72 | - | ||
| 90 | + | + | |
| 73 | - | ||
| 91 | + | +pthread_mutex_init_log.o: pthread_mutex_init_log.c | |
| 74 | - | ||
| 92 | + | + gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o | |
| 75 | - | ||
| 93 | + | +pthread_mutex_init_log.so: pthread_mutex_init_log.o | |
| 76 | - | ||
| 94 | + | + gcc -std=gnu11 -g pthread_mutex_init_log.o -fPIC -shared -ldl -lpthread -o pthread_mutex_init_log.so | |
| 77 | - | ||
| 95 | + | + | |
| 78 | - | ||
| 96 | + | +test1: test1.c | |
| 79 | - | ||
| 97 | + | + gcc -std=gnu11 -g test1.c -o test1 | |
| 80 | - | ||
| 98 | + | + | |
| 81 | - | ||
| 99 | + | +run_test: test1 pthread_mutex_init_log.so | |
| 82 | - | ||
| 100 | + | + ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1' | |
| 83 | - | ||
| 101 | + | + | |
| 84 | - | ||
| 102 | + | +clean: | |
| 85 | - | ||
| 103 | + | + rm -f *.o *.so test1 | |
| 86 | 104 |
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.
rovema.kpaste.net RSS