[systemd-devel] [RFC 4/6] proxy-discoveryd: Execute the PAC based proxy in a thread

Tomasz Bursztyka tomasz.bursztyka at linux.intel.com
Fri Apr 10 05:17:41 PDT 2015


This is sub-optimal as duktape JS engine does not provide any mean to
share the same context over different threads. Each one needs its own,
thus the need to load the PAC file everytime. This will need to be
investigated and fixed.
---
 src/proxy-discovery/proxy-discoveryd-pac.c   |  7 +---
 src/proxy-discovery/proxy-discoveryd-proxy.c | 58 ++++++++++++++++++++++++++++
 src/proxy-discovery/proxy-discoveryd.h       |  2 +
 3 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/src/proxy-discovery/proxy-discoveryd-pac.c b/src/proxy-discovery/proxy-discoveryd-pac.c
index 5c779bb..1999b78 100644
--- a/src/proxy-discovery/proxy-discoveryd-pac.c
+++ b/src/proxy-discovery/proxy-discoveryd-pac.c
@@ -111,7 +111,7 @@ static int pac_my_ip_address(duk_context *ctx) {
         return 1;
 }
 
-static int create_context(struct PAC *pac, char *pac_file, void *user_data) {
+static int create_context(struct PAC *pac, char *pac_file) {
         duk_context *ctx;
 
         ctx = duk_create_heap(NULL, NULL, NULL, NULL, NULL);
@@ -124,9 +124,6 @@ static int create_context(struct PAC *pac, char *pac_file, void *user_data) {
         duk_push_c_function(ctx, pac_my_ip_address, 0);
         duk_put_prop_string(ctx, -2, "myIpAddress");
 
-        duk_push_pointer(ctx, user_data);
-        duk_put_prop_string(ctx, -2, "_user_data_");
-
         duk_pop(ctx);
 
         if (duk_peval_file(ctx, pac_file) != 0) {
@@ -150,7 +147,7 @@ int pac_load(Proxy *proxy, struct PAC **ret) {
         if (!pac)
                 return -ENOMEM;
 
-        r = create_context(pac, proxy->pac_path, proxy);
+        r = create_context(pac, proxy->pac_path);
         if (r < 0)
                 return r;
 
diff --git a/src/proxy-discovery/proxy-discoveryd-proxy.c b/src/proxy-discovery/proxy-discoveryd-proxy.c
index d18395b..43d258f 100644
--- a/src/proxy-discovery/proxy-discoveryd-proxy.c
+++ b/src/proxy-discovery/proxy-discoveryd-proxy.c
@@ -22,6 +22,12 @@
 #include "proxy-discoveryd.h"
 
 #include "conf-parser.h"
+#include "async.h"
+
+struct proxy_parameters {
+        sd_bus_message *message;
+        Proxy *proxy;
+};
 
 void proxy_free(Proxy *proxy) {
         if (!proxy)
@@ -75,3 +81,55 @@ int proxy_load(Manager *manager, const char *filename, Proxy **ret) {
 
         return 0;
 }
+
+static void *run_thread(void *data) {
+        _cleanup_free_ struct proxy_parameters *params = NULL;
+        _cleanup_pac_free_ struct PAC *pac = NULL;
+        _cleanup_free_ char *result = NULL;
+        const char *url, *host;
+
+        params = data;
+
+        if (sd_bus_message_read(params->message, "ss", &url, &host) < 0)
+                goto answer;
+
+        if (pac_load(params->proxy, &pac) < 0)
+                goto answer;
+
+        pac_execute(pac, url, host, &result);
+
+answer:
+        if (!result)
+                result = strdup("DIRECT");
+
+        sd_bus_reply_method_return(params->message, "s", result);
+
+        params->message = sd_bus_message_unref(params->message);
+
+        return NULL;
+}
+
+int proxy_execute(Proxy *proxy, sd_bus_message *message) {
+        _cleanup_free_ struct proxy_parameters *params = NULL;
+        int r;
+
+        if (!proxy)
+                return -EINVAL;
+
+        params = new0(struct proxy_parameters, 1);
+        if (!params)
+                return log_oom();
+
+        params->message = sd_bus_message_ref(message);
+        params->proxy = proxy;
+
+        r = asynchronous_job(run_thread, params);
+        if (r < 0) {
+                params->message = sd_bus_message_unref(params->message);
+                return r;
+        }
+
+        params = NULL;
+
+        return 0;
+}
diff --git a/src/proxy-discovery/proxy-discoveryd.h b/src/proxy-discovery/proxy-discoveryd.h
index 17c5378..d8d9482 100644
--- a/src/proxy-discovery/proxy-discoveryd.h
+++ b/src/proxy-discovery/proxy-discoveryd.h
@@ -22,6 +22,7 @@
 ***/
 
 #include "sd-event.h"
+#include "sd-bus.h"
 
 #include "util.h"
 #include "list.h"
@@ -61,6 +62,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
 void proxy_free(Proxy *proxy);
 
 int proxy_load(Manager *manager, const char *filename, Proxy **ret);
+int proxy_execute(Proxy *proxy, sd_bus_message *message);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(Proxy*, proxy_free);
 #define _cleanup_proxy_free_ _cleanup_(proxy_freep)
-- 
2.0.5



More information about the systemd-devel mailing list