PROBLEM: getpid() returning same value as getppid()

From: SÃbastien Paumier
Date: Wed May 26 2010 - 11:04:52 EST


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
#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;
}