1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #define STRSAFE_NO_DEPRECATE
33 #define _WIN32_WINNT 0x0500
36 #include "dbus-internals.h"
37 #include "dbus-sysdeps.h"
38 #include "dbus-threads.h"
39 #include "dbus-protocol.h"
40 #include "dbus-string.h"
41 #include "dbus-sysdeps-win.h"
42 #include "dbus-protocol.h"
43 #include "dbus-hash.h"
44 #include "dbus-sockets-win.h"
45 #include "dbus-userdb.h"
46 #include "dbus-list.h"
53 #include <sys/types.h>
59 #ifndef HAVE_SOCKLEN_T
63 _DBUS_DEFINE_GLOBAL_LOCK (win_fds);
64 _DBUS_DEFINE_GLOBAL_LOCK (sid_atom_cache);
71 _dbus_assert (win_fds!=0);
77 _dbus_unlock_sockets()
79 _dbus_assert (win_fds!=0);
80 _DBUS_UNLOCK (win_fds);
83 #ifdef _DBUS_WIN_USE_RANDOMIZER
84 static int win_encap_randomizer;
86 static DBusHashTable *sid_atom_cache = NULL;
89 static DBusString dbusdir;
90 static int working_dir_init = 0;
92 int _dbus_init_working_dir(char *s)
94 /* change working directory to one level above
95 of dbus-daemon executable path.
96 This allows the usage of relative path in
97 config files or command line parameters */
98 DBusString daemon_path,bin_path;
100 if (!_dbus_string_init (&daemon_path))
103 if (!_dbus_string_init (&bin_path))
106 if (!_dbus_string_init (&dbusdir))
109 _dbus_string_append(&daemon_path,s);
110 _dbus_string_get_dirname(&daemon_path,&bin_path);
111 _dbus_string_get_dirname(&bin_path,&dbusdir);
112 chdir(_dbus_string_get_const_data(&dbusdir));
113 _dbus_verbose ("Change working path to %s\n",_dbus_string_get_const_data (&dbusdir));
114 working_dir_init = 1;
118 DBusString *_dbus_get_working_dir(void)
120 if (!working_dir_init)
123 _dbus_verbose ("retrieving working path %s\n",_dbus_string_get_const_data (&dbusdir));
132 _dbus_file_open (DBusFile *file,
133 const char *filename,
138 file->FDATA = _open (filename, oflag, pmode);
140 file->FDATA = _open (filename, oflag);
141 if (file->FDATA >= 0)
151 _dbus_file_close (DBusFile *file,
154 const int fd = file->FDATA;
156 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
158 _dbus_assert (fd >= 0);
160 if (_close (fd) == -1)
162 dbus_set_error (error, _dbus_error_from_errno (errno),
163 "Could not close fd %d: %s", fd,
164 _dbus_strerror (errno));
169 _dbus_verbose ("closed C file descriptor %d:\n",fd);
175 _dbus_file_read(DBusFile *file,
179 const int fd = file->FDATA;
183 _dbus_assert (count >= 0);
185 start = _dbus_string_get_length (buffer);
187 if (!_dbus_string_lengthen (buffer, count))
193 data = _dbus_string_get_data_len (buffer, start, count);
195 _dbus_assert (fd >= 0);
197 _dbus_verbose ("read: count=%d fd=%d\n", count, fd);
198 bytes_read = read (fd, data, count);
200 if (bytes_read == -1)
201 _dbus_verbose ("read: failed: %s\n", _dbus_strerror (errno));
203 _dbus_verbose ("read: = %d\n", bytes_read);
207 /* put length back (note that this doesn't actually realloc anything) */
208 _dbus_string_set_length (buffer, start);
213 /* put length back (doesn't actually realloc) */
214 _dbus_string_set_length (buffer, start + bytes_read);
219 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
227 _dbus_file_write (DBusFile *file,
228 const DBusString *buffer,
232 const int fd = file->FDATA;
236 data = _dbus_string_get_const_data_len (buffer, start, len);
238 _dbus_assert (fd >= 0);
240 _dbus_verbose ("write: len=%d fd=%d\n", len, fd);
241 bytes_written = write (fd, data, len);
243 if (bytes_written == -1)
244 _dbus_verbose ("write: failed: %s\n", _dbus_strerror (errno));
246 _dbus_verbose ("write: = %d\n", bytes_written);
250 if (bytes_written > 0)
251 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
254 return bytes_written;
258 _dbus_is_valid_file (DBusFile* file)
260 return file->FDATA >= 0;
263 dbus_bool_t _dbus_fstat (DBusFile *file,
266 return fstat(file->FDATA, sb) >= 0;
270 * write data to a pipe.
272 * @param pipe the pipe instance
273 * @param buffer the buffer to write data from
274 * @param start the first byte in the buffer to write
275 * @param len the number of bytes to try to write
276 * @param error error return
277 * @returns the number of bytes written or -1 on error
280 _dbus_pipe_write (DBusPipe *pipe,
281 const DBusString *buffer,
288 file.FDATA = pipe->fd_or_handle;
289 written = _dbus_file_write (&file, buffer, start, len);
292 dbus_set_error (error, DBUS_ERROR_FAILED,
293 "Writing to pipe: %s\n",
294 _dbus_strerror (errno));
302 * @param pipe the pipe instance
303 * @param error return location for an error
304 * @returns #FALSE if error is set
307 _dbus_pipe_close (DBusPipe *pipe,
311 file.FDATA = pipe->fd_or_handle;
312 if (_dbus_file_close (&file, error) < 0)
318 _dbus_pipe_invalidate (pipe);
330 static DBusSocket *win_fds = NULL;
331 static int win_n_fds = 0; // is this the size? rename to win_fds_size? #
334 #define TO_HANDLE(n) ((n)^win32_encap_randomizer)
335 #define FROM_HANDLE(n) ((n)^win32_encap_randomizer)
337 #define TO_HANDLE(n) ((n)+0x10000000)
338 #define FROM_HANDLE(n) ((n)-0x10000000)
339 #define IS_HANDLE(n) ((n)&0x10000000)
345 _dbus_win_deallocate_fd (int fd)
347 _DBUS_LOCK (win_fds);
348 win_fds[FROM_HANDLE (fd)].is_used = 0;
349 _DBUS_UNLOCK (win_fds);
354 _dbus_win_allocate_fd (void)
358 _DBUS_LOCK (win_fds);
362 #ifdef _DBUS_WIN_USE_RANDOMIZER
367 /* Use malloc to avoid memory leak failure in dbus-test */
368 win_fds = malloc (win_n_fds * sizeof (*win_fds));
370 _dbus_assert (win_fds != NULL);
372 for (i = 0; i < win_n_fds; i++)
373 win_fds[i].is_used = 0;
375 #ifdef _DBUS_WIN_USE_RANDOMIZER
377 _dbus_string_init (&random);
378 _dbus_generate_random_bytes (&random, sizeof (int));
379 memmove (&win_encap_randomizer, _dbus_string_get_const_data (&random), sizeof (int));
380 win_encap_randomizer &= 0xFF;
381 _dbus_string_free (&random);
386 for (i = 0; i < win_n_fds && win_fds[i].is_used != 0; i++)
391 int oldn = win_n_fds;
395 win_fds = realloc (win_fds, win_n_fds * sizeof (*win_fds));
397 _dbus_assert (win_fds != NULL);
399 for (j = oldn; j < win_n_fds; j++)
400 win_fds[i].is_used = 0;
403 memset(&win_fds[i], 0, sizeof(win_fds[i]));
405 win_fds[i].is_used = 1;
407 win_fds[i].port_file_fd = -1;
408 win_fds[i].close_on_exec = FALSE;
409 win_fds[i].non_blocking = FALSE;
411 _DBUS_UNLOCK (win_fds);
418 _dbus_create_handle_from_socket (int s)
423 // check: parameter must be a valid value
424 _dbus_assert(s != -1);
425 _dbus_assert(!IS_HANDLE(s));
427 // get index of a new position in the map
428 i = _dbus_win_allocate_fd ();
430 // fill new posiiton in the map: value->index
432 win_fds[i].is_used = 1;
434 // create handle from the index: index->handle
435 handle = TO_HANDLE (i);
437 _dbus_verbose ("_dbus_create_handle_from_value, value: %d, handle: %d\n", s, handle);
443 _dbus_socket_to_handle (DBusSocket *s)
448 // check: parameter must be a valid socket
449 _dbus_assert(s != NULL);
450 _dbus_assert(s->fd != -1);
451 _dbus_assert(!IS_HANDLE(s->fd));
453 _DBUS_LOCK (win_fds);
455 // at the first call there is no win_fds
456 // will be constructed _dbus_create_handle_from_socket
457 // because handle = -1
460 // search for the value in the map
461 // find the index of the value: value->index
462 for (i = 0; i < win_n_fds; i++)
463 if (win_fds[i].is_used == 1 && win_fds[i].fd == s->fd)
465 // create handle from the index: index->handle
466 handle = TO_HANDLE (i);
470 _DBUS_UNLOCK (win_fds);
475 handle = _dbus_create_handle_from_socket(s->fd);
478 _dbus_assert(handle != -1);
485 _dbus_handle_to_socket_unlocked (int handle,
490 // check: parameter must be a valid handle
491 _dbus_assert(handle != -1);
492 _dbus_assert(IS_HANDLE(handle));
493 _dbus_assert(ptr != NULL);
495 // map from handle to index: handle->index
496 i = FROM_HANDLE (handle);
498 _dbus_assert (win_fds != NULL);
499 _dbus_assert (i >= 0 && i < win_n_fds);
501 // check for if fd is valid
502 _dbus_assert (win_fds[i].is_used == 1);
504 // get socket from index: index->socket
507 _dbus_verbose ("_dbus_socket_to_handle_unlocked: socket=%d, handle=%d, index=%d\n", (*ptr)->fd, handle, i);
511 _dbus_handle_to_socket (int handle,
514 _dbus_lock_sockets();
515 _dbus_handle_to_socket_unlocked (handle, ptr);
516 _dbus_unlock_sockets();
522 #define FROM_HANDLE(n) 1==DBUS_WIN_DONT_USE__FROM_HANDLE__DIRECTLY
523 #define win_fds 1==DBUS_WIN_DONT_USE_win_fds_DIRECTLY
529 * Thin wrapper around the read() system call that appends
530 * the data it reads to the DBusString buffer. It appends
531 * up to the given count, and returns the same value
532 * and same errno as read(). The only exception is that
533 * _dbus_read() handles EINTR for you. _dbus_read() can
534 * return ENOMEM, even though regular UNIX read doesn't.
536 * @param fd the file descriptor to read from
537 * @param buffer the buffer to append data to
538 * @param count the amount of data to read
539 * @returns the number of bytes read or -1
542 _dbus_read_socket (int handle,
551 _dbus_assert (count >= 0);
553 start = _dbus_string_get_length (buffer);
555 if (!_dbus_string_lengthen (buffer, count))
561 data = _dbus_string_get_data_len (buffer, start, count);
563 _dbus_handle_to_socket(handle, &s);
567 _dbus_verbose ("recv: count=%d socket=%d\n", count, s->fd);
568 bytes_read = recv (s->fd, data, count, 0);
569 if (bytes_read == SOCKET_ERROR)
571 DBUS_SOCKET_SET_ERRNO();
572 _dbus_verbose ("recv: failed: %s\n", _dbus_strerror (errno));
576 _dbus_verbose ("recv: = %d\n", bytes_read);
580 _dbus_assert_not_reached ("no valid socket");
585 /* put length back (note that this doesn't actually realloc anything) */
586 _dbus_string_set_length (buffer, start);
591 /* put length back (doesn't actually realloc) */
592 _dbus_string_set_length (buffer, start + bytes_read);
597 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
605 * Thin wrapper around the write() system call that writes a part of a
606 * DBusString and handles EINTR for you.
608 * @param fd the file descriptor to write
609 * @param buffer the buffer to write data from
610 * @param start the first byte in the buffer to write
611 * @param len the number of bytes to try to write
612 * @returns the number of bytes written or -1 on error
615 _dbus_write_socket (int handle,
616 const DBusString *buffer,
625 data = _dbus_string_get_const_data_len (buffer, start, len);
627 _dbus_handle_to_socket(handle, &s);
631 _dbus_verbose ("send: len=%d socket=%d\n", len, s->fd);
632 bytes_written = send (s->fd, data, len, 0);
633 if (bytes_written == SOCKET_ERROR)
635 DBUS_SOCKET_SET_ERRNO();
636 _dbus_verbose ("send: failed: %s\n", _dbus_strerror (errno));
640 _dbus_verbose ("send: = %d\n", bytes_written);
644 _dbus_assert_not_reached ("unhandled fd type");
648 if (bytes_written > 0)
649 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
652 return bytes_written;
657 * Closes a file descriptor.
659 * @param fd the file descriptor
660 * @param error error object
661 * @returns #FALSE if error set
664 _dbus_close_socket (int handle,
669 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
671 _dbus_lock_sockets();
673 _dbus_handle_to_socket_unlocked (handle, &s);
678 if (s->port_file_fd >= 0)
680 _chsize (s->port_file_fd, 0);
681 close (s->port_file_fd);
682 s->port_file_fd = -1;
683 unlink (_dbus_string_get_const_data (&s->port_file));
684 free ((char *) _dbus_string_get_const_data (&s->port_file));
687 if (closesocket (s->fd) == SOCKET_ERROR)
689 DBUS_SOCKET_SET_ERRNO ();
690 dbus_set_error (error, _dbus_error_from_errno (errno),
691 "Could not close socket: socket=%d, handle=%d, %s",
692 s->fd, handle, _dbus_strerror (errno));
693 _dbus_unlock_sockets();
696 _dbus_verbose ("_dbus_close_socket: socket=%d, handle=%d\n",
701 _dbus_assert_not_reached ("unhandled fd type");
704 _dbus_unlock_sockets();
706 _dbus_win_deallocate_fd (handle);
713 * Sets the file descriptor to be close
714 * on exec. Should be called for all file
715 * descriptors in D-Bus code.
717 * @param fd the file descriptor
720 _dbus_fd_set_close_on_exec (int handle)
726 _dbus_lock_sockets();
728 _dbus_handle_to_socket_unlocked (handle, &s);
729 s->close_on_exec = TRUE;
731 _dbus_unlock_sockets();
735 * Sets a file descriptor to be nonblocking.
737 * @param fd the file descriptor.
738 * @param error address of error location.
739 * @returns #TRUE on success.
742 _dbus_set_fd_nonblocking (int handle,
748 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
750 _dbus_lock_sockets();
752 _dbus_handle_to_socket_unlocked(handle, &s);
756 if (ioctlsocket (s->fd, FIONBIO, &one) == SOCKET_ERROR)
758 dbus_set_error (error, _dbus_error_from_errno (WSAGetLastError ()),
759 "Failed to set socket %d:%d to nonblocking: %s", s->fd,
760 _dbus_strerror (WSAGetLastError ()));
761 _dbus_unlock_sockets();
767 _dbus_assert_not_reached ("unhandled fd type");
770 _dbus_unlock_sockets();
777 * Like _dbus_write() but will use writev() if possible
778 * to write both buffers in sequence. The return value
779 * is the number of bytes written in the first buffer,
780 * plus the number written in the second. If the first
781 * buffer is written successfully and an error occurs
782 * writing the second, the number of bytes in the first
783 * is returned (i.e. the error is ignored), on systems that
784 * don't have writev. Handles EINTR for you.
785 * The second buffer may be #NULL.
787 * @param fd the file descriptor
788 * @param buffer1 first buffer
789 * @param start1 first byte to write in first buffer
790 * @param len1 number of bytes to write from first buffer
791 * @param buffer2 second buffer, or #NULL
792 * @param start2 first byte to write in second buffer
793 * @param len2 number of bytes to write in second buffer
794 * @returns total bytes written from both buffers, or -1 on error
797 _dbus_write_socket_two (int handle,
798 const DBusString *buffer1,
801 const DBusString *buffer2,
813 _dbus_assert (buffer1 != NULL);
814 _dbus_assert (start1 >= 0);
815 _dbus_assert (start2 >= 0);
816 _dbus_assert (len1 >= 0);
817 _dbus_assert (len2 >= 0);
819 _dbus_handle_to_socket(handle, &s);
821 data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
824 data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
834 vectors[0].buf = (char*) data1;
835 vectors[0].len = len1;
836 vectors[1].buf = (char*) data2;
837 vectors[1].len = len2;
839 _dbus_verbose ("WSASend: len1+2=%d+%d socket=%d\n", len1, len2, s->fd);
840 rc = WSASend (s->fd, vectors, data2 ? 2 : 1, &bytes_written,
844 DBUS_SOCKET_SET_ERRNO ();
845 _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror (errno));
849 _dbus_verbose ("WSASend: = %ld\n", bytes_written);
850 return bytes_written;
854 _dbus_assert_not_reached ("unhandled fd type");
860 * @def _DBUS_MAX_SUN_PATH_LENGTH
862 * Maximum length of the path to a UNIX domain socket,
863 * sockaddr_un::sun_path member. POSIX requires that all systems
864 * support at least 100 bytes here, including the nul termination.
865 * We use 99 for the max value to allow for the nul.
867 * We could probably also do sizeof (addr.sun_path)
868 * but this way we are the same on all platforms
869 * which is probably a good idea.
873 * Creates a socket and connects it to the UNIX domain socket at the
874 * given path. The connection fd is returned, and is set up as
877 * On Windows there are no UNIX domain sockets. Instead, connects to a
878 * localhost-bound TCP socket, whose port number is stored in a file
881 * Uses abstract sockets instead of filesystem-linked sockets if
882 * requested (it's possible only on Linux; see "man 7 unix" on Linux).
883 * On non-Linux abstract socket usage always fails.
885 * @param path the path to UNIX domain socket
886 * @param abstract #TRUE to use abstract namespace
887 * @param error return location for error code
888 * @returns connection file descriptor or -1 on error
891 _dbus_connect_unix_socket (const char *path,
892 dbus_bool_t abstract,
901 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
903 _dbus_verbose ("connecting to pseudo-unix socket at %s\n",
908 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
909 "Implementation does not support abstract socket namespace\n");
913 fd = _sopen (path, O_RDONLY, SH_DENYNO);
917 dbus_set_error (error, _dbus_error_from_errno (errno),
918 "Failed to open file %s: %s",
919 path, _dbus_strerror (errno));
923 n = read (fd, buf, sizeof (buf) - 1);
928 dbus_set_error (error, DBUS_ERROR_FAILED,
929 "Failed to read port number from file %s",
937 if (port <= 0 || port > 0xFFFF)
939 dbus_set_error (error, DBUS_ERROR_FAILED,
940 "Invalid port numer in file %s",
945 return _dbus_connect_tcp_socket (NULL, port, error);
951 * Creates a socket and binds it to the given path,
952 * then listens on the socket. The socket is
953 * set to be nonblocking.
955 * Uses abstract sockets instead of filesystem-linked
956 * sockets if requested (it's possible only on Linux;
957 * see "man 7 unix" on Linux).
958 * On non-Linux abstract socket usage always fails.
960 * @param path the socket name
961 * @param abstract #TRUE to use abstract namespace
962 * @param error return location for errors
963 * @returns the listening file descriptor or -1 on error
966 _dbus_listen_unix_socket (const char *path,
967 dbus_bool_t abstract,
981 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
983 _dbus_verbose ("listening on pseudo-unix socket at %s\n",
988 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
989 "Implementation does not support abstract socket namespace\n");
993 listen_handle = _dbus_listen_tcp_socket (NULL, 0, error);
995 if (listen_handle == -1)
998 _dbus_handle_to_socket(listen_handle, &s);
1000 addr_len = sizeof (sa);
1001 if (getsockname (s->fd, &sa, &addr_len) == SOCKET_ERROR)
1003 DBUS_SOCKET_SET_ERRNO ();
1004 dbus_set_error (error, _dbus_error_from_errno (errno),
1005 "getsockname failed: %s",
1006 _dbus_strerror (errno));
1007 _dbus_close_socket (listen_handle, NULL);
1011 _dbus_assert (((struct sockaddr_in*) &sa)->sin_family == AF_INET);
1013 filefd = _sopen (path, O_CREAT|O_WRONLY|_O_SHORT_LIVED, SH_DENYWR, 0666);
1017 dbus_set_error (error, _dbus_error_from_errno (errno),
1018 "Failed to create pseudo-unix socket port number file %s: %s",
1019 path, _dbus_strerror (errno));
1020 _dbus_close_socket (listen_handle, NULL);
1024 _dbus_lock_sockets();
1025 _dbus_handle_to_socket_unlocked(listen_handle, &s);
1026 s->port_file_fd = filefd;
1027 _dbus_unlock_sockets();
1029 /* Use strdup() to avoid memory leak in dbus-test */
1030 path = strdup (path);
1033 _DBUS_SET_OOM (error);
1034 _dbus_close_socket (listen_handle, NULL);
1038 _dbus_string_init_const (&s->port_file, path);
1040 if (!_dbus_string_init (&portstr))
1042 _DBUS_SET_OOM (error);
1043 _dbus_close_socket (listen_handle, NULL);
1047 if (!_dbus_string_append_int (&portstr, ntohs (((struct sockaddr_in*) &sa)->sin_port)))
1049 _DBUS_SET_OOM (error);
1050 _dbus_close_socket (listen_handle, NULL);
1054 l = _dbus_string_get_length (&portstr);
1055 n = write (filefd, _dbus_string_get_const_data (&portstr), l);
1056 _dbus_string_free (&portstr);
1060 dbus_set_error (error, _dbus_error_from_errno (errno),
1061 "Failed to write port number to file %s: %s",
1062 path, _dbus_strerror (errno));
1063 _dbus_close_socket (listen_handle, NULL);
1068 dbus_set_error (error, _dbus_error_from_errno (errno),
1069 "Failed to write port number to file %s",
1071 _dbus_close_socket (listen_handle, NULL);
1075 return listen_handle;
1082 * Opens the client side of a Windows named pipe. The connection D-BUS
1083 * file descriptor index is returned. It is set up as nonblocking.
1085 * @param path the path to named pipe socket
1086 * @param error return location for error code
1087 * @returns connection D-BUS file descriptor or -1 on error
1090 _dbus_connect_named_pipe (const char *path,
1093 _dbus_assert_not_reached ("not implemented");
1100 _dbus_account_to_win_sid (const wchar_t *waccount,
1104 dbus_bool_t retval = FALSE;
1105 DWORD sid_length, wdomain_length;
1113 if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1114 NULL, &wdomain_length, &use)
1115 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1117 _dbus_win_set_error_from_win_error (error, GetLastError ());
1121 *ppsid = dbus_malloc (sid_length);
1124 _DBUS_SET_OOM (error);
1128 wdomain = dbus_new (wchar_t, wdomain_length);
1131 _DBUS_SET_OOM (error);
1135 if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1136 wdomain, &wdomain_length, &use))
1138 _dbus_win_set_error_from_win_error (error, GetLastError ());
1142 if (!IsValidSid ((PSID) *ppsid))
1144 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1151 dbus_free (wdomain);
1164 fill_win_user_info_name_and_groups (wchar_t *wname,
1172 dbus_bool_t retval = FALSE;
1173 char *name, *domain;
1174 LPLOCALGROUP_USERS_INFO_0 local_groups = NULL;
1175 LPGROUP_USERS_INFO_0 global_groups = NULL;
1176 DWORD nread, ntotal;
1178 name = _dbus_win_utf16_to_utf8 (wname, error);
1182 domain = _dbus_win_utf16_to_utf8 (wdomain, error);
1186 info->username = dbus_malloc (strlen (domain) + 1 + strlen (name) + 1);
1187 if (!info->username)
1189 _DBUS_SET_OOM (error);
1193 strcpy (info->username, domain);
1194 strcat (info->username, "\\");
1195 strcat (info->username, name);
1197 info->n_group_ids = 0;
1198 if (NetUserGetLocalGroups (NULL, wname, 0, LG_INCLUDE_INDIRECT,
1199 (LPBYTE *) &local_groups, MAX_PREFERRED_LENGTH,
1200 &nread, &ntotal) == NERR_Success)
1205 info->group_ids = dbus_new (dbus_gid_t, nread);
1206 if (!info->group_ids)
1208 _DBUS_SET_OOM (error);
1212 for (i = n = 0; i < nread; i++)
1215 if (_dbus_account_to_win_sid (local_groups[i].lgrui0_name,
1218 info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1219 dbus_free (group_sid);
1222 info->n_group_ids = n;
1225 if (NetUserGetGroups (NULL, wname, 0,
1226 (LPBYTE *) &global_groups, MAX_PREFERRED_LENGTH,
1227 &nread, &ntotal) == NERR_Success)
1230 int n = info->n_group_ids;
1232 info->group_ids = dbus_realloc (info->group_ids, (n + nread) * sizeof (dbus_gid_t));
1233 if (!info->group_ids)
1235 _DBUS_SET_OOM (error);
1239 for (i = 0; i < nread; i++)
1242 if (_dbus_account_to_win_sid (global_groups[i].grui0_name,
1245 info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1246 dbus_free (group_sid);
1249 info->n_group_ids = n;
1252 if (info->n_group_ids > 0)
1254 /* FIXME: find out actual primary group */
1255 info->primary_gid = info->group_ids[0];
1259 info->group_ids = dbus_new (dbus_gid_t, 1);
1260 info->n_group_ids = 1;
1261 info->group_ids[0] = DBUS_GID_UNSET;
1262 info->primary_gid = DBUS_GID_UNSET;
1268 if (global_groups != NULL)
1269 NetApiBufferFree (global_groups);
1271 if (local_groups != NULL)
1272 NetApiBufferFree (local_groups);
1283 fill_win_user_info_homedir (wchar_t *wname,
1292 dbus_bool_t retval = FALSE;
1293 USER_INFO_1 *user_info = NULL;
1294 wchar_t wcomputername[MAX_COMPUTERNAME_LENGTH + 1];
1295 DWORD wcomputername_length = MAX_COMPUTERNAME_LENGTH + 1;
1296 dbus_bool_t local_computer;
1298 NET_API_STATUS ret = 0;
1300 /* If the domain is this computer's name, assume it's a local user.
1301 * Otherwise look up a DC for the domain, and ask it.
1304 GetComputerNameW (wcomputername, &wcomputername_length);
1305 local_computer = (wcsicmp (wcomputername, wdomain) == 0);
1307 if (!local_computer)
1309 ret = NetGetAnyDCName (NULL, wdomain, (LPBYTE *) &dc);
1310 if (ret != NERR_Success)
1312 info->homedir = _dbus_strdup ("\\");
1313 _dbus_warn("NetGetAnyDCName() failed with errorcode %d '%s'\n",ret,_dbus_lm_strerror(ret));
1318 /* No way to find out the profile of another user, let's try the
1319 * "home directory" from NetUserGetInfo's USER_INFO_1.
1321 ret = NetUserGetInfo (dc, wname, 1, (LPBYTE *) &user_info);
1322 if (ret == NERR_Success )
1323 if(user_info->usri1_home_dir != NULL &&
1324 user_info->usri1_home_dir != (LPWSTR)0xfeeefeee && /* freed memory http://www.gamedev.net/community/forums/topic.asp?topic_id=158402 */
1325 user_info->usri1_home_dir[0] != '\0')
1327 info->homedir = _dbus_win_utf16_to_utf8 (user_info->usri1_home_dir, error);
1333 _dbus_verbose("NetUserGetInfo() returned no home dir entry\n");
1334 /* Not set, so use something random. */
1335 info->homedir = _dbus_strdup ("\\");
1339 char *dc_string = _dbus_win_utf16_to_utf8(dc,error);
1340 char *user_name = _dbus_win_utf16_to_utf8(wname,error);
1341 _dbus_warn("NetUserGetInfo() for user '%s' failed with errorcode %d '%s', %s\n",user_name, ret,_dbus_lm_strerror(ret),dc_string);
1342 dbus_free(user_name);
1343 dbus_free(dc_string);
1344 /* Not set, so use something random. */
1345 info->homedir = _dbus_strdup ("\\");
1352 NetApiBufferFree (dc);
1353 if (user_info != NULL)
1354 NetApiBufferFree (user_info);
1361 fill_win_user_info_from_name (wchar_t *wname,
1369 dbus_bool_t retval = FALSE;
1372 DWORD sid_length, wdomain_length;
1377 if (!LookupAccountNameW (NULL, wname, NULL, &sid_length,
1378 NULL, &wdomain_length, &use) &&
1379 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1381 _dbus_win_set_error_from_win_error (error, GetLastError ());
1385 sid = dbus_malloc (sid_length);
1388 _DBUS_SET_OOM (error);
1392 wdomain = dbus_new (wchar_t, wdomain_length);
1395 _DBUS_SET_OOM (error);
1399 if (!LookupAccountNameW (NULL, wname, sid, &sid_length,
1400 wdomain, &wdomain_length, &use))
1402 _dbus_win_set_error_from_win_error (error, GetLastError ());
1406 if (!IsValidSid (sid))
1408 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1412 info->uid = _dbus_win_sid_to_uid_t (sid);
1414 if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1417 if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1423 dbus_free (wdomain);
1432 _dbus_win_sid_to_name_and_domain (dbus_uid_t uid,
1442 DWORD wname_length, wdomain_length;
1445 if (!_dbus_uid_t_to_win_sid (uid, &sid))
1447 _dbus_win_set_error_from_win_error (error, GetLastError ());
1453 if (!LookupAccountSidW (NULL, sid, NULL, &wname_length,
1454 NULL, &wdomain_length, &use) &&
1455 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1457 _dbus_win_set_error_from_win_error (error, GetLastError ());
1461 *wname = dbus_new (wchar_t, wname_length);
1464 _DBUS_SET_OOM (error);
1468 *wdomain = dbus_new (wchar_t, wdomain_length);
1471 _DBUS_SET_OOM (error);
1475 if (!LookupAccountSidW (NULL, sid, *wname, &wname_length,
1476 *wdomain, &wdomain_length, &use))
1478 _dbus_win_set_error_from_win_error (error, GetLastError ());
1485 dbus_free (*wdomain);
1498 fill_win_user_info_from_uid (dbus_uid_t uid,
1507 dbus_bool_t retval = FALSE;
1508 wchar_t *wname, *wdomain;
1512 if (!_dbus_win_sid_to_name_and_domain (uid, &wname, &wdomain, error))
1514 _dbus_verbose("%s after _dbus_win_sid_to_name_and_domain\n",__FUNCTION__);
1518 if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1520 _dbus_verbose("%s after fill_win_user_info_name_and_groups\n",__FUNCTION__);
1525 if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1527 _dbus_verbose("%s after fill_win_user_info_homedir\n",__FUNCTION__);
1534 dbus_free (wdomain);
1545 _dbus_win_startup_winsock (void)
1547 /* Straight from MSDN, deuglified */
1549 static dbus_bool_t beenhere = FALSE;
1551 WORD wVersionRequested;
1558 wVersionRequested = MAKEWORD (2, 0);
1560 err = WSAStartup (wVersionRequested, &wsaData);
1563 _dbus_assert_not_reached ("Could not initialize WinSock");
1567 /* Confirm that the WinSock DLL supports 2.0. Note that if the DLL
1568 * supports versions greater than 2.0 in addition to 2.0, it will
1569 * still return 2.0 in wVersion since that is the version we
1572 if (LOBYTE (wsaData.wVersion) != 2 ||
1573 HIBYTE (wsaData.wVersion) != 0)
1575 _dbus_assert_not_reached ("No usable WinSock found");
1590 /************************************************************************
1594 ************************************************************************/
1597 * Measure the message length without terminating nul
1599 int _dbus_printf_string_upper_bound (const char *format,
1602 /* MSVCRT's vsnprintf semantics are a bit different */
1603 /* The C library source in the Platform SDK indicates that this
1604 * would work, but alas, it doesn't. At least not on Windows
1605 * 2000. Presumably those sources correspond to the C library on
1606 * some newer or even future Windows version.
1608 len = _vsnprintf (NULL, _DBUS_INT_MAX, format, args);
1612 len = vsnprintf (p, sizeof(p)-1, format, args);
1613 if (len == -1) // try again
1616 p = malloc (strlen(format)*3);
1617 len = vsnprintf (p, sizeof(p)-1, format, args);
1625 * Returns the UTF-16 form of a UTF-8 string. The result should be
1626 * freed with dbus_free() when no longer needed.
1628 * @param str the UTF-8 string
1629 * @param error return location for error code
1632 _dbus_win_utf8_to_utf16 (const char *str,
1639 _dbus_string_init_const (&s, str);
1641 if (!_dbus_string_validate_utf8 (&s, 0, _dbus_string_get_length (&s)))
1643 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid UTF-8");
1647 n = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0);
1651 _dbus_win_set_error_from_win_error (error, GetLastError ());
1655 retval = dbus_new (wchar_t, n);
1659 _DBUS_SET_OOM (error);
1663 if (MultiByteToWideChar (CP_UTF8, 0, str, -1, retval, n) != n)
1666 dbus_set_error_const (error, DBUS_ERROR_FAILED, "MultiByteToWideChar inconsistency");
1674 * Returns the UTF-8 form of a UTF-16 string. The result should be
1675 * freed with dbus_free() when no longer needed.
1677 * @param str the UTF-16 string
1678 * @param error return location for error code
1681 _dbus_win_utf16_to_utf8 (const wchar_t *str,
1687 n = WideCharToMultiByte (CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
1691 _dbus_win_set_error_from_win_error (error, GetLastError ());
1695 retval = dbus_malloc (n);
1699 _DBUS_SET_OOM (error);
1703 if (WideCharToMultiByte (CP_UTF8, 0, str, -1, retval, n, NULL, NULL) != n)
1706 dbus_set_error_const (error, DBUS_ERROR_FAILED, "WideCharToMultiByte inconsistency");
1718 /************************************************************************
1720 uid ... <-> win sid functions
1722 ************************************************************************/
1725 _dbus_win_account_to_sid (const wchar_t *waccount,
1729 dbus_bool_t retval = FALSE;
1730 DWORD sid_length, wdomain_length;
1738 if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1739 NULL, &wdomain_length, &use) &&
1740 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1742 _dbus_win_set_error_from_win_error (error, GetLastError ());
1746 *ppsid = dbus_malloc (sid_length);
1749 _DBUS_SET_OOM (error);
1753 wdomain = dbus_new (wchar_t, wdomain_length);
1756 _DBUS_SET_OOM (error);
1760 if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1761 wdomain, &wdomain_length, &use))
1763 _dbus_win_set_error_from_win_error (error, GetLastError ());
1767 if (!IsValidSid ((PSID) *ppsid))
1769 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1776 dbus_free (wdomain);
1788 _sid_atom_cache_shutdown (void *unused)
1791 _DBUS_LOCK (sid_atom_cache);
1792 _dbus_hash_iter_init (sid_atom_cache, &iter);
1793 while (_dbus_hash_iter_next (&iter))
1796 atom = (ATOM) _dbus_hash_iter_get_value (&iter);
1797 GlobalDeleteAtom(atom);
1798 _dbus_hash_iter_remove_entry(&iter);
1800 _DBUS_UNLOCK (sid_atom_cache);
1801 _dbus_hash_table_unref (sid_atom_cache);
1802 sid_atom_cache = NULL;
1806 * Returns the 2-way associated dbus_uid_t form a SID.
1808 * @param psid pointer to the SID
1811 _dbus_win_sid_to_uid_t (PSID psid)
1818 if (!IsValidSid (psid))
1820 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1821 return DBUS_UID_UNSET;
1823 if (!ConvertSidToStringSidA (psid, &string))
1825 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1826 return DBUS_UID_UNSET;
1829 atom = GlobalAddAtom(string);
1833 _dbus_verbose("%s GlobalAddAtom failed\n",__FUNCTION__);
1835 return DBUS_UID_UNSET;
1838 _DBUS_LOCK (sid_atom_cache);
1840 if (sid_atom_cache == NULL)
1842 sid_atom_cache = _dbus_hash_table_new (DBUS_HASH_ULONG, NULL, NULL);
1843 _dbus_register_shutdown_func (_sid_atom_cache_shutdown, NULL);
1847 olduid = (dbus_uid_t) _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1851 _dbus_verbose("%s sid with id %i found in cache\n",__FUNCTION__, olduid);
1856 _dbus_hash_table_insert_ulong (sid_atom_cache, uid, (void*) uid);
1857 _dbus_verbose("%s sid %s added with uid %i to cache\n",__FUNCTION__, string, uid);
1860 _DBUS_UNLOCK (sid_atom_cache);
1865 dbus_bool_t _dbus_uid_t_to_win_sid (dbus_uid_t uid, PSID *ppsid)
1870 atom = _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1873 _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__,uid);
1876 memset( string, '.', sizeof(string) );
1877 if (!GlobalGetAtomNameA( (ATOM) atom, string, 255 ))
1879 _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__, uid);
1882 if (!ConvertStringSidToSidA(string, ppsid))
1884 _dbus_verbose("%s could not convert %s into sid \n",__FUNCTION__, string);
1887 _dbus_verbose("%s converted %s into sid \n",__FUNCTION__, string);
1892 /** @} end of sysdeps-win */
1896 * @returns process UID
1901 dbus_uid_t retval = DBUS_UID_UNSET;
1902 HANDLE process_token = NULL;
1903 TOKEN_USER *token_user = NULL;
1906 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1907 _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1908 else if ((!GetTokenInformation (process_token, TokenUser, NULL, 0, &n)
1909 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1910 || (token_user = alloca (n)) == NULL
1911 || !GetTokenInformation (process_token, TokenUser, token_user, n, &n))
1912 _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1914 retval = _dbus_win_sid_to_uid_t (token_user->User.Sid);
1916 if (process_token != NULL)
1917 CloseHandle (process_token);
1919 _dbus_verbose("_dbus_getuid() returns %d\n",retval);
1923 #ifdef DBUS_BUILD_TESTS
1925 * @returns process GID
1930 dbus_gid_t retval = DBUS_GID_UNSET;
1931 HANDLE process_token = NULL;
1932 TOKEN_PRIMARY_GROUP *token_primary_group = NULL;
1935 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1936 _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1937 else if ((!GetTokenInformation (process_token, TokenPrimaryGroup,
1939 GetLastError () != ERROR_INSUFFICIENT_BUFFER) ||
1940 (token_primary_group = alloca (n)) == NULL ||
1941 !GetTokenInformation (process_token, TokenPrimaryGroup,
1942 token_primary_group, n, &n))
1943 _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1945 retval = _dbus_win_sid_to_uid_t (token_primary_group->PrimaryGroup);
1947 if (process_token != NULL)
1948 CloseHandle (process_token);
1955 _dbus_domain_test (const char *test_data_dir)
1957 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1958 check_spawn_nonexistent,
1965 #endif //DBUS_BUILD_TESTS
1967 /************************************************************************
1971 ************************************************************************/
1974 * Creates a full-duplex pipe (as in socketpair()).
1975 * Sets both ends of the pipe nonblocking.
1977 * @todo libdbus only uses this for the debug-pipe server, so in
1978 * principle it could be in dbus-sysdeps-util.c, except that
1979 * dbus-sysdeps-util.c isn't in libdbus when tests are enabled and the
1980 * debug-pipe server is used.
1982 * @param fd1 return location for one end
1983 * @param fd2 return location for the other end
1984 * @param blocking #TRUE if pipe should be blocking
1985 * @param error error return
1986 * @returns #FALSE on failure (if error is set)
1989 _dbus_full_duplex_pipe (int *fd1,
1991 dbus_bool_t blocking,
1994 SOCKET temp, socket1 = -1, socket2 = -1;
1995 struct sockaddr_in saddr;
1998 fd_set read_set, write_set;
2003 _dbus_win_startup_winsock ();
2005 temp = socket (AF_INET, SOCK_STREAM, 0);
2006 if (temp == INVALID_SOCKET)
2008 DBUS_SOCKET_SET_ERRNO ();
2013 if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR)
2015 DBUS_SOCKET_SET_ERRNO ();
2020 saddr.sin_family = AF_INET;
2022 saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
2024 if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)))
2026 DBUS_SOCKET_SET_ERRNO ();
2030 if (listen (temp, 1) == SOCKET_ERROR)
2032 DBUS_SOCKET_SET_ERRNO ();
2036 len = sizeof (saddr);
2037 if (getsockname (temp, (struct sockaddr *)&saddr, &len))
2039 DBUS_SOCKET_SET_ERRNO ();
2043 socket1 = socket (AF_INET, SOCK_STREAM, 0);
2044 if (socket1 == INVALID_SOCKET)
2046 DBUS_SOCKET_SET_ERRNO ();
2051 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2053 DBUS_SOCKET_SET_ERRNO ();
2057 if (connect (socket1, (struct sockaddr *)&saddr, len) != SOCKET_ERROR ||
2058 WSAGetLastError () != WSAEWOULDBLOCK)
2060 dbus_set_error_const (error, DBUS_ERROR_FAILED,
2061 "_dbus_full_duplex_pipe socketpair() emulation failed");
2065 FD_ZERO (&read_set);
2066 FD_SET (temp, &read_set);
2071 if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR)
2073 DBUS_SOCKET_SET_ERRNO ();
2077 _dbus_assert (FD_ISSET (temp, &read_set));
2079 socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
2080 if (socket2 == INVALID_SOCKET)
2082 DBUS_SOCKET_SET_ERRNO ();
2086 FD_ZERO (&write_set);
2087 FD_SET (socket1, &write_set);
2092 if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR)
2094 DBUS_SOCKET_SET_ERRNO ();
2098 _dbus_assert (FD_ISSET (socket1, &write_set));
2103 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2105 DBUS_SOCKET_SET_ERRNO ();
2110 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2112 DBUS_SOCKET_SET_ERRNO ();
2119 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2121 DBUS_SOCKET_SET_ERRNO ();
2127 *fd1 = _dbus_socket_to_handle (&sock);
2129 *fd2 = _dbus_socket_to_handle (&sock);
2131 _dbus_verbose ("full-duplex pipe %d:%d <-> %d:%d\n",
2132 *fd1, socket1, *fd2, socket2);
2139 closesocket (socket2);
2141 closesocket (socket1);
2145 dbus_set_error (error, _dbus_error_from_errno (errno),
2146 "Could not setup socket pair: %s",
2147 _dbus_strerror (errno));
2153 * Wrapper for poll().
2155 * @param fds the file descriptors to poll
2156 * @param n_fds number of descriptors in the array
2157 * @param timeout_milliseconds timeout or -1 for infinite
2158 * @returns numbers of fds with revents, or <0 on error
2160 #define USE_CHRIS_IMPL 0
2163 _dbus_poll (DBusPollFD *fds,
2165 int timeout_milliseconds)
2167 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2168 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2176 #define DBUS_STACK_WSAEVENTS 256
2177 WSAEVENT eventsOnStack[DBUS_STACK_WSAEVENTS];
2178 WSAEVENT *pEvents = NULL;
2179 if (n_fds > DBUS_STACK_WSAEVENTS)
2180 pEvents = calloc(sizeof(WSAEVENT), n_fds);
2182 pEvents = eventsOnStack;
2184 _dbus_lock_sockets();
2186 #ifdef DBUS_ENABLE_VERBOSE_MODE
2188 msgp += sprintf (msgp, "WSAEventSelect: to=%d\n\t", timeout_milliseconds);
2189 for (i = 0; i < n_fds; i++)
2191 static dbus_bool_t warned = FALSE;
2193 DBusPollFD *fdp = &fds[i];
2195 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2197 if (s->is_used == 0)
2199 _dbus_warn ("no valid socket");
2203 if (fdp->events & _DBUS_POLLIN)
2204 msgp += sprintf (msgp, "R:%d ", s->fd);
2206 if (fdp->events & _DBUS_POLLOUT)
2207 msgp += sprintf (msgp, "W:%d ", s->fd);
2209 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2211 // FIXME: more robust code for long msg
2212 // create on heap when msg[] becomes too small
2213 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2215 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2219 msgp += sprintf (msgp, "\n");
2220 _dbus_verbose ("%s",msg);
2222 for (i = 0; i < n_fds; i++)
2225 DBusPollFD *fdp = &fds[i];
2227 long lNetworkEvents = FD_OOB;
2229 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2231 if (s->is_used == 0)
2234 ev = WSACreateEvent();
2236 if (fdp->events & _DBUS_POLLIN)
2237 lNetworkEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
2239 if (fdp->events & _DBUS_POLLOUT)
2240 lNetworkEvents |= FD_WRITE | FD_CONNECT;
2242 WSAEventSelect(s->fd, ev, lNetworkEvents);
2247 _dbus_unlock_sockets();
2249 ready = WSAWaitForMultipleEvents (n_fds, pEvents, FALSE, timeout_milliseconds, FALSE);
2251 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2253 DBUS_SOCKET_SET_ERRNO ();
2254 if (errno != EWOULDBLOCK)
2255 _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", _dbus_strerror (errno));
2258 else if (ready == WSA_WAIT_TIMEOUT)
2260 _dbus_verbose ("WSAWaitForMultipleEvents: WSA_WAIT_TIMEOUT\n");
2263 else if (ready >= WSA_WAIT_EVENT_0 && ready < (int)(WSA_WAIT_EVENT_0 + n_fds))
2266 msgp += sprintf (msgp, "WSAWaitForMultipleEvents: =%d\n\t", ready);
2268 _dbus_lock_sockets();
2269 for (i = 0; i < n_fds; i++)
2272 DBusPollFD *fdp = &fds[i];
2273 WSANETWORKEVENTS ne;
2275 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2279 WSAEnumNetworkEvents(s->fd, pEvents[i], &ne);
2281 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2282 fdp->revents |= _DBUS_POLLIN;
2284 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2285 fdp->revents |= _DBUS_POLLOUT;
2287 if (ne.lNetworkEvents & (FD_OOB))
2288 fdp->revents |= _DBUS_POLLERR;
2290 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2291 msgp += sprintf (msgp, "R:%d ", s->fd);
2293 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2294 msgp += sprintf (msgp, "W:%d ", s->fd);
2296 if (ne.lNetworkEvents & (FD_OOB))
2297 msgp += sprintf (msgp, "E:%d ", s->fd);
2299 msgp += sprintf (msgp, "lNetworkEvents:%d ", ne.lNetworkEvents);
2301 if(ne.lNetworkEvents)
2304 WSAEventSelect(s->fd, pEvents[i], 0);
2306 _dbus_unlock_sockets();
2308 msgp += sprintf (msgp, "\n");
2309 _dbus_verbose ("%s",msg);
2313 _dbus_verbose ("WSAWaitForMultipleEvents: failed for unknown reason!");
2317 for(i = 0; i < n_fds; i++)
2319 WSACloseEvent(pEvents[i]);
2322 if (n_fds > DBUS_STACK_WSAEVENTS)
2328 #else // USE_CHRIS_IMPL
2331 _dbus_poll (DBusPollFD *fds,
2333 int timeout_milliseconds)
2335 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2336 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2339 fd_set read_set, write_set, err_set;
2345 FD_ZERO (&read_set);
2346 FD_ZERO (&write_set);
2349 _dbus_lock_sockets();
2351 #ifdef DBUS_ENABLE_VERBOSE_MODE
2353 msgp += sprintf (msgp, "select: to=%d\n\t", timeout_milliseconds);
2354 for (i = 0; i < n_fds; i++)
2356 static dbus_bool_t warned = FALSE;
2358 DBusPollFD *fdp = &fds[i];
2360 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2362 if (s->is_used == 0)
2364 _dbus_warn ("no valid socket");
2368 if (fdp->events & _DBUS_POLLIN)
2369 msgp += sprintf (msgp, "R:%d ", s->fd);
2371 if (fdp->events & _DBUS_POLLOUT)
2372 msgp += sprintf (msgp, "W:%d ", s->fd);
2374 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2376 // FIXME: more robust code for long msg
2377 // create on heap when msg[] becomes too small
2378 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2380 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2384 msgp += sprintf (msgp, "\n");
2385 _dbus_verbose ("%s",msg);
2387 for (i = 0; i < n_fds; i++)
2390 DBusPollFD *fdp = &fds[i];
2392 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2394 if (s->is_used != 1)
2397 if (fdp->events & _DBUS_POLLIN)
2398 FD_SET (s->fd, &read_set);
2400 if (fdp->events & _DBUS_POLLOUT)
2401 FD_SET (s->fd, &write_set);
2403 FD_SET (s->fd, &err_set);
2405 max_fd = MAX (max_fd, s->fd);
2408 _dbus_unlock_sockets();
2410 tv.tv_sec = timeout_milliseconds / 1000;
2411 tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
2413 ready = select (max_fd + 1, &read_set, &write_set, &err_set,
2414 timeout_milliseconds < 0 ? NULL : &tv);
2416 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2418 DBUS_SOCKET_SET_ERRNO ();
2419 if (errno != EWOULDBLOCK)
2420 _dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno));
2422 else if (ready == 0)
2423 _dbus_verbose ("select: = 0\n");
2427 #ifdef DBUS_ENABLE_VERBOSE_MODE
2429 msgp += sprintf (msgp, "select: = %d:\n\t", ready);
2430 _dbus_lock_sockets();
2431 for (i = 0; i < n_fds; i++)
2434 DBusPollFD *fdp = &fds[i];
2436 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2438 if (FD_ISSET (s->fd, &read_set))
2439 msgp += sprintf (msgp, "R:%d ", s->fd);
2441 if (FD_ISSET (s->fd, &write_set))
2442 msgp += sprintf (msgp, "W:%d ", s->fd);
2444 if (FD_ISSET (s->fd, &err_set))
2445 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2447 msgp += sprintf (msgp, "\n");
2448 _dbus_verbose ("%s",msg);
2451 for (i = 0; i < n_fds; i++)
2454 DBusPollFD *fdp = &fds[i];
2456 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2460 if (FD_ISSET (s->fd, &read_set))
2461 fdp->revents |= _DBUS_POLLIN;
2463 if (FD_ISSET (s->fd, &write_set))
2464 fdp->revents |= _DBUS_POLLOUT;
2466 if (FD_ISSET (s->fd, &err_set))
2467 fdp->revents |= _DBUS_POLLERR;
2469 _dbus_unlock_sockets();
2473 #endif // USE_CHRIS_IMPL
2476 /************************************************************************
2480 ************************************************************************/
2484 * Assigns an error name and message corresponding to a Win32 error
2485 * code to a DBusError. Does nothing if error is #NULL.
2487 * @param error the error.
2488 * @param code the Win32 error code
2491 _dbus_win_set_error_from_win_error (DBusError *error,
2496 /* As we want the English message, use the A API */
2497 FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
2498 FORMAT_MESSAGE_IGNORE_INSERTS |
2499 FORMAT_MESSAGE_FROM_SYSTEM,
2500 NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
2501 (LPTSTR) &msg, 0, NULL);
2506 msg_copy = dbus_malloc (strlen (msg));
2507 strcpy (msg_copy, msg);
2510 dbus_set_error (error, "win32.error", "%s", msg_copy);
2513 dbus_set_error (error, "win32.error", "Unknown error code %d or FormatMessage failed", code);
2517 _dbus_win_warn_win_error (const char *message,
2522 dbus_error_init (&error);
2523 _dbus_win_set_error_from_win_error (&error, code);
2524 _dbus_warn ("%s: %s\n", message, error.message);
2525 dbus_error_free (&error);
2529 * A wrapper around strerror() because some platforms
2530 * may be lame and not have strerror().
2532 * @param error_number errno.
2533 * @returns error description.
2536 _dbus_strerror (int error_number)
2544 switch (error_number)
2547 return "Interrupted function call";
2549 return "Permission denied";
2551 return "Bad address";
2553 return "Invalid argument";
2555 return "Too many open files";
2556 case WSAEWOULDBLOCK:
2557 return "Resource temporarily unavailable";
2558 case WSAEINPROGRESS:
2559 return "Operation now in progress";
2561 return "Operation already in progress";
2563 return "Socket operation on nonsocket";
2564 case WSAEDESTADDRREQ:
2565 return "Destination address required";
2567 return "Message too long";
2569 return "Protocol wrong type for socket";
2570 case WSAENOPROTOOPT:
2571 return "Bad protocol option";
2572 case WSAEPROTONOSUPPORT:
2573 return "Protocol not supported";
2574 case WSAESOCKTNOSUPPORT:
2575 return "Socket type not supported";
2577 return "Operation not supported";
2578 case WSAEPFNOSUPPORT:
2579 return "Protocol family not supported";
2580 case WSAEAFNOSUPPORT:
2581 return "Address family not supported by protocol family";
2583 return "Address already in use";
2584 case WSAEADDRNOTAVAIL:
2585 return "Cannot assign requested address";
2587 return "Network is down";
2588 case WSAENETUNREACH:
2589 return "Network is unreachable";
2591 return "Network dropped connection on reset";
2592 case WSAECONNABORTED:
2593 return "Software caused connection abort";
2595 return "Connection reset by peer";
2597 return "No buffer space available";
2599 return "Socket is already connected";
2601 return "Socket is not connected";
2603 return "Cannot send after socket shutdown";
2605 return "Connection timed out";
2606 case WSAECONNREFUSED:
2607 return "Connection refused";
2609 return "Host is down";
2610 case WSAEHOSTUNREACH:
2611 return "No route to host";
2613 return "Too many processes";
2615 return "Graceful shutdown in progress";
2616 case WSATYPE_NOT_FOUND:
2617 return "Class type not found";
2618 case WSAHOST_NOT_FOUND:
2619 return "Host not found";
2621 return "Nonauthoritative host not found";
2622 case WSANO_RECOVERY:
2623 return "This is a nonrecoverable error";
2625 return "Valid name, no data record of requested type";
2626 case WSA_INVALID_HANDLE:
2627 return "Specified event object handle is invalid";
2628 case WSA_INVALID_PARAMETER:
2629 return "One or more parameters are invalid";
2630 case WSA_IO_INCOMPLETE:
2631 return "Overlapped I/O event object not in signaled state";
2632 case WSA_IO_PENDING:
2633 return "Overlapped operations will complete later";
2634 case WSA_NOT_ENOUGH_MEMORY:
2635 return "Insufficient memory available";
2636 case WSA_OPERATION_ABORTED:
2637 return "Overlapped operation aborted";
2638 #ifdef WSAINVALIDPROCTABLE
2640 case WSAINVALIDPROCTABLE:
2641 return "Invalid procedure table from service provider";
2643 #ifdef WSAINVALIDPROVIDER
2645 case WSAINVALIDPROVIDER:
2646 return "Invalid service provider version number";
2648 #ifdef WSAPROVIDERFAILEDINIT
2650 case WSAPROVIDERFAILEDINIT:
2651 return "Unable to initialize a service provider";
2654 case WSASYSCALLFAILURE:
2655 return "System call failure";
2657 msg = strerror (error_number);
2667 /* lan manager error codes */
2669 _dbus_lm_strerror(int error_number)
2676 switch (error_number)
2678 case NERR_NetNotStarted:
2679 return "The workstation driver is not installed.";
2680 case NERR_UnknownServer:
2681 return "The server could not be located.";
2683 return "An internal error occurred. The network cannot access a shared memory segment.";
2684 case NERR_NoNetworkResource:
2685 return "A network resource shortage occurred.";
2686 case NERR_RemoteOnly:
2687 return "This operation is not supported on workstations.";
2688 case NERR_DevNotRedirected:
2689 return "The device is not connected.";
2690 case NERR_ServerNotStarted:
2691 return "The Server service is not started.";
2692 case NERR_ItemNotFound:
2693 return "The queue is empty.";
2694 case NERR_UnknownDevDir:
2695 return "The device or directory does not exist.";
2696 case NERR_RedirectedPath:
2697 return "The operation is invalid on a redirected resource.";
2698 case NERR_DuplicateShare:
2699 return "The name has already been shared.";
2701 return "The server is currently out of the requested resource.";
2702 case NERR_TooManyItems:
2703 return "Requested addition of items exceeds the maximum allowed.";
2704 case NERR_InvalidMaxUsers:
2705 return "The Peer service supports only two simultaneous users.";
2706 case NERR_BufTooSmall:
2707 return "The API return buffer is too small.";
2708 case NERR_RemoteErr:
2709 return "A remote API error occurred.";
2710 case NERR_LanmanIniError:
2711 return "An error occurred when opening or reading the configuration file.";
2712 case NERR_NetworkError:
2713 return "A general network error occurred.";
2714 case NERR_WkstaInconsistentState:
2715 return "The Workstation service is in an inconsistent state. Restart the computer before restarting the Workstation service.";
2716 case NERR_WkstaNotStarted:
2717 return "The Workstation service has not been started.";
2718 case NERR_BrowserNotStarted:
2719 return "The requested information is not available.";
2720 case NERR_InternalError:
2721 return "An internal error occurred.";
2722 case NERR_BadTransactConfig:
2723 return "The server is not configured for transactions.";
2724 case NERR_InvalidAPI:
2725 return "The requested API is not supported on the remote server.";
2726 case NERR_BadEventName:
2727 return "The event name is invalid.";
2728 case NERR_DupNameReboot:
2729 return "The computer name already exists on the network. Change it and restart the computer.";
2730 case NERR_CfgCompNotFound:
2731 return "The specified component could not be found in the configuration information.";
2732 case NERR_CfgParamNotFound:
2733 return "The specified parameter could not be found in the configuration information.";
2734 case NERR_LineTooLong:
2735 return "A line in the configuration file is too long.";
2736 case NERR_QNotFound:
2737 return "The printer does not exist.";
2738 case NERR_JobNotFound:
2739 return "The print job does not exist.";
2740 case NERR_DestNotFound:
2741 return "The printer destination cannot be found.";
2742 case NERR_DestExists:
2743 return "The printer destination already exists.";
2745 return "The printer queue already exists.";
2747 return "No more printers can be added.";
2748 case NERR_JobNoRoom:
2749 return "No more print jobs can be added.";
2750 case NERR_DestNoRoom:
2751 return "No more printer destinations can be added.";
2753 return "This printer destination is idle and cannot accept control operations.";
2754 case NERR_DestInvalidOp:
2755 return "This printer destination request contains an invalid control function.";
2756 case NERR_ProcNoRespond:
2757 return "The print processor is not responding.";
2758 case NERR_SpoolerNotLoaded:
2759 return "The spooler is not running.";
2760 case NERR_DestInvalidState:
2761 return "This operation cannot be performed on the print destination in its current state.";
2762 case NERR_QInvalidState:
2763 return "This operation cannot be performed on the printer queue in its current state.";
2764 case NERR_JobInvalidState:
2765 return "This operation cannot be performed on the print job in its current state.";
2766 case NERR_SpoolNoMemory:
2767 return "A spooler memory allocation failure occurred.";
2768 case NERR_DriverNotFound:
2769 return "The device driver does not exist.";
2770 case NERR_DataTypeInvalid:
2771 return "The data type is not supported by the print processor.";
2772 case NERR_ProcNotFound:
2773 return "The print processor is not installed.";
2774 case NERR_ServiceTableLocked:
2775 return "The service database is locked.";
2776 case NERR_ServiceTableFull:
2777 return "The service table is full.";
2778 case NERR_ServiceInstalled:
2779 return "The requested service has already been started.";
2780 case NERR_ServiceEntryLocked:
2781 return "The service does not respond to control actions.";
2782 case NERR_ServiceNotInstalled:
2783 return "The service has not been started.";
2784 case NERR_BadServiceName:
2785 return "The service name is invalid.";
2786 case NERR_ServiceCtlTimeout:
2787 return "The service is not responding to the control function.";
2788 case NERR_ServiceCtlBusy:
2789 return "The service control is busy.";
2790 case NERR_BadServiceProgName:
2791 return "The configuration file contains an invalid service program name.";
2792 case NERR_ServiceNotCtrl:
2793 return "The service could not be controlled in its present state.";
2794 case NERR_ServiceKillProc:
2795 return "The service ended abnormally.";
2796 case NERR_ServiceCtlNotValid:
2797 return "The requested pause or stop is not valid for this service.";
2798 case NERR_NotInDispatchTbl:
2799 return "The service control dispatcher could not find the service name in the dispatch table.";
2800 case NERR_BadControlRecv:
2801 return "The service control dispatcher pipe read failed.";
2802 case NERR_ServiceNotStarting:
2803 return "A thread for the new service could not be created.";
2804 case NERR_AlreadyLoggedOn:
2805 return "This workstation is already logged on to the local-area network.";
2806 case NERR_NotLoggedOn:
2807 return "The workstation is not logged on to the local-area network.";
2808 case NERR_BadUsername:
2809 return "The user name or group name parameter is invalid.";
2810 case NERR_BadPassword:
2811 return "The password parameter is invalid.";
2812 case NERR_UnableToAddName_W:
2813 return "@W The logon processor did not add the message alias.";
2814 case NERR_UnableToAddName_F:
2815 return "The logon processor did not add the message alias.";
2816 case NERR_UnableToDelName_W:
2817 return "@W The logoff processor did not delete the message alias.";
2818 case NERR_UnableToDelName_F:
2819 return "The logoff processor did not delete the message alias.";
2820 case NERR_LogonsPaused:
2821 return "Network logons are paused.";
2822 case NERR_LogonServerConflict:
2823 return "A centralized logon-server conflict occurred.";
2824 case NERR_LogonNoUserPath:
2825 return "The server is configured without a valid user path.";
2826 case NERR_LogonScriptError:
2827 return "An error occurred while loading or running the logon script.";
2828 case NERR_StandaloneLogon:
2829 return "The logon server was not specified. Your computer will be logged on as STANDALONE.";
2830 case NERR_LogonServerNotFound:
2831 return "The logon server could not be found.";
2832 case NERR_LogonDomainExists:
2833 return "There is already a logon domain for this computer.";
2834 case NERR_NonValidatedLogon:
2835 return "The logon server could not validate the logon.";
2836 case NERR_ACFNotFound:
2837 return "The security database could not be found.";
2838 case NERR_GroupNotFound:
2839 return "The group name could not be found.";
2840 case NERR_UserNotFound:
2841 return "The user name could not be found.";
2842 case NERR_ResourceNotFound:
2843 return "The resource name could not be found.";
2844 case NERR_GroupExists:
2845 return "The group already exists.";
2846 case NERR_UserExists:
2847 return "The user account already exists.";
2848 case NERR_ResourceExists:
2849 return "The resource permission list already exists.";
2850 case NERR_NotPrimary:
2851 return "This operation is only allowed on the primary domain controller of the domain.";
2852 case NERR_ACFNotLoaded:
2853 return "The security database has not been started.";
2854 case NERR_ACFNoRoom:
2855 return "There are too many names in the user accounts database.";
2856 case NERR_ACFFileIOFail:
2857 return "A disk I/O failure occurred.";
2858 case NERR_ACFTooManyLists:
2859 return "The limit of 64 entries per resource was exceeded.";
2860 case NERR_UserLogon:
2861 return "Deleting a user with a session is not allowed.";
2862 case NERR_ACFNoParent:
2863 return "The parent directory could not be located.";
2864 case NERR_CanNotGrowSegment:
2865 return "Unable to add to the security database session cache segment.";
2866 case NERR_SpeGroupOp:
2867 return "This operation is not allowed on this special group.";
2868 case NERR_NotInCache:
2869 return "This user is not cached in user accounts database session cache.";
2870 case NERR_UserInGroup:
2871 return "The user already belongs to this group.";
2872 case NERR_UserNotInGroup:
2873 return "The user does not belong to this group.";
2874 case NERR_AccountUndefined:
2875 return "This user account is undefined.";
2876 case NERR_AccountExpired:
2877 return "This user account has expired.";
2878 case NERR_InvalidWorkstation:
2879 return "The user is not allowed to log on from this workstation.";
2880 case NERR_InvalidLogonHours:
2881 return "The user is not allowed to log on at this time.";
2882 case NERR_PasswordExpired:
2883 return "The password of this user has expired.";
2884 case NERR_PasswordCantChange:
2885 return "The password of this user cannot change.";
2886 case NERR_PasswordHistConflict:
2887 return "This password cannot be used now.";
2888 case NERR_PasswordTooShort:
2889 return "The password does not meet the password policy requirements. Check the minimum password length, password complexity and password history requirements.";
2890 case NERR_PasswordTooRecent:
2891 return "The password of this user is too recent to change.";
2892 case NERR_InvalidDatabase:
2893 return "The security database is corrupted.";
2894 case NERR_DatabaseUpToDate:
2895 return "No updates are necessary to this replicant network/local security database.";
2896 case NERR_SyncRequired:
2897 return "This replicant database is outdated; synchronization is required.";
2898 case NERR_UseNotFound:
2899 return "The network connection could not be found.";
2900 case NERR_BadAsgType:
2901 return "This asg_type is invalid.";
2902 case NERR_DeviceIsShared:
2903 return "This device is currently being shared.";
2904 case NERR_NoComputerName:
2905 return "The computer name could not be added as a message alias. The name may already exist on the network.";
2906 case NERR_MsgAlreadyStarted:
2907 return "The Messenger service is already started.";
2908 case NERR_MsgInitFailed:
2909 return "The Messenger service failed to start.";
2910 case NERR_NameNotFound:
2911 return "The message alias could not be found on the network.";
2912 case NERR_AlreadyForwarded:
2913 return "This message alias has already been forwarded.";
2914 case NERR_AddForwarded:
2915 return "This message alias has been added but is still forwarded.";
2916 case NERR_AlreadyExists:
2917 return "This message alias already exists locally.";
2918 case NERR_TooManyNames:
2919 return "The maximum number of added message aliases has been exceeded.";
2920 case NERR_DelComputerName:
2921 return "The computer name could not be deleted.";
2922 case NERR_LocalForward:
2923 return "Messages cannot be forwarded back to the same workstation.";
2924 case NERR_GrpMsgProcessor:
2925 return "An error occurred in the domain message processor.";
2926 case NERR_PausedRemote:
2927 return "The message was sent, but the recipient has paused the Messenger service.";
2928 case NERR_BadReceive:
2929 return "The message was sent but not received.";
2930 case NERR_NameInUse:
2931 return "The message alias is currently in use. Try again later.";
2932 case NERR_MsgNotStarted:
2933 return "The Messenger service has not been started.";
2934 case NERR_NotLocalName:
2935 return "The name is not on the local computer.";
2936 case NERR_NoForwardName:
2937 return "The forwarded message alias could not be found on the network.";
2938 case NERR_RemoteFull:
2939 return "The message alias table on the remote station is full.";
2940 case NERR_NameNotForwarded:
2941 return "Messages for this alias are not currently being forwarded.";
2942 case NERR_TruncatedBroadcast:
2943 return "The broadcast message was truncated.";
2944 case NERR_InvalidDevice:
2945 return "This is an invalid device name.";
2946 case NERR_WriteFault:
2947 return "A write fault occurred.";
2948 case NERR_DuplicateName:
2949 return "A duplicate message alias exists on the network.";
2950 case NERR_DeleteLater:
2951 return "@W This message alias will be deleted later.";
2952 case NERR_IncompleteDel:
2953 return "The message alias was not successfully deleted from all networks.";
2954 case NERR_MultipleNets:
2955 return "This operation is not supported on computers with multiple networks.";
2956 case NERR_NetNameNotFound:
2957 return "This shared resource does not exist.";
2958 case NERR_DeviceNotShared:
2959 return "This device is not shared.";
2960 case NERR_ClientNameNotFound:
2961 return "A session does not exist with that computer name.";
2962 case NERR_FileIdNotFound:
2963 return "There is not an open file with that identification number.";
2964 case NERR_ExecFailure:
2965 return "A failure occurred when executing a remote administration command.";
2967 return "A failure occurred when opening a remote temporary file.";
2968 case NERR_TooMuchData:
2969 return "The data returned from a remote administration command has been truncated to 64K.";
2970 case NERR_DeviceShareConflict:
2971 return "This device cannot be shared as both a spooled and a non-spooled resource.";
2972 case NERR_BrowserTableIncomplete:
2973 return "The information in the list of servers may be incorrect.";
2974 case NERR_NotLocalDomain:
2975 return "The computer is not active in this domain.";
2976 #ifdef NERR_IsDfsShare
2978 case NERR_IsDfsShare:
2979 return "The share must be removed from the Distributed File System before it can be deleted.";
2982 case NERR_DevInvalidOpCode:
2983 return "The operation is invalid for this device.";
2984 case NERR_DevNotFound:
2985 return "This device cannot be shared.";
2986 case NERR_DevNotOpen:
2987 return "This device was not open.";
2988 case NERR_BadQueueDevString:
2989 return "This device name list is invalid.";
2990 case NERR_BadQueuePriority:
2991 return "The queue priority is invalid.";
2992 case NERR_NoCommDevs:
2993 return "There are no shared communication devices.";
2994 case NERR_QueueNotFound:
2995 return "The queue you specified does not exist.";
2996 case NERR_BadDevString:
2997 return "This list of devices is invalid.";
2999 return "The requested device is invalid.";
3000 case NERR_InUseBySpooler:
3001 return "This device is already in use by the spooler.";
3002 case NERR_CommDevInUse:
3003 return "This device is already in use as a communication device.";
3004 case NERR_InvalidComputer:
3005 return "This computer name is invalid.";
3006 case NERR_MaxLenExceeded:
3007 return "The string and prefix specified are too long.";
3008 case NERR_BadComponent:
3009 return "This path component is invalid.";
3011 return "Could not determine the type of input.";
3012 case NERR_TooManyEntries:
3013 return "The buffer for types is not big enough.";
3014 case NERR_ProfileFileTooBig:
3015 return "Profile files cannot exceed 64K.";
3016 case NERR_ProfileOffset:
3017 return "The start offset is out of range.";
3018 case NERR_ProfileCleanup:
3019 return "The system cannot delete current connections to network resources.";
3020 case NERR_ProfileUnknownCmd:
3021 return "The system was unable to parse the command line in this file.";
3022 case NERR_ProfileLoadErr:
3023 return "An error occurred while loading the profile file.";
3024 case NERR_ProfileSaveErr:
3025 return "@W Errors occurred while saving the profile file. The profile was partially saved.";
3026 case NERR_LogOverflow:
3027 return "Log file %1 is full.";
3028 case NERR_LogFileChanged:
3029 return "This log file has changed between reads.";
3030 case NERR_LogFileCorrupt:
3031 return "Log file %1 is corrupt.";
3032 case NERR_SourceIsDir:
3033 return "The source path cannot be a directory.";
3034 case NERR_BadSource:
3035 return "The source path is illegal.";
3037 return "The destination path is illegal.";
3038 case NERR_DifferentServers:
3039 return "The source and destination paths are on different servers.";
3040 case NERR_RunSrvPaused:
3041 return "The Run server you requested is paused.";
3042 case NERR_ErrCommRunSrv:
3043 return "An error occurred when communicating with a Run server.";
3044 case NERR_ErrorExecingGhost:
3045 return "An error occurred when starting a background process.";
3046 case NERR_ShareNotFound:
3047 return "The shared resource you are connected to could not be found.";
3048 case NERR_InvalidLana:
3049 return "The LAN adapter number is invalid.";
3050 case NERR_OpenFiles:
3051 return "There are open files on the connection.";
3052 case NERR_ActiveConns:
3053 return "Active connections still exist.";
3054 case NERR_BadPasswordCore:
3055 return "This share name or password is invalid.";
3057 return "The device is being accessed by an active process.";
3058 case NERR_LocalDrive:
3059 return "The drive letter is in use locally.";
3060 case NERR_AlertExists:
3061 return "The specified client is already registered for the specified event.";
3062 case NERR_TooManyAlerts:
3063 return "The alert table is full.";
3064 case NERR_NoSuchAlert:
3065 return "An invalid or nonexistent alert name was raised.";
3066 case NERR_BadRecipient:
3067 return "The alert recipient is invalid.";
3068 case NERR_AcctLimitExceeded:
3069 return "A user's session with this server has been deleted.";
3070 case NERR_InvalidLogSeek:
3071 return "The log file does not contain the requested record number.";
3072 case NERR_BadUasConfig:
3073 return "The user accounts database is not configured correctly.";
3074 case NERR_InvalidUASOp:
3075 return "This operation is not permitted when the Netlogon service is running.";
3076 case NERR_LastAdmin:
3077 return "This operation is not allowed on the last administrative account.";
3078 case NERR_DCNotFound:
3079 return "Could not find domain controller for this domain.";
3080 case NERR_LogonTrackingError:
3081 return "Could not set logon information for this user.";
3082 case NERR_NetlogonNotStarted:
3083 return "The Netlogon service has not been started.";
3084 case NERR_CanNotGrowUASFile:
3085 return "Unable to add to the user accounts database.";
3086 case NERR_TimeDiffAtDC:
3087 return "This server's clock is not synchronized with the primary domain controller's clock.";
3088 case NERR_PasswordMismatch:
3089 return "A password mismatch has been detected.";
3090 case NERR_NoSuchServer:
3091 return "The server identification does not specify a valid server.";
3092 case NERR_NoSuchSession:
3093 return "The session identification does not specify a valid session.";
3094 case NERR_NoSuchConnection:
3095 return "The connection identification does not specify a valid connection.";
3096 case NERR_TooManyServers:
3097 return "There is no space for another entry in the table of available servers.";
3098 case NERR_TooManySessions:
3099 return "The server has reached the maximum number of sessions it supports.";
3100 case NERR_TooManyConnections:
3101 return "The server has reached the maximum number of connections it supports.";
3102 case NERR_TooManyFiles:
3103 return "The server cannot open more files because it has reached its maximum number.";
3104 case NERR_NoAlternateServers:
3105 return "There are no alternate servers registered on this server.";
3106 case NERR_TryDownLevel:
3107 return "Try down-level (remote admin protocol) version of API instead.";
3108 case NERR_UPSDriverNotStarted:
3109 return "The UPS driver could not be accessed by the UPS service.";
3110 case NERR_UPSInvalidConfig:
3111 return "The UPS service is not configured correctly.";
3112 case NERR_UPSInvalidCommPort:
3113 return "The UPS service could not access the specified Comm Port.";
3114 case NERR_UPSSignalAsserted:
3115 return "The UPS indicated a line fail or low battery situation. Service not started.";
3116 case NERR_UPSShutdownFailed:
3117 return "The UPS service failed to perform a system shut down.";
3118 case NERR_BadDosRetCode:
3119 return "The program below returned an MS-DOS error code:";
3120 case NERR_ProgNeedsExtraMem:
3121 return "The program below needs more memory:";
3122 case NERR_BadDosFunction:
3123 return "The program below called an unsupported MS-DOS function:";
3124 case NERR_RemoteBootFailed:
3125 return "The workstation failed to boot.";
3126 case NERR_BadFileCheckSum:
3127 return "The file below is corrupt.";
3128 case NERR_NoRplBootSystem:
3129 return "No loader is specified in the boot-block definition file.";
3130 case NERR_RplLoadrNetBiosErr:
3131 return "NetBIOS returned an error: The NCB and SMB are dumped above.";
3132 case NERR_RplLoadrDiskErr:
3133 return "A disk I/O error occurred.";
3134 case NERR_ImageParamErr:
3135 return "Image parameter substitution failed.";
3136 case NERR_TooManyImageParams:
3137 return "Too many image parameters cross disk sector boundaries.";
3138 case NERR_NonDosFloppyUsed:
3139 return "The image was not generated from an MS-DOS diskette formatted with /S.";
3140 case NERR_RplBootRestart:
3141 return "Remote boot will be restarted later.";
3142 case NERR_RplSrvrCallFailed:
3143 return "The call to the Remoteboot server failed.";
3144 case NERR_CantConnectRplSrvr:
3145 return "Cannot connect to the Remoteboot server.";
3146 case NERR_CantOpenImageFile:
3147 return "Cannot open image file on the Remoteboot server.";
3148 case NERR_CallingRplSrvr:
3149 return "Connecting to the Remoteboot server...";
3150 case NERR_StartingRplBoot:
3151 return "Connecting to the Remoteboot server...";
3152 case NERR_RplBootServiceTerm:
3153 return "Remote boot service was stopped; check the error log for the cause of the problem.";
3154 case NERR_RplBootStartFailed:
3155 return "Remote boot startup failed; check the error log for the cause of the problem.";
3156 case NERR_RPL_CONNECTED:
3157 return "A second connection to a Remoteboot resource is not allowed.";
3158 case NERR_BrowserConfiguredToNotRun:
3159 return "The browser service was configured with MaintainServerList=No.";
3160 case NERR_RplNoAdaptersStarted:
3161 return "Service failed to start since none of the network adapters started with this service.";
3162 case NERR_RplBadRegistry:
3163 return "Service failed to start due to bad startup information in the registry.";
3164 case NERR_RplBadDatabase:
3165 return "Service failed to start because its database is absent or corrupt.";
3166 case NERR_RplRplfilesShare:
3167 return "Service failed to start because RPLFILES share is absent.";
3168 case NERR_RplNotRplServer:
3169 return "Service failed to start because RPLUSER group is absent.";
3170 case NERR_RplCannotEnum:
3171 return "Cannot enumerate service records.";
3172 case NERR_RplWkstaInfoCorrupted:
3173 return "Workstation record information has been corrupted.";
3174 case NERR_RplWkstaNotFound:
3175 return "Workstation record was not found.";
3176 case NERR_RplWkstaNameUnavailable:
3177 return "Workstation name is in use by some other workstation.";
3178 case NERR_RplProfileInfoCorrupted:
3179 return "Profile record information has been corrupted.";
3180 case NERR_RplProfileNotFound:
3181 return "Profile record was not found.";
3182 case NERR_RplProfileNameUnavailable:
3183 return "Profile name is in use by some other profile.";
3184 case NERR_RplProfileNotEmpty:
3185 return "There are workstations using this profile.";
3186 case NERR_RplConfigInfoCorrupted:
3187 return "Configuration record information has been corrupted.";
3188 case NERR_RplConfigNotFound:
3189 return "Configuration record was not found.";
3190 case NERR_RplAdapterInfoCorrupted:
3191 return "Adapter ID record information has been corrupted.";
3192 case NERR_RplInternal:
3193 return "An internal service error has occurred.";
3194 case NERR_RplVendorInfoCorrupted:
3195 return "Vendor ID record information has been corrupted.";
3196 case NERR_RplBootInfoCorrupted:
3197 return "Boot block record information has been corrupted.";
3198 case NERR_RplWkstaNeedsUserAcct:
3199 return "The user account for this workstation record is missing.";
3200 case NERR_RplNeedsRPLUSERAcct:
3201 return "The RPLUSER local group could not be found.";
3202 case NERR_RplBootNotFound:
3203 return "Boot block record was not found.";
3204 case NERR_RplIncompatibleProfile:
3205 return "Chosen profile is incompatible with this workstation.";
3206 case NERR_RplAdapterNameUnavailable:
3207 return "Chosen network adapter ID is in use by some other workstation.";
3208 case NERR_RplConfigNotEmpty:
3209 return "There are profiles using this configuration.";
3210 case NERR_RplBootInUse:
3211 return "There are workstations, profiles, or configurations using this boot block.";
3212 case NERR_RplBackupDatabase:
3213 return "Service failed to backup Remoteboot database.";
3214 case NERR_RplAdapterNotFound:
3215 return "Adapter record was not found.";
3216 case NERR_RplVendorNotFound:
3217 return "Vendor record was not found.";
3218 case NERR_RplVendorNameUnavailable:
3219 return "Vendor name is in use by some other vendor record.";
3220 case NERR_RplBootNameUnavailable:
3221 return "(boot name, vendor ID) is in use by some other boot block record.";
3222 case NERR_RplConfigNameUnavailable:
3223 return "Configuration name is in use by some other configuration.";
3224 case NERR_DfsInternalCorruption:
3225 return "The internal database maintained by the Dfs service is corrupt.";
3226 case NERR_DfsVolumeDataCorrupt:
3227 return "One of the records in the internal Dfs database is corrupt.";
3228 case NERR_DfsNoSuchVolume:
3229 return "There is no DFS name whose entry path matches the input Entry Path.";
3230 case NERR_DfsVolumeAlreadyExists:
3231 return "A root or link with the given name already exists.";
3232 case NERR_DfsAlreadyShared:
3233 return "The server share specified is already shared in the Dfs.";
3234 case NERR_DfsNoSuchShare:
3235 return "The indicated server share does not support the indicated DFS namespace.";
3236 case NERR_DfsNotALeafVolume:
3237 return "The operation is not valid on this portion of the namespace.";
3238 case NERR_DfsLeafVolume:
3239 return "The operation is not valid on this portion of the namespace.";
3240 case NERR_DfsVolumeHasMultipleServers:
3241 return "The operation is ambiguous because the link has multiple servers.";
3242 case NERR_DfsCantCreateJunctionPoint:
3243 return "Unable to create a link.";
3244 case NERR_DfsServerNotDfsAware:
3245 return "The server is not Dfs Aware.";
3246 case NERR_DfsBadRenamePath:
3247 return "The specified rename target path is invalid.";
3248 case NERR_DfsVolumeIsOffline:
3249 return "The specified DFS link is offline.";
3250 case NERR_DfsNoSuchServer:
3251 return "The specified server is not a server for this link.";
3252 case NERR_DfsCyclicalName:
3253 return "A cycle in the Dfs name was detected.";
3254 case NERR_DfsNotSupportedInServerDfs:
3255 return "The operation is not supported on a server-based Dfs.";
3256 case NERR_DfsDuplicateService:
3257 return "This link is already supported by the specified server-share.";
3258 case NERR_DfsCantRemoveLastServerShare:
3259 return "Can't remove the last server-share supporting this root or link.";
3260 case NERR_DfsVolumeIsInterDfs:
3261 return "The operation is not supported for an Inter-DFS link.";
3262 case NERR_DfsInconsistent:
3263 return "The internal state of the Dfs Service has become inconsistent.";
3264 case NERR_DfsServerUpgraded:
3265 return "The Dfs Service has been installed on the specified server.";
3266 case NERR_DfsDataIsIdentical:
3267 return "The Dfs data being reconciled is identical.";
3268 case NERR_DfsCantRemoveDfsRoot:
3269 return "The DFS root cannot be deleted. Uninstall DFS if required.";
3270 case NERR_DfsChildOrParentInDfs:
3271 return "A child or parent directory of the share is already in a Dfs.";
3272 case NERR_DfsInternalError:
3273 return "Dfs internal error.";
3274 /* the following are not defined in mingw */
3277 case NERR_SetupAlreadyJoined:
3278 return "This machine is already joined to a domain.";
3279 case NERR_SetupNotJoined:
3280 return "This machine is not currently joined to a domain.";
3281 case NERR_SetupDomainController:
3282 return "This machine is a domain controller and cannot be unjoined from a domain.";
3283 case NERR_DefaultJoinRequired:
3284 return "The destination domain controller does not support creating machine accounts in OUs.";
3285 case NERR_InvalidWorkgroupName:
3286 return "The specified workgroup name is invalid.";
3287 case NERR_NameUsesIncompatibleCodePage:
3288 return "The specified computer name is incompatible with the default language used on the domain controller.";
3289 case NERR_ComputerAccountNotFound:
3290 return "The specified computer account could not be found.";
3291 case NERR_PersonalSku:
3292 return "This version of Windows cannot be joined to a domain.";
3293 case NERR_PasswordMustChange:
3294 return "The password must change at the next logon.";
3295 case NERR_AccountLockedOut:
3296 return "The account is locked out.";
3297 case NERR_PasswordTooLong:
3298 return "The password is too long.";
3299 case NERR_PasswordNotComplexEnough:
3300 return "The password does not meet the complexity policy.";
3301 case NERR_PasswordFilterError:
3302 return "The password does not meet the requirements of the password filter DLLs.";
3306 msg = strerror (error_number);
3322 /******************************************************************************
3324 Original CVS version of dbus-sysdeps.c
3326 ******************************************************************************/
3327 /* -*- mode: C; c-file-style: "gnu" -*- */
3328 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-Bus implementation)
3330 * Copyright (C) 2002, 2003 Red Hat, Inc.
3331 * Copyright (C) 2003 CodeFactory AB
3332 * Copyright (C) 2005 Novell, Inc.
3334 * Licensed under the Academic Free License version 2.1
3336 * This program is free software; you can redistribute it and/or modify
3337 * it under the terms of the GNU General Public License as published by
3338 * the Free Software Foundation; either version 2 of the License, or
3339 * (at your option) any later version.
3341 * This program is distributed in the hope that it will be useful,
3342 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3343 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3344 * GNU General Public License for more details.
3346 * You should have received a copy of the GNU General Public License
3347 * along with this program; if not, write to the Free Software
3348 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3354 * @addtogroup DBusInternalsUtils
3358 int _dbus_mkdir (const char *path,
3361 return _mkdir(path);
3365 * Exit the process, returning the given value.
3367 * @param code the exit code
3370 _dbus_exit (int code)
3376 * Creates a socket and connects to a socket at the given host
3377 * and port. The connection fd is returned, and is set up as
3380 * @param host the host name to connect to, NULL for loopback
3381 * @param port the prot to connect to
3382 * @param error return location for error code
3383 * @returns connection file descriptor or -1 on error
3386 _dbus_connect_tcp_socket (const char *host,
3392 struct sockaddr_in addr;
3394 struct in_addr *haddr;
3397 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3399 _dbus_win_startup_winsock ();
3401 s.fd = socket (AF_INET, SOCK_STREAM, 0);
3403 if (DBUS_SOCKET_IS_INVALID (s.fd))
3405 DBUS_SOCKET_SET_ERRNO ();
3406 dbus_set_error (error,
3407 _dbus_error_from_errno (errno),
3408 "Failed to create socket: %s",
3409 _dbus_strerror (errno));
3417 ina.s_addr = htonl (INADDR_LOOPBACK);
3421 he = gethostbyname (host);
3424 DBUS_SOCKET_SET_ERRNO ();
3425 dbus_set_error (error,
3426 _dbus_error_from_errno (errno),
3427 "Failed to lookup hostname: %s",
3429 DBUS_CLOSE_SOCKET (s.fd);
3433 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3436 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
3437 addr.sin_family = AF_INET;
3438 addr.sin_port = htons (port);
3440 if (DBUS_SOCKET_API_RETURNS_ERROR
3441 (connect (s.fd, (struct sockaddr*) &addr, sizeof (addr)) < 0))
3443 DBUS_SOCKET_SET_ERRNO ();
3444 dbus_set_error (error,
3445 _dbus_error_from_errno (errno),
3446 "Failed to connect to socket %s:%d %s",
3447 host, port, _dbus_strerror (errno));
3449 DBUS_CLOSE_SOCKET (s.fd);
3455 handle = _dbus_socket_to_handle (&s);
3457 if (!_dbus_set_fd_nonblocking (handle, error))
3459 _dbus_close_socket (handle, NULL);
3469 _dbus_daemon_init(const char *host, dbus_uint32_t port);
3471 * Creates a socket and binds it to the given port,
3472 * then listens on the socket. The socket is
3473 * set to be nonblocking.
3474 * In case of port=0 a random free port is used and
3475 * returned in the port parameter.
3477 * @param host the interface to listen on, NULL for loopback, empty for any
3478 * @param port the port to listen on, if zero a free port will be used
3479 * @param error return location for errors
3480 * @returns the listening file descriptor or -1 on error
3484 _dbus_listen_tcp_socket (const char *host,
3485 dbus_uint32_t *port,
3490 struct sockaddr_in addr;
3492 struct in_addr *haddr;
3493 socklen_t len = (socklen_t) sizeof (struct sockaddr);
3497 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3499 _dbus_win_startup_winsock ();
3501 slisten.fd = socket (AF_INET, SOCK_STREAM, 0);
3503 if (DBUS_SOCKET_IS_INVALID (slisten.fd))
3505 DBUS_SOCKET_SET_ERRNO ();
3506 dbus_set_error (error, _dbus_error_from_errno (errno),
3507 "Failed to create socket \"%s:%d\": %s",
3508 host, port, _dbus_strerror (errno));
3514 ina.s_addr = htonl (INADDR_LOOPBACK);
3519 ina.s_addr = htonl (INADDR_ANY);
3524 he = gethostbyname (host);
3527 DBUS_SOCKET_SET_ERRNO ();
3528 dbus_set_error (error,
3529 _dbus_error_from_errno (errno),
3530 "Failed to lookup hostname: %s",
3532 DBUS_CLOSE_SOCKET (slisten.fd);
3536 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3540 memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
3541 addr.sin_family = AF_INET;
3542 addr.sin_port = htons (*port);
3544 if (bind (slisten.fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
3546 DBUS_SOCKET_SET_ERRNO ();
3547 dbus_set_error (error, _dbus_error_from_errno (errno),
3548 "Failed to bind socket \"%s:%d\": %s",
3549 host, *port, _dbus_strerror (errno));
3550 DBUS_CLOSE_SOCKET (slisten.fd);
3554 if (DBUS_SOCKET_API_RETURNS_ERROR (listen (slisten.fd, 30 /* backlog */)))
3556 DBUS_SOCKET_SET_ERRNO ();
3557 dbus_set_error (error, _dbus_error_from_errno (errno),
3558 "Failed to listen on socket \"%s:%d\": %s",
3559 host, *port, _dbus_strerror (errno));
3560 DBUS_CLOSE_SOCKET (slisten.fd);
3564 getsockname(slisten.fd, (struct sockaddr*) &addr, &len);
3565 *port = (dbus_uint32_t) ntohs(addr.sin_port);
3567 _dbus_daemon_init(host, ntohs(addr.sin_port));
3569 handle = _dbus_socket_to_handle (&slisten);
3571 if (!_dbus_set_fd_nonblocking (handle, error))
3573 _dbus_close_socket (handle, NULL);
3581 * Accepts a connection on a listening socket.
3582 * Handles EINTR for you.
3584 * @param listen_fd the listen file descriptor
3585 * @returns the connection fd of the client, or -1 on error
3588 _dbus_accept (int listen_handle)
3590 DBusSocket *slisten;
3592 struct sockaddr addr;
3595 _dbus_handle_to_socket(listen_handle, &slisten);
3597 addrlen = sizeof (addr);
3599 //FIXME: why do we not try it again on Windows?
3600 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3604 sclient.fd = accept (slisten->fd, &addr, &addrlen);
3606 if (DBUS_SOCKET_IS_INVALID (sclient.fd))
3608 DBUS_SOCKET_SET_ERRNO ();
3609 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3617 return _dbus_socket_to_handle (&sclient);
3623 write_credentials_byte (int handle,
3626 /* FIXME: for the session bus credentials shouldn't matter (?), but
3627 * for the system bus they are presumably essential. A rough outline
3628 * of a way to implement the credential transfer would be this:
3630 * client waits to *read* a byte.
3632 * server creates a named pipe with a random name, sends a byte
3633 * contining its length, and its name.
3635 * client reads the name, connects to it (using Win32 API).
3637 * server waits for connection to the named pipe, then calls
3638 * ImpersonateNamedPipeClient(), notes its now-current credentials,
3639 * calls RevertToSelf(), closes its handles to the named pipe, and
3640 * is done. (Maybe there is some other way to get the SID of a named
3641 * pipe client without having to use impersonation?)
3643 * client closes its handles and is done.
3645 * Ralf: Why not sending credentials over the given this connection ?
3646 * Using named pipes makes it impossible to be connected from a unix client.
3652 _dbus_string_init_const_len (&buf, "\0", 1);
3654 bytes_written = _dbus_write_socket (handle, &buf, 0, 1 );
3656 if (bytes_written < 0 && errno == EINTR)
3659 if (bytes_written < 0)
3661 dbus_set_error (error, _dbus_error_from_errno (errno),
3662 "Failed to write credentials byte: %s",
3663 _dbus_strerror (errno));
3666 else if (bytes_written == 0)
3668 dbus_set_error (error, DBUS_ERROR_IO_ERROR,
3669 "wrote zero bytes writing credentials byte");
3674 _dbus_assert (bytes_written == 1);
3675 _dbus_verbose ("wrote 1 zero byte, credential sending isn't implemented yet\n");
3682 * Reads a single byte which must be nul (an error occurs otherwise),
3683 * and reads unix credentials if available. Fills in pid/uid/gid with
3684 * -1 if no credentials are available. Return value indicates whether
3685 * a byte was read, not whether we got valid credentials. On some
3686 * systems, such as Linux, reading/writing the byte isn't actually
3687 * required, but we do it anyway just to avoid multiple codepaths.
3689 * Fails if no byte is available, so you must select() first.
3691 * The point of the byte is that on some systems we have to
3692 * use sendmsg()/recvmsg() to transmit credentials.
3694 * @param client_fd the client file descriptor
3695 * @param credentials struct to fill with credentials of client
3696 * @param error location to store error code
3697 * @returns #TRUE on success
3700 _dbus_read_credentials_unix_socket (int handle,
3701 DBusCredentials *credentials,
3706 _dbus_string_init(&buf);
3708 bytes_read = _dbus_read_socket(handle, &buf, 1 );
3711 _dbus_verbose("got one zero byte from server");
3714 _dbus_string_free(&buf);
3715 _dbus_credentials_from_current_process (credentials);
3716 _dbus_verbose("FIXME: get faked credentials from current process");
3722 * Checks to make sure the given directory is
3723 * private to the user
3725 * @param dir the name of the directory
3726 * @param error error return
3727 * @returns #FALSE on failure
3730 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
3732 const char *directory;
3735 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3742 * Gets user info for the given user ID.
3744 * @param info user info object to initialize
3745 * @param uid the user ID
3746 * @param error error return
3747 * @returns #TRUE on success
3750 _dbus_user_info_fill_uid (DBusUserInfo *info,
3754 return fill_user_info (info, uid,
3759 * Gets user info for the given username.
3761 * @param info user info object to initialize
3762 * @param username the username
3763 * @param error error return
3764 * @returns #TRUE on success
3767 _dbus_user_info_fill (DBusUserInfo *info,
3768 const DBusString *username,
3771 return fill_user_info (info, DBUS_UID_UNSET,
3777 fill_user_info (DBusUserInfo *info,
3779 const DBusString *username,
3782 const char *username_c;
3784 /* exactly one of username/uid provided */
3785 _dbus_assert (username != NULL || uid != DBUS_UID_UNSET);
3786 _dbus_assert (username == NULL || uid == DBUS_UID_UNSET);
3788 info->uid = DBUS_UID_UNSET;
3789 info->primary_gid = DBUS_GID_UNSET;
3790 info->group_ids = NULL;
3791 info->n_group_ids = 0;
3792 info->username = NULL;
3793 info->homedir = NULL;
3795 if (username != NULL)
3796 username_c = _dbus_string_get_const_data (username);
3800 if (uid != DBUS_UID_UNSET)
3802 if (!fill_win_user_info_from_uid (uid, info, error))
3804 _dbus_verbose("%s after fill_win_user_info_from_uid\n",__FUNCTION__);
3810 wchar_t *wname = _dbus_win_utf8_to_utf16 (username_c, error);
3815 if (!fill_win_user_info_from_name (wname, info, error))
3828 * Appends the given filename to the given directory.
3830 * @todo it might be cute to collapse multiple '/' such as "foo//"
3833 * @param dir the directory name
3834 * @param next_component the filename
3835 * @returns #TRUE on success
3838 _dbus_concat_dir_and_file (DBusString *dir,
3839 const DBusString *next_component)
3841 dbus_bool_t dir_ends_in_slash;
3842 dbus_bool_t file_starts_with_slash;
3844 if (_dbus_string_get_length (dir) == 0 ||
3845 _dbus_string_get_length (next_component) == 0)
3849 ('/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1) ||
3850 '\\' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1));
3852 file_starts_with_slash =
3853 ('/' == _dbus_string_get_byte (next_component, 0) ||
3854 '\\' == _dbus_string_get_byte (next_component, 0));
3856 if (dir_ends_in_slash && file_starts_with_slash)
3858 _dbus_string_shorten (dir, 1);
3860 else if (!(dir_ends_in_slash || file_starts_with_slash))
3862 if (!_dbus_string_append_byte (dir, '\\'))
3866 return _dbus_string_copy (next_component, 0, dir,
3867 _dbus_string_get_length (dir));
3874 * Gets our process ID
3875 * @returns process ID
3880 return GetCurrentProcessId ();
3883 /** nanoseconds in a second */
3884 #define NANOSECONDS_PER_SECOND 1000000000
3885 /** microseconds in a second */
3886 #define MICROSECONDS_PER_SECOND 1000000
3887 /** milliseconds in a second */
3888 #define MILLISECONDS_PER_SECOND 1000
3889 /** nanoseconds in a millisecond */
3890 #define NANOSECONDS_PER_MILLISECOND 1000000
3891 /** microseconds in a millisecond */
3892 #define MICROSECONDS_PER_MILLISECOND 1000
3895 * Sleeps the given number of milliseconds.
3896 * @param milliseconds number of milliseconds
3899 _dbus_sleep_milliseconds (int milliseconds)
3901 Sleep (milliseconds);
3906 * Get current time, as in gettimeofday().
3908 * @param tv_sec return location for number of seconds
3909 * @param tv_usec return location for number of microseconds
3912 _dbus_get_current_time (long *tv_sec,
3916 dbus_uint64_t *time64 = (dbus_uint64_t *) &ft;
3918 GetSystemTimeAsFileTime (&ft);
3920 /* Convert from 100s of nanoseconds since 1601-01-01
3921 * to Unix epoch. Yes, this is Y2038 unsafe.
3923 *time64 -= DBUS_INT64_CONSTANT (116444736000000000);
3927 *tv_sec = *time64 / 1000000;
3930 *tv_usec = *time64 % 1000000;
3935 * signal (SIGPIPE, SIG_IGN);
3938 _dbus_disable_sigpipe (void)
3940 _dbus_verbose("FIXME: implement _dbus_disable_sigpipe (void)\n");
3944 * Gets the credentials of the current process.
3946 * @param credentials credentials to fill in.
3949 _dbus_credentials_from_current_process (DBusCredentials *credentials)
3951 credentials->pid = _dbus_getpid ();
3952 credentials->uid = _dbus_getuid ();
3953 credentials->gid = _dbus_getgid ();
3957 * Appends the contents of the given file to the string,
3958 * returning error code. At the moment, won't open a file
3959 * more than a megabyte in size.
3961 * @param str the string to append to
3962 * @param filename filename to load
3963 * @param error place to set an error
3964 * @returns #FALSE if error was set
3967 _dbus_file_get_contents (DBusString *str,
3968 const DBusString *filename,
3975 const char *filename_c;
3977 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3979 filename_c = _dbus_string_get_const_data (filename);
3981 /* O_BINARY useful on Cygwin and Win32 */
3982 if (!_dbus_file_open (&file, filename_c, O_RDONLY | O_BINARY, -1))
3984 dbus_set_error (error, _dbus_error_from_errno (errno),
3985 "Failed to open \"%s\": %s",
3987 _dbus_strerror (errno));
3991 if (!_dbus_fstat (&file, &sb))
3993 dbus_set_error (error, _dbus_error_from_errno (errno),
3994 "Failed to stat \"%s\": %s",
3996 _dbus_strerror (errno));
3998 _dbus_verbose ("fstat() failed: %s",
3999 _dbus_strerror (errno));
4001 _dbus_file_close (&file, NULL);
4006 if (sb.st_size > _DBUS_ONE_MEGABYTE)
4008 dbus_set_error (error, DBUS_ERROR_FAILED,
4009 "File size %lu of \"%s\" is too large.",
4010 (unsigned long) sb.st_size, filename_c);
4011 _dbus_file_close (&file, NULL);
4016 orig_len = _dbus_string_get_length (str);
4017 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
4021 while (total < (int) sb.st_size)
4023 bytes_read = _dbus_file_read (&file, str,
4024 sb.st_size - total);
4025 if (bytes_read <= 0)
4027 dbus_set_error (error, _dbus_error_from_errno (errno),
4028 "Error reading \"%s\": %s",
4030 _dbus_strerror (errno));
4032 _dbus_verbose ("read() failed: %s",
4033 _dbus_strerror (errno));
4035 _dbus_file_close (&file, NULL);
4036 _dbus_string_set_length (str, orig_len);
4040 total += bytes_read;
4043 _dbus_file_close (&file, NULL);
4046 else if (sb.st_size != 0)
4048 _dbus_verbose ("Can only open regular files at the moment.\n");
4049 dbus_set_error (error, DBUS_ERROR_FAILED,
4050 "\"%s\" is not a regular file",
4052 _dbus_file_close (&file, NULL);
4057 _dbus_file_close (&file, NULL);
4063 * Writes a string out to a file. If the file exists,
4064 * it will be atomically overwritten by the new data.
4066 * @param str the string to write out
4067 * @param filename the file to save string to
4068 * @param error error to be filled in on failure
4069 * @returns #FALSE on failure
4072 _dbus_string_save_to_file (const DBusString *str,
4073 const DBusString *filename,
4078 const char *filename_c;
4079 DBusString tmp_filename;
4080 const char *tmp_filename_c;
4082 dbus_bool_t need_unlink;
4085 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4088 need_unlink = FALSE;
4090 if (!_dbus_string_init (&tmp_filename))
4092 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4096 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
4098 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4099 _dbus_string_free (&tmp_filename);
4103 if (!_dbus_string_append (&tmp_filename, "."))
4105 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4106 _dbus_string_free (&tmp_filename);
4110 #define N_TMP_FILENAME_RANDOM_BYTES 8
4111 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
4113 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4114 _dbus_string_free (&tmp_filename);
4118 filename_c = _dbus_string_get_const_data (filename);
4119 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
4121 if (!_dbus_file_open (&file, tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4124 dbus_set_error (error, _dbus_error_from_errno (errno),
4125 "Could not create %s: %s", tmp_filename_c,
4126 _dbus_strerror (errno));
4133 bytes_to_write = _dbus_string_get_length (str);
4135 while (total < bytes_to_write)
4139 bytes_written = _dbus_file_write (&file, str, total,
4140 bytes_to_write - total);
4142 if (bytes_written <= 0)
4144 dbus_set_error (error, _dbus_error_from_errno (errno),
4145 "Could not write to %s: %s", tmp_filename_c,
4146 _dbus_strerror (errno));
4151 total += bytes_written;
4154 if (!_dbus_file_close (&file, NULL))
4156 dbus_set_error (error, _dbus_error_from_errno (errno),
4157 "Could not close file %s: %s",
4158 tmp_filename_c, _dbus_strerror (errno));
4164 if ((unlink (filename_c) == -1 && errno != ENOENT) ||
4165 rename (tmp_filename_c, filename_c) < 0)
4167 dbus_set_error (error, _dbus_error_from_errno (errno),
4168 "Could not rename %s to %s: %s",
4169 tmp_filename_c, filename_c,
4170 _dbus_strerror (errno));
4175 need_unlink = FALSE;
4180 /* close first, then unlink, to prevent ".nfs34234235" garbage
4184 if (_dbus_is_valid_file(&file))
4185 _dbus_file_close (&file, NULL);
4187 if (need_unlink && unlink (tmp_filename_c) < 0)
4188 _dbus_verbose ("Failed to unlink temp file %s: %s\n",
4189 tmp_filename_c, _dbus_strerror (errno));
4191 _dbus_string_free (&tmp_filename);
4194 _DBUS_ASSERT_ERROR_IS_SET (error);
4200 /** Creates the given file, failing if the file already exists.
4202 * @param filename the filename
4203 * @param error error location
4204 * @returns #TRUE if we created the file and it didn't exist
4207 _dbus_create_file_exclusively (const DBusString *filename,
4211 const char *filename_c;
4213 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4215 filename_c = _dbus_string_get_const_data (filename);
4217 if (!_dbus_file_open (&file, filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4220 dbus_set_error (error,
4222 "Could not create file %s: %s\n",
4224 _dbus_strerror (errno));
4228 if (!_dbus_file_close (&file, NULL))
4230 dbus_set_error (error,
4232 "Could not close file %s: %s\n",
4234 _dbus_strerror (errno));
4243 * Creates a directory; succeeds if the directory
4244 * is created or already existed.
4246 * @param filename directory filename
4247 * @param error initialized error object
4248 * @returns #TRUE on success
4251 _dbus_create_directory (const DBusString *filename,
4254 const char *filename_c;
4256 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4258 filename_c = _dbus_string_get_const_data (filename);
4260 if (_dbus_mkdir (filename_c, 0700) < 0)
4262 if (errno == EEXIST)
4265 dbus_set_error (error, DBUS_ERROR_FAILED,
4266 "Failed to create directory %s: %s\n",
4267 filename_c, _dbus_strerror (errno));
4276 pseudorandom_generate_random_bytes_buffer (char *buffer,
4282 /* fall back to pseudorandom */
4283 _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
4286 _dbus_get_current_time (NULL, &tv_usec);
4296 b = (r / (double) RAND_MAX) * 255.0;
4305 pseudorandom_generate_random_bytes (DBusString *str,
4311 old_len = _dbus_string_get_length (str);
4313 if (!_dbus_string_lengthen (str, n_bytes))
4316 p = _dbus_string_get_data_len (str, old_len, n_bytes);
4318 pseudorandom_generate_random_bytes_buffer (p, n_bytes);
4324 * Gets the temporary files directory by inspecting the environment variables
4325 * TMPDIR, TMP, and TEMP in that order. If none of those are set "/tmp" is returned
4327 * @returns location of temp directory
4330 _dbus_get_tmpdir(void)
4332 static const char* tmpdir = NULL;
4337 tmpdir = getenv("TMP");
4339 tmpdir = getenv("TEMP");
4341 tmpdir = getenv("TMPDIR");
4343 tmpdir = "C:\\Temp";
4346 _dbus_assert(tmpdir != NULL);
4353 * Deletes the given file.
4355 * @param filename the filename
4356 * @param error error location
4358 * @returns #TRUE if unlink() succeeded
4361 _dbus_delete_file (const DBusString *filename,
4364 const char *filename_c;
4366 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4368 filename_c = _dbus_string_get_const_data (filename);
4370 if (unlink (filename_c) < 0)
4372 dbus_set_error (error, DBUS_ERROR_FAILED,
4373 "Failed to delete file %s: %s\n",
4374 filename_c, _dbus_strerror (errno));
4382 * Generates the given number of random bytes,
4383 * using the best mechanism we can come up with.
4385 * @param str the string
4386 * @param n_bytes the number of random bytes to append to string
4387 * @returns #TRUE on success, #FALSE if no memory
4390 _dbus_generate_random_bytes (DBusString *str,
4393 return pseudorandom_generate_random_bytes (str, n_bytes);
4396 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_BUILD_TESTS)
4408 * Backtrace Generator
4410 * Copyright 2004 Eric Poech
4411 * Copyright 2004 Robert Shearman
4413 * This library is free software; you can redistribute it and/or
4414 * modify it under the terms of the GNU Lesser General Public
4415 * License as published by the Free Software Foundation; either
4416 * version 2.1 of the License, or (at your option) any later version.
4418 * This library is distributed in the hope that it will be useful,
4419 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4420 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4421 * Lesser General Public License for more details.
4423 * You should have received a copy of the GNU Lesser General Public
4424 * License along with this library; if not, write to the Free Software
4425 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4429 #include <imagehlp.h>
4432 #define DPRINTF _dbus_warn
4440 //#define MAKE_FUNCPTR(f) static typeof(f) * p##f
4442 //MAKE_FUNCPTR(StackWalk);
4443 //MAKE_FUNCPTR(SymGetModuleBase);
4444 //MAKE_FUNCPTR(SymFunctionTableAccess);
4445 //MAKE_FUNCPTR(SymInitialize);
4446 //MAKE_FUNCPTR(SymGetSymFromAddr);
4447 //MAKE_FUNCPTR(SymGetModuleInfo);
4448 static BOOL (WINAPI *pStackWalk)(
4452 LPSTACKFRAME StackFrame,
4453 PVOID ContextRecord,
4454 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4455 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4456 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4457 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4459 static DWORD (WINAPI *pSymGetModuleBase)(
4463 static PVOID (WINAPI *pSymFunctionTableAccess)(
4467 static BOOL (WINAPI *pSymInitialize)(
4469 PSTR UserSearchPath,
4472 static BOOL (WINAPI *pSymGetSymFromAddr)(
4475 PDWORD Displacement,
4476 PIMAGEHLP_SYMBOL Symbol
4478 static BOOL (WINAPI *pSymGetModuleInfo)(
4481 PIMAGEHLP_MODULE ModuleInfo
4483 static DWORD (WINAPI *pSymSetOptions)(
4488 static BOOL init_backtrace()
4490 HMODULE hmodDbgHelp = LoadLibraryA("dbghelp");
4492 #define GETFUNC(x) \
4493 p##x = (typeof(x)*)GetProcAddress(hmodDbgHelp, #x); \
4501 // GETFUNC(StackWalk);
4502 // GETFUNC(SymGetModuleBase);
4503 // GETFUNC(SymFunctionTableAccess);
4504 // GETFUNC(SymInitialize);
4505 // GETFUNC(SymGetSymFromAddr);
4506 // GETFUNC(SymGetModuleInfo);
4510 pStackWalk = (BOOL (WINAPI *)(
4514 LPSTACKFRAME StackFrame,
4515 PVOID ContextRecord,
4516 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4517 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4518 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4519 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4520 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
4521 pSymGetModuleBase=(DWORD (WINAPI *)(
4524 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
4525 pSymFunctionTableAccess=(PVOID (WINAPI *)(
4528 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
4529 pSymInitialize = (BOOL (WINAPI *)(
4531 PSTR UserSearchPath,
4533 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
4534 pSymGetSymFromAddr = (BOOL (WINAPI *)(
4537 PDWORD Displacement,
4538 PIMAGEHLP_SYMBOL Symbol
4539 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
4540 pSymGetModuleInfo = (BOOL (WINAPI *)(
4543 PIMAGEHLP_MODULE ModuleInfo
4544 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
4545 pSymSetOptions = (DWORD (WINAPI *)(
4547 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
4550 pSymSetOptions(SYMOPT_UNDNAME);
4552 pSymInitialize(GetCurrentProcess(), NULL, TRUE);
4557 static void dump_backtrace_for_thread(HANDLE hThread)
4564 if (!init_backtrace())
4567 /* can't use this function for current thread as GetThreadContext
4568 * doesn't support getting context from current thread */
4569 if (hThread == GetCurrentThread())
4572 DPRINTF("Backtrace:\n");
4574 memset(&context, 0, sizeof(context));
4575 context.ContextFlags = CONTEXT_FULL;
4577 SuspendThread(hThread);
4579 if (!GetThreadContext(hThread, &context))
4581 DPRINTF("Couldn't get thread context (error %ld)\n", GetLastError());
4582 ResumeThread(hThread);
4586 memset(&sf, 0, sizeof(sf));
4589 sf.AddrFrame.Offset = context.Ebp;
4590 sf.AddrFrame.Mode = AddrModeFlat;
4591 sf.AddrPC.Offset = context.Eip;
4592 sf.AddrPC.Mode = AddrModeFlat;
4593 dwImageType = IMAGE_FILE_MACHINE_I386;
4595 # error You need to fill in the STACKFRAME structure for your architecture
4598 while (pStackWalk(dwImageType, GetCurrentProcess(),
4599 hThread, &sf, &context, NULL, pSymFunctionTableAccess,
4600 pSymGetModuleBase, NULL))
4603 IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
4604 DWORD dwDisplacement;
4606 pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
4607 pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1;
4609 if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
4610 &dwDisplacement, pSymbol))
4612 IMAGEHLP_MODULE ModuleInfo;
4613 ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
4615 if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
4617 DPRINTF("1\t%p\n", (void*)sf.AddrPC.Offset);
4619 DPRINTF("2\t%s+0x%lx\n", ModuleInfo.ImageName,
4620 sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
4622 else if (dwDisplacement)
4623 DPRINTF("3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
4625 DPRINTF("4\t%s\n", pSymbol->Name);
4628 ResumeThread(hThread);
4631 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
4633 dump_backtrace_for_thread((HANDLE)lpParameter);
4637 /* cannot get valid context from current thread, so we have to execute
4638 * backtrace from another thread */
4639 static void dump_backtrace()
4641 HANDLE hCurrentThread;
4644 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
4645 GetCurrentProcess(), &hCurrentThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
4646 hThread = CreateThread(NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
4648 WaitForSingleObject(hThread, INFINITE);
4649 CloseHandle(hThread);
4650 CloseHandle(hCurrentThread);
4653 void _dbus_print_backtrace(void)
4659 void _dbus_print_backtrace(void)
4661 _dbus_verbose (" D-Bus not compiled with backtrace support\n");
4666 * Sends a single nul byte with our UNIX credentials as ancillary
4667 * data. Returns #TRUE if the data was successfully written. On
4668 * systems that don't support sending credentials, just writes a byte,
4669 * doesn't send any credentials. On some systems, such as Linux,
4670 * reading/writing the byte isn't actually required, but we do it
4671 * anyway just to avoid multiple codepaths.
4673 * Fails if no byte can be written, so you must select() first.
4675 * The point of the byte is that on some systems we have to
4676 * use sendmsg()/recvmsg() to transmit credentials.
4678 * @param server_fd file descriptor for connection to server
4679 * @param error return location for error code
4680 * @returns #TRUE if the byte was sent
4683 _dbus_send_credentials_unix_socket (int server_fd,
4686 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4688 if (write_credentials_byte (server_fd, error))
4694 static dbus_uint32_t fromAscii(char ascii)
4696 if(ascii >= '0' && ascii <= '9')
4698 if(ascii >= 'A' && ascii <= 'F')
4699 return ascii - 'A' + 10;
4700 if(ascii >= 'a' && ascii <= 'f')
4701 return ascii - 'a' + 10;
4705 dbus_bool_t _dbus_read_local_machine_uuid (DBusGUID *machine_id,
4706 dbus_bool_t create_if_not_found,
4713 HW_PROFILE_INFOA info;
4714 char *lpc = &info.szHwProfileGuid[0];
4717 // the hw-profile guid lives long enough
4718 if(!GetCurrentHwProfileA(&info))
4720 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); // FIXME
4724 // Form: {12340001-4980-1920-6788-123456789012}
4727 u = ((fromAscii(lpc[0]) << 0) |
4728 (fromAscii(lpc[1]) << 4) |
4729 (fromAscii(lpc[2]) << 8) |
4730 (fromAscii(lpc[3]) << 12) |
4731 (fromAscii(lpc[4]) << 16) |
4732 (fromAscii(lpc[5]) << 20) |
4733 (fromAscii(lpc[6]) << 24) |
4734 (fromAscii(lpc[7]) << 28));
4735 machine_id->as_uint32s[0] = u;
4739 u = ((fromAscii(lpc[0]) << 0) |
4740 (fromAscii(lpc[1]) << 4) |
4741 (fromAscii(lpc[2]) << 8) |
4742 (fromAscii(lpc[3]) << 12) |
4743 (fromAscii(lpc[5]) << 16) |
4744 (fromAscii(lpc[6]) << 20) |
4745 (fromAscii(lpc[7]) << 24) |
4746 (fromAscii(lpc[8]) << 28));
4747 machine_id->as_uint32s[1] = u;
4751 u = ((fromAscii(lpc[0]) << 0) |
4752 (fromAscii(lpc[1]) << 4) |
4753 (fromAscii(lpc[2]) << 8) |
4754 (fromAscii(lpc[3]) << 12) |
4755 (fromAscii(lpc[5]) << 16) |
4756 (fromAscii(lpc[6]) << 20) |
4757 (fromAscii(lpc[7]) << 24) |
4758 (fromAscii(lpc[8]) << 28));
4759 machine_id->as_uint32s[2] = u;
4763 u = ((fromAscii(lpc[0]) << 0) |
4764 (fromAscii(lpc[1]) << 4) |
4765 (fromAscii(lpc[2]) << 8) |
4766 (fromAscii(lpc[3]) << 12) |
4767 (fromAscii(lpc[4]) << 16) |
4768 (fromAscii(lpc[5]) << 20) |
4769 (fromAscii(lpc[6]) << 24) |
4770 (fromAscii(lpc[7]) << 28));
4771 machine_id->as_uint32s[3] = u;
4777 HANDLE _dbus_global_lock (const char *mutexname)
4782 mutex = CreateMutex( NULL, FALSE, mutexname );
4788 gotMutex = WaitForSingleObject( mutex, INFINITE );
4791 case WAIT_ABANDONED:
4792 ReleaseMutex (mutex);
4793 CloseHandle (mutex);
4804 void _dbus_global_unlock (HANDLE mutex)
4806 ReleaseMutex (mutex);
4807 CloseHandle (mutex);
4810 // for proper cleanup in dbus-daemon
4811 static HANDLE hDBusDaemonMutex = NULL;
4812 static HANDLE hDBusSharedMem = NULL;
4813 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4814 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
4815 // sync _dbus_get_autolaunch_address
4816 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
4817 // mutex to determine if dbus-daemon is already started (per user)
4818 static const char *cDBusDaemonMutex = "DBusDaemonMutex";
4819 // named shm for dbus adress info (per user)
4820 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
4823 _dbus_daemon_init(const char *host, dbus_uint32_t port)
4826 const char *adr = NULL;
4827 char szUserName[64];
4828 DWORD dwUserNameSize = sizeof(szUserName);
4829 char szDBusDaemonMutex[128];
4830 char szDBusDaemonAddressInfo[128];
4831 char szAddress[128];
4836 _snprintf(szAddress, sizeof(szAddress) - 1, "tcp:host=%s,port=%d", host, port);
4838 _dbus_assert( GetUserName(szUserName, &dwUserNameSize) != 0);
4839 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4840 cDBusDaemonMutex, szUserName);
4841 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4842 cDBusDaemonAddressInfo, szUserName);
4844 // before _dbus_global_lock to keep correct lock/release order
4845 hDBusDaemonMutex = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4847 _dbus_assert(WaitForSingleObject( hDBusDaemonMutex, 1000 ) == WAIT_OBJECT_0);
4849 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4850 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4853 hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
4854 0, strlen( szAddress ) + 1, szDBusDaemonAddressInfo );
4855 _dbus_assert( hDBusSharedMem );
4857 adr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
4859 _dbus_assert( adr );
4861 strcpy(adr, szAddress);
4864 UnmapViewOfFile( adr );
4866 _dbus_global_unlock( lock );
4870 _dbus_daemon_release()
4874 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4875 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4877 CloseHandle( hDBusSharedMem );
4879 hDBusSharedMem = NULL;
4881 ReleaseMutex( hDBusDaemonMutex );
4883 CloseHandle( hDBusDaemonMutex );
4885 hDBusDaemonMutex = NULL;
4887 _dbus_global_unlock( lock );
4891 _dbus_get_autolaunch_shm(DBusString *adress)
4895 char szUserName[64];
4896 DWORD dwUserNameSize = sizeof(szUserName);
4897 char szDBusDaemonAddressInfo[128];
4899 if( !GetUserName(szUserName, &dwUserNameSize) )
4901 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4902 cDBusDaemonAddressInfo, szUserName);
4906 // we know that dbus-daemon is available, so we wait until shm is available
4907 sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, szDBusDaemonAddressInfo );
4908 if( sharedMem == 0 )
4910 } while( sharedMem == 0 );
4912 if( sharedMem == 0 )
4915 adr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
4920 _dbus_string_init( adress );
4922 _dbus_string_append( adress, adr );
4925 UnmapViewOfFile( adr );
4927 CloseHandle( sharedMem );
4933 _dbus_daemon_already_runs (DBusString *adress)
4937 dbus_bool_t bRet = TRUE;
4938 char szUserName[64];
4939 DWORD dwUserNameSize = sizeof(szUserName);
4940 char szDBusDaemonMutex[128];
4942 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4943 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4945 if( !GetUserName(szUserName, &dwUserNameSize) )
4947 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4948 cDBusDaemonMutex, szUserName);
4951 daemon = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4952 if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
4954 ReleaseMutex (daemon);
4955 CloseHandle (daemon);
4957 _dbus_global_unlock( lock );
4962 bRet = _dbus_get_autolaunch_shm( adress );
4965 CloseHandle ( daemon );
4967 _dbus_global_unlock( lock );
4973 _dbus_get_autolaunch_address (DBusString *address,
4978 PROCESS_INFORMATION pi;
4979 dbus_bool_t retval = FALSE;
4981 char dbus_exe_path[MAX_PATH];
4982 char dbus_args[MAX_PATH * 2];
4984 mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
4986 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4988 if (_dbus_daemon_already_runs(address))
4990 printf("dbus daemon already exists\n");
4995 if (!SearchPathA(NULL, "dbus-daemon.exe", NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
4997 printf ("could not find dbus-daemon executable\n");
5002 ZeroMemory( &si, sizeof(si) );
5004 ZeroMemory( &pi, sizeof(pi) );
5006 _snprintf(dbus_args, sizeof(dbus_args) - 1, "\"%s\" %s", dbus_exe_path, " --session");
5008 // argv[i] = "--config-file=bus\\session.conf";
5009 printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
5010 if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
5014 // Wait until started (see _dbus_get_autolaunch_shm())
5015 WaitForInputIdle(pi.hProcess, INFINITE);
5017 retval = _dbus_get_autolaunch_shm( address );
5024 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
5026 _DBUS_ASSERT_ERROR_IS_SET (error);
5028 _dbus_global_unlock (mutex);
5034 /** Makes the file readable by every user in the system.
5036 * @param filename the filename
5037 * @param error error location
5038 * @returns #TRUE if the file's permissions could be changed.
5041 _dbus_make_file_world_readable(const DBusString *filename,
5049 #define DBUS_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
5053 * Returns the standard directories for a session bus to look for service
5056 * On Windows this should be data directories:
5058 * %CommonProgramFiles%/dbus
5064 * @param dirs the directory list we are returning
5065 * @returns #FALSE on OOM
5069 _dbus_get_standard_session_servicedirs (DBusList **dirs)
5071 const char *common_progs;
5072 DBusString servicedir_path;
5074 if (!_dbus_string_init (&servicedir_path))
5077 if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR _DBUS_PATH_SEPARATOR))
5080 common_progs = _dbus_getenv ("CommonProgramFiles");
5082 if (common_progs != NULL)
5084 if (!_dbus_string_append (&servicedir_path, common_progs))
5087 if (!_dbus_string_append (&servicedir_path, _DBUS_PATH_SEPARATOR))
5091 if (!_dbus_split_paths_and_append (&servicedir_path,
5092 DBUS_STANDARD_SESSION_SERVICEDIR,
5096 _dbus_string_free (&servicedir_path);
5100 _dbus_string_free (&servicedir_path);
5104 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
5107 * Atomically increments an integer
5109 * @param atomic pointer to the integer to increment
5110 * @returns the value before incrementing
5114 _dbus_atomic_inc (DBusAtomic *atomic)
5116 // +/- 1 is needed here!
5117 return InterlockedIncrement (&atomic->value) - 1;
5121 * Atomically decrement an integer
5123 * @param atomic pointer to the integer to decrement
5124 * @returns the value before decrementing
5128 _dbus_atomic_dec (DBusAtomic *atomic)
5130 // +/- 1 is needed here!
5131 return InterlockedDecrement (&atomic->value) + 1;
5134 #endif /* asserts or tests enabled */
5136 /** @} end of sysdeps-win */
5138 /* tests in dbus-sysdeps-util.c */