13

I have a sample application of a SIP server listening on both tcp and udp ports 5060. At some point in the code, I do a system("pppd file /etc/ppp/myoptions &");

After this if I do a netstat -apn, It shows me that ports 5060 are also opened for pppd! Is there any method to avoid this? Is this standard behaviour of the system function in Linux?

Thanks, Elison

Elison Niven
  • 236
  • 3
  • 12

5 Answers5

15

Yes, by default whenever you fork a process (which system does), the child inherits all the parent's file descriptors. If the child doesn't need those descriptors, it SHOULD close them. The way to do this with system (or any other method that does a fork+exec) is to set the FD_CLOEXEC flag on all file descriptors that shouldn't be used by the children of you process. This will cause them to be closed automatically whenever any child execs some other program.

In general, ANY TIME your program opens ANY KIND of file descriptor that will live for an extended period of time (such as a listen socket in your example), and which should not be shared with children, you should do

fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);

on the file descriptor.


As of the 2016? revision of POSIX.1, you can use the SOCK_CLOEXEC flag or'd into the type of the socket to get this behavior automatically when you create the socket:

listenfd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
bind(listenfd, ...
listen(listemfd, ...

which guarentees it will be closed properly even if some other simultaneously running thread does a system or fork+exec call. Fortunately, this flag has been supported for awhile on Linux and BSD unixes (but not OSX, unfortunately).

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Yes, I did come across with this. Till the time I change my code to use fork()/exec() I will use this method only. – Elison Niven Aug 04 '11 at 18:52
  • 3
    With POSIX 2008 or GNU, you can also use the `O_CLOEXEC` flag when opening the file. This avoids having to make a separate call, and makes the operation *atomic*, which matters if another thread (or signal handler) could fork-and-exec between the `open` call and the `fcntl` call. – R.. GitHub STOP HELPING ICE Aug 04 '11 at 21:23
3

You should probably avoid the system() function altogether. It's inherently dangerous, in that it invokes the shell, which can be tampered with and rather non-portable, even between Unicies.

What you should do is the fork()/exec() dance. It goes something like this

if(!fork()){
     //close file descriptors
     ...

    execlp("pppd", "pppd", "file", "/etc/ppp/myoptions", NULL);
    perror("exec");
    exit(-1);
}
Dave
  • 10,964
  • 3
  • 32
  • 54
  • you might want to add some advice about how to find out which fds to close if there's more than just the socket. – Spudd86 Aug 04 '11 at 18:10
  • Thanks. Yes, I will definitely replace the system() with fork()/exec(). Presently my code calls system() many times ! I use system because I can get the return status with WIFEXITED and its much faster to code ! Btw, what method do you suggest I follow If I need a lot of things to done from my C code like write /etc/resolv.conf, start pppd, kill pppd, run iptables -F etc.. Right now all these are done my system(). – Elison Niven Aug 04 '11 at 18:46
1

Yes, this is standard behavior of fork() in Linux, from which system() is implemented.

The identifier returned from the socket() call is a valid file descriptor. This value is usable with file-oriented functions such as read(), write(), ioctl(), and close().

The converse, that every file descriptor is a socket, is not true. One cannot open a regular file with open() and pass that descriptor to, e.g., bind() or listen().

When you call system() the child process inherits the same file descriptors as the parent. This is how stdout (0), stdin (1), and stderr (2) are inherited by child processes. If you arrange to open a socket with a file descriptor of 0, 1 or 2, the child process will inherit that socket as one of the standard I/O file descriptors.

Your child process is inheriting every open file descriptor from the parent, including the socket you opened.

Heath Hunnicutt
  • 18,667
  • 3
  • 39
  • 62
  • I open the SIP server on 2 network interfaces for both udp and tcp ports. So before the system() call, netstat shows me 4 entries for 5060 - 2 tcp for eth0, 2 udp for eth1. After the system(pppd...) call, netstat gives me 6 entries for pppd(). pppd inherits 2 sockets for eth0. May be this is because once pppd link is established, I close the SIP server sockets on eth1 interface in my main application. This explains why only 2 are inherited. I call netstat from a shell script at intervals of 2 seconds so may be it missed to show me when pppd had inherited all 4 sockets. – Elison Niven Aug 04 '11 at 18:49
1

As others have stated, this is standard behavior that programs depend on.

When it comes to preventing it you have a few options. Firstly is closing all file descriptors after the fork(), as Dave suggests. Second, there is the POSIX support for using fcntl with FD_CLOEXEC to set a 'close on exec' bit on a per-fd basis.

Finally, though, since you mention you are running on Linux, there are a set of changes designed to let you set the bit right at the point of opening things. Naturally, this is platform dependent. An overview can be found at http://udrepper.livejournal.com/20407.html

What this means is that you can use a bitwise or with the 'type' in your socket creation call to set the SOCK_CLOEXEC flag. Provided you're running kernel 2.6.27 or later, that is.

Arthur Shipkowski
  • 3,606
  • 1
  • 22
  • 30
-1

system() copies current process and then launch a child on top of it. (current process is no more there. that is probably why pppd uses 5060. You can try fork()/exec() to create a child process and keep parent alive.

hari
  • 9,439
  • 27
  • 76
  • 110
  • 1
    -1: System does not replace/terminate the parent process. In fact, ```system()``` uses ```fork()```/```exec()```. – Heath Hunnicutt Aug 04 '11 at 17:46
  • Heath Hunnicutt: Thanks, so after `system()` 2 processes exist? parent and child? – hari Aug 04 '11 at 18:59
  • After ```system()```, there are 2 processes, the parent and child -- but the call has not yet returned. The ```system()``` implementation calls ```waitpid``` on the child PID. In the case of ```pppd```, the process self-backgrounds (```fork```/```exec``` without a ```waitpid```), so there are momentarily *three* processes, two of ppd, one parent. By the time ```system``` returns, the child has exited, because the return value within C contains the value supplied to ```exit``` by the spawned program. Once ```system``` returns, the child must have exited. – Heath Hunnicutt Aug 04 '11 at 20:52
  • @Heath Hunnicutt: Cool, thanks for the explanation. Now it kinda makes sense. – hari Aug 04 '11 at 21:06