w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RlinkPortFifo.cpp
Go to the documentation of this file.
1// $Id: RlinkPortFifo.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2017-04-15 875 1.2.1 Open(): set default scheme
8// 2015-04-12 666 1.2 add xon,noinit attributes
9// 2013-02-23 492 1.1 use RparseUrl
10// 2011-03-27 374 1.0 Initial version
11// 2011-01-15 356 0.1 First draft
12// ---------------------------------------------------------------------------
13
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <errno.h>
22#include <unistd.h>
23
24#include "RlinkPortFifo.hpp"
25
26using namespace std;
27
33// all method definitions in namespace Retro
34namespace Retro {
35
36//------------------------------------------+-----------------------------------
38
40 : RlinkPort()
41{}
42
43//------------------------------------------+-----------------------------------
45
47{
48 // no need to call Close() here, no RlinkPortFifo::Close()
49 // cleanup will be done by ~RlinkPort()
50}
51
52//------------------------------------------+-----------------------------------
54
55bool RlinkPortFifo::Open(const std::string& url, RerrMsg& emsg)
56{
57 if (IsOpen()) Close();
58
59 if (!fUrl.Set(url, "|keep|xon|noinit|", "fifo", emsg)) return false;
60
61 // Note: _rx fifo must be opened before the _tx fifo, otherwise the test
62 // bench might close with EOF on read prematurely (is a race condition).
63
64 fFdWrite = OpenFifo(fUrl.Path() + "_rx", true, emsg);
65 if (fFdWrite < 0) return false;
66
67 fFdRead = OpenFifo(fUrl.Path() + "_tx", false, emsg);
68 if (fFdRead < 0) {
69 ::close(fFdWrite);
70 fFdWrite = -1;
71 return false;
72 }
73
74 fXon = fUrl.FindOpt("xon");
75 fIsOpen = true;
76
77 return true;
78}
79
80//------------------------------------------+-----------------------------------
82
83int RlinkPortFifo::OpenFifo(const std::string& name, bool snd, RerrMsg& emsg)
84{
85 struct stat stat_fifo;
86 int irc = ::stat(name.c_str(), &stat_fifo);
87 if (irc == 0) {
88 if ((stat_fifo.st_mode & S_IFIFO) == 0) {
89 emsg.Init("RlinkPortFifo::OpenFiFo()",
90 string("'") + name + "' exists but is not a pipe");
91 return -1;
92 }
93 } else {
94 mode_t mode = S_IRUSR | S_IWUSR; // user read and write allowed
95 irc = ::mkfifo(name.c_str(), mode);
96 if (irc != 0) {
97 emsg.InitErrno("RlinkPortFifo::OpenFifo()",
98 string("mkfifo() for '") + name + "' failed: ",
99 errno);
100 return -1;
101 }
102 }
103
104 /* coverity[toctou] */
105 irc = ::open(name.c_str(), snd ? O_WRONLY : O_RDONLY);
106 if (irc < 0) {
107 emsg.InitErrno("RlinkPortFifo::OpenFifo()",
108 string("open() for '") + name + "' failed: ",
109 errno);
110 return -1;
111 }
112
113 return irc;
114}
115
116} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
void Init(const std::string &meth, const std::string &text)
FIXME_docs.
Definition: RerrMsg.cpp:74
void InitErrno(const std::string &meth, const std::string &text, int errnum)
FIXME_docs.
Definition: RerrMsg.cpp:84
RlinkPortFifo()
Default constructor.
virtual bool Open(const std::string &url, RerrMsg &emsg)
FIXME_docs.
virtual ~RlinkPortFifo()
Destructor.
int OpenFifo(const std::string &, bool snd, RerrMsg &emsg)
FIXME_docs.
FIXME_docs.
Definition: RlinkPort.hpp:45
int fFdWrite
fd for write
Definition: RlinkPort.hpp:109
bool IsOpen() const
FIXME_docs.
Definition: RlinkPort.ipp:27
RparseUrl fUrl
parsed url
Definition: RlinkPort.hpp:106
int fFdRead
fd for read
Definition: RlinkPort.hpp:108
bool fIsOpen
is open flag
Definition: RlinkPort.hpp:105
bool fXon
xon attribute set
Definition: RlinkPort.hpp:107
virtual void Close()
FIXME_docs.
Definition: RlinkPort.cpp:98
bool FindOpt(const std::string &name) const
FIXME_docs.
Definition: RparseUrl.cpp:196
bool Set(const std::string &url, const std::string &optlist, RerrMsg &emsg)
FIXME_docs.
Definition: RparseUrl.cpp:55
const std::string & Path() const
FIXME_docs.
Definition: RparseUrl.ipp:45
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47