Apache Mesos
containerizer.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 __MESOS_SLAVE_CONTAINERIZER_HPP__
18 #define __MESOS_SLAVE_CONTAINERIZER_HPP__
19 
20 #include <string>
21 
22 #include <process/shared.hpp>
23 #include <process/subprocess.hpp>
24 
25 #include <stout/option.hpp>
26 
27 // ONLY USEFUL AFTER RUNNING PROTOC.
28 #include <mesos/slave/containerizer.pb.h>
29 
30 namespace mesos {
31 namespace slave {
32 
38 {
52  class IO
53  {
54  public:
55  enum class Type
56  {
57  FD,
58  PATH
59  };
60 
61  static IO PATH(const std::string& path)
62  {
63  return IO(Type::PATH, path);
64  }
65 
66  static IO FD(int_fd fd, bool closeOnDestruction = true)
67  {
68  return IO(Type::FD, fd, closeOnDestruction);
69  }
70 
71  operator process::Subprocess::IO () const
72  {
73  switch (type_) {
74  case Type::FD:
75  return process::Subprocess::FD(*fd_);
76  case Type::PATH:
77  return process::Subprocess::PATH(path_.get());
78  default:
79  UNREACHABLE();
80  }
81  }
82 
83  private:
84  // A simple abstraction to wrap an FD and (optionally) close it
85  // on destruction. We know that we never copy instances of this
86  // class once they are instantiated, so it's OK to call
87  // `close()` in the destructor since only one reference will
88  // ever exist to it.
89  class FDWrapper
90  {
91  public:
92  FDWrapper(int_fd _fd, bool _closeOnDestruction)
93  : fd(_fd), closeOnDestruction(_closeOnDestruction) {}
94 
95  ~FDWrapper() {
96  CHECK(fd >= 0);
97  if (closeOnDestruction) {
98  close(fd);
99  }
100  }
101 
102  operator int_fd() const { return fd; }
103 
104  private:
105  FDWrapper(const FDWrapper& fd) = delete;
106 
107  int_fd fd;
108  bool closeOnDestruction;
109  };
110 
111  IO(Type _type, int_fd _fd, bool closeOnDestruction)
112  : type_(_type),
113  fd_(new FDWrapper(_fd, closeOnDestruction)),
114  path_(None()) {}
115 
116  IO(Type _type, const std::string& _path)
117  : type_(_type),
118  fd_(),
119  path_(_path) {}
120 
121  Type type_;
123  Option<std::string> path_;
124  };
125 
131 
137 
142 };
143 
144 } // namespace slave {
145 } // namespace mesos {
146 
147 #endif // __MESOS_SLAVE_CONTAINERIZER_HPP__
Describes how the containerizer redirects I/O for stdin/stdout/stderr of a container.
Definition: containerizer.hpp:52
static IO PATH(const std::string &path)
An abstraction around the IO classes used to redirect stdin/stdout/stderr to/from a container by the ...
Definition: containerizer.hpp:37
static IO FD(int_fd fd, bool closeOnDestruction=true)
Definition: containerizer.hpp:66
#define STDERR_FILENO
Definition: windows.hpp:161
IO in
How to redirect the stdin of the executable.
Definition: containerizer.hpp:130
static IO FD(int_fd fd, IO::FDType type=IO::DUPLICATED)
#define STDOUT_FILENO
Definition: windows.hpp:160
Describes how the I/O is redirected for stdin/stdout/stderr.
Definition: subprocess.hpp:61
const T & get() const &
Definition: option.hpp:118
#define STDIN_FILENO
Definition: windows.hpp:159
IO err
Similar to out, except this describes how to redirect stderr.
Definition: containerizer.hpp:141
#define UNREACHABLE()
Definition: unreachable.hpp:22
Definition: none.hpp:27
static IO PATH(const std::string &path)
Definition: containerizer.hpp:61
int int_fd
Definition: int_fd.hpp:35
Type
Definition: containerizer.hpp:55
IO out
How to redirect the stdout of the executable.
Definition: containerizer.hpp:136