| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <errno.h> |
| 9 |
#include <string.h> |
| 10 |
#include <sys/resource.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#if defined(__linux__) |
| 15 |
#include <sys/syscall.h> |
| 16 |
#elif defined(__NetBSD__) |
| 17 |
#include <lwp.h> |
| 18 |
#elif defined(__FreeBSD__) |
| 19 |
#include <sys/thr.h> |
| 20 |
#elif defined(__DragonFly__) |
| 21 |
#include <sys/lwp.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "log.h" |
| 25 |
#include "rtkit.h" |
| 26 |
#include "utils/misc.h" |
| 27 |
|
| 28 |
#define RTKIT_SERVICE_NAME "org.freedesktop.RealtimeKit1" |
| 29 |
#define RTKIT_OBJECT_PATH "/org/freedesktop/RealtimeKit1" |
| 30 |
#define RTKIT_INTERFACE "org.freedesktop.RealtimeKit1" |
| 31 |
|
| 32 |
static inline long compat_gettid(void) { |
| 33 |
long ret = -1; |
| 34 |
#if defined(__linux__) |
| 35 |
ret = (pid_t)syscall(SYS_gettid); |
| 36 |
#elif defined(__NetBSD__) |
| 37 |
ret = _lwp_self(); |
| 38 |
#elif defined(__FreeBSD__) |
| 39 |
long lwpid; |
| 40 |
thr_self(&lwpid); |
| 41 |
ret = lwpid; |
| 42 |
#elif defined(__DragonFly__) |
| 43 |
ret = lwp_gettid(); |
| 44 |
#endif |
| 45 |
return ret; |
| 46 |
} |
| 47 |
|
| 48 |
static bool |
| 49 |
rtkit_get_int_property(DBusConnection *connection, const char *propname, long long *propval) { |
| 50 |
DBusMessage *m = NULL, *r = NULL; |
| 51 |
DBusMessageIter iter, subiter; |
| 52 |
dbus_int64_t i64; |
| 53 |
dbus_int32_t i32; |
| 54 |
DBusError error; |
| 55 |
int current_type; |
| 56 |
int ret = 0; |
| 57 |
const char *interfacestr = RTKIT_INTERFACE; |
| 58 |
|
| 59 |
dbus_error_init(&error); |
| 60 |
|
| 61 |
m = dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH, |
| 62 |
"org.freedesktop.DBus.Properties", "Get"); |
| 63 |
if (!m) { |
| 64 |
ret = -ENOMEM; |
| 65 |
goto finish; |
| 66 |
} |
| 67 |
|
| 68 |
if (!dbus_message_append_args(m, DBUS_TYPE_STRING, &interfacestr, |
| 69 |
DBUS_TYPE_STRING, &propname, DBUS_TYPE_INVALID)) { |
| 70 |
ret = -ENOMEM; |
| 71 |
goto finish; |
| 72 |
} |
| 73 |
|
| 74 |
r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error); |
| 75 |
if (!r) { |
| 76 |
goto finish; |
| 77 |
} |
| 78 |
|
| 79 |
if (dbus_set_error_from_message(&error, r)) { |
| 80 |
goto finish; |
| 81 |
} |
| 82 |
|
| 83 |
ret = -EBADMSG; |
| 84 |
dbus_message_iter_init(r, &iter); |
| 85 |
while ((current_type = dbus_message_iter_get_arg_type(&iter)) != DBUS_TYPE_INVALID) { |
| 86 |
if (current_type == DBUS_TYPE_VARIANT) { |
| 87 |
dbus_message_iter_recurse(&iter, &subiter); |
| 88 |
|
| 89 |
while ((current_type = dbus_message_iter_get_arg_type(&subiter)) != |
| 90 |
DBUS_TYPE_INVALID) { |
| 91 |
|
| 92 |
if (current_type == DBUS_TYPE_INT32) { |
| 93 |
dbus_message_iter_get_basic(&subiter, &i32); |
| 94 |
*propval = i32; |
| 95 |
ret = 0; |
| 96 |
} |
| 97 |
|
| 98 |
if (current_type == DBUS_TYPE_INT64) { |
| 99 |
dbus_message_iter_get_basic(&subiter, &i64); |
| 100 |
*propval = i64; |
| 101 |
ret = 0; |
| 102 |
} |
| 103 |
|
| 104 |
dbus_message_iter_next(&subiter); |
| 105 |
} |
| 106 |
} |
| 107 |
dbus_message_iter_next(&iter); |
| 108 |
} |
| 109 |
|
| 110 |
finish: |
| 111 |
if (m) { |
| 112 |
dbus_message_unref(m); |
| 113 |
} |
| 114 |
if (r) { |
| 115 |
dbus_message_unref(r); |
| 116 |
} |
| 117 |
if (dbus_error_is_set(&error)) { |
| 118 |
log_debug("Couldn't get property %s from rtkit: (dbus) %s", propname, |
| 119 |
error.message); |
| 120 |
dbus_error_free(&error); |
| 121 |
return false; |
| 122 |
} |
| 123 |
if (ret != 0) { |
| 124 |
log_debug("Couldn't get property %s from rtkit: %s", propname, strerror(-ret)); |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
static bool rtkit_get_rttime_usec_max(DBusConnection *connection, long long *retval) { |
| 132 |
return rtkit_get_int_property(connection, "RTTimeUSecMax", retval); |
| 133 |
} |
| 134 |
|
| 135 |
static inline void free_dbus_connection(DBusConnection **connection) { |
| 136 |
if (*connection) { |
| 137 |
dbus_connection_close(*connection); |
| 138 |
dbus_connection_unref(*connection); |
| 139 |
*connection = NULL; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
static inline void free_dbus_message(DBusMessage **message) { |
| 144 |
if (*message) { |
| 145 |
dbus_message_unref(*message); |
| 146 |
*message = NULL; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
bool rtkit_make_realtime(long thread, int priority) { |
| 151 |
cleanup(free_dbus_message) DBusMessage *m = NULL; |
| 152 |
cleanup(free_dbus_message) DBusMessage *r = NULL; |
| 153 |
dbus_uint64_t u64; |
| 154 |
dbus_uint32_t u32; |
| 155 |
DBusError error; |
| 156 |
int ret = 0; |
| 157 |
long long rttime_usec_max = 0; |
| 158 |
bool succeeded = true; |
| 159 |
|
| 160 |
dbus_error_init(&error); |
| 161 |
|
| 162 |
cleanup(free_dbus_connection) DBusConnection *connection = |
| 163 |
dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); |
| 164 |
if (dbus_error_is_set(&error)) { |
| 165 |
log_info("Couldn't get system bus: %s", error.message); |
| 166 |
dbus_error_free(&error); |
| 167 |
return false; |
| 168 |
} |
| 169 |
dbus_connection_set_exit_on_disconnect(connection, false); |
| 170 |
|
| 171 |
if (thread == 0) { |
| 172 |
thread = compat_gettid(); |
| 173 |
} |
| 174 |
|
| 175 |
if (!rtkit_get_rttime_usec_max(connection, &rttime_usec_max)) { |
| 176 |
log_debug("Couldn't get RTTimeUSecMax from rtkit."); |
| 177 |
return false; |
| 178 |
} |
| 179 |
if (rttime_usec_max <= 0) { |
| 180 |
log_debug("Unreasonable RTTimeUSecMax from rtkit: %lld", rttime_usec_max); |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
#if defined(RLIMIT_RTTIME) |
| 185 |
struct rlimit old_rlim, new_rlim; |
| 186 |
|
| 187 |
|
| 188 |
if (getrlimit(RLIMIT_RTTIME, &old_rlim) != 0) { |
| 189 |
log_debug("Couldn't get RLIMIT_RTTIME."); |
| 190 |
return false; |
| 191 |
} |
| 192 |
new_rlim = old_rlim; |
| 193 |
new_rlim.rlim_cur = min3(new_rlim.rlim_max, (rlim_t)rttime_usec_max, 100000); |
| 194 |
new_rlim.rlim_max = new_rlim.rlim_cur; |
| 195 |
if (setrlimit(RLIMIT_RTTIME, &new_rlim) != 0) { |
| 196 |
log_debug("Couldn't set RLIMIT_RTTIME."); |
| 197 |
return false; |
| 198 |
} |
| 199 |
#endif |
| 200 |
|
| 201 |
m = dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH, |
| 202 |
RTKIT_INTERFACE, "MakeThreadRealtime"); |
| 203 |
if (!m) { |
| 204 |
ret = -ENOMEM; |
| 205 |
goto finish; |
| 206 |
} |
| 207 |
|
| 208 |
u64 = (dbus_uint64_t)thread; |
| 209 |
u32 = (dbus_uint32_t)priority; |
| 210 |
|
| 211 |
if (!dbus_message_append_args(m, DBUS_TYPE_UINT64, &u64, DBUS_TYPE_UINT32, &u32, |
| 212 |
DBUS_TYPE_INVALID)) { |
| 213 |
ret = -ENOMEM; |
| 214 |
goto finish; |
| 215 |
} |
| 216 |
|
| 217 |
r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error); |
| 218 |
if (!r) { |
| 219 |
goto finish; |
| 220 |
} |
| 221 |
|
| 222 |
if (dbus_set_error_from_message(&error, r)) { |
| 223 |
goto finish; |
| 224 |
} |
| 225 |
|
| 226 |
ret = 0; |
| 227 |
|
| 228 |
finish: |
| 229 |
if (dbus_error_is_set(&error)) { |
| 230 |
log_info("Couldn't make thread realtime with rtkit: (dbus) %s", error.message); |
| 231 |
dbus_error_free(&error); |
| 232 |
succeeded = false; |
| 233 |
} else if (ret != 0) { |
| 234 |
log_info("Couldn't make thread realtime with rtkit: %s", strerror(-ret)); |
| 235 |
succeeded = false; |
| 236 |
} |
| 237 |
#if defined(RLIMIT_RTTIME) |
| 238 |
if (!succeeded) { |
| 239 |
|
| 240 |
setrlimit(RLIMIT_RTTIME, &old_rlim); |
| 241 |
} |
| 242 |
#endif |
| 243 |
|
| 244 |
return succeeded; |
| 245 |
} |