Be very specific that max must be greater than keep.
[darkstat] / err.c
1 /* darkstat 3
2 * copyright (c) 2001-2008 Emil Mikulic.
3 *
4 * err.c: BSD-like err() and warn() functions
5 *
6 * Permission to use, copy, modify, and distribute this file for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "darkstat.h"
20 #include "err.h"
21 #include "pidfile.h"
22
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <unistd.h>
30
31 void
32 err(const int code, const char *format, ...)
33 {
34 va_list va;
35
36 fprintf(stderr, "%5d: error: ", (int)getpid());
37 va_start(va, format);
38 vfprintf(stderr, format, va);
39 va_end(va);
40 fprintf(stderr, ": %s\n", strerror(errno));
41 pidfile_unlink();
42 exit(code);
43 }
44
45 void
46 errx(const int code, const char *format, ...)
47 {
48 va_list va;
49
50 fprintf(stderr, "%5d: error: ", (int)getpid());
51 va_start(va, format);
52 vfprintf(stderr, format, va);
53 va_end(va);
54 fprintf(stderr, "\n");
55 pidfile_unlink();
56 exit(code);
57 }
58
59 void
60 warn(const char *format, ...)
61 {
62 va_list va;
63
64 fprintf(stderr, "%5d: warning: ", (int)getpid());
65 va_start(va, format);
66 vfprintf(stderr, format, va);
67 va_end(va);
68 fprintf(stderr, ": %s\n", strerror(errno));
69 }
70
71 void
72 warnx(const char *format, ...)
73 {
74 va_list va;
75
76 fprintf(stderr, "%5d: warning: ", (int)getpid());
77 va_start(va, format);
78 vfprintf(stderr, format, va);
79 va_end(va);
80 fprintf(stderr, "\n");
81 }
82
83 /* We interlock verbosef() between processes by using a pipe with a single
84 * byte in it. This pipe must be initialized before the first fork() in order
85 * to work. Then, verbosef() will block on a read() until it is able to
86 * retrieve the byte. After doing its business, it will put a byte back into
87 * the pipe.
88 *
89 * This is completely silly and largely unnecessary.
90 */
91 static int inited = 0;
92 static int lockpipe[2];
93
94 static void unlock(void);
95
96 static void
97 initlock(void)
98 {
99 if (pipe(lockpipe) == -1)
100 err(1, "pipe(lockpipe)");
101 inited = 1;
102 unlock();
103 }
104
105 static void
106 lock(void)
107 {
108 char buf[1];
109
110 if (!inited) initlock();
111 if (read(lockpipe[0], buf, 1) != 1) {
112 fprintf(stderr, "lock failed!\n");
113 pidfile_unlink();
114 exit(1);
115 }
116 }
117
118 static void
119 unlock(void)
120 {
121 char c = 0;
122
123 if (write(lockpipe[1], &c, 1) != 1) {
124 fprintf(stderr, "unlock failed!\n");
125 pidfile_unlink();
126 exit(1);
127 }
128 }
129
130 int want_verbose = 0;
131
132 void
133 verbosef(const char *format, ...)
134 {
135 va_list va;
136
137 if (!want_verbose) return;
138 lock();
139 fprintf(stderr, "darkstat (%05d): ", (int)getpid());
140 va_start(va, format);
141 vfprintf(stderr, format, va);
142 va_end(va);
143 fprintf(stderr, "\n");
144 unlock();
145 }
146
147 void
148 dverbosef(const char *format _unused_, ...)
149 {
150 /* disabled / do-nothing verbosef */
151 }
152
153 /* vim:set ts=3 sw=3 tw=78 expandtab: */