1 // Copyright (C) 2020 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 #pragma once
16 
17 #include <poll.h>
18 
19 #include <string>
20 #include <vector>
21 
22 #include "dm-snapshot-merge/snapuserd_server.h"
23 #include "user-space-merge/snapuserd_server.h"
24 
25 namespace android {
26 namespace snapshot {
27 
28 class Daemon {
29     // The Daemon class is a singleton to avoid
30     // instantiating more than once
31   public:
Daemon()32     Daemon() {}
33 
Instance()34     static Daemon& Instance() {
35         static Daemon instance;
36         return instance;
37     }
38 
39     bool StartServerForDmSnapshot(int arg_start, int argc, char** argv);
40     bool StartServerForUserspaceSnapshots(int arg_start, int argc, char** argv);
41     void Interrupt();
42     void ReceivedSocketSignal();
43     bool IsUserspaceSnapshotsEnabled();
44     bool IsDmSnapshotTestingEnabled();
45     bool StartDaemon(int argc, char** argv);
46 
47   private:
48     // Signal mask used with ppoll()
49     sigset_t signal_mask_;
50 
51     Daemon(Daemon const&) = delete;
52     void operator=(Daemon const&) = delete;
53 
54     SnapuserdServer server_;
55     UserSnapshotServer user_server_;
56     void MaskAllSignalsExceptIntAndTerm();
57     void MaskAllSignals();
58     static void SignalHandler(int signal);
59 };
60 
61 }  // namespace snapshot
62 }  // namespace android
63