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