You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_DBUS_GLIB
  28. #include "talk/base/dbus.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/thread.h"
  31. namespace talk_base {
  32. // Avoid static object construction/destruction on startup/shutdown.
  33. static pthread_once_t g_dbus_init_once = PTHREAD_ONCE_INIT;
  34. static LibDBusGlibSymbolTable *g_dbus_symbol = NULL;
  35. // Releases DBus-Glib symbols.
  36. static void ReleaseDBusGlibSymbol() {
  37. if (g_dbus_symbol != NULL) {
  38. delete g_dbus_symbol;
  39. g_dbus_symbol = NULL;
  40. }
  41. }
  42. // Loads DBus-Glib symbols.
  43. static void InitializeDBusGlibSymbol() {
  44. // This is thread safe.
  45. if (NULL == g_dbus_symbol) {
  46. g_dbus_symbol = new LibDBusGlibSymbolTable();
  47. // Loads dbus-glib
  48. if (NULL == g_dbus_symbol || !g_dbus_symbol->Load()) {
  49. LOG(LS_WARNING) << "Failed to load dbus-glib symbol table.";
  50. ReleaseDBusGlibSymbol();
  51. } else {
  52. // Nothing we can do if atexit() failed. Just ignore its returned value.
  53. atexit(ReleaseDBusGlibSymbol);
  54. }
  55. }
  56. }
  57. inline static LibDBusGlibSymbolTable *GetSymbols() {
  58. return DBusMonitor::GetDBusGlibSymbolTable();
  59. }
  60. // Implementation of class DBusSigMessageData
  61. DBusSigMessageData::DBusSigMessageData(DBusMessage *message)
  62. : TypedMessageData<DBusMessage *>(message) {
  63. GetSymbols()->dbus_message_ref()(data());
  64. }
  65. DBusSigMessageData::~DBusSigMessageData() {
  66. GetSymbols()->dbus_message_unref()(data());
  67. }
  68. // Implementation of class DBusSigFilter
  69. // Builds a DBus filter string from given DBus path, interface and member.
  70. std::string DBusSigFilter::BuildFilterString(const std::string &path,
  71. const std::string &interface,
  72. const std::string &member) {
  73. std::string ret(DBUS_TYPE "='" DBUS_SIGNAL "'");
  74. if (!path.empty()) {
  75. ret += ("," DBUS_PATH "='");
  76. ret += path;
  77. ret += "'";
  78. }
  79. if (!interface.empty()) {
  80. ret += ("," DBUS_INTERFACE "='");
  81. ret += interface;
  82. ret += "'";
  83. }
  84. if (!member.empty()) {
  85. ret += ("," DBUS_MEMBER "='");
  86. ret += member;
  87. ret += "'";
  88. }
  89. return ret;
  90. }
  91. // Forwards the message to the given instance.
  92. DBusHandlerResult DBusSigFilter::DBusCallback(DBusConnection *dbus_conn,
  93. DBusMessage *message,
  94. void *instance) {
  95. ASSERT(instance);
  96. if (instance) {
  97. return static_cast<DBusSigFilter *>(instance)->Callback(message);
  98. }
  99. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  100. }
  101. // Posts a message to caller thread.
  102. DBusHandlerResult DBusSigFilter::Callback(DBusMessage *message) {
  103. if (caller_thread_) {
  104. caller_thread_->Post(this, DSM_SIGNAL, new DBusSigMessageData(message));
  105. }
  106. // Don't "eat" the message here. Let it pop up.
  107. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  108. }
  109. // From MessageHandler.
  110. void DBusSigFilter::OnMessage(Message *message) {
  111. if (message != NULL && DSM_SIGNAL == message->message_id) {
  112. DBusSigMessageData *msg =
  113. static_cast<DBusSigMessageData *>(message->pdata);
  114. if (msg) {
  115. ProcessSignal(msg->data());
  116. delete msg;
  117. }
  118. }
  119. }
  120. // Definition of private class DBusMonitoringThread.
  121. // It creates a worker-thread to listen signals on DBus. The worker-thread will
  122. // be running in a priate GMainLoop forever until either Stop() has been invoked
  123. // or it hits an error.
  124. class DBusMonitor::DBusMonitoringThread : public talk_base::Thread {
  125. public:
  126. explicit DBusMonitoringThread(DBusMonitor *monitor,
  127. GMainContext *context,
  128. GMainLoop *mainloop,
  129. std::vector<DBusSigFilter *> *filter_list)
  130. : monitor_(monitor),
  131. context_(context),
  132. mainloop_(mainloop),
  133. connection_(NULL),
  134. idle_source_(NULL),
  135. filter_list_(filter_list) {
  136. ASSERT(monitor_);
  137. ASSERT(context_);
  138. ASSERT(mainloop_);
  139. ASSERT(filter_list_);
  140. }
  141. // Override virtual method of Thread. Context: worker-thread.
  142. virtual void Run() {
  143. ASSERT(NULL == connection_);
  144. // Setup DBus connection and start monitoring.
  145. monitor_->OnMonitoringStatusChanged(DMS_INITIALIZING);
  146. if (!Setup()) {
  147. LOG(LS_ERROR) << "DBus monitoring setup failed.";
  148. monitor_->OnMonitoringStatusChanged(DMS_FAILED);
  149. CleanUp();
  150. return;
  151. }
  152. monitor_->OnMonitoringStatusChanged(DMS_RUNNING);
  153. GetSymbols()->g_main_loop_run()(mainloop_);
  154. monitor_->OnMonitoringStatusChanged(DMS_STOPPED);
  155. // Done normally. Clean up DBus connection.
  156. CleanUp();
  157. return;
  158. }
  159. // Override virtual method of Thread. Context: caller-thread.
  160. virtual void Stop() {
  161. ASSERT(NULL == idle_source_);
  162. // Add an idle source and let the gmainloop quit on idle.
  163. idle_source_ = GetSymbols()->g_idle_source_new()();
  164. if (idle_source_) {
  165. GetSymbols()->g_source_set_callback()(idle_source_, &Idle, this, NULL);
  166. GetSymbols()->g_source_attach()(idle_source_, context_);
  167. } else {
  168. LOG(LS_ERROR) << "g_idle_source_new() failed.";
  169. QuitGMainloop(); // Try to quit anyway.
  170. }
  171. Thread::Stop(); // Wait for the thread.
  172. }
  173. private:
  174. // Registers all DBus filters.
  175. void RegisterAllFilters() {
  176. ASSERT(NULL != GetSymbols()->dbus_g_connection_get_connection()(
  177. connection_));
  178. for (std::vector<DBusSigFilter *>::iterator it = filter_list_->begin();
  179. it != filter_list_->end(); ++it) {
  180. DBusSigFilter *filter = (*it);
  181. if (!filter) {
  182. LOG(LS_ERROR) << "DBusSigFilter list corrupted.";
  183. continue;
  184. }
  185. GetSymbols()->dbus_bus_add_match()(
  186. GetSymbols()->dbus_g_connection_get_connection()(connection_),
  187. filter->filter().c_str(), NULL);
  188. if (!GetSymbols()->dbus_connection_add_filter()(
  189. GetSymbols()->dbus_g_connection_get_connection()(connection_),
  190. &DBusSigFilter::DBusCallback, filter, NULL)) {
  191. LOG(LS_ERROR) << "dbus_connection_add_filter() failed."
  192. << "Filter: " << filter->filter();
  193. continue;
  194. }
  195. }
  196. }
  197. // Unregisters all DBus filters.
  198. void UnRegisterAllFilters() {
  199. ASSERT(NULL != GetSymbols()->dbus_g_connection_get_connection()(
  200. connection_));
  201. for (std::vector<DBusSigFilter *>::iterator it = filter_list_->begin();
  202. it != filter_list_->end(); ++it) {
  203. DBusSigFilter *filter = (*it);
  204. if (!filter) {
  205. LOG(LS_ERROR) << "DBusSigFilter list corrupted.";
  206. continue;
  207. }
  208. GetSymbols()->dbus_connection_remove_filter()(
  209. GetSymbols()->dbus_g_connection_get_connection()(connection_),
  210. &DBusSigFilter::DBusCallback, filter);
  211. }
  212. }
  213. // Sets up the monitoring thread.
  214. bool Setup() {
  215. GetSymbols()->g_main_context_push_thread_default()(context_);
  216. // Start connection to dbus.
  217. // If dbus daemon is not running, returns false immediately.
  218. connection_ = GetSymbols()->dbus_g_bus_get_private()(monitor_->type_,
  219. context_, NULL);
  220. if (NULL == connection_) {
  221. LOG(LS_ERROR) << "dbus_g_bus_get_private() unable to get connection.";
  222. return false;
  223. }
  224. if (NULL == GetSymbols()->dbus_g_connection_get_connection()(connection_)) {
  225. LOG(LS_ERROR) << "dbus_g_connection_get_connection() returns NULL. "
  226. << "DBus daemon is probably not running.";
  227. return false;
  228. }
  229. // Application don't exit if DBus daemon die.
  230. GetSymbols()->dbus_connection_set_exit_on_disconnect()(
  231. GetSymbols()->dbus_g_connection_get_connection()(connection_), FALSE);
  232. // Connect all filters.
  233. RegisterAllFilters();
  234. return true;
  235. }
  236. // Cleans up the monitoring thread.
  237. void CleanUp() {
  238. if (idle_source_) {
  239. // We did an attach() with the GSource, so we need to destroy() it.
  240. GetSymbols()->g_source_destroy()(idle_source_);
  241. // We need to unref() the GSource to end the last reference we got.
  242. GetSymbols()->g_source_unref()(idle_source_);
  243. idle_source_ = NULL;
  244. }
  245. if (connection_) {
  246. if (GetSymbols()->dbus_g_connection_get_connection()(connection_)) {
  247. UnRegisterAllFilters();
  248. GetSymbols()->dbus_connection_close()(
  249. GetSymbols()->dbus_g_connection_get_connection()(connection_));
  250. }
  251. GetSymbols()->dbus_g_connection_unref()(connection_);
  252. connection_ = NULL;
  253. }
  254. GetSymbols()->g_main_loop_unref()(mainloop_);
  255. mainloop_ = NULL;
  256. GetSymbols()->g_main_context_unref()(context_);
  257. context_ = NULL;
  258. }
  259. // Handles callback on Idle. We only add this source when ready to stop.
  260. static gboolean Idle(gpointer data) {
  261. static_cast<DBusMonitoringThread *>(data)->QuitGMainloop();
  262. return TRUE;
  263. }
  264. // We only hit this when ready to quit.
  265. void QuitGMainloop() {
  266. GetSymbols()->g_main_loop_quit()(mainloop_);
  267. }
  268. DBusMonitor *monitor_;
  269. GMainContext *context_;
  270. GMainLoop *mainloop_;
  271. DBusGConnection *connection_;
  272. GSource *idle_source_;
  273. std::vector<DBusSigFilter *> *filter_list_;
  274. };
  275. // Implementation of class DBusMonitor
  276. // Returns DBus-Glib symbol handle. Initialize it first if hasn't.
  277. LibDBusGlibSymbolTable *DBusMonitor::GetDBusGlibSymbolTable() {
  278. // This is multi-thread safe.
  279. pthread_once(&g_dbus_init_once, InitializeDBusGlibSymbol);
  280. return g_dbus_symbol;
  281. };
  282. // Creates an instance of DBusMonitor
  283. DBusMonitor *DBusMonitor::Create(DBusBusType type) {
  284. if (NULL == DBusMonitor::GetDBusGlibSymbolTable()) {
  285. return NULL;
  286. }
  287. return new DBusMonitor(type);
  288. }
  289. DBusMonitor::DBusMonitor(DBusBusType type)
  290. : type_(type),
  291. status_(DMS_NOT_INITIALIZED),
  292. monitoring_thread_(NULL) {
  293. ASSERT(type_ == DBUS_BUS_SYSTEM || type_ == DBUS_BUS_SESSION);
  294. }
  295. DBusMonitor::~DBusMonitor() {
  296. StopMonitoring();
  297. }
  298. bool DBusMonitor::AddFilter(DBusSigFilter *filter) {
  299. if (monitoring_thread_) {
  300. return false;
  301. }
  302. if (!filter) {
  303. return false;
  304. }
  305. filter_list_.push_back(filter);
  306. return true;
  307. }
  308. bool DBusMonitor::StartMonitoring() {
  309. if (!monitoring_thread_) {
  310. GetSymbols()->g_type_init()();
  311. GetSymbols()->g_thread_init()(NULL);
  312. GetSymbols()->dbus_g_thread_init()();
  313. GMainContext *context = GetSymbols()->g_main_context_new()();
  314. if (NULL == context) {
  315. LOG(LS_ERROR) << "g_main_context_new() failed.";
  316. return false;
  317. }
  318. GMainLoop *mainloop = GetSymbols()->g_main_loop_new()(context, FALSE);
  319. if (NULL == mainloop) {
  320. LOG(LS_ERROR) << "g_main_loop_new() failed.";
  321. GetSymbols()->g_main_context_unref()(context);
  322. return false;
  323. }
  324. monitoring_thread_ = new DBusMonitoringThread(this, context, mainloop,
  325. &filter_list_);
  326. if (monitoring_thread_ == NULL) {
  327. LOG(LS_ERROR) << "Failed to create DBus monitoring thread.";
  328. GetSymbols()->g_main_context_unref()(context);
  329. GetSymbols()->g_main_loop_unref()(mainloop);
  330. return false;
  331. }
  332. monitoring_thread_->Start();
  333. }
  334. return true;
  335. }
  336. bool DBusMonitor::StopMonitoring() {
  337. if (monitoring_thread_) {
  338. monitoring_thread_->Stop();
  339. monitoring_thread_ = NULL;
  340. }
  341. return true;
  342. }
  343. DBusMonitor::DBusMonitorStatus DBusMonitor::GetStatus() {
  344. return status_;
  345. }
  346. void DBusMonitor::OnMonitoringStatusChanged(DBusMonitorStatus status) {
  347. status_ = status;
  348. }
  349. #undef LATE
  350. } // namespace talk_base
  351. #endif // HAVE_DBUS_GLIB