winss
service.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
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 #ifndef LIB_WINSS_SVSCAN_SERVICE_HPP_
18 #define LIB_WINSS_SVSCAN_SERVICE_HPP_
19 
20 #include <windows.h>
21 #include <filesystem>
22 #include <utility>
23 #include <string>
24 #include "easylogging/easylogging++.hpp"
25 #include "../windows_interface.hpp"
26 #include "../filesystem_interface.hpp"
27 #include "../handle_wrapper.hpp"
28 #include "service_process.hpp"
29 
30 namespace fs = std::experimental::filesystem;
31 
32 namespace winss {
41 template<typename TServiceProcess>
42 class ServiceTmpl {
43  protected:
44  std::string name;
45  TServiceProcess main;
46  TServiceProcess log;
47  bool flagged = false;
55  SECURITY_ATTRIBUTES sa;
56  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
57  sa.bInheritHandle = FALSE;
58  sa.lpSecurityDescriptor = nullptr;
59 
60  HANDLE stdin_pipe = nullptr;
61  HANDLE stdout_pipe = nullptr;
62 
63  if (!WINDOWS.CreatePipe(&stdin_pipe, &stdout_pipe, &sa, 0)) {
64  VLOG(1) << "CreatePipe() failed: " << ::GetLastError();
65  return winss::ServicePipes{};
66  }
67 
68  return winss::ServicePipes{
69  winss::HandleWrapper(stdin_pipe),
70  winss::HandleWrapper(stdout_pipe)
71  };
72  }
73 
74  public:
75  static constexpr const char kLogDir[4] = "log";
81 
87  explicit ServiceTmpl(const std::string& name) : name(name),
88  main(TServiceProcess(name)),
89  log(TServiceProcess(name / fs::path(kLogDir))) {}
90 
91  ServiceTmpl(const ServiceTmpl&) = delete;
98  ServiceTmpl(ServiceTmpl&& s) : name(std::move(s.name)),
99  main(std::move(s.main)), log(std::move(s.log)),
100  flagged(s.flagged) {}
101 
107  virtual const std::string& GetName() const {
108  return name;
109  }
110 
116  virtual bool IsFlagged() const {
117  return flagged;
118  }
119 
123  virtual void Reset() {
124  flagged = false;
125  }
126 
130  virtual void Check() {
131  flagged = true;
132 
133  if (main.IsCreated()) {
134  return;
135  }
136 
137  winss::ServicePipes pipes;
138 
139  if (FILESYSTEM.DirectoryExists(log.GetServiceDir())) {
140  VLOG(3) << "Log directory exists for service " << name;
141  pipes = CreatePipes();
142  log.Start(pipes, true);
143  }
144 
145  main.Start(pipes, false);
146  }
147 
154  virtual bool Close(bool ignore_flagged) {
155  if (ignore_flagged || !flagged) {
156  main.Close();
157  log.Close();
158  flagged = false;
159  }
160 
161  return flagged;
162  }
163 
164  ServiceTmpl& operator=(const ServiceTmpl&) = delete;
173  name = std::move(s.name);
174  main = std::move(s.main);
175  log = std::move(s.log);
176  flagged = s.flagged;
177  return *this;
178  }
179 };
180 
185 } // namespace winss
186 
187 #endif // LIB_WINSS_SVSCAN_SERVICE_HPP_
A wrapper for a Windows HANDLE.
Definition: handle_wrapper.hpp:39
ServiceTmpl()
The default service template constructor.
Definition: service.hpp:80
TServiceProcess log
The log supervisor.
Definition: service.hpp:46
ServiceTmpl & operator=(ServiceTmpl &&s)
Moves the service object to this object.
Definition: service.hpp:172
#define WINDOWS
Definition: windows_interface.hpp:25
ServiceTmpl(const std::string &name)
Initializes the service with the name and directory.
Definition: service.hpp:87
Definition: case_ignore.hpp:23
virtual void Check()
Checks the service is running.
Definition: service.hpp:130
bool flagged
Flagged for removal.
Definition: service.hpp:47
TServiceProcess main
The main supervisor.
Definition: service.hpp:45
winss::ServicePipes CreatePipes()
Creates pipes for redirecting STDIN and STDOUT.
Definition: service.hpp:54
A template for a service.
Definition: service.hpp:42
virtual bool Close(bool ignore_flagged)
Close the service.
Definition: service.hpp:154
static constexpr const char kLogDir[4]
The log definition.
Definition: service.hpp:75
#define FILESYSTEM
Definition: filesystem_interface.hpp:26
ServiceTmpl< winss::ServiceProcess > Service
Concrete service implementation.
Definition: service.hpp:184
virtual const std::string & GetName() const
Gets the name of the service.
Definition: service.hpp:107
Holds the STDIN and STDOUT pipes for redirecting.
Definition: service_process.hpp:34
ServiceTmpl(ServiceTmpl &&s)
Creates a new service and moves it from an old one.
Definition: service.hpp:98
ServiceTmpl & operator=(const ServiceTmpl &)=delete
No copy.
std::string name
The name of the service.
Definition: service.hpp:44
virtual bool IsFlagged() const
Gets if the service is flagged or not.
Definition: service.hpp:116
virtual void Reset()
Resets the service.
Definition: service.hpp:123