Blame AuLock.c

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