dbus: fix 64-bit compiler warnings
[platform/upstream/dbus.git] / dbus / dbus-userdb.c
index bfa4c43..10434bb 100644 (file)
@@ -20,6 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
+#include <config.h>
 #define DBUS_USERDB_INCLUDES_PRIVATE 1
 #include "dbus-userdb.h"
 #include "dbus-hash.h"
  * @{
  */
 
+static DBusUserInfo *
+_dbus_user_info_ref (DBusUserInfo *info)
+{
+  _dbus_assert (info->refcount > 0);
+  _dbus_assert (info->refcount < SIZE_MAX);
+  info->refcount++;
+  return info;
+}
+
 /**
- * Frees the given #DBusUserInfo's members with _dbus_user_info_free()
+ * Decrements the reference count. If it reaches 0,
+ * frees the given #DBusUserInfo's members with _dbus_user_info_free()
  * and also calls dbus_free() on the block itself
  *
  * @param info the info
  */
 void
-_dbus_user_info_free_allocated (DBusUserInfo *info)
+_dbus_user_info_unref (DBusUserInfo *info)
 {
   if (info == NULL) /* hash table will pass NULL */
     return;
 
+  _dbus_assert (info->refcount > 0);
+  _dbus_assert (info->refcount < SIZE_MAX);
+
+  if (--info->refcount > 0)
+    return;
+
   _dbus_user_info_free (info);
   dbus_free (info);
 }
 
 /**
- * Frees the given #DBusGroupInfo's members with _dbus_group_info_free()
+ * Decrements the reference count. If it reaches 0,
+ * frees the given #DBusGroupInfo's members with _dbus_group_info_free()
  * and also calls dbus_free() on the block itself
  *
  * @param info the info
  */
 void
-_dbus_group_info_free_allocated (DBusGroupInfo *info)
+_dbus_group_info_unref (DBusGroupInfo *info)
 {
   if (info == NULL) /* hash table will pass NULL */
     return;
 
+  _dbus_assert (info->refcount > 0);
+  _dbus_assert (info->refcount < SIZE_MAX);
+
+  if (--info->refcount > 0)
+    return;
+
   _dbus_group_info_free (info);
   dbus_free (info);
 }
@@ -121,9 +145,9 @@ _dbus_is_a_number (const DBusString *str,
  * @param uid the user ID or #DBUS_UID_UNSET
  * @param username username or #NULL 
  * @param error error to fill in
- * @returns the entry in the database
+ * @returns the entry in the database (borrowed, do not free)
  */
-DBusUserInfo*
+const DBusUserInfo *
 _dbus_user_database_lookup (DBusUserDatabase *db,
                             dbus_uid_t        uid,
                             const DBusString *username,
@@ -143,9 +167,8 @@ _dbus_user_database_lookup (DBusUserDatabase *db,
         uid = n;
     }
 
-#ifdef DBUS_ENABLE_USERDB_CACHE  
   if (uid != DBUS_UID_UNSET)
-    info = _dbus_hash_table_lookup_ulong (db->users, uid);
+    info = _dbus_hash_table_lookup_uintptr (db->users, uid);
   else
     info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
 
@@ -156,9 +179,6 @@ _dbus_user_database_lookup (DBusUserDatabase *db,
       return info;
     }
   else
-#else 
-  if (1)
-#endif
     {
       if (uid != DBUS_UID_UNSET)
        _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
@@ -173,13 +193,14 @@ _dbus_user_database_lookup (DBusUserDatabase *db,
           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
           return NULL;
         }
+      info->refcount = 1;
 
       if (uid != DBUS_UID_UNSET)
         {
           if (!_dbus_user_info_fill_uid (info, uid, error))
             {
               _DBUS_ASSERT_ERROR_IS_SET (error);
-              _dbus_user_info_free_allocated (info);
+              _dbus_user_info_unref (info);
               return NULL;
             }
         }
@@ -188,7 +209,7 @@ _dbus_user_database_lookup (DBusUserDatabase *db,
           if (!_dbus_user_info_fill (info, username, error))
             {
               _DBUS_ASSERT_ERROR_IS_SET (error);
-              _dbus_user_info_free_allocated (info);
+              _dbus_user_info_unref (info);
               return NULL;
             }
         }
@@ -198,22 +219,35 @@ _dbus_user_database_lookup (DBusUserDatabase *db,
       username = NULL;
 
       /* insert into hash */
-      if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
+      if (_dbus_hash_table_insert_uintptr (db->users, info->uid, info))
+        {
+          _dbus_user_info_ref (info);
+        }
+      else
         {
           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-          _dbus_user_info_free_allocated (info);
+          _dbus_user_info_unref (info);
           return NULL;
         }
 
-      if (!_dbus_hash_table_insert_string (db->users_by_name,
-                                           info->username,
-                                           info))
+      if (_dbus_hash_table_insert_string (db->users_by_name,
+                                          info->username,
+                                          info))
+        {
+          _dbus_user_info_ref (info);
+        }
+      else
         {
-          _dbus_hash_table_remove_ulong (db->users, info->uid);
+          _dbus_hash_table_remove_uintptr (db->users, info->uid);
           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+          _dbus_user_info_unref (info);
           return NULL;
         }
-      
+
+      _dbus_user_info_unref (info);
+
+      /* Return a borrowed pointer to the DBusUserInfo owned by the
+       * hash tables */
       return info;
     }
 }
@@ -263,7 +297,7 @@ init_system_db (void)
           else
             {
               /* This really should not happen. */
-              _dbus_warn ("Could not get password database information for UID of current process: %s\n",
+              _dbus_warn ("Could not get password database information for UID of current process: %s",
                           error.message);
               dbus_error_free (&error);
               return FALSE;
@@ -305,11 +339,18 @@ init_system_db (void)
 /**
  * Locks global system user database.
  */
-void
+dbus_bool_t
 _dbus_user_database_lock_system (void)
 {
-  _DBUS_LOCK (system_users);
-  database_locked = TRUE;
+  if (_DBUS_LOCK (system_users))
+    {
+      database_locked = TRUE;
+      return TRUE;
+    }
+  else
+    {
+      return FALSE;
+    }
 }
 
 /**
@@ -344,8 +385,12 @@ _dbus_user_database_get_system (void)
 void
 _dbus_user_database_flush_system (void)
 {
-  _dbus_user_database_lock_system ();
-   
+  if (!_dbus_user_database_lock_system ())
+    {
+      /* nothing to flush */
+      return;
+    }
+
    if (system_db != NULL)
     _dbus_user_database_flush (system_db);
 
@@ -362,7 +407,9 @@ _dbus_user_database_flush_system (void)
 dbus_bool_t
 _dbus_username_from_current_process (const DBusString **username)
 {
-  _dbus_user_database_lock_system ();
+  if (!_dbus_user_database_lock_system ())
+    return FALSE;
+
   if (!init_system_db ())
     {
       _dbus_user_database_unlock_system ();
@@ -384,7 +431,9 @@ _dbus_username_from_current_process (const DBusString **username)
 dbus_bool_t
 _dbus_homedir_from_current_process (const DBusString  **homedir)
 {
-  _dbus_user_database_lock_system ();
+  if (!_dbus_user_database_lock_system ())
+    return FALSE;
+
   if (!init_system_db ())
     {
       _dbus_user_database_unlock_system ();
@@ -409,7 +458,10 @@ _dbus_homedir_from_username (const DBusString *username,
 {
   DBusUserDatabase *db;
   const DBusUserInfo *info;
-  _dbus_user_database_lock_system ();
+
+  /* FIXME: this can't distinguish ENOMEM from other errors */
+  if (!_dbus_user_database_lock_system ())
+    return FALSE;
 
   db = _dbus_user_database_get_system ();
   if (db == NULL)
@@ -448,7 +500,20 @@ _dbus_homedir_from_uid (dbus_uid_t         uid,
 {
   DBusUserDatabase *db;
   const DBusUserInfo *info;
-  _dbus_user_database_lock_system ();
+
+  if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
+    {
+      const char *from_environment;
+
+      from_environment = _dbus_getenv ("HOME");
+
+      if (from_environment != NULL)
+        return _dbus_string_append (homedir, from_environment);
+    }
+
+  /* FIXME: this can't distinguish ENOMEM from other errors */
+  if (!_dbus_user_database_lock_system ())
+    return FALSE;
 
   db = _dbus_user_database_get_system ();
   if (db == NULL)
@@ -495,7 +560,9 @@ _dbus_credentials_add_from_user (DBusCredentials  *credentials,
   DBusUserDatabase *db;
   const DBusUserInfo *info;
 
-  _dbus_user_database_lock_system ();
+  /* FIXME: this can't distinguish ENOMEM from other errors */
+  if (!_dbus_user_database_lock_system ())
+    return FALSE;
 
   db = _dbus_user_database_get_system ();
   if (db == NULL)
@@ -537,25 +604,25 @@ _dbus_user_database_new (void)
 
   db->refcount = 1;
 
-  db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
-                                    NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
+  db->users = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
+                                    NULL, (DBusFreeFunction) _dbus_user_info_unref);
   
   if (db->users == NULL)
     goto failed;
 
-  db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
-                                     NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
+  db->groups = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
+                                     NULL, (DBusFreeFunction) _dbus_group_info_unref);
   
   if (db->groups == NULL)
     goto failed;
 
   db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
-                                            NULL, NULL);
+                                            NULL, (DBusFreeFunction) _dbus_user_info_unref);
   if (db->users_by_name == NULL)
     goto failed;
   
   db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
-                                             NULL, NULL);
+                                             NULL, (DBusFreeFunction) _dbus_group_info_unref);
   if (db->groups_by_name == NULL)
     goto failed;
   
@@ -578,7 +645,7 @@ _dbus_user_database_flush (DBusUserDatabase *db)
   _dbus_hash_table_remove_all(db->groups);
 }
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 /**
  * Increments refcount of user database.
  * @param db the database
@@ -593,7 +660,7 @@ _dbus_user_database_ref (DBusUserDatabase  *db)
 
   return db;
 }
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
 
 /**
  * Decrements refcount of user database.