SessionBus.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "SessionBus.h"
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <filesystem>
  5. #include <fstream>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <sys/time.h>
  9. #include <sys/un.h>
  10. #include <system_error>
  11. #include <unistd.h>
  12. namespace {
  13. constexpr int CALL_TIMEOUT_MS = 2000;
  14. /** per-frame cap, a read pulls ~2KB at most */
  15. constexpr int MAX_READS_PER_DRAIN = 512;
  16. }
  17. bool WallpaperEngine::Desktop::desktopListHas (std::string_view name) {
  18. const char* desktops = std::getenv ("XDG_CURRENT_DESKTOP");
  19. if (desktops == nullptr) {
  20. return false;
  21. }
  22. std::string_view rest (desktops);
  23. while (!rest.empty ()) {
  24. const auto colon = rest.find (':');
  25. if (rest.substr (0, colon) == name) {
  26. return true;
  27. }
  28. if (colon == std::string_view::npos) {
  29. break;
  30. }
  31. rest.remove_prefix (colon + 1);
  32. }
  33. return false;
  34. }
  35. std::string WallpaperEngine::Desktop::runtimeDirectory () {
  36. const char* runtimeDir = std::getenv ("XDG_RUNTIME_DIR");
  37. if (runtimeDir == nullptr) {
  38. return {};
  39. }
  40. std::string dir = runtimeDir;
  41. // Flatpak shares only the app's runtime folder; FLATPAK_ID alone can be inherited
  42. if (const char* flatpakId = std::getenv ("FLATPAK_ID");
  43. flatpakId != nullptr && access ("/.flatpak-info", F_OK) == 0) {
  44. const std::string appDir = dir + "/app/" + flatpakId;
  45. std::error_code error;
  46. if (std::filesystem::is_directory (appDir, error)) {
  47. dir = appDir;
  48. }
  49. }
  50. return dir;
  51. }
  52. std::string WallpaperEngine::Desktop::flatpakInfo (const std::string& key) {
  53. std::ifstream info ("/.flatpak-info");
  54. std::string line;
  55. while (std::getline (info, line)) {
  56. if (line.starts_with (key + "=")) {
  57. return line.substr (key.size () + 1);
  58. }
  59. }
  60. return {};
  61. }
  62. std::string WallpaperEngine::Desktop::processTag () {
  63. static const std::string instance = [] {
  64. const std::string id = flatpakInfo ("instance-id");
  65. return id.empty () ? id : id + "_";
  66. }();
  67. return instance + std::to_string (getpid ());
  68. }
  69. DBusMessage*
  70. WallpaperEngine::Desktop::callBlocking (DBusConnection* connection, DBusMessage* call, std::string& error) {
  71. DBusError dbusError;
  72. dbus_error_init (&dbusError);
  73. DBusMessage* reply = dbus_connection_send_with_reply_and_block (connection, call, CALL_TIMEOUT_MS, &dbusError);
  74. dbus_message_unref (call);
  75. if (dbus_error_is_set (&dbusError)) {
  76. error = dbusError.message;
  77. dbus_error_free (&dbusError);
  78. return nullptr;
  79. }
  80. return reply;
  81. }
  82. bool WallpaperEngine::Desktop::drainBus (DBusConnection* connection) {
  83. for (int reads = 0; reads < MAX_READS_PER_DRAIN; reads++) {
  84. if (!dbus_connection_read_write (connection, 0)) {
  85. return false;
  86. }
  87. bool dispatched = false;
  88. while (dbus_connection_get_dispatch_status (connection) == DBUS_DISPATCH_DATA_REMAINS) {
  89. dbus_connection_dispatch (connection);
  90. dispatched = true;
  91. }
  92. if (!dispatched) {
  93. break;
  94. }
  95. }
  96. return true;
  97. }
  98. DBusMessage* WallpaperEngine::Desktop::getProperty (
  99. DBusConnection* connection, const char* service, const char* path, const char* interface, const char* property,
  100. bool autoStart
  101. ) {
  102. DBusMessage* call = dbus_message_new_method_call (service, path, "org.freedesktop.DBus.Properties", "Get");
  103. dbus_message_set_auto_start (call, autoStart ? TRUE : FALSE);
  104. dbus_message_append_args (call, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID);
  105. std::string error;
  106. return callBlocking (connection, call, error);
  107. }
  108. bool WallpaperEngine::Desktop::propertyValue (DBusMessage* reply, DBusMessageIter& value) {
  109. DBusMessageIter iter;
  110. if (!dbus_message_iter_init (reply, &iter) || dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_VARIANT) {
  111. return false;
  112. }
  113. dbus_message_iter_recurse (&iter, &value);
  114. return true;
  115. }
  116. DBusConnection* WallpaperEngine::Desktop::privateBus (DBusBusType type) {
  117. DBusError error;
  118. dbus_error_init (&error);
  119. DBusConnection* bus = dbus_bus_get_private (type, &error);
  120. if (dbus_error_is_set (&error)) {
  121. dbus_error_free (&error);
  122. return nullptr;
  123. }
  124. dbus_connection_set_exit_on_disconnect (bus, FALSE);
  125. return bus;
  126. }
  127. void WallpaperEngine::Desktop::closeBus (DBusConnection* bus) {
  128. if (bus != nullptr) {
  129. dbus_connection_close (bus);
  130. dbus_connection_unref (bus);
  131. }
  132. }
  133. int WallpaperEngine::Desktop::receiveDescriptor (int connection) {
  134. char byte = 0;
  135. iovec data = { .iov_base = &byte, .iov_len = 1 };
  136. union {
  137. char buffer[CMSG_SPACE (sizeof (int))];
  138. cmsghdr align;
  139. } control {};
  140. msghdr message = {};
  141. message.msg_iov = &data;
  142. message.msg_iovlen = 1;
  143. message.msg_control = control.buffer;
  144. message.msg_controllen = sizeof (control.buffer);
  145. if (recvmsg (connection, &message, MSG_CMSG_CLOEXEC) != 1) {
  146. return -1;
  147. }
  148. const cmsghdr* header = CMSG_FIRSTHDR (&message);
  149. if (header == nullptr || header->cmsg_level != SOL_SOCKET || header->cmsg_type != SCM_RIGHTS) {
  150. return -1;
  151. }
  152. int fd = -1;
  153. std::memcpy (&fd, CMSG_DATA (header), sizeof (int));
  154. return fd;
  155. }
  156. bool WallpaperEngine::Desktop::connectToService (
  157. DBusConnection*& connection, const char* busName, std::string& owner, std::string& reason,
  158. const std::string& notRunning
  159. ) {
  160. DBusError error;
  161. dbus_error_init (&error);
  162. // private, so we never eat messages meant for the shared connection
  163. connection = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  164. if (dbus_error_is_set (&error)) {
  165. reason = std::string ("no session bus: ") + error.message;
  166. dbus_error_free (&error);
  167. connection = nullptr;
  168. return false;
  169. }
  170. dbus_connection_set_exit_on_disconnect (connection, false);
  171. DBusMessage* message = dbus_message_new_method_call (
  172. "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetNameOwner"
  173. );
  174. dbus_message_append_args (message, DBUS_TYPE_STRING, &busName, DBUS_TYPE_INVALID);
  175. std::string callError;
  176. DBusMessage* reply = callBlocking (connection, message, callError);
  177. if (reply == nullptr) {
  178. reason = notRunning + " (" + callError + ")";
  179. return false;
  180. }
  181. const char* name = nullptr;
  182. dbus_message_get_args (reply, nullptr, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
  183. owner = name != nullptr ? name : "";
  184. dbus_message_unref (reply);
  185. return true;
  186. }
  187. int WallpaperEngine::Desktop::listenOnSocket (const std::string& path, std::string& reason) {
  188. sockaddr_un address = {};
  189. address.sun_family = AF_UNIX;
  190. if (path.size () >= sizeof (address.sun_path)) {
  191. reason = "the runtime folder's path is too long for a socket";
  192. return -1;
  193. }
  194. std::strncpy (address.sun_path, path.c_str (), sizeof (address.sun_path) - 1);
  195. unlink (path.c_str ());
  196. const int listener = socket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
  197. const mode_t mask = umask (0077);
  198. const bool bound = listener >= 0 && bind (listener, reinterpret_cast<sockaddr*> (&address), sizeof (address)) == 0
  199. && listen (listener, 1) == 0;
  200. umask (mask);
  201. if (!bound) {
  202. reason = "cannot listen on " + path + ": " + std::error_code (errno, std::generic_category ()).message ();
  203. if (listener >= 0) {
  204. close (listener);
  205. }
  206. unlink (path.c_str ());
  207. return -1;
  208. }
  209. return listener;
  210. }
  211. int WallpaperEngine::Desktop::connectUnixSocket (const std::string& socketPath, const long timeoutMicros) {
  212. sockaddr_un addr {};
  213. if (socketPath.size () >= sizeof (addr.sun_path)) {
  214. return -1;
  215. }
  216. const int fd = socket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
  217. if (fd < 0) {
  218. return -1;
  219. }
  220. timeval timeout {};
  221. timeout.tv_sec = timeoutMicros / 1000000;
  222. timeout.tv_usec = timeoutMicros % 1000000;
  223. addr.sun_family = AF_UNIX;
  224. std::strncpy (addr.sun_path, socketPath.c_str (), sizeof (addr.sun_path) - 1);
  225. if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout)) != 0
  226. || setsockopt (fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof (timeout)) != 0
  227. || connect (fd, reinterpret_cast<sockaddr*> (&addr), sizeof (addr)) != 0) {
  228. close (fd);
  229. return -1;
  230. }
  231. return fd;
  232. }