jack2 codebase
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.

679 lines
15KB

  1. /***
  2. Copyright 2009 Lennart Poettering
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation files
  5. (the "Software"), to deal in the Software without restriction,
  6. including without limitation the rights to use, copy, modify, merge,
  7. publish, distribute, sublicense, and/or sell copies of the Software,
  8. and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  16. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  17. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. ***/
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include <stdint.h>
  28. #include "reserve.h"
  29. #include "jack/control.h"
  30. #define RESERVE_ERROR_NO_MEMORY "org.freedesktop.ReserveDevice1.Error.NoMemory"
  31. #define RESERVE_ERROR_PROTOCOL_VIOLATION "org.freedesktop.ReserveDevice1.Error.Protocol"
  32. #define RESERVE_ERROR_RELEASE_DENIED "org.freedesktop.ReserveDevice1.Error.ReleaseDenied"
  33. struct rd_device {
  34. int ref;
  35. char *device_name;
  36. char *application_name;
  37. char *application_device_name;
  38. char *service_name;
  39. char *object_path;
  40. int32_t priority;
  41. DBusConnection *connection;
  42. int owning:1;
  43. int registered:1;
  44. int filtering:1;
  45. int gave_up:1;
  46. rd_request_cb_t request_cb;
  47. void *userdata;
  48. };
  49. #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
  50. #define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"
  51. static const char introspection[] =
  52. DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
  53. "<node>"
  54. " <!-- If you are looking for documentation make sure to check out\n"
  55. " http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n"
  56. " <interface name=\"org.freedesktop.ReserveDevice1\">"
  57. " <method name=\"RequestRelease\">"
  58. " <arg name=\"priority\" type=\"i\" direction=\"in\"/>"
  59. " <arg name=\"result\" type=\"b\" direction=\"out\"/>"
  60. " </method>"
  61. " <property name=\"Priority\" type=\"i\" access=\"read\"/>"
  62. " <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>"
  63. " <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>"
  64. " </interface>"
  65. " <interface name=\"org.freedesktop.DBus.Properties\">"
  66. " <method name=\"Get\">"
  67. " <arg name=\"interface\" direction=\"in\" type=\"s\"/>"
  68. " <arg name=\"property\" direction=\"in\" type=\"s\"/>"
  69. " <arg name=\"value\" direction=\"out\" type=\"v\"/>"
  70. " </method>"
  71. " </interface>"
  72. " <interface name=\"org.freedesktop.DBus.Introspectable\">"
  73. " <method name=\"Introspect\">"
  74. " <arg name=\"data\" type=\"s\" direction=\"out\"/>"
  75. " </method>"
  76. " </interface>"
  77. "</node>";
  78. static dbus_bool_t add_variant(
  79. DBusMessage *m,
  80. int type,
  81. const void *data) {
  82. DBusMessageIter iter, sub;
  83. char t[2];
  84. t[0] = (char) type;
  85. t[1] = 0;
  86. dbus_message_iter_init_append(m, &iter);
  87. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub))
  88. return FALSE;
  89. if (!dbus_message_iter_append_basic(&sub, type, data))
  90. return FALSE;
  91. if (!dbus_message_iter_close_container(&iter, &sub))
  92. return FALSE;
  93. return TRUE;
  94. }
  95. static DBusHandlerResult object_handler(
  96. DBusConnection *c,
  97. DBusMessage *m,
  98. void *userdata) {
  99. rd_device *d;
  100. DBusError error;
  101. DBusMessage *reply = NULL;
  102. dbus_error_init(&error);
  103. d = (rd_device*)userdata;
  104. assert(d->ref >= 1);
  105. if (dbus_message_is_method_call(
  106. m,
  107. "org.freedesktop.ReserveDevice1",
  108. "RequestRelease")) {
  109. int32_t priority;
  110. dbus_bool_t ret;
  111. if (!dbus_message_get_args(
  112. m,
  113. &error,
  114. DBUS_TYPE_INT32, &priority,
  115. DBUS_TYPE_INVALID))
  116. goto invalid;
  117. ret = FALSE;
  118. if (priority > d->priority && d->request_cb) {
  119. d->ref++;
  120. if (d->request_cb(d, 0) > 0) {
  121. ret = TRUE;
  122. d->gave_up = 1;
  123. }
  124. rd_release(d);
  125. }
  126. if (!(reply = dbus_message_new_method_return(m)))
  127. goto oom;
  128. if (!dbus_message_append_args(
  129. reply,
  130. DBUS_TYPE_BOOLEAN, &ret,
  131. DBUS_TYPE_INVALID))
  132. goto oom;
  133. if (!dbus_connection_send(c, reply, NULL))
  134. goto oom;
  135. dbus_message_unref(reply);
  136. return DBUS_HANDLER_RESULT_HANDLED;
  137. } else if (dbus_message_is_method_call(
  138. m,
  139. "org.freedesktop.DBus.Properties",
  140. "Get")) {
  141. const char *interface, *property;
  142. if (!dbus_message_get_args(
  143. m,
  144. &error,
  145. DBUS_TYPE_STRING, &interface,
  146. DBUS_TYPE_STRING, &property,
  147. DBUS_TYPE_INVALID))
  148. goto invalid;
  149. if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) {
  150. const char *empty = "";
  151. if (strcmp(property, "ApplicationName") == 0 && d->application_name) {
  152. if (!(reply = dbus_message_new_method_return(m)))
  153. goto oom;
  154. if (!add_variant(
  155. reply,
  156. DBUS_TYPE_STRING,
  157. d->application_name ? (const char**) &d->application_name : &empty))
  158. goto oom;
  159. } else if (strcmp(property, "ApplicationDeviceName") == 0) {
  160. if (!(reply = dbus_message_new_method_return(m)))
  161. goto oom;
  162. if (!add_variant(
  163. reply,
  164. DBUS_TYPE_STRING,
  165. d->application_device_name ? (const char**) &d->application_device_name : &empty))
  166. goto oom;
  167. } else if (strcmp(property, "Priority") == 0) {
  168. if (!(reply = dbus_message_new_method_return(m)))
  169. goto oom;
  170. if (!add_variant(
  171. reply,
  172. DBUS_TYPE_INT32,
  173. &d->priority))
  174. goto oom;
  175. } else {
  176. if (!(reply = dbus_message_new_error_printf(
  177. m,
  178. DBUS_ERROR_UNKNOWN_METHOD,
  179. "Unknown property %s",
  180. property)))
  181. goto oom;
  182. }
  183. if (!dbus_connection_send(c, reply, NULL))
  184. goto oom;
  185. dbus_message_unref(reply);
  186. return DBUS_HANDLER_RESULT_HANDLED;
  187. }
  188. } else if (dbus_message_is_method_call(
  189. m,
  190. "org.freedesktop.DBus.Introspectable",
  191. "Introspect")) {
  192. const char *i = introspection;
  193. if (!(reply = dbus_message_new_method_return(m)))
  194. goto oom;
  195. if (!dbus_message_append_args(
  196. reply,
  197. DBUS_TYPE_STRING,
  198. &i,
  199. DBUS_TYPE_INVALID))
  200. goto oom;
  201. if (!dbus_connection_send(c, reply, NULL))
  202. goto oom;
  203. dbus_message_unref(reply);
  204. return DBUS_HANDLER_RESULT_HANDLED;
  205. }
  206. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  207. invalid:
  208. if (reply)
  209. dbus_message_unref(reply);
  210. if (!(reply = dbus_message_new_error(
  211. m,
  212. DBUS_ERROR_INVALID_ARGS,
  213. "Invalid arguments")))
  214. goto oom;
  215. if (!dbus_connection_send(c, reply, NULL))
  216. goto oom;
  217. dbus_message_unref(reply);
  218. dbus_error_free(&error);
  219. return DBUS_HANDLER_RESULT_HANDLED;
  220. oom:
  221. if (reply)
  222. dbus_message_unref(reply);
  223. dbus_error_free(&error);
  224. return DBUS_HANDLER_RESULT_NEED_MEMORY;
  225. }
  226. static DBusHandlerResult filter_handler(
  227. DBusConnection *c,
  228. DBusMessage *m,
  229. void *userdata) {
  230. DBusMessage *reply;
  231. rd_device *d;
  232. DBusError error;
  233. dbus_error_init(&error);
  234. d = (rd_device*)userdata;
  235. if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
  236. const char *name;
  237. if (!dbus_message_get_args(
  238. m,
  239. &error,
  240. DBUS_TYPE_STRING, &name,
  241. DBUS_TYPE_INVALID))
  242. goto invalid;
  243. if (strcmp(name, d->service_name) == 0 && d->owning) {
  244. d->owning = 0;
  245. if (!d->gave_up) {
  246. d->ref++;
  247. if (d->request_cb)
  248. d->request_cb(d, 1);
  249. d->gave_up = 1;
  250. rd_release(d);
  251. }
  252. return DBUS_HANDLER_RESULT_HANDLED;
  253. }
  254. }
  255. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  256. invalid:
  257. if (!(reply = dbus_message_new_error(
  258. m,
  259. DBUS_ERROR_INVALID_ARGS,
  260. "Invalid arguments")))
  261. goto oom;
  262. if (!dbus_connection_send(c, reply, NULL))
  263. goto oom;
  264. dbus_message_unref(reply);
  265. dbus_error_free(&error);
  266. return DBUS_HANDLER_RESULT_HANDLED;
  267. oom:
  268. if (reply)
  269. dbus_message_unref(reply);
  270. dbus_error_free(&error);
  271. return DBUS_HANDLER_RESULT_NEED_MEMORY;
  272. }
  273. static DBusObjectPathVTable vtable;
  274. int rd_acquire(
  275. rd_device **_d,
  276. DBusConnection *connection,
  277. const char *device_name,
  278. const char *application_name,
  279. int32_t priority,
  280. rd_request_cb_t request_cb,
  281. DBusError *error) {
  282. rd_device *d = NULL;
  283. int r, k;
  284. DBusError _error;
  285. DBusMessage *m = NULL, *reply = NULL;
  286. dbus_bool_t good;
  287. vtable.message_function = object_handler;
  288. if (!error) {
  289. error = &_error;
  290. dbus_error_init(error);
  291. }
  292. if (!_d) {
  293. assert(0);
  294. r = -EINVAL;
  295. goto fail;
  296. }
  297. if (!connection) {
  298. assert(0);
  299. r = -EINVAL;
  300. goto fail;
  301. }
  302. if (!device_name) {
  303. assert(0);
  304. r = -EINVAL;
  305. goto fail;
  306. }
  307. if (!request_cb && priority != INT32_MAX) {
  308. assert(0);
  309. r = -EINVAL;
  310. goto fail;
  311. }
  312. if (!(d = (rd_device *)calloc(sizeof(rd_device), 1))) {
  313. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot allocate memory for rd_device struct");
  314. r = -ENOMEM;
  315. goto fail;
  316. }
  317. d->ref = 1;
  318. if (!(d->device_name = strdup(device_name))) {
  319. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot duplicate device name string");
  320. r = -ENOMEM;
  321. goto fail;
  322. }
  323. if (!(d->application_name = strdup(application_name))) {
  324. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot duplicate application name string");
  325. r = -ENOMEM;
  326. goto fail;
  327. }
  328. d->priority = priority;
  329. d->connection = dbus_connection_ref(connection);
  330. d->request_cb = request_cb;
  331. if (!(d->service_name = (char*)malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
  332. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot allocate memory for service name string");
  333. r = -ENOMEM;
  334. goto fail;
  335. }
  336. sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name);
  337. if (!(d->object_path = (char*)malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) {
  338. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot allocate memory for object path string");
  339. r = -ENOMEM;
  340. goto fail;
  341. }
  342. sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name);
  343. if ((k = dbus_bus_request_name(
  344. d->connection,
  345. d->service_name,
  346. DBUS_NAME_FLAG_DO_NOT_QUEUE|
  347. (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
  348. error)) < 0) {
  349. jack_error("dbus_bus_request_name() failed. (1)");
  350. r = -EIO;
  351. goto fail;
  352. }
  353. switch (k) {
  354. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  355. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  356. goto success;
  357. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  358. break;
  359. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE : /* DBUS_NAME_FLAG_DO_NOT_QUEUE was specified */
  360. default: /* unknown reply returned */
  361. jack_error("request name reply with unexpected value %d.", k);
  362. assert(0);
  363. r = -EIO;
  364. goto fail;
  365. }
  366. if (priority <= INT32_MIN) {
  367. r = -EBUSY;
  368. dbus_set_error(error, RESERVE_ERROR_RELEASE_DENIED, "Device reservation request with priority %"PRIi32" denied for \"%s\"", priority, device_name);
  369. goto fail;
  370. }
  371. if (!(m = dbus_message_new_method_call(
  372. d->service_name,
  373. d->object_path,
  374. "org.freedesktop.ReserveDevice1",
  375. "RequestRelease"))) {
  376. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot allocate memory for RequestRelease method call");
  377. r = -ENOMEM;
  378. goto fail;
  379. }
  380. if (!dbus_message_append_args(
  381. m,
  382. DBUS_TYPE_INT32, &d->priority,
  383. DBUS_TYPE_INVALID)) {
  384. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot append args for RequestRelease method call");
  385. r = -ENOMEM;
  386. goto fail;
  387. }
  388. if (!(reply = dbus_connection_send_with_reply_and_block(
  389. d->connection,
  390. m,
  391. 5000, /* 5s */
  392. error))) {
  393. if (dbus_error_has_name(error, DBUS_ERROR_TIMED_OUT) ||
  394. dbus_error_has_name(error, DBUS_ERROR_UNKNOWN_METHOD) ||
  395. dbus_error_has_name(error, DBUS_ERROR_NO_REPLY)) {
  396. /* This must be treated as denied. */
  397. jack_info("Device reservation request with priority %"PRIi32" denied for \"%s\": %s (%s)", priority, device_name, error->name, error->message);
  398. r = -EBUSY;
  399. goto fail;
  400. }
  401. jack_error("dbus_connection_send_with_reply_and_block(RequestRelease) failed.");
  402. r = -EIO;
  403. goto fail;
  404. }
  405. if (!dbus_message_get_args(
  406. reply,
  407. error,
  408. DBUS_TYPE_BOOLEAN, &good,
  409. DBUS_TYPE_INVALID)) {
  410. jack_error("RequestRelease() reply is invalid.");
  411. r = -EIO;
  412. goto fail;
  413. }
  414. if (!good) {
  415. dbus_set_error(error, RESERVE_ERROR_RELEASE_DENIED, "Device reservation request with priority %"PRIi32" denied for \"%s\" via RequestRelease()", priority, device_name);
  416. r = -EBUSY;
  417. goto fail;
  418. }
  419. if ((k = dbus_bus_request_name(
  420. d->connection,
  421. d->service_name,
  422. DBUS_NAME_FLAG_DO_NOT_QUEUE|
  423. (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0)|
  424. DBUS_NAME_FLAG_REPLACE_EXISTING,
  425. error)) < 0) {
  426. jack_error("dbus_bus_request_name() failed. (2)");
  427. r = -EIO;
  428. goto fail;
  429. }
  430. if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
  431. /* this is racy, another contender may have acquired the device */
  432. dbus_set_error(error, RESERVE_ERROR_PROTOCOL_VIOLATION, "request name reply is not DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER but %d.", k);
  433. r = -EIO;
  434. goto fail;
  435. }
  436. success:
  437. d->owning = 1;
  438. if (!(dbus_connection_try_register_object_path(
  439. d->connection,
  440. d->object_path,
  441. &vtable,
  442. d,
  443. error))) {
  444. jack_error("cannot register object path \"%s\": %s", d->object_path, error->message);
  445. r = -ENOMEM;
  446. goto fail;
  447. }
  448. d->registered = 1;
  449. if (!dbus_connection_add_filter(
  450. d->connection,
  451. filter_handler,
  452. d,
  453. NULL)) {
  454. dbus_set_error(error, RESERVE_ERROR_NO_MEMORY, "Cannot add filter");
  455. r = -ENOMEM;
  456. goto fail;
  457. }
  458. d->filtering = 1;
  459. *_d = d;
  460. return 0;
  461. fail:
  462. if (m)
  463. dbus_message_unref(m);
  464. if (reply)
  465. dbus_message_unref(reply);
  466. if (&_error == error)
  467. dbus_error_free(&_error);
  468. if (d)
  469. rd_release(d);
  470. return r;
  471. }
  472. void rd_release(
  473. rd_device *d) {
  474. if (!d)
  475. return;
  476. assert(d->ref > 0);
  477. if (--d->ref)
  478. return;
  479. if (d->filtering)
  480. dbus_connection_remove_filter(
  481. d->connection,
  482. filter_handler,
  483. d);
  484. if (d->registered)
  485. dbus_connection_unregister_object_path(
  486. d->connection,
  487. d->object_path);
  488. if (d->owning) {
  489. DBusError error;
  490. dbus_error_init(&error);
  491. dbus_bus_release_name(
  492. d->connection,
  493. d->service_name,
  494. &error);
  495. dbus_error_free(&error);
  496. }
  497. free(d->device_name);
  498. free(d->application_name);
  499. free(d->application_device_name);
  500. free(d->service_name);
  501. free(d->object_path);
  502. if (d->connection)
  503. dbus_connection_unref(d->connection);
  504. free(d);
  505. }
  506. int rd_set_application_device_name(rd_device *d, const char *n) {
  507. char *t;
  508. if (!d)
  509. return -EINVAL;
  510. assert(d->ref > 0);
  511. if (!(t = strdup(n)))
  512. return -ENOMEM;
  513. free(d->application_device_name);
  514. d->application_device_name = t;
  515. return 0;
  516. }
  517. void rd_set_userdata(rd_device *d, void *userdata) {
  518. if (!d)
  519. return;
  520. assert(d->ref > 0);
  521. d->userdata = userdata;
  522. }
  523. void* rd_get_userdata(rd_device *d) {
  524. if (!d)
  525. return NULL;
  526. assert(d->ref > 0);
  527. return d->userdata;
  528. }