This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[tkunstmann@lycosmail.com] libc/1943: libpthread.so and libdl.so are problematic when a shared library is linked against both of them



We've received the appended bug report.  To me, the solutions looks
like adding -lpthread to the link line for test:
gcc -o test main.o -L. -lx -lpthread

Since main uses pthreads, it has to be linked with -lpthread.

Any other opinions?
Andreas

Subject: Topics

Topics:
   libc/1943: libpthread.so and libdl.so are problematic when a shared library is linked against both of them



>Number:         1943
>Category:       libc
>Synopsis:       libpthread.so and libdl.so are problematic when a shared library is linked against both of them
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    libc-gnats
>State:          open
>Quarter:        
>Keywords:       
>Class:          sw-bug
>Submitter-Id:   gnatsweb
>Arrival-Date:   Thu Nov 30 04:01:12 -0500 2000
>Cases:          
>Originator:     Tobias Kunstmann
>Release:        libc-2.1.3
>Organization:

>Environment:
Athlon 500MHz, 128MB, GNU/Linux (SuSE 7.0)

Host type: i686-pc-linux-gnu
System: Linux phoenix 2.2.16 #1 Tue Oct 3 17:59:54 CEST 2000 i686 unknown
Architecture: i686

Addons:	crypt linuxthreads noversion nss-v1

Build CC: gcc
Compiler version: 2.95.2 19991024 (release)
Kernel headers: 2.2.16
Symbol versioning: yes
Build static: yes
Build shared: yes
Build pic-default: no
Build profile: yes
Build omitfp: yes
Build bounded: no
Build static-nss: no
Stdio: libio
>Description:
There is some strange interaction between libdl.so and
libpthread.so when one uses a shared library which itself
is dynamically linked against both these libraries,
i.e., which was generated by a command like

   gcc -shared -o libfo.so [ lots of object files ] 
               -lpthread -ldl

Specifically, in the example below ("How-To-Repeat"), the
following happens:

 - fork() will work, execl() won't return, but the command 
   that execl() specifies (i.e., "/bin/ls") is not executed

 - the "manager thread" (here 9993) dies
 - the thread which is created by pthread_create() won't
   terminate

Output of "./test":
> main(): here I am (9992).
> foo!
> main(): go sleeping...
> child: my pid is 9995.
> thread_func(): here I am (9994); go sleeping....
> main(): ...woken up.

Output of "ps ax | grep test":
> 9992 pts/4    S      0:00 ./test
> 9993 pts/4    Z      0:00 [test <defunct>]
> 9995 pts/4    S      0:00 ./test

and again a little later (after main() has terminated):
> 9995 pts/4    S      0:00 ./test


This problem does not happen when the shared library is
created without specyfying either or both of "-lpthread"
and "-ldl".

Please note that this bug also happens with the libraries
and the dynamic linker that came with my Linux distribution,
which are not optimized (i.e., the "Build omitfp:" field
above would be "no").
>How-To-Repeat:
------------------------ snip -----------------------------------

# makefile

all: test

test: main.o libx.so
        gcc -o test main.o -L. -lx

libx.so: x.c
        gcc -shared -o libx.so x.c -ldl -lpthread

main.o: main.c
        gcc -o main.o -c main.c

clean:
        rm -f main.o libx.so test


------------------------ snip -----------------------------------

/*
 * x.c
 */

#define _REENTRANT

#include <stdio.h>

void foo()
{
  printf( "foo!\n" );
}

------------------------ snip -----------------------------------
/*
 * main.c
 */

#define _REENTRANT

#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

extern void foo();

void * thread_func( void *p )
{
  printf( "thread_func(): here I am (%d); go sleeping....\n",
getpid() );
  sleep( 5 );
  printf( "thread_func(): ...woken up.\n" );
  return NULL;
}

int main()
{
  pthread_t      thr;
  int            err;
  pid_t          pid;

  printf( "main(): here I am (%d).\n", getpid() );
  foo();

  err = pthread_create( &thr, NULL, thread_func, NULL );

  if ( 0 !=  err )
  {
    printf( "main(): pthread_create() returned with %d.\n", err );
    return 0;
  }

  pid = fork();

  if ( pid == -1 )
    printf( "main(): fork() failed; errno = %d.\n", errno );
  if ( pid == 0 )
  {
    printf( "child: my pid is %d.\n", getpid() );
    execl( "/bin/ls", NULL );
    printf( "child: execl() failed; errno = %d.\n", errno );
    _exit( 127 );
  }

  printf( "main(): go sleeping...\n" );
  sleep( 10 );
  printf( "main(): ...woken up.\a\n" );

  return 0;
}
------------------------ snip -----------------------------------

>Fix:
Unknown
>Unformatted:
 





-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.inka.de
    http://www.suse.de/~aj

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]