Belle II Software  release-08-01-10
Fifo.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include "daq/slc/system/Fifo.h"
9 
10 #include <daq/slc/base/IOException.h>
11 
12 #include <unistd.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 
16 #include <cstdio>
17 
18 using namespace Belle2;
19 
20 Fifo Fifo::mkfifo(const std::string& path)
21 {
22  ::mkfifo(path.c_str(), 0666);
23  Fifo fifo;
24  fifo.open(path.c_str());
25  return fifo;
26 }
27 
28 void Fifo::open(const std::string& path, const std::string& mode_s)
29 {
30  int mode = O_RDONLY;
31  if (mode_s.find("w") != std::string::npos) {
32  mode = O_WRONLY;
33  if (mode_s.find("r") != std::string::npos) {
34  mode = O_RDWR;
35  }
36  }
37  if ((m_fd = ::open(path.c_str(), mode)) < 0) {
38  perror("open");
39  throw (IOException("Failed to open fifo."));
40  }
41 }
42 
43 void Fifo::unlink(const std::string& path)
44 {
45  if ((::unlink(path.c_str())) < 0) {
46  perror("unlink");
47  }
48  close();
49 }
50 
51 size_t Fifo::write(const void* buf, size_t count)
52 {
53  return ::write(m_fd, buf, count);
54 }
55 
56 size_t Fifo::read(void* buf, size_t count)
57 {
58  return ::read(m_fd, buf, count);
59 }
Abstract base class for different kinds of events.