File: lockfile.c

package info (click to toggle)
liblockfile 0.1-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 80 kB
  • ctags: 29
  • sloc: ansic: 306; makefile: 90; sh: 50
file content (202 lines) | stat: -rw-r--r-- 4,041 bytes parent folder | download | duplicates (2)
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
/*
 * lockfile.c	Safely creates a lockfile, also over NFS.
 *		This file also holds the implementation for
 *		the Svr4 maillock functions.
 *
 * Version:	@(#)lockfile.c  0.1.1  19-Feb-1998  miquels@cistron.nl
 *
 *		Copyright (C) Miquel van Smoorenburg 1997,1998.
 *
 *		This library is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU Library General Public
 *		License as published by the Free Software Foundation; either
 *		version 2 of the License, or (at your option) any later version.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <utime.h>
#include <paths.h>
#include <errno.h>
#include <lockfile.h>
#include <maillock.h>

static char mlockfile[MAXPATHLEN];
static int  islocked = 0;

/*
 *	Create a lockfile.
 */
int lockfile_create(const char *lockfile, int retries)
{
	struct stat st, st1;
	time_t	now;
	int sleeptime = 5;
	int statfailed = 0;
	int fd;
	int i, e;
	char tmplock[MAXPATHLEN];
	char sysname[256];
	char buf[4];
	char *p;

	/*
	 *	Safety measure.
	 */
	if (strlen(lockfile) + 32 > MAXPATHLEN) {
		errno = ENAMETOOLONG;
		return L_ERROR;
	}

	/*
	 *	Create a temp lockfile (hopefully unique) and
	 *	write 0\0 into the lockfile for svr4 compatibility.
	 */
	if (gethostname(sysname, sizeof(sysname)) < 0)
		return L_ERROR;
	if ((p = strchr(sysname, '.')) != NULL)
		*p = 0;
	strcpy(tmplock, lockfile);
	if ((p = strrchr(tmplock, '/')) == NULL)
		p = tmplock;
	else
		p++;
	sprintf(p, ".lk%05d%x%s",
		getpid(), (int)time(NULL) & 15, sysname);
	i = umask(022);
	fd = open(tmplock, O_WRONLY|O_CREAT|O_EXCL, 0644);
	e = errno;
	umask(i);
	if (fd < 0) {
		errno = e;
		return L_TMPLOCK;
	}
	i = write(fd, "0", 2);
	e = errno;
	if (close(fd) != 0) {
		e = errno;
		i = -1;
	}
	if (i != 2) {
		unlink(tmplock);
		errno = i < 0 ? e : EAGAIN;
		return L_TMPWRITE;
	}

	/*
	 *	Now try to link the temporary lock to the lock.
	 */
	for (i = 0; i < retries && retries > 0; i++) {

		sleeptime = i > 12 ? 60 : 5 * i;
		if (sleeptime > 0)
			sleep(sleeptime);

		/*
		 *	KLUDGE: some people say the return code of
		 *	link() over NFS can't be trusted.
		 *	EXTRA FIX: the value of the nlink field
		 *	can't be trusted (may be cached).
		 */
		(void)link(tmplock, lockfile);

		if (lstat(tmplock, &st1) < 0)
			return L_ERROR; /* Can't happen */

		if (lstat(lockfile, &st) < 0) {
			if (statfailed++ > 5) {
				/*
				 *	Normally, this can't happen; either
				 *	another process holds the lockfile or
				 *	we do. So if this error pops up
				 *	repeatedly, just exit...
				 */
				e = errno;
				(void)unlink(tmplock);
				errno = e;
				return L_MAXTRYS;
			}
			continue;
		}

		/*
		 *	See if we got the lock.
		 */
		if (st.st_rdev == st1.st_rdev &&
		    st.st_ino  == st1.st_ino) {
			(void)unlink(tmplock);
			return L_SUCCESS;
		}
		statfailed = 0;

		/*
		 *	Locks are invalid after 5 minutes.
		 *	Use the time of the file system.
		 */
		time(&now);
		if ((fd  = open(lockfile, O_RDONLY)) >= 0) {
			if (read(fd, buf, 1) >= 0 && fstat(fd, &st1) == 0)
				now = st1.st_atime;
			close(fd);
		}
		if (now < st.st_ctime + 300)
			continue;
		(void)unlink(lockfile);
	}
	(void)unlink(tmplock);
	errno = EAGAIN;
	return L_MAXTRYS;
}

/*
 *	Remove a lock.
 */
int lockfile_remove(const char *lockfile)
{
	return unlink(lockfile);
}

/*
 *	Touch a lock.
 */
int lockfile_touch(const char *lockfile)
{
	return utime(lockfile, NULL);
}

/*
 *	Lock a mailfile. This looks a lot like the SVR4 lockmbox() thingy.
 *	Arguments: lusername, retries.
 */
int maillock(const char *name, int retries)
{
	int i;

	if (islocked) return 0;

	snprintf(mlockfile, MAXPATHLEN, "%s/%s.lock", _PATH_MAILDIR, name);
	i = lockfile_create(mlockfile, retries);
	if (i == 0) islocked = 1;

	return i;
}

void mailunlock(void)
{
	if (!islocked) return;
	lockfile_remove(mlockfile);
	islocked = 0;
}

void touchlock(void)
{
	lockfile_touch(mlockfile);
}