D-Bus  1.8.20
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  {
2234  if (GetLastError () == ERROR_ALREADY_EXISTS)
2235  return TRUE;
2236 
2238  "Failed to create directory %s: %s\n",
2239  filename_c, _dbus_strerror_from_errno ());
2240  return FALSE;
2241  }
2242  else
2243  return TRUE;
2244 }
2245 
2246 
2257  int n_bytes)
2258 {
2259  int old_len;
2260  char *p;
2261  HCRYPTPROV hprov;
2262 
2263  old_len = _dbus_string_get_length (str);
2264 
2265  if (!_dbus_string_lengthen (str, n_bytes))
2266  return FALSE;
2267 
2268  p = _dbus_string_get_data_len (str, old_len, n_bytes);
2269 
2270  if (!CryptAcquireContext (&hprov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
2271  return FALSE;
2272 
2273  if (!CryptGenRandom (hprov, n_bytes, p))
2274  {
2275  CryptReleaseContext (hprov, 0);
2276  return FALSE;
2277  }
2278 
2279  CryptReleaseContext (hprov, 0);
2280 
2281  return TRUE;
2282 }
2283 
2290 const char*
2292 {
2293  /* Protected by _DBUS_LOCK_sysdeps */
2294  static const char* tmpdir = NULL;
2295  static char buf[1000];
2296 
2297  if (!_DBUS_LOCK (sysdeps))
2298  return NULL;
2299 
2300  if (tmpdir == NULL)
2301  {
2302  char *last_slash;
2303 
2304  if (!GetTempPathA (sizeof (buf), buf))
2305  {
2306  _dbus_warn ("GetTempPath failed\n");
2307  _dbus_abort ();
2308  }
2309 
2310  /* Drop terminating backslash or slash */
2311  last_slash = _mbsrchr (buf, '\\');
2312  if (last_slash > buf && last_slash[1] == '\0')
2313  last_slash[0] = '\0';
2314  last_slash = _mbsrchr (buf, '/');
2315  if (last_slash > buf && last_slash[1] == '\0')
2316  last_slash[0] = '\0';
2317 
2318  tmpdir = buf;
2319  }
2320 
2321  _DBUS_UNLOCK (sysdeps);
2322 
2323  _dbus_assert(tmpdir != NULL);
2324 
2325  return tmpdir;
2326 }
2327 
2328 
2339  DBusError *error)
2340 {
2341  const char *filename_c;
2342 
2343  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2344 
2345  filename_c = _dbus_string_get_const_data (filename);
2346 
2347  if (DeleteFileA (filename_c) == 0)
2348  {
2350  "Failed to delete file %s: %s\n",
2351  filename_c, _dbus_strerror_from_errno ());
2352  return FALSE;
2353  }
2354  else
2355  return TRUE;
2356 }
2357 
2358 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_ENABLE_EMBEDDED_TESTS)
2359 
2360 #if defined(_MSC_VER) || defined(DBUS_WINCE)
2361 # ifdef BACKTRACES
2362 # undef BACKTRACES
2363 # endif
2364 #else
2365 # define BACKTRACES
2366 #endif
2367 
2368 #ifdef BACKTRACES
2369 /*
2370  * Backtrace Generator
2371  *
2372  * Copyright 2004 Eric Poech
2373  * Copyright 2004 Robert Shearman
2374  *
2375  * This library is free software; you can redistribute it and/or
2376  * modify it under the terms of the GNU Lesser General Public
2377  * License as published by the Free Software Foundation; either
2378  * version 2.1 of the License, or (at your option) any later version.
2379  *
2380  * This library is distributed in the hope that it will be useful,
2381  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2382  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2383  * Lesser General Public License for more details.
2384  *
2385  * You should have received a copy of the GNU Lesser General Public
2386  * License along with this library; if not, write to the Free Software
2387  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2388  */
2389 
2390 #include <winver.h>
2391 #include <imagehlp.h>
2392 #include <stdio.h>
2393 
2394 #define DPRINTF _dbus_warn
2395 
2396 #ifdef _MSC_VER
2397 #define BOOL int
2398 
2399 #define __i386__
2400 #endif
2401 
2402 //#define MAKE_FUNCPTR(f) static typeof(f) * p##f
2403 
2404 //MAKE_FUNCPTR(StackWalk);
2405 //MAKE_FUNCPTR(SymGetModuleBase);
2406 //MAKE_FUNCPTR(SymFunctionTableAccess);
2407 //MAKE_FUNCPTR(SymInitialize);
2408 //MAKE_FUNCPTR(SymGetSymFromAddr);
2409 //MAKE_FUNCPTR(SymGetModuleInfo);
2410 static BOOL (WINAPI *pStackWalk)(
2411  DWORD MachineType,
2412  HANDLE hProcess,
2413  HANDLE hThread,
2414  LPSTACKFRAME StackFrame,
2415  PVOID ContextRecord,
2416  PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2417  PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2418  PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2419  PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2420 );
2421 #ifdef _WIN64
2422 static DWORD64 (WINAPI *pSymGetModuleBase)(
2423  HANDLE hProcess,
2424  DWORD64 dwAddr
2425 );
2426 static PVOID (WINAPI *pSymFunctionTableAccess)(
2427  HANDLE hProcess,
2428  DWORD64 AddrBase
2429 );
2430 #else
2431 static DWORD (WINAPI *pSymGetModuleBase)(
2432  HANDLE hProcess,
2433  DWORD dwAddr
2434 );
2435 static PVOID (WINAPI *pSymFunctionTableAccess)(
2436  HANDLE hProcess,
2437  DWORD AddrBase
2438 );
2439 #endif
2440 static BOOL (WINAPI *pSymInitialize)(
2441  HANDLE hProcess,
2442  PSTR UserSearchPath,
2443  BOOL fInvadeProcess
2444 );
2445 static BOOL (WINAPI *pSymGetSymFromAddr)(
2446  HANDLE hProcess,
2447  DWORD Address,
2448  PDWORD Displacement,
2449  PIMAGEHLP_SYMBOL Symbol
2450 );
2451 static BOOL (WINAPI *pSymGetModuleInfo)(
2452  HANDLE hProcess,
2453  DWORD dwAddr,
2454  PIMAGEHLP_MODULE ModuleInfo
2455 );
2456 static DWORD (WINAPI *pSymSetOptions)(
2457  DWORD SymOptions
2458 );
2459 
2460 
2461 static BOOL init_backtrace()
2462 {
2463  HMODULE hmodDbgHelp = LoadLibraryA("dbghelp");
2464 /*
2465  #define GETFUNC(x) \
2466  p##x = (typeof(x)*)GetProcAddress(hmodDbgHelp, #x); \
2467  if (!p##x) \
2468  { \
2469  return FALSE; \
2470  }
2471  */
2472 
2473 
2474 // GETFUNC(StackWalk);
2475 // GETFUNC(SymGetModuleBase);
2476 // GETFUNC(SymFunctionTableAccess);
2477 // GETFUNC(SymInitialize);
2478 // GETFUNC(SymGetSymFromAddr);
2479 // GETFUNC(SymGetModuleInfo);
2480 
2481 #define FUNC(x) #x
2482 
2483  pStackWalk = (BOOL (WINAPI *)(
2484 DWORD MachineType,
2485 HANDLE hProcess,
2486 HANDLE hThread,
2487 LPSTACKFRAME StackFrame,
2488 PVOID ContextRecord,
2489 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2490 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2491 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2492 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2493 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
2494 #ifdef _WIN64
2495  pSymGetModuleBase=(DWORD64 (WINAPI *)(
2496  HANDLE hProcess,
2497  DWORD64 dwAddr
2498 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
2499  pSymFunctionTableAccess=(PVOID (WINAPI *)(
2500  HANDLE hProcess,
2501  DWORD64 AddrBase
2502 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
2503 #else
2504  pSymGetModuleBase=(DWORD (WINAPI *)(
2505  HANDLE hProcess,
2506  DWORD dwAddr
2507 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
2508  pSymFunctionTableAccess=(PVOID (WINAPI *)(
2509  HANDLE hProcess,
2510  DWORD AddrBase
2511 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
2512 #endif
2513  pSymInitialize = (BOOL (WINAPI *)(
2514  HANDLE hProcess,
2515  PSTR UserSearchPath,
2516  BOOL fInvadeProcess
2517 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
2518  pSymGetSymFromAddr = (BOOL (WINAPI *)(
2519  HANDLE hProcess,
2520  DWORD Address,
2521  PDWORD Displacement,
2522  PIMAGEHLP_SYMBOL Symbol
2523 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
2524  pSymGetModuleInfo = (BOOL (WINAPI *)(
2525  HANDLE hProcess,
2526  DWORD dwAddr,
2527  PIMAGEHLP_MODULE ModuleInfo
2528 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
2529 pSymSetOptions = (DWORD (WINAPI *)(
2530 DWORD SymOptions
2531 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
2532 
2533 
2534  pSymSetOptions(SYMOPT_UNDNAME);
2535 
2536  pSymInitialize(GetCurrentProcess(), NULL, TRUE);
2537 
2538  return TRUE;
2539 }
2540 
2541 static void dump_backtrace_for_thread(HANDLE hThread)
2542 {
2543  STACKFRAME sf;
2544  CONTEXT context;
2545  DWORD dwImageType;
2546 
2547  if (!pStackWalk)
2548  if (!init_backtrace())
2549  return;
2550 
2551  /* can't use this function for current thread as GetThreadContext
2552  * doesn't support getting context from current thread */
2553  if (hThread == GetCurrentThread())
2554  return;
2555 
2556  DPRINTF("Backtrace:\n");
2557 
2558  _DBUS_ZERO(context);
2559  context.ContextFlags = CONTEXT_FULL;
2560 
2561  SuspendThread(hThread);
2562 
2563  if (!GetThreadContext(hThread, &context))
2564  {
2565  DPRINTF("Couldn't get thread context (error %ld)\n", GetLastError());
2566  ResumeThread(hThread);
2567  return;
2568  }
2569 
2570  _DBUS_ZERO(sf);
2571 
2572 #ifdef __i386__
2573  sf.AddrFrame.Offset = context.Ebp;
2574  sf.AddrFrame.Mode = AddrModeFlat;
2575  sf.AddrPC.Offset = context.Eip;
2576  sf.AddrPC.Mode = AddrModeFlat;
2577  dwImageType = IMAGE_FILE_MACHINE_I386;
2578 #elif _M_X64
2579  dwImageType = IMAGE_FILE_MACHINE_AMD64;
2580  sf.AddrPC.Offset = context.Rip;
2581  sf.AddrPC.Mode = AddrModeFlat;
2582  sf.AddrFrame.Offset = context.Rsp;
2583  sf.AddrFrame.Mode = AddrModeFlat;
2584  sf.AddrStack.Offset = context.Rsp;
2585  sf.AddrStack.Mode = AddrModeFlat;
2586 #elif _M_IA64
2587  dwImageType = IMAGE_FILE_MACHINE_IA64;
2588  sf.AddrPC.Offset = context.StIIP;
2589  sf.AddrPC.Mode = AddrModeFlat;
2590  sf.AddrFrame.Offset = context.IntSp;
2591  sf.AddrFrame.Mode = AddrModeFlat;
2592  sf.AddrBStore.Offset= context.RsBSP;
2593  sf.AddrBStore.Mode = AddrModeFlat;
2594  sf.AddrStack.Offset = context.IntSp;
2595  sf.AddrStack.Mode = AddrModeFlat;
2596 #else
2597 # error You need to fill in the STACKFRAME structure for your architecture
2598 #endif
2599 
2600  while (pStackWalk(dwImageType, GetCurrentProcess(),
2601  hThread, &sf, &context, NULL, pSymFunctionTableAccess,
2602  pSymGetModuleBase, NULL))
2603  {
2604  BYTE buffer[256];
2605  IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
2606  DWORD dwDisplacement;
2607 
2608  pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
2609  pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1;
2610 
2611  if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
2612  &dwDisplacement, pSymbol))
2613  {
2614  IMAGEHLP_MODULE ModuleInfo;
2615  ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
2616 
2617  if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
2618  &ModuleInfo))
2619  DPRINTF("1\t%p\n", (void*)sf.AddrPC.Offset);
2620  else
2621  DPRINTF("2\t%s+0x%lx\n", ModuleInfo.ImageName,
2622  sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
2623  }
2624  else if (dwDisplacement)
2625  DPRINTF("3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
2626  else
2627  DPRINTF("4\t%s\n", pSymbol->Name);
2628  }
2629 
2630  ResumeThread(hThread);
2631 }
2632 
2633 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
2634 {
2635  dump_backtrace_for_thread((HANDLE)lpParameter);
2636  return 0;
2637 }
2638 
2639 /* cannot get valid context from current thread, so we have to execute
2640  * backtrace from another thread */
2641 static void dump_backtrace()
2642 {
2643  HANDLE hCurrentThread;
2644  HANDLE hThread;
2645  DWORD dwThreadId;
2646  DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
2647  GetCurrentProcess(), &hCurrentThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
2648  hThread = CreateThread(NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
2649  0, &dwThreadId);
2650  WaitForSingleObject(hThread, INFINITE);
2651  CloseHandle(hThread);
2652  CloseHandle(hCurrentThread);
2653 }
2654 #endif
2655 #endif /* asserts or tests enabled */
2656 
2657 #ifdef BACKTRACES
2659 {
2660  init_backtrace();
2661  dump_backtrace();
2662 }
2663 #else
2664 void _dbus_print_backtrace(void)
2665 {
2666  _dbus_verbose (" D-Bus not compiled with backtrace support\n");
2667 }
2668 #endif
2669 
2670 static dbus_uint32_t fromAscii(char ascii)
2671 {
2672  if(ascii >= '0' && ascii <= '9')
2673  return ascii - '0';
2674  if(ascii >= 'A' && ascii <= 'F')
2675  return ascii - 'A' + 10;
2676  if(ascii >= 'a' && ascii <= 'f')
2677  return ascii - 'a' + 10;
2678  return 0;
2679 }
2680 
2682  dbus_bool_t create_if_not_found,
2683  DBusError *error)
2684 {
2685 #ifdef DBUS_WINCE
2686  return TRUE;
2687  // TODO
2688 #else
2689  HW_PROFILE_INFOA info;
2690  char *lpc = &info.szHwProfileGuid[0];
2691  dbus_uint32_t u;
2692 
2693  // the hw-profile guid lives long enough
2694  if(!GetCurrentHwProfileA(&info))
2695  {
2696  dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); // FIXME
2697  return FALSE;
2698  }
2699 
2700  // Form: {12340001-4980-1920-6788-123456789012}
2701  lpc++;
2702  // 12340001
2703  u = ((fromAscii(lpc[0]) << 0) |
2704  (fromAscii(lpc[1]) << 4) |
2705  (fromAscii(lpc[2]) << 8) |
2706  (fromAscii(lpc[3]) << 12) |
2707  (fromAscii(lpc[4]) << 16) |
2708  (fromAscii(lpc[5]) << 20) |
2709  (fromAscii(lpc[6]) << 24) |
2710  (fromAscii(lpc[7]) << 28));
2711  machine_id->as_uint32s[0] = u;
2712 
2713  lpc += 9;
2714  // 4980-1920
2715  u = ((fromAscii(lpc[0]) << 0) |
2716  (fromAscii(lpc[1]) << 4) |
2717  (fromAscii(lpc[2]) << 8) |
2718  (fromAscii(lpc[3]) << 12) |
2719  (fromAscii(lpc[5]) << 16) |
2720  (fromAscii(lpc[6]) << 20) |
2721  (fromAscii(lpc[7]) << 24) |
2722  (fromAscii(lpc[8]) << 28));
2723  machine_id->as_uint32s[1] = u;
2724 
2725  lpc += 10;
2726  // 6788-1234
2727  u = ((fromAscii(lpc[0]) << 0) |
2728  (fromAscii(lpc[1]) << 4) |
2729  (fromAscii(lpc[2]) << 8) |
2730  (fromAscii(lpc[3]) << 12) |
2731  (fromAscii(lpc[5]) << 16) |
2732  (fromAscii(lpc[6]) << 20) |
2733  (fromAscii(lpc[7]) << 24) |
2734  (fromAscii(lpc[8]) << 28));
2735  machine_id->as_uint32s[2] = u;
2736 
2737  lpc += 9;
2738  // 56789012
2739  u = ((fromAscii(lpc[0]) << 0) |
2740  (fromAscii(lpc[1]) << 4) |
2741  (fromAscii(lpc[2]) << 8) |
2742  (fromAscii(lpc[3]) << 12) |
2743  (fromAscii(lpc[4]) << 16) |
2744  (fromAscii(lpc[5]) << 20) |
2745  (fromAscii(lpc[6]) << 24) |
2746  (fromAscii(lpc[7]) << 28));
2747  machine_id->as_uint32s[3] = u;
2748 #endif
2749  return TRUE;
2750 }
2751 
2752 static
2753 HANDLE _dbus_global_lock (const char *mutexname)
2754 {
2755  HANDLE mutex;
2756  DWORD gotMutex;
2757 
2758  mutex = CreateMutexA( NULL, FALSE, mutexname );
2759  if( !mutex )
2760  {
2761  return FALSE;
2762  }
2763 
2764  gotMutex = WaitForSingleObject( mutex, INFINITE );
2765  switch( gotMutex )
2766  {
2767  case WAIT_ABANDONED:
2768  ReleaseMutex (mutex);
2769  CloseHandle (mutex);
2770  return 0;
2771  case WAIT_FAILED:
2772  case WAIT_TIMEOUT:
2773  return 0;
2774  }
2775 
2776  return mutex;
2777 }
2778 
2779 static
2780 void _dbus_global_unlock (HANDLE mutex)
2781 {
2782  ReleaseMutex (mutex);
2783  CloseHandle (mutex);
2784 }
2785 
2786 // for proper cleanup in dbus-daemon
2787 static HANDLE hDBusDaemonMutex = NULL;
2788 static HANDLE hDBusSharedMem = NULL;
2789 // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
2790 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
2791 // sync _dbus_get_autolaunch_address
2792 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
2793 // mutex to determine if dbus-daemon is already started (per user)
2794 static const char *cDBusDaemonMutex = "DBusDaemonMutex";
2795 // named shm for dbus adress info (per user)
2796 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
2797 
2798 static dbus_bool_t
2799 _dbus_get_install_root_as_hash(DBusString *out)
2800 {
2801  DBusString install_path;
2802 
2803  char path[MAX_PATH*2];
2804  int path_size = sizeof(path);
2805 
2806  if (!_dbus_get_install_root(path,path_size))
2807  return FALSE;
2808 
2809  _dbus_string_init(&install_path);
2810  _dbus_string_append(&install_path,path);
2811 
2812  _dbus_string_init(out);
2813  _dbus_string_tolower_ascii(&install_path,0,_dbus_string_get_length(&install_path));
2814 
2815  if (!_dbus_sha_compute (&install_path, out))
2816  return FALSE;
2817 
2818  return TRUE;
2819 }
2820 
2821 static dbus_bool_t
2822 _dbus_get_address_string (DBusString *out, const char *basestring, const char *scope)
2823 {
2824  _dbus_string_init(out);
2825  _dbus_string_append(out,basestring);
2826 
2827  if (!scope)
2828  {
2829  return TRUE;
2830  }
2831  else if (strcmp(scope,"*install-path") == 0
2832  // for 1.3 compatibility
2833  || strcmp(scope,"install-path") == 0)
2834  {
2835  DBusString temp;
2836  if (!_dbus_get_install_root_as_hash(&temp))
2837  {
2838  _dbus_string_free(out);
2839  return FALSE;
2840  }
2841  _dbus_string_append(out,"-");
2843  _dbus_string_free(&temp);
2844  }
2845  else if (strcmp(scope,"*user") == 0)
2846  {
2847  _dbus_string_append(out,"-");
2849  {
2850  _dbus_string_free(out);
2851  return FALSE;
2852  }
2853  }
2854  else if (strlen(scope) > 0)
2855  {
2856  _dbus_string_append(out,"-");
2857  _dbus_string_append(out,scope);
2858  return TRUE;
2859  }
2860  return TRUE;
2861 }
2862 
2863 static dbus_bool_t
2864 _dbus_get_shm_name (DBusString *out,const char *scope)
2865 {
2866  return _dbus_get_address_string (out,cDBusDaemonAddressInfo,scope);
2867 }
2868 
2869 static dbus_bool_t
2870 _dbus_get_mutex_name (DBusString *out,const char *scope)
2871 {
2872  return _dbus_get_address_string (out,cDBusDaemonMutex,scope);
2873 }
2874 
2876 _dbus_daemon_is_session_bus_address_published (const char *scope)
2877 {
2878  HANDLE lock;
2879  DBusString mutex_name;
2880 
2881  if (!_dbus_get_mutex_name(&mutex_name,scope))
2882  {
2883  _dbus_string_free( &mutex_name );
2884  return FALSE;
2885  }
2886 
2887  if (hDBusDaemonMutex)
2888  return TRUE;
2889 
2890  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
2891  lock = _dbus_global_lock( cUniqueDBusInitMutex );
2892 
2893  // we use CreateMutex instead of OpenMutex because of possible race conditions,
2894  // see http://msdn.microsoft.com/en-us/library/ms684315%28VS.85%29.aspx
2895  hDBusDaemonMutex = CreateMutexA( NULL, FALSE, _dbus_string_get_const_data(&mutex_name) );
2896 
2897  /* The client uses mutex ownership to detect a running server, so the server should do so too.
2898  Fortunally the client deletes the mutex in the lock protected area, so checking presence
2899  will work too. */
2900 
2901  _dbus_global_unlock( lock );
2902 
2903  _dbus_string_free( &mutex_name );
2904 
2905  if (hDBusDaemonMutex == NULL)
2906  return FALSE;
2907  if (GetLastError() == ERROR_ALREADY_EXISTS)
2908  {
2909  CloseHandle(hDBusDaemonMutex);
2910  hDBusDaemonMutex = NULL;
2911  return TRUE;
2912  }
2913  // mutex wasn't created before, so return false.
2914  // We leave the mutex name allocated for later reusage
2915  // in _dbus_daemon_publish_session_bus_address.
2916  return FALSE;
2917 }
2918 
2920 _dbus_daemon_publish_session_bus_address (const char* address, const char *scope)
2921 {
2922  HANDLE lock;
2923  char *shared_addr = NULL;
2924  DBusString shm_name;
2925  DBusString mutex_name;
2926 
2927  _dbus_assert (address);
2928 
2929  if (!_dbus_get_mutex_name(&mutex_name,scope))
2930  {
2931  _dbus_string_free( &mutex_name );
2932  return FALSE;
2933  }
2934 
2935  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
2936  lock = _dbus_global_lock( cUniqueDBusInitMutex );
2937 
2938  if (!hDBusDaemonMutex)
2939  {
2940  hDBusDaemonMutex = CreateMutexA( NULL, FALSE, _dbus_string_get_const_data(&mutex_name) );
2941  }
2942  _dbus_string_free( &mutex_name );
2943 
2944  // acquire the mutex
2945  if (WaitForSingleObject( hDBusDaemonMutex, 10 ) != WAIT_OBJECT_0)
2946  {
2947  _dbus_global_unlock( lock );
2948  CloseHandle( hDBusDaemonMutex );
2949  return FALSE;
2950  }
2951 
2952  if (!_dbus_get_shm_name(&shm_name,scope))
2953  {
2954  _dbus_string_free( &shm_name );
2955  _dbus_global_unlock( lock );
2956  return FALSE;
2957  }
2958 
2959  // create shm
2960  hDBusSharedMem = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
2961  0, strlen( address ) + 1, _dbus_string_get_const_data(&shm_name) );
2962  _dbus_assert( hDBusSharedMem );
2963 
2964  shared_addr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
2965 
2966  _dbus_assert (shared_addr);
2967 
2968  strcpy( shared_addr, address);
2969 
2970  // cleanup
2971  UnmapViewOfFile( shared_addr );
2972 
2973  _dbus_global_unlock( lock );
2974  _dbus_verbose( "published session bus address at %s\n",_dbus_string_get_const_data (&shm_name) );
2975 
2976  _dbus_string_free( &shm_name );
2977  return TRUE;
2978 }
2979 
2980 void
2981 _dbus_daemon_unpublish_session_bus_address (void)
2982 {
2983  HANDLE lock;
2984 
2985  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
2986  lock = _dbus_global_lock( cUniqueDBusInitMutex );
2987 
2988  CloseHandle( hDBusSharedMem );
2989 
2990  hDBusSharedMem = NULL;
2991 
2992  ReleaseMutex( hDBusDaemonMutex );
2993 
2994  CloseHandle( hDBusDaemonMutex );
2995 
2996  hDBusDaemonMutex = NULL;
2997 
2998  _dbus_global_unlock( lock );
2999 }
3000 
3001 static dbus_bool_t
3002 _dbus_get_autolaunch_shm (DBusString *address, DBusString *shm_name)
3003 {
3004  HANDLE sharedMem;
3005  char *shared_addr;
3006  int i;
3007 
3008  // read shm
3009  for(i=0;i<20;++i) {
3010  // we know that dbus-daemon is available, so we wait until shm is available
3011  sharedMem = OpenFileMappingA( FILE_MAP_READ, FALSE, _dbus_string_get_const_data(shm_name));
3012  if( sharedMem == 0 )
3013  Sleep( 100 );
3014  if ( sharedMem != 0)
3015  break;
3016  }
3017 
3018  if( sharedMem == 0 )
3019  return FALSE;
3020 
3021  shared_addr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
3022 
3023  if( !shared_addr )
3024  return FALSE;
3025 
3026  _dbus_string_init( address );
3027 
3028  _dbus_string_append( address, shared_addr );
3029 
3030  // cleanup
3031  UnmapViewOfFile( shared_addr );
3032 
3033  CloseHandle( sharedMem );
3034 
3035  return TRUE;
3036 }
3037 
3038 static dbus_bool_t
3039 _dbus_daemon_already_runs (DBusString *address, DBusString *shm_name, const char *scope)
3040 {
3041  HANDLE lock;
3042  HANDLE daemon;
3043  DBusString mutex_name;
3044  dbus_bool_t bRet = TRUE;
3045 
3046  if (!_dbus_get_mutex_name(&mutex_name,scope))
3047  {
3048  _dbus_string_free( &mutex_name );
3049  return FALSE;
3050  }
3051 
3052  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
3053  lock = _dbus_global_lock( cUniqueDBusInitMutex );
3054 
3055  // do checks
3056  daemon = CreateMutexA( NULL, FALSE, _dbus_string_get_const_data(&mutex_name) );
3057  if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
3058  {
3059  ReleaseMutex (daemon);
3060  CloseHandle (daemon);
3061 
3062  _dbus_global_unlock( lock );
3063  _dbus_string_free( &mutex_name );
3064  return FALSE;
3065  }
3066 
3067  // read shm
3068  bRet = _dbus_get_autolaunch_shm( address, shm_name );
3069 
3070  // cleanup
3071  CloseHandle ( daemon );
3072 
3073  _dbus_global_unlock( lock );
3074  _dbus_string_free( &mutex_name );
3075 
3076  return bRet;
3077 }
3078 
3080 _dbus_get_autolaunch_address (const char *scope, DBusString *address,
3081  DBusError *error)
3082 {
3083  HANDLE mutex;
3084  STARTUPINFOA si;
3085  PROCESS_INFORMATION pi;
3086  dbus_bool_t retval = FALSE;
3087  LPSTR lpFile;
3088  char dbus_exe_path[MAX_PATH];
3089  char dbus_args[MAX_PATH * 2];
3090  const char * daemon_name = DBUS_DAEMON_NAME ".exe";
3091  DBusString shm_name;
3092 
3093  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3094 
3095  if (!_dbus_get_shm_name(&shm_name,scope))
3096  {
3097  dbus_set_error_const (error, DBUS_ERROR_FAILED, "could not determine shm name");
3098  return FALSE;
3099  }
3100 
3101  mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
3102 
3103  if (_dbus_daemon_already_runs(address,&shm_name,scope))
3104  {
3105  _dbus_verbose( "found running dbus daemon at %s\n",
3106  _dbus_string_get_const_data (&shm_name) );
3107  retval = TRUE;
3108  goto out;
3109  }
3110 
3111  if (!SearchPathA(NULL, daemon_name, NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
3112  {
3113  // Look in directory containing dbus shared library
3114  HMODULE hmod;
3115  char dbus_module_path[MAX_PATH];
3116  DWORD rc;
3117 
3118  _dbus_verbose( "did not found dbus daemon executable on default search path, "
3119  "trying path where dbus shared library is located");
3120 
3121  hmod = _dbus_win_get_dll_hmodule();
3122  rc = GetModuleFileNameA(hmod, dbus_module_path, sizeof(dbus_module_path));
3123  if (rc <= 0)
3124  {
3125  dbus_set_error_const (error, DBUS_ERROR_FAILED, "could not retrieve dbus shared library file name");
3126  retval = FALSE;
3127  goto out;
3128  }
3129  else
3130  {
3131  char *ext_idx = strrchr(dbus_module_path, '\\');
3132  if (ext_idx)
3133  *ext_idx = '\0';
3134  if (!SearchPathA(dbus_module_path, daemon_name, NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
3135  {
3136  dbus_set_error_const (error, DBUS_ERROR_FAILED, "could not find dbus-daemon executable");
3137  retval = FALSE;
3138  printf ("please add the path to %s to your PATH environment variable\n", daemon_name);
3139  printf ("or start the daemon manually\n\n");
3140  goto out;
3141  }
3142  _dbus_verbose( "found dbus daemon executable at %s",dbus_module_path);
3143  }
3144  }
3145 
3146 
3147  // Create process
3148  ZeroMemory( &si, sizeof(si) );
3149  si.cb = sizeof(si);
3150  ZeroMemory( &pi, sizeof(pi) );
3151 
3152  _snprintf(dbus_args, sizeof(dbus_args) - 1, "\"%s\" %s", dbus_exe_path, " --session");
3153 
3154 // argv[i] = "--config-file=bus\\session.conf";
3155 // printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
3156  if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
3157  {
3158  CloseHandle (pi.hThread);
3159  CloseHandle (pi.hProcess);
3160  retval = _dbus_get_autolaunch_shm( address, &shm_name );
3161  if (retval == FALSE)
3162  dbus_set_error_const (error, DBUS_ERROR_FAILED, "Failed to get autolaunch address from launched dbus-daemon");
3163  }
3164  else
3165  {
3166  dbus_set_error_const (error, DBUS_ERROR_FAILED, "Failed to launch dbus-daemon");
3167  retval = FALSE;
3168  }
3169 
3170 out:
3171  if (retval)
3172  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3173  else
3174  _DBUS_ASSERT_ERROR_IS_SET (error);
3175 
3176  _dbus_global_unlock (mutex);
3177 
3178  return retval;
3179  }
3180 
3181 
3190  DBusError *error)
3191 {
3192  // TODO
3193  return TRUE;
3194 }
3195 
3205 {
3206  // +/- 1 is needed here!
3207  // no volatile argument with mingw
3208  return InterlockedIncrement (&atomic->value) - 1;
3209 }
3210 
3220 {
3221  // +/- 1 is needed here!
3222  // no volatile argument with mingw
3223  return InterlockedDecrement (&atomic->value) + 1;
3224 }
3225 
3235 {
3236  /* In this situation, GLib issues a MemoryBarrier() and then returns
3237  * atomic->value. However, mingw from mingw.org (not to be confused with
3238  * mingw-w64 from mingw-w64.sf.net) does not have MemoryBarrier in its
3239  * headers, so we have to get a memory barrier some other way.
3240  *
3241  * InterlockedIncrement is older, and is documented on MSDN to be a full
3242  * memory barrier, so let's use that.
3243  */
3244  long dummy = 0;
3245 
3246  InterlockedExchange (&dummy, 1);
3247 
3248  return atomic->value;
3249 }
3250 
3258 void
3260 {
3261 }
3262 
3271 {
3272  return errno == WSAEWOULDBLOCK;
3273 }
3274 
3283 _dbus_get_install_root(char *prefix, int len)
3284 {
3285  //To find the prefix, we cut the filename and also \bin\ if present
3286  DWORD pathLength;
3287  char *lastSlash;
3288  SetLastError( 0 );
3289  pathLength = GetModuleFileNameA(_dbus_win_get_dll_hmodule(), prefix, len);
3290  if ( pathLength == 0 || GetLastError() != 0 ) {
3291  *prefix = '\0';
3292  return FALSE;
3293  }
3294  lastSlash = _mbsrchr(prefix, '\\');
3295  if (lastSlash == NULL) {
3296  *prefix = '\0';
3297  return FALSE;
3298  }
3299  //cut off binary name
3300  lastSlash[1] = 0;
3301 
3302  //cut possible "\\bin"
3303 
3304  //this fails if we are in a double-byte system codepage and the
3305  //folder's name happens to end with the *bytes*
3306  //"\\bin"... (I.e. the second byte of some Han character and then
3307  //the Latin "bin", but that is not likely I think...
3308  if (lastSlash - prefix >= 4 && strnicmp(lastSlash - 4, "\\bin", 4) == 0)
3309  lastSlash[-3] = 0;
3310  else if (lastSlash - prefix >= 10 && strnicmp(lastSlash - 10, "\\bin\\debug", 10) == 0)
3311  lastSlash[-9] = 0;
3312  else if (lastSlash - prefix >= 12 && strnicmp(lastSlash - 12, "\\bin\\release", 12) == 0)
3313  lastSlash[-11] = 0;
3314 
3315  return TRUE;
3316 }
3317 
3331 dbus_bool_t
3332 _dbus_get_config_file_name(DBusString *config_file, char *s)
3333 {
3334  char path[MAX_PATH*2];
3335  int path_size = sizeof(path);
3336 
3337  if (!_dbus_get_install_root(path,path_size))
3338  return FALSE;
3339 
3340  if(strlen(s) + 4 + strlen(path) > sizeof(path)-2)
3341  return FALSE;
3342  strcat(path,"etc\\");
3343  strcat(path,s);
3344  if (_dbus_file_exists(path))
3345  {
3346  // find path from executable
3347  if (!_dbus_string_append (config_file, path))
3348  return FALSE;
3349  }
3350  else
3351  {
3352  if (!_dbus_get_install_root(path,path_size))
3353  return FALSE;
3354  if(strlen(s) + 11 + strlen(path) > sizeof(path)-2)
3355  return FALSE;
3356  strcat(path,"etc\\dbus-1\\");
3357  strcat(path,s);
3358 
3359  if (_dbus_file_exists(path))
3360  {
3361  if (!_dbus_string_append (config_file, path))
3362  return FALSE;
3363  }
3364  else
3365  {
3366  if (!_dbus_get_install_root(path,path_size))
3367  return FALSE;
3368  if(strlen(s) + 4 + strlen(path) > sizeof(path)-2)
3369  return FALSE;
3370  strcat(path,"bus\\");
3371  strcat(path,s);
3372 
3373  if (_dbus_file_exists(path))
3374  {
3375  if (!_dbus_string_append (config_file, path))
3376  return FALSE;
3377  }
3378  }
3379  }
3380  return TRUE;
3381 }
3382 
3383 /* See comment in dbus-sysdeps-unix.c */
3386  DBusString *address,
3387  DBusError *error)
3388 {
3389  /* Probably fill this in with something based on COM? */
3390  *supported = FALSE;
3391  return TRUE;
3392 }
3393 
3409  DBusCredentials *credentials)
3410 {
3411  DBusString homedir;
3412  DBusString dotdir;
3413  const char *homepath;
3414  const char *homedrive;
3415 
3416  _dbus_assert (credentials != NULL);
3418 
3419  if (!_dbus_string_init (&homedir))
3420  return FALSE;
3421 
3422  homedrive = _dbus_getenv("HOMEDRIVE");
3423  if (homedrive != NULL && *homedrive != '\0')
3424  {
3425  _dbus_string_append(&homedir,homedrive);
3426  }
3427 
3428  homepath = _dbus_getenv("HOMEPATH");
3429  if (homepath != NULL && *homepath != '\0')
3430  {
3431  _dbus_string_append(&homedir,homepath);
3432  }
3433 
3434 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
3435  {
3436  const char *override;
3437 
3438  override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
3439  if (override != NULL && *override != '\0')
3440  {
3441  _dbus_string_set_length (&homedir, 0);
3442  if (!_dbus_string_append (&homedir, override))
3443  goto failed;
3444 
3445  _dbus_verbose ("Using fake homedir for testing: %s\n",
3446  _dbus_string_get_const_data (&homedir));
3447  }
3448  else
3449  {
3450  /* Not strictly thread-safe, but if we fail at thread-safety here,
3451  * the worst that will happen is some extra warnings. */
3452  static dbus_bool_t already_warned = FALSE;
3453  if (!already_warned)
3454  {
3455  _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
3456  already_warned = TRUE;
3457  }
3458  }
3459  }
3460 #endif
3461 
3462 #ifdef DBUS_WINCE
3463  /* It's not possible to create a .something directory in Windows CE
3464  using the file explorer. */
3465 #define KEYRING_DIR "dbus-keyrings"
3466 #else
3467 #define KEYRING_DIR ".dbus-keyrings"
3468 #endif
3469 
3470  _dbus_string_init_const (&dotdir, KEYRING_DIR);
3471  if (!_dbus_concat_dir_and_file (&homedir,
3472  &dotdir))
3473  goto failed;
3474 
3475  if (!_dbus_string_copy (&homedir, 0,
3476  directory, _dbus_string_get_length (directory))) {
3477  goto failed;
3478  }
3479 
3480  _dbus_string_free (&homedir);
3481  return TRUE;
3482 
3483  failed:
3484  _dbus_string_free (&homedir);
3485  return FALSE;
3486 }
3487 
3493 dbus_bool_t
3494 _dbus_file_exists (const char *file)
3495 {
3496  DWORD attributes = GetFileAttributesA (file);
3497 
3498  if (attributes != INVALID_FILE_ATTRIBUTES && GetLastError() != ERROR_PATH_NOT_FOUND)
3499  return TRUE;
3500  else
3501  return FALSE;
3502 }
3503 
3511 const char*
3512 _dbus_strerror (int error_number)
3513 {
3514 #ifdef DBUS_WINCE
3515  // TODO
3516  return "unknown";
3517 #else
3518  const char *msg;
3519 
3520  switch (error_number)
3521  {
3522  case WSAEINTR:
3523  return "Interrupted function call";
3524  case WSAEACCES:
3525  return "Permission denied";
3526  case WSAEFAULT:
3527  return "Bad address";
3528  case WSAEINVAL:
3529  return "Invalid argument";
3530  case WSAEMFILE:
3531  return "Too many open files";
3532  case WSAEWOULDBLOCK:
3533  return "Resource temporarily unavailable";
3534  case WSAEINPROGRESS:
3535  return "Operation now in progress";
3536  case WSAEALREADY:
3537  return "Operation already in progress";
3538  case WSAENOTSOCK:
3539  return "Socket operation on nonsocket";
3540  case WSAEDESTADDRREQ:
3541  return "Destination address required";
3542  case WSAEMSGSIZE:
3543  return "Message too long";
3544  case WSAEPROTOTYPE:
3545  return "Protocol wrong type for socket";
3546  case WSAENOPROTOOPT:
3547  return "Bad protocol option";
3548  case WSAEPROTONOSUPPORT:
3549  return "Protocol not supported";
3550  case WSAESOCKTNOSUPPORT:
3551  return "Socket type not supported";
3552  case WSAEOPNOTSUPP:
3553  return "Operation not supported";
3554  case WSAEPFNOSUPPORT:
3555  return "Protocol family not supported";
3556  case WSAEAFNOSUPPORT:
3557  return "Address family not supported by protocol family";
3558  case WSAEADDRINUSE:
3559  return "Address already in use";
3560  case WSAEADDRNOTAVAIL:
3561  return "Cannot assign requested address";
3562  case WSAENETDOWN:
3563  return "Network is down";
3564  case WSAENETUNREACH:
3565  return "Network is unreachable";
3566  case WSAENETRESET:
3567  return "Network dropped connection on reset";
3568  case WSAECONNABORTED:
3569  return "Software caused connection abort";
3570  case WSAECONNRESET:
3571  return "Connection reset by peer";
3572  case WSAENOBUFS:
3573  return "No buffer space available";
3574  case WSAEISCONN:
3575  return "Socket is already connected";
3576  case WSAENOTCONN:
3577  return "Socket is not connected";
3578  case WSAESHUTDOWN:
3579  return "Cannot send after socket shutdown";
3580  case WSAETIMEDOUT:
3581  return "Connection timed out";
3582  case WSAECONNREFUSED:
3583  return "Connection refused";
3584  case WSAEHOSTDOWN:
3585  return "Host is down";
3586  case WSAEHOSTUNREACH:
3587  return "No route to host";
3588  case WSAEPROCLIM:
3589  return "Too many processes";
3590  case WSAEDISCON:
3591  return "Graceful shutdown in progress";
3592  case WSATYPE_NOT_FOUND:
3593  return "Class type not found";
3594  case WSAHOST_NOT_FOUND:
3595  return "Host not found";
3596  case WSATRY_AGAIN:
3597  return "Nonauthoritative host not found";
3598  case WSANO_RECOVERY:
3599  return "This is a nonrecoverable error";
3600  case WSANO_DATA:
3601  return "Valid name, no data record of requested type";
3602  case WSA_INVALID_HANDLE:
3603  return "Specified event object handle is invalid";
3604  case WSA_INVALID_PARAMETER:
3605  return "One or more parameters are invalid";
3606  case WSA_IO_INCOMPLETE:
3607  return "Overlapped I/O event object not in signaled state";
3608  case WSA_IO_PENDING:
3609  return "Overlapped operations will complete later";
3610  case WSA_NOT_ENOUGH_MEMORY:
3611  return "Insufficient memory available";
3612  case WSA_OPERATION_ABORTED:
3613  return "Overlapped operation aborted";
3614 #ifdef WSAINVALIDPROCTABLE
3615 
3616  case WSAINVALIDPROCTABLE:
3617  return "Invalid procedure table from service provider";
3618 #endif
3619 #ifdef WSAINVALIDPROVIDER
3620 
3621  case WSAINVALIDPROVIDER:
3622  return "Invalid service provider version number";
3623 #endif
3624 #ifdef WSAPROVIDERFAILEDINIT
3625 
3626  case WSAPROVIDERFAILEDINIT:
3627  return "Unable to initialize a service provider";
3628 #endif
3629 
3630  case WSASYSCALLFAILURE:
3631  return "System call failure";
3632  }
3633  msg = strerror (error_number);
3634  if (msg == NULL)
3635  msg = "unknown";
3636 
3637  return msg;
3638 #endif //DBUS_WINCE
3639 }
3640 
3648 void
3649 _dbus_win_set_error_from_win_error (DBusError *error,
3650  int code)
3651 {
3652  char *msg;
3653 
3654  /* As we want the English message, use the A API */
3655  FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
3656  FORMAT_MESSAGE_IGNORE_INSERTS |
3657  FORMAT_MESSAGE_FROM_SYSTEM,
3658  NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
3659  (LPSTR) &msg, 0, NULL);
3660  if (msg)
3661  {
3662  char *msg_copy;
3663 
3664  msg_copy = dbus_malloc (strlen (msg));
3665  strcpy (msg_copy, msg);
3666  LocalFree (msg);
3667 
3668  dbus_set_error (error, "win32.error", "%s", msg_copy);
3669  }
3670  else
3671  dbus_set_error (error, "win32.error", "Unknown error code %d or FormatMessage failed", code);
3672 }
3673 
3674 void
3675 _dbus_win_warn_win_error (const char *message,
3676  unsigned long code)
3677 {
3678  DBusError error;
3679 
3680  dbus_error_init (&error);
3681  _dbus_win_set_error_from_win_error (&error, code);
3682  _dbus_warn ("%s: %s\n", message, error.message);
3683  dbus_error_free (&error);
3684 }
3685 
3695  DBusError *error)
3696 {
3697  const char *filename_c;
3698 
3699  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3700 
3701  filename_c = _dbus_string_get_const_data (filename);
3702 
3703  if (RemoveDirectoryA (filename_c) == 0)
3704  {
3705  char *emsg = _dbus_win_error_string (GetLastError ());
3706  dbus_set_error (error, _dbus_win_error_from_last_error (),
3707  "Failed to remove directory %s: %s",
3708  filename_c, emsg);
3709  _dbus_win_free_error_string (emsg);
3710  return FALSE;
3711  }
3712 
3713  return TRUE;
3714 }
3715 
3724 {
3725  if (_dbus_string_get_length (filename) > 0)
3726  return _dbus_string_get_byte (filename, 1) == ':'
3727  || _dbus_string_get_byte (filename, 0) == '\\'
3728  || _dbus_string_get_byte (filename, 0) == '/';
3729  else
3730  return FALSE;
3731 }
3732 
3735 {
3736  return FALSE;
3737 }
3738 
3740 /* tests in dbus-sysdeps-util.c */
3741 
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 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.
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; succeeds if the directory is created or already existed.
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.