File: locks.c

package info (click to toggle)
openct 0.6.20-1.2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,672 kB
  • sloc: ansic: 21,876; sh: 10,560; makefile: 272
file content (131 lines) | stat: -rw-r--r-- 2,754 bytes parent folder | download | duplicates (4)
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
/*
 * Locking functions - these are somewhat simplified
 * by the fact that we have one manager process per reader,
 * so we don't have to worry about different readers here,
 * just different slots.
 *
 * Copyright (C) 2003 Olaf Kirch <okir@suse.de>
 *
 * FIXME - prevent denial of service from clients allocating
 * huge numbers of locks. There should be a maximum of one shared
 * and one exclusive lock per client.
 */

#include "internal.h"
#include <stdlib.h>
#include "ifdhandler.h"

typedef struct ct_lock {
	struct ct_lock *next;
	unsigned int slot;
	uid_t uid;
	ct_lock_handle handle;
	ct_socket_t *owner;
	int exclusive;
} ct_lock_t;

static ct_lock_t *locks;
static unsigned int lock_handle = 0;

/*
 * Try to establish a lock
 */
int ifdhandler_lock(ct_socket_t * sock, int slot, int type,
		    ct_lock_handle * res)
{
	ct_lock_t *l;
	int rc;

	/* See if we have a locking conflict */
	if ((rc = ifdhandler_check_lock(sock, slot, type)) < 0)
		return rc;

	/* No conflict - grant lock and record this fact */
	l = (ct_lock_t *) calloc(1, sizeof(*l));
	if (!l) {
		ct_error("out of memory");
		return IFD_ERROR_NO_MEMORY;
	}
	l->exclusive = (type == IFD_LOCK_EXCLUSIVE);
	l->uid = sock->client_uid;
	l->handle = lock_handle++;
	l->owner = sock;
	l->slot = slot;

	l->next = locks;
	locks = l;

	ifd_debug(1, "granted %s lock %u for slot %u by uid=%u",
		  l->exclusive ? "excl" : "shared", l->handle, l->slot, l->uid);

	*res = l->handle;
	return 0;
}

/*
 * Check if a slot is locked by someone else
 */
int ifdhandler_check_lock(ct_socket_t * sock, int slot, int type)
{
	ct_lock_t *l;

	for (l = locks; l; l = l->next) {
		if (l->slot != slot)
			continue;

		if (l->owner == sock)
			continue;

		if (l->exclusive
		    || type == IFD_LOCK_EXCLUSIVE || l->uid != sock->client_uid)
			return IFD_ERROR_LOCKED;
	}

	return 0;
}

/*
 * Release a lock
 */
int ifdhandler_unlock(ct_socket_t * sock, int slot, ct_lock_handle handle)
{
	ct_lock_t *l, **lp;

	for (lp = &locks; (l = *lp) != NULL; lp = &l->next) {
		if (l->owner == sock && l->slot == slot && l->handle == handle) {
			ifd_debug(1,
				  "released %s lock %u for slot %u by uid=%u",
				  l->exclusive ? "excl" : "shared",
				  l->handle, l->slot, l->uid);

			*lp = l->next;
			free(l);
			return 0;
		}
	}

	return IFD_ERROR_NOLOCK;
}

/*
 * Release all locks held by a client
 * (called when the client socket is closed)
 */
void ifdhandler_unlock_all(ct_socket_t * sock)
{
	ct_lock_t *l, **lp;

	lp = &locks;
	while ((l = *lp) != NULL) {
		if (l->owner == sock) {
			ifd_debug(1,
				  "released %s lock %u for slot %u by uid=%u",
				  l->exclusive ? "excl" : "shared",
				  l->handle, l->slot, l->uid);
			*lp = l->next;
			free(l);
		} else {
			lp = &l->next;
		}
	}
}