{"id":2267,"date":"2016-06-26T01:02:31","date_gmt":"2016-06-25T17:02:31","guid":{"rendered":"https:\/\/www.csslayer.info\/wordpress\/?p=2267"},"modified":"2016-06-26T01:02:31","modified_gmt":"2016-06-25T17:02:31","slug":"one-bad-api-in-sd-bus","status":"publish","type":"post","link":"https:\/\/www.csslayer.info\/wordpress\/linux\/one-bad-api-in-sd-bus\/","title":{"rendered":"One bad API in sd-bus"},"content":{"rendered":"<p>I&#8217;m using sd-bus to develop the dbus part for fcitx5, because I&#8217;d like to seamlessly support kdbus in the future. (Don&#8217;t argue with me about bsd or sth else right now, for now I don&#8217;t care about that.)<\/p>\n<p>But one api of sd-bus obviously blows up my mind.<\/p>\n<p>https:\/\/github.com\/systemd\/systemd\/blob\/09541e49ebd17b41482e447dd8194942f39788c0\/src\/systemd\/sd-bus.h#L428<\/p>\n<pre><span class=\"pl-k\">typedef<\/span> <span class=\"pl-en\">int<\/span> (*<span class=\"pl-c1\">sd_bus_track_handler_t<\/span>) (sd_bus_track *track, <span class=\"pl-k\">void<\/span> *userdata);\r\n<span class=\"pl-k\">int<\/span> <span class=\"pl-en\">sd_bus_track_new<\/span>(sd_bus *bus, sd_bus_track **track, <span class=\"pl-c1\">sd_bus_track_handler_t<\/span> handler, <span class=\"pl-k\">void<\/span> *userdata);<\/pre>\n<p>So basically, it&#8217;s a wrapper around NameOwnerChanged dbus signal. And let you get notification about when the service disappears (only disappears, yeah).<\/p>\n<p>And it also has capability to track more than one service name by using.<\/p>\n<pre>int sd_bus_track_add_sender(sd_bus_track *track, sd_bus_message *m);\r\nint sd_bus_track_remove_sender(sd_bus_track *track, sd_bus_message *m);\r\nint sd_bus_track_add_name(sd_bus_track *track, const char *name);\r\nint sd_bus_track_remove_name(sd_bus_track *track, const char *name);<\/pre>\n<p>And you are able to query which name is being tracked via:<\/p>\n<pre>unsigned sd_bus_track_count(sd_bus_track *track);\r\nconst char* sd_bus_track_contains(sd_bus_track *track, const char *names);\r\nconst char* sd_bus_track_first(sd_bus_track *track);\r\nconst char* sd_bus_track_next(sd_bus_track *track);<\/pre>\n<p>So far so good, yeah?<\/p>\n<p>But if you pay attention to the prototype of sd_bus_track_handler_t, you&#8217;ll notice that you won&#8217;t be able to get the tracked service name when this handler being called.<\/p>\n<p>Which means you simply can&#8217;t use it to track more than one service name, because you can&#8217;t distinguish which service actually exits.<\/p>\n<p>If I checked all existing usages of sd_bus_track in systemd, they imply that my impression is correct.<\/p>\n<p>So obviously &#8220;tracking multiple name&#8221; functionality is totally useless.<\/p>\n<p>So here, this is a good example about how this api should looks like: http:\/\/doc.qt.io\/qt-5\/qdbusservicewatcher.html .<\/p>\n<p>For my own use case, I&#8217;ll go back to write my own service tracker with other sd-bus api.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using sd-bus to develop the dbus part for fcitx5, because I&#8217;d like to seamlessly support kdbus in the future. (Don&#8217;t argue with me about bsd or sth else right now, for now I don&#8217;t care about that.) But one &hellip; <a href=\"https:\/\/www.csslayer.info\/wordpress\/linux\/one-bad-api-in-sd-bus\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,5],"tags":[223,222],"class_list":["post-2267","post","type-post","status-publish","format-standard","hentry","category-fcitx-dev","category-linux","tag-sd-bus","tag-systemd"],"_links":{"self":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/2267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/comments?post=2267"}],"version-history":[{"count":1,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/2267\/revisions"}],"predecessor-version":[{"id":2268,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/2267\/revisions\/2268"}],"wp:attachment":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/media?parent=2267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/categories?post=2267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/tags?post=2267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}