Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
EthanLogLoop.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2016 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * File: EthanLogLoop.h
21  *
22  */
23 
24 #ifndef ETHANLOGLOOP_H
25 #define ETHANLOGLOOP_H
26 
27 #include "ContainerId.h"
28 #include "IDobbyUtils.h"
29 
30 #include <systemd/sd-event.h>
31 
32 #include <memory>
33 #include <string>
34 #include <thread>
35 #include <mutex>
36 #include <deque>
37 #include <list>
38 
39 
40 typedef struct sd_event_source sd_event_source;
41 
42 class EthanLogClient;
43 
44 
46 {
47 public:
48  explicit EthanLogLoop(const std::string& memCgroupMountPoint);
49  ~EthanLogLoop();
50 
51  int addClient(const ContainerId& id, const std::string &tag,
52  unsigned allowedLevels,
53  uint64_t rate = 0, uint64_t burstSize = 0);
54 
55  void setClientBasePid(const ContainerId& id, pid_t basePid);
56 
57 private:
58  void eventLoop();
59  void wakeLoop();
60 
61  static int eventFdHandler(sd_event_source *source, int fd,
62  uint32_t revents, void *userData);
63 
64 
65 private:
66  const std::string mMemCgroupMountPoint;
67 
68  std::thread mThread;
69  std::mutex mLock;
70  int mEventFd;
71 
72  struct Event
73  {
74  enum Type { Terminate, AddClient, SetClientBasePid } type;
75 
76  ContainerId id;
77  int pipeFd;
78  pid_t basePid;
79  std::string tag;
80  unsigned allowedLevels;
81  uint64_t rate;
82  uint64_t burstSize;
83 
84  protected:
85  explicit Event(Type type_)
86  : type(type_), id(), pipeFd(-1), basePid(-1)
87  , allowedLevels(0), rate(0), burstSize(0)
88  { }
89 
90  Event(Type type_, const ContainerId &id_, const std::string &name, int fd,
91  unsigned levels, uint64_t rate, uint64_t burst)
92  : type(type_), id(id_), pipeFd(fd), basePid(-1), tag(name)
93  , allowedLevels(levels) , rate(rate), burstSize(burst)
94  { }
95 
96  Event(Type type_, const ContainerId &id_, pid_t basePid_)
97  : type(type_), id(id_), pipeFd(-1), basePid(basePid_), tag()
98  , allowedLevels(0) , rate(0), burstSize(0)
99  { }
100  };
101 
103  {
105  : Event(Terminate)
106  { }
107  };
108 
110  {
111  AddClientEvent(const ContainerId &id, const std::string &name, int fd,
112  unsigned levels, uint64_t rate, uint64_t burst)
113  : Event(AddClient, id, name, fd, levels, rate, burst)
114  { }
115  };
116 
118  {
119  SetClientBasePidEvent(const ContainerId &id, pid_t basePid)
120  : Event(SetClientBasePid, id, basePid)
121  { }
122  };
123 
124  std::deque<Event> mEvents;
125 
126  std::list< std::unique_ptr<EthanLogClient> > mClients;
127 };
128 
129 
130 #endif // ETHANLOGLOOP_H
A wrapper around a std::string, used to add some type definition to to an id and also to sanity check...
Definition: ContainerId.h:41
Definition: EthanLogClient.h:43
Definition: EthanLogLoop.h:46
void eventLoop()
Event loop thread function.
Definition: EthanLogLoop.cpp:298
void wakeLoop()
Wakes the event loop.
Definition: EthanLogLoop.cpp:178
void setClientBasePid(const ContainerId &id, pid_t basePid)
Sets the base pid number for the given container.
Definition: EthanLogLoop.cpp:149
static int eventFdHandler(sd_event_source *source, int fd, uint32_t revents, void *userData)
Handler for wake ups from the event fd.
Definition: EthanLogLoop.cpp:206
int addClient(const ContainerId &id, const std::string &tag, unsigned allowedLevels, uint64_t rate=0, uint64_t burstSize=0)
Creates a new logging client, which is just pipe with some meta data stored.
Definition: EthanLogLoop.cpp:102
Definition: EthanLogLoop.h:110
Definition: EthanLogLoop.h:73
Definition: EthanLogLoop.h:118
Definition: EthanLogLoop.h:103