PolicyKit: Branch 'master'

David Zeuthen david at kemper.freedesktop.org
Wed Nov 14 09:13:16 PST 2012


 src/polkitbackend/Makefile.am                |    3 
 src/polkitbackend/polkitbackendjsauthority.c |  448 ++++++++++++++++++---------
 2 files changed, 304 insertions(+), 147 deletions(-)

New commits:
commit 34cb6353b9affd6c04ea480df5fc39d0ca72319d
Author: David Zeuthen <zeuthen at gmail.com>
Date:   Wed Nov 14 12:12:16 2012 -0500

    Dynamically load libmozjs185.so and cope with it not being available
    
    A number of downstream distributors have - one way or the other -
    requested this feature.
    
    Signed-off-by: David Zeuthen <zeuthen at gmail.com>

diff --git a/src/polkitbackend/Makefile.am b/src/polkitbackend/Makefile.am
index 9f430d0..aa57a97 100644
--- a/src/polkitbackend/Makefile.am
+++ b/src/polkitbackend/Makefile.am
@@ -54,12 +54,13 @@ libpolkit_backend_1_la_CFLAGS =                                        	\
 	$(LIBJS_CFLAGS)							\
         $(NULL)
 
+# Note: We don't include $(LIBJS_LIBS) here since it's dlopen()'ed in polkitbackendjsauthority.c
+#
 libpolkit_backend_1_la_LIBADD =                               		\
         $(GLIB_LIBS)							\
 	$(LIBSYSTEMD_LOGIN_LIBS)					\
 	$(top_builddir)/src/polkit/libpolkit-gobject-1.la		\
 	$(EXPAT_LIBS)							\
-	$(LIBJS_LIBS)							\
         $(NULL)
 
 rulesdir = $(sysconfdir)/polkit-1/rules.d
diff --git a/src/polkitbackend/polkitbackendjsauthority.c b/src/polkitbackend/polkitbackendjsauthority.c
index bdfaa73..034edc4 100644
--- a/src/polkitbackend/polkitbackendjsauthority.c
+++ b/src/polkitbackend/polkitbackendjsauthority.c
@@ -73,6 +73,8 @@ struct _PolkitBackendJsAuthorityPrivate
   GMainLoop *rkt_loop;
   GSource *rkt_source;
 
+  gboolean have_js;
+
   /* A list of JSObject instances */
   GList *scripts;
 };
@@ -135,35 +137,145 @@ G_DEFINE_TYPE (PolkitBackendJsAuthority, polkit_backend_js_authority, POLKIT_BAC
 
 /* ---------------------------------------------------------------------------------------------------- */
 
-static JSClass js_global_class = {
-  "global",
-  JSCLASS_GLOBAL_FLAGS,
-  JS_PropertyStub,
-  JS_PropertyStub,
-  JS_PropertyStub,
-  JS_StrictPropertyStub,
-  JS_EnumerateStub,
-  JS_ResolveStub,
-  JS_ConvertStub,
-  JS_FinalizeStub,
-  JSCLASS_NO_OPTIONAL_MEMBERS
+static JSBool          (*dJS_CallFunctionName)(JSContext *cx, JSObject *obj, const char *name, uintN argc, jsval *argv, jsval *rval);
+static JSObject       *(*dJS_CompileFile)(JSContext *cx, JSObject *obj, const char *filename);
+static JSBool          (*dJS_ConvertArguments)(JSContext *cx, uintN argc, jsval *argv, const char *format, ...);
+static JSBool          (*dJS_ConvertStub)(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
+static JSBool          (*dJS_DefineFunctions)(JSContext *cx, JSObject *obj, JSFunctionSpec *fs);
+static JSObject       *(*dJS_DefineObject)(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp, JSObject *proto, uintN attrs);
+static void            (*dJS_DestroyContext)(JSContext *cx);
+#define dJS_DestroyRuntime dJS_Finish
+static void            (*dJS_Finish)(JSRuntime *rt);
+static char           *(*dJS_EncodeString)(JSContext *cx, JSString *str);
+static JSBool          (*dJS_EnumerateStub)(JSContext *cx, JSObject *obj);
+static JSBool          (*dJS_EvaluateScript)(JSContext *cx, JSObject *obj,
+                                             const char *bytes, uintN length,
+                                             const char *filename, uintN lineno,
+                                             jsval *rval);
+static JSBool          (*dJS_ExecuteScript)(JSContext *cx, JSObject *obj, JSObject *scriptObj, jsval *rval);
+static void            (*dJS_FinalizeStub)(JSContext *cx, JSObject *obj);
+static void            (*dJS_free)(JSContext *cx, void *p);
+static void            (*dJS_GC)(JSContext *cx);
+static JSBool          (*dJS_GetArrayLength)(JSContext *cx, JSObject *obj, jsuint *lengthp);
+static void           *(*dJS_GetContextPrivate)(JSContext *cx);
+static JSBool          (*dJS_GetElement)(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
+static const jschar   *(*dJS_GetStringCharsZ)(JSContext *cx, JSString *str);
+static JSBool          (*dJS_InitStandardClasses)(JSContext *cx, JSObject *obj);
+static void            (*dJS_MaybeGC)(JSContext *cx);
+static JSObject       *(*dJS_NewArrayObject)(JSContext *cx, jsint length, jsval *vector);
+static JSObject       *(*dJS_NewCompartmentAndGlobalObject)(JSContext *cx, JSClass *clasp, JSPrincipals *principals);
+static JSContext      *(*dJS_NewContext)(JSRuntime *rt, size_t stackChunkSize);
+#define dJS_NewRuntime dJS_Init
+static JSRuntime      *(*dJS_Init)(uint32 maxbytes);
+static JSString       *(*dJS_NewStringCopyZ)(JSContext *cx, const char *s);
+static JSBool          (*dJS_PropertyStub)(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
+static void            (*dJS_ReportError)(JSContext *cx, const char *format, ...);
+static JSBool          (*dJS_ReportWarning)(JSContext *cx, const char *format, ...);
+static JSBool          (*dJS_ResolveStub)(JSContext *cx, JSObject *obj, jsid id);
+static void            (*dJS_SetContextPrivate)(JSContext *cx, void *data);
+static JSErrorReporter (*dJS_SetErrorReporter)(JSContext *cx, JSErrorReporter er);
+JSOperationCallback    (*dJS_SetOperationCallback)(JSContext *cx, JSOperationCallback callback);
+static uint32          (*dJS_SetOptions)(JSContext *cx, uint32 options);
+static void            (*dJS_SetPendingException)(JSContext *cx, jsval v);
+static JSBool          (*dJS_SetProperty)(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
+static JSVersion       (*dJS_SetVersion)(JSContext *cx, JSVersion version);
+static void            (*dJS_ShutDown)(void);
+static JSBool          (*dJS_StrictPropertyStub)(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp);
+static void            (*dJS_TriggerOperationCallback)(JSContext *cx);
+
+#define DJS_SYMBOL(x) {#x, (void*) &d ## x}
+static const struct {
+  const char *name;
+  gpointer *ptr;
+} djs_symbols[] = {
+  DJS_SYMBOL(JS_CallFunctionName),
+  DJS_SYMBOL(JS_CompileFile),
+  DJS_SYMBOL(JS_ConvertArguments),
+  DJS_SYMBOL(JS_ConvertStub),
+  DJS_SYMBOL(JS_DefineFunctions),
+  DJS_SYMBOL(JS_DefineObject),
+  DJS_SYMBOL(JS_DestroyContext),
+  DJS_SYMBOL(JS_Finish), /* Macro: JS_DestroyRuntime */
+  DJS_SYMBOL(JS_EncodeString),
+  DJS_SYMBOL(JS_EnumerateStub),
+  DJS_SYMBOL(JS_EvaluateScript),
+  DJS_SYMBOL(JS_ExecuteScript),
+  DJS_SYMBOL(JS_FinalizeStub),
+  DJS_SYMBOL(JS_free),
+  DJS_SYMBOL(JS_GC),
+  DJS_SYMBOL(JS_GetArrayLength),
+  DJS_SYMBOL(JS_GetContextPrivate),
+  DJS_SYMBOL(JS_GetElement),
+  DJS_SYMBOL(JS_GetStringCharsZ),
+  DJS_SYMBOL(JS_InitStandardClasses),
+  DJS_SYMBOL(JS_MaybeGC),
+  DJS_SYMBOL(JS_NewArrayObject),
+  DJS_SYMBOL(JS_NewCompartmentAndGlobalObject),
+  DJS_SYMBOL(JS_NewContext),
+  DJS_SYMBOL(JS_Init), /* Macro: JS_NewRuntime */
+  DJS_SYMBOL(JS_NewStringCopyZ),
+  DJS_SYMBOL(JS_PropertyStub),
+  DJS_SYMBOL(JS_ReportError),
+  DJS_SYMBOL(JS_ReportWarning),
+  DJS_SYMBOL(JS_ResolveStub),
+  DJS_SYMBOL(JS_SetContextPrivate),
+  DJS_SYMBOL(JS_SetErrorReporter),
+  DJS_SYMBOL(JS_SetOperationCallback),
+  DJS_SYMBOL(JS_SetOptions),
+  DJS_SYMBOL(JS_SetPendingException),
+  DJS_SYMBOL(JS_SetProperty),
+  DJS_SYMBOL(JS_SetVersion),
+  DJS_SYMBOL(JS_ShutDown),
+  DJS_SYMBOL(JS_StrictPropertyStub),
+  DJS_SYMBOL(JS_TriggerOperationCallback),
 };
 
+static gboolean
+djs_init (PolkitBackendJsAuthority *authority)
+{
+  gboolean ret = FALSE;
+  GModule *module = NULL;
+  guint n;
+  const gchar *library_name;
+
+  library_name = "libmozjs185.so";
+  module = g_module_open (library_name, 0);
+  if (module == NULL)
+    goto out;
+
+  for (n = 0; n < G_N_ELEMENTS (djs_symbols); n++)
+    {
+      if (!g_module_symbol (module,
+                            djs_symbols[n].name,
+                            djs_symbols[n].ptr) || (*djs_symbols[n].ptr) == NULL)
+        {
+          polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
+                                        "Error looking up symbol `%s' in %s",
+                                        djs_symbols[n].name, library_name);
+          goto out;
+        }
+    }
+
+  ret = TRUE;
+  authority->priv->have_js = TRUE;
+
+ out:
+  if (!ret)
+    {
+      if (module != NULL)
+        g_module_close (module);
+      polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
+                                    "Error opening %s. This is not a fatal error but polkit .rules files will not be evaluated. To enable rules evaluation, install the library",
+                                    library_name);
+    }
+  return ret;
+}
+
 /* ---------------------------------------------------------------------------------------------------- */
 
-static JSClass js_polkit_class = {
-  "Polkit",
-  0,
-  JS_PropertyStub,
-  JS_PropertyStub,
-  JS_PropertyStub,
-  JS_StrictPropertyStub,
-  JS_EnumerateStub,
-  JS_ResolveStub,
-  JS_ConvertStub,
-  JS_FinalizeStub,
-  JSCLASS_NO_OPTIONAL_MEMBERS
-};
+/* members of these structs are set in set in polkit_backend_js_authority_constructed */
+static JSClass js_global_class = {0};
+static JSClass js_polkit_class = {0};
 
 static JSBool js_polkit_log (JSContext *cx, uintN argc, jsval *vp);
 static JSBool js_polkit_spawn (JSContext *cx, uintN argc, jsval *vp);
@@ -183,7 +295,7 @@ static void report_error (JSContext     *cx,
                           const char    *message,
                           JSErrorReport *report)
 {
-  PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (JS_GetContextPrivate (cx));
+  PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (dJS_GetContextPrivate (cx));
   polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
                                 "%s:%u: %s",
                                 report->filename ? report->filename : "<no filename>",
@@ -275,9 +387,9 @@ load_scripts (PolkitBackendJsAuthority  *authority)
       const gchar *filename = l->data;
       JSObject *script;
 
-      script = JS_CompileFile (authority->priv->cx,
-                               authority->priv->js_global,
-                               filename);
+      script = dJS_CompileFile (authority->priv->cx,
+                                authority->priv->js_global,
+                                filename);
       if (script == NULL)
         {
           polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
@@ -315,12 +427,12 @@ reload_scripts (PolkitBackendJsAuthority *authority)
   jsval argv[1] = {0};
   jsval rval = {0};
 
-  if (!JS_CallFunctionName(authority->priv->cx,
-                           authority->priv->js_polkit,
-                           "_deleteRules",
-                           0,
-                           argv,
-                           &rval))
+  if (!dJS_CallFunctionName (authority->priv->cx,
+                             authority->priv->js_polkit,
+                             "_deleteRules",
+                             0,
+                             argv,
+                             &rval))
     {
       polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
                                     "Error deleting old rules, not loading new ones");
@@ -329,7 +441,7 @@ reload_scripts (PolkitBackendJsAuthority *authority)
 
   polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
                                 "Collecting garbage unconditionally...");
-  JS_GC (authority->priv->cx);
+  dJS_GC (authority->priv->cx);
 
   load_scripts (authority);
 
@@ -419,53 +531,78 @@ polkit_backend_js_authority_constructed (GObject *object)
 {
   PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (object);
 
-  authority->priv->rt = JS_NewRuntime (8L * 1024L * 1024L);
+  if (!djs_init (authority))
+    goto no_js;
+
+  authority->priv->rt = dJS_NewRuntime (8L * 1024L * 1024L);
   if (authority->priv->rt == NULL)
     goto fail;
 
-  authority->priv->cx = JS_NewContext (authority->priv->rt, 8192);
+  authority->priv->cx = dJS_NewContext (authority->priv->rt, 8192);
   if (authority->priv->cx == NULL)
     goto fail;
 
   /* TODO: JIT'ing doesn't work will with killing runaway scripts... I think
    *       this is just a SpiderMonkey bug. So disable the JIT for now.
    */
-  JS_SetOptions (authority->priv->cx,
-                 JSOPTION_VAROBJFIX
-                 /* | JSOPTION_JIT | JSOPTION_METHODJIT*/);
-  JS_SetVersion(authority->priv->cx, JSVERSION_LATEST);
-  JS_SetErrorReporter(authority->priv->cx, report_error);
-  JS_SetContextPrivate (authority->priv->cx, authority);
-
-  authority->priv->js_global = JS_NewCompartmentAndGlobalObject (authority->priv->cx,
-                                                                 &js_global_class,
-                                                                 NULL);
+  dJS_SetOptions (authority->priv->cx,
+                  JSOPTION_VAROBJFIX
+                  /* | JSOPTION_JIT | JSOPTION_METHODJIT*/);
+  dJS_SetVersion (authority->priv->cx, JSVERSION_LATEST);
+  dJS_SetErrorReporter (authority->priv->cx, report_error);
+  dJS_SetContextPrivate (authority->priv->cx, authority);
+
+  js_global_class.name        = "global";
+  js_global_class.flags       = JSCLASS_GLOBAL_FLAGS;
+  js_global_class.addProperty = dJS_PropertyStub;
+  js_global_class.delProperty = dJS_PropertyStub;
+  js_global_class.getProperty = dJS_PropertyStub;
+  js_global_class.setProperty = dJS_StrictPropertyStub;
+  js_global_class.enumerate   = dJS_EnumerateStub;
+  js_global_class.resolve     = dJS_ResolveStub;
+  js_global_class.convert     = dJS_ConvertStub;
+  js_global_class.finalize    = dJS_FinalizeStub;
+
+  js_polkit_class.name        = "Polkit";
+  js_polkit_class.flags       = 0;
+  js_polkit_class.addProperty = dJS_PropertyStub;
+  js_polkit_class.delProperty = dJS_PropertyStub;
+  js_polkit_class.getProperty = dJS_PropertyStub;
+  js_polkit_class.setProperty = dJS_StrictPropertyStub;
+  js_polkit_class.enumerate   = dJS_EnumerateStub;
+  js_polkit_class.resolve     = dJS_ResolveStub;
+  js_polkit_class.convert     = dJS_ConvertStub;
+  js_polkit_class.finalize    = dJS_FinalizeStub;
+
+  authority->priv->js_global = dJS_NewCompartmentAndGlobalObject (authority->priv->cx,
+                                                                  &js_global_class,
+                                                                  NULL);
   if (authority->priv->js_global == NULL)
     goto fail;
 
-  if (!JS_InitStandardClasses (authority->priv->cx, authority->priv->js_global))
+  if (!dJS_InitStandardClasses (authority->priv->cx, authority->priv->js_global))
     goto fail;
 
-  authority->priv->js_polkit = JS_DefineObject (authority->priv->cx,
-                                                authority->priv->js_global,
-                                                "polkit",
-                                                &js_polkit_class,
-                                                NULL,
-                                                JSPROP_ENUMERATE);
+  authority->priv->js_polkit = dJS_DefineObject (authority->priv->cx,
+                                                 authority->priv->js_global,
+                                                 "polkit",
+                                                 &js_polkit_class,
+                                                 NULL,
+                                                 JSPROP_ENUMERATE);
   if (authority->priv->js_polkit == NULL)
     goto fail;
 
-  if (!JS_DefineFunctions (authority->priv->cx,
-                           authority->priv->js_polkit,
-                           js_polkit_functions))
+  if (!dJS_DefineFunctions (authority->priv->cx,
+                            authority->priv->js_polkit,
+                            js_polkit_functions))
     goto fail;
 
-  if (!JS_EvaluateScript (authority->priv->cx,
-                          authority->priv->js_global,
-                          init_js, strlen (init_js), /* init.js */
-                          "init.js",  /* filename */
-                          0,     /* lineno */
-                          NULL)) /* rval */
+  if (!dJS_EvaluateScript (authority->priv->cx,
+                           authority->priv->js_global,
+                           init_js, strlen (init_js), /* init.js */
+                           "init.js",  /* filename */
+                           0,     /* lineno */
+                           NULL)) /* rval */
     {
       goto fail;
     }
@@ -492,6 +629,8 @@ polkit_backend_js_authority_constructed (GObject *object)
   setup_file_monitors (authority);
   load_scripts (authority);
 
+ no_js:
+
   G_OBJECT_CLASS (polkit_backend_js_authority_parent_class)->constructed (object);
   return;
 
@@ -504,31 +643,35 @@ static void
 polkit_backend_js_authority_finalize (GObject *object)
 {
   PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (object);
-  guint n;
 
-  g_mutex_clear (&authority->priv->rkt_init_mutex);
-  g_cond_clear (&authority->priv->rkt_init_cond);
+  if (authority->priv->have_js)
+    {
+      guint n;
 
-  /* shut down the killer thread */
-  g_assert (authority->priv->rkt_loop != NULL);
-  g_main_loop_quit (authority->priv->rkt_loop);
-  g_thread_join (authority->priv->runaway_killer_thread);
-  g_assert (authority->priv->rkt_loop == NULL);
+      g_mutex_clear (&authority->priv->rkt_init_mutex);
+      g_cond_clear (&authority->priv->rkt_init_cond);
 
-  for (n = 0; authority->priv->dir_monitors != NULL && authority->priv->dir_monitors[n] != NULL; n++)
-    {
-      GFileMonitor *monitor = authority->priv->dir_monitors[n];
-      g_signal_handlers_disconnect_by_func (monitor,
-                                            G_CALLBACK (on_dir_monitor_changed),
-                                            authority);
-      g_object_unref (monitor);
-    }
-  g_free (authority->priv->dir_monitors);
-  g_strfreev (authority->priv->rules_dirs);
+      /* shut down the killer thread */
+      g_assert (authority->priv->rkt_loop != NULL);
+      g_main_loop_quit (authority->priv->rkt_loop);
+      g_thread_join (authority->priv->runaway_killer_thread);
+      g_assert (authority->priv->rkt_loop == NULL);
+
+      for (n = 0; authority->priv->dir_monitors != NULL && authority->priv->dir_monitors[n] != NULL; n++)
+        {
+          GFileMonitor *monitor = authority->priv->dir_monitors[n];
+          g_signal_handlers_disconnect_by_func (monitor,
+                                                G_CALLBACK (on_dir_monitor_changed),
+                                                authority);
+          g_object_unref (monitor);
+        }
+      g_free (authority->priv->dir_monitors);
+      g_strfreev (authority->priv->rules_dirs);
 
-  JS_DestroyContext (authority->priv->cx);
-  JS_DestroyRuntime (authority->priv->rt);
-  /* JS_ShutDown (); */
+      dJS_DestroyContext (authority->priv->cx);
+      dJS_DestroyRuntime (authority->priv->rt);
+      /* dJS_ShutDown (); */
+    }
 
   G_OBJECT_CLASS (polkit_backend_js_authority_parent_class)->finalize (object);
 }
@@ -616,9 +759,9 @@ set_property_str (PolkitBackendJsAuthority  *authority,
 {
   JSString *value_jsstr;
   jsval value_jsval;
-  value_jsstr = JS_NewStringCopyZ (authority->priv->cx, value);
+  value_jsstr = dJS_NewStringCopyZ (authority->priv->cx, value);
   value_jsval = STRING_TO_JSVAL (value_jsstr);
-  JS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
+  dJS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
 }
 
 static void
@@ -640,14 +783,14 @@ set_property_strv (PolkitBackendJsAuthority  *authority,
   for (n = 0; n < len; n++)
     {
       JSString *jsstr;
-      jsstr = JS_NewStringCopyZ (authority->priv->cx, value[n]);
+      jsstr = dJS_NewStringCopyZ (authority->priv->cx, value[n]);
       jsvals[n] = STRING_TO_JSVAL (jsstr);
     }
 
-  array_object = JS_NewArrayObject (authority->priv->cx, (jsint) len, jsvals);
+  array_object = dJS_NewArrayObject (authority->priv->cx, (jsint) len, jsvals);
 
   value_jsval = OBJECT_TO_JSVAL (array_object);
-  JS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
+  dJS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
 
   g_free (jsvals);
 }
@@ -661,7 +804,7 @@ set_property_int32 (PolkitBackendJsAuthority  *authority,
 {
   jsval value_jsval;
   value_jsval = INT_TO_JSVAL ((int32) value);
-  JS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
+  dJS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
 }
 
 static void
@@ -672,7 +815,7 @@ set_property_bool (PolkitBackendJsAuthority  *authority,
 {
   jsval value_jsval;
   value_jsval = BOOLEAN_TO_JSVAL ((JSBool) value);
-  JS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
+  dJS_SetProperty (authority->priv->cx, obj, name, &value_jsval);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -700,11 +843,11 @@ subject_to_jsval (PolkitBackendJsAuthority  *authority,
 
   src = "new Subject();";
 
-  if (!JS_EvaluateScript (authority->priv->cx,
-                          authority->priv->js_global,
-                          src, strlen (src),
-                          __FILE__, __LINE__,
-                          &ret_jsval))
+  if (!dJS_EvaluateScript (authority->priv->cx,
+                           authority->priv->js_global,
+                           src, strlen (src),
+                           __FILE__, __LINE__,
+                           &ret_jsval))
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Evaluting '%s' failed", src);
       goto out;
@@ -826,11 +969,11 @@ action_and_details_to_jsval (PolkitBackendJsAuthority  *authority,
   guint n;
 
   src = "new Action();";
-  if (!JS_EvaluateScript (authority->priv->cx,
-                          authority->priv->js_global,
-                          src, strlen (src),
-                          __FILE__, __LINE__,
-                          &ret_jsval))
+  if (!dJS_EvaluateScript (authority->priv->cx,
+                           authority->priv->js_global,
+                           src, strlen (src),
+                           __FILE__, __LINE__,
+                           &ret_jsval))
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Evaluting '%s' failed", src);
       goto out;
@@ -895,7 +1038,7 @@ runaway_killer_thread_func (gpointer user_data)
 static JSBool
 js_operation_callback (JSContext *cx)
 {
-  PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (JS_GetContextPrivate (cx));
+  PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (dJS_GetContextPrivate (cx));
   JSString *val_str;
   jsval val;
 
@@ -903,9 +1046,9 @@ js_operation_callback (JSContext *cx)
   polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), "Terminating runaway script");
 
   /* Throw an exception - this way the JS code can ignore the runaway script handling */
-  val_str = JS_NewStringCopyZ (cx, "Terminating runaway script");
+  val_str = dJS_NewStringCopyZ (cx, "Terminating runaway script");
   val = STRING_TO_JSVAL (val_str);
-  JS_SetPendingException (authority->priv->cx, val);
+  dJS_SetPendingException (authority->priv->cx, val);
   return JS_FALSE;
 }
 
@@ -915,7 +1058,7 @@ rkt_on_timeout (gpointer user_data)
   PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (user_data);
 
   /* Supposedly this is thread-safe... */
-  JS_TriggerOperationCallback (authority->priv->cx);
+  dJS_TriggerOperationCallback (authority->priv->cx);
 
   /* keep source around so we keep trying to kill even if the JS bit catches the exception
    * thrown in js_operation_callback()
@@ -936,13 +1079,13 @@ runaway_killer_setup (PolkitBackendJsAuthority *authority)
   /* ... rkt_on_timeout() will then poke the JSContext so js_operation_callback() is
    * called... and from there we throw an exception
    */
-  JS_SetOperationCallback (authority->priv->cx, js_operation_callback);
+  dJS_SetOperationCallback (authority->priv->cx, js_operation_callback);
 }
 
 static void
 runaway_killer_teardown (PolkitBackendJsAuthority *authority)
 {
-  JS_SetOperationCallback (authority->priv->cx, NULL);
+  dJS_SetOperationCallback (authority->priv->cx, NULL);
   g_source_destroy (authority->priv->rkt_source);
   g_source_unref (authority->priv->rkt_source);
   authority->priv->rkt_source = NULL;
@@ -956,10 +1099,10 @@ execute_script_with_runaway_killer (PolkitBackendJsAuthority *authority,
   JSBool ret;
 
   runaway_killer_setup (authority);
-  ret = JS_ExecuteScript (authority->priv->cx,
-                          authority->priv->js_global,
-                          script,
-                          rval);
+  ret = dJS_ExecuteScript (authority->priv->cx,
+                           authority->priv->js_global,
+                           script,
+                           rval);
   runaway_killer_teardown (authority);
 
   return ret;
@@ -974,12 +1117,12 @@ call_js_function_with_runaway_killer (PolkitBackendJsAuthority *authority,
 {
   JSBool ret;
   runaway_killer_setup (authority);
-  ret = JS_CallFunctionName(authority->priv->cx,
-                            authority->priv->js_polkit,
-                            function_name,
-                            argc,
-                            argv,
-                            rval);
+  ret = dJS_CallFunctionName (authority->priv->cx,
+                              authority->priv->js_polkit,
+                              function_name,
+                              argc,
+                              argv,
+                              rval);
   runaway_killer_teardown (authority);
   return ret;
 }
@@ -1006,6 +1149,10 @@ polkit_backend_js_authority_get_admin_auth_identities (PolkitBackendInteractiveA
   gchar *ret_str = NULL;
   gchar **ret_strs = NULL;
 
+  /* If we don't have JS, we fall back to uid 0 as per below */
+  if (!authority->priv->have_js)
+    goto out;
+
   if (!action_and_details_to_jsval (authority, action_id, details, &argv[0], &error))
     {
       polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
@@ -1048,7 +1195,7 @@ polkit_backend_js_authority_get_admin_auth_identities (PolkitBackendInteractiveA
     }
 
   ret_jsstr = JSVAL_TO_STRING (rval);
-  ret_str = g_utf16_to_utf8 (JS_GetStringCharsZ (authority->priv->cx, ret_jsstr), -1, NULL, NULL, NULL);
+  ret_str = g_utf16_to_utf8 (dJS_GetStringCharsZ (authority->priv->cx, ret_jsstr), -1, NULL, NULL, NULL);
   if (ret_str == NULL)
     {
       g_warning ("Error converting resulting string to UTF-8: %s", error->message);
@@ -1083,7 +1230,8 @@ polkit_backend_js_authority_get_admin_auth_identities (PolkitBackendInteractiveA
   if (ret == NULL)
     ret = g_list_prepend (ret, polkit_unix_user_new (0));
 
-  JS_MaybeGC (authority->priv->cx);
+  if (authority->priv->have_js)
+    dJS_MaybeGC (authority->priv->cx);
 
   return ret;
 }
@@ -1111,6 +1259,13 @@ polkit_backend_js_authority_check_authorization_sync (PolkitBackendInteractiveAu
   gchar *ret_str = NULL;
   gboolean good = FALSE;
 
+  /* If we don't have JS, use implicit authorization */
+  if (!authority->priv->have_js)
+    {
+      good = TRUE;
+      goto out;
+    }
+
   if (!action_and_details_to_jsval (authority, action_id, details, &argv[0], &error))
     {
       polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority),
@@ -1160,7 +1315,7 @@ polkit_backend_js_authority_check_authorization_sync (PolkitBackendInteractiveAu
       goto out;
     }
 
-  ret_utf16 = JS_GetStringCharsZ (authority->priv->cx, ret_jsstr);
+  ret_utf16 = dJS_GetStringCharsZ (authority->priv->cx, ret_jsstr);
   ret_str = g_utf16_to_utf8 (ret_utf16, -1, NULL, NULL, &error);
   if (ret_str == NULL)
     {
@@ -1185,7 +1340,8 @@ polkit_backend_js_authority_check_authorization_sync (PolkitBackendInteractiveAu
     ret = POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED;
   g_free (ret_str);
 
-  JS_MaybeGC (authority->priv->cx);
+  if (authority->priv->have_js)
+    dJS_MaybeGC (authority->priv->cx);
 
   return ret;
 }
@@ -1197,17 +1353,17 @@ js_polkit_log (JSContext  *cx,
                uintN       argc,
                jsval      *vp)
 {
-  /* PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (JS_GetContextPrivate (cx)); */
+  /* PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (dJS_GetContextPrivate (cx)); */
   JSBool ret = JS_FALSE;
   JSString *str;
   char *s;
 
-  if (!JS_ConvertArguments (cx, argc, JS_ARGV (cx, vp), "S", &str))
+  if (!dJS_ConvertArguments (cx, argc, JS_ARGV (cx, vp), "S", &str))
     goto out;
 
-  s = JS_EncodeString (cx, str);
-  JS_ReportWarning (cx, s);
-  JS_free (cx, s);
+  s = dJS_EncodeString (cx, str);
+  dJS_ReportWarning (cx, s);
+  dJS_free (cx, s);
 
   ret = JS_TRUE;
 
@@ -1280,7 +1436,7 @@ js_polkit_spawn (JSContext  *cx,
                  uintN       js_argc,
                  jsval      *vp)
 {
-  /* PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (JS_GetContextPrivate (cx)); */
+  /* PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (dJS_GetContextPrivate (cx)); */
   JSBool ret = JS_FALSE;
   JSObject *array_object;
   gchar *standard_output = NULL;
@@ -1295,12 +1451,12 @@ js_polkit_spawn (JSContext  *cx,
   SpawnData data = {0};
   guint n;
 
-  if (!JS_ConvertArguments (cx, js_argc, JS_ARGV (cx, vp), "o", &array_object))
+  if (!dJS_ConvertArguments (cx, js_argc, JS_ARGV (cx, vp), "o", &array_object))
     goto out;
 
-  if (!JS_GetArrayLength (cx, array_object, &array_len))
+  if (!dJS_GetArrayLength (cx, array_object, &array_len))
     {
-      JS_ReportError (cx, "Failed to get array length");
+      dJS_ReportError (cx, "Failed to get array length");
       goto out;
     }
 
@@ -1310,14 +1466,14 @@ js_polkit_spawn (JSContext  *cx,
       jsval elem_val;
       char *s;
 
-      if (!JS_GetElement (cx, array_object, n, &elem_val))
+      if (!dJS_GetElement (cx, array_object, n, &elem_val))
         {
-          JS_ReportError (cx, "Failed to get element %d", n);
+          dJS_ReportError (cx, "Failed to get element %d", n);
           goto out;
         }
-      s = JS_EncodeString (cx, JSVAL_TO_STRING (elem_val));
+      s = dJS_EncodeString (cx, JSVAL_TO_STRING (elem_val));
       argv[n] = g_strdup (s);
-      JS_free (cx, s);
+      dJS_free (cx, s);
     }
 
   context = g_main_context_new ();
@@ -1342,9 +1498,9 @@ js_polkit_spawn (JSContext  *cx,
                            &standard_error,
                            &error))
     {
-      JS_ReportError (cx,
-                      "Error spawning helper: %s (%s, %d)",
-                      error->message, g_quark_to_string (error->domain), error->code);
+      dJS_ReportError (cx,
+                       "Error spawning helper: %s (%s, %d)",
+                       error->message, g_quark_to_string (error->domain), error->code);
       g_clear_error (&error);
       goto out;
     }
@@ -1368,14 +1524,14 @@ js_polkit_spawn (JSContext  *cx,
         }
       g_string_append_printf (gstr, ", stdout=`%s', stderr=`%s'",
                               standard_output, standard_error);
-      JS_ReportError (cx, gstr->str);
+      dJS_ReportError (cx, gstr->str);
       g_string_free (gstr, TRUE);
       goto out;
     }
 
   ret = JS_TRUE;
 
-  ret_jsstr = JS_NewStringCopyZ (cx, standard_output);
+  ret_jsstr = dJS_NewStringCopyZ (cx, standard_output);
   JS_SET_RVAL (cx, vp, STRING_TO_JSVAL (ret_jsstr));
 
  out:
@@ -1398,7 +1554,7 @@ js_polkit_user_is_in_netgroup (JSContext  *cx,
                                uintN       argc,
                                jsval      *vp)
 {
-  /* PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (JS_GetContextPrivate (cx)); */
+  /* PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (dJS_GetContextPrivate (cx)); */
   JSBool ret = JS_FALSE;
   JSString *user_str;
   JSString *netgroup_str;
@@ -1406,11 +1562,11 @@ js_polkit_user_is_in_netgroup (JSContext  *cx,
   char *netgroup;
   JSBool is_in_netgroup = JS_FALSE;
 
-  if (!JS_ConvertArguments (cx, argc, JS_ARGV (cx, vp), "SS", &user_str, &netgroup_str))
+  if (!dJS_ConvertArguments (cx, argc, JS_ARGV (cx, vp), "SS", &user_str, &netgroup_str))
     goto out;
 
-  user = JS_EncodeString (cx, user_str);
-  netgroup = JS_EncodeString (cx, netgroup_str);
+  user = dJS_EncodeString (cx, user_str);
+  netgroup = dJS_EncodeString (cx, netgroup_str);
 
   if (innetgr (netgroup,
                NULL,  /* host */
@@ -1420,8 +1576,8 @@ js_polkit_user_is_in_netgroup (JSContext  *cx,
       is_in_netgroup =  JS_TRUE;
     }
 
-  JS_free (cx, netgroup);
-  JS_free (cx, user);
+  dJS_free (cx, netgroup);
+  dJS_free (cx, user);
 
   ret = JS_TRUE;
 


More information about the hal-commit mailing list