Process started from system command in C inherits parent fd's

后端 未结 5 912
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 19:36

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 &\");

5条回答
  •  悲&欢浪女
    2020-12-09 20:19

    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.

提交回复
热议问题