🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

232 lines
6.9 KiB

  1. /*-*- Mode: C; c-basic-offset: 8 -*-*/
  2. /***
  3. Copyright 2009 Lennart Poettering
  4. Copyright 2010 David Henningsson <diwic@ubuntu.com>
  5. Copyright 2021 Chris Robinson
  6. Permission is hereby granted, free of charge, to any person
  7. obtaining a copy of this software and associated documentation files
  8. (the "Software"), to deal in the Software without restriction,
  9. including without limitation the rights to use, copy, modify, merge,
  10. publish, distribute, sublicense, and/or sell copies of the Software,
  11. and to permit persons to whom the Software is furnished to do so,
  12. subject to the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. ***/
  24. #include "config.h"
  25. #include "rtkit.h"
  26. #include <errno.h>
  27. #ifndef _GNU_SOURCE
  28. #define _GNU_SOURCE
  29. #endif
  30. #include <memory>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/types.h>
  34. #include <sys/syscall.h>
  35. namespace dbus {
  36. constexpr int TypeString{'s'};
  37. constexpr int TypeVariant{'v'};
  38. constexpr int TypeInt32{'i'};
  39. constexpr int TypeUInt32{'u'};
  40. constexpr int TypeInt64{'x'};
  41. constexpr int TypeUInt64{'t'};
  42. constexpr int TypeInvalid{'\0'};
  43. struct MessageDeleter {
  44. void operator()(DBusMessage *m) { dbus_message_unref(m); }
  45. };
  46. using MessagePtr = std::unique_ptr<DBusMessage,MessageDeleter>;
  47. } // namespace dbus
  48. namespace {
  49. inline pid_t _gettid()
  50. {
  51. #ifdef __linux__
  52. return static_cast<pid_t>(syscall(SYS_gettid));
  53. #elif defined(__FreeBSD__)
  54. long pid{};
  55. thr_self(&pid);
  56. return static_cast<pid_t>(pid);
  57. #else
  58. #warning gettid not available
  59. return 0;
  60. #endif
  61. }
  62. int translate_error(const char *name)
  63. {
  64. if(strcmp(name, DBUS_ERROR_NO_MEMORY) == 0)
  65. return -ENOMEM;
  66. if(strcmp(name, DBUS_ERROR_SERVICE_UNKNOWN) == 0
  67. || strcmp(name, DBUS_ERROR_NAME_HAS_NO_OWNER) == 0)
  68. return -ENOENT;
  69. if(strcmp(name, DBUS_ERROR_ACCESS_DENIED) == 0
  70. || strcmp(name, DBUS_ERROR_AUTH_FAILED) == 0)
  71. return -EACCES;
  72. return -EIO;
  73. }
  74. int rtkit_get_int_property(DBusConnection *connection, const char *propname, long long *propval)
  75. {
  76. dbus::MessagePtr m{dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH,
  77. "org.freedesktop.DBus.Properties", "Get")};
  78. if(!m) return -ENOMEM;
  79. const char *interfacestr = RTKIT_SERVICE_NAME;
  80. auto ready = dbus_message_append_args(m.get(),
  81. dbus::TypeString, &interfacestr,
  82. dbus::TypeString, &propname,
  83. dbus::TypeInvalid);
  84. if(!ready) return -ENOMEM;
  85. dbus::Error error;
  86. dbus::MessagePtr r{dbus_connection_send_with_reply_and_block(connection, m.get(), -1,
  87. &error.get())};
  88. if(!r) return translate_error(error->name);
  89. if(dbus_set_error_from_message(&error.get(), r.get()))
  90. return translate_error(error->name);
  91. int ret{-EBADMSG};
  92. DBusMessageIter iter{};
  93. dbus_message_iter_init(r.get(), &iter);
  94. while(int curtype{dbus_message_iter_get_arg_type(&iter)})
  95. {
  96. if(curtype == dbus::TypeVariant)
  97. {
  98. DBusMessageIter subiter{};
  99. dbus_message_iter_recurse(&iter, &subiter);
  100. while((curtype=dbus_message_iter_get_arg_type(&subiter)) != dbus::TypeInvalid)
  101. {
  102. if(curtype == dbus::TypeInt32)
  103. {
  104. dbus_int32_t i32{};
  105. dbus_message_iter_get_basic(&subiter, &i32);
  106. *propval = i32;
  107. ret = 0;
  108. }
  109. if(curtype == dbus::TypeInt64)
  110. {
  111. dbus_int64_t i64{};
  112. dbus_message_iter_get_basic(&subiter, &i64);
  113. *propval = i64;
  114. ret = 0;
  115. }
  116. dbus_message_iter_next(&subiter);
  117. }
  118. }
  119. dbus_message_iter_next(&iter);
  120. }
  121. return ret;
  122. }
  123. } // namespace
  124. int rtkit_get_max_realtime_priority(DBusConnection *connection)
  125. {
  126. long long retval{};
  127. int err{rtkit_get_int_property(connection, "MaxRealtimePriority", &retval)};
  128. return err < 0 ? err : static_cast<int>(retval);
  129. }
  130. int rtkit_get_min_nice_level(DBusConnection *connection, int *min_nice_level)
  131. {
  132. long long retval{};
  133. int err{rtkit_get_int_property(connection, "MinNiceLevel", &retval)};
  134. if(err >= 0) *min_nice_level = static_cast<int>(retval);
  135. return err;
  136. }
  137. long long rtkit_get_rttime_usec_max(DBusConnection *connection)
  138. {
  139. long long retval{};
  140. int err{rtkit_get_int_property(connection, "RTTimeUSecMax", &retval)};
  141. return err < 0 ? err : retval;
  142. }
  143. int rtkit_make_realtime(DBusConnection *connection, pid_t thread, int priority)
  144. {
  145. if(thread == 0)
  146. thread = _gettid();
  147. if(thread == 0)
  148. return -ENOTSUP;
  149. dbus::MessagePtr m{dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH,
  150. "org.freedesktop.RealtimeKit1", "MakeThreadRealtime")};
  151. if(!m) return -ENOMEM;
  152. auto u64 = static_cast<dbus_uint64_t>(thread);
  153. auto u32 = static_cast<dbus_uint32_t>(priority);
  154. auto ready = dbus_message_append_args(m.get(),
  155. dbus::TypeUInt64, &u64,
  156. dbus::TypeUInt32, &u32,
  157. dbus::TypeInvalid);
  158. if(!ready) return -ENOMEM;
  159. dbus::Error error;
  160. dbus::MessagePtr r{dbus_connection_send_with_reply_and_block(connection, m.get(), -1,
  161. &error.get())};
  162. if(!r) return translate_error(error->name);
  163. if(dbus_set_error_from_message(&error.get(), r.get()))
  164. return translate_error(error->name);
  165. return 0;
  166. }
  167. int rtkit_make_high_priority(DBusConnection *connection, pid_t thread, int nice_level)
  168. {
  169. if(thread == 0)
  170. thread = _gettid();
  171. if(thread == 0)
  172. return -ENOTSUP;
  173. dbus::MessagePtr m{dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH,
  174. "org.freedesktop.RealtimeKit1", "MakeThreadHighPriority")};
  175. if(!m) return -ENOMEM;
  176. auto u64 = static_cast<dbus_uint64_t>(thread);
  177. auto s32 = static_cast<dbus_int32_t>(nice_level);
  178. auto ready = dbus_message_append_args(m.get(),
  179. dbus::TypeUInt64, &u64,
  180. dbus::TypeInt32, &s32,
  181. dbus::TypeInvalid);
  182. if(!ready) return -ENOMEM;
  183. dbus::Error error;
  184. dbus::MessagePtr r{dbus_connection_send_with_reply_and_block(connection, m.get(), -1,
  185. &error.get())};
  186. if(!r) return translate_error(error->name);
  187. if(dbus_set_error_from_message(&error.get(), r.get()))
  188. return translate_error(error->name);
  189. return 0;
  190. }