94 lines
1.8 KiB
C
94 lines
1.8 KiB
C
#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>
|
|
#else
|
|
#include <bsd/string.h>
|
|
#endif
|
|
|
|
/* 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__
|
|
void
|
|
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);
|
|
}
|
|
}
|
|
|
|
void
|
|
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);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
size_t
|
|
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;
|
|
}
|
|
|
|
size_t
|
|
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;
|
|
}
|
|
|
|
int
|
|
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 */
|
|
void
|
|
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);
|
|
va_end(ap);
|
|
|
|
syslog(LOG_ERR, "%s", e);
|
|
err(1, "%s", e);
|
|
}
|