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 _dbus_write_pipe (DBusPipe pipe,
271 const DBusString *buffer,
277 return _dbus_write_file(&file, buffer, start, len);
281 _dbus_read_pipe(DBusPipe pipe,
287 return _dbus_read_file(&file, buffer, count);
297 static DBusSocket *win_fds = NULL;
298 static int win_n_fds = 0; // is this the size? rename to win_fds_size? #
301 #define TO_HANDLE(n) ((n)^win32_encap_randomizer)
302 #define FROM_HANDLE(n) ((n)^win32_encap_randomizer)
304 #define TO_HANDLE(n) ((n)+0x10000000)
305 #define FROM_HANDLE(n) ((n)-0x10000000)
306 #define IS_HANDLE(n) ((n)&0x10000000)
312 _dbus_win_deallocate_fd (int fd)
314 _DBUS_LOCK (win_fds);
315 win_fds[FROM_HANDLE (fd)].is_used = 0;
316 _DBUS_UNLOCK (win_fds);
321 _dbus_win_allocate_fd (void)
325 _DBUS_LOCK (win_fds);
329 #ifdef _DBUS_WIN_USE_RANDOMIZER
334 /* Use malloc to avoid memory leak failure in dbus-test */
335 win_fds = malloc (win_n_fds * sizeof (*win_fds));
337 _dbus_assert (win_fds != NULL);
339 for (i = 0; i < win_n_fds; i++)
340 win_fds[i].is_used = 0;
342 #ifdef _DBUS_WIN_USE_RANDOMIZER
344 _dbus_string_init (&random);
345 _dbus_generate_random_bytes (&random, sizeof (int));
346 memmove (&win_encap_randomizer, _dbus_string_get_const_data (&random), sizeof (int));
347 win_encap_randomizer &= 0xFF;
348 _dbus_string_free (&random);
353 for (i = 0; i < win_n_fds && win_fds[i].is_used != 0; i++)
358 int oldn = win_n_fds;
362 win_fds = realloc (win_fds, win_n_fds * sizeof (*win_fds));
364 _dbus_assert (win_fds != NULL);
366 for (j = oldn; j < win_n_fds; j++)
367 win_fds[i].is_used = 0;
370 memset(&win_fds[i], 0, sizeof(win_fds[i]));
372 win_fds[i].is_used = 1;
374 win_fds[i].port_file_fd = -1;
375 win_fds[i].close_on_exec = FALSE;
376 win_fds[i].non_blocking = FALSE;
378 _DBUS_UNLOCK (win_fds);
385 _dbus_create_handle_from_socket (int s)
390 // check: parameter must be a valid value
391 _dbus_assert(s != -1);
392 _dbus_assert(!IS_HANDLE(s));
394 // get index of a new position in the map
395 i = _dbus_win_allocate_fd ();
397 // fill new posiiton in the map: value->index
399 win_fds[i].is_used = 1;
401 // create handle from the index: index->handle
402 handle = TO_HANDLE (i);
404 _dbus_verbose ("_dbus_create_handle_from_value, value: %d, handle: %d\n", s, handle);
410 _dbus_socket_to_handle (DBusSocket *s)
415 // check: parameter must be a valid socket
416 _dbus_assert(s != NULL);
417 _dbus_assert(s->fd != -1);
418 _dbus_assert(!IS_HANDLE(s->fd));
420 _DBUS_LOCK (win_fds);
422 // at the first call there is no win_fds
423 // will be constructed _dbus_create_handle_from_socket
424 // because handle = -1
427 // search for the value in the map
428 // find the index of the value: value->index
429 for (i = 0; i < win_n_fds; i++)
430 if (win_fds[i].is_used == 1 && win_fds[i].fd == s->fd)
432 // create handle from the index: index->handle
433 handle = TO_HANDLE (i);
437 _DBUS_UNLOCK (win_fds);
442 handle = _dbus_create_handle_from_socket(s->fd);
445 _dbus_assert(handle != -1);
452 _dbus_handle_to_socket_unlocked (int handle,
457 // check: parameter must be a valid handle
458 _dbus_assert(handle != -1);
459 _dbus_assert(IS_HANDLE(handle));
460 _dbus_assert(ptr != NULL);
462 // map from handle to index: handle->index
463 i = FROM_HANDLE (handle);
465 _dbus_assert (win_fds != NULL);
466 _dbus_assert (i >= 0 && i < win_n_fds);
468 // check for if fd is valid
469 _dbus_assert (win_fds[i].is_used == 1);
471 // get socket from index: index->socket
474 _dbus_verbose ("_dbus_socket_to_handle_unlocked: socket=%d, handle=%d, index=%d\n", (*ptr)->fd, handle, i);
478 _dbus_handle_to_socket (int handle,
481 _dbus_lock_sockets();
482 _dbus_handle_to_socket_unlocked (handle, ptr);
483 _dbus_unlock_sockets();
489 #define FROM_HANDLE(n) 1==DBUS_WIN_DONT_USE__FROM_HANDLE__DIRECTLY
490 #define win_fds 1==DBUS_WIN_DONT_USE_win_fds_DIRECTLY
496 * Thin wrapper around the read() system call that appends
497 * the data it reads to the DBusString buffer. It appends
498 * up to the given count, and returns the same value
499 * and same errno as read(). The only exception is that
500 * _dbus_read() handles EINTR for you. _dbus_read() can
501 * return ENOMEM, even though regular UNIX read doesn't.
503 * @param fd the file descriptor to read from
504 * @param buffer the buffer to append data to
505 * @param count the amount of data to read
506 * @returns the number of bytes read or -1
509 _dbus_read_socket (int handle,
518 _dbus_assert (count >= 0);
520 start = _dbus_string_get_length (buffer);
522 if (!_dbus_string_lengthen (buffer, count))
528 data = _dbus_string_get_data_len (buffer, start, count);
530 _dbus_handle_to_socket(handle, &s);
534 _dbus_verbose ("recv: count=%d socket=%d\n", count, s->fd);
535 bytes_read = recv (s->fd, data, count, 0);
536 if (bytes_read == SOCKET_ERROR)
538 DBUS_SOCKET_SET_ERRNO();
539 _dbus_verbose ("recv: failed: %s\n", _dbus_strerror (errno));
543 _dbus_verbose ("recv: = %d\n", bytes_read);
547 _dbus_assert_not_reached ("no valid socket");
552 /* put length back (note that this doesn't actually realloc anything) */
553 _dbus_string_set_length (buffer, start);
558 /* put length back (doesn't actually realloc) */
559 _dbus_string_set_length (buffer, start + bytes_read);
564 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
572 * Thin wrapper around the write() system call that writes a part of a
573 * DBusString and handles EINTR for you.
575 * @param fd the file descriptor to write
576 * @param buffer the buffer to write data from
577 * @param start the first byte in the buffer to write
578 * @param len the number of bytes to try to write
579 * @returns the number of bytes written or -1 on error
582 _dbus_write_socket (int handle,
583 const DBusString *buffer,
592 data = _dbus_string_get_const_data_len (buffer, start, len);
594 _dbus_handle_to_socket(handle, &s);
598 _dbus_verbose ("send: len=%d socket=%d\n", len, s->fd);
599 bytes_written = send (s->fd, data, len, 0);
600 if (bytes_written == SOCKET_ERROR)
602 DBUS_SOCKET_SET_ERRNO();
603 _dbus_verbose ("send: failed: %s\n", _dbus_strerror (errno));
607 _dbus_verbose ("send: = %d\n", bytes_written);
611 _dbus_assert_not_reached ("unhandled fd type");
615 if (bytes_written > 0)
616 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
619 return bytes_written;
624 * Closes a file descriptor.
626 * @param fd the file descriptor
627 * @param error error object
628 * @returns #FALSE if error set
631 _dbus_close_socket (int handle,
636 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
638 _dbus_lock_sockets();
640 _dbus_handle_to_socket_unlocked (handle, &s);
645 if (s->port_file_fd >= 0)
647 _chsize (s->port_file_fd, 0);
648 close (s->port_file_fd);
649 s->port_file_fd = -1;
650 unlink (_dbus_string_get_const_data (&s->port_file));
651 free ((char *) _dbus_string_get_const_data (&s->port_file));
654 if (closesocket (s->fd) == SOCKET_ERROR)
656 DBUS_SOCKET_SET_ERRNO ();
657 dbus_set_error (error, _dbus_error_from_errno (errno),
658 "Could not close socket: socket=%d, handle=%d, %s",
659 s->fd, handle, _dbus_strerror (errno));
660 _dbus_unlock_sockets();
663 _dbus_verbose ("_dbus_close_socket: socket=%d, handle=%d\n",
668 _dbus_assert_not_reached ("unhandled fd type");
671 _dbus_unlock_sockets();
673 _dbus_win_deallocate_fd (handle);
680 * Sets the file descriptor to be close
681 * on exec. Should be called for all file
682 * descriptors in D-Bus code.
684 * @param fd the file descriptor
687 _dbus_fd_set_close_on_exec (int handle)
693 _dbus_lock_sockets();
695 _dbus_handle_to_socket_unlocked (handle, &s);
696 s->close_on_exec = TRUE;
698 _dbus_unlock_sockets();
702 * Sets a file descriptor to be nonblocking.
704 * @param fd the file descriptor.
705 * @param error address of error location.
706 * @returns #TRUE on success.
709 _dbus_set_fd_nonblocking (int handle,
715 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
717 _dbus_lock_sockets();
719 _dbus_handle_to_socket_unlocked(handle, &s);
723 if (ioctlsocket (s->fd, FIONBIO, &one) == SOCKET_ERROR)
725 dbus_set_error (error, _dbus_error_from_errno (WSAGetLastError ()),
726 "Failed to set socket %d:%d to nonblocking: %s", s->fd,
727 _dbus_strerror (WSAGetLastError ()));
728 _dbus_unlock_sockets();
734 _dbus_assert_not_reached ("unhandled fd type");
737 _dbus_unlock_sockets();
744 * Like _dbus_write() but will use writev() if possible
745 * to write both buffers in sequence. The return value
746 * is the number of bytes written in the first buffer,
747 * plus the number written in the second. If the first
748 * buffer is written successfully and an error occurs
749 * writing the second, the number of bytes in the first
750 * is returned (i.e. the error is ignored), on systems that
751 * don't have writev. Handles EINTR for you.
752 * The second buffer may be #NULL.
754 * @param fd the file descriptor
755 * @param buffer1 first buffer
756 * @param start1 first byte to write in first buffer
757 * @param len1 number of bytes to write from first buffer
758 * @param buffer2 second buffer, or #NULL
759 * @param start2 first byte to write in second buffer
760 * @param len2 number of bytes to write in second buffer
761 * @returns total bytes written from both buffers, or -1 on error
764 _dbus_write_socket_two (int handle,
765 const DBusString *buffer1,
768 const DBusString *buffer2,
780 _dbus_assert (buffer1 != NULL);
781 _dbus_assert (start1 >= 0);
782 _dbus_assert (start2 >= 0);
783 _dbus_assert (len1 >= 0);
784 _dbus_assert (len2 >= 0);
786 _dbus_handle_to_socket(handle, &s);
788 data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
791 data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
801 vectors[0].buf = (char*) data1;
802 vectors[0].len = len1;
803 vectors[1].buf = (char*) data2;
804 vectors[1].len = len2;
806 _dbus_verbose ("WSASend: len1+2=%d+%d socket=%d\n", len1, len2, s->fd);
807 rc = WSASend (s->fd, vectors, data2 ? 2 : 1, &bytes_written,
811 DBUS_SOCKET_SET_ERRNO ();
812 _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror (errno));
816 _dbus_verbose ("WSASend: = %ld\n", bytes_written);
817 return bytes_written;
821 _dbus_assert_not_reached ("unhandled fd type");
827 * @def _DBUS_MAX_SUN_PATH_LENGTH
829 * Maximum length of the path to a UNIX domain socket,
830 * sockaddr_un::sun_path member. POSIX requires that all systems
831 * support at least 100 bytes here, including the nul termination.
832 * We use 99 for the max value to allow for the nul.
834 * We could probably also do sizeof (addr.sun_path)
835 * but this way we are the same on all platforms
836 * which is probably a good idea.
840 * Creates a socket and connects it to the UNIX domain socket at the
841 * given path. The connection fd is returned, and is set up as
844 * On Windows there are no UNIX domain sockets. Instead, connects to a
845 * localhost-bound TCP socket, whose port number is stored in a file
848 * Uses abstract sockets instead of filesystem-linked sockets if
849 * requested (it's possible only on Linux; see "man 7 unix" on Linux).
850 * On non-Linux abstract socket usage always fails.
852 * @param path the path to UNIX domain socket
853 * @param abstract #TRUE to use abstract namespace
854 * @param error return location for error code
855 * @returns connection file descriptor or -1 on error
858 _dbus_connect_unix_socket (const char *path,
859 dbus_bool_t abstract,
868 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
870 _dbus_verbose ("connecting to pseudo-unix socket at %s\n",
875 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
876 "Implementation does not support abstract socket namespace\n");
880 fd = _sopen (path, O_RDONLY, SH_DENYNO);
884 dbus_set_error (error, _dbus_error_from_errno (errno),
885 "Failed to open file %s: %s",
886 path, _dbus_strerror (errno));
890 n = read (fd, buf, sizeof (buf) - 1);
895 dbus_set_error (error, DBUS_ERROR_FAILED,
896 "Failed to read port number from file %s",
904 if (port <= 0 || port > 0xFFFF)
906 dbus_set_error (error, DBUS_ERROR_FAILED,
907 "Invalid port numer in file %s",
912 return _dbus_connect_tcp_socket (NULL, port, error);
918 * Creates a socket and binds it to the given path,
919 * then listens on the socket. The socket is
920 * set to be nonblocking.
922 * Uses abstract sockets instead of filesystem-linked
923 * sockets if requested (it's possible only on Linux;
924 * see "man 7 unix" on Linux).
925 * On non-Linux abstract socket usage always fails.
927 * @param path the socket name
928 * @param abstract #TRUE to use abstract namespace
929 * @param error return location for errors
930 * @returns the listening file descriptor or -1 on error
933 _dbus_listen_unix_socket (const char *path,
934 dbus_bool_t abstract,
948 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
950 _dbus_verbose ("listening on pseudo-unix socket at %s\n",
955 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
956 "Implementation does not support abstract socket namespace\n");
960 listen_handle = _dbus_listen_tcp_socket (NULL, 0, error);
962 if (listen_handle == -1)
965 _dbus_handle_to_socket(listen_handle, &s);
967 addr_len = sizeof (sa);
968 if (getsockname (s->fd, &sa, &addr_len) == SOCKET_ERROR)
970 DBUS_SOCKET_SET_ERRNO ();
971 dbus_set_error (error, _dbus_error_from_errno (errno),
972 "getsockname failed: %s",
973 _dbus_strerror (errno));
974 _dbus_close_socket (listen_handle, NULL);
978 _dbus_assert (((struct sockaddr_in*) &sa)->sin_family == AF_INET);
980 filefd = _sopen (path, O_CREAT|O_WRONLY|_O_SHORT_LIVED, SH_DENYWR, 0666);
984 dbus_set_error (error, _dbus_error_from_errno (errno),
985 "Failed to create pseudo-unix socket port number file %s: %s",
986 path, _dbus_strerror (errno));
987 _dbus_close_socket (listen_handle, NULL);
991 _dbus_lock_sockets();
992 _dbus_handle_to_socket_unlocked(listen_handle, &s);
993 s->port_file_fd = filefd;
994 _dbus_unlock_sockets();
996 /* Use strdup() to avoid memory leak in dbus-test */
997 path = strdup (path);
1000 _DBUS_SET_OOM (error);
1001 _dbus_close_socket (listen_handle, NULL);
1005 _dbus_string_init_const (&s->port_file, path);
1007 if (!_dbus_string_init (&portstr))
1009 _DBUS_SET_OOM (error);
1010 _dbus_close_socket (listen_handle, NULL);
1014 if (!_dbus_string_append_int (&portstr, ntohs (((struct sockaddr_in*) &sa)->sin_port)))
1016 _DBUS_SET_OOM (error);
1017 _dbus_close_socket (listen_handle, NULL);
1021 l = _dbus_string_get_length (&portstr);
1022 n = write (filefd, _dbus_string_get_const_data (&portstr), l);
1023 _dbus_string_free (&portstr);
1027 dbus_set_error (error, _dbus_error_from_errno (errno),
1028 "Failed to write port number to file %s: %s",
1029 path, _dbus_strerror (errno));
1030 _dbus_close_socket (listen_handle, NULL);
1035 dbus_set_error (error, _dbus_error_from_errno (errno),
1036 "Failed to write port number to file %s",
1038 _dbus_close_socket (listen_handle, NULL);
1042 return listen_handle;
1049 * Opens the client side of a Windows named pipe. The connection D-BUS
1050 * file descriptor index is returned. It is set up as nonblocking.
1052 * @param path the path to named pipe socket
1053 * @param error return location for error code
1054 * @returns connection D-BUS file descriptor or -1 on error
1057 _dbus_connect_named_pipe (const char *path,
1060 _dbus_assert_not_reached ("not implemented");
1067 _dbus_account_to_win_sid (const wchar_t *waccount,
1071 dbus_bool_t retval = FALSE;
1072 DWORD sid_length, wdomain_length;
1080 if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1081 NULL, &wdomain_length, &use)
1082 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1084 _dbus_win_set_error_from_win_error (error, GetLastError ());
1088 *ppsid = dbus_malloc (sid_length);
1091 _DBUS_SET_OOM (error);
1095 wdomain = dbus_new (wchar_t, wdomain_length);
1098 _DBUS_SET_OOM (error);
1102 if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1103 wdomain, &wdomain_length, &use))
1105 _dbus_win_set_error_from_win_error (error, GetLastError ());
1109 if (!IsValidSid ((PSID) *ppsid))
1111 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1118 dbus_free (wdomain);
1131 fill_win_user_info_name_and_groups (wchar_t *wname,
1139 dbus_bool_t retval = FALSE;
1140 char *name, *domain;
1141 LPLOCALGROUP_USERS_INFO_0 local_groups = NULL;
1142 LPGROUP_USERS_INFO_0 global_groups = NULL;
1143 DWORD nread, ntotal;
1145 name = _dbus_win_utf16_to_utf8 (wname, error);
1149 domain = _dbus_win_utf16_to_utf8 (wdomain, error);
1153 info->username = dbus_malloc (strlen (domain) + 1 + strlen (name) + 1);
1154 if (!info->username)
1156 _DBUS_SET_OOM (error);
1160 strcpy (info->username, domain);
1161 strcat (info->username, "\\");
1162 strcat (info->username, name);
1164 info->n_group_ids = 0;
1165 if (NetUserGetLocalGroups (NULL, wname, 0, LG_INCLUDE_INDIRECT,
1166 (LPBYTE *) &local_groups, MAX_PREFERRED_LENGTH,
1167 &nread, &ntotal) == NERR_Success)
1172 info->group_ids = dbus_new (dbus_gid_t, nread);
1173 if (!info->group_ids)
1175 _DBUS_SET_OOM (error);
1179 for (i = n = 0; i < nread; i++)
1182 if (_dbus_account_to_win_sid (local_groups[i].lgrui0_name,
1185 info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1186 dbus_free (group_sid);
1189 info->n_group_ids = n;
1192 if (NetUserGetGroups (NULL, wname, 0,
1193 (LPBYTE *) &global_groups, MAX_PREFERRED_LENGTH,
1194 &nread, &ntotal) == NERR_Success)
1197 int n = info->n_group_ids;
1199 info->group_ids = dbus_realloc (info->group_ids, (n + nread) * sizeof (dbus_gid_t));
1200 if (!info->group_ids)
1202 _DBUS_SET_OOM (error);
1206 for (i = 0; i < nread; i++)
1209 if (_dbus_account_to_win_sid (global_groups[i].grui0_name,
1212 info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1213 dbus_free (group_sid);
1216 info->n_group_ids = n;
1219 if (info->n_group_ids > 0)
1221 /* FIXME: find out actual primary group */
1222 info->primary_gid = info->group_ids[0];
1226 info->group_ids = dbus_new (dbus_gid_t, 1);
1227 info->n_group_ids = 1;
1228 info->group_ids[0] = DBUS_GID_UNSET;
1229 info->primary_gid = DBUS_GID_UNSET;
1235 if (global_groups != NULL)
1236 NetApiBufferFree (global_groups);
1238 if (local_groups != NULL)
1239 NetApiBufferFree (local_groups);
1250 fill_win_user_info_homedir (wchar_t *wname,
1259 dbus_bool_t retval = FALSE;
1260 USER_INFO_1 *user_info = NULL;
1261 wchar_t wcomputername[MAX_COMPUTERNAME_LENGTH + 1];
1262 DWORD wcomputername_length = MAX_COMPUTERNAME_LENGTH + 1;
1263 dbus_bool_t local_computer;
1265 NET_API_STATUS ret = 0;
1267 /* If the domain is this computer's name, assume it's a local user.
1268 * Otherwise look up a DC for the domain, and ask it.
1271 GetComputerNameW (wcomputername, &wcomputername_length);
1272 local_computer = (wcsicmp (wcomputername, wdomain) == 0);
1274 if (!local_computer)
1276 ret = NetGetAnyDCName (NULL, wdomain, (LPBYTE *) &dc);
1277 if (ret != NERR_Success)
1279 info->homedir = _dbus_strdup ("\\");
1280 _dbus_warn("NetGetAnyDCName() failed with errorcode %d '%s'\n",ret,_dbus_lm_strerror(ret));
1285 /* No way to find out the profile of another user, let's try the
1286 * "home directory" from NetUserGetInfo's USER_INFO_1.
1288 ret = NetUserGetInfo (dc, wname, 1, (LPBYTE *) &user_info);
1289 if (ret == NERR_Success )
1290 if(user_info->usri1_home_dir != NULL &&
1291 user_info->usri1_home_dir != (LPWSTR)0xfeeefeee && /* freed memory http://www.gamedev.net/community/forums/topic.asp?topic_id=158402 */
1292 user_info->usri1_home_dir[0] != '\0')
1294 info->homedir = _dbus_win_utf16_to_utf8 (user_info->usri1_home_dir, error);
1300 _dbus_verbose("NetUserGetInfo() returned no home dir entry\n");
1301 /* Not set, so use something random. */
1302 info->homedir = _dbus_strdup ("\\");
1306 char *dc_string = _dbus_win_utf16_to_utf8(dc,error);
1307 char *user_name = _dbus_win_utf16_to_utf8(wname,error);
1308 _dbus_warn("NetUserGetInfo() for user '%s' failed with errorcode %d '%s', %s\n",user_name, ret,_dbus_lm_strerror(ret),dc_string);
1309 dbus_free(user_name);
1310 dbus_free(dc_string);
1311 /* Not set, so use something random. */
1312 info->homedir = _dbus_strdup ("\\");
1319 NetApiBufferFree (dc);
1320 if (user_info != NULL)
1321 NetApiBufferFree (user_info);
1328 fill_win_user_info_from_name (wchar_t *wname,
1336 dbus_bool_t retval = FALSE;
1339 DWORD sid_length, wdomain_length;
1344 if (!LookupAccountNameW (NULL, wname, NULL, &sid_length,
1345 NULL, &wdomain_length, &use) &&
1346 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1348 _dbus_win_set_error_from_win_error (error, GetLastError ());
1352 sid = dbus_malloc (sid_length);
1355 _DBUS_SET_OOM (error);
1359 wdomain = dbus_new (wchar_t, wdomain_length);
1362 _DBUS_SET_OOM (error);
1366 if (!LookupAccountNameW (NULL, wname, sid, &sid_length,
1367 wdomain, &wdomain_length, &use))
1369 _dbus_win_set_error_from_win_error (error, GetLastError ());
1373 if (!IsValidSid (sid))
1375 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1379 info->uid = _dbus_win_sid_to_uid_t (sid);
1381 if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1384 if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1390 dbus_free (wdomain);
1399 _dbus_win_sid_to_name_and_domain (dbus_uid_t uid,
1409 DWORD wname_length, wdomain_length;
1412 if (!_dbus_uid_t_to_win_sid (uid, &sid))
1414 _dbus_win_set_error_from_win_error (error, GetLastError ());
1420 if (!LookupAccountSidW (NULL, sid, NULL, &wname_length,
1421 NULL, &wdomain_length, &use) &&
1422 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1424 _dbus_win_set_error_from_win_error (error, GetLastError ());
1428 *wname = dbus_new (wchar_t, wname_length);
1431 _DBUS_SET_OOM (error);
1435 *wdomain = dbus_new (wchar_t, wdomain_length);
1438 _DBUS_SET_OOM (error);
1442 if (!LookupAccountSidW (NULL, sid, *wname, &wname_length,
1443 *wdomain, &wdomain_length, &use))
1445 _dbus_win_set_error_from_win_error (error, GetLastError ());
1452 dbus_free (*wdomain);
1465 fill_win_user_info_from_uid (dbus_uid_t uid,
1474 dbus_bool_t retval = FALSE;
1475 wchar_t *wname, *wdomain;
1479 if (!_dbus_win_sid_to_name_and_domain (uid, &wname, &wdomain, error))
1481 _dbus_verbose("%s after _dbus_win_sid_to_name_and_domain\n",__FUNCTION__);
1485 if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1487 _dbus_verbose("%s after fill_win_user_info_name_and_groups\n",__FUNCTION__);
1492 if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1494 _dbus_verbose("%s after fill_win_user_info_homedir\n",__FUNCTION__);
1501 dbus_free (wdomain);
1512 _dbus_win_startup_winsock (void)
1514 /* Straight from MSDN, deuglified */
1516 static dbus_bool_t beenhere = FALSE;
1518 WORD wVersionRequested;
1525 wVersionRequested = MAKEWORD (2, 0);
1527 err = WSAStartup (wVersionRequested, &wsaData);
1530 _dbus_assert_not_reached ("Could not initialize WinSock");
1534 /* Confirm that the WinSock DLL supports 2.0. Note that if the DLL
1535 * supports versions greater than 2.0 in addition to 2.0, it will
1536 * still return 2.0 in wVersion since that is the version we
1539 if (LOBYTE (wsaData.wVersion) != 2 ||
1540 HIBYTE (wsaData.wVersion) != 0)
1542 _dbus_assert_not_reached ("No usable WinSock found");
1557 /************************************************************************
1561 ************************************************************************/
1564 * Measure the message length without terminating nul
1566 int _dbus_printf_string_upper_bound (const char *format,
1569 /* MSVCRT's vsnprintf semantics are a bit different */
1570 /* The C library source in the Platform SDK indicates that this
1571 * would work, but alas, it doesn't. At least not on Windows
1572 * 2000. Presumably those sources correspond to the C library on
1573 * some newer or even future Windows version.
1575 len = _vsnprintf (NULL, _DBUS_INT_MAX, format, args);
1579 len = vsnprintf (p, sizeof(p)-1, format, args);
1580 if (len == -1) // try again
1583 p = malloc (strlen(format)*3);
1584 len = vsnprintf (p, sizeof(p)-1, format, args);
1592 * Returns the UTF-16 form of a UTF-8 string. The result should be
1593 * freed with dbus_free() when no longer needed.
1595 * @param str the UTF-8 string
1596 * @param error return location for error code
1599 _dbus_win_utf8_to_utf16 (const char *str,
1606 _dbus_string_init_const (&s, str);
1608 if (!_dbus_string_validate_utf8 (&s, 0, _dbus_string_get_length (&s)))
1610 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid UTF-8");
1614 n = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0);
1618 _dbus_win_set_error_from_win_error (error, GetLastError ());
1622 retval = dbus_new (wchar_t, n);
1626 _DBUS_SET_OOM (error);
1630 if (MultiByteToWideChar (CP_UTF8, 0, str, -1, retval, n) != n)
1633 dbus_set_error_const (error, DBUS_ERROR_FAILED, "MultiByteToWideChar inconsistency");
1641 * Returns the UTF-8 form of a UTF-16 string. The result should be
1642 * freed with dbus_free() when no longer needed.
1644 * @param str the UTF-16 string
1645 * @param error return location for error code
1648 _dbus_win_utf16_to_utf8 (const wchar_t *str,
1654 n = WideCharToMultiByte (CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
1658 _dbus_win_set_error_from_win_error (error, GetLastError ());
1662 retval = dbus_malloc (n);
1666 _DBUS_SET_OOM (error);
1670 if (WideCharToMultiByte (CP_UTF8, 0, str, -1, retval, n, NULL, NULL) != n)
1673 dbus_set_error_const (error, DBUS_ERROR_FAILED, "WideCharToMultiByte inconsistency");
1685 /************************************************************************
1687 uid ... <-> win sid functions
1689 ************************************************************************/
1692 _dbus_win_account_to_sid (const wchar_t *waccount,
1696 dbus_bool_t retval = FALSE;
1697 DWORD sid_length, wdomain_length;
1705 if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1706 NULL, &wdomain_length, &use) &&
1707 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1709 _dbus_win_set_error_from_win_error (error, GetLastError ());
1713 *ppsid = dbus_malloc (sid_length);
1716 _DBUS_SET_OOM (error);
1720 wdomain = dbus_new (wchar_t, wdomain_length);
1723 _DBUS_SET_OOM (error);
1727 if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1728 wdomain, &wdomain_length, &use))
1730 _dbus_win_set_error_from_win_error (error, GetLastError ());
1734 if (!IsValidSid ((PSID) *ppsid))
1736 dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1743 dbus_free (wdomain);
1755 _sid_atom_cache_shutdown (void *unused)
1758 _DBUS_LOCK (sid_atom_cache);
1759 _dbus_hash_iter_init (sid_atom_cache, &iter);
1760 while (_dbus_hash_iter_next (&iter))
1763 atom = (ATOM) _dbus_hash_iter_get_value (&iter);
1764 GlobalDeleteAtom(atom);
1765 _dbus_hash_iter_remove_entry(&iter);
1767 _DBUS_UNLOCK (sid_atom_cache);
1768 _dbus_hash_table_unref (sid_atom_cache);
1769 sid_atom_cache = NULL;
1773 * Returns the 2-way associated dbus_uid_t form a SID.
1775 * @param psid pointer to the SID
1778 _dbus_win_sid_to_uid_t (PSID psid)
1785 if (!IsValidSid (psid))
1787 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1788 return DBUS_UID_UNSET;
1790 if (!ConvertSidToStringSidA (psid, &string))
1792 _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1793 return DBUS_UID_UNSET;
1796 atom = GlobalAddAtom(string);
1800 _dbus_verbose("%s GlobalAddAtom failed\n",__FUNCTION__);
1802 return DBUS_UID_UNSET;
1805 _DBUS_LOCK (sid_atom_cache);
1807 if (sid_atom_cache == NULL)
1809 sid_atom_cache = _dbus_hash_table_new (DBUS_HASH_ULONG, NULL, NULL);
1810 _dbus_register_shutdown_func (_sid_atom_cache_shutdown, NULL);
1814 olduid = (dbus_uid_t) _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1818 _dbus_verbose("%s sid with id %i found in cache\n",__FUNCTION__, olduid);
1823 _dbus_hash_table_insert_ulong (sid_atom_cache, uid, (void*) uid);
1824 _dbus_verbose("%s sid %s added with uid %i to cache\n",__FUNCTION__, string, uid);
1827 _DBUS_UNLOCK (sid_atom_cache);
1832 dbus_bool_t _dbus_uid_t_to_win_sid (dbus_uid_t uid, PSID *ppsid)
1837 atom = _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1840 _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__,uid);
1843 memset( string, '.', sizeof(string) );
1844 if (!GlobalGetAtomNameA( (ATOM) atom, string, 255 ))
1846 _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__, uid);
1849 if (!ConvertStringSidToSidA(string, ppsid))
1851 _dbus_verbose("%s could not convert %s into sid \n",__FUNCTION__, string);
1854 _dbus_verbose("%s converted %s into sid \n",__FUNCTION__, string);
1859 /** @} end of sysdeps-win */
1863 * @returns process UID
1868 dbus_uid_t retval = DBUS_UID_UNSET;
1869 HANDLE process_token = NULL;
1870 TOKEN_USER *token_user = NULL;
1873 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1874 _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1875 else if ((!GetTokenInformation (process_token, TokenUser, NULL, 0, &n)
1876 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1877 || (token_user = alloca (n)) == NULL
1878 || !GetTokenInformation (process_token, TokenUser, token_user, n, &n))
1879 _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1881 retval = _dbus_win_sid_to_uid_t (token_user->User.Sid);
1883 if (process_token != NULL)
1884 CloseHandle (process_token);
1886 _dbus_verbose("_dbus_getuid() returns %d\n",retval);
1890 #ifdef DBUS_BUILD_TESTS
1892 * @returns process GID
1897 dbus_gid_t retval = DBUS_GID_UNSET;
1898 HANDLE process_token = NULL;
1899 TOKEN_PRIMARY_GROUP *token_primary_group = NULL;
1902 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1903 _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1904 else if ((!GetTokenInformation (process_token, TokenPrimaryGroup,
1906 GetLastError () != ERROR_INSUFFICIENT_BUFFER) ||
1907 (token_primary_group = alloca (n)) == NULL ||
1908 !GetTokenInformation (process_token, TokenPrimaryGroup,
1909 token_primary_group, n, &n))
1910 _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1912 retval = _dbus_win_sid_to_uid_t (token_primary_group->PrimaryGroup);
1914 if (process_token != NULL)
1915 CloseHandle (process_token);
1922 _dbus_domain_test (const char *test_data_dir)
1924 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1925 check_spawn_nonexistent,
1932 #endif //DBUS_BUILD_TESTS
1934 /************************************************************************
1938 ************************************************************************/
1941 * Creates a full-duplex pipe (as in socketpair()).
1942 * Sets both ends of the pipe nonblocking.
1944 * @todo libdbus only uses this for the debug-pipe server, so in
1945 * principle it could be in dbus-sysdeps-util.c, except that
1946 * dbus-sysdeps-util.c isn't in libdbus when tests are enabled and the
1947 * debug-pipe server is used.
1949 * @param fd1 return location for one end
1950 * @param fd2 return location for the other end
1951 * @param blocking #TRUE if pipe should be blocking
1952 * @param error error return
1953 * @returns #FALSE on failure (if error is set)
1956 _dbus_full_duplex_pipe (int *fd1,
1958 dbus_bool_t blocking,
1961 SOCKET temp, socket1 = -1, socket2 = -1;
1962 struct sockaddr_in saddr;
1965 fd_set read_set, write_set;
1970 _dbus_win_startup_winsock ();
1972 temp = socket (AF_INET, SOCK_STREAM, 0);
1973 if (temp == INVALID_SOCKET)
1975 DBUS_SOCKET_SET_ERRNO ();
1980 if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR)
1982 DBUS_SOCKET_SET_ERRNO ();
1987 saddr.sin_family = AF_INET;
1989 saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1991 if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)))
1993 DBUS_SOCKET_SET_ERRNO ();
1997 if (listen (temp, 1) == SOCKET_ERROR)
1999 DBUS_SOCKET_SET_ERRNO ();
2003 len = sizeof (saddr);
2004 if (getsockname (temp, (struct sockaddr *)&saddr, &len))
2006 DBUS_SOCKET_SET_ERRNO ();
2010 socket1 = socket (AF_INET, SOCK_STREAM, 0);
2011 if (socket1 == INVALID_SOCKET)
2013 DBUS_SOCKET_SET_ERRNO ();
2018 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2020 DBUS_SOCKET_SET_ERRNO ();
2024 if (connect (socket1, (struct sockaddr *)&saddr, len) != SOCKET_ERROR ||
2025 WSAGetLastError () != WSAEWOULDBLOCK)
2027 dbus_set_error_const (error, DBUS_ERROR_FAILED,
2028 "_dbus_full_duplex_pipe socketpair() emulation failed");
2032 FD_ZERO (&read_set);
2033 FD_SET (temp, &read_set);
2038 if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR)
2040 DBUS_SOCKET_SET_ERRNO ();
2044 _dbus_assert (FD_ISSET (temp, &read_set));
2046 socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
2047 if (socket2 == INVALID_SOCKET)
2049 DBUS_SOCKET_SET_ERRNO ();
2053 FD_ZERO (&write_set);
2054 FD_SET (socket1, &write_set);
2059 if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR)
2061 DBUS_SOCKET_SET_ERRNO ();
2065 _dbus_assert (FD_ISSET (socket1, &write_set));
2070 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2072 DBUS_SOCKET_SET_ERRNO ();
2077 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2079 DBUS_SOCKET_SET_ERRNO ();
2086 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2088 DBUS_SOCKET_SET_ERRNO ();
2094 *fd1 = _dbus_socket_to_handle (&sock);
2096 *fd2 = _dbus_socket_to_handle (&sock);
2098 _dbus_verbose ("full-duplex pipe %d:%d <-> %d:%d\n",
2099 *fd1, socket1, *fd2, socket2);
2106 closesocket (socket2);
2108 closesocket (socket1);
2112 dbus_set_error (error, _dbus_error_from_errno (errno),
2113 "Could not setup socket pair: %s",
2114 _dbus_strerror (errno));
2120 * Wrapper for poll().
2122 * @param fds the file descriptors to poll
2123 * @param n_fds number of descriptors in the array
2124 * @param timeout_milliseconds timeout or -1 for infinite
2125 * @returns numbers of fds with revents, or <0 on error
2127 #define USE_CHRIS_IMPL 0
2130 _dbus_poll (DBusPollFD *fds,
2132 int timeout_milliseconds)
2134 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2135 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2143 #define DBUS_STACK_WSAEVENTS 256
2144 WSAEVENT eventsOnStack[DBUS_STACK_WSAEVENTS];
2145 WSAEVENT *pEvents = NULL;
2146 if (n_fds > DBUS_STACK_WSAEVENTS)
2147 pEvents = calloc(sizeof(WSAEVENT), n_fds);
2149 pEvents = eventsOnStack;
2151 _dbus_lock_sockets();
2153 #ifdef DBUS_ENABLE_VERBOSE_MODE
2155 msgp += sprintf (msgp, "WSAEventSelect: to=%d\n\t", timeout_milliseconds);
2156 for (i = 0; i < n_fds; i++)
2158 static dbus_bool_t warned = FALSE;
2160 DBusPollFD *fdp = &fds[i];
2162 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2164 if (s->is_used == 0)
2166 _dbus_warn ("no valid socket");
2170 if (fdp->events & _DBUS_POLLIN)
2171 msgp += sprintf (msgp, "R:%d ", s->fd);
2173 if (fdp->events & _DBUS_POLLOUT)
2174 msgp += sprintf (msgp, "W:%d ", s->fd);
2176 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2178 // FIXME: more robust code for long msg
2179 // create on heap when msg[] becomes too small
2180 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2182 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2186 msgp += sprintf (msgp, "\n");
2187 _dbus_verbose ("%s",msg);
2189 for (i = 0; i < n_fds; i++)
2192 DBusPollFD *fdp = &fds[i];
2194 long lNetworkEvents = FD_OOB;
2196 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2198 if (s->is_used == 0)
2201 ev = WSACreateEvent();
2203 if (fdp->events & _DBUS_POLLIN)
2204 lNetworkEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
2206 if (fdp->events & _DBUS_POLLOUT)
2207 lNetworkEvents |= FD_WRITE | FD_CONNECT;
2209 WSAEventSelect(s->fd, ev, lNetworkEvents);
2214 _dbus_unlock_sockets();
2216 ready = WSAWaitForMultipleEvents (n_fds, pEvents, FALSE, timeout_milliseconds, FALSE);
2218 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2220 DBUS_SOCKET_SET_ERRNO ();
2221 if (errno != EWOULDBLOCK)
2222 _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", _dbus_strerror (errno));
2225 else if (ready == WSA_WAIT_TIMEOUT)
2227 _dbus_verbose ("WSAWaitForMultipleEvents: WSA_WAIT_TIMEOUT\n");
2230 else if (ready >= WSA_WAIT_EVENT_0 && ready < (int)(WSA_WAIT_EVENT_0 + n_fds))
2233 msgp += sprintf (msgp, "WSAWaitForMultipleEvents: =%d\n\t", ready);
2235 _dbus_lock_sockets();
2236 for (i = 0; i < n_fds; i++)
2239 DBusPollFD *fdp = &fds[i];
2240 WSANETWORKEVENTS ne;
2242 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2246 WSAEnumNetworkEvents(s->fd, pEvents[i], &ne);
2248 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2249 fdp->revents |= _DBUS_POLLIN;
2251 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2252 fdp->revents |= _DBUS_POLLOUT;
2254 if (ne.lNetworkEvents & (FD_OOB))
2255 fdp->revents |= _DBUS_POLLERR;
2257 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2258 msgp += sprintf (msgp, "R:%d ", s->fd);
2260 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2261 msgp += sprintf (msgp, "W:%d ", s->fd);
2263 if (ne.lNetworkEvents & (FD_OOB))
2264 msgp += sprintf (msgp, "E:%d ", s->fd);
2266 msgp += sprintf (msgp, "lNetworkEvents:%d ", ne.lNetworkEvents);
2268 if(ne.lNetworkEvents)
2271 WSAEventSelect(s->fd, pEvents[i], 0);
2273 _dbus_unlock_sockets();
2275 msgp += sprintf (msgp, "\n");
2276 _dbus_verbose ("%s",msg);
2280 _dbus_verbose ("WSAWaitForMultipleEvents: failed for unknown reason!");
2284 for(i = 0; i < n_fds; i++)
2286 WSACloseEvent(pEvents[i]);
2289 if (n_fds > DBUS_STACK_WSAEVENTS)
2295 #else // USE_CHRIS_IMPL
2298 _dbus_poll (DBusPollFD *fds,
2300 int timeout_milliseconds)
2302 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2303 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2306 fd_set read_set, write_set, err_set;
2312 FD_ZERO (&read_set);
2313 FD_ZERO (&write_set);
2316 _dbus_lock_sockets();
2318 #ifdef DBUS_ENABLE_VERBOSE_MODE
2320 msgp += sprintf (msgp, "select: to=%d\n\t", timeout_milliseconds);
2321 for (i = 0; i < n_fds; i++)
2323 static dbus_bool_t warned = FALSE;
2325 DBusPollFD *fdp = &fds[i];
2327 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2329 if (s->is_used == 0)
2331 _dbus_warn ("no valid socket");
2335 if (fdp->events & _DBUS_POLLIN)
2336 msgp += sprintf (msgp, "R:%d ", s->fd);
2338 if (fdp->events & _DBUS_POLLOUT)
2339 msgp += sprintf (msgp, "W:%d ", s->fd);
2341 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2343 // FIXME: more robust code for long msg
2344 // create on heap when msg[] becomes too small
2345 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2347 _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2351 msgp += sprintf (msgp, "\n");
2352 _dbus_verbose ("%s",msg);
2354 for (i = 0; i < n_fds; i++)
2357 DBusPollFD *fdp = &fds[i];
2359 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2361 if (s->is_used != 1)
2364 if (fdp->events & _DBUS_POLLIN)
2365 FD_SET (s->fd, &read_set);
2367 if (fdp->events & _DBUS_POLLOUT)
2368 FD_SET (s->fd, &write_set);
2370 FD_SET (s->fd, &err_set);
2372 max_fd = MAX (max_fd, s->fd);
2375 _dbus_unlock_sockets();
2377 tv.tv_sec = timeout_milliseconds / 1000;
2378 tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
2380 ready = select (max_fd + 1, &read_set, &write_set, &err_set,
2381 timeout_milliseconds < 0 ? NULL : &tv);
2383 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2385 DBUS_SOCKET_SET_ERRNO ();
2386 if (errno != EWOULDBLOCK)
2387 _dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno));
2389 else if (ready == 0)
2390 _dbus_verbose ("select: = 0\n");
2394 #ifdef DBUS_ENABLE_VERBOSE_MODE
2396 msgp += sprintf (msgp, "select: = %d:\n\t", ready);
2397 _dbus_lock_sockets();
2398 for (i = 0; i < n_fds; i++)
2401 DBusPollFD *fdp = &fds[i];
2403 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2405 if (FD_ISSET (s->fd, &read_set))
2406 msgp += sprintf (msgp, "R:%d ", s->fd);
2408 if (FD_ISSET (s->fd, &write_set))
2409 msgp += sprintf (msgp, "W:%d ", s->fd);
2411 if (FD_ISSET (s->fd, &err_set))
2412 msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2414 msgp += sprintf (msgp, "\n");
2415 _dbus_verbose ("%s",msg);
2418 for (i = 0; i < n_fds; i++)
2421 DBusPollFD *fdp = &fds[i];
2423 _dbus_handle_to_socket_unlocked(fdp->fd, &s);
2427 if (FD_ISSET (s->fd, &read_set))
2428 fdp->revents |= _DBUS_POLLIN;
2430 if (FD_ISSET (s->fd, &write_set))
2431 fdp->revents |= _DBUS_POLLOUT;
2433 if (FD_ISSET (s->fd, &err_set))
2434 fdp->revents |= _DBUS_POLLERR;
2436 _dbus_unlock_sockets();
2440 #endif // USE_CHRIS_IMPL
2443 /************************************************************************
2447 ************************************************************************/
2451 * Assigns an error name and message corresponding to a Win32 error
2452 * code to a DBusError. Does nothing if error is #NULL.
2454 * @param error the error.
2455 * @param code the Win32 error code
2458 _dbus_win_set_error_from_win_error (DBusError *error,
2463 /* As we want the English message, use the A API */
2464 FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
2465 FORMAT_MESSAGE_IGNORE_INSERTS |
2466 FORMAT_MESSAGE_FROM_SYSTEM,
2467 NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
2468 (LPTSTR) &msg, 0, NULL);
2473 msg_copy = dbus_malloc (strlen (msg));
2474 strcpy (msg_copy, msg);
2477 dbus_set_error (error, "win32.error", "%s", msg_copy);
2480 dbus_set_error_const (error, "win32.error", "Unknown error code or FormatMessage failed");
2484 _dbus_win_warn_win_error (const char *message,
2489 dbus_error_init (&error);
2490 _dbus_win_set_error_from_win_error (&error, code);
2491 _dbus_warn ("%s: %s\n", message, error.message);
2492 dbus_error_free (&error);
2496 * A wrapper around strerror() because some platforms
2497 * may be lame and not have strerror().
2499 * @param error_number errno.
2500 * @returns error description.
2503 _dbus_strerror (int error_number)
2511 switch (error_number)
2514 return "Interrupted function call";
2516 return "Permission denied";
2518 return "Bad address";
2520 return "Invalid argument";
2522 return "Too many open files";
2523 case WSAEWOULDBLOCK:
2524 return "Resource temporarily unavailable";
2525 case WSAEINPROGRESS:
2526 return "Operation now in progress";
2528 return "Operation already in progress";
2530 return "Socket operation on nonsocket";
2531 case WSAEDESTADDRREQ:
2532 return "Destination address required";
2534 return "Message too long";
2536 return "Protocol wrong type for socket";
2537 case WSAENOPROTOOPT:
2538 return "Bad protocol option";
2539 case WSAEPROTONOSUPPORT:
2540 return "Protocol not supported";
2541 case WSAESOCKTNOSUPPORT:
2542 return "Socket type not supported";
2544 return "Operation not supported";
2545 case WSAEPFNOSUPPORT:
2546 return "Protocol family not supported";
2547 case WSAEAFNOSUPPORT:
2548 return "Address family not supported by protocol family";
2550 return "Address already in use";
2551 case WSAEADDRNOTAVAIL:
2552 return "Cannot assign requested address";
2554 return "Network is down";
2555 case WSAENETUNREACH:
2556 return "Network is unreachable";
2558 return "Network dropped connection on reset";
2559 case WSAECONNABORTED:
2560 return "Software caused connection abort";
2562 return "Connection reset by peer";
2564 return "No buffer space available";
2566 return "Socket is already connected";
2568 return "Socket is not connected";
2570 return "Cannot send after socket shutdown";
2572 return "Connection timed out";
2573 case WSAECONNREFUSED:
2574 return "Connection refused";
2576 return "Host is down";
2577 case WSAEHOSTUNREACH:
2578 return "No route to host";
2580 return "Too many processes";
2582 return "Graceful shutdown in progress";
2583 case WSATYPE_NOT_FOUND:
2584 return "Class type not found";
2585 case WSAHOST_NOT_FOUND:
2586 return "Host not found";
2588 return "Nonauthoritative host not found";
2589 case WSANO_RECOVERY:
2590 return "This is a nonrecoverable error";
2592 return "Valid name, no data record of requested type";
2593 case WSA_INVALID_HANDLE:
2594 return "Specified event object handle is invalid";
2595 case WSA_INVALID_PARAMETER:
2596 return "One or more parameters are invalid";
2597 case WSA_IO_INCOMPLETE:
2598 return "Overlapped I/O event object not in signaled state";
2599 case WSA_IO_PENDING:
2600 return "Overlapped operations will complete later";
2601 case WSA_NOT_ENOUGH_MEMORY:
2602 return "Insufficient memory available";
2603 case WSA_OPERATION_ABORTED:
2604 return "Overlapped operation aborted";
2605 #ifdef WSAINVALIDPROCTABLE
2607 case WSAINVALIDPROCTABLE:
2608 return "Invalid procedure table from service provider";
2610 #ifdef WSAINVALIDPROVIDER
2612 case WSAINVALIDPROVIDER:
2613 return "Invalid service provider version number";
2615 #ifdef WSAPROVIDERFAILEDINIT
2617 case WSAPROVIDERFAILEDINIT:
2618 return "Unable to initialize a service provider";
2621 case WSASYSCALLFAILURE:
2622 return "System call failure";
2624 msg = strerror (error_number);
2634 /* lan manager error codes */
2636 _dbus_lm_strerror(int error_number)
2643 switch (error_number)
2645 case NERR_NetNotStarted:
2646 return "The workstation driver is not installed.";
2647 case NERR_UnknownServer:
2648 return "The server could not be located.";
2650 return "An internal error occurred. The network cannot access a shared memory segment.";
2651 case NERR_NoNetworkResource:
2652 return "A network resource shortage occurred.";
2653 case NERR_RemoteOnly:
2654 return "This operation is not supported on workstations.";
2655 case NERR_DevNotRedirected:
2656 return "The device is not connected.";
2657 case NERR_ServerNotStarted:
2658 return "The Server service is not started.";
2659 case NERR_ItemNotFound:
2660 return "The queue is empty.";
2661 case NERR_UnknownDevDir:
2662 return "The device or directory does not exist.";
2663 case NERR_RedirectedPath:
2664 return "The operation is invalid on a redirected resource.";
2665 case NERR_DuplicateShare:
2666 return "The name has already been shared.";
2668 return "The server is currently out of the requested resource.";
2669 case NERR_TooManyItems:
2670 return "Requested addition of items exceeds the maximum allowed.";
2671 case NERR_InvalidMaxUsers:
2672 return "The Peer service supports only two simultaneous users.";
2673 case NERR_BufTooSmall:
2674 return "The API return buffer is too small.";
2675 case NERR_RemoteErr:
2676 return "A remote API error occurred.";
2677 case NERR_LanmanIniError:
2678 return "An error occurred when opening or reading the configuration file.";
2679 case NERR_NetworkError:
2680 return "A general network error occurred.";
2681 case NERR_WkstaInconsistentState:
2682 return "The Workstation service is in an inconsistent state. Restart the computer before restarting the Workstation service.";
2683 case NERR_WkstaNotStarted:
2684 return "The Workstation service has not been started.";
2685 case NERR_BrowserNotStarted:
2686 return "The requested information is not available.";
2687 case NERR_InternalError:
2688 return "An internal error occurred.";
2689 case NERR_BadTransactConfig:
2690 return "The server is not configured for transactions.";
2691 case NERR_InvalidAPI:
2692 return "The requested API is not supported on the remote server.";
2693 case NERR_BadEventName:
2694 return "The event name is invalid.";
2695 case NERR_DupNameReboot:
2696 return "The computer name already exists on the network. Change it and restart the computer.";
2697 case NERR_CfgCompNotFound:
2698 return "The specified component could not be found in the configuration information.";
2699 case NERR_CfgParamNotFound:
2700 return "The specified parameter could not be found in the configuration information.";
2701 case NERR_LineTooLong:
2702 return "A line in the configuration file is too long.";
2703 case NERR_QNotFound:
2704 return "The printer does not exist.";
2705 case NERR_JobNotFound:
2706 return "The print job does not exist.";
2707 case NERR_DestNotFound:
2708 return "The printer destination cannot be found.";
2709 case NERR_DestExists:
2710 return "The printer destination already exists.";
2712 return "The printer queue already exists.";
2714 return "No more printers can be added.";
2715 case NERR_JobNoRoom:
2716 return "No more print jobs can be added.";
2717 case NERR_DestNoRoom:
2718 return "No more printer destinations can be added.";
2720 return "This printer destination is idle and cannot accept control operations.";
2721 case NERR_DestInvalidOp:
2722 return "This printer destination request contains an invalid control function.";
2723 case NERR_ProcNoRespond:
2724 return "The print processor is not responding.";
2725 case NERR_SpoolerNotLoaded:
2726 return "The spooler is not running.";
2727 case NERR_DestInvalidState:
2728 return "This operation cannot be performed on the print destination in its current state.";
2729 case NERR_QInvalidState:
2730 return "This operation cannot be performed on the printer queue in its current state.";
2731 case NERR_JobInvalidState:
2732 return "This operation cannot be performed on the print job in its current state.";
2733 case NERR_SpoolNoMemory:
2734 return "A spooler memory allocation failure occurred.";
2735 case NERR_DriverNotFound:
2736 return "The device driver does not exist.";
2737 case NERR_DataTypeInvalid:
2738 return "The data type is not supported by the print processor.";
2739 case NERR_ProcNotFound:
2740 return "The print processor is not installed.";
2741 case NERR_ServiceTableLocked:
2742 return "The service database is locked.";
2743 case NERR_ServiceTableFull:
2744 return "The service table is full.";
2745 case NERR_ServiceInstalled:
2746 return "The requested service has already been started.";
2747 case NERR_ServiceEntryLocked:
2748 return "The service does not respond to control actions.";
2749 case NERR_ServiceNotInstalled:
2750 return "The service has not been started.";
2751 case NERR_BadServiceName:
2752 return "The service name is invalid.";
2753 case NERR_ServiceCtlTimeout:
2754 return "The service is not responding to the control function.";
2755 case NERR_ServiceCtlBusy:
2756 return "The service control is busy.";
2757 case NERR_BadServiceProgName:
2758 return "The configuration file contains an invalid service program name.";
2759 case NERR_ServiceNotCtrl:
2760 return "The service could not be controlled in its present state.";
2761 case NERR_ServiceKillProc:
2762 return "The service ended abnormally.";
2763 case NERR_ServiceCtlNotValid:
2764 return "The requested pause or stop is not valid for this service.";
2765 case NERR_NotInDispatchTbl:
2766 return "The service control dispatcher could not find the service name in the dispatch table.";
2767 case NERR_BadControlRecv:
2768 return "The service control dispatcher pipe read failed.";
2769 case NERR_ServiceNotStarting:
2770 return "A thread for the new service could not be created.";
2771 case NERR_AlreadyLoggedOn:
2772 return "This workstation is already logged on to the local-area network.";
2773 case NERR_NotLoggedOn:
2774 return "The workstation is not logged on to the local-area network.";
2775 case NERR_BadUsername:
2776 return "The user name or group name parameter is invalid.";
2777 case NERR_BadPassword:
2778 return "The password parameter is invalid.";
2779 case NERR_UnableToAddName_W:
2780 return "@W The logon processor did not add the message alias.";
2781 case NERR_UnableToAddName_F:
2782 return "The logon processor did not add the message alias.";
2783 case NERR_UnableToDelName_W:
2784 return "@W The logoff processor did not delete the message alias.";
2785 case NERR_UnableToDelName_F:
2786 return "The logoff processor did not delete the message alias.";
2787 case NERR_LogonsPaused:
2788 return "Network logons are paused.";
2789 case NERR_LogonServerConflict:
2790 return "A centralized logon-server conflict occurred.";
2791 case NERR_LogonNoUserPath:
2792 return "The server is configured without a valid user path.";
2793 case NERR_LogonScriptError:
2794 return "An error occurred while loading or running the logon script.";
2795 case NERR_StandaloneLogon:
2796 return "The logon server was not specified. Your computer will be logged on as STANDALONE.";
2797 case NERR_LogonServerNotFound:
2798 return "The logon server could not be found.";
2799 case NERR_LogonDomainExists:
2800 return "There is already a logon domain for this computer.";
2801 case NERR_NonValidatedLogon:
2802 return "The logon server could not validate the logon.";
2803 case NERR_ACFNotFound:
2804 return "The security database could not be found.";
2805 case NERR_GroupNotFound:
2806 return "The group name could not be found.";
2807 case NERR_UserNotFound:
2808 return "The user name could not be found.";
2809 case NERR_ResourceNotFound:
2810 return "The resource name could not be found.";
2811 case NERR_GroupExists:
2812 return "The group already exists.";
2813 case NERR_UserExists:
2814 return "The user account already exists.";
2815 case NERR_ResourceExists:
2816 return "The resource permission list already exists.";
2817 case NERR_NotPrimary:
2818 return "This operation is only allowed on the primary domain controller of the domain.";
2819 case NERR_ACFNotLoaded:
2820 return "The security database has not been started.";
2821 case NERR_ACFNoRoom:
2822 return "There are too many names in the user accounts database.";
2823 case NERR_ACFFileIOFail:
2824 return "A disk I/O failure occurred.";
2825 case NERR_ACFTooManyLists:
2826 return "The limit of 64 entries per resource was exceeded.";
2827 case NERR_UserLogon:
2828 return "Deleting a user with a session is not allowed.";
2829 case NERR_ACFNoParent:
2830 return "The parent directory could not be located.";
2831 case NERR_CanNotGrowSegment:
2832 return "Unable to add to the security database session cache segment.";
2833 case NERR_SpeGroupOp:
2834 return "This operation is not allowed on this special group.";
2835 case NERR_NotInCache:
2836 return "This user is not cached in user accounts database session cache.";
2837 case NERR_UserInGroup:
2838 return "The user already belongs to this group.";
2839 case NERR_UserNotInGroup:
2840 return "The user does not belong to this group.";
2841 case NERR_AccountUndefined:
2842 return "This user account is undefined.";
2843 case NERR_AccountExpired:
2844 return "This user account has expired.";
2845 case NERR_InvalidWorkstation:
2846 return "The user is not allowed to log on from this workstation.";
2847 case NERR_InvalidLogonHours:
2848 return "The user is not allowed to log on at this time.";
2849 case NERR_PasswordExpired:
2850 return "The password of this user has expired.";
2851 case NERR_PasswordCantChange:
2852 return "The password of this user cannot change.";
2853 case NERR_PasswordHistConflict:
2854 return "This password cannot be used now.";
2855 case NERR_PasswordTooShort:
2856 return "The password does not meet the password policy requirements. Check the minimum password length, password complexity and password history requirements.";
2857 case NERR_PasswordTooRecent:
2858 return "The password of this user is too recent to change.";
2859 case NERR_InvalidDatabase:
2860 return "The security database is corrupted.";
2861 case NERR_DatabaseUpToDate:
2862 return "No updates are necessary to this replicant network/local security database.";
2863 case NERR_SyncRequired:
2864 return "This replicant database is outdated; synchronization is required.";
2865 case NERR_UseNotFound:
2866 return "The network connection could not be found.";
2867 case NERR_BadAsgType:
2868 return "This asg_type is invalid.";
2869 case NERR_DeviceIsShared:
2870 return "This device is currently being shared.";
2871 case NERR_NoComputerName:
2872 return "The computer name could not be added as a message alias. The name may already exist on the network.";
2873 case NERR_MsgAlreadyStarted:
2874 return "The Messenger service is already started.";
2875 case NERR_MsgInitFailed:
2876 return "The Messenger service failed to start.";
2877 case NERR_NameNotFound:
2878 return "The message alias could not be found on the network.";
2879 case NERR_AlreadyForwarded:
2880 return "This message alias has already been forwarded.";
2881 case NERR_AddForwarded:
2882 return "This message alias has been added but is still forwarded.";
2883 case NERR_AlreadyExists:
2884 return "This message alias already exists locally.";
2885 case NERR_TooManyNames:
2886 return "The maximum number of added message aliases has been exceeded.";
2887 case NERR_DelComputerName:
2888 return "The computer name could not be deleted.";
2889 case NERR_LocalForward:
2890 return "Messages cannot be forwarded back to the same workstation.";
2891 case NERR_GrpMsgProcessor:
2892 return "An error occurred in the domain message processor.";
2893 case NERR_PausedRemote:
2894 return "The message was sent, but the recipient has paused the Messenger service.";
2895 case NERR_BadReceive:
2896 return "The message was sent but not received.";
2897 case NERR_NameInUse:
2898 return "The message alias is currently in use. Try again later.";
2899 case NERR_MsgNotStarted:
2900 return "The Messenger service has not been started.";
2901 case NERR_NotLocalName:
2902 return "The name is not on the local computer.";
2903 case NERR_NoForwardName:
2904 return "The forwarded message alias could not be found on the network.";
2905 case NERR_RemoteFull:
2906 return "The message alias table on the remote station is full.";
2907 case NERR_NameNotForwarded:
2908 return "Messages for this alias are not currently being forwarded.";
2909 case NERR_TruncatedBroadcast:
2910 return "The broadcast message was truncated.";
2911 case NERR_InvalidDevice:
2912 return "This is an invalid device name.";
2913 case NERR_WriteFault:
2914 return "A write fault occurred.";
2915 case NERR_DuplicateName:
2916 return "A duplicate message alias exists on the network.";
2917 case NERR_DeleteLater:
2918 return "@W This message alias will be deleted later.";
2919 case NERR_IncompleteDel:
2920 return "The message alias was not successfully deleted from all networks.";
2921 case NERR_MultipleNets:
2922 return "This operation is not supported on computers with multiple networks.";
2923 case NERR_NetNameNotFound:
2924 return "This shared resource does not exist.";
2925 case NERR_DeviceNotShared:
2926 return "This device is not shared.";
2927 case NERR_ClientNameNotFound:
2928 return "A session does not exist with that computer name.";
2929 case NERR_FileIdNotFound:
2930 return "There is not an open file with that identification number.";
2931 case NERR_ExecFailure:
2932 return "A failure occurred when executing a remote administration command.";
2934 return "A failure occurred when opening a remote temporary file.";
2935 case NERR_TooMuchData:
2936 return "The data returned from a remote administration command has been truncated to 64K.";
2937 case NERR_DeviceShareConflict:
2938 return "This device cannot be shared as both a spooled and a non-spooled resource.";
2939 case NERR_BrowserTableIncomplete:
2940 return "The information in the list of servers may be incorrect.";
2941 case NERR_NotLocalDomain:
2942 return "The computer is not active in this domain.";
2943 #ifdef NERR_IsDfsShare
2945 case NERR_IsDfsShare:
2946 return "The share must be removed from the Distributed File System before it can be deleted.";
2949 case NERR_DevInvalidOpCode:
2950 return "The operation is invalid for this device.";
2951 case NERR_DevNotFound:
2952 return "This device cannot be shared.";
2953 case NERR_DevNotOpen:
2954 return "This device was not open.";
2955 case NERR_BadQueueDevString:
2956 return "This device name list is invalid.";
2957 case NERR_BadQueuePriority:
2958 return "The queue priority is invalid.";
2959 case NERR_NoCommDevs:
2960 return "There are no shared communication devices.";
2961 case NERR_QueueNotFound:
2962 return "The queue you specified does not exist.";
2963 case NERR_BadDevString:
2964 return "This list of devices is invalid.";
2966 return "The requested device is invalid.";
2967 case NERR_InUseBySpooler:
2968 return "This device is already in use by the spooler.";
2969 case NERR_CommDevInUse:
2970 return "This device is already in use as a communication device.";
2971 case NERR_InvalidComputer:
2972 return "This computer name is invalid.";
2973 case NERR_MaxLenExceeded:
2974 return "The string and prefix specified are too long.";
2975 case NERR_BadComponent:
2976 return "This path component is invalid.";
2978 return "Could not determine the type of input.";
2979 case NERR_TooManyEntries:
2980 return "The buffer for types is not big enough.";
2981 case NERR_ProfileFileTooBig:
2982 return "Profile files cannot exceed 64K.";
2983 case NERR_ProfileOffset:
2984 return "The start offset is out of range.";
2985 case NERR_ProfileCleanup:
2986 return "The system cannot delete current connections to network resources.";
2987 case NERR_ProfileUnknownCmd:
2988 return "The system was unable to parse the command line in this file.";
2989 case NERR_ProfileLoadErr:
2990 return "An error occurred while loading the profile file.";
2991 case NERR_ProfileSaveErr:
2992 return "@W Errors occurred while saving the profile file. The profile was partially saved.";
2993 case NERR_LogOverflow:
2994 return "Log file %1 is full.";
2995 case NERR_LogFileChanged:
2996 return "This log file has changed between reads.";
2997 case NERR_LogFileCorrupt:
2998 return "Log file %1 is corrupt.";
2999 case NERR_SourceIsDir:
3000 return "The source path cannot be a directory.";
3001 case NERR_BadSource:
3002 return "The source path is illegal.";
3004 return "The destination path is illegal.";
3005 case NERR_DifferentServers:
3006 return "The source and destination paths are on different servers.";
3007 case NERR_RunSrvPaused:
3008 return "The Run server you requested is paused.";
3009 case NERR_ErrCommRunSrv:
3010 return "An error occurred when communicating with a Run server.";
3011 case NERR_ErrorExecingGhost:
3012 return "An error occurred when starting a background process.";
3013 case NERR_ShareNotFound:
3014 return "The shared resource you are connected to could not be found.";
3015 case NERR_InvalidLana:
3016 return "The LAN adapter number is invalid.";
3017 case NERR_OpenFiles:
3018 return "There are open files on the connection.";
3019 case NERR_ActiveConns:
3020 return "Active connections still exist.";
3021 case NERR_BadPasswordCore:
3022 return "This share name or password is invalid.";
3024 return "The device is being accessed by an active process.";
3025 case NERR_LocalDrive:
3026 return "The drive letter is in use locally.";
3027 case NERR_AlertExists:
3028 return "The specified client is already registered for the specified event.";
3029 case NERR_TooManyAlerts:
3030 return "The alert table is full.";
3031 case NERR_NoSuchAlert:
3032 return "An invalid or nonexistent alert name was raised.";
3033 case NERR_BadRecipient:
3034 return "The alert recipient is invalid.";
3035 case NERR_AcctLimitExceeded:
3036 return "A user's session with this server has been deleted.";
3037 case NERR_InvalidLogSeek:
3038 return "The log file does not contain the requested record number.";
3039 case NERR_BadUasConfig:
3040 return "The user accounts database is not configured correctly.";
3041 case NERR_InvalidUASOp:
3042 return "This operation is not permitted when the Netlogon service is running.";
3043 case NERR_LastAdmin:
3044 return "This operation is not allowed on the last administrative account.";
3045 case NERR_DCNotFound:
3046 return "Could not find domain controller for this domain.";
3047 case NERR_LogonTrackingError:
3048 return "Could not set logon information for this user.";
3049 case NERR_NetlogonNotStarted:
3050 return "The Netlogon service has not been started.";
3051 case NERR_CanNotGrowUASFile:
3052 return "Unable to add to the user accounts database.";
3053 case NERR_TimeDiffAtDC:
3054 return "This server's clock is not synchronized with the primary domain controller's clock.";
3055 case NERR_PasswordMismatch:
3056 return "A password mismatch has been detected.";
3057 case NERR_NoSuchServer:
3058 return "The server identification does not specify a valid server.";
3059 case NERR_NoSuchSession:
3060 return "The session identification does not specify a valid session.";
3061 case NERR_NoSuchConnection:
3062 return "The connection identification does not specify a valid connection.";
3063 case NERR_TooManyServers:
3064 return "There is no space for another entry in the table of available servers.";
3065 case NERR_TooManySessions:
3066 return "The server has reached the maximum number of sessions it supports.";
3067 case NERR_TooManyConnections:
3068 return "The server has reached the maximum number of connections it supports.";
3069 case NERR_TooManyFiles:
3070 return "The server cannot open more files because it has reached its maximum number.";
3071 case NERR_NoAlternateServers:
3072 return "There are no alternate servers registered on this server.";
3073 case NERR_TryDownLevel:
3074 return "Try down-level (remote admin protocol) version of API instead.";
3075 case NERR_UPSDriverNotStarted:
3076 return "The UPS driver could not be accessed by the UPS service.";
3077 case NERR_UPSInvalidConfig:
3078 return "The UPS service is not configured correctly.";
3079 case NERR_UPSInvalidCommPort:
3080 return "The UPS service could not access the specified Comm Port.";
3081 case NERR_UPSSignalAsserted:
3082 return "The UPS indicated a line fail or low battery situation. Service not started.";
3083 case NERR_UPSShutdownFailed:
3084 return "The UPS service failed to perform a system shut down.";
3085 case NERR_BadDosRetCode:
3086 return "The program below returned an MS-DOS error code:";
3087 case NERR_ProgNeedsExtraMem:
3088 return "The program below needs more memory:";
3089 case NERR_BadDosFunction:
3090 return "The program below called an unsupported MS-DOS function:";
3091 case NERR_RemoteBootFailed:
3092 return "The workstation failed to boot.";
3093 case NERR_BadFileCheckSum:
3094 return "The file below is corrupt.";
3095 case NERR_NoRplBootSystem:
3096 return "No loader is specified in the boot-block definition file.";
3097 case NERR_RplLoadrNetBiosErr:
3098 return "NetBIOS returned an error: The NCB and SMB are dumped above.";
3099 case NERR_RplLoadrDiskErr:
3100 return "A disk I/O error occurred.";
3101 case NERR_ImageParamErr:
3102 return "Image parameter substitution failed.";
3103 case NERR_TooManyImageParams:
3104 return "Too many image parameters cross disk sector boundaries.";
3105 case NERR_NonDosFloppyUsed:
3106 return "The image was not generated from an MS-DOS diskette formatted with /S.";
3107 case NERR_RplBootRestart:
3108 return "Remote boot will be restarted later.";
3109 case NERR_RplSrvrCallFailed:
3110 return "The call to the Remoteboot server failed.";
3111 case NERR_CantConnectRplSrvr:
3112 return "Cannot connect to the Remoteboot server.";
3113 case NERR_CantOpenImageFile:
3114 return "Cannot open image file on the Remoteboot server.";
3115 case NERR_CallingRplSrvr:
3116 return "Connecting to the Remoteboot server...";
3117 case NERR_StartingRplBoot:
3118 return "Connecting to the Remoteboot server...";
3119 case NERR_RplBootServiceTerm:
3120 return "Remote boot service was stopped; check the error log for the cause of the problem.";
3121 case NERR_RplBootStartFailed:
3122 return "Remote boot startup failed; check the error log for the cause of the problem.";
3123 case NERR_RPL_CONNECTED:
3124 return "A second connection to a Remoteboot resource is not allowed.";
3125 case NERR_BrowserConfiguredToNotRun:
3126 return "The browser service was configured with MaintainServerList=No.";
3127 case NERR_RplNoAdaptersStarted:
3128 return "Service failed to start since none of the network adapters started with this service.";
3129 case NERR_RplBadRegistry:
3130 return "Service failed to start due to bad startup information in the registry.";
3131 case NERR_RplBadDatabase:
3132 return "Service failed to start because its database is absent or corrupt.";
3133 case NERR_RplRplfilesShare:
3134 return "Service failed to start because RPLFILES share is absent.";
3135 case NERR_RplNotRplServer:
3136 return "Service failed to start because RPLUSER group is absent.";
3137 case NERR_RplCannotEnum:
3138 return "Cannot enumerate service records.";
3139 case NERR_RplWkstaInfoCorrupted:
3140 return "Workstation record information has been corrupted.";
3141 case NERR_RplWkstaNotFound:
3142 return "Workstation record was not found.";
3143 case NERR_RplWkstaNameUnavailable:
3144 return "Workstation name is in use by some other workstation.";
3145 case NERR_RplProfileInfoCorrupted:
3146 return "Profile record information has been corrupted.";
3147 case NERR_RplProfileNotFound:
3148 return "Profile record was not found.";
3149 case NERR_RplProfileNameUnavailable:
3150 return "Profile name is in use by some other profile.";
3151 case NERR_RplProfileNotEmpty:
3152 return "There are workstations using this profile.";
3153 case NERR_RplConfigInfoCorrupted:
3154 return "Configuration record information has been corrupted.";
3155 case NERR_RplConfigNotFound:
3156 return "Configuration record was not found.";
3157 case NERR_RplAdapterInfoCorrupted:
3158 return "Adapter ID record information has been corrupted.";
3159 case NERR_RplInternal:
3160 return "An internal service error has occurred.";
3161 case NERR_RplVendorInfoCorrupted:
3162 return "Vendor ID record information has been corrupted.";
3163 case NERR_RplBootInfoCorrupted:
3164 return "Boot block record information has been corrupted.";
3165 case NERR_RplWkstaNeedsUserAcct:
3166 return "The user account for this workstation record is missing.";
3167 case NERR_RplNeedsRPLUSERAcct:
3168 return "The RPLUSER local group could not be found.";
3169 case NERR_RplBootNotFound:
3170 return "Boot block record was not found.";
3171 case NERR_RplIncompatibleProfile:
3172 return "Chosen profile is incompatible with this workstation.";
3173 case NERR_RplAdapterNameUnavailable:
3174 return "Chosen network adapter ID is in use by some other workstation.";
3175 case NERR_RplConfigNotEmpty:
3176 return "There are profiles using this configuration.";
3177 case NERR_RplBootInUse:
3178 return "There are workstations, profiles, or configurations using this boot block.";
3179 case NERR_RplBackupDatabase:
3180 return "Service failed to backup Remoteboot database.";
3181 case NERR_RplAdapterNotFound:
3182 return "Adapter record was not found.";
3183 case NERR_RplVendorNotFound:
3184 return "Vendor record was not found.";
3185 case NERR_RplVendorNameUnavailable:
3186 return "Vendor name is in use by some other vendor record.";
3187 case NERR_RplBootNameUnavailable:
3188 return "(boot name, vendor ID) is in use by some other boot block record.";
3189 case NERR_RplConfigNameUnavailable:
3190 return "Configuration name is in use by some other configuration.";
3191 case NERR_DfsInternalCorruption:
3192 return "The internal database maintained by the Dfs service is corrupt.";
3193 case NERR_DfsVolumeDataCorrupt:
3194 return "One of the records in the internal Dfs database is corrupt.";
3195 case NERR_DfsNoSuchVolume:
3196 return "There is no DFS name whose entry path matches the input Entry Path.";
3197 case NERR_DfsVolumeAlreadyExists:
3198 return "A root or link with the given name already exists.";
3199 case NERR_DfsAlreadyShared:
3200 return "The server share specified is already shared in the Dfs.";
3201 case NERR_DfsNoSuchShare:
3202 return "The indicated server share does not support the indicated DFS namespace.";
3203 case NERR_DfsNotALeafVolume:
3204 return "The operation is not valid on this portion of the namespace.";
3205 case NERR_DfsLeafVolume:
3206 return "The operation is not valid on this portion of the namespace.";
3207 case NERR_DfsVolumeHasMultipleServers:
3208 return "The operation is ambiguous because the link has multiple servers.";
3209 case NERR_DfsCantCreateJunctionPoint:
3210 return "Unable to create a link.";
3211 case NERR_DfsServerNotDfsAware:
3212 return "The server is not Dfs Aware.";
3213 case NERR_DfsBadRenamePath:
3214 return "The specified rename target path is invalid.";
3215 case NERR_DfsVolumeIsOffline:
3216 return "The specified DFS link is offline.";
3217 case NERR_DfsNoSuchServer:
3218 return "The specified server is not a server for this link.";
3219 case NERR_DfsCyclicalName:
3220 return "A cycle in the Dfs name was detected.";
3221 case NERR_DfsNotSupportedInServerDfs:
3222 return "The operation is not supported on a server-based Dfs.";
3223 case NERR_DfsDuplicateService:
3224 return "This link is already supported by the specified server-share.";
3225 case NERR_DfsCantRemoveLastServerShare:
3226 return "Can't remove the last server-share supporting this root or link.";
3227 case NERR_DfsVolumeIsInterDfs:
3228 return "The operation is not supported for an Inter-DFS link.";
3229 case NERR_DfsInconsistent:
3230 return "The internal state of the Dfs Service has become inconsistent.";
3231 case NERR_DfsServerUpgraded:
3232 return "The Dfs Service has been installed on the specified server.";
3233 case NERR_DfsDataIsIdentical:
3234 return "The Dfs data being reconciled is identical.";
3235 case NERR_DfsCantRemoveDfsRoot:
3236 return "The DFS root cannot be deleted. Uninstall DFS if required.";
3237 case NERR_DfsChildOrParentInDfs:
3238 return "A child or parent directory of the share is already in a Dfs.";
3239 case NERR_DfsInternalError:
3240 return "Dfs internal error.";
3241 /* the following are not defined in mingw */
3244 case NERR_SetupAlreadyJoined:
3245 return "This machine is already joined to a domain.";
3246 case NERR_SetupNotJoined:
3247 return "This machine is not currently joined to a domain.";
3248 case NERR_SetupDomainController:
3249 return "This machine is a domain controller and cannot be unjoined from a domain.";
3250 case NERR_DefaultJoinRequired:
3251 return "The destination domain controller does not support creating machine accounts in OUs.";
3252 case NERR_InvalidWorkgroupName:
3253 return "The specified workgroup name is invalid.";
3254 case NERR_NameUsesIncompatibleCodePage:
3255 return "The specified computer name is incompatible with the default language used on the domain controller.";
3256 case NERR_ComputerAccountNotFound:
3257 return "The specified computer account could not be found.";
3258 case NERR_PersonalSku:
3259 return "This version of Windows cannot be joined to a domain.";
3260 case NERR_PasswordMustChange:
3261 return "The password must change at the next logon.";
3262 case NERR_AccountLockedOut:
3263 return "The account is locked out.";
3264 case NERR_PasswordTooLong:
3265 return "The password is too long.";
3266 case NERR_PasswordNotComplexEnough:
3267 return "The password does not meet the complexity policy.";
3268 case NERR_PasswordFilterError:
3269 return "The password does not meet the requirements of the password filter DLLs.";
3273 msg = strerror (error_number);
3289 /******************************************************************************
3291 Original CVS version of dbus-sysdeps.c
3293 ******************************************************************************/
3294 /* -*- mode: C; c-file-style: "gnu" -*- */
3295 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-Bus implementation)
3297 * Copyright (C) 2002, 2003 Red Hat, Inc.
3298 * Copyright (C) 2003 CodeFactory AB
3299 * Copyright (C) 2005 Novell, Inc.
3301 * Licensed under the Academic Free License version 2.1
3303 * This program is free software; you can redistribute it and/or modify
3304 * it under the terms of the GNU General Public License as published by
3305 * the Free Software Foundation; either version 2 of the License, or
3306 * (at your option) any later version.
3308 * This program is distributed in the hope that it will be useful,
3309 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3310 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3311 * GNU General Public License for more details.
3313 * You should have received a copy of the GNU General Public License
3314 * along with this program; if not, write to the Free Software
3315 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3321 * @addtogroup DBusInternalsUtils
3325 int _dbus_mkdir (const char *path,
3328 return _mkdir(path);
3332 * Exit the process, returning the given value.
3334 * @param code the exit code
3337 _dbus_exit (int code)
3343 * Creates a socket and connects to a socket at the given host
3344 * and port. The connection fd is returned, and is set up as
3347 * @param host the host name to connect to, NULL for loopback
3348 * @param port the prot to connect to
3349 * @param error return location for error code
3350 * @returns connection file descriptor or -1 on error
3353 _dbus_connect_tcp_socket (const char *host,
3359 struct sockaddr_in addr;
3361 struct in_addr *haddr;
3364 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3366 _dbus_win_startup_winsock ();
3368 s.fd = socket (AF_INET, SOCK_STREAM, 0);
3370 if (DBUS_SOCKET_IS_INVALID (s.fd))
3372 DBUS_SOCKET_SET_ERRNO ();
3373 dbus_set_error (error,
3374 _dbus_error_from_errno (errno),
3375 "Failed to create socket: %s",
3376 _dbus_strerror (errno));
3384 ina.s_addr = htonl (INADDR_LOOPBACK);
3388 he = gethostbyname (host);
3391 DBUS_SOCKET_SET_ERRNO ();
3392 dbus_set_error (error,
3393 _dbus_error_from_errno (errno),
3394 "Failed to lookup hostname: %s",
3396 DBUS_CLOSE_SOCKET (s.fd);
3400 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3403 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
3404 addr.sin_family = AF_INET;
3405 addr.sin_port = htons (port);
3407 if (DBUS_SOCKET_API_RETURNS_ERROR
3408 (connect (s.fd, (struct sockaddr*) &addr, sizeof (addr)) < 0))
3410 DBUS_SOCKET_SET_ERRNO ();
3411 dbus_set_error (error,
3412 _dbus_error_from_errno (errno),
3413 "Failed to connect to socket %s:%d %s",
3414 host, port, _dbus_strerror (errno));
3416 DBUS_CLOSE_SOCKET (s.fd);
3422 handle = _dbus_socket_to_handle (&s);
3424 if (!_dbus_set_fd_nonblocking (handle, error))
3426 _dbus_close_socket (handle, NULL);
3436 _dbus_daemon_init(const char *host, dbus_uint32_t port);
3438 * Creates a socket and binds it to the given port,
3439 * then listens on the socket. The socket is
3440 * set to be nonblocking.
3442 * @param host the interface to listen on, NULL for loopback, empty for any
3443 * @param port the port to listen on, if zero a free port will be used
3444 * @param error return location for errors
3445 * @returns the listening file descriptor or -1 on error
3449 _dbus_listen_tcp_socket (const char *host,
3455 struct sockaddr_in addr;
3457 struct in_addr *haddr;
3458 int len = sizeof (struct sockaddr);
3462 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3464 _dbus_win_startup_winsock ();
3466 slisten.fd = socket (AF_INET, SOCK_STREAM, 0);
3468 if (DBUS_SOCKET_IS_INVALID (slisten.fd))
3470 DBUS_SOCKET_SET_ERRNO ();
3471 dbus_set_error (error, _dbus_error_from_errno (errno),
3472 "Failed to create socket \"%s:%d\": %s",
3473 host, port, _dbus_strerror (errno));
3479 ina.s_addr = htonl (INADDR_LOOPBACK);
3484 ina.s_addr = htonl (INADDR_ANY);
3489 he = gethostbyname (host);
3492 DBUS_SOCKET_SET_ERRNO ();
3493 dbus_set_error (error,
3494 _dbus_error_from_errno (errno),
3495 "Failed to lookup hostname: %s",
3497 DBUS_CLOSE_SOCKET (slisten.fd);
3501 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3505 memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
3506 addr.sin_family = AF_INET;
3507 addr.sin_port = htons (port);
3509 if (bind (slisten.fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
3511 DBUS_SOCKET_SET_ERRNO ();
3512 dbus_set_error (error, _dbus_error_from_errno (errno),
3513 "Failed to bind socket \"%s:%d\": %s",
3514 host, port, _dbus_strerror (errno));
3515 DBUS_CLOSE_SOCKET (slisten.fd);
3519 if (DBUS_SOCKET_API_RETURNS_ERROR (listen (slisten.fd, 30 /* backlog */)))
3521 DBUS_SOCKET_SET_ERRNO ();
3522 dbus_set_error (error, _dbus_error_from_errno (errno),
3523 "Failed to listen on socket \"%s:%d\": %s",
3524 host, port, _dbus_strerror (errno));
3525 DBUS_CLOSE_SOCKET (slisten.fd);
3530 getsockname(slisten.fd, (struct sockaddr*) &addr, &len);
3532 _dbus_daemon_init(host, ntohs(addr.sin_port));
3534 handle = _dbus_socket_to_handle (&slisten);
3536 if (!_dbus_set_fd_nonblocking (handle, error))
3538 _dbus_close_socket (handle, NULL);
3546 * Accepts a connection on a listening socket.
3547 * Handles EINTR for you.
3549 * @param listen_fd the listen file descriptor
3550 * @returns the connection fd of the client, or -1 on error
3553 _dbus_accept (int listen_handle)
3555 DBusSocket *slisten;
3557 struct sockaddr addr;
3560 _dbus_handle_to_socket(listen_handle, &slisten);
3562 addrlen = sizeof (addr);
3564 //FIXME: why do we not try it again on Windows?
3565 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3569 sclient.fd = accept (slisten->fd, &addr, &addrlen);
3571 if (DBUS_SOCKET_IS_INVALID (sclient.fd))
3573 DBUS_SOCKET_SET_ERRNO ();
3574 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3582 return _dbus_socket_to_handle (&sclient);
3588 write_credentials_byte (int server_fd,
3591 /* FIXME: for the session bus credentials shouldn't matter (?), but
3592 * for the system bus they are presumably essential. A rough outline
3593 * of a way to implement the credential transfer would be this:
3595 * client waits to *read* a byte.
3597 * server creates a named pipe with a random name, sends a byte
3598 * contining its length, and its name.
3600 * client reads the name, connects to it (using Win32 API).
3602 * server waits for connection to the named pipe, then calls
3603 * ImpersonateNamedPipeClient(), notes its now-current credentials,
3604 * calls RevertToSelf(), closes its handles to the named pipe, and
3605 * is done. (Maybe there is some other way to get the SID of a named
3606 * pipe client without having to use impersonation?)
3608 * client closes its handles and is done.
3616 * Reads a single byte which must be nul (an error occurs otherwise),
3617 * and reads unix credentials if available. Fills in pid/uid/gid with
3618 * -1 if no credentials are available. Return value indicates whether
3619 * a byte was read, not whether we got valid credentials. On some
3620 * systems, such as Linux, reading/writing the byte isn't actually
3621 * required, but we do it anyway just to avoid multiple codepaths.
3623 * Fails if no byte is available, so you must select() first.
3625 * The point of the byte is that on some systems we have to
3626 * use sendmsg()/recvmsg() to transmit credentials.
3628 * @param client_fd the client file descriptor
3629 * @param credentials struct to fill with credentials of client
3630 * @param error location to store error code
3631 * @returns #TRUE on success
3634 _dbus_read_credentials_unix_socket (int client_fd,
3635 DBusCredentials *credentials,
3638 /* FIXME bogus testing credentials */
3639 _dbus_credentials_from_current_process (credentials);
3645 * Checks to make sure the given directory is
3646 * private to the user
3648 * @param dir the name of the directory
3649 * @param error error return
3650 * @returns #FALSE on failure
3653 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
3655 const char *directory;
3658 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3665 * Gets user info for the given user ID.
3667 * @param info user info object to initialize
3668 * @param uid the user ID
3669 * @param error error return
3670 * @returns #TRUE on success
3673 _dbus_user_info_fill_uid (DBusUserInfo *info,
3677 return fill_user_info (info, uid,
3682 * Gets user info for the given username.
3684 * @param info user info object to initialize
3685 * @param username the username
3686 * @param error error return
3687 * @returns #TRUE on success
3690 _dbus_user_info_fill (DBusUserInfo *info,
3691 const DBusString *username,
3694 return fill_user_info (info, DBUS_UID_UNSET,
3700 fill_user_info (DBusUserInfo *info,
3702 const DBusString *username,
3705 const char *username_c;
3707 /* exactly one of username/uid provided */
3708 _dbus_assert (username != NULL || uid != DBUS_UID_UNSET);
3709 _dbus_assert (username == NULL || uid == DBUS_UID_UNSET);
3711 info->uid = DBUS_UID_UNSET;
3712 info->primary_gid = DBUS_GID_UNSET;
3713 info->group_ids = NULL;
3714 info->n_group_ids = 0;
3715 info->username = NULL;
3716 info->homedir = NULL;
3718 if (username != NULL)
3719 username_c = _dbus_string_get_const_data (username);
3723 if (uid != DBUS_UID_UNSET)
3725 if (!fill_win_user_info_from_uid (uid, info, error))
3727 _dbus_verbose("%s after fill_win_user_info_from_uid\n",__FUNCTION__);
3733 wchar_t *wname = _dbus_win_utf8_to_utf16 (username_c, error);
3738 if (!fill_win_user_info_from_name (wname, info, error))
3751 * Appends the given filename to the given directory.
3753 * @todo it might be cute to collapse multiple '/' such as "foo//"
3756 * @param dir the directory name
3757 * @param next_component the filename
3758 * @returns #TRUE on success
3761 _dbus_concat_dir_and_file (DBusString *dir,
3762 const DBusString *next_component)
3764 dbus_bool_t dir_ends_in_slash;
3765 dbus_bool_t file_starts_with_slash;
3767 if (_dbus_string_get_length (dir) == 0 ||
3768 _dbus_string_get_length (next_component) == 0)
3772 ('/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1) ||
3773 '\\' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1));
3775 file_starts_with_slash =
3776 ('/' == _dbus_string_get_byte (next_component, 0) ||
3777 '\\' == _dbus_string_get_byte (next_component, 0));
3779 if (dir_ends_in_slash && file_starts_with_slash)
3781 _dbus_string_shorten (dir, 1);
3783 else if (!(dir_ends_in_slash || file_starts_with_slash))
3785 if (!_dbus_string_append_byte (dir, '\\'))
3789 return _dbus_string_copy (next_component, 0, dir,
3790 _dbus_string_get_length (dir));
3797 * Gets our process ID
3798 * @returns process ID
3803 return GetCurrentProcessId ();
3806 /** nanoseconds in a second */
3807 #define NANOSECONDS_PER_SECOND 1000000000
3808 /** microseconds in a second */
3809 #define MICROSECONDS_PER_SECOND 1000000
3810 /** milliseconds in a second */
3811 #define MILLISECONDS_PER_SECOND 1000
3812 /** nanoseconds in a millisecond */
3813 #define NANOSECONDS_PER_MILLISECOND 1000000
3814 /** microseconds in a millisecond */
3815 #define MICROSECONDS_PER_MILLISECOND 1000
3818 * Sleeps the given number of milliseconds.
3819 * @param milliseconds number of milliseconds
3822 _dbus_sleep_milliseconds (int milliseconds)
3824 Sleep (milliseconds);
3829 * Get current time, as in gettimeofday().
3831 * @param tv_sec return location for number of seconds
3832 * @param tv_usec return location for number of microseconds
3835 _dbus_get_current_time (long *tv_sec,
3839 dbus_uint64_t *time64 = (dbus_uint64_t *) &ft;
3841 GetSystemTimeAsFileTime (&ft);
3843 /* Convert from 100s of nanoseconds since 1601-01-01
3844 * to Unix epoch. Yes, this is Y2038 unsafe.
3846 *time64 -= DBUS_INT64_CONSTANT (116444736000000000);
3850 *tv_sec = *time64 / 1000000;
3853 *tv_usec = *time64 % 1000000;
3858 * signal (SIGPIPE, SIG_IGN);
3861 _dbus_disable_sigpipe (void)
3863 _dbus_verbose("FIXME: implement _dbus_disable_sigpipe (void)\n");
3867 * Gets the credentials of the current process.
3869 * @param credentials credentials to fill in.
3872 _dbus_credentials_from_current_process (DBusCredentials *credentials)
3874 credentials->pid = _dbus_getpid ();
3875 credentials->uid = _dbus_getuid ();
3876 credentials->gid = _dbus_getgid ();
3880 * Appends the contents of the given file to the string,
3881 * returning error code. At the moment, won't open a file
3882 * more than a megabyte in size.
3884 * @param str the string to append to
3885 * @param filename filename to load
3886 * @param error place to set an error
3887 * @returns #FALSE if error was set
3890 _dbus_file_get_contents (DBusString *str,
3891 const DBusString *filename,
3898 const char *filename_c;
3900 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3902 filename_c = _dbus_string_get_const_data (filename);
3904 /* O_BINARY useful on Cygwin and Win32 */
3905 if (!_dbus_open_file (&file, filename_c, O_RDONLY | O_BINARY, -1))
3907 dbus_set_error (error, _dbus_error_from_errno (errno),
3908 "Failed to open \"%s\": %s",
3910 _dbus_strerror (errno));
3914 if (!_dbus_fstat (&file, &sb))
3916 dbus_set_error (error, _dbus_error_from_errno (errno),
3917 "Failed to stat \"%s\": %s",
3919 _dbus_strerror (errno));
3921 _dbus_verbose ("fstat() failed: %s",
3922 _dbus_strerror (errno));
3924 _dbus_close_file (&file, NULL);
3929 if (sb.st_size > _DBUS_ONE_MEGABYTE)
3931 dbus_set_error (error, DBUS_ERROR_FAILED,
3932 "File size %lu of \"%s\" is too large.",
3933 (unsigned long) sb.st_size, filename_c);
3934 _dbus_close_file (&file, NULL);
3939 orig_len = _dbus_string_get_length (str);
3940 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
3944 while (total < (int) sb.st_size)
3946 bytes_read = _dbus_read_file (&file, str,
3947 sb.st_size - total);
3948 if (bytes_read <= 0)
3950 dbus_set_error (error, _dbus_error_from_errno (errno),
3951 "Error reading \"%s\": %s",
3953 _dbus_strerror (errno));
3955 _dbus_verbose ("read() failed: %s",
3956 _dbus_strerror (errno));
3958 _dbus_close_file (&file, NULL);
3959 _dbus_string_set_length (str, orig_len);
3963 total += bytes_read;
3966 _dbus_close_file (&file, NULL);
3969 else if (sb.st_size != 0)
3971 _dbus_verbose ("Can only open regular files at the moment.\n");
3972 dbus_set_error (error, DBUS_ERROR_FAILED,
3973 "\"%s\" is not a regular file",
3975 _dbus_close_file (&file, NULL);
3980 _dbus_close_file (&file, NULL);
3986 * Writes a string out to a file. If the file exists,
3987 * it will be atomically overwritten by the new data.
3989 * @param str the string to write out
3990 * @param filename the file to save string to
3991 * @param error error to be filled in on failure
3992 * @returns #FALSE on failure
3995 _dbus_string_save_to_file (const DBusString *str,
3996 const DBusString *filename,
4001 const char *filename_c;
4002 DBusString tmp_filename;
4003 const char *tmp_filename_c;
4005 dbus_bool_t need_unlink;
4008 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4011 need_unlink = FALSE;
4013 if (!_dbus_string_init (&tmp_filename))
4015 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4019 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
4021 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4022 _dbus_string_free (&tmp_filename);
4026 if (!_dbus_string_append (&tmp_filename, "."))
4028 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4029 _dbus_string_free (&tmp_filename);
4033 #define N_TMP_FILENAME_RANDOM_BYTES 8
4034 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
4036 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4037 _dbus_string_free (&tmp_filename);
4041 filename_c = _dbus_string_get_const_data (filename);
4042 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
4044 if (!_dbus_open_file (&file, tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4047 dbus_set_error (error, _dbus_error_from_errno (errno),
4048 "Could not create %s: %s", tmp_filename_c,
4049 _dbus_strerror (errno));
4056 bytes_to_write = _dbus_string_get_length (str);
4058 while (total < bytes_to_write)
4062 bytes_written = _dbus_write_file (&file, str, total,
4063 bytes_to_write - total);
4065 if (bytes_written <= 0)
4067 dbus_set_error (error, _dbus_error_from_errno (errno),
4068 "Could not write to %s: %s", tmp_filename_c,
4069 _dbus_strerror (errno));
4074 total += bytes_written;
4077 if (!_dbus_close_file (&file, NULL))
4079 dbus_set_error (error, _dbus_error_from_errno (errno),
4080 "Could not close file %s: %s",
4081 tmp_filename_c, _dbus_strerror (errno));
4087 if ((unlink (filename_c) == -1 && errno != ENOENT) ||
4088 rename (tmp_filename_c, filename_c) < 0)
4090 dbus_set_error (error, _dbus_error_from_errno (errno),
4091 "Could not rename %s to %s: %s",
4092 tmp_filename_c, filename_c,
4093 _dbus_strerror (errno));
4098 need_unlink = FALSE;
4103 /* close first, then unlink, to prevent ".nfs34234235" garbage
4107 if (_dbus_is_valid_file(&file))
4108 _dbus_close_file (&file, NULL);
4110 if (need_unlink && unlink (tmp_filename_c) < 0)
4111 _dbus_verbose ("Failed to unlink temp file %s: %s\n",
4112 tmp_filename_c, _dbus_strerror (errno));
4114 _dbus_string_free (&tmp_filename);
4117 _DBUS_ASSERT_ERROR_IS_SET (error);
4123 /** Creates the given file, failing if the file already exists.
4125 * @param filename the filename
4126 * @param error error location
4127 * @returns #TRUE if we created the file and it didn't exist
4130 _dbus_create_file_exclusively (const DBusString *filename,
4134 const char *filename_c;
4136 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4138 filename_c = _dbus_string_get_const_data (filename);
4140 if (!_dbus_open_file (&file, filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4143 dbus_set_error (error,
4145 "Could not create file %s: %s\n",
4147 _dbus_strerror (errno));
4151 if (!_dbus_close_file (&file, NULL))
4153 dbus_set_error (error,
4155 "Could not close file %s: %s\n",
4157 _dbus_strerror (errno));
4166 * Creates a directory; succeeds if the directory
4167 * is created or already existed.
4169 * @param filename directory filename
4170 * @param error initialized error object
4171 * @returns #TRUE on success
4174 _dbus_create_directory (const DBusString *filename,
4177 const char *filename_c;
4179 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4181 filename_c = _dbus_string_get_const_data (filename);
4183 if (_dbus_mkdir (filename_c, 0700) < 0)
4185 if (errno == EEXIST)
4188 dbus_set_error (error, DBUS_ERROR_FAILED,
4189 "Failed to create directory %s: %s\n",
4190 filename_c, _dbus_strerror (errno));
4199 pseudorandom_generate_random_bytes_buffer (char *buffer,
4205 /* fall back to pseudorandom */
4206 _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
4209 _dbus_get_current_time (NULL, &tv_usec);
4219 b = (r / (double) RAND_MAX) * 255.0;
4228 pseudorandom_generate_random_bytes (DBusString *str,
4234 old_len = _dbus_string_get_length (str);
4236 if (!_dbus_string_lengthen (str, n_bytes))
4239 p = _dbus_string_get_data_len (str, old_len, n_bytes);
4241 pseudorandom_generate_random_bytes_buffer (p, n_bytes);
4247 * Gets the temporary files directory by inspecting the environment variables
4248 * TMPDIR, TMP, and TEMP in that order. If none of those are set "/tmp" is returned
4250 * @returns location of temp directory
4253 _dbus_get_tmpdir(void)
4255 static const char* tmpdir = NULL;
4260 tmpdir = getenv("TMP");
4262 tmpdir = getenv("TEMP");
4264 tmpdir = getenv("TMPDIR");
4266 tmpdir = "C:\\Temp";
4269 _dbus_assert(tmpdir != NULL);
4276 * Deletes the given file.
4278 * @param filename the filename
4279 * @param error error location
4281 * @returns #TRUE if unlink() succeeded
4284 _dbus_delete_file (const DBusString *filename,
4287 const char *filename_c;
4289 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4291 filename_c = _dbus_string_get_const_data (filename);
4293 if (unlink (filename_c) < 0)
4295 dbus_set_error (error, DBUS_ERROR_FAILED,
4296 "Failed to delete file %s: %s\n",
4297 filename_c, _dbus_strerror (errno));
4305 * Generates the given number of random bytes,
4306 * using the best mechanism we can come up with.
4308 * @param str the string
4309 * @param n_bytes the number of random bytes to append to string
4310 * @returns #TRUE on success, #FALSE if no memory
4313 _dbus_generate_random_bytes (DBusString *str,
4316 return pseudorandom_generate_random_bytes (str, n_bytes);
4319 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_BUILD_TESTS)
4331 * Backtrace Generator
4333 * Copyright 2004 Eric Poech
4334 * Copyright 2004 Robert Shearman
4336 * This library is free software; you can redistribute it and/or
4337 * modify it under the terms of the GNU Lesser General Public
4338 * License as published by the Free Software Foundation; either
4339 * version 2.1 of the License, or (at your option) any later version.
4341 * This library is distributed in the hope that it will be useful,
4342 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4343 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4344 * Lesser General Public License for more details.
4346 * You should have received a copy of the GNU Lesser General Public
4347 * License along with this library; if not, write to the Free Software
4348 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4352 #include <imagehlp.h>
4355 #define DPRINTF _dbus_warn
4363 //#define MAKE_FUNCPTR(f) static typeof(f) * p##f
4365 //MAKE_FUNCPTR(StackWalk);
4366 //MAKE_FUNCPTR(SymGetModuleBase);
4367 //MAKE_FUNCPTR(SymFunctionTableAccess);
4368 //MAKE_FUNCPTR(SymInitialize);
4369 //MAKE_FUNCPTR(SymGetSymFromAddr);
4370 //MAKE_FUNCPTR(SymGetModuleInfo);
4371 static BOOL (WINAPI *pStackWalk)(
4375 LPSTACKFRAME StackFrame,
4376 PVOID ContextRecord,
4377 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4378 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4379 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4380 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4382 static DWORD (WINAPI *pSymGetModuleBase)(
4386 static PVOID (WINAPI *pSymFunctionTableAccess)(
4390 static BOOL (WINAPI *pSymInitialize)(
4392 PSTR UserSearchPath,
4395 static BOOL (WINAPI *pSymGetSymFromAddr)(
4398 PDWORD Displacement,
4399 PIMAGEHLP_SYMBOL Symbol
4401 static BOOL (WINAPI *pSymGetModuleInfo)(
4404 PIMAGEHLP_MODULE ModuleInfo
4406 static DWORD (WINAPI *pSymSetOptions)(
4411 static BOOL init_backtrace()
4413 HMODULE hmodDbgHelp = LoadLibraryA("dbghelp");
4415 #define GETFUNC(x) \
4416 p##x = (typeof(x)*)GetProcAddress(hmodDbgHelp, #x); \
4424 // GETFUNC(StackWalk);
4425 // GETFUNC(SymGetModuleBase);
4426 // GETFUNC(SymFunctionTableAccess);
4427 // GETFUNC(SymInitialize);
4428 // GETFUNC(SymGetSymFromAddr);
4429 // GETFUNC(SymGetModuleInfo);
4433 pStackWalk = (BOOL (WINAPI *)(
4437 LPSTACKFRAME StackFrame,
4438 PVOID ContextRecord,
4439 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4440 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4441 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4442 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4443 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
4444 pSymGetModuleBase=(DWORD (WINAPI *)(
4447 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
4448 pSymFunctionTableAccess=(PVOID (WINAPI *)(
4451 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
4452 pSymInitialize = (BOOL (WINAPI *)(
4454 PSTR UserSearchPath,
4456 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
4457 pSymGetSymFromAddr = (BOOL (WINAPI *)(
4460 PDWORD Displacement,
4461 PIMAGEHLP_SYMBOL Symbol
4462 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
4463 pSymGetModuleInfo = (BOOL (WINAPI *)(
4466 PIMAGEHLP_MODULE ModuleInfo
4467 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
4468 pSymSetOptions = (DWORD (WINAPI *)(
4470 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
4473 pSymSetOptions(SYMOPT_UNDNAME);
4475 pSymInitialize(GetCurrentProcess(), NULL, TRUE);
4480 static void dump_backtrace_for_thread(HANDLE hThread)
4487 if (!init_backtrace())
4490 /* can't use this function for current thread as GetThreadContext
4491 * doesn't support getting context from current thread */
4492 if (hThread == GetCurrentThread())
4495 DPRINTF("Backtrace:\n");
4497 memset(&context, 0, sizeof(context));
4498 context.ContextFlags = CONTEXT_FULL;
4500 SuspendThread(hThread);
4502 if (!GetThreadContext(hThread, &context))
4504 DPRINTF("Couldn't get thread context (error %ld)\n", GetLastError());
4505 ResumeThread(hThread);
4509 memset(&sf, 0, sizeof(sf));
4512 sf.AddrFrame.Offset = context.Ebp;
4513 sf.AddrFrame.Mode = AddrModeFlat;
4514 sf.AddrPC.Offset = context.Eip;
4515 sf.AddrPC.Mode = AddrModeFlat;
4516 dwImageType = IMAGE_FILE_MACHINE_I386;
4518 # error You need to fill in the STACKFRAME structure for your architecture
4521 while (pStackWalk(dwImageType, GetCurrentProcess(),
4522 hThread, &sf, &context, NULL, pSymFunctionTableAccess,
4523 pSymGetModuleBase, NULL))
4526 IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
4527 DWORD dwDisplacement;
4529 pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
4530 pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1;
4532 if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
4533 &dwDisplacement, pSymbol))
4535 IMAGEHLP_MODULE ModuleInfo;
4536 ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
4538 if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
4540 DPRINTF("1\t%p\n", (void*)sf.AddrPC.Offset);
4542 DPRINTF("2\t%s+0x%lx\n", ModuleInfo.ImageName,
4543 sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
4545 else if (dwDisplacement)
4546 DPRINTF("3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
4548 DPRINTF("4\t%s\n", pSymbol->Name);
4551 ResumeThread(hThread);
4554 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
4556 dump_backtrace_for_thread((HANDLE)lpParameter);
4560 /* cannot get valid context from current thread, so we have to execute
4561 * backtrace from another thread */
4562 static void dump_backtrace()
4564 HANDLE hCurrentThread;
4567 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
4568 GetCurrentProcess(), &hCurrentThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
4569 hThread = CreateThread(NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
4571 WaitForSingleObject(hThread, INFINITE);
4572 CloseHandle(hThread);
4573 CloseHandle(hCurrentThread);
4576 void _dbus_print_backtrace(void)
4582 void _dbus_print_backtrace(void)
4584 _dbus_verbose (" D-Bus not compiled with backtrace support\n");
4589 * Sends a single nul byte with our UNIX credentials as ancillary
4590 * data. Returns #TRUE if the data was successfully written. On
4591 * systems that don't support sending credentials, just writes a byte,
4592 * doesn't send any credentials. On some systems, such as Linux,
4593 * reading/writing the byte isn't actually required, but we do it
4594 * anyway just to avoid multiple codepaths.
4596 * Fails if no byte can be written, so you must select() first.
4598 * The point of the byte is that on some systems we have to
4599 * use sendmsg()/recvmsg() to transmit credentials.
4601 * @param server_fd file descriptor for connection to server
4602 * @param error return location for error code
4603 * @returns #TRUE if the byte was sent
4606 _dbus_send_credentials_unix_socket (int server_fd,
4609 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4611 if (write_credentials_byte (server_fd, error))
4617 static dbus_uint32_t fromAscii(char ascii)
4619 if(ascii >= '0' && ascii <= '9')
4621 if(ascii >= 'A' && ascii <= 'F')
4622 return ascii - 'A' + 10;
4623 if(ascii >= 'a' && ascii <= 'f')
4624 return ascii - 'a' + 10;
4628 dbus_bool_t _dbus_read_local_machine_uuid (DBusGUID *machine_id,
4629 dbus_bool_t create_if_not_found,
4636 HW_PROFILE_INFOA info;
4637 char *lpc = &info.szHwProfileGuid[0];
4640 // the hw-profile guid lives long enough
4641 if(!GetCurrentHwProfileA(&info))
4643 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); // FIXME
4647 // Form: {12340001-4980-1920-6788-123456789012}
4650 u = ((fromAscii(lpc[0]) << 0) |
4651 (fromAscii(lpc[1]) << 4) |
4652 (fromAscii(lpc[2]) << 8) |
4653 (fromAscii(lpc[3]) << 12) |
4654 (fromAscii(lpc[4]) << 16) |
4655 (fromAscii(lpc[5]) << 20) |
4656 (fromAscii(lpc[6]) << 24) |
4657 (fromAscii(lpc[7]) << 28));
4658 machine_id->as_uint32s[0] = u;
4662 u = ((fromAscii(lpc[0]) << 0) |
4663 (fromAscii(lpc[1]) << 4) |
4664 (fromAscii(lpc[2]) << 8) |
4665 (fromAscii(lpc[3]) << 12) |
4666 (fromAscii(lpc[5]) << 16) |
4667 (fromAscii(lpc[6]) << 20) |
4668 (fromAscii(lpc[7]) << 24) |
4669 (fromAscii(lpc[8]) << 28));
4670 machine_id->as_uint32s[1] = u;
4674 u = ((fromAscii(lpc[0]) << 0) |
4675 (fromAscii(lpc[1]) << 4) |
4676 (fromAscii(lpc[2]) << 8) |
4677 (fromAscii(lpc[3]) << 12) |
4678 (fromAscii(lpc[5]) << 16) |
4679 (fromAscii(lpc[6]) << 20) |
4680 (fromAscii(lpc[7]) << 24) |
4681 (fromAscii(lpc[8]) << 28));
4682 machine_id->as_uint32s[2] = u;
4686 u = ((fromAscii(lpc[0]) << 0) |
4687 (fromAscii(lpc[1]) << 4) |
4688 (fromAscii(lpc[2]) << 8) |
4689 (fromAscii(lpc[3]) << 12) |
4690 (fromAscii(lpc[4]) << 16) |
4691 (fromAscii(lpc[5]) << 20) |
4692 (fromAscii(lpc[6]) << 24) |
4693 (fromAscii(lpc[7]) << 28));
4694 machine_id->as_uint32s[3] = u;
4700 HANDLE _dbus_global_lock (const char *mutexname)
4705 mutex = CreateMutex( NULL, FALSE, mutexname );
4711 gotMutex = WaitForSingleObject( mutex, INFINITE );
4714 case WAIT_ABANDONED:
4715 ReleaseMutex (mutex);
4716 CloseHandle (mutex);
4727 void _dbus_global_unlock (HANDLE mutex)
4729 ReleaseMutex (mutex);
4730 CloseHandle (mutex);
4733 // for proper cleanup in dbus-daemon
4734 static HANDLE hDBusDaemonMutex = NULL;
4735 static HANDLE hDBusSharedMem = NULL;
4736 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4737 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
4738 // sync _dbus_get_autolaunch_address
4739 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
4740 // mutex to determine if dbus-daemon is already started (per user)
4741 static const char *cDBusDaemonMutex = "DBusDaemonMutex";
4742 // named shm for dbus adress info (per user)
4743 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
4746 _dbus_daemon_init(const char *host, dbus_uint32_t port)
4749 const char *adr = NULL;
4750 char szUserName[64];
4751 DWORD dwUserNameSize = sizeof(szUserName);
4752 char szDBusDaemonMutex[128];
4753 char szDBusDaemonAddressInfo[128];
4754 char szAddress[128];
4759 _snprintf(szAddress, sizeof(szAddress) - 1, "tcp:host=%s,port=%d", host, port);
4761 _dbus_assert( GetUserName(szUserName, &dwUserNameSize) != 0);
4762 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4763 cDBusDaemonMutex, szUserName);
4764 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4765 cDBusDaemonAddressInfo, szUserName);
4767 // before _dbus_global_lock to keep correct lock/release order
4768 hDBusDaemonMutex = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4770 _dbus_assert(WaitForSingleObject( hDBusDaemonMutex, 1000 ) == WAIT_OBJECT_0);
4772 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4773 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4776 hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
4777 0, strlen( szAddress ) + 1, szDBusDaemonAddressInfo );
4778 _dbus_assert( hDBusSharedMem );
4780 adr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
4782 _dbus_assert( adr );
4784 strcpy(adr, szAddress);
4787 UnmapViewOfFile( adr );
4789 _dbus_global_unlock( lock );
4793 _dbus_daemon_release()
4797 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4798 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4800 CloseHandle( hDBusSharedMem );
4802 hDBusSharedMem = NULL;
4804 ReleaseMutex( hDBusDaemonMutex );
4806 CloseHandle( hDBusDaemonMutex );
4808 hDBusDaemonMutex = NULL;
4810 _dbus_global_unlock( lock );
4814 _dbus_get_autolaunch_shm(DBusString *adress)
4818 char szUserName[64];
4819 DWORD dwUserNameSize = sizeof(szUserName);
4820 char szDBusDaemonAddressInfo[128];
4822 if( !GetUserName(szUserName, &dwUserNameSize) )
4824 _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4825 cDBusDaemonAddressInfo, szUserName);
4829 // we know that dbus-daemon is available, so we wait until shm is available
4830 sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, szDBusDaemonAddressInfo );
4831 if( sharedMem == 0 )
4833 } while( sharedMem == 0 );
4835 if( sharedMem == 0 )
4838 adr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
4843 _dbus_string_init( adress );
4845 _dbus_string_append( adress, adr );
4848 UnmapViewOfFile( adr );
4850 CloseHandle( sharedMem );
4856 _dbus_daemon_already_runs (DBusString *adress)
4860 dbus_bool_t bRet = TRUE;
4861 char szUserName[64];
4862 DWORD dwUserNameSize = sizeof(szUserName);
4863 char szDBusDaemonMutex[128];
4865 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4866 lock = _dbus_global_lock( cUniqueDBusInitMutex );
4868 if( !GetUserName(szUserName, &dwUserNameSize) )
4870 _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4871 cDBusDaemonMutex, szUserName);
4874 daemon = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4875 if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
4877 ReleaseMutex (daemon);
4878 CloseHandle (daemon);
4880 _dbus_global_unlock( lock );
4885 bRet = _dbus_get_autolaunch_shm( adress );
4888 CloseHandle ( daemon );
4890 _dbus_global_unlock( lock );
4896 _dbus_get_autolaunch_address (DBusString *address,
4901 PROCESS_INFORMATION pi;
4902 dbus_bool_t retval = FALSE;
4904 char dbus_exe_path[MAX_PATH];
4905 char dbus_args[MAX_PATH * 2];
4907 mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
4909 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4911 if (_dbus_daemon_already_runs(address))
4913 printf("dbus daemon already exists\n");
4918 if (!SearchPathA(NULL, "dbus-daemon.exe", NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
4920 printf ("could not find dbus-daemon executable\n");
4925 ZeroMemory( &si, sizeof(si) );
4927 ZeroMemory( &pi, sizeof(pi) );
4929 _snprintf(dbus_args, sizeof(dbus_args) - 1, "\"%s\" %s", dbus_exe_path, " --session");
4931 // argv[i] = "--config-file=bus\\session.conf";
4932 printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
4933 if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
4937 // Wait until started (see _dbus_get_autolaunch_shm())
4938 WaitForInputIdle(pi.hProcess, INFINITE);
4940 retval = _dbus_get_autolaunch_shm( address );
4947 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4949 _DBUS_ASSERT_ERROR_IS_SET (error);
4951 _dbus_global_unlock (mutex);
4957 /** Makes the file readable by every user in the system.
4959 * @param filename the filename
4960 * @param error error location
4961 * @returns #TRUE if the file's permissions could be changed.
4964 _dbus_make_file_world_readable(const DBusString *filename,
4972 #define DBUS_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
4974 // @TODO: this function is duplicated from dbus-sysdeps-unix.c
4975 // and differs only in the path separator, may be we should
4976 // use a dbus path separator variable
4978 #define _dbus_path_seperator ";"
4981 split_paths_and_append (DBusString *dirs,
4983 DBusList **dir_list)
4989 const DBusString file_suffix;
4994 _dbus_string_init_const (&file_suffix, suffix);
4996 len = _dbus_string_get_length (dirs);
4998 while (_dbus_string_find (dirs, start, _dbus_path_seperator, &i))
5002 if (!_dbus_string_init (&path))
5005 if (!_dbus_string_copy_len (dirs,
5011 _dbus_string_free (&path);
5015 _dbus_string_chop_white (&path);
5017 /* check for an empty path */
5018 if (_dbus_string_get_length (&path) == 0)
5021 if (!_dbus_concat_dir_and_file (&path,
5024 _dbus_string_free (&path);
5028 if (!_dbus_string_copy_data(&path, &cpath))
5030 _dbus_string_free (&path);
5034 if (!_dbus_list_append (dir_list, cpath))
5036 _dbus_string_free (&path);
5042 _dbus_string_free (&path);
5050 if (!_dbus_string_init (&path))
5053 if (!_dbus_string_copy_len (dirs,
5059 _dbus_string_free (&path);
5063 if (!_dbus_concat_dir_and_file (&path,
5066 _dbus_string_free (&path);
5070 if (!_dbus_string_copy_data(&path, &cpath))
5072 _dbus_string_free (&path);
5076 if (!_dbus_list_append (dir_list, cpath))
5078 _dbus_string_free (&path);
5083 _dbus_string_free (&path);
5089 _dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL);
5090 _dbus_list_clear (dir_list);
5095 * Returns the standard directories for a session bus to look for service
5098 * On Windows this should be data directories:
5100 * %CommonProgramFiles%/dbus
5106 * @param dirs the directory list we are returning
5107 * @returns #FALSE on OOM
5111 _dbus_get_standard_session_servicedirs (DBusList **dirs)
5113 const char *common_progs;
5114 DBusString servicedir_path;
5116 if (!_dbus_string_init (&servicedir_path))
5119 if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR";"))
5122 common_progs = _dbus_getenv ("CommonProgramFiles");
5124 if (common_progs != NULL)
5126 if (!_dbus_string_append (&servicedir_path, common_progs))
5129 if (!_dbus_string_append (&servicedir_path, ";"))
5133 if (!split_paths_and_append (&servicedir_path,
5134 DBUS_STANDARD_SESSION_SERVICEDIR,
5138 _dbus_string_free (&servicedir_path);
5142 _dbus_string_free (&servicedir_path);
5146 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
5149 * Atomically increments an integer
5151 * @param atomic pointer to the integer to increment
5152 * @returns the value before incrementing
5156 _dbus_atomic_inc (DBusAtomic *atomic)
5158 // +/- 1 is needed here!
5159 return InterlockedIncrement (&atomic->value) - 1;
5163 * Atomically decrement an integer
5165 * @param atomic pointer to the integer to decrement
5166 * @returns the value before decrementing
5170 _dbus_atomic_dec (DBusAtomic *atomic)
5172 // +/- 1 is needed here!
5173 return InterlockedDecrement (&atomic->value) + 1;
5176 #endif /* asserts or tests enabled */
5178 /** @} end of sysdeps-win */
5180 /* tests in dbus-sysdeps-util.c */