summaryrefslogtreecommitdiffstats
path: root/src/master/service-anvil.c
blob: f5f86328c55684f9828ba1f7f8fb8b41c2d11ba7 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */

#include "common.h"
#include "ioloop.h"
#include "fdpass.h"
#include "service.h"
#include "service-process.h"
#include "service-process-notify.h"
#include "service-anvil.h"

#include <unistd.h>

#define ANVIL_HANDSHAKE "VERSION\tanvil\t1\t0\n"

struct service_anvil_global *service_anvil_global;

static void
service_list_anvil_discard_input_stop(struct service_anvil_global *anvil)
{
	if (anvil->io_blocking != NULL) {
		io_remove(&anvil->io_blocking);
		io_remove(&anvil->io_nonblocking);
	}
}

static void anvil_input_fd_discard(struct service_anvil_global *anvil, int fd)
{
	char buf[1024];
	ssize_t ret;

	ret = read(fd, buf, sizeof(buf));
	if (ret <= 0) {
		i_error("read(anvil fd) failed: %m");
		service_list_anvil_discard_input_stop(anvil);
	}
}

static void anvil_input_blocking_discard(struct service_anvil_global *anvil)
{
	anvil_input_fd_discard(anvil, anvil->blocking_fd[0]);
}

static void anvil_input_nonblocking_discard(struct service_anvil_global *anvil)
{
	anvil_input_fd_discard(anvil, anvil->nonblocking_fd[0]);
}

static void service_list_anvil_discard_input(struct service_anvil_global *anvil)
{
	if (anvil->io_blocking != NULL)
		return;

	anvil->io_blocking = io_add(anvil->blocking_fd[0], IO_READ,
				    anvil_input_blocking_discard, anvil);
	anvil->io_nonblocking = io_add(anvil->nonblocking_fd[0], IO_READ,
				       anvil_input_nonblocking_discard, anvil);
}

static int anvil_send_handshake(int fd, const char **error_r)
{
	ssize_t ret;

	ret = write(fd, ANVIL_HANDSHAKE, strlen(ANVIL_HANDSHAKE));
	if (ret < 0) {
		*error_r = t_strdup_printf("write(anvil) failed: %m");
		return -1;
	}
	if (ret == 0) {
		*error_r = t_strdup_printf("write(anvil) returned EOF");
		return -1;
	}
	/* this is a pipe, it either wrote all of it or nothing */
	i_assert((size_t)ret == strlen(ANVIL_HANDSHAKE));
	return 0;
}

static int
service_process_write_anvil_kill(int fd, struct service_process *process)
{
	const char *data;

	data = t_strdup_printf("KILL\t%s\n", dec2str(process->pid));
	if (write(fd, data, strlen(data)) < 0) {
		if (errno != EAGAIN)
			i_error("write(anvil process) failed: %m");
		return -1;
	}
	return 0;
}

void service_anvil_monitor_start(struct service_list *service_list)
{
	struct service *service;

	if (service_anvil_global->process_count == 0)
		service_list_anvil_discard_input(service_anvil_global);
	else {
		service = service_lookup_type(service_list, SERVICE_TYPE_ANVIL);
		(void)service_process_create(service);
	}
}

void service_anvil_process_created(struct service_process *process)
{
	struct service_anvil_global *anvil = service_anvil_global;
	const char *error;

	service_anvil_global->pid = process->pid;
	service_anvil_global->uid = process->uid;
	service_anvil_global->process_count++;
	service_list_anvil_discard_input_stop(anvil);

	if (anvil_send_handshake(anvil->blocking_fd[1], &error) < 0 ||
	    anvil_send_handshake(anvil->nonblocking_fd[1], &error) < 0)
		service_error(process->service, "%s", error);
}

void service_anvil_process_destroyed(struct service_process *process)
{
	i_assert(service_anvil_global->process_count > 0);
	if (--service_anvil_global->process_count == 0)
		service_list_anvil_discard_input(service_anvil_global);

	if (service_anvil_global->pid == process->pid)
		service_anvil_global->pid = 0;
	service_anvil_global->restarted = TRUE;
}

void service_anvil_send_log_fd(void)
{
	ssize_t ret;
	char b = 0;

	if (service_anvil_global->process_count == 0)
		return;

	ret = fd_send(service_anvil_global->log_fdpass_fd[1],
		      services->anvil->log_fd[1], &b, 1);
	if (ret < 0)
		i_error("fd_send(anvil log fd) failed: %m");
	else if (ret == 0)
		i_error("fd_send(anvil log fd) failed: disconnected");
}

void service_anvil_global_init(void)
{
	struct service_anvil_global *anvil;

	anvil = i_new(struct service_anvil_global, 1);
	if (pipe(anvil->status_fd) < 0)
		i_fatal("pipe() failed: %m");
	if (pipe(anvil->blocking_fd) < 0)
		i_fatal("pipe() failed: %m");
	if (pipe(anvil->nonblocking_fd) < 0)
		i_fatal("pipe() failed: %m");
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, anvil->log_fdpass_fd) < 0)
		i_fatal("socketpair() failed: %m");
	fd_set_nonblock(anvil->status_fd[0], TRUE);
	fd_set_nonblock(anvil->status_fd[1], TRUE);
	fd_set_nonblock(anvil->nonblocking_fd[1], TRUE);

	fd_close_on_exec(anvil->status_fd[0], TRUE);
	fd_close_on_exec(anvil->status_fd[1], TRUE);
	fd_close_on_exec(anvil->blocking_fd[0], TRUE);
	fd_close_on_exec(anvil->blocking_fd[1], TRUE);
	fd_close_on_exec(anvil->nonblocking_fd[0], TRUE);
	fd_close_on_exec(anvil->nonblocking_fd[1], TRUE);
	fd_close_on_exec(anvil->log_fdpass_fd[0], TRUE);
	fd_close_on_exec(anvil->log_fdpass_fd[1], TRUE);

	anvil->kills =
		service_process_notify_init(anvil->nonblocking_fd[1],
					    service_process_write_anvil_kill);
	service_anvil_global = anvil;
}

void service_anvil_global_deinit(void)
{
	struct service_anvil_global *anvil = service_anvil_global;

	service_list_anvil_discard_input_stop(anvil);
	service_process_notify_deinit(&anvil->kills);
	if (close(anvil->log_fdpass_fd[0]) < 0)
		i_error("close(anvil) failed: %m");
	if (close(anvil->log_fdpass_fd[1]) < 0)
		i_error("close(anvil) failed: %m");
	if (close(anvil->blocking_fd[0]) < 0)
		i_error("close(anvil) failed: %m");
	if (close(anvil->blocking_fd[1]) < 0)
		i_error("close(anvil) failed: %m");
	if (close(anvil->nonblocking_fd[0]) < 0)
		i_error("close(anvil) failed: %m");
	if (close(anvil->nonblocking_fd[1]) < 0)
		i_error("close(anvil) failed: %m");
	if (close(anvil->status_fd[0]) < 0)
		i_error("close(anvil) failed: %m");
	if (close(anvil->status_fd[1]) < 0)
		i_error("close(anvil) failed: %m");
	i_free(anvil);

	service_anvil_global = NULL;
}