Req #43290 [Opn]: Proposed new socket function: socket_set_close_on_exec

From: Date: Mon, 04 Aug 2014 19:46:07 +0000
Subject: Req #43290 [Opn]: Proposed new socket function: socket_set_close_on_exec
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-186967@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=43290&edit=1 ID: 43290 Updated by: langemeijer@php.net Reported by: roelf at neology dot co dot za Summary: Proposed new socket function: socket_set_close_on_exec Status: Open Type: Feature/Change Request -Package: Feature/Change Request +Package: *General Issues Operating System: Debian GNU/Linux Etch PHP Version: 5.2.5 Block user comment: N Private report: N New Comment: Related to bug #67383. Really the same issue, but that bug report is saying it's a bug, not a missing feature. Previous Comments: ------------------------------------------------------------------------ [2007-11-14 08:58:25] roelf at neology dot co dot za Description: ------------ I would like to propose a new socket function: socket_set_close_on_exec Reasoning: I have a case where I developed a php CLI sapi script, that manages an entire Linux system's bootup process, and acts as a system process supervisor, similar to 'init'. The supervisor listens using php socket functions on port 19992, in order to receive text commands that administer the supervisor. One of the processes that my script supervises, is a PHP CLI implementation of a webserver, called 'nanoweb'. When the supervisor 'exec()'s the nanoweb server (and any other process for that matter) the child inherits the listening socket, port 19992. If the supervisor is unexpectedly killed e.g. 'kill -9' nanoweb typically starts listening on this socket. The problem is that the supervisor is incapable now of being restarted, due to the fact that it's port is in use, and the supervisor is unable to bind the port. The problem is not with nanoweb, itsself, but the behaviour where the listening socket is inherited. I propose an additional socket function socket_set_close_on_exec() which will do the appropriate fcntl() on the socket handle, so that child processes do not inherit the socket. This elegantly solves the problem in my case without changing the default behaviour of php. Below is the example code I used to patch my PHP - it is based entirely on the socket_set_nonblock() function's code. Reproduce code: --------------- /* {{{ proto bool socket_set_close_on_exec(resource socket) Sets close-on-exec mode on a socket resource */ PHP_FUNCTION(socket_set_close_on_exec) { zval *arg1; php_socket *php_sock; int flags; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) return; ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket); flags = fcntl(php_sock->bsd_socket, F_GETFD); /* Safely append close-on-exec to other flags unless the get fails. */ if (flags > -1) flags |= FD_CLOEXEC; else flags = FD_CLOEXEC; if (fcntl(php_sock->bsd_socket, F_SETFD, flags) > -1) { RETURN_TRUE; } RETURN_FALSE; } /* }}} */ Expected result: ---------------- I expect a function to be available to set the FD_CLOEXEC flag on socket handles Actual result: -------------- There is no such function available ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=43290&edit=1

« previous php.bugs (#186967) next »