18 vwarnx(const char *err
, const char *fmt
, va_list args
)
20 fprintf(stderr
, "%s: ", progname
);
22 vfprintf(stderr
, fmt
, args
);
23 if (err
) fprintf(stderr
, ": %s", err
);
29 warn(const char *fmt
, ...)
31 char *err
= strerror(errno
); /* must be done first thing */
35 vwarnx(err
, fmt
, args
);
40 warnx(const char *fmt
, ...)
44 vwarnx(NULL
, fmt
, args
);
50 fmtalloc(char *fmt
, ...)
56 // find out how much space is needed
58 n
= vsnprintf(0, 0, fmt
, ap
) + 1; // include space for trailing NUL
64 vsnprintf(buf
, n
, fmt
, ap
);
71 // Zalloc allocates n bytes of zeroed memory and
72 // returns a pointer to it.
73 // If insufficient memory is available, zalloc returns 0.
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
));
100 fprintf(stderr
, "Use: %s [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
);
123 static char *flagusage(char *flag
) __attribute__ ((noreturn
));
125 flagusage(char *flag
)
127 warnx("flag requires an argument: %s", flag
);
133 parse_size_t(char *str
)
138 r
= sscanf(str
, "%zu%c", &size
, &x
);
140 warnx("invalid size: %s", str
);
148 optparse(Server
*s
, char **argv
)
152 # define EARGF(x) (*arg ? (tmp=arg,arg="",tmp) : *argv ? *argv++ : (x))
154 while ((arg
= *argv
++) && *arg
++ == '-' && *arg
) {
155 while ((c
= *arg
++)) {
158 s
->port
= EARGF(flagusage("-p"));
159 warn_systemd_ignored_option("-p", s
->port
);
162 s
->addr
= EARGF(flagusage("-l"));
163 warn_systemd_ignored_option("-l", s
->addr
);
166 job_data_size_limit
= parse_size_t(EARGF(flagusage("-z")));
169 s
->wal
.filesize
= parse_size_t(EARGF(flagusage("-s")));
178 ms
= (int64
)parse_size_t(EARGF(flagusage("-f")));
179 s
->wal
.syncrate
= ms
* 1000000;
186 s
->user
= EARGF(flagusage("-u"));
189 s
->wal
.dir
= EARGF(flagusage("-b"));
195 printf("beanstalkd %s\n", version
);
201 warnx("unknown flag: %s", arg
-2);
207 warnx("unknown argument: %s", arg
-1);