Prepare for WinCE port: Convert windows code to native API, avoid errno.

This commit is contained in:
Marcus Brinkmann 2010-03-16 03:27:42 +01:00 committed by Ralf Habacker
parent 4708efeebb
commit 6e214b5b3c
11 changed files with 497 additions and 415 deletions

View file

@ -29,12 +29,72 @@
#include "dbus-sysdeps-win.h" #include "dbus-sysdeps-win.h"
#include "dbus-pipe.h" #include "dbus-pipe.h"
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <windows.h> #include <windows.h>
/**
* Thin wrapper around the read() system call that appends
* the data it reads to the DBusString buffer. It appends
* up to the given count.
*
* @param hnd the HANDLE to read from
* @param buffer the buffer to append data to
* @param count the amount of data to read
* @param error place to set an error
* @returns the number of bytes read or -1
*/
static int
_dbus_file_read (HANDLE hnd,
DBusString *buffer,
int count,
DBusError *error)
{
BOOL result;
DWORD bytes_read;
int start;
char *data;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
_dbus_assert (count >= 0);
start = _dbus_string_get_length (buffer);
if (!_dbus_string_lengthen (buffer, count))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return -1;
}
data = _dbus_string_get_data_len (buffer, start, count);
again:
result = ReadFile (hnd, data, count, &bytes_read, NULL);
if (result == 0)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Failed to read from 0x%x: %s", hnd, emsg);
_dbus_win_free_error_string (emsg);
return -1;
}
if (bytes_read)
{
/* put length back (doesn't actually realloc) */
_dbus_string_set_length (buffer, start + bytes_read);
#if 0
if (bytes_read > 0)
_dbus_verbose_bytes_of_string (buffer, start, bytes_read);
#endif
}
return bytes_read;
}
/** /**
* Appends the contents of the given file to the string, * Appends the contents of the given file to the string,
* returning error code. At the moment, won't open a file * returning error code. At the moment, won't open a file
@ -50,8 +110,9 @@ _dbus_file_get_contents (DBusString *str,
const DBusString *filename, const DBusString *filename,
DBusError *error) DBusError *error)
{ {
int fd; HANDLE hnd;
struct _stati64 sb; DWORD fsize;
DWORD fsize_hi;
int orig_len; int orig_len;
int total; int total;
const char *filename_c; const char *filename_c;
@ -60,62 +121,67 @@ _dbus_file_get_contents (DBusString *str,
filename_c = _dbus_string_get_const_data (filename); filename_c = _dbus_string_get_const_data (filename);
fd = _open (filename_c, O_RDONLY | O_BINARY); hnd = CreateFileA (filename_c, GENERIC_READ,
if (fd < 0) FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hnd == INVALID_HANDLE_VALUE)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
"Failed to open \"%s\": %s", dbus_set_error (error, _dbus_win_error_from_last_error (),
filename_c, "Failed to open \"%s\": %s", filename_c, emsg);
strerror (errno)); _dbus_win_free_error_string (emsg);
return FALSE; return FALSE;
} }
_dbus_verbose ("file %s fd %d opened\n", filename_c, fd); _dbus_verbose ("file %s fd 0x%x opened\n", filename_c, hnd);
fsize = GetFileSize (hnd, &fsize_hi);
if (fsize == 0xFFFFFFFF && GetLastError() != NO_ERROR)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Failed to get file size for \"%s\": %s",
filename_c, emsg);
_dbus_win_free_error_string (emsg);
if (_fstati64 (fd, &sb) < 0) _dbus_verbose ("GetFileSize() failed: %s", emsg);
{
dbus_set_error (error, _dbus_error_from_errno (errno),
"Failed to stat \"%s\": %s",
filename_c,
strerror (errno));
_dbus_verbose ("fstat() failed: %s", CloseHandle (hnd);
strerror (errno));
_close (fd);
return FALSE; return FALSE;
} }
if (sb.st_size > _DBUS_ONE_MEGABYTE) if (fsize_hi != 0 || fsize > _DBUS_ONE_MEGABYTE)
{ {
dbus_set_error (error, DBUS_ERROR_FAILED, dbus_set_error (error, DBUS_ERROR_FAILED,
"File size %lu of \"%s\" is too large.", "File size %lu/%lu of \"%s\" is too large.",
(unsigned long) sb.st_size, filename_c); (unsigned long) fsize_hi,
_close (fd); (unsigned long) fsize, filename_c);
CloseHandle (hnd);
return FALSE; return FALSE;
} }
total = 0; total = 0;
orig_len = _dbus_string_get_length (str); orig_len = _dbus_string_get_length (str);
if (sb.st_size > 0 && S_ISREG (sb.st_mode)) if (fsize > 0)
{ {
int bytes_read; int bytes_read;
while (total < (int) sb.st_size) while (total < fsize)
{ {
bytes_read = _dbus_file_read (fd, str, sb.st_size - total); bytes_read = _dbus_file_read (hnd, str, fsize - total, error);
if (bytes_read <= 0) if (bytes_read <= 0)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), if (bytes_read == 0)
"Error reading \"%s\": %s", {
filename_c, dbus_set_error (error, DBUS_ERROR_FAILED,
strerror (errno)); "Premature EOF reading \"%s\"",
filename_c);
}
else
_DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_verbose ("read() failed: %s", CloseHandle (hnd);
strerror (errno));
_close (fd);
_dbus_string_set_length (str, orig_len); _dbus_string_set_length (str, orig_len);
return FALSE; return FALSE;
} }
@ -123,25 +189,17 @@ _dbus_file_get_contents (DBusString *str,
total += bytes_read; total += bytes_read;
} }
_close (fd); CloseHandle (hnd);
return TRUE; return TRUE;
} }
else if (sb.st_size != 0)
{
_dbus_verbose ("Can only open regular files at the moment.\n");
dbus_set_error (error, DBUS_ERROR_FAILED,
"\"%s\" is not a regular file",
filename_c);
_close (fd);
return FALSE;
}
else else
{ {
_close (fd); CloseHandle (hnd);
return TRUE; return TRUE;
} }
} }
/** /**
* Writes a string out to a file. If the file exists, * Writes a string out to a file. If the file exists,
* it will be atomically overwritten by the new data. * it will be atomically overwritten by the new data.
@ -156,7 +214,7 @@ _dbus_string_save_to_file (const DBusString *str,
const DBusString *filename, const DBusString *filename,
DBusError *error) DBusError *error)
{ {
int fd; HANDLE hnd;
int bytes_to_write; int bytes_to_write;
const char *filename_c; const char *filename_c;
DBusString tmp_filename; DBusString tmp_filename;
@ -168,7 +226,7 @@ _dbus_string_save_to_file (const DBusString *str,
_DBUS_ASSERT_ERROR_IS_CLEAR (error); _DBUS_ASSERT_ERROR_IS_CLEAR (error);
fd = -1; hnd = INVALID_HANDLE_VALUE;
retval = FALSE; retval = FALSE;
need_unlink = FALSE; need_unlink = FALSE;
@ -203,17 +261,20 @@ _dbus_string_save_to_file (const DBusString *str,
filename_c = _dbus_string_get_const_data (filename); filename_c = _dbus_string_get_const_data (filename);
tmp_filename_c = _dbus_string_get_const_data (&tmp_filename); tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
fd = _open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT, hnd = CreateFileA (tmp_filename_c, GENERIC_WRITE,
0600); FILE_SHARE_READ | FILE_SHARE_WRITE,
if (fd < 0) NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE);
if (hnd == INVALID_HANDLE_VALUE)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
"Could not create %s: %s", tmp_filename_c, dbus_set_error (error, _dbus_win_error_from_last_error (),
strerror (errno)); "Could not create \"%s\": %s", filename_c, emsg);
_dbus_win_free_error_string (emsg);
goto out; goto out;
} }
_dbus_verbose ("tmp file %s fd %d opened\n", tmp_filename_c, fd); _dbus_verbose ("tmp file %s fd 0x%x opened\n", tmp_filename_c, hnd);
need_unlink = TRUE; need_unlink = TRUE;
@ -223,40 +284,42 @@ _dbus_string_save_to_file (const DBusString *str,
while (total < bytes_to_write) while (total < bytes_to_write)
{ {
int bytes_written; DWORD bytes_written;
BOOL res;
bytes_written = _write (fd, str_c + total, bytes_to_write - total); res = WriteFile (hnd, str_c + total, bytes_to_write - total,
&bytes_written, NULL);
if (bytes_written <= 0) if (res == 0 || bytes_written <= 0)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
"Could not write to %s: %s", tmp_filename_c, dbus_set_error (error, _dbus_win_error_from_last_error (),
strerror (errno)); "Could not write to %s: %s", tmp_filename_c, emsg);
_dbus_win_free_error_string (emsg);
goto out; goto out;
} }
total += bytes_written; total += bytes_written;
} }
if (_close (fd) < 0) if (CloseHandle (hnd) == 0)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
"Could not close file %s: %s", dbus_set_error (error, _dbus_win_error_from_last_error (),
tmp_filename_c, strerror (errno)); "Could not close file %s: %s", tmp_filename_c, emsg);
_dbus_win_free_error_string (emsg);
goto out; goto out;
} }
fd = -1; hnd = INVALID_HANDLE_VALUE;
/* Unlike rename(), MoveFileEx() can replace existing files */ /* Unlike rename(), MoveFileEx() can replace existing files */
if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING)) if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING))
{ {
char *emsg = _dbus_win_error_string (GetLastError ()); char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, DBUS_ERROR_FAILED, dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not rename %s to %s: %s", "Could not rename %s to %s: %s",
tmp_filename_c, filename_c, tmp_filename_c, filename_c, emsg);
emsg);
_dbus_win_free_error_string (emsg); _dbus_win_free_error_string (emsg);
goto out; goto out;
@ -269,12 +332,16 @@ _dbus_string_save_to_file (const DBusString *str,
out: out:
/* close first, then unlink */ /* close first, then unlink */
if (fd >= 0) if (hnd != INVALID_HANDLE_VALUE)
_close (fd); CloseHandle (hnd);
if (need_unlink && _unlink (tmp_filename_c) < 0) if (need_unlink && DeleteFileA (tmp_filename_c) == 0)
_dbus_verbose ("failed to unlink temp file %s: %s\n", {
tmp_filename_c, strerror (errno)); char *emsg = _dbus_win_error_string (GetLastError ());
_dbus_verbose ("Failed to unlink temp file %s: %s", tmp_filename_c,
emsg);
_dbus_win_free_error_string (emsg);
}
_dbus_string_free (&tmp_filename); _dbus_string_free (&tmp_filename);
@ -295,99 +362,40 @@ dbus_bool_t
_dbus_create_file_exclusively (const DBusString *filename, _dbus_create_file_exclusively (const DBusString *filename,
DBusError *error) DBusError *error)
{ {
int fd; HANDLE hnd;
const char *filename_c; const char *filename_c;
_DBUS_ASSERT_ERROR_IS_CLEAR (error); _DBUS_ASSERT_ERROR_IS_CLEAR (error);
filename_c = _dbus_string_get_const_data (filename); filename_c = _dbus_string_get_const_data (filename);
fd = _open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT, hnd = CreateFileA (filename_c, GENERIC_WRITE,
0600); FILE_SHARE_READ | FILE_SHARE_WRITE,
if (fd < 0) NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE);
if (hnd == INVALID_HANDLE_VALUE)
{ {
dbus_set_error (error, char *emsg = _dbus_win_error_string (GetLastError ());
DBUS_ERROR_FAILED, dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not create file %s: %s\n", "Could not create file %s: %s",
filename_c, filename_c, emsg);
strerror (errno)); _dbus_win_free_error_string (emsg);
return FALSE; return FALSE;
} }
_dbus_verbose ("exclusive file %s fd %d opened\n", filename_c, fd); _dbus_verbose ("exclusive file %s fd 0x%x opened\n", filename_c, hnd);
if (_close (fd) < 0) if (CloseHandle (hnd) == 0)
{ {
dbus_set_error (error, char *emsg = _dbus_win_error_string (GetLastError ());
DBUS_ERROR_FAILED, dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not close file %s: %s\n", "Could not close file %s: %s",
filename_c, filename_c, emsg);
strerror (errno)); _dbus_win_free_error_string (emsg);
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
} }
/**
* Thin wrapper around the read() system call that appends
* the data it reads to the DBusString buffer. It appends
* up to the given count, and returns the same value
* and same errno as read(). The only exception is that
* _dbus_file_read() handles EINTR for you. Also,
* _dbus_file_read() can return ENOMEM.
*
* @param fd the file descriptor to read from
* @param buffer the buffer to append data to
* @param count the amount of data to read
* @returns the number of bytes read or -1
*/
int
_dbus_file_read (int fd,
DBusString *buffer,
int count)
{
int bytes_read;
int start;
char *data;
_dbus_assert (count >= 0);
start = _dbus_string_get_length (buffer);
if (!_dbus_string_lengthen (buffer, count))
{
errno = ENOMEM;
return -1;
}
data = _dbus_string_get_data_len (buffer, start, count);
again:
bytes_read = _read (fd, data, count);
if (bytes_read < 0)
{
if (errno == EINTR)
goto again;
else
{
/* put length back (note that this doesn't actually realloc anything) */
_dbus_string_set_length (buffer, start);
return -1;
}
}
else
{
/* put length back (doesn't actually realloc) */
_dbus_string_set_length (buffer, start + bytes_read);
#if 0
if (bytes_read > 0)
_dbus_verbose_bytes_of_string (buffer, start, bytes_read);
#endif
return bytes_read;
}
}

View file

@ -54,9 +54,6 @@ dbus_bool_t _dbus_create_file_exclusively (const DBusString *filename,
DBusError *error); DBusError *error);
dbus_bool_t _dbus_delete_file (const DBusString *filename, dbus_bool_t _dbus_delete_file (const DBusString *filename,
DBusError *error); DBusError *error);
int _dbus_file_read (int fd,
DBusString *buffer,
int count);
/** @} */ /** @} */

View file

@ -30,10 +30,6 @@
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
static dbus_bool_t static dbus_bool_t
do_check_nonce (int fd, const DBusString *nonce, DBusError *error) do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
{ {
@ -191,7 +187,7 @@ generate_and_write_nonce (const DBusString *filename, DBusError *error)
* indicate whether the server accepted the nonce. * indicate whether the server accepted the nonce.
*/ */
dbus_bool_t dbus_bool_t
_dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error) _dbus_send_nonce (int fd, const DBusString *noncefile, DBusError *error)
{ {
dbus_bool_t read_result; dbus_bool_t read_result;
int send_result; int send_result;
@ -203,36 +199,33 @@ _dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error)
if (_dbus_string_get_length (noncefile) == 0) if (_dbus_string_get_length (noncefile) == 0)
return FALSE; return FALSE;
if ( !_dbus_string_init (&nonce) ) if (!_dbus_string_init (&nonce))
{ {
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return FALSE; return FALSE;
} }
read_result = _dbus_read_nonce (noncefile, &nonce, NULL);
read_result = _dbus_read_nonce (noncefile, &nonce, error);
if (!read_result) if (!read_result)
{ {
dbus_set_error (error, _DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_error_from_errno (errno),
"Could not read nonce from file %s (%s)",
_dbus_string_get_const_data (noncefile), _dbus_strerror(errno));
_dbus_string_free (&nonce); _dbus_string_free (&nonce);
return FALSE; return FALSE;
} }
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce)); send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
_dbus_string_free (&nonce); _dbus_string_free (&nonce);
if (send_result == -1) if (send_result == -1)
{ {
dbus_set_error (error, dbus_set_error (error,
_dbus_error_from_errno (errno), _dbus_error_from_system_errno (),
"Failed to send nonce (fd=%d): %s", "Failed to send nonce (fd=%d): %s",
fd, _dbus_strerror(errno)); fd, _dbus_strerror_from_errno ());
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
} }
@ -284,8 +277,10 @@ do_noncefile_create (DBusNonceFile *noncefile,
} }
if (!_dbus_create_directory (&noncefile->dir, error)) if (!_dbus_create_directory (&noncefile->dir, error))
{ {
_DBUS_ASSERT_ERROR_IS_SET (error);
goto on_error; goto on_error;
} }
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
} }
else else
@ -303,10 +298,12 @@ do_noncefile_create (DBusNonceFile *noncefile,
if (!generate_and_write_nonce (&noncefile->path, error)) if (!generate_and_write_nonce (&noncefile->path, error))
{ {
_DBUS_ASSERT_ERROR_IS_SET (error);
if (use_subdir) if (use_subdir)
_dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
goto on_error; goto on_error;
} }
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
_dbus_string_free (&randomStr); _dbus_string_free (&randomStr);

View file

@ -28,8 +28,7 @@
#include "dbus-internals.h" #include "dbus-internals.h"
#include "dbus-pipe.h" #include "dbus-pipe.h"
#include <io.h> #include <windows.h>
#include <errno.h>
/** /**
* write data to a pipe. * write data to a pipe.
@ -48,15 +47,17 @@ _dbus_pipe_write (DBusPipe *pipe,
int len, int len,
DBusError *error) DBusError *error)
{ {
int written; DWORD written;
BOOL res;
const char *buffer_c = _dbus_string_get_const_data (buffer); const char *buffer_c = _dbus_string_get_const_data (buffer);
written = _write (pipe->fd_or_handle, buffer_c + start, len); res = WriteFile ((HANDLE) pipe->fd_or_handle, buffer_c + start, len, &written, NULL);
if (written < 0) if (res == 0 || written < 0)
{ {
dbus_set_error (error, DBUS_ERROR_FAILED, dbus_set_error (error, DBUS_ERROR_FAILED,
"Writing to pipe: %s\n", "Writing to pipe: %s\n",
strerror (errno)); _dbus_strerror_from_errno ());
} }
return written; return written;
} }
@ -74,10 +75,11 @@ _dbus_pipe_close (DBusPipe *pipe,
{ {
_DBUS_ASSERT_ERROR_IS_CLEAR (error); _DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (_close (pipe->fd_or_handle) < 0) if (CloseHandle ((HANDLE) pipe->fd_or_handle) == 0)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_system_errno (),
"Could not close pipe %d: %s", pipe->fd_or_handle, strerror (errno)); "Could not close pipe %d: %s", pipe->fd_or_handle,
_dbus_strerror_from_errno ());
return -1; return -1;
} }
else else

View file

@ -37,7 +37,9 @@
#undef interface #undef interface
#if HAVE_ERRNO_H
#include <errno.h> #include <errno.h>
#endif
/* Make use of the fact that the WSAE* error codes don't /* Make use of the fact that the WSAE* error codes don't
* overlap with errno E* codes. Wrapper functions store * overlap with errno E* codes. Wrapper functions store
@ -51,7 +53,7 @@
#define DBUS_SOCKET_IS_INVALID(s) ((SOCKET)(s) == INVALID_SOCKET) #define DBUS_SOCKET_IS_INVALID(s) ((SOCKET)(s) == INVALID_SOCKET)
#define DBUS_SOCKET_API_RETURNS_ERROR(n) ((n) == SOCKET_ERROR) #define DBUS_SOCKET_API_RETURNS_ERROR(n) ((n) == SOCKET_ERROR)
#define DBUS_SOCKET_SET_ERRNO() errno = WSAGetLastError() #define DBUS_SOCKET_SET_ERRNO() (_dbus_win_set_errno (WSAGetLastError()))
#define DBUS_CLOSE_SOCKET(s) closesocket(s) #define DBUS_CLOSE_SOCKET(s) closesocket(s)

View file

@ -315,9 +315,11 @@ _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
PING(); PING();
if (sitter->have_spawn_errno) if (sitter->have_spawn_errno)
{ {
char *emsg = _dbus_win_error_string (sitter->spawn_errno);
dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED, dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
"Failed to execute program %s: %s", "Failed to execute program %s: %s",
sitter->executable, _dbus_strerror (sitter->spawn_errno)); sitter->executable, emsg);
_dbus_win_free_error_string (emsg);
} }
else if (sitter->have_child_status) else if (sitter->have_child_status)
{ {
@ -467,7 +469,64 @@ protect_argv (char **argv,
return argc; return argc;
} }
static unsigned __stdcall
/* From GPGME, relicensed by g10 Code GmbH. */
static char *
build_commandline (char **argv)
{
int i;
int n = 0;
char *buf;
char *p;
const char *ptr;
for (i = 0; argv[i]; i++)
n += strlen (argv[i]) + 1;
n++;
buf = p = malloc (n);
if (!buf)
return NULL;
for (i = 0; argv[i]; i++)
{
strcpy (p, argv[i]);
p += strlen (argv[i]);
*(p++) = ' ';
}
if (i)
p--;
*p = '\0';
return buf;
}
static HANDLE
spawn_program (const char* name, char** argv, char** envp)
{
PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
STARTUPINFOA si;
char *arg_string;
BOOL result;
arg_string = build_commandline (argv);
if (!arg_string)
return INVALID_HANDLE_VALUE;
memset (&si, 0, sizeof (si));
si.cb = sizeof (si);
result = CreateProcessA (name, arg_string, NULL, NULL, FALSE, 0,
envp, NULL, &si, &pi);
free (arg_string);
if (!result)
return INVALID_HANDLE_VALUE;
CloseHandle (pi.hThread);
return pi.hProcess;
}
static DWORD __stdcall
babysitter (void *parameter) babysitter (void *parameter)
{ {
DBusBabysitter *sitter = (DBusBabysitter *) parameter; DBusBabysitter *sitter = (DBusBabysitter *) parameter;
@ -484,22 +543,17 @@ babysitter (void *parameter)
_dbus_verbose ("babysitter: spawning %s\n", sitter->executable); _dbus_verbose ("babysitter: spawning %s\n", sitter->executable);
PING(); PING();
if (sitter->envp != NULL) sitter->child_handle = spawn_program (sitter->executable,
sitter->child_handle = (HANDLE) spawnve (P_NOWAIT, sitter->executable, sitter->argv, sitter->envp);
(const char * const *) sitter->argv,
(const char * const *) sitter->envp);
else
sitter->child_handle = (HANDLE) spawnv (P_NOWAIT, sitter->executable,
(const char * const *) sitter->argv);
PING(); PING();
if (sitter->child_handle == (HANDLE) -1) if (sitter->child_handle == (HANDLE) -1)
{ {
sitter->child_handle = NULL; sitter->child_handle = NULL;
sitter->have_spawn_errno = TRUE; sitter->have_spawn_errno = TRUE;
sitter->spawn_errno = errno; sitter->spawn_errno = GetLastError();
} }
PING(); PING();
SetEvent (sitter->start_sync_event); SetEvent (sitter->start_sync_event);
@ -543,8 +597,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
{ {
DBusBabysitter *sitter; DBusBabysitter *sitter;
HANDLE sitter_thread; HANDLE sitter_thread;
int sitter_thread_id; DWORD sitter_thread_id;
_DBUS_ASSERT_ERROR_IS_CLEAR (error); _DBUS_ASSERT_ERROR_IS_CLEAR (error);
*sitter_p = NULL; *sitter_p = NULL;
@ -599,7 +653,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
sitter->envp = envp; sitter->envp = envp;
PING(); PING();
sitter_thread = (HANDLE) _beginthreadex (NULL, 0, babysitter, sitter_thread = (HANDLE) CreateThread (NULL, 0, babysitter,
sitter, 0, &sitter_thread_id); sitter, 0, &sitter_thread_id);
if (sitter_thread == 0) if (sitter_thread == 0)

View file

@ -43,7 +43,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#if HAVE_ERRNO_H
#include <errno.h> #include <errno.h>
#endif
#include <winsock2.h> // WSA error codes #include <winsock2.h> // WSA error codes
/** /**
@ -78,44 +80,70 @@ _dbus_write_pid_file (const DBusString *filename,
DBusError *error) DBusError *error)
{ {
const char *cfilename; const char *cfilename;
int fd; HANDLE hnd;
FILE *f; char pidstr[20];
int total;
int bytes_to_write;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
cfilename = _dbus_string_get_const_data (filename); cfilename = _dbus_string_get_const_data (filename);
fd = _open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644); hnd = CreateFileA (cfilename, GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
if (fd < 0) NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE);
if (hnd == INVALID_HANDLE_VALUE)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
"Failed to open \"%s\": %s", cfilename, dbus_set_error (error, _dbus_win_error_from_last_error (),
strerror (errno)); "Could not create PID file %s: %s",
cfilename, emsg);
_dbus_win_free_error_string (emsg);
return FALSE; return FALSE;
} }
if ((f = fdopen (fd, "w")) == NULL) if (snprintf (pidstr, sizeof (pidstr), "%lu\n", pid) < 0)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_system_errno (),
"Failed to fdopen fd %d: %s", fd, strerror (errno)); "Failed to format PID for \"%s\": %s", cfilename,
_close (fd); _dbus_strerror_from_errno ());
CloseHandle (hnd);
return FALSE; return FALSE;
} }
if (fprintf (f, "%lu\n", pid) < 0) total = 0;
{ bytes_to_write = strlen (pidstr);;
dbus_set_error (error, _dbus_error_from_errno (errno),
"Failed to write to \"%s\": %s", cfilename,
strerror (errno));
fclose (f); while (total < bytes_to_write)
return FALSE; {
DWORD bytes_written;
BOOL res;
res = WriteFile (hnd, pidstr + total, bytes_to_write - total,
&bytes_written, NULL);
if (res == 0 || bytes_written <= 0)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not write to %s: %s", cfilename, emsg);
_dbus_win_free_error_string (emsg);
CloseHandle (hnd);
return FALSE;
}
total += bytes_written;
} }
if (fclose (f) == EOF) if (CloseHandle (hnd) == 0)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
"Failed to close \"%s\": %s", cfilename, dbus_set_error (error, _dbus_win_error_from_last_error (),
strerror (errno)); "Could not close file %s: %s",
cfilename, emsg);
_dbus_win_free_error_string (emsg);
return FALSE; return FALSE;
} }
@ -293,22 +321,16 @@ _dbus_stat(const DBusString *filename,
DBusStat *statbuf, DBusStat *statbuf,
DBusError *error) DBusError *error)
{ {
#ifdef DBUS_WINCE
return TRUE;
//TODO
#else
const char *filename_c; const char *filename_c;
WIN32_FILE_ATTRIBUTE_DATA wfad; WIN32_FILE_ATTRIBUTE_DATA wfad;
char *lastdot; char *lastdot;
DWORD rc; DWORD rc;
PSID owner_sid, group_sid;
PSECURITY_DESCRIPTOR sd;
_DBUS_ASSERT_ERROR_IS_CLEAR (error); _DBUS_ASSERT_ERROR_IS_CLEAR (error);
filename_c = _dbus_string_get_const_data (filename); filename_c = _dbus_string_get_const_data (filename);
if (!GetFileAttributesEx (filename_c, GetFileExInfoStandard, &wfad)) if (!GetFileAttributesExA (filename_c, GetFileExInfoStandard, &wfad))
{ {
_dbus_win_set_error_from_win_error (error, GetLastError ()); _dbus_win_set_error_from_win_error (error, GetLastError ());
return FALSE; return FALSE;
@ -332,28 +354,36 @@ _dbus_stat(const DBusString *filename,
statbuf->nlink = 1; statbuf->nlink = 1;
sd = NULL;
rc = GetNamedSecurityInfo ((char *) filename_c, SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION,
&owner_sid, &group_sid,
NULL, NULL,
&sd);
if (rc != ERROR_SUCCESS)
{
_dbus_win_set_error_from_win_error (error, rc);
if (sd != NULL)
LocalFree (sd);
return FALSE;
}
#ifdef ENABLE_UID_TO_SID #ifdef ENABLE_UID_TO_SID
/* FIXME */ {
statbuf->uid = _dbus_win_sid_to_uid_t (owner_sid); PSID owner_sid, group_sid;
statbuf->gid = _dbus_win_sid_to_uid_t (group_sid); PSECURITY_DESCRIPTOR sd;
#endif
LocalFree (sd); sd = NULL;
rc = GetNamedSecurityInfo ((char *) filename_c, SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION,
&owner_sid, &group_sid,
NULL, NULL,
&sd);
if (rc != ERROR_SUCCESS)
{
_dbus_win_set_error_from_win_error (error, rc);
if (sd != NULL)
LocalFree (sd);
return FALSE;
}
/* FIXME */
statbuf->uid = _dbus_win_sid_to_uid_t (owner_sid);
statbuf->gid = _dbus_win_sid_to_uid_t (group_sid);
LocalFree (sd);
}
#else
statbuf->uid = DBUS_UID_UNSET;
statbuf->gid = DBUS_GID_UNSET;
#endif
statbuf->size = ((dbus_int64_t) wfad.nFileSizeHigh << 32) + wfad.nFileSizeLow; statbuf->size = ((dbus_int64_t) wfad.nFileSizeHigh << 32) + wfad.nFileSizeLow;
@ -370,27 +400,9 @@ _dbus_stat(const DBusString *filename,
wfad.ftCreationTime.dwLowDateTime) / 10000000 - DBUS_INT64_CONSTANT (116444736000000000); wfad.ftCreationTime.dwLowDateTime) / 10000000 - DBUS_INT64_CONSTANT (116444736000000000);
return TRUE; return TRUE;
#endif //DBUS_WINCE
} }
#ifdef HAVE_DIRENT_H
// mingw ships with dirent.h
#include <dirent.h>
#define _dbus_opendir opendir
#define _dbus_readdir readdir
#define _dbus_closedir closedir
#else
#ifdef HAVE_IO_H
#include <io.h> // win32 file functions
#endif
#include <sys/types.h>
#include <stdlib.h>
/* This file is part of the KDE project /* This file is part of the KDE project
Copyright (C) 2000 Werner Almesberger Copyright (C) 2000 Werner Almesberger
@ -430,10 +442,10 @@ struct dirent
/* typedef DIR - not the same as Unix */ /* typedef DIR - not the same as Unix */
typedef struct typedef struct
{ {
long handle; /* _findfirst/_findnext handle */ HANDLE handle; /* FindFirst/FindNext handle */
short offset; /* offset into directory */ short offset; /* offset into directory */
short finished; /* 1 if there are not more files */ short finished; /* 1 if there are not more files */
struct _finddata_t fileinfo; /* from _findfirst/_findnext */ WIN32_FIND_DATAA fileinfo; /* from FindFirst/FindNext */
char *dir; /* the dir we are reading */ char *dir; /* the dir we are reading */
struct dirent dent; /* the dirent to return */ struct dirent dent; /* the dirent to return */
} }
@ -449,6 +461,8 @@ DIR;
* The dirent struct is compatible with Unix, except that d_ino is * The dirent struct is compatible with Unix, except that d_ino is
* always 1 and d_off is made up as we go along. * always 1 and d_off is made up as we go along.
* *
* Error codes are not available with errno but GetLastError.
*
* The DIR typedef is not compatible with Unix. * The DIR typedef is not compatible with Unix.
**********************************************************************/ **********************************************************************/
@ -456,7 +470,7 @@ static DIR * _dbus_opendir(const char *dir)
{ {
DIR *dp; DIR *dp;
char *filespec; char *filespec;
long handle; HANDLE handle;
int index; int index;
filespec = malloc(strlen(dir) + 2 + 1); filespec = malloc(strlen(dir) + 2 + 1);
@ -471,9 +485,10 @@ static DIR * _dbus_opendir(const char *dir)
dp->finished = 0; dp->finished = 0;
dp->dir = strdup(dir); dp->dir = strdup(dir);
if ((handle = _findfirst(filespec, &(dp->fileinfo))) < 0) handle = FindFirstFileA(filespec, &(dp->fileinfo));
if (handle == INVALID_HANDLE_VALUE)
{ {
if (errno == ENOENT) if (GetLastError() == ERROR_NO_MORE_FILES)
dp->finished = 1; dp->finished = 1;
else else
return NULL; return NULL;
@ -486,35 +501,40 @@ static DIR * _dbus_opendir(const char *dir)
} }
static struct dirent * _dbus_readdir(DIR *dp) static struct dirent * _dbus_readdir(DIR *dp)
{ {
if (!dp || dp->finished) int saved_err = GetLastError();
return NULL;
if (dp->offset != 0) if (!dp || dp->finished)
{ return NULL;
if (_findnext(dp->handle, &(dp->fileinfo)) < 0)
{
dp->finished = 1;
errno = 0;
return NULL;
}
}
dp->offset++;
strncpy(dp->dent.d_name, dp->fileinfo.name, _MAX_FNAME); if (dp->offset != 0)
dp->dent.d_ino = 1; {
dp->dent.d_reclen = strlen(dp->dent.d_name); if (FindNextFileA(dp->handle, &(dp->fileinfo)) == 0)
dp->dent.d_off = dp->offset; {
if (GetLastError() == ERROR_NO_MORE_FILES)
return &(dp->dent); {
} SetLastError(saved_err);
dp->finished = 1;
}
return NULL;
}
}
dp->offset++;
strncpy(dp->dent.d_name, dp->fileinfo.cFileName, _MAX_FNAME);
dp->dent.d_ino = 1;
dp->dent.d_reclen = strlen(dp->dent.d_name);
dp->dent.d_off = dp->offset;
return &(dp->dent);
}
static int _dbus_closedir(DIR *dp) static int _dbus_closedir(DIR *dp)
{ {
if (!dp) if (!dp)
return 0; return 0;
_findclose(dp->handle); FindClose(dp->handle);
if (dp->dir) if (dp->dir)
free(dp->dir); free(dp->dir);
if (dp) if (dp)
@ -523,7 +543,6 @@ static int _dbus_closedir(DIR *dp)
return 0; return 0;
} }
#endif //#ifdef HAVE_DIRENT_H
/** /**
* Internals of directory iterator * Internals of directory iterator
@ -556,10 +575,11 @@ _dbus_directory_open (const DBusString *filename,
d = _dbus_opendir (filename_c); d = _dbus_opendir (filename_c);
if (d == NULL) if (d == NULL)
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Failed to read directory \"%s\": %s", "Failed to read directory \"%s\": %s",
filename_c, filename_c, emsg);
_dbus_strerror (errno)); _dbus_win_free_error_string (emsg);
return NULL; return NULL;
} }
iter = dbus_new0 (DBusDirIter, 1); iter = dbus_new0 (DBusDirIter, 1);
@ -599,14 +619,17 @@ _dbus_directory_get_next_file (DBusDirIter *iter,
_DBUS_ASSERT_ERROR_IS_CLEAR (error); _DBUS_ASSERT_ERROR_IS_CLEAR (error);
again: again:
errno = 0; SetLastError (0);
ent = _dbus_readdir (iter->d); ent = _dbus_readdir (iter->d);
if (ent == NULL) if (ent == NULL)
{ {
if (errno != 0) if (GetLastError() != 0)
dbus_set_error (error, {
_dbus_error_from_errno (errno), char *emsg = _dbus_win_error_string (GetLastError ());
"%s", _dbus_strerror (errno)); dbus_set_error (error, _dbus_win_error_from_last_error (),
"Failed to get next in directory: %s", emsg);
_dbus_win_free_error_string (emsg);
}
return FALSE; return FALSE;
} }
else if (ent->d_name[0] == '.' && else if (ent->d_name[0] == '.' &&

View file

@ -65,7 +65,9 @@ extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
#include <string.h> #include <string.h>
#include <mbstring.h> #include <mbstring.h>
#if HAVE_ERRNO_H
#include <errno.h> #include <errno.h>
#endif
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -85,16 +87,56 @@ extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
typedef int socklen_t; typedef int socklen_t;
void
_dbus_win_set_errno (int err)
{
errno = err;
}
/* Convert GetLastError() to a dbus error. */
const char*
_dbus_win_error_from_last_error (void)
{
switch (GetLastError())
{
case 0:
return DBUS_ERROR_FAILED;
case ERROR_NO_MORE_FILES:
case ERROR_TOO_MANY_OPEN_FILES:
return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
case ERROR_ACCESS_DENIED:
case ERROR_CANNOT_MAKE:
return DBUS_ERROR_ACCESS_DENIED;
case ERROR_NOT_ENOUGH_MEMORY:
return DBUS_ERROR_NO_MEMORY;
case ERROR_FILE_EXISTS:
return DBUS_ERROR_FILE_EXISTS;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return DBUS_ERROR_FILE_NOT_FOUND;
}
return DBUS_ERROR_FAILED;
}
char* char*
_dbus_win_error_string (int error_number) _dbus_win_error_string (int error_number)
{ {
char *msg; char *msg;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error_number, 0, NULL, error_number, 0,
(LPSTR) &msg, 0, NULL); (LPSTR) &msg, 0, NULL);
if (msg[strlen (msg) - 1] == '\n') if (msg[strlen (msg) - 1] == '\n')
msg[strlen (msg) - 1] = '\0'; msg[strlen (msg) - 1] = '\0';
@ -145,7 +187,7 @@ _dbus_read_socket (int fd,
if (!_dbus_string_lengthen (buffer, count)) if (!_dbus_string_lengthen (buffer, count))
{ {
errno = ENOMEM; _dbus_win_set_errno (ENOMEM);
return -1; return -1;
} }
@ -219,7 +261,7 @@ _dbus_write_socket (int fd,
if (bytes_written == SOCKET_ERROR) if (bytes_written == SOCKET_ERROR)
{ {
DBUS_SOCKET_SET_ERRNO(); DBUS_SOCKET_SET_ERRNO();
_dbus_verbose ("send: failed: %s\n", _dbus_strerror (errno)); _dbus_verbose ("send: failed: %s\n", _dbus_strerror_from_errno ());
bytes_written = -1; bytes_written = -1;
} }
else else
@ -260,7 +302,7 @@ _dbus_close_socket (int fd,
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_errno (errno),
"Could not close socket: socket=%d, , %s", "Could not close socket: socket=%d, , %s",
fd, _dbus_strerror (errno)); fd, _dbus_strerror_from_errno ());
return FALSE; return FALSE;
} }
_dbus_verbose ("_dbus_close_socket: socket=%d, \n", fd); _dbus_verbose ("_dbus_close_socket: socket=%d, \n", fd);
@ -385,7 +427,7 @@ _dbus_write_socket_two (int fd,
if (rc < 0) if (rc < 0)
{ {
DBUS_SOCKET_SET_ERRNO (); DBUS_SOCKET_SET_ERRNO ();
_dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror (errno)); _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror_from_errno ());
bytes_written = -1; bytes_written = -1;
} }
else else
@ -763,6 +805,7 @@ _dbus_full_duplex_pipe (int *fd1,
u_long arg; u_long arg;
fd_set read_set, write_set; fd_set read_set, write_set;
struct timeval tv; struct timeval tv;
int res;
_dbus_win_startup_winsock (); _dbus_win_startup_winsock ();
@ -773,13 +816,6 @@ _dbus_full_duplex_pipe (int *fd1,
goto out0; goto out0;
} }
arg = 1;
if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR)
{
DBUS_SOCKET_SET_ERRNO ();
goto out0;
}
_DBUS_ZERO (saddr); _DBUS_ZERO (saddr);
saddr.sin_family = AF_INET; saddr.sin_family = AF_INET;
saddr.sin_port = 0; saddr.sin_port = 0;
@ -811,34 +847,12 @@ _dbus_full_duplex_pipe (int *fd1,
goto out0; goto out0;
} }
arg = 1; if (connect (socket1, (struct sockaddr *)&saddr, len) == SOCKET_ERROR)
if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
{ {
DBUS_SOCKET_SET_ERRNO (); DBUS_SOCKET_SET_ERRNO ();
goto out1; goto out1;
} }
if (connect (socket1, (struct sockaddr *)&saddr, len) != SOCKET_ERROR ||
WSAGetLastError () != WSAEWOULDBLOCK)
{
DBUS_SOCKET_SET_ERRNO ();
goto out1;
}
FD_ZERO (&read_set);
FD_SET (temp, &read_set);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR)
{
DBUS_SOCKET_SET_ERRNO ();
goto out1;
}
_dbus_assert (FD_ISSET (temp, &read_set));
socket2 = accept (temp, (struct sockaddr *) &saddr, &len); socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
if (socket2 == INVALID_SOCKET) if (socket2 == INVALID_SOCKET)
{ {
@ -846,38 +860,15 @@ _dbus_full_duplex_pipe (int *fd1,
goto out1; goto out1;
} }
FD_ZERO (&write_set); if (!blocking)
FD_SET (socket1, &write_set);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR)
{ {
DBUS_SOCKET_SET_ERRNO (); arg = 1;
goto out2;
}
_dbus_assert (FD_ISSET (socket1, &write_set));
if (blocking)
{
arg = 0;
if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR) if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
{ {
DBUS_SOCKET_SET_ERRNO (); DBUS_SOCKET_SET_ERRNO ();
goto out2; goto out2;
} }
arg = 0;
if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
{
DBUS_SOCKET_SET_ERRNO ();
goto out2;
}
}
else
{
arg = 1; arg = 1;
if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR) if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
{ {
@ -905,7 +896,7 @@ out0:
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_errno (errno),
"Could not setup socket pair: %s", "Could not setup socket pair: %s",
_dbus_strerror (errno)); _dbus_strerror_from_errno ());
return FALSE; return FALSE;
} }
@ -999,7 +990,7 @@ _dbus_poll (DBusPollFD *fds,
{ {
DBUS_SOCKET_SET_ERRNO (); DBUS_SOCKET_SET_ERRNO ();
if (errno != WSAEWOULDBLOCK) if (errno != WSAEWOULDBLOCK)
_dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", strerror (errno)); _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", _dbus_strerror_from_errno ());
ret = -1; ret = -1;
} }
else if (ready == WSA_WAIT_TIMEOUT) else if (ready == WSA_WAIT_TIMEOUT)
@ -1137,7 +1128,7 @@ _dbus_poll (DBusPollFD *fds,
{ {
DBUS_SOCKET_SET_ERRNO (); DBUS_SOCKET_SET_ERRNO ();
if (errno != WSAEWOULDBLOCK) if (errno != WSAEWOULDBLOCK)
_dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno)); _dbus_verbose ("select: failed: %s\n", _dbus_strerror_from_errno ());
} }
else if (ready == 0) else if (ready == 0)
_dbus_verbose ("select: = 0\n"); _dbus_verbose ("select: = 0\n");
@ -1273,7 +1264,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host,
dbus_set_error (error, dbus_set_error (error,
_dbus_error_from_errno (errno), _dbus_error_from_errno (errno),
"Failed to create socket: %s", "Failed to create socket: %s",
_dbus_strerror (errno)); _dbus_strerror_from_errno ());
return -1; return -1;
} }
@ -1322,7 +1313,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host,
dbus_set_error (error, dbus_set_error (error,
_dbus_error_from_errno (errno), _dbus_error_from_errno (errno),
"Failed to open socket: %s", "Failed to open socket: %s",
_dbus_strerror (errno)); _dbus_strerror_from_errno ());
return -1; return -1;
} }
_DBUS_ASSERT_ERROR_IS_CLEAR(error); _DBUS_ASSERT_ERROR_IS_CLEAR(error);
@ -1465,7 +1456,7 @@ _dbus_listen_tcp_socket (const char *host,
dbus_set_error (error, dbus_set_error (error,
_dbus_error_from_errno (errno), _dbus_error_from_errno (errno),
"Failed to open socket: %s", "Failed to open socket: %s",
_dbus_strerror (errno)); _dbus_strerror_from_errno ());
goto failed; goto failed;
} }
_DBUS_ASSERT_ERROR_IS_CLEAR(error); _DBUS_ASSERT_ERROR_IS_CLEAR(error);
@ -1475,7 +1466,7 @@ _dbus_listen_tcp_socket (const char *host,
closesocket (fd); closesocket (fd);
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_errno (errno),
"Failed to bind socket \"%s:%s\": %s", "Failed to bind socket \"%s:%s\": %s",
host ? host : "*", port, _dbus_strerror (errno)); host ? host : "*", port, _dbus_strerror_from_errno ());
goto failed; goto failed;
} }
@ -1484,7 +1475,7 @@ _dbus_listen_tcp_socket (const char *host,
closesocket (fd); closesocket (fd);
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_errno (errno),
"Failed to listen on socket \"%s:%s\": %s", "Failed to listen on socket \"%s:%s\": %s",
host ? host : "*", port, _dbus_strerror (errno)); host ? host : "*", port, _dbus_strerror_from_errno ());
goto failed; goto failed;
} }
@ -1494,7 +1485,7 @@ _dbus_listen_tcp_socket (const char *host,
closesocket (fd); closesocket (fd);
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_errno (errno),
"Failed to allocate file handle array: %s", "Failed to allocate file handle array: %s",
_dbus_strerror (errno)); _dbus_strerror_from_errno ());
goto failed; goto failed;
} }
listen_fd = newlisten_fd; listen_fd = newlisten_fd;
@ -1549,10 +1540,10 @@ _dbus_listen_tcp_socket (const char *host,
if (!nlisten_fd) if (!nlisten_fd)
{ {
errno = WSAEADDRINUSE; _dbus_win_set_errno (WSAEADDRINUSE);
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_errno (errno),
"Failed to bind socket \"%s:%s\": %s", "Failed to bind socket \"%s:%s\": %s",
host ? host : "*", port, _dbus_strerror (errno)); host ? host : "*", port, _dbus_strerror_from_errno ());
return -1; return -1;
} }
@ -1651,7 +1642,7 @@ again:
{ {
dbus_set_error (error, _dbus_error_from_errno (errno), dbus_set_error (error, _dbus_error_from_errno (errno),
"Failed to write credentials byte: %s", "Failed to write credentials byte: %s",
_dbus_strerror (errno)); _dbus_strerror_from_errno ());
return FALSE; return FALSE;
} }
else if (bytes_written == 0) else if (bytes_written == 0)
@ -1942,14 +1933,14 @@ _dbus_create_directory (const DBusString *filename,
filename_c = _dbus_string_get_const_data (filename); filename_c = _dbus_string_get_const_data (filename);
if (!CreateDirectory (filename_c, NULL)) if (!CreateDirectoryA (filename_c, NULL))
{ {
if (GetLastError () == ERROR_ALREADY_EXISTS) if (GetLastError () == ERROR_ALREADY_EXISTS)
return TRUE; return TRUE;
dbus_set_error (error, DBUS_ERROR_FAILED, dbus_set_error (error, DBUS_ERROR_FAILED,
"Failed to create directory %s: %s\n", "Failed to create directory %s: %s\n",
filename_c, strerror (errno)); filename_c, _dbus_strerror_from_errno ());
return FALSE; return FALSE;
} }
else else
@ -2010,7 +2001,7 @@ _dbus_get_tmpdir(void)
{ {
char *last_slash; char *last_slash;
if (!GetTempPath (sizeof (buf), buf)) if (!GetTempPathA (sizeof (buf), buf))
{ {
_dbus_warn ("GetTempPath failed\n"); _dbus_warn ("GetTempPath failed\n");
_dbus_abort (); _dbus_abort ();
@ -2055,7 +2046,7 @@ _dbus_delete_file (const DBusString *filename,
{ {
dbus_set_error (error, DBUS_ERROR_FAILED, dbus_set_error (error, DBUS_ERROR_FAILED,
"Failed to delete file %s: %s\n", "Failed to delete file %s: %s\n",
filename_c, strerror (errno)); filename_c, _dbus_strerror_from_errno ());
return FALSE; return FALSE;
} }
else else
@ -2437,7 +2428,7 @@ HANDLE _dbus_global_lock (const char *mutexname)
HANDLE mutex; HANDLE mutex;
DWORD gotMutex; DWORD gotMutex;
mutex = CreateMutex( NULL, FALSE, mutexname ); mutex = CreateMutexA( NULL, FALSE, mutexname );
if( !mutex ) if( !mutex )
{ {
return FALSE; return FALSE;
@ -2491,7 +2482,7 @@ _dbus_daemon_publish_session_bus_address (const char* address)
_dbus_assert (address); _dbus_assert (address);
// before _dbus_global_lock to keep correct lock/release order // before _dbus_global_lock to keep correct lock/release order
hDBusDaemonMutex = CreateMutex( NULL, FALSE, cDBusDaemonMutex ); hDBusDaemonMutex = CreateMutexA( NULL, FALSE, cDBusDaemonMutex );
ret = WaitForSingleObject( hDBusDaemonMutex, 1000 ); ret = WaitForSingleObject( hDBusDaemonMutex, 1000 );
if ( ret != WAIT_OBJECT_0 ) { if ( ret != WAIT_OBJECT_0 ) {
_dbus_warn("Could not lock mutex %s (return code %d). daemon already running? Bus address not published.\n", cDBusDaemonMutex, ret ); _dbus_warn("Could not lock mutex %s (return code %d). daemon already running? Bus address not published.\n", cDBusDaemonMutex, ret );
@ -2502,7 +2493,7 @@ _dbus_daemon_publish_session_bus_address (const char* address)
lock = _dbus_global_lock( cUniqueDBusInitMutex ); lock = _dbus_global_lock( cUniqueDBusInitMutex );
// create shm // create shm
hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, hDBusSharedMem = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
0, strlen( address ) + 1, cDBusDaemonAddressInfo ); 0, strlen( address ) + 1, cDBusDaemonAddressInfo );
_dbus_assert( hDBusSharedMem ); _dbus_assert( hDBusSharedMem );
@ -2549,7 +2540,7 @@ _dbus_get_autolaunch_shm (DBusString *address)
// read shm // read shm
for(i=0;i<20;++i) { for(i=0;i<20;++i) {
// we know that dbus-daemon is available, so we wait until shm is available // we know that dbus-daemon is available, so we wait until shm is available
sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, cDBusDaemonAddressInfo ); sharedMem = OpenFileMappingA( FILE_MAP_READ, FALSE, cDBusDaemonAddressInfo );
if( sharedMem == 0 ) if( sharedMem == 0 )
Sleep( 100 ); Sleep( 100 );
if ( sharedMem != 0) if ( sharedMem != 0)
@ -2587,7 +2578,7 @@ _dbus_daemon_already_runs (DBusString *address)
lock = _dbus_global_lock( cUniqueDBusInitMutex ); lock = _dbus_global_lock( cUniqueDBusInitMutex );
// do checks // do checks
daemon = CreateMutex( NULL, FALSE, cDBusDaemonMutex ); daemon = CreateMutexA( NULL, FALSE, cDBusDaemonMutex );
if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT) if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
{ {
ReleaseMutex (daemon); ReleaseMutex (daemon);
@ -2840,7 +2831,7 @@ _dbus_get_install_root(char *prefix, int len)
DWORD pathLength; DWORD pathLength;
char *lastSlash; char *lastSlash;
SetLastError( 0 ); SetLastError( 0 );
pathLength = GetModuleFileName(_dbus_win_get_dll_hmodule(), prefix, len); pathLength = GetModuleFileNameA(_dbus_win_get_dll_hmodule(), prefix, len);
if ( pathLength == 0 || GetLastError() != 0 ) { if ( pathLength == 0 || GetLastError() != 0 ) {
*prefix = '\0'; *prefix = '\0';
return FALSE; return FALSE;
@ -3064,7 +3055,7 @@ _dbus_append_keyring_directory_for_credentials (DBusString *directory,
dbus_bool_t dbus_bool_t
_dbus_file_exists (const char *file) _dbus_file_exists (const char *file)
{ {
DWORD attributes = GetFileAttributes (file); DWORD attributes = GetFileAttributesA (file);
if (attributes != INVALID_FILE_ATTRIBUTES && GetLastError() != ERROR_PATH_NOT_FOUND) if (attributes != INVALID_FILE_ATTRIBUTES && GetLastError() != ERROR_PATH_NOT_FOUND)
return TRUE; return TRUE;
@ -3227,7 +3218,7 @@ _dbus_win_set_error_from_win_error (DBusError *error,
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_FROM_SYSTEM,
NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPTSTR) &msg, 0, NULL); (LPSTR) &msg, 0, NULL);
if (msg) if (msg)
{ {
char *msg_copy; char *msg_copy;
@ -3271,11 +3262,13 @@ _dbus_delete_directory (const DBusString *filename,
filename_c = _dbus_string_get_const_data (filename); filename_c = _dbus_string_get_const_data (filename);
if (_rmdir (filename_c) != 0) if (RemoveDirectoryA (filename_c) == 0)
{ {
dbus_set_error (error, DBUS_ERROR_FAILED, char *emsg = _dbus_win_error_string (GetLastError ());
"Failed to remove directory %s: %s\n", dbus_set_error (error, _dbus_win_error_from_last_error (),
filename_c, strerror (errno)); "Failed to remove directory %s: %s",
filename_c, emsg);
_dbus_win_free_error_string (emsg);
return FALSE; return FALSE;
} }

View file

@ -40,20 +40,14 @@ extern void *_dbus_win_get_dll_hmodule (void);
#include <lm.h> #include <lm.h>
#include <io.h> #include <io.h>
#include <share.h> #include <share.h>
#include <direct.h>
#define mkdir(path, mode) _mkdir (path)
#ifndef DBUS_WINCE
#ifndef S_ISREG
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#endif
#endif
#define DBUS_CONSOLE_DIR "/var/run/console/" #define DBUS_CONSOLE_DIR "/var/run/console/"
void _dbus_win_set_errno (int err);
const char* _dbus_win_error_from_last_error (void);
void _dbus_win_startup_winsock (void); void _dbus_win_startup_winsock (void);
void _dbus_win_warn_win_error (const char *message, void _dbus_win_warn_win_error (const char *message,
int code); int code);

View file

@ -518,7 +518,7 @@ _dbus_string_parse_int (const DBusString *str,
_dbus_string_get_length (str) - start); _dbus_string_get_length (str) - start);
end = NULL; end = NULL;
errno = 0; _dbus_set_errno_to_zero ();
v = strtol (p, &end, 0); v = strtol (p, &end, 0);
if (end == NULL || end == p || errno != 0) if (end == NULL || end == p || errno != 0)
return FALSE; return FALSE;
@ -557,7 +557,7 @@ _dbus_string_parse_uint (const DBusString *str,
_dbus_string_get_length (str) - start); _dbus_string_get_length (str) - start);
end = NULL; end = NULL;
errno = 0; _dbus_set_errno_to_zero ();
v = strtoul (p, &end, 0); v = strtoul (p, &end, 0);
if (end == NULL || end == p || errno != 0) if (end == NULL || end == p || errno != 0)
return FALSE; return FALSE;
@ -699,7 +699,7 @@ ascii_strtod (const char *nptr,
/* Set errno to zero, so that we can distinguish zero results /* Set errno to zero, so that we can distinguish zero results
and underflows */ and underflows */
errno = 0; _dbus_set_errno_to_zero ();
if (decimal_point_pos) if (decimal_point_pos)
{ {
@ -773,7 +773,7 @@ _dbus_string_parse_double (const DBusString *str,
return FALSE; return FALSE;
end = NULL; end = NULL;
errno = 0; _dbus_set_errno_to_zero ();
v = ascii_strtod (p, &end); v = ascii_strtod (p, &end);
if (end == NULL || end == p || errno != 0) if (end == NULL || end == p || errno != 0)
return FALSE; return FALSE;
@ -993,6 +993,17 @@ _dbus_error_from_errno (int error_number)
return DBUS_ERROR_FAILED; return DBUS_ERROR_FAILED;
} }
/**
* Converts the current system errno value into a #DBusError name.
*
* @returns an error name
*/
const char*
_dbus_error_from_system_errno (void)
{
return _dbus_error_from_errno (errno);
}
/** /**
* Assign 0 to the global errno variable * Assign 0 to the global errno variable
*/ */

View file

@ -343,6 +343,7 @@ dbus_bool_t _dbus_generate_random_ascii (DBusString *str,
int n_bytes); int n_bytes);
const char* _dbus_error_from_errno (int error_number); const char* _dbus_error_from_errno (int error_number);
const char* _dbus_error_from_system_errno (void);
void _dbus_set_errno_to_zero (void); void _dbus_set_errno_to_zero (void);
dbus_bool_t _dbus_get_is_errno_nonzero (void); dbus_bool_t _dbus_get_is_errno_nonzero (void);