home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xau / AuLock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  2.4 KB  |  94 lines

  1. /*
  2.  * Xau - X Authorization Database Library
  3.  *
  4.  * $XConsortium: AuLock.c,v 1.8 91/12/16 19:56:07 gildea Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Keith Packard, MIT X Consortium
  19.  */
  20.  
  21. #include <X11/Xauth.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25.  
  26. extern int errno;
  27.  
  28. #if NeedFunctionPrototypes
  29. int
  30. XauLockAuth (
  31. _Xconst char *file_name,
  32. int    retries,
  33. int    timeout,
  34. long    dead)
  35. #else
  36. int
  37. XauLockAuth (file_name, retries, timeout, dead)
  38. char    *file_name;
  39. int    retries;
  40. int    timeout;
  41. long    dead;
  42. #endif
  43. {
  44.     char    creat_name[1025], link_name[1025];
  45.     char    *strcpy (), *strcat ();
  46.     long    time ();
  47.     unsigned    sleep ();
  48.     struct stat    statb;
  49.     long    now;
  50.     int        creat_fd = -1;
  51.  
  52.     if (strlen (file_name) > 1022)
  53.     return LOCK_ERROR;
  54.     (void) strcpy (creat_name, file_name);
  55.     (void) strcat (creat_name, "-c");
  56.     (void) strcpy (link_name, file_name);
  57.     (void) strcat (link_name, "-l");
  58.     if (stat (creat_name, &statb) != -1) {
  59.     now = time ((long *) 0);
  60.     /*
  61.      * NFS may cause ctime to be before now, special
  62.      * case a 0 deadtime to force lock removal
  63.      */
  64.     if (dead == 0 || now - statb.st_ctime > dead) {
  65.         (void) unlink (creat_name);
  66.         (void) unlink (link_name);
  67.     }
  68.     }
  69.     
  70.     while (retries > 0) {
  71.     if (creat_fd == -1) {
  72.         creat_fd = creat (creat_name, 0666);
  73.         if (creat_fd == -1) {
  74.         if (errno != EACCES)
  75.             return LOCK_ERROR;
  76.         } else
  77.         (void) close (creat_fd);
  78.     }
  79.     if (creat_fd != -1) {
  80.         if (link (creat_name, link_name) != -1)
  81.         return LOCK_SUCCESS;
  82.         if (errno == ENOENT) {
  83.         creat_fd = -1;    /* force re-creat next time around */
  84.         continue;
  85.         }
  86.         if (errno != EEXIST)
  87.         return LOCK_ERROR;
  88.     }
  89.     (void) sleep ((unsigned) timeout);
  90.     --retries;
  91.     }
  92.     return LOCK_TIMEOUT;
  93. }
  94.