virtual-sink: Fix a crash when moving the sink to a new master right after setup.
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / pulsecore / rtkit.c
blobaecc4e3241b7eed028ec93beca752264f4f02e27
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
3 /***
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
24 SOFTWARE.
25 ***/
27 #include <errno.h>
29 #include "rtkit.h"
31 #ifdef __linux__
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
37 #include <string.h>
38 #include <unistd.h>
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)
48 return -ENOMEM;
49 if (strcmp(name, DBUS_ERROR_SERVICE_UNKNOWN) == 0 ||
50 strcmp(name, DBUS_ERROR_NAME_HAS_NO_OWNER) == 0)
51 return -ENOENT;
52 if (strcmp(name, DBUS_ERROR_ACCESS_DENIED) == 0 ||
53 strcmp(name, DBUS_ERROR_AUTH_FAILED) == 0)
54 return -EACCES;
56 return -EIO;
59 int rtkit_make_realtime(DBusConnection *connection, pid_t thread, int priority) {
60 DBusMessage *m = NULL, *r = NULL;
61 dbus_uint64_t u64;
62 dbus_uint32_t u32;
63 DBusError error;
64 int ret;
66 dbus_error_init(&error);
68 if (thread == 0)
69 thread = _gettid();
71 if (!(m = dbus_message_new_method_call(
72 RTKIT_SERVICE_NAME,
73 RTKIT_OBJECT_PATH,
74 "org.freedesktop.RealtimeKit1",
75 "MakeThreadRealtime"))) {
76 ret = -ENOMEM;
77 goto finish;
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,
87 DBUS_TYPE_INVALID)) {
88 ret = -ENOMEM;
89 goto finish;
92 if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
93 ret = translate_error(error.name);
94 goto finish;
98 if (dbus_set_error_from_message(&error, r)) {
99 ret = translate_error(error.name);
100 goto finish;
103 ret = 0;
105 finish:
107 if (m)
108 dbus_message_unref(m);
110 if (r)
111 dbus_message_unref(r);
113 dbus_error_free(&error);
115 return ret;
118 int rtkit_make_high_priority(DBusConnection *connection, pid_t thread, int nice_level) {
119 DBusMessage *m = NULL, *r = NULL;
120 dbus_uint64_t u64;
121 dbus_int32_t s32;
122 DBusError error;
123 int ret;
125 dbus_error_init(&error);
127 if (thread == 0)
128 thread = _gettid();
130 if (!(m = dbus_message_new_method_call(
131 RTKIT_SERVICE_NAME,
132 RTKIT_OBJECT_PATH,
133 "org.freedesktop.RealtimeKit1",
134 "MakeThreadHighPriority"))) {
135 ret = -ENOMEM;
136 goto finish;
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)) {
147 ret = -ENOMEM;
148 goto finish;
153 if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
154 ret = translate_error(error.name);
155 goto finish;
159 if (dbus_set_error_from_message(&error, r)) {
160 ret = translate_error(error.name);
161 goto finish;
164 ret = 0;
166 finish:
168 if (m)
169 dbus_message_unref(m);
171 if (r)
172 dbus_message_unref(r);
174 dbus_error_free(&error);
176 return ret;
179 #else
181 int rtkit_make_realtime(DBusConnection *connection, pid_t thread, int priority) {
182 return -ENOTSUP;
185 int rtkit_make_high_priority(DBusConnection *connection, pid_t thread, int nice_level) {
186 return -ENOTSUP;
189 #endif