2021-01-14 23:31:51 +11:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
2021-03-15 07:29:51 +11:00
|
|
|
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined( _NetBSD__) || defined(__DragonFly__)
|
2021-03-15 00:18:06 +11:00
|
|
|
#include <string.h>
|
|
|
|
#else
|
|
|
|
#include <bsd/string.h>
|
|
|
|
#endif
|
|
|
|
|
2021-03-23 07:44:23 +11:00
|
|
|
/* e*foo() functions are the equivalent of foo() but handle errors.
|
|
|
|
* In case an error happens:
|
|
|
|
* The error is printed to stdout
|
|
|
|
* return 1
|
|
|
|
*/
|
|
|
|
|
2021-02-04 06:46:36 +11:00
|
|
|
#ifdef __OpenBSD__
|
2021-01-14 23:31:51 +11:00
|
|
|
void
|
|
|
|
eunveil(const char *path, const char *permissions)
|
|
|
|
{
|
|
|
|
if (unveil(path, permissions) == -1) {
|
|
|
|
syslog(LOG_DAEMON, "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_DAEMON, "pledge failed for: %s", promises);
|
|
|
|
err(1, "pledge failed for: %s", promises);
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 06:46:36 +11:00
|
|
|
#endif
|
2021-01-14 23:31:51 +11:00
|
|
|
|
|
|
|
size_t
|
|
|
|
estrlcpy(char *dst, const char *src, size_t dstsize)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
n = strlcpy(dst, src, dstsize);
|
|
|
|
if (n >= dstsize) {
|
2021-02-01 08:05:48 +11:00
|
|
|
err(1, "strlcpy failed for %s = %s", dst, src);
|
2021-01-14 23:31:51 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-23 07:44:23 +11:00
|
|
|
/* send error in syslog, to stdout and die */
|
2021-01-14 23:31:51 +11:00
|
|
|
void
|
|
|
|
errlog(const char *format, ...)
|
|
|
|
{
|
|
|
|
char e[1024] = {'\0'};
|
|
|
|
va_list ap;
|
|
|
|
|
2021-04-30 20:45:34 +10:00
|
|
|
fflush(stdout); /* make sure older messages are printed */
|
|
|
|
|
2021-01-14 23:31:51 +11:00
|
|
|
va_start(ap, format);
|
|
|
|
vsnprintf(e, sizeof(e), format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
syslog(LOG_DAEMON, "%s", e);
|
|
|
|
err(1, "%s", e);
|
|
|
|
}
|