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
|
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
#include "common.h"
#include "array.h"
#include "aqueue.h"
#include "hash.h"
#include "ioloop.h"
#include "service.h"
#include "service-process.h"
#include "service-process-notify.h"
#include "service-anvil.h"
#include "service-log.h"
#include <unistd.h>
static int service_log_fds_init(const char *log_prefix, int log_fd[2],
buffer_t *handshake_buf)
{
struct log_service_handshake handshake;
ssize_t ret;
i_assert(log_fd[0] == -1);
if (pipe(log_fd) < 0) {
i_error("pipe() failed: %m");
return -1;
}
fd_close_on_exec(log_fd[0], TRUE);
fd_close_on_exec(log_fd[1], TRUE);
i_zero(&handshake);
handshake.log_magic = MASTER_LOG_MAGIC;
handshake.prefix_len = strlen(log_prefix);
buffer_set_used_size(handshake_buf, 0);
buffer_append(handshake_buf, &handshake, sizeof(handshake));
buffer_append(handshake_buf, log_prefix, strlen(log_prefix));
ret = write(log_fd[1], handshake_buf->data, handshake_buf->used);
if (ret < 0) {
i_error("write(log handshake) failed: %m");
return -1;
}
if ((size_t)ret != handshake_buf->used) {
i_error("write(log handshake) didn't write everything");
return -1;
}
return 0;
}
static int
service_process_write_log_bye(int fd, struct service_process *process)
{
const char *data;
if (process->service->log_process_internal_fd == -1) {
/* another log process was just destroyed */
return 0;
}
data = t_strdup_printf("%d %s BYE\n",
process->service->log_process_internal_fd,
dec2str(process->pid));
if (write(fd, data, strlen(data)) < 0) {
if (errno != EAGAIN)
i_error("write(log process) failed: %m");
return -1;
}
return 0;
}
int services_log_init(struct service_list *service_list)
{
struct service *service;
const char *log_prefix;
buffer_t *handshake_buf;
ssize_t ret = 0;
int fd;
handshake_buf = buffer_create_dynamic(default_pool, 256);
if (service_log_fds_init(MASTER_LOG_PREFIX_NAME,
service_list->master_log_fd,
handshake_buf) < 0)
ret = -1;
else
fd_set_nonblock(service_list->master_log_fd[1], TRUE);
i_assert(service_list->log_byes == NULL);
service_list->log_byes =
service_process_notify_init(service_list->master_log_fd[1],
service_process_write_log_bye);
fd = MASTER_LISTEN_FD_FIRST + 1;
array_foreach_elem(&service_list->services, service) {
if (service->type == SERVICE_TYPE_LOG)
continue;
log_prefix = t_strconcat(service->set->name, ": ", NULL);
if (service_log_fds_init(log_prefix, service->log_fd,
handshake_buf) < 0) {
ret = -1;
break;
}
service->log_process_internal_fd = fd++;
}
buffer_free(&handshake_buf);
if (ret < 0) {
services_log_deinit(service_list);
return -1;
}
service_anvil_send_log_fd();
return 0;
}
void services_log_deinit(struct service_list *service_list)
{
struct service *const *services;
unsigned int i, count;
services = array_get(&service_list->services, &count);
for (i = 0; i < count; i++) {
if (services[i]->log_fd[0] != -1) {
if (close(services[i]->log_fd[0]) < 0) {
service_error(services[i],
"close(log_fd) failed: %m");
}
if (close(services[i]->log_fd[1]) < 0) {
service_error(services[i],
"close(log_fd) failed: %m");
}
services[i]->log_fd[0] = -1;
services[i]->log_fd[1] = -1;
services[i]->log_process_internal_fd = -1;
}
}
if (service_list->log_byes != NULL)
service_process_notify_deinit(&service_list->log_byes);
if (service_list->master_log_fd[0] != -1) {
if (close(service_list->master_log_fd[0]) < 0)
i_error("close(master log fd) failed: %m");
if (close(service_list->master_log_fd[1]) < 0)
i_error("close(master log fd) failed: %m");
service_list->master_log_fd[0] = -1;
service_list->master_log_fd[1] = -1;
}
}
void services_log_dup2(ARRAY_TYPE(dup2) *dups,
struct service_list *service_list,
unsigned int first_fd, unsigned int *fd_count)
{
struct service *service;
unsigned int n = 0;
/* master log fd is always the first one */
dup2_append(dups, service_list->master_log_fd[0], first_fd);
n++; *fd_count += 1;
array_foreach_elem(&service_list->services, service) {
if (service->log_fd[1] == -1)
continue;
i_assert((int)(first_fd + n) == service->log_process_internal_fd);
dup2_append(dups, service->log_fd[0], first_fd + n);
n++; *fd_count += 1;
}
}
|