summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-internals.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2009-08-07 01:33:14 +0200
committerLennart Poettering <lennart@poettering.net>2009-10-17 00:28:31 +0200
commit1f179e0f27f656ed3c5d1ee2ac5e1bbfe26d9d94 (patch)
treef239fdaf9e72954e153d33b88e0c1fd2a04d90bf /dbus/dbus-internals.c
parent2ed34f69d4400f863835c4149be4ecb82ffe1c72 (diff)
desktop-file: fix stat() race
_dbus_desktop_file_load() used to stat the desktop file before reading it, to verify its size. This is both racy and unnecessary since _dbus_file_get_contents() which it uses stats the file anyway -- does that however in a race-free fashion with fstat() instead of stat(). This patch gets rid of the redundant stat(). Also, since the desktop file change logic requires the mtime of the file it read we now export that in _dbus_file_get_contents(). This patch probably breaks Win32 builds, but afaik those are broken anyway.
Diffstat (limited to 'dbus/dbus-internals.c')
-rw-r--r--dbus/dbus-internals.c90
1 files changed, 45 insertions, 45 deletions
diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c
index c5885eae..56b1eec0 100644
--- a/dbus/dbus-internals.c
+++ b/dbus/dbus-internals.c
@@ -4,7 +4,7 @@
* Copyright (C) 2002, 2003 Red Hat, Inc.
*
* Licensed under the Academic Free License version 2.1
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
@@ -14,7 +14,7 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
@@ -32,7 +32,7 @@
/**
* @defgroup DBusInternals D-Bus secret internal implementation details
* @brief Documentation useful when developing or debugging D-Bus itself.
- *
+ *
*/
/**
@@ -46,7 +46,7 @@
* @def _dbus_assert
*
* Aborts with an error message if the condition is false.
- *
+ *
* @param condition condition which must be true.
*/
@@ -55,7 +55,7 @@
*
* Aborts with an error message if called.
* The given explanation will be printed.
- *
+ *
* @param explanation explanation of what happened if the code was reached.
*/
@@ -144,7 +144,7 @@
/**
* @typedef DBusForeachFunction
- *
+ *
* Used to iterate over each item in a collection, such as
* a DBusList.
*/
@@ -242,7 +242,7 @@ _dbus_warn (const char *format,
if (!warn_initted)
init_warnings ();
-
+
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
@@ -267,12 +267,12 @@ _dbus_warn_check_failed(const char *format,
...)
{
va_list args;
-
+
if (!warn_initted)
init_warnings ();
fprintf (stderr, "process %lu: ", _dbus_pid_for_log ());
-
+
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
@@ -304,7 +304,7 @@ _dbus_verbose_init (void)
{
if (!verbose_initted)
{
- const char *p = _dbus_getenv ("DBUS_VERBOSE");
+ const char *p = _dbus_getenv ("DBUS_VERBOSE");
verbose = p != NULL && *p == '1';
verbose_initted = TRUE;
}
@@ -337,7 +337,7 @@ _dbus_verbose_real (const char *format,
va_list args;
static dbus_bool_t need_pid = TRUE;
int len;
-
+
/* things are written a bit oddly here so that
* in the non-verbose case we just have the one
* conditional and return immediately.
@@ -354,7 +354,7 @@ _dbus_verbose_real (const char *format,
fprintf (stderr, "%lu: ", _dbus_pid_for_log ());
#endif
}
-
+
/* Only print pid again if the next line is a new line */
len = strlen (format);
@@ -362,7 +362,7 @@ _dbus_verbose_real (const char *format,
need_pid = TRUE;
else
need_pid = FALSE;
-
+
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
@@ -388,7 +388,7 @@ _dbus_verbose_reset_real (void)
* Duplicates a string. Result must be freed with
* dbus_free(). Returns #NULL if memory allocation fails.
* If the string to be duplicated is #NULL, returns #NULL.
- *
+ *
* @param str string to duplicate.
* @returns newly-allocated copy.
*/
@@ -397,10 +397,10 @@ _dbus_strdup (const char *str)
{
size_t len;
char *copy;
-
+
if (str == NULL)
return NULL;
-
+
len = strlen (str);
copy = dbus_malloc (len + 1);
@@ -408,7 +408,7 @@ _dbus_strdup (const char *str)
return NULL;
memcpy (copy, str, len + 1);
-
+
return copy;
}
@@ -431,7 +431,7 @@ _dbus_memdup (const void *mem,
return NULL;
memcpy (copy, mem, n_bytes);
-
+
return copy;
}
@@ -439,7 +439,7 @@ _dbus_memdup (const void *mem,
* Duplicates a string array. Result may be freed with
* dbus_free_string_array(). Returns #NULL if memory allocation fails.
* If the array to be duplicated is #NULL, returns #NULL.
- *
+ *
* @param array array to duplicate.
* @returns newly-allocated copy.
*/
@@ -449,7 +449,7 @@ _dbus_dup_string_array (const char **array)
int len;
int i;
char **copy;
-
+
if (array == NULL)
return NULL;
@@ -478,7 +478,7 @@ _dbus_dup_string_array (const char **array)
/**
* Checks whether a string array contains the given string.
- *
+ *
* @param array array to search.
* @param str string to look for
* @returns #TRUE if array contains string
@@ -514,7 +514,7 @@ _dbus_generate_uuid (DBusGUID *uuid)
_dbus_get_current_time (&now, NULL);
uuid->as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now);
-
+
_dbus_generate_random_bytes_buffer (uuid->as_bytes, DBUS_UUID_LENGTH_BYTES - 4);
}
@@ -542,7 +542,7 @@ _dbus_read_uuid_file_without_creating (const DBusString *filename,
DBusString contents;
DBusString decoded;
int end;
-
+
if (!_dbus_string_init (&contents))
{
_DBUS_SET_OOM (error);
@@ -555,8 +555,8 @@ _dbus_read_uuid_file_without_creating (const DBusString *filename,
_DBUS_SET_OOM (error);
return FALSE;
}
-
- if (!_dbus_file_get_contents (&contents, filename, error))
+
+ if (!_dbus_file_get_contents (&contents, filename, NULL, error))
goto error;
_dbus_string_chop_white (&contents);
@@ -603,7 +603,7 @@ _dbus_read_uuid_file_without_creating (const DBusString *filename,
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return TRUE;
-
+
error:
_DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_string_free (&contents);
@@ -625,13 +625,13 @@ _dbus_create_uuid_file_exclusively (const DBusString *filename,
}
_dbus_generate_uuid (uuid);
-
+
if (!_dbus_uuid_encode (uuid, &encoded))
{
_DBUS_SET_OOM (error);
goto error;
}
-
+
/* FIXME this is racy; we need a save_file_exclusively
* function. But in practice this should be fine for now.
*
@@ -648,7 +648,7 @@ _dbus_create_uuid_file_exclusively (const DBusString *filename,
_DBUS_SET_OOM (error);
goto error;
}
-
+
if (!_dbus_string_save_to_file (&encoded, filename, error))
goto error;
@@ -659,11 +659,11 @@ _dbus_create_uuid_file_exclusively (const DBusString *filename,
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return TRUE;
-
+
error:
_DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_string_free (&encoded);
- return FALSE;
+ return FALSE;
}
/**
@@ -719,7 +719,7 @@ static DBusGUID machine_uuid;
* machine at least until it next reboots, though it also
* makes some effort to be the same forever, it may change if the
* machine is reconfigured or its hardware is modified.
- *
+ *
* @param uuid_str string to append hex-encoded machine uuid to
* @returns #FALSE if no memory
*/
@@ -727,7 +727,7 @@ dbus_bool_t
_dbus_get_local_machine_uuid_encoded (DBusString *uuid_str)
{
dbus_bool_t ok;
-
+
_DBUS_LOCK (machine_uuid);
if (machine_uuid_initialized_generation != _dbus_current_generation)
{
@@ -735,7 +735,7 @@ _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str)
if (!_dbus_read_local_machine_uuid (&machine_uuid, FALSE,
&error))
- {
+ {
#ifndef DBUS_BUILD_TESTS
/* For the test suite, we may not be installed so just continue silently
* here. But in a production build, we want to be nice and loud about
@@ -745,9 +745,9 @@ _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str)
"See the manual page for dbus-uuidgen to correct this issue.\n",
error.message);
#endif
-
+
dbus_error_free (&error);
-
+
_dbus_generate_uuid (&machine_uuid);
}
}
@@ -850,7 +850,7 @@ _dbus_real_assert_not_reached (const char *explanation,
_dbus_abort ();
}
#endif /* DBUS_DISABLE_ASSERT */
-
+
#ifdef DBUS_BUILD_TESTS
static dbus_bool_t
run_failing_each_malloc (int n_mallocs,
@@ -859,9 +859,9 @@ run_failing_each_malloc (int n_mallocs,
void *data)
{
n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
-
+
while (n_mallocs >= 0)
- {
+ {
_dbus_set_fail_alloc_counter (n_mallocs);
_dbus_verbose ("\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
@@ -870,14 +870,14 @@ run_failing_each_malloc (int n_mallocs,
if (!(* func) (data))
return FALSE;
-
+
n_mallocs -= 1;
}
_dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
return TRUE;
-}
+}
/**
* Tests how well the given function responds to out-of-memory
@@ -903,14 +903,14 @@ _dbus_test_oom_handling (const char *description,
int i;
/* Run once to see about how many mallocs are involved */
-
+
_dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
_dbus_verbose ("Running once to count mallocs\n");
-
+
if (!(* func) (data))
return FALSE;
-
+
approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
_dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
@@ -940,7 +940,7 @@ _dbus_test_oom_handling (const char *description,
return FALSE;
++i;
}
-
+
_dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
description);