|
|
|
|
File: [XFree86 CVS] / xc / programs / Xserver / hw / xfree86 / common / Attic / xf86Lock.c
(download)
Revision: 3.11, Sat Jan 18 06:55:34 1997 UTC (16 years, 4 months ago) by dawes Branch: MAIN CVS Tags: xf-3_9z, xf-3_9y, xf-3_9x, xf-3_9w, xf-3_9v, xf-3_9u, xf-3_9t, xf-3_9s, xf-3_9r, xf-3_9q, xf-3_9p, xf-3_9o, xf-3_9n, xf-3_9m, xf-3_9l, xf-3_9k, xf-3_9jk, xf-3_9jj, xf-3_9ji, xf-3_9jh, xf-3_9jg, xf-3_9jf, xf-3_9je, xf-3_9jd, xf-3_9jc, xf-3_9jb, xf-3_9ja, xf-3_9j, xf-3_9i, xf-3_9h, xf-3_9g, xf-3_9f, xf-3_9e, xf-3_9d, xf-3_9c, xf-3_9b, xf-3_9a, xf-3_9Ak, xf-3_9Aj, xf-3_9Ai, xf-3_9Ah, xf-3_9Ag, xf-3_9Af, xf-3_9Ae, xf-3_9Ad, xf-3_9Ac, xf-3_9Ab, xf-3_9Aa, xf-3_3g, xf-3_3f, xf-3_3e, xf-3_3d, xf-3_3c, xf-3_3b, xf-3_3a, xf-3_3_4b, xf-3_3_4a, xf-3_3_4, xf-3_3_3b, xf-3_3_3a, xf-3_3_3_1f, xf-3_3_3_1e, xf-3_3_3_1d, xf-3_3_3_1c, xf-3_3_3_1b, xf-3_3_3_1a, xf-3_3_3_1Z, xf-3_3_3_1, xf-3_3_3Z, xf-3_3_3, xf-3_3_2j, xf-3_3_2i, xf-3_3_2h, xf-3_3_2g, xf-3_3_2f, xf-3_3_2e, xf-3_3_2d, xf-3_3_2c, xf-3_3_2b, xf-3_3_2a, xf-3_3_2_4, xf-3_3_2_3, xf-3_3_2_2, xf-3_3_2_1, xf-3_3_2Zc, xf-3_3_2Zb, xf-3_3_2Za, xf-3_3_2Z, xf-3_3_2, xf-3_3_1z, xf-3_3_1g, xf-3_3_1f, xf-3_3_1e, xf-3_3_1d, xf-3_3_1c, xf-3_3_1b, xf-3_3_1a, xf-3_3_1, xf-3_3, xf-3_2y, xf-3_2x, xf-3_2w, xf-3_2v, xf-3_2u, xf-3_2t, xf-3_2s, xf-3_2Zc, xf-3_2Zb, xf-3_2Za, xf-3_2Z, xf-3_2Xn, xf-3_2Xm, xf-3_2Xl, xf-3_2Xk, xf-3_2Xj, xf-3_2Xi, xf-3_2Xh, xf-3_2Xg, xf-3_2Xf, xf-3_2Xe, xf-3_2Xd, xf-3_2Xc, xf-3_2Xb, xf-3_2Xa, xf-3_2At, xf-3_2As, xf-3_2Ar, xf-3_2Aq, xf-3_2Ap, xf-3_2Ao, xf-3_2An, xf-3_2Am, xf-3_2Al, xf-3_2Ak, xf-3_2Aj, xf-3_2Ai, xf-3_2Ah, xf-3_2Ag, xf-3_2Af, xf-3_2Ae, xf-3_2Ad, xf-3_2Ac, xf-3_2Ab, xf-3_2Aa, xf-3_2A, xf-39ja, xf-3331_shark, dhd-1 Branch point for: xf-3_9j-branch, xf-3_3-branch, xf-3_2A-branch Changes since 3.10: +5 -1 lines updates |
/* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86Lock.c,v 3.10 1997/01/14 22:17:34 dawes Exp $ */
/*
* Explicit support for a server lock file like the ones used for UUCP.
* For architectures with virtual terminals that can run more than one
* server at a time. This keeps the servers from stomping on each other
* if the user forgets to give them different display numbers.
*/
/* $XConsortium: xf86Lock.c /main/9 1996/04/01 18:07:58 kaleb $ */
#define LOCK_PATH "/tmp/.X"
#define LOCK_TMPPATH "/tmp/.tX"
#define LOCK_SUFFIX "-lock"
#include "Xos.h"
#include <sys/stat.h>
#include <stdio.h>
#include "misc.h"
#ifdef _MINIX
#include <limits.h> /* For PATH_MAX */
#endif
#ifdef __EMX__
#define link rename
#endif
#ifndef PATH_MAX
#ifndef Lynx
#include <sys/param.h>
#else
#include <param.h>
#endif
#ifndef PATH_MAX
#ifdef MAXPATHLEN
#define PATH_MAX MAXPATHLEN
#else
#define PATH_MAX 1024
#endif
#endif
#endif
#include <errno.h>
extern int errno;
#ifdef USE_XF86_SERVERLOCK
static Bool StillLocking = FALSE;
extern char* display;
/*
* LockServer --
* Check if the server lock file exists. If so, check if the PID
* contained inside is valid. If so, then die. Otherwise, create
* the lock file containing the PID.
*/
void
xf86LockServer()
{
#ifndef AMOEBA
char tmp[PATH_MAX], lock[PATH_MAX], pid_str[12];
int lfd, i, haslock, l_pid, t;
/*
* Path names
*/
#ifndef __EMX__
(void) sprintf(tmp, "%s%s%s", LOCK_TMPPATH, display, LOCK_SUFFIX);
(void) sprintf(lock, "%s%s%s", LOCK_PATH, display, LOCK_SUFFIX);
#else
/* OS/2 uses TMP directory, must also prepare for 8.3 names */
char *tmppath = getenv("TMP");
if (!tmppath)
FatalError("No TMP dir found\n", tmp);
(void) sprintf(tmp, "%s/xf86$%s.lck",tmppath, display);
(void) sprintf(lock, "%s/xf86_%s.lck",tmppath, display);
#endif
/*
* Create a temporary file containing our PID. Attempt three times
* to create the file.
*/
StillLocking = TRUE;
i = 0;
do {
i++;
lfd = open(tmp, O_CREAT | O_EXCL | O_WRONLY, 0644);
if (lfd < 0)
sleep(2);
else
break;
} while (i < 3);
if (lfd < 0) {
unlink(tmp);
i = 0;
do {
i++;
lfd = open(tmp, O_CREAT | O_EXCL | O_WRONLY, 0644);
if (lfd < 0)
sleep(2);
else
break;
} while (i < 3);
}
if (lfd < 0)
FatalError("Could not create lock file in %s\n", tmp);
(void) sprintf(pid_str, "%10d\n", getpid());
(void) write(lfd, pid_str, 11);
#ifndef __EMX__
#ifndef USE_CHMOD
(void) fchmod(lfd, 0444);
#else
/* ISC prior Version 4.1 doesn't have fchmod :-( */
(void) chmod(tmp, 0444);
#endif
#endif
(void) close(lfd);
/*
* OK. Now the tmp file exists. Try three times to move it in place
* for the lock.
*/
i = 0;
haslock = 0;
while ((!haslock) && (i++ < 3)) {
haslock = (link(tmp,lock) == 0);
if (haslock) {
/*
* We're done.
*/
break;
}
else {
/*
* Read the pid from the existing file
*/
lfd = open(lock, O_RDONLY);
if (lfd < 0) {
unlink(tmp);
FatalError("Can't read lock file %s\n", lock);
}
pid_str[0] = '\0';
if (read(lfd, pid_str, 11) != 11) {
/*
* Bogus lock file.
*/
unlink(lock);
close(lfd);
continue;
}
pid_str[11] = '\0';
sscanf(pid_str, "%d", &l_pid);
close(lfd);
/*
* Now try to kill the PID to see if it exists.
*/
errno = 0;
t = kill(l_pid, 0);
if ((t< 0) && (errno == ESRCH)) {
/*
* Stale lock file.
*/
unlink(lock);
continue;
}
else if (((t < 0) && (errno == EPERM)) || (t == 0)) {
/*
* Process is still active.
*/
unlink(tmp);
FatalError("Server is already active for display %s\n%s %s\n%s\n",
display, "\tIf this server is no longer running, remove",
lock, "\tand start again.");
}
}
}
unlink(tmp);
if (!haslock)
FatalError("Could not create server lock file: %s\n", lock);
StillLocking = FALSE;
#endif /* !AMOEBA */
}
/*
* Unlock_Server --
* Remove the server lock file.
*/
void
xf86UnlockServer()
{
#ifndef AMOEBA
char buf[PATH_MAX];
if (StillLocking)
return;
(void)sprintf(buf, "%s%s%s", LOCK_PATH, display, LOCK_SUFFIX);
(void) unlink(buf);
#endif
}
#endif /* USE_XF86_SERVERLOCK */
|
Powered by ViewCVS 0.9.2 |