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 <filesystem>
10#include <string>
11
21
22namespace openvpn {
23
24class ProcFS : public Action
25{
26 public:
27 OPENVPN_EXCEPTION(procfs_error);
28
29 ProcFS(std::string fn_arg, std::string text_arg)
30 : fn(std::move(fn_arg)),
31 text(std::move(text_arg))
32 {
33 }
34
35 virtual void execute(std::ostream &os) override
36 {
37 os << to_string() << std::endl;
38 try
39 {
41 }
42 catch (const std::exception &e)
43 {
44 os << "ProcFS exception: " << e.what() << std::endl;
45 }
46 }
47
48 virtual std::string to_string() const override
49 {
50 return to_string(fn, text);
51 }
52
53 static std::string to_string(const std::string &fn, const std::string &text)
54 {
55 return "ProcFS: " + fn + " -> " + string::trim_crlf_copy(text);
56 }
57
58 static void write_sys(const std::string &fn, const std::string &text, Stop *async_stop = nullptr)
59 {
60 const unsigned int n_retries = 200;
61 const unsigned int milliseconds_per_retry = 100;
62 volatile bool stop = false;
63
64 // allow asynchronous stop
65 Stop::Scope stop_scope(async_stop, [&stop]()
66 { stop = true; });
67
68 for (unsigned int i = 0; i < n_retries && !stop; ++i)
69 {
70 std::error_code ec;
71 if (std::filesystem::exists(fn, ec))
72 {
73 try
74 {
75 OPENVPN_LOG("ProcFS: " << fn << " -> '" << string::trim_crlf_copy(text) << '\'');
76 write_text_unix(fn, 0777, 0, text);
77 }
78 catch (const std::exception &e)
79 {
80 OPENVPN_LOG("ProcFS exception: " << e.what());
81 }
82#ifdef OPENVPN_PROCFS_DEBUG
84 std::string text_verify;
85 BufferAllocated buf(256);
86 const int status = read_binary_unix_fast(fn, buf);
87 if (status)
88 {
89 text_verify = strerror_str(status);
90 }
91 else
92 {
94 text_verify = buf_to_string(buf);
95 }
96 OPENVPN_LOG("WRITE_SYS verify fn=" << fn << " text=" << string::trim_crlf_copy(text) << " verify=" << text_verify);
97#endif
98 return;
99 }
100 sleep_milliseconds(milliseconds_per_retry);
101 }
102 if (stop)
103 OPENVPN_THROW(procfs_error, "file " << fn << " : aborting write attempt due to stop signal");
104 else
105 OPENVPN_THROW(procfs_error, "file " << fn << " failed to appear within " << (n_retries * milliseconds_per_retry / 1000) << " seconds");
106 }
107
108 private:
109 std::string fn;
110 std::string text;
111};
112
114{
115 public:
116 IPv4ReversePathFilter(const std::string &dev, const unsigned int value)
117 : ProcFS(key_fn(dev), openvpn::to_string(value))
118 {
119 OPENVPN_LOG("IPv4ReversePathFilter " << dev << " -> " << value);
120 }
121
122 static void write(const std::string &dev, const unsigned int value, Stop *stop = nullptr)
123 {
124 ProcFS::write_sys(key_fn(dev), openvpn::to_string(value), stop);
125 }
126
127 private:
128 static std::string key_fn(const std::string &dev)
129 {
130 return printfmt("/proc/sys/net/ipv4/conf/%s/rp_filter", dev);
131 }
132};
133} // namespace openvpn
static std::string key_fn(const std::string &dev)
Definition procfs.hpp:128
static void write(const std::string &dev, const unsigned int value, Stop *stop=nullptr)
Definition procfs.hpp:122
IPv4ReversePathFilter(const std::string &dev, const unsigned int value)
Definition procfs.hpp:116
std::string fn
Definition procfs.hpp:109
static void write_sys(const std::string &fn, const std::string &text, Stop *async_stop=nullptr)
Definition procfs.hpp:58
OPENVPN_EXCEPTION(procfs_error)
ProcFS(std::string fn_arg, std::string text_arg)
Definition procfs.hpp:29
std::string text
Definition procfs.hpp:110
virtual std::string to_string() const override
Definition procfs.hpp:48
virtual void execute(std::ostream &os) override
Definition procfs.hpp:35
static std::string to_string(const std::string &fn, const std::string &text)
Definition procfs.hpp:53
#define OPENVPN_THROW(exc, stuff)
#define OPENVPN_LOG(args)
void trim_crlf(STRING &str)
Definition string.hpp:224
std::string trim_crlf_copy(std::string str)
Definition string.hpp:231
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
bool sleep_milliseconds(const unsigned int milliseconds)
Definition sleep.hpp:26
std::string printfmt(const std::string &fmt, Args... args)
Definition format.hpp:314
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