home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / slip / cslip-2.6 / tip / uucplock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-30  |  2.7 KB  |  113 lines

  1. /*
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #ifndef lint
  21. static char sccsid[] = "@(#)uucplock.c    5.3 (Berkeley) 9/2/88";
  22. #endif /* not lint */
  23.  
  24. #include <sys/types.h>
  25. #include <sys/file.h>
  26. #include <sys/dir.h>
  27. #include <errno.h>
  28.  
  29. /* pick the directory naming scheme you are using */
  30.  
  31. #define LOCKDIRNAME    "/tmp/LCK..%s"    /**/
  32.  
  33. extern int slip;
  34.  
  35. /* 
  36.  * uucp style locking routines
  37.  * return: 0 - success
  38.  *       -1 - failure
  39.  */
  40.  
  41. uu_lock(ttyname)
  42.     char *ttyname;
  43. {
  44. #ifndef NOLOCK
  45.     extern int errno;
  46.     int fd, pid;
  47.     char tbuf[sizeof(LOCKDIRNAME) + MAXNAMLEN];
  48.     off_t lseek();
  49.     register int mode;
  50.  
  51.     (void)sprintf(tbuf, LOCKDIRNAME, ttyname);
  52.     mode = O_RDWR|O_CREAT;
  53.     if (slip)
  54.         mode = O_RDWR|O_CREAT;
  55.     else
  56.         mode = O_RDWR|O_CREAT|O_EXCL;
  57.     fd = open(tbuf, mode, 0660);
  58.     if (fd < 0) {
  59.         /*
  60.          * file is already locked
  61.          * check to see if the process holding the lock still exists
  62.          */
  63.         fd = open(tbuf, O_RDWR, 0);
  64.         if (fd < 0) {
  65.             perror("lock open");
  66.             return(-1);
  67.         }
  68.         if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) {
  69.             (void)close(fd);
  70.             perror("lock read");
  71.             return(-1);
  72.         }
  73.  
  74.         if (kill(pid, 0) == 0 || errno != ESRCH) {
  75.             (void)close(fd);    /* process is still running */
  76.             return(-1);
  77.         }
  78.         /*
  79.          * The process that locked the file isn't running, so
  80.          * we'll lock it ourselves
  81.          */
  82.         if (lseek(fd, 0L, L_SET) < 0) {
  83.             (void)close(fd);
  84.             perror("lock lseek");
  85.             return(-1);
  86.         }
  87.         /* fall out and finish the locking process */
  88.     }
  89.     pid = getpid();
  90.     if (write(fd, (char *)&pid, sizeof(pid)) != sizeof(pid)) {
  91.         (void)close(fd);
  92.         (void)unlink(tbuf);
  93.         perror("lock write");
  94.         return(-1);
  95.     }
  96.     (void)close(fd);
  97. #endif
  98.     return(0);
  99. }
  100.  
  101. uu_unlock(ttyname)
  102.     char *ttyname;
  103. {
  104. #ifndef NOLOCK
  105.     char tbuf[sizeof(LOCKDIRNAME) + MAXNAMLEN];
  106.  
  107.     (void)sprintf(tbuf, LOCKDIRNAME, ttyname);
  108.     return(unlink(tbuf));
  109. #else
  110.     return (0);
  111. #endif
  112. }
  113.