Pass file descriptor via a dbus function call from Python (aka call flatpak's HostCommand)
<p>I want to call Flatpak's <a href="https://github.com/flatpak/flatpak/commit/c8df0e6208e96e2743aa63a824d2ea7d19ce4cde" rel="nofollow">new Development DBus service to spawn a process on the host</a>, rather than in the sandbox.</p>

<p>To call the DBus service, I've come up with the following piece of code:</p>

<pre><code>#!/usr/bin/env python
import logging
import os
import sys

import dbus

def call_on_host(cmd):
    "Calls Flatpak via DBus to spawn a process"
    name = "org.freedesktop.Flatpak"
    path = "/org/freedesktop/Flatpak/Development"
    bus = dbus.SessionBus()
    proxy = bus.get_object(name, path)

    iface = "org.freedesktop.Flatpak.Development"
    fp_helper = dbus.Interface(proxy, iface)

    wd = '/tmp/'
    read_fd, write_fd = os.pipe()
    fds = {0:dbus.types.UnixFd(read_fd)}
    envs = {'FOO':'bar'}
    flags = 1

    # cwd, cmd, fds, env, flags = ('/', ['ls'], {0:dbus.types.UnixFd(open('/etc/passwd'))}, {'foo':'bar'}, 1)
    logging.info("Executing %r %r %r %r %r", wd, cmd, fds, envs, flags)
    ret = fp_helper.HostCommand(wd, cmd, fds, envs, flags)
    return ret


logging.basicConfig(level=logging.DEBUG)

print (call_on_host(sys.argv[1:]))
</code></pre>

<p>That, however, does not work so well.  The Flatpak DBus helper does not receive any values, i.e. they are all NULL.</p>

<pre><code>$ python execute_on_host.py  'ls' /
INFO:root:Executing '/tmp/' ['ls'] {0: &lt;dbus.UnixFd object at 0x7f4b5ae6c120&gt;} {'FOO': 'bar'} 1
Traceback (most recent call last):
  File "execute_on_host.py", line 42, in &lt;module&gt;
    print (call_on_host(sys.argv[1:]))
  File "execute_on_host.py", line 35, in call_on_host
    ret = fp_helper.HostCommand(wd, cmd, fds, envs, flags)
  File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
  File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs: No command 19430 given!!1 - *arg_argv[0] == 0
</code></pre>

<p>I'm a bit confused now. Do I need to wrap my types before calling into GVariants the function on the proxy object?</p>

<p>To test whether I can call a service with that signature at all, I stole most of the suff from <a href="http://stackoverflow.com/a/512723">this question</a> and came up with the following:</p>

<pre><code>import unittest

import os
import sys
import subprocess
import time

import dbus
import dbus.service
import dbus.glib
import gobject

class MyDBUSService(dbus.service.Object):

    def __init__(self):
        bus_name = dbus.service.BusName('test.helloservice', bus = dbus.SessionBus())
        dbus.service.Object.__init__(self, bus_name, '/test/helloservice')

    def listen(self):
        loop = gobject.MainLoop()
        loop.run()

    @dbus.service.method('test.helloservice', in_signature="ayaaya{uh}a{ss}u")
    def hello(self, cwd, cmd, fds, env, flags):
        print ([type(foo) for foo in (cwd, cmd, fds, env, flags)]  )
        print ("cwd: %s" % cwd)
        print ("cmd: %s" % cmd)
        print ("fsd: %s" % fds)
        r = os.fdopen(fds[0].take()).read()
        return r


class BaseTestCase(unittest.TestCase):

    def setUp(self):
        env = os.environ.copy()
        self.p = subprocess.Popen(['python', __file__, 'server'], env=env)
        # Wait for the service to become available
        time.sleep(1)
        assert self.p.stdout == None
        assert self.p.stderr == None

        open("/tmp/dbus-test", "w").write("Hello, World!")

    def testHelloService(self):
        bus = dbus.SessionBus()
        helloservice = bus.get_object('test.helloservice', '/test/helloservice')
        hello = helloservice.get_dbus_method('hello', 'test.helloservice')
        cwd, cmd, fds, env, flags = ('/', ['ls'], {0:dbus.types.UnixFd(open('/tmp/dbus-test'))}, {'foo':'bar'}, 1)
        r = hello(cwd, cmd, fds, env, flags)
        assert r == "Hello, World!"

    def tearDown(self):
        # terminate() not supported in Python 2.5
        #self.p.terminate()
        os.kill(self.p.pid, 15)


if __name__ == '__main__':

    arg = ""
    if len(sys.argv) &gt; 1:
        arg = sys.argv[1]

    if arg == "server":
        myservice = MyDBUSService()
        myservice.listen()

    else:
        unittest.main()
</code></pre>

<p>That works nicely.</p>

<p>So I'm wondering:  How can I call the Flatpak Development service from Python?</p>

<p>To investigate whether my code is causing the same messages to be sent over the bus, I started <code>dbus-monitor</code> to check what happens if a known-good client sends that message.  I got the following:</p>

<pre><code>method call time=14743.5 sender=:1.6736 -&gt; destination=org.freedesktop.Flatpak serial=8 path=/org/freedesktop/Flatpak/Development; interface=org.freedesktop.Flatpak.Development; member=HostCommand
   array of bytes "/" + \0
   array [
      array of bytes "ls" + \0
   ]
   array [
      dict entry(
         uint32 0
         file descriptor
               inode: 40
               type: char
      )
      dict entry(
         uint32 1
         file descriptor
               inode: 58091333
               type: fifo
      )
      dict entry(
         uint32 2
         file descriptor
               inode: 40
               type: char
      )
   ]
   array [
      dict entry(
         string "CLUTTER_IM_MODULE"
         string "xim"
      )
   ]
   uint32 1
</code></pre>

<p>My own client, however, produced:</p>

<pre><code>array of bytes "/"
array [
  array of bytes "ls"
]
array [
  dict entry(
     uint32 0
     file descriptor
           inode: 1866322
           type: file
  )
]
array [
  dict entry(
     string "FOO"
     string "bar"
  )
]
uint32 1
</code></pre>

<p>So the difference is the null-byte.  When adding that to my code, it works. It turns out that there is <a href="https://github.com/flatpak/flatpak/issues/319" rel="nofollow">bug report</a> about this issue.</p>

python file-descriptor dbus flatpak