forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock.c
More file actions
executable file
·62 lines (53 loc) · 1.16 KB
/
Copy pathlock.c
File metadata and controls
executable file
·62 lines (53 loc) · 1.16 KB
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
#include <lib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#if _ANSI
#include <stdlib.h>
#endif
typedef enum {
False, True
} BOOLEAN;
#define LOCKDIR "/tmp/" /* or /usr/tmp/ as the case may be */
#define MAXTRIES 3
#define NAPTIME (unsigned int)5
PRIVATE _PROTOTYPE( char *lockpath, (char *name));
_PROTOTYPE( void syserr, (char *errstring));
_PROTOTYPE( BOOLEAN lock, (char *name));
_PROTOTYPE( void unlock, (char *name));
void
syserr(errstring)
char *errstring;
{
fprintf(stderr,"couldn't %s\n", errstring);
exit(1);
}
BOOLEAN lock(name) /* acquire lock */
char *name;
{
char *path;
int fd, tries;
path = lockpath(name);
tries = 0;
while ((fd = creat(path, 0)) == -1 && errno == EACCES) {
if (++tries >= MAXTRIES) return(False);
sleep(NAPTIME);
}
if (fd == -1 || close(fd) == -1) syserr("lock");
return(True);
}
void unlock(name) /* free lock */
char *name;
{
if (unlink(lockpath(name)) == -1) syserr("unlock");
}
PRIVATE char *lockpath(name) /* generate lock file path */
char *name;
{
PRIVATE char path[20];
strcpy(path, LOCKDIR);
return(strcat(path, name));
}