Difference between
modified post ce2 by Anonymous on Wed 6th Jul 2022 12:53
original post a1ab9a3 by Anonymous on Wed 6th Jul 2022 12:17
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 13:16:29.462859109 +0200 | |
| 2 | + | +++ pthread_interceptor.c 2022-07-06 13:47:43.337141261 +0200 | |
| 3 | - | @@ -0,0 +1,82 @@ | |
| 3 | + | @@ -0,0 +1,86 @@ | |
| 4 | 4 | +/* | |
| 5 | - | + * phread_mutext_init_log.c | |
| 5 | + | + * pthread_interceptor.c | |
| 6 | 6 | + * | |
| 7 | - | + * intercept |pthread_mutex_init()| via LD_PRELOAD | |
| 7 | + | + * intercept pthread calls liek |pthread_mutex_init()| via LD_PRELOAD | |
| 8 | 8 | + * | |
| 9 | 9 | + * Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 10 | 10 | + */ | |
| 11 | 11 | + | |
| 12 | 12 | +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */ | |
| 13 | 13 | + | |
| 14 | 14 | +#include <pthread.h> | |
| 15 | 15 | +#include <unistd.h> | |
| 16 | 16 | +#include <errno.h> | |
| 17 | 17 | +#include <stdio.h> | |
| 18 | 18 | +#include <string.h> | |
| 19 | 19 | +#include <stdbool.h> | |
| 20 | 20 | +#include <dlfcn.h> | |
| 21 | 21 | + | |
| 22 | 22 | +/* config */ | |
| 23 | - | +#define PMICFG_PRINT_ONCE_MSG 1 | |
| 23 | + | +#define PICFG_PRINT_ONCE_MSG 1 | |
| 24 | - | +#define PMICFG_ENFORCE_PRIO_INHERIT 1 | |
| 24 | + | +#define PICFG_ENFORCE_PRIO_INHERIT 1 | |
| 25 | 25 | + | |
| 26 | + | +/* misc */ | |
| 27 | 27 | +#define STDERR_MSG(msg) \ | |
| 28 | 28 | + (void)write(STDERR_FILENO, (msg), strlen(msg)) | |
| 29 | 29 | + | |
| 30 | + | + | |
| 30 | 34 | +int pthread_mutex_init(pthread_mutex_t *restrict mutex, | |
| 31 | 35 | + const pthread_mutexattr_t *restrict attr) { | |
| 32 | 36 | + pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr; | |
| 33 | 37 | + int pmi_res; | |
| 34 | 38 | + int pmi_errno; | |
| 35 | 39 | + | |
| 36 | - | +#if PMICFG_PRINT_ONCE_MSG | |
| 40 | + | +#if PICFG_PRINT_ONCE_MSG | |
| 37 | 41 | + static int once = 0; | |
| 38 | - | + const char *mymessage = "pthread_mutex_init_log.so: " | |
| 42 | + | + const char *mymessage = "pthread_interceptor.so: " | |
| 39 | 43 | + "Once: In our own pthread_mutex_init()\n"; | |
| 40 | 44 | + /* just print message ONCE */ | |
| 41 | 45 | + if (once++ == 0) | |
| 42 | 46 | + STDERR_MSG(mymessage); | |
| 43 | - | +#endif /* PMICFG_PRINT_ONCE_MSG */ | |
| 47 | + | +#endif /* PICFG_PRINT_ONCE_MSG */ | |
| 44 | 48 | + | |
| 45 | 49 | + int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex, | |
| 46 | 50 | + const pthread_mutexattr_t *restrict attr); | |
| 47 | 51 | + original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init"); | |
| 48 | 52 | + | |
| 49 | 53 | + pthread_mutexattr_t myattr; | |
| 50 | 54 | + bool myattr_used = false; | |
| 51 | 55 | + | |
| 52 | 56 | + if (!xattr) { | |
| 53 | 57 | + (void)pthread_mutexattr_init(&myattr); | |
| 54 | 58 | + myattr_used = true; | |
| 55 | 59 | + xattr = &myattr; | |
| 56 | 60 | + } | |
| 57 | 61 | + | |
| 58 | - | +#if PMICFG_ENFORCE_PRIO_INHERIT | |
| 62 | + | +#if PICFG_ENFORCE_PRIO_INHERIT | |
| 59 | 63 | + int mutex_protocol = -666; | |
| 60 | 64 | + (void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol); | |
| 61 | 65 | + if (mutex_protocol != PTHREAD_PRIO_INHERIT) { | |
| 62 | 66 | + char msgbuff[256]; /* fixed size, but no |malloc()| allowed here */ | |
| 63 | 67 | + | |
| 64 | 68 | + (void)pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT); | |
| 65 | 69 | + (void)snprintf(msgbuff, | |
| 66 | 70 | + sizeof(msgbuff), | |
| 67 | - | + "pthread_mutex_init_log.so: " | |
| 71 | + | + "pthread_interceptor.so: " | |
| 68 | 72 | + "PTHREAD_PRIO_INHERIT enforced " | |
| 69 | - | + "for mutex=%lx, was protocol=%d\n", | |
| 73 | + | + "for mutex=0x%lx, was protocol=%d\n", | |
| 70 | - | + (void *)mutex, | |
| 74 | + | + (unsigned long)(void *)mutex, | |
| 71 | 75 | + mutex_protocol); | |
| 72 | 76 | + STDERR_MSG(msgbuff); | |
| 73 | 77 | + } | |
| 74 | - | +#endif /* PMICFG_ENFORCE_PRIO_INHERIT */ | |
| 78 | + | +#endif /* PICFG_ENFORCE_PRIO_INHERIT */ | |
| 75 | 79 | + | |
| 76 | 80 | + pmi_res = (*original_pthread_mutex_init)(mutex, xattr); | |
| 77 | 81 | + pmi_errno = errno; | |
| 78 | 82 | + | |
| 79 | 83 | + if (myattr_used) { | |
| 80 | 84 | + (void)pthread_mutexattr_destroy(&myattr); | |
| 81 | 85 | + } | |
| 82 | 86 | + | |
| 83 | 87 | + errno = pmi_errno; | |
| 84 | 88 | + return pmi_res; | |
| 85 | 89 | +} | |
| 86 | 90 | --- /dev/null 2022-04-19 22:28:32.062891741 +0200 | |
| 87 | - | +++ test1.c 2022-07-06 13:15:43.266402121 +0200 | |
| 91 | + | +++ test1.c 2022-07-06 13:52:16.151772792 +0200 | |
| 88 | - | @@ -0,0 +1,20 @@ | |
| 92 | + | @@ -0,0 +1,27 @@ | |
| 93 | + | +/* | |
| 92 | 97 | + | |
| 93 | - | +int main(void) { | |
| 98 | + | +#include <stdlib.h> | |
| 99 | + | +#include <stdio.h> | |
| 95 | 102 | + | |
| 103 | + | +int main(int ac, char *av[]) | |
| 96 | 107 | + pthread_mutex_t mutex1; | |
| 97 | 108 | + pthread_mutex_t mutex2; | |
| 98 | 109 | + | |
| 99 | - | + pthread_mutex_init(&mutex1, NULL); | |
| 110 | + | + (void)pthread_mutex_init(&mutex1, NULL); | |
| 100 | 111 | + perror("pthread_mutex_init(1) result"); | |
| 101 | 112 | + | |
| 102 | - | + pthread_mutex_init(&mutex2, NULL); | |
| 113 | + | + (void)pthread_mutex_init(&mutex2, NULL); | |
| 103 | 114 | + perror("pthread_mutex_init(2) result"); | |
| 104 | 115 | + | |
| 105 | 116 | + (void)puts("# end."); | |
| 106 | 117 | + | |
| 107 | - | + return 0; | |
| 118 | + | + return EXIT_SUCCESS; | |
| 108 | 119 | +} | |
| 109 | 120 | --- /dev/null 2022-04-19 22:28:32.062891741 +0200 | |
| 110 | - | +++ Makefile 2022-07-06 13:07:04.461318913 +0200 | |
| 121 | + | +++ Makefile 2022-07-06 13:50:52.426969179 +0200 | |
| 111 | - | @@ -0,0 +1,22 @@ | |
| 122 | + | @@ -0,0 +1,24 @@ | |
| 113 | 123 | +# | |
| 114 | - | +# Makefile for pthread_mutex_init.so | |
| 124 | + | +# Makefile for LD_PRELOAD pthreadinterceptor | |
| 116 | 125 | +# | |
| 117 | 126 | + | |
| 118 | - | +pthread_mutex_init_log.o: pthread_mutex_init_log.c | |
| 127 | + | +CSTDFLAGS=-std=c11 | |
| 119 | - | + gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o | |
| 128 | + | +CC=gcc # gcc or clang | |
| 122 | 129 | + | |
| 130 | + | +pthread_interceptor.o: pthread_interceptor.c | |
| 123 | 135 | +test1: test1.c | |
| 124 | - | + gcc -std=gnu11 -g test1.c -o test1 | |
| 136 | + | + $(CC) $(CSTDFLAGS) -Wall -g test1.c -o test1 | |
| 125 | 137 | + | |
| 126 | - | +run_test: test1 pthread_mutex_init_log.so | |
| 138 | + | +run_test: test1 pthread_interceptor.so | |
| 127 | - | + ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1' | |
| 139 | + | + ksh93 -c 'LD_PRELOAD=$$PWD/pthread_interceptor.so ./test1' | |
| 128 | 140 | + | |
| 129 | 141 | +all: run_test | |
| 130 | 142 | + | |
| 131 | 143 | +clean: | |
| 132 | 144 | + rm -f *.o *.so test1 | |
| 133 | 145 | + | |
| 146 | + | +# EOF. | |
| 134 | 147 |
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