This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: fork call __attribute__((destructor))
- From: Yubin Ruan <ablacktshirt at gmail dot com>
- To: "Carlos O'Donell" <carlos at redhat dot com>
- Cc: binutils at sourceware dot org, libc-help at sourceware dot org
- Date: Sat, 5 Aug 2017 15:02:29 +0800
- Subject: Re: fork call __attribute__((destructor))
- Authentication-results: sourceware.org; auth=none
- References: <CAJYFCiOQBKJPMRhmoFerxtWB94+3urPCNizSzfp4n01oC5wAhw@mail.gmail.com> <c115149e-051e-f447-3c35-28529a6beda6@redhat.com>
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