summaryrefslogtreecommitdiffstats
path: root/src/old-stats/fifo-input-connection.c
blob: 1b9759d39e82b7436bf7bc0c9b5fe22759ed6de9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "llist.h"
#include "strescape.h"
#include "istream.h"
#include "ostream.h"
#include "master-service.h"
#include "mail-session.h"
#include "mail-user.h"
#include "mail-command.h"
#include "fifo-input-connection.h"

#include <unistd.h>

#define MAX_INBUF_SIZE (PIPE_BUF*2)

struct fifo_input_connection {
	struct fifo_input_connection *prev, *next;

	int fd;
	struct istream *input;
	struct io *io;
};

static struct fifo_input_connection *fifo_conns = NULL;

static int
fifo_input_connection_request(const char *const *args, const char **error_r)
{
	const char *cmd = args[0];

	if (cmd == NULL) {
		*error_r = "Missing command";
		return -1;
	}
	args++;

	if (strcmp(cmd, "CONNECT") == 0)
		return mail_session_connect_parse(args, error_r);
	if (strcmp(cmd, "DISCONNECT") == 0)
		return mail_session_disconnect_parse(args, error_r);
	if (strcmp(cmd, "UPDATE-SESSION") == 0)
		return mail_session_update_parse(args, error_r);
	if (strcmp(cmd, "ADD-USER") == 0)
		return mail_user_add_parse(args, error_r);
	if (strcmp(cmd, "UPDATE-CMD") == 0)
		return mail_command_update_parse(args, error_r);

	*error_r = "Unknown command";
	return -1;
}

static void fifo_input_connection_input(struct fifo_input_connection *conn)
{
	const char *line, *const *args, *error;

	switch (i_stream_read(conn->input)) {
	case -2:
		i_error("BUG: Mail server sent too much data");
		fifo_input_connection_destroy(&conn);
		return;
	case -1:
		fifo_input_connection_destroy(&conn);
		return;
	}

	while ((line = i_stream_next_line(conn->input)) != NULL) T_BEGIN {
		args = t_strsplit_tabescaped(line);
		if (fifo_input_connection_request(args, &error) < 0)
			i_error("FIFO input error: %s", error);
	} T_END;
}

struct fifo_input_connection *fifo_input_connection_create(int fd)
{
	struct fifo_input_connection *conn;

	conn = i_new(struct fifo_input_connection, 1);
	conn->fd = fd;
	conn->input = i_stream_create_fd(fd, MAX_INBUF_SIZE);
	conn->io = io_add(fd, IO_READ, fifo_input_connection_input, conn);
	DLLIST_PREPEND(&fifo_conns, conn);
	return conn;
}

void fifo_input_connection_destroy(struct fifo_input_connection **_conn)
{
	struct fifo_input_connection *conn = *_conn;

	*_conn = NULL;

	DLLIST_REMOVE(&fifo_conns, conn);
	io_remove(&conn->io);
	i_stream_destroy(&conn->input);
	if (close(conn->fd) < 0)
		i_error("close(conn) failed: %m");
	i_free(conn);
}

void fifo_input_connections_destroy_all(void)
{
	while (fifo_conns != NULL) {
		struct fifo_input_connection *conn = fifo_conns;

		fifo_input_connection_destroy(&conn);
	}
}