问题
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
回答1:
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).
回答2:
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);
}
回答3:
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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/6945865/process-started-from-system-command-in-c-inherits-parent-fds