Repository navigation
Expand file tree
/
Copy pathprocess_stream.cpp
More file actions
191 lines (153 loc) · 5.82 KB
/
Copy pathprocess_stream.cpp
File metadata and controls
191 lines (153 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/***
This file is part of snapcast
Copyright (C) 2014-2025 Johannes Pohl
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
// prototype/interface header file
#include "process_stream.hpp"
// local headers
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/utils.hpp"
#include "common/utils/file_utils.hpp"
// standard headers
#include <cstdio>
using namespace std;
namespace streamreader
{
static constexpr auto LOG_TAG = "ProcessStream";
ProcessStream::ProcessStream(PcmStream::Listener* pcmListener, boost::asio::io_context& ioc, const ServerSettings& server_settings, const StreamUri& uri,
PcmStream::Source source)
: AsioStream<stream_descriptor>(pcmListener, ioc, server_settings, uri, source)
{
params_ = uri_.getQuery("params");
wd_timeout_sec_ = cpt::stoul(uri_.getQuery("wd_timeout", "0"));
LOG(DEBUG, LOG_TAG) << "Watchdog timeout: " << wd_timeout_sec_ << "\n";
logStderr_ = (uri_.getQuery("log_stderr", "false") == "true");
if (source == Source::rpc)
{
auto exe_path = utils::file::isInDirectory(uri.path, server_settings.stream.sandbox_dir);
// check if exe is located in sandbox_dir
if (!exe_path)
throw SnapException("Process stream executable must be located in '" + server_settings.stream.sandbox_dir.native() + "'");
uri_.path = exe_path->native();
}
}
std::string ProcessStream::findExe(const std::string& filename) const
{
/// check if filename exists
if (utils::file::exists(filename))
return filename;
std::string exe = filename;
if (exe.find('/') != string::npos)
exe = exe.substr(exe.find_last_of('/') + 1);
/// check with "which"
string which = execGetOutput("which " + exe);
if (!which.empty())
return which;
/// check in the same path as this binary
std::array<char, PATH_MAX> buff;
std::array<char, 32> szTmp;
sprintf(szTmp.data(), "/proc/%d/exe", getpid());
ssize_t len = readlink(szTmp.data(), buff.data(), buff.size() - 1);
if (len != -1)
{
buff[len] = '\0';
return string(buff.data()) + "/" + exe;
}
return "";
}
void ProcessStream::initExeAndPath(const std::string& filename)
{
path_ = "";
exe_ = findExe(filename);
if (exe_.find('/') != string::npos)
{
path_ = exe_.substr(0, exe_.find_last_of('/') + 1);
exe_ = exe_.substr(exe_.find_last_of('/') + 1);
}
if (!utils::file::exists(path_ + exe_))
throw SnapException("file not found: \"" + filename + "\"");
}
void ProcessStream::connect()
{
if (!active_)
return;
initExeAndPath(uri_.path);
LOG(DEBUG, LOG_TAG) << "Launching: '" << path_ + exe_ << "', with params: '" << params_ << "', in path: '" << path_ << "'\n";
pipe_stdout_ = bp::pipe();
// could use bp::async_pipe, but this is broken in boost 1.72:
// https://github.com/boostorg/process/issues/116
pipe_stderr_ = bp::pipe();
// stdout pipe should not block
int flags = fcntl(pipe_stdout_.native_source(), F_GETFL, 0);
fcntl(pipe_stdout_.native_source(), F_SETFL, flags | O_NONBLOCK);
process_ = bp::child(path_ + exe_ + " " + params_, bp::std_out > pipe_stdout_, bp::std_err > pipe_stderr_, bp::start_dir = path_);
stream_ = make_unique<stream_descriptor>(strand_, pipe_stdout_.native_source());
stream_stderr_ = make_unique<stream_descriptor>(strand_, pipe_stderr_.native_source());
on_connect();
if (wd_timeout_sec_ > 0)
{
watchdog_ = make_unique<Watchdog>(strand_);
watchdog_->start(std::chrono::seconds(wd_timeout_sec_), [this](std::chrono::milliseconds ms) { onTimeout(ms); });
}
else
{
watchdog_ = nullptr;
}
stderrReadLine();
}
void ProcessStream::disconnect()
{
if (process_.running())
::kill(-process_.native_handle(), SIGINT);
AsioStream<stream_descriptor>::disconnect();
}
void ProcessStream::onStderrMsg(const std::string& line)
{
if (logStderr_)
{
LOG(INFO, LOG_TAG) << "(" << getName() << ") " << line << "\n";
}
}
void ProcessStream::stderrReadLine()
{
const std::string delimiter = "\n";
boost::asio::async_read_until(*stream_stderr_, streambuf_stderr_, delimiter,
[this, self = shared_from_this(), delimiter](const std::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
LOG(ERROR, LOG_TAG) << "Error while reading from stderr: " << ec.message() << "\n";
return;
}
if (watchdog_)
watchdog_->trigger();
// Extract up to the first delimiter.
std::string line{buffers_begin(streambuf_stderr_.data()), buffers_begin(streambuf_stderr_.data()) + bytes_transferred - delimiter.length()};
if (!line.empty())
{
if (line.back() == '\r')
line.resize(line.size() - 1);
onStderrMsg(line);
}
streambuf_stderr_.consume(bytes_transferred);
stderrReadLine();
});
}
void ProcessStream::onTimeout(std::chrono::milliseconds ms)
{
LOG(ERROR, LOG_TAG) << "Watchdog timeout: " << ms.count() / 1000 << "s\n";
if (process_)
::kill(-process_.native_handle(), SIGINT);
}
} // namespace streamreader