This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] Detect chdir races.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Mon, 21 Oct 2013 23:35:26 +0200
- Subject: [PATCH] Detect chdir races.
- Authentication-results: sourceware.org; auth=none
- References: <20131021193617 dot GA29829 at domone dot podge> <20131021212655 dot GA32168 at domone dot podge>
Another application is race detection. Here we can detect unsynchronized
chdirs by adding a mutex and aborting when second thread tries to lock
it.
A possible enchancement would be storing backtrace of first thread that
did locking to make race clear.
Comments?
* preload/chdir.c: New file.
---
preload/chdir.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
create mode 100644 preload/chdir.c
diff --git a/preload/chdir.c b/preload/chdir.c
new file mode 100644
index 0000000..19683db
--- /dev/null
+++ b/preload/chdir.c
@@ -0,0 +1,47 @@
+#include <unistd.h>
+#include <pthread.h>
+#include "common.h"
+static pthread_mutex_t mutex;
+static void __attribute__((constructor)) init(){
+ pthread_mutex_init (&mutex, NULL);
+}
+
+int chdir(const char *path)
+{
+ int (*fp)(const char *) = dlsym (__libc_handle, "chdir");
+ if (pthread_mutex_trylock (&mutex)) {
+ log ("race condition in chdir(%s)\n", path);
+ }
+ int ret = fp (path);
+ pthread_mutex_unlock (&mutex);
+ return ret;
+}
+int fchdir(int fd)
+{
+ int (*fp)(int) = dlsym (__libc_handle, "fchdir");
+ if (pthread_mutex_trylock (&mutex)) {
+ log ("race condition in fchdir(%i)\n", fd);
+ }
+ int ret = fp (fd);
+ pthread_mutex_unlock (&mutex);
+ return ret;
+}
+
+/*
+FILE *fopen(const char *path, const char *mode)
+{
+ FILE *(*fp)(const char *, const char *) = dlsym (__libc_handle, "fopen");
+ FILE *ret;
+
+ if (path[0]!='/')
+ return fp (path, mode);
+ else {
+ if (0 && !pthread_mutex_trylock (&mutex)) {
+ ret = fp (path, mode);
+ pthread_mutex_unlock (&mutex);
+ } else
+ ret = fp (path, mode);
+ return ret;
+ }
+}
+*/
--
1.8.4.rc3