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 int
XauLockAuth(_Xconst char * file_name,int retries,int timeout,long dead)45 XauLockAuth (
46 _Xconst char *file_name,
47 int	retries,
48 int	timeout,
49 long	dead)
50 {
51     char	creat_name[1025], link_name[1025];
52     struct stat	statb;
53     Time_t	now;
54     int		creat_fd = -1;
55 
56     if (strlen (file_name) > 1022)
57 	return LOCK_ERROR;
58     snprintf (creat_name, sizeof(creat_name), "%s-c", file_name);
59     snprintf (link_name, sizeof(link_name), "%s-l", file_name);
60     if (stat (creat_name, &statb) != -1) {
61 	now = time ((Time_t *) 0);
62 	/*
63 	 * NFS may cause ctime to be before now, special
64 	 * case a 0 deadtime to force lock removal
65 	 */
66 	if (dead == 0 || now - statb.st_ctime > dead) {
67 	    (void) remove (creat_name);
68 	    (void) remove (link_name);
69 	}
70     }
71 
72     while (retries > 0) {
73 	if (creat_fd == -1) {
74 	    creat_fd = open (creat_name, O_WRONLY | O_CREAT | O_EXCL, 0600);
75 	    if (creat_fd == -1) {
76 		if (errno != EACCES && errno != EEXIST)
77 		    return LOCK_ERROR;
78 	    } else
79 		(void) close (creat_fd);
80 	}
81 	if (creat_fd != -1) {
82 #ifdef HAVE_PATHCONF
83 	    /* The file system may not support hard links, and pathconf should tell us that. */
84 	    if (1 == pathconf(creat_name, _PC_LINK_MAX)) {
85 		if (-1 == rename(creat_name, link_name)) {
86 		    /* Is this good enough?  Perhaps we should retry.  TEST */
87 		    return LOCK_ERROR;
88 		} else {
89 		    return LOCK_SUCCESS;
90 		}
91 	    } else
92 #endif
93 	    {
94 	    	if (link (creat_name, link_name) != -1)
95 		    return LOCK_SUCCESS;
96 		if (errno == ENOENT) {
97 		    creat_fd = -1;	/* force re-creat next time around */
98 		    continue;
99 	    	}
100 	    	if (errno != EEXIST)
101 		    return LOCK_ERROR;
102 	   }
103 	}
104 	(void) sleep ((unsigned) timeout);
105 	--retries;
106     }
107     return LOCK_TIMEOUT;
108 }
109