10 date 90.04.05.15.34.26; author sources; state Exp;
15 date 90.04.05.14.53.44; author sources; state Exp;
20 date 90.01.29.15.22.04; author sources; state Exp;
25 date 90.01.29.15.21.54; author sources; state Exp;
39 @/* lock.c - universal locking routines */
41 static char ident[] = "@@(#)$Id:$";
50 #include "../h/strings.h"
61 #include <sys/types.h>
68 #define NULLCP ((char *) 0)
72 #define rindex strrchr
80 char *lockldir = "/usr/spool/locks";
89 int lkopen (file, access)
97 return f_lkopen (file, access);
101 return b_lkopen (file, access);
107 static int b_lkopen (file, access)
114 char curlock[BUFSIZ],
118 if (stat (file, &st) == NOTOK)
120 lockname (curlock, tmplock, file, (int) st.st_dev, (int) st.st_ino);
123 switch (lockit (tmplock, curlock)) {
125 if ((i = open (file, access)) == NOTOK) {
127 (void) unlink (curlock);
130 timerON (curlock, i);
134 if (stat (curlock, &st) == NOTOK) {
142 (void) time (&curtime);
143 if (curtime < st.st_ctime + 60L)
146 (void) unlink (curlock);
152 static int lockit (tmp, file)
158 if ((fd = creat (tmp, 0400)) == NOTOK)
162 fd = link (tmp, file);
165 return (fd != NOTOK ? OK : NOTOK);
170 static lockname (curlock, tmplock, file, dev, ino)
171 register char *curlock,
181 if ((cp = rindex (file, '/')) == NULL || *++cp == NULL)
183 if (lockldir == NULL || *lockldir == NULL) {
185 (void) sprintf (bp, "%.*s", cp - file, file);
190 (void) sprintf (bp, "%s/", lockldir);
197 (void) sprintf (bp, "%s.lock", cp);
201 (void) sprintf (bp, "LCK%05d.%05d", dev, ino);
206 if ((cp = rindex (curlock, '/')) == NULL || *++cp == NULL)
207 (void) strcpy (tmplock, ",LCK.XXXXXX");
209 (void) sprintf (tmplock, "%.*s,LCK.XXXXXX",
210 cp - curlock, curlock);
211 (void) unlink (mktemp (tmplock));
219 #include <sys/file.h>
221 #include <sys/fcntl.h>
224 static int f_lkopen (file, access)
232 for (i = 0; i < 5; i++) {
233 if ((fd = open (file, access | O_NDELAY)) == NOTOK)
235 if (flock (fd, LOCK_EX | LOCK_NB) != NOTOK)
253 int lkclose (fd, file)
257 char curlock[BUFSIZ];
269 if (fstat (fd, &st) != NOTOK) {
270 lockname (curlock, NULLCP, file, (int) st.st_dev, (int) st.st_ino);
271 (void) unlink (curlock);
282 FILE *lkfopen (file, mode)
289 if ((fd = lkopen (file, strcmp (mode, "r") ? 2 : 0)) == NOTOK)
292 if ((fp = fdopen (fd, mode)) == NULL) {
303 int lkfclose (fp, file)
307 char curlock[BUFSIZ];
320 if (fstat (fileno (fp), &st) != NOTOK) {
321 lockname (curlock, NULLCP, file, (int) st.st_dev, (int) st.st_ino);
322 (void) unlink (curlock);
326 return (fclose (fp));
333 #define NSECS ((unsigned) 20)
341 #define NULLP ((struct lock *) 0)
343 static struct lock *l_top = NULLP;
353 register struct lock *lp;
356 (void) signal (SIGALRM, alrmser);
359 for (lp = l_top; lp; lp = lp -> l_next)
360 if (*(cp = lp -> l_lock) && (j = creat (cp, 0400)) != NOTOK)
363 (void) alarm (NSECS);
368 static timerON (lock, fd)
372 register struct lock *lp;
374 if ((lp = (struct lock *) malloc ((unsigned) (sizeof *lp))) == NULLP)
378 if ((lp -> l_lock = malloc ((unsigned) (strlen (lock) + 1))) == NULLCP) {
382 (void) strcpy (lp -> l_lock, lock);
383 lp -> l_next = NULLP;
386 lp -> l_next = l_top -> l_next;
388 (void) signal (SIGALRM, alrmser);/* perhaps SIGT{STP,TIN,TOU} */
389 (void) alarm (NSECS);
398 register struct lock *pp,
404 for (pp = lp = l_top; lp; pp = lp, lp = lp -> l_next)
405 if (lp -> l_fd == fd)
409 l_top = lp -> l_next;
411 pp -> l_next = lp -> l_next;
419 (void) alarm (NSECS);
431 static char ident[] = "@@(#)$Id:";
437 @fix for null pointer