windowsqtservicedbusqdbus

QtService application cannot connect to system bus when running as a service


I have recompiled Qt with integrated dbus for windows.

Further, I have implemented a QtService application which registers a service and an object at the system bus as follows:

QDBusConnection::systemBus().registerService("com.mycompany.Configuration");
QDBusConnection::systemBus().registerObject("/com/mycompany/Configuration/MySetting", myObject);

Now when I am running the service application with the -e parameter (running as regular application) it works fine and both dbus-monitor and qdbus --system show me the registered service and object.

However, when I am running the application as a windows service it does not register anything at the system bus.

Debugging the service gives me the error message

Not connected to D-Bus server

when calling registerService(...) and registerObject(...).

I have set the environment variables:

DBUS_SYSTEM_BUS_ADDRESS = autolaunch:
DBUS_SYSTEM_BUS_DEFAULT_ADDRESS = autolaunch:

My system.conf file:

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>

  <type>system</type>

  <fork/>

  <pidfile>/pid</pidfile>

  <listen>autolaunch:</listen>

  <policy context="default">

    <allow user="*"/>

    <deny own="*"/>
    <deny send_type="method_call"/>

    <allow send_type="signal"/>
    <allow send_requested_reply="true" send_type="method_return"/>
    <allow send_requested_reply="true" send_type="error"/>

    <allow receive_type="method_call"/>
    <allow receive_type="method_return"/>
    <allow receive_type="error"/>
    <allow receive_type="signal"/>

    <allow send_destination="org.freedesktop.DBus"/>

    <deny send_destination="org.freedesktop.DBus"
          send_interface="org.freedesktop.DBus"
          send_member="UpdateActivationEnvironment"/>
  </policy>

  <includedir>system.d</includedir>

  <include ignore_missing="yes">system-local.conf</include>

  <include if_selinux_enabled="yes" selinux_root_relative="yes">contexts/dbus_contexts</include>

</busconfig>

Additionally, I have a com.mycompany.Configuration.conf file in the system.d folder:

<!DOCTYPE busconfig PUBLIC
      "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
      "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>

  <!-- Only root can own the service -->
  <policy context="default">
    <allow own="com.mycompany.Configuration"/>
    <allow send_interface="com.mycompany.Configuration"/>
    <allow send_destination="com.mycompany.Configuration"/>
    <allow receive_sender="com.mycompany.Configuration"/>
  </policy>

</busconfig>

The windows service itself is called com.mycompany.Configuration and is executed as the Local System account.

Moreover, dbus verbose output shows that something is going on when my windows service gets started:

[...]
1292: [dbus\dbus-connection.c(1154):_dbus_connection_release_io_path] locking io_path_mutex
1292: [dbus\dbus-connection.c(1160):_dbus_connection_release_io_path] start connection->io_path_acquired = 1
1292: [dbus\dbus-connection.c(1165):_dbus_connection_release_io_path] unlockingio_path_mutex
1292: [dbus\dbus-connection.c(1522):_dbus_connection_handle_watch] middle
1292: [dbus\dbus-connection.c(4222):_dbus_connection_get_dispatch_status_unlocked] dispatch status = complete is_connected = 1
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(1529):_dbus_connection_handle_watch] end
1292: [dbus\dbus-watch.c(696):dbus_watch_handle] After sanitization, watch flags on fd 1932 were 0
1292: [dbus\dbus-sysdeps-win.c(1110):_dbus_poll] select: to=7875
        R:1944 E:1944
        R:1936 E:1936
        R:1932 E:1932

[dbus\dbus-sysdeps-win.c(1162):_dbus_poll] select: = 1:
        R:1932
[dbus\dbus-connection.c(1503):_dbus_connection_handle_watch] start
1292: [dbus\dbus-connection.c(1505):_dbus_connection_handle_watch] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(1081):_dbus_connection_acquire_io_path] locking io_path_mutex
1292: [dbus\dbus-connection.c(1085):_dbus_connection_acquire_io_path] start connection->io_path_acquired = 0 timeout = 1
1292: [dbus\dbus-connection.c(1128):_dbus_connection_acquire_io_path] end connection->io_path_acquired = 1 we_acquired = 1
1292: [dbus\dbus-connection.c(1130):_dbus_connection_acquire_io_path] unlocking io_path_mutex
1292: [dbus\dbus-connection.c(1133):_dbus_connection_acquire_io_path] LOCK
1292: [dbus\dbus-transport-socket.c(919):socket_handle_watch] handling read watch 003d6f58 flags = 1
1292: [dbus\dbus-transport-socket.c(345):exchange_credentials] exchange_credentials: do_reading = 1, do_writing = 0
1292: [dbus\dbus-transport-socket.c(446):do_authentication]  server auth state:waiting for input
1292: [dbus\dbus-sysdeps-win.c(207):_dbus_read_socket] recv: count=2048 fd=1932
1292: [dbus\dbus-sysdeps-win.c(217):_dbus_read_socket] recv: = 153
1292: [dbus\dbus-transport-socket.c(263):read_data_into_auth]  read 153 bytes in auth phase
1292: [dbus\dbus-auth.c(2187):process_command] server: got command "DATA 30333432316532346364316236363730303938303934303836666364653631372063303064313764623036653935643732336665666330396131633131653265353030643632616237"
1292: [dbus\dbus-auth.c(1624):process_data] server: data: '03421e24cd1b6670098094086fcde617 c00d17db06e95d723fefc09a1c11e2e500d62ab7'
1292: [dbus\dbus-auth.c(2134):goto_state] server: going from state WaitingForData to state WaitingForBegin
1292: [dbus\dbus-auth.c(760):sha1_handle_second_client_response] server: authenticated client using DBUS_COOKIE_SHA1
1292: [dbus\dbus-transport-socket.c(345):exchange_credentials] exchange_credentials: do_reading = 1, do_writing = 0
1292: [dbus\dbus-transport-socket.c(460):do_authentication]  server auth state:bytes to send
1292: [dbus\dbus-transport-socket.c(180):check_read_watch] fd = 1932
1292: [dbus\dbus-transport-socket.c(226):check_read_watch]   setting read watch enabled = 0
1292: [dbus\dbus-watch.c(449):_dbus_watch_list_toggle_watch] Toggling watch 003d6f58 on fd 1932 to 0
1292: [dbus\dbus-transport-socket.c(165):check_write_watch] check_write_watch(): needed = 1 on connection 003d88b0 watch 003d7260 fd = 1932 outgoing messages exist 0
1292: [dbus\dbus-watch.c(449):_dbus_watch_list_toggle_watch] Toggling watch 003d7260 on fd 1932 to 1
1292: [dbus\dbus-transport-socket.c(703):do_reading] fd = 1932
1292: [dbus\dbus-connection.c(1154):_dbus_connection_release_io_path] locking io_path_mutex
1292: [dbus\dbus-connection.c(1160):_dbus_connection_release_io_path] start connection->io_path_acquired = 1
1292: [dbus\dbus-connection.c(1165):_dbus_connection_release_io_path] unlockingio_path_mutex
1292: [dbus\dbus-connection.c(1522):_dbus_connection_handle_watch] middle
1292: [dbus\dbus-connection.c(4222):_dbus_connection_get_dispatch_status_unlocked] dispatch status = complete is_connected = 1
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(1529):_dbus_connection_handle_watch] end
1292: [dbus\dbus-sysdeps-win.c(1110):_dbus_poll] select: to=5641
        R:1944 E:1944
        R:1936 E:1936
        W:1932 E:1932

[dbus\dbus-sysdeps-win.c(1162):_dbus_poll] select: = 1:
        W:1932
[dbus\dbus-connection.c(1503):_dbus_connection_handle_watch] start
1292: [dbus\dbus-connection.c(1505):_dbus_connection_handle_watch] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(1081):_dbus_connection_acquire_io_path] locking io_path_mutex
1292: [dbus\dbus-connection.c(1085):_dbus_connection_acquire_io_path] start connection->io_path_acquired = 0 timeout = 1
1292: [dbus\dbus-connection.c(1128):_dbus_connection_acquire_io_path] end connection->io_path_acquired = 1 we_acquired = 1
1292: [dbus\dbus-connection.c(1130):_dbus_connection_acquire_io_path] unlockingio_path_mutex
1292: [dbus\dbus-connection.c(1133):_dbus_connection_acquire_io_path] LOCK
1292: [dbus\dbus-transport-socket.c(948):socket_handle_watch] handling write watch, have_outgoing_messages = 0
1292: [dbus\dbus-transport-socket.c(345):exchange_credentials] exchange_credentials: do_reading = 0, do_writing = 1
1292: [dbus\dbus-transport-socket.c(460):do_authentication]  server auth state:bytes to send
1292: [dbus\dbus-sysdeps-win.c(267):_dbus_write_socket] send: len=37 fd=1932
1292: [dbus\dbus-sysdeps-win.c(277):_dbus_write_socket] send: = 37
1292: [dbus\dbus-auth.c(2506):_dbus_auth_bytes_sent] server: Sent 37 bytes of: OK 4d63998db68399d8b48494595200cd61

1292: [dbus\dbus-transport-socket.c(345):exchange_credentials] exchange_credentials: do_reading = 0, do_writing = 1
1292: [dbus\dbus-transport-socket.c(446):do_authentication]  server auth state:waiting for input
1292: [dbus\dbus-transport-socket.c(180):check_read_watch] fd = 1932
1292: [dbus\dbus-transport-socket.c(226):check_read_watch]   setting read watchenabled = 1
1292: [dbus\dbus-watch.c(449):_dbus_watch_list_toggle_watch] Toggling watch 003d6f58 on fd 1932 to 1
1292: [dbus\dbus-transport-socket.c(165):check_write_watch] check_write_watch(): needed = 0 on connection 003d88b0 watch 003d7260 fd = 1932 outgoing messages exist 0
1292: [dbus\dbus-watch.c(449):_dbus_watch_list_toggle_watch] Toggling watch 003d7260 on fd 1932 to 0
1292: [dbus\dbus-transport-socket.c(503):do_writing] Not authenticated, not writing anything
1292: [dbus\dbus-transport-socket.c(165):check_write_watch] check_write_watch(): needed = 0 on connection 003d88b0 watch 003d7260 fd = 1932 outgoing messages exist 0
1292: [dbus\dbus-connection.c(1154):_dbus_connection_release_io_path] locking io_path_mutex
1292: [dbus\dbus-connection.c(1160):_dbus_connection_release_io_path] start connection->io_path_acquired = 1
1292: [dbus\dbus-connection.c(1165):_dbus_connection_release_io_path] unlockingio_path_mutex
1292: [dbus\dbus-connection.c(1522):_dbus_connection_handle_watch] middle
1292: [dbus\dbus-connection.c(4222):_dbus_connection_get_dispatch_status_unlocked] dispatch status = complete is_connected = 1
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(1529):_dbus_connection_handle_watch] end
1292: [dbus\dbus-watch.c(696):dbus_watch_handle] After sanitization, watch flags on fd 1932 were 0
1292: [dbus\dbus-sysdeps-win.c(1110):_dbus_poll] select: to=3484
        R:1944 E:1944
        R:1936 E:1936
        R:1932 E:1932

[dbus\dbus-sysdeps-win.c(1162):_dbus_poll] select: = 1:
        R:1932
[dbus\dbus-connection.c(1503):_dbus_connection_handle_watch] start
1292: [dbus\dbus-connection.c(1505):_dbus_connection_handle_watch] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(1081):_dbus_connection_acquire_io_path] locking io_path_mutex
1292: [dbus\dbus-connection.c(1085):_dbus_connection_acquire_io_path] start connection->io_path_acquired = 0 timeout = 1
1292: [dbus\dbus-connection.c(1128):_dbus_connection_acquire_io_path] end connection->io_path_acquired = 1 we_acquired = 1
1292: [dbus\dbus-connection.c(1130):_dbus_connection_acquire_io_path] unlockingio_path_mutex
1292: [dbus\dbus-connection.c(1133):_dbus_connection_acquire_io_path] LOCK
1292: [dbus\dbus-transport-socket.c(919):socket_handle_watch] handling read watch 003d6f58 flags = 1
1292: [dbus\dbus-transport-socket.c(345):exchange_credentials] exchange_credentials: do_reading = 1, do_writing = 0
1292: [dbus\dbus-transport-socket.c(446):do_authentication]  server auth state:waiting for input
1292: [dbus\dbus-sysdeps-win.c(207):_dbus_read_socket] recv: count=2048 fd=1932
1292: [dbus\dbus-sysdeps-win.c(217):_dbus_read_socket] recv: = 135
1292: [dbus\dbus-transport-socket.c(263):read_data_into_auth]  read 135 bytes in auth phase
1292: [dbus\dbus-auth.c(2187):process_command] server: got command "BEGIN"
1292: [dbus\dbus-auth.c(2134):goto_state] server: going from state WaitingForBegin to state Authenticated
1292: [dbus\dbus-sysdeps-win.c(787):_dbus_getsid] _dbus_getsid() returns 1
1292: [dbus\dbus-transport.c(673):auth_via_default_rules] Client authorized as S    ID 'S-1-5-18' but our SID is 'S-1-5-21-4012214046-2405994403-1636280286-500', disconnecting
1292: [dbus\dbus-transport.c(503):_dbus_transport_disconnect] start
1292: [dbus\dbus-transport-socket.c(985):socket_disconnect]
1292: [dbus\dbus-transport-socket.c(76):free_watches] start
1292: [dbus\dbus-watch.c(417):_dbus_watch_list_remove_watch] Removing watch on fd 1932
1292: [dbus\dbus-watch.c(417):_dbus_watch_list_remove_watch] Removing watch on fd 1932
1292: [dbus\dbus-socket-set-poll.c(221):socket_set_poll_remove] after removing fd 1932 from 003d2430, 2 en/2 res/9 alloc
1292: [dbus\dbus-watch.c(628):dbus_watch_set_data] Setting watch fd -1 data to data = 00000000 function = 00000000 from data = 00000000 function = 00000000
1292: [dbus\dbus-transport-socket.c(98):free_watches] end
1292: [dbus\dbus-sysdeps-win.c(317):_dbus_close_socket] _dbus_close_socket: socket=1932,
1292: [dbus\dbus-transport.c(514):_dbus_transport_disconnect] end
1292: [dbus\dbus-transport-socket.c(180):check_read_watch] fd = -1
1292: [dbus\dbus-transport-socket.c(703):do_reading] fd = -1
1292: [dbus\dbus-watch.c(628):dbus_watch_set_data] Setting watch fd -1 data to data = 00000000 function = 00000000 from data = 00000000 function = 00000000
1292: [dbus\dbus-connection.c(1154):_dbus_connection_release_io_path] locking io_path_mutex
1292: [dbus\dbus-connection.c(1160):_dbus_connection_release_io_path] start connection->io_path_acquired = 1
1292: [dbus\dbus-connection.c(1165):_dbus_connection_release_io_path] unlockingio_path_mutex
1292: [dbus\dbus-connection.c(1522):_dbus_connection_handle_watch] middle
1292: [dbus\dbus-connection.c(4222):_dbus_connection_get_dispatch_status_unlocked] dispatch status = complete is_connected = 0
1292: [dbus\dbus-connection.c(4184):notify_disconnected_and_dispatch_complete_unlocked] Sending disconnect message
1292: [dbus\dbus-connection.c(567):_dbus_connection_queue_synthesized_message_link] Synthesized message 003d8948 added to incoming queue 003d88b0, 1 incoming
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(4299):_dbus_connection_update_dispatch_status_and_unlock] Notifying of change to dispatch status of 003d88b0 now 0 (data remains)
1292: [dbus\dbus-connection.c(1529):_dbus_connection_handle_watch] end
1292: [dbus\dbus-connection.c(4515):dbus_connection_dispatch]
1292: [dbus\dbus-connection.c(4517):dbus_connection_dispatch] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(4090):_dbus_connection_acquire_dispatch] locking dispatch_mutex
1292: [dbus\dbus-connection.c(4104):_dbus_connection_acquire_dispatch] unlocking dispatch_mutex
1292: [dbus\dbus-connection.c(4107):_dbus_connection_acquire_dispatch] LOCK
1292: [dbus\dbus-connection.c(3954):_dbus_connection_pop_message_link_unlocked]Message 003d8948 (signal /org/freedesktop/DBus/Local org.freedesktop.DBus.Local Disconnected '') removed from incoming queue 003d88b0, 0 incoming
1292: [dbus\dbus-connection.c(4563):dbus_connection_dispatch]  dispatching message 003d8948 (signal org.freedesktop.DBus.Local Disconnected '')
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(4630):dbus_connection_dispatch]   running filter on message 003d8948
1292: [dbus\dbus-connection.c(3126):dbus_connection_preallocate_send] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [bus\dispatch.c(216):bus_dispatch] DISPATCH: org.freedesktop.DBus.Local Disconnected (no error name) to peer
1292: [bus\connection.c(198):bus_connection_disconnected] (inactive) disconnected, dropping all service ownership and releasing
1292: [dbus\dbus-connection.c(5526):dbus_connection_remove_filter] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(4891):dbus_connection_set_watch_functions] LOCK
1292: [dbus\dbus-watch.c(347):_dbus_watch_list_set_functions] Removing all pre-existing watches
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(4954):dbus_connection_set_timeout_functions] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(5268):dbus_connection_set_unix_user_function] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(5367):dbus_connection_set_windows_user_function] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(5037):dbus_connection_set_dispatch_status_function] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [bus\connection.c(1587):bus_connection_drop_pending_replies] Dropping pending replies that involve connection 003d88b0
1292: [dbus\dbus-connection.c(4644):dbus_connection_dispatch] LOCK
1292: [dbus\dbus-connection.c(4653):dbus_connection_dispatch] filter handled message in dispatch
1292: [dbus\dbus-connection.c(4784):dbus_connection_dispatch]  ... done dispatching
1292: [dbus\dbus-connection.c(4123):_dbus_connection_release_dispatch] locking dispatch_mutex
1292: [dbus\dbus-connection.c(4131):_dbus_connection_release_dispatch] unlocking dispatch_mutex
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(4801):dbus_connection_dispatch] LOCK
1292: [dbus\dbus-connection.c(4807):dbus_connection_dispatch] before final status update
1292: [dbus\dbus-connection.c(4222):_dbus_connection_get_dispatch_status_unlocked] dispatch status = complete is_connected = 0
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(2670):_dbus_connection_last_unref] Finalizing connection 003d88b0
1292: [dbus\dbus-connection.c(5037):dbus_connection_set_dispatch_status_function] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(4991):dbus_connection_set_wakeup_main_function] LOCK
1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-connection.c(5268):dbus_connection_set_unix_user_function] LOCK

1292: [dbus\dbus-connection.c(413):_dbus_connection_unlock] UNLOCK
1292: [dbus\dbus-transport.c(484):_dbus_transport_unref] finalizing
1292: [dbus\dbus-transport-socket.c(106):socket_finalize]
1292: [dbus\dbus-transport-socket.c(76):free_watches] start
1292: [dbus\dbus-transport-socket.c(98):free_watches] end
1292: [dbus\dbus-auth.c(430):shutdown_mech] server: Shutting down mechanism DBUS_COOKIE_SHA1
1292: [dbus\dbus-sysdeps-win.c(1110):_dbus_poll] select: to=0
        R:1944 E:1944
        R:1936 E:1936

[dbus\dbus-sysdeps-win.c(1140):_dbus_poll] select: = 0
1292: [bus\connection.c(828):expire_incomplete_timeout] Running
1292: [bus\expirelist.c(111):bus_expire_timeout_set_interval] Disabled an expire timeout
1292: [dbus\dbus-sysdeps-win.c(1110):_dbus_poll] select: to=-1
        R:1944 E:1944
        R:1936 E:1936
[...]

I really don't know what the issue could be and would appreciate any help.

Edit: This one seems to have the same issue: http://comments.gmane.org/gmane.comp.freedesktop.dbus/14895


Solution

  • In the end I solved the problem and my service is now able to connect to the system bus.

    I made the following changes:

    In system.conf I changed the listen property and the authentication property:

    <listen>tcp:host=localhost,port=13337,family=ipv4</listen>
    <auth>ANONYMOUS</auth>
    <allow_anonymous/>
    

    According to that changes I set the system environment variables analogously:

    DBUS_SYSTEM_BUS_ADDRESS = tcp:host=localhost,port=13337,family=ipv4
    DBUS_SYSTEM_BUS_DEFAULT_ADDRESS = tcp:host=localhost,port=13337,family=ipv4
    

    This answer led me in the right direction.