MacOS: added a setup section to avoid script in application bundle
authorFriedrich Beckmann <friedrich.beckmann@gmx.de>
Tue, 14 Apr 2020 15:09:30 +0000 (17:09 +0200)
committerFriedrich Beckmann <friedrich.beckmann@gmx.de>
Tue, 14 Apr 2020 15:09:30 +0000 (17:09 +0200)
MacOS Catalina has added security features which do not allow to
start an application via a script in an application bundle. See:

https://savannah.gnu.org/bugs/?58128

With this change the application does not need to set environment
variables before start for the relocation.

src/ui/gui/main.c

index 157a0dc8f4bd8ed94bfe4268df3bbe6c1fd62055..f1b525f31c92d5467a4c26478f306a2b397c4212 100644 (file)
@@ -20,6 +20,9 @@
 
 #include <gtk/gtk.h>
 #include <stdlib.h>
+#if ENABLE_RELOCATABLE && defined(__APPLE__)
+#include <sys/stat.h>
+#endif
 
 #include "language/lexer/include-path.h"
 #include "libpspp/argv-parser.h"
@@ -279,10 +282,88 @@ process_pre_start_arguments (int *argc, char ***argv)
   g_option_context_parse (oc, argc, argv, NULL);
 }
 
+#if ENABLE_RELOCATABLE && defined(__APPLE__)
+static void
+pspp_macos_setenv (const char * progname)
+{
+  /* helper to set environment variables for pspp to be relocatable.
+   * Due to the latest changes it is not recommended to set it in the shell
+   * wrapper anymore.
+   */
+  gchar resolved_path[PATH_MAX];
+  /* on some OSX installations open file limit is 256 and GIMP needs more */
+  struct rlimit limit;
+  limit.rlim_cur = 10000;
+  limit.rlim_max = 10000;
+  setrlimit (RLIMIT_NOFILE, &limit);
+  if (realpath (progname, resolved_path))
+    {
+      gchar  tmp[PATH_MAX];
+      gchar *app_dir;
+      gchar  res_dir[PATH_MAX];
+      struct stat sb;
+
+      app_dir = g_path_get_dirname (resolved_path);
+      g_snprintf (tmp, sizeof(tmp), "%s/../../Resources", app_dir);
+      if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
+        g_print ("pspp is started as MacOS application\n");
+      else
+        return;
+      g_free (app_dir);
+
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-3.0/3.0.0", res_dir);
+      g_setenv ("GTK_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-3.0/gtk.immodules", res_dir);
+      g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
+      g_setenv ("GEGL_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
+      g_setenv ("BABL_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
+      g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
+      g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
+      g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/etc/xdg", res_dir);
+      g_setenv ("XDG_CONFIG_DIRS", tmp, TRUE);
+      g_snprintf (tmp, sizeof(tmp), "%s/share", res_dir);
+      g_setenv ("XDG_DATA_DIRS", tmp, TRUE);
+
+      if (g_getenv ("HOME")!=NULL)
+        {
+          g_snprintf (tmp, sizeof(tmp),
+                      "%s/Library/Application Support/pspp/1.3/cache",
+                      g_getenv("HOME"));
+          g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
+        }
+    }
+}
+#endif
 
 int
 main (int argc, char *argv[])
 {
+
+#if ENABLE_RELOCATABLE && defined(__APPLE__)
+  /* remove MacOS session identifier from the command line args */
+  gint newargc = 0;
+  for (gint i = 0; i < argc; i++)
+    {
+      if (!g_str_has_prefix (argv[i], "-psn_"))
+        {
+          argv[newargc] = argv[i];
+          newargc++;
+        }
+    }
+  if (argc > newargc)
+    {
+      argv[newargc] = NULL; /* glib expects NULL terminated array */
+      argc = newargc;
+    }
+  pspp_macos_setenv (argv[0]);
+#endif
+
   set_program_name (argv[0]);
 
   GtkApplication *app =