json-c: Remove escaping of slashs
[src/app-framework-binder.git] / src / afb-trace.c
index 0e285e1..d81e09c 100644 (file)
@@ -27,6 +27,9 @@
 #include <pthread.h>
 
 #include <json-c/json.h>
+#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
+#define JSON_C_TO_STRING_NOSLASHESCAPE 0
+#endif
 
 #define AFB_BINDING_VERSION 3
 #include <afb/afb-binding.h>
@@ -169,6 +172,7 @@ static int get_flag(const char *name, struct flag flags[], int count)
 /* timestamp */
 static struct json_object *timestamp(const struct afb_hookid *hookid)
 {
+#if JSON_C_MAJOR_VERSION > 0 || JSON_C_MINOR_VERSION >= 12
        char ts[50];
 
        snprintf(ts, sizeof ts, "%llu.%06lu",
@@ -176,9 +180,9 @@ static struct json_object *timestamp(const struct afb_hookid *hookid)
                        (long unsigned)((hookid->time.tv_nsec + 500) / 1000));
 
        return json_object_new_double_s(0.0f, ts); /* the real value isn't used */
-#if 0
-       return json_object_new_string(ts);
-       return json_object_new_double_s(0f, ts); /* the real value isn't used */
+#else
+       return json_object_new_double((double)hookid->time.tv_sec + 
+                       (double)hookid->time.tv_nsec * .000000001);
 #endif
 }
 
@@ -687,10 +691,12 @@ static void hook_api_rootdir_get_fd(void *closure, const struct afb_hookid *hook
 {
        char path[PATH_MAX], proc[100];
        const char *key, *val;
+       ssize_t s;
 
        if (result >= 0) {
                snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
-               readlink(proc, path, sizeof path);
+               s = readlink(proc, path, sizeof path);
+               path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
                key = "path";
                val = path;
        } else {
@@ -705,10 +711,12 @@ static void hook_api_rootdir_open_locale(void *closure, const struct afb_hookid
 {
        char path[PATH_MAX], proc[100];
        const char *key, *val;
+       ssize_t s;
 
        if (result >= 0) {
                snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
-               readlink(proc, path, sizeof path);
+               s = readlink(proc, path, sizeof path);
+               path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
                key = "path";
                val = path;
        } else {
@@ -1021,7 +1029,7 @@ static void hook_session(void *closure, const struct afb_hookid *hookid, struct
 
        va_start(ap, format);
        emit(closure, hookid, "session", "{ss ss}", format, ap,
-                                       "uuid", session,
+                                       "uuid", afb_session_uuid(session),
                                        "action", action);
        va_end(ap);
 }
@@ -1482,7 +1490,7 @@ static void add_flags(void *closure, struct json_object *object, enum trace_type
        if (wrap_json_unpack(object, "s", &name))
                ctxt_error(&desc->context->errors, "unexpected %s value %s",
                                        abstracting[type].name,
-                                       json_object_to_json_string(object));
+                                       json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
        else {
                queried = (name[0] == '*' && !name[1]) ? "all" : name;
                value = abstracting[type].get_flag(queried);
@@ -1595,7 +1603,7 @@ static void add(void *closure, struct json_object *object)
                        wrap_json_optarray_for_all(event, add_evt_flags, &desc);
 
                if (session)
-                       wrap_json_optarray_for_all(event, add_session_flags, &desc);
+                       wrap_json_optarray_for_all(session, add_session_flags, &desc);
 
                if (global)
                        wrap_json_optarray_for_all(global, add_global_flags, &desc);
@@ -1622,7 +1630,7 @@ static void drop_tag(void *closure, struct json_object *object)
 
        rc = wrap_json_unpack(object, "s", &name);
        if (rc)
-               ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
+               ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
        else {
                tag = trace_get_tag(context->trace, name, 0);
                if (!tag)
@@ -1642,7 +1650,7 @@ static void drop_event(void *closure, struct json_object *object)
 
        rc = wrap_json_unpack(object, "s", &name);
        if (rc)
-               ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
+               ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
        else {
                event = trace_get_event(context->trace, name, 0);
                if (!event)
@@ -1662,7 +1670,7 @@ static void drop_session(void *closure, struct json_object *object)
 
        rc = wrap_json_unpack(object, "s", &uuid);
        if (rc)
-               ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
+               ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
        else {
                session = trace_get_session_by_uuid(context->trace, uuid, 0);
                if (!session)
@@ -1739,7 +1747,7 @@ int afb_trace_add(afb_req_t req, struct json_object *args, struct afb_trace *tra
 }
 
 /* drop traces */
-extern int afb_trace_drop(afb_req_t req, struct json_object *args, struct afb_trace *trace)
+int afb_trace_drop(afb_req_t req, struct json_object *args, struct afb_trace *trace)
 {
        int rc;
        struct context context;