compositor: Verify XDG_RUNTIME_DIR variable
authorMartin Minarik <minarik11@student.fiit.stuba.sk>
Mon, 18 Jun 2012 18:15:18 +0000 (20:15 +0200)
committerKristian Høgsberg <krh@bitplanet.net>
Wed, 20 Jun 2012 17:42:50 +0000 (13:42 -0400)
Print an user friendly error mesage when
the variable is not a valid directory.

krh: Edited to make message a litle more precise and added a check to
verify XDG_RUNTIME_DIR ownership and access mode.

src/compositor.c

index 48b26a5..4c569c6 100644 (file)
@@ -39,6 +39,7 @@
 #include <sys/wait.h>
 #include <sys/socket.h>
 #include <sys/utsname.h>
+#include <sys/stat.h>
 #include <unistd.h>
 #include <math.h>
 #include <linux/input.h>
@@ -3160,11 +3161,48 @@ load_module(const char *name, const char *entrypoint, void **handle)
 }
 
 static const char xdg_error_message[] =
-       "fatal: environment variable XDG_RUNTIME_DIR is not set.\n"
+       "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
+
+static const char xdg_wrong_message[] =
+       "fatal: environment variable XDG_RUNTIME_DIR\n"
+       "is set to \"%s\", which is not a directory.\n";
+
+static const char xdg_wrong_mode_message[] =
+       "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
+       "correctly.  Unix access mode must be 0700 but is %o,\n"
+       "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
+       "owned by UID %d.\n"
+
+static const char xdg_detail_message[] =
        "Refer to your distribution on how to get it, or\n"
        "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
        "on how to implement it.\n";
 
+static void
+verify_xdg_runtime_dir(void)
+{
+       char *dir = getenv("XDG_RUNTIME_DIR");
+       struct stat s;
+
+       if (!dir) {
+               weston_log(xdg_error_message);
+               weston_log_continue(xdg_detail_message);
+               exit(EXIT_FAILURE);
+       }
+
+       if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
+               weston_log(xdg_wrong_message, dir);
+               weston_log_continue(xdg_detail_message);
+               exit(EXIT_FAILURE);
+       }
+
+       if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
+               weston_log(xdg_wrong_mode_message,
+                          dir, s.st_mode & 0777, s.st_uid);
+               weston_log_continue(xdg_detail_message);
+       }
+}
+
 int main(int argc, char *argv[])
 {
        int ret = EXIT_SUCCESS;
@@ -3211,11 +3249,6 @@ int main(int argc, char *argv[])
 
        weston_log_file_open(log);
        
-       if (!getenv("XDG_RUNTIME_DIR")) {
-               weston_log(xdg_error_message);
-               exit(EXIT_FAILURE);
-       }
-
        weston_log("%s\n"
                   STAMP_SPACE "%s\n"
                   STAMP_SPACE "Bug reports to: %s\n"
@@ -3224,6 +3257,8 @@ int main(int argc, char *argv[])
                   BUILD_ID);
        log_uname();
 
+       verify_xdg_runtime_dir();
+
        display = wl_display_create();
 
        loop = wl_display_get_event_loop(display);