Package: liblua5.1-posix1
Version: 5.1.2-3
Severity: normal


  How to reproduce:

[isbear:~] cat one
adsf
fasdfea adf
fdasfeaf
fdaaaffff
fdag
fdasffffasddf

[isbear:~] cat luatest2.lua
-- pipe

require ( 'posix' )

f = io.popen ( 'cat one' )
c = 10
while c > 0 do
        c   = c - 1
        ret = posix.rpoll ( f, 500 )
        lin = nil
--      if ret == 1 then
                lin = f:read ()
--      end
        print ( ret, lin )
end

-- The End.
[isbear:~] lua luatest2.lua
nil     adsf
nil     fasdfea adf
nil     fdasfeaf
nil     fdaaaffff
nil     fdag
nil     fdasffffasddf
nil
nil     nil
nil     nil
nil     nil

As we see, rpoll returns nil on successfully opened pipe. Though, there
are even more amazing example:

[isbear:~] cat luatest3.lua
-- pipe with delay

require ( 'posix' )

f = io.popen ( 'sleep 1 ; cat one' )
c = 10
while c > 0 do
        c   = c - 1
        ret = posix.rpoll ( f, 500 )
        lin = nil
        if ( ret == 1 ) then
                lin = f:read ()
        end
        print ( ret, lin )
end

-- The End.
[isbear:~] lua luatest3.lua
0       nil
0       nil
1       adsf
nil     nil
nil     nil
nil     nil
nil     nil
nil     nil
nil     nil
nil     nil

rpoll's behaviour looks strange, isn't it?



  No any error messages



  How it is expected to be

There is corresponding code snippet from lposix.c:

/* from http://lua-users.org/lists/lua-l/2007-11/msg00346.html */
static int Ppoll(lua_State *L)   /** poll(filehandle, timeout) */
{
        struct pollfd fds;
        FILE* file = *(FILE**)luaL_checkudata(L,1,LUA_FILEHANDLE);
        int ret,timeout = luaL_checkint(L,2);
        fds.fd = fileno(file);
        fds.events = POLLIN;
        ret = poll(&fds,1,timeout);
        if (ret == -1) {
                lua_error(L);
        }
        if (ret == 1 && ((fds.revents & POLLHUP) || (fds.revents & POLLNVAL))) {
                lua_pushnil(L);
        } else {
                lua_pushnumber(L,ret);
        }    
        return 1;
}


I'll use next simple C program to demonstrate what is going behind the
scenes:
[isbear:~] cat poll.c

#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>

int main ( int argc, char **argv )
{
        int ret;
        struct pollfd fds;
        char a[10];
        int c = 10;

        switch ( *argv[1] ) {
        case 'p':
                fds.fd = fileno ( popen ( argv[2], "r" ) );
                break;
        case 'f':
                fds.fd = open ( argv[2], O_RDONLY );
                break;
        default:
                fds.fd = fileno ( stdin );
                break;
        }

        fds.events = POLLIN;
        printf ( "file number: %d\n", fds.fd );
        printf ( "(POLLIN %X, POLLPRI %X, POLLOUT %X, POLLERR %X,"
                 " POLLHUP %X, POLLNVAL %X)\n",
                 POLLIN, POLLPRI, POLLOUT, POLLERR, POLLHUP, POLLNVAL );

        do {
                ret = poll ( &fds, 1, 400 );
                printf ( "Poll: %d, revents: %X", ret, fds.revents );

                if ( ret ) {
                        int num = read ( fds.fd, a, 9 );
                        printf ( ", read: %d", num );
                }

                puts ("");

                sleep ( 1 );
        } while ( c-- > 0 );
}

/* The End. */
[isbear:~] gcc poll.c
[isbear:~] ./a.out f 'one'
file number: 3
(POLLIN 1, POLLPRI 2, POLLOUT 4, POLLERR 8, POLLHUP 10, POLLNVAL 20)
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 2
Poll: 1, revents: 1, read: 0
Poll: 1, revents: 1, read: 0
Poll: 1, revents: 1, read: 0
Poll: 1, revents: 1, read: 0
[isbear:~] ./a.out p 'cat one'
file number: 3
(POLLIN 1, POLLPRI 2, POLLOUT 4, POLLERR 8, POLLHUP 10, POLLNVAL 20)
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 2
Poll: 1, revents: 10, read: 0
Poll: 1, revents: 10, read: 0
Poll: 1, revents: 10, read: 0
Poll: 1, revents: 10, read: 0
[isbear:~] ./a.out p 'sleep 1; cat one; sleep 3'
file number: 3
(POLLIN 1, POLLPRI 2, POLLOUT 4, POLLERR 8, POLLHUP 10, POLLNVAL 20)
Poll: 0, revents: 0
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 1, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 9
Poll: 1, revents: 11, read: 2
Poll: 1, revents: 10, read: 0
Poll: 1, revents: 10, read: 0
Poll: 1, revents: 10, read: 0

so, when pipe command finishes (but data are still available), poll
sets POLLHUP, and rpoll then returns nil.

man 2 poll says:
              POLLHUP
                     Hang up (output only).
              POLLNVAL
                     Invalid request: fd not open (output only).

So, these flags are relative to output, not to input (as we claimed in
fds.events, we interested only in input events). IMHO, these flags
should be ignored.

When I added patch below and rebuilt package, all works fine.
--- lua-posix-5.1.2.a/posix.c   2008-01-29 14:54:24.000000000 +0200
+++ lua-posix-5.1.2/lposix.c    2008-07-18 03:39:39.000000000 +0300
@@ -495,11 +495,11 @@
        if (ret == -1) {
                lua_error(L);
        }
-       if (ret == 1 && ((fds.revents & POLLHUP) || (fds.revents & POLLNVAL))) {
+/*     if (ret == 1 && ((fds.revents & POLLHUP) || (fds.revents & POLLNVAL))) {
                lua_pushnil(L);
-       } else {
+       } else {*/
                lua_pushnumber(L,ret);
-       }    
+/*     }    */
        return 1;
 }



  Reportbug output:

-- System Information:
Debian Release: lenny/sid
  APT prefers testing
  APT policy: (500, 'testing'), (50, 'unstable')
Architecture: i386 (i686)

Kernel: Linux 2.6.24-1-686 (SMP w/1 CPU core)
Locale: LANG=uk_UA.UTF-8, LC_CTYPE=uk_UA.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages liblua5.1-posix1 depends on:
ii  libc6                         2.7-10     GNU C Library: Shared libraries

liblua5.1-posix1 recommends no packages.

-- no debconf information


-- 
Михайло Даниленко
-------------------------------
jabber: <[EMAIL PROTECTED]>
icq:    200352743



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to