This is the mail archive of the libc-help@sourceware.org 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]
Other format: [Raw text]

Re: fork call __attribute__((destructor))


2017-08-05 2:10 GMT+08:00 Carlos O'Donell <carlos@redhat.com>:
> On 08/04/2017 01:01 PM, Yubin Ruan wrote:
>> Hi,
>> I used to assumed that a function marked with a
>> "__attribute__((destructor))" would be called after the .so is
>> unloaded, typically when the program exit. However, I discover that
>> when I call "fork()" the destructor is also called.
>>
>> How could that happen? Is it a bug or something? Am I doing something
>> wrong? What is the rationale behind that?
>>
>> And, is there any way to prevent the destructor being called when
>> somebody call fork()?
>
> Please provide an example program that does this.

/* forklib.c, compile this to the .so file */
__attribute__((destructor)) {
    printf("Destructor is called\n");
}

/* main.c, use LD_PRELOAD=/path/to/the/xxx.so to tell the dynamic linker
 * to load the .so file. You will see that the "destructor" is called
 * after fork, in both parent and child
 */
#include <stdio.h>
#include <unistd.h>
int main()
{
    pid_t pid = fork();
    if(0 == pid) {
        printf("In child process\n");
    }else{
        printf("In parent process\n");
    }
    return 0;
}


> Also note that the running of the destructor is part of the dynamic
> loader's responsibility, not the static linker (binutils).
>
> I suggest asking on libc-help@sourceware.org.

Have add that to the Cc.

Thanks,
Yubin


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