Git init
[framework/multimedia/pulseaudio.git] / src / pulsecore / rtkit.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   Copyright 2009 Lennart Poettering
5
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
14   The above copyright notice and this permission notice shall be
15   included in all copies or substantial portions of the Software.
16
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 ***/
26
27 #include <errno.h>
28
29 #include "rtkit.h"
30
31 #ifdef __linux__
32
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
36
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/syscall.h>
41
42 static pid_t _gettid(void) {
43         return (pid_t) syscall(SYS_gettid);
44 }
45
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;
55
56         return -EIO;
57 }
58
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;
65
66         dbus_error_init(&error);
67
68         if (thread == 0)
69                 thread = _gettid();
70
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;
78         }
79
80         u64 = (dbus_uint64_t) thread;
81         u32 = (dbus_uint32_t) priority;
82
83         if (!dbus_message_append_args(
84                             m,
85                             DBUS_TYPE_UINT64, &u64,
86                             DBUS_TYPE_UINT32, &u32,
87                             DBUS_TYPE_INVALID)) {
88                 ret = -ENOMEM;
89                 goto finish;
90         }
91
92         if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
93                 ret = translate_error(error.name);
94                 goto finish;
95         }
96
97
98         if (dbus_set_error_from_message(&error, r)) {
99                 ret = translate_error(error.name);
100                 goto finish;
101         }
102
103         ret = 0;
104
105 finish:
106
107         if (m)
108                 dbus_message_unref(m);
109
110         if (r)
111                 dbus_message_unref(r);
112
113         dbus_error_free(&error);
114
115         return ret;
116 }
117
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;
124
125         dbus_error_init(&error);
126
127         if (thread == 0)
128                 thread = _gettid();
129
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;
137         }
138
139         u64 = (dbus_uint64_t) thread;
140         s32 = (dbus_int32_t) nice_level;
141
142         if (!dbus_message_append_args(
143                             m,
144                             DBUS_TYPE_UINT64, &u64,
145                             DBUS_TYPE_INT32, &s32,
146                             DBUS_TYPE_INVALID)) {
147                 ret = -ENOMEM;
148                 goto finish;
149         }
150
151
152
153         if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
154                 ret = translate_error(error.name);
155                 goto finish;
156         }
157
158
159         if (dbus_set_error_from_message(&error, r)) {
160                 ret = translate_error(error.name);
161                 goto finish;
162         }
163
164         ret = 0;
165
166 finish:
167
168         if (m)
169                 dbus_message_unref(m);
170
171         if (r)
172                 dbus_message_unref(r);
173
174         dbus_error_free(&error);
175
176         return ret;
177 }
178
179 #else
180
181 int rtkit_make_realtime(DBusConnection *connection, pid_t thread, int priority) {
182         return -ENOTSUP;
183 }
184
185 int rtkit_make_high_priority(DBusConnection *connection, pid_t thread, int nice_level) {
186         return -ENOTSUP;
187 }
188
189 #endif