File: lockfile.c

package info (click to toggle)
liblockfile 1.01
  • links: PTS
  • area: main
  • in suites: potato
  • size: 168 kB
  • ctags: 43
  • sloc: ansic: 638; makefile: 144; sh: 50
file content (433 lines) | stat: -rw-r--r-- 8,636 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * lockfile.c 	Safely creates a lockfile, also over NFS.
 *		This file also holds the implementation for
 *		the Svr4 maillock functions.
 *
 * Version:	@(#)lockfile.c  1.0  05-Jun-1999  miquels@cistron.nl
 *
 *		Copyright (C) Miquel van Smoorenburg 1997,1998,1999.
 *
 *		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 "autoconf.h"

#include <sys/types.h>
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <lockfile.h>
#include <maillock.h>

#ifdef HAVE_UTIME
#include <utime.h>
#endif

#ifdef LIB
static char mlockfile[MAXPATHLEN];
static int  islocked = 0;
#endif

#ifndef LIB
extern int check_sleep(int);
#endif

#if !defined(LIB) || defined(MAILGROUP)
/*
 *	See if we can write to the directory.
 *	Returns: -1 fail
 *		  0 OK writable
 */
#ifdef LIB
static
#endif
int eaccess(char *fn, gid_t gid, struct stat *st)
{
	struct stat	tmp;
	uid_t		uid = geteuid();

	if (st == NULL) st = &tmp;

	if (stat(fn, st) != 0)
		return -1;
	errno = EPERM;
	if (uid == 0) return 0;
	if (st->st_uid == uid)
		return (st->st_mode & 0200) ? 0 : -1;
	if (st->st_gid == gid)
		return (st->st_mode & 0020) ? 0 : -1;
	return (st->st_mode & 0002) ? 0 : -1;
}
#endif

#if defined(LIB) && defined(MAILGROUP)
/*
 *	Can we write to the directory of the lockfile ?
 */
static int need_extern(const char *file)
{
	gid_t		egid = getegid();
	struct stat	st;
	static gid_t	mailgid = -1;
	char		dir[MAXPATHLEN];
	char		*p;

	/*
	 *	Find directory.
	 */
	strcpy(dir, file);
	if ((p = strrchr(dir, '/')) != NULL)
		*p = 0;
	else
		strcpy(dir, ".");

	if (eaccess(dir, egid, NULL) >= 0)
		return 0;

	/*
	 *	See if accessible for group mail. We find out what
	 *	"group mail" is by statting LOCKPROG, that saves us
	 *	from having to call getgrgid() in a library.
	 */
	if (mailgid == (gid_t)-1) {
		if (stat(LOCKPROG, &st) < 0 || !(st.st_mode & S_ISGID))
			return 0;
		mailgid = st.st_gid;
	}
	return (eaccess(dir, mailgid, NULL) >= 0);
}

/*
 *	Call external program to do the actual locking.
 */
static int do_extern(char *opt, const char *lockfile, int retries, int flags)
{
	sigset_t	set, oldset;
	char		buf[4];
	pid_t		pid, n;
	int		st;

	/*
	 *	Block SIGCHLD. The main program might have installed
	 *	handlers we don't want to call.
	 */
	sigemptyset(&set);
	sigaddset(&set, SIGCHLD);
	sigprocmask(SIG_BLOCK, &set, &oldset);

	/*
	 *	Fork, execute locking program and wait.
	 */
	if ((pid = fork()) < 0)
		return L_ERROR;
	if (pid == 0) {
		sprintf(buf, "%d", retries % 1000);
		execl(LOCKPROG, LOCKPROG, opt, "-r", buf, "-q",
			(flags & L_PID) ? "-p" : "-N", lockfile, NULL);
		_exit(L_ERROR);
	}

	/*
	 *	Wait for return status - do something appropriate
	 *	if program died or returned L_ERROR.
	 */
	while ((n = waitpid(pid, &st, 0)) != pid)
		if (n < 0 && errno != EINTR)
			break;
	if (!sigismember(&oldset, SIGCHLD))
		sigprocmask(SIG_UNBLOCK, &set, NULL);
	if (n < 0)
		return L_ERROR;
	if (!WIFEXITED(st) || WEXITSTATUS(st) == L_ERROR) {
		errno = EINTR;
		return L_ERROR;
	}

	return WEXITSTATUS(st);
}

#endif

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

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

#if defined(LIB) && defined(MAILGROUP)
	if (need_extern(lockfile))
		return do_extern("-l", lockfile, retries, flags);
#endif

	/*
	 *	Create a temp lockfile (hopefully unique) and write
	 *	either our pid/ppid in it, or 0\0 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",
		(int)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;
	}
	if (flags & (L_PID | L_PPID)) {
		sprintf(buf, "%d\n",
			(flags & L_PID) ? (int)getpid() : (int)getppid());
		p = buf;
		len = strlen(buf);
	} else {
		p = "0";
		len = 2;
	}
	i = write(fd, p, len);
	e = errno;
	if (close(fd) != 0) {
		e = errno;
		i = -1;
	}
	if (i != len) {
		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)
#ifdef LIB
			sleep(sleeptime);
#else
			if ((e = check_sleep(sleeptime)) != 0) {
				unlink(tmplock);
				return e;
			}
#endif
		/*
		 *	Now lock by linking the tempfile to the lock.
		 *
		 *	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;

		/*
		 *	If there is a lockfile and it is invalid,
		 *	remove the lockfile.
		 */
		if (lockfile_check(lockfile, flags) == -1)
			unlink(lockfile);

	}
	(void)unlink(tmplock);
	errno = EAGAIN;
	return L_MAXTRYS;
}

/*
 *	See if a valid lockfile is present.
 *	Returns 0 if so, -1 if not.
 */
int lockfile_check(const char *lockfile, int flags)
{
	struct stat	st;
	char		buf[16];
	time_t		now;
	pid_t		pid;
	int		fd, len, r;

	if (stat(lockfile, &st) < 0)
		return -1;

	/*
	 *	Get the contents and mtime of the lockfile.
	 *	Use the time of the file system.
	 */
	time(&now);
	pid = 0;
	if ((fd = open(lockfile, O_RDONLY)) >= 0) {
		if ((len = read(fd, buf, sizeof(buf))) >= 0 &&
		    fstat(fd, &st) == 0)
			now = st.st_atime;
		close(fd);
		if (len > 0 && (flags & (L_PID|L_PPID))) {
		buf[len] = 0;
			pid = atoi(buf);
		}
	}

	if (pid > 0) {
		/*
		 *	If we have a pid, see if the process
		 *	owning the lockfile is still alive.
		 */
		r = kill(pid, 0);
		if (r == 0 || errno == EPERM)
			return 0;
		if (r < 0 && errno == ESRCH)
			return -1;
		/* EINVAL - FALLTHRU */
	}

	/*
	 *	Without a pid in the lockfile, the lock
	 *	is valid if it is newer than 5 mins.
	 */
	if (now < st.st_mtime + 300)
		return 0;
	return -1;
}

/*
 *	Remove a lock.
 */
int lockfile_remove(const char *lockfile)
{
#if defined(LIB) && defined(MAILGROUP)
	if (need_extern(lockfile))
		return do_extern("-u", lockfile, 0, 0);
#endif
	return (unlink(lockfile) < 0 && errno != ENOENT) ? -1 : 0;
}

/*
 *	Touch a lock.
 */
int lockfile_touch(const char *lockfile)
{
#ifdef HAVE_UTIME
	return utime(lockfile, NULL);
#else
	return utimes(lockfile, NULL);
#endif
}

#ifdef LIB
/*
 *	Lock a mailfile. This looks a lot like the SVR4 function.
 *	Arguments: lusername, retries.
 */
int maillock(const char *name, int retries)
{
	char		*p, *mail;
	int		i;

	if (islocked) return 0;
	if (strlen(name) + sizeof(MAILDIR) + 6 > MAXPATHLEN) {
		errno = ENAMETOOLONG;
		return L_NAMELEN;
	}

	/*
	 *	If $MAIL is for the same username as "name"
	 *	then use $MAIL instead.
	 */
	sprintf(mlockfile, "%s%s.lock", MAILDIR, name);
	if ((mail = getenv("MAIL")) != NULL) {
		if ((p = strrchr(mail, '/')) != NULL)
			p++;
		else
			p = mail;
		if (strcmp(p, name) == 0) {
			if (strlen(mail) + 6 > MAXPATHLEN) {
				errno = ENAMETOOLONG;
				return L_NAMELEN;
			}
			sprintf(mlockfile, "%s.lock", mail);
		}
	}
	i = lockfile_create(mlockfile, retries, 0);
	if (i == 0) islocked = 1;

	return i;
}

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

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