1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/logging.h>
18 #include <android-base/properties.h>
19 #include <android-base/strings.h>
20 #include <gflags/gflags.h>
21 #include <snapuserd/snapuserd_client.h>
22
23 #include "snapuserd_daemon.h"
24
25 DEFINE_string(socket, android::snapshot::kSnapuserdSocket, "Named socket or socket path.");
26 DEFINE_bool(no_socket, false,
27 "If true, no socket is used. Each additional argument is an INIT message.");
28 DEFINE_bool(socket_handoff, false,
29 "If true, perform a socket hand-off with an existing snapuserd instance, then exit.");
30 DEFINE_bool(user_snapshot, false, "If true, user-space snapshots are used");
31 DEFINE_bool(io_uring, false, "If true, io_uring feature is enabled");
32 DEFINE_bool(o_direct, false, "If true, enable direct reads on source device");
33
34 namespace android {
35 namespace snapshot {
36
IsUserspaceSnapshotsEnabled()37 bool Daemon::IsUserspaceSnapshotsEnabled() {
38 const std::string UNKNOWN = "unknown";
39 const std::string vendor_release =
40 android::base::GetProperty("ro.vendor.build.version.release_or_codename", UNKNOWN);
41
42 // If the vendor is on Android S, install process will forcefully take the
43 // userspace snapshots path.
44 //
45 // We will not reach here post OTA reboot as the binary will be from vendor
46 // ramdisk which is on Android S.
47 if (vendor_release.find("12") != std::string::npos) {
48 LOG(INFO) << "Userspace snapshots enabled as vendor partition is on Android: "
49 << vendor_release;
50 return true;
51 }
52
53 return android::base::GetBoolProperty("ro.virtual_ab.userspace.snapshots.enabled", false);
54 }
55
StartDaemon(int argc,char ** argv)56 bool Daemon::StartDaemon(int argc, char** argv) {
57 int arg_start = gflags::ParseCommandLineFlags(&argc, &argv, true);
58
59 // Daemon launched from first stage init and during selinux transition
60 // will have the command line "-user_snapshot" flag set if the user-space
61 // snapshots are enabled.
62 //
63 // Daemon launched as a init service during "socket-handoff" and when OTA
64 // is applied will check for the property. This is ok as the system
65 // properties are valid at this point. We can't do this during first
66 // stage init and hence use the command line flags to get the information.
67 bool user_snapshots = FLAGS_user_snapshot;
68 if (!user_snapshots) {
69 user_snapshots = IsUserspaceSnapshotsEnabled();
70 }
71 if (user_snapshots) {
72 LOG(INFO) << "Starting daemon for user-space snapshots.....";
73 return StartServerForUserspaceSnapshots(arg_start, argc, argv);
74 } else {
75 LOG(ERROR) << "Userspace snapshots not enabled. No support for legacy snapshots";
76 }
77 return false;
78 }
79
StartServerForUserspaceSnapshots(int arg_start,int argc,char ** argv)80 bool Daemon::StartServerForUserspaceSnapshots(int arg_start, int argc, char** argv) {
81 sigfillset(&signal_mask_);
82 sigdelset(&signal_mask_, SIGINT);
83 sigdelset(&signal_mask_, SIGTERM);
84 sigdelset(&signal_mask_, SIGUSR1);
85
86 // Masking signals here ensure that after this point, we won't handle INT/TERM
87 // until after we call into ppoll()
88 signal(SIGINT, Daemon::SignalHandler);
89 signal(SIGTERM, Daemon::SignalHandler);
90 signal(SIGPIPE, Daemon::SignalHandler);
91 signal(SIGUSR1, Daemon::SignalHandler);
92
93 MaskAllSignalsExceptIntAndTerm();
94
95 user_server_.SetServerRunning();
96 if (FLAGS_io_uring) {
97 user_server_.SetIouringEnabled();
98 }
99
100 if (FLAGS_socket_handoff) {
101 return user_server_.RunForSocketHandoff();
102 }
103 if (!FLAGS_no_socket) {
104 if (!user_server_.Start(FLAGS_socket)) {
105 return false;
106 }
107 return user_server_.Run();
108 }
109
110 for (int i = arg_start; i < argc; i++) {
111 auto parts = android::base::Split(argv[i], ",");
112
113 if (parts.size() != 4) {
114 LOG(ERROR) << "Malformed message, expected at least four sub-arguments.";
115 return false;
116 }
117 auto handler =
118 user_server_.AddHandler(parts[0], parts[1], parts[2], parts[3], FLAGS_o_direct);
119 if (!handler || !user_server_.StartHandler(parts[0])) {
120 return false;
121 }
122 }
123
124 // We reach this point only during selinux transition during device boot.
125 // At this point, all threads are spin up and are ready to serve the I/O
126 // requests for dm-user. Lets inform init.
127 auto client = std::make_unique<SnapuserdClient>();
128 client->NotifyTransitionDaemonIsReady();
129
130 // Skip the accept() call to avoid spurious log spam. The server will still
131 // run until all handlers have completed.
132 return user_server_.WaitForSocket();
133 }
134
MaskAllSignalsExceptIntAndTerm()135 void Daemon::MaskAllSignalsExceptIntAndTerm() {
136 sigset_t signal_mask;
137 sigfillset(&signal_mask);
138 sigdelset(&signal_mask, SIGINT);
139 sigdelset(&signal_mask, SIGTERM);
140 sigdelset(&signal_mask, SIGPIPE);
141 sigdelset(&signal_mask, SIGUSR1);
142 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0) {
143 PLOG(ERROR) << "Failed to set sigprocmask";
144 }
145 }
146
MaskAllSignals()147 void Daemon::MaskAllSignals() {
148 sigset_t signal_mask;
149 sigfillset(&signal_mask);
150 if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0) {
151 PLOG(ERROR) << "Couldn't mask all signals";
152 }
153 }
154
Interrupt()155 void Daemon::Interrupt() {
156 // TODO: We cannot access system property during first stage init.
157 // Until we remove the dm-snapshot code, we will have this check
158 // and verify it through a temp variable.
159 if (user_server_.IsServerRunning()) {
160 user_server_.Interrupt();
161 }
162 }
163
ReceivedSocketSignal()164 void Daemon::ReceivedSocketSignal() {
165 if (user_server_.IsServerRunning()) {
166 user_server_.ReceivedSocketSignal();
167 }
168 }
169
SignalHandler(int signal)170 void Daemon::SignalHandler(int signal) {
171 LOG(DEBUG) << "Snapuserd received signal: " << signal;
172 switch (signal) {
173 case SIGINT:
174 case SIGTERM: {
175 Daemon::Instance().Interrupt();
176 break;
177 }
178 case SIGPIPE: {
179 LOG(ERROR) << "Received SIGPIPE signal";
180 break;
181 }
182 case SIGUSR1: {
183 LOG(INFO) << "Received SIGUSR1, attaching to proxy socket";
184 Daemon::Instance().ReceivedSocketSignal();
185 break;
186 }
187 default:
188 LOG(ERROR) << "Received unknown signal " << signal;
189 break;
190 }
191 }
192
193 } // namespace snapshot
194 } // namespace android
195
main(int argc,char ** argv)196 int main(int argc, char** argv) {
197 android::base::InitLogging(argv, &android::base::KernelLogger);
198
199 android::snapshot::Daemon& daemon = android::snapshot::Daemon::Instance();
200
201 if (!daemon.StartDaemon(argc, argv)) {
202 LOG(ERROR) << "Snapuserd daemon failed to start";
203 exit(EXIT_FAILURE);
204 }
205
206 return 0;
207 }
208