1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 Copyright 2009 Lennart Poettering
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:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #include <sys/types.h>
40 #include <sys/syscall.h>
42 static pid_t
_gettid(void) {
43 return (pid_t
) syscall(SYS_gettid
);
46 static int translate_error(const char *name
) {
47 if (strcmp(name
, DBUS_ERROR_NO_MEMORY
) == 0)
49 if (strcmp(name
, DBUS_ERROR_SERVICE_UNKNOWN
) == 0 ||
50 strcmp(name
, DBUS_ERROR_NAME_HAS_NO_OWNER
) == 0)
52 if (strcmp(name
, DBUS_ERROR_ACCESS_DENIED
) == 0 ||
53 strcmp(name
, DBUS_ERROR_AUTH_FAILED
) == 0)
59 int rtkit_make_realtime(DBusConnection
*connection
, pid_t thread
, int priority
) {
60 DBusMessage
*m
= NULL
, *r
= NULL
;
66 dbus_error_init(&error
);
71 if (!(m
= dbus_message_new_method_call(
74 "org.freedesktop.RealtimeKit1",
75 "MakeThreadRealtime"))) {
80 u64
= (dbus_uint64_t
) thread
;
81 u32
= (dbus_uint32_t
) priority
;
83 if (!dbus_message_append_args(
85 DBUS_TYPE_UINT64
, &u64
,
86 DBUS_TYPE_UINT32
, &u32
,
92 if (!(r
= dbus_connection_send_with_reply_and_block(connection
, m
, -1, &error
))) {
93 ret
= translate_error(error
.name
);
98 if (dbus_set_error_from_message(&error
, r
)) {
99 ret
= translate_error(error
.name
);
108 dbus_message_unref(m
);
111 dbus_message_unref(r
);
113 dbus_error_free(&error
);
118 int rtkit_make_high_priority(DBusConnection
*connection
, pid_t thread
, int nice_level
) {
119 DBusMessage
*m
= NULL
, *r
= NULL
;
125 dbus_error_init(&error
);
130 if (!(m
= dbus_message_new_method_call(
133 "org.freedesktop.RealtimeKit1",
134 "MakeThreadHighPriority"))) {
139 u64
= (dbus_uint64_t
) thread
;
140 s32
= (dbus_int32_t
) nice_level
;
142 if (!dbus_message_append_args(
144 DBUS_TYPE_UINT64
, &u64
,
145 DBUS_TYPE_INT32
, &s32
,
146 DBUS_TYPE_INVALID
)) {
153 if (!(r
= dbus_connection_send_with_reply_and_block(connection
, m
, -1, &error
))) {
154 ret
= translate_error(error
.name
);
159 if (dbus_set_error_from_message(&error
, r
)) {
160 ret
= translate_error(error
.name
);
169 dbus_message_unref(m
);
172 dbus_message_unref(r
);
174 dbus_error_free(&error
);
181 int rtkit_make_realtime(DBusConnection
*connection
, pid_t thread
, int priority
) {
185 int rtkit_make_high_priority(DBusConnection
*connection
, pid_t thread
, int nice_level
) {