]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdmon: satisfy glibc tls abi requirements with pthreads
authorDan Williams <dan.j.williams@intel.com>
Tue, 6 Jul 2010 19:48:56 +0000 (12:48 -0700)
committerDan Williams <dan.j.williams@intel.com>
Tue, 6 Jul 2010 19:48:56 +0000 (12:48 -0700)
Setting up a proper tls descriptor is required to conform to the abi
[1].  Until it can be implemented in mdmon use pthreads instead of
clone(2) to let glibc handle the details.  The old behaviour can be had
by un-defining USE_PTHREADS.

Note, the "O2" builds need LDFLAGS now to pick up the '-pthread' option.

[1]: http://people.redhat.com/drepper/tls.pdf

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Makefile
mdmon.c

index 237f4fc9cceb679164b2872de93ef7dc1e3fbf4c..0f42f8829a30a8cdb9a9c8e41f382924aea83d13 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -75,6 +75,14 @@ ALTFLAGS = -DALT_RUN=\"$(ALT_RUN)\" -DALT_MAPFILE=\"$(ALT_MAPFILE)\"
 VARFLAGS = -DVAR_RUN=\"$(VAR_RUN)\"
 CFLAGS = $(CWFLAGS) $(CXFLAGS) -DSendmail=\""$(MAILCMD)"\" $(CONFFILEFLAGS) $(ALTFLAGS) $(VARFLAGS)
 
+# The glibc TLS ABI requires applications that call clone(2) to set up
+# TLS data structures, use pthreads until mdmon implements this support
+USE_PTHREADS = 1
+ifdef USE_PTHREADS
+CFLAGS += -DUSE_PTHREADS
+LDFLAGS += -pthread
+endif
+
 # If you want a static binary, you might uncomment these
 # LDFLAGS = -static
 # STRIP = -s
@@ -149,13 +157,13 @@ mdadm.klibc : $(SRCS) mdadm.h
        $(CC) -nostdinc -iwithprefix include -I$(KLIBC)/klibc/include -I$(KLIBC)/linux/include -I$(KLIBC)/klibc/arch/i386/include -I$(KLIBC)/klibc/include/bits32 $(CFLAGS) $(SRCS)
 
 mdadm.Os : $(SRCS) mdadm.h
-       $(CC) -o mdadm.Os $(CFLAGS)  -DHAVE_STDINT_H -Os $(SRCS)
+       $(CC) -o mdadm.Os $(CFLAGS) $(LDFLAGS) -DHAVE_STDINT_H -Os $(SRCS)
 
 mdadm.O2 : $(SRCS) mdadm.h mdmon.O2
-       $(CC) -o mdadm.O2 $(CFLAGS)  -DHAVE_STDINT_H -O2 -D_FORTIFY_SOURCE=2 $(SRCS)
+       $(CC) -o mdadm.O2 $(CFLAGS) $(LDFLAGS) -DHAVE_STDINT_H -O2 -D_FORTIFY_SOURCE=2 $(SRCS)
 
 mdmon.O2 : $(MON_SRCS) mdadm.h mdmon.h
-       $(CC) -o mdmon.O2 $(CFLAGS)  -DHAVE_STDINT_H -O2 -D_FORTIFY_SOURCE=2 $(MON_SRCS)
+       $(CC) -o mdmon.O2 $(CFLAGS) $(LDFLAGS) -DHAVE_STDINT_H -O2 -D_FORTIFY_SOURCE=2 $(MON_SRCS)
 
 # use '-z now' to guarantee no dynamic linker interactions with the monitor thread
 mdmon : $(MON_OBJS)
diff --git a/mdmon.c b/mdmon.c
index 0c3742603ee5fdf6527ed585b1d08736baceea75..c4c0181a8b81bd800b640e515a323f2b80c60b1b 100644 (file)
--- a/mdmon.c
+++ b/mdmon.c
 #include       <fcntl.h>
 #include       <signal.h>
 #include       <dirent.h>
-
+#ifdef USE_PTHREADS
+#include       <pthread.h>
+#else
 #include       <sched.h>
+#endif
 
 #include       "mdadm.h"
 #include       "mdmon.h"
@@ -71,7 +74,39 @@ int mon_tid, mgr_tid;
 
 int sigterm;
 
-int run_child(void *v)
+#ifdef USE_PTHREADS
+static void *run_child(void *v)
+{
+       struct supertype *c = v;
+
+       mon_tid = syscall(SYS_gettid);
+       do_monitor(c);
+       return 0;
+}
+
+static int clone_monitor(struct supertype *container)
+{
+       pthread_attr_t attr;
+       pthread_t thread;
+       int rc;
+
+       mon_tid = -1;
+       pthread_attr_init(&attr);
+       pthread_attr_setstacksize(&attr, 4096);
+       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+       rc = pthread_create(&thread, &attr, run_child, container);
+       if (rc)
+               return rc;
+       while (mon_tid == -1)
+               usleep(10);
+       pthread_attr_destroy(&attr);
+
+       mgr_tid = syscall(SYS_gettid);
+
+       return mon_tid;
+}
+#else /* USE_PTHREADS */
+static int run_child(void *v)
 {
        struct supertype *c = v;
 
@@ -85,7 +120,7 @@ int __clone2(int (*fn)(void *),
            int flags, void *arg, ...
         /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );
 #endif
- int clone_monitor(struct supertype *container)
+static int clone_monitor(struct supertype *container)
 {
        static char stack[4096];
 
@@ -103,6 +138,7 @@ int __clone2(int (*fn)(void *),
 
        return mon_tid;
 }
+#endif /* USE_PTHREADS */
 
 static int make_pidfile(char *devname)
 {