D-Bus  1.8.22
dbus-sysdeps-win.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
3  *
4  * Copyright (C) 2002, 2003 Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  * Copyright (C) 2005 Novell, Inc.
7  * Copyright (C) 2006 Peter Kümmel <syntheticpp@gmx.net>
8  * Copyright (C) 2006 Christian Ehrlicher <ch.ehrlicher@gmx.de>
9  * Copyright (C) 2006-2013 Ralf Habacker <ralf.habacker@freenet.de>
10  *
11  * Licensed under the Academic Free License version 2.1
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  *
27  */
28 
29 #include <config.h>
30 
31 #define STRSAFE_NO_DEPRECATE
32 
33 #ifndef DBUS_WINCE
34 #ifndef _WIN32_WINNT
35 #define _WIN32_WINNT 0x0501
36 #endif
37 #endif
38 
39 #include "dbus-internals.h"
40 #include "dbus-sha.h"
41 #include "dbus-sysdeps.h"
42 #include "dbus-threads.h"
43 #include "dbus-protocol.h"
44 #include "dbus-string.h"
45 #include "dbus-sysdeps.h"
46 #include "dbus-sysdeps-win.h"
47 #include "dbus-protocol.h"
48 #include "dbus-hash.h"
49 #include "dbus-sockets-win.h"
50 #include "dbus-list.h"
51 #include "dbus-nonce.h"
52 #include "dbus-credentials.h"
53 
54 #include <windows.h>
55 #include <ws2tcpip.h>
56 #include <wincrypt.h>
57 #include <iphlpapi.h>
58 
59 /* Declarations missing in mingw's and windows sdk 7.0 headers */
60 extern BOOL WINAPI ConvertStringSidToSidA (LPCSTR StringSid, PSID *Sid);
61 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
62 
63 #include <stdio.h>
64 #include <stdlib.h>
65 
66 #include <string.h>
67 #if HAVE_ERRNO_H
68 #include <errno.h>
69 #endif
70 #ifndef DBUS_WINCE
71 #include <mbstring.h>
72 #include <sys/stat.h>
73 #include <sys/types.h>
74 #endif
75 
76 #ifdef HAVE_WS2TCPIP_H
77 /* getaddrinfo for Windows CE (and Windows). */
78 #include <ws2tcpip.h>
79 #endif
80 
81 #ifndef O_BINARY
82 #define O_BINARY 0
83 #endif
84 
85 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
86 /* MinGW32 < 4 does not define this value in its headers */
87 #define PROCESS_QUERY_LIMITED_INFORMATION (0x1000)
88 #endif
89 
90 typedef int socklen_t;
91 
92 
93 void
94 _dbus_win_set_errno (int err)
95 {
96 #ifdef DBUS_WINCE
97  SetLastError (err);
98 #else
99  errno = err;
100 #endif
101 }
102 
103 static BOOL is_winxp_sp3_or_lower();
104 
105 /*
106  * _MIB_TCPROW_EX and friends are not available in system headers
107  * and are mapped to attribute identical ...OWNER_PID typedefs.
108  */
109 typedef MIB_TCPROW_OWNER_PID _MIB_TCPROW_EX;
110 typedef MIB_TCPTABLE_OWNER_PID MIB_TCPTABLE_EX;
111 typedef PMIB_TCPTABLE_OWNER_PID PMIB_TCPTABLE_EX;
112 typedef DWORD (WINAPI *ProcAllocateAndGetTcpExtTableFromStack)(PMIB_TCPTABLE_EX*,BOOL,HANDLE,DWORD,DWORD);
113 static ProcAllocateAndGetTcpExtTableFromStack lpfnAllocateAndGetTcpExTableFromStack = NULL;
114 
120 static BOOL
121 load_ex_ip_helper_procedures(void)
122 {
123  HMODULE hModule = LoadLibrary ("iphlpapi.dll");
124  if (hModule == NULL)
125  {
126  _dbus_verbose ("could not load iphlpapi.dll\n");
127  return FALSE;
128  }
129 
130  lpfnAllocateAndGetTcpExTableFromStack = (ProcAllocateAndGetTcpExtTableFromStack)GetProcAddress (hModule, "AllocateAndGetTcpExTableFromStack");
131  if (lpfnAllocateAndGetTcpExTableFromStack == NULL)
132  {
133  _dbus_verbose ("could not find function AllocateAndGetTcpExTableFromStack in iphlpapi.dll\n");
134  return FALSE;
135  }
136  return TRUE;
137 }
138 
145 static dbus_pid_t
146 get_pid_from_extended_tcp_table(int peer_port)
147 {
148  dbus_pid_t result;
149  DWORD errorCode, size = 0, i;
150  MIB_TCPTABLE_OWNER_PID *tcp_table;
151 
152  if ((errorCode =
153  GetExtendedTcpTable (NULL, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == ERROR_INSUFFICIENT_BUFFER)
154  {
155  tcp_table = (MIB_TCPTABLE_OWNER_PID *) dbus_malloc (size);
156  if (tcp_table == NULL)
157  {
158  _dbus_verbose ("Error allocating memory\n");
159  return 0;
160  }
161  }
162  else
163  {
164  _dbus_win_warn_win_error ("unexpected error returned from GetExtendedTcpTable", errorCode);
165  return 0;
166  }
167 
168  if ((errorCode = GetExtendedTcpTable (tcp_table, &size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) != NO_ERROR)
169  {
170  _dbus_verbose ("Error fetching tcp table %d\n", (int)errorCode);
171  dbus_free (tcp_table);
172  return 0;
173  }
174 
175  result = 0;
176  for (i = 0; i < tcp_table->dwNumEntries; i++)
177  {
178  MIB_TCPROW_OWNER_PID *p = &tcp_table->table[i];
179  int local_address = ntohl (p->dwLocalAddr);
180  int local_port = ntohs (p->dwLocalPort);
181  if (p->dwState == MIB_TCP_STATE_ESTAB
182  && local_address == INADDR_LOOPBACK && local_port == peer_port)
183  result = p->dwOwningPid;
184  }
185 
186  dbus_free (tcp_table);
187  _dbus_verbose ("got pid %lu\n", result);
188  return result;
189 }
190 
198 static dbus_pid_t
199 get_pid_from_tcp_ex_table(int peer_port)
200 {
201  dbus_pid_t result;
202  DWORD errorCode, i;
203  PMIB_TCPTABLE_EX tcp_table = NULL;
204 
205  if (!load_ex_ip_helper_procedures ())
206  {
207  _dbus_verbose
208  ("Error not been able to load iphelper procedures\n");
209  return 0;
210  }
211 
212  errorCode = lpfnAllocateAndGetTcpExTableFromStack (&tcp_table, TRUE, GetProcessHeap(), 0, 2);
213 
214  if (errorCode != NO_ERROR)
215  {
216  _dbus_verbose
217  ("Error not been able to call AllocateAndGetTcpExTableFromStack()\n");
218  return 0;
219  }
220 
221  result = 0;
222  for (i = 0; i < tcp_table->dwNumEntries; i++)
223  {
224  _MIB_TCPROW_EX *p = &tcp_table->table[i];
225  int local_port = ntohs (p->dwLocalPort);
226  int local_address = ntohl (p->dwLocalAddr);
227  if (local_address == INADDR_LOOPBACK && local_port == peer_port)
228  {
229  result = p->dwOwningPid;
230  break;
231  }
232  }
233 
234  HeapFree (GetProcessHeap(), 0, tcp_table);
235  _dbus_verbose ("got pid %lu\n", result);
236  return result;
237 }
238 
244 static dbus_pid_t
245 _dbus_get_peer_pid_from_tcp_handle (int handle)
246 {
247  struct sockaddr_storage addr;
248  socklen_t len = sizeof (addr);
249  int peer_port;
250 
251  dbus_pid_t result;
252  dbus_bool_t is_localhost = FALSE;
253 
254  getpeername (handle, (struct sockaddr *) &addr, &len);
255 
256  if (addr.ss_family == AF_INET)
257  {
258  struct sockaddr_in *s = (struct sockaddr_in *) &addr;
259  peer_port = ntohs (s->sin_port);
260  is_localhost = (ntohl (s->sin_addr.s_addr) == INADDR_LOOPBACK);
261  }
262  else if (addr.ss_family == AF_INET6)
263  {
264  _dbus_verbose ("FIXME [61922]: IPV6 support not working on windows\n");
265  return 0;
266  /*
267  struct sockaddr_in6 *s = (struct sockaddr_in6 * )&addr;
268  peer_port = ntohs (s->sin6_port);
269  is_localhost = (memcmp(s->sin6_addr.s6_addr, in6addr_loopback.s6_addr, 16) == 0);
270  _dbus_verbose ("IPV6 %08x %08x\n", s->sin6_addr.s6_addr, in6addr_loopback.s6_addr);
271  */
272  }
273  else
274  {
275  _dbus_verbose ("no idea what address family %d is\n", addr.ss_family);
276  return 0;
277  }
278 
279  if (!is_localhost)
280  {
281  _dbus_verbose ("could not fetch process id from remote process\n");
282  return 0;
283  }
284 
285  if (peer_port == 0)
286  {
287  _dbus_verbose
288  ("Error not been able to fetch tcp peer port from connection\n");
289  return 0;
290  }
291 
292  _dbus_verbose ("trying to get peers pid");
293 
294  result = get_pid_from_extended_tcp_table (peer_port);
295  if (result > 0)
296  return result;
297  result = get_pid_from_tcp_ex_table (peer_port);
298  return result;
299 }
300 
301 /* Convert GetLastError() to a dbus error. */
302 const char*
303 _dbus_win_error_from_last_error (void)
304 {
305  switch (GetLastError())
306  {
307  case 0:
308  return DBUS_ERROR_FAILED;
309 
310  case ERROR_NO_MORE_FILES:
311  case ERROR_TOO_MANY_OPEN_FILES:
312  return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
313 
314  case ERROR_ACCESS_DENIED:
315  case ERROR_CANNOT_MAKE:
317 
318  case ERROR_NOT_ENOUGH_MEMORY:
319  return DBUS_ERROR_NO_MEMORY;
320 
321  case ERROR_FILE_EXISTS:
322  return DBUS_ERROR_FILE_EXISTS;
323 
324  case ERROR_FILE_NOT_FOUND:
325  case ERROR_PATH_NOT_FOUND:
327  }
328 
329  return DBUS_ERROR_FAILED;
330 }
331 
332 
333 char*
334 _dbus_win_error_string (int error_number)
335 {
336  char *msg;
337 
338  FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
339  FORMAT_MESSAGE_IGNORE_INSERTS |
340  FORMAT_MESSAGE_FROM_SYSTEM,
341  NULL, error_number, 0,
342  (LPSTR) &msg, 0, NULL);
343 
344  if (msg[strlen (msg) - 1] == '\n')
345  msg[strlen (msg) - 1] = '\0';
346  if (msg[strlen (msg) - 1] == '\r')
347  msg[strlen (msg) - 1] = '\0';
348 
349  return msg;
350 }
351 
352 void
353 _dbus_win_free_error_string (char *string)
354 {
355  LocalFree (string);
356 }
357 
378 int
380  DBusString *buffer,
381  int count)
382 {
383  int bytes_read;
384  int start;
385  char *data;
386 
387  _dbus_assert (count >= 0);
388 
389  start = _dbus_string_get_length (buffer);
390 
391  if (!_dbus_string_lengthen (buffer, count))
392  {
393  _dbus_win_set_errno (ENOMEM);
394  return -1;
395  }
396 
397  data = _dbus_string_get_data_len (buffer, start, count);
398 
399  again:
400 
401  _dbus_verbose ("recv: count=%d fd=%d\n", count, fd);
402  bytes_read = recv (fd, data, count, 0);
403 
404  if (bytes_read == SOCKET_ERROR)
405  {
406  DBUS_SOCKET_SET_ERRNO();
407  _dbus_verbose ("recv: failed: %s (%d)\n", _dbus_strerror (errno), errno);
408  bytes_read = -1;
409  }
410  else
411  _dbus_verbose ("recv: = %d\n", bytes_read);
412 
413  if (bytes_read < 0)
414  {
415  if (errno == EINTR)
416  goto again;
417  else
418  {
419  /* put length back (note that this doesn't actually realloc anything) */
420  _dbus_string_set_length (buffer, start);
421  return -1;
422  }
423  }
424  else
425  {
426  /* put length back (doesn't actually realloc) */
427  _dbus_string_set_length (buffer, start + bytes_read);
428 
429 #if 0
430  if (bytes_read > 0)
431  _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
432 #endif
433 
434  return bytes_read;
435  }
436 }
437 
448 int
450  const DBusString *buffer,
451  int start,
452  int len)
453 {
454  const char *data;
455  int bytes_written;
456 
457  data = _dbus_string_get_const_data_len (buffer, start, len);
458 
459  again:
460 
461  _dbus_verbose ("send: len=%d fd=%d\n", len, fd);
462  bytes_written = send (fd, data, len, 0);
463 
464  if (bytes_written == SOCKET_ERROR)
465  {
466  DBUS_SOCKET_SET_ERRNO();
467  _dbus_verbose ("send: failed: %s\n", _dbus_strerror_from_errno ());
468  bytes_written = -1;
469  }
470  else
471  _dbus_verbose ("send: = %d\n", bytes_written);
472 
473  if (bytes_written < 0 && errno == EINTR)
474  goto again;
475 
476 #if 0
477  if (bytes_written > 0)
478  _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
479 #endif
480 
481  return bytes_written;
482 }
483 
484 
494  DBusError *error)
495 {
496  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
497 
498  again:
499  if (closesocket (fd) == SOCKET_ERROR)
500  {
501  DBUS_SOCKET_SET_ERRNO ();
502 
503  if (errno == EINTR)
504  goto again;
505 
506  dbus_set_error (error, _dbus_error_from_errno (errno),
507  "Could not close socket: socket=%d, , %s",
509  return FALSE;
510  }
511  _dbus_verbose ("_dbus_close_socket: socket=%d, \n", fd);
512 
513  return TRUE;
514 }
515 
523 void
524 _dbus_fd_set_close_on_exec (intptr_t handle)
525 {
526  if ( !SetHandleInformation( (HANDLE) handle,
527  HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE,
528  0 /*disable both flags*/ ) )
529  {
530  _dbus_win_warn_win_error ("Disabling socket handle inheritance failed:", GetLastError());
531  }
532 }
533 
542 _dbus_set_fd_nonblocking (int handle,
543  DBusError *error)
544 {
545  u_long one = 1;
546 
547  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
548 
549  if (ioctlsocket (handle, FIONBIO, &one) == SOCKET_ERROR)
550  {
551  DBUS_SOCKET_SET_ERRNO ();
552  dbus_set_error (error, _dbus_error_from_errno (errno),
553  "Failed to set socket %d:%d to nonblocking: %s", handle,
555  return FALSE;
556  }
557 
558  return TRUE;
559 }
560 
561 
582 int
584  const DBusString *buffer1,
585  int start1,
586  int len1,
587  const DBusString *buffer2,
588  int start2,
589  int len2)
590 {
591  WSABUF vectors[2];
592  const char *data1;
593  const char *data2;
594  int rc;
595  DWORD bytes_written;
596 
597  _dbus_assert (buffer1 != NULL);
598  _dbus_assert (start1 >= 0);
599  _dbus_assert (start2 >= 0);
600  _dbus_assert (len1 >= 0);
601  _dbus_assert (len2 >= 0);
602 
603 
604  data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
605 
606  if (buffer2 != NULL)
607  data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
608  else
609  {
610  data2 = NULL;
611  start2 = 0;
612  len2 = 0;
613  }
614 
615  vectors[0].buf = (char*) data1;
616  vectors[0].len = len1;
617  vectors[1].buf = (char*) data2;
618  vectors[1].len = len2;
619 
620  again:
621 
622  _dbus_verbose ("WSASend: len1+2=%d+%d fd=%d\n", len1, len2, fd);
623  rc = WSASend (fd,
624  vectors,
625  data2 ? 2 : 1,
626  &bytes_written,
627  0,
628  NULL,
629  NULL);
630 
631  if (rc == SOCKET_ERROR)
632  {
633  DBUS_SOCKET_SET_ERRNO ();
634  _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror_from_errno ());
635  bytes_written = -1;
636  }
637  else
638  _dbus_verbose ("WSASend: = %ld\n", bytes_written);
639 
640  if (bytes_written < 0 && errno == EINTR)
641  goto again;
642 
643  return bytes_written;
644 }
645 
647 _dbus_socket_is_invalid (int fd)
648 {
649  return fd == INVALID_SOCKET ? TRUE : FALSE;
650 }
651 
652 #if 0
653 
662 int
663 _dbus_connect_named_pipe (const char *path,
664  DBusError *error)
665 {
666  _dbus_assert_not_reached ("not implemented");
667 }
668 
669 #endif
670 
675 _dbus_win_startup_winsock (void)
676 {
677  /* Straight from MSDN, deuglified */
678 
679  /* Protected by _DBUS_LOCK_sysdeps */
680  static dbus_bool_t beenhere = FALSE;
681 
682  WORD wVersionRequested;
683  WSADATA wsaData;
684  int err;
685 
686  if (!_DBUS_LOCK (sysdeps))
687  return FALSE;
688 
689  if (beenhere)
690  goto out;
691 
692  wVersionRequested = MAKEWORD (2, 0);
693 
694  err = WSAStartup (wVersionRequested, &wsaData);
695  if (err != 0)
696  {
697  _dbus_assert_not_reached ("Could not initialize WinSock");
698  _dbus_abort ();
699  }
700 
701  /* Confirm that the WinSock DLL supports 2.0. Note that if the DLL
702  * supports versions greater than 2.0 in addition to 2.0, it will
703  * still return 2.0 in wVersion since that is the version we
704  * requested.
705  */
706  if (LOBYTE (wsaData.wVersion) != 2 ||
707  HIBYTE (wsaData.wVersion) != 0)
708  {
709  _dbus_assert_not_reached ("No usable WinSock found");
710  _dbus_abort ();
711  }
712 
713  beenhere = TRUE;
714 
715 out:
716  _DBUS_UNLOCK (sysdeps);
717  return TRUE;
718 }
719 
720 
721 
722 
723 
724 
725 
726 
727 
728 /************************************************************************
729 
730  UTF / string code
731 
732  ************************************************************************/
733 
737 int _dbus_printf_string_upper_bound (const char *format,
738  va_list args)
739 {
740  /* MSVCRT's vsnprintf semantics are a bit different */
741  char buf[1024];
742  int bufsize;
743  int len;
744  va_list args_copy;
745 
746  bufsize = sizeof (buf);
747  DBUS_VA_COPY (args_copy, args);
748  len = _vsnprintf (buf, bufsize - 1, format, args_copy);
749  va_end (args_copy);
750 
751  while (len == -1) /* try again */
752  {
753  char *p;
754 
755  bufsize *= 2;
756 
757  p = malloc (bufsize);
758 
759  if (p == NULL)
760  return -1;
761 
762  DBUS_VA_COPY (args_copy, args);
763  len = _vsnprintf (p, bufsize - 1, format, args_copy);
764  va_end (args_copy);
765  free (p);
766  }
767 
768  return len;
769 }
770 
771 
779 wchar_t *
780 _dbus_win_utf8_to_utf16 (const char *str,
781  DBusError *error)
782 {
783  DBusString s;
784  int n;
785  wchar_t *retval;
786 
787  _dbus_string_init_const (&s, str);
788 
790  {
791  dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid UTF-8");
792  return NULL;
793  }
794 
795  n = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0);
796 
797  if (n == 0)
798  {
799  _dbus_win_set_error_from_win_error (error, GetLastError ());
800  return NULL;
801  }
802 
803  retval = dbus_new (wchar_t, n);
804 
805  if (!retval)
806  {
807  _DBUS_SET_OOM (error);
808  return NULL;
809  }
810 
811  if (MultiByteToWideChar (CP_UTF8, 0, str, -1, retval, n) != n)
812  {
813  dbus_free (retval);
814  dbus_set_error_const (error, DBUS_ERROR_FAILED, "MultiByteToWideChar inconsistency");
815  return NULL;
816  }
817 
818  return retval;
819 }
820 
828 char *
829 _dbus_win_utf16_to_utf8 (const wchar_t *str,
830  DBusError *error)
831 {
832  int n;
833  char *retval;
834 
835  n = WideCharToMultiByte (CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
836 
837  if (n == 0)
838  {
839  _dbus_win_set_error_from_win_error (error, GetLastError ());
840  return NULL;
841  }
842 
843  retval = dbus_malloc (n);
844 
845  if (!retval)
846  {
847  _DBUS_SET_OOM (error);
848  return NULL;
849  }
850 
851  if (WideCharToMultiByte (CP_UTF8, 0, str, -1, retval, n, NULL, NULL) != n)
852  {
853  dbus_free (retval);
854  dbus_set_error_const (error, DBUS_ERROR_FAILED, "WideCharToMultiByte inconsistency");
855  return NULL;
856  }
857 
858  return retval;
859 }
860 
861 
862 
863 
864 
865 
866 /************************************************************************
867 
868 
869  ************************************************************************/
870 
872 _dbus_win_account_to_sid (const wchar_t *waccount,
873  void **ppsid,
874  DBusError *error)
875 {
876  dbus_bool_t retval = FALSE;
877  DWORD sid_length, wdomain_length;
878  SID_NAME_USE use;
879  wchar_t *wdomain;
880 
881  *ppsid = NULL;
882 
883  sid_length = 0;
884  wdomain_length = 0;
885  if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
886  NULL, &wdomain_length, &use) &&
887  GetLastError () != ERROR_INSUFFICIENT_BUFFER)
888  {
889  _dbus_win_set_error_from_win_error (error, GetLastError ());
890  return FALSE;
891  }
892 
893  *ppsid = dbus_malloc (sid_length);
894  if (!*ppsid)
895  {
896  _DBUS_SET_OOM (error);
897  return FALSE;
898  }
899 
900  wdomain = dbus_new (wchar_t, wdomain_length);
901  if (!wdomain)
902  {
903  _DBUS_SET_OOM (error);
904  goto out1;
905  }
906 
907  if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
908  wdomain, &wdomain_length, &use))
909  {
910  _dbus_win_set_error_from_win_error (error, GetLastError ());
911  goto out2;
912  }
913 
914  if (!IsValidSid ((PSID) *ppsid))
915  {
916  dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
917  goto out2;
918  }
919 
920  retval = TRUE;
921 
922 out2:
923  dbus_free (wdomain);
924 out1:
925  if (!retval)
926  {
927  dbus_free (*ppsid);
928  *ppsid = NULL;
929  }
930 
931  return retval;
932 }
933 
943 unsigned long
945 {
946  return _dbus_getpid ();
947 }
948 
949 #ifndef DBUS_WINCE
950 
951 static BOOL is_winxp_sp3_or_lower()
952 {
953  OSVERSIONINFOEX osvi;
954  DWORDLONG dwlConditionMask = 0;
955  int op=VER_LESS_EQUAL;
956 
957  // Initialize the OSVERSIONINFOEX structure.
958 
959  ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
960  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
961  osvi.dwMajorVersion = 5;
962  osvi.dwMinorVersion = 1;
963  osvi.wServicePackMajor = 3;
964  osvi.wServicePackMinor = 0;
965 
966  // Initialize the condition mask.
967 
968  VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
969  VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
970  VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
971  VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
972 
973  // Perform the test.
974 
975  return VerifyVersionInfo(
976  &osvi,
977  VER_MAJORVERSION | VER_MINORVERSION |
978  VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
979  dwlConditionMask);
980 }
981 
987 static dbus_bool_t
988 _dbus_getsid(char **sid, dbus_pid_t process_id)
989 {
990  HANDLE process_token = INVALID_HANDLE_VALUE;
991  TOKEN_USER *token_user = NULL;
992  DWORD n;
993  PSID psid;
994  int retval = FALSE;
995 
996  HANDLE process_handle = OpenProcess(is_winxp_sp3_or_lower() ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id);
997 
998  if (!OpenProcessToken (process_handle, TOKEN_QUERY, &process_token))
999  {
1000  _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1001  goto failed;
1002  }
1003  if ((!GetTokenInformation (process_token, TokenUser, NULL, 0, &n)
1004  && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1005  || (token_user = alloca (n)) == NULL
1006  || !GetTokenInformation (process_token, TokenUser, token_user, n, &n))
1007  {
1008  _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1009  goto failed;
1010  }
1011  psid = token_user->User.Sid;
1012  if (!IsValidSid (psid))
1013  {
1014  _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1015  goto failed;
1016  }
1017  if (!ConvertSidToStringSidA (psid, sid))
1018  {
1019  _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1020  goto failed;
1021  }
1022 //okay:
1023  retval = TRUE;
1024 
1025 failed:
1026  CloseHandle (process_handle);
1027  if (process_token != INVALID_HANDLE_VALUE)
1028  CloseHandle (process_token);
1029 
1030  _dbus_verbose("_dbus_getsid() got '%s' and returns %d\n", *sid, retval);
1031  return retval;
1032 }
1033 #endif
1034 
1035 /************************************************************************
1036 
1037  pipes
1038 
1039  ************************************************************************/
1040 
1053  int *fd2,
1054  dbus_bool_t blocking,
1055  DBusError *error)
1056 {
1057  SOCKET temp, socket1 = -1, socket2 = -1;
1058  struct sockaddr_in saddr;
1059  int len;
1060  u_long arg;
1061 
1062  if (!_dbus_win_startup_winsock ())
1063  {
1064  _DBUS_SET_OOM (error);
1065  return FALSE;
1066  }
1067 
1068  temp = socket (AF_INET, SOCK_STREAM, 0);
1069  if (temp == INVALID_SOCKET)
1070  {
1071  DBUS_SOCKET_SET_ERRNO ();
1072  goto out0;
1073  }
1074 
1075  _DBUS_ZERO (saddr);
1076  saddr.sin_family = AF_INET;
1077  saddr.sin_port = 0;
1078  saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1079 
1080  if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)) == SOCKET_ERROR)
1081  {
1082  DBUS_SOCKET_SET_ERRNO ();
1083  goto out0;
1084  }
1085 
1086  if (listen (temp, 1) == SOCKET_ERROR)
1087  {
1088  DBUS_SOCKET_SET_ERRNO ();
1089  goto out0;
1090  }
1091 
1092  len = sizeof (saddr);
1093  if (getsockname (temp, (struct sockaddr *)&saddr, &len) == SOCKET_ERROR)
1094  {
1095  DBUS_SOCKET_SET_ERRNO ();
1096  goto out0;
1097  }
1098 
1099  socket1 = socket (AF_INET, SOCK_STREAM, 0);
1100  if (socket1 == INVALID_SOCKET)
1101  {
1102  DBUS_SOCKET_SET_ERRNO ();
1103  goto out0;
1104  }
1105 
1106  if (connect (socket1, (struct sockaddr *)&saddr, len) == SOCKET_ERROR)
1107  {
1108  DBUS_SOCKET_SET_ERRNO ();
1109  goto out1;
1110  }
1111 
1112  socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
1113  if (socket2 == INVALID_SOCKET)
1114  {
1115  DBUS_SOCKET_SET_ERRNO ();
1116  goto out1;
1117  }
1118 
1119  if (!blocking)
1120  {
1121  arg = 1;
1122  if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
1123  {
1124  DBUS_SOCKET_SET_ERRNO ();
1125  goto out2;
1126  }
1127 
1128  arg = 1;
1129  if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
1130  {
1131  DBUS_SOCKET_SET_ERRNO ();
1132  goto out2;
1133  }
1134  }
1135 
1136  *fd1 = socket1;
1137  *fd2 = socket2;
1138 
1139  _dbus_verbose ("full-duplex pipe %d:%d <-> %d:%d\n",
1140  *fd1, socket1, *fd2, socket2);
1141 
1142  closesocket (temp);
1143 
1144  return TRUE;
1145 
1146 out2:
1147  closesocket (socket2);
1148 out1:
1149  closesocket (socket1);
1150 out0:
1151  closesocket (temp);
1152 
1153  dbus_set_error (error, _dbus_error_from_errno (errno),
1154  "Could not setup socket pair: %s",
1156 
1157  return FALSE;
1158 }
1159 
1168 int
1170  int n_fds,
1171  int timeout_milliseconds)
1172 {
1173 #define USE_CHRIS_IMPL 0
1174 
1175 #if USE_CHRIS_IMPL
1176 
1177 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
1178  char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
1179  char *msgp;
1180 
1181  int ret = 0;
1182  int i;
1183  struct timeval tv;
1184  int ready;
1185 
1186 #define DBUS_STACK_WSAEVENTS 256
1187  WSAEVENT eventsOnStack[DBUS_STACK_WSAEVENTS];
1188  WSAEVENT *pEvents = NULL;
1189  if (n_fds > DBUS_STACK_WSAEVENTS)
1190  pEvents = calloc(sizeof(WSAEVENT), n_fds);
1191  else
1192  pEvents = eventsOnStack;
1193 
1194 
1195 #ifdef DBUS_ENABLE_VERBOSE_MODE
1196  msgp = msg;
1197  msgp += sprintf (msgp, "WSAEventSelect: to=%d\n\t", timeout_milliseconds);
1198  for (i = 0; i < n_fds; i++)
1199  {
1200  DBusPollFD *fdp = &fds[i];
1201 
1202 
1203  if (fdp->events & _DBUS_POLLIN)
1204  msgp += sprintf (msgp, "R:%d ", fdp->fd);
1205 
1206  if (fdp->events & _DBUS_POLLOUT)
1207  msgp += sprintf (msgp, "W:%d ", fdp->fd);
1208 
1209  msgp += sprintf (msgp, "E:%d\n\t", fdp->fd);
1210 
1211  // FIXME: more robust code for long msg
1212  // create on heap when msg[] becomes too small
1213  if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
1214  {
1215  _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
1216  }
1217  }
1218 
1219  msgp += sprintf (msgp, "\n");
1220  _dbus_verbose ("%s",msg);
1221 #endif
1222  for (i = 0; i < n_fds; i++)
1223  {
1224  DBusPollFD *fdp = &fds[i];
1225  WSAEVENT ev;
1226  long lNetworkEvents = FD_OOB;
1227 
1228  ev = WSACreateEvent();
1229 
1230  if (fdp->events & _DBUS_POLLIN)
1231  lNetworkEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
1232 
1233  if (fdp->events & _DBUS_POLLOUT)
1234  lNetworkEvents |= FD_WRITE | FD_CONNECT;
1235 
1236  WSAEventSelect(fdp->fd, ev, lNetworkEvents);
1237 
1238  pEvents[i] = ev;
1239  }
1240 
1241 
1242  ready = WSAWaitForMultipleEvents (n_fds, pEvents, FALSE, timeout_milliseconds, FALSE);
1243 
1244  if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
1245  {
1246  DBUS_SOCKET_SET_ERRNO ();
1247  if (errno != WSAEWOULDBLOCK)
1248  _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", _dbus_strerror_from_errno ());
1249  ret = -1;
1250  }
1251  else if (ready == WSA_WAIT_TIMEOUT)
1252  {
1253  _dbus_verbose ("WSAWaitForMultipleEvents: WSA_WAIT_TIMEOUT\n");
1254  ret = 0;
1255  }
1256  else if (ready >= WSA_WAIT_EVENT_0 && ready < (int)(WSA_WAIT_EVENT_0 + n_fds))
1257  {
1258  msgp = msg;
1259  msgp += sprintf (msgp, "WSAWaitForMultipleEvents: =%d\n\t", ready);
1260 
1261  for (i = 0; i < n_fds; i++)
1262  {
1263  DBusPollFD *fdp = &fds[i];
1264  WSANETWORKEVENTS ne;
1265 
1266  fdp->revents = 0;
1267 
1268  WSAEnumNetworkEvents(fdp->fd, pEvents[i], &ne);
1269 
1270  if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1271  fdp->revents |= _DBUS_POLLIN;
1272 
1273  if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
1274  fdp->revents |= _DBUS_POLLOUT;
1275 
1276  if (ne.lNetworkEvents & (FD_OOB))
1277  fdp->revents |= _DBUS_POLLERR;
1278 
1279  if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1280  msgp += sprintf (msgp, "R:%d ", fdp->fd);
1281 
1282  if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
1283  msgp += sprintf (msgp, "W:%d ", fdp->fd);
1284 
1285  if (ne.lNetworkEvents & (FD_OOB))
1286  msgp += sprintf (msgp, "E:%d ", fdp->fd);
1287 
1288  msgp += sprintf (msgp, "lNetworkEvents:%d ", ne.lNetworkEvents);
1289 
1290  if(ne.lNetworkEvents)
1291  ret++;
1292 
1293  WSAEventSelect(fdp->fd, pEvents[i], 0);
1294  }
1295 
1296  msgp += sprintf (msgp, "\n");
1297  _dbus_verbose ("%s",msg);
1298  }
1299  else
1300  {
1301  _dbus_verbose ("WSAWaitForMultipleEvents: failed for unknown reason!");
1302  ret = -1;
1303  }
1304 
1305  for(i = 0; i < n_fds; i++)
1306  {
1307  WSACloseEvent(pEvents[i]);
1308  }
1309 
1310  if (n_fds > DBUS_STACK_WSAEVENTS)
1311  free(pEvents);
1312 
1313  return ret;
1314 
1315 #else /* USE_CHRIS_IMPL */
1316 
1317 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
1318  char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
1319  char *msgp;
1320 
1321  fd_set read_set, write_set, err_set;
1322  int max_fd = 0;
1323  int i;
1324  struct timeval tv;
1325  int ready;
1326 
1327  FD_ZERO (&read_set);
1328  FD_ZERO (&write_set);
1329  FD_ZERO (&err_set);
1330 
1331 
1332 #ifdef DBUS_ENABLE_VERBOSE_MODE
1333  msgp = msg;
1334  msgp += sprintf (msgp, "select: to=%d\n\t", timeout_milliseconds);
1335  for (i = 0; i < n_fds; i++)
1336  {
1337  DBusPollFD *fdp = &fds[i];
1338 
1339 
1340  if (fdp->events & _DBUS_POLLIN)
1341  msgp += sprintf (msgp, "R:%d ", fdp->fd);
1342 
1343  if (fdp->events & _DBUS_POLLOUT)
1344  msgp += sprintf (msgp, "W:%d ", fdp->fd);
1345 
1346  msgp += sprintf (msgp, "E:%d\n\t", fdp->fd);
1347 
1348  // FIXME: more robust code for long msg
1349  // create on heap when msg[] becomes too small
1350  if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
1351  {
1352  _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
1353  }
1354  }
1355 
1356  msgp += sprintf (msgp, "\n");
1357  _dbus_verbose ("%s",msg);
1358 #endif
1359  for (i = 0; i < n_fds; i++)
1360  {
1361  DBusPollFD *fdp = &fds[i];
1362 
1363  if (fdp->events & _DBUS_POLLIN)
1364  FD_SET (fdp->fd, &read_set);
1365 
1366  if (fdp->events & _DBUS_POLLOUT)
1367  FD_SET (fdp->fd, &write_set);
1368 
1369  FD_SET (fdp->fd, &err_set);
1370 
1371  max_fd = MAX (max_fd, fdp->fd);
1372  }
1373 
1374  // Avoid random lockups with send(), for lack of a better solution so far
1375  tv.tv_sec = timeout_milliseconds < 0 ? 1 : timeout_milliseconds / 1000;
1376  tv.tv_usec = timeout_milliseconds < 0 ? 0 : (timeout_milliseconds % 1000) * 1000;
1377 
1378  ready = select (max_fd + 1, &read_set, &write_set, &err_set, &tv);
1379 
1380  if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
1381  {
1382  DBUS_SOCKET_SET_ERRNO ();
1383  if (errno != WSAEWOULDBLOCK)
1384  _dbus_verbose ("select: failed: %s\n", _dbus_strerror_from_errno ());
1385  }
1386  else if (ready == 0)
1387  _dbus_verbose ("select: = 0\n");
1388  else
1389  if (ready > 0)
1390  {
1391 #ifdef DBUS_ENABLE_VERBOSE_MODE
1392  msgp = msg;
1393  msgp += sprintf (msgp, "select: = %d:\n\t", ready);
1394 
1395  for (i = 0; i < n_fds; i++)
1396  {
1397  DBusPollFD *fdp = &fds[i];
1398 
1399  if (FD_ISSET (fdp->fd, &read_set))
1400  msgp += sprintf (msgp, "R:%d ", fdp->fd);
1401 
1402  if (FD_ISSET (fdp->fd, &write_set))
1403  msgp += sprintf (msgp, "W:%d ", fdp->fd);
1404 
1405  if (FD_ISSET (fdp->fd, &err_set))
1406  msgp += sprintf (msgp, "E:%d\n\t", fdp->fd);
1407  }
1408  msgp += sprintf (msgp, "\n");
1409  _dbus_verbose ("%s",msg);
1410 #endif
1411 
1412  for (i = 0; i < n_fds; i++)
1413  {
1414  DBusPollFD *fdp = &fds[i];
1415 
1416  fdp->revents = 0;
1417 
1418  if (FD_ISSET (fdp->fd, &read_set))
1419  fdp->revents |= _DBUS_POLLIN;
1420 
1421  if (FD_ISSET (fdp->fd, &write_set))
1422  fdp->revents |= _DBUS_POLLOUT;
1423 
1424  if (FD_ISSET (fdp->fd, &err_set))
1425  fdp->revents |= _DBUS_POLLERR;
1426  }
1427  }
1428  return ready;
1429 #endif /* USE_CHRIS_IMPL */
1430 }
1431 
1432 
1433 
1434 
1435 /******************************************************************************
1436 
1437 Original CVS version of dbus-sysdeps.c
1438 
1439 ******************************************************************************/
1440 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
1441 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-Bus implementation)
1442  *
1443  * Copyright (C) 2002, 2003 Red Hat, Inc.
1444  * Copyright (C) 2003 CodeFactory AB
1445  * Copyright (C) 2005 Novell, Inc.
1446  *
1447  * Licensed under the Academic Free License version 2.1
1448  *
1449  * This program is free software; you can redistribute it and/or modify
1450  * it under the terms of the GNU General Public License as published by
1451  * the Free Software Foundation; either version 2 of the License, or
1452  * (at your option) any later version.
1453  *
1454  * This program is distributed in the hope that it will be useful,
1455  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1456  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1457  * GNU General Public License for more details.
1458  *
1459  * You should have received a copy of the GNU General Public License
1460  * along with this program; if not, write to the Free Software
1461  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1462  *
1463  */
1464 
1465 
1471 void
1472 _dbus_exit (int code)
1473 {
1474  _exit (code);
1475 }
1476 
1488 int
1489 _dbus_connect_tcp_socket (const char *host,
1490  const char *port,
1491  const char *family,
1492  DBusError *error)
1493 {
1494  return _dbus_connect_tcp_socket_with_nonce (host, port, family, (const char*)NULL, error);
1495 }
1496 
1497 int
1498 _dbus_connect_tcp_socket_with_nonce (const char *host,
1499  const char *port,
1500  const char *family,
1501  const char *noncefile,
1502  DBusError *error)
1503 {
1504  int fd = -1, res;
1505  struct addrinfo hints;
1506  struct addrinfo *ai, *tmp;
1507 
1508  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1509 
1510  if (!_dbus_win_startup_winsock ())
1511  {
1512  _DBUS_SET_OOM (error);
1513  return -1;
1514  }
1515 
1516  _DBUS_ZERO (hints);
1517 
1518  if (!family)
1519  hints.ai_family = AF_UNSPEC;
1520  else if (!strcmp(family, "ipv4"))
1521  hints.ai_family = AF_INET;
1522  else if (!strcmp(family, "ipv6"))
1523  hints.ai_family = AF_INET6;
1524  else
1525  {
1526  dbus_set_error (error,
1528  "Unknown address family %s", family);
1529  return -1;
1530  }
1531  hints.ai_protocol = IPPROTO_TCP;
1532  hints.ai_socktype = SOCK_STREAM;
1533 #ifdef AI_ADDRCONFIG
1534  hints.ai_flags = AI_ADDRCONFIG;
1535 #else
1536  hints.ai_flags = 0;
1537 #endif
1538 
1539  if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
1540  {
1541  dbus_set_error (error,
1542  _dbus_error_from_errno (res),
1543  "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1544  host, port, _dbus_strerror(res), res);
1545  return -1;
1546  }
1547 
1548  tmp = ai;
1549  while (tmp)
1550  {
1551  if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
1552  {
1553  DBUS_SOCKET_SET_ERRNO ();
1554  dbus_set_error (error,
1555  _dbus_error_from_errno (errno),
1556  "Failed to open socket: %s",
1558  freeaddrinfo(ai);
1559  return -1;
1560  }
1561  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1562 
1563  if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR)
1564  {
1565  DBUS_SOCKET_SET_ERRNO ();
1566  closesocket(fd);
1567  fd = -1;
1568  tmp = tmp->ai_next;
1569  continue;
1570  }
1571 
1572  break;
1573  }
1574  freeaddrinfo(ai);
1575 
1576  if (fd == -1)
1577  {
1578  dbus_set_error (error,
1579  _dbus_error_from_errno (errno),
1580  "Failed to connect to socket \"%s:%s\" %s",
1581  host, port, _dbus_strerror_from_errno ());
1582  return -1;
1583  }
1584 
1585  if (noncefile != NULL)
1586  {
1587  DBusString noncefileStr;
1588  dbus_bool_t ret;
1589  if (!_dbus_string_init (&noncefileStr) ||
1590  !_dbus_string_append(&noncefileStr, noncefile))
1591  {
1592  closesocket (fd);
1594  return -1;
1595  }
1596 
1597  ret = _dbus_send_nonce (fd, &noncefileStr, error);
1598 
1599  _dbus_string_free (&noncefileStr);
1600 
1601  if (!ret)
1602  {
1603  closesocket (fd);
1604  return -1;
1605  }
1606  }
1607 
1609 
1610  if (!_dbus_set_fd_nonblocking (fd, error))
1611  {
1612  closesocket (fd);
1613  return -1;
1614  }
1615 
1616  return fd;
1617 }
1618 
1634 int
1635 _dbus_listen_tcp_socket (const char *host,
1636  const char *port,
1637  const char *family,
1638  DBusString *retport,
1639  int **fds_p,
1640  DBusError *error)
1641 {
1642  int nlisten_fd = 0, *listen_fd = NULL, res, i, port_num = -1;
1643  struct addrinfo hints;
1644  struct addrinfo *ai, *tmp;
1645 
1646  // On Vista, sockaddr_gen must be a sockaddr_in6, and not a sockaddr_in6_old
1647  //That's required for family == IPv6(which is the default on Vista if family is not given)
1648  //So we use our own union instead of sockaddr_gen:
1649 
1650  typedef union {
1651  struct sockaddr Address;
1652  struct sockaddr_in AddressIn;
1653  struct sockaddr_in6 AddressIn6;
1654  } mysockaddr_gen;
1655 
1656  *fds_p = NULL;
1657  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1658 
1659  if (!_dbus_win_startup_winsock ())
1660  {
1661  _DBUS_SET_OOM (error);
1662  return -1;
1663  }
1664 
1665  _DBUS_ZERO (hints);
1666 
1667  if (!family)
1668  hints.ai_family = AF_UNSPEC;
1669  else if (!strcmp(family, "ipv4"))
1670  hints.ai_family = AF_INET;
1671  else if (!strcmp(family, "ipv6"))
1672  hints.ai_family = AF_INET6;
1673  else
1674  {
1675  dbus_set_error (error,
1677  "Unknown address family %s", family);
1678  return -1;
1679  }
1680 
1681  hints.ai_protocol = IPPROTO_TCP;
1682  hints.ai_socktype = SOCK_STREAM;
1683 #ifdef AI_ADDRCONFIG
1684  hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1685 #else
1686  hints.ai_flags = AI_PASSIVE;
1687 #endif
1688 
1689  redo_lookup_with_port:
1690  if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
1691  {
1692  dbus_set_error (error,
1693  _dbus_error_from_errno (res),
1694  "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1695  host ? host : "*", port, _dbus_strerror(res), res);
1696  return -1;
1697  }
1698 
1699  tmp = ai;
1700  while (tmp)
1701  {
1702  int fd = -1, *newlisten_fd;
1703  if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
1704  {
1705  DBUS_SOCKET_SET_ERRNO ();
1706  dbus_set_error (error,
1707  _dbus_error_from_errno (errno),
1708  "Failed to open socket: %s",
1710  goto failed;
1711  }
1712  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1713 
1714  if (bind (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR)
1715  {
1716  DBUS_SOCKET_SET_ERRNO ();
1717  closesocket (fd);
1718  if (errno == WSAEADDRINUSE)
1719  {
1720  /* Calling this function with port=0 tries to
1721  * bind the same port twice, so we should
1722  * ignore the second bind.
1723  */
1724  tmp = tmp->ai_next;
1725  continue;
1726  }
1727  dbus_set_error (error, _dbus_error_from_errno (errno),
1728  "Failed to bind socket \"%s:%s\": %s",
1729  host ? host : "*", port, _dbus_strerror_from_errno ());
1730  goto failed;
1731  }
1732 
1733  if (listen (fd, 30 /* backlog */) == SOCKET_ERROR)
1734  {
1735  DBUS_SOCKET_SET_ERRNO ();
1736  dbus_set_error (error, _dbus_error_from_errno (errno),
1737  "Failed to listen on socket \"%s:%s\": %s",
1738  host ? host : "*", port, _dbus_strerror_from_errno ());
1739  closesocket (fd);
1740  goto failed;
1741  }
1742 
1743  newlisten_fd = dbus_realloc(listen_fd, sizeof(int)*(nlisten_fd+1));
1744  if (!newlisten_fd)
1745  {
1746  closesocket (fd);
1748  "Failed to allocate file handle array");
1749  goto failed;
1750  }
1751  listen_fd = newlisten_fd;
1752  listen_fd[nlisten_fd] = fd;
1753  nlisten_fd++;
1754 
1755  if (!_dbus_string_get_length(retport))
1756  {
1757  /* If the user didn't specify a port, or used 0, then
1758  the kernel chooses a port. After the first address
1759  is bound to, we need to force all remaining addresses
1760  to use the same port */
1761  if (!port || !strcmp(port, "0"))
1762  {
1763  mysockaddr_gen addr;
1764  socklen_t addrlen = sizeof(addr);
1765  char portbuf[10];
1766 
1767  if (getsockname(fd, &addr.Address, &addrlen) == SOCKET_ERROR)
1768  {
1769  DBUS_SOCKET_SET_ERRNO ();
1770  dbus_set_error (error, _dbus_error_from_errno (errno),
1771  "Failed to resolve port \"%s:%s\": %s",
1772  host ? host : "*", port, _dbus_strerror_from_errno());
1773  goto failed;
1774  }
1775  if (addr.AddressIn.sin_family = AF_INET)
1776  snprintf( portbuf, sizeof( portbuf ) - 1, "%d", ntohs(addr.AddressIn.sin_port) );
1777  else
1778  snprintf( portbuf, sizeof( portbuf ) - 1, "%d", ntohs(addr.AddressIn6.sin6_port) );
1779  if (!_dbus_string_append(retport, portbuf))
1780  {
1782  goto failed;
1783  }
1784 
1785  /* Release current address list & redo lookup */
1786  port = _dbus_string_get_const_data(retport);
1787  freeaddrinfo(ai);
1788  goto redo_lookup_with_port;
1789  }
1790  else
1791  {
1792  if (!_dbus_string_append(retport, port))
1793  {
1795  goto failed;
1796  }
1797  }
1798  }
1799 
1800  tmp = tmp->ai_next;
1801  }
1802  freeaddrinfo(ai);
1803  ai = NULL;
1804 
1805  if (!nlisten_fd)
1806  {
1807  _dbus_win_set_errno (WSAEADDRINUSE);
1808  dbus_set_error (error, _dbus_error_from_errno (errno),
1809  "Failed to bind socket \"%s:%s\": %s",
1810  host ? host : "*", port, _dbus_strerror_from_errno ());
1811  return -1;
1812  }
1813 
1814  sscanf(_dbus_string_get_const_data(retport), "%d", &port_num);
1815 
1816  for (i = 0 ; i < nlisten_fd ; i++)
1817  {
1818  _dbus_fd_set_close_on_exec (listen_fd[i]);
1819  if (!_dbus_set_fd_nonblocking (listen_fd[i], error))
1820  {
1821  goto failed;
1822  }
1823  }
1824 
1825  *fds_p = listen_fd;
1826 
1827  return nlisten_fd;
1828 
1829  failed:
1830  if (ai)
1831  freeaddrinfo(ai);
1832  for (i = 0 ; i < nlisten_fd ; i++)
1833  closesocket (listen_fd[i]);
1834  dbus_free(listen_fd);
1835  return -1;
1836 }
1837 
1838 
1846 int
1847 _dbus_accept (int listen_fd)
1848 {
1849  int client_fd;
1850 
1851  retry:
1852  client_fd = accept (listen_fd, NULL, NULL);
1853 
1854  if (DBUS_SOCKET_IS_INVALID (client_fd))
1855  {
1856  DBUS_SOCKET_SET_ERRNO ();
1857  if (errno == EINTR)
1858  goto retry;
1859  }
1860 
1861  _dbus_verbose ("client fd %d accepted\n", client_fd);
1862 
1863  return client_fd;
1864 }
1865 
1866 
1867 
1868 
1871  DBusError *error)
1872 {
1873 /* FIXME: for the session bus credentials shouldn't matter (?), but
1874  * for the system bus they are presumably essential. A rough outline
1875  * of a way to implement the credential transfer would be this:
1876  *
1877  * client waits to *read* a byte.
1878  *
1879  * server creates a named pipe with a random name, sends a byte
1880  * contining its length, and its name.
1881  *
1882  * client reads the name, connects to it (using Win32 API).
1883  *
1884  * server waits for connection to the named pipe, then calls
1885  * ImpersonateNamedPipeClient(), notes its now-current credentials,
1886  * calls RevertToSelf(), closes its handles to the named pipe, and
1887  * is done. (Maybe there is some other way to get the SID of a named
1888  * pipe client without having to use impersonation?)
1889  *
1890  * client closes its handles and is done.
1891  *
1892  * Ralf: Why not sending credentials over the given this connection ?
1893  * Using named pipes makes it impossible to be connected from a unix client.
1894  *
1895  */
1896  int bytes_written;
1897  DBusString buf;
1898 
1899  _dbus_string_init_const_len (&buf, "\0", 1);
1900 again:
1901  bytes_written = _dbus_write_socket (handle, &buf, 0, 1 );
1902 
1903  if (bytes_written < 0 && errno == EINTR)
1904  goto again;
1905 
1906  if (bytes_written < 0)
1907  {
1908  dbus_set_error (error, _dbus_error_from_errno (errno),
1909  "Failed to write credentials byte: %s",
1911  return FALSE;
1912  }
1913  else if (bytes_written == 0)
1914  {
1916  "wrote zero bytes writing credentials byte");
1917  return FALSE;
1918  }
1919  else
1920  {
1921  _dbus_assert (bytes_written == 1);
1922  _dbus_verbose ("wrote 1 zero byte, credential sending isn't implemented yet\n");
1923  return TRUE;
1924  }
1925  return TRUE;
1926 }
1927 
1948  DBusCredentials *credentials,
1949  DBusError *error)
1950 {
1951  int bytes_read = 0;
1952  DBusString buf;
1953 
1954  char *sid = NULL;
1955  dbus_pid_t pid;
1956  int retval = FALSE;
1957 
1958  // could fail due too OOM
1959  if (_dbus_string_init (&buf))
1960  {
1961  bytes_read = _dbus_read_socket (handle, &buf, 1 );
1962 
1963  if (bytes_read > 0)
1964  _dbus_verbose ("got one zero byte from server\n");
1965 
1966  _dbus_string_free (&buf);
1967  }
1968 
1969  pid = _dbus_get_peer_pid_from_tcp_handle (handle);
1970  if (pid == 0)
1971  return TRUE;
1972 
1973  _dbus_credentials_add_pid (credentials, pid);
1974 
1975  if (_dbus_getsid (&sid, pid))
1976  {
1977  if (!_dbus_credentials_add_windows_sid (credentials, sid))
1978  goto out;
1979  }
1980 
1981  retval = TRUE;
1982 
1983 out:
1984  if (sid)
1985  LocalFree (sid);
1986 
1987  return retval;
1988 }
1989 
2000 {
2001  /* TODO */
2002  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2003  return TRUE;
2004 }
2005 
2006 
2019  const DBusString *next_component)
2020 {
2021  dbus_bool_t dir_ends_in_slash;
2022  dbus_bool_t file_starts_with_slash;
2023 
2024  if (_dbus_string_get_length (dir) == 0 ||
2025  _dbus_string_get_length (next_component) == 0)
2026  return TRUE;
2027 
2028  dir_ends_in_slash =
2029  ('/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1) ||
2030  '\\' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1));
2031 
2032  file_starts_with_slash =
2033  ('/' == _dbus_string_get_byte (next_component, 0) ||
2034  '\\' == _dbus_string_get_byte (next_component, 0));
2035 
2036  if (dir_ends_in_slash && file_starts_with_slash)
2037  {
2038  _dbus_string_shorten (dir, 1);
2039  }
2040  else if (!(dir_ends_in_slash || file_starts_with_slash))
2041  {
2042  if (!_dbus_string_append_byte (dir, '\\'))
2043  return FALSE;
2044  }
2045 
2046  return _dbus_string_copy (next_component, 0, dir,
2047  _dbus_string_get_length (dir));
2048 }
2049 
2050 /*---------------- DBusCredentials ----------------------------------*/
2051 
2061  const DBusString *username)
2062 {
2063  return _dbus_credentials_add_windows_sid (credentials,
2064  _dbus_string_get_const_data(username));
2065 }
2066 
2077 {
2078  dbus_bool_t retval = FALSE;
2079  char *sid = NULL;
2080 
2081  if (!_dbus_getsid(&sid, _dbus_getpid()))
2082  goto failed;
2083 
2084  if (!_dbus_credentials_add_pid (credentials, _dbus_getpid()))
2085  goto failed;
2086 
2087  if (!_dbus_credentials_add_windows_sid (credentials,sid))
2088  goto failed;
2089 
2090  retval = TRUE;
2091  goto end;
2092 failed:
2093  retval = FALSE;
2094 end:
2095  if (sid)
2096  LocalFree(sid);
2097 
2098  return retval;
2099 }
2100 
2115 {
2116  dbus_bool_t retval = FALSE;
2117  char *sid = NULL;
2118 
2119  if (!_dbus_getsid(&sid, _dbus_getpid()))
2120  return FALSE;
2121 
2122  retval = _dbus_string_append (str,sid);
2123 
2124  LocalFree(sid);
2125  return retval;
2126 }
2127 
2132 dbus_pid_t
2134 {
2135  return GetCurrentProcessId ();
2136 }
2137 
2139 #define NANOSECONDS_PER_SECOND 1000000000
2140 
2141 #define MICROSECONDS_PER_SECOND 1000000
2142 
2143 #define MILLISECONDS_PER_SECOND 1000
2144 
2145 #define NANOSECONDS_PER_MILLISECOND 1000000
2146 
2147 #define MICROSECONDS_PER_MILLISECOND 1000
2148 
2153 void
2154 _dbus_sleep_milliseconds (int milliseconds)
2155 {
2156  Sleep (milliseconds);
2157 }
2158 
2159 
2167 void
2168 _dbus_get_real_time (long *tv_sec,
2169  long *tv_usec)
2170 {
2171  FILETIME ft;
2172  dbus_uint64_t time64;
2173 
2174  GetSystemTimeAsFileTime (&ft);
2175 
2176  memcpy (&time64, &ft, sizeof (time64));
2177 
2178  /* Convert from 100s of nanoseconds since 1601-01-01
2179  * to Unix epoch. Yes, this is Y2038 unsafe.
2180  */
2181  time64 -= DBUS_INT64_CONSTANT (116444736000000000);
2182  time64 /= 10;
2183 
2184  if (tv_sec)
2185  *tv_sec = time64 / 1000000;
2186 
2187  if (tv_usec)
2188  *tv_usec = time64 % 1000000;
2189 }
2190 
2198 void
2200  long *tv_usec)
2201 {
2202  /* no implementation yet, fall back to wall-clock time */
2203  _dbus_get_real_time (tv_sec, tv_usec);
2204 }
2205 
2209 void
2211 {
2212 }
2213 
2224  DBusError *error)
2225 {
2226  const char *filename_c;
2227 
2228  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2229 
2230  filename_c = _dbus_string_get_const_data (filename);
2231 
2232  if (!CreateDirectoryA (filename_c, NULL))
2233  {
2235  "Failed to create directory %s: %s\n",
2236  filename_c, _dbus_strerror_from_errno ());
2237  return FALSE;
2238  }
2239  else
2240  return TRUE;
2241 }
2242 
2253  DBusError *error)
2254 {
2255  const char *filename_c;
2256 
2257  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2258 
2259  filename_c = _dbus_string_get_const_data (filename);
2260 
2261  if (!CreateDirectoryA (filename_c, NULL))
2262  {
2263  if (GetLastError () == ERROR_ALREADY_EXISTS)
2264  return TRUE;
2265 
2267  "Failed to create directory %s: %s\n",
2268  filename_c, _dbus_strerror_from_errno ());
2269  return FALSE;
2270  }
2271  else
2272  return TRUE;
2273 }
2274 
2275 
2286  int n_bytes)
2287 {
2288  int old_len;
2289  char *p;
2290  HCRYPTPROV hprov;
2291 
2292  old_len = _dbus_string_get_length (str);
2293 
2294  if (!_dbus_string_lengthen (str, n_bytes))
2295  return FALSE;
2296 
2297  p = _dbus_string_get_data_len (str, old_len, n_bytes);
2298 
2299  if (!CryptAcquireContext (&hprov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
2300  return FALSE;
2301 
2302  if (!CryptGenRandom (hprov, n_bytes, p))
2303  {
2304  CryptReleaseContext (hprov, 0);
2305  return FALSE;
2306  }
2307 
2308  CryptReleaseContext (hprov, 0);
2309 
2310  return TRUE;
2311 }
2312 
2319 const char*
2321 {
2322  /* Protected by _DBUS_LOCK_sysdeps */
2323  static const char* tmpdir = NULL;
2324  static char buf[1000];
2325 
2326  if (!_DBUS_LOCK (sysdeps))
2327  return NULL;
2328 
2329  if (tmpdir == NULL)
2330  {
2331  char *last_slash;
2332 
2333  if (!GetTempPathA (sizeof (buf), buf))
2334  {
2335  _dbus_warn ("GetTempPath failed\n");
2336  _dbus_abort ();
2337  }
2338 
2339  /* Drop terminating backslash or slash */
2340  last_slash = _mbsrchr (buf, '\\');
2341  if (last_slash > buf && last_slash[1] == '\0')
2342  last_slash[0] = '\0';
2343  last_slash = _mbsrchr (buf, '/');
2344  if (last_slash > buf && last_slash[1] == '\0')
2345  last_slash[0] = '\0';
2346 
2347  tmpdir = buf;
2348  }
2349 
2350  _DBUS_UNLOCK (sysdeps);
2351 
2352  _dbus_assert(tmpdir != NULL);
2353 
2354  return tmpdir;
2355 }
2356 
2357 
2368  DBusError *error)
2369 {
2370  const char *filename_c;
2371 
2372  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2373 
2374  filename_c = _dbus_string_get_const_data (filename);
2375 
2376  if (DeleteFileA (filename_c) == 0)
2377  {
2379  "Failed to delete file %s: %s\n",
2380  filename_c, _dbus_strerror_from_errno ());
2381  return FALSE;
2382  }
2383  else
2384  return TRUE;
2385 }
2386 
2387 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_ENABLE_EMBEDDED_TESTS)
2388 
2389 #if defined(_MSC_VER) || defined(DBUS_WINCE)
2390 # ifdef BACKTRACES
2391 # undef BACKTRACES
2392 # endif
2393 #else
2394 # define BACKTRACES
2395 #endif
2396 
2397 #ifdef BACKTRACES
2398 /*
2399  * Backtrace Generator
2400  *
2401  * Copyright 2004 Eric Poech
2402  * Copyright 2004 Robert Shearman
2403  *
2404  * This library is free software; you can redistribute it and/or
2405  * modify it under the terms of the GNU Lesser General Public
2406  * License as published by the Free Software Foundation; either
2407  * version 2.1 of the License, or (at your option) any later version.
2408  *
2409  * This library is distributed in the hope that it will be useful,
2410  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2411  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2412  * Lesser General Public License for more details.
2413  *
2414  * You should have received a copy of the GNU Lesser General Public
2415  * License along with this library; if not, write to the Free Software
2416  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2417  */
2418 
2419 #include <winver.h>
2420 #include <imagehlp.h>
2421 #include <stdio.h>
2422 
2423 #define DPRINTF _dbus_warn
2424 
2425 #ifdef _MSC_VER
2426 #define BOOL int
2427 
2428 #define __i386__
2429 #endif
2430 
2431 //#define MAKE_FUNCPTR(f) static typeof(f) * p##f
2432 
2433 //MAKE_FUNCPTR(StackWalk);
2434 //MAKE_FUNCPTR(SymGetModuleBase);
2435 //MAKE_FUNCPTR(SymFunctionTableAccess);
2436 //MAKE_FUNCPTR(SymInitialize);
2437 //MAKE_FUNCPTR(SymGetSymFromAddr);
2438 //MAKE_FUNCPTR(SymGetModuleInfo);
2439 static BOOL (WINAPI *pStackWalk)(
2440  DWORD MachineType,
2441  HANDLE hProcess,
2442  HANDLE hThread,
2443  LPSTACKFRAME StackFrame,
2444  PVOID ContextRecord,
2445  PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2446  PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2447  PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2448  PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2449 );
2450 #ifdef _WIN64
2451 static DWORD64 (WINAPI *pSymGetModuleBase)(
2452  HANDLE hProcess,
2453  DWORD64 dwAddr
2454 );
2455 static PVOID (WINAPI *pSymFunctionTableAccess)(
2456  HANDLE hProcess,
2457  DWORD64 AddrBase
2458 );
2459 #else
2460 static DWORD (WINAPI *pSymGetModuleBase)(
2461  HANDLE hProcess,
2462  DWORD dwAddr
2463 );
2464 static PVOID (WINAPI *pSymFunctionTableAccess)(
2465  HANDLE hProcess,
2466  DWORD AddrBase
2467 );
2468 #endif
2469 static BOOL (WINAPI *pSymInitialize)(
2470  HANDLE hProcess,
2471  PSTR UserSearchPath,
2472  BOOL fInvadeProcess
2473 );
2474 static BOOL (WINAPI *pSymGetSymFromAddr)(
2475  HANDLE hProcess,
2476  DWORD Address,
2477  PDWORD Displacement,
2478  PIMAGEHLP_SYMBOL Symbol
2479 );
2480 static BOOL (WINAPI *pSymGetModuleInfo)(
2481  HANDLE hProcess,
2482  DWORD dwAddr,
2483  PIMAGEHLP_MODULE ModuleInfo
2484 );
2485 static DWORD (WINAPI *pSymSetOptions)(
2486  DWORD SymOptions
2487 );
2488 
2489 
2490 static BOOL init_backtrace()
2491 {
2492  HMODULE hmodDbgHelp = LoadLibraryA("dbghelp");
2493 /*
2494  #define GETFUNC(x) \
2495  p##x = (typeof(x)*)GetProcAddress(hmodDbgHelp, #x); \
2496  if (!p##x) \
2497  { \
2498  return FALSE; \
2499  }
2500  */
2501 
2502 
2503 // GETFUNC(StackWalk);
2504 // GETFUNC(SymGetModuleBase);
2505 // GETFUNC(SymFunctionTableAccess);
2506 // GETFUNC(SymInitialize);
2507 // GETFUNC(SymGetSymFromAddr);
2508 // GETFUNC(SymGetModuleInfo);
2509 
2510 #define FUNC(x) #x
2511 
2512  pStackWalk = (BOOL (WINAPI *)(
2513 DWORD MachineType,
2514 HANDLE hProcess,
2515 HANDLE hThread,
2516 LPSTACKFRAME StackFrame,
2517 PVOID ContextRecord,
2518 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2519 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2520 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2521 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2522 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
2523 #ifdef _WIN64
2524  pSymGetModuleBase=(DWORD64 (WINAPI *)(
2525  HANDLE hProcess,
2526  DWORD64 dwAddr
2527 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
2528  pSymFunctionTableAccess=(PVOID (WINAPI *)(
2529  HANDLE hProcess,
2530  DWORD64 AddrBase
2531 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
2532 #else
2533  pSymGetModuleBase=(DWORD (WINAPI *)(
2534  HANDLE hProcess,
2535  DWORD dwAddr
2536 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
2537  pSymFunctionTableAccess=(PVOID (WINAPI *)(
2538  HANDLE hProcess,
2539  DWORD AddrBase
2540 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
2541 #endif
2542  pSymInitialize = (BOOL (WINAPI *)(
2543  HANDLE hProcess,
2544  PSTR UserSearchPath,
2545  BOOL fInvadeProcess
2546 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
2547  pSymGetSymFromAddr = (BOOL (WINAPI *)(
2548  HANDLE hProcess,
2549  DWORD Address,
2550  PDWORD Displacement,
2551  PIMAGEHLP_SYMBOL Symbol
2552 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
2553  pSymGetModuleInfo = (BOOL (WINAPI *)(
2554  HANDLE hProcess,
2555  DWORD dwAddr,
2556  PIMAGEHLP_MODULE ModuleInfo
2557 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
2558 pSymSetOptions = (DWORD (WINAPI *)(
2559 DWORD SymOptions
2560 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
2561 
2562 
2563  pSymSetOptions(SYMOPT_UNDNAME);
2564 
2565  pSymInitialize(GetCurrentProcess(), NULL, TRUE);
2566 
2567  return TRUE;
2568 }
2569 
2570 static void dump_backtrace_for_thread(HANDLE hThread)
2571 {
2572  STACKFRAME sf;
2573  CONTEXT context;
2574  DWORD dwImageType;
2575 
2576  if (!pStackWalk)
2577  if (!init_backtrace())
2578  return;
2579 
2580  /* can't use this function for current thread as GetThreadContext
2581  * doesn't support getting context from current thread */
2582  if (hThread == GetCurrentThread())
2583  return;
2584 
2585  DPRINTF("Backtrace:\n");
2586 
2587  _DBUS_ZERO(context);
2588  context.ContextFlags = CONTEXT_FULL;
2589 
2590  SuspendThread(hThread);
2591 
2592  if (!GetThreadContext(hThread, &context))
2593  {
2594  DPRINTF("Couldn't get thread context (error %ld)\n", GetLastError());
2595  ResumeThread(hThread);
2596  return;
2597  }
2598 
2599  _DBUS_ZERO(sf);
2600 
2601 #ifdef __i386__
2602  sf.AddrFrame.Offset = context.Ebp;
2603  sf.AddrFrame.Mode = AddrModeFlat;
2604  sf.AddrPC.Offset = context.Eip;
2605  sf.AddrPC.Mode = AddrModeFlat;
2606  dwImageType = IMAGE_FILE_MACHINE_I386;
2607 #elif _M_X64
2608  dwImageType = IMAGE_FILE_MACHINE_AMD64;
2609  sf.AddrPC.Offset = context.Rip;
2610  sf.AddrPC.Mode = AddrModeFlat;
2611  sf.AddrFrame.Offset = context.Rsp;
2612  sf.AddrFrame.Mode = AddrModeFlat;
2613  sf.AddrStack.Offset = context.Rsp;
2614  sf.AddrStack.Mode = AddrModeFlat;
2615 #elif _M_IA64
2616  dwImageType = IMAGE_FILE_MACHINE_IA64;
2617  sf.AddrPC.Offset = context.StIIP;
2618  sf.AddrPC.Mode = AddrModeFlat;
2619  sf.AddrFrame.Offset = context.IntSp;
2620  sf.AddrFrame.Mode = AddrModeFlat;
2621  sf.AddrBStore.Offset= context.RsBSP;
2622  sf.AddrBStore.Mode = AddrModeFlat;
2623  sf.AddrStack.Offset = context.IntSp;
2624  sf.AddrStack.Mode = AddrModeFlat;
2625 #else
2626 # error You need to fill in the STACKFRAME structure for your architecture
2627 #endif
2628 
2629  while (pStackWalk(dwImageType, GetCurrentProcess(),
2630  hThread, &sf, &context, NULL, pSymFunctionTableAccess,
2631  pSymGetModuleBase, NULL))
2632  {
2633  BYTE buffer[256];
2634  IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
2635  DWORD dwDisplacement;
2636 
2637  pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
2638  pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1;
2639 
2640  if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
2641  &dwDisplacement, pSymbol))
2642  {
2643  IMAGEHLP_MODULE ModuleInfo;
2644  ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
2645 
2646  if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
2647  &ModuleInfo))
2648  DPRINTF("1\t%p\n", (void*)sf.AddrPC.Offset);
2649  else
2650  DPRINTF("2\t%s+0x%lx\n", ModuleInfo.ImageName,
2651  sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
2652  }
2653  else if (dwDisplacement)
2654  DPRINTF("3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
2655  else
2656  DPRINTF("4\t%s\n", pSymbol->Name);
2657  }
2658 
2659  ResumeThread(hThread);
2660 }
2661 
2662 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
2663 {
2664  dump_backtrace_for_thread((HANDLE)lpParameter);
2665  return 0;
2666 }
2667 
2668 /* cannot get valid context from current thread, so we have to execute
2669  * backtrace from another thread */
2670 static void dump_backtrace()
2671 {
2672  HANDLE hCurrentThread;
2673  HANDLE hThread;
2674  DWORD dwThreadId;
2675  DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
2676  GetCurrentProcess(), &hCurrentThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
2677  hThread = CreateThread(NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
2678  0, &dwThreadId);
2679  WaitForSingleObject(hThread, INFINITE);
2680  CloseHandle(hThread);
2681  CloseHandle(hCurrentThread);
2682 }
2683 #endif
2684 #endif /* asserts or tests enabled */
2685 
2686 #ifdef BACKTRACES
2688 {
2689  init_backtrace();
2690  dump_backtrace();
2691 }
2692 #else
2693 void _dbus_print_backtrace(void)
2694 {
2695  _dbus_verbose (" D-Bus not compiled with backtrace support\n");
2696 }
2697 #endif
2698 
2699 static dbus_uint32_t fromAscii(char ascii)
2700 {
2701  if(ascii >= '0' && ascii <= '9')
2702  return ascii - '0';
2703  if(ascii >= 'A' && ascii <= 'F')
2704  return ascii - 'A' + 10;
2705  if(ascii >= 'a' && ascii <= 'f')
2706  return ascii - 'a' + 10;
2707  return 0;
2708 }
2709 
2711  dbus_bool_t create_if_not_found,
2712  DBusError *error)
2713 {
2714 #ifdef DBUS_WINCE
2715  return TRUE;
2716  // TODO
2717 #else
2718  HW_PROFILE_INFOA info;
2719  char *lpc = &info.szHwProfileGuid[0];
2720  dbus_uint32_t u;
2721 
2722  // the hw-profile guid lives long enough
2723  if(!GetCurrentHwProfileA(&info))
2724  {
2725  dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); // FIXME
2726  return FALSE;
2727  }
2728 
2729  // Form: {12340001-4980-1920-6788-123456789012}
2730  lpc++;
2731  // 12340001
2732  u = ((fromAscii(lpc[0]) << 0) |
2733  (fromAscii(lpc[1]) << 4) |
2734  (fromAscii(lpc[2]) << 8) |
2735  (fromAscii(lpc[3]) << 12) |
2736  (fromAscii(lpc[4]) << 16) |
2737  (fromAscii(lpc[5]) << 20) |
2738  (fromAscii(lpc[6]) << 24) |
2739  (fromAscii(lpc[7]) << 28));
2740  machine_id->as_uint32s[0] = u;
2741 
2742  lpc += 9;
2743  // 4980-1920
2744  u = ((fromAscii(lpc[0]) << 0) |
2745  (fromAscii(lpc[1]) << 4) |
2746  (fromAscii(lpc[2]) << 8) |
2747  (fromAscii(lpc[3]) << 12) |
2748  (fromAscii(lpc[5]) << 16) |
2749  (fromAscii(lpc[6]) << 20) |
2750  (fromAscii(lpc[7]) << 24) |
2751  (fromAscii(lpc[8]) << 28));
2752  machine_id->as_uint32s[1] = u;
2753 
2754  lpc += 10;
2755  // 6788-1234
2756  u = ((fromAscii(lpc[0]) << 0) |
2757  (fromAscii(lpc[1]) << 4) |
2758  (fromAscii(lpc[2]) << 8) |
2759  (fromAscii(lpc[3]) << 12) |
2760  (fromAscii(lpc[5]) << 16) |
2761  (fromAscii(lpc[6]) << 20) |
2762  (fromAscii(lpc[7]) << 24) |
2763  (fromAscii(lpc[8]) << 28));
2764  machine_id->as_uint32s[2] = u;
2765 
2766  lpc += 9;
2767  // 56789012
2768  u = ((fromAscii(lpc[0]) << 0) |
2769  (fromAscii(lpc[1]) << 4) |
2770  (fromAscii(lpc[2]) << 8) |
2771  (fromAscii(lpc[3]) << 12) |
2772  (fromAscii(lpc[4]) << 16) |
2773  (fromAscii(lpc[5]) << 20) |
2774  (fromAscii(lpc[6]) << 24) |
2775  (fromAscii(lpc[7]) << 28));
2776  machine_id->as_uint32s[3] = u;
2777 #endif
2778  return TRUE;
2779 }
2780 
2781 static
2782 HANDLE _dbus_global_lock (const char *mutexname)
2783 {
2784  HANDLE mutex;
2785  DWORD gotMutex;
2786 
2787  mutex = CreateMutexA( NULL, FALSE, mutexname );
2788  if( !mutex )
2789  {
2790  return FALSE;
2791  }
2792 
2793  gotMutex = WaitForSingleObject( mutex, INFINITE );
2794  switch( gotMutex )
2795  {
2796  case WAIT_ABANDONED:
2797  ReleaseMutex (mutex);
2798  CloseHandle (mutex);
2799  return 0;
2800  case WAIT_FAILED:
2801  case WAIT_TIMEOUT:
2802  return 0;
2803  }
2804 
2805  return mutex;
2806 }
2807 
2808 static
2809 void _dbus_global_unlock (HANDLE mutex)
2810 {
2811  ReleaseMutex (mutex);
2812  CloseHandle (mutex);
2813 }
2814 
2815 // for proper cleanup in dbus-daemon
2816 static HANDLE hDBusDaemonMutex = NULL;
2817 static HANDLE hDBusSharedMem = NULL;
2818 // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
2819 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
2820 // sync _dbus_get_autolaunch_address
2821 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
2822 // mutex to determine if dbus-daemon is already started (per user)
2823 static const char *cDBusDaemonMutex = "DBusDaemonMutex";
2824 // named shm for dbus adress info (per user)
2825 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
2826 
2827 static dbus_bool_t
2828 _dbus_get_install_root_as_hash(DBusString *out)
2829 {
2830  DBusString install_path;
2831 
2832  char path[MAX_PATH*2];
2833  int path_size = sizeof(path);
2834 
2835  if (!_dbus_get_install_root(path,path_size))
2836  return FALSE;
2837 
2838  _dbus_string_init(&install_path);
2839  _dbus_string_append(&install_path,path);
2840 
2841  _dbus_string_init(out);
2842  _dbus_string_tolower_ascii(&install_path,0,_dbus_string_get_length(&install_path));
2843 
2844  if (!_dbus_sha_compute (&install_path, out))
2845  return FALSE;
2846 
2847  return TRUE;
2848 }
2849 
2850 static dbus_bool_t
2851 _dbus_get_address_string (DBusString *out, const char *basestring, const char *scope)
2852 {
2853  _dbus_string_init(out);
2854  _dbus_string_append(out,basestring);
2855 
2856  if (!scope)
2857  {
2858  return TRUE;
2859  }
2860  else if (strcmp(scope,"*install-path") == 0
2861  // for 1.3 compatibility
2862  || strcmp(scope,"install-path") == 0)
2863  {
2864  DBusString temp;
2865  if (!_dbus_get_install_root_as_hash(&temp))
2866  {
2867  _dbus_string_free(out);
2868  return FALSE;
2869  }
2870  _dbus_string_append(out,"-");
2872  _dbus_string_free(&temp);
2873  }
2874  else if (strcmp(scope,"*user") == 0)
2875  {
2876  _dbus_string_append(out,"-");
2878  {
2879  _dbus_string_free(out);
2880  return FALSE;
2881  }
2882  }
2883  else if (strlen(scope) > 0)
2884  {
2885  _dbus_string_append(out,"-");
2886  _dbus_string_append(out,scope);
2887  return TRUE;
2888  }
2889  return TRUE;
2890 }
2891 
2892 static dbus_bool_t
2893 _dbus_get_shm_name (DBusString *out,const char *scope)
2894 {
2895  return _dbus_get_address_string (out,cDBusDaemonAddressInfo,scope);
2896 }
2897 
2898 static dbus_bool_t
2899 _dbus_get_mutex_name (DBusString *out,const char *scope)
2900 {
2901  return _dbus_get_address_string (out,cDBusDaemonMutex,scope);
2902 }
2903 
2905 _dbus_daemon_is_session_bus_address_published (const char *scope)
2906 {
2907  HANDLE lock;
2908  DBusString mutex_name;
2909 
2910  if (!_dbus_get_mutex_name(&mutex_name,scope))
2911  {
2912  _dbus_string_free( &mutex_name );
2913  return FALSE;
2914  }
2915 
2916  if (hDBusDaemonMutex)
2917  return TRUE;
2918 
2919  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
2920  lock = _dbus_global_lock( cUniqueDBusInitMutex );
2921 
2922  // we use CreateMutex instead of OpenMutex because of possible race conditions,
2923  // see http://msdn.microsoft.com/en-us/library/ms684315%28VS.85%29.aspx
2924  hDBusDaemonMutex = CreateMutexA( NULL, FALSE, _dbus_string_get_const_data(&mutex_name) );
2925 
2926  /* The client uses mutex ownership to detect a running server, so the server should do so too.
2927  Fortunally the client deletes the mutex in the lock protected area, so checking presence
2928  will work too. */
2929 
2930  _dbus_global_unlock( lock );
2931 
2932  _dbus_string_free( &mutex_name );
2933 
2934  if (hDBusDaemonMutex == NULL)
2935  return FALSE;
2936  if (GetLastError() == ERROR_ALREADY_EXISTS)
2937  {
2938  CloseHandle(hDBusDaemonMutex);
2939  hDBusDaemonMutex = NULL;
2940  return TRUE;
2941  }
2942  // mutex wasn't created before, so return false.
2943  // We leave the mutex name allocated for later reusage
2944  // in _dbus_daemon_publish_session_bus_address.
2945  return FALSE;
2946 }
2947 
2949 _dbus_daemon_publish_session_bus_address (const char* address, const char *scope)
2950 {
2951  HANDLE lock;
2952  char *shared_addr = NULL;
2953  DBusString shm_name;
2954  DBusString mutex_name;
2955 
2956  _dbus_assert (address);
2957 
2958  if (!_dbus_get_mutex_name(&mutex_name,scope))
2959  {
2960  _dbus_string_free( &mutex_name );
2961  return FALSE;
2962  }
2963 
2964  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
2965  lock = _dbus_global_lock( cUniqueDBusInitMutex );
2966 
2967  if (!hDBusDaemonMutex)
2968  {
2969  hDBusDaemonMutex = CreateMutexA( NULL, FALSE, _dbus_string_get_const_data(&mutex_name) );
2970  }
2971  _dbus_string_free( &mutex_name );
2972 
2973  // acquire the mutex
2974  if (WaitForSingleObject( hDBusDaemonMutex, 10 ) != WAIT_OBJECT_0)
2975  {
2976  _dbus_global_unlock( lock );
2977  CloseHandle( hDBusDaemonMutex );
2978  return FALSE;
2979  }
2980 
2981  if (!_dbus_get_shm_name(&shm_name,scope))
2982  {
2983  _dbus_string_free( &shm_name );
2984  _dbus_global_unlock( lock );
2985  return FALSE;
2986  }
2987 
2988  // create shm
2989  hDBusSharedMem = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
2990  0, strlen( address ) + 1, _dbus_string_get_const_data(&shm_name) );
2991  _dbus_assert( hDBusSharedMem );
2992 
2993  shared_addr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
2994 
2995  _dbus_assert (shared_addr);
2996 
2997  strcpy( shared_addr, address);
2998 
2999  // cleanup
3000  UnmapViewOfFile( shared_addr );
3001 
3002  _dbus_global_unlock( lock );
3003  _dbus_verbose( "published session bus address at %s\n",_dbus_string_get_const_data (&shm_name) );
3004 
3005  _dbus_string_free( &shm_name );
3006  return TRUE;
3007 }
3008 
3009 void
3010 _dbus_daemon_unpublish_session_bus_address (void)
3011 {
3012  HANDLE lock;
3013 
3014  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
3015  lock = _dbus_global_lock( cUniqueDBusInitMutex );
3016 
3017  CloseHandle( hDBusSharedMem );
3018 
3019  hDBusSharedMem = NULL;
3020 
3021  ReleaseMutex( hDBusDaemonMutex );
3022 
3023  CloseHandle( hDBusDaemonMutex );
3024 
3025  hDBusDaemonMutex = NULL;
3026 
3027  _dbus_global_unlock( lock );
3028 }
3029 
3030 static dbus_bool_t
3031 _dbus_get_autolaunch_shm (DBusString *address, DBusString *shm_name)
3032 {
3033  HANDLE sharedMem;
3034  char *shared_addr;
3035  int i;
3036 
3037  // read shm
3038  for(i=0;i<20;++i) {
3039  // we know that dbus-daemon is available, so we wait until shm is available
3040  sharedMem = OpenFileMappingA( FILE_MAP_READ, FALSE, _dbus_string_get_const_data(shm_name));
3041  if( sharedMem == 0 )
3042  Sleep( 100 );
3043  if ( sharedMem != 0)
3044  break;
3045  }
3046 
3047  if( sharedMem == 0 )
3048  return FALSE;
3049 
3050  shared_addr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
3051 
3052  if( !shared_addr )
3053  return FALSE;
3054 
3055  _dbus_string_init( address );
3056 
3057  _dbus_string_append( address, shared_addr );
3058 
3059  // cleanup
3060  UnmapViewOfFile( shared_addr );
3061 
3062  CloseHandle( sharedMem );
3063 
3064  return TRUE;
3065 }
3066 
3067 static dbus_bool_t
3068 _dbus_daemon_already_runs (DBusString *address, DBusString *shm_name, const char *scope)
3069 {
3070  HANDLE lock;
3071  HANDLE daemon;
3072  DBusString mutex_name;
3073  dbus_bool_t bRet = TRUE;
3074 
3075  if (!_dbus_get_mutex_name(&mutex_name,scope))
3076  {
3077  _dbus_string_free( &mutex_name );
3078  return FALSE;
3079  }
3080 
3081  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
3082  lock = _dbus_global_lock( cUniqueDBusInitMutex );
3083 
3084  // do checks
3085  daemon = CreateMutexA( NULL, FALSE, _dbus_string_get_const_data(&mutex_name) );
3086  if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
3087  {
3088  ReleaseMutex (daemon);
3089  CloseHandle (daemon);
3090 
3091  _dbus_global_unlock( lock );
3092  _dbus_string_free( &mutex_name );
3093  return FALSE;
3094  }
3095 
3096  // read shm
3097  bRet = _dbus_get_autolaunch_shm( address, shm_name );
3098 
3099  // cleanup
3100  CloseHandle ( daemon );
3101 
3102  _dbus_global_unlock( lock );
3103  _dbus_string_free( &mutex_name );
3104 
3105  return bRet;
3106 }
3107 
3109 _dbus_get_autolaunch_address (const char *scope, DBusString *address,
3110  DBusError *error)
3111 {
3112  HANDLE mutex;
3113  STARTUPINFOA si;
3114  PROCESS_INFORMATION pi;
3115  dbus_bool_t retval = FALSE;
3116  LPSTR lpFile;
3117  char dbus_exe_path[MAX_PATH];
3118  char dbus_args[MAX_PATH * 2];
3119  const char * daemon_name = DBUS_DAEMON_NAME ".exe";
3120  DBusString shm_name;
3121 
3122  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3123 
3124  if (!_dbus_get_shm_name(&shm_name,scope))
3125  {
3126  dbus_set_error_const (error, DBUS_ERROR_FAILED, "could not determine shm name");
3127  return FALSE;
3128  }
3129 
3130  mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
3131 
3132  if (_dbus_daemon_already_runs(address,&shm_name,scope))
3133  {
3134  _dbus_verbose( "found running dbus daemon at %s\n",
3135  _dbus_string_get_const_data (&shm_name) );
3136  retval = TRUE;
3137  goto out;
3138  }
3139 
3140  if (!SearchPathA(NULL, daemon_name, NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
3141  {
3142  // Look in directory containing dbus shared library
3143  HMODULE hmod;
3144  char dbus_module_path[MAX_PATH];
3145  DWORD rc;
3146 
3147  _dbus_verbose( "did not found dbus daemon executable on default search path, "
3148  "trying path where dbus shared library is located");
3149 
3150  hmod = _dbus_win_get_dll_hmodule();
3151  rc = GetModuleFileNameA(hmod, dbus_module_path, sizeof(dbus_module_path));
3152  if (rc <= 0)
3153  {
3154  dbus_set_error_const (error, DBUS_ERROR_FAILED, "could not retrieve dbus shared library file name");
3155  retval = FALSE;
3156  goto out;
3157  }
3158  else
3159  {
3160  char *ext_idx = strrchr(dbus_module_path, '\\');
3161  if (ext_idx)
3162  *ext_idx = '\0';
3163  if (!SearchPathA(dbus_module_path, daemon_name, NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
3164  {
3165  dbus_set_error_const (error, DBUS_ERROR_FAILED, "could not find dbus-daemon executable");
3166  retval = FALSE;
3167  printf ("please add the path to %s to your PATH environment variable\n", daemon_name);
3168  printf ("or start the daemon manually\n\n");
3169  goto out;
3170  }
3171  _dbus_verbose( "found dbus daemon executable at %s",dbus_module_path);
3172  }
3173  }
3174 
3175 
3176  // Create process
3177  ZeroMemory( &si, sizeof(si) );
3178  si.cb = sizeof(si);
3179  ZeroMemory( &pi, sizeof(pi) );
3180 
3181  _snprintf(dbus_args, sizeof(dbus_args) - 1, "\"%s\" %s", dbus_exe_path, " --session");
3182 
3183 // argv[i] = "--config-file=bus\\session.conf";
3184 // printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
3185  if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
3186  {
3187  CloseHandle (pi.hThread);
3188  CloseHandle (pi.hProcess);
3189  retval = _dbus_get_autolaunch_shm( address, &shm_name );
3190  if (retval == FALSE)
3191  dbus_set_error_const (error, DBUS_ERROR_FAILED, "Failed to get autolaunch address from launched dbus-daemon");
3192  }
3193  else
3194  {
3195  dbus_set_error_const (error, DBUS_ERROR_FAILED, "Failed to launch dbus-daemon");
3196  retval = FALSE;
3197  }
3198 
3199 out:
3200  if (retval)
3201  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3202  else
3203  _DBUS_ASSERT_ERROR_IS_SET (error);
3204 
3205  _dbus_global_unlock (mutex);
3206 
3207  return retval;
3208  }
3209 
3210 
3219  DBusError *error)
3220 {
3221  // TODO
3222  return TRUE;
3223 }
3224 
3234 {
3235  // +/- 1 is needed here!
3236  // no volatile argument with mingw
3237  return InterlockedIncrement (&atomic->value) - 1;
3238 }
3239 
3249 {
3250  // +/- 1 is needed here!
3251  // no volatile argument with mingw
3252  return InterlockedDecrement (&atomic->value) + 1;
3253 }
3254 
3264 {
3265  /* In this situation, GLib issues a MemoryBarrier() and then returns
3266  * atomic->value. However, mingw from mingw.org (not to be confused with
3267  * mingw-w64 from mingw-w64.sf.net) does not have MemoryBarrier in its
3268  * headers, so we have to get a memory barrier some other way.
3269  *
3270  * InterlockedIncrement is older, and is documented on MSDN to be a full
3271  * memory barrier, so let's use that.
3272  */
3273  long dummy = 0;
3274 
3275  InterlockedExchange (&dummy, 1);
3276 
3277  return atomic->value;
3278 }
3279 
3287 void
3289 {
3290 }
3291 
3300 {
3301  return errno == WSAEWOULDBLOCK;
3302 }
3303 
3312 _dbus_get_install_root(char *prefix, int len)
3313 {
3314  //To find the prefix, we cut the filename and also \bin\ if present
3315  DWORD pathLength;
3316  char *lastSlash;
3317  SetLastError( 0 );
3318  pathLength = GetModuleFileNameA(_dbus_win_get_dll_hmodule(), prefix, len);
3319  if ( pathLength == 0 || GetLastError() != 0 ) {
3320  *prefix = '\0';
3321  return FALSE;
3322  }
3323  lastSlash = _mbsrchr(prefix, '\\');
3324  if (lastSlash == NULL) {
3325  *prefix = '\0';
3326  return FALSE;
3327  }
3328  //cut off binary name
3329  lastSlash[1] = 0;
3330 
3331  //cut possible "\\bin"
3332 
3333  //this fails if we are in a double-byte system codepage and the
3334  //folder's name happens to end with the *bytes*
3335  //"\\bin"... (I.e. the second byte of some Han character and then
3336  //the Latin "bin", but that is not likely I think...
3337  if (lastSlash - prefix >= 4 && strnicmp(lastSlash - 4, "\\bin", 4) == 0)
3338  lastSlash[-3] = 0;
3339  else if (lastSlash - prefix >= 10 && strnicmp(lastSlash - 10, "\\bin\\debug", 10) == 0)
3340  lastSlash[-9] = 0;
3341  else if (lastSlash - prefix >= 12 && strnicmp(lastSlash - 12, "\\bin\\release", 12) == 0)
3342  lastSlash[-11] = 0;
3343 
3344  return TRUE;
3345 }
3346 
3360 dbus_bool_t
3361 _dbus_get_config_file_name(DBusString *config_file, char *s)
3362 {
3363  char path[MAX_PATH*2];
3364  int path_size = sizeof(path);
3365 
3366  if (!_dbus_get_install_root(path,path_size))
3367  return FALSE;
3368 
3369  if(strlen(s) + 4 + strlen(path) > sizeof(path)-2)
3370  return FALSE;
3371  strcat(path,"etc\\");
3372  strcat(path,s);
3373  if (_dbus_file_exists(path))
3374  {
3375  // find path from executable
3376  if (!_dbus_string_append (config_file, path))
3377  return FALSE;
3378  }
3379  else
3380  {
3381  if (!_dbus_get_install_root(path,path_size))
3382  return FALSE;
3383  if(strlen(s) + 11 + strlen(path) > sizeof(path)-2)
3384  return FALSE;
3385  strcat(path,"etc\\dbus-1\\");
3386  strcat(path,s);
3387 
3388  if (_dbus_file_exists(path))
3389  {
3390  if (!_dbus_string_append (config_file, path))
3391  return FALSE;
3392  }
3393  else
3394  {
3395  if (!_dbus_get_install_root(path,path_size))
3396  return FALSE;
3397  if(strlen(s) + 4 + strlen(path) > sizeof(path)-2)
3398  return FALSE;
3399  strcat(path,"bus\\");
3400  strcat(path,s);
3401 
3402  if (_dbus_file_exists(path))
3403  {
3404  if (!_dbus_string_append (config_file, path))
3405  return FALSE;
3406  }
3407  }
3408  }
3409  return TRUE;
3410 }
3411 
3412 /* See comment in dbus-sysdeps-unix.c */
3415  DBusString *address,
3416  DBusError *error)
3417 {
3418  /* Probably fill this in with something based on COM? */
3419  *supported = FALSE;
3420  return TRUE;
3421 }
3422 
3438  DBusCredentials *credentials)
3439 {
3440  DBusString homedir;
3441  DBusString dotdir;
3442  const char *homepath;
3443  const char *homedrive;
3444 
3445  _dbus_assert (credentials != NULL);
3447 
3448  if (!_dbus_string_init (&homedir))
3449  return FALSE;
3450 
3451  homedrive = _dbus_getenv("HOMEDRIVE");
3452  if (homedrive != NULL && *homedrive != '\0')
3453  {
3454  _dbus_string_append(&homedir,homedrive);
3455  }
3456 
3457  homepath = _dbus_getenv("HOMEPATH");
3458  if (homepath != NULL && *homepath != '\0')
3459  {
3460  _dbus_string_append(&homedir,homepath);
3461  }
3462 
3463 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
3464  {
3465  const char *override;
3466 
3467  override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
3468  if (override != NULL && *override != '\0')
3469  {
3470  _dbus_string_set_length (&homedir, 0);
3471  if (!_dbus_string_append (&homedir, override))
3472  goto failed;
3473 
3474  _dbus_verbose ("Using fake homedir for testing: %s\n",
3475  _dbus_string_get_const_data (&homedir));
3476  }
3477  else
3478  {
3479  /* Not strictly thread-safe, but if we fail at thread-safety here,
3480  * the worst that will happen is some extra warnings. */
3481  static dbus_bool_t already_warned = FALSE;
3482  if (!already_warned)
3483  {
3484  _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
3485  already_warned = TRUE;
3486  }
3487  }
3488  }
3489 #endif
3490 
3491 #ifdef DBUS_WINCE
3492  /* It's not possible to create a .something directory in Windows CE
3493  using the file explorer. */
3494 #define KEYRING_DIR "dbus-keyrings"
3495 #else
3496 #define KEYRING_DIR ".dbus-keyrings"
3497 #endif
3498 
3499  _dbus_string_init_const (&dotdir, KEYRING_DIR);
3500  if (!_dbus_concat_dir_and_file (&homedir,
3501  &dotdir))
3502  goto failed;
3503 
3504  if (!_dbus_string_copy (&homedir, 0,
3505  directory, _dbus_string_get_length (directory))) {
3506  goto failed;
3507  }
3508 
3509  _dbus_string_free (&homedir);
3510  return TRUE;
3511 
3512  failed:
3513  _dbus_string_free (&homedir);
3514  return FALSE;
3515 }
3516 
3522 dbus_bool_t
3523 _dbus_file_exists (const char *file)
3524 {
3525  DWORD attributes = GetFileAttributesA (file);
3526 
3527  if (attributes != INVALID_FILE_ATTRIBUTES && GetLastError() != ERROR_PATH_NOT_FOUND)
3528  return TRUE;
3529  else
3530  return FALSE;
3531 }
3532 
3540 const char*
3541 _dbus_strerror (int error_number)
3542 {
3543 #ifdef DBUS_WINCE
3544  // TODO
3545  return "unknown";
3546 #else
3547  const char *msg;
3548 
3549  switch (error_number)
3550  {
3551  case WSAEINTR:
3552  return "Interrupted function call";
3553  case WSAEACCES:
3554  return "Permission denied";
3555  case WSAEFAULT:
3556  return "Bad address";
3557  case WSAEINVAL:
3558  return "Invalid argument";
3559  case WSAEMFILE:
3560  return "Too many open files";
3561  case WSAEWOULDBLOCK:
3562  return "Resource temporarily unavailable";
3563  case WSAEINPROGRESS:
3564  return "Operation now in progress";
3565  case WSAEALREADY:
3566  return "Operation already in progress";
3567  case WSAENOTSOCK:
3568  return "Socket operation on nonsocket";
3569  case WSAEDESTADDRREQ:
3570  return "Destination address required";
3571  case WSAEMSGSIZE:
3572  return "Message too long";
3573  case WSAEPROTOTYPE:
3574  return "Protocol wrong type for socket";
3575  case WSAENOPROTOOPT:
3576  return "Bad protocol option";
3577  case WSAEPROTONOSUPPORT:
3578  return "Protocol not supported";
3579  case WSAESOCKTNOSUPPORT:
3580  return "Socket type not supported";
3581  case WSAEOPNOTSUPP:
3582  return "Operation not supported";
3583  case WSAEPFNOSUPPORT:
3584  return "Protocol family not supported";
3585  case WSAEAFNOSUPPORT:
3586  return "Address family not supported by protocol family";
3587  case WSAEADDRINUSE:
3588  return "Address already in use";
3589  case WSAEADDRNOTAVAIL:
3590  return "Cannot assign requested address";
3591  case WSAENETDOWN:
3592  return "Network is down";
3593  case WSAENETUNREACH:
3594  return "Network is unreachable";
3595  case WSAENETRESET:
3596  return "Network dropped connection on reset";
3597  case WSAECONNABORTED:
3598  return "Software caused connection abort";
3599  case WSAECONNRESET:
3600  return "Connection reset by peer";
3601  case WSAENOBUFS:
3602  return "No buffer space available";
3603  case WSAEISCONN:
3604  return "Socket is already connected";
3605  case WSAENOTCONN:
3606  return "Socket is not connected";
3607  case WSAESHUTDOWN:
3608  return "Cannot send after socket shutdown";
3609  case WSAETIMEDOUT:
3610  return "Connection timed out";
3611  case WSAECONNREFUSED:
3612  return "Connection refused";
3613  case WSAEHOSTDOWN:
3614  return "Host is down";
3615  case WSAEHOSTUNREACH:
3616  return "No route to host";
3617  case WSAEPROCLIM:
3618  return "Too many processes";
3619  case WSAEDISCON:
3620  return "Graceful shutdown in progress";
3621  case WSATYPE_NOT_FOUND:
3622  return "Class type not found";
3623  case WSAHOST_NOT_FOUND:
3624  return "Host not found";
3625  case WSATRY_AGAIN:
3626  return "Nonauthoritative host not found";
3627  case WSANO_RECOVERY:
3628  return "This is a nonrecoverable error";
3629  case WSANO_DATA:
3630  return "Valid name, no data record of requested type";
3631  case WSA_INVALID_HANDLE:
3632  return "Specified event object handle is invalid";
3633  case WSA_INVALID_PARAMETER:
3634  return "One or more parameters are invalid";
3635  case WSA_IO_INCOMPLETE:
3636  return "Overlapped I/O event object not in signaled state";
3637  case WSA_IO_PENDING:
3638  return "Overlapped operations will complete later";
3639  case WSA_NOT_ENOUGH_MEMORY:
3640  return "Insufficient memory available";
3641  case WSA_OPERATION_ABORTED:
3642  return "Overlapped operation aborted";
3643 #ifdef WSAINVALIDPROCTABLE
3644 
3645  case WSAINVALIDPROCTABLE:
3646  return "Invalid procedure table from service provider";
3647 #endif
3648 #ifdef WSAINVALIDPROVIDER
3649 
3650  case WSAINVALIDPROVIDER:
3651  return "Invalid service provider version number";
3652 #endif
3653 #ifdef WSAPROVIDERFAILEDINIT
3654 
3655  case WSAPROVIDERFAILEDINIT:
3656  return "Unable to initialize a service provider";
3657 #endif
3658 
3659  case WSASYSCALLFAILURE:
3660  return "System call failure";
3661  }
3662  msg = strerror (error_number);
3663  if (msg == NULL)
3664  msg = "unknown";
3665 
3666  return msg;
3667 #endif //DBUS_WINCE
3668 }
3669 
3677 void
3678 _dbus_win_set_error_from_win_error (DBusError *error,
3679  int code)
3680 {
3681  char *msg;
3682 
3683  /* As we want the English message, use the A API */
3684  FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
3685  FORMAT_MESSAGE_IGNORE_INSERTS |
3686  FORMAT_MESSAGE_FROM_SYSTEM,
3687  NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
3688  (LPSTR) &msg, 0, NULL);
3689  if (msg)
3690  {
3691  char *msg_copy;
3692 
3693  msg_copy = dbus_malloc (strlen (msg));
3694  strcpy (msg_copy, msg);
3695  LocalFree (msg);
3696 
3697  dbus_set_error (error, "win32.error", "%s", msg_copy);
3698  }
3699  else
3700  dbus_set_error (error, "win32.error", "Unknown error code %d or FormatMessage failed", code);
3701 }
3702 
3703 void
3704 _dbus_win_warn_win_error (const char *message,
3705  unsigned long code)
3706 {
3707  DBusError error;
3708 
3709  dbus_error_init (&error);
3710  _dbus_win_set_error_from_win_error (&error, code);
3711  _dbus_warn ("%s: %s\n", message, error.message);
3712  dbus_error_free (&error);
3713 }
3714 
3724  DBusError *error)
3725 {
3726  const char *filename_c;
3727 
3728  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3729 
3730  filename_c = _dbus_string_get_const_data (filename);
3731 
3732  if (RemoveDirectoryA (filename_c) == 0)
3733  {
3734  char *emsg = _dbus_win_error_string (GetLastError ());
3735  dbus_set_error (error, _dbus_win_error_from_last_error (),
3736  "Failed to remove directory %s: %s",
3737  filename_c, emsg);
3738  _dbus_win_free_error_string (emsg);
3739  return FALSE;
3740  }
3741 
3742  return TRUE;
3743 }
3744 
3753 {
3754  if (_dbus_string_get_length (filename) > 0)
3755  return _dbus_string_get_byte (filename, 1) == ':'
3756  || _dbus_string_get_byte (filename, 0) == '\\'
3757  || _dbus_string_get_byte (filename, 0) == '/';
3758  else
3759  return FALSE;
3760 }
3761 
3764 {
3765  return FALSE;
3766 }
3767 
3769 /* tests in dbus-sysdeps-util.c */
3770 
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:918
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:227
#define DBUS_ERROR_FILE_NOT_FOUND
Missing file.
#define DBUS_ERROR_FILE_EXISTS
Existing file and the operation you're using does not silently overwrite.
const char * message
public error message field
Definition: dbus-errors.h:51
dbus_int32_t _dbus_atomic_get(DBusAtomic *atomic)
Atomically get the value of an integer.
#define NULL
A null pointer, defined appropriately for C or C++.
volatile dbus_int32_t value
Value of the atomic integer.
Definition: dbus-sysdeps.h:232
dbus_bool_t _dbus_check_dir_is_private_to_user(DBusString *dir, DBusError *error)
Checks to make sure the given directory is private to the user.
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:601
dbus_bool_t _dbus_string_lengthen(DBusString *str, int additional_length)
Makes a string longer by the given number of bytes.
Definition: dbus-string.c:743
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
dbus_bool_t _dbus_delete_directory(const DBusString *filename, DBusError *error)
Removes a directory; Directory must be empty.
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:58
dbus_uint32_t as_uint32s[DBUS_UUID_LENGTH_WORDS]
guid as four uint32 values
void _dbus_flush_caches(void)
Called when the bus daemon is signaled to reload its configuration; any caches should be nuked...
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_full_duplex_pipe(int *fd1, int *fd2, dbus_bool_t blocking, DBusError *error)
Creates a full-duplex pipe (as in socketpair()).
int _dbus_write_socket_two(int fd, const DBusString *buffer1, int start1, int len1, const DBusString *buffer2, int start2, int len2)
Like _dbus_write() but will use writev() if possible to write both buffers in sequence.
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib...
void _dbus_string_tolower_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to lower case.
Definition: dbus-string.c:2467
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
A portable struct pollfd wrapper.
Definition: dbus-sysdeps.h:310
A globally unique ID ; we have one for each DBusServer, and also one for each machine with libdbus in...
int _dbus_accept(int listen_fd)
Accepts a connection on a listening socket.
unsigned char _dbus_string_get_byte(const DBusString *str, int start)
Gets the byte at the given position.
Definition: dbus-string.c:545
#define _DBUS_POLLIN
There is data to read.
Definition: dbus-sysdeps.h:294
dbus_bool_t _dbus_file_exists(const char *file)
Checks if a file exists.
char * _dbus_string_get_data_len(DBusString *str, int start, int len)
Gets a sub-portion of the raw character buffer from the string.
Definition: dbus-string.c:473
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_pid_t _dbus_getpid(void)
Gets our process ID.
dbus_bool_t _dbus_append_user_from_current_process(DBusString *str)
Append to the string the identity we would like to have when we authenticate, on UNIX this is the cur...
#define DBUS_ERROR_IO_ERROR
Something went wrong reading or writing to a socket, for example.
void _dbus_string_shorten(DBusString *str, int length_to_remove)
Makes a string shorter by the given number of bytes.
Definition: dbus-string.c:763
short events
Events to poll for.
Definition: dbus-sysdeps.h:313
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
Definition: dbus-string.c:1265
dbus_bool_t _dbus_read_credentials_socket(int handle, DBusCredentials *credentials, DBusError *error)
Reads a single byte which must be nul (an error occurs otherwise), and reads unix credentials if avai...
dbus_bool_t _dbus_credentials_add_windows_sid(DBusCredentials *credentials, const char *windows_sid)
Add a Windows user SID to the credentials.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:185
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core).
Definition: dbus-sysdeps.c:77
dbus_bool_t _dbus_get_autolaunch_address(const char *scope, DBusString *address, DBusError *error)
Returns the address of a new session bus.
const char * _dbus_error_from_errno(int error_number)
Converts a UNIX errno, or Windows errno or WinSock error value into a DBusError name.
Definition: dbus-sysdeps.c:614
dbus_bool_t _dbus_path_is_absolute(const DBusString *filename)
Checks whether the filename is an absolute path.
void _dbus_get_monotonic_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:96
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
void _dbus_fd_set_close_on_exec(intptr_t handle)
Sets the file descriptor to be close on exec.
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:461
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
_DBUS_GNUC_EXTENSION typedef unsigned long long dbus_uint64_t
A 64-bit unsigned integer.
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
dbus_bool_t _dbus_make_file_world_readable(const DBusString *filename, DBusError *error)
Makes the file readable by every user in the system.
#define _DBUS_POLLOUT
Writing now will not block.
Definition: dbus-sysdeps.h:298
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:722
dbus_bool_t _dbus_append_keyring_directory_for_credentials(DBusString *directory, DBusCredentials *credentials)
Appends the directory in which a keyring for the given credentials should be stored.
int _dbus_read_socket(int fd, DBusString *buffer, int count)
Socket interface.
Object representing an exception.
Definition: dbus-errors.h:48
dbus_bool_t _dbus_string_validate_utf8(const DBusString *str, int start, int len)
Checks that the given range of the string is valid UTF-8.
Definition: dbus-string.c:2537
void _dbus_disable_sigpipe(void)
signal (SIGPIPE, SIG_IGN);
dbus_bool_t _dbus_send_credentials_socket(int handle, DBusError *error)
Sends a single nul byte with our UNIX credentials as ancillary data.
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
int fd
File descriptor.
Definition: dbus-sysdeps.h:312
dbus_bool_t _dbus_create_directory(const DBusString *filename, DBusError *error)
Creates a directory.
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition: dbus-string.c:1139
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
dbus_bool_t _dbus_credentials_add_from_current_process(DBusCredentials *credentials)
Adds the credentials of the current process to the passed-in credentials object.
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_credentials_add_pid(DBusCredentials *credentials, dbus_pid_t pid)
Add a UNIX process ID to the credentials.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
const char * _dbus_get_tmpdir(void)
Gets the temporary files directory by inspecting the environment variables TMPDIR, TMP, and TEMP in that order.
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:783
unsigned long _dbus_pid_for_log(void)
The only reason this is separate from _dbus_getpid() is to allow it on Windows for logging but not fo...
#define DBUS_INT64_CONSTANT(val)
Declare a 64-bit signed integer constant.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:188
int _dbus_write_socket(int fd, const DBusString *buffer, int start, int len)
Thin wrapper around the write() system call that writes a part of a DBusString and handles EINTR for ...
const char * _dbus_string_get_const_data_len(const DBusString *str, int start, int len)
const version of _dbus_string_get_data_len().
Definition: dbus-string.c:497
void _dbus_exit(int code)
Exit the process, returning the given value.
#define DBUS_ERROR_ACCESS_DENIED
Security restrictions don't allow doing what you're trying to do.
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(void)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted) ...
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_lookup_session_address(dbus_bool_t *supported, DBusString *address, DBusError *error)
Determines the address of the session bus by querying a platform-specific method. ...
#define FALSE
Expands to "0".
dbus_bool_t _dbus_read_local_machine_uuid(DBusGUID *machine_id, dbus_bool_t create_if_not_found, DBusError *error)
Reads the uuid of the machine we're running on from the dbus configuration.
void _dbus_print_backtrace(void)
On GNU libc systems, print a crude backtrace to stderr.
dbus_bool_t _dbus_sha_compute(const DBusString *data, DBusString *ascii_output)
Computes the ASCII hex-encoded shasum of the given data and appends it to the output string...
Definition: dbus-sha.c:483
void dbus_set_error_const(DBusError *error, const char *name, const char *message)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:243
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:785
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
int _dbus_printf_string_upper_bound(const char *format, va_list args)
Measure the message length without terminating nul.
void _dbus_verbose_bytes_of_string(const DBusString *str, int start, int len)
Dump the given part of the string to verbose log.
int dbus_int32_t
A 32-bit signed integer on all platforms.
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a file descriptor.
#define _DBUS_ZERO(object)
Sets all bits in an object to zero.
#define DBUS_ERROR_INVALID_ARGS
Invalid arguments passed to a method call.
short revents
Events that occurred.
Definition: dbus-sysdeps.h:314
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:451
#define DBUS_ERROR_LIMITS_EXCEEDED
Some limited resource is exhausted.
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.
Definition: dbus-string.c:210
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
int _dbus_connect_tcp_socket(const char *host, const char *port, const char *family, DBusError *error)
Creates a socket and connects to a socket at the given host and port.
int _dbus_listen_tcp_socket(const char *host, const char *port, const char *family, DBusString *retport, int **fds_p, DBusError *error)
Creates a socket and binds it to the given path, then listens on the socket.
dbus_bool_t _dbus_generate_random_bytes(DBusString *str, int n_bytes)
Generates the given number of random bytes, using the best mechanism we can come up with...
#define _DBUS_POLLERR
Error condition.
Definition: dbus-sysdeps.h:300
dbus_bool_t _dbus_credentials_add_from_user(DBusCredentials *credentials, const DBusString *username)
Adds the credentials corresponding to the given username.