pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post a1ab9a3 by Anonymous on Wed 6th Jul 2022 12:17
original post 0ae238f15 by Anonymous on Wed 6th Jul 2022 11:58
Download diff
Show old version | new version | both versions

    
11
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
2-
+++ pthread_mutex_init_log.c	2022-07-06 12:57:16.111555681 +0200
2+
+++ pthread_mutex_init_log.c	2022-07-06 13:16:29.462859109 +0200
3-
@@ -0,0 +1,64 @@
3+
@@ -0,0 +1,82 @@
4+
+/*
511
+
12+
+#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
614
+#include <pthread.h>
715
+#include <unistd.h>
816
+#include <errno.h>
917
+#include <stdio.h>
1018
+#include <string.h>
1119
+#include <stdbool.h>
1220
+#include <dlfcn.h>
1321
+
14-
+static int once = 0;
22+
+/* config */
23+
+#define PMICFG_PRINT_ONCE_MSG 1
1525
+
26+
+
1627
+#define STDERR_MSG(msg) \
17-
+	(void)write(2, (msg), strlen(msg))
28+
+	(void)write(STDERR_FILENO, (msg), strlen(msg))
1829
+
1930
+int pthread_mutex_init(pthread_mutex_t *restrict mutex,
2031
+		const pthread_mutexattr_t *restrict attr) {
2132
+	pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
2233
+	int pmi_res;
2334
+	int pmi_errno;
2435
+
25-
+	const char *mymessage = "pthread_mutex_init_log.so: In our own pthread_mutex_init()\n";
36+
+#if PMICFG_PRINT_ONCE_MSG
37+
+	static int once = 0;
2640
+	/* just print message ONCE */
2741
+	if (once++ == 0)
2842
+		STDERR_MSG(mymessage);
43+
+#endif /* PMICFG_PRINT_ONCE_MSG */
2944
+
3045
+	int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
3146
+		const pthread_mutexattr_t *restrict attr);
3247
+	original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
3348
+
3449
+	pthread_mutexattr_t myattr;
3550
+	bool myattr_used = false;
3651
+
3752
+	if (!xattr) {
38-
+		pthread_mutexattr_init(&myattr);
53+
+		(void)pthread_mutexattr_init(&myattr);
3954
+		myattr_used = true;
4055
+		xattr = &myattr;
4156
+	}
4257
+
58+
+#if PMICFG_ENFORCE_PRIO_INHERIT
4359
+	int mutex_protocol = -666;
4460
+	(void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
4561
+	if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
46-
+		char msgbuff[1024];
62+
+		char msgbuff[256]; /* fixed size, but no |malloc()| allowed here */
4763
+
48-
+		pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
64+
+		(void)pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
49-
+		snprintf(msgbuff,
65+
+		(void)snprintf(msgbuff,
5066
+			sizeof(msgbuff),
5167
+			"pthread_mutex_init_log.so: "
52-
+			"PTHREAD_PRIO_INHERIT enforced for %lx, was %d\n",
68+
+			"PTHREAD_PRIO_INHERIT enforced "
69+
+			"for mutex=%lx, was protocol=%d\n",
5370
+			(void *)mutex,
5471
+			mutex_protocol);
5572
+		STDERR_MSG(msgbuff);
5673
+	}
74+
+#endif /* PMICFG_ENFORCE_PRIO_INHERIT */
5775
+
5876
+	pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
5977
+	pmi_errno = errno;
6078
+
6179
+	if (myattr_used) {
62-
+		pthread_mutexattr_destroy(&myattr);
80+
+		(void)pthread_mutexattr_destroy(&myattr);
6381
+	}
6482
+
6583
+	errno = pmi_errno;
6684
+	return pmi_res;
6785
+}
6886
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
69-
+++ test1.c	2022-07-06 11:16:22.874096153 +0200
87+
+++ test1.c	2022-07-06 13:15:43.266402121 +0200
70-
@@ -0,0 +1,16 @@
88+
@@ -0,0 +1,20 @@
7189
+#include <pthread.h>
7290
+#include <errno.h>
7391
+#include <stdio.h>
7492
+
7593
+int main(void) {
7694
+	(void)puts("# start.");
7795
+
78-
+	pthread_mutex_t mutex;
96+
+	pthread_mutex_t mutex1;
97+
+	pthread_mutex_t mutex2;
7998
+
80-
+	pthread_mutex_init(&mutex, NULL);
99+
+	pthread_mutex_init(&mutex1, NULL);
81-
+	perror("pthread_mutex_init() result");
100+
+	perror("pthread_mutex_init(1) result");
82101
+
102+
+	pthread_mutex_init(&mutex2, NULL);
84104
+
105+
+	(void)puts("# end.");
85107
+	return 0;
86108
+}
87109
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
88-
+++ Makefile	2022-07-06 12:55:59.122798683 +0200
110+
+++ Makefile	2022-07-06 13:07:04.461318913 +0200
89-
@@ -0,0 +1,14 @@
111+
@@ -0,0 +1,22 @@
90112
+
113+
+#
91118
+pthread_mutex_init_log.o: pthread_mutex_init_log.c
92119
+	gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o
93120
+pthread_mutex_init_log.so: pthread_mutex_init_log.o
94121
+	gcc -std=gnu11 -g pthread_mutex_init_log.o -fPIC -shared -ldl -lpthread -o pthread_mutex_init_log.so
95122
+
96123
+test1: test1.c
97124
+	gcc -std=gnu11 -g test1.c -o test1
98125
+
99126
+run_test: test1 pthread_mutex_init_log.so
100127
+	ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1'
101128
+
129+
+all:	run_test
102131
+clean:
103132
+	rm -f *.o *.so test1
133+
+
104134

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at