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/hashset.hpp>
24 #include <stout/nothing.hpp>
25 #include <stout/path.hpp>
26 #include <stout/try.hpp>
27 
28 namespace systemd {
29 
30 // TODO(jmlvanre): Consider moving the generic systemd behaviour into
31 // stout, and leaving the mesos specific behavior here.
32 namespace mesos {
33 
41 // TODO(jmlvanre): We may want to allow this to be configured.
42 static const char MESOS_EXECUTORS_SLICE[] = "mesos_executors.slice";
43 
44 
53 
54 } // namespace mesos {
55 
56 
57 namespace socket_activation {
58 
59 // A re-implementation of the systemd socket activation API.
60 //
61 // To implement the socket-passing protocol, systemd uses the
62 // environment variables `$LISTEN_PID`, `$LISTEN_FDS` and `$LISTEN_FDNAMES`
63 // according to the scheme documented in [1], [2].
64 //
65 // Users of libsystemd can use the following API to interface
66 // with the socket passing functionality:
67 //
68 // #include <systemd/sd-daemon.h>
69 // int sd_listen_fds(int unset_environment);
70 // int sd_listen_fds_with_names(int unset_environment, char ***names);
71 //
72 // The `sd_listen_fds()` function does the following:
73 //
74 // * The return value is the number of listening sockets passed by
75 // systemd. The actual file descriptors of these sockets are
76 // numbered 3...n+3.
77 // * If the current pid is different from the one specified by the
78 // environment variable $LISTEN_PID, 0 is returned
79 // * The `CLOEXEC` option will be set on all file descriptors "returned"
80 // by this function.
81 // * If `unset_environment` is true, the environment variables $LISTEN_PID,
82 // $LISTEN_FDS, $LISTEN_FDNAMES will be cleared.
83 //
84 // The `sd_listen_fds_with_names()` function does the following:
85 //
86 // * If $LISTEN_FDS is set, will return an array of strings with the
87 // names. By default, the name of a socket will be equal to the
88 // name of the unit file containing the socket description.
89 // * The special string "unknown" is used for sockets where no name
90 // could be determined.
91 //
92 // For this reimplementation, the interface was slightly changed to better
93 // suit the needs of the Mesos codebase. However, we still set the `CLOEXEC`
94 // flag on all file descriptors passed via socket activation when one of
95 // these functions is called.
96 //
97 // [1] https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html#Notes
98 // [2] http://0pointer.de/blog/projects/socket-activation.html
99 
101 
102 // The names are set by the `FileDescriptorName=` directive in the unit file.
103 // This requires systemd 227 or newer. Since any number of unit files can
104 // specify the same name, this can return more than one file descriptor.
106 
107 // Clear the `$LISTEN_PID`, `$LISTEN_FDS` and `$LISTEN_FDNAMES` environment
108 // variables.
109 //
110 // *NOTE*: This function is not thread-safe, since it modifies the global
111 // environment.
112 void clearEnvironment();
113 
114 // Defined in `man(3) sd_listen_fds`.
115 constexpr int SD_LISTEN_FDS_START = 3;
116 
117 } // namespace socket_activation {
118 
119 
123 class Flags : public virtual flags::FlagsBase
124 {
125 public:
126  Flags();
127 
128  bool enabled;
129  std::string runtime_directory;
130  std::string cgroups_hierarchy;
131 };
132 
133 
134 const Flags& flags();
135 
136 
143 
144 
157 bool exists();
158 
159 
163 bool enabled();
164 
165 
170 
171 
175 Path hierarchy();
176 
177 
186 
187 namespace slices {
188 
192 bool exists(const Path& path);
193 
194 
203 Try<Nothing> create(const Path& path, const std::string& data);
204 
205 
209 Try<Nothing> start(const std::string& name);
210 
211 } // namespace slices {
212 
213 } // namespace systemd {
214 
215 #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
std::string runtime_directory
Definition: systemd.hpp:129
Try< Nothing > start(const std::string &name)
Starts the slice with the given name (via &#39;systemctl start <name>&#39;).
Definition: systemd.hpp:28
Path runtimeDirectory()
Returns the path to the runtime directory for systemd units.
DWORD pid_t
Definition: windows.hpp:181
bool enabled
Definition: systemd.hpp:128
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:115
Try< Nothing > daemonReload()
Runs systemctl daemon-reload.
std::string cgroups_hierarchy
Definition: systemd.hpp:130
Path hierarchy()
Return the path to the systemd hierarchy.
Try< std::vector< int > > listenFdsWithNames(const hashset< std::string > &names)
Flags to initialize systemd state.
Definition: systemd.hpp:123
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:41