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