You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.8 KiB

#include <err.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include "utils.h"
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined( __NetBSD__) || defined(__DragonFly__)
#include <string.h>
#include <bsd/string.h>
/* e*foo() functions are the equivalent of foo() but handle errors.
* In case an error happens:
* The error is printed to stdout
* return 1
#ifdef __OpenBSD__
eunveil(const char *path, const char *permissions)
if (unveil(path, permissions) == -1) {
syslog(LOG_ERR, "unveil on %s failed", path);
err(1, "unveil on %s failed", path);
epledge(const char *promises, const char *execpromises)
if (pledge(promises, execpromises) == -1) {
syslog(LOG_ERR, "pledge failed for: %s", promises);
err(1, "pledge failed for: %s", promises);
estrlcpy(char *dst, const char *src, size_t dstsize)
size_t n = 0;
n = strlcpy(dst, src, dstsize);
if (n >= dstsize) {
err(1, "strlcpy failed for %s = %s", dst, src);
return n;
estrlcat(char *dst, const char *src, size_t dstsize)
size_t size;
if ((size = strlcat(dst, src, dstsize)) >= dstsize)
err(1, "strlcat on %s + %s", dst, src);
return size;
esetenv(const char *name, const char *value, int overwrite)
int ret = 0;
ret = setenv(name, value, overwrite);
if (ret != 0) {
err(1, "setenv %s:%s", name, value);
return ret;
/* send error in syslog, to stdout and die */
errlog(const char *format, ...)
char e[1024] = {'\0'};
va_list ap;
fflush(stdout); /* make sure older messages are printed */
va_start(ap, format);
vsnprintf(e, sizeof(e), format, ap);
syslog(LOG_ERR, "%s", e);
err(1, "%s", e);