. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / other / lock.c
blob94b6010bc1c6d016a2dd90b9abf472faae078814
1 #include <lib.h>
2 #include <errno.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #if _ANSI
9 #include <stdlib.h>
10 #endif
12 typedef enum {
13 False, True
14 } BOOLEAN;
16 #define LOCKDIR "/tmp/" /* or /usr/tmp/ as the case may be */
17 #define MAXTRIES 3
18 #define NAPTIME (unsigned int)5
20 PRIVATE _PROTOTYPE( char *lockpath, (char *name));
21 _PROTOTYPE( void syserr, (char *errstring));
22 _PROTOTYPE( BOOLEAN lock, (char *name));
23 _PROTOTYPE( void unlock, (char *name));
25 void
26 syserr(errstring)
27 char *errstring;
29 fprintf(stderr,"couldn't %s\n", errstring);
30 exit(1);
33 BOOLEAN lock(name) /* acquire lock */
34 char *name;
36 char *path;
37 int fd, tries;
39 path = lockpath(name);
40 tries = 0;
41 while ((fd = creat(path, 0)) == -1 && errno == EACCES) {
42 if (++tries >= MAXTRIES) return(False);
43 sleep(NAPTIME);
45 if (fd == -1 || close(fd) == -1) syserr("lock");
46 return(True);
49 void unlock(name) /* free lock */
50 char *name;
52 if (unlink(lockpath(name)) == -1) syserr("unlock");
55 PRIVATE char *lockpath(name) /* generate lock file path */
56 char *name;
58 PRIVATE char path[20];
60 strcpy(path, LOCKDIR);
61 return(strcat(path, name));