9 date 93.08.25.17.33.09; author jromine; state Exp;
14 date 92.11.12.23.26.49; author jromine; state Exp;
19 date 92.11.12.19.32.01; author jromine; state Exp;
24 date 92.11.12.19.29.42; author jromine; state Exp;
29 date 92.11.12.01.04.29; author jromine; state Exp;
34 date 92.11.11.20.55.04; author jromine; state Exp;
39 date 92.11.06.03.23.24; author jromine; state Exp;
44 date 92.11.02.18.58.35; author jromine; state Exp;
49 date 92.10.27.16.42.22; author jromine; state Exp;
54 date 92.10.16.16.52.18; author jromine; state Exp;
59 date 92.02.07.19.25.18; author jromine; state Exp;
64 date 92.02.07.17.47.51; author jromine; state Exp;
69 date 92.02.07.17.25.22; author jromine; state Exp;
74 date 92.02.05.19.10.05; author jromine; state Exp;
79 date 92.01.31.22.35.13; author jromine; state Exp;
84 date 91.01.18.08.43.29; author mh; state Exp;
89 date 90.04.05.15.04.28; author sources; state Exp;
94 date 90.02.06.13.41.44; author sources; state Exp;
99 date 90.02.01.14.36.01; author sources; state Exp;
104 date 89.11.17.15.58.35; author sources; state Exp;
109 date 89.11.17.15.44.58; author sources; state Exp;
120 @off_t fixes for BSD44
123 @/* lock.c - universal locking routines */
125 static char ident[] = "@@(#)$Id: lock.c,v 2.18 1992/11/12 23:26:49 jromine Exp jromine $";
127 /* compile-time priority:
128 * LOCKF use if defined
129 * FCNTL use if SYS5 defined and LOCKF not defined
130 * FLOCK use if BSD42 defined and LOCKF and SYS5 not defined
140 #include "../h/strings.h"
145 #include "mmdfonly.h"
147 #else /* not MMDFONLY */
148 #include "lockonly.h"
149 #endif /* not MMDFONLY */
150 #endif /* LOCKONLY */
151 #include <sys/types.h>
152 #include <sys/stat.h>
158 #include <sys/errno.h>
159 #include <sys/file.h>
164 #include <sys/fcntl.h>
168 #if defined(_AIX) || defined(AUX)
169 #include <sys/file.h>
173 #define u_short ushort
178 #if defined(SYS5) && !defined(_AIX)
180 #define rindex strrchr
183 #define FLOCK /* LOCKF will override this, if defined */
190 char *lockldir = "/usr/spool/locks";
191 #endif /* not MMDFONLY */
192 #endif /* LOCKONLY */
194 static int b_lkopen(), lockit(), f_lkopen();
195 static lockname(), timerON(), timerOFF();
201 int lkopen (file, access)
208 #if defined (FLOCK) || defined(LOCKF) || defined(FCNTL)
209 return f_lkopen (file, access);
213 return b_lkopen (file, access);
219 static int b_lkopen (file, access)
226 char curlock[BUFSIZ],
230 if (stat (file, &st) == NOTOK)
232 lockname (curlock, tmplock, file, (int) st.st_dev, (int) st.st_ino);
235 switch (lockit (tmplock, curlock)) {
237 if ((i = open (file, access)) == NOTOK) {
239 (void) unlink (curlock);
242 timerON (curlock, i);
246 if (stat (curlock, &st) == NOTOK) {
254 (void) time (&curtime);
255 if (curtime < st.st_ctime + 60L)
258 (void) unlink (curlock);
264 static int lockit (tmp, file)
270 if ((fd = creat (tmp, 0400)) == NOTOK)
272 #if defined(hpux) || defined(ncr)
273 write(fd, "MH lock\n",8);
277 fd = link (tmp, file);
280 return (fd != NOTOK ? OK : NOTOK);
285 static lockname (curlock, tmplock, file, dev, ino)
286 register char *curlock,
296 if ((cp = rindex (file, '/')) == NULL || *++cp == 0)
298 if (lockldir == NULL || *lockldir == 0) {
300 (void) sprintf (bp, "%.*s", cp - file, file);
305 (void) sprintf (bp, "%s/", lockldir);
312 (void) sprintf (bp, "%s.lock", cp);
316 (void) sprintf (bp, "LCK%05d.%05d", dev, ino);
321 if ((cp = rindex (curlock, '/')) == NULL || *++cp == 0)
322 (void) strcpy (tmplock, ",LCK.XXXXXX");
324 (void) sprintf (tmplock, "%.*s,LCK.XXXXXX",
325 cp - curlock, curlock);
326 (void) unlink (mktemp (tmplock));
332 #if defined(FLOCK) || defined(LOCKF) || defined(FCNTL)
334 #if defined(BSD42) || defined(SVR4)
335 #include <sys/file.h>
336 #if defined(SUN40) || defined(SVR4)
337 #include <sys/fcntl.h>
345 static int f_lkopen (file, access)
356 for (i = 0; i < 5; i++) {
357 #if defined(LOCKF) || defined(FCNTL)
359 access &= ~O_APPEND; /* make sure we open at the beginning */
360 if ((access & 03) == O_RDONLY) {
361 /* We MUST have write permission or lockf/fcntl() won't work */
366 #endif /* LOCKF || FCNTL */
367 if ((fd = open (file, access | O_NDELAY)) == NOTOK)
372 /* should be an error? */
374 buf.l_type = F_WRLCK;
378 if (fcntl (fd, F_SETLK, &buf) != NOTOK)
382 if (flock (fd, LOCK_EX | LOCK_NB) != NOTOK)
386 if (lockf (fd, F_TLOCK, 0L) != NOTOK) {
387 /* see if we should be at the end */
390 lseek (fd, (off_t)0, SEEK_END);
392 lseek (fd, (off_t)0, L_XTND);
396 /* Fix errno - lockf screws it */
410 #endif /* FLOCK || LOCKF || FCNTL */
416 int lkclose (fd, file)
420 char curlock[BUFSIZ];
433 /* should be an error? */
435 buf.l_type = F_UNLCK;
439 fcntl(fd, F_SETLK, &buf);
447 lseek (fd, (off_t)0, L_SET); /* make sure we unlock the whole thing */
448 lockf (fd, F_ULOCK, 0L);
453 if (fstat (fd, &st) != NOTOK) {
454 lockname (curlock, NULLCP, file, (int) st.st_dev, (int) st.st_ino);
455 (void) unlink (curlock);
466 FILE *lkfopen (file, mode)
473 if ((fd = lkopen (file, strcmp (mode, "r") ? 2 : 0)) == NOTOK)
476 if ((fp = fdopen (fd, mode)) == NULL) {
487 int lkfclose (fp, file)
491 char curlock[BUFSIZ];
505 /* should be an error? */
507 buf.l_type = F_UNLCK;
511 fcntl(fileno(fp), F_SETLK, &buf);
515 flock (fileno(fp), LOCK_UN);
519 fseek (fp, 0L, 0); /* make sure we unlock the whole thing */
520 lockf (fileno(fp), F_ULOCK, 0L);
525 if (fstat (fileno (fp), &st) != NOTOK) {
526 lockname (curlock, NULLCP, file, (int) st.st_dev, (int) st.st_ino);
527 (void) unlink (curlock);
531 return (fclose (fp));
538 #define NSECS ((unsigned) 20)
546 #define NULLP ((struct lock *) 0)
548 static struct lock *l_top = NULLP;
553 static TYPESIG alrmser (sig)
558 register struct lock *lp;
561 (void) signal (SIGALRM, alrmser);
564 for (lp = l_top; lp; lp = lp -> l_next)
565 if (*(cp = lp -> l_lock) && (j = creat (cp, 0400)) != NOTOK)
568 (void) alarm (NSECS);
573 static timerON (lock, fd)
577 register struct lock *lp;
579 if ((lp = (struct lock *) malloc ((unsigned) (sizeof *lp))) == NULLP)
583 if ((lp -> l_lock = malloc ((unsigned) (strlen (lock) + 1))) == NULLCP) {
587 (void) strcpy (lp -> l_lock, lock);
588 lp -> l_next = NULLP;
591 lp -> l_next = l_top -> l_next;
593 (void) signal (SIGALRM, alrmser);/* perhaps SIGT{STP,TIN,TOU} */
594 (void) alarm (NSECS);
603 register struct lock *pp,
609 for (pp = lp = l_top; lp; pp = lp, lp = lp -> l_next)
610 if (lp -> l_fd == fd)
614 l_top = lp -> l_next;
616 pp -> l_next = lp -> l_next;
624 (void) alarm (NSECS);
631 @write to lock file on NCR
636 static char ident[] = "@@(#)$Id: lock.c,v 2.17 1992/11/12 19:32:01 jromine Exp jromine $";
639 lseek (fd, 0L, SEEK_END);
642 lseek (fd, 0L, L_XTND);
645 lseek (fd, 0L, L_SET); /* make sure we unlock the whole thing */
651 @try to fixup u_short without U_SHORT option
656 static char ident[] = "@@(#)$Id: lock.c,v 2.16 1992/11/12 19:29:42 jromine Exp jromine $";
670 static char ident[] = "@@(#)$Id: lock.c,v 2.15 1992/11/12 01:04:29 jromine Exp jromine $";
672 #if defined(SYS5) && !defined(U_SHORT)
673 #define u_short ushort
688 static char ident[] = "@@(#)$Id: lock.c,v 2.14 1992/11/11 20:55:04 jromine Exp jromine $";
691 #if defined(SYS5) && !defined(_AIX) && !defined(SVR4)
697 @don't define u_short under aix
702 static char ident[] = "@@(#)$Id: lock.c,v 2.13 1992/11/06 03:23:24 jromine Exp jromine $";
705 #if defined(SYS5) && !defined(_AIX)
711 @fixup u_short define for AUX
716 static char ident[] = "@@(#)$Id: lock.c,v 2.12 1992/11/02 18:58:35 jromine Exp jromine $";
730 static char ident[] = "@@(#)$Id: lock.c,v 2.11 1992/10/27 16:42:22 jromine Exp jromine $";
745 static char ident[] = "@@(#)$Id: lock.c,v 2.10 1992/10/16 16:52:18 jromine Exp jromine $";
754 @TYPESIG missing on alrmser
759 static char ident[] = "@@(#)$Id: lock.c,v 2.9 1992/02/07 19:25:18 jromine Exp jromine $";
768 #define NULLCP ((char *) 0)
779 static char ident[] = "@@(#)$Id: lock.c,v 2.8 1992/02/07 17:47:51 jromine Exp jromine $";
788 @changes for SYS5 merged.
793 static char ident[] = "@@(#)$Id: lock.c,v 2.7 1992/02/07 17:25:22 jromine Exp jromine $";
797 #define FCNTL /* assume all SYS5's have fcntl() locking */
811 if (j & O_APPEND) lseek (fd, 0L, L_XTND);
822 static char ident[] = "@@(#)$Id: lock.c,v 2.6 1992/02/05 19:10:05 jromine Exp jromine $";
826 #endif /* MMDFONLY */
839 #if defined(BSD42) || defined(FCNTL)
842 #endif /* BSD42 || FCNTL */
845 #if defined(BSD42) || defined(FCNTL)
870 if (errno == EACCES) errno = EWOULDBLOCK;
874 #endif /* BSD42 || FCNTL */
876 #if defined(BSD42) || defined(FCNTL)
892 #if defined(BSD42) || defined(FCNTL)
917 static char ident[] = "@@(#)$Id: lock.c,v 2.5 1992/01/31 22:35:13 jromine Exp jromine $";
968 /* We MUST have write permission of lockf won't work */
1024 static char ident[] = "@@(#)$Id: lock.c,v 2.4 91/01/18 08:43:29 mh Exp Locker: mh $";
1027 access &= ! O_APPEND; /* make sure we open at the beginning */
1030 access &= ! O_RDONLY;
1036 @add sys/file.h under LOCKF
1041 static char ident[] = "@@(#)$Id: lock.c,v 2.3 90/04/05 15:04:28 sources Exp Locker: mh $";
1048 if ((cp = rindex (file, '/')) == NULL || *++cp == NULL)
1051 if (lockldir == NULL || *lockldir == NULL) {
1054 if ((cp = rindex (curlock, '/')) == NULL || *++cp == NULL)
1065 static char ident[] = "@@(#)$Id:$";
1090 @changes for SUN40 shared libraries and NNTP under bbc