xf86LogInit: log to XDG_DATA_HOME when not running as root
authorHans de Goede <hdegoede@redhat.com>
Tue, 1 Apr 2014 09:24:17 +0000 (11:24 +0200)
committerPeter Hutterer <peter.hutterer@who-t.net>
Thu, 3 Apr 2014 04:17:35 +0000 (14:17 +1000)
When no logfile was specified (xf86LogFileFrom == X_DEFAULT) and we're not
running as root log to $XDG_DATA_HOME/xorg/Xorg.#.log as Xorg won't be able to
log to the default /var/log/... when it is not running as root.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
configure.ac
hw/xfree86/common/xf86Helper.c
hw/xfree86/man/Xorg.man
hw/xfree86/man/xorg.conf.man
include/xorg-config.h.in

index bc643e8..cdd3258 100644 (file)
@@ -2043,6 +2043,8 @@ if test "x$XORG" = xyes; then
        AC_SUBST(XF86CONFIGDIR)
        CONFIGFILE="$sysconfdir/$XF86CONFIGFILE"
        LOGPREFIX="Xorg."
+       XDG_DATA_HOME=".local/share"
+       XDG_DATA_HOME_LOGDIR="xorg"
        AC_DEFINE(XORG_SERVER, 1, [Building Xorg server])
        AC_DEFINE(XORGSERVER, 1, [Building Xorg server])
        AC_DEFINE(XFree86Server, 1, [Building XFree86 server])
@@ -2057,6 +2059,8 @@ if test "x$XORG" = xyes; then
        AC_DEFINE_DIR(DEFAULT_LIBRARY_PATH, libdir, [Default library install path])
        AC_DEFINE_DIR(DEFAULT_LOGDIR, logdir, [Default log location])
        AC_DEFINE_DIR(DEFAULT_LOGPREFIX, LOGPREFIX, [Default logfile prefix])
+       AC_DEFINE_DIR(DEFAULT_XDG_DATA_HOME, XDG_DATA_HOME, [Default XDG_DATA dir under HOME])
+       AC_DEFINE_DIR(DEFAULT_XDG_DATA_HOME_LOGDIR, XDG_DATA_HOME_LOGDIR, [Default log dir under XDG_DATA_HOME])
        AC_DEFINE_UNQUOTED(__VENDORDWEBSUPPORT__, ["$VENDOR_WEB"], [Vendor web address for support])
        if test "x$VGAHW" = xyes; then
                AC_DEFINE(WITH_VGAHW, 1, [Building vgahw module])
index 12a8771..e2b32a0 100644 (file)
@@ -1217,16 +1217,45 @@ xf86ErrorF(const char *format, ...)
     va_end(ap);
 }
 
+/* Note temporarily modifies the passed in buffer! */
+static void xf86_mkdir_p(char *path)
+{
+    char *sep = path;
+
+    while ((sep = strchr(sep + 1, '/'))) {
+        *sep = 0;
+        (void)mkdir(path, 0777);
+        *sep = '/';
+    }
+    (void)mkdir(path, 0777);
+}
+
 void
 xf86LogInit(void)
 {
-    char *lf = NULL;
+    char *env, *lf = NULL;
+    char buf[PATH_MAX];
 
 #define LOGSUFFIX ".log"
 #define LOGOLDSUFFIX ".old"
 
     /* Get the log file name */
     if (xf86LogFileFrom == X_DEFAULT) {
+        /* When not running as root, we won't be able to write to /var/log */
+        if (geteuid() != 0) {
+            if ((env = getenv("XDG_DATA_HOME")))
+                snprintf(buf, sizeof(buf), "%s/%s", env,
+                         DEFAULT_XDG_DATA_HOME_LOGDIR);
+            else if ((env = getenv("HOME")))
+                snprintf(buf, sizeof(buf), "%s/%s/%s", env,
+                         DEFAULT_XDG_DATA_HOME, DEFAULT_XDG_DATA_HOME_LOGDIR);
+
+            if (env) {
+                xf86_mkdir_p(buf);
+                strlcat(buf, "/" DEFAULT_LOGPREFIX, sizeof(buf));
+                xf86LogFile = buf;
+            }
+        }
         /* Append the display number and ".log" */
         if (asprintf(&lf, "%s%%s" LOGSUFFIX, xf86LogFile) == -1)
             FatalError("Cannot allocate space for the log file name\n");
index 0cd5a10..3ff6aef 100644 (file)
@@ -301,9 +301,11 @@ Use the file called
 .I filename
 as the
 .B Xorg
-server log file.  The default log file is
+server log file.  The default log file when running as root is
 .BI __logdir__/Xorg. n .log
-on most platforms, where
+and for non root it is
+.BI $XDG_DATA_HOME/xorg/Xorg. n .log
+where
 .I n
 is the display number of the
 .B Xorg
index 85f9f2e..6d2652e 100644 (file)
@@ -442,11 +442,15 @@ __modulepath__
 .TP 7
 .BI "LogFile \*q" path \*q
 sets the name of the Xorg server log file.
-The default log file name is
+The default log file name when running as root is
 .PP
 .RS 11
 .RI __logdir__/Xorg. <n> .log
 .RE
+and for non root it is
+.RS 11
+.RI $XDG_DATA_HOME/xorg/Xorg. <n> .log
+.RE
 .PP
 .RS 7
 where
index 4e2a45b..629ae40 100644 (file)
 /* Default logfile prefix */
 #undef DEFAULT_LOGPREFIX
 
+/* Default XDG_DATA dir under HOME */
+#undef DEFAULT_XDG_DATA_HOME
+
+/* Default log dir under XDG_DATA_HOME */
+#undef DEFAULT_XDG_DATA_HOME_LOGDIR
+
 /* Building DRI-capable DDX. */
 #undef XF86DRI