~ubuntu-branches/ubuntu/maverick/dbus/maverick

« back to all changes in this revision

Viewing changes to dbus/dbus-internals.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-27 13:06:32 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20100927130632-bqs145trvchd2lmf
Tags: 1.4.0-0ubuntu1
* New upstream release
 - Fixes https://bugs.freedesktop.org/show_bug.cgi?id=17754 Race condition in protected_change_timeout
 - Requested by various upstream KDE developers http://lists.kde.org/?t=128514970000004&r=1&w=2

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
21
 *
22
22
 */
 
23
 
 
24
#include <config.h>
23
25
#include "dbus-internals.h"
24
26
#include "dbus-protocol.h"
25
27
#include "dbus-marshal-basic.h"
28
30
#include <stdarg.h>
29
31
#include <string.h>
30
32
#include <stdlib.h>
 
33
#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
 
34
#include <windows.h>
 
35
#include <mbstring.h>
 
36
#endif
31
37
 
32
38
/**
33
39
 * @defgroup DBusInternals D-Bus secret internal implementation details
189
195
 * making up a different string every time and wasting
190
196
 * space.
191
197
 */
192
 
const char _dbus_no_memory_message[] = "Not enough memory";
 
198
const char *_dbus_no_memory_message = "Not enough memory";
193
199
 
194
200
static dbus_bool_t warn_initted = FALSE;
195
201
static dbus_bool_t fatal_warnings = FALSE;
295
301
#include <pthread.h>
296
302
#endif
297
303
 
298
 
#ifdef DBUS_WIN
 
304
#ifdef _MSC_VER
299
305
#define inline
300
306
#endif
 
307
#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
 
308
static char module_name[1024];
 
309
#endif
301
310
 
302
311
static inline void
303
312
_dbus_verbose_init (void)
304
313
{
305
314
  if (!verbose_initted)
306
315
    {
307
 
      const char *p = _dbus_getenv ("DBUS_VERBOSE"); 
 
316
      const char *p = _dbus_getenv ("DBUS_VERBOSE");
308
317
      verbose = p != NULL && *p == '1';
309
318
      verbose_initted = TRUE;
310
 
    }
 
319
#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
 
320
      {
 
321
        char *last_period, *last_slash;
 
322
        GetModuleFileName(0,module_name,sizeof(module_name)-1);
 
323
        last_period = _mbsrchr(module_name,'.');
 
324
        if (last_period)
 
325
          *last_period ='\0';
 
326
        last_slash = _mbsrchr(module_name,'\\');
 
327
        if (last_slash)
 
328
          strcpy(module_name,last_slash+1);
 
329
        strcat(module_name,": ");
 
330
      }
 
331
#endif
 
332
    }
 
333
}
 
334
 
 
335
/** @def DBUS_IS_DIR_SEPARATOR(c)
 
336
 * macro for checking if character c is a patch separator
 
337
 * 
 
338
 * @todo move to a header file so that others can use this too
 
339
 */
 
340
#ifdef DBUS_WIN 
 
341
#define DBUS_IS_DIR_SEPARATOR(c) (c == '\\' || c == '/')
 
342
#else
 
343
#define DBUS_IS_DIR_SEPARATOR(c) (c == '/')
 
344
#endif
 
345
 
 
346
/** 
 
347
 remove source root from file path 
 
348
 the source root is determined by 
 
349
*/ 
 
350
static char *_dbus_file_path_extract_elements_from_tail(const char *file,int level)
 
351
{
 
352
  static int prefix = -1;
 
353
  char *p;
 
354
 
 
355
  if (prefix == -1) 
 
356
    {
 
357
      char *p = (char *)file + strlen(file);
 
358
      int i = 0;
 
359
      prefix = 0;
 
360
      for (;p >= file;p--)
 
361
        {
 
362
          if (DBUS_IS_DIR_SEPARATOR(*p))
 
363
            {
 
364
              if (++i >= level) 
 
365
                {
 
366
                  prefix = p-file+1;
 
367
                  break;
 
368
                }
 
369
           }
 
370
        }
 
371
    }
 
372
  return (char *)file+prefix;
311
373
}
312
374
 
313
375
/**
331
393
 * @param format printf-style format string.
332
394
 */
333
395
void
 
396
#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
 
397
_dbus_verbose_real (const char *file, 
 
398
                    const int line, 
 
399
                    const char *function, 
 
400
                    const char *format,
 
401
#else
334
402
_dbus_verbose_real (const char *format,
 
403
#endif
335
404
                    ...)
336
405
{
337
406
  va_list args;
345
414
  if (!_dbus_is_verbose_real())
346
415
    return;
347
416
 
 
417
#ifndef DBUS_USE_OUTPUT_DEBUG_STRING
348
418
  /* Print out pid before the line */
349
419
  if (need_pid)
350
420
    {
354
424
      fprintf (stderr, "%lu: ", _dbus_pid_for_log ());
355
425
#endif
356
426
    }
357
 
      
 
427
#endif
358
428
 
359
429
  /* Only print pid again if the next line is a new line */
360
430
  len = strlen (format);
362
432
    need_pid = TRUE;
363
433
  else
364
434
    need_pid = FALSE;
365
 
  
 
435
 
366
436
  va_start (args, format);
 
437
#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
 
438
  {
 
439
  char buf[1024];
 
440
  strcpy(buf,module_name);
 
441
#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
 
442
  sprintf (buf+strlen(buf), "[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,function);
 
443
#endif
 
444
  vsprintf (buf+strlen(buf),format, args);
 
445
  va_end (args);
 
446
  OutputDebugStringA(buf);
 
447
  }
 
448
#else
 
449
#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
 
450
  fprintf (stderr, "[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,function);
 
451
#endif
 
452
 
367
453
  vfprintf (stderr, format, args);
368
454
  va_end (args);
369
455
 
370
456
  fflush (stderr);
 
457
#endif
371
458
}
372
459
 
373
460
/**
632
719
      goto error;
633
720
    }
634
721
  
635
 
  /* FIXME this is racy; we need a save_file_exclusively
636
 
   * function. But in practice this should be fine for now.
637
 
   *
638
 
   * - first be sure we can create the file and it
639
 
   *   doesn't exist by creating it empty with O_EXCL
640
 
   * - then create it by creating a temporary file and
641
 
   *   overwriting atomically with rename()
642
 
   */
643
 
  if (!_dbus_create_file_exclusively (filename, error))
644
 
    goto error;
645
 
 
646
722
  if (!_dbus_string_append_byte (&encoded, '\n'))
647
723
    {
648
724
      _DBUS_SET_OOM (error);
649
725
      goto error;
650
726
    }
651
727
  
652
 
  if (!_dbus_string_save_to_file (&encoded, filename, error))
653
 
    goto error;
654
 
 
655
 
  if (!_dbus_make_file_world_readable (filename, error))
 
728
  if (!_dbus_string_save_to_file (&encoded, filename, TRUE, error))
656
729
    goto error;
657
730
 
658
731
  _dbus_string_free (&encoded);
797
870
 
798
871
#ifndef DBUS_DISABLE_CHECKS
799
872
/** String used in _dbus_return_if_fail macro */
800
 
const char _dbus_return_if_fail_warning_format[] =
 
873
const char *_dbus_return_if_fail_warning_format =
801
874
"arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
802
875
"This is normally a bug in some application using the D-Bus library.\n";
803
876
#endif