php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24629 socket_select() fails: "Reason: Invalid argument"
Submitted: 2003-07-12 20:23 UTC Modified: 2003-08-26 07:10 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: zparta at skebo dot ac Assigned: jason
Status: Closed Package: Sockets related
PHP Version: 4.3.3RC2-dev OS: *BSD (Only!)
Private report: No CVE-ID:
 [2003-07-12 20:23 UTC] zparta at skebo dot ac
Description:
------------
cant use socket_select in freebsd with neither 4.3.3rc1 or 4.3.2 when using real sockets not fsockets

Reproduce code:
---------------
dont have any

Expected result:
----------------
dont know


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-12 20:27 UTC] elmicha@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.

 [2003-07-12 20:47 UTC] zparta at skebo dot ac
Reproduce code:
---------------
$this->_address = 'irc.homelien.no';
$this->_port = '6667';
$this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = @socket_connect($this->_socket, $this->_address, $this->_port);

        while ($this->_state() == SMARTIRC_STATE_CONNECTED) {
            $this->_checkbuffer();
            
            $timeout = $this->_selecttimeout();
            if ($this->_usesockets == true) {
                $sread = array($this->_socket);
                $result = @socket_select($sread, $w = null, $e = null, 0, $timeout*1000);
                
                if ($result == 1) {
                    // the socket got data to read
                    $rawdata = @socket_read($this->_socket, 10240);
                } else if ($result === false) {
                    // panic! panic! something went wrong!
                    $this->log(SMARTIRC_DEBUG_NOTICE, 'WARNING: socket_select() returned false, something went wrong! Reason: '.socket_strerror(socket_last_error()), __FILE__, __LINE__);
                    exit;
                } else {
                    // no data
                    $rawdata = null;
                }
//this code is taken from the Net_SmartIRC class from pear

Expected result:
----------------
Jul 13 03:47:04 SmartIRC.php(1636) WARNING: socket_select() returned false, something went wrong! Reason: Invalid argument


and the author of Net_SmartIRC has tested this on my machine and he says that i should bugreport this here because socket_select doesnt work on freebsd 4.8 dont know if it is version specific.
 [2003-07-12 21:10 UTC] zparta at skebo dot ac
doesnt work with PHP4.3.3RC2-dev either the latest stable from today php4-STABLE-200307130130
 [2003-07-13 00:04 UTC] sniper@php.net
Works fine with latest CVS of PHP and SmartIRC.
(under Linux, so it's propably yet another FreeBSD-only bug)

 [2003-07-13 00:16 UTC] zparta at skebo dot ac
ok thats to bad :( il just have to wait until its fixed
 [2003-07-20 13:18 UTC] meebey@php.net
I tried using fsockopen and stream_select() on the freebsd box, and it returned false, so also some error happened. Is there a way of giving out the error msg of the failed stream_select()?

 [2003-07-20 13:47 UTC] meebey@php.net
I just straced the script with stream_select() and it's also getting the "Invalid argument" error msg.

select(4, [3], [], [], {0, 300000000})  = -1 EINVAL (Invalid argument)

 [2003-07-20 16:13 UTC] meebey@php.net
I found the problem with the select() under freebsd, the select() checks the timeout values (sec and usec). When they exceed the max value which is on freebsd for usec 999,999, after this number it must be 1 sec and not 1,000,000 usecs. This is whats happening here, the script wants 300,000,000 usecs, which are 300 seconds. Linux doesn't care about this and accepts this number (and works fine with it).

I wrote the patch and tested it on the freebsd 4.8 box, strace now shows (as expected):
select(4, [3], [], [], {300, 0}

Here the patch which fixes this bug:
--- sockets.c.orig      Tue Jul 15 08:08:02 2003
+++ sockets.c   Sun Jul 20 23:01:21 2003
@@ -591,8 +591,15 @@
                        convert_to_long(&tmp);
                        sec = &tmp;
                }
-               tv.tv_sec = Z_LVAL_P(sec);
-               tv.tv_usec = usec;
+
+               if (usec > 999999) {
+                       tv.tv_sec = Z_LVAL_P(sec)+(usec/1000000);
+                       tv.tv_usec = usec%1000000;
+               } else {
+                       tv.tv_sec = Z_LVAL_P(sec);
+                       tv.tv_usec = usec;
+               }
+
                tv_p = &tv;

                if (sec == &tmp) {

 [2003-07-20 16:26 UTC] meebey@php.net
here the same patch for the stream_select()

--- file.c.orig Sun Jul 20 23:25:34 2003
+++ file.c      Sun Jul 20 23:24:55 2003
@@ -803,8 +803,15 @@
        /* If seconds is not set to null, build the timeval, else we wait indefinitely */
        if (sec != NULL) {
                convert_to_long_ex(&sec);
-               tv.tv_sec = Z_LVAL_P(sec);
-               tv.tv_usec = usec;
+
+               if (usec > 999999) {
+                       tv.tv_sec = Z_LVAL_P(sec)+(usec/1000000);
+                       tv.tv_usec = usec%1000000;
+               } else {
+                       tv.tv_sec = Z_LVAL_P(sec);
+                       tv.tv_usec = usec;
+               }
+
                tv_p = &tv;
        }

 [2003-07-21 10:13 UTC] jason@php.net
I will take a look at this patch tonight

-Jason
 [2003-07-22 02:21 UTC] jason@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

Verified, apparently there are severl OSs that have this problem
 [2003-07-22 10:03 UTC] meebey@php.net
note: you misspelled my email address in the CVS commit :(

It says: "Patch submitted from meebery@php.net"
but it should be:
"Patch submitted from meebey@php.net"
without the "r"

 [2003-07-22 10:07 UTC] meebey@php.net
also the same problem occurs with stream_select()
it also uses the select() call with sec and usec, see:
http://cvs.php.net/co.php/php-src/ext/standard/file.c?r=1.279.2.30
 [2003-07-22 19:36 UTC] jason@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

Sorry about the type-o, i will make sure its correct in the relase notes. As to the other file, that functionality was moved awhile back to streamfuncs.c which I included in the commit.

-Jason
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 04:02:11 2014 UTC