31 #define STRSAFE_NO_DEPRECATE
35 #define _WIN32_WINNT 0x0501
39 #include "dbus-internals.h"
41 #include "dbus-sysdeps.h"
42 #include "dbus-threads.h"
43 #include "dbus-protocol.h"
44 #include "dbus-string.h"
45 #include "dbus-sysdeps.h"
46 #include "dbus-sysdeps-win.h"
47 #include "dbus-protocol.h"
48 #include "dbus-hash.h"
49 #include "dbus-sockets-win.h"
50 #include "dbus-list.h"
51 #include "dbus-nonce.h"
52 #include "dbus-credentials.h"
60 extern BOOL WINAPI ConvertStringSidToSidA (LPCSTR StringSid, PSID *Sid);
61 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
72 #include <sys/types.h>
75 #ifdef HAVE_WS2TCPIP_H
84 typedef int socklen_t;
88 _dbus_win_set_errno (
int err)
97 static BOOL is_winxp_sp3_or_lower();
103 typedef MIB_TCPROW_OWNER_PID _MIB_TCPROW_EX;
104 typedef MIB_TCPTABLE_OWNER_PID MIB_TCPTABLE_EX;
105 typedef PMIB_TCPTABLE_OWNER_PID PMIB_TCPTABLE_EX;
106 typedef DWORD (WINAPI *ProcAllocateAndGetTcpExtTableFromStack)(PMIB_TCPTABLE_EX*,BOOL,HANDLE,DWORD,DWORD);
107 static ProcAllocateAndGetTcpExtTableFromStack lpfnAllocateAndGetTcpExTableFromStack =
NULL;
115 load_ex_ip_helper_procedures(
void)
117 HMODULE hModule = LoadLibrary (
"iphlpapi.dll");
120 _dbus_verbose (
"could not load iphlpapi.dll\n");
124 lpfnAllocateAndGetTcpExTableFromStack = (ProcAllocateAndGetTcpExtTableFromStack)GetProcAddress (hModule,
"AllocateAndGetTcpExTableFromStack");
125 if (lpfnAllocateAndGetTcpExTableFromStack ==
NULL)
127 _dbus_verbose (
"could not find function AllocateAndGetTcpExTableFromStack in iphlpapi.dll\n");
140 get_pid_from_extended_tcp_table(
int peer_port)
143 DWORD errorCode, size, i;
144 MIB_TCPTABLE_OWNER_PID *tcp_table;
147 GetExtendedTcpTable (
NULL, &size,
TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == ERROR_INSUFFICIENT_BUFFER)
149 tcp_table = (MIB_TCPTABLE_OWNER_PID *)
dbus_malloc (size);
150 if (tcp_table ==
NULL)
152 _dbus_verbose (
"Error allocating memory\n");
158 _dbus_win_warn_win_error (
"unexpected error returned from GetExtendedTcpTable", errorCode);
162 if ((errorCode = GetExtendedTcpTable (tcp_table, &size,
TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) != NO_ERROR)
164 _dbus_verbose (
"Error fetching tcp table %d\n", (
int)errorCode);
170 for (i = 0; i < tcp_table->dwNumEntries; i++)
172 MIB_TCPROW_OWNER_PID *p = &tcp_table->table[i];
173 int local_address = ntohl (p->dwLocalAddr);
174 int local_port = ntohs (p->dwLocalPort);
175 if (p->dwState == MIB_TCP_STATE_ESTAB
176 && local_address == INADDR_LOOPBACK && local_port == peer_port)
177 result = p->dwOwningPid;
181 _dbus_verbose (
"got pid %lu\n", result);
193 get_pid_from_tcp_ex_table(
int peer_port)
197 PMIB_TCPTABLE_EX tcp_table =
NULL;
199 if (!load_ex_ip_helper_procedures ())
202 (
"Error not been able to load iphelper procedures\n");
206 errorCode = lpfnAllocateAndGetTcpExTableFromStack (&tcp_table,
TRUE, GetProcessHeap(), 0, 2);
208 if (errorCode != NO_ERROR)
211 (
"Error not been able to call AllocateAndGetTcpExTableFromStack()\n");
216 for (i = 0; i < tcp_table->dwNumEntries; i++)
218 _MIB_TCPROW_EX *p = &tcp_table->table[i];
219 int local_port = ntohs (p->dwLocalPort);
220 int local_address = ntohl (p->dwLocalAddr);
221 if (local_address == INADDR_LOOPBACK && local_port == peer_port)
223 result = p->dwOwningPid;
228 HeapFree (GetProcessHeap(), 0, tcp_table);
229 _dbus_verbose (
"got pid %lu\n", result);
239 _dbus_get_peer_pid_from_tcp_handle (
int handle)
241 struct sockaddr_storage addr;
242 socklen_t len =
sizeof (addr);
248 getpeername (handle, (
struct sockaddr *) &addr, &len);
250 if (addr.ss_family == AF_INET)
252 struct sockaddr_in *s = (
struct sockaddr_in *) &addr;
253 peer_port = ntohs (s->sin_port);
254 is_localhost = (ntohl (s->sin_addr.s_addr) == INADDR_LOOPBACK);
256 else if (addr.ss_family == AF_INET6)
258 _dbus_verbose (
"FIXME [61922]: IPV6 support not working on windows\n");
269 _dbus_verbose (
"no idea what address family %d is\n", addr.ss_family);
275 _dbus_verbose (
"could not fetch process id from remote process\n");
282 (
"Error not been able to fetch tcp peer port from connection\n");
286 _dbus_verbose (
"trying to get peers pid");
288 result = get_pid_from_extended_tcp_table (peer_port);
291 result = get_pid_from_tcp_ex_table (peer_port);
297 _dbus_win_error_from_last_error (
void)
299 switch (GetLastError())
304 case ERROR_NO_MORE_FILES:
305 case ERROR_TOO_MANY_OPEN_FILES:
308 case ERROR_ACCESS_DENIED:
309 case ERROR_CANNOT_MAKE:
312 case ERROR_NOT_ENOUGH_MEMORY:
315 case ERROR_FILE_EXISTS:
318 case ERROR_FILE_NOT_FOUND:
319 case ERROR_PATH_NOT_FOUND:
328 _dbus_win_error_string (
int error_number)
332 FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
333 FORMAT_MESSAGE_IGNORE_INSERTS |
334 FORMAT_MESSAGE_FROM_SYSTEM,
335 NULL, error_number, 0,
336 (LPSTR) &msg, 0,
NULL);
338 if (msg[strlen (msg) - 1] ==
'\n')
339 msg[strlen (msg) - 1] =
'\0';
340 if (msg[strlen (msg) - 1] ==
'\r')
341 msg[strlen (msg) - 1] =
'\0';
347 _dbus_win_free_error_string (
char *
string)
383 start = _dbus_string_get_length (buffer);
387 _dbus_win_set_errno (ENOMEM);
395 _dbus_verbose (
"recv: count=%d fd=%d\n", count, fd);
396 bytes_read = recv (fd, data, count, 0);
398 if (bytes_read == SOCKET_ERROR)
400 DBUS_SOCKET_SET_ERRNO();
401 _dbus_verbose (
"recv: failed: %s (%d)\n", _dbus_strerror (errno), errno);
405 _dbus_verbose (
"recv: = %d\n", bytes_read);
451 data = _dbus_string_get_const_data_len (buffer, start, len);
455 _dbus_verbose (
"send: len=%d fd=%d\n", len, fd);
456 bytes_written = send (fd, data, len, 0);
458 if (bytes_written == SOCKET_ERROR)
460 DBUS_SOCKET_SET_ERRNO();
465 _dbus_verbose (
"send: = %d\n", bytes_written);
467 if (bytes_written < 0 && errno == EINTR)
471 if (bytes_written > 0)
475 return bytes_written;
490 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
493 if (closesocket (fd) == SOCKET_ERROR)
495 DBUS_SOCKET_SET_ERRNO ();
501 "Could not close socket: socket=%d, , %s",
505 _dbus_verbose (
"_dbus_close_socket: socket=%d, \n", fd);
520 if ( !SetHandleInformation( (HANDLE) handle,
521 HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE,
524 _dbus_win_warn_win_error (
"Disabling socket handle inheritance failed:", GetLastError());
536 _dbus_set_fd_nonblocking (
int handle,
541 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
543 if (ioctlsocket (handle, FIONBIO, &one) == SOCKET_ERROR)
545 DBUS_SOCKET_SET_ERRNO ();
547 "Failed to set socket %d:%d to nonblocking: %s", handle,
598 data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
601 data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
609 vectors[0].buf = (
char*) data1;
610 vectors[0].len = len1;
611 vectors[1].buf = (
char*) data2;
612 vectors[1].len = len2;
616 _dbus_verbose (
"WSASend: len1+2=%d+%d fd=%d\n", len1, len2, fd);
625 if (rc == SOCKET_ERROR)
627 DBUS_SOCKET_SET_ERRNO ();
632 _dbus_verbose (
"WSASend: = %ld\n", bytes_written);
634 if (bytes_written < 0 && errno == EINTR)
637 return bytes_written;
641 _dbus_socket_is_invalid (
int fd)
643 return fd == INVALID_SOCKET ?
TRUE :
FALSE;
657 _dbus_connect_named_pipe (
const char *path,
669 _dbus_win_startup_winsock (
void)
676 WORD wVersionRequested;
686 wVersionRequested = MAKEWORD (2, 0);
688 err = WSAStartup (wVersionRequested, &wsaData);
700 if (LOBYTE (wsaData.wVersion) != 2 ||
701 HIBYTE (wsaData.wVersion) != 0)
740 bufsize =
sizeof (buf);
741 DBUS_VA_COPY (args_copy, args);
742 len = _vsnprintf (buf, bufsize - 1, format, args_copy);
751 p = malloc (bufsize);
756 DBUS_VA_COPY (args_copy, args);
757 len = _vsnprintf (p, bufsize - 1, format, args_copy);
774 _dbus_win_utf8_to_utf16 (
const char *str,
789 n = MultiByteToWideChar (CP_UTF8, 0, str, -1,
NULL, 0);
793 _dbus_win_set_error_from_win_error (error, GetLastError ());
801 _DBUS_SET_OOM (error);
805 if (MultiByteToWideChar (CP_UTF8, 0, str, -1, retval, n) != n)
823 _dbus_win_utf16_to_utf8 (
const wchar_t *str,
829 n = WideCharToMultiByte (CP_UTF8, 0, str, -1,
NULL, 0,
NULL,
NULL);
833 _dbus_win_set_error_from_win_error (error, GetLastError ());
841 _DBUS_SET_OOM (error);
845 if (WideCharToMultiByte (CP_UTF8, 0, str, -1, retval, n,
NULL,
NULL) != n)
866 _dbus_win_account_to_sid (
const wchar_t *waccount,
871 DWORD sid_length, wdomain_length;
879 if (!LookupAccountNameW (
NULL, waccount,
NULL, &sid_length,
880 NULL, &wdomain_length, &use) &&
881 GetLastError () != ERROR_INSUFFICIENT_BUFFER)
883 _dbus_win_set_error_from_win_error (error, GetLastError ());
890 _DBUS_SET_OOM (error);
894 wdomain =
dbus_new (
wchar_t, wdomain_length);
897 _DBUS_SET_OOM (error);
901 if (!LookupAccountNameW (
NULL, waccount, (PSID) *ppsid, &sid_length,
902 wdomain, &wdomain_length, &use))
904 _dbus_win_set_error_from_win_error (error, GetLastError ());
908 if (!IsValidSid ((PSID) *ppsid))
945 static BOOL is_winxp_sp3_or_lower()
947 OSVERSIONINFOEX osvi;
948 DWORDLONG dwlConditionMask = 0;
949 int op=VER_LESS_EQUAL;
953 ZeroMemory(&osvi,
sizeof(OSVERSIONINFOEX));
954 osvi.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEX);
955 osvi.dwMajorVersion = 5;
956 osvi.dwMinorVersion = 1;
957 osvi.wServicePackMajor = 3;
958 osvi.wServicePackMinor = 0;
962 VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
963 VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
964 VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
965 VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
969 return VerifyVersionInfo(
971 VER_MAJORVERSION | VER_MINORVERSION |
972 VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
982 _dbus_getsid(
char **sid,
dbus_pid_t process_id)
984 HANDLE process_token = INVALID_HANDLE_VALUE;
985 TOKEN_USER *token_user =
NULL;
990 HANDLE process_handle = OpenProcess(is_winxp_sp3_or_lower() ? PROCESS_QUERY_INFORMATION : PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, process_id);
992 if (!OpenProcessToken (process_handle, TOKEN_QUERY, &process_token))
994 _dbus_win_warn_win_error (
"OpenProcessToken failed", GetLastError ());
997 if ((!GetTokenInformation (process_token, TokenUser,
NULL, 0, &n)
998 && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
999 || (token_user = alloca (n)) ==
NULL
1000 || !GetTokenInformation (process_token, TokenUser, token_user, n, &n))
1002 _dbus_win_warn_win_error (
"GetTokenInformation failed", GetLastError ());
1005 psid = token_user->User.Sid;
1006 if (!IsValidSid (psid))
1008 _dbus_verbose(
"%s invalid sid\n",__FUNCTION__);
1011 if (!ConvertSidToStringSidA (psid, sid))
1013 _dbus_verbose(
"%s invalid sid\n",__FUNCTION__);
1020 CloseHandle (process_handle);
1021 if (process_token != INVALID_HANDLE_VALUE)
1022 CloseHandle (process_token);
1024 _dbus_verbose(
"_dbus_getsid() got '%s' and returns %d\n", *sid, retval);
1051 SOCKET temp, socket1 = -1, socket2 = -1;
1052 struct sockaddr_in saddr;
1056 if (!_dbus_win_startup_winsock ())
1058 _DBUS_SET_OOM (error);
1062 temp = socket (AF_INET, SOCK_STREAM, 0);
1063 if (temp == INVALID_SOCKET)
1065 DBUS_SOCKET_SET_ERRNO ();
1070 saddr.sin_family = AF_INET;
1072 saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1074 if (bind (temp, (
struct sockaddr *)&saddr,
sizeof (saddr)) == SOCKET_ERROR)
1076 DBUS_SOCKET_SET_ERRNO ();
1080 if (listen (temp, 1) == SOCKET_ERROR)
1082 DBUS_SOCKET_SET_ERRNO ();
1086 len =
sizeof (saddr);
1087 if (getsockname (temp, (
struct sockaddr *)&saddr, &len) == SOCKET_ERROR)
1089 DBUS_SOCKET_SET_ERRNO ();
1093 socket1 = socket (AF_INET, SOCK_STREAM, 0);
1094 if (socket1 == INVALID_SOCKET)
1096 DBUS_SOCKET_SET_ERRNO ();
1100 if (connect (socket1, (
struct sockaddr *)&saddr, len) == SOCKET_ERROR)
1102 DBUS_SOCKET_SET_ERRNO ();
1106 socket2 = accept (temp, (
struct sockaddr *) &saddr, &len);
1107 if (socket2 == INVALID_SOCKET)
1109 DBUS_SOCKET_SET_ERRNO ();
1116 if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
1118 DBUS_SOCKET_SET_ERRNO ();
1123 if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
1125 DBUS_SOCKET_SET_ERRNO ();
1133 _dbus_verbose (
"full-duplex pipe %d:%d <-> %d:%d\n",
1134 *fd1, socket1, *fd2, socket2);
1141 closesocket (socket2);
1143 closesocket (socket1);
1148 "Could not setup socket pair: %s",
1165 int timeout_milliseconds)
1167 #define USE_CHRIS_IMPL 0
1171 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
1172 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
1180 #define DBUS_STACK_WSAEVENTS 256
1181 WSAEVENT eventsOnStack[DBUS_STACK_WSAEVENTS];
1182 WSAEVENT *pEvents =
NULL;
1183 if (n_fds > DBUS_STACK_WSAEVENTS)
1184 pEvents = calloc(
sizeof(WSAEVENT), n_fds);
1186 pEvents = eventsOnStack;
1189 #ifdef DBUS_ENABLE_VERBOSE_MODE
1191 msgp += sprintf (msgp,
"WSAEventSelect: to=%d\n\t", timeout_milliseconds);
1192 for (i = 0; i < n_fds; i++)
1198 msgp += sprintf (msgp,
"R:%d ", fdp->
fd);
1201 msgp += sprintf (msgp,
"W:%d ", fdp->
fd);
1203 msgp += sprintf (msgp,
"E:%d\n\t", fdp->
fd);
1207 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
1213 msgp += sprintf (msgp,
"\n");
1214 _dbus_verbose (
"%s",msg);
1216 for (i = 0; i < n_fds; i++)
1220 long lNetworkEvents = FD_OOB;
1222 ev = WSACreateEvent();
1225 lNetworkEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
1228 lNetworkEvents |= FD_WRITE | FD_CONNECT;
1230 WSAEventSelect(fdp->
fd, ev, lNetworkEvents);
1236 ready = WSAWaitForMultipleEvents (n_fds, pEvents,
FALSE, timeout_milliseconds,
FALSE);
1238 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
1240 DBUS_SOCKET_SET_ERRNO ();
1241 if (errno != WSAEWOULDBLOCK)
1245 else if (ready == WSA_WAIT_TIMEOUT)
1247 _dbus_verbose (
"WSAWaitForMultipleEvents: WSA_WAIT_TIMEOUT\n");
1250 else if (ready >= WSA_WAIT_EVENT_0 && ready < (
int)(WSA_WAIT_EVENT_0 + n_fds))
1253 msgp += sprintf (msgp,
"WSAWaitForMultipleEvents: =%d\n\t", ready);
1255 for (i = 0; i < n_fds; i++)
1258 WSANETWORKEVENTS ne;
1262 WSAEnumNetworkEvents(fdp->
fd, pEvents[i], &ne);
1264 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1267 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
1270 if (ne.lNetworkEvents & (FD_OOB))
1273 if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1274 msgp += sprintf (msgp,
"R:%d ", fdp->
fd);
1276 if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
1277 msgp += sprintf (msgp,
"W:%d ", fdp->
fd);
1279 if (ne.lNetworkEvents & (FD_OOB))
1280 msgp += sprintf (msgp,
"E:%d ", fdp->
fd);
1282 msgp += sprintf (msgp,
"lNetworkEvents:%d ", ne.lNetworkEvents);
1284 if(ne.lNetworkEvents)
1287 WSAEventSelect(fdp->
fd, pEvents[i], 0);
1290 msgp += sprintf (msgp,
"\n");
1291 _dbus_verbose (
"%s",msg);
1295 _dbus_verbose (
"WSAWaitForMultipleEvents: failed for unknown reason!");
1299 for(i = 0; i < n_fds; i++)
1301 WSACloseEvent(pEvents[i]);
1304 if (n_fds > DBUS_STACK_WSAEVENTS)
1311 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
1312 char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
1315 fd_set read_set, write_set, err_set;
1321 FD_ZERO (&read_set);
1322 FD_ZERO (&write_set);
1326 #ifdef DBUS_ENABLE_VERBOSE_MODE
1328 msgp += sprintf (msgp,
"select: to=%d\n\t", timeout_milliseconds);
1329 for (i = 0; i < n_fds; i++)
1335 msgp += sprintf (msgp,
"R:%d ", fdp->
fd);
1338 msgp += sprintf (msgp,
"W:%d ", fdp->
fd);
1340 msgp += sprintf (msgp,
"E:%d\n\t", fdp->
fd);
1344 if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
1350 msgp += sprintf (msgp,
"\n");
1351 _dbus_verbose (
"%s",msg);
1353 for (i = 0; i < n_fds; i++)
1358 FD_SET (fdp->
fd, &read_set);
1361 FD_SET (fdp->
fd, &write_set);
1363 FD_SET (fdp->
fd, &err_set);
1365 max_fd = MAX (max_fd, fdp->
fd);
1369 tv.tv_sec = timeout_milliseconds < 0 ? 1 : timeout_milliseconds / 1000;
1370 tv.tv_usec = timeout_milliseconds < 0 ? 0 : (timeout_milliseconds % 1000) * 1000;
1372 ready = select (max_fd + 1, &read_set, &write_set, &err_set, &tv);
1374 if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
1376 DBUS_SOCKET_SET_ERRNO ();
1377 if (errno != WSAEWOULDBLOCK)
1380 else if (ready == 0)
1381 _dbus_verbose (
"select: = 0\n");
1385 #ifdef DBUS_ENABLE_VERBOSE_MODE
1387 msgp += sprintf (msgp,
"select: = %d:\n\t", ready);
1389 for (i = 0; i < n_fds; i++)
1393 if (FD_ISSET (fdp->
fd, &read_set))
1394 msgp += sprintf (msgp,
"R:%d ", fdp->
fd);
1396 if (FD_ISSET (fdp->
fd, &write_set))
1397 msgp += sprintf (msgp,
"W:%d ", fdp->
fd);
1399 if (FD_ISSET (fdp->
fd, &err_set))
1400 msgp += sprintf (msgp,
"E:%d\n\t", fdp->
fd);
1402 msgp += sprintf (msgp,
"\n");
1403 _dbus_verbose (
"%s",msg);
1406 for (i = 0; i < n_fds; i++)
1412 if (FD_ISSET (fdp->
fd, &read_set))
1415 if (FD_ISSET (fdp->
fd, &write_set))
1418 if (FD_ISSET (fdp->
fd, &err_set))
1488 return _dbus_connect_tcp_socket_with_nonce (host, port, family, (
const char*)
NULL, error);
1492 _dbus_connect_tcp_socket_with_nonce (
const char *host,
1495 const char *noncefile,
1499 struct addrinfo hints;
1500 struct addrinfo *ai, *tmp;
1502 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1504 if (!_dbus_win_startup_winsock ())
1506 _DBUS_SET_OOM (error);
1513 hints.ai_family = AF_UNSPEC;
1514 else if (!strcmp(family,
"ipv4"))
1515 hints.ai_family = AF_INET;
1516 else if (!strcmp(family,
"ipv6"))
1517 hints.ai_family = AF_INET6;
1522 "Unknown address family %s", family);
1525 hints.ai_protocol = IPPROTO_TCP;
1526 hints.ai_socktype = SOCK_STREAM;
1527 #ifdef AI_ADDRCONFIG
1528 hints.ai_flags = AI_ADDRCONFIG;
1533 if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
1537 "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1538 host, port, _dbus_strerror(res), res);
1545 if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
1547 DBUS_SOCKET_SET_ERRNO ();
1550 "Failed to open socket: %s",
1555 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1557 if (connect (fd, (
struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR)
1559 DBUS_SOCKET_SET_ERRNO ();
1574 "Failed to connect to socket \"%s:%s\" %s",
1579 if (noncefile !=
NULL)
1591 ret = _dbus_send_nonce (fd, &noncefileStr, error);
1604 if (!_dbus_set_fd_nonblocking (fd, error))
1636 int nlisten_fd = 0, *listen_fd =
NULL, res, i, port_num = -1;
1637 struct addrinfo hints;
1638 struct addrinfo *ai, *tmp;
1645 struct sockaddr Address;
1646 struct sockaddr_in AddressIn;
1647 struct sockaddr_in6 AddressIn6;
1651 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1653 if (!_dbus_win_startup_winsock ())
1655 _DBUS_SET_OOM (error);
1662 hints.ai_family = AF_UNSPEC;
1663 else if (!strcmp(family,
"ipv4"))
1664 hints.ai_family = AF_INET;
1665 else if (!strcmp(family,
"ipv6"))
1666 hints.ai_family = AF_INET6;
1671 "Unknown address family %s", family);
1675 hints.ai_protocol = IPPROTO_TCP;
1676 hints.ai_socktype = SOCK_STREAM;
1677 #ifdef AI_ADDRCONFIG
1678 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1680 hints.ai_flags = AI_PASSIVE;
1683 redo_lookup_with_port:
1684 if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
1688 "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1689 host ? host :
"*", port, _dbus_strerror(res), res);
1696 int fd = -1, *newlisten_fd;
1697 if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
1699 DBUS_SOCKET_SET_ERRNO ();
1702 "Failed to open socket: %s",
1706 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1708 if (bind (fd, (
struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR)
1710 DBUS_SOCKET_SET_ERRNO ();
1712 "Failed to bind socket \"%s:%s\": %s",
1718 if (listen (fd, 30 ) == SOCKET_ERROR)
1720 DBUS_SOCKET_SET_ERRNO ();
1722 "Failed to listen on socket \"%s:%s\": %s",
1728 newlisten_fd =
dbus_realloc(listen_fd,
sizeof(
int)*(nlisten_fd+1));
1733 "Failed to allocate file handle array");
1736 listen_fd = newlisten_fd;
1737 listen_fd[nlisten_fd] = fd;
1740 if (!_dbus_string_get_length(retport))
1746 if (!port || !strcmp(port,
"0"))
1748 mysockaddr_gen addr;
1749 socklen_t addrlen =
sizeof(addr);
1752 if (getsockname(fd, &addr.Address, &addrlen) == SOCKET_ERROR)
1754 DBUS_SOCKET_SET_ERRNO ();
1756 "Failed to resolve port \"%s:%s\": %s",
1760 snprintf( portbuf,
sizeof( portbuf ) - 1,
"%d", addr.AddressIn.sin_port );
1768 port = _dbus_string_get_const_data(retport);
1770 goto redo_lookup_with_port;
1789 _dbus_win_set_errno (WSAEADDRINUSE);
1791 "Failed to bind socket \"%s:%s\": %s",
1796 sscanf(_dbus_string_get_const_data(retport),
"%d", &port_num);
1798 for (i = 0 ; i < nlisten_fd ; i++)
1801 if (!_dbus_set_fd_nonblocking (listen_fd[i], error))
1814 for (i = 0 ; i < nlisten_fd ; i++)
1815 closesocket (listen_fd[i]);
1834 client_fd = accept (listen_fd,
NULL,
NULL);
1836 if (DBUS_SOCKET_IS_INVALID (client_fd))
1838 DBUS_SOCKET_SET_ERRNO ();
1843 _dbus_verbose (
"client fd %d accepted\n", client_fd);
1885 if (bytes_written < 0 && errno == EINTR)
1888 if (bytes_written < 0)
1891 "Failed to write credentials byte: %s",
1895 else if (bytes_written == 0)
1898 "wrote zero bytes writing credentials byte");
1904 _dbus_verbose (
"wrote 1 zero byte, credential sending isn't implemented yet\n");
1946 _dbus_verbose (
"got one zero byte from server\n");
1951 pid = _dbus_get_peer_pid_from_tcp_handle (handle);
1957 if (_dbus_getsid (&sid, pid))
1984 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2006 if (_dbus_string_get_length (dir) == 0 ||
2007 _dbus_string_get_length (next_component) == 0)
2011 (
'/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1) ||
2012 '\\' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1));
2014 file_starts_with_slash =
2015 (
'/' == _dbus_string_get_byte (next_component, 0) ||
2016 '\\' == _dbus_string_get_byte (next_component, 0));
2018 if (dir_ends_in_slash && file_starts_with_slash)
2022 else if (!(dir_ends_in_slash || file_starts_with_slash))
2029 _dbus_string_get_length (dir));
2046 _dbus_string_get_const_data(username));
2117 return GetCurrentProcessId ();
2121 #define NANOSECONDS_PER_SECOND 1000000000
2123 #define MICROSECONDS_PER_SECOND 1000000
2125 #define MILLISECONDS_PER_SECOND 1000
2127 #define NANOSECONDS_PER_MILLISECOND 1000000
2129 #define MICROSECONDS_PER_MILLISECOND 1000
2138 Sleep (milliseconds);
2154 dbus_uint64_t time64;
2156 GetSystemTimeAsFileTime (&ft);
2158 memcpy (&time64, &ft,
sizeof (time64));
2163 time64 -= DBUS_INT64_CONSTANT (116444736000000000);
2167 *tv_sec = time64 / 1000000;
2170 *tv_usec = time64 % 1000000;
2208 const char *filename_c;
2210 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2212 filename_c = _dbus_string_get_const_data (filename);
2214 if (!CreateDirectoryA (filename_c,
NULL))
2216 if (GetLastError () == ERROR_ALREADY_EXISTS)
2220 "Failed to create directory %s: %s\n",
2245 old_len = _dbus_string_get_length (str);
2252 if (!CryptAcquireContext (&hprov,
NULL,
NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
2255 if (!CryptGenRandom (hprov, n_bytes, p))
2257 CryptReleaseContext (hprov, 0);
2261 CryptReleaseContext (hprov, 0);
2276 static const char* tmpdir =
NULL;
2277 static char buf[1000];
2286 if (!GetTempPathA (
sizeof (buf), buf))
2293 last_slash = _mbsrchr (buf,
'\\');
2294 if (last_slash > buf && last_slash[1] ==
'\0')
2295 last_slash[0] =
'\0';
2296 last_slash = _mbsrchr (buf,
'/');
2297 if (last_slash > buf && last_slash[1] ==
'\0')
2298 last_slash[0] =
'\0';
2323 const char *filename_c;
2325 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2327 filename_c = _dbus_string_get_const_data (filename);
2329 if (DeleteFileA (filename_c) == 0)
2332 "Failed to delete file %s: %s\n",
2340 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_ENABLE_EMBEDDED_TESTS)
2342 #if defined(_MSC_VER) || defined(DBUS_WINCE)
2373 #include <imagehlp.h>
2376 #define DPRINTF _dbus_warn
2392 static BOOL (WINAPI *pStackWalk)(
2396 LPSTACKFRAME StackFrame,
2397 PVOID ContextRecord,
2398 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2399 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2400 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2401 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2404 static DWORD64 (WINAPI *pSymGetModuleBase)(
2408 static PVOID (WINAPI *pSymFunctionTableAccess)(
2413 static DWORD (WINAPI *pSymGetModuleBase)(
2417 static PVOID (WINAPI *pSymFunctionTableAccess)(
2422 static BOOL (WINAPI *pSymInitialize)(
2424 PSTR UserSearchPath,
2427 static BOOL (WINAPI *pSymGetSymFromAddr)(
2430 PDWORD Displacement,
2431 PIMAGEHLP_SYMBOL Symbol
2433 static BOOL (WINAPI *pSymGetModuleInfo)(
2436 PIMAGEHLP_MODULE ModuleInfo
2438 static DWORD (WINAPI *pSymSetOptions)(
2443 static BOOL init_backtrace()
2445 HMODULE hmodDbgHelp = LoadLibraryA(
"dbghelp");
2465 pStackWalk = (BOOL (WINAPI *)(
2469 LPSTACKFRAME StackFrame,
2470 PVOID ContextRecord,
2471 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
2472 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
2473 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
2474 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
2475 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
2477 pSymGetModuleBase=(DWORD64 (WINAPI *)(
2480 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
2481 pSymFunctionTableAccess=(PVOID (WINAPI *)(
2484 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
2486 pSymGetModuleBase=(DWORD (WINAPI *)(
2489 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
2490 pSymFunctionTableAccess=(PVOID (WINAPI *)(
2493 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
2495 pSymInitialize = (BOOL (WINAPI *)(
2497 PSTR UserSearchPath,
2499 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
2500 pSymGetSymFromAddr = (BOOL (WINAPI *)(
2503 PDWORD Displacement,
2504 PIMAGEHLP_SYMBOL Symbol
2505 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
2506 pSymGetModuleInfo = (BOOL (WINAPI *)(
2509 PIMAGEHLP_MODULE ModuleInfo
2510 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
2511 pSymSetOptions = (DWORD (WINAPI *)(
2513 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
2516 pSymSetOptions(SYMOPT_UNDNAME);
2518 pSymInitialize(GetCurrentProcess(),
NULL,
TRUE);
2523 static void dump_backtrace_for_thread(HANDLE hThread)
2530 if (!init_backtrace())
2535 if (hThread == GetCurrentThread())
2538 DPRINTF(
"Backtrace:\n");
2541 context.ContextFlags = CONTEXT_FULL;
2543 SuspendThread(hThread);
2545 if (!GetThreadContext(hThread, &context))
2547 DPRINTF(
"Couldn't get thread context (error %ld)\n", GetLastError());
2548 ResumeThread(hThread);
2555 sf.AddrFrame.Offset = context.Ebp;
2556 sf.AddrFrame.Mode = AddrModeFlat;
2557 sf.AddrPC.Offset = context.Eip;
2558 sf.AddrPC.Mode = AddrModeFlat;
2559 dwImageType = IMAGE_FILE_MACHINE_I386;
2561 dwImageType = IMAGE_FILE_MACHINE_AMD64;
2562 sf.AddrPC.Offset = context.Rip;
2563 sf.AddrPC.Mode = AddrModeFlat;
2564 sf.AddrFrame.Offset = context.Rsp;
2565 sf.AddrFrame.Mode = AddrModeFlat;
2566 sf.AddrStack.Offset = context.Rsp;
2567 sf.AddrStack.Mode = AddrModeFlat;
2569 dwImageType = IMAGE_FILE_MACHINE_IA64;
2570 sf.AddrPC.Offset = context.StIIP;
2571 sf.AddrPC.Mode = AddrModeFlat;
2572 sf.AddrFrame.Offset = context.IntSp;
2573 sf.AddrFrame.Mode = AddrModeFlat;
2574 sf.AddrBStore.Offset= context.RsBSP;
2575 sf.AddrBStore.Mode = AddrModeFlat;
2576 sf.AddrStack.Offset = context.IntSp;
2577 sf.AddrStack.Mode = AddrModeFlat;
2579 # error You need to fill in the STACKFRAME structure for your architecture
2582 while (pStackWalk(dwImageType, GetCurrentProcess(),
2583 hThread, &sf, &context,
NULL, pSymFunctionTableAccess,
2584 pSymGetModuleBase,
NULL))
2587 IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
2588 DWORD dwDisplacement;
2590 pSymbol->SizeOfStruct =
sizeof(IMAGEHLP_SYMBOL);
2591 pSymbol->MaxNameLength =
sizeof(buffer) -
sizeof(IMAGEHLP_SYMBOL) + 1;
2593 if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
2594 &dwDisplacement, pSymbol))
2596 IMAGEHLP_MODULE ModuleInfo;
2597 ModuleInfo.SizeOfStruct =
sizeof(ModuleInfo);
2599 if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
2601 DPRINTF(
"1\t%p\n", (
void*)sf.AddrPC.Offset);
2603 DPRINTF(
"2\t%s+0x%lx\n", ModuleInfo.ImageName,
2604 sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
2606 else if (dwDisplacement)
2607 DPRINTF(
"3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
2609 DPRINTF(
"4\t%s\n", pSymbol->Name);
2612 ResumeThread(hThread);
2615 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
2617 dump_backtrace_for_thread((HANDLE)lpParameter);
2623 static void dump_backtrace()
2625 HANDLE hCurrentThread;
2628 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
2629 GetCurrentProcess(), &hCurrentThread, 0,
FALSE, DUPLICATE_SAME_ACCESS);
2630 hThread = CreateThread(
NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
2632 WaitForSingleObject(hThread, INFINITE);
2633 CloseHandle(hThread);
2634 CloseHandle(hCurrentThread);
2648 _dbus_verbose (
" D-Bus not compiled with backtrace support\n");
2652 static dbus_uint32_t fromAscii(
char ascii)
2654 if(ascii >=
'0' && ascii <=
'9')
2656 if(ascii >=
'A' && ascii <=
'F')
2657 return ascii -
'A' + 10;
2658 if(ascii >=
'a' && ascii <=
'f')
2659 return ascii -
'a' + 10;
2671 HW_PROFILE_INFOA info;
2672 char *lpc = &info.szHwProfileGuid[0];
2676 if(!GetCurrentHwProfileA(&info))
2685 u = ((fromAscii(lpc[0]) << 0) |
2686 (fromAscii(lpc[1]) << 4) |
2687 (fromAscii(lpc[2]) << 8) |
2688 (fromAscii(lpc[3]) << 12) |
2689 (fromAscii(lpc[4]) << 16) |
2690 (fromAscii(lpc[5]) << 20) |
2691 (fromAscii(lpc[6]) << 24) |
2692 (fromAscii(lpc[7]) << 28));
2697 u = ((fromAscii(lpc[0]) << 0) |
2698 (fromAscii(lpc[1]) << 4) |
2699 (fromAscii(lpc[2]) << 8) |
2700 (fromAscii(lpc[3]) << 12) |
2701 (fromAscii(lpc[5]) << 16) |
2702 (fromAscii(lpc[6]) << 20) |
2703 (fromAscii(lpc[7]) << 24) |
2704 (fromAscii(lpc[8]) << 28));
2709 u = ((fromAscii(lpc[0]) << 0) |
2710 (fromAscii(lpc[1]) << 4) |
2711 (fromAscii(lpc[2]) << 8) |
2712 (fromAscii(lpc[3]) << 12) |
2713 (fromAscii(lpc[5]) << 16) |
2714 (fromAscii(lpc[6]) << 20) |
2715 (fromAscii(lpc[7]) << 24) |
2716 (fromAscii(lpc[8]) << 28));
2721 u = ((fromAscii(lpc[0]) << 0) |
2722 (fromAscii(lpc[1]) << 4) |
2723 (fromAscii(lpc[2]) << 8) |
2724 (fromAscii(lpc[3]) << 12) |
2725 (fromAscii(lpc[4]) << 16) |
2726 (fromAscii(lpc[5]) << 20) |
2727 (fromAscii(lpc[6]) << 24) |
2728 (fromAscii(lpc[7]) << 28));
2735 HANDLE _dbus_global_lock (
const char *mutexname)
2740 mutex = CreateMutexA(
NULL,
FALSE, mutexname );
2746 gotMutex = WaitForSingleObject( mutex, INFINITE );
2749 case WAIT_ABANDONED:
2750 ReleaseMutex (mutex);
2751 CloseHandle (mutex);
2762 void _dbus_global_unlock (HANDLE mutex)
2764 ReleaseMutex (mutex);
2765 CloseHandle (mutex);
2769 static HANDLE hDBusDaemonMutex =
NULL;
2770 static HANDLE hDBusSharedMem =
NULL;
2772 static const char *cUniqueDBusInitMutex =
"UniqueDBusInitMutex";
2774 static const char *cDBusAutolaunchMutex =
"DBusAutolaunchMutex";
2776 static const char *cDBusDaemonMutex =
"DBusDaemonMutex";
2778 static const char *cDBusDaemonAddressInfo =
"DBusDaemonAddressInfo";
2781 _dbus_get_install_root_as_hash(
DBusString *out)
2785 char path[MAX_PATH*2];
2786 int path_size =
sizeof(path);
2788 if (!_dbus_get_install_root(path,path_size))
2804 _dbus_get_address_string (
DBusString *out,
const char *basestring,
const char *scope)
2813 else if (strcmp(scope,
"*install-path") == 0
2815 || strcmp(scope,
"install-path") == 0)
2818 if (!_dbus_get_install_root_as_hash(&temp))
2827 else if (strcmp(scope,
"*user") == 0)
2836 else if (strlen(scope) > 0)
2846 _dbus_get_shm_name (
DBusString *out,
const char *scope)
2848 return _dbus_get_address_string (out,cDBusDaemonAddressInfo,scope);
2852 _dbus_get_mutex_name (
DBusString *out,
const char *scope)
2854 return _dbus_get_address_string (out,cDBusDaemonMutex,scope);
2858 _dbus_daemon_is_session_bus_address_published (
const char *scope)
2863 if (!_dbus_get_mutex_name(&mutex_name,scope))
2869 if (hDBusDaemonMutex)
2873 lock = _dbus_global_lock( cUniqueDBusInitMutex );
2877 hDBusDaemonMutex = CreateMutexA(
NULL,
FALSE, _dbus_string_get_const_data(&mutex_name) );
2883 _dbus_global_unlock( lock );
2887 if (hDBusDaemonMutex ==
NULL)
2889 if (GetLastError() == ERROR_ALREADY_EXISTS)
2891 CloseHandle(hDBusDaemonMutex);
2892 hDBusDaemonMutex =
NULL;
2902 _dbus_daemon_publish_session_bus_address (
const char* address,
const char *scope)
2905 char *shared_addr =
NULL;
2911 if (!_dbus_get_mutex_name(&mutex_name,scope))
2918 lock = _dbus_global_lock( cUniqueDBusInitMutex );
2920 if (!hDBusDaemonMutex)
2922 hDBusDaemonMutex = CreateMutexA(
NULL,
FALSE, _dbus_string_get_const_data(&mutex_name) );
2927 if (WaitForSingleObject( hDBusDaemonMutex, 10 ) != WAIT_OBJECT_0)
2929 _dbus_global_unlock( lock );
2930 CloseHandle( hDBusDaemonMutex );
2934 if (!_dbus_get_shm_name(&shm_name,scope))
2937 _dbus_global_unlock( lock );
2942 hDBusSharedMem = CreateFileMappingA( INVALID_HANDLE_VALUE,
NULL, PAGE_READWRITE,
2943 0, strlen( address ) + 1, _dbus_string_get_const_data(&shm_name) );
2946 shared_addr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
2950 strcpy( shared_addr, address);
2953 UnmapViewOfFile( shared_addr );
2955 _dbus_global_unlock( lock );
2956 _dbus_verbose(
"published session bus address at %s\n",_dbus_string_get_const_data (&shm_name) );
2963 _dbus_daemon_unpublish_session_bus_address (
void)
2968 lock = _dbus_global_lock( cUniqueDBusInitMutex );
2970 CloseHandle( hDBusSharedMem );
2972 hDBusSharedMem =
NULL;
2974 ReleaseMutex( hDBusDaemonMutex );
2976 CloseHandle( hDBusDaemonMutex );
2978 hDBusDaemonMutex =
NULL;
2980 _dbus_global_unlock( lock );
2993 sharedMem = OpenFileMappingA( FILE_MAP_READ,
FALSE, _dbus_string_get_const_data(shm_name));
2994 if( sharedMem == 0 )
2996 if ( sharedMem != 0)
3000 if( sharedMem == 0 )
3003 shared_addr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
3013 UnmapViewOfFile( shared_addr );
3015 CloseHandle( sharedMem );
3028 if (!_dbus_get_mutex_name(&mutex_name,scope))
3035 lock = _dbus_global_lock( cUniqueDBusInitMutex );
3038 daemon = CreateMutexA(
NULL,
FALSE, _dbus_string_get_const_data(&mutex_name) );
3039 if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
3041 ReleaseMutex (daemon);
3042 CloseHandle (daemon);
3044 _dbus_global_unlock( lock );
3050 bRet = _dbus_get_autolaunch_shm( address, shm_name );
3053 CloseHandle ( daemon );
3055 _dbus_global_unlock( lock );
3067 PROCESS_INFORMATION pi;
3070 char dbus_exe_path[MAX_PATH];
3071 char dbus_args[MAX_PATH * 2];
3072 const char * daemon_name = DBUS_DAEMON_NAME
".exe";
3075 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3077 if (!_dbus_get_shm_name(&shm_name,scope))
3083 mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
3085 if (_dbus_daemon_already_runs(address,&shm_name,scope))
3087 _dbus_verbose(
"found running dbus daemon at %s\n",
3088 _dbus_string_get_const_data (&shm_name) );
3093 if (!SearchPathA(
NULL, daemon_name,
NULL,
sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
3097 char dbus_module_path[MAX_PATH];
3100 _dbus_verbose(
"did not found dbus daemon executable on default search path, "
3101 "trying path where dbus shared library is located");
3103 hmod = _dbus_win_get_dll_hmodule();
3104 rc = GetModuleFileNameA(hmod, dbus_module_path,
sizeof(dbus_module_path));
3113 char *ext_idx = strrchr(dbus_module_path,
'\\');
3116 if (!SearchPathA(dbus_module_path, daemon_name,
NULL,
sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
3120 printf (
"please add the path to %s to your PATH environment variable\n", daemon_name);
3121 printf (
"or start the daemon manually\n\n");
3124 _dbus_verbose(
"found dbus daemon executable at %s",dbus_module_path);
3130 ZeroMemory( &si,
sizeof(si) );
3132 ZeroMemory( &pi,
sizeof(pi) );
3134 _snprintf(dbus_args,
sizeof(dbus_args) - 1,
"\"%s\" %s", dbus_exe_path,
" --session");
3138 if(CreateProcessA(dbus_exe_path, dbus_args,
NULL,
NULL,
FALSE, CREATE_NO_WINDOW,
NULL,
NULL, &si, &pi))
3140 CloseHandle (pi.hThread);
3141 CloseHandle (pi.hProcess);
3142 retval = _dbus_get_autolaunch_shm( address, &shm_name );
3143 if (retval ==
FALSE)
3154 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3156 _DBUS_ASSERT_ERROR_IS_SET (error);
3158 _dbus_global_unlock (mutex);
3190 return InterlockedIncrement (&atomic->
value) - 1;
3205 return InterlockedDecrement (&atomic->
value) + 1;
3228 InterlockedExchange (&dummy, 1);
3230 return atomic->
value;
3254 return errno == WSAEWOULDBLOCK;
3265 _dbus_get_install_root(
char *prefix,
int len)
3271 pathLength = GetModuleFileNameA(_dbus_win_get_dll_hmodule(), prefix, len);
3272 if ( pathLength == 0 || GetLastError() != 0 ) {
3276 lastSlash = _mbsrchr(prefix,
'\\');
3277 if (lastSlash ==
NULL) {
3290 if (lastSlash - prefix >= 4 && strnicmp(lastSlash - 4,
"\\bin", 4) == 0)
3292 else if (lastSlash - prefix >= 10 && strnicmp(lastSlash - 10,
"\\bin\\debug", 10) == 0)
3294 else if (lastSlash - prefix >= 12 && strnicmp(lastSlash - 12,
"\\bin\\release", 12) == 0)
3314 _dbus_get_config_file_name(
DBusString *config_file,
char *s)
3316 char path[MAX_PATH*2];
3317 int path_size =
sizeof(path);
3319 if (!_dbus_get_install_root(path,path_size))
3322 if(strlen(s) + 4 + strlen(path) >
sizeof(path)-2)
3324 strcat(path,
"etc\\");
3334 if (!_dbus_get_install_root(path,path_size))
3336 if(strlen(s) + 11 + strlen(path) >
sizeof(path)-2)
3338 strcat(path,
"etc\\dbus-1\\");
3348 if (!_dbus_get_install_root(path,path_size))
3350 if(strlen(s) + 4 + strlen(path) >
sizeof(path)-2)
3352 strcat(path,
"bus\\");
3395 const char *homepath;
3396 const char *homedrive;
3405 if (homedrive !=
NULL && *homedrive !=
'\0')
3411 if (homepath !=
NULL && *homepath !=
'\0')
3416 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
3418 const char *
override;
3421 if (
override !=
NULL && *
override !=
'\0')
3427 _dbus_verbose (
"Using fake homedir for testing: %s\n",
3428 _dbus_string_get_const_data (&homedir));
3435 if (!already_warned)
3437 _dbus_warn (
"Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
3438 already_warned =
TRUE;
3447 #define KEYRING_DIR "dbus-keyrings"
3449 #define KEYRING_DIR ".dbus-keyrings"
3458 directory, _dbus_string_get_length (directory))) {
3478 DWORD attributes = GetFileAttributesA (file);
3480 if (attributes != INVALID_FILE_ATTRIBUTES && GetLastError() != ERROR_PATH_NOT_FOUND)
3494 _dbus_strerror (
int error_number)
3502 switch (error_number)
3505 return "Interrupted function call";
3507 return "Permission denied";
3509 return "Bad address";
3511 return "Invalid argument";
3513 return "Too many open files";
3514 case WSAEWOULDBLOCK:
3515 return "Resource temporarily unavailable";
3516 case WSAEINPROGRESS:
3517 return "Operation now in progress";
3519 return "Operation already in progress";
3521 return "Socket operation on nonsocket";
3522 case WSAEDESTADDRREQ:
3523 return "Destination address required";
3525 return "Message too long";
3527 return "Protocol wrong type for socket";
3528 case WSAENOPROTOOPT:
3529 return "Bad protocol option";
3530 case WSAEPROTONOSUPPORT:
3531 return "Protocol not supported";
3532 case WSAESOCKTNOSUPPORT:
3533 return "Socket type not supported";
3535 return "Operation not supported";
3536 case WSAEPFNOSUPPORT:
3537 return "Protocol family not supported";
3538 case WSAEAFNOSUPPORT:
3539 return "Address family not supported by protocol family";
3541 return "Address already in use";
3542 case WSAEADDRNOTAVAIL:
3543 return "Cannot assign requested address";
3545 return "Network is down";
3546 case WSAENETUNREACH:
3547 return "Network is unreachable";
3549 return "Network dropped connection on reset";
3550 case WSAECONNABORTED:
3551 return "Software caused connection abort";
3553 return "Connection reset by peer";
3555 return "No buffer space available";
3557 return "Socket is already connected";
3559 return "Socket is not connected";
3561 return "Cannot send after socket shutdown";
3563 return "Connection timed out";
3564 case WSAECONNREFUSED:
3565 return "Connection refused";
3567 return "Host is down";
3568 case WSAEHOSTUNREACH:
3569 return "No route to host";
3571 return "Too many processes";
3573 return "Graceful shutdown in progress";
3574 case WSATYPE_NOT_FOUND:
3575 return "Class type not found";
3576 case WSAHOST_NOT_FOUND:
3577 return "Host not found";
3579 return "Nonauthoritative host not found";
3580 case WSANO_RECOVERY:
3581 return "This is a nonrecoverable error";
3583 return "Valid name, no data record of requested type";
3584 case WSA_INVALID_HANDLE:
3585 return "Specified event object handle is invalid";
3586 case WSA_INVALID_PARAMETER:
3587 return "One or more parameters are invalid";
3588 case WSA_IO_INCOMPLETE:
3589 return "Overlapped I/O event object not in signaled state";
3590 case WSA_IO_PENDING:
3591 return "Overlapped operations will complete later";
3592 case WSA_NOT_ENOUGH_MEMORY:
3593 return "Insufficient memory available";
3594 case WSA_OPERATION_ABORTED:
3595 return "Overlapped operation aborted";
3596 #ifdef WSAINVALIDPROCTABLE
3598 case WSAINVALIDPROCTABLE:
3599 return "Invalid procedure table from service provider";
3601 #ifdef WSAINVALIDPROVIDER
3603 case WSAINVALIDPROVIDER:
3604 return "Invalid service provider version number";
3606 #ifdef WSAPROVIDERFAILEDINIT
3608 case WSAPROVIDERFAILEDINIT:
3609 return "Unable to initialize a service provider";
3612 case WSASYSCALLFAILURE:
3613 return "System call failure";
3615 msg = strerror (error_number);
3631 _dbus_win_set_error_from_win_error (
DBusError *error,
3637 FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
3638 FORMAT_MESSAGE_IGNORE_INSERTS |
3639 FORMAT_MESSAGE_FROM_SYSTEM,
3640 NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
3641 (LPSTR) &msg, 0,
NULL);
3647 strcpy (msg_copy, msg);
3653 dbus_set_error (error,
"win32.error",
"Unknown error code %d or FormatMessage failed", code);
3657 _dbus_win_warn_win_error (
const char *message,
3663 _dbus_win_set_error_from_win_error (&error, code);
3679 const char *filename_c;
3681 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3683 filename_c = _dbus_string_get_const_data (filename);
3685 if (RemoveDirectoryA (filename_c) == 0)
3687 char *emsg = _dbus_win_error_string (GetLastError ());
3689 "Failed to remove directory %s: %s",
3691 _dbus_win_free_error_string (emsg);
3707 if (_dbus_string_get_length (filename) > 0)
3708 return _dbus_string_get_byte (filename, 1) ==
':'
3709 || _dbus_string_get_byte (filename, 0) ==
'\\'
3710 || _dbus_string_get_byte (filename, 0) ==
'/';