A different clone() issue

Tim Hockin (thockin@isunix.it.ilstu.edu)
Sat, 17 Jul 1999 21:00:14 -0500 (CDT)


It's not strictly a kernel issue, but can anyone reproduce this?

Run this program, look for the return status printed.

The clone process is getting a segv when it returns. If you change
return(10); t exit(10) or _exit(10); it works as expected..

gdb'ing the cloned process shows a segfault in thread_start, which is a
function in clone.S of libc, pasted below, too. My knowledge of intel
assembly is small. Why could this be segv'ing? Or is it pilot error? Can
anyone else get this error?

-----------------------------------------------------------------

#include <stdio.h>
#include <sched.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <waitflags.h>
#include <sys/types.h>
#include <sys/wait.h>

void sig(int s)
{
printf("caught signal %d\n", s);
fflush(stdout);
_exit(-1);
}

int fn(void *arg)
{
char *s = (char *)arg;
signal(11, sig);
printf("%s: Pid: %d, PPid: %d\n", s, getpid(), getppid());
fflush(stdout);
sleep(3);
return(10);
}

int main()
{
char *sp;
char *tos;
unsigned long flags = 0;
int r;
int s;
int status;

sp = (char *)malloc(8192*8);
if (sp)
tos = sp + 8192*8;
else
exit(errno);

//signal(SIGUSR1, SIG_IGN);

//flags = SIGUSR1;
//flags |= CLONE_FS|CLONE_FILES|CLONE_SIGHAND;
r = __clone(fn, tos, flags, "Cloned process");
if (r < 0) {
perror("__clone");
exit(-1);
}

s = waitpid(r, &status, __WCLONE);
perror("waitpid");
if (WIFEXITED(status))
printf("pid: %d, status: %d\n", r, WEXITSTATUS(status));
if (WIFSIGNALED(status))
printf("pid: %d, signal: %d\n", r, WTERMSIG(status));

return(0);
}

---------------------------------------------------------------
ENTRY(__clone)
/* Sanity check arguments. */
movl $-EINVAL,%eax
movl 4(%esp),%ecx /* no NULL function pointers */
testl %ecx,%ecx
jz syscall_error
movl 8(%esp),%ecx /* no NULL stack pointers */
testl %ecx,%ecx
jz syscall_error

/* Insert the argument onto the new stack. */
subl $8,%ecx
movl 16(%esp),%eax /* no negative argument counts */
movl %eax,4(%ecx)

/* Save the function pointer as the zeroth argument.
It will be popped off in the child in the ebx frobbing below. */
movl 4(%esp),%eax
movl %eax,0(%ecx)

/* Do the system call */
pushl %ebx
movl 16(%esp),%ebx
movl $SYS_ify(clone),%eax
int $0x80
popl %ebx

test %eax,%eax
jl syscall_error
jz thread_start

ret

thread_start:
subl %ebp,%ebp /* terminate the stack frame */
call *%ebx
pushl %eax
call JUMPTARGET (_exit)

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/