Apache Mesos
systemd.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __SYSTEMD_HPP__
18 #define __SYSTEMD_HPP__
19 
20 #include <process/subprocess.hpp>
21 
22 #include <stout/flags.hpp>
23 #include <stout/nothing.hpp>
24 #include <stout/path.hpp>
25 #include <stout/try.hpp>
26 
27 namespace systemd {
28 
29 // TODO(jmlvanre): Consider moving the generic systemd behaviour into
30 // stout, and leaving the mesos specific behavior here.
31 namespace mesos {
32 
40 // TODO(jmlvanre): We may want to allow this to be configured.
41 static const char MESOS_EXECUTORS_SLICE[] = "mesos_executors.slice";
42 
43 
52 
53 } // namespace mesos {
54 
55 
56 namespace socket_activation {
57 
58 // A re-implementation of the systemd socket activation API.
59 //
60 // To implement the socket-passing protocol, systemd uses the
61 // environment variables `$LISTEN_PID`, `$LISTEN_FDS` and `$LISTEN_FDNAMES`
62 // according to the scheme documented in [1], [2].
63 //
64 // Users of libsystemd can use the following API to interface
65 // with the socket passing functionality:
66 //
67 // #include <systemd/sd-daemon.h>
68 // int sd_listen_fds(int unset_environment);
69 // int sd_listen_fds_with_names(int unset_environment, char ***names);
70 //
71 // The `sd_listen_fds()` function does the following:
72 //
73 // * The return value is the number of listening sockets passed by
74 // systemd. The actual file descriptors of these sockets are
75 // numbered 3...n+3.
76 // * If the current pid is different from the one specified by the
77 // environment variable $LISTEN_PID, 0 is returned
78 // * The `CLOEXEC` option will be set on all file descriptors "returned"
79 // by this function.
80 // * If `unset_environment` is true, the environment variables $LISTEN_PID,
81 // $LISTEN_FDS, $LISTEN_FDNAMES will be cleared.
82 //
83 // The `sd_listen_fds_with_names()` function does the following:
84 //
85 // * If $LISTEN_FDS is set, will return an array of strings with the
86 // names. By default, the name of a socket will be equal to the
87 // name of the unit file containing the socket description.
88 // * The special string "unknown" is used for sockets where no name
89 // could be determined.
90 //
91 // For this reimplementation, the interface was slightly changed to better
92 // suit the needs of the Mesos codebase. However, we still set the `CLOEXEC`
93 // flag on all file descriptors passed via socket activation when one of
94 // these functions is called.
95 //
96 // [1] https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html#Notes
97 // [2] http://0pointer.de/blog/projects/socket-activation.html
98 
100 
101 // The names are set by the `FileDescriptorName=` directive in the unit file.
102 // This requires systemd 227 or newer. Since any number of unit files can
103 // specify the same name, this can return more than one file descriptor.
104 Try<std::vector<int>> listenFdsWithName(const std::string& name);
105 
106 // Clear the `$LISTEN_PID`, `$LISTEN_FDS` and `$LISTEN_FDNAMES` environment
107 // variables.
108 //
109 // *NOTE*: This function is not thread-safe, since it modifies the global
110 // environment.
111 void clearEnvironment();
112 
113 // Defined in `man(3) sd_listen_fds`.
114 constexpr int SD_LISTEN_FDS_START = 3;
115 
116 } // namespace socket_activation {
117 
118 
122 class Flags : public virtual flags::FlagsBase
123 {
124 public:
125  Flags();
126 
127  bool enabled;
128  std::string runtime_directory;
129  std::string cgroups_hierarchy;
130 };
131 
132 
133 const Flags& flags();
134 
135 
142 
143 
156 bool exists();
157 
158 
162 bool enabled();
163 
164 
169 
170 
174 Path hierarchy();
175 
176 
185 
186 namespace slices {
187 
191 bool exists(const Path& path);
192 
193 
202 Try<Nothing> create(const Path& path, const std::string& data);
203 
204 
208 Try<Nothing> start(const std::string& name);
209 
210 } // namespace slices {
211 
212 } // namespace systemd {
213 
214 #endif // __SYSTEMD_HPP__
bool enabled()
Check if systemd exists, and whether we have initialized it.
Definition: path.hpp:29
Try< std::vector< int > > listenFds()
Try< Nothing > extendLifetime(pid_t child)
A hook that is executed in the parent process.
Try< Nothing > create(const Path &path, const std::string &data)
Creates a slice configuration with the provided contents at the given path.
Definition: check.hpp:33
Try< std::vector< int > > listenFdsWithName(const std::string &name)
std::string runtime_directory
Definition: systemd.hpp:128
Try< Nothing > start(const std::string &name)
Starts the slice with the given name (via &#39;systemctl start <name>&#39;).
Definition: systemd.hpp:27
Path runtimeDirectory()
Returns the path to the runtime directory for systemd units.
DWORD pid_t
Definition: windows.hpp:181
bool enabled
Definition: systemd.hpp:127
Try< Nothing > initialize(const Flags &flags)
Initialized state for support of systemd functions in this file.
Represents a POSIX or Windows file system path and offers common path manipulations.
Definition: path.hpp:212
Definition: flags.hpp:44
Definition: agent.hpp:25
constexpr int SD_LISTEN_FDS_START
Definition: systemd.hpp:114
Try< Nothing > daemonReload()
Runs systemctl daemon-reload.
std::string cgroups_hierarchy
Definition: systemd.hpp:129
Path hierarchy()
Return the path to the systemd hierarchy.
Flags to initialize systemd state.
Definition: systemd.hpp:122
bool exists()
Check if we are on a systemd environment by: (1) Testing whether /sbin/init links to systemd...
const Flags & flags()
Definition: parse.hpp:33
constexpr const char * name
Definition: shell.hpp:43