summaryrefslogtreecommitdiffstats
path: root/status.c
blob: 1b8081050fa2fe30c5161e76bbf9052ed1744532 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 *  OpenVPN -- An application to securely tunnel IP networks
 *             over a single TCP/UDP port, with support for SSL/TLS-based
 *             session authentication and key exchange,
 *             packet encryption, packet authentication, and
 *             packet compression.
 *
 *  Copyright (C) 2002-2008 OpenVPN Technologies, Inc. <sales@openvpn.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  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 (see the file COPYING included with this
 *  distribution); if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "syshead.h"

#include "status.h"
#include "perf.h"

#include "memdbg.h"

/*
 * printf-style interface for outputting status info
 */

static const char *
print_status_mode (unsigned int flags)
{
  switch (flags)
    {
    case STATUS_OUTPUT_WRITE:
      return "WRITE";
    case STATUS_OUTPUT_READ:
      return "READ";
    case STATUS_OUTPUT_READ|STATUS_OUTPUT_WRITE:
      return "READ/WRITE";
    default:
      return "UNDEF";
    }
}

struct status_output *
status_open (const char *filename,
	     const int refresh_freq,
	     const int msglevel,
	     const struct virtual_output *vout,
	     const unsigned int flags)
{
  struct status_output *so = NULL;
  if (filename || msglevel >= 0 || vout)
    {
      ALLOC_OBJ_CLEAR (so, struct status_output);
      so->flags = flags;
      so->msglevel = msglevel;
      so->vout = vout;
      so->fd = -1;
      buf_reset (&so->read_buf);
      event_timeout_clear (&so->et);
      if (filename)
	{
	  switch (so->flags)
	    {
#ifdef _MSC_VER
            case STATUS_OUTPUT_WRITE:
              so->fd = open (filename,
                             O_CREAT | O_TRUNC | O_WRONLY,
			     _S_IREAD | _S_IWRITE);
              break;
            case STATUS_OUTPUT_READ:
              so->fd = open (filename,
                             O_RDONLY,
			     _S_IREAD | _S_IWRITE);
              break;
            case STATUS_OUTPUT_READ|STATUS_OUTPUT_WRITE:
              so->fd = open (filename,
                             O_CREAT | O_RDWR,
			     _S_IREAD | _S_IWRITE);
              break;
#else
	    case STATUS_OUTPUT_WRITE:
	      so->fd = open (filename,
			     O_CREAT | O_TRUNC | O_WRONLY,
			     S_IRUSR | S_IWUSR);
	      break;
	    case STATUS_OUTPUT_READ:
	      so->fd = open (filename,
			     O_RDONLY,
			     S_IRUSR | S_IWUSR);
	      break;
	    case STATUS_OUTPUT_READ|STATUS_OUTPUT_WRITE:
	      so->fd = open (filename,
			     O_CREAT | O_RDWR,
			     S_IRUSR | S_IWUSR);
	      break;
#endif
	    default:
	      ASSERT (0);
	    }
	  if (so->fd >= 0)
	    {
	      so->filename = string_alloc (filename, NULL);

	      /* allocate read buffer */
	      if (so->flags & STATUS_OUTPUT_READ)
		so->read_buf = alloc_buf (512);
	    }
	  else
	    {
	      msg (M_WARN, "Note: cannot open %s for %s", filename, print_status_mode (so->flags));
	      so->errors = true;
	    }
	}
      else
	so->flags = STATUS_OUTPUT_WRITE;

      if ((so->flags & STATUS_OUTPUT_WRITE) && refresh_freq > 0)
	{
	  event_timeout_init (&so->et, refresh_freq, 0);
	}
    }
  return so;
}

bool
status_trigger (struct status_output *so)
{
  if (so)
    {
      struct timeval null;
      CLEAR (null);
      return event_timeout_trigger (&so->et, &null, ETT_DEFAULT);
    }
  else
    return false;
}

bool
status_trigger_tv (struct status_output *so, struct timeval *tv)
{
  if (so)
    return event_timeout_trigger (&so->et, tv, ETT_DEFAULT);
  else
    return false;
}

void
status_reset (struct status_output *so)
{
  if (so && so->fd >= 0)
    lseek (so->fd, (off_t)0, SEEK_SET);
}

void
status_flush (struct status_output *so)
{
  if (so && so->fd >= 0 && (so->flags & STATUS_OUTPUT_WRITE))
    {
#if defined(HAVE_FTRUNCATE)
      {
	const off_t off = lseek (so->fd, (off_t)0, SEEK_CUR);
	ftruncate (so->fd, off);
      }
#elif defined(HAVE_CHSIZE)
      {
	const long off = (long) lseek (so->fd, (off_t)0, SEEK_CUR);
	chsize (so->fd, off);
      }
#else
#warning both ftruncate and chsize functions appear to be missing from this OS
#endif

      /* clear read buffer */
      if (buf_defined (&so->read_buf))
	{
	  ASSERT (buf_init (&so->read_buf, 0));
	}
    }
}

/* return false if error occurred */
bool
status_close (struct status_output *so)
{
  bool ret = true;
  if (so)
    {
      if (so->errors)
	ret = false;
      if (so->fd >= 0)
	{
	  if (close (so->fd) < 0)
	    ret = false;
	}
      if (so->filename)
	free (so->filename);
      if (buf_defined (&so->read_buf))
	free_buf (&so->read_buf);
      free (so);
    }
  else
    ret = false;
  return ret;
}

#define STATUS_PRINTF_MAXLEN 512

void
status_printf (struct status_output *so, const char *format, ...)
{
  if (so && (so->flags & STATUS_OUTPUT_WRITE))
    {
      char buf[STATUS_PRINTF_MAXLEN+2]; /* leave extra bytes for CR, LF */
      va_list arglist;
      int stat;

      va_start (arglist, format);
      stat = vsnprintf (buf, STATUS_PRINTF_MAXLEN, format, arglist);
      va_end (arglist);
      buf[STATUS_PRINTF_MAXLEN - 1] = 0;

      if (stat < 0 || stat >= STATUS_PRINTF_MAXLEN)
	so->errors = true;

      if (so->msglevel >= 0 && !so->errors)
	msg (so->msglevel, "%s", buf);

      if (so->fd >= 0 && !so->errors)
	{
	  int len;
	  strcat (buf, "\n");
	  len = strlen (buf);
	  if (len > 0)
	    {
	      if (write (so->fd, buf, len) != len)
		so->errors = true;
	    }
	}

      if (so->vout && !so->errors)
	{
	  chomp (buf);
	  (*so->vout->func) (so->vout->arg, so->vout->flags_default, buf);
	}
    }
}

bool
status_read (struct status_output *so, struct buffer *buf)
{
  bool ret = false;

  if (so && so->fd >= 0 && (so->flags & STATUS_OUTPUT_READ))
    {
      ASSERT (buf_defined (&so->read_buf));      
      ASSERT (buf_defined (buf));
      while (true)
	{
	  const int c = buf_read_u8 (&so->read_buf);

	  /* read more of file into buffer */
	  if (c == -1)
	    {
	      int len;

	      ASSERT (buf_init (&so->read_buf, 0));
	      len = read (so->fd, BPTR (&so->read_buf), BCAP (&so->read_buf));
	      if (len <= 0)
		break;

	      ASSERT (buf_inc_len (&so->read_buf, len));
	      continue;
	    }

	  ret = true;

	  if (c == '\r')
	    continue;

	  if (c == '\n')
	    break;

	  buf_write_u8 (buf, c);
	}

      buf_null_terminate (buf);
    }

  return ret;
}