rtkit.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*-*- Mode: C; c-basic-offset: 8 -*-*/
  2. /***
  3. Copyright 2009 Lennart Poettering
  4. Copyright 2010 David Henningsson <[email protected]>
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ***/
  23. #ifdef __linux__ /* OpenMPT */
  24. #ifndef _GNU_SOURCE /* OpenMPT */
  25. #define _GNU_SOURCE /* OpenMPT */
  26. #endif /* OpenMPT */
  27. #endif /* OpenMPT */
  28. #include <errno.h>
  29. #include "rtkit.h"
  30. #ifdef __linux__
  31. #ifndef _GNU_SOURCE
  32. #define _GNU_SOURCE
  33. #endif
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include <sys/types.h>
  37. #include <sys/syscall.h>
  38. static pid_t _gettid(void) {
  39. return (pid_t) syscall(SYS_gettid);
  40. }
  41. static int translate_error(const char *name) {
  42. if (strcmp(name, DBUS_ERROR_NO_MEMORY) == 0)
  43. return -ENOMEM;
  44. if (strcmp(name, DBUS_ERROR_SERVICE_UNKNOWN) == 0 ||
  45. strcmp(name, DBUS_ERROR_NAME_HAS_NO_OWNER) == 0)
  46. return -ENOENT;
  47. if (strcmp(name, DBUS_ERROR_ACCESS_DENIED) == 0 ||
  48. strcmp(name, DBUS_ERROR_AUTH_FAILED) == 0)
  49. return -EACCES;
  50. return -EIO;
  51. }
  52. static long long rtkit_get_int_property(DBusConnection *connection, const char* propname, long long* propval) {
  53. DBusMessage *m = NULL, *r = NULL;
  54. DBusMessageIter iter, subiter;
  55. dbus_int64_t i64;
  56. dbus_int32_t i32;
  57. DBusError error;
  58. int current_type;
  59. long long ret;
  60. const char * interfacestr = "org.freedesktop.RealtimeKit1";
  61. dbus_error_init(&error);
  62. if (!(m = dbus_message_new_method_call(
  63. RTKIT_SERVICE_NAME,
  64. RTKIT_OBJECT_PATH,
  65. "org.freedesktop.DBus.Properties",
  66. "Get"))) {
  67. ret = -ENOMEM;
  68. goto finish;
  69. }
  70. if (!dbus_message_append_args(
  71. m,
  72. DBUS_TYPE_STRING, &interfacestr,
  73. DBUS_TYPE_STRING, &propname,
  74. DBUS_TYPE_INVALID)) {
  75. ret = -ENOMEM;
  76. goto finish;
  77. }
  78. if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
  79. ret = translate_error(error.name);
  80. goto finish;
  81. }
  82. if (dbus_set_error_from_message(&error, r)) {
  83. ret = translate_error(error.name);
  84. goto finish;
  85. }
  86. ret = -EBADMSG;
  87. dbus_message_iter_init(r, &iter);
  88. while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID) {
  89. if (current_type == DBUS_TYPE_VARIANT) {
  90. dbus_message_iter_recurse(&iter, &subiter);
  91. while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID) {
  92. if (current_type == DBUS_TYPE_INT32) {
  93. dbus_message_iter_get_basic(&subiter, &i32);
  94. *propval = i32;
  95. ret = 0;
  96. }
  97. if (current_type == DBUS_TYPE_INT64) {
  98. dbus_message_iter_get_basic(&subiter, &i64);
  99. *propval = i64;
  100. ret = 0;
  101. }
  102. dbus_message_iter_next (&subiter);
  103. }
  104. }
  105. dbus_message_iter_next (&iter);
  106. }
  107. finish:
  108. if (m)
  109. dbus_message_unref(m);
  110. if (r)
  111. dbus_message_unref(r);
  112. dbus_error_free(&error);
  113. return ret;
  114. }
  115. int rtkit_get_max_realtime_priority(DBusConnection *connection) {
  116. long long retval;
  117. int err;
  118. err = rtkit_get_int_property(connection, "MaxRealtimePriority", &retval);
  119. return err < 0 ? err : retval;
  120. }
  121. int rtkit_get_min_nice_level(DBusConnection *connection, int* min_nice_level) {
  122. long long retval;
  123. int err;
  124. err = rtkit_get_int_property(connection, "MinNiceLevel", &retval);
  125. if (err >= 0)
  126. *min_nice_level = retval;
  127. return err;
  128. }
  129. long long rtkit_get_rttime_usec_max(DBusConnection *connection) {
  130. long long retval;
  131. int err;
  132. err = rtkit_get_int_property(connection, "RTTimeUSecMax", &retval);
  133. return err < 0 ? err : retval;
  134. }
  135. int rtkit_make_realtime(DBusConnection *connection, pid_t thread, int priority) {
  136. DBusMessage *m = NULL, *r = NULL;
  137. dbus_uint64_t u64;
  138. dbus_uint32_t u32;
  139. DBusError error;
  140. int ret;
  141. dbus_error_init(&error);
  142. if (thread == 0)
  143. thread = _gettid();
  144. if (!(m = dbus_message_new_method_call(
  145. RTKIT_SERVICE_NAME,
  146. RTKIT_OBJECT_PATH,
  147. "org.freedesktop.RealtimeKit1",
  148. "MakeThreadRealtime"))) {
  149. ret = -ENOMEM;
  150. goto finish;
  151. }
  152. u64 = (dbus_uint64_t) thread;
  153. u32 = (dbus_uint32_t) priority;
  154. if (!dbus_message_append_args(
  155. m,
  156. DBUS_TYPE_UINT64, &u64,
  157. DBUS_TYPE_UINT32, &u32,
  158. DBUS_TYPE_INVALID)) {
  159. ret = -ENOMEM;
  160. goto finish;
  161. }
  162. if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
  163. ret = translate_error(error.name);
  164. goto finish;
  165. }
  166. if (dbus_set_error_from_message(&error, r)) {
  167. ret = translate_error(error.name);
  168. goto finish;
  169. }
  170. ret = 0;
  171. finish:
  172. if (m)
  173. dbus_message_unref(m);
  174. if (r)
  175. dbus_message_unref(r);
  176. dbus_error_free(&error);
  177. return ret;
  178. }
  179. int rtkit_make_high_priority(DBusConnection *connection, pid_t thread, int nice_level) {
  180. DBusMessage *m = NULL, *r = NULL;
  181. dbus_uint64_t u64;
  182. dbus_int32_t s32;
  183. DBusError error;
  184. int ret;
  185. dbus_error_init(&error);
  186. if (thread == 0)
  187. thread = _gettid();
  188. if (!(m = dbus_message_new_method_call(
  189. RTKIT_SERVICE_NAME,
  190. RTKIT_OBJECT_PATH,
  191. "org.freedesktop.RealtimeKit1",
  192. "MakeThreadHighPriority"))) {
  193. ret = -ENOMEM;
  194. goto finish;
  195. }
  196. u64 = (dbus_uint64_t) thread;
  197. s32 = (dbus_int32_t) nice_level;
  198. if (!dbus_message_append_args(
  199. m,
  200. DBUS_TYPE_UINT64, &u64,
  201. DBUS_TYPE_INT32, &s32,
  202. DBUS_TYPE_INVALID)) {
  203. ret = -ENOMEM;
  204. goto finish;
  205. }
  206. if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
  207. ret = translate_error(error.name);
  208. goto finish;
  209. }
  210. if (dbus_set_error_from_message(&error, r)) {
  211. ret = translate_error(error.name);
  212. goto finish;
  213. }
  214. ret = 0;
  215. finish:
  216. if (m)
  217. dbus_message_unref(m);
  218. if (r)
  219. dbus_message_unref(r);
  220. dbus_error_free(&error);
  221. return ret;
  222. }
  223. #else
  224. int rtkit_make_realtime(DBusConnection *connection, pid_t thread, int priority) {
  225. return -ENOTSUP;
  226. }
  227. int rtkit_make_high_priority(DBusConnection *connection, pid_t thread, int nice_level) {
  228. return -ENOTSUP;
  229. }
  230. int rtkit_get_max_realtime_priority(DBusConnection *connection) {
  231. return -ENOTSUP;
  232. }
  233. int rtkit_get_min_nice_level(DBusConnection *connection, int* min_nice_level) {
  234. return -ENOTSUP;
  235. }
  236. long long rtkit_get_rttime_usec_max(DBusConnection *connection) {
  237. return -ENOTSUP;
  238. }
  239. #endif