* PROBLEM: getpid() returning same value as getppid()
@ 2010-05-26 14:45 Sébastien Paumier
2010-05-26 15:16 ` Nick Bowler
0 siblings, 1 reply; 3+ messages in thread
From: Sébastien Paumier @ 2010-05-26 14:45 UTC (permalink / raw)
To: linux-kernel
[-- Attachment #1: Type: text/plain, Size: 311 bytes --]
Hi,
here is a bug that occurs on my kernel 2.6.31-21, maybe with older ones.
If a C program contains a function with the constructor attribute that
calls getpid(), then, a call to syscall(SYS_fork) produces a son that
obtains the same value calling getpid() or getppid().
Best regards,
Sébastien Paumier
[-- Attachment #2: test.c --]
[-- Type: text/x-csrc, Size: 510 bytes --]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
void foo(void) __attribute__ ((constructor));
void foo(void) {
/* Removing this call to getpid() removes the bug */
getpid();
}
int main(void){
int i=syscall(SYS_fork);
if (0==i)
/* Big problem: the son prints the same value for getpid() and getppid() */
printf("__son : %d father : %d__\n", getpid(), getppid());
else
printf("<<father : %d son : %d>>\n", getpid(), i);
return 0;
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: PROBLEM: getpid() returning same value as getppid()
2010-05-26 14:45 PROBLEM: getpid() returning same value as getppid() Sébastien Paumier
@ 2010-05-26 15:16 ` Nick Bowler
2010-06-02 15:02 ` Michal Hocko
0 siblings, 1 reply; 3+ messages in thread
From: Nick Bowler @ 2010-05-26 15:16 UTC (permalink / raw)
To: Sébastien Paumier; +Cc: linux-kernel
On 16:45 Wed 26 May , Sébastien Paumier wrote:
> Hi,
> here is a bug that occurs on my kernel 2.6.31-21, maybe with older ones.
> If a C program contains a function with the constructor attribute that
> calls getpid(), then, a call to syscall(SYS_fork) produces a son that
> obtains the same value calling getpid() or getppid().
The GNU C library caches the results of getpid, which is probably the
cause of your grief. This cache relies on the glibc wrappers for fork
and friends, which you have bypassed by using syscall directly.
--
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: PROBLEM: getpid() returning same value as getppid()
2010-05-26 15:16 ` Nick Bowler
@ 2010-06-02 15:02 ` Michal Hocko
0 siblings, 0 replies; 3+ messages in thread
From: Michal Hocko @ 2010-06-02 15:02 UTC (permalink / raw)
To: S?bastien Paumier, linux-kernel
On Wed 26-05-10 11:16:51, Nick Bowler wrote:
> On 16:45 Wed 26 May , S?bastien Paumier wrote:
> > Hi,
> > here is a bug that occurs on my kernel 2.6.31-21, maybe with older ones.
> > If a C program contains a function with the constructor attribute that
> > calls getpid(), then, a call to syscall(SYS_fork) produces a son that
> > obtains the same value calling getpid() or getppid().
>
> The GNU C library caches the results of getpid, which is probably the
> cause of your grief. This cache relies on the glibc wrappers for fork
> and friends, which you have bypassed by using syscall directly.
man page for getpid states that explicitly:
NOTES
Since glibc version 2.3.4, the glibc wrapper function for
getpid() caches PIDs, so as to avoid additional system calls when
a process calls getpid() repeatedly. Normally this caching is
invisible, but its correct operation relies on support in the
wrapper functions for fork(2), vfork(2), and clone(2):
if an application bypasses the glibc wrappers for these system
calls by using syscall(2), then a call to getpid() in the child
will return the wrong value (to be precise: it will return the
PID of the parent process). See also clone(2) for discussion
of a case where getpid() may return the wrong value even when
invoking clone(2) via the glibc wrapper function.
>
> --
> Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Michal Hocko
L3 team
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9
Czech Republic
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2010-06-02 15:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-26 14:45 PROBLEM: getpid() returning same value as getppid() Sébastien Paumier
2010-05-26 15:16 ` Nick Bowler
2010-06-02 15:02 ` Michal Hocko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox