File: err.c

package info (click to toggle)
darkstat 3.0.715-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 816 kB
  • sloc: ansic: 6,542; sh: 355; makefile: 172; php: 15
file content (197 lines) | stat: -rw-r--r-- 4,484 bytes parent folder | download
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
/* darkstat 3
 * copyright (c) 2001-2011 Emil Mikulic.
 *
 * err.c: BSD-like err() and warn() functions
 *
 * Permission to use, copy, modify, and distribute this file for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "cdefs.h"
#include "err.h"
#include "opt.h"
#include "pidfile.h"
#include "bsd.h" /* for strlcpy */

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <unistd.h>

static void to_syslog(const char *type, const int want_err,
          const char *format, va_list va) _printflike_(3, 0);

static void
to_syslog(const char *type, const int want_err,
          const char *format, va_list va)
{
   char buf[512];
   size_t pos = 0;
   int saved_errno = errno;

   if (type != NULL) {
      strlcpy(buf, type, sizeof(buf));
      pos = strlen(buf);
   }
   vsnprintf(buf+pos, sizeof(buf)-pos, format, va);
   if (want_err) {
      strlcat(buf, ": ", sizeof(buf));
      strlcat(buf, strerror(saved_errno), sizeof(buf));
   }
   syslog(LOG_DEBUG, "%s", buf);
}

void
err(const int code, const char *format, ...)
{
   va_list va;

   va_start(va, format);
   if (opt_want_syslog)
      to_syslog("ERROR: ", 1, format, va);
   else {
      fprintf(stderr, "%5d: error: ", (int)getpid());
      vfprintf(stderr, format, va);
      fprintf(stderr, ": %s\n", strerror(errno));
   }
   va_end(va);
   pidfile_unlink();
   exit(code);
}

void
errx(const int code, const char *format, ...)
{
   va_list va;

   va_start(va, format);
   if (opt_want_syslog)
      to_syslog("ERROR: ", 0, format, va);
   else {
      fprintf(stderr, "%5d: error: ", (int)getpid());
      vfprintf(stderr, format, va);
      fprintf(stderr, "\n");
   }
   va_end(va);
   pidfile_unlink();
   exit(code);
}

void
warn(const char *format, ...)
{
   va_list va;

   va_start(va, format);
   if (opt_want_syslog)
      to_syslog("WARNING: ", 1, format, va);
   else {
      fprintf(stderr, "%5d: warning: ", (int)getpid());
      vfprintf(stderr, format, va);
      fprintf(stderr, ": %s\n", strerror(errno));
   }
   va_end(va);
}

void
warnx(const char *format, ...)
{
   va_list va;

   va_start(va, format);
   if (opt_want_syslog)
      to_syslog("WARNING: ", 0, format, va);
   else {
      fprintf(stderr, "%5d: warning: ", (int)getpid());
      vfprintf(stderr, format, va);
      fprintf(stderr, "\n");
   }
   va_end(va);
}

/* We interlock verbosef() between processes by using a pipe with a single
 * byte in it.  This pipe must be initialized before the first fork() in order
 * to work.  Then, verbosef() will block on a read() until it is able to
 * retrieve the byte.  After doing its business, it will put a byte back into
 * the pipe.
 *
 * This is completely silly and largely unnecessary.
 */
static int inited = 0;
static int lockpipe[2];

static void unlock(void);

static void
initlock(void)
{
   if (pipe(lockpipe) == -1)
      err(1, "pipe(lockpipe)");
   inited = 1;
   unlock();
}

static void
lock(void)
{
   char buf[1];

   if (!inited) initlock();
   if (read(lockpipe[0], buf, 1) != 1) {
      fprintf(stderr, "lock failed!\n");
      pidfile_unlink();
      exit(1);
   }
}

static void
unlock(void)
{
   char c = 0;

   if (write(lockpipe[1], &c, 1) != 1) {
      fprintf(stderr, "unlock failed!\n");
      pidfile_unlink();
      exit(1);
   }
}

void
verbosef(const char *format, ...)
{
   va_list va;

   if (!opt_want_verbose) return;
   va_start(va, format);
   if (opt_want_syslog)
      to_syslog(NULL, 0, format, va);
   else {
      lock();
      fprintf(stderr, "darkstat (%05d): ", (int)getpid());
      vfprintf(stderr, format, va);
      fprintf(stderr, "\n");
      unlock();
   }
   va_end(va);
}

void
dverbosef(const char *format _unused_, ...)
{
   /* disabled / do-nothing verbosef */
}

/* vim:set ts=3 sw=3 tw=78 expandtab: */