Make a couple more operator bools explicit
[openal-soft.git] / core / rtkit.cpp
blob9210220ef8fb16e45b56f117bfd5ad3b3d348f9f
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>
46 namespace dbus {
48 constexpr int TypeString{'s'};
49 constexpr int TypeVariant{'v'};
50 constexpr int TypeInt32{'i'};
51 constexpr int TypeUInt32{'u'};
52 constexpr int TypeInt64{'x'};
53 constexpr int TypeUInt64{'t'};
54 constexpr int TypeInvalid{'\0'};
56 struct MessageDeleter {
57 void operator()(DBusMessage *m) { dbus_message_unref(m); }
59 using MessagePtr = std::unique_ptr<DBusMessage,MessageDeleter>;
61 } // namespace dbus
63 namespace {
65 inline pid_t _gettid()
67 #ifdef __linux__
68 return static_cast<pid_t>(syscall(SYS_gettid));
69 #elif defined(__FreeBSD__)
70 long pid{};
71 thr_self(&pid);
72 return static_cast<pid_t>(pid);
73 #else
74 #warning gettid not available
75 return 0;
76 #endif
79 int translate_error(const char *name)
81 if(strcmp(name, DBUS_ERROR_NO_MEMORY) == 0)
82 return -ENOMEM;
83 if(strcmp(name, DBUS_ERROR_SERVICE_UNKNOWN) == 0
84 || strcmp(name, DBUS_ERROR_NAME_HAS_NO_OWNER) == 0)
85 return -ENOENT;
86 if(strcmp(name, DBUS_ERROR_ACCESS_DENIED) == 0
87 || strcmp(name, DBUS_ERROR_AUTH_FAILED) == 0)
88 return -EACCES;
89 return -EIO;
92 int rtkit_get_int_property(DBusConnection *connection, const char *propname, long long *propval)
94 dbus::MessagePtr m{dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH,
95 "org.freedesktop.DBus.Properties", "Get")};
96 if(!m) return -ENOMEM;
98 const char *interfacestr = RTKIT_SERVICE_NAME;
99 auto ready = dbus_message_append_args(m.get(),
100 dbus::TypeString, &interfacestr,
101 dbus::TypeString, &propname,
102 dbus::TypeInvalid);
103 if(!ready) return -ENOMEM;
105 dbus::Error error;
106 dbus::MessagePtr r{dbus_connection_send_with_reply_and_block(connection, m.get(), -1,
107 &error.get())};
108 if(!r) return translate_error(error->name);
110 if(dbus_set_error_from_message(&error.get(), r.get()))
111 return translate_error(error->name);
113 int ret{-EBADMSG};
114 DBusMessageIter iter{};
115 dbus_message_iter_init(r.get(), &iter);
116 while(int curtype{dbus_message_iter_get_arg_type(&iter)})
118 if(curtype == dbus::TypeVariant)
120 DBusMessageIter subiter{};
121 dbus_message_iter_recurse(&iter, &subiter);
123 while((curtype=dbus_message_iter_get_arg_type(&subiter)) != dbus::TypeInvalid)
125 if(curtype == dbus::TypeInt32)
127 dbus_int32_t i32{};
128 dbus_message_iter_get_basic(&subiter, &i32);
129 *propval = i32;
130 ret = 0;
133 if(curtype == dbus::TypeInt64)
135 dbus_int64_t i64{};
136 dbus_message_iter_get_basic(&subiter, &i64);
137 *propval = i64;
138 ret = 0;
141 dbus_message_iter_next(&subiter);
144 dbus_message_iter_next(&iter);
147 return ret;
150 } // namespace
152 int rtkit_get_max_realtime_priority(DBusConnection *connection)
154 long long retval{};
155 int err{rtkit_get_int_property(connection, "MaxRealtimePriority", &retval)};
156 return err < 0 ? err : static_cast<int>(retval);
159 int rtkit_get_min_nice_level(DBusConnection *connection, int *min_nice_level)
161 long long retval{};
162 int err{rtkit_get_int_property(connection, "MinNiceLevel", &retval)};
163 if(err >= 0) *min_nice_level = static_cast<int>(retval);
164 return err;
167 long long rtkit_get_rttime_usec_max(DBusConnection *connection)
169 long long retval{};
170 int err{rtkit_get_int_property(connection, "RTTimeUSecMax", &retval)};
171 return err < 0 ? err : retval;
174 int rtkit_make_realtime(DBusConnection *connection, pid_t thread, int priority)
176 if(thread == 0)
177 thread = _gettid();
178 if(thread == 0)
179 return -ENOTSUP;
181 dbus::MessagePtr m{dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH,
182 "org.freedesktop.RealtimeKit1", "MakeThreadRealtime")};
183 if(!m) return -ENOMEM;
185 auto u64 = static_cast<dbus_uint64_t>(thread);
186 auto u32 = static_cast<dbus_uint32_t>(priority);
187 auto ready = dbus_message_append_args(m.get(),
188 dbus::TypeUInt64, &u64,
189 dbus::TypeUInt32, &u32,
190 dbus::TypeInvalid);
191 if(!ready) return -ENOMEM;
193 dbus::Error error;
194 dbus::MessagePtr r{dbus_connection_send_with_reply_and_block(connection, m.get(), -1,
195 &error.get())};
196 if(!r) return translate_error(error->name);
198 if(dbus_set_error_from_message(&error.get(), r.get()))
199 return translate_error(error->name);
201 return 0;
204 int rtkit_make_high_priority(DBusConnection *connection, pid_t thread, int nice_level)
206 if(thread == 0)
207 thread = _gettid();
208 if(thread == 0)
209 return -ENOTSUP;
211 dbus::MessagePtr m{dbus_message_new_method_call(RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH,
212 "org.freedesktop.RealtimeKit1", "MakeThreadHighPriority")};
213 if(!m) return -ENOMEM;
215 auto u64 = static_cast<dbus_uint64_t>(thread);
216 auto s32 = static_cast<dbus_int32_t>(nice_level);
217 auto ready = dbus_message_append_args(m.get(),
218 dbus::TypeUInt64, &u64,
219 dbus::TypeInt32, &s32,
220 dbus::TypeInvalid);
221 if(!ready) return -ENOMEM;
223 dbus::Error error;
224 dbus::MessagePtr r{dbus_connection_send_with_reply_and_block(connection, m.get(), -1,
225 &error.get())};
226 if(!r) return translate_error(error->name);
228 if(dbus_set_error_from_message(&error.get(), r.get()))
229 return translate_error(error->name);
231 return 0;