Blame AuLock.c

Packit Service f89583
/*
Packit Service f89583
Packit Service f89583
Copyright 1988, 1998  The Open Group
Packit Service f89583
Packit Service f89583
Permission to use, copy, modify, distribute, and sell this software and its
Packit Service f89583
documentation for any purpose is hereby granted without fee, provided that
Packit Service f89583
the above copyright notice appear in all copies and that both that
Packit Service f89583
copyright notice and this permission notice appear in supporting
Packit Service f89583
documentation.
Packit Service f89583
Packit Service f89583
The above copyright notice and this permission notice shall be included in
Packit Service f89583
all copies or substantial portions of the Software.
Packit Service f89583
Packit Service f89583
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service f89583
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service f89583
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit Service f89583
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit Service f89583
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service f89583
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Service f89583
Packit Service f89583
Except as contained in this notice, the name of The Open Group shall not be
Packit Service f89583
used in advertising or otherwise to promote the sale, use or other dealings
Packit Service f89583
in this Software without prior written authorization from The Open Group.
Packit Service f89583
Packit Service f89583
*/
Packit Service f89583
Packit Service f89583
#ifdef HAVE_CONFIG_H
Packit Service f89583
#include <config.h>
Packit Service f89583
#endif
Packit Service f89583
#include <X11/Xauth.h>
Packit Service f89583
#include <X11/Xos.h>
Packit Service f89583
#include <sys/stat.h>
Packit Service f89583
#include <errno.h>
Packit Service f89583
#include <time.h>
Packit Service f89583
#define Time_t time_t
Packit Service f89583
#ifdef HAVE_UNISTD_H
Packit Service f89583
# include <unistd.h>
Packit Service f89583
#endif
Packit Service f89583
#ifdef WIN32
Packit Service f89583
# include <X11/Xwindows.h>
Packit Service f89583
# define link rename
Packit Service f89583
#endif
Packit Service f89583
Packit Service f89583
int
Packit Service f89583
XauLockAuth (
Packit Service f89583
_Xconst char *file_name,
Packit Service f89583
int	retries,
Packit Service f89583
int	timeout,
Packit Service f89583
long	dead)
Packit Service f89583
{
Packit Service f89583
    char	creat_name[1025], link_name[1025];
Packit Service f89583
    struct stat	statb;
Packit Service f89583
    Time_t	now;
Packit Service f89583
    int		creat_fd = -1;
Packit Service f89583
Packit Service f89583
    if (strlen (file_name) > 1022)
Packit Service f89583
	return LOCK_ERROR;
Packit Service f89583
    snprintf (creat_name, sizeof(creat_name), "%s-c", file_name);
Packit Service f89583
    snprintf (link_name, sizeof(link_name), "%s-l", file_name);
Packit Service f89583
    if (stat (creat_name, &statb) != -1) {
Packit Service f89583
	now = time ((Time_t *) 0);
Packit Service f89583
	/*
Packit Service f89583
	 * NFS may cause ctime to be before now, special
Packit Service f89583
	 * case a 0 deadtime to force lock removal
Packit Service f89583
	 */
Packit Service f89583
	if (dead == 0 || now - statb.st_ctime > dead) {
Packit Service f89583
	    (void) remove (creat_name);
Packit Service f89583
	    (void) remove (link_name);
Packit Service f89583
	}
Packit Service f89583
    }
Packit Service f89583
Packit Service f89583
    while (retries > 0) {
Packit Service f89583
	if (creat_fd == -1) {
Packit Service f89583
	    creat_fd = open (creat_name, O_WRONLY | O_CREAT | O_EXCL, 0600);
Packit Service f89583
	    if (creat_fd == -1) {
Packit Service f89583
		if (errno != EACCES && errno != EEXIST)
Packit Service f89583
		    return LOCK_ERROR;
Packit Service f89583
	    } else
Packit Service f89583
		(void) close (creat_fd);
Packit Service f89583
	}
Packit Service f89583
	if (creat_fd != -1) {
Packit Service f89583
#ifdef HAVE_PATHCONF
Packit Service f89583
	    /* The file system may not support hard links, and pathconf should tell us that. */
Packit Service f89583
	    if (1 == pathconf(creat_name, _PC_LINK_MAX)) {
Packit Service f89583
		if (-1 == rename(creat_name, link_name)) {
Packit Service f89583
		    /* Is this good enough?  Perhaps we should retry.  TEST */
Packit Service f89583
		    return LOCK_ERROR;
Packit Service f89583
		} else {
Packit Service f89583
		    return LOCK_SUCCESS;
Packit Service f89583
		}
Packit Service f89583
	    } else
Packit Service f89583
#endif
Packit Service f89583
	    {
Packit Service f89583
	    	if (link (creat_name, link_name) != -1)
Packit Service f89583
		    return LOCK_SUCCESS;
Packit Service f89583
		if (errno == ENOENT) {
Packit Service f89583
		    creat_fd = -1;	/* force re-creat next time around */
Packit Service f89583
		    continue;
Packit Service f89583
	    	}
Packit Service f89583
	    	if (errno != EEXIST)
Packit Service f89583
		    return LOCK_ERROR;
Packit Service f89583
	   }
Packit Service f89583
	}
Packit Service f89583
	(void) sleep ((unsigned) timeout);
Packit Service f89583
	--retries;
Packit Service f89583
    }
Packit Service f89583
    return LOCK_TIMEOUT;
Packit Service f89583
}