Home | History | Annotate | Line # | Download | only in dist
      1 /*
      2 
      3 Copyright 1988, 1998  The Open Group
      4 
      5 Permission to use, copy, modify, distribute, and sell this software and its
      6 documentation for any purpose is hereby granted without fee, provided that
      7 the above copyright notice appear in all copies and that both that
      8 copyright notice and this permission notice appear in supporting
      9 documentation.
     10 
     11 The above copyright notice and this permission notice shall be included in
     12 all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     20 
     21 Except as contained in this notice, the name of The Open Group shall not be
     22 used in advertising or otherwise to promote the sale, use or other dealings
     23 in this Software without prior written authorization from The Open Group.
     24 
     25 */
     26 
     27 #ifdef HAVE_CONFIG_H
     28 #include <config.h>
     29 #endif
     30 #include <X11/Xauth.h>
     31 #include <X11/Xos.h>
     32 #include <sys/stat.h>
     33 #include <errno.h>
     34 #include <time.h>
     35 #define Time_t time_t
     36 #ifdef HAVE_UNISTD_H
     37 # include <unistd.h>
     38 #endif
     39 #ifdef WIN32
     40 # include <X11/Xwindows.h>
     41 # define link rename
     42 #endif
     43 
     44 #ifndef O_CLOEXEC
     45 #define O_CLOEXEC	0
     46 #endif
     47 
     48 int
     49 XauLockAuth (
     50 _Xconst char *file_name,
     51 int	retries,
     52 int	timeout,
     53 long	dead)
     54 {
     55     char	creat_name[1025], link_name[1025];
     56     struct stat	statb;
     57     Time_t	now;
     58     int		creat_fd = -1;
     59 
     60     if (strlen (file_name) > 1022)
     61 	return LOCK_ERROR;
     62     snprintf (creat_name, sizeof(creat_name), "%s-c", file_name);
     63     snprintf (link_name, sizeof(link_name), "%s-l", file_name);
     64     if (stat (creat_name, &statb) != -1) {
     65 	now = time ((Time_t *) 0);
     66 	/*
     67 	 * NFS may cause ctime to be before now, special
     68 	 * case a 0 deadtime to force lock removal
     69 	 */
     70 	if (dead == 0 || now - statb.st_ctime > dead) {
     71 	    (void) remove (creat_name);
     72 	    (void) remove (link_name);
     73 	}
     74     }
     75 
     76     while (retries > 0) {
     77 	if (creat_fd == -1) {
     78 	    creat_fd = open (creat_name, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC,
     79 			     0600);
     80 	    if (creat_fd == -1) {
     81 		if (errno != EACCES && errno != EEXIST)
     82 		    return LOCK_ERROR;
     83 	    } else
     84 		(void) close (creat_fd);
     85 	}
     86 	if (creat_fd != -1) {
     87 #ifdef HAVE_PATHCONF
     88 	    /* The file system may not support hard links, and pathconf should tell us that. */
     89 	    if (1 == pathconf(creat_name, _PC_LINK_MAX)) {
     90 		if (-1 == rename(creat_name, link_name)) {
     91 		    /* Is this good enough?  Perhaps we should retry.  TEST */
     92 		    return LOCK_ERROR;
     93 		} else {
     94 		    return LOCK_SUCCESS;
     95 		}
     96 	    } else
     97 #endif
     98 	    {
     99 	    	if (link (creat_name, link_name) != -1)
    100 		    return LOCK_SUCCESS;
    101 		if (errno == ENOENT) {
    102 		    creat_fd = -1;	/* force re-creat next time around */
    103 		    continue;
    104 	    	}
    105 	    	if (errno != EEXIST)
    106 		    return LOCK_ERROR;
    107 	   }
    108 	}
    109 	(void) sleep ((unsigned) timeout);
    110 	--retries;
    111     }
    112     return LOCK_TIMEOUT;
    113 }
    114