Re: [OFF TOPIC] Why is a process killed by close()?

Richard B. Johnson (root@chaos.analogic.com)
Wed, 9 Sep 1998 13:27:52 -0400 (EDT)


On Wed, 9 Sep 1998, Miklos Szeredi wrote:

> This is not really a Linux related question, as I experienced it first
> under Solaris. But it seems, that the linux behavior WRT this problem
> changed in recent dev. kernels, so I thought, maybe someone knows the
> answer.
>
> The symptom is that a process is Kill-ed, when the file descriptors 0,
> 1 or 2 are close()-ed (or dup2()-ed). These file descriptors are
> actually connected to /dev/null before closing.
>
> The circumstances are rather foggy, while this all happens in a shared
> library, which overrides some libc functions. This actually happens
> inside an opendir() syscall made from tcsh, when TAB is pressed for
> filename completion.
>

The following code and test results show that what you observe is not
what is happening. Try it yourself.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>

void dummy(int unused)
{ }

main()
{
int fd;
if((fd = open("/dev/null", O_RDWR)) < 0)
fprintf(stderr, "Open failed, %s\n", strerror(errno)), exit(1);
(void)signal(SIGALRM, dummy);
(void)close(0);
(void)close(1);
(void)close(2);
(void)dup(fd);
(void)dup(fd);
(void)dup(fd);
alarm(20);
pause();
(void)close(0);
(void)close(1);
(void)close(2);
(void)close(3);
pause();
return 0;

}

Script started on Wed Sep 9 13:16:21 1998
# ./a.out &
[1] 333
# file /proc/333/fd/*
/proc/333/fd/0: symbolic link to /dev/null
/proc/333/fd/1: symbolic link to /dev/null
/proc/333/fd/2: symbolic link to /dev/null
/proc/333/fd/3: symbolic link to /dev/null
# file /proc/333/fd/*
/proc/333/fd/*: can't stat `/proc/333/fd/*' (No such file or directory).
# ps
PID TTY STAT TIME COMMAND
93 1 S 0:00 -bash
94 2 S 0:00 -bash
95 3 S 0:00 /sbin/agetty 38400 tty3
96 4 S 0:00 /sbin/agetty 38400 tty4
97 5 S 0:00 /sbin/agetty 38400 tty5
98 6 S 0:00 /sbin/agetty 38400 tty6
329 1 S 0:00 script
330 1 S 0:00 script
331 ? S 0:00 bash -i
333 ? S 0:00 ./a.out
336 ? R 0:00 ps
# kill 333
[1]+ Terminated ./a.out
# exit
exit
Script done on Wed Sep 9 13:17:10 1998

As you can see. The program opens a fd, closes 0, 1, and 2, then
dups the open fd to 0, 1, 2, which can be observed when the program
is run.

Then, after the alarm signal is delivered, the fds are all closed.
The proc entry now shows that there are no open file-descriptors
as expected.

'ps' shows that the program is still alive and well. I finally kill
it.

Cheers,
Dick Johnson
***** FILE SYSTEM WAS MODIFIED *****
Penguin : Linux version 2.1.118 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.

-
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/faq.html