# execve => zombie



## OwnTech (May 3, 2009)

Hi,

i'm create custom /sbin/init program.

to execute file use function:

```
void sigCHLD(int i UNUSED)
{
    pid_t pid = getpid();
    while(waitpid(pid, NULL, 0) > 0)
        continue;
}

int execbin(char *argv[])
{
    if(!argv[0])
        return 1;

    pid_t pid = fork();
    if(pid == 0)
    {
        struct sigaction sa;
        sa.sa_flags = 0;
        sigemptyset(&sa.sa_mask);
        sa.sa_handler = sigCHLD;
        sigaction(SIGCHLD, &sa, NULL);

        execve(argv[0], argv, NULL);
	exit(0);
    }

    if(pid == -1)
    {
        printf("execbin(%s): fork error\n", argv[0]);
        return 1;
    }

    while(waitpid(pid, NULL, 0) > 0)
        continue;

    return 0;
}
```

after start zombie test program:

```
int main(int argc, char *argv[])
{
    printf("Start\n");
    pid_t pid;
    if((pid = fork()) > 0)
        waitpid(pid, NULL, 0);
    else
    {
        if(fork() > 0)
            exit(0);
        else
        {
            char *ar[] = {"/bin/sleep", "5", NULL};
            execve(ar[0], ar, NULL);
            exit(0);
        }
        exit(0);
    }
    printf("Finish\n");
    return 0;
}
```
sleep die and leave zombie

what need do with execbin()?

---
sorry for my bad english :r


----------



## OwnTech (May 5, 2009)

I find solution 
From execbin() function, remove

```
struct sigaction sa;
        sa.sa_flags = 0;
        sigemptyset(&sa.sa_mask);
        sa.sa_handler = sigCHLD;
        sigaction(SIGCHLD, &sa, NULL);
```

change sigCHLD() function

```
void sigCHLD(int i UNUSED)
{
    waitpid(-1, 0, WNOHANG);
}
```

into main() function add

```
signal(SIGCHLD, sigCHLD);
```

---
sorry for my bad english :r


----------

