[PATCH 2/3] xf86LogInit: log to XDG_DATA_HOME when not running as root

Peter Hutterer peter.hutterer at who-t.net
Tue Mar 25 17:02:16 PDT 2014


On Mon, Mar 24, 2014 at 04:32:00PM +0100, Hans de Goede wrote:
> 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 at redhat.com>
> ---
>  hw/xfree86/common/xf86Helper.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/xfree86/common/xf86Helper.c b/hw/xfree86/common/xf86Helper.c
> index 12a8771..b9d6c1b 100644
> --- a/hw/xfree86/common/xf86Helper.c
> +++ b/hw/xfree86/common/xf86Helper.c
> @@ -1220,13 +1220,27 @@ xf86ErrorF(const char *format, ...)
>  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/xorg", env);
> +            else if ((env = getenv("HOME")))
> +                snprintf(buf, sizeof(buf), "%s/.local/share/xorg", env);
> +
> +            if (env) {
> +                (void)mkdir(buf, 0777);
> +                strlcat(buf, "/Xorg.", sizeof(buf));

I'd prefer if you split LOGPREFIX up in configure.ac into the base
directories and the various extensions so re-use them  here. it's nicer to
have one location where this is defined. likewise, I'd use a pre-defined
DEFAULT_XDG_DATA_HOME here instead of the manual .local path.

Cheers,
   Peter

> +                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");
> -- 
> 1.9.0
> 
 


More information about the xorg-devel mailing list