File: script-client.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (329 lines) | stat: -rw-r--r-- 7,832 bytes parent folder | download | duplicates (3)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "istream-private.h"
#include "ostream.h"

#include "script-client-private.h"

#include <unistd.h>

#define MAX_OUTBUF_SIZE 16384

static void script_client_timeout(struct script_client *sclient)
{
	script_client_fail(sclient, SCRIPT_CLIENT_ERROR_RUN_TIMEOUT);
}

static void script_client_connect_timeout(struct script_client *sclient)
{
	script_client_fail(sclient, SCRIPT_CLIENT_ERROR_CONNECT_TIMEOUT);
}

static int script_client_connect(struct script_client *sclient)
{
	if (sclient->set->client_connect_timeout_msecs != 0) {
		sclient->to = timeout_add
			(sclient->set->client_connect_timeout_msecs,
				script_client_connect_timeout, sclient);
	}

	if ( sclient->connect(sclient) < 0 ) {
		script_client_fail(sclient, SCRIPT_CLIENT_ERROR_IO);
		return -1;
	}

	return 1;
}

static void script_client_disconnect
(struct script_client *sclient, bool force)
{
	int ret, error = FALSE;

	if ( sclient->ioloop != NULL )
		io_loop_stop(sclient->ioloop);

	if ( sclient->disconnected )
		return;

	if ( (ret=sclient->close_output(sclient)) < 0 )
		error = TRUE;
	
	if ( (ret=sclient->disconnect(sclient, force)) < 0 )
		error = TRUE;

	if ( sclient->script_input != NULL )
		i_stream_destroy(&sclient->script_input);
	if ( sclient->script_output != NULL )
		o_stream_destroy(&sclient->script_output);

	if ( sclient->to != NULL )
		timeout_remove(&sclient->to);
	if ( sclient->io != NULL )
		io_remove(&sclient->io);

	if (sclient->fd_in != -1 && close(sclient->fd_in) < 0)
		i_error("close(%s) failed: %m", sclient->path);
	if (sclient->fd_out != -1 && sclient->fd_out != sclient->fd_out)
		i_error("close(%s/out) failed: %m", sclient->path);
	sclient->fd_in = sclient->fd_out = -1;
	
	sclient->disconnected = TRUE;
	if (error && sclient->error == SCRIPT_CLIENT_ERROR_NONE ) {
		sclient->error = SCRIPT_CLIENT_ERROR_UNKNOWN;
	}
}

void script_client_fail
(struct script_client *sclient, enum script_client_error error)
{
	if ( sclient->error != SCRIPT_CLIENT_ERROR_NONE )
		return;

	sclient->error = error;
	script_client_disconnect(sclient, TRUE);

	sclient->failure(sclient, error);
}

static int script_client_script_output(struct script_client *sclient)
{
	struct istream *input = sclient->input;
	struct ostream *output = sclient->script_output;
	const unsigned char *data;
	size_t size;
	int ret = 0;

	if ((ret = o_stream_flush(output)) <= 0) {
		if (ret < 0)
			script_client_fail(sclient, SCRIPT_CLIENT_ERROR_IO);
		return ret;
	}

	if ( input != NULL && output != NULL ) {
		do {
			while ( (data=i_stream_get_data(input, &size)) != NULL ) {
				ssize_t sent;
	
				if ( (sent=o_stream_send(output, data, size)) < 0 ) {
					script_client_fail(sclient, SCRIPT_CLIENT_ERROR_IO);
					return -1;
				}
	
				if ( sent == 0 )
					return 0;
				i_stream_skip(input, sent);
			}
		} while ( (ret=i_stream_read(input)) > 0 );

		if ( ret == 0 ) {
			// FIXME: not supposed to happen; returning 0 will poll the input stream
			return 0;
		}

		if ( ret < 0 ) {
			if ( !input->eof ) {
				script_client_fail(sclient, SCRIPT_CLIENT_ERROR_IO);
				return -1;
			} else if ( !i_stream_have_bytes_left(input) ) {
				i_stream_unref(&sclient->input);
				input = NULL;

				if ( (ret = o_stream_flush(output)) <= 0 ) {
					if ( ret < 0 )
						script_client_fail(sclient, SCRIPT_CLIENT_ERROR_IO);
					return ret;
				}
			} 
		}
	}

	if ( input == NULL ) {
		o_stream_unref(&sclient->script_output);

		if ( sclient->script_input == NULL ) {
			script_client_disconnect(sclient, FALSE);
		} else {
			sclient->close_output(sclient);
		}
		return 0;
	}

	return 1;
}

static void script_client_script_input(struct script_client *sclient)
{
	struct istream *input = sclient->script_input;
	struct ostream *output = sclient->output;
	const unsigned char *data;
	size_t size;
	int ret = 0;

	if ( input != NULL ) {
		while ( (ret=i_stream_read_data(input, &data, &size, 0)) > 0 ) {
			if ( output != NULL ) {
				ssize_t sent;

				if ( (sent=o_stream_send(output, data, size)) < 0 ) {
					script_client_fail(sclient, SCRIPT_CLIENT_ERROR_IO);
					return;
				}
				size = (size_t)sent;
			}

			i_stream_skip(input, size);
		}

		if ( ret < 0 ) {
			if ( input->eof ) {
				script_client_disconnect(sclient, FALSE);
				return;
			}
			script_client_fail(sclient, SCRIPT_CLIENT_ERROR_IO);
		}
	}
}

int script_client_script_connected
(struct script_client *sclient)
{
	int ret = 1;

	sclient->start_time = ioloop_time;
	if (sclient->to != NULL)
		timeout_remove(&sclient->to);
	if ( sclient->set->input_idle_timeout_secs != 0 ) {
		sclient->to = timeout_add(sclient->set->input_idle_timeout_secs*1000,
      script_client_timeout, sclient);
	}

	/* run output */
	if ( sclient->script_output != NULL &&
		(ret=script_client_script_output(sclient)) == 0 ) {
		if ( sclient->script_output != NULL ) {
			o_stream_set_flush_callback
				(sclient->script_output, script_client_script_output, sclient);
		}
	}

	return ret;
}

void script_client_init
(struct script_client *sclient, pool_t pool, const char *path,
	const char *const *args, const struct script_client_settings *set)
{
	sclient->pool = pool;
	sclient->path = p_strdup(pool, path);
	if ( args != NULL )
		sclient->args = p_strarray_dup(pool, args);
	sclient->set = set;
	sclient->debug = set->debug;
	sclient->fd_in = -1;
	sclient->fd_out = -1;
}

void script_client_set_input
(struct script_client *sclient, struct istream *input)
{
	if ( sclient->input )
		i_stream_unref(&sclient->input);
	if ( input != NULL )
		i_stream_ref(input);
	sclient->input = input;
}

void script_client_set_output
(struct script_client *sclient, struct ostream *output)
{
	if ( sclient->output )
		o_stream_unref(&sclient->output);
	if ( output != NULL )
		o_stream_ref(output);
	sclient->output = output;
}

void script_client_set_env
(struct script_client *sclient, const char *name, const char *value)
{
	const char *env;

	if ( !array_is_created(&sclient->envs) )
		p_array_init(&sclient->envs, sclient->pool, 16);

	env = p_strdup_printf(sclient->pool, "%s=%s", name, value);
	array_append(&sclient->envs, &env, 1);
}

void script_client_init_streams(struct script_client *sclient)
{
	if ( sclient->fd_out >= 0 ) {
		sclient->script_output =
			o_stream_create_fd(sclient->fd_out, MAX_OUTBUF_SIZE, FALSE);
	}
	if ( sclient->fd_in >= 0 ) {
		sclient->script_input =
			i_stream_create_fd(sclient->fd_in, (size_t)-1, FALSE);
		sclient->io = io_add
			(sclient->fd_in, IO_READ, script_client_script_input, sclient);
	}
}

void script_client_destroy(struct script_client **_sclient)
{
	struct script_client *sclient = *_sclient;

	script_client_disconnect(sclient, TRUE);

	if ( sclient->input != NULL )
		i_stream_unref(&sclient->input);
	if ( sclient->output != NULL )
		o_stream_unref(&sclient->output);
	if ( sclient->io != NULL )
		io_remove(&sclient->io);
	if ( sclient->ioloop != NULL )
		io_loop_destroy(&sclient->ioloop);

	pool_unref(&sclient->pool);
	*_sclient = NULL;
}

int script_client_run(struct script_client *sclient)
{
	int ret;

	sclient->ioloop = io_loop_create();

	if ( script_client_connect(sclient) >= 0 ) {
		/* run output */
		ret = 1;		
		if ( sclient->script_output != NULL &&
			(ret=o_stream_flush(sclient->script_output)) == 0 ) {
			o_stream_set_flush_callback
				(sclient->script_output, script_client_script_output, sclient);
		}

		/* run i/o event loop */
		if ( ret < 0 ) {
			sclient->error = SCRIPT_CLIENT_ERROR_IO;
		} else if ( sclient->io != NULL || ret == 0 ) {
			io_loop_run(sclient->ioloop);
		}

		/* finished */
		script_client_disconnect(sclient, FALSE);
	}

	io_loop_destroy(&sclient->ioloop);

	if ( sclient->error != SCRIPT_CLIENT_ERROR_NONE )
		return -1;

	return sclient->exit_code;
}