OpenVPN 3 Core Library
Loading...
Searching...
No Matches
procfs.hpp
Go to the documentation of this file.
1// Private Gateway
2// Copyright (C) 2012-2020 OpenVPN Technologies, Inc.
3// All rights reserved
4
5#pragma once
6
7// #define OPENVPN_PROCFS_DEBUG
8
9#include <chrono>
10#include <filesystem>
11#include <string>
12#include <thread>
13
22
23namespace openvpn {
24
25class ProcFS : public Action
26{
27 public:
28 OPENVPN_EXCEPTION(procfs_error);
29
30 ProcFS(std::string fn_arg, std::string text_arg)
31 : fn(std::move(fn_arg)),
32 text(std::move(text_arg))
33 {
34 }
35
36 void execute(std::ostream &os) override
37 {
38 os << to_string() << "\n";
39 try
40 {
42 }
43 catch (const std::exception &e)
44 {
45 os << "ProcFS exception: " << e.what() << "\n";
46 }
47 }
48
49 std::string to_string() const override
50 {
51 return to_string(fn, text);
52 }
53
54 static std::string to_string(const std::string &fn, const std::string &text)
55 {
56 return "ProcFS: " + fn + " -> " + string::trim_crlf_copy(text);
57 }
58
59 static void write_sys(const std::string &fn, const std::string &text, Stop *async_stop = nullptr)
60 {
61 using namespace std::chrono_literals;
62
63 const unsigned int n_retries = 200;
64 const unsigned int milliseconds_per_retry = 100;
65 volatile bool stop = false;
66
67 // allow asynchronous stop
68 Stop::Scope stop_scope(async_stop, [&stop]()
69 { stop = true; });
70
71 for (unsigned int i = 0; i < n_retries && !stop; ++i)
72 {
73 std::error_code ec;
74 if (std::filesystem::exists(fn, ec))
75 {
76 try
77 {
78 OPENVPN_LOG("ProcFS: " << fn << " -> '" << string::trim_crlf_copy(text) << '\'');
79 write_text_unix(fn, 0777, 0, text);
80 }
81 catch (const std::exception &e)
82 {
83 OPENVPN_LOG("ProcFS exception: " << e.what());
84 }
85#ifdef OPENVPN_PROCFS_DEBUG
86 std::this_thread::sleep_for(100ms);
87 std::string text_verify;
88 BufferAllocated buf(256);
89 const int status = read_binary_unix_fast(fn, buf);
90 if (status)
91 {
92 text_verify = strerror_str(status);
93 }
94 else
95 {
97 text_verify = buf_to_string(buf);
98 }
99 OPENVPN_LOG("WRITE_SYS verify fn=" << fn << " text=" << string::trim_crlf_copy(text) << " verify=" << text_verify);
100#endif
101 return;
102 }
103 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds_per_retry));
104 }
105 if (stop)
106 OPENVPN_THROW(procfs_error, "file " << fn << " : aborting write attempt due to stop signal");
107 else
108 OPENVPN_THROW(procfs_error, "file " << fn << " failed to appear within " << (n_retries * milliseconds_per_retry / 1000) << " seconds");
109 }
110
111 private:
112 std::string fn;
113 std::string text;
114};
115
117{
118 public:
119 IPv4ReversePathFilter(const std::string &dev, const unsigned int value)
120 : ProcFS(key_fn(dev), openvpn::to_string(value))
121 {
122 OPENVPN_LOG("IPv4ReversePathFilter " << dev << " -> " << value);
123 }
124
125 static void write(const std::string &dev, const unsigned int value, Stop *stop = nullptr)
126 {
127 ProcFS::write_sys(key_fn(dev), openvpn::to_string(value), stop);
128 }
129
130 private:
131 static std::string key_fn(const std::string &dev)
132 {
133 return printfmt("/proc/sys/net/ipv4/conf/%s/rp_filter", dev);
134 }
135};
136} // namespace openvpn
static std::string key_fn(const std::string &dev)
Definition procfs.hpp:131
static void write(const std::string &dev, const unsigned int value, Stop *stop=nullptr)
Definition procfs.hpp:125
IPv4ReversePathFilter(const std::string &dev, const unsigned int value)
Definition procfs.hpp:119
std::string fn
Definition procfs.hpp:112
static void write_sys(const std::string &fn, const std::string &text, Stop *async_stop=nullptr)
Definition procfs.hpp:59
OPENVPN_EXCEPTION(procfs_error)
ProcFS(std::string fn_arg, std::string text_arg)
Definition procfs.hpp:30
std::string text
Definition procfs.hpp:113
void execute(std::ostream &os) override
Definition procfs.hpp:36
std::string to_string() const override
Definition procfs.hpp:49
static std::string to_string(const std::string &fn, const std::string &text)
Definition procfs.hpp:54
#define OPENVPN_THROW(exc, stuff)
#define OPENVPN_LOG(args)
void trim_crlf(STRING &str)
Definition string.hpp:173
std::string trim_crlf_copy(std::string str)
Definition string.hpp:180
int read_binary_unix_fast(const STRING &fn, Buffer &out, std::uint64_t *mtime_ns=nullptr)
Definition fileunix.hpp:172
std::string to_string(const T &t)
Convert a value to a string.
Definition to_string.hpp:45
void write_text_unix(const std::string &fn, const mode_t mode, const std::uint64_t mtime_ns, const std::string &content)
Definition fileunix.hpp:105
std::string printfmt(const std::string &fmt, Args... args)
Definition format.hpp:313
std::string buf_to_string(const Buffer &buf)
Definition bufstr.hpp:22
std::string strerror_str(const int errnum)
Definition strerror.hpp:21
std::ostringstream os