xserver: Branch 'master' - 4 commits

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jul 26 22:32:45 UTC 2024


 include/meson.build |    1 
 include/os.h        |    5 -
 os/meson.build      |    1 
 os/osinit.c         |    1 
 os/serverlock.c     |  253 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 os/serverlock.h     |   13 ++
 os/utils.c          |  191 ---------------------------------------
 7 files changed, 273 insertions(+), 192 deletions(-)

New commits:
commit 96c99b83183c889e499edb8535c7b9e9025fe11a
Author: Enrico Weigelt, metux IT consult <info at metux.net>
Date:   Tue Feb 27 17:14:47 2024 +0100

    os: move -nolock help message printing to serverlock.h
    
    Reduce #ifdef cluttering in os/utils.c a little bit my moving the (built-time-)
    conditional printing of the help message into serverlock.c
    
    Signed-off-by: Enrico Weigelt, metux IT consult <info at metux.net>
    Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1320>

diff --git a/os/serverlock.c b/os/serverlock.c
index f5c03e32f..36efc9bc0 100644
--- a/os/serverlock.c
+++ b/os/serverlock.c
@@ -239,10 +239,15 @@ void DisableServerLock(void) {
     nolock = TRUE;
 }
 
+void LockServerUseMsg(void) {
+    ErrorF("-nolock                disable the locking mechanism\n");
+}
+
 #else /* LOCK_SERVER */
 
 void LockServer(void) {}
 void UnlockServer(void) {}
 void DisableServerLock(void) {}
+void LockServerUseMsg(void) {}
 
 #endif /* LOCK_SERVER */
diff --git a/os/serverlock.h b/os/serverlock.h
index ec74c2bb2..dae1cf2a4 100644
--- a/os/serverlock.h
+++ b/os/serverlock.h
@@ -8,5 +8,6 @@
 void LockServer(void);
 void UnlockServer(void);
 void DisableServerLock(void);
+void LockServerUseMsg(void);
 
 #endif /* _XSERVER_SERVERLOCK_H */
diff --git a/os/utils.c b/os/utils.c
index ded9a463e..ebd3ece60 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -371,9 +371,7 @@ UseMsg(void)
 #ifdef RLIMIT_STACK
     ErrorF("-ls int                limit stack space to N Kb\n");
 #endif
-#ifdef LOCK_SERVER
-    ErrorF("-nolock                disable the locking mechanism\n");
-#endif
+    LockServerUseMsg();
     ErrorF("-maxclients n          set maximum number of clients (power of two)\n");
     ErrorF("-nolisten string       don't listen on protocol\n");
     ErrorF("-listen string         listen on protocol\n");
commit 749046e5e9f04b6b2fce95233904cccf83e31d0e
Author: Enrico Weigelt, metux IT consult <info at metux.net>
Date:   Tue Feb 20 19:33:26 2024 +0100

    os: move out LockServer logic from util.c to serverlock.c
    
    Signed-off-by: Enrico Weigelt, metux IT consult <info at metux.net>
    Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1320>

diff --git a/os/meson.build b/os/meson.build
index 38d3d2fae..e12e2c9e7 100644
--- a/os/meson.build
+++ b/os/meson.build
@@ -12,6 +12,7 @@ srcs_os = [
     'mitauth.c',
     'osinit.c',
     'ospoll.c',
+    'serverlock.c',
     'string.c',
     'utils.c',
     'xdmauth.c',
diff --git a/os/serverlock.c b/os/serverlock.c
new file mode 100644
index 000000000..f5c03e32f
--- /dev/null
+++ b/os/serverlock.c
@@ -0,0 +1,248 @@
+/* SPDX-License-Identifier: MIT OR X11
+ *
+ * Copyright © 2024 Enrico Weigelt, metux IT consult <info at metux.net>
+ */
+/*
+
+Copyright 1987, 1998  The Open Group
+
+Permission to use, copy, modify, distribute, and sell this software and its
+documentation for any purpose is hereby granted without fee, provided that
+the above copyright notice appear in all copies and that both that
+copyright notice and this permission notice appear in supporting
+documentation.
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+Except as contained in this notice, the name of The Open Group shall
+not be used in advertising or otherwise to promote the sale, use or
+other dealings in this Software without prior written authorization
+from The Open Group.
+
+Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
+Copyright 1994 Quarterdeck Office Systems.
+
+                        All Rights Reserved
+
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
+provided that the above copyright notice appear in all copies and that
+both that copyright notice and this permission notice appear in
+supporting documentation, and that the names of Digital and
+Quarterdeck not be used in advertising or publicity pertaining to
+distribution of the software without specific, written prior
+permission.
+
+DIGITAL AND QUARTERDECK DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
+OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
+OR PERFORMANCE OF THIS SOFTWARE.
+
+*/
+#include <dix-config.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "dix/dix_priv.h"
+#include "os/serverlock.h"
+#include "os/osdep.h"
+
+#include "os.h"
+#include "opaque.h"
+
+/*
+ * 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.
+ */
+#define LOCK_DIR "/tmp"
+#define LOCK_TMP_PREFIX "/.tX"
+#define LOCK_PREFIX "/.X"
+#define LOCK_SUFFIX "-lock"
+
+#ifdef LOCK_SERVER
+
+static Bool StillLocking = FALSE;
+static char LockFile[PATH_MAX];
+static Bool nolock = FALSE;
+
+/*
+ * 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
+LockServer(void)
+{
+    char tmp[PATH_MAX], pid_str[12];
+    int lfd, i, haslock, l_pid, t;
+    const char *tmppath = LOCK_DIR;
+    int len;
+    char port[20];
+
+    if (nolock || NoListenAll)
+        return;
+    /*
+     * Path names
+     */
+    snprintf(port, sizeof(port), "%d", atoi(display));
+    len = strlen(LOCK_PREFIX) > strlen(LOCK_TMP_PREFIX) ? strlen(LOCK_PREFIX) :
+        strlen(LOCK_TMP_PREFIX);
+    len += strlen(tmppath) + strlen(port) + strlen(LOCK_SUFFIX) + 1;
+    if (len > sizeof(LockFile))
+        FatalError("Display name `%s' is too long\n", port);
+    (void) sprintf(tmp, "%s" LOCK_TMP_PREFIX "%s" LOCK_SUFFIX, tmppath, port);
+    (void) sprintf(LockFile, "%s" LOCK_PREFIX "%s" LOCK_SUFFIX, tmppath, port);
+
+    /*
+     * 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);
+    snprintf(pid_str, sizeof(pid_str), "%10lu\n", (unsigned long) getpid());
+    if (write(lfd, pid_str, 11) != 11)
+        FatalError("Could not write pid to lock file in %s\n", tmp);
+    (void) fchmod(lfd, 0444);
+    (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, LockFile) == 0);
+        if (haslock) {
+            /*
+             * We're done.
+             */
+            break;
+        }
+        else if (errno == EEXIST) {
+            /*
+             * Read the pid from the existing file
+             */
+            lfd = open(LockFile, O_RDONLY | O_NOFOLLOW);
+            if (lfd < 0) {
+                unlink(tmp);
+                FatalError("Can't read lock file %s\n", LockFile);
+            }
+            pid_str[0] = '\0';
+            if (read(lfd, pid_str, 11) != 11) {
+                /*
+                 * Bogus lock file.
+                 */
+                unlink(LockFile);
+                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(LockFile);
+                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",
+                     port, "\tIf this server is no longer running, remove",
+                     LockFile, "\tand start again.");
+            }
+        }
+        else {
+            unlink(tmp);
+            FatalError
+                ("Linking lock file (%s) in place failed: %s\n",
+                 LockFile, strerror(errno));
+        }
+    }
+    unlink(tmp);
+    if (!haslock)
+        FatalError("Could not create server lock file: %s\n", LockFile);
+    StillLocking = FALSE;
+}
+
+/*
+ * UnlockServer --
+ *      Remove the server lock file.
+ */
+void
+UnlockServer(void)
+{
+    if (nolock || NoListenAll)
+        return;
+
+    if (!StillLocking) {
+
+        (void) unlink(LockFile);
+    }
+}
+
+void DisableServerLock(void) {
+    nolock = TRUE;
+}
+
+#else /* LOCK_SERVER */
+
+void LockServer(void) {}
+void UnlockServer(void) {}
+void DisableServerLock(void) {}
+
+#endif /* LOCK_SERVER */
diff --git a/os/serverlock.h b/os/serverlock.h
index 429c3a04b..ec74c2bb2 100644
--- a/os/serverlock.h
+++ b/os/serverlock.h
@@ -7,5 +7,6 @@
 
 void LockServer(void);
 void UnlockServer(void);
+void DisableServerLock(void);
 
 #endif /* _XSERVER_SERVERLOCK_H */
diff --git a/os/utils.c b/os/utils.c
index e45683cef..ded9a463e 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -224,182 +224,6 @@ OsSignal(int sig, OsSigHandlerPtr handler)
 #endif
 }
 
-/*
- * 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.
- */
-#define LOCK_DIR "/tmp"
-#define LOCK_TMP_PREFIX "/.tX"
-#define LOCK_PREFIX "/.X"
-#define LOCK_SUFFIX "-lock"
-
-#ifndef LOCK_SERVER
-void
-LockServer(void)
-{}
-
-void
-UnlockServer(void)
-{}
-#else /* LOCK_SERVER */
-static Bool StillLocking = FALSE;
-static char LockFile[PATH_MAX];
-static Bool nolock = FALSE;
-
-/*
- * 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
-LockServer(void)
-{
-    char tmp[PATH_MAX], pid_str[12];
-    int lfd, i, haslock, l_pid, t;
-    const char *tmppath = LOCK_DIR;
-    int len;
-    char port[20];
-
-    if (nolock || NoListenAll)
-        return;
-    /*
-     * Path names
-     */
-    snprintf(port, sizeof(port), "%d", atoi(display));
-    len = strlen(LOCK_PREFIX) > strlen(LOCK_TMP_PREFIX) ? strlen(LOCK_PREFIX) :
-        strlen(LOCK_TMP_PREFIX);
-    len += strlen(tmppath) + strlen(port) + strlen(LOCK_SUFFIX) + 1;
-    if (len > sizeof(LockFile))
-        FatalError("Display name `%s' is too long\n", port);
-    (void) sprintf(tmp, "%s" LOCK_TMP_PREFIX "%s" LOCK_SUFFIX, tmppath, port);
-    (void) sprintf(LockFile, "%s" LOCK_PREFIX "%s" LOCK_SUFFIX, tmppath, port);
-
-    /*
-     * 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);
-    snprintf(pid_str, sizeof(pid_str), "%10lu\n", (unsigned long) getpid());
-    if (write(lfd, pid_str, 11) != 11)
-        FatalError("Could not write pid to lock file in %s\n", tmp);
-    (void) fchmod(lfd, 0444);
-    (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, LockFile) == 0);
-        if (haslock) {
-            /*
-             * We're done.
-             */
-            break;
-        }
-        else if (errno == EEXIST) {
-            /*
-             * Read the pid from the existing file
-             */
-            lfd = open(LockFile, O_RDONLY | O_NOFOLLOW);
-            if (lfd < 0) {
-                unlink(tmp);
-                FatalError("Can't read lock file %s\n", LockFile);
-            }
-            pid_str[0] = '\0';
-            if (read(lfd, pid_str, 11) != 11) {
-                /*
-                 * Bogus lock file.
-                 */
-                unlink(LockFile);
-                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(LockFile);
-                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",
-                     port, "\tIf this server is no longer running, remove",
-                     LockFile, "\tand start again.");
-            }
-        }
-        else {
-            unlink(tmp);
-            FatalError
-                ("Linking lock file (%s) in place failed: %s\n",
-                 LockFile, strerror(errno));
-        }
-    }
-    unlink(tmp);
-    if (!haslock)
-        FatalError("Could not create server lock file: %s\n", LockFile);
-    StillLocking = FALSE;
-}
-
-/*
- * UnlockServer --
- *      Remove the server lock file.
- */
-void
-UnlockServer(void)
-{
-    if (nolock || NoListenAll)
-        return;
-
-    if (!StillLocking) {
-
-        (void) unlink(LockFile);
-    }
-}
-#endif /* LOCK_SERVER */
-
 /* Force connections to close on SIGHUP from init */
 
 void
@@ -763,9 +587,7 @@ ProcessCommandLine(int argc, char *argv[])
         else if (strcmp(argv[i], "-displayfd") == 0) {
             if (++i < argc) {
                 displayfd = atoi(argv[i]);
-#ifdef LOCK_SERVER
-                nolock = TRUE;
-#endif
+                DisableServerLock();
             }
             else
                 UseMsg();
@@ -854,7 +676,7 @@ ProcessCommandLine(int argc, char *argv[])
                     ("Warning: the -nolock option can only be used by root\n");
             else
 #endif
-                nolock = TRUE;
+                DisableServerLock();
         }
 #endif
 	else if ( strcmp( argv[i], "-maxclients") == 0)
commit 4773182c721c45731db4cd119441722d03edb8b9
Author: Enrico Weigelt, metux IT consult <info at metux.net>
Date:   Tue Feb 20 11:49:57 2024 +0100

    os: unexport LockServer() and UnlockServer() from public module API
    
    These functions work on server process level, and shouldn't be touched
    by drivers at all, thus shouldn't be exported to them.
    
    (couldn't find any driver/module using these symbols)
    
    Signed-off-by: Enrico Weigelt, metux IT consult <info at metux.net>
    Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1320>

diff --git a/include/os.h b/include/os.h
index 3adc1f7c9..105c5e08e 100644
--- a/include/os.h
+++ b/include/os.h
@@ -257,11 +257,6 @@ typedef int (*OsSigWrapperPtr) (int /* sig */ );
 extern _X_EXPORT OsSigWrapperPtr
 OsRegisterSigWrapper(OsSigWrapperPtr newWrap);
 
-extern _X_EXPORT void
-LockServer(void);
-extern _X_EXPORT void
-UnlockServer(void);
-
 extern _X_EXPORT Bool
 PrivsElevated(void);
 
diff --git a/os/osinit.c b/os/osinit.c
index 01bb21743..9a4a5ee7e 100644
--- a/os/osinit.c
+++ b/os/osinit.c
@@ -63,6 +63,7 @@ SOFTWARE.
 #include "dix/dix_priv.h"
 #include "os/busfault.h"
 #include "os/osdep.h"
+#include "os/serverlock.h"
 
 #include "misc.h"
 #include "os.h"
diff --git a/os/serverlock.h b/os/serverlock.h
new file mode 100644
index 000000000..429c3a04b
--- /dev/null
+++ b/os/serverlock.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: MIT OR X11
+ *
+ * Copyright © 2024 Enrico Weigelt, metux IT consult <info at metux.net>
+ */
+#ifndef _XSERVER_SERVERLOCK_H
+#define _XSERVER_SERVERLOCK_H
+
+void LockServer(void);
+void UnlockServer(void);
+
+#endif /* _XSERVER_SERVERLOCK_H */
diff --git a/os/utils.c b/os/utils.c
index c29c085a3..e45683cef 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -110,6 +110,7 @@ __stdcall unsigned long GetTickCount(void);
 #include "os/cmdline.h"
 #include "os/ddx_priv.h"
 #include "os/osdep.h"
+#include "os/serverlock.h"
 
 #include "dixstruct.h"
 #include "xkbsrv.h"
commit 69b57fd01f8427268b4ab44f2ca70388407de0bf
Author: Enrico Weigelt, metux IT consult <info at metux.net>
Date:   Tue Apr 16 16:43:28 2024 +0200

    os: move defining LOCK_SERVER into meson
    
    Signed-off-by: Enrico Weigelt, metux IT consult <info at metux.net>
    Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1320>

diff --git a/include/meson.build b/include/meson.build
index 9f84f2b95..77c761862 100644
--- a/include/meson.build
+++ b/include/meson.build
@@ -176,6 +176,7 @@ conf_data.set('HAVE_VASPRINTF', cc.has_function('vasprintf') ? '1' : false)
 conf_data.set('HAVE_VSNPRINTF', cc.has_function('vsnprintf') ? '1' : false)
 conf_data.set('HAVE_WALKCONTEXT', cc.has_function('walkcontext') ? '1' : false)
 conf_data.set('HAVE_XUCRED_CR_PID', cc.has_member('struct xucred', 'cr_pid', prefix : '#include <sys/ucred.h>') ? '1' : false)
+conf_data.set('LOCK_SERVER', host_machine.system() != 'windows' ? '1' : false)
 
 # Don't let X dependencies typedef 'pointer'
 conf_data.set('_XTYPEDEF_POINTER', '1')
diff --git a/os/utils.c b/os/utils.c
index 802b01090..c29c085a3 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -234,10 +234,6 @@ OsSignal(int sig, OsSigHandlerPtr handler)
 #define LOCK_PREFIX "/.X"
 #define LOCK_SUFFIX "-lock"
 
-#if !defined(WIN32) || defined(__CYGWIN__)
-#define LOCK_SERVER
-#endif
-
 #ifndef LOCK_SERVER
 void
 LockServer(void)


More information about the xorg-commit mailing list