summaryrefslogtreecommitdiff
path: root/camel/camel-lock-client.c
blob: 012bec3324af9516c98f590140fc59b1db7136c3 (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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <glib/gi18n-lib.h>

#include "camel-lock-client.h"
#include "camel-lock-helper.h"
#include "camel-object.h"

#define d(x)

#define CHECK_CALL(x) G_STMT_START { \
	if ((x) == -1) { \
		g_debug ("%s: Call of '" #x "' failed: %s", G_STRFUNC, g_strerror (errno)); \
	} \
	} G_STMT_END

static GMutex lock_lock;
#define LOCK() g_mutex_lock(&lock_lock)
#define UNLOCK() g_mutex_unlock(&lock_lock)

static gint lock_sequence;
static gint lock_helper_pid = -1;
static gint lock_stdin_pipe[2], lock_stdout_pipe[2];

static gint read_n (gint fd, gpointer buffer, gint inlen)
{
	gchar *p = buffer;
	gint len, left = inlen;

	do {
		len = read (fd, p, left);
		if (len == -1) {
			if (errno != EINTR)
				return -1;
		} else {
			left -= len;
			p += len;
		}
	} while (left > 0 && len != 0);

	return inlen - left;
}

static gint write_n (gint fd, gpointer buffer, gint inlen)
{
	gchar *p = buffer;
	gint len, left = inlen;

	do {
		len = write (fd, p, left);
		if (len == -1) {
			if (errno != EINTR)
				return -1;
		} else {
			left -= len;
			p += len;
		}
	} while (left > 0);

	return inlen;
}

static gint
lock_helper_init (GError **error)
{
	gint i, dupfd1, dupfd2;

	lock_stdin_pipe[0] = -1;
	lock_stdin_pipe[1] = -1;
	lock_stdout_pipe[0] = -1;
	lock_stdout_pipe[1] = -1;
	if (pipe (lock_stdin_pipe) == -1
	    || pipe (lock_stdout_pipe) == -1) {
		g_set_error (
			error, G_IO_ERROR,
			g_io_error_from_errno (errno),
			_("Cannot build locking helper pipe: %s"),
			g_strerror (errno));
		if (lock_stdin_pipe[0] != -1)
			close (lock_stdin_pipe[0]);
		if (lock_stdin_pipe[1] != -1)
			close (lock_stdin_pipe[1]);
		if (lock_stdout_pipe[0] != -1)
			close (lock_stdout_pipe[0]);
		if (lock_stdout_pipe[1] != -1)
			close (lock_stdout_pipe[1]);

		return -1;
	}

	lock_helper_pid = fork ();
	switch (lock_helper_pid) {
	case -1:
		close (lock_stdin_pipe[0]);
		close (lock_stdin_pipe[1]);
		close (lock_stdout_pipe[0]);
		close (lock_stdout_pipe[1]);
		g_set_error (
			error, G_IO_ERROR,
			g_io_error_from_errno (errno),
			_("Cannot fork locking helper: %s"),
			g_strerror (errno));
		return -1;
	case 0:
		close (STDIN_FILENO);
		dupfd1 = dup (lock_stdin_pipe[0]);
		close (STDOUT_FILENO);
		dupfd2 = dup (lock_stdout_pipe[1]);
		close (lock_stdin_pipe[0]);
		close (lock_stdin_pipe[1]);
		close (lock_stdout_pipe[0]);
		close (lock_stdout_pipe[1]);
		for (i = 3; i < 255; i++)
			     close (i);
		execl (CAMEL_LIBEXECDIR "/camel-lock-helper-" API_VERSION, "camel-lock-helper", NULL);

		if (dupfd1 != -1)
			close (dupfd1);
		if (dupfd2 != -1)
			close (dupfd2);

		/* it'll pick this up when it tries to use us */
		exit (255);
	default:
		close (lock_stdin_pipe[0]);
		close (lock_stdout_pipe[1]);

		/* so the child knows when we vanish */
		CHECK_CALL (fcntl (lock_stdin_pipe[1], F_SETFD, FD_CLOEXEC));
		CHECK_CALL (fcntl (lock_stdout_pipe[0], F_SETFD, FD_CLOEXEC));
	}

	return 0;
}

gint
camel_lock_helper_lock (const gchar *path,
                        GError **error)
{
	struct _CamelLockHelperMsg *msg;
	gint len = strlen (path);
	gint res = -1;
	gint retry = 3;

	LOCK ();

	if (lock_helper_pid == -1) {
		if (lock_helper_init (error) == -1) {
			UNLOCK ();
			return -1;
		}
	}

	msg = alloca (len + sizeof (*msg));
again:
	msg->magic = CAMEL_LOCK_HELPER_MAGIC;
	msg->seq = lock_sequence;
	msg->id = CAMEL_LOCK_HELPER_LOCK;
	msg->data = len;
	memcpy (msg + 1, path, len);

	write_n (lock_stdin_pipe[1], msg, len + sizeof (*msg));

	do {
		/* should also have a timeout here?  cancellation? */
		len = read_n (lock_stdout_pipe[0], msg, sizeof (*msg));
		if (len == 0) {
			/* child quit, do we try ressurect it? */
			res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
			/* if the child exited, this should get it, waidpid returns 0 if the child hasn't */
			if (waitpid (lock_helper_pid, NULL, WNOHANG) > 0) {
				lock_helper_pid = -1;
				close (lock_stdout_pipe[0]);
				close (lock_stdin_pipe[1]);
				lock_stdout_pipe[0] = -1;
				lock_stdin_pipe[1] = -1;
			}
			goto fail;
		}

		if (msg->magic != CAMEL_LOCK_HELPER_RETURN_MAGIC
		    || msg->seq > lock_sequence) {
			res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
			d (printf ("lock child protocol error\n"));
			g_set_error (
				error, CAMEL_ERROR,
				CAMEL_ERROR_GENERIC,
				_("Could not lock '%s': protocol "
				"error with lock-helper"), path);
			goto fail;
		}
	} while (msg->seq < lock_sequence);

	if (msg->seq == lock_sequence) {
		switch (msg->id) {
		case CAMEL_LOCK_HELPER_STATUS_OK:
			d (printf ("lock child locked ok, id is %d\n", msg->data));
			res = msg->data;
			break;
		default:
			g_set_error (
				error, CAMEL_ERROR,
				CAMEL_ERROR_GENERIC,
				_("Could not lock '%s'"), path);
			d (printf ("locking failed ! status = %d\n", msg->id));
			break;
		}
	} else if (retry > 0) {
		d (printf ("sequence failure, lost message? retry?\n"));
		retry--;
		goto again;
	} else {
		g_set_error (
			error, CAMEL_ERROR,
			CAMEL_ERROR_GENERIC,
			_("Could not lock '%s': protocol "
			"error with lock-helper"), path);
	}

fail:
	lock_sequence++;

	UNLOCK ();

	return res;
}

gint camel_lock_helper_unlock (gint lockid)
{
	struct _CamelLockHelperMsg *msg;
	gint res = -1;
	gint retry = 3;
	gint len;

	d (printf ("unlocking lock id %d\n", lockid));

	LOCK ();

	/* impossible to unlock if we haven't locked yet */
	if (lock_helper_pid == -1) {
		UNLOCK ();
		return -1;
	}

	msg = alloca (sizeof (*msg));
again:
	msg->magic = CAMEL_LOCK_HELPER_MAGIC;
	msg->seq = lock_sequence;
	msg->id = CAMEL_LOCK_HELPER_UNLOCK;
	msg->data = lockid;

	write_n (lock_stdin_pipe[1], msg, sizeof (*msg));

	do {
		/* should also have a timeout here?  cancellation? */
		len = read_n (lock_stdout_pipe[0], msg, sizeof (*msg));
		if (len == 0) {
			/* child quit, do we try ressurect it? */
			res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
			if (waitpid (lock_helper_pid, NULL, WNOHANG) > 0) {
				lock_helper_pid = -1;
				close (lock_stdout_pipe[0]);
				close (lock_stdin_pipe[1]);
				lock_stdout_pipe[0] = -1;
				lock_stdin_pipe[1] = -1;
			}
			goto fail;
		}

		if (msg->magic != CAMEL_LOCK_HELPER_RETURN_MAGIC
		    || msg->seq > lock_sequence) {
			goto fail;
		}
	} while (msg->seq < lock_sequence);

	if (msg->seq == lock_sequence) {
		switch (msg->id) {
		case CAMEL_LOCK_HELPER_STATUS_OK:
			d (printf ("lock child unlocked ok\n"));
			res = 0;
			break;
		default:
			d (printf ("locking failed !\n"));
			break;
		}
	} else if (retry > 0) {
		d (printf ("sequence failure, lost message? retry?\n"));
		lock_sequence++;
		retry--;
		goto again;
	}

fail:
	lock_sequence++;

	UNLOCK ();

	return res;
}

#if 0
gint main (gint argc, gchar **argv)
{
	gint id1, id2;

	d (printf ("locking started\n"));
	lock_helper_init ();

	id1 = camel_lock_helper_lock ("1 path 1");
	if (id1 != -1) {
		d (printf ("lock ok, unlock\n"));
		camel_lock_helper_unlock (id1);
	}

	id1 = camel_lock_helper_lock ("2 path 1");
	id2 = camel_lock_helper_lock ("2 path 2");
	camel_lock_helper_unlock (id2);
	camel_lock_helper_unlock (id1);

	id1 = camel_lock_helper_lock ("3 path 1");
	id2 = camel_lock_helper_lock ("3 path 2");
	camel_lock_helper_unlock (id1);
}
#endif