21#ifndef OPENVPN_COMMON_RUNCONTEXT_H
22#define OPENVPN_COMMON_RUNCONTEXT_H
48#ifdef ASIO_HAS_LOCAL_SOCKETS
66template <
typename RC_TYPE>
90template <
typename ServerThread,
typename Stats>
139 void set_thread(
const unsigned int unit, std::thread *thread)
144 throw Exception(
"RunContext::set_thread: overwrite");
151 std::lock_guard<std::recursive_mutex> lock(
mutex);
153 throw Exception(
"RunContext::set_server: halting");
157 throw Exception(
"RunContext::set_server: overwrite");
164 std::lock_guard<std::recursive_mutex> lock(
mutex);
176 std::lock_guard<std::recursive_mutex> lock(
mutex);
177 std::vector<typename ServerThread::Ptr>
ret;
182 ret.emplace_back(sp);
188 std::lock_guard<std::recursive_mutex> lock(
mutex);
190 log_history.reset(
new std::vector<RunContextLogEntry>());
195 std::lock_guard<std::recursive_mutex> lock(
mutex);
201 std::lock_guard<std::recursive_mutex> lock(
mutex);
208 return std::vector<RunContextLogEntry>();
211#ifdef ASIO_HAS_LOCAL_SOCKETS
214 exit_sock.reset(
new openvpn_io::posix::stream_descriptor(
io_context, fd.
release()));
215 exit_sock->async_read_some(openvpn_io::null_buffers(),
216 [self =
Ptr(
this)](
const openvpn_io::error_code &error,
const size_t bytes_recvd)
237 for (
size_t i = 0; i <
threadlist.size(); ++i)
249 template <
typename SVC>
251 const unsigned int unit,
252 const bool io_context_run_called,
256 const std::exception &e)
258 event_loop_bar.
error();
264 if (io_context_run_called)
266 OPENVPN_LOG(thread_name <<
" thread exception: " << e.what());
269 void log(
const std::string &str)
override
274 std::lock_guard<std::recursive_mutex> lock(
mutex);
275 std::cout << ts <<
' ' << str << std::flush;
299 std::lock_guard<std::recursive_mutex> lock(self->mutex);
305 if (self->async_stop_)
306 self->async_stop_->stop();
308 self->exit_timer.cancel();
309#ifdef ASIO_HAS_LOCAL_SOCKETS
310 self->exit_sock.reset();
313 self->signals->cancel();
317 unsigned int stopped = 0;
318 for (
size_t i = 0; i < self->servlist.size(); ++i)
326 self->servlist[i] =
nullptr;
328 OPENVPN_LOG(self->prefix <<
"Stopping " << stopped <<
'/' << self->servlist.size() <<
" thread(s)");
351 std::lock_guard<std::recursive_mutex> lock(
mutex);
360 std::lock_guard<std::recursive_mutex> lock(
mutex);
368 virtual void signal(
const openvpn_io::error_code &error,
int signum)
379#if !defined(OPENVPN_PLATFORM_WIN)
402 { self->signal(error, signal_number); });
411 const unsigned int n_sec = parse_number_throw<unsigned int>(exit_in,
"error parsing EXIT_IN");
413 exit_timer.async_wait([self =
Ptr(
this)](
const openvpn_io::error_code &error)
415 if (error || self->halt)
428#ifdef ASIO_HAS_LOCAL_SOCKETS
429 std::unique_ptr<openvpn_io::posix::stream_descriptor> exit_sock;
void thread_safe_stop() override
void register_signals_all(SignalHandler stop_handler)
std::size_t expires_after(const Time::Duration &d)
static std::string find_static(const std::string &name)
void reset() noexcept
Points this RCPtr<T> to nullptr safely.
implements a weak pointer for reference counted objects.
ThreadContext(RunContext &ctx_arg)
void set_server(const unsigned int unit, ServerThread *serv)
std::unique_ptr< std::vector< RunContextLogEntry > > log_history
openvpn_io::io_context io_context
void set_async_stop(Stop *async_stop)
Log::Context::Wrapper log_wrap
std::vector< RunContextLogEntry > add_log_observer(const unsigned int unit) override
void set_thread(const unsigned int unit, std::thread *thread)
void schedule_debug_exit()
Stop * async_stop() override
void clear_server(const unsigned int unit)
std::vector< ServerThread * > servlist
void log(const std::string &str) override
std::vector< typename ServerThread::Ptr > get_servers()
void set_log_reopen(LogSetup::Ptr lr)
void set_stats_obj(const typename Stats::Ptr &stats_arg)
const Log::Context::Wrapper & log_wrapper()
std::recursive_mutex mutex
std::vector< std::thread * > threadlist
virtual void signal(const openvpn_io::error_code &error, int signum)
void enable_log_history()
void process_exception(const std::string &thread_name, const unsigned int unit, const bool io_context_run_called, openvpn_io::io_context &io_context, SVC &svc, PThreadBarrier &event_loop_bar, const std::exception &e)
std::vector< unsigned int > log_observers
openvpn_io::io_context * io_context_ptr()
void set_prefix(const std::string &pre)
void disable_log_history() override
#define OPENVPN_LOG(args)
Support deferred server-side state creation when client connects.
ServerThreadType< RC< thread_safe_refcount > > ServerThreadBase
std::string date_time_store_time_t(time_t &save)
ServerThreadType< RCWeak< thread_safe_refcount > > ServerThreadWeakBase
std::string signal_name(const int signum)
The logging interface, simple, logs a string.
Argument to construct a Context in a different thread.
Scoped RAII for the global_log pointer.
virtual std::vector< RunContextLogEntry > add_log_observer(const unsigned int unit)=0
virtual void disable_log_history()=0
virtual Stop * async_stop()=0
RunContextLogEntry(const time_t timestamp_arg, const std::string &text_arg)
virtual void log_notify(const RunContextLogEntry &le)
RCWeakPtr< ServerThreadType > WPtr
virtual void thread_safe_stop()=0
RCPtr< ServerThreadType > Ptr
std::vector< std::complex< double > > svc