Work with stdin and stdout subsidiary process simultaneously in lua



  • When the lua scrupt was written, it came to one thing: io.popen, judging by http://www.lua.ru/doc/5.7.html may not transfer at the same time the descriptors stdin and stdout of the launch process.

    Example of problem code:

    cmdexecute = function(cmd, input)
        local f = io.popen(cmd, "w");
        if tostring(input) ~= nil then
            f:write(tostring(input));
        end;
        local l = f:read("*a");
        f:close();
        return (l);
    end;
    

    if cmdexecute('xargs ps', 'aux') ~= nil then
    print 'ps aux что-то выдал';
    else
    print 'ps aux промолчал';
    end;

    Result of:

    Длинный-длинный вывод ps aux...
    ps aux промолчал

    It should be the idea:

    ps aux что-то выдал

    Question: How do you get the descriptors and stdin and stdout from popen? Or maybe there's some other solution to the task, free io.popen?

    Upd: Because of the relevance of the question, I explain: decisions c ffi Permissibility



  • io.popen gives an interface to the system posix function popen, yes, it can only work one way. In your case, you don't need to write anything in ps. If you open it for reading, she'll read it. But I understand. ps You only have an example...

    POSIX has only one way to deal with both file descriptors of the process. Setting up a pair of sockets, linking one of these compounds to file descriptors 0 and 1dup2) Follow-up fork and the daughter-in-law cause a proper program.

    In Lua, there is no evidence of such funds. So the direct answer to your question is to get stdin and stdout at the same time impossible. Only the redirection of the programme to the file and the subsequent reading of the file can be seen from the decisions. Or an entry for the program is written in advance and submitted to the entrance. And we can still experiment with known channels. mkfifo To create a nameable channel in the file system to send it to the program or the recording from the program to redirect it into it and to lua to open this channel as a regular file and try writing/reading.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2