home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / SUN / SLIP / CSN_SLIP.TAR / slip-tip / uucplock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-22  |  2.6 KB  |  109 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 the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)uucplock.c    5.3 (Berkeley) 9/2/88";
  20. #endif /* not lint */
  21.  
  22. #include <sys/types.h>
  23. #include <sys/file.h>
  24. #include <sys/dir.h>
  25. #include <errno.h>
  26.  
  27. #include "tip.h"
  28.  
  29. /* pick the directory naming scheme you are using */
  30.  
  31. #ifdef SUNOS411
  32. #define LOCKDIRNAME    "/usr/spool/locks/LCK..%s"    /**/
  33. #else
  34. #define LOCKDIRNAME    "/usr/spool/uucp/LCK..%s"    /**/
  35. #endif
  36.  
  37. /* #define LOCKDIRNAME    "/usr/spool/uucp/LCK/LCK..%s"    /**/
  38.  
  39. /* 
  40.  * uucp style locking routines
  41.  * return: 0 - success
  42.  *       -1 - failure
  43.  */
  44.  
  45. uu_lock(ttyname)
  46.     char *ttyname;
  47. {
  48.     extern int errno;
  49.     int fd, pid;
  50.     char tbuf[sizeof(LOCKDIRNAME) + MAXNAMLEN];
  51.     off_t lseek();
  52.  
  53.     (void)sprintf(tbuf, LOCKDIRNAME, ttyname);
  54.     fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
  55.     if (fd < 0) {
  56.         /*
  57.          * file is already locked
  58.          * check to see if the process holding the lock still exists
  59.          */
  60.         fd = open(tbuf, O_RDWR, 0);
  61.         if (fd < 0) {
  62.             perror("lock open");
  63.             return(-1);
  64.         }
  65.         if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) {
  66.             (void)close(fd);
  67.             perror("lock read");
  68.             return(-1);
  69.         }
  70.  
  71.         if (kill(pid, 0) == 0 || errno != ESRCH) {
  72.             (void)close(fd);    /* process is still running */
  73.             return(-1);
  74.         }
  75.         /*
  76.          * The process that locked the file isn't running, so
  77.          * we'll lock it ourselves
  78.          */
  79.         if (lseek(fd, 0L, L_SET) < 0) {
  80.             (void)close(fd);
  81.             perror("lock lseek");
  82.             return(-1);
  83.         }
  84.         /* fall out and finish the locking process */
  85.  
  86.     }
  87.     pid = getpid();
  88.     if (write(fd, (char *)&pid, sizeof(pid)) != sizeof(pid)) {
  89.         (void)close(fd);
  90.         (void)unlink(tbuf);
  91.         perror("lock write");
  92.         return(-1);
  93.     }
  94.     if (boolean(value(VERBOSE))) {
  95.         printf("uu_lock: pid is: %d\n",pid);
  96.     }
  97.     (void)close(fd);
  98.     return(0);
  99. }
  100.  
  101. uu_unlock(ttyname)
  102.     char *ttyname;
  103. {
  104.     char tbuf[sizeof(LOCKDIRNAME) + MAXNAMLEN];
  105.  
  106.     (void)sprintf(tbuf, LOCKDIRNAME, ttyname);
  107.     return(unlink(tbuf));
  108. }
  109.