1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "utility.h"
16
17 #include <sys/resource.h>
18 #include <sys/utsname.h>
19 #include <unistd.h>
20
21 #include <android-base/file.h>
22 #include <processgroup/processgroup.h>
23
24 #include <private/android_filesystem_config.h>
25
26 namespace android {
27 namespace snapshot {
28
29 using android::base::unique_fd;
30
SetThreadPriority(int priority)31 bool SetThreadPriority([[maybe_unused]] int priority) {
32 #ifdef __ANDROID__
33 return setpriority(PRIO_PROCESS, gettid(), priority) != -1;
34 #else
35 return true;
36 #endif
37 }
38
SetProfiles(std::initializer_list<std::string_view> profiles)39 bool SetProfiles([[maybe_unused]] std::initializer_list<std::string_view> profiles) {
40 #ifdef __ANDROID__
41 if (setgid(AID_SYSTEM)) {
42 return false;
43 }
44 return SetTaskProfiles(gettid(), profiles);
45 #else
46 return true;
47 #endif
48 }
49
KernelSupportsIoUring()50 bool KernelSupportsIoUring() {
51 struct utsname uts {};
52 unsigned int major, minor;
53
54 uname(&uts);
55 if (sscanf(uts.release, "%u.%u", &major, &minor) != 2) {
56 return false;
57 }
58
59 // We will only support kernels from 5.6 onwards as IOSQE_ASYNC flag and
60 // IO_URING_OP_READ/WRITE opcodes were introduced only on 5.6 kernel
61 return major > 5 || (major == 5 && minor >= 6);
62 }
63
64 } // namespace snapshot
65 } // namespace android
66