forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
222 lines (196 loc) · 5.59 KB
/
util.c
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
#include "dat.h"
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
const char *progname;
static void
vwarnx(const char *err, const char *fmt, va_list args)
__attribute__((format(printf, 2, 0)));
static void
vwarnx(const char *err, const char *fmt, va_list args)
{
fprintf(stderr, "%s: ", progname);
if (fmt) {
vfprintf(stderr, fmt, args);
if (err) fprintf(stderr, ": %s", err);
}
fputc('\n', stderr);
}
void
warn(const char *fmt, ...)
{
char *err = strerror(errno); /* must be done first thing */
va_list args;
va_start(args, fmt);
vwarnx(err, fmt, args);
va_end(args);
}
void
warnx(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vwarnx(NULL, fmt, args);
va_end(args);
}
char*
fmtalloc(char *fmt, ...)
{
int n;
char *buf;
va_list ap;
// find out how much space is needed
va_start(ap, fmt);
n = vsnprintf(0, 0, fmt, ap) + 1; // include space for trailing NUL
va_end(ap);
buf = malloc(n);
if (buf) {
va_start(ap, fmt);
vsnprintf(buf, n, fmt, ap);
va_end(ap);
}
return buf;
}
// Zalloc allocates n bytes of zeroed memory and
// returns a pointer to it.
// If insufficient memory is available, zalloc returns 0.
void*
zalloc(int n)
{
void *p;
p = malloc(n);
if (p) {
memset(p, 0, n);
}
return p;
}
static void
warn_systemd_ignored_option(char *opt, char *arg)
{
#ifdef HAVE_LIBSYSTEMD
if (sd_listen_fds(0) > 0) {
warnx("inherited listen fd; ignoring option: %s %s", opt, arg);
}
#endif
}
static void usage(int code) __attribute__ ((noreturn));
static void
usage(int code)
{
fprintf(stderr, "Use: %s [OPTIONS]\n"
"\n"
"Options:\n"
" -b DIR write-ahead log directory\n"
" -f MS fsync at most once every MS milliseconds (default is %dms);\n"
" use -f0 for \"always fsync\"\n"
" -F never fsync\n"
" -l ADDR listen on address (default is 0.0.0.0)\n"
" -p PORT listen on port (default is " Portdef ")\n"
" -u USER become user and group\n"
" -z BYTES set the maximum job size in bytes (default is %d);\n"
" max allowed is %d bytes\n"
" -s BYTES set the size of each write-ahead log file (default is %d);\n"
" will be rounded up to a multiple of 4096 bytes\n"
" -v show version information\n"
" -V increase verbosity\n"
" -h show this help\n",
progname,
DEFAULT_FSYNC_MS,
JOB_DATA_SIZE_LIMIT_DEFAULT,
JOB_DATA_SIZE_LIMIT_MAX,
Filesizedef);
exit(code);
}
static char *flagusage(char *flag) __attribute__ ((noreturn));
static char *
flagusage(char *flag)
{
warnx("flag requires an argument: %s", flag);
usage(5);
}
static size_t
parse_size_t(char *str)
{
char r, x;
size_t size;
r = sscanf(str, "%zu%c", &size, &x);
if (1 != r) {
warnx("invalid size: %s", str);
usage(5);
}
return size;
}
void
optparse(Server *s, char **argv)
{
int64 ms;
char *arg, *tmp;
# define EARGF(x) (*arg ? (tmp=arg,arg="",tmp) : *argv ? *argv++ : (x))
while ((arg = *argv++) && *arg++ == '-' && *arg) {
char c;
while ((c = *arg++)) {
switch (c) {
case 'p':
s->port = EARGF(flagusage("-p"));
warn_systemd_ignored_option("-p", s->port);
break;
case 'l':
s->addr = EARGF(flagusage("-l"));
warn_systemd_ignored_option("-l", s->addr);
break;
case 'z':
job_data_size_limit = parse_size_t(EARGF(flagusage("-z")));
if (job_data_size_limit > JOB_DATA_SIZE_LIMIT_MAX) {
warnx("maximum job size was set to %d", JOB_DATA_SIZE_LIMIT_MAX);
job_data_size_limit = JOB_DATA_SIZE_LIMIT_MAX;
}
break;
case 's':
s->wal.filesize = parse_size_t(EARGF(flagusage("-s")));
break;
case 'c':
warnx("-c flag was removed. binlog is always compacted.");
break;
case 'n':
warnx("-n flag was removed. binlog is always compacted.");
break;
case 'f':
ms = (int64)parse_size_t(EARGF(flagusage("-f")));
s->wal.syncrate = ms * 1000000;
s->wal.wantsync = 1;
break;
case 'F':
s->wal.wantsync = 0;
break;
case 'u':
s->user = EARGF(flagusage("-u"));
break;
case 'b':
s->wal.dir = EARGF(flagusage("-b"));
s->wal.use = 1;
break;
case 'h':
usage(0);
case 'v':
printf("beanstalkd %s\n", version);
exit(0);
case 'V':
verbose++;
break;
default:
warnx("unknown flag: %s", arg-2);
usage(5);
}
}
}
if (arg) {
warnx("unknown argument: %s", arg-1);
usage(5);
}
}