xref: /aosp_15_r20/system/core/fs_mgr/libsnapshot/snapuserd/utility.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 <android-base/properties.h>
18 #include <sys/resource.h>
19 #include <sys/utsname.h>
20 #include <unistd.h>
21 
22 #include <android-base/file.h>
23 #include <android-base/logging.h>
24 #include <libdm/dm.h>
25 #include <processgroup/processgroup.h>
26 
27 #include <private/android_filesystem_config.h>
28 
29 namespace android {
30 namespace snapshot {
31 
32 using android::base::unique_fd;
33 using android::dm::DeviceMapper;
34 
SetThreadPriority(int priority)35 bool SetThreadPriority([[maybe_unused]] int priority) {
36 #ifdef __ANDROID__
37     return setpriority(PRIO_PROCESS, gettid(), priority) != -1;
38 #else
39     return true;
40 #endif
41 }
42 
SetProfiles(std::initializer_list<std::string_view> profiles)43 bool SetProfiles([[maybe_unused]] std::initializer_list<std::string_view> profiles) {
44 #ifdef __ANDROID__
45     if (setgid(AID_SYSTEM)) {
46         return false;
47     }
48     return SetTaskProfiles(gettid(), profiles);
49 #else
50     return true;
51 #endif
52 }
53 
KernelSupportsIoUring()54 bool KernelSupportsIoUring() {
55     struct utsname uts {};
56     unsigned int major, minor;
57 
58     uname(&uts);
59     if (sscanf(uts.release, "%u.%u", &major, &minor) != 2) {
60         return false;
61     }
62 
63     // We will only support kernels from 5.6 onwards as IOSQE_ASYNC flag and
64     // IO_URING_OP_READ/WRITE opcodes were introduced only on 5.6 kernel
65     return major > 5 || (major == 5 && minor >= 6);
66 }
67 
GetUserspaceSnapshotsEnabledProperty()68 bool GetUserspaceSnapshotsEnabledProperty() {
69     return android::base::GetBoolProperty("ro.virtual_ab.userspace.snapshots.enabled", false);
70 }
71 
KernelSupportsCompressedSnapshots()72 bool KernelSupportsCompressedSnapshots() {
73     auto& dm = DeviceMapper::Instance();
74     return dm.GetTargetByName("user", nullptr);
75 }
76 
IsVendorFromAndroid12()77 bool IsVendorFromAndroid12() {
78     const std::string UNKNOWN = "unknown";
79     const std::string vendor_release =
80             android::base::GetProperty("ro.vendor.build.version.release_or_codename", UNKNOWN);
81 
82     if (vendor_release.find("12") != std::string::npos) {
83         return true;
84     }
85     return false;
86 }
87 
CanUseUserspaceSnapshots()88 bool CanUseUserspaceSnapshots() {
89     if (!GetUserspaceSnapshotsEnabledProperty()) {
90         LOG(INFO) << "Virtual A/B - Userspace snapshots disabled";
91         return false;
92     }
93 
94     if (!KernelSupportsCompressedSnapshots()) {
95         LOG(ERROR) << "Userspace snapshots requested, but no kernel support is available.";
96         return false;
97     }
98     return true;
99 }
100 
101 }  // namespace snapshot
102 }  // namespace android
103