1/*
2
3Copyright 1988, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in 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
48int
49XauLockAuth (
50_Xconst char *file_name,
51int	retries,
52int	timeout,
53long	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