/
ipc-client.c
177 lines (147 loc) · 3.97 KB
/
ipc-client.c
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
/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "net.h"
#include "llist.h"
#include "istream.h"
#include "ostream.h"
#include "hostpid.h"
#include "master-service.h"
#include "ipc-client.h"
#include <unistd.h>
struct ipc_client_cmd {
struct ipc_client_cmd *prev, *next;
ipc_client_callback_t *callback;
void *context;
};
struct ipc_client {
char *path;
ipc_client_callback_t *callback;
int fd;
struct io *io;
struct timeout *to;
struct istream *input;
struct ostream *output;
struct ipc_client_cmd *cmds_head, *cmds_tail;
};
static void ipc_client_disconnect(struct ipc_client *client);
static void ipc_client_input_line(struct ipc_client *client, const char *line)
{
struct ipc_client_cmd *cmd = client->cmds_head;
enum ipc_client_cmd_state state;
bool disconnect = FALSE;
if (cmd == NULL) {
i_error("IPC proxy sent unexpected input: %s", line);
return;
}
switch (*line++) {
case ':':
state = IPC_CLIENT_CMD_STATE_REPLY;
break;
case '+':
state = IPC_CLIENT_CMD_STATE_OK;
break;
case '-':
state = IPC_CLIENT_CMD_STATE_ERROR;
break;
default:
i_error("IPC proxy sent invalid input: %s", line);
line = "Invalid input";
disconnect = TRUE;
state = IPC_CLIENT_CMD_STATE_ERROR;
break;
}
if (state != IPC_CLIENT_CMD_STATE_REPLY)
DLLIST2_REMOVE(&client->cmds_head, &client->cmds_tail, cmd);
cmd->callback(state, line, cmd->context);
i_free(cmd);
if (disconnect)
ipc_client_disconnect(client);
}
static void ipc_client_input(struct ipc_client *client)
{
const char *line;
if (i_stream_read(client->input) < 0) {
ipc_client_disconnect(client);
return;
}
while ((line = i_stream_next_line(client->input)) != NULL) T_BEGIN {
ipc_client_input_line(client, line);
} T_END;
}
static int ipc_client_connect(struct ipc_client *client)
{
if (client->fd != -1)
return 0;
client->fd = net_connect_unix(client->path);
if (client->fd == -1) {
i_error("connect(%s) failed: %m", client->path);
return -1;
}
client->io = io_add(client->fd, IO_READ, ipc_client_input, client);
client->input = i_stream_create_fd(client->fd, (size_t)-1, FALSE);
client->output = o_stream_create_fd(client->fd, (size_t)-1, FALSE);
o_stream_set_no_error_handling(client->output, TRUE);
return 0;
}
static void ipc_client_abort_commands(struct ipc_client *client,
const char *reason)
{
struct ipc_client_cmd *cmd, *next;
cmd = client->cmds_head;
client->cmds_head = client->cmds_tail = NULL;
for (; cmd != NULL; cmd = next) {
cmd->callback(IPC_CLIENT_CMD_STATE_ERROR, reason, cmd->context);
next = cmd->next;
i_free(cmd);
}
}
static void ipc_client_disconnect(struct ipc_client *client)
{
if (client->fd == -1)
return;
ipc_client_abort_commands(client, "Disconnected");
io_remove(&client->io);
i_stream_destroy(&client->input);
o_stream_destroy(&client->output);
if (close(client->fd) < 0)
i_error("close(%s) failed: %m", client->path);
client->fd = -1;
}
struct ipc_client *
ipc_client_init(const char *ipc_socket_path)
{
struct ipc_client *client;
client = i_new(struct ipc_client, 1);
client->path = i_strdup(ipc_socket_path);
client->fd = -1;
return client;
}
void ipc_client_deinit(struct ipc_client **_client)
{
struct ipc_client *client = *_client;
*_client = NULL;
ipc_client_disconnect(client);
i_free(client->path);
i_free(client);
}
void ipc_client_cmd(struct ipc_client *client, const char *cmd,
ipc_client_callback_t *callback, void *context)
{
struct ipc_client_cmd *ipc_cmd;
struct const_iovec iov[2];
if (ipc_client_connect(client) < 0) {
callback(IPC_CLIENT_CMD_STATE_ERROR,
"ipc connect failed", context);
return;
}
iov[0].iov_base = cmd;
iov[0].iov_len = strlen(cmd);
iov[1].iov_base = "\n";
iov[1].iov_len = 1;
o_stream_nsendv(client->output, iov, N_ELEMENTS(iov));
ipc_cmd = i_new(struct ipc_client_cmd, 1);
ipc_cmd->callback = callback;
ipc_cmd->context = context;
DLLIST2_APPEND(&client->cmds_head, &client->cmds_tail, ipc_cmd);
}