New command: kick-job.
[beanstalkd.git] / util.c
blob80ae06e01e1d2aae91033b2fca633650eaeabf3b
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdarg.h>
7 #include "sd-daemon.h"
8 #include "dat.h"
10 const char *progname;
12 void
13 v()
17 static void
18 vwarnx(const char *err, const char *fmt, va_list args)
20 fprintf(stderr, "%s: ", progname);
21 if (fmt) {
22 vfprintf(stderr, fmt, args);
23 if (err) fprintf(stderr, ": %s", err);
25 fputc('\n', stderr);
28 void
29 warn(const char *fmt, ...)
31 char *err = strerror(errno); /* must be done first thing */
32 va_list args;
34 va_start(args, fmt);
35 vwarnx(err, fmt, args);
36 va_end(args);
39 void
40 warnx(const char *fmt, ...)
42 va_list args;
43 va_start(args, fmt);
44 vwarnx(NULL, fmt, args);
45 va_end(args);
49 char*
50 fmtalloc(char *fmt, ...)
52 int n;
53 char *buf;
54 va_list ap;
56 // find out how much space is needed
57 va_start(ap, fmt);
58 n = vsnprintf(0, 0, fmt, ap) + 1; // include space for trailing NUL
59 va_end(ap);
61 buf = malloc(n);
62 if (buf) {
63 va_start(ap, fmt);
64 vsnprintf(buf, n, fmt, ap);
65 va_end(ap);
67 return buf;
71 // Zalloc allocates n bytes of zeroed memory and
72 // returns a pointer to it.
73 // If insufficient memory is available, zalloc returns 0.
74 void*
75 zalloc(int n)
77 void *p;
79 p = malloc(n);
80 if (p) {
81 memset(p, 0, n);
83 return p;
87 static void
88 warn_systemd_ignored_option(char *opt, char *arg)
90 if (sd_listen_fds(0) > 0) {
91 warnx("inherited listen fd; ignoring option: %s %s", opt, arg);
96 static void usage(int code) __attribute__ ((noreturn));
97 static void
98 usage(int code)
100 fprintf(stderr, "Use: %s [OPTIONS]\n"
101 "\n"
102 "Options:\n"
103 " -b DIR wal directory\n"
104 " -f MS fsync at most once every MS milliseconds"
105 " (use -f0 for \"always fsync\")\n"
106 " -F never fsync (default)\n"
107 " -l ADDR listen on address (default is 0.0.0.0)\n"
108 " -p PORT listen on port (default is " Portdef ")\n"
109 " -u USER become user and group\n"
110 " -z BYTES set the maximum job size in bytes (default is %d)\n"
111 " -s BYTES set the size of each wal file (default is %d)\n"
112 " (will be rounded up to a multiple of 512 bytes)\n"
113 " -c compact the binlog (default)\n"
114 " -n do not compact the binlog\n"
115 " -v show version information\n"
116 " -V increase verbosity\n"
117 " -h show this help\n",
118 progname, JOB_DATA_SIZE_LIMIT_DEFAULT, Filesizedef);
119 exit(code);
123 static char *flagusage(char *flag) __attribute__ ((noreturn));
124 static char *
125 flagusage(char *flag)
127 warnx("flag requires an argument: %s", flag);
128 usage(5);
132 static size_t
133 parse_size_t(char *str)
135 char r, x;
136 size_t size;
138 r = sscanf(str, "%zu%c", &size, &x);
139 if (1 != r) {
140 warnx("invalid size: %s", str);
141 usage(5);
143 return size;
147 void
148 optparse(Server *s, char **argv)
150 int64 ms;
151 char *arg, c, *tmp;
152 # define EARGF(x) (*arg ? (tmp=arg,arg="",tmp) : *argv ? *argv++ : (x))
154 while ((arg = *argv++) && *arg++ == '-' && *arg) {
155 while ((c = *arg++)) {
156 switch (c) {
157 case 'p':
158 s->port = EARGF(flagusage("-p"));
159 warn_systemd_ignored_option("-p", s->port);
160 break;
161 case 'l':
162 s->addr = EARGF(flagusage("-l"));
163 warn_systemd_ignored_option("-l", s->addr);
164 break;
165 case 'z':
166 job_data_size_limit = parse_size_t(EARGF(flagusage("-z")));
167 break;
168 case 's':
169 s->wal.filesize = parse_size_t(EARGF(flagusage("-s")));
170 break;
171 case 'c':
172 s->wal.nocomp = 0;
173 break;
174 case 'n':
175 s->wal.nocomp = 1;
176 break;
177 case 'f':
178 ms = (int64)parse_size_t(EARGF(flagusage("-f")));
179 s->wal.syncrate = ms * 1000000;
180 s->wal.wantsync = 1;
181 break;
182 case 'F':
183 s->wal.wantsync = 0;
184 break;
185 case 'u':
186 s->user = EARGF(flagusage("-u"));
187 break;
188 case 'b':
189 s->wal.dir = EARGF(flagusage("-b"));
190 s->wal.use = 1;
191 break;
192 case 'h':
193 usage(0);
194 case 'v':
195 printf("beanstalkd %s\n", version);
196 exit(0);
197 case 'V':
198 verbose++;
199 break;
200 default:
201 warnx("unknown flag: %s", arg-2);
202 usage(5);
206 if (arg) {
207 warnx("unknown argument: %s", arg-1);
208 usage(5);