D-Bus自启动服务(五)

D-Bus 中的service,有两种启动模式:一种开机就启动,另一种按需启动服务

在之前看的/etc/dbus-1/session/conf配置文件有这一行配置:

 <standard_session_servicedirs />   这个就按需启动的service文件目录:/usr/share/dbus-1/service

下面我们来做一个按需启动的例子

准备条件:参考 D-Bus 配置相关(四)最后编译部分

如果做了(四)的实验,记得删掉加的<deny send_destination="hello.world.service"/> 

1. 进入/usr/share/dbus-1/service

创建hello.world.service文件,添加下列配置

[D-BUS Service]
Name=hello.world.service
Exec=/home/june/document/comb/service

Name为提供服务的bus name

Exec为可执行文件的执行路径

2.查看服务是否被DBus-daemon识别

june@june:~/document/comb$ dbus-send --session --print-reply --dest=org.freedesktop.DBus / org.freedesktop.DBus.ListActivatableNames
method return sender=org.freedesktop.DBus -> dest=:1.8 reply_serial=2
   array [
      string "org.freedesktop.DBus"
      string "org.freedesktop.Notifications"
      string "com.canonical.Unity.Scope.Music.Musique"
      string "org.freedesktop.Telepathy.Client.Empathy.FileTransfer"
      string "com.canonical.Unity.Scope.Notes.Tomboy"
      string "com.canonical.Unity.Scope.Help.Texdoc"
      string "org.freedesktop.Telepathy.Client.Logger"
      string "com.canonical.webcredentials.capture"
      string "org.gtk.Private.AfcVolumeMonitor"
      string "com.canonical.Unity.Scope.Music.Gmusicbrowser"
      string "com.canonical.Unity.Scope.Info.Calculator"
      string "com.canonical.Unity.Scope.Graphics.Colourlovers"
      string "org.freedesktop.ColorHelper"
      string "org.fedoraproject.Config.Printing"
      string "org.gtk.vfs.Daemon"
      string "com.canonical.Unity.Scope.Music.Clementine"
      string "org.onboard.WordPrediction"
      string "com.canonical.Unity.Scope.Music"
      string "com.canonical.Unity.Webapps.Service"
      string "org.freedesktop.Geoclue.Master"
      string "com.canonical.hud"
      string "org.gnome.evolution.dataserver.Sources2"
      string "com.ubuntu.OneConf"
      string "org.freedesktop.Telepathy.Client.Empathy.Chat"
      string "com.ubuntu.SoftwareCenterDataProvider"
      string "com.nokia.singlesignonui"
      string "org.freedesktop.Telepathy.ConnectionManager.salut"
      string "com.canonical.Unity.Voice"
      string "org.gnome.GConf"
      string "com.canonical.Unity.Scope.Music.Audacious"
      string "com.nokia.SingleSignOn.Backup"
      string "org.a11y.Bus"
      string "com.ubuntu.sso"
      string "com.canonical.Unity.Scope.MusicStore"
      string "org.gnome.keyring"
      string "org.freedesktop.Telepathy.Client.Empathy.Call"
      string "net.launchpad.scope.RemoteVideos"
      string "org.freedesktop.PackageKit"
      string "com.canonical.Unity.Scope.Photos.Facebook"
      string "org.openobex"
      string "com.canonical.Unity.Scope.Photos.Flickr"
      string "com.canonical.Unity.Scope.Photos.Shotwell"
      string "org.gtk.Private.MTPVolumeMonitor"
      string "org.freedesktop.Telepathy.ConnectionManager.gabble"
      string "ca.desrt.dconf"
      string "org.a11y.atspi.Registry"
      string "org.gnome.evolution.dataserver.UserPrompter0"
      string "org.openobex.client"
      string "org.freedesktop.Telepathy.ConnectionManager.idle"
      string "org.gnome.evolution.dataserver.Calendar4"
      string "com.canonical.Unity.Scope.Graphics.Openclipart"
      string "org.freedesktop.Telepathy.Client.Empathy.Auth"
      string "com.google.code.AccountsSSO.SingleSignOn"
      string "org.gnome.evince.Daemon"
      string "com.canonical.Unity.Scope.Help.Yelp"
      string "com.canonical.Unity.Scope.LocalFiles"
      string "com.canonical.Friends.Dispatcher"
      string "org.ayatana.bamf"
      string "com.canonical.Unity.Scope.Music.Guayadeque"
      string "org.gnome.seahorse.Application"
      string "org.freedesktop.FileManager1"
      string "net.launchpad.scope.LocalVideos"
      string "org.freedesktop.Telepathy.Client.Vino"
      string "com.canonical.indicators.webcredentials"
      string "org.gnome.Nautilus"
      string "org.freedesktop.Telepathy.AccountManager"
      string "org.gtk.GLib.PACRunner"
      string "com.canonical.indicator.keyboard"
      string "org.gnome.ArchiveManager1"
      string "org.gnome.evolution.dataserver.AddressBook5"
      string "org.freedesktop.Telepathy.ConnectionManager.haze"
      string "com.canonical.Unity.Scope.Boxes.Virtualbox"
      string "com.canonical.Unity.Lens.Music.PreviewPlayer"
      string "com.canonical.Unity.Scope.Academic.Zotero"
      string "org.gnome.ScreenSaver"
      string "org.gnome.zeitgeist.Engine"
      string "com.canonical.Unity.Scope.Webhistory.Chromiumbookmarks"
      string "org.gtk.Private.UDisks2VolumeMonitor"
      string "com.canonical.Unity.Scope.Recipes.Gourmet"
      string "org.gtk.vfs.Metadata"
      string "com.canonical.Friends.Service"
      string "com.canonical.certification.PlainBox1"
      string "org.gnome.Rhythmbox3"
      string "org.gnome.keyring.SystemPrompter"
      string "com.canonical.Unity.Scope.Development.Manpages"
      string "org.freedesktop.Telepathy.MissionControl5"
      string "com.canonical.Unity.Scope.Webhistory.Firefoxbookmarks"
      string "com.canonical.Unity.Scope.Developer.Devhelp"
      string "com.canonical.Unity.Scope.Photos.Picasa"
      string "com.canonical.Unity.Scope.Home"
      string "org.gnome.gedit"
      string "org.freedesktop.Geoclue.Providers.UbuntuGeoIP"
      string "org.freedesktop.secrets"
      string "com.canonical.Unity.Scope.Friends"
      string "com.canonical.Unity.Scope.File.Gdrive"
      string "org.freedesktop.Telepathy.Logger"
      string "org.gnome.Contacts.SearchProvider"
      string "org.gnome.Nautilus.SearchProvider"
      string "org.gnome.zeitgeist.SimpleIndexer"
      string "org.gnome.keyring.PrivatePrompter"
      string "com.canonical.Unity.Scope.Applications"
      string "hello.world.service"
      string "org.gtk.Private.GPhoto2VolumeMonitor"
   ]

此时已可以识别些服务,现在只需要执行client就行,而不必先执行servcie & 然后执行client, 是不是很方便.

三.如果不写hello.world.service配置,直接执行client,会发生如下错误

Name error: org.freedesktop.DBus.Error.ServiceUnknown -- The name hello.world.service was not provided by any .service files


但需要在 client.c 源码中添加一句才可以打印出来发生什么错误.

在request_name调用下,添加如下代码,即可获取service是否存在.或已启动

        ret = dbus_bus_start_service_by_name(connection, "hello.world.service", 0, NULL, &err);
        if(dbus_error_is_set(&err))
        {
                printf("Name error: %s -- %s\n", err.name, err.message);
                dbus_error_free(&err);
                return NULL;
        }
        if(ret == 0)
        {
                printf("start hello.world.sercie service is failed!!\n");
        }

->上面这个代码加不加都可以自启动service

->自启动的发生,肯定是发生一个method,而不是signal,因为signal没有指定bus name.


猜你喜欢

转载自blog.csdn.net/u012385733/article/details/80884372