1#include "fifo.h"
2
3#include <base/log.h>
4#include <base/str.h>
5#if defined(CONF_FAMILY_UNIX)
6#include <base/fs.h>
7
8#include <fcntl.h>
9#include <sys/stat.h>
10#include <sys/types.h>
11#include <unistd.h>
12
13#include <cerrno>
14#include <cstdlib>
15
16void CFifo::Init(IConsole *pConsole, const char *pFifoFile, int Flag)
17{
18 m_File = -1;
19
20 m_pConsole = pConsole;
21 m_IsInit = true;
22 if(pFifoFile[0] == '\0')
23 return;
24
25 str_copy(dst&: m_aFilename, src: pFifoFile);
26 m_Flag = Flag;
27
28 bool IsFifo;
29 if(mkfifo(path: m_aFilename, mode: 0600) == 0)
30 {
31 struct stat Attribute;
32 if(stat(file: m_aFilename, buf: &Attribute) == 0)
33 {
34 IsFifo = S_ISFIFO(Attribute.st_mode);
35 }
36 else
37 {
38 log_warn("fifo", "Failed to stat fifo '%s' (%d '%s')", m_aFilename, errno, strerror(errno));
39 IsFifo = false;
40 }
41 }
42 else
43 {
44 log_warn("fifo", "Failed to create fifo '%s' (%d '%s')", m_aFilename, errno, strerror(errno));
45 IsFifo = false;
46 }
47 if(!IsFifo)
48 {
49 log_warn("fifo", "File '%s' is not a fifo, removing", m_aFilename);
50 if(fs_remove(filename: m_aFilename) != 0)
51 {
52 log_error("fifo", "Failed to remove non-fifo '%s'", m_aFilename); // details logged in fs_remove
53 return;
54 }
55 if(mkfifo(path: m_aFilename, mode: 0600) != 0)
56 {
57 log_error("fifo", "Failed to create fifo '%s' (%d '%s')", m_aFilename, errno, strerror(errno));
58 return;
59 }
60 struct stat Attribute;
61 if(stat(file: m_aFilename, buf: &Attribute) != 0)
62 {
63 log_error("fifo", "Failed to stat fifo '%s' (%d '%s')", m_aFilename, errno, strerror(errno));
64 return;
65 }
66 if(!S_ISFIFO(Attribute.st_mode))
67 {
68 log_error("fifo", "File '%s' is not a fifo", m_aFilename);
69 return;
70 }
71 }
72
73 m_File = open(file: m_aFilename, O_RDONLY | O_NONBLOCK);
74 if(m_File < 0)
75 {
76 log_error("fifo", "Failed to open fifo '%s' (%d '%s')", m_aFilename, errno, strerror(errno));
77 }
78 else
79 {
80 log_info("fifo", "Opened fifo '%s'", m_aFilename);
81 }
82}
83
84void CFifo::Shutdown()
85{
86 if(m_File < 0)
87 return;
88
89 close(fd: m_File);
90 (void)fs_remove(filename: m_aFilename);
91}
92
93void CFifo::Update()
94{
95 if(m_File < 0)
96 return;
97
98 char aBuf[8192];
99 int Length = read(fd: m_File, buf: aBuf, nbytes: sizeof(aBuf) - 1);
100 if(Length <= 0)
101 return;
102 aBuf[Length] = '\0';
103
104 char *pCur = aBuf;
105 for(int i = 0; i < Length; ++i)
106 {
107 if(aBuf[i] != '\n')
108 continue;
109 aBuf[i] = '\0';
110 if(str_utf8_check(str: pCur))
111 {
112 m_pConsole->ExecuteLineFlag(pStr: pCur, FlasgMask: m_Flag, ClientId: IConsole::CLIENT_ID_UNSPECIFIED);
113 }
114 pCur = aBuf + i + 1;
115 }
116 if(pCur < aBuf + Length && str_utf8_check(str: pCur)) // missed the last line
117 {
118 m_pConsole->ExecuteLineFlag(pStr: pCur, FlasgMask: m_Flag, ClientId: IConsole::CLIENT_ID_UNSPECIFIED);
119 }
120}
121
122#elif defined(CONF_FAMILY_WINDOWS)
123
124#include <base/windows.h>
125
126#include <windows.h>
127
128void CFifo::Init(IConsole *pConsole, const char *pFifoFile, int Flag)
129{
130 m_pConsole = pConsole;
131 m_IsInit = true;
132 if(pFifoFile[0] == '\0')
133 {
134 m_pPipe = INVALID_HANDLE_VALUE;
135 return;
136 }
137
138 str_copy(m_aFilename, "\\\\.\\pipe\\");
139 str_append(m_aFilename, pFifoFile);
140 m_Flag = Flag;
141
142 const std::wstring WideFilename = windows_utf8_to_wide(m_aFilename);
143 m_pPipe = CreateNamedPipeW(WideFilename.c_str(),
144 PIPE_ACCESS_DUPLEX,
145 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT | PIPE_REJECT_REMOTE_CLIENTS,
146 PIPE_UNLIMITED_INSTANCES,
147 8192,
148 8192,
149 NMPWAIT_USE_DEFAULT_WAIT,
150 nullptr);
151 if(m_pPipe == INVALID_HANDLE_VALUE)
152 {
153 const DWORD LastError = GetLastError();
154 log_error("fifo", "Failed to create named pipe '%s' (%ld '%s')", m_aFilename, LastError, windows_format_system_message(LastError).c_str());
155 }
156 else
157 {
158 log_info("fifo", "Created named pipe '%s'", m_aFilename);
159 }
160}
161
162void CFifo::Shutdown()
163{
164 if(m_pPipe == INVALID_HANDLE_VALUE)
165 return;
166
167 DisconnectNamedPipe(m_pPipe);
168 CloseHandle(m_pPipe);
169 m_pPipe = INVALID_HANDLE_VALUE;
170}
171
172void CFifo::Update()
173{
174 if(m_pPipe == INVALID_HANDLE_VALUE)
175 return;
176
177 if(!ConnectNamedPipe(m_pPipe, nullptr))
178 {
179 const DWORD LastError = GetLastError();
180 if(LastError == ERROR_PIPE_LISTENING) // waiting for clients to connect
181 return;
182 if(LastError == ERROR_NO_DATA) // pipe was disconnected from the other end, also disconnect it from this end
183 {
184 // disconnect the previous client so we can connect to a new one
185 DisconnectNamedPipe(m_pPipe);
186 return;
187 }
188 if(LastError != ERROR_PIPE_CONNECTED) // pipe already connected, not an error
189 {
190 log_error("fifo", "Failed to connect named pipe '%s' (%ld '%s')", m_aFilename, LastError, windows_format_system_message(LastError).c_str());
191 return;
192 }
193 }
194
195 while(true) // read all messages from the pipe
196 {
197 DWORD BytesAvailable;
198 if(!PeekNamedPipe(m_pPipe, nullptr, 0, nullptr, &BytesAvailable, nullptr))
199 {
200 const DWORD LastError = GetLastError();
201 if(LastError == ERROR_BROKEN_PIPE)
202 {
203 // Pipe was disconnected from the other side, either immediately
204 // after connecting or after reading the previous message.
205 DisconnectNamedPipe(m_pPipe);
206 }
207 else
208 {
209 log_error("fifo", "Failed to peek at pipe '%s' (%ld '%s')", m_aFilename, LastError, windows_format_system_message(LastError).c_str());
210 }
211 return;
212 }
213 if(BytesAvailable == 0) // pipe connected but no data available
214 return;
215
216 char *pBuf = static_cast<char *>(malloc(BytesAvailable + 1));
217 DWORD Length;
218 if(!ReadFile(m_pPipe, pBuf, BytesAvailable, &Length, nullptr))
219 {
220 const DWORD LastError = GetLastError();
221 log_error("fifo", "Failed to read from pipe '%s' (%ld '%s')", m_aFilename, LastError, windows_format_system_message(LastError).c_str());
222 free(pBuf);
223 return;
224 }
225 pBuf[Length] = '\0';
226
227 char *pCur = pBuf;
228 for(DWORD i = 0; i < Length; ++i)
229 {
230 if(pBuf[i] != '\n' && (pBuf[i] != '\r' || pBuf[i + 1] != '\n'))
231 {
232 continue;
233 }
234 if(pBuf[i] == '\r')
235 {
236 pBuf[i] = '\0';
237 ++i;
238 }
239 pBuf[i] = '\0';
240 if(pCur[0] != '\0' && str_utf8_check(pCur))
241 {
242 m_pConsole->ExecuteLineFlag(pCur, m_Flag, IConsole::CLIENT_ID_UNSPECIFIED);
243 }
244 pCur = pBuf + i + 1;
245 }
246 if(pCur < pBuf + Length && pCur[0] != '\0' && str_utf8_check(pCur)) // missed the last line
247 {
248 m_pConsole->ExecuteLineFlag(pCur, m_Flag, IConsole::CLIENT_ID_UNSPECIFIED);
249 }
250
251 free(pBuf);
252 }
253}
254#endif
255