2024-11-21 05:42:22 -08:00
|
|
|
/* $OpenBSD: mproc.c,v 1.47 2024/11/21 13:42:22 claudio Exp $ */
|
2013-01-26 01:37:22 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Eric Faurot <eric@faurot.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "smtpd.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
static void mproc_dispatch(int, short, void *);
|
|
|
|
|
|
|
|
int
|
2014-07-08 06:49:09 -07:00
|
|
|
mproc_fork(struct mproc *p, const char *path, char *argv[])
|
2013-01-26 01:37:22 -08:00
|
|
|
{
|
|
|
|
int sp[2];
|
|
|
|
|
2019-06-28 06:32:41 -07:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1)
|
2013-01-26 01:37:22 -08:00
|
|
|
return (-1);
|
|
|
|
|
2016-03-25 08:06:58 -07:00
|
|
|
io_set_nonblocking(sp[0]);
|
|
|
|
io_set_nonblocking(sp[1]);
|
2013-01-26 01:37:22 -08:00
|
|
|
|
|
|
|
if ((p->pid = fork()) == -1)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (p->pid == 0) {
|
|
|
|
/* child process */
|
|
|
|
dup2(sp[0], STDIN_FILENO);
|
2019-06-28 06:32:41 -07:00
|
|
|
if (closefrom(STDERR_FILENO + 1) == -1)
|
2013-01-26 01:37:22 -08:00
|
|
|
exit(1);
|
|
|
|
|
2014-07-08 06:49:09 -07:00
|
|
|
execv(path, argv);
|
2021-05-26 11:08:55 -07:00
|
|
|
fatal("execv: %s", path);
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parent process */
|
|
|
|
close(sp[0]);
|
|
|
|
mproc_init(p, sp[1]);
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
err:
|
2014-07-08 06:49:09 -07:00
|
|
|
log_warn("warn: Failed to start process %s, instance of %s", argv[0], path);
|
2013-01-26 01:37:22 -08:00
|
|
|
close(sp[0]);
|
|
|
|
close(sp[1]);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mproc_init(struct mproc *p, int fd)
|
|
|
|
{
|
2024-11-21 05:42:22 -08:00
|
|
|
if (imsgbuf_init(&p->imsgbuf, fd) == -1)
|
|
|
|
fatal("mproc_init: imsgbuf_init");
|
|
|
|
if (p->proc != PROC_CLIENT)
|
|
|
|
imsgbuf_allow_fdpass(&p->imsgbuf);
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mproc_clear(struct mproc *p)
|
|
|
|
{
|
2016-09-08 05:06:43 -07:00
|
|
|
log_debug("debug: clearing p=%s, fd=%d, pid=%d", p->name, p->imsgbuf.fd, p->pid);
|
|
|
|
|
2020-12-20 06:06:12 -08:00
|
|
|
if (p->events)
|
|
|
|
event_del(&p->ev);
|
2013-01-26 01:37:22 -08:00
|
|
|
close(p->imsgbuf.fd);
|
2024-11-21 05:17:01 -08:00
|
|
|
imsgbuf_clear(&p->imsgbuf);
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mproc_enable(struct mproc *p)
|
|
|
|
{
|
|
|
|
if (p->enable == 0) {
|
2013-05-24 10:03:14 -07:00
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s: enabled",
|
|
|
|
proc_name(smtpd_process),
|
2013-01-26 01:37:22 -08:00
|
|
|
proc_name(p->proc));
|
|
|
|
p->enable = 1;
|
|
|
|
}
|
|
|
|
mproc_event_add(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mproc_disable(struct mproc *p)
|
|
|
|
{
|
|
|
|
if (p->enable == 1) {
|
2013-05-24 10:03:14 -07:00
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s: disabled",
|
|
|
|
proc_name(smtpd_process),
|
2013-01-26 01:37:22 -08:00
|
|
|
proc_name(p->proc));
|
|
|
|
p->enable = 0;
|
|
|
|
}
|
|
|
|
mproc_event_add(p);
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:13:13 -07:00
|
|
|
void
|
2013-01-26 01:37:22 -08:00
|
|
|
mproc_event_add(struct mproc *p)
|
|
|
|
{
|
|
|
|
short events;
|
|
|
|
|
|
|
|
if (p->enable)
|
|
|
|
events = EV_READ;
|
|
|
|
else
|
|
|
|
events = 0;
|
|
|
|
|
2024-11-21 05:18:37 -08:00
|
|
|
if (imsgbuf_queuelen(&p->imsgbuf) > 0)
|
2013-01-26 01:37:22 -08:00
|
|
|
events |= EV_WRITE;
|
|
|
|
|
|
|
|
if (p->events)
|
|
|
|
event_del(&p->ev);
|
|
|
|
|
|
|
|
p->events = events;
|
|
|
|
if (events) {
|
|
|
|
event_set(&p->ev, p->imsgbuf.fd, events, mproc_dispatch, p);
|
|
|
|
event_add(&p->ev, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mproc_dispatch(int fd, short event, void *arg)
|
|
|
|
{
|
|
|
|
struct mproc *p = arg;
|
|
|
|
struct imsg imsg;
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
p->events = 0;
|
|
|
|
|
|
|
|
if (event & EV_READ) {
|
|
|
|
|
2024-11-21 05:32:02 -08:00
|
|
|
n = imsgbuf_read(&p->imsgbuf);
|
2015-10-01 17:32:05 -07:00
|
|
|
|
2016-02-10 07:03:37 -08:00
|
|
|
switch (n) {
|
|
|
|
case -1:
|
2024-11-21 05:17:01 -08:00
|
|
|
log_warn("warn: %s -> %s: imsgbuf_read",
|
2013-05-24 10:03:14 -07:00
|
|
|
proc_name(smtpd_process), p->name);
|
|
|
|
fatal("exiting");
|
2016-02-10 07:03:37 -08:00
|
|
|
/* NOTREACHED */
|
|
|
|
case 0:
|
2013-01-26 01:37:22 -08:00
|
|
|
/* this pipe is dead, so remove the event handler */
|
2016-09-08 05:06:43 -07:00
|
|
|
log_debug("debug: %s -> %s: pipe closed",
|
|
|
|
proc_name(smtpd_process), p->name);
|
2013-01-26 01:37:22 -08:00
|
|
|
p->handler(p, NULL);
|
|
|
|
return;
|
2016-02-10 07:03:37 -08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event & EV_WRITE) {
|
2024-11-21 05:17:01 -08:00
|
|
|
if (imsgbuf_write(&p->imsgbuf) == -1) {
|
2013-10-24 12:20:46 -07:00
|
|
|
/* this pipe is dead, so remove the event handler */
|
2016-09-08 05:06:43 -07:00
|
|
|
log_debug("debug: %s -> %s: pipe closed",
|
|
|
|
proc_name(smtpd_process), p->name);
|
2013-10-24 12:20:46 -07:00
|
|
|
p->handler(p, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2013-01-31 10:34:43 -08:00
|
|
|
if ((n = imsg_get(&p->imsgbuf, &imsg)) == -1) {
|
2015-06-11 12:27:16 -07:00
|
|
|
|
|
|
|
if (smtpd_process == PROC_CONTROL &&
|
|
|
|
p->proc == PROC_CLIENT) {
|
|
|
|
log_warnx("warn: client sent invalid imsg "
|
|
|
|
"over control socket");
|
|
|
|
p->handler(p, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-31 10:34:43 -08:00
|
|
|
log_warn("fatal: %s: error in imsg_get for %s",
|
|
|
|
proc_name(smtpd_process), p->name);
|
|
|
|
fatalx(NULL);
|
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p->handler(p, &imsg);
|
|
|
|
|
|
|
|
imsg_free(&imsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
mproc_event_add(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_forward(struct mproc *p, struct imsg *imsg)
|
|
|
|
{
|
|
|
|
imsg_compose(&p->imsgbuf, imsg->hdr.type, imsg->hdr.peerid,
|
2024-01-20 01:01:03 -08:00
|
|
|
imsg->hdr.pid, imsg_get_fd(imsg), imsg->data,
|
2013-01-26 01:37:22 -08:00
|
|
|
imsg->hdr.len - sizeof(imsg->hdr));
|
|
|
|
|
2015-10-14 14:18:19 -07:00
|
|
|
if (imsg->hdr.type != IMSG_STAT_DECREMENT &&
|
|
|
|
imsg->hdr.type != IMSG_STAT_INCREMENT)
|
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s : %zu %s (forward)",
|
2013-05-24 10:03:14 -07:00
|
|
|
proc_name(smtpd_process),
|
|
|
|
proc_name(p->proc),
|
|
|
|
imsg->hdr.len - sizeof(imsg->hdr),
|
|
|
|
imsg_to_str(imsg->hdr.type));
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
mproc_event_add(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_compose(struct mproc *p, uint32_t type, uint32_t peerid, pid_t pid, int fd,
|
|
|
|
void *data, size_t len)
|
|
|
|
{
|
|
|
|
imsg_compose(&p->imsgbuf, type, peerid, pid, fd, data, len);
|
|
|
|
|
2015-10-14 14:18:19 -07:00
|
|
|
if (type != IMSG_STAT_DECREMENT &&
|
|
|
|
type != IMSG_STAT_INCREMENT)
|
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s : %zu %s",
|
2013-05-24 10:03:14 -07:00
|
|
|
proc_name(smtpd_process),
|
|
|
|
proc_name(p->proc),
|
|
|
|
len,
|
|
|
|
imsg_to_str(type));
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
mproc_event_add(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_composev(struct mproc *p, uint32_t type, uint32_t peerid, pid_t pid,
|
|
|
|
int fd, const struct iovec *iov, int n)
|
|
|
|
{
|
2013-05-24 10:03:14 -07:00
|
|
|
size_t len;
|
2013-01-26 01:37:22 -08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
imsg_composev(&p->imsgbuf, type, peerid, pid, fd, iov, n);
|
|
|
|
|
2013-05-24 10:03:14 -07:00
|
|
|
len = 0;
|
2013-01-26 01:37:22 -08:00
|
|
|
for (i = 0; i < n; i++)
|
2013-05-24 10:03:14 -07:00
|
|
|
len += iov[i].iov_len;
|
|
|
|
|
2015-10-14 14:18:19 -07:00
|
|
|
if (type != IMSG_STAT_DECREMENT &&
|
|
|
|
type != IMSG_STAT_INCREMENT)
|
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s : %zu %s",
|
2013-05-24 10:03:14 -07:00
|
|
|
proc_name(smtpd_process),
|
|
|
|
proc_name(p->proc),
|
|
|
|
len,
|
|
|
|
imsg_to_str(type));
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
mproc_event_add(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-24 10:03:14 -07:00
|
|
|
m_create(struct mproc *p, uint32_t type, uint32_t peerid, pid_t pid, int fd)
|
2013-01-26 01:37:22 -08:00
|
|
|
{
|
2013-05-24 10:03:14 -07:00
|
|
|
p->m_pos = 0;
|
|
|
|
p->m_type = type;
|
|
|
|
p->m_peerid = peerid;
|
|
|
|
p->m_pid = pid;
|
|
|
|
p->m_fd = fd;
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add(struct mproc *p, const void *data, size_t len)
|
|
|
|
{
|
2013-05-24 10:03:14 -07:00
|
|
|
size_t alloc;
|
|
|
|
void *tmp;
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2013-05-24 10:03:14 -07:00
|
|
|
if (p->m_pos + len + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
|
2019-05-24 07:31:30 -07:00
|
|
|
log_warnx("warn: message too large");
|
2013-05-24 10:03:14 -07:00
|
|
|
fatal(NULL);
|
|
|
|
}
|
|
|
|
|
2017-03-17 13:57:57 -07:00
|
|
|
alloc = p->m_alloc ? p->m_alloc : 128;
|
2013-05-24 10:03:14 -07:00
|
|
|
while (p->m_pos + len > alloc)
|
|
|
|
alloc *= 2;
|
|
|
|
if (alloc != p->m_alloc) {
|
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s: realloc %zu -> %zu",
|
|
|
|
proc_name(smtpd_process),
|
|
|
|
proc_name(p->proc),
|
|
|
|
p->m_alloc,
|
|
|
|
alloc);
|
|
|
|
|
2017-03-17 13:57:57 -07:00
|
|
|
tmp = recallocarray(p->m_buf, p->m_alloc, alloc, 1);
|
2013-05-24 10:03:14 -07:00
|
|
|
if (tmp == NULL)
|
|
|
|
fatal("realloc");
|
|
|
|
p->m_alloc = alloc;
|
|
|
|
p->m_buf = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(p->m_buf + p->m_pos, data, len);
|
|
|
|
p->m_pos += len;
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_close(struct mproc *p)
|
|
|
|
{
|
2013-05-24 10:03:14 -07:00
|
|
|
if (imsg_compose(&p->imsgbuf, p->m_type, p->m_peerid, p->m_pid, p->m_fd,
|
|
|
|
p->m_buf, p->m_pos) == -1)
|
|
|
|
fatal("imsg_compose");
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2013-05-24 10:03:14 -07:00
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s : %zu %s",
|
2013-01-31 10:34:43 -08:00
|
|
|
proc_name(smtpd_process),
|
|
|
|
proc_name(p->proc),
|
2013-05-24 10:03:14 -07:00
|
|
|
p->m_pos,
|
|
|
|
imsg_to_str(p->m_type));
|
2013-01-26 01:37:22 -08:00
|
|
|
|
|
|
|
mproc_event_add(p);
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:13:13 -07:00
|
|
|
void
|
|
|
|
m_flush(struct mproc *p)
|
|
|
|
{
|
|
|
|
if (imsg_compose(&p->imsgbuf, p->m_type, p->m_peerid, p->m_pid, p->m_fd,
|
|
|
|
p->m_buf, p->m_pos) == -1)
|
|
|
|
fatal("imsg_compose");
|
|
|
|
|
|
|
|
log_trace(TRACE_MPROC, "mproc: %s -> %s : %zu %s (flush)",
|
|
|
|
proc_name(smtpd_process),
|
|
|
|
proc_name(p->proc),
|
|
|
|
p->m_pos,
|
|
|
|
imsg_to_str(p->m_type));
|
|
|
|
|
|
|
|
p->m_pos = 0;
|
|
|
|
|
2024-11-21 05:17:01 -08:00
|
|
|
if (imsgbuf_flush(&p->imsgbuf) == -1)
|
|
|
|
fatal("imsgbuf_flush");
|
2014-04-29 12:13:13 -07:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
static struct imsg * current;
|
|
|
|
|
|
|
|
static void
|
|
|
|
m_error(const char *error)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
|
2014-04-19 10:45:05 -07:00
|
|
|
(void)snprintf(buf, sizeof buf, "%s: %s: %s",
|
2013-01-26 01:37:22 -08:00
|
|
|
proc_name(smtpd_process),
|
|
|
|
imsg_to_str(current->hdr.type),
|
|
|
|
error);
|
2014-07-08 06:49:09 -07:00
|
|
|
fatalx("%s", buf);
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_msg(struct msg *m, struct imsg *imsg)
|
|
|
|
{
|
|
|
|
current = imsg;
|
|
|
|
m->pos = imsg->data;
|
|
|
|
m->end = m->pos + (imsg->hdr.len - sizeof(imsg->hdr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_end(struct msg *m)
|
|
|
|
{
|
|
|
|
if (m->pos != m->end)
|
|
|
|
m_error("not at msg end");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
m_is_eom(struct msg *m)
|
|
|
|
{
|
|
|
|
return (m->pos == m->end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
m_get(struct msg *m, void *dst, size_t sz)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
if (sz > MAX_IMSGSIZE ||
|
|
|
|
m->end - m->pos < (ssize_t)sz)
|
|
|
|
fatalx("msg too short");
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
memmove(dst, m->pos, sz);
|
|
|
|
m->pos += sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_int(struct mproc *m, int v)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, &v, sizeof(v));
|
2013-01-26 01:37:22 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_u32(struct mproc *m, uint32_t u32)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, &u32, sizeof(u32));
|
2013-01-26 01:37:22 -08:00
|
|
|
};
|
|
|
|
|
2014-04-29 12:13:13 -07:00
|
|
|
void
|
|
|
|
m_add_size(struct mproc *m, size_t sz)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, &sz, sizeof(sz));
|
2014-04-29 12:13:13 -07:00
|
|
|
};
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
void
|
|
|
|
m_add_time(struct mproc *m, time_t v)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, &v, sizeof(v));
|
2013-01-26 01:37:22 -08:00
|
|
|
};
|
|
|
|
|
2018-12-13 09:08:10 -08:00
|
|
|
void
|
|
|
|
m_add_timeval(struct mproc *m, struct timeval *tv)
|
|
|
|
{
|
|
|
|
m_add(m, tv, sizeof(*tv));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
void
|
|
|
|
m_add_string(struct mproc *m, const char *v)
|
|
|
|
{
|
2018-11-11 06:00:51 -08:00
|
|
|
if (v) {
|
|
|
|
m_add(m, "s", 1);
|
|
|
|
m_add(m, v, strlen(v) + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_add(m, "\0", 1);
|
2013-01-26 01:37:22 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_data(struct mproc *m, const void *v, size_t len)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add_size(m, len);
|
|
|
|
m_add(m, v, len);
|
2013-01-26 01:37:22 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_id(struct mproc *m, uint64_t v)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, &v, sizeof(v));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_evpid(struct mproc *m, uint64_t v)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, &v, sizeof(v));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_msgid(struct mproc *m, uint32_t v)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, &v, sizeof(v));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_sockaddr(struct mproc *m, const struct sockaddr *sa)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add_size(m, sa->sa_len);
|
|
|
|
m_add(m, sa, sa->sa_len);
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_mailaddr(struct mproc *m, const struct mailaddr *maddr)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add(m, maddr, sizeof(*maddr));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_add_envelope(struct mproc *m, const struct envelope *evp)
|
|
|
|
{
|
|
|
|
char buf[sizeof(*evp)];
|
|
|
|
|
|
|
|
envelope_dump_buffer(evp, buf, sizeof(buf));
|
|
|
|
m_add_evpid(m, evp->id);
|
2016-09-03 09:06:26 -07:00
|
|
|
m_add_string(m, buf);
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
2014-07-08 06:49:09 -07:00
|
|
|
void
|
|
|
|
m_add_params(struct mproc *m, struct dict *d)
|
|
|
|
{
|
|
|
|
const char *key;
|
|
|
|
char *value;
|
|
|
|
void *iter;
|
|
|
|
|
|
|
|
if (d == NULL) {
|
|
|
|
m_add_size(m, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_add_size(m, dict_count(d));
|
|
|
|
iter = NULL;
|
|
|
|
while (dict_iter(d, &iter, &key, (void **)&value)) {
|
|
|
|
m_add_string(m, key);
|
|
|
|
m_add_string(m, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
void
|
|
|
|
m_get_int(struct msg *m, int *i)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, i, sizeof(*i));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_u32(struct msg *m, uint32_t *u32)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, u32, sizeof(*u32));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
2014-04-29 12:13:13 -07:00
|
|
|
void
|
|
|
|
m_get_size(struct msg *m, size_t *sz)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, sz, sizeof(*sz));
|
2014-04-29 12:13:13 -07:00
|
|
|
}
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
void
|
|
|
|
m_get_time(struct msg *m, time_t *t)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, t, sizeof(*t));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
2018-12-13 09:08:10 -08:00
|
|
|
void
|
|
|
|
m_get_timeval(struct msg *m, struct timeval *tv)
|
|
|
|
{
|
|
|
|
m_get(m, tv, sizeof(*tv));
|
|
|
|
}
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
void
|
|
|
|
m_get_string(struct msg *m, const char **s)
|
|
|
|
{
|
|
|
|
uint8_t *end;
|
2018-11-11 06:00:51 -08:00
|
|
|
char c;
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2016-09-03 09:06:26 -07:00
|
|
|
if (m->pos >= m->end)
|
2013-01-26 01:37:22 -08:00
|
|
|
m_error("msg too short");
|
|
|
|
|
2018-11-11 06:00:51 -08:00
|
|
|
c = *m->pos++;
|
|
|
|
if (c == '\0') {
|
|
|
|
*s = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->pos >= m->end)
|
|
|
|
m_error("msg too short");
|
2016-09-03 09:06:26 -07:00
|
|
|
end = memchr(m->pos, 0, m->end - m->pos);
|
2013-01-26 01:37:22 -08:00
|
|
|
if (end == NULL)
|
|
|
|
m_error("unterminated string");
|
2015-10-14 14:18:19 -07:00
|
|
|
|
2016-09-03 09:06:26 -07:00
|
|
|
*s = m->pos;
|
2013-01-26 01:37:22 -08:00
|
|
|
m->pos = end + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_data(struct msg *m, const void **data, size_t *sz)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get_size(m, sz);
|
|
|
|
|
2018-12-17 00:56:31 -08:00
|
|
|
if (*sz == 0) {
|
|
|
|
*data = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-03 09:06:26 -07:00
|
|
|
if (m->pos + *sz > m->end)
|
|
|
|
m_error("msg too short");
|
|
|
|
|
|
|
|
*data = m->pos;
|
|
|
|
m->pos += *sz;
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_evpid(struct msg *m, uint64_t *evpid)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, evpid, sizeof(*evpid));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_msgid(struct msg *m, uint32_t *msgid)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, msgid, sizeof(*msgid));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_id(struct msg *m, uint64_t *id)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, id, sizeof(*id));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_sockaddr(struct msg *m, struct sockaddr *sa)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
size_t len;
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get_size(m, &len);
|
|
|
|
m_get(m, sa, len);
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_mailaddr(struct msg *m, struct mailaddr *maddr)
|
|
|
|
{
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get(m, maddr, sizeof(*maddr));
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_get_envelope(struct msg *m, struct envelope *evp)
|
|
|
|
{
|
|
|
|
uint64_t evpid;
|
2016-09-03 09:06:26 -07:00
|
|
|
const char *buf;
|
2013-01-26 01:37:22 -08:00
|
|
|
|
|
|
|
m_get_evpid(m, &evpid);
|
2016-09-03 09:06:26 -07:00
|
|
|
m_get_string(m, &buf);
|
2020-03-17 02:01:53 -07:00
|
|
|
if (buf == NULL)
|
|
|
|
fatalx("empty envelope buffer");
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2016-09-03 09:06:26 -07:00
|
|
|
if (!envelope_load_buffer(evp, buf, strlen(buf)))
|
2013-07-19 08:18:42 -07:00
|
|
|
fatalx("failed to retrieve envelope");
|
2013-01-26 01:37:22 -08:00
|
|
|
evp->id = evpid;
|
|
|
|
}
|
2014-07-08 06:49:09 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
m_get_params(struct msg *m, struct dict *d)
|
|
|
|
{
|
|
|
|
size_t c;
|
|
|
|
const char *key;
|
|
|
|
const char *value;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
dict_init(d);
|
|
|
|
|
|
|
|
m_get_size(m, &c);
|
|
|
|
|
|
|
|
for (; c; c--) {
|
|
|
|
m_get_string(m, &key);
|
|
|
|
m_get_string(m, &value);
|
|
|
|
if ((tmp = strdup(value)) == NULL)
|
|
|
|
fatal("m_get_params");
|
|
|
|
dict_set(d, key, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
m_clear_params(struct dict *d)
|
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
while (dict_poproot(d, (void **)&value))
|
|
|
|
free(value);
|
|
|
|
}
|