OpenVPN
status.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "status.h"
30#include "misc.h"
31#include "fdmisc.h"
32
33#include "memdbg.h"
34
35/*
36 * printf-style interface for outputting status info
37 */
38
39static const char *
40print_status_mode(unsigned int flags)
41{
42 switch (flags)
43 {
45 return "WRITE";
46
48 return "READ";
49
51 return "READ/WRITE";
52
53 default:
54 return "UNDEF";
55 }
56}
57
58#if defined(__GNUC__) || defined(__clang__)
59#pragma GCC diagnostic push
60#pragma GCC diagnostic ignored "-Wsign-conversion"
61#endif
62
63struct status_output *
64status_open(const char *filename, const int refresh_freq, const int msglevel,
65 const struct virtual_output *vout, const unsigned int flags)
66{
67 struct status_output *so = NULL;
68 if (filename || msglevel >= 0 || vout)
69 {
71 so->flags = flags;
72 so->msglevel = msglevel;
73 so->vout = vout;
74 so->fd = -1;
75 buf_reset(&so->read_buf);
77 if (filename)
78 {
79 switch (so->flags)
80 {
82 so->fd =
83 platform_open(filename, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
84 break;
85
87 so->fd = platform_open(filename, O_RDONLY, S_IRUSR | S_IWUSR);
88 break;
89
91 so->fd = platform_open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
92 break;
93
94 default:
95 ASSERT(0);
96 }
97 if (so->fd >= 0)
98 {
99 so->filename = string_alloc(filename, NULL);
100 set_cloexec(so->fd);
101
102 /* allocate read buffer */
103 if (so->flags & STATUS_OUTPUT_READ)
104 {
105 so->read_buf = alloc_buf(512);
106 }
107 }
108 else
109 {
110 msg(M_WARN, "Note: cannot open %s for %s", filename, print_status_mode(so->flags));
111 so->errors = true;
112 }
113 }
114 else
115 {
117 }
118
119 if ((so->flags & STATUS_OUTPUT_WRITE) && refresh_freq > 0)
120 {
121 event_timeout_init(&so->et, refresh_freq, 0);
122 }
123 }
124 return so;
125}
126
127#if defined(__GNUC__) || defined(__clang__)
128#pragma GCC diagnostic pop
129#endif
130
131bool
133{
134 if (so)
135 {
136 struct timeval null;
137 CLEAR(null);
138 return event_timeout_trigger(&so->et, &null, ETT_DEFAULT);
139 }
140 else
141 {
142 return false;
143 }
144}
145
146void
148{
149 if (so && so->fd >= 0)
150 {
151 lseek(so->fd, (off_t)0, SEEK_SET);
152 }
153}
154
155void
157{
158 if (so && so->fd >= 0 && (so->flags & STATUS_OUTPUT_WRITE))
159 {
160#if defined(HAVE_FTRUNCATE)
161 {
162 const off_t off = lseek(so->fd, (off_t)0, SEEK_CUR);
163 if (ftruncate(so->fd, off) != 0)
164 {
165 msg(M_WARN | M_ERRNO, "Failed to truncate status file");
166 }
167 }
168#elif defined(HAVE_CHSIZE)
169 {
170 const long off = (long)lseek(so->fd, (off_t)0, SEEK_CUR);
171 chsize(so->fd, off);
172 }
173#else /* if defined(HAVE_FTRUNCATE) */
174#warning both ftruncate and chsize functions appear to be missing from this OS
175#endif
176
177 /* clear read buffer */
178 if (buf_defined(&so->read_buf))
179 {
180 ASSERT(buf_init(&so->read_buf, 0));
181 }
182 }
183}
184
185/* return false if error occurred */
186bool
188{
189 bool ret = true;
190 if (so)
191 {
192 if (so->errors)
193 {
194 ret = false;
195 }
196 if (so->fd >= 0)
197 {
198 if (close(so->fd) < 0)
199 {
200 ret = false;
201 }
202 }
203 free(so->filename);
204
205 if (buf_defined(&so->read_buf))
206 {
207 free_buf(&so->read_buf);
208 }
209 free(so);
210 }
211 else
212 {
213 ret = false;
214 }
215 return ret;
216}
217
218#define STATUS_PRINTF_MAXLEN 512
219
220void
221status_printf(struct status_output *so, const char *format, ...)
222{
223 if (so && (so->flags & STATUS_OUTPUT_WRITE))
224 {
225 char buf[STATUS_PRINTF_MAXLEN + 2]; /* leave extra bytes for CR, LF */
226 va_list arglist;
227 int stat;
228
229 va_start(arglist, format);
230 stat = vsnprintf(buf, STATUS_PRINTF_MAXLEN, format, arglist);
231 va_end(arglist);
232 buf[STATUS_PRINTF_MAXLEN - 1] = 0;
233
234 if (stat < 0 || stat >= STATUS_PRINTF_MAXLEN)
235 {
236 so->errors = true;
237 }
238
239 if (so->msglevel >= 0 && !so->errors)
240 {
241 msg((msglvl_t)so->msglevel, "%s", buf);
242 }
243
244 if (so->fd >= 0 && !so->errors)
245 {
246 size_t len = strlcat(buf, "\n", sizeof(buf));
247 ASSERT(len < sizeof(buf));
248 if (write(so->fd, buf, (unsigned int)len) != (ssize_t)len)
249 {
250 so->errors = true;
251 }
252 }
253
254 if (so->vout && !so->errors)
255 {
256 chomp(buf);
257 (*so->vout->func)(so->vout->arg, so->vout->flags_default, buf);
258 }
259 }
260}
261
262bool
263status_read(struct status_output *so, struct buffer *buf)
264{
265 bool ret = false;
266
267 if (so && so->fd >= 0 && (so->flags & STATUS_OUTPUT_READ))
268 {
270 ASSERT(buf_defined(buf));
271 while (true)
272 {
273 const int c = buf_read_u8(&so->read_buf);
274
275 /* read more of file into buffer */
276 if (c == -1)
277 {
278 ASSERT(buf_init(&so->read_buf, 0));
279 ssize_t len = read(so->fd, BPTR(&so->read_buf), (unsigned int)BCAP(&so->read_buf));
280 if (len <= 0)
281 {
282 break;
283 }
284
285 ASSERT(buf_inc_len(&so->read_buf, (int)len));
286 continue;
287 }
288
289 ret = true;
290
291 if (c == '\r')
292 {
293 continue;
294 }
295
296 if (c == '\n')
297 {
298 break;
299 }
300
301 buf_write_u8(buf, (uint8_t)c);
302 }
303
305 }
306
307 return ret;
308}
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:174
void buf_null_terminate(struct buffer *buf)
Force a null terminator at the end of the buffer content.
Definition buffer.c:509
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:596
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_inc_len(struct buffer *buf, int inc)
Increase or decrease the length of a buffer.
Definition buffer.h:1119
static void buf_reset(struct buffer *buf)
Reset a buffer to an undefined (unallocated) state.
Definition buffer.h:599
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
static int buf_read_u8(struct buffer *buf)
Read and consume a uint8_t from the front of a buffer.
Definition buffer.h:1495
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
Definition buffer.h:161
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
static bool buf_defined(const struct buffer *buf)
Return true iff buf has a non-NULL data pointer.
Definition buffer.h:399
#define buf_init(buf, offset)
Definition buffer.h:364
size_t strlcat(char *dest, const char *src, size_t maxlen)
void set_cloexec(socket_descriptor_t fd)
Definition fdmisc.c:87
@ write
@ read
bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
This is the principal function for testing and triggering recurring timers.
Definition interval.c:42
#define ETT_DEFAULT
Definition interval.h:222
static void event_timeout_init(struct event_timeout *et, interval_t n, const time_t last)
Initialises a timer struct.
Definition interval.h:172
static void event_timeout_clear(struct event_timeout *et)
Clears the timeout and reset all values to 0.
Definition interval.h:153
#define CLEAR(x)
Definition basic.h:32
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
int platform_open(const char *path, int flags, int mode)
Definition platform.c:518
#define STATUS_PRINTF_MAXLEN
Definition status.c:218
void status_reset(const struct status_output *so)
Definition status.c:147
bool status_trigger(struct status_output *so)
Definition status.c:132
static const char * print_status_mode(unsigned int flags)
Definition status.c:40
void status_printf(struct status_output *so, const char *format,...)
Definition status.c:221
bool status_read(struct status_output *so, struct buffer *buf)
Definition status.c:263
struct status_output * status_open(const char *filename, const int refresh_freq, const int msglevel, const struct virtual_output *vout, const unsigned int flags)
Definition status.c:64
void status_flush(struct status_output *so)
Definition status.c:156
bool status_close(struct status_output *so)
Definition status.c:187
#define STATUS_OUTPUT_WRITE
Definition status.h:51
#define STATUS_OUTPUT_READ
Definition status.h:50
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
struct buffer read_buf
Definition status.h:60
const struct virtual_output * vout
Definition status.h:58
bool errors
Definition status.h:64
struct event_timeout et
Definition status.h:62
int msglevel
Definition status.h:57
unsigned int flags
Definition status.h:52
char * filename
Definition status.h:54
unsigned int flags_default
Definition status.h:34
void * arg
Definition status.h:33
void(* func)(void *arg, const unsigned int flags, const char *str)
Definition status.h:35