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)
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
6 * Copyright (C) 2005 Novell, Inc.
7 * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
8 * Copyright (C) 2006 Peter Kümmel <syntheticpp@gmx.net>
9 * Copyright (C) 2006 Christian Ehrlicher <ch.ehrlicher@gmx.de>
11 * Licensed under the Academic Free License version 2.1
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.
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.
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
31 #define STRSAFE_NO_DEPRECATE
35 #define _WIN32_WINNT 0x0501
39 #include "dbus-internals.h"
40 #include "dbus-sysdeps.h"
41 #include "dbus-threads.h"
42 #include "dbus-protocol.h"
43 #include "dbus-string.h"
44 #include "dbus-sysdeps-win.h"
45 #include "dbus-protocol.h"
46 #include "dbus-hash.h"
47 #include "dbus-sockets-win.h"
48 #include "dbus-list.h"
49 #include "dbus-credentials.h"
64 #include <sys/types.h>
67 // needed for w2k compatibility (getaddrinfo/freeaddrinfo/getnameinfo)
74 #endif // HAVE_WSPIAPI_H
80 typedef int socklen_t;
83 _dbus_win_error_string (int error_number)
87 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
88 FORMAT_MESSAGE_IGNORE_INSERTS |
89 FORMAT_MESSAGE_FROM_SYSTEM,
90 NULL, error_number, 0,
91 (LPSTR) &msg, 0, NULL);
93 if (msg[strlen (msg) - 1] == '\n')
94 msg[strlen (msg) - 1] = '\0';
95 if (msg[strlen (msg) - 1] == '\r')
96 msg[strlen (msg) - 1] = '\0';
102 _dbus_win_free_error_string (char *string)
108 * write data to a pipe.
110 * @param pipe the pipe instance
111 * @param buffer the buffer to write data from
112 * @param start the first byte in the buffer to write
113 * @param len the number of bytes to try to write
114 * @param error error return
115 * @returns the number of bytes written or -1 on error
118 _dbus_pipe_write (DBusPipe *pipe,
119 const DBusString *buffer,
125 const char *buffer_c = _dbus_string_get_const_data (buffer);
127 written = _write (pipe->fd_or_handle, buffer_c + start, len);
130 dbus_set_error (error, DBUS_ERROR_FAILED,
131 "Writing to pipe: %s\n",
140 * @param pipe the pipe instance
141 * @param error return location for an error
142 * @returns #FALSE if error is set
145 _dbus_pipe_close (DBusPipe *pipe,
148 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
150 if (_close (pipe->fd_or_handle) < 0)
152 dbus_set_error (error, _dbus_error_from_errno (errno),
153 "Could not close pipe %d: %s", pipe->fd_or_handle, strerror (errno));
158 _dbus_pipe_invalidate (pipe);
169 * Thin wrapper around the read() system call that appends
170 * the data it reads to the DBusString buffer. It appends
171 * up to the given count, and returns the same value
172 * and same errno as read(). The only exception is that
173 * _dbus_read() handles EINTR for you. _dbus_read() can
174 * return ENOMEM, even though regular UNIX read doesn't.
176 * @param fd the file descriptor to read from
177 * @param buffer the buffer to append data to
178 * @param count the amount of data to read
179 * @returns the number of bytes read or -1
182 _dbus_read_socket (int fd,
190 _dbus_assert (count >= 0);
192 start = _dbus_string_get_length (buffer);
194 if (!_dbus_string_lengthen (buffer, count))
200 data = _dbus_string_get_data_len (buffer, start, count);
204 _dbus_verbose ("recv: count=%d fd=%d\n", count, fd);
205 bytes_read = recv (fd, data, count, 0);
207 if (bytes_read == SOCKET_ERROR)
209 DBUS_SOCKET_SET_ERRNO();
210 _dbus_verbose ("recv: failed: %s\n", _dbus_strerror (errno));
214 _dbus_verbose ("recv: = %d\n", bytes_read);
222 /* put length back (note that this doesn't actually realloc anything) */
223 _dbus_string_set_length (buffer, start);
229 /* put length back (doesn't actually realloc) */
230 _dbus_string_set_length (buffer, start + bytes_read);
234 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
242 * Thin wrapper around the write() system call that writes a part of a
243 * DBusString and handles EINTR for you.
245 * @param fd the file descriptor to write
246 * @param buffer the buffer to write data from
247 * @param start the first byte in the buffer to write
248 * @param len the number of bytes to try to write
249 * @returns the number of bytes written or -1 on error
252 _dbus_write_socket (int fd,
253 const DBusString *buffer,
260 data = _dbus_string_get_const_data_len (buffer, start, len);
264 _dbus_verbose ("send: len=%d fd=%d\n", len, fd);
265 bytes_written = send (fd, data, len, 0);
267 if (bytes_written == SOCKET_ERROR)
269 DBUS_SOCKET_SET_ERRNO();
270 _dbus_verbose ("send: failed: %s\n", _dbus_strerror (errno));
274 _dbus_verbose ("send: = %d\n", bytes_written);
276 if (bytes_written < 0 && errno == EINTR)
280 if (bytes_written > 0)
281 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
284 return bytes_written;
289 * Closes a file descriptor.
291 * @param fd the file descriptor
292 * @param error error object
293 * @returns #FALSE if error set
296 _dbus_close_socket (int fd,
299 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 if (closesocket (fd) == SOCKET_ERROR)
304 DBUS_SOCKET_SET_ERRNO ();
309 dbus_set_error (error, _dbus_error_from_errno (errno),
310 "Could not close socket: socket=%d, , %s",
311 fd, _dbus_strerror (errno));
314 _dbus_verbose ("_dbus_close_socket: socket=%d, \n", fd);
320 * Sets the file descriptor to be close
321 * on exec. Should be called for all file
322 * descriptors in D-Bus code.
324 * @param fd the file descriptor
327 _dbus_fd_set_close_on_exec (int handle)
329 #ifdef ENABLE_DBUSSOCKET
334 _dbus_lock_sockets();
336 _dbus_handle_to_socket_unlocked (handle, &s);
337 s->close_on_exec = TRUE;
339 _dbus_unlock_sockets();
344 val = fcntl (fd, F_GETFD, 0);
351 fcntl (fd, F_SETFD, val);
357 * Sets a file descriptor to be nonblocking.
359 * @param fd the file descriptor.
360 * @param error address of error location.
361 * @returns #TRUE on success.
364 _dbus_set_fd_nonblocking (int handle,
369 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
371 if (ioctlsocket (handle, FIONBIO, &one) == SOCKET_ERROR)
373 dbus_set_error (error, _dbus_error_from_errno (WSAGetLastError ()),
374 "Failed to set socket %d:%d to nonblocking: %s", handle,
375 _dbus_strerror (WSAGetLastError ()));
384 * Like _dbus_write() but will use writev() if possible
385 * to write both buffers in sequence. The return value
386 * is the number of bytes written in the first buffer,
387 * plus the number written in the second. If the first
388 * buffer is written successfully and an error occurs
389 * writing the second, the number of bytes in the first
390 * is returned (i.e. the error is ignored), on systems that
391 * don't have writev. Handles EINTR for you.
392 * The second buffer may be #NULL.
394 * @param fd the file descriptor
395 * @param buffer1 first buffer
396 * @param start1 first byte to write in first buffer
397 * @param len1 number of bytes to write from first buffer
398 * @param buffer2 second buffer, or #NULL
399 * @param start2 first byte to write in second buffer
400 * @param len2 number of bytes to write in second buffer
401 * @returns total bytes written from both buffers, or -1 on error
404 _dbus_write_socket_two (int fd,
405 const DBusString *buffer1,
408 const DBusString *buffer2,
418 _dbus_assert (buffer1 != NULL);
419 _dbus_assert (start1 >= 0);
420 _dbus_assert (start2 >= 0);
421 _dbus_assert (len1 >= 0);
422 _dbus_assert (len2 >= 0);
425 data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
428 data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
436 vectors[0].buf = (char*) data1;
437 vectors[0].len = len1;
438 vectors[1].buf = (char*) data2;
439 vectors[1].len = len2;
443 _dbus_verbose ("WSASend: len1+2=%d+%d fd=%d\n", len1, len2, fd);
454 DBUS_SOCKET_SET_ERRNO ();
455 _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror (errno));
459 _dbus_verbose ("WSASend: = %ld\n", bytes_written);
461 if (bytes_written < 0 && errno == EINTR)
464 return bytes_written;
470 * Opens the client side of a Windows named pipe. The connection D-BUS
471 * file descriptor index is returned. It is set up as nonblocking.
473 * @param path the path to named pipe socket
474 * @param error return location for error code
475 * @returns connection D-BUS file descriptor or -1 on error
478 _dbus_connect_named_pipe (const char *path,
481 _dbus_assert_not_reached ("not implemented");
489 _dbus_win_startup_winsock (void)
491 /* Straight from MSDN, deuglified */
493 static dbus_bool_t beenhere = FALSE;
495 WORD wVersionRequested;
502 wVersionRequested = MAKEWORD (2, 0);
504 err = WSAStartup (wVersionRequested, &wsaData);
507 _dbus_assert_not_reached ("Could not initialize WinSock");
511 /* Confirm that the WinSock DLL supports 2.0. Note that if the DLL
512 * supports versions greater than 2.0 in addition to 2.0, it will
513 * still return 2.0 in wVersion since that is the version we
516 if (LOBYTE (wsaData.wVersion) != 2 ||
517 HIBYTE (wsaData.wVersion) != 0)
519 _dbus_assert_not_reached ("No usable WinSock found");
534 /************************************************************************
538 ************************************************************************/
541 * Measure the message length without terminating nul
543 int _dbus_printf_string_upper_bound (const char *format,
546 /* MSVCRT's vsnprintf semantics are a bit different */
551 bufsize = sizeof (buf);
552 len = _vsnprintf (buf, bufsize - 1, format, args);
554 while (len == -1) /* try again */
560 p = malloc (bufsize);
561 len = _vsnprintf (p, bufsize - 1, format, args);
570 * Returns the UTF-16 form of a UTF-8 string. The result should be
571 * freed with dbus_free() when no longer needed.
573 * @param str the UTF-8 string
574 * @param error return location for error code
577 _dbus_win_utf8_to_utf16 (const char *str,
584 _dbus_string_init_const (&s, str);
586 if (!_dbus_string_validate_utf8 (&s, 0, _dbus_string_get_length (&s)))
588 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid UTF-8");
592 n = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0);
596 _dbus_win_set_error_from_win_error (error, GetLastError ());
600 retval = dbus_new (wchar_t, n);
604 _DBUS_SET_OOM (error);
608 if (MultiByteToWideChar (CP_UTF8, 0, str, -1, retval, n) != n)
611 dbus_set_error_const (error, DBUS_ERROR_FAILED, "MultiByteToWideChar inconsistency");
619 * Returns the UTF-8 form of a UTF-16 string. The result should be
620 * freed with dbus_free() when no longer needed.
622 * @param str the UTF-16 string
623 * @param error return location for error code
626 _dbus_win_utf16_to_utf8 (const wchar_t *str,
632 n = WideCharToMultiByte (CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
636 _dbus_win_set_error_from_win_error (error, GetLastError ());
640 retval = dbus_malloc (n);
644 _DBUS_SET_OOM (error);
648 if (WideCharToMultiByte (CP_UTF8, 0, str, -1, retval, n, NULL, NULL) != n)
651 dbus_set_error_const (error, DBUS_ERROR_FAILED, "WideCharToMultiByte inconsistency");
663 /************************************************************************
666 ************************************************************************/
669 _dbus_win_account_to_sid (const wchar_t *waccount,
673 dbus_bool_t retval = FALSE;
674 DWORD sid_length, wdomain_length;
682 if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
683 NULL, &wdomain_length, &use) &&
684 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
686 _dbus_win_set_error_from_win_error (error, GetLastError ());
690 *ppsid = dbus_malloc (sid_length);
693 _DBUS_SET_OOM (error);
697 wdomain = dbus_new (wchar_t, wdomain_length);
700 _DBUS_SET_OOM (error);
704 if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
705 wdomain, &wdomain_length, &use))
707 _dbus_win_set_error_from_win_error (error, GetLastError ());
711 if (!IsValidSid ((PSID) *ppsid))
713 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
731 /** @} end of sysdeps-win */
735 * @returns process UID
740 return DBUS_UID_UNSET;
744 * The only reason this is separate from _dbus_getpid() is to allow it
745 * on Windows for logging but not for other purposes.
747 * @returns process ID to put in log messages
750 _dbus_pid_for_log (void)
752 return _dbus_getpid ();
756 * @param points to sid buffer, need to be freed with LocalFree()
757 * @returns process sid
760 _dbus_getsid(char **sid)
762 HANDLE process_token = NULL;
763 TOKEN_USER *token_user = NULL;
768 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
770 _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
773 if ((!GetTokenInformation (process_token, TokenUser, NULL, 0, &n)
774 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
775 || (token_user = alloca (n)) == NULL
776 || !GetTokenInformation (process_token, TokenUser, token_user, n, &n))
778 _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
781 psid = token_user->User.Sid;
782 if (!IsValidSid (psid))
784 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
787 if (!ConvertSidToStringSidA (psid, sid))
789 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
796 if (process_token != NULL)
797 CloseHandle (process_token);
799 _dbus_verbose("_dbus_getsid() returns %d\n",retval);
804 #ifdef DBUS_BUILD_TESTS
806 * @returns process GID
811 return DBUS_GID_UNSET;
816 _dbus_domain_test (const char *test_data_dir)
818 if (!_dbus_test_oom_handling ("spawn_nonexistent",
819 check_spawn_nonexistent,
826 #endif //DBUS_BUILD_TESTS
828 /************************************************************************
832 ************************************************************************/
835 * Creates a full-duplex pipe (as in socketpair()).
836 * Sets both ends of the pipe nonblocking.
838 * @todo libdbus only uses this for the debug-pipe server, so in
839 * principle it could be in dbus-sysdeps-util.c, except that
840 * dbus-sysdeps-util.c isn't in libdbus when tests are enabled and the
841 * debug-pipe server is used.
843 * @param fd1 return location for one end
844 * @param fd2 return location for the other end
845 * @param blocking #TRUE if pipe should be blocking
846 * @param error error return
847 * @returns #FALSE on failure (if error is set)
850 _dbus_full_duplex_pipe (int *fd1,
852 dbus_bool_t blocking,
855 SOCKET temp, socket1 = -1, socket2 = -1;
856 struct sockaddr_in saddr;
859 fd_set read_set, write_set;
862 _dbus_win_startup_winsock ();
864 temp = socket (AF_INET, SOCK_STREAM, 0);
865 if (temp == INVALID_SOCKET)
867 DBUS_SOCKET_SET_ERRNO ();
872 if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR)
874 DBUS_SOCKET_SET_ERRNO ();
879 saddr.sin_family = AF_INET;
881 saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
883 if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)))
885 DBUS_SOCKET_SET_ERRNO ();
889 if (listen (temp, 1) == SOCKET_ERROR)
891 DBUS_SOCKET_SET_ERRNO ();
895 len = sizeof (saddr);
896 if (getsockname (temp, (struct sockaddr *)&saddr, &len))
898 DBUS_SOCKET_SET_ERRNO ();
902 socket1 = socket (AF_INET, SOCK_STREAM, 0);
903 if (socket1 == INVALID_SOCKET)
905 DBUS_SOCKET_SET_ERRNO ();
910 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
912 DBUS_SOCKET_SET_ERRNO ();
916 if (connect (socket1, (struct sockaddr *)&saddr, len) != SOCKET_ERROR ||
917 WSAGetLastError () != WSAEWOULDBLOCK)
919 DBUS_SOCKET_SET_ERRNO ();
924 FD_SET (temp, &read_set);
929 if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR)
931 DBUS_SOCKET_SET_ERRNO ();
935 _dbus_assert (FD_ISSET (temp, &read_set));
937 socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
938 if (socket2 == INVALID_SOCKET)
940 DBUS_SOCKET_SET_ERRNO ();
944 FD_ZERO (&write_set);
945 FD_SET (socket1, &write_set);
950 if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR)
952 DBUS_SOCKET_SET_ERRNO ();
956 _dbus_assert (FD_ISSET (socket1, &write_set));
961 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
963 DBUS_SOCKET_SET_ERRNO ();
968 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
970 DBUS_SOCKET_SET_ERRNO ();
977 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
979 DBUS_SOCKET_SET_ERRNO ();
987 _dbus_verbose ("full-duplex pipe %d:%d <-> %d:%d\n",
988 *fd1, socket1, *fd2, socket2);
995 closesocket (socket2);
997 closesocket (socket1);
1001 dbus_set_error (error, _dbus_error_from_errno (errno),
1002 "Could not setup socket pair: %s",
1003 _dbus_strerror (errno));
1009 * Wrapper for poll().
1011 * @param fds the file descriptors to poll
1012 * @param n_fds number of descriptors in the array
1013 * @param timeout_milliseconds timeout or -1 for infinite
1014 * @returns numbers of fds with revents, or <0 on error
1016 #define USE_CHRIS_IMPL 0
1019 _dbus_poll (DBusPollFD *fds,
1021 int timeout_milliseconds)
1023 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
1024 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
1032 #define DBUS_STACK_WSAEVENTS 256
1033 WSAEVENT eventsOnStack[DBUS_STACK_WSAEVENTS];
1034 WSAEVENT *pEvents = NULL;
1035 if (n_fds > DBUS_STACK_WSAEVENTS)
1036 pEvents = calloc(sizeof(WSAEVENT), n_fds);
1038 pEvents = eventsOnStack;
1041 #ifdef DBUS_ENABLE_VERBOSE_MODE
1043 msgp += sprintf (msgp, "WSAEventSelect: to=%d\n\t", timeout_milliseconds);
1044 for (i = 0; i < n_fds; i++)
1046 static dbus_bool_t warned = FALSE;
1047 DBusPollFD *fdp = &fds[i];
1050 if (fdp->events & _DBUS_POLLIN)
1051 msgp += sprintf (msgp, "R:%d ", fdp->fd);
1053 if (fdp->events & _DBUS_POLLOUT)
1054 msgp += sprintf (msgp, "W:%d ", fdp->fd);
1056 msgp += sprintf (msgp, "E:%d\n\t", fdp->fd);
1058 // FIXME: more robust code for long msg
1059 // create on heap when msg[] becomes too small
1060 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
1062 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
1066 msgp += sprintf (msgp, "\n");
1067 _dbus_verbose ("%s",msg);
1069 for (i = 0; i < n_fds; i++)
1071 DBusPollFD *fdp = &fds[i];
1073 long lNetworkEvents = FD_OOB;
1075 ev = WSACreateEvent();
1077 if (fdp->events & _DBUS_POLLIN)
1078 lNetworkEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
1080 if (fdp->events & _DBUS_POLLOUT)
1081 lNetworkEvents |= FD_WRITE | FD_CONNECT;
1083 WSAEventSelect(fdp->fd, ev, lNetworkEvents);
1089 ready = WSAWaitForMultipleEvents (n_fds, pEvents, FALSE, timeout_milliseconds, FALSE);
1091 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
1093 DBUS_SOCKET_SET_ERRNO ();
1094 if (errno != EWOULDBLOCK)
1095 _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", strerror (errno));
1098 else if (ready == WSA_WAIT_TIMEOUT)
1100 _dbus_verbose ("WSAWaitForMultipleEvents: WSA_WAIT_TIMEOUT\n");
1103 else if (ready >= WSA_WAIT_EVENT_0 && ready < (int)(WSA_WAIT_EVENT_0 + n_fds))
1106 msgp += sprintf (msgp, "WSAWaitForMultipleEvents: =%d\n\t", ready);
1108 for (i = 0; i < n_fds; i++)
1110 DBusPollFD *fdp = &fds[i];
1111 WSANETWORKEVENTS ne;
1115 WSAEnumNetworkEvents(fdp->fd, pEvents[i], &ne);
1117 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1118 fdp->revents |= _DBUS_POLLIN;
1120 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
1121 fdp->revents |= _DBUS_POLLOUT;
1123 if (ne.lNetworkEvents & (FD_OOB))
1124 fdp->revents |= _DBUS_POLLERR;
1126 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1127 msgp += sprintf (msgp, "R:%d ", fdp->fd);
1129 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
1130 msgp += sprintf (msgp, "W:%d ", fdp->fd);
1132 if (ne.lNetworkEvents & (FD_OOB))
1133 msgp += sprintf (msgp, "E:%d ", fdp->fd);
1135 msgp += sprintf (msgp, "lNetworkEvents:%d ", ne.lNetworkEvents);
1137 if(ne.lNetworkEvents)
1140 WSAEventSelect(fdp->fd, pEvents[i], 0);
1143 msgp += sprintf (msgp, "\n");
1144 _dbus_verbose ("%s",msg);
1148 _dbus_verbose ("WSAWaitForMultipleEvents: failed for unknown reason!");
1152 for(i = 0; i < n_fds; i++)
1154 WSACloseEvent(pEvents[i]);
1157 if (n_fds > DBUS_STACK_WSAEVENTS)
1163 #else // USE_CHRIS_IMPL
1166 _dbus_poll (DBusPollFD *fds,
1168 int timeout_milliseconds)
1170 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
1171 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
1174 fd_set read_set, write_set, err_set;
1180 FD_ZERO (&read_set);
1181 FD_ZERO (&write_set);
1185 #ifdef DBUS_ENABLE_VERBOSE_MODE
1187 msgp += sprintf (msgp, "select: to=%d\n\t", timeout_milliseconds);
1188 for (i = 0; i < n_fds; i++)
1190 static dbus_bool_t warned = FALSE;
1191 DBusPollFD *fdp = &fds[i];
1194 if (fdp->events & _DBUS_POLLIN)
1195 msgp += sprintf (msgp, "R:%d ", fdp->fd);
1197 if (fdp->events & _DBUS_POLLOUT)
1198 msgp += sprintf (msgp, "W:%d ", fdp->fd);
1200 msgp += sprintf (msgp, "E:%d\n\t", fdp->fd);
1202 // FIXME: more robust code for long msg
1203 // create on heap when msg[] becomes too small
1204 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
1206 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
1210 msgp += sprintf (msgp, "\n");
1211 _dbus_verbose ("%s",msg);
1213 for (i = 0; i < n_fds; i++)
1215 DBusPollFD *fdp = &fds[i];
1217 if (fdp->events & _DBUS_POLLIN)
1218 FD_SET (fdp->fd, &read_set);
1220 if (fdp->events & _DBUS_POLLOUT)
1221 FD_SET (fdp->fd, &write_set);
1223 FD_SET (fdp->fd, &err_set);
1225 max_fd = MAX (max_fd, fdp->fd);
1229 tv.tv_sec = timeout_milliseconds / 1000;
1230 tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
1232 ready = select (max_fd + 1, &read_set, &write_set, &err_set,
1233 timeout_milliseconds < 0 ? NULL : &tv);
1235 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
1237 DBUS_SOCKET_SET_ERRNO ();
1238 if (errno != EWOULDBLOCK)
1239 _dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno));
1241 else if (ready == 0)
1242 _dbus_verbose ("select: = 0\n");
1246 #ifdef DBUS_ENABLE_VERBOSE_MODE
1248 msgp += sprintf (msgp, "select: = %d:\n\t", ready);
1250 for (i = 0; i < n_fds; i++)
1252 DBusPollFD *fdp = &fds[i];
1254 if (FD_ISSET (fdp->fd, &read_set))
1255 msgp += sprintf (msgp, "R:%d ", fdp->fd);
1257 if (FD_ISSET (fdp->fd, &write_set))
1258 msgp += sprintf (msgp, "W:%d ", fdp->fd);
1260 if (FD_ISSET (fdp->fd, &err_set))
1261 msgp += sprintf (msgp, "E:%d\n\t", fdp->fd);
1263 msgp += sprintf (msgp, "\n");
1264 _dbus_verbose ("%s",msg);
1267 for (i = 0; i < n_fds; i++)
1269 DBusPollFD *fdp = &fds[i];
1273 if (FD_ISSET (fdp->fd, &read_set))
1274 fdp->revents |= _DBUS_POLLIN;
1276 if (FD_ISSET (fdp->fd, &write_set))
1277 fdp->revents |= _DBUS_POLLOUT;
1279 if (FD_ISSET (fdp->fd, &err_set))
1280 fdp->revents |= _DBUS_POLLERR;
1286 #endif // USE_CHRIS_IMPL
1291 /******************************************************************************
1293 Original CVS version of dbus-sysdeps.c
1295 ******************************************************************************/
1296 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
1297 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-Bus implementation)
1299 * Copyright (C) 2002, 2003 Red Hat, Inc.
1300 * Copyright (C) 2003 CodeFactory AB
1301 * Copyright (C) 2005 Novell, Inc.
1303 * Licensed under the Academic Free License version 2.1
1305 * This program is free software; you can redistribute it and/or modify
1306 * it under the terms of the GNU General Public License as published by
1307 * the Free Software Foundation; either version 2 of the License, or
1308 * (at your option) any later version.
1310 * This program is distributed in the hope that it will be useful,
1311 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1312 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313 * GNU General Public License for more details.
1315 * You should have received a copy of the GNU General Public License
1316 * along with this program; if not, write to the Free Software
1317 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1323 * Exit the process, returning the given value.
1325 * @param code the exit code
1328 _dbus_exit (int code)
1334 * Creates a socket and connects to a socket at the given host
1335 * and port. The connection fd is returned, and is set up as
1338 * @param host the host name to connect to
1339 * @param port the port to connect to
1340 * @param family the address family to listen on, NULL for all
1341 * @param error return location for error code
1342 * @returns connection file descriptor or -1 on error
1345 _dbus_connect_tcp_socket (const char *host,
1351 struct addrinfo hints;
1352 struct addrinfo *ai, *tmp;
1354 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1356 _dbus_win_startup_winsock ();
1358 fd = socket (AF_INET, SOCK_STREAM, 0);
1360 if (DBUS_SOCKET_IS_INVALID (fd))
1362 DBUS_SOCKET_SET_ERRNO ();
1363 dbus_set_error (error,
1364 _dbus_error_from_errno (errno),
1365 "Failed to create socket: %s",
1366 _dbus_strerror (errno));
1371 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1376 hints.ai_family = AF_UNSPEC;
1377 else if (!strcmp(family, "ipv4"))
1378 hints.ai_family = AF_INET;
1379 else if (!strcmp(family, "ipv6"))
1380 hints.ai_family = AF_INET6;
1383 dbus_set_error (error,
1384 _dbus_error_from_errno (errno),
1385 "Unknown address family %s", family);
1388 hints.ai_protocol = IPPROTO_TCP;
1389 hints.ai_socktype = SOCK_STREAM;
1390 #ifdef AI_ADDRCONFIG
1391 hints.ai_flags = AI_ADDRCONFIG;
1396 if ((res = getaddrinfo(host, port, &hints, &ai)) != 0)
1398 dbus_set_error (error,
1399 _dbus_error_from_errno (errno),
1400 "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1401 host, port, gai_strerror(res), res);
1409 if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) < 0)
1412 dbus_set_error (error,
1413 _dbus_error_from_errno (errno),
1414 "Failed to open socket: %s",
1415 _dbus_strerror (errno));
1418 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1420 if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0)
1434 dbus_set_error (error,
1435 _dbus_error_from_errno (errno),
1436 "Failed to connect to socket \"%s:%s\" %s",
1437 host, port, _dbus_strerror(errno));
1442 if (!_dbus_set_fd_nonblocking (fd, error))
1455 _dbus_daemon_init(const char *host, dbus_uint32_t port);
1458 * Creates a socket and binds it to the given path, then listens on
1459 * the socket. The socket is set to be nonblocking. In case of port=0
1460 * a random free port is used and returned in the port parameter.
1461 * If inaddr_any is specified, the hostname is ignored.
1463 * @param host the host name to listen on
1464 * @param port the port to listen on, if zero a free port will be used
1465 * @param family the address family to listen on, NULL for all
1466 * @param retport string to return the actual port listened on
1467 * @param fds_p location to store returned file descriptors
1468 * @param error return location for errors
1469 * @returns the number of listening file descriptors or -1 on error
1473 _dbus_listen_tcp_socket (const char *host,
1476 DBusString *retport,
1480 int nlisten_fd = 0, *listen_fd = NULL, res, i, port_num = -1;
1481 struct addrinfo hints;
1482 struct addrinfo *ai, *tmp;
1485 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1487 _dbus_win_startup_winsock ();
1492 hints.ai_family = AF_UNSPEC;
1493 else if (!strcmp(family, "ipv4"))
1494 hints.ai_family = AF_INET;
1495 else if (!strcmp(family, "ipv6"))
1496 hints.ai_family = AF_INET6;
1499 dbus_set_error (error,
1500 _dbus_error_from_errno (errno),
1501 "Unknown address family %s", family);
1505 hints.ai_protocol = IPPROTO_TCP;
1506 hints.ai_socktype = SOCK_STREAM;
1507 #ifdef AI_ADDRCONFIG
1508 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1510 hints.ai_flags = AI_PASSIVE;
1513 redo_lookup_with_port:
1514 if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
1516 dbus_set_error (error,
1517 _dbus_error_from_errno (errno),
1518 "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1519 host ? host : "*", port, gai_strerror(res), res);
1526 int fd = -1, *newlisten_fd;
1527 if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) < 0)
1529 dbus_set_error (error,
1530 _dbus_error_from_errno (errno),
1531 "Failed to open socket: %s",
1532 _dbus_strerror (errno));
1535 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1537 if (bind (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR)
1540 dbus_set_error (error, _dbus_error_from_errno (errno),
1541 "Failed to bind socket \"%s:%s\": %s",
1542 host ? host : "*", port, _dbus_strerror (errno));
1546 if (listen (fd, 30 /* backlog */) == SOCKET_ERROR)
1549 dbus_set_error (error, _dbus_error_from_errno (errno),
1550 "Failed to listen on socket \"%s:%s\": %s",
1551 host ? host : "*", port, _dbus_strerror (errno));
1555 newlisten_fd = dbus_realloc(listen_fd, sizeof(int)*(nlisten_fd+1));
1559 dbus_set_error (error, _dbus_error_from_errno (errno),
1560 "Failed to allocate file handle array: %s",
1561 _dbus_strerror (errno));
1564 listen_fd = newlisten_fd;
1565 listen_fd[nlisten_fd] = fd;
1568 if (!_dbus_string_get_length(retport))
1570 /* If the user didn't specify a port, or used 0, then
1571 the kernel chooses a port. After the first address
1572 is bound to, we need to force all remaining addresses
1573 to use the same port */
1574 if (!port || !strcmp(port, "0"))
1577 socklen_t addrlen = sizeof(addr);
1580 if ((res = getsockname(fd, &addr.Address, &addrlen)) != 0)
1582 dbus_set_error (error, _dbus_error_from_errno (errno),
1583 "Failed to resolve port \"%s:%s\": %s (%d)",
1584 host ? host : "*", port, gai_strerror(res), res);
1587 snprintf( portbuf, sizeof( portbuf ) - 1, "%d", addr.AddressIn.sin_port );
1588 if (!_dbus_string_append(retport, portbuf))
1590 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1594 /* Release current address list & redo lookup */
1595 port = _dbus_string_get_const_data(retport);
1597 goto redo_lookup_with_port;
1601 if (!_dbus_string_append(retport, port))
1603 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1616 errno = WSAEADDRINUSE;
1617 dbus_set_error (error, _dbus_error_from_errno (errno),
1618 "Failed to bind socket \"%s:%s\": %s",
1619 host ? host : "*", port, _dbus_strerror (errno));
1623 sscanf(_dbus_string_get_const_data(retport), "%d", &port_num);
1624 _dbus_daemon_init(host, port_num);
1626 for (i = 0 ; i < nlisten_fd ; i++)
1628 if (!_dbus_set_fd_nonblocking (listen_fd[i], error))
1641 for (i = 0 ; i < nlisten_fd ; i++)
1642 closesocket (listen_fd[i]);
1643 dbus_free(listen_fd);
1649 * Accepts a connection on a listening socket.
1650 * Handles EINTR for you.
1652 * @param listen_fd the listen file descriptor
1653 * @returns the connection fd of the client, or -1 on error
1656 _dbus_accept (int listen_fd)
1661 client_fd = accept (listen_fd, NULL, NULL);
1663 if (DBUS_SOCKET_IS_INVALID (client_fd))
1665 DBUS_SOCKET_SET_ERRNO ();
1670 _dbus_verbose ("client fd %d accepted\n", client_fd);
1679 _dbus_send_credentials_socket (int handle,
1682 /* FIXME: for the session bus credentials shouldn't matter (?), but
1683 * for the system bus they are presumably essential. A rough outline
1684 * of a way to implement the credential transfer would be this:
1686 * client waits to *read* a byte.
1688 * server creates a named pipe with a random name, sends a byte
1689 * contining its length, and its name.
1691 * client reads the name, connects to it (using Win32 API).
1693 * server waits for connection to the named pipe, then calls
1694 * ImpersonateNamedPipeClient(), notes its now-current credentials,
1695 * calls RevertToSelf(), closes its handles to the named pipe, and
1696 * is done. (Maybe there is some other way to get the SID of a named
1697 * pipe client without having to use impersonation?)
1699 * client closes its handles and is done.
1701 * Ralf: Why not sending credentials over the given this connection ?
1702 * Using named pipes makes it impossible to be connected from a unix client.
1708 _dbus_string_init_const_len (&buf, "\0", 1);
1710 bytes_written = _dbus_write_socket (handle, &buf, 0, 1 );
1712 if (bytes_written < 0 && errno == EINTR)
1715 if (bytes_written < 0)
1717 dbus_set_error (error, _dbus_error_from_errno (errno),
1718 "Failed to write credentials byte: %s",
1719 _dbus_strerror (errno));
1722 else if (bytes_written == 0)
1724 dbus_set_error (error, DBUS_ERROR_IO_ERROR,
1725 "wrote zero bytes writing credentials byte");
1730 _dbus_assert (bytes_written == 1);
1731 _dbus_verbose ("wrote 1 zero byte, credential sending isn't implemented yet\n");
1738 * Reads a single byte which must be nul (an error occurs otherwise),
1739 * and reads unix credentials if available. Fills in pid/uid/gid with
1740 * -1 if no credentials are available. Return value indicates whether
1741 * a byte was read, not whether we got valid credentials. On some
1742 * systems, such as Linux, reading/writing the byte isn't actually
1743 * required, but we do it anyway just to avoid multiple codepaths.
1745 * Fails if no byte is available, so you must select() first.
1747 * The point of the byte is that on some systems we have to
1748 * use sendmsg()/recvmsg() to transmit credentials.
1750 * @param client_fd the client file descriptor
1751 * @param credentials struct to fill with credentials of client
1752 * @param error location to store error code
1753 * @returns #TRUE on success
1756 _dbus_read_credentials_socket (int handle,
1757 DBusCredentials *credentials,
1763 // could fail due too OOM
1764 if (_dbus_string_init(&buf))
1766 bytes_read = _dbus_read_socket(handle, &buf, 1 );
1769 _dbus_verbose("got one zero byte from server");
1771 _dbus_string_free(&buf);
1774 _dbus_credentials_add_from_current_process (credentials);
1775 _dbus_verbose("FIXME: get faked credentials from current process");
1781 * Checks to make sure the given directory is
1782 * private to the user
1784 * @param dir the name of the directory
1785 * @param error error return
1786 * @returns #FALSE on failure
1789 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
1791 const char *directory;
1794 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1801 * Appends the given filename to the given directory.
1803 * @todo it might be cute to collapse multiple '/' such as "foo//"
1806 * @param dir the directory name
1807 * @param next_component the filename
1808 * @returns #TRUE on success
1811 _dbus_concat_dir_and_file (DBusString *dir,
1812 const DBusString *next_component)
1814 dbus_bool_t dir_ends_in_slash;
1815 dbus_bool_t file_starts_with_slash;
1817 if (_dbus_string_get_length (dir) == 0 ||
1818 _dbus_string_get_length (next_component) == 0)
1822 ('/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1) ||
1823 '\\' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1));
1825 file_starts_with_slash =
1826 ('/' == _dbus_string_get_byte (next_component, 0) ||
1827 '\\' == _dbus_string_get_byte (next_component, 0));
1829 if (dir_ends_in_slash && file_starts_with_slash)
1831 _dbus_string_shorten (dir, 1);
1833 else if (!(dir_ends_in_slash || file_starts_with_slash))
1835 if (!_dbus_string_append_byte (dir, '\\'))
1839 return _dbus_string_copy (next_component, 0, dir,
1840 _dbus_string_get_length (dir));
1843 /*---------------- DBusCredentials ----------------------------------
1846 * Adds the credentials corresponding to the given username.
1848 * @param credentials credentials to fill in
1849 * @param username the username
1850 * @returns #TRUE if the username existed and we got some credentials
1853 _dbus_credentials_add_from_user (DBusCredentials *credentials,
1854 const DBusString *username)
1856 return _dbus_credentials_add_windows_sid (credentials,
1857 _dbus_string_get_const_data(username));
1861 * Adds the credentials of the current process to the
1862 * passed-in credentials object.
1864 * @param credentials credentials to add to
1865 * @returns #FALSE if no memory; does not properly roll back on failure, so only some credentials may have been added
1869 _dbus_credentials_add_from_current_process (DBusCredentials *credentials)
1871 dbus_bool_t retval = FALSE;
1874 if (!_dbus_getsid(&sid))
1877 if (!_dbus_credentials_add_unix_pid(credentials, _dbus_getpid()))
1880 if (!_dbus_credentials_add_windows_sid (credentials,sid))
1895 * Append to the string the identity we would like to have when we
1896 * authenticate, on UNIX this is the current process UID and on
1897 * Windows something else, probably a Windows SID string. No escaping
1898 * is required, that is done in dbus-auth.c. The username here
1899 * need not be anything human-readable, it can be the machine-readable
1900 * form i.e. a user id.
1902 * @param str the string to append to
1903 * @returns #FALSE on no memory
1904 * @todo to which class belongs this
1907 _dbus_append_user_from_current_process (DBusString *str)
1909 dbus_bool_t retval = FALSE;
1912 if (!_dbus_getsid(&sid))
1915 retval = _dbus_string_append (str,sid);
1922 * Gets our process ID
1923 * @returns process ID
1928 return GetCurrentProcessId ();
1931 /** nanoseconds in a second */
1932 #define NANOSECONDS_PER_SECOND 1000000000
1933 /** microseconds in a second */
1934 #define MICROSECONDS_PER_SECOND 1000000
1935 /** milliseconds in a second */
1936 #define MILLISECONDS_PER_SECOND 1000
1937 /** nanoseconds in a millisecond */
1938 #define NANOSECONDS_PER_MILLISECOND 1000000
1939 /** microseconds in a millisecond */
1940 #define MICROSECONDS_PER_MILLISECOND 1000
1943 * Sleeps the given number of milliseconds.
1944 * @param milliseconds number of milliseconds
1947 _dbus_sleep_milliseconds (int milliseconds)
1949 Sleep (milliseconds);
1954 * Get current time, as in gettimeofday().
1956 * @param tv_sec return location for number of seconds
1957 * @param tv_usec return location for number of microseconds
1960 _dbus_get_current_time (long *tv_sec,
1964 dbus_uint64_t *time64 = (dbus_uint64_t *) &ft;
1966 GetSystemTimeAsFileTime (&ft);
1968 /* Convert from 100s of nanoseconds since 1601-01-01
1969 * to Unix epoch. Yes, this is Y2038 unsafe.
1971 *time64 -= DBUS_INT64_CONSTANT (116444736000000000);
1975 *tv_sec = *time64 / 1000000;
1978 *tv_usec = *time64 % 1000000;
1983 * signal (SIGPIPE, SIG_IGN);
1986 _dbus_disable_sigpipe (void)
1991 /* _dbus_read() is static on Windows, only used below in this file.
2002 _dbus_assert (count >= 0);
2004 start = _dbus_string_get_length (buffer);
2006 if (!_dbus_string_lengthen (buffer, count))
2012 data = _dbus_string_get_data_len (buffer, start, count);
2016 bytes_read = _read (fd, data, count);
2024 /* put length back (note that this doesn't actually realloc anything) */
2025 _dbus_string_set_length (buffer, start);
2031 /* put length back (doesn't actually realloc) */
2032 _dbus_string_set_length (buffer, start + bytes_read);
2036 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
2044 * Appends the contents of the given file to the string,
2045 * returning error code. At the moment, won't open a file
2046 * more than a megabyte in size.
2048 * @param str the string to append to
2049 * @param filename filename to load
2050 * @param error place to set an error
2051 * @returns #FALSE if error was set
2054 _dbus_file_get_contents (DBusString *str,
2055 const DBusString *filename,
2062 const char *filename_c;
2064 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2066 filename_c = _dbus_string_get_const_data (filename);
2068 fd = _open (filename_c, O_RDONLY | O_BINARY);
2071 dbus_set_error (error, _dbus_error_from_errno (errno),
2072 "Failed to open \"%s\": %s",
2078 _dbus_verbose ("file %s fd %d opened\n", filename_c, fd);
2080 if (_fstati64 (fd, &sb) < 0)
2082 dbus_set_error (error, _dbus_error_from_errno (errno),
2083 "Failed to stat \"%s\": %s",
2087 _dbus_verbose ("fstat() failed: %s",
2095 if (sb.st_size > _DBUS_ONE_MEGABYTE)
2097 dbus_set_error (error, DBUS_ERROR_FAILED,
2098 "File size %lu of \"%s\" is too large.",
2099 (unsigned long) sb.st_size, filename_c);
2105 orig_len = _dbus_string_get_length (str);
2106 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
2110 while (total < (int) sb.st_size)
2112 bytes_read = _dbus_read (fd, str, sb.st_size - total);
2113 if (bytes_read <= 0)
2115 dbus_set_error (error, _dbus_error_from_errno (errno),
2116 "Error reading \"%s\": %s",
2120 _dbus_verbose ("read() failed: %s",
2124 _dbus_string_set_length (str, orig_len);
2128 total += bytes_read;
2134 else if (sb.st_size != 0)
2136 _dbus_verbose ("Can only open regular files at the moment.\n");
2137 dbus_set_error (error, DBUS_ERROR_FAILED,
2138 "\"%s\" is not a regular file",
2151 * Writes a string out to a file. If the file exists,
2152 * it will be atomically overwritten by the new data.
2154 * @param str the string to write out
2155 * @param filename the file to save string to
2156 * @param error error to be filled in on failure
2157 * @returns #FALSE on failure
2160 _dbus_string_save_to_file (const DBusString *str,
2161 const DBusString *filename,
2166 const char *filename_c;
2167 DBusString tmp_filename;
2168 const char *tmp_filename_c;
2171 dbus_bool_t need_unlink;
2174 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2178 need_unlink = FALSE;
2180 if (!_dbus_string_init (&tmp_filename))
2182 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2186 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
2188 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2189 _dbus_string_free (&tmp_filename);
2193 if (!_dbus_string_append (&tmp_filename, "."))
2195 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2196 _dbus_string_free (&tmp_filename);
2200 #define N_TMP_FILENAME_RANDOM_BYTES 8
2201 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
2203 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2204 _dbus_string_free (&tmp_filename);
2208 filename_c = _dbus_string_get_const_data (filename);
2209 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
2211 fd = _open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
2215 dbus_set_error (error, _dbus_error_from_errno (errno),
2216 "Could not create %s: %s", tmp_filename_c,
2221 _dbus_verbose ("tmp file %s fd %d opened\n", tmp_filename_c, fd);
2226 bytes_to_write = _dbus_string_get_length (str);
2227 str_c = _dbus_string_get_const_data (str);
2229 while (total < bytes_to_write)
2233 bytes_written = _write (fd, str_c + total, bytes_to_write - total);
2235 if (bytes_written <= 0)
2237 dbus_set_error (error, _dbus_error_from_errno (errno),
2238 "Could not write to %s: %s", tmp_filename_c,
2243 total += bytes_written;
2246 if (_close (fd) < 0)
2248 dbus_set_error (error, _dbus_error_from_errno (errno),
2249 "Could not close file %s: %s",
2250 tmp_filename_c, strerror (errno));
2257 /* Unlike rename(), MoveFileEx() can replace existing files */
2258 if (MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING) < 0)
2260 char *emsg = _dbus_win_error_string (GetLastError ());
2261 dbus_set_error (error, DBUS_ERROR_FAILED,
2262 "Could not rename %s to %s: %s",
2263 tmp_filename_c, filename_c,
2265 _dbus_win_free_error_string (emsg);
2270 need_unlink = FALSE;
2275 /* close first, then unlink */
2280 if (need_unlink && _unlink (tmp_filename_c) < 0)
2281 _dbus_verbose ("failed to unlink temp file %s: %s\n",
2282 tmp_filename_c, strerror (errno));
2284 _dbus_string_free (&tmp_filename);
2287 _DBUS_ASSERT_ERROR_IS_SET (error);
2293 /** Creates the given file, failing if the file already exists.
2295 * @param filename the filename
2296 * @param error error location
2297 * @returns #TRUE if we created the file and it didn't exist
2300 _dbus_create_file_exclusively (const DBusString *filename,
2304 const char *filename_c;
2306 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2308 filename_c = _dbus_string_get_const_data (filename);
2310 fd = _open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
2314 dbus_set_error (error,
2316 "Could not create file %s: %s\n",
2322 _dbus_verbose ("exclusive file %s fd %d opened\n", filename_c, fd);
2324 if (_close (fd) < 0)
2326 dbus_set_error (error,
2328 "Could not close file %s: %s\n",
2339 * Creates a directory; succeeds if the directory
2340 * is created or already existed.
2342 * @param filename directory filename
2343 * @param error initialized error object
2344 * @returns #TRUE on success
2347 _dbus_create_directory (const DBusString *filename,
2350 const char *filename_c;
2352 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2354 filename_c = _dbus_string_get_const_data (filename);
2356 if (!CreateDirectory (filename_c, NULL))
2358 if (GetLastError () == ERROR_ALREADY_EXISTS)
2361 dbus_set_error (error, DBUS_ERROR_FAILED,
2362 "Failed to create directory %s: %s\n",
2363 filename_c, strerror (errno));
2372 * Generates the given number of random bytes,
2373 * using the best mechanism we can come up with.
2375 * @param str the string
2376 * @param n_bytes the number of random bytes to append to string
2377 * @returns #TRUE on success, #FALSE if no memory
2380 _dbus_generate_random_bytes (DBusString *str,
2387 old_len = _dbus_string_get_length (str);
2389 if (!_dbus_string_lengthen (str, n_bytes))
2392 p = _dbus_string_get_data_len (str, old_len, n_bytes);
2394 if (!CryptAcquireContext (&hprov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
2397 if (!CryptGenRandom (hprov, n_bytes, p))
2399 CryptReleaseContext (hprov, 0);
2403 CryptReleaseContext (hprov, 0);
2409 * Gets the temporary files directory by inspecting the environment variables
2410 * TMPDIR, TMP, and TEMP in that order. If none of those are set "/tmp" is returned
2412 * @returns location of temp directory
2415 _dbus_get_tmpdir(void)
2417 static const char* tmpdir = NULL;
2418 static char buf[1000];
2422 if (!GetTempPath (sizeof (buf), buf))
2428 _dbus_assert(tmpdir != NULL);
2435 * Deletes the given file.
2437 * @param filename the filename
2438 * @param error error location
2440 * @returns #TRUE if unlink() succeeded
2443 _dbus_delete_file (const DBusString *filename,
2446 const char *filename_c;
2448 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2450 filename_c = _dbus_string_get_const_data (filename);
2452 if (_unlink (filename_c) < 0)
2454 dbus_set_error (error, DBUS_ERROR_FAILED,
2455 "Failed to delete file %s: %s\n",
2456 filename_c, strerror (errno));
2463 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_BUILD_TESTS)
2475 * Backtrace Generator
2477 * Copyright 2004 Eric Poech
2478 * Copyright 2004 Robert Shearman
2480 * This library is free software; you can redistribute it and/or
2481 * modify it under the terms of the GNU Lesser General Public
2482 * License as published by the Free Software Foundation; either
2483 * version 2.1 of the License, or (at your option) any later version.
2485 * This library is distributed in the hope that it will be useful,
2486 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2487 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2488 * Lesser General Public License for more details.
2490 * You should have received a copy of the GNU Lesser General Public
2491 * License along with this library; if not, write to the Free Software
2492 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2496 #include <imagehlp.h>
2499 #define DPRINTF _dbus_warn
2507 //#define MAKE_FUNCPTR(f) static typeof(f) * p##f
2509 //MAKE_FUNCPTR(StackWalk);
2510 //MAKE_FUNCPTR(SymGetModuleBase);
2511 //MAKE_FUNCPTR(SymFunctionTableAccess);
2512 //MAKE_FUNCPTR(SymInitialize);
2513 //MAKE_FUNCPTR(SymGetSymFromAddr);
2514 //MAKE_FUNCPTR(SymGetModuleInfo);
2515 static BOOL (WINAPI *pStackWalk)(
2519 LPSTACKFRAME StackFrame,
2520 PVOID ContextRecord,
2521 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2522 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2523 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2524 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2526 static DWORD (WINAPI *pSymGetModuleBase)(
2530 static PVOID (WINAPI *pSymFunctionTableAccess)(
2534 static BOOL (WINAPI *pSymInitialize)(
2536 PSTR UserSearchPath,
2539 static BOOL (WINAPI *pSymGetSymFromAddr)(
2542 PDWORD Displacement,
2543 PIMAGEHLP_SYMBOL Symbol
2545 static BOOL (WINAPI *pSymGetModuleInfo)(
2548 PIMAGEHLP_MODULE ModuleInfo
2550 static DWORD (WINAPI *pSymSetOptions)(
2555 static BOOL init_backtrace()
2557 HMODULE hmodDbgHelp = LoadLibraryA("dbghelp");
2559 #define GETFUNC(x) \
2560 p##x = (typeof(x)*)GetProcAddress(hmodDbgHelp, #x); \
2568 // GETFUNC(StackWalk);
2569 // GETFUNC(SymGetModuleBase);
2570 // GETFUNC(SymFunctionTableAccess);
2571 // GETFUNC(SymInitialize);
2572 // GETFUNC(SymGetSymFromAddr);
2573 // GETFUNC(SymGetModuleInfo);
2577 pStackWalk = (BOOL (WINAPI *)(
2581 LPSTACKFRAME StackFrame,
2582 PVOID ContextRecord,
2583 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2584 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2585 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2586 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2587 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
2588 pSymGetModuleBase=(DWORD (WINAPI *)(
2591 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
2592 pSymFunctionTableAccess=(PVOID (WINAPI *)(
2595 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
2596 pSymInitialize = (BOOL (WINAPI *)(
2598 PSTR UserSearchPath,
2600 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
2601 pSymGetSymFromAddr = (BOOL (WINAPI *)(
2604 PDWORD Displacement,
2605 PIMAGEHLP_SYMBOL Symbol
2606 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
2607 pSymGetModuleInfo = (BOOL (WINAPI *)(
2610 PIMAGEHLP_MODULE ModuleInfo
2611 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
2612 pSymSetOptions = (DWORD (WINAPI *)(
2614 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
2617 pSymSetOptions(SYMOPT_UNDNAME);
2619 pSymInitialize(GetCurrentProcess(), NULL, TRUE);
2624 static void dump_backtrace_for_thread(HANDLE hThread)
2631 if (!init_backtrace())
2634 /* can't use this function for current thread as GetThreadContext
2635 * doesn't support getting context from current thread */
2636 if (hThread == GetCurrentThread())
2639 DPRINTF("Backtrace:\n");
2641 _DBUS_ZERO(context);
2642 context.ContextFlags = CONTEXT_FULL;
2644 SuspendThread(hThread);
2646 if (!GetThreadContext(hThread, &context))
2648 DPRINTF("Couldn't get thread context (error %ld)\n", GetLastError());
2649 ResumeThread(hThread);
2656 sf.AddrFrame.Offset = context.Ebp;
2657 sf.AddrFrame.Mode = AddrModeFlat;
2658 sf.AddrPC.Offset = context.Eip;
2659 sf.AddrPC.Mode = AddrModeFlat;
2660 dwImageType = IMAGE_FILE_MACHINE_I386;
2662 # error You need to fill in the STACKFRAME structure for your architecture
2665 while (pStackWalk(dwImageType, GetCurrentProcess(),
2666 hThread, &sf, &context, NULL, pSymFunctionTableAccess,
2667 pSymGetModuleBase, NULL))
2670 IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
2671 DWORD dwDisplacement;
2673 pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
2674 pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1;
2676 if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
2677 &dwDisplacement, pSymbol))
2679 IMAGEHLP_MODULE ModuleInfo;
2680 ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
2682 if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
2684 DPRINTF("1\t%p\n", (void*)sf.AddrPC.Offset);
2686 DPRINTF("2\t%s+0x%lx\n", ModuleInfo.ImageName,
2687 sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
2689 else if (dwDisplacement)
2690 DPRINTF("3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
2692 DPRINTF("4\t%s\n", pSymbol->Name);
2695 ResumeThread(hThread);
2698 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
2700 dump_backtrace_for_thread((HANDLE)lpParameter);
2704 /* cannot get valid context from current thread, so we have to execute
2705 * backtrace from another thread */
2706 static void dump_backtrace()
2708 HANDLE hCurrentThread;
2711 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
2712 GetCurrentProcess(), &hCurrentThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
2713 hThread = CreateThread(NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
2715 WaitForSingleObject(hThread, INFINITE);
2716 CloseHandle(hThread);
2717 CloseHandle(hCurrentThread);
2720 void _dbus_print_backtrace(void)
2726 void _dbus_print_backtrace(void)
2728 _dbus_verbose (" D-Bus not compiled with backtrace support\n");
2732 static dbus_uint32_t fromAscii(char ascii)
2734 if(ascii >= '0' && ascii <= '9')
2736 if(ascii >= 'A' && ascii <= 'F')
2737 return ascii - 'A' + 10;
2738 if(ascii >= 'a' && ascii <= 'f')
2739 return ascii - 'a' + 10;
2743 dbus_bool_t _dbus_read_local_machine_uuid (DBusGUID *machine_id,
2744 dbus_bool_t create_if_not_found,
2751 HW_PROFILE_INFOA info;
2752 char *lpc = &info.szHwProfileGuid[0];
2755 // the hw-profile guid lives long enough
2756 if(!GetCurrentHwProfileA(&info))
2758 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); // FIXME
2762 // Form: {12340001-4980-1920-6788-123456789012}
2765 u = ((fromAscii(lpc[0]) << 0) |
2766 (fromAscii(lpc[1]) << 4) |
2767 (fromAscii(lpc[2]) << 8) |
2768 (fromAscii(lpc[3]) << 12) |
2769 (fromAscii(lpc[4]) << 16) |
2770 (fromAscii(lpc[5]) << 20) |
2771 (fromAscii(lpc[6]) << 24) |
2772 (fromAscii(lpc[7]) << 28));
2773 machine_id->as_uint32s[0] = u;
2777 u = ((fromAscii(lpc[0]) << 0) |
2778 (fromAscii(lpc[1]) << 4) |
2779 (fromAscii(lpc[2]) << 8) |
2780 (fromAscii(lpc[3]) << 12) |
2781 (fromAscii(lpc[5]) << 16) |
2782 (fromAscii(lpc[6]) << 20) |
2783 (fromAscii(lpc[7]) << 24) |
2784 (fromAscii(lpc[8]) << 28));
2785 machine_id->as_uint32s[1] = u;
2789 u = ((fromAscii(lpc[0]) << 0) |
2790 (fromAscii(lpc[1]) << 4) |
2791 (fromAscii(lpc[2]) << 8) |
2792 (fromAscii(lpc[3]) << 12) |
2793 (fromAscii(lpc[5]) << 16) |
2794 (fromAscii(lpc[6]) << 20) |
2795 (fromAscii(lpc[7]) << 24) |
2796 (fromAscii(lpc[8]) << 28));
2797 machine_id->as_uint32s[2] = u;
2801 u = ((fromAscii(lpc[0]) << 0) |
2802 (fromAscii(lpc[1]) << 4) |
2803 (fromAscii(lpc[2]) << 8) |
2804 (fromAscii(lpc[3]) << 12) |
2805 (fromAscii(lpc[4]) << 16) |
2806 (fromAscii(lpc[5]) << 20) |
2807 (fromAscii(lpc[6]) << 24) |
2808 (fromAscii(lpc[7]) << 28));
2809 machine_id->as_uint32s[3] = u;
2815 HANDLE _dbus_global_lock (const char *mutexname)
2820 mutex = CreateMutex( NULL, FALSE, mutexname );
2826 gotMutex = WaitForSingleObject( mutex, INFINITE );
2829 case WAIT_ABANDONED:
2830 ReleaseMutex (mutex);
2831 CloseHandle (mutex);
2842 void _dbus_global_unlock (HANDLE mutex)
2844 ReleaseMutex (mutex);
2845 CloseHandle (mutex);
2848 // for proper cleanup in dbus-daemon
2849 static HANDLE hDBusDaemonMutex = NULL;
2850 static HANDLE hDBusSharedMem = NULL;
2851 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
2852 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
2853 // sync _dbus_get_autolaunch_address
2854 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
2855 // mutex to determine if dbus-daemon is already started (per user)
2856 static const char *cDBusDaemonMutex = "DBusDaemonMutex";
2857 // named shm for dbus adress info (per user)
2859 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfoDebug";
2861 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
2865 _dbus_daemon_init(const char *host, dbus_uint32_t port)
2869 char szUserName[64];
2870 DWORD dwUserNameSize = sizeof(szUserName);
2871 char szDBusDaemonMutex[128];
2872 char szDBusDaemonAddressInfo[128];
2873 char szAddress[128];
2879 _snprintf(szAddress, sizeof(szAddress) - 1, "tcp:host=%s,port=%d", host, port);
2880 ret = GetUserName(szUserName, &dwUserNameSize);
2881 _dbus_assert(ret != 0);
2882 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
2883 cDBusDaemonMutex, szUserName);
2884 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
2885 cDBusDaemonAddressInfo, szUserName);
2887 // before _dbus_global_lock to keep correct lock/release order
2888 hDBusDaemonMutex = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
2889 ret = WaitForSingleObject( hDBusDaemonMutex, 1000 );
2890 if ( ret != WAIT_OBJECT_0 ) {
2891 _dbus_warn("Could not lock mutex %s (return code %d). daemon already running?\n", szDBusDaemonMutex, ret );
2892 _dbus_assert( !"Could not lock mutex, daemon already running?" );
2895 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
2896 lock = _dbus_global_lock( cUniqueDBusInitMutex );
2899 hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
2900 0, strlen( szAddress ) + 1, szDBusDaemonAddressInfo );
2901 _dbus_assert( hDBusSharedMem );
2903 adr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
2905 _dbus_assert( adr );
2907 strcpy( adr, szAddress);
2910 UnmapViewOfFile( adr );
2912 _dbus_global_unlock( lock );
2916 _dbus_daemon_release()
2920 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
2921 lock = _dbus_global_lock( cUniqueDBusInitMutex );
2923 CloseHandle( hDBusSharedMem );
2925 hDBusSharedMem = NULL;
2927 ReleaseMutex( hDBusDaemonMutex );
2929 CloseHandle( hDBusDaemonMutex );
2931 hDBusDaemonMutex = NULL;
2933 _dbus_global_unlock( lock );
2937 _dbus_get_autolaunch_shm(DBusString *adress)
2941 char szUserName[64];
2942 DWORD dwUserNameSize = sizeof(szUserName);
2943 char szDBusDaemonAddressInfo[128];
2946 if( !GetUserName(szUserName, &dwUserNameSize) )
2948 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
2949 cDBusDaemonAddressInfo, szUserName);
2953 // we know that dbus-daemon is available, so we wait until shm is available
2954 sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, szDBusDaemonAddressInfo );
2955 if( sharedMem == 0 )
2957 if ( sharedMem != 0)
2961 if( sharedMem == 0 )
2964 adr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
2969 _dbus_string_init( adress );
2971 _dbus_string_append( adress, adr );
2974 UnmapViewOfFile( adr );
2976 CloseHandle( sharedMem );
2982 _dbus_daemon_already_runs (DBusString *adress)
2986 dbus_bool_t bRet = TRUE;
2987 char szUserName[64];
2988 DWORD dwUserNameSize = sizeof(szUserName);
2989 char szDBusDaemonMutex[128];
2991 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
2992 lock = _dbus_global_lock( cUniqueDBusInitMutex );
2994 if( !GetUserName(szUserName, &dwUserNameSize) )
2996 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
2997 cDBusDaemonMutex, szUserName);
3000 daemon = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
3001 if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
3003 ReleaseMutex (daemon);
3004 CloseHandle (daemon);
3006 _dbus_global_unlock( lock );
3011 bRet = _dbus_get_autolaunch_shm( adress );
3014 CloseHandle ( daemon );
3016 _dbus_global_unlock( lock );
3022 _dbus_get_autolaunch_address (DBusString *address,
3027 PROCESS_INFORMATION pi;
3028 dbus_bool_t retval = FALSE;
3030 char dbus_exe_path[MAX_PATH];
3031 char dbus_args[MAX_PATH * 2];
3033 const char * daemon_name = "dbus-daemond.exe";
3035 const char * daemon_name = "dbus-daemon.exe";
3038 mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
3040 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3042 if (_dbus_daemon_already_runs(address))
3044 _dbus_verbose("found already running dbus daemon\n");
3049 if (!SearchPathA(NULL, daemon_name, NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
3051 printf ("please add the path to %s to your PATH environment variable\n", daemon_name);
3052 printf ("or start the daemon manually\n\n");
3058 ZeroMemory( &si, sizeof(si) );
3060 ZeroMemory( &pi, sizeof(pi) );
3062 _snprintf(dbus_args, sizeof(dbus_args) - 1, "\"%s\" %s", dbus_exe_path, " --session");
3064 // argv[i] = "--config-file=bus\\session.conf";
3065 // printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
3066 if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
3069 retval = _dbus_get_autolaunch_shm( address );
3072 if (retval == FALSE)
3073 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Failed to launch dbus-daemon");
3077 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3079 _DBUS_ASSERT_ERROR_IS_SET (error);
3081 _dbus_global_unlock (mutex);
3087 /** Makes the file readable by every user in the system.
3089 * @param filename the filename
3090 * @param error error location
3091 * @returns #TRUE if the file's permissions could be changed.
3094 _dbus_make_file_world_readable(const DBusString *filename,
3102 #define DBUS_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
3103 #define DBUS_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
3106 * Returns the standard directories for a session bus to look for service
3109 * On Windows this should be data directories:
3111 * %CommonProgramFiles%/dbus
3117 * @param dirs the directory list we are returning
3118 * @returns #FALSE on OOM
3122 _dbus_get_standard_session_servicedirs (DBusList **dirs)
3124 const char *common_progs;
3125 DBusString servicedir_path;
3127 if (!_dbus_string_init (&servicedir_path))
3130 if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR _DBUS_PATH_SEPARATOR))
3133 common_progs = _dbus_getenv ("CommonProgramFiles");
3135 if (common_progs != NULL)
3137 if (!_dbus_string_append (&servicedir_path, common_progs))
3140 if (!_dbus_string_append (&servicedir_path, _DBUS_PATH_SEPARATOR))
3144 if (!_dbus_split_paths_and_append (&servicedir_path,
3145 DBUS_STANDARD_SESSION_SERVICEDIR,
3149 _dbus_string_free (&servicedir_path);
3153 _dbus_string_free (&servicedir_path);
3158 * Returns the standard directories for a system bus to look for service
3161 * On UNIX this should be the standard xdg freedesktop.org data directories:
3163 * XDG_DATA_DIRS=${XDG_DATA_DIRS-/usr/local/share:/usr/share}
3169 * On Windows there is no system bus and this function can return nothing.
3171 * @param dirs the directory list we are returning
3172 * @returns #FALSE on OOM
3176 _dbus_get_standard_system_servicedirs (DBusList **dirs)
3182 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
3185 * Atomically increments an integer
3187 * @param atomic pointer to the integer to increment
3188 * @returns the value before incrementing
3192 _dbus_atomic_inc (DBusAtomic *atomic)
3194 // +/- 1 is needed here!
3195 // no volatile argument with mingw
3196 return InterlockedIncrement (&atomic->value) - 1;
3200 * Atomically decrement an integer
3202 * @param atomic pointer to the integer to decrement
3203 * @returns the value before decrementing
3207 _dbus_atomic_dec (DBusAtomic *atomic)
3209 // +/- 1 is needed here!
3210 // no volatile argument with mingw
3211 return InterlockedDecrement (&atomic->value) + 1;
3214 #endif /* asserts or tests enabled */
3217 * Called when the bus daemon is signaled to reload its configuration; any
3218 * caches should be nuked. Of course any caches that need explicit reload
3219 * are probably broken, but c'est la vie.
3224 _dbus_flush_caches (void)
3229 dbus_bool_t _dbus_windows_user_is_process_owner (const char *windows_sid)
3235 * See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently
3236 * for Winsock so is abstracted)
3238 * @returns #TRUE if errno == EAGAIN or errno == EWOULDBLOCK
3241 _dbus_get_is_errno_eagain_or_ewouldblock (void)
3243 return errno == EAGAIN || errno == EWOULDBLOCK;
3247 * return the absolute path of the dbus installation
3249 * @param s buffer for installation path
3250 * @param len length of buffer
3251 * @returns #FALSE on failure
3254 _dbus_get_install_root(char *s, int len)
3257 int ret = GetModuleFileName(NULL,s,len);
3259 || ret == len && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
3264 else if ((p = strstr(s,"\\bin\\")))
3277 find config file either from installation or build root according to
3278 the following path layout
3280 bin/dbus-daemon[d].exe
3281 etc/<config-file>.conf
3284 bin/dbus-daemon[d].exe
3285 bus/<config-file>.conf
3288 _dbus_get_config_file_name(DBusString *config_file, char *s)
3290 char path[MAX_PATH*2];
3291 int path_size = sizeof(path);
3292 int len = 4 + strlen(s);
3294 if (!_dbus_get_install_root(path,path_size))
3297 if(len > sizeof(path)-2)
3299 strcat(path,"etc\\");
3301 if (_dbus_file_exists(path))
3303 // find path from executable
3304 if (!_dbus_string_append (config_file, path))
3309 if (!_dbus_get_install_root(path,path_size))
3311 if(len + strlen(path) > sizeof(path)-2)
3313 strcat(path,"bus\\");
3316 if (_dbus_file_exists(path))
3318 if (!_dbus_string_append (config_file, path))
3326 * Append the absolute path of the system.conf file
3327 * (there is no system bus on Windows so this can just
3328 * return FALSE and print a warning or something)
3330 * @param str the string to append to
3331 * @returns #FALSE if no memory
3334 _dbus_append_system_config_file (DBusString *str)
3336 return _dbus_get_config_file_name(str, "system.conf");
3340 * Append the absolute path of the session.conf file.
3342 * @param str the string to append to
3343 * @returns #FALSE if no memory
3346 _dbus_append_session_config_file (DBusString *str)
3348 return _dbus_get_config_file_name(str, "session.conf");
3351 /* See comment in dbus-sysdeps-unix.c */
3353 _dbus_lookup_session_address (dbus_bool_t *supported,
3354 DBusString *address,
3357 /* Probably fill this in with something based on COM? */
3363 * Appends the directory in which a keyring for the given credentials
3364 * should be stored. The credentials should have either a Windows or
3365 * UNIX user in them. The directory should be an absolute path.
3367 * On UNIX the directory is ~/.dbus-keyrings while on Windows it should probably
3368 * be something else, since the dotfile convention is not normal on Windows.
3370 * @param directory string to append directory to
3371 * @param credentials credentials the directory should be for
3373 * @returns #FALSE on no memory
3376 _dbus_append_keyring_directory_for_credentials (DBusString *directory,
3377 DBusCredentials *credentials)
3382 const char *homepath;
3384 _dbus_assert (credentials != NULL);
3385 _dbus_assert (!_dbus_credentials_are_anonymous (credentials));
3387 if (!_dbus_string_init (&homedir))
3390 homepath = _dbus_getenv("HOMEPATH");
3391 if (homepath != NULL && *homepath != '\0')
3393 _dbus_string_append(&homedir,homepath);
3396 #ifdef DBUS_BUILD_TESTS
3398 const char *override;
3400 override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
3401 if (override != NULL && *override != '\0')
3403 _dbus_string_set_length (&homedir, 0);
3404 if (!_dbus_string_append (&homedir, override))
3407 _dbus_verbose ("Using fake homedir for testing: %s\n",
3408 _dbus_string_get_const_data (&homedir));
3412 static dbus_bool_t already_warned = FALSE;
3413 if (!already_warned)
3415 _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
3416 already_warned = TRUE;
3422 _dbus_string_init_const (&dotdir, ".dbus-keyrings");
3423 if (!_dbus_concat_dir_and_file (&homedir,
3427 if (!_dbus_string_copy (&homedir, 0,
3428 directory, _dbus_string_get_length (directory))) {
3432 _dbus_string_free (&homedir);
3436 _dbus_string_free (&homedir);
3440 /** @} end of sysdeps-win */
3441 /* tests in dbus-sysdeps-util.c */