[daemon-dev][lib-opt] Fix in freeing memory, cleanup, comments, renames
[platform/upstream/dbus.git] / bus / connection.c
index d74b326..1f2c955 100644 (file)
@@ -2,6 +2,7 @@
 /* connection.c  Client connections
  *
  * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2013  Samsung Electronics
  *
  * Licensed under the Academic Free License version 2.1
  * 
@@ -62,6 +63,16 @@ struct BusConnections
   DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
   int stamp;                   /**< Incrementing number */
   BusExpireList *pending_replies; /**< List of pending replies */
+
+#ifdef DBUS_ENABLE_STATS
+  int total_match_rules;
+  int peak_match_rules;
+  int peak_match_rules_per_conn;
+
+  int total_bus_names;
+  int peak_bus_names;
+  int peak_bus_names_per_conn;
+#endif
 };
 
 static dbus_int32_t connection_data_slot = -1;
@@ -87,6 +98,11 @@ typedef struct
   long connection_tv_sec;  /**< Time when we connected (seconds component) */
   long connection_tv_usec; /**< Time when we connected (microsec component) */
   int stamp;               /**< connections->stamp last time we were traversed */
+
+#ifdef DBUS_ENABLE_STATS
+  int peak_match_rules;
+  int peak_bus_names;
+#endif
 } BusConnectionData;
 
 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
@@ -175,6 +191,9 @@ bus_connection_disconnected (DBusConnection *connection)
   BusConnectionData *d;
   BusService *service;
   BusMatchmaker *matchmaker;
+#ifdef ENABLE_KDBUS_TRANSPORT
+  dbus_bool_t is_phantom = FALSE;
+#endif
   
   d = BUS_CONNECTION_DATA (connection);
   _dbus_assert (d != NULL);
@@ -182,6 +201,11 @@ bus_connection_disconnected (DBusConnection *connection)
   _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
                  d->name ? d->name : "(inactive)");
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(bus_context_is_kdbus(d->connections->context) && (strcmp(bus_connection_get_name(connection), ":1.1")))
+      is_phantom = TRUE;
+#endif
+
   /* Delete our match rules */
   if (d->n_match_rules > 0)
     {
@@ -290,8 +314,13 @@ bus_connection_disconnected (DBusConnection *connection)
   dbus_connection_set_data (connection,
                             connection_data_slot,
                             NULL, NULL);
-  
-  dbus_connection_unref (connection);
+
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(is_phantom)
+      dbus_connection_unref_phantom(connection);
+  else
+#endif
+      dbus_connection_unref (connection);
 }
 
 static dbus_bool_t
@@ -312,6 +341,15 @@ remove_connection_watch (DBusWatch      *watch,
   _dbus_loop_remove_watch (connection_get_loop (connection), watch);
 }
 
+static void
+toggle_connection_watch (DBusWatch      *watch,
+                         void           *data)
+{
+  DBusConnection *connection = data;
+
+  _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
+}
+
 static dbus_bool_t
 add_connection_timeout (DBusTimeout    *timeout,
                         void           *data)
@@ -388,14 +426,6 @@ free_connection_data (void *data)
   dbus_free (d);
 }
 
-static void
-call_timeout_callback (DBusTimeout   *timeout,
-                       void          *data)
-{
-  /* can return FALSE on OOM but we just let it fire again later */
-  dbus_timeout_handle (timeout);
-}
-
 BusConnections*
 bus_connections_new (BusContext *context)
 {
@@ -590,8 +620,8 @@ bus_connections_setup_connection (BusConnections *connections,
   d->connections = connections;
   d->connection = connection;
   
-  _dbus_get_current_time (&d->connection_tv_sec,
-                          &d->connection_tv_usec);
+  _dbus_get_monotonic_time (&d->connection_tv_sec,
+                            &d->connection_tv_usec);
   
   _dbus_assert (connection_data_slot >= 0);
   
@@ -624,7 +654,7 @@ bus_connections_setup_connection (BusConnections *connections,
   if (!dbus_connection_set_watch_functions (connection,
                                             add_connection_watch,
                                             remove_connection_watch,
-                                            NULL,
+                                            toggle_connection_watch,
                                             connection,
                                             NULL))
     goto out;
@@ -760,7 +790,7 @@ bus_connections_expire_incomplete (BusConnections *connections)
       DBusList *link;
       int auth_timeout;
       
-      _dbus_get_current_time (&tv_sec, &tv_usec);
+      _dbus_get_monotonic_time (&tv_sec, &tv_usec);
       auth_timeout = bus_context_get_auth_timeout (connections->context);
   
       link = _dbus_list_get_first_link (&connections->incomplete);
@@ -823,12 +853,7 @@ bus_connection_get_unix_groups  (DBusConnection   *connection,
                                  int              *n_groups,
                                  DBusError        *error)
 {
-  BusConnectionData *d;
   unsigned long uid;
-  
-  d = BUS_CONNECTION_DATA (connection);
-
-  _dbus_assert (d != NULL);
 
   *groups = NULL;
   *n_groups = 0;
@@ -984,6 +1009,28 @@ bus_connections_foreach (BusConnections               *connections,
   foreach_inactive (connections, function, data);
 }
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+DBusConnection*
+bus_connections_find_conn_by_name(BusConnections *connections, const char* name)
+{
+    DBusList *link;
+
+    link = _dbus_list_get_first_link (&connections->completed);
+    while (link != NULL)
+      {
+        DBusConnection *connection = link->data;
+        DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
+
+        if (!strcmp(bus_connection_get_name(connection), name))
+          return connection;
+
+        link = next;
+      }
+
+    return NULL;
+}
+#endif
+
 BusContext*
 bus_connections_get_context (BusConnections *connections)
 {
@@ -1191,6 +1238,16 @@ bus_connection_send_oom_error (DBusConnection *connection,
   d->oom_preallocated = NULL;
 }
 
+#ifdef DBUS_ENABLE_STATS
+static void
+update_peak (int *peak,
+             int n)
+{
+  if (*peak < n)
+    *peak = n;
+}
+#endif
+
 void
 bus_connection_add_match_rule_link (DBusConnection *connection,
                                     DBusList       *link)
@@ -1203,6 +1260,15 @@ bus_connection_add_match_rule_link (DBusConnection *connection,
   _dbus_list_append_link (&d->match_rules, link);
 
   d->n_match_rules += 1;
+
+#ifdef DBUS_ENABLE_STATS
+  update_peak (&d->peak_match_rules, d->n_match_rules);
+  update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
+
+  d->connections->total_match_rules += 1;
+  update_peak (&d->connections->peak_match_rules,
+               d->connections->total_match_rules);
+#endif
 }
 
 dbus_bool_t
@@ -1234,6 +1300,10 @@ bus_connection_remove_match_rule (DBusConnection *connection,
 
   d->n_match_rules -= 1;
   _dbus_assert (d->n_match_rules >= 0);
+
+#ifdef DBUS_ENABLE_STATS
+  d->connections->total_match_rules -= 1;
+#endif
 }
 
 int
@@ -1259,6 +1329,16 @@ bus_connection_add_owned_service_link (DBusConnection *connection,
   _dbus_list_append_link (&d->services_owned, link);
 
   d->n_services_owned += 1;
+
+#ifdef DBUS_ENABLE_STATS
+  update_peak (&d->peak_bus_names, d->n_services_owned);
+  update_peak (&d->connections->peak_bus_names_per_conn,
+               d->n_services_owned);
+
+  d->connections->total_bus_names += 1;
+  update_peak (&d->connections->peak_bus_names,
+               d->connections->total_bus_names);
+#endif
 }
 
 dbus_bool_t
@@ -1290,6 +1370,10 @@ bus_connection_remove_owned_service (DBusConnection *connection,
 
   d->n_services_owned -= 1;
   _dbus_assert (d->n_services_owned >= 0);
+
+#ifdef DBUS_ENABLE_STATS
+  d->connections->total_bus_names -= 1;
+#endif
 }
 
 int
@@ -1303,6 +1387,18 @@ bus_connection_get_n_services_owned (DBusConnection *connection)
   return d->n_services_owned;
 }
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+DBusList**
+bus_connection_get_services_owned (DBusConnection *connection)
+{
+    BusConnectionData *d;
+    d = BUS_CONNECTION_DATA (connection);
+    _dbus_assert (d != NULL);
+
+    return &d->services_owned;
+}
+#endif
+
 dbus_bool_t
 bus_connection_complete (DBusConnection   *connection,
                         const DBusString *name,
@@ -1407,13 +1503,7 @@ bus_connections_check_limits (BusConnections  *connections,
                               DBusConnection  *requesting_completion,
                               DBusError       *error)
 {
-  BusConnectionData *d;
   unsigned long uid;
-  
-  d = BUS_CONNECTION_DATA (requesting_completion);
-  _dbus_assert (d != NULL);
-
-  _dbus_assert (d->name == NULL);
 
   if (connections->n_completed >=
       bus_context_get_max_completed_connections (connections->context))
@@ -1730,8 +1820,8 @@ bus_connections_expect_reply (BusConnections  *connections,
   cprd->pending = pending;
   cprd->connections = connections;
   
-  _dbus_get_current_time (&pending->expire_item.added_tv_sec,
-                          &pending->expire_item.added_tv_usec);
+  _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
+                            &pending->expire_item.added_tv_usec);
 
   _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
                  pending,
@@ -1974,7 +2064,10 @@ bus_transaction_send_from_driver (BusTransaction *transaction,
   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
     return FALSE;
 
-  if (bus_connection_is_active (connection))
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* we can't set destination on the basis of connection when on kdbus*/
+#endif
+    if (bus_connection_is_active (connection))
     {
       if (!dbus_message_set_destination (message,
                                          bus_connection_get_name (connection)))
@@ -2273,3 +2366,71 @@ bus_transaction_add_cancel_hook (BusTransaction               *transaction,
 
   return TRUE;
 }
+
+#ifdef DBUS_ENABLE_STATS
+int
+bus_connections_get_n_active (BusConnections *connections)
+{
+  return connections->n_completed;
+}
+
+int
+bus_connections_get_n_incomplete (BusConnections *connections)
+{
+  return connections->n_incomplete;
+}
+
+int
+bus_connections_get_total_match_rules (BusConnections *connections)
+{
+  return connections->total_match_rules;
+}
+
+int
+bus_connections_get_peak_match_rules (BusConnections *connections)
+{
+  return connections->peak_match_rules;
+}
+
+int
+bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
+{
+  return connections->peak_match_rules_per_conn;
+}
+
+int
+bus_connections_get_total_bus_names (BusConnections *connections)
+{
+  return connections->total_bus_names;
+}
+
+int
+bus_connections_get_peak_bus_names (BusConnections *connections)
+{
+  return connections->peak_bus_names;
+}
+
+int
+bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
+{
+  return connections->peak_bus_names_per_conn;
+}
+
+int
+bus_connection_get_peak_match_rules (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  return d->peak_match_rules;
+}
+
+int
+bus_connection_get_peak_bus_names (DBusConnection *connection)
+{
+  BusConnectionData *d;
+
+  d = BUS_CONNECTION_DATA (connection);
+  return d->peak_bus_names;
+}
+#endif /* DBUS_ENABLE_STATS */