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 <arpa/inet.h>
18 #include <cutils/sockets.h>
19 #include <errno.h>
20 #include <netinet/in.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 #include <android-base/logging.h>
29 
30 #include "snapuserd.h"
31 #include "snapuserd_server.h"
32 
33 namespace android {
34 namespace snapshot {
35 
Resolveop(std::string & input)36 DaemonOperations SnapuserdServer::Resolveop(std::string& input) {
37     if (input == "init") return DaemonOperations::INIT;
38     if (input == "start") return DaemonOperations::START;
39     if (input == "stop") return DaemonOperations::STOP;
40     if (input == "query") return DaemonOperations::QUERY;
41     if (input == "delete") return DaemonOperations::DELETE;
42     if (input == "detach") return DaemonOperations::DETACH;
43 
44     return DaemonOperations::INVALID;
45 }
46 
~SnapuserdServer()47 SnapuserdServer::~SnapuserdServer() {
48     // Close any client sockets that were added via AcceptClient().
49     for (size_t i = 1; i < watched_fds_.size(); i++) {
50         close(watched_fds_[i].fd);
51     }
52 }
53 
GetDaemonStatus()54 std::string SnapuserdServer::GetDaemonStatus() {
55     std::string msg = "";
56 
57     if (IsTerminating())
58         msg = "passive";
59     else
60         msg = "active";
61 
62     return msg;
63 }
64 
Parsemsg(std::string const & msg,const char delim,std::vector<std::string> & out)65 void SnapuserdServer::Parsemsg(std::string const& msg, const char delim,
66                                std::vector<std::string>& out) {
67     std::stringstream ss(msg);
68     std::string s;
69 
70     while (std::getline(ss, s, delim)) {
71         out.push_back(s);
72     }
73 }
74 
ShutdownThreads()75 void SnapuserdServer::ShutdownThreads() {
76     StopThreads();
77     JoinAllThreads();
78 }
79 
DmUserHandler(std::shared_ptr<Snapuserd> snapuserd)80 DmUserHandler::DmUserHandler(std::shared_ptr<Snapuserd> snapuserd)
81     : snapuserd_(snapuserd), misc_name_(snapuserd_->GetMiscName()) {}
82 
Sendmsg(android::base::borrowed_fd fd,const std::string & msg)83 bool SnapuserdServer::Sendmsg(android::base::borrowed_fd fd, const std::string& msg) {
84     ssize_t ret = TEMP_FAILURE_RETRY(send(fd.get(), msg.data(), msg.size(), MSG_NOSIGNAL));
85     if (ret < 0) {
86         PLOG(ERROR) << "Snapuserd:server: send() failed";
87         return false;
88     }
89 
90     if (ret < msg.size()) {
91         LOG(ERROR) << "Partial send; expected " << msg.size() << " bytes, sent " << ret;
92         return false;
93     }
94     return true;
95 }
96 
Recv(android::base::borrowed_fd fd,std::string * data)97 bool SnapuserdServer::Recv(android::base::borrowed_fd fd, std::string* data) {
98     char msg[MAX_PACKET_SIZE];
99     ssize_t rv = TEMP_FAILURE_RETRY(recv(fd.get(), msg, sizeof(msg), 0));
100     if (rv < 0) {
101         PLOG(ERROR) << "recv failed";
102         return false;
103     }
104     *data = std::string(msg, rv);
105     return true;
106 }
107 
Receivemsg(android::base::borrowed_fd fd,const std::string & str)108 bool SnapuserdServer::Receivemsg(android::base::borrowed_fd fd, const std::string& str) {
109     const char delim = ',';
110 
111     std::vector<std::string> out;
112     Parsemsg(str, delim, out);
113     DaemonOperations op = Resolveop(out[0]);
114 
115     switch (op) {
116         case DaemonOperations::INIT: {
117             // Message format:
118             // init,<misc_name>,<cow_device_path>,<backing_device>
119             //
120             // Reads the metadata and send the number of sectors
121             if (out.size() != 4) {
122                 LOG(ERROR) << "Malformed init message, " << out.size() << " parts";
123                 return Sendmsg(fd, "fail");
124             }
125 
126             auto handler = AddHandler(out[1], out[2], out[3]);
127             if (!handler) {
128                 return Sendmsg(fd, "fail");
129             }
130 
131             auto retval = "success," + std::to_string(handler->snapuserd()->GetNumSectors());
132             return Sendmsg(fd, retval);
133         }
134         case DaemonOperations::START: {
135             // Message format:
136             // start,<misc_name>
137             //
138             // Start the new thread which binds to dm-user misc device
139             if (out.size() != 2) {
140                 LOG(ERROR) << "Malformed start message, " << out.size() << " parts";
141                 return Sendmsg(fd, "fail");
142             }
143 
144             std::lock_guard<std::mutex> lock(lock_);
145             auto iter = FindHandler(&lock, out[1]);
146             if (iter == dm_users_.end()) {
147                 LOG(ERROR) << "Could not find handler: " << out[1];
148                 return Sendmsg(fd, "fail");
149             }
150             if (!(*iter)->snapuserd() || (*iter)->snapuserd()->IsAttached()) {
151                 LOG(ERROR) << "Tried to re-attach control device: " << out[1];
152                 return Sendmsg(fd, "fail");
153             }
154             if (!StartHandler(*iter)) {
155                 return Sendmsg(fd, "fail");
156             }
157             return Sendmsg(fd, "success");
158         }
159         case DaemonOperations::STOP: {
160             // Message format: stop
161             //
162             // Stop all the threads gracefully and then shutdown the
163             // main thread
164             SetTerminating();
165             ShutdownThreads();
166             return true;
167         }
168         case DaemonOperations::QUERY: {
169             // Message format: query
170             //
171             // As part of transition, Second stage daemon will be
172             // created before terminating the first stage daemon. Hence,
173             // for a brief period client may have to distiguish between
174             // first stage daemon and second stage daemon.
175             //
176             // Second stage daemon is marked as active and hence will
177             // be ready to receive control message.
178             return Sendmsg(fd, GetDaemonStatus());
179         }
180         case DaemonOperations::DELETE: {
181             // Message format:
182             // delete,<misc_name>
183             if (out.size() != 2) {
184                 LOG(ERROR) << "Malformed delete message, " << out.size() << " parts";
185                 return Sendmsg(fd, "fail");
186             }
187             if (!RemoveAndJoinHandler(out[1])) {
188                 return Sendmsg(fd, "fail");
189             }
190             return Sendmsg(fd, "success");
191         }
192         case DaemonOperations::DETACH: {
193             terminating_ = true;
194             return true;
195         }
196         default: {
197             LOG(ERROR) << "Received unknown message type from client";
198             Sendmsg(fd, "fail");
199             return false;
200         }
201     }
202 }
203 
RunThread(std::shared_ptr<DmUserHandler> handler)204 void SnapuserdServer::RunThread(std::shared_ptr<DmUserHandler> handler) {
205     LOG(INFO) << "Entering thread for handler: " << handler->misc_name();
206 
207     if (!handler->snapuserd()->Start()) {
208         LOG(ERROR) << " Failed to launch all worker threads";
209     }
210 
211     handler->snapuserd()->CloseFds();
212     handler->snapuserd()->CheckMergeCompletionStatus();
213     handler->snapuserd()->UnmapBufferRegion();
214 
215     auto misc_name = handler->misc_name();
216     LOG(INFO) << "Handler thread about to exit: " << misc_name;
217 
218     {
219         std::lock_guard<std::mutex> lock(lock_);
220         auto iter = FindHandler(&lock, handler->misc_name());
221         if (iter == dm_users_.end()) {
222             // RemoveAndJoinHandler() already removed us from the list, and is
223             // now waiting on a join(), so just return. Additionally, release
224             // all the resources held by snapuserd object which are shared
225             // by worker threads. This should be done when the last reference
226             // of "handler" is released; but we will explicitly release here
227             // to make sure snapuserd object is freed as it is the biggest
228             // consumer of memory in the daemon.
229             handler->FreeResources();
230             LOG(INFO) << "Exiting handler thread to allow for join: " << misc_name;
231             return;
232         }
233 
234         LOG(INFO) << "Exiting handler thread and freeing resources: " << misc_name;
235 
236         if (handler->snapuserd()->IsAttached()) {
237             handler->thread().detach();
238         }
239 
240         // Important: free resources within the lock. This ensures that if
241         // WaitForDelete() is called, the handler is either in the list, or
242         // it's not and its resources are guaranteed to be freed.
243         handler->FreeResources();
244     }
245 }
246 
Start(const std::string & socketname)247 bool SnapuserdServer::Start(const std::string& socketname) {
248     sockfd_.reset(android_get_control_socket(socketname.c_str()));
249     if (sockfd_ >= 0) {
250         if (listen(sockfd_.get(), 4) < 0) {
251             PLOG(ERROR) << "listen socket failed: " << socketname;
252             return false;
253         }
254     } else {
255         sockfd_.reset(socket_local_server(socketname.c_str(), ANDROID_SOCKET_NAMESPACE_RESERVED,
256                                           SOCK_STREAM));
257         if (sockfd_ < 0) {
258             PLOG(ERROR) << "Failed to create server socket " << socketname;
259             return false;
260         }
261     }
262 
263     AddWatchedFd(sockfd_);
264 
265     LOG(DEBUG) << "Snapuserd server successfully started with socket name " << socketname;
266     return true;
267 }
268 
Run()269 bool SnapuserdServer::Run() {
270     while (!IsTerminating()) {
271         int rv = TEMP_FAILURE_RETRY(poll(watched_fds_.data(), watched_fds_.size(), -1));
272         if (rv < 0) {
273             PLOG(ERROR) << "poll failed";
274             return false;
275         }
276         if (!rv) {
277             continue;
278         }
279 
280         if (watched_fds_[0].revents) {
281             AcceptClient();
282         }
283 
284         auto iter = watched_fds_.begin() + 1;
285         while (iter != watched_fds_.end()) {
286             if (iter->revents && !HandleClient(iter->fd, iter->revents)) {
287                 close(iter->fd);
288                 iter = watched_fds_.erase(iter);
289             } else {
290                 iter++;
291             }
292         }
293     }
294 
295     JoinAllThreads();
296     return true;
297 }
298 
JoinAllThreads()299 void SnapuserdServer::JoinAllThreads() {
300     // Acquire the thread list within the lock.
301     std::vector<std::shared_ptr<DmUserHandler>> dm_users;
302     {
303         std::lock_guard<std::mutex> guard(lock_);
304         dm_users = std::move(dm_users_);
305     }
306 
307     for (auto& client : dm_users) {
308         auto& th = client->thread();
309 
310         if (th.joinable()) th.join();
311     }
312 }
313 
AddWatchedFd(android::base::borrowed_fd fd)314 void SnapuserdServer::AddWatchedFd(android::base::borrowed_fd fd) {
315     struct pollfd p = {};
316     p.fd = fd.get();
317     p.events = POLLIN;
318     watched_fds_.emplace_back(std::move(p));
319 }
320 
AcceptClient()321 void SnapuserdServer::AcceptClient() {
322     int fd = TEMP_FAILURE_RETRY(accept4(sockfd_.get(), nullptr, nullptr, SOCK_CLOEXEC));
323     if (fd < 0) {
324         PLOG(ERROR) << "accept4 failed";
325         return;
326     }
327 
328     AddWatchedFd(fd);
329 }
330 
HandleClient(android::base::borrowed_fd fd,int revents)331 bool SnapuserdServer::HandleClient(android::base::borrowed_fd fd, int revents) {
332     if (revents & POLLHUP) {
333         LOG(DEBUG) << "Snapuserd client disconnected";
334         return false;
335     }
336 
337     std::string str;
338     if (!Recv(fd, &str)) {
339         return false;
340     }
341     if (!Receivemsg(fd, str)) {
342         LOG(ERROR) << "Encountered error handling client message, revents: " << revents;
343         return false;
344     }
345     return true;
346 }
347 
Interrupt()348 void SnapuserdServer::Interrupt() {
349     // Force close the socket so poll() fails.
350     sockfd_ = {};
351     SetTerminating();
352 }
353 
AddHandler(const std::string & misc_name,const std::string & cow_device_path,const std::string & backing_device)354 std::shared_ptr<DmUserHandler> SnapuserdServer::AddHandler(const std::string& misc_name,
355                                                            const std::string& cow_device_path,
356                                                            const std::string& backing_device) {
357     auto snapuserd = std::make_shared<Snapuserd>(misc_name, cow_device_path, backing_device);
358     if (!snapuserd->InitCowDevice()) {
359         LOG(ERROR) << "Failed to initialize Snapuserd";
360         return nullptr;
361     }
362 
363     if (!snapuserd->InitializeWorkers()) {
364         LOG(ERROR) << "Failed to initialize workers";
365         return nullptr;
366     }
367 
368     auto handler = std::make_shared<DmUserHandler>(snapuserd);
369     {
370         std::lock_guard<std::mutex> lock(lock_);
371         if (FindHandler(&lock, misc_name) != dm_users_.end()) {
372             LOG(ERROR) << "Handler already exists: " << misc_name;
373             return nullptr;
374         }
375         dm_users_.push_back(handler);
376     }
377     return handler;
378 }
379 
StartHandler(const std::shared_ptr<DmUserHandler> & handler)380 bool SnapuserdServer::StartHandler(const std::shared_ptr<DmUserHandler>& handler) {
381     if (handler->snapuserd()->IsAttached()) {
382         LOG(ERROR) << "Handler already attached";
383         return false;
384     }
385 
386     handler->snapuserd()->AttachControlDevice();
387 
388     handler->thread() = std::thread(std::bind(&SnapuserdServer::RunThread, this, handler));
389     return true;
390 }
391 
FindHandler(std::lock_guard<std::mutex> * proof_of_lock,const std::string & misc_name)392 auto SnapuserdServer::FindHandler(std::lock_guard<std::mutex>* proof_of_lock,
393                                   const std::string& misc_name) -> HandlerList::iterator {
394     CHECK(proof_of_lock);
395 
396     for (auto iter = dm_users_.begin(); iter != dm_users_.end(); iter++) {
397         if ((*iter)->misc_name() == misc_name) {
398             return iter;
399         }
400     }
401     return dm_users_.end();
402 }
403 
RemoveAndJoinHandler(const std::string & misc_name)404 bool SnapuserdServer::RemoveAndJoinHandler(const std::string& misc_name) {
405     std::shared_ptr<DmUserHandler> handler;
406     {
407         std::lock_guard<std::mutex> lock(lock_);
408 
409         auto iter = FindHandler(&lock, misc_name);
410         if (iter == dm_users_.end()) {
411             // Client already deleted.
412             return true;
413         }
414         handler = std::move(*iter);
415         dm_users_.erase(iter);
416     }
417 
418     auto& th = handler->thread();
419     if (th.joinable()) {
420         th.join();
421     }
422     return true;
423 }
424 
425 }  // namespace snapshot
426 }  // namespace android
427