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_open_file (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_close_file (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_read_file(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_write_file (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 * init a pipe instance.
272 * @param fd the file descriptor to init from
273 * @returns a DBusPipe instance
275 DBusPipe _dbus_pipe_init(int fd)
283 * write data to a pipe.
285 * @param pipe the pipe instance
286 * @param buffer the buffer to write data from
287 * @param start the first byte in the buffer to write
288 * @param len the number of bytes to try to write
289 * @returns the number of bytes written or -1 on error
292 _dbus_pipe_write (DBusPipe pipe,
293 const DBusString *buffer,
298 file.FDATA = pipe.fd;
299 return _dbus_write_file(&file, buffer, start, len);
303 * read data from a pipe.
305 * @param pipe the pipe instance
306 * @param buffer the buffer to read data in
307 * @param count the number of bytes to try to read
308 * @returns the number of bytes read or -1 on error
311 _dbus_pipe_read(DBusPipe pipe,
316 file.FDATA = pipe.fd;
317 return _dbus_read_file(&file, buffer, count);
323 * @param pipe the pipe instance
324 * @param error return location for an error
325 * @returns #FALSE if error is set
328 _dbus_pipe_close(DBusPipe pipe,
332 file.FDATA = pipe.fd;
333 return _dbus_close_file(&file, error);
337 * check if a pipe is valid, which means is constructed
338 * by a valid file descriptor
340 * @param pipe the pipe instance
341 * @returns #FALSE if pipe is not valid
343 dbus_bool_t _dbus_pipe_is_valid(DBusPipe pipe)
349 * check if a pipe is a special pipe, which means using
350 * a non default file descriptor (>2)
352 * @param pipe the pipe instance
353 * @returns #FALSE if pipe is not a special pipe
355 dbus_bool_t _dbus_pipe_is_special(DBusPipe pipe)
367 static DBusSocket *win_fds = NULL;
368 static int win_n_fds = 0; // is this the size? rename to win_fds_size? #
371 #define TO_HANDLE(n) ((n)^win32_encap_randomizer)
372 #define FROM_HANDLE(n) ((n)^win32_encap_randomizer)
374 #define TO_HANDLE(n) ((n)+0x10000000)
375 #define FROM_HANDLE(n) ((n)-0x10000000)
376 #define IS_HANDLE(n) ((n)&0x10000000)
382 _dbus_win_deallocate_fd (int fd)
384 _DBUS_LOCK (win_fds);
385 win_fds[FROM_HANDLE (fd)].is_used = 0;
386 _DBUS_UNLOCK (win_fds);
391 _dbus_win_allocate_fd (void)
395 _DBUS_LOCK (win_fds);
399 #ifdef _DBUS_WIN_USE_RANDOMIZER
404 /* Use malloc to avoid memory leak failure in dbus-test */
405 win_fds = malloc (win_n_fds * sizeof (*win_fds));
407 _dbus_assert (win_fds != NULL);
409 for (i = 0; i < win_n_fds; i++)
410 win_fds[i].is_used = 0;
412 #ifdef _DBUS_WIN_USE_RANDOMIZER
414 _dbus_string_init (&random);
415 _dbus_generate_random_bytes (&random, sizeof (int));
416 memmove (&win_encap_randomizer, _dbus_string_get_const_data (&random), sizeof (int));
417 win_encap_randomizer &= 0xFF;
418 _dbus_string_free (&random);
423 for (i = 0; i < win_n_fds && win_fds[i].is_used != 0; i++)
428 int oldn = win_n_fds;
432 win_fds = realloc (win_fds, win_n_fds * sizeof (*win_fds));
434 _dbus_assert (win_fds != NULL);
436 for (j = oldn; j < win_n_fds; j++)
437 win_fds[i].is_used = 0;
440 memset(&win_fds[i], 0, sizeof(win_fds[i]));
442 win_fds[i].is_used = 1;
444 win_fds[i].port_file_fd = -1;
445 win_fds[i].close_on_exec = FALSE;
446 win_fds[i].non_blocking = FALSE;
448 _DBUS_UNLOCK (win_fds);
455 _dbus_create_handle_from_socket (int s)
460 // check: parameter must be a valid value
461 _dbus_assert(s != -1);
462 _dbus_assert(!IS_HANDLE(s));
464 // get index of a new position in the map
465 i = _dbus_win_allocate_fd ();
467 // fill new posiiton in the map: value->index
469 win_fds[i].is_used = 1;
471 // create handle from the index: index->handle
472 handle = TO_HANDLE (i);
474 _dbus_verbose ("_dbus_create_handle_from_value, value: %d, handle: %d\n", s, handle);
480 _dbus_socket_to_handle (DBusSocket *s)
485 // check: parameter must be a valid socket
486 _dbus_assert(s != NULL);
487 _dbus_assert(s->fd != -1);
488 _dbus_assert(!IS_HANDLE(s->fd));
490 _DBUS_LOCK (win_fds);
492 // at the first call there is no win_fds
493 // will be constructed _dbus_create_handle_from_socket
494 // because handle = -1
497 // search for the value in the map
498 // find the index of the value: value->index
499 for (i = 0; i < win_n_fds; i++)
500 if (win_fds[i].is_used == 1 && win_fds[i].fd == s->fd)
502 // create handle from the index: index->handle
503 handle = TO_HANDLE (i);
507 _DBUS_UNLOCK (win_fds);
512 handle = _dbus_create_handle_from_socket(s->fd);
515 _dbus_assert(handle != -1);
522 _dbus_handle_to_socket_unlocked (int handle,
527 // check: parameter must be a valid handle
528 _dbus_assert(handle != -1);
529 _dbus_assert(IS_HANDLE(handle));
530 _dbus_assert(ptr != NULL);
532 // map from handle to index: handle->index
533 i = FROM_HANDLE (handle);
535 _dbus_assert (win_fds != NULL);
536 _dbus_assert (i >= 0 && i < win_n_fds);
538 // check for if fd is valid
539 _dbus_assert (win_fds[i].is_used == 1);
541 // get socket from index: index->socket
544 _dbus_verbose ("_dbus_socket_to_handle_unlocked: socket=%d, handle=%d, index=%d\n", (*ptr)->fd, handle, i);
548 _dbus_handle_to_socket (int handle,
551 _dbus_lock_sockets();
552 _dbus_handle_to_socket_unlocked (handle, ptr);
553 _dbus_unlock_sockets();
559 #define FROM_HANDLE(n) 1==DBUS_WIN_DONT_USE__FROM_HANDLE__DIRECTLY
560 #define win_fds 1==DBUS_WIN_DONT_USE_win_fds_DIRECTLY
566 * Thin wrapper around the read() system call that appends
567 * the data it reads to the DBusString buffer. It appends
568 * up to the given count, and returns the same value
569 * and same errno as read(). The only exception is that
570 * _dbus_read() handles EINTR for you. _dbus_read() can
571 * return ENOMEM, even though regular UNIX read doesn't.
573 * @param fd the file descriptor to read from
574 * @param buffer the buffer to append data to
575 * @param count the amount of data to read
576 * @returns the number of bytes read or -1
579 _dbus_read_socket (int handle,
588 _dbus_assert (count >= 0);
590 start = _dbus_string_get_length (buffer);
592 if (!_dbus_string_lengthen (buffer, count))
598 data = _dbus_string_get_data_len (buffer, start, count);
600 _dbus_handle_to_socket(handle, &s);
604 _dbus_verbose ("recv: count=%d socket=%d\n", count, s->fd);
605 bytes_read = recv (s->fd, data, count, 0);
606 if (bytes_read == SOCKET_ERROR)
608 DBUS_SOCKET_SET_ERRNO();
609 _dbus_verbose ("recv: failed: %s\n", _dbus_strerror (errno));
613 _dbus_verbose ("recv: = %d\n", bytes_read);
617 _dbus_assert_not_reached ("no valid socket");
622 /* put length back (note that this doesn't actually realloc anything) */
623 _dbus_string_set_length (buffer, start);
628 /* put length back (doesn't actually realloc) */
629 _dbus_string_set_length (buffer, start + bytes_read);
634 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
642 * Thin wrapper around the write() system call that writes a part of a
643 * DBusString and handles EINTR for you.
645 * @param fd the file descriptor to write
646 * @param buffer the buffer to write data from
647 * @param start the first byte in the buffer to write
648 * @param len the number of bytes to try to write
649 * @returns the number of bytes written or -1 on error
652 _dbus_write_socket (int handle,
653 const DBusString *buffer,
662 data = _dbus_string_get_const_data_len (buffer, start, len);
664 _dbus_handle_to_socket(handle, &s);
668 _dbus_verbose ("send: len=%d socket=%d\n", len, s->fd);
669 bytes_written = send (s->fd, data, len, 0);
670 if (bytes_written == SOCKET_ERROR)
672 DBUS_SOCKET_SET_ERRNO();
673 _dbus_verbose ("send: failed: %s\n", _dbus_strerror (errno));
677 _dbus_verbose ("send: = %d\n", bytes_written);
681 _dbus_assert_not_reached ("unhandled fd type");
685 if (bytes_written > 0)
686 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
689 return bytes_written;
694 * Closes a file descriptor.
696 * @param fd the file descriptor
697 * @param error error object
698 * @returns #FALSE if error set
701 _dbus_close_socket (int handle,
706 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
708 _dbus_lock_sockets();
710 _dbus_handle_to_socket_unlocked (handle, &s);
715 if (s->port_file_fd >= 0)
717 _chsize (s->port_file_fd, 0);
718 close (s->port_file_fd);
719 s->port_file_fd = -1;
720 unlink (_dbus_string_get_const_data (&s->port_file));
721 free ((char *) _dbus_string_get_const_data (&s->port_file));
724 if (closesocket (s->fd) == SOCKET_ERROR)
726 DBUS_SOCKET_SET_ERRNO ();
727 dbus_set_error (error, _dbus_error_from_errno (errno),
728 "Could not close socket: socket=%d, handle=%d, %s",
729 s->fd, handle, _dbus_strerror (errno));
730 _dbus_unlock_sockets();
733 _dbus_verbose ("_dbus_close_socket: socket=%d, handle=%d\n",
738 _dbus_assert_not_reached ("unhandled fd type");
741 _dbus_unlock_sockets();
743 _dbus_win_deallocate_fd (handle);
750 * Sets the file descriptor to be close
751 * on exec. Should be called for all file
752 * descriptors in D-Bus code.
754 * @param fd the file descriptor
757 _dbus_fd_set_close_on_exec (int handle)
763 _dbus_lock_sockets();
765 _dbus_handle_to_socket_unlocked (handle, &s);
766 s->close_on_exec = TRUE;
768 _dbus_unlock_sockets();
772 * Sets a file descriptor to be nonblocking.
774 * @param fd the file descriptor.
775 * @param error address of error location.
776 * @returns #TRUE on success.
779 _dbus_set_fd_nonblocking (int handle,
785 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
787 _dbus_lock_sockets();
789 _dbus_handle_to_socket_unlocked(handle, &s);
793 if (ioctlsocket (s->fd, FIONBIO, &one) == SOCKET_ERROR)
795 dbus_set_error (error, _dbus_error_from_errno (WSAGetLastError ()),
796 "Failed to set socket %d:%d to nonblocking: %s", s->fd,
797 _dbus_strerror (WSAGetLastError ()));
798 _dbus_unlock_sockets();
804 _dbus_assert_not_reached ("unhandled fd type");
807 _dbus_unlock_sockets();
814 * Like _dbus_write() but will use writev() if possible
815 * to write both buffers in sequence. The return value
816 * is the number of bytes written in the first buffer,
817 * plus the number written in the second. If the first
818 * buffer is written successfully and an error occurs
819 * writing the second, the number of bytes in the first
820 * is returned (i.e. the error is ignored), on systems that
821 * don't have writev. Handles EINTR for you.
822 * The second buffer may be #NULL.
824 * @param fd the file descriptor
825 * @param buffer1 first buffer
826 * @param start1 first byte to write in first buffer
827 * @param len1 number of bytes to write from first buffer
828 * @param buffer2 second buffer, or #NULL
829 * @param start2 first byte to write in second buffer
830 * @param len2 number of bytes to write in second buffer
831 * @returns total bytes written from both buffers, or -1 on error
834 _dbus_write_socket_two (int handle,
835 const DBusString *buffer1,
838 const DBusString *buffer2,
850 _dbus_assert (buffer1 != NULL);
851 _dbus_assert (start1 >= 0);
852 _dbus_assert (start2 >= 0);
853 _dbus_assert (len1 >= 0);
854 _dbus_assert (len2 >= 0);
856 _dbus_handle_to_socket(handle, &s);
858 data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
861 data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
871 vectors[0].buf = (char*) data1;
872 vectors[0].len = len1;
873 vectors[1].buf = (char*) data2;
874 vectors[1].len = len2;
876 _dbus_verbose ("WSASend: len1+2=%d+%d socket=%d\n", len1, len2, s->fd);
877 rc = WSASend (s->fd, vectors, data2 ? 2 : 1, &bytes_written,
881 DBUS_SOCKET_SET_ERRNO ();
882 _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror (errno));
886 _dbus_verbose ("WSASend: = %ld\n", bytes_written);
887 return bytes_written;
891 _dbus_assert_not_reached ("unhandled fd type");
897 * @def _DBUS_MAX_SUN_PATH_LENGTH
899 * Maximum length of the path to a UNIX domain socket,
900 * sockaddr_un::sun_path member. POSIX requires that all systems
901 * support at least 100 bytes here, including the nul termination.
902 * We use 99 for the max value to allow for the nul.
904 * We could probably also do sizeof (addr.sun_path)
905 * but this way we are the same on all platforms
906 * which is probably a good idea.
910 * Creates a socket and connects it to the UNIX domain socket at the
911 * given path. The connection fd is returned, and is set up as
914 * On Windows there are no UNIX domain sockets. Instead, connects to a
915 * localhost-bound TCP socket, whose port number is stored in a file
918 * Uses abstract sockets instead of filesystem-linked sockets if
919 * requested (it's possible only on Linux; see "man 7 unix" on Linux).
920 * On non-Linux abstract socket usage always fails.
922 * @param path the path to UNIX domain socket
923 * @param abstract #TRUE to use abstract namespace
924 * @param error return location for error code
925 * @returns connection file descriptor or -1 on error
928 _dbus_connect_unix_socket (const char *path,
929 dbus_bool_t abstract,
938 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
940 _dbus_verbose ("connecting to pseudo-unix socket at %s\n",
945 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
946 "Implementation does not support abstract socket namespace\n");
950 fd = _sopen (path, O_RDONLY, SH_DENYNO);
954 dbus_set_error (error, _dbus_error_from_errno (errno),
955 "Failed to open file %s: %s",
956 path, _dbus_strerror (errno));
960 n = read (fd, buf, sizeof (buf) - 1);
965 dbus_set_error (error, DBUS_ERROR_FAILED,
966 "Failed to read port number from file %s",
974 if (port <= 0 || port > 0xFFFF)
976 dbus_set_error (error, DBUS_ERROR_FAILED,
977 "Invalid port numer in file %s",
982 return _dbus_connect_tcp_socket (NULL, port, error);
988 * Creates a socket and binds it to the given path,
989 * then listens on the socket. The socket is
990 * set to be nonblocking.
992 * Uses abstract sockets instead of filesystem-linked
993 * sockets if requested (it's possible only on Linux;
994 * see "man 7 unix" on Linux).
995 * On non-Linux abstract socket usage always fails.
997 * @param path the socket name
998 * @param abstract #TRUE to use abstract namespace
999 * @param error return location for errors
1000 * @returns the listening file descriptor or -1 on error
1003 _dbus_listen_unix_socket (const char *path,
1004 dbus_bool_t abstract,
1018 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1020 _dbus_verbose ("listening on pseudo-unix socket at %s\n",
1025 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
1026 "Implementation does not support abstract socket namespace\n");
1030 listen_handle = _dbus_listen_tcp_socket (NULL, 0, error);
1032 if (listen_handle == -1)
1035 _dbus_handle_to_socket(listen_handle, &s);
1037 addr_len = sizeof (sa);
1038 if (getsockname (s->fd, &sa, &addr_len) == SOCKET_ERROR)
1040 DBUS_SOCKET_SET_ERRNO ();
1041 dbus_set_error (error, _dbus_error_from_errno (errno),
1042 "getsockname failed: %s",
1043 _dbus_strerror (errno));
1044 _dbus_close_socket (listen_handle, NULL);
1048 _dbus_assert (((struct sockaddr_in*) &sa)->sin_family == AF_INET);
1050 filefd = _sopen (path, O_CREAT|O_WRONLY|_O_SHORT_LIVED, SH_DENYWR, 0666);
1054 dbus_set_error (error, _dbus_error_from_errno (errno),
1055 "Failed to create pseudo-unix socket port number file %s: %s",
1056 path, _dbus_strerror (errno));
1057 _dbus_close_socket (listen_handle, NULL);
1061 _dbus_lock_sockets();
1062 _dbus_handle_to_socket_unlocked(listen_handle, &s);
1063 s->port_file_fd = filefd;
1064 _dbus_unlock_sockets();
1066 /* Use strdup() to avoid memory leak in dbus-test */
1067 path = strdup (path);
1070 _DBUS_SET_OOM (error);
1071 _dbus_close_socket (listen_handle, NULL);
1075 _dbus_string_init_const (&s->port_file, path);
1077 if (!_dbus_string_init (&portstr))
1079 _DBUS_SET_OOM (error);
1080 _dbus_close_socket (listen_handle, NULL);
1084 if (!_dbus_string_append_int (&portstr, ntohs (((struct sockaddr_in*) &sa)->sin_port)))
1086 _DBUS_SET_OOM (error);
1087 _dbus_close_socket (listen_handle, NULL);
1091 l = _dbus_string_get_length (&portstr);
1092 n = write (filefd, _dbus_string_get_const_data (&portstr), l);
1093 _dbus_string_free (&portstr);
1097 dbus_set_error (error, _dbus_error_from_errno (errno),
1098 "Failed to write port number to file %s: %s",
1099 path, _dbus_strerror (errno));
1100 _dbus_close_socket (listen_handle, NULL);
1105 dbus_set_error (error, _dbus_error_from_errno (errno),
1106 "Failed to write port number to file %s",
1108 _dbus_close_socket (listen_handle, NULL);
1112 return listen_handle;
1119 * Opens the client side of a Windows named pipe. The connection D-BUS
1120 * file descriptor index is returned. It is set up as nonblocking.
1122 * @param path the path to named pipe socket
1123 * @param error return location for error code
1124 * @returns connection D-BUS file descriptor or -1 on error
1127 _dbus_connect_named_pipe (const char *path,
1130 _dbus_assert_not_reached ("not implemented");
1137 _dbus_account_to_win_sid (const wchar_t *waccount,
1141 dbus_bool_t retval = FALSE;
1142 DWORD sid_length, wdomain_length;
1150 if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1151 NULL, &wdomain_length, &use)
1152 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1154 _dbus_win_set_error_from_win_error (error, GetLastError ());
1158 *ppsid = dbus_malloc (sid_length);
1161 _DBUS_SET_OOM (error);
1165 wdomain = dbus_new (wchar_t, wdomain_length);
1168 _DBUS_SET_OOM (error);
1172 if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1173 wdomain, &wdomain_length, &use))
1175 _dbus_win_set_error_from_win_error (error, GetLastError ());
1179 if (!IsValidSid ((PSID) *ppsid))
1181 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1188 dbus_free (wdomain);
1201 fill_win_user_info_name_and_groups (wchar_t *wname,
1209 dbus_bool_t retval = FALSE;
1210 char *name, *domain;
1211 LPLOCALGROUP_USERS_INFO_0 local_groups = NULL;
1212 LPGROUP_USERS_INFO_0 global_groups = NULL;
1213 DWORD nread, ntotal;
1215 name = _dbus_win_utf16_to_utf8 (wname, error);
1219 domain = _dbus_win_utf16_to_utf8 (wdomain, error);
1223 info->username = dbus_malloc (strlen (domain) + 1 + strlen (name) + 1);
1224 if (!info->username)
1226 _DBUS_SET_OOM (error);
1230 strcpy (info->username, domain);
1231 strcat (info->username, "\\");
1232 strcat (info->username, name);
1234 info->n_group_ids = 0;
1235 if (NetUserGetLocalGroups (NULL, wname, 0, LG_INCLUDE_INDIRECT,
1236 (LPBYTE *) &local_groups, MAX_PREFERRED_LENGTH,
1237 &nread, &ntotal) == NERR_Success)
1242 info->group_ids = dbus_new (dbus_gid_t, nread);
1243 if (!info->group_ids)
1245 _DBUS_SET_OOM (error);
1249 for (i = n = 0; i < nread; i++)
1252 if (_dbus_account_to_win_sid (local_groups[i].lgrui0_name,
1255 info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1256 dbus_free (group_sid);
1259 info->n_group_ids = n;
1262 if (NetUserGetGroups (NULL, wname, 0,
1263 (LPBYTE *) &global_groups, MAX_PREFERRED_LENGTH,
1264 &nread, &ntotal) == NERR_Success)
1267 int n = info->n_group_ids;
1269 info->group_ids = dbus_realloc (info->group_ids, (n + nread) * sizeof (dbus_gid_t));
1270 if (!info->group_ids)
1272 _DBUS_SET_OOM (error);
1276 for (i = 0; i < nread; i++)
1279 if (_dbus_account_to_win_sid (global_groups[i].grui0_name,
1282 info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1283 dbus_free (group_sid);
1286 info->n_group_ids = n;
1289 if (info->n_group_ids > 0)
1291 /* FIXME: find out actual primary group */
1292 info->primary_gid = info->group_ids[0];
1296 info->group_ids = dbus_new (dbus_gid_t, 1);
1297 info->n_group_ids = 1;
1298 info->group_ids[0] = DBUS_GID_UNSET;
1299 info->primary_gid = DBUS_GID_UNSET;
1305 if (global_groups != NULL)
1306 NetApiBufferFree (global_groups);
1308 if (local_groups != NULL)
1309 NetApiBufferFree (local_groups);
1320 fill_win_user_info_homedir (wchar_t *wname,
1329 dbus_bool_t retval = FALSE;
1330 USER_INFO_1 *user_info = NULL;
1331 wchar_t wcomputername[MAX_COMPUTERNAME_LENGTH + 1];
1332 DWORD wcomputername_length = MAX_COMPUTERNAME_LENGTH + 1;
1333 dbus_bool_t local_computer;
1335 NET_API_STATUS ret = 0;
1337 /* If the domain is this computer's name, assume it's a local user.
1338 * Otherwise look up a DC for the domain, and ask it.
1341 GetComputerNameW (wcomputername, &wcomputername_length);
1342 local_computer = (wcsicmp (wcomputername, wdomain) == 0);
1344 if (!local_computer)
1346 ret = NetGetAnyDCName (NULL, wdomain, (LPBYTE *) &dc);
1347 if (ret != NERR_Success)
1349 info->homedir = _dbus_strdup ("\\");
1350 _dbus_warn("NetGetAnyDCName() failed with errorcode %d '%s'\n",ret,_dbus_lm_strerror(ret));
1355 /* No way to find out the profile of another user, let's try the
1356 * "home directory" from NetUserGetInfo's USER_INFO_1.
1358 ret = NetUserGetInfo (dc, wname, 1, (LPBYTE *) &user_info);
1359 if (ret == NERR_Success )
1360 if(user_info->usri1_home_dir != NULL &&
1361 user_info->usri1_home_dir != (LPWSTR)0xfeeefeee && /* freed memory http://www.gamedev.net/community/forums/topic.asp?topic_id=158402 */
1362 user_info->usri1_home_dir[0] != '\0')
1364 info->homedir = _dbus_win_utf16_to_utf8 (user_info->usri1_home_dir, error);
1370 _dbus_verbose("NetUserGetInfo() returned no home dir entry\n");
1371 /* Not set, so use something random. */
1372 info->homedir = _dbus_strdup ("\\");
1376 char *dc_string = _dbus_win_utf16_to_utf8(dc,error);
1377 char *user_name = _dbus_win_utf16_to_utf8(wname,error);
1378 _dbus_warn("NetUserGetInfo() for user '%s' failed with errorcode %d '%s', %s\n",user_name, ret,_dbus_lm_strerror(ret),dc_string);
1379 dbus_free(user_name);
1380 dbus_free(dc_string);
1381 /* Not set, so use something random. */
1382 info->homedir = _dbus_strdup ("\\");
1389 NetApiBufferFree (dc);
1390 if (user_info != NULL)
1391 NetApiBufferFree (user_info);
1398 fill_win_user_info_from_name (wchar_t *wname,
1406 dbus_bool_t retval = FALSE;
1409 DWORD sid_length, wdomain_length;
1414 if (!LookupAccountNameW (NULL, wname, NULL, &sid_length,
1415 NULL, &wdomain_length, &use) &&
1416 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1418 _dbus_win_set_error_from_win_error (error, GetLastError ());
1422 sid = dbus_malloc (sid_length);
1425 _DBUS_SET_OOM (error);
1429 wdomain = dbus_new (wchar_t, wdomain_length);
1432 _DBUS_SET_OOM (error);
1436 if (!LookupAccountNameW (NULL, wname, sid, &sid_length,
1437 wdomain, &wdomain_length, &use))
1439 _dbus_win_set_error_from_win_error (error, GetLastError ());
1443 if (!IsValidSid (sid))
1445 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1449 info->uid = _dbus_win_sid_to_uid_t (sid);
1451 if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1454 if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1460 dbus_free (wdomain);
1469 _dbus_win_sid_to_name_and_domain (dbus_uid_t uid,
1479 DWORD wname_length, wdomain_length;
1482 if (!_dbus_uid_t_to_win_sid (uid, &sid))
1484 _dbus_win_set_error_from_win_error (error, GetLastError ());
1490 if (!LookupAccountSidW (NULL, sid, NULL, &wname_length,
1491 NULL, &wdomain_length, &use) &&
1492 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1494 _dbus_win_set_error_from_win_error (error, GetLastError ());
1498 *wname = dbus_new (wchar_t, wname_length);
1501 _DBUS_SET_OOM (error);
1505 *wdomain = dbus_new (wchar_t, wdomain_length);
1508 _DBUS_SET_OOM (error);
1512 if (!LookupAccountSidW (NULL, sid, *wname, &wname_length,
1513 *wdomain, &wdomain_length, &use))
1515 _dbus_win_set_error_from_win_error (error, GetLastError ());
1522 dbus_free (*wdomain);
1535 fill_win_user_info_from_uid (dbus_uid_t uid,
1544 dbus_bool_t retval = FALSE;
1545 wchar_t *wname, *wdomain;
1549 if (!_dbus_win_sid_to_name_and_domain (uid, &wname, &wdomain, error))
1551 _dbus_verbose("%s after _dbus_win_sid_to_name_and_domain\n",__FUNCTION__);
1555 if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1557 _dbus_verbose("%s after fill_win_user_info_name_and_groups\n",__FUNCTION__);
1562 if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1564 _dbus_verbose("%s after fill_win_user_info_homedir\n",__FUNCTION__);
1571 dbus_free (wdomain);
1582 _dbus_win_startup_winsock (void)
1584 /* Straight from MSDN, deuglified */
1586 static dbus_bool_t beenhere = FALSE;
1588 WORD wVersionRequested;
1595 wVersionRequested = MAKEWORD (2, 0);
1597 err = WSAStartup (wVersionRequested, &wsaData);
1600 _dbus_assert_not_reached ("Could not initialize WinSock");
1604 /* Confirm that the WinSock DLL supports 2.0. Note that if the DLL
1605 * supports versions greater than 2.0 in addition to 2.0, it will
1606 * still return 2.0 in wVersion since that is the version we
1609 if (LOBYTE (wsaData.wVersion) != 2 ||
1610 HIBYTE (wsaData.wVersion) != 0)
1612 _dbus_assert_not_reached ("No usable WinSock found");
1627 /************************************************************************
1631 ************************************************************************/
1634 * Measure the message length without terminating nul
1636 int _dbus_printf_string_upper_bound (const char *format,
1639 /* MSVCRT's vsnprintf semantics are a bit different */
1640 /* The C library source in the Platform SDK indicates that this
1641 * would work, but alas, it doesn't. At least not on Windows
1642 * 2000. Presumably those sources correspond to the C library on
1643 * some newer or even future Windows version.
1645 len = _vsnprintf (NULL, _DBUS_INT_MAX, format, args);
1649 len = vsnprintf (p, sizeof(p)-1, format, args);
1650 if (len == -1) // try again
1653 p = malloc (strlen(format)*3);
1654 len = vsnprintf (p, sizeof(p)-1, format, args);
1662 * Returns the UTF-16 form of a UTF-8 string. The result should be
1663 * freed with dbus_free() when no longer needed.
1665 * @param str the UTF-8 string
1666 * @param error return location for error code
1669 _dbus_win_utf8_to_utf16 (const char *str,
1676 _dbus_string_init_const (&s, str);
1678 if (!_dbus_string_validate_utf8 (&s, 0, _dbus_string_get_length (&s)))
1680 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid UTF-8");
1684 n = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0);
1688 _dbus_win_set_error_from_win_error (error, GetLastError ());
1692 retval = dbus_new (wchar_t, n);
1696 _DBUS_SET_OOM (error);
1700 if (MultiByteToWideChar (CP_UTF8, 0, str, -1, retval, n) != n)
1703 dbus_set_error_const (error, DBUS_ERROR_FAILED, "MultiByteToWideChar inconsistency");
1711 * Returns the UTF-8 form of a UTF-16 string. The result should be
1712 * freed with dbus_free() when no longer needed.
1714 * @param str the UTF-16 string
1715 * @param error return location for error code
1718 _dbus_win_utf16_to_utf8 (const wchar_t *str,
1724 n = WideCharToMultiByte (CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
1728 _dbus_win_set_error_from_win_error (error, GetLastError ());
1732 retval = dbus_malloc (n);
1736 _DBUS_SET_OOM (error);
1740 if (WideCharToMultiByte (CP_UTF8, 0, str, -1, retval, n, NULL, NULL) != n)
1743 dbus_set_error_const (error, DBUS_ERROR_FAILED, "WideCharToMultiByte inconsistency");
1755 /************************************************************************
1757 uid ... <-> win sid functions
1759 ************************************************************************/
1762 _dbus_win_account_to_sid (const wchar_t *waccount,
1766 dbus_bool_t retval = FALSE;
1767 DWORD sid_length, wdomain_length;
1775 if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1776 NULL, &wdomain_length, &use) &&
1777 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1779 _dbus_win_set_error_from_win_error (error, GetLastError ());
1783 *ppsid = dbus_malloc (sid_length);
1786 _DBUS_SET_OOM (error);
1790 wdomain = dbus_new (wchar_t, wdomain_length);
1793 _DBUS_SET_OOM (error);
1797 if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1798 wdomain, &wdomain_length, &use))
1800 _dbus_win_set_error_from_win_error (error, GetLastError ());
1804 if (!IsValidSid ((PSID) *ppsid))
1806 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1813 dbus_free (wdomain);
1825 _sid_atom_cache_shutdown (void *unused)
1828 _DBUS_LOCK (sid_atom_cache);
1829 _dbus_hash_iter_init (sid_atom_cache, &iter);
1830 while (_dbus_hash_iter_next (&iter))
1833 atom = (ATOM) _dbus_hash_iter_get_value (&iter);
1834 GlobalDeleteAtom(atom);
1835 _dbus_hash_iter_remove_entry(&iter);
1837 _DBUS_UNLOCK (sid_atom_cache);
1838 _dbus_hash_table_unref (sid_atom_cache);
1839 sid_atom_cache = NULL;
1843 * Returns the 2-way associated dbus_uid_t form a SID.
1845 * @param psid pointer to the SID
1848 _dbus_win_sid_to_uid_t (PSID psid)
1855 if (!IsValidSid (psid))
1857 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1858 return DBUS_UID_UNSET;
1860 if (!ConvertSidToStringSidA (psid, &string))
1862 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1863 return DBUS_UID_UNSET;
1866 atom = GlobalAddAtom(string);
1870 _dbus_verbose("%s GlobalAddAtom failed\n",__FUNCTION__);
1872 return DBUS_UID_UNSET;
1875 _DBUS_LOCK (sid_atom_cache);
1877 if (sid_atom_cache == NULL)
1879 sid_atom_cache = _dbus_hash_table_new (DBUS_HASH_ULONG, NULL, NULL);
1880 _dbus_register_shutdown_func (_sid_atom_cache_shutdown, NULL);
1884 olduid = (dbus_uid_t) _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1888 _dbus_verbose("%s sid with id %i found in cache\n",__FUNCTION__, olduid);
1893 _dbus_hash_table_insert_ulong (sid_atom_cache, uid, (void*) uid);
1894 _dbus_verbose("%s sid %s added with uid %i to cache\n",__FUNCTION__, string, uid);
1897 _DBUS_UNLOCK (sid_atom_cache);
1902 dbus_bool_t _dbus_uid_t_to_win_sid (dbus_uid_t uid, PSID *ppsid)
1907 atom = _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1910 _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__,uid);
1913 memset( string, '.', sizeof(string) );
1914 if (!GlobalGetAtomNameA( (ATOM) atom, string, 255 ))
1916 _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__, uid);
1919 if (!ConvertStringSidToSidA(string, ppsid))
1921 _dbus_verbose("%s could not convert %s into sid \n",__FUNCTION__, string);
1924 _dbus_verbose("%s converted %s into sid \n",__FUNCTION__, string);
1929 /** @} end of sysdeps-win */
1933 * @returns process UID
1938 dbus_uid_t retval = DBUS_UID_UNSET;
1939 HANDLE process_token = NULL;
1940 TOKEN_USER *token_user = NULL;
1943 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1944 _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1945 else if ((!GetTokenInformation (process_token, TokenUser, NULL, 0, &n)
1946 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1947 || (token_user = alloca (n)) == NULL
1948 || !GetTokenInformation (process_token, TokenUser, token_user, n, &n))
1949 _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1951 retval = _dbus_win_sid_to_uid_t (token_user->User.Sid);
1953 if (process_token != NULL)
1954 CloseHandle (process_token);
1956 _dbus_verbose("_dbus_getuid() returns %d\n",retval);
1960 #ifdef DBUS_BUILD_TESTS
1962 * @returns process GID
1967 dbus_gid_t retval = DBUS_GID_UNSET;
1968 HANDLE process_token = NULL;
1969 TOKEN_PRIMARY_GROUP *token_primary_group = NULL;
1972 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1973 _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1974 else if ((!GetTokenInformation (process_token, TokenPrimaryGroup,
1976 GetLastError () != ERROR_INSUFFICIENT_BUFFER) ||
1977 (token_primary_group = alloca (n)) == NULL ||
1978 !GetTokenInformation (process_token, TokenPrimaryGroup,
1979 token_primary_group, n, &n))
1980 _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1982 retval = _dbus_win_sid_to_uid_t (token_primary_group->PrimaryGroup);
1984 if (process_token != NULL)
1985 CloseHandle (process_token);
1992 _dbus_domain_test (const char *test_data_dir)
1994 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1995 check_spawn_nonexistent,
2002 #endif //DBUS_BUILD_TESTS
2004 /************************************************************************
2008 ************************************************************************/
2011 * Creates a full-duplex pipe (as in socketpair()).
2012 * Sets both ends of the pipe nonblocking.
2014 * @todo libdbus only uses this for the debug-pipe server, so in
2015 * principle it could be in dbus-sysdeps-util.c, except that
2016 * dbus-sysdeps-util.c isn't in libdbus when tests are enabled and the
2017 * debug-pipe server is used.
2019 * @param fd1 return location for one end
2020 * @param fd2 return location for the other end
2021 * @param blocking #TRUE if pipe should be blocking
2022 * @param error error return
2023 * @returns #FALSE on failure (if error is set)
2026 _dbus_full_duplex_pipe (int *fd1,
2028 dbus_bool_t blocking,
2031 SOCKET temp, socket1 = -1, socket2 = -1;
2032 struct sockaddr_in saddr;
2035 fd_set read_set, write_set;
2040 _dbus_win_startup_winsock ();
2042 temp = socket (AF_INET, SOCK_STREAM, 0);
2043 if (temp == INVALID_SOCKET)
2045 DBUS_SOCKET_SET_ERRNO ();
2050 if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR)
2052 DBUS_SOCKET_SET_ERRNO ();
2057 saddr.sin_family = AF_INET;
2059 saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
2061 if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)))
2063 DBUS_SOCKET_SET_ERRNO ();
2067 if (listen (temp, 1) == SOCKET_ERROR)
2069 DBUS_SOCKET_SET_ERRNO ();
2073 len = sizeof (saddr);
2074 if (getsockname (temp, (struct sockaddr *)&saddr, &len))
2076 DBUS_SOCKET_SET_ERRNO ();
2080 socket1 = socket (AF_INET, SOCK_STREAM, 0);
2081 if (socket1 == INVALID_SOCKET)
2083 DBUS_SOCKET_SET_ERRNO ();
2088 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2090 DBUS_SOCKET_SET_ERRNO ();
2094 if (connect (socket1, (struct sockaddr *)&saddr, len) != SOCKET_ERROR ||
2095 WSAGetLastError () != WSAEWOULDBLOCK)
2097 dbus_set_error_const (error, DBUS_ERROR_FAILED,
2098 "_dbus_full_duplex_pipe socketpair() emulation failed");
2102 FD_ZERO (&read_set);
2103 FD_SET (temp, &read_set);
2108 if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR)
2110 DBUS_SOCKET_SET_ERRNO ();
2114 _dbus_assert (FD_ISSET (temp, &read_set));
2116 socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
2117 if (socket2 == INVALID_SOCKET)
2119 DBUS_SOCKET_SET_ERRNO ();
2123 FD_ZERO (&write_set);
2124 FD_SET (socket1, &write_set);
2129 if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR)
2131 DBUS_SOCKET_SET_ERRNO ();
2135 _dbus_assert (FD_ISSET (socket1, &write_set));
2140 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2142 DBUS_SOCKET_SET_ERRNO ();
2147 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2149 DBUS_SOCKET_SET_ERRNO ();
2156 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2158 DBUS_SOCKET_SET_ERRNO ();
2164 *fd1 = _dbus_socket_to_handle (&sock);
2166 *fd2 = _dbus_socket_to_handle (&sock);
2168 _dbus_verbose ("full-duplex pipe %d:%d <-> %d:%d\n",
2169 *fd1, socket1, *fd2, socket2);
2176 closesocket (socket2);
2178 closesocket (socket1);
2182 dbus_set_error (error, _dbus_error_from_errno (errno),
2183 "Could not setup socket pair: %s",
2184 _dbus_strerror (errno));
2190 * Wrapper for poll().
2192 * @param fds the file descriptors to poll
2193 * @param n_fds number of descriptors in the array
2194 * @param timeout_milliseconds timeout or -1 for infinite
2195 * @returns numbers of fds with revents, or <0 on error
2197 #define USE_CHRIS_IMPL 0
2200 _dbus_poll (DBusPollFD *fds,
2202 int timeout_milliseconds)
2204 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2205 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2213 #define DBUS_STACK_WSAEVENTS 256
2214 WSAEVENT eventsOnStack[DBUS_STACK_WSAEVENTS];
2215 WSAEVENT *pEvents = NULL;
2216 if (n_fds > DBUS_STACK_WSAEVENTS)
2217 pEvents = calloc(sizeof(WSAEVENT), n_fds);
2219 pEvents = eventsOnStack;
2221 _dbus_lock_sockets();
2223 #ifdef DBUS_ENABLE_VERBOSE_MODE
2225 msgp += sprintf (msgp, "WSAEventSelect: to=%d\n\t", timeout_milliseconds);
2226 for (i = 0; i < n_fds; i++)
2228 static dbus_bool_t warned = FALSE;
2230 DBusPollFD *fdp = &fds[i];
2232 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2234 if (s->is_used == 0)
2236 _dbus_warn ("no valid socket");
2240 if (fdp->events & _DBUS_POLLIN)
2241 msgp += sprintf (msgp, "R:%d ", s->fd);
2243 if (fdp->events & _DBUS_POLLOUT)
2244 msgp += sprintf (msgp, "W:%d ", s->fd);
2246 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2248 // FIXME: more robust code for long msg
2249 // create on heap when msg[] becomes too small
2250 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2252 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2256 msgp += sprintf (msgp, "\n");
2257 _dbus_verbose ("%s",msg);
2259 for (i = 0; i < n_fds; i++)
2262 DBusPollFD *fdp = &fds[i];
2264 long lNetworkEvents = FD_OOB;
2266 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2268 if (s->is_used == 0)
2271 ev = WSACreateEvent();
2273 if (fdp->events & _DBUS_POLLIN)
2274 lNetworkEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
2276 if (fdp->events & _DBUS_POLLOUT)
2277 lNetworkEvents |= FD_WRITE | FD_CONNECT;
2279 WSAEventSelect(s->fd, ev, lNetworkEvents);
2284 _dbus_unlock_sockets();
2286 ready = WSAWaitForMultipleEvents (n_fds, pEvents, FALSE, timeout_milliseconds, FALSE);
2288 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2290 DBUS_SOCKET_SET_ERRNO ();
2291 if (errno != EWOULDBLOCK)
2292 _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", _dbus_strerror (errno));
2295 else if (ready == WSA_WAIT_TIMEOUT)
2297 _dbus_verbose ("WSAWaitForMultipleEvents: WSA_WAIT_TIMEOUT\n");
2300 else if (ready >= WSA_WAIT_EVENT_0 && ready < (int)(WSA_WAIT_EVENT_0 + n_fds))
2303 msgp += sprintf (msgp, "WSAWaitForMultipleEvents: =%d\n\t", ready);
2305 _dbus_lock_sockets();
2306 for (i = 0; i < n_fds; i++)
2309 DBusPollFD *fdp = &fds[i];
2310 WSANETWORKEVENTS ne;
2312 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2316 WSAEnumNetworkEvents(s->fd, pEvents[i], &ne);
2318 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2319 fdp->revents |= _DBUS_POLLIN;
2321 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2322 fdp->revents |= _DBUS_POLLOUT;
2324 if (ne.lNetworkEvents & (FD_OOB))
2325 fdp->revents |= _DBUS_POLLERR;
2327 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2328 msgp += sprintf (msgp, "R:%d ", s->fd);
2330 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2331 msgp += sprintf (msgp, "W:%d ", s->fd);
2333 if (ne.lNetworkEvents & (FD_OOB))
2334 msgp += sprintf (msgp, "E:%d ", s->fd);
2336 msgp += sprintf (msgp, "lNetworkEvents:%d ", ne.lNetworkEvents);
2338 if(ne.lNetworkEvents)
2341 WSAEventSelect(s->fd, pEvents[i], 0);
2343 _dbus_unlock_sockets();
2345 msgp += sprintf (msgp, "\n");
2346 _dbus_verbose ("%s",msg);
2350 _dbus_verbose ("WSAWaitForMultipleEvents: failed for unknown reason!");
2354 for(i = 0; i < n_fds; i++)
2356 WSACloseEvent(pEvents[i]);
2359 if (n_fds > DBUS_STACK_WSAEVENTS)
2365 #else // USE_CHRIS_IMPL
2368 _dbus_poll (DBusPollFD *fds,
2370 int timeout_milliseconds)
2372 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2373 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2376 fd_set read_set, write_set, err_set;
2382 FD_ZERO (&read_set);
2383 FD_ZERO (&write_set);
2386 _dbus_lock_sockets();
2388 #ifdef DBUS_ENABLE_VERBOSE_MODE
2390 msgp += sprintf (msgp, "select: to=%d\n\t", timeout_milliseconds);
2391 for (i = 0; i < n_fds; i++)
2393 static dbus_bool_t warned = FALSE;
2395 DBusPollFD *fdp = &fds[i];
2397 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2399 if (s->is_used == 0)
2401 _dbus_warn ("no valid socket");
2405 if (fdp->events & _DBUS_POLLIN)
2406 msgp += sprintf (msgp, "R:%d ", s->fd);
2408 if (fdp->events & _DBUS_POLLOUT)
2409 msgp += sprintf (msgp, "W:%d ", s->fd);
2411 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2413 // FIXME: more robust code for long msg
2414 // create on heap when msg[] becomes too small
2415 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2417 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2421 msgp += sprintf (msgp, "\n");
2422 _dbus_verbose ("%s",msg);
2424 for (i = 0; i < n_fds; i++)
2427 DBusPollFD *fdp = &fds[i];
2429 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2431 if (s->is_used != 1)
2434 if (fdp->events & _DBUS_POLLIN)
2435 FD_SET (s->fd, &read_set);
2437 if (fdp->events & _DBUS_POLLOUT)
2438 FD_SET (s->fd, &write_set);
2440 FD_SET (s->fd, &err_set);
2442 max_fd = MAX (max_fd, s->fd);
2445 _dbus_unlock_sockets();
2447 tv.tv_sec = timeout_milliseconds / 1000;
2448 tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
2450 ready = select (max_fd + 1, &read_set, &write_set, &err_set,
2451 timeout_milliseconds < 0 ? NULL : &tv);
2453 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2455 DBUS_SOCKET_SET_ERRNO ();
2456 if (errno != EWOULDBLOCK)
2457 _dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno));
2459 else if (ready == 0)
2460 _dbus_verbose ("select: = 0\n");
2464 #ifdef DBUS_ENABLE_VERBOSE_MODE
2466 msgp += sprintf (msgp, "select: = %d:\n\t", ready);
2467 _dbus_lock_sockets();
2468 for (i = 0; i < n_fds; i++)
2471 DBusPollFD *fdp = &fds[i];
2473 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2475 if (FD_ISSET (s->fd, &read_set))
2476 msgp += sprintf (msgp, "R:%d ", s->fd);
2478 if (FD_ISSET (s->fd, &write_set))
2479 msgp += sprintf (msgp, "W:%d ", s->fd);
2481 if (FD_ISSET (s->fd, &err_set))
2482 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2484 msgp += sprintf (msgp, "\n");
2485 _dbus_verbose ("%s",msg);
2488 for (i = 0; i < n_fds; i++)
2491 DBusPollFD *fdp = &fds[i];
2493 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2497 if (FD_ISSET (s->fd, &read_set))
2498 fdp->revents |= _DBUS_POLLIN;
2500 if (FD_ISSET (s->fd, &write_set))
2501 fdp->revents |= _DBUS_POLLOUT;
2503 if (FD_ISSET (s->fd, &err_set))
2504 fdp->revents |= _DBUS_POLLERR;
2506 _dbus_unlock_sockets();
2510 #endif // USE_CHRIS_IMPL
2513 /************************************************************************
2517 ************************************************************************/
2521 * Assigns an error name and message corresponding to a Win32 error
2522 * code to a DBusError. Does nothing if error is #NULL.
2524 * @param error the error.
2525 * @param code the Win32 error code
2528 _dbus_win_set_error_from_win_error (DBusError *error,
2533 /* As we want the English message, use the A API */
2534 FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
2535 FORMAT_MESSAGE_IGNORE_INSERTS |
2536 FORMAT_MESSAGE_FROM_SYSTEM,
2537 NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
2538 (LPTSTR) &msg, 0, NULL);
2543 msg_copy = dbus_malloc (strlen (msg));
2544 strcpy (msg_copy, msg);
2547 dbus_set_error (error, "win32.error", "%s", msg_copy);
2550 dbus_set_error_const (error, "win32.error", "Unknown error code or FormatMessage failed");
2554 _dbus_win_warn_win_error (const char *message,
2559 dbus_error_init (&error);
2560 _dbus_win_set_error_from_win_error (&error, code);
2561 _dbus_warn ("%s: %s\n", message, error.message);
2562 dbus_error_free (&error);
2566 * A wrapper around strerror() because some platforms
2567 * may be lame and not have strerror().
2569 * @param error_number errno.
2570 * @returns error description.
2573 _dbus_strerror (int error_number)
2581 switch (error_number)
2584 return "Interrupted function call";
2586 return "Permission denied";
2588 return "Bad address";
2590 return "Invalid argument";
2592 return "Too many open files";
2593 case WSAEWOULDBLOCK:
2594 return "Resource temporarily unavailable";
2595 case WSAEINPROGRESS:
2596 return "Operation now in progress";
2598 return "Operation already in progress";
2600 return "Socket operation on nonsocket";
2601 case WSAEDESTADDRREQ:
2602 return "Destination address required";
2604 return "Message too long";
2606 return "Protocol wrong type for socket";
2607 case WSAENOPROTOOPT:
2608 return "Bad protocol option";
2609 case WSAEPROTONOSUPPORT:
2610 return "Protocol not supported";
2611 case WSAESOCKTNOSUPPORT:
2612 return "Socket type not supported";
2614 return "Operation not supported";
2615 case WSAEPFNOSUPPORT:
2616 return "Protocol family not supported";
2617 case WSAEAFNOSUPPORT:
2618 return "Address family not supported by protocol family";
2620 return "Address already in use";
2621 case WSAEADDRNOTAVAIL:
2622 return "Cannot assign requested address";
2624 return "Network is down";
2625 case WSAENETUNREACH:
2626 return "Network is unreachable";
2628 return "Network dropped connection on reset";
2629 case WSAECONNABORTED:
2630 return "Software caused connection abort";
2632 return "Connection reset by peer";
2634 return "No buffer space available";
2636 return "Socket is already connected";
2638 return "Socket is not connected";
2640 return "Cannot send after socket shutdown";
2642 return "Connection timed out";
2643 case WSAECONNREFUSED:
2644 return "Connection refused";
2646 return "Host is down";
2647 case WSAEHOSTUNREACH:
2648 return "No route to host";
2650 return "Too many processes";
2652 return "Graceful shutdown in progress";
2653 case WSATYPE_NOT_FOUND:
2654 return "Class type not found";
2655 case WSAHOST_NOT_FOUND:
2656 return "Host not found";
2658 return "Nonauthoritative host not found";
2659 case WSANO_RECOVERY:
2660 return "This is a nonrecoverable error";
2662 return "Valid name, no data record of requested type";
2663 case WSA_INVALID_HANDLE:
2664 return "Specified event object handle is invalid";
2665 case WSA_INVALID_PARAMETER:
2666 return "One or more parameters are invalid";
2667 case WSA_IO_INCOMPLETE:
2668 return "Overlapped I/O event object not in signaled state";
2669 case WSA_IO_PENDING:
2670 return "Overlapped operations will complete later";
2671 case WSA_NOT_ENOUGH_MEMORY:
2672 return "Insufficient memory available";
2673 case WSA_OPERATION_ABORTED:
2674 return "Overlapped operation aborted";
2675 #ifdef WSAINVALIDPROCTABLE
2677 case WSAINVALIDPROCTABLE:
2678 return "Invalid procedure table from service provider";
2680 #ifdef WSAINVALIDPROVIDER
2682 case WSAINVALIDPROVIDER:
2683 return "Invalid service provider version number";
2685 #ifdef WSAPROVIDERFAILEDINIT
2687 case WSAPROVIDERFAILEDINIT:
2688 return "Unable to initialize a service provider";
2691 case WSASYSCALLFAILURE:
2692 return "System call failure";
2694 msg = strerror (error_number);
2704 /* lan manager error codes */
2706 _dbus_lm_strerror(int error_number)
2713 switch (error_number)
2715 case NERR_NetNotStarted:
2716 return "The workstation driver is not installed.";
2717 case NERR_UnknownServer:
2718 return "The server could not be located.";
2720 return "An internal error occurred. The network cannot access a shared memory segment.";
2721 case NERR_NoNetworkResource:
2722 return "A network resource shortage occurred.";
2723 case NERR_RemoteOnly:
2724 return "This operation is not supported on workstations.";
2725 case NERR_DevNotRedirected:
2726 return "The device is not connected.";
2727 case NERR_ServerNotStarted:
2728 return "The Server service is not started.";
2729 case NERR_ItemNotFound:
2730 return "The queue is empty.";
2731 case NERR_UnknownDevDir:
2732 return "The device or directory does not exist.";
2733 case NERR_RedirectedPath:
2734 return "The operation is invalid on a redirected resource.";
2735 case NERR_DuplicateShare:
2736 return "The name has already been shared.";
2738 return "The server is currently out of the requested resource.";
2739 case NERR_TooManyItems:
2740 return "Requested addition of items exceeds the maximum allowed.";
2741 case NERR_InvalidMaxUsers:
2742 return "The Peer service supports only two simultaneous users.";
2743 case NERR_BufTooSmall:
2744 return "The API return buffer is too small.";
2745 case NERR_RemoteErr:
2746 return "A remote API error occurred.";
2747 case NERR_LanmanIniError:
2748 return "An error occurred when opening or reading the configuration file.";
2749 case NERR_NetworkError:
2750 return "A general network error occurred.";
2751 case NERR_WkstaInconsistentState:
2752 return "The Workstation service is in an inconsistent state. Restart the computer before restarting the Workstation service.";
2753 case NERR_WkstaNotStarted:
2754 return "The Workstation service has not been started.";
2755 case NERR_BrowserNotStarted:
2756 return "The requested information is not available.";
2757 case NERR_InternalError:
2758 return "An internal error occurred.";
2759 case NERR_BadTransactConfig:
2760 return "The server is not configured for transactions.";
2761 case NERR_InvalidAPI:
2762 return "The requested API is not supported on the remote server.";
2763 case NERR_BadEventName:
2764 return "The event name is invalid.";
2765 case NERR_DupNameReboot:
2766 return "The computer name already exists on the network. Change it and restart the computer.";
2767 case NERR_CfgCompNotFound:
2768 return "The specified component could not be found in the configuration information.";
2769 case NERR_CfgParamNotFound:
2770 return "The specified parameter could not be found in the configuration information.";
2771 case NERR_LineTooLong:
2772 return "A line in the configuration file is too long.";
2773 case NERR_QNotFound:
2774 return "The printer does not exist.";
2775 case NERR_JobNotFound:
2776 return "The print job does not exist.";
2777 case NERR_DestNotFound:
2778 return "The printer destination cannot be found.";
2779 case NERR_DestExists:
2780 return "The printer destination already exists.";
2782 return "The printer queue already exists.";
2784 return "No more printers can be added.";
2785 case NERR_JobNoRoom:
2786 return "No more print jobs can be added.";
2787 case NERR_DestNoRoom:
2788 return "No more printer destinations can be added.";
2790 return "This printer destination is idle and cannot accept control operations.";
2791 case NERR_DestInvalidOp:
2792 return "This printer destination request contains an invalid control function.";
2793 case NERR_ProcNoRespond:
2794 return "The print processor is not responding.";
2795 case NERR_SpoolerNotLoaded:
2796 return "The spooler is not running.";
2797 case NERR_DestInvalidState:
2798 return "This operation cannot be performed on the print destination in its current state.";
2799 case NERR_QInvalidState:
2800 return "This operation cannot be performed on the printer queue in its current state.";
2801 case NERR_JobInvalidState:
2802 return "This operation cannot be performed on the print job in its current state.";
2803 case NERR_SpoolNoMemory:
2804 return "A spooler memory allocation failure occurred.";
2805 case NERR_DriverNotFound:
2806 return "The device driver does not exist.";
2807 case NERR_DataTypeInvalid:
2808 return "The data type is not supported by the print processor.";
2809 case NERR_ProcNotFound:
2810 return "The print processor is not installed.";
2811 case NERR_ServiceTableLocked:
2812 return "The service database is locked.";
2813 case NERR_ServiceTableFull:
2814 return "The service table is full.";
2815 case NERR_ServiceInstalled:
2816 return "The requested service has already been started.";
2817 case NERR_ServiceEntryLocked:
2818 return "The service does not respond to control actions.";
2819 case NERR_ServiceNotInstalled:
2820 return "The service has not been started.";
2821 case NERR_BadServiceName:
2822 return "The service name is invalid.";
2823 case NERR_ServiceCtlTimeout:
2824 return "The service is not responding to the control function.";
2825 case NERR_ServiceCtlBusy:
2826 return "The service control is busy.";
2827 case NERR_BadServiceProgName:
2828 return "The configuration file contains an invalid service program name.";
2829 case NERR_ServiceNotCtrl:
2830 return "The service could not be controlled in its present state.";
2831 case NERR_ServiceKillProc:
2832 return "The service ended abnormally.";
2833 case NERR_ServiceCtlNotValid:
2834 return "The requested pause or stop is not valid for this service.";
2835 case NERR_NotInDispatchTbl:
2836 return "The service control dispatcher could not find the service name in the dispatch table.";
2837 case NERR_BadControlRecv:
2838 return "The service control dispatcher pipe read failed.";
2839 case NERR_ServiceNotStarting:
2840 return "A thread for the new service could not be created.";
2841 case NERR_AlreadyLoggedOn:
2842 return "This workstation is already logged on to the local-area network.";
2843 case NERR_NotLoggedOn:
2844 return "The workstation is not logged on to the local-area network.";
2845 case NERR_BadUsername:
2846 return "The user name or group name parameter is invalid.";
2847 case NERR_BadPassword:
2848 return "The password parameter is invalid.";
2849 case NERR_UnableToAddName_W:
2850 return "@W The logon processor did not add the message alias.";
2851 case NERR_UnableToAddName_F:
2852 return "The logon processor did not add the message alias.";
2853 case NERR_UnableToDelName_W:
2854 return "@W The logoff processor did not delete the message alias.";
2855 case NERR_UnableToDelName_F:
2856 return "The logoff processor did not delete the message alias.";
2857 case NERR_LogonsPaused:
2858 return "Network logons are paused.";
2859 case NERR_LogonServerConflict:
2860 return "A centralized logon-server conflict occurred.";
2861 case NERR_LogonNoUserPath:
2862 return "The server is configured without a valid user path.";
2863 case NERR_LogonScriptError:
2864 return "An error occurred while loading or running the logon script.";
2865 case NERR_StandaloneLogon:
2866 return "The logon server was not specified. Your computer will be logged on as STANDALONE.";
2867 case NERR_LogonServerNotFound:
2868 return "The logon server could not be found.";
2869 case NERR_LogonDomainExists:
2870 return "There is already a logon domain for this computer.";
2871 case NERR_NonValidatedLogon:
2872 return "The logon server could not validate the logon.";
2873 case NERR_ACFNotFound:
2874 return "The security database could not be found.";
2875 case NERR_GroupNotFound:
2876 return "The group name could not be found.";
2877 case NERR_UserNotFound:
2878 return "The user name could not be found.";
2879 case NERR_ResourceNotFound:
2880 return "The resource name could not be found.";
2881 case NERR_GroupExists:
2882 return "The group already exists.";
2883 case NERR_UserExists:
2884 return "The user account already exists.";
2885 case NERR_ResourceExists:
2886 return "The resource permission list already exists.";
2887 case NERR_NotPrimary:
2888 return "This operation is only allowed on the primary domain controller of the domain.";
2889 case NERR_ACFNotLoaded:
2890 return "The security database has not been started.";
2891 case NERR_ACFNoRoom:
2892 return "There are too many names in the user accounts database.";
2893 case NERR_ACFFileIOFail:
2894 return "A disk I/O failure occurred.";
2895 case NERR_ACFTooManyLists:
2896 return "The limit of 64 entries per resource was exceeded.";
2897 case NERR_UserLogon:
2898 return "Deleting a user with a session is not allowed.";
2899 case NERR_ACFNoParent:
2900 return "The parent directory could not be located.";
2901 case NERR_CanNotGrowSegment:
2902 return "Unable to add to the security database session cache segment.";
2903 case NERR_SpeGroupOp:
2904 return "This operation is not allowed on this special group.";
2905 case NERR_NotInCache:
2906 return "This user is not cached in user accounts database session cache.";
2907 case NERR_UserInGroup:
2908 return "The user already belongs to this group.";
2909 case NERR_UserNotInGroup:
2910 return "The user does not belong to this group.";
2911 case NERR_AccountUndefined:
2912 return "This user account is undefined.";
2913 case NERR_AccountExpired:
2914 return "This user account has expired.";
2915 case NERR_InvalidWorkstation:
2916 return "The user is not allowed to log on from this workstation.";
2917 case NERR_InvalidLogonHours:
2918 return "The user is not allowed to log on at this time.";
2919 case NERR_PasswordExpired:
2920 return "The password of this user has expired.";
2921 case NERR_PasswordCantChange:
2922 return "The password of this user cannot change.";
2923 case NERR_PasswordHistConflict:
2924 return "This password cannot be used now.";
2925 case NERR_PasswordTooShort:
2926 return "The password does not meet the password policy requirements. Check the minimum password length, password complexity and password history requirements.";
2927 case NERR_PasswordTooRecent:
2928 return "The password of this user is too recent to change.";
2929 case NERR_InvalidDatabase:
2930 return "The security database is corrupted.";
2931 case NERR_DatabaseUpToDate:
2932 return "No updates are necessary to this replicant network/local security database.";
2933 case NERR_SyncRequired:
2934 return "This replicant database is outdated; synchronization is required.";
2935 case NERR_UseNotFound:
2936 return "The network connection could not be found.";
2937 case NERR_BadAsgType:
2938 return "This asg_type is invalid.";
2939 case NERR_DeviceIsShared:
2940 return "This device is currently being shared.";
2941 case NERR_NoComputerName:
2942 return "The computer name could not be added as a message alias. The name may already exist on the network.";
2943 case NERR_MsgAlreadyStarted:
2944 return "The Messenger service is already started.";
2945 case NERR_MsgInitFailed:
2946 return "The Messenger service failed to start.";
2947 case NERR_NameNotFound:
2948 return "The message alias could not be found on the network.";
2949 case NERR_AlreadyForwarded:
2950 return "This message alias has already been forwarded.";
2951 case NERR_AddForwarded:
2952 return "This message alias has been added but is still forwarded.";
2953 case NERR_AlreadyExists:
2954 return "This message alias already exists locally.";
2955 case NERR_TooManyNames:
2956 return "The maximum number of added message aliases has been exceeded.";
2957 case NERR_DelComputerName:
2958 return "The computer name could not be deleted.";
2959 case NERR_LocalForward:
2960 return "Messages cannot be forwarded back to the same workstation.";
2961 case NERR_GrpMsgProcessor:
2962 return "An error occurred in the domain message processor.";
2963 case NERR_PausedRemote:
2964 return "The message was sent, but the recipient has paused the Messenger service.";
2965 case NERR_BadReceive:
2966 return "The message was sent but not received.";
2967 case NERR_NameInUse:
2968 return "The message alias is currently in use. Try again later.";
2969 case NERR_MsgNotStarted:
2970 return "The Messenger service has not been started.";
2971 case NERR_NotLocalName:
2972 return "The name is not on the local computer.";
2973 case NERR_NoForwardName:
2974 return "The forwarded message alias could not be found on the network.";
2975 case NERR_RemoteFull:
2976 return "The message alias table on the remote station is full.";
2977 case NERR_NameNotForwarded:
2978 return "Messages for this alias are not currently being forwarded.";
2979 case NERR_TruncatedBroadcast:
2980 return "The broadcast message was truncated.";
2981 case NERR_InvalidDevice:
2982 return "This is an invalid device name.";
2983 case NERR_WriteFault:
2984 return "A write fault occurred.";
2985 case NERR_DuplicateName:
2986 return "A duplicate message alias exists on the network.";
2987 case NERR_DeleteLater:
2988 return "@W This message alias will be deleted later.";
2989 case NERR_IncompleteDel:
2990 return "The message alias was not successfully deleted from all networks.";
2991 case NERR_MultipleNets:
2992 return "This operation is not supported on computers with multiple networks.";
2993 case NERR_NetNameNotFound:
2994 return "This shared resource does not exist.";
2995 case NERR_DeviceNotShared:
2996 return "This device is not shared.";
2997 case NERR_ClientNameNotFound:
2998 return "A session does not exist with that computer name.";
2999 case NERR_FileIdNotFound:
3000 return "There is not an open file with that identification number.";
3001 case NERR_ExecFailure:
3002 return "A failure occurred when executing a remote administration command.";
3004 return "A failure occurred when opening a remote temporary file.";
3005 case NERR_TooMuchData:
3006 return "The data returned from a remote administration command has been truncated to 64K.";
3007 case NERR_DeviceShareConflict:
3008 return "This device cannot be shared as both a spooled and a non-spooled resource.";
3009 case NERR_BrowserTableIncomplete:
3010 return "The information in the list of servers may be incorrect.";
3011 case NERR_NotLocalDomain:
3012 return "The computer is not active in this domain.";
3013 #ifdef NERR_IsDfsShare
3015 case NERR_IsDfsShare:
3016 return "The share must be removed from the Distributed File System before it can be deleted.";
3019 case NERR_DevInvalidOpCode:
3020 return "The operation is invalid for this device.";
3021 case NERR_DevNotFound:
3022 return "This device cannot be shared.";
3023 case NERR_DevNotOpen:
3024 return "This device was not open.";
3025 case NERR_BadQueueDevString:
3026 return "This device name list is invalid.";
3027 case NERR_BadQueuePriority:
3028 return "The queue priority is invalid.";
3029 case NERR_NoCommDevs:
3030 return "There are no shared communication devices.";
3031 case NERR_QueueNotFound:
3032 return "The queue you specified does not exist.";
3033 case NERR_BadDevString:
3034 return "This list of devices is invalid.";
3036 return "The requested device is invalid.";
3037 case NERR_InUseBySpooler:
3038 return "This device is already in use by the spooler.";
3039 case NERR_CommDevInUse:
3040 return "This device is already in use as a communication device.";
3041 case NERR_InvalidComputer:
3042 return "This computer name is invalid.";
3043 case NERR_MaxLenExceeded:
3044 return "The string and prefix specified are too long.";
3045 case NERR_BadComponent:
3046 return "This path component is invalid.";
3048 return "Could not determine the type of input.";
3049 case NERR_TooManyEntries:
3050 return "The buffer for types is not big enough.";
3051 case NERR_ProfileFileTooBig:
3052 return "Profile files cannot exceed 64K.";
3053 case NERR_ProfileOffset:
3054 return "The start offset is out of range.";
3055 case NERR_ProfileCleanup:
3056 return "The system cannot delete current connections to network resources.";
3057 case NERR_ProfileUnknownCmd:
3058 return "The system was unable to parse the command line in this file.";
3059 case NERR_ProfileLoadErr:
3060 return "An error occurred while loading the profile file.";
3061 case NERR_ProfileSaveErr:
3062 return "@W Errors occurred while saving the profile file. The profile was partially saved.";
3063 case NERR_LogOverflow:
3064 return "Log file %1 is full.";
3065 case NERR_LogFileChanged:
3066 return "This log file has changed between reads.";
3067 case NERR_LogFileCorrupt:
3068 return "Log file %1 is corrupt.";
3069 case NERR_SourceIsDir:
3070 return "The source path cannot be a directory.";
3071 case NERR_BadSource:
3072 return "The source path is illegal.";
3074 return "The destination path is illegal.";
3075 case NERR_DifferentServers:
3076 return "The source and destination paths are on different servers.";
3077 case NERR_RunSrvPaused:
3078 return "The Run server you requested is paused.";
3079 case NERR_ErrCommRunSrv:
3080 return "An error occurred when communicating with a Run server.";
3081 case NERR_ErrorExecingGhost:
3082 return "An error occurred when starting a background process.";
3083 case NERR_ShareNotFound:
3084 return "The shared resource you are connected to could not be found.";
3085 case NERR_InvalidLana:
3086 return "The LAN adapter number is invalid.";
3087 case NERR_OpenFiles:
3088 return "There are open files on the connection.";
3089 case NERR_ActiveConns:
3090 return "Active connections still exist.";
3091 case NERR_BadPasswordCore:
3092 return "This share name or password is invalid.";
3094 return "The device is being accessed by an active process.";
3095 case NERR_LocalDrive:
3096 return "The drive letter is in use locally.";
3097 case NERR_AlertExists:
3098 return "The specified client is already registered for the specified event.";
3099 case NERR_TooManyAlerts:
3100 return "The alert table is full.";
3101 case NERR_NoSuchAlert:
3102 return "An invalid or nonexistent alert name was raised.";
3103 case NERR_BadRecipient:
3104 return "The alert recipient is invalid.";
3105 case NERR_AcctLimitExceeded:
3106 return "A user's session with this server has been deleted.";
3107 case NERR_InvalidLogSeek:
3108 return "The log file does not contain the requested record number.";
3109 case NERR_BadUasConfig:
3110 return "The user accounts database is not configured correctly.";
3111 case NERR_InvalidUASOp:
3112 return "This operation is not permitted when the Netlogon service is running.";
3113 case NERR_LastAdmin:
3114 return "This operation is not allowed on the last administrative account.";
3115 case NERR_DCNotFound:
3116 return "Could not find domain controller for this domain.";
3117 case NERR_LogonTrackingError:
3118 return "Could not set logon information for this user.";
3119 case NERR_NetlogonNotStarted:
3120 return "The Netlogon service has not been started.";
3121 case NERR_CanNotGrowUASFile:
3122 return "Unable to add to the user accounts database.";
3123 case NERR_TimeDiffAtDC:
3124 return "This server's clock is not synchronized with the primary domain controller's clock.";
3125 case NERR_PasswordMismatch:
3126 return "A password mismatch has been detected.";
3127 case NERR_NoSuchServer:
3128 return "The server identification does not specify a valid server.";
3129 case NERR_NoSuchSession:
3130 return "The session identification does not specify a valid session.";
3131 case NERR_NoSuchConnection:
3132 return "The connection identification does not specify a valid connection.";
3133 case NERR_TooManyServers:
3134 return "There is no space for another entry in the table of available servers.";
3135 case NERR_TooManySessions:
3136 return "The server has reached the maximum number of sessions it supports.";
3137 case NERR_TooManyConnections:
3138 return "The server has reached the maximum number of connections it supports.";
3139 case NERR_TooManyFiles:
3140 return "The server cannot open more files because it has reached its maximum number.";
3141 case NERR_NoAlternateServers:
3142 return "There are no alternate servers registered on this server.";
3143 case NERR_TryDownLevel:
3144 return "Try down-level (remote admin protocol) version of API instead.";
3145 case NERR_UPSDriverNotStarted:
3146 return "The UPS driver could not be accessed by the UPS service.";
3147 case NERR_UPSInvalidConfig:
3148 return "The UPS service is not configured correctly.";
3149 case NERR_UPSInvalidCommPort:
3150 return "The UPS service could not access the specified Comm Port.";
3151 case NERR_UPSSignalAsserted:
3152 return "The UPS indicated a line fail or low battery situation. Service not started.";
3153 case NERR_UPSShutdownFailed:
3154 return "The UPS service failed to perform a system shut down.";
3155 case NERR_BadDosRetCode:
3156 return "The program below returned an MS-DOS error code:";
3157 case NERR_ProgNeedsExtraMem:
3158 return "The program below needs more memory:";
3159 case NERR_BadDosFunction:
3160 return "The program below called an unsupported MS-DOS function:";
3161 case NERR_RemoteBootFailed:
3162 return "The workstation failed to boot.";
3163 case NERR_BadFileCheckSum:
3164 return "The file below is corrupt.";
3165 case NERR_NoRplBootSystem:
3166 return "No loader is specified in the boot-block definition file.";
3167 case NERR_RplLoadrNetBiosErr:
3168 return "NetBIOS returned an error: The NCB and SMB are dumped above.";
3169 case NERR_RplLoadrDiskErr:
3170 return "A disk I/O error occurred.";
3171 case NERR_ImageParamErr:
3172 return "Image parameter substitution failed.";
3173 case NERR_TooManyImageParams:
3174 return "Too many image parameters cross disk sector boundaries.";
3175 case NERR_NonDosFloppyUsed:
3176 return "The image was not generated from an MS-DOS diskette formatted with /S.";
3177 case NERR_RplBootRestart:
3178 return "Remote boot will be restarted later.";
3179 case NERR_RplSrvrCallFailed:
3180 return "The call to the Remoteboot server failed.";
3181 case NERR_CantConnectRplSrvr:
3182 return "Cannot connect to the Remoteboot server.";
3183 case NERR_CantOpenImageFile:
3184 return "Cannot open image file on the Remoteboot server.";
3185 case NERR_CallingRplSrvr:
3186 return "Connecting to the Remoteboot server...";
3187 case NERR_StartingRplBoot:
3188 return "Connecting to the Remoteboot server...";
3189 case NERR_RplBootServiceTerm:
3190 return "Remote boot service was stopped; check the error log for the cause of the problem.";
3191 case NERR_RplBootStartFailed:
3192 return "Remote boot startup failed; check the error log for the cause of the problem.";
3193 case NERR_RPL_CONNECTED:
3194 return "A second connection to a Remoteboot resource is not allowed.";
3195 case NERR_BrowserConfiguredToNotRun:
3196 return "The browser service was configured with MaintainServerList=No.";
3197 case NERR_RplNoAdaptersStarted:
3198 return "Service failed to start since none of the network adapters started with this service.";
3199 case NERR_RplBadRegistry:
3200 return "Service failed to start due to bad startup information in the registry.";
3201 case NERR_RplBadDatabase:
3202 return "Service failed to start because its database is absent or corrupt.";
3203 case NERR_RplRplfilesShare:
3204 return "Service failed to start because RPLFILES share is absent.";
3205 case NERR_RplNotRplServer:
3206 return "Service failed to start because RPLUSER group is absent.";
3207 case NERR_RplCannotEnum:
3208 return "Cannot enumerate service records.";
3209 case NERR_RplWkstaInfoCorrupted:
3210 return "Workstation record information has been corrupted.";
3211 case NERR_RplWkstaNotFound:
3212 return "Workstation record was not found.";
3213 case NERR_RplWkstaNameUnavailable:
3214 return "Workstation name is in use by some other workstation.";
3215 case NERR_RplProfileInfoCorrupted:
3216 return "Profile record information has been corrupted.";
3217 case NERR_RplProfileNotFound:
3218 return "Profile record was not found.";
3219 case NERR_RplProfileNameUnavailable:
3220 return "Profile name is in use by some other profile.";
3221 case NERR_RplProfileNotEmpty:
3222 return "There are workstations using this profile.";
3223 case NERR_RplConfigInfoCorrupted:
3224 return "Configuration record information has been corrupted.";
3225 case NERR_RplConfigNotFound:
3226 return "Configuration record was not found.";
3227 case NERR_RplAdapterInfoCorrupted:
3228 return "Adapter ID record information has been corrupted.";
3229 case NERR_RplInternal:
3230 return "An internal service error has occurred.";
3231 case NERR_RplVendorInfoCorrupted:
3232 return "Vendor ID record information has been corrupted.";
3233 case NERR_RplBootInfoCorrupted:
3234 return "Boot block record information has been corrupted.";
3235 case NERR_RplWkstaNeedsUserAcct:
3236 return "The user account for this workstation record is missing.";
3237 case NERR_RplNeedsRPLUSERAcct:
3238 return "The RPLUSER local group could not be found.";
3239 case NERR_RplBootNotFound:
3240 return "Boot block record was not found.";
3241 case NERR_RplIncompatibleProfile:
3242 return "Chosen profile is incompatible with this workstation.";
3243 case NERR_RplAdapterNameUnavailable:
3244 return "Chosen network adapter ID is in use by some other workstation.";
3245 case NERR_RplConfigNotEmpty:
3246 return "There are profiles using this configuration.";
3247 case NERR_RplBootInUse:
3248 return "There are workstations, profiles, or configurations using this boot block.";
3249 case NERR_RplBackupDatabase:
3250 return "Service failed to backup Remoteboot database.";
3251 case NERR_RplAdapterNotFound:
3252 return "Adapter record was not found.";
3253 case NERR_RplVendorNotFound:
3254 return "Vendor record was not found.";
3255 case NERR_RplVendorNameUnavailable:
3256 return "Vendor name is in use by some other vendor record.";
3257 case NERR_RplBootNameUnavailable:
3258 return "(boot name, vendor ID) is in use by some other boot block record.";
3259 case NERR_RplConfigNameUnavailable:
3260 return "Configuration name is in use by some other configuration.";
3261 case NERR_DfsInternalCorruption:
3262 return "The internal database maintained by the Dfs service is corrupt.";
3263 case NERR_DfsVolumeDataCorrupt:
3264 return "One of the records in the internal Dfs database is corrupt.";
3265 case NERR_DfsNoSuchVolume:
3266 return "There is no DFS name whose entry path matches the input Entry Path.";
3267 case NERR_DfsVolumeAlreadyExists:
3268 return "A root or link with the given name already exists.";
3269 case NERR_DfsAlreadyShared:
3270 return "The server share specified is already shared in the Dfs.";
3271 case NERR_DfsNoSuchShare:
3272 return "The indicated server share does not support the indicated DFS namespace.";
3273 case NERR_DfsNotALeafVolume:
3274 return "The operation is not valid on this portion of the namespace.";
3275 case NERR_DfsLeafVolume:
3276 return "The operation is not valid on this portion of the namespace.";
3277 case NERR_DfsVolumeHasMultipleServers:
3278 return "The operation is ambiguous because the link has multiple servers.";
3279 case NERR_DfsCantCreateJunctionPoint:
3280 return "Unable to create a link.";
3281 case NERR_DfsServerNotDfsAware:
3282 return "The server is not Dfs Aware.";
3283 case NERR_DfsBadRenamePath:
3284 return "The specified rename target path is invalid.";
3285 case NERR_DfsVolumeIsOffline:
3286 return "The specified DFS link is offline.";
3287 case NERR_DfsNoSuchServer:
3288 return "The specified server is not a server for this link.";
3289 case NERR_DfsCyclicalName:
3290 return "A cycle in the Dfs name was detected.";
3291 case NERR_DfsNotSupportedInServerDfs:
3292 return "The operation is not supported on a server-based Dfs.";
3293 case NERR_DfsDuplicateService:
3294 return "This link is already supported by the specified server-share.";
3295 case NERR_DfsCantRemoveLastServerShare:
3296 return "Can't remove the last server-share supporting this root or link.";
3297 case NERR_DfsVolumeIsInterDfs:
3298 return "The operation is not supported for an Inter-DFS link.";
3299 case NERR_DfsInconsistent:
3300 return "The internal state of the Dfs Service has become inconsistent.";
3301 case NERR_DfsServerUpgraded:
3302 return "The Dfs Service has been installed on the specified server.";
3303 case NERR_DfsDataIsIdentical:
3304 return "The Dfs data being reconciled is identical.";
3305 case NERR_DfsCantRemoveDfsRoot:
3306 return "The DFS root cannot be deleted. Uninstall DFS if required.";
3307 case NERR_DfsChildOrParentInDfs:
3308 return "A child or parent directory of the share is already in a Dfs.";
3309 case NERR_DfsInternalError:
3310 return "Dfs internal error.";
3311 /* the following are not defined in mingw */
3314 case NERR_SetupAlreadyJoined:
3315 return "This machine is already joined to a domain.";
3316 case NERR_SetupNotJoined:
3317 return "This machine is not currently joined to a domain.";
3318 case NERR_SetupDomainController:
3319 return "This machine is a domain controller and cannot be unjoined from a domain.";
3320 case NERR_DefaultJoinRequired:
3321 return "The destination domain controller does not support creating machine accounts in OUs.";
3322 case NERR_InvalidWorkgroupName:
3323 return "The specified workgroup name is invalid.";
3324 case NERR_NameUsesIncompatibleCodePage:
3325 return "The specified computer name is incompatible with the default language used on the domain controller.";
3326 case NERR_ComputerAccountNotFound:
3327 return "The specified computer account could not be found.";
3328 case NERR_PersonalSku:
3329 return "This version of Windows cannot be joined to a domain.";
3330 case NERR_PasswordMustChange:
3331 return "The password must change at the next logon.";
3332 case NERR_AccountLockedOut:
3333 return "The account is locked out.";
3334 case NERR_PasswordTooLong:
3335 return "The password is too long.";
3336 case NERR_PasswordNotComplexEnough:
3337 return "The password does not meet the complexity policy.";
3338 case NERR_PasswordFilterError:
3339 return "The password does not meet the requirements of the password filter DLLs.";
3343 msg = strerror (error_number);
3359 /******************************************************************************
3361 Original CVS version of dbus-sysdeps.c
3363 ******************************************************************************/
3364 /* -*- mode: C; c-file-style: "gnu" -*- */
3365 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-Bus implementation)
3367 * Copyright (C) 2002, 2003 Red Hat, Inc.
3368 * Copyright (C) 2003 CodeFactory AB
3369 * Copyright (C) 2005 Novell, Inc.
3371 * Licensed under the Academic Free License version 2.1
3373 * This program is free software; you can redistribute it and/or modify
3374 * it under the terms of the GNU General Public License as published by
3375 * the Free Software Foundation; either version 2 of the License, or
3376 * (at your option) any later version.
3378 * This program is distributed in the hope that it will be useful,
3379 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3380 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3381 * GNU General Public License for more details.
3383 * You should have received a copy of the GNU General Public License
3384 * along with this program; if not, write to the Free Software
3385 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3391 * @addtogroup DBusInternalsUtils
3395 int _dbus_mkdir (const char *path,
3398 return _mkdir(path);
3402 * Exit the process, returning the given value.
3404 * @param code the exit code
3407 _dbus_exit (int code)
3413 * Creates a socket and connects to a socket at the given host
3414 * and port. The connection fd is returned, and is set up as
3417 * @param host the host name to connect to, NULL for loopback
3418 * @param port the prot to connect to
3419 * @param error return location for error code
3420 * @returns connection file descriptor or -1 on error
3423 _dbus_connect_tcp_socket (const char *host,
3429 struct sockaddr_in addr;
3431 struct in_addr *haddr;
3434 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3436 _dbus_win_startup_winsock ();
3438 s.fd = socket (AF_INET, SOCK_STREAM, 0);
3440 if (DBUS_SOCKET_IS_INVALID (s.fd))
3442 DBUS_SOCKET_SET_ERRNO ();
3443 dbus_set_error (error,
3444 _dbus_error_from_errno (errno),
3445 "Failed to create socket: %s",
3446 _dbus_strerror (errno));
3454 ina.s_addr = htonl (INADDR_LOOPBACK);
3458 he = gethostbyname (host);
3461 DBUS_SOCKET_SET_ERRNO ();
3462 dbus_set_error (error,
3463 _dbus_error_from_errno (errno),
3464 "Failed to lookup hostname: %s",
3466 DBUS_CLOSE_SOCKET (s.fd);
3470 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3473 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
3474 addr.sin_family = AF_INET;
3475 addr.sin_port = htons (port);
3477 if (DBUS_SOCKET_API_RETURNS_ERROR
3478 (connect (s.fd, (struct sockaddr*) &addr, sizeof (addr)) < 0))
3480 DBUS_SOCKET_SET_ERRNO ();
3481 dbus_set_error (error,
3482 _dbus_error_from_errno (errno),
3483 "Failed to connect to socket %s:%d %s",
3484 host, port, _dbus_strerror (errno));
3486 DBUS_CLOSE_SOCKET (s.fd);
3492 handle = _dbus_socket_to_handle (&s);
3494 if (!_dbus_set_fd_nonblocking (handle, error))
3496 _dbus_close_socket (handle, NULL);
3506 _dbus_daemon_init(const char *host, dbus_uint32_t port);
3508 * Creates a socket and binds it to the given port,
3509 * then listens on the socket. The socket is
3510 * set to be nonblocking.
3511 * In case of port=0 a random free port is used and
3512 * returned in the port parameter.
3514 * @param host the interface to listen on, NULL for loopback, empty for any
3515 * @param port the port to listen on, if zero a free port will be used
3516 * @param error return location for errors
3517 * @returns the listening file descriptor or -1 on error
3521 _dbus_listen_tcp_socket (const char *host,
3522 dbus_uint32_t *port,
3527 struct sockaddr_in addr;
3529 struct in_addr *haddr;
3530 socklen_t len = (socklen_t) sizeof (struct sockaddr);
3534 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3536 _dbus_win_startup_winsock ();
3538 slisten.fd = socket (AF_INET, SOCK_STREAM, 0);
3540 if (DBUS_SOCKET_IS_INVALID (slisten.fd))
3542 DBUS_SOCKET_SET_ERRNO ();
3543 dbus_set_error (error, _dbus_error_from_errno (errno),
3544 "Failed to create socket \"%s:%d\": %s",
3545 host, port, _dbus_strerror (errno));
3551 ina.s_addr = htonl (INADDR_LOOPBACK);
3556 ina.s_addr = htonl (INADDR_ANY);
3561 he = gethostbyname (host);
3564 DBUS_SOCKET_SET_ERRNO ();
3565 dbus_set_error (error,
3566 _dbus_error_from_errno (errno),
3567 "Failed to lookup hostname: %s",
3569 DBUS_CLOSE_SOCKET (slisten.fd);
3573 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3577 memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
3578 addr.sin_family = AF_INET;
3579 addr.sin_port = htons (*port);
3581 if (bind (slisten.fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
3583 DBUS_SOCKET_SET_ERRNO ();
3584 dbus_set_error (error, _dbus_error_from_errno (errno),
3585 "Failed to bind socket \"%s:%d\": %s",
3586 host, *port, _dbus_strerror (errno));
3587 DBUS_CLOSE_SOCKET (slisten.fd);
3591 if (DBUS_SOCKET_API_RETURNS_ERROR (listen (slisten.fd, 30 /* backlog */)))
3593 DBUS_SOCKET_SET_ERRNO ();
3594 dbus_set_error (error, _dbus_error_from_errno (errno),
3595 "Failed to listen on socket \"%s:%d\": %s",
3596 host, *port, _dbus_strerror (errno));
3597 DBUS_CLOSE_SOCKET (slisten.fd);
3601 getsockname(slisten.fd, (struct sockaddr*) &addr, &len);
3602 *port = (dbus_uint32_t) ntohs(addr.sin_port);
3604 _dbus_daemon_init(host, ntohs(addr.sin_port));
3606 handle = _dbus_socket_to_handle (&slisten);
3608 if (!_dbus_set_fd_nonblocking (handle, error))
3610 _dbus_close_socket (handle, NULL);
3618 * Accepts a connection on a listening socket.
3619 * Handles EINTR for you.
3621 * @param listen_fd the listen file descriptor
3622 * @returns the connection fd of the client, or -1 on error
3625 _dbus_accept (int listen_handle)
3627 DBusSocket *slisten;
3629 struct sockaddr addr;
3632 _dbus_handle_to_socket(listen_handle, &slisten);
3634 addrlen = sizeof (addr);
3636 //FIXME: why do we not try it again on Windows?
3637 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3641 sclient.fd = accept (slisten->fd, &addr, &addrlen);
3643 if (DBUS_SOCKET_IS_INVALID (sclient.fd))
3645 DBUS_SOCKET_SET_ERRNO ();
3646 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3654 return _dbus_socket_to_handle (&sclient);
3660 write_credentials_byte (int server_fd,
3663 /* FIXME: for the session bus credentials shouldn't matter (?), but
3664 * for the system bus they are presumably essential. A rough outline
3665 * of a way to implement the credential transfer would be this:
3667 * client waits to *read* a byte.
3669 * server creates a named pipe with a random name, sends a byte
3670 * contining its length, and its name.
3672 * client reads the name, connects to it (using Win32 API).
3674 * server waits for connection to the named pipe, then calls
3675 * ImpersonateNamedPipeClient(), notes its now-current credentials,
3676 * calls RevertToSelf(), closes its handles to the named pipe, and
3677 * is done. (Maybe there is some other way to get the SID of a named
3678 * pipe client without having to use impersonation?)
3680 * client closes its handles and is done.
3688 * Reads a single byte which must be nul (an error occurs otherwise),
3689 * and reads unix credentials if available. Fills in pid/uid/gid with
3690 * -1 if no credentials are available. Return value indicates whether
3691 * a byte was read, not whether we got valid credentials. On some
3692 * systems, such as Linux, reading/writing the byte isn't actually
3693 * required, but we do it anyway just to avoid multiple codepaths.
3695 * Fails if no byte is available, so you must select() first.
3697 * The point of the byte is that on some systems we have to
3698 * use sendmsg()/recvmsg() to transmit credentials.
3700 * @param client_fd the client file descriptor
3701 * @param credentials struct to fill with credentials of client
3702 * @param error location to store error code
3703 * @returns #TRUE on success
3706 _dbus_read_credentials_unix_socket (int client_fd,
3707 DBusCredentials *credentials,
3710 /* FIXME bogus testing credentials */
3711 _dbus_credentials_from_current_process (credentials);
3717 * Checks to make sure the given directory is
3718 * private to the user
3720 * @param dir the name of the directory
3721 * @param error error return
3722 * @returns #FALSE on failure
3725 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
3727 const char *directory;
3730 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3737 * Gets user info for the given user ID.
3739 * @param info user info object to initialize
3740 * @param uid the user ID
3741 * @param error error return
3742 * @returns #TRUE on success
3745 _dbus_user_info_fill_uid (DBusUserInfo *info,
3749 return fill_user_info (info, uid,
3754 * Gets user info for the given username.
3756 * @param info user info object to initialize
3757 * @param username the username
3758 * @param error error return
3759 * @returns #TRUE on success
3762 _dbus_user_info_fill (DBusUserInfo *info,
3763 const DBusString *username,
3766 return fill_user_info (info, DBUS_UID_UNSET,
3772 fill_user_info (DBusUserInfo *info,
3774 const DBusString *username,
3777 const char *username_c;
3779 /* exactly one of username/uid provided */
3780 _dbus_assert (username != NULL || uid != DBUS_UID_UNSET);
3781 _dbus_assert (username == NULL || uid == DBUS_UID_UNSET);
3783 info->uid = DBUS_UID_UNSET;
3784 info->primary_gid = DBUS_GID_UNSET;
3785 info->group_ids = NULL;
3786 info->n_group_ids = 0;
3787 info->username = NULL;
3788 info->homedir = NULL;
3790 if (username != NULL)
3791 username_c = _dbus_string_get_const_data (username);
3795 if (uid != DBUS_UID_UNSET)
3797 if (!fill_win_user_info_from_uid (uid, info, error))
3799 _dbus_verbose("%s after fill_win_user_info_from_uid\n",__FUNCTION__);
3805 wchar_t *wname = _dbus_win_utf8_to_utf16 (username_c, error);
3810 if (!fill_win_user_info_from_name (wname, info, error))
3823 * Appends the given filename to the given directory.
3825 * @todo it might be cute to collapse multiple '/' such as "foo//"
3828 * @param dir the directory name
3829 * @param next_component the filename
3830 * @returns #TRUE on success
3833 _dbus_concat_dir_and_file (DBusString *dir,
3834 const DBusString *next_component)
3836 dbus_bool_t dir_ends_in_slash;
3837 dbus_bool_t file_starts_with_slash;
3839 if (_dbus_string_get_length (dir) == 0 ||
3840 _dbus_string_get_length (next_component) == 0)
3844 ('/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1) ||
3845 '\\' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1));
3847 file_starts_with_slash =
3848 ('/' == _dbus_string_get_byte (next_component, 0) ||
3849 '\\' == _dbus_string_get_byte (next_component, 0));
3851 if (dir_ends_in_slash && file_starts_with_slash)
3853 _dbus_string_shorten (dir, 1);
3855 else if (!(dir_ends_in_slash || file_starts_with_slash))
3857 if (!_dbus_string_append_byte (dir, '\\'))
3861 return _dbus_string_copy (next_component, 0, dir,
3862 _dbus_string_get_length (dir));
3869 * Gets our process ID
3870 * @returns process ID
3875 return GetCurrentProcessId ();
3878 /** nanoseconds in a second */
3879 #define NANOSECONDS_PER_SECOND 1000000000
3880 /** microseconds in a second */
3881 #define MICROSECONDS_PER_SECOND 1000000
3882 /** milliseconds in a second */
3883 #define MILLISECONDS_PER_SECOND 1000
3884 /** nanoseconds in a millisecond */
3885 #define NANOSECONDS_PER_MILLISECOND 1000000
3886 /** microseconds in a millisecond */
3887 #define MICROSECONDS_PER_MILLISECOND 1000
3890 * Sleeps the given number of milliseconds.
3891 * @param milliseconds number of milliseconds
3894 _dbus_sleep_milliseconds (int milliseconds)
3896 Sleep (milliseconds);
3901 * Get current time, as in gettimeofday().
3903 * @param tv_sec return location for number of seconds
3904 * @param tv_usec return location for number of microseconds
3907 _dbus_get_current_time (long *tv_sec,
3911 dbus_uint64_t *time64 = (dbus_uint64_t *) &ft;
3913 GetSystemTimeAsFileTime (&ft);
3915 /* Convert from 100s of nanoseconds since 1601-01-01
3916 * to Unix epoch. Yes, this is Y2038 unsafe.
3918 *time64 -= DBUS_INT64_CONSTANT (116444736000000000);
3922 *tv_sec = *time64 / 1000000;
3925 *tv_usec = *time64 % 1000000;
3930 * signal (SIGPIPE, SIG_IGN);
3933 _dbus_disable_sigpipe (void)
3935 _dbus_verbose("FIXME: implement _dbus_disable_sigpipe (void)\n");
3939 * Gets the credentials of the current process.
3941 * @param credentials credentials to fill in.
3944 _dbus_credentials_from_current_process (DBusCredentials *credentials)
3946 credentials->pid = _dbus_getpid ();
3947 credentials->uid = _dbus_getuid ();
3948 credentials->gid = _dbus_getgid ();
3952 * Appends the contents of the given file to the string,
3953 * returning error code. At the moment, won't open a file
3954 * more than a megabyte in size.
3956 * @param str the string to append to
3957 * @param filename filename to load
3958 * @param error place to set an error
3959 * @returns #FALSE if error was set
3962 _dbus_file_get_contents (DBusString *str,
3963 const DBusString *filename,
3970 const char *filename_c;
3972 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3974 filename_c = _dbus_string_get_const_data (filename);
3976 /* O_BINARY useful on Cygwin and Win32 */
3977 if (!_dbus_open_file (&file, filename_c, O_RDONLY | O_BINARY, -1))
3979 dbus_set_error (error, _dbus_error_from_errno (errno),
3980 "Failed to open \"%s\": %s",
3982 _dbus_strerror (errno));
3986 if (!_dbus_fstat (&file, &sb))
3988 dbus_set_error (error, _dbus_error_from_errno (errno),
3989 "Failed to stat \"%s\": %s",
3991 _dbus_strerror (errno));
3993 _dbus_verbose ("fstat() failed: %s",
3994 _dbus_strerror (errno));
3996 _dbus_close_file (&file, NULL);
4001 if (sb.st_size > _DBUS_ONE_MEGABYTE)
4003 dbus_set_error (error, DBUS_ERROR_FAILED,
4004 "File size %lu of \"%s\" is too large.",
4005 (unsigned long) sb.st_size, filename_c);
4006 _dbus_close_file (&file, NULL);
4011 orig_len = _dbus_string_get_length (str);
4012 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
4016 while (total < (int) sb.st_size)
4018 bytes_read = _dbus_read_file (&file, str,
4019 sb.st_size - total);
4020 if (bytes_read <= 0)
4022 dbus_set_error (error, _dbus_error_from_errno (errno),
4023 "Error reading \"%s\": %s",
4025 _dbus_strerror (errno));
4027 _dbus_verbose ("read() failed: %s",
4028 _dbus_strerror (errno));
4030 _dbus_close_file (&file, NULL);
4031 _dbus_string_set_length (str, orig_len);
4035 total += bytes_read;
4038 _dbus_close_file (&file, NULL);
4041 else if (sb.st_size != 0)
4043 _dbus_verbose ("Can only open regular files at the moment.\n");
4044 dbus_set_error (error, DBUS_ERROR_FAILED,
4045 "\"%s\" is not a regular file",
4047 _dbus_close_file (&file, NULL);
4052 _dbus_close_file (&file, NULL);
4058 * Writes a string out to a file. If the file exists,
4059 * it will be atomically overwritten by the new data.
4061 * @param str the string to write out
4062 * @param filename the file to save string to
4063 * @param error error to be filled in on failure
4064 * @returns #FALSE on failure
4067 _dbus_string_save_to_file (const DBusString *str,
4068 const DBusString *filename,
4073 const char *filename_c;
4074 DBusString tmp_filename;
4075 const char *tmp_filename_c;
4077 dbus_bool_t need_unlink;
4080 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4083 need_unlink = FALSE;
4085 if (!_dbus_string_init (&tmp_filename))
4087 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4091 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
4093 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4094 _dbus_string_free (&tmp_filename);
4098 if (!_dbus_string_append (&tmp_filename, "."))
4100 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4101 _dbus_string_free (&tmp_filename);
4105 #define N_TMP_FILENAME_RANDOM_BYTES 8
4106 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
4108 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4109 _dbus_string_free (&tmp_filename);
4113 filename_c = _dbus_string_get_const_data (filename);
4114 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
4116 if (!_dbus_open_file (&file, tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4119 dbus_set_error (error, _dbus_error_from_errno (errno),
4120 "Could not create %s: %s", tmp_filename_c,
4121 _dbus_strerror (errno));
4128 bytes_to_write = _dbus_string_get_length (str);
4130 while (total < bytes_to_write)
4134 bytes_written = _dbus_write_file (&file, str, total,
4135 bytes_to_write - total);
4137 if (bytes_written <= 0)
4139 dbus_set_error (error, _dbus_error_from_errno (errno),
4140 "Could not write to %s: %s", tmp_filename_c,
4141 _dbus_strerror (errno));
4146 total += bytes_written;
4149 if (!_dbus_close_file (&file, NULL))
4151 dbus_set_error (error, _dbus_error_from_errno (errno),
4152 "Could not close file %s: %s",
4153 tmp_filename_c, _dbus_strerror (errno));
4159 if ((unlink (filename_c) == -1 && errno != ENOENT) ||
4160 rename (tmp_filename_c, filename_c) < 0)
4162 dbus_set_error (error, _dbus_error_from_errno (errno),
4163 "Could not rename %s to %s: %s",
4164 tmp_filename_c, filename_c,
4165 _dbus_strerror (errno));
4170 need_unlink = FALSE;
4175 /* close first, then unlink, to prevent ".nfs34234235" garbage
4179 if (_dbus_is_valid_file(&file))
4180 _dbus_close_file (&file, NULL);
4182 if (need_unlink && unlink (tmp_filename_c) < 0)
4183 _dbus_verbose ("Failed to unlink temp file %s: %s\n",
4184 tmp_filename_c, _dbus_strerror (errno));
4186 _dbus_string_free (&tmp_filename);
4189 _DBUS_ASSERT_ERROR_IS_SET (error);
4195 /** Creates the given file, failing if the file already exists.
4197 * @param filename the filename
4198 * @param error error location
4199 * @returns #TRUE if we created the file and it didn't exist
4202 _dbus_create_file_exclusively (const DBusString *filename,
4206 const char *filename_c;
4208 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4210 filename_c = _dbus_string_get_const_data (filename);
4212 if (!_dbus_open_file (&file, filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4215 dbus_set_error (error,
4217 "Could not create file %s: %s\n",
4219 _dbus_strerror (errno));
4223 if (!_dbus_close_file (&file, NULL))
4225 dbus_set_error (error,
4227 "Could not close file %s: %s\n",
4229 _dbus_strerror (errno));
4238 * Creates a directory; succeeds if the directory
4239 * is created or already existed.
4241 * @param filename directory filename
4242 * @param error initialized error object
4243 * @returns #TRUE on success
4246 _dbus_create_directory (const DBusString *filename,
4249 const char *filename_c;
4251 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4253 filename_c = _dbus_string_get_const_data (filename);
4255 if (_dbus_mkdir (filename_c, 0700) < 0)
4257 if (errno == EEXIST)
4260 dbus_set_error (error, DBUS_ERROR_FAILED,
4261 "Failed to create directory %s: %s\n",
4262 filename_c, _dbus_strerror (errno));
4271 pseudorandom_generate_random_bytes_buffer (char *buffer,
4277 /* fall back to pseudorandom */
4278 _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
4281 _dbus_get_current_time (NULL, &tv_usec);
4291 b = (r / (double) RAND_MAX) * 255.0;
4300 pseudorandom_generate_random_bytes (DBusString *str,
4306 old_len = _dbus_string_get_length (str);
4308 if (!_dbus_string_lengthen (str, n_bytes))
4311 p = _dbus_string_get_data_len (str, old_len, n_bytes);
4313 pseudorandom_generate_random_bytes_buffer (p, n_bytes);
4319 * Gets the temporary files directory by inspecting the environment variables
4320 * TMPDIR, TMP, and TEMP in that order. If none of those are set "/tmp" is returned
4322 * @returns location of temp directory
4325 _dbus_get_tmpdir(void)
4327 static const char* tmpdir = NULL;
4332 tmpdir = getenv("TMP");
4334 tmpdir = getenv("TEMP");
4336 tmpdir = getenv("TMPDIR");
4338 tmpdir = "C:\\Temp";
4341 _dbus_assert(tmpdir != NULL);
4348 * Deletes the given file.
4350 * @param filename the filename
4351 * @param error error location
4353 * @returns #TRUE if unlink() succeeded
4356 _dbus_delete_file (const DBusString *filename,
4359 const char *filename_c;
4361 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4363 filename_c = _dbus_string_get_const_data (filename);
4365 if (unlink (filename_c) < 0)
4367 dbus_set_error (error, DBUS_ERROR_FAILED,
4368 "Failed to delete file %s: %s\n",
4369 filename_c, _dbus_strerror (errno));
4377 * Generates the given number of random bytes,
4378 * using the best mechanism we can come up with.
4380 * @param str the string
4381 * @param n_bytes the number of random bytes to append to string
4382 * @returns #TRUE on success, #FALSE if no memory
4385 _dbus_generate_random_bytes (DBusString *str,
4388 return pseudorandom_generate_random_bytes (str, n_bytes);
4391 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_BUILD_TESTS)
4403 * Backtrace Generator
4405 * Copyright 2004 Eric Poech
4406 * Copyright 2004 Robert Shearman
4408 * This library is free software; you can redistribute it and/or
4409 * modify it under the terms of the GNU Lesser General Public
4410 * License as published by the Free Software Foundation; either
4411 * version 2.1 of the License, or (at your option) any later version.
4413 * This library is distributed in the hope that it will be useful,
4414 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4415 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4416 * Lesser General Public License for more details.
4418 * You should have received a copy of the GNU Lesser General Public
4419 * License along with this library; if not, write to the Free Software
4420 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4424 #include <imagehlp.h>
4427 #define DPRINTF _dbus_warn
4435 //#define MAKE_FUNCPTR(f) static typeof(f) * p##f
4437 //MAKE_FUNCPTR(StackWalk);
4438 //MAKE_FUNCPTR(SymGetModuleBase);
4439 //MAKE_FUNCPTR(SymFunctionTableAccess);
4440 //MAKE_FUNCPTR(SymInitialize);
4441 //MAKE_FUNCPTR(SymGetSymFromAddr);
4442 //MAKE_FUNCPTR(SymGetModuleInfo);
4443 static BOOL (WINAPI *pStackWalk)(
4447 LPSTACKFRAME StackFrame,
4448 PVOID ContextRecord,
4449 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4450 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4451 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4452 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4454 static DWORD (WINAPI *pSymGetModuleBase)(
4458 static PVOID (WINAPI *pSymFunctionTableAccess)(
4462 static BOOL (WINAPI *pSymInitialize)(
4464 PSTR UserSearchPath,
4467 static BOOL (WINAPI *pSymGetSymFromAddr)(
4470 PDWORD Displacement,
4471 PIMAGEHLP_SYMBOL Symbol
4473 static BOOL (WINAPI *pSymGetModuleInfo)(
4476 PIMAGEHLP_MODULE ModuleInfo
4478 static DWORD (WINAPI *pSymSetOptions)(
4483 static BOOL init_backtrace()
4485 HMODULE hmodDbgHelp = LoadLibraryA("dbghelp");
4487 #define GETFUNC(x) \
4488 p##x = (typeof(x)*)GetProcAddress(hmodDbgHelp, #x); \
4496 // GETFUNC(StackWalk);
4497 // GETFUNC(SymGetModuleBase);
4498 // GETFUNC(SymFunctionTableAccess);
4499 // GETFUNC(SymInitialize);
4500 // GETFUNC(SymGetSymFromAddr);
4501 // GETFUNC(SymGetModuleInfo);
4505 pStackWalk = (BOOL (WINAPI *)(
4509 LPSTACKFRAME StackFrame,
4510 PVOID ContextRecord,
4511 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4512 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4513 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4514 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4515 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
4516 pSymGetModuleBase=(DWORD (WINAPI *)(
4519 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
4520 pSymFunctionTableAccess=(PVOID (WINAPI *)(
4523 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
4524 pSymInitialize = (BOOL (WINAPI *)(
4526 PSTR UserSearchPath,
4528 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
4529 pSymGetSymFromAddr = (BOOL (WINAPI *)(
4532 PDWORD Displacement,
4533 PIMAGEHLP_SYMBOL Symbol
4534 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
4535 pSymGetModuleInfo = (BOOL (WINAPI *)(
4538 PIMAGEHLP_MODULE ModuleInfo
4539 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
4540 pSymSetOptions = (DWORD (WINAPI *)(
4542 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
4545 pSymSetOptions(SYMOPT_UNDNAME);
4547 pSymInitialize(GetCurrentProcess(), NULL, TRUE);
4552 static void dump_backtrace_for_thread(HANDLE hThread)
4559 if (!init_backtrace())
4562 /* can't use this function for current thread as GetThreadContext
4563 * doesn't support getting context from current thread */
4564 if (hThread == GetCurrentThread())
4567 DPRINTF("Backtrace:\n");
4569 memset(&context, 0, sizeof(context));
4570 context.ContextFlags = CONTEXT_FULL;
4572 SuspendThread(hThread);
4574 if (!GetThreadContext(hThread, &context))
4576 DPRINTF("Couldn't get thread context (error %ld)\n", GetLastError());
4577 ResumeThread(hThread);
4581 memset(&sf, 0, sizeof(sf));
4584 sf.AddrFrame.Offset = context.Ebp;
4585 sf.AddrFrame.Mode = AddrModeFlat;
4586 sf.AddrPC.Offset = context.Eip;
4587 sf.AddrPC.Mode = AddrModeFlat;
4588 dwImageType = IMAGE_FILE_MACHINE_I386;
4590 # error You need to fill in the STACKFRAME structure for your architecture
4593 while (pStackWalk(dwImageType, GetCurrentProcess(),
4594 hThread, &sf, &context, NULL, pSymFunctionTableAccess,
4595 pSymGetModuleBase, NULL))
4598 IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
4599 DWORD dwDisplacement;
4601 pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
4602 pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1;
4604 if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
4605 &dwDisplacement, pSymbol))
4607 IMAGEHLP_MODULE ModuleInfo;
4608 ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
4610 if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
4612 DPRINTF("1\t%p\n", (void*)sf.AddrPC.Offset);
4614 DPRINTF("2\t%s+0x%lx\n", ModuleInfo.ImageName,
4615 sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
4617 else if (dwDisplacement)
4618 DPRINTF("3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
4620 DPRINTF("4\t%s\n", pSymbol->Name);
4623 ResumeThread(hThread);
4626 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
4628 dump_backtrace_for_thread((HANDLE)lpParameter);
4632 /* cannot get valid context from current thread, so we have to execute
4633 * backtrace from another thread */
4634 static void dump_backtrace()
4636 HANDLE hCurrentThread;
4639 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
4640 GetCurrentProcess(), &hCurrentThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
4641 hThread = CreateThread(NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
4643 WaitForSingleObject(hThread, INFINITE);
4644 CloseHandle(hThread);
4645 CloseHandle(hCurrentThread);
4648 void _dbus_print_backtrace(void)
4654 void _dbus_print_backtrace(void)
4656 _dbus_verbose (" D-Bus not compiled with backtrace support\n");
4661 * Sends a single nul byte with our UNIX credentials as ancillary
4662 * data. Returns #TRUE if the data was successfully written. On
4663 * systems that don't support sending credentials, just writes a byte,
4664 * doesn't send any credentials. On some systems, such as Linux,
4665 * reading/writing the byte isn't actually required, but we do it
4666 * anyway just to avoid multiple codepaths.
4668 * Fails if no byte can be written, so you must select() first.
4670 * The point of the byte is that on some systems we have to
4671 * use sendmsg()/recvmsg() to transmit credentials.
4673 * @param server_fd file descriptor for connection to server
4674 * @param error return location for error code
4675 * @returns #TRUE if the byte was sent
4678 _dbus_send_credentials_unix_socket (int server_fd,
4681 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4683 if (write_credentials_byte (server_fd, error))
4689 static dbus_uint32_t fromAscii(char ascii)
4691 if(ascii >= '0' && ascii <= '9')
4693 if(ascii >= 'A' && ascii <= 'F')
4694 return ascii - 'A' + 10;
4695 if(ascii >= 'a' && ascii <= 'f')
4696 return ascii - 'a' + 10;
4700 dbus_bool_t _dbus_read_local_machine_uuid (DBusGUID *machine_id,
4701 dbus_bool_t create_if_not_found,
4708 HW_PROFILE_INFOA info;
4709 char *lpc = &info.szHwProfileGuid[0];
4712 // the hw-profile guid lives long enough
4713 if(!GetCurrentHwProfileA(&info))
4715 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); // FIXME
4719 // Form: {12340001-4980-1920-6788-123456789012}
4722 u = ((fromAscii(lpc[0]) << 0) |
4723 (fromAscii(lpc[1]) << 4) |
4724 (fromAscii(lpc[2]) << 8) |
4725 (fromAscii(lpc[3]) << 12) |
4726 (fromAscii(lpc[4]) << 16) |
4727 (fromAscii(lpc[5]) << 20) |
4728 (fromAscii(lpc[6]) << 24) |
4729 (fromAscii(lpc[7]) << 28));
4730 machine_id->as_uint32s[0] = u;
4734 u = ((fromAscii(lpc[0]) << 0) |
4735 (fromAscii(lpc[1]) << 4) |
4736 (fromAscii(lpc[2]) << 8) |
4737 (fromAscii(lpc[3]) << 12) |
4738 (fromAscii(lpc[5]) << 16) |
4739 (fromAscii(lpc[6]) << 20) |
4740 (fromAscii(lpc[7]) << 24) |
4741 (fromAscii(lpc[8]) << 28));
4742 machine_id->as_uint32s[1] = u;
4746 u = ((fromAscii(lpc[0]) << 0) |
4747 (fromAscii(lpc[1]) << 4) |
4748 (fromAscii(lpc[2]) << 8) |
4749 (fromAscii(lpc[3]) << 12) |
4750 (fromAscii(lpc[5]) << 16) |
4751 (fromAscii(lpc[6]) << 20) |
4752 (fromAscii(lpc[7]) << 24) |
4753 (fromAscii(lpc[8]) << 28));
4754 machine_id->as_uint32s[2] = u;
4758 u = ((fromAscii(lpc[0]) << 0) |
4759 (fromAscii(lpc[1]) << 4) |
4760 (fromAscii(lpc[2]) << 8) |
4761 (fromAscii(lpc[3]) << 12) |
4762 (fromAscii(lpc[4]) << 16) |
4763 (fromAscii(lpc[5]) << 20) |
4764 (fromAscii(lpc[6]) << 24) |
4765 (fromAscii(lpc[7]) << 28));
4766 machine_id->as_uint32s[3] = u;
4772 HANDLE _dbus_global_lock (const char *mutexname)
4777 mutex = CreateMutex( NULL, FALSE, mutexname );
4783 gotMutex = WaitForSingleObject( mutex, INFINITE );
4786 case WAIT_ABANDONED:
4787 ReleaseMutex (mutex);
4788 CloseHandle (mutex);
4799 void _dbus_global_unlock (HANDLE mutex)
4801 ReleaseMutex (mutex);
4802 CloseHandle (mutex);
4805 // for proper cleanup in dbus-daemon
4806 static HANDLE hDBusDaemonMutex = NULL;
4807 static HANDLE hDBusSharedMem = NULL;
4808 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4809 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
4810 // sync _dbus_get_autolaunch_address
4811 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
4812 // mutex to determine if dbus-daemon is already started (per user)
4813 static const char *cDBusDaemonMutex = "DBusDaemonMutex";
4814 // named shm for dbus adress info (per user)
4815 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
4818 _dbus_daemon_init(const char *host, dbus_uint32_t port)
4821 const char *adr = NULL;
4822 char szUserName[64];
4823 DWORD dwUserNameSize = sizeof(szUserName);
4824 char szDBusDaemonMutex[128];
4825 char szDBusDaemonAddressInfo[128];
4826 char szAddress[128];
4831 _snprintf(szAddress, sizeof(szAddress) - 1, "tcp:host=%s,port=%d", host, port);
4833 _dbus_assert( GetUserName(szUserName, &dwUserNameSize) != 0);
4834 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4835 cDBusDaemonMutex, szUserName);
4836 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4837 cDBusDaemonAddressInfo, szUserName);
4839 // before _dbus_global_lock to keep correct lock/release order
4840 hDBusDaemonMutex = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4842 _dbus_assert(WaitForSingleObject( hDBusDaemonMutex, 1000 ) == WAIT_OBJECT_0);
4844 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4845 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4848 hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
4849 0, strlen( szAddress ) + 1, szDBusDaemonAddressInfo );
4850 _dbus_assert( hDBusSharedMem );
4852 adr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
4854 _dbus_assert( adr );
4856 strcpy(adr, szAddress);
4859 UnmapViewOfFile( adr );
4861 _dbus_global_unlock( lock );
4865 _dbus_daemon_release()
4869 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4870 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4872 CloseHandle( hDBusSharedMem );
4874 hDBusSharedMem = NULL;
4876 ReleaseMutex( hDBusDaemonMutex );
4878 CloseHandle( hDBusDaemonMutex );
4880 hDBusDaemonMutex = NULL;
4882 _dbus_global_unlock( lock );
4886 _dbus_get_autolaunch_shm(DBusString *adress)
4890 char szUserName[64];
4891 DWORD dwUserNameSize = sizeof(szUserName);
4892 char szDBusDaemonAddressInfo[128];
4894 if( !GetUserName(szUserName, &dwUserNameSize) )
4896 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4897 cDBusDaemonAddressInfo, szUserName);
4901 // we know that dbus-daemon is available, so we wait until shm is available
4902 sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, szDBusDaemonAddressInfo );
4903 if( sharedMem == 0 )
4905 } while( sharedMem == 0 );
4907 if( sharedMem == 0 )
4910 adr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
4915 _dbus_string_init( adress );
4917 _dbus_string_append( adress, adr );
4920 UnmapViewOfFile( adr );
4922 CloseHandle( sharedMem );
4928 _dbus_daemon_already_runs (DBusString *adress)
4932 dbus_bool_t bRet = TRUE;
4933 char szUserName[64];
4934 DWORD dwUserNameSize = sizeof(szUserName);
4935 char szDBusDaemonMutex[128];
4937 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4938 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4940 if( !GetUserName(szUserName, &dwUserNameSize) )
4942 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4943 cDBusDaemonMutex, szUserName);
4946 daemon = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4947 if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
4949 ReleaseMutex (daemon);
4950 CloseHandle (daemon);
4952 _dbus_global_unlock( lock );
4957 bRet = _dbus_get_autolaunch_shm( adress );
4960 CloseHandle ( daemon );
4962 _dbus_global_unlock( lock );
4968 _dbus_get_autolaunch_address (DBusString *address,
4973 PROCESS_INFORMATION pi;
4974 dbus_bool_t retval = FALSE;
4976 char dbus_exe_path[MAX_PATH];
4977 char dbus_args[MAX_PATH * 2];
4979 mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
4981 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4983 if (_dbus_daemon_already_runs(address))
4985 printf("dbus daemon already exists\n");
4990 if (!SearchPathA(NULL, "dbus-daemon.exe", NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
4992 printf ("could not find dbus-daemon executable\n");
4997 ZeroMemory( &si, sizeof(si) );
4999 ZeroMemory( &pi, sizeof(pi) );
5001 _snprintf(dbus_args, sizeof(dbus_args) - 1, "\"%s\" %s", dbus_exe_path, " --session");
5003 // argv[i] = "--config-file=bus\\session.conf";
5004 printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
5005 if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
5009 // Wait until started (see _dbus_get_autolaunch_shm())
5010 WaitForInputIdle(pi.hProcess, INFINITE);
5012 retval = _dbus_get_autolaunch_shm( address );
5019 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
5021 _DBUS_ASSERT_ERROR_IS_SET (error);
5023 _dbus_global_unlock (mutex);
5029 /** Makes the file readable by every user in the system.
5031 * @param filename the filename
5032 * @param error error location
5033 * @returns #TRUE if the file's permissions could be changed.
5036 _dbus_make_file_world_readable(const DBusString *filename,
5044 #define DBUS_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
5046 // @TODO: this function is duplicated from dbus-sysdeps-unix.c
5047 // and differs only in the path separator, may be we should
5048 // use a dbus path separator variable
5050 #define _dbus_path_seperator ";"
5053 split_paths_and_append (DBusString *dirs,
5055 DBusList **dir_list)
5061 const DBusString file_suffix;
5066 _dbus_string_init_const (&file_suffix, suffix);
5068 len = _dbus_string_get_length (dirs);
5070 while (_dbus_string_find (dirs, start, _dbus_path_seperator, &i))
5074 if (!_dbus_string_init (&path))
5077 if (!_dbus_string_copy_len (dirs,
5083 _dbus_string_free (&path);
5087 _dbus_string_chop_white (&path);
5089 /* check for an empty path */
5090 if (_dbus_string_get_length (&path) == 0)
5093 if (!_dbus_concat_dir_and_file (&path,
5096 _dbus_string_free (&path);
5100 if (!_dbus_string_copy_data(&path, &cpath))
5102 _dbus_string_free (&path);
5106 if (!_dbus_list_append (dir_list, cpath))
5108 _dbus_string_free (&path);
5114 _dbus_string_free (&path);
5122 if (!_dbus_string_init (&path))
5125 if (!_dbus_string_copy_len (dirs,
5131 _dbus_string_free (&path);
5135 if (!_dbus_concat_dir_and_file (&path,
5138 _dbus_string_free (&path);
5142 if (!_dbus_string_copy_data(&path, &cpath))
5144 _dbus_string_free (&path);
5148 if (!_dbus_list_append (dir_list, cpath))
5150 _dbus_string_free (&path);
5155 _dbus_string_free (&path);
5161 _dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL);
5162 _dbus_list_clear (dir_list);
5167 * Returns the standard directories for a session bus to look for service
5170 * On Windows this should be data directories:
5172 * %CommonProgramFiles%/dbus
5178 * @param dirs the directory list we are returning
5179 * @returns #FALSE on OOM
5183 _dbus_get_standard_session_servicedirs (DBusList **dirs)
5185 const char *common_progs;
5186 DBusString servicedir_path;
5188 if (!_dbus_string_init (&servicedir_path))
5191 if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR";"))
5194 common_progs = _dbus_getenv ("CommonProgramFiles");
5196 if (common_progs != NULL)
5198 if (!_dbus_string_append (&servicedir_path, common_progs))
5201 if (!_dbus_string_append (&servicedir_path, ";"))
5205 if (!split_paths_and_append (&servicedir_path,
5206 DBUS_STANDARD_SESSION_SERVICEDIR,
5210 _dbus_string_free (&servicedir_path);
5214 _dbus_string_free (&servicedir_path);
5218 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
5221 * Atomically increments an integer
5223 * @param atomic pointer to the integer to increment
5224 * @returns the value before incrementing
5228 _dbus_atomic_inc (DBusAtomic *atomic)
5230 // +/- 1 is needed here!
5231 return InterlockedIncrement (&atomic->value) - 1;
5235 * Atomically decrement an integer
5237 * @param atomic pointer to the integer to decrement
5238 * @returns the value before decrementing
5242 _dbus_atomic_dec (DBusAtomic *atomic)
5244 // +/- 1 is needed here!
5245 return InterlockedDecrement (&atomic->value) + 1;
5248 #endif /* asserts or tests enabled */
5250 /** @} end of sysdeps-win */
5252 /* tests in dbus-sysdeps-util.c */