[Uludag-commits] r16210 - in branches/comar-dbus: include src

uludag-commits at pardus.org.tr uludag-commits at pardus.org.tr
3 Ara 2007 Pzt 14:15:28 EET


Author: bahadir.kandemir
Date: Mon Dec  3 14:15:28 2007
New Revision: 16210

Modified:
   branches/comar-dbus/include/csl.h
   branches/comar-dbus/include/utility.h
   branches/comar-dbus/src/csl.c
   branches/comar-dbus/src/dbus.c
   branches/comar-dbus/src/main.c
   branches/comar-dbus/src/utility.c
Log:
- Check interface and object path existance before forking dbus call
- Validate argument tuple
- Show execution time


Modified: branches/comar-dbus/include/csl.h
=================================================================
--- branches/comar-dbus/include/csl.h	(original)
+++ branches/comar-dbus/include/csl.h	Mon Dec  3 14:15:28 2007
@@ -13,3 +13,4 @@
 PyObject *py_call_method(const char *model, const char *app, const char *method, PyObject *args);
 PyObject *dbus_py_import(DBusMessage *msg);
 void dbus_py_export(DBusMessageIter *iter, PyObject *obj);
+int py_check_args(PyObject *tuple);

Modified: branches/comar-dbus/include/utility.h
=================================================================
--- branches/comar-dbus/include/utility.h	(original)
+++ branches/comar-dbus/include/utility.h	Mon Dec  3 14:15:28 2007
@@ -7,7 +7,9 @@
 ** option) any later version. Please read the COPYING file.
 */
 
+int check_file(const char *fname);
 unsigned char *load_file(const char *fname, int *sizeptr);
 int check_interface_format(const char *interface);
 int check_path_format(const char *path);
 char *get_script_path(const char *interface, const char *path);
+unsigned long time_diff(struct timeval *start, struct timeval *end);

Modified: branches/comar-dbus/src/csl.c
=================================================================
--- branches/comar-dbus/src/csl.c	(original)
+++ branches/comar-dbus/src/csl.c	Mon Dec  3 14:15:28 2007
@@ -27,6 +27,30 @@
     return 0;
 }
 
+int
+py_check_args(PyObject *tuple)
+{
+    PyObject *pItem, *pKey;
+    int i;
+
+    if (!PyTuple_Check(tuple)) {
+        return 0;
+    }
+
+    for (i == 0; i < PyTuple_Size(tuple); i++) {
+        pItem = PyTuple_GetItem(tuple, i);
+        if (!PyTuple_Check(pItem) || PyTuple_Size(pItem) != 2) {
+            return 0;
+        }
+        pKey = PyTuple_GetItem(pItem, 0);
+        if (!PyString_Check(pKey)) {
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
 //! Call model's method with given arguments
 PyObject *
 py_call_method(const char *model, const char *path, const char *method, PyObject *args)

Modified: branches/comar-dbus/src/dbus.c
=================================================================
--- branches/comar-dbus/src/dbus.c	(original)
+++ branches/comar-dbus/src/dbus.c	Mon Dec  3 14:15:28 2007
@@ -54,6 +54,20 @@
 }
 
 static void
+dbus_send(DBusConnection *conn, DBusMessage *reply)
+{
+    dbus_uint32_t serial = 0;
+
+    if (!dbus_connection_send(conn, reply, &serial)) {
+        log_error("Out Of Memory!\n");
+        exit(1);
+    }
+
+    dbus_connection_flush(conn);
+    dbus_message_unref(reply);
+}
+
+static void
 dbus_method_call()
 {
     DBusMessage *reply;
@@ -61,31 +75,35 @@
     dbus_uint32_t serial = 0;
     PyObject *obj, *ret;
 
+    struct timeval time_start, time_end;
+    unsigned long msec;
+
     const char *interface = dbus_message_get_interface(my_proc.bus_msg);
     const char *path = dbus_message_get_path(my_proc.bus_msg);
     const char *method = dbus_message_get_member(my_proc.bus_msg);
 
     dbus_bool_t no_reply = dbus_message_get_no_reply(my_proc.bus_msg);
 
-    if (!check_interface_format(interface)) {
-        log_error("Invalid interface: %s\n", interface);
-        if (!no_reply) {
-            reply = dbus_message_new_error(my_proc.bus_msg, DBUS_ERROR_FAILED, "Invalid interface");
-        }
-    }
-    else if (!check_path_format(path)) {
-        log_error("Invalid application\n");
+    Py_Initialize();
+
+    obj = dbus_py_import(my_proc.bus_msg);
+    obj = PyList_GetItem(obj, 0);
+
+    if (!py_check_args(obj)) {
+        log_error("%s.%s() argument format is not valid.\n", interface, method);
         if (!no_reply) {
-            reply = dbus_message_new_error(my_proc.bus_msg, DBUS_ERROR_FAILED, "Invalid application");
+            reply = dbus_message_new_error(my_proc.bus_msg, DBUS_ERROR_FAILED, "Argument format not valid");
         }
     }
     else {
-        Py_Initialize();
+        log_debug(LOG_CALL, "Executing %s.%s (%s)\n", interface, method, path);
 
-        obj = dbus_py_import(my_proc.bus_msg);
-        obj = PyList_GetItem(obj, 0);
-        log_debug(LOG_CALL, "Calling %s.%s (%s)\n", interface, method, path);
+        gettimeofday(&time_start, NULL);
         ret = py_call_method(interface, path, method, obj);
+        gettimeofday(&time_end, NULL);
+        msec = time_diff(&time_start, &time_end);
+
+        log_debug(LOG_PERF, "Execution took %.3f seconds\n", (float) msec / 1000);
 
         if (ret == NULL) {
             reply = log_exception(my_proc.bus_msg, my_proc.bus_conn);
@@ -95,9 +113,10 @@
             dbus_message_iter_init_append(reply, &iter);
             dbus_py_export(&iter, ret);
         }
-        Py_Finalize();
     }
 
+    Py_Finalize();
+
     if (!no_reply) {
         if (!dbus_connection_send(my_proc.bus_conn, reply, &serial)) {
             log_error("Out Of Memory!\n");
@@ -115,8 +134,10 @@
     int size;
 
     DBusConnection *conn;
-    DBusMessage *msg;
+    DBusMessage *msg, *reply;
+    DBusMessageIter iter;
     DBusError err;
+    dbus_uint32_t serial = 0;
     int ret;
     const char *unique_name;
     PyObject *args;
@@ -132,7 +153,7 @@
         exit(1);
     }
 
-    ret = dbus_bus_request_name(conn, cfg_bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
+    ret = dbus_bus_request_name(conn, cfg_bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
     if (dbus_error_is_set(&err)) {
         log_error("Name Error (%s)\n", err.message);
         dbus_error_free(&err);
@@ -145,6 +166,8 @@
     unique_name = dbus_bus_get_unique_name(conn);
     log_info("Listening on %s (%s)...\n", cfg_bus_name, unique_name);
 
+    const char *introspection = load_file("/home/bahadir/repos/works/comar-dbus/introspection.xml", NULL);
+
     while (1) {
         dbus_connection_read_write(conn, 0);
         msg = dbus_connection_pop_message(conn);
@@ -159,21 +182,53 @@
             continue;
         }
 
+        const char *sender = dbus_message_get_sender(msg);
+        const char *interface = dbus_message_get_interface(msg);
+        const char *path = dbus_message_get_path(msg);
+        const char *method = dbus_message_get_member(msg);
+        dbus_bool_t no_reply = dbus_message_get_no_reply(msg);
+
         switch (dbus_message_get_type(msg)) {
             case DBUS_MESSAGE_TYPE_METHOD_CALL:
-                log_debug(LOG_DBUS, "Caught message call: %s.%s\n", dbus_message_get_interface(msg), dbus_message_get_member(msg));
+                log_debug(LOG_DBUS, "DBus method call [%s.%s] from [%s]\n", interface, method, sender);
                 if (dbus_message_has_interface(msg, "org.freedesktop.DBus.Introspectable")) {
                     if (dbus_message_has_member(msg, "Introspect")) {
-                        // Give introspection
+                        // FIXME: Give introspection
                     }
                 }
                 else if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_METHOD_CALL) {
-                    proc_fork(dbus_method_call, "ComarDBusJob", conn, msg);
+                    if (!check_interface_format(interface)) {
+                        log_error("Invalid interface: %s\n", interface);
+                        if (!no_reply) {
+                            reply = dbus_message_new_error(msg, DBUS_ERROR_FAILED, "Invalid interface");
+                            dbus_send(conn, reply);
+                        }
+                    }
+                    else if (!check_path_format(path)) {
+                        log_error("Invalid object path: %s\n", path);
+                        if (!no_reply) {
+                            reply = dbus_message_new_error(msg, DBUS_ERROR_FAILED, "Invalid object path");
+                            dbus_send(conn, reply);
+                        }
+                    }
+                    else {
+                        char *script_path = get_script_path(interface, path);
+                        if (!check_file(script_path)) {
+                            log_error("Interface/object path does not exist.\n", interface, path);
+                            if (!no_reply) {
+                                reply = dbus_message_new_error(msg, DBUS_ERROR_FAILED, "Interface/object path does not exist.");
+                                dbus_send(conn, reply);
+                            }
+                        }
+                        else {
+                            proc_fork(dbus_method_call, "ComarDBusJob", conn, msg);
+                        }
+                        free(script_path);
+                    }
                 }
                 break;
             case DBUS_MESSAGE_TYPE_SIGNAL:
-                args = dbus_py_import(msg);
-                log_debug(LOG_DBUS, "Caught signal: %s.%s\n", dbus_message_get_interface(msg), dbus_message_get_member(msg));
+                log_debug(LOG_DBUS, "DBus signal [%s.%s] from [%s]\n", interface, method, sender);
                 break;
         }
     }

Modified: branches/comar-dbus/src/main.c
=================================================================
--- branches/comar-dbus/src/main.c	(original)
+++ branches/comar-dbus/src/main.c	Mon Dec  3 14:15:28 2007
@@ -23,24 +23,31 @@
     struct ProcChild *p;
     int size;
 
+    // l10n
     setlocale(LC_MESSAGES, "");
     bindtextdomain("comar", "/usr/share/locale");
     bind_textdomain_codeset("comar", "UTF-8");
     bind_textdomain_codeset("libc", "UTF-8");
     textdomain("comar");
 
+    // Parse commandline options
     cfg_init(argc, argv);
 
+    // Only root can register system bus
     if (cfg_bus_type == DBUS_BUS_SYSTEM && getuid() != 0) {
         puts(_("System service should be started as root."));
         exit(1);
     }
 
+    // Initialize
     proc_init(argc, argv, "Comar");
 
+    // Start logging
     log_start();
 
+    // Listen for DBus calls
     proc_fork(dbus_listen, "ComarDBus", NULL, NULL);
+
     while (1) {
         if (shutdown_activated || my_proc.nr_children == 0) {
             proc_finish();

Modified: branches/comar-dbus/src/utility.c
=================================================================
--- branches/comar-dbus/src/utility.c	(original)
+++ branches/comar-dbus/src/utility.c	Mon Dec  3 14:15:28 2007
@@ -11,6 +11,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <time.h>
 #include <sys/stat.h>
 
 #include "cfg.h"
@@ -20,6 +21,13 @@
 const char *valid_interface_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.";
 const char *path_prefix = "/package/";
 
+int
+check_file(const char *fname)
+{
+    struct stat fs;
+    return (stat(fname, &fs) == 0);
+}
+
 unsigned char *
 load_file(const char *fname, int *sizeptr)
 {
@@ -68,6 +76,10 @@
 {
     int i;
 
+    if (interface == NULL) {
+        return 0;
+    }
+
     for (i = 0; i < strlen(interface); i++) {
         if (!in_str(interface[i], valid_interface_chars)) {
             return 0;
@@ -81,6 +93,10 @@
 {
     int i;
 
+    if (path == NULL) {
+        return 0;
+    }
+
     if (strncmp(path, path_prefix, strlen(path_prefix))) {
         return 0;
     }
@@ -140,3 +156,13 @@
     free(model);
     return realpath;
 }
+
+unsigned long
+time_diff(struct timeval *start, struct timeval *end)
+{
+    unsigned long msec;
+
+    msec = (end->tv_sec * 1000) + (end->tv_usec / 1000);
+    msec -= (start->tv_sec * 1000) + (start->tv_usec / 1000);
+    return msec;
+}


Uludag-commits mesaj listesiyle ilgili daha fazla bilgi