Commit 6e214b5b authored by Marcus Brinkmann's avatar Marcus Brinkmann Committed by Ralf Habacker

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

parent 4708efee
This diff is collapsed.
......@@ -54,9 +54,6 @@ dbus_bool_t _dbus_create_file_exclusively (const DBusString *filename,
DBusError *error);
dbus_bool_t _dbus_delete_file (const DBusString *filename,
DBusError *error);
int _dbus_file_read (int fd,
DBusString *buffer,
int count);
/** @} */
......
......@@ -30,10 +30,6 @@
#include <stdio.h>
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
static dbus_bool_t
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.
*/
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;
int send_result;
......@@ -203,36 +199,33 @@ _dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error)
if (_dbus_string_get_length (noncefile) == 0)
return FALSE;
if ( !_dbus_string_init (&nonce) )
if (!_dbus_string_init (&nonce))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return FALSE;
}
read_result = _dbus_read_nonce (noncefile, &nonce, NULL);
}
read_result = _dbus_read_nonce (noncefile, &nonce, error);
if (!read_result)
{
dbus_set_error (error,
_dbus_error_from_errno (errno),
"Could not read nonce from file %s (%s)",
_dbus_string_get_const_data (noncefile), _dbus_strerror(errno));
_DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_string_free (&nonce);
return FALSE;
}
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
_dbus_string_free (&nonce);
if (send_result == -1)
{
dbus_set_error (error,
_dbus_error_from_errno (errno),
"Failed to send nonce (fd=%d): %s",
fd, _dbus_strerror(errno));
return FALSE;
}
{
dbus_set_error (error,
_dbus_error_from_system_errno (),
"Failed to send nonce (fd=%d): %s",
fd, _dbus_strerror_from_errno ());
return FALSE;
}
return TRUE;
}
......@@ -284,8 +277,10 @@ do_noncefile_create (DBusNonceFile *noncefile,
}
if (!_dbus_create_directory (&noncefile->dir, error))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
goto on_error;
}
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
}
else
......@@ -303,10 +298,12 @@ do_noncefile_create (DBusNonceFile *noncefile,
if (!generate_and_write_nonce (&noncefile->path, error))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
if (use_subdir)
_dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
goto on_error;
}
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
_dbus_string_free (&randomStr);
......
......@@ -28,8 +28,7 @@
#include "dbus-internals.h"
#include "dbus-pipe.h"
#include <io.h>
#include <errno.h>
#include <windows.h>
/**
* write data to a pipe.
......@@ -48,15 +47,17 @@ _dbus_pipe_write (DBusPipe *pipe,
int len,
DBusError *error)
{
int written;
DWORD written;
BOOL res;
const char *buffer_c = _dbus_string_get_const_data (buffer);
written = _write (pipe->fd_or_handle, buffer_c + start, len);
if (written < 0)
res = WriteFile ((HANDLE) pipe->fd_or_handle, buffer_c + start, len, &written, NULL);
if (res == 0 || written < 0)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Writing to pipe: %s\n",
strerror (errno));
_dbus_strerror_from_errno ());
}
return written;
}
......@@ -74,10 +75,11 @@ _dbus_pipe_close (DBusPipe *pipe,
{
_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),
"Could not close pipe %d: %s", pipe->fd_or_handle, strerror (errno));
dbus_set_error (error, _dbus_error_from_system_errno (),
"Could not close pipe %d: %s", pipe->fd_or_handle,
_dbus_strerror_from_errno ());
return -1;
}
else
......
......@@ -37,7 +37,9 @@
#undef interface
#if HAVE_ERRNO_H
#include <errno.h>
#endif
/* Make use of the fact that the WSAE* error codes don't
* 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_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)
......
......@@ -315,9 +315,11 @@ _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
PING();
if (sitter->have_spawn_errno)
{
char *emsg = _dbus_win_error_string (sitter->spawn_errno);
dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
"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)
{
......@@ -467,7 +469,64 @@ protect_argv (char **argv,
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)
{
DBusBabysitter *sitter = (DBusBabysitter *) parameter;
......@@ -484,22 +543,17 @@ babysitter (void *parameter)
_dbus_verbose ("babysitter: spawning %s\n", sitter->executable);
PING();
if (sitter->envp != NULL)
sitter->child_handle = (HANDLE) spawnve (P_NOWAIT, sitter->executable,
(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);
sitter->child_handle = spawn_program (sitter->executable,
sitter->argv, sitter->envp);
PING();
if (sitter->child_handle == (HANDLE) -1)
{
sitter->child_handle = NULL;
sitter->have_spawn_errno = TRUE;
sitter->spawn_errno = errno;
sitter->spawn_errno = GetLastError();
}
PING();
SetEvent (sitter->start_sync_event);
......@@ -543,8 +597,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
{
DBusBabysitter *sitter;
HANDLE sitter_thread;
int sitter_thread_id;
DWORD sitter_thread_id;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
*sitter_p = NULL;
......@@ -599,7 +653,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
sitter->envp = envp;
PING();
sitter_thread = (HANDLE) _beginthreadex (NULL, 0, babysitter,
sitter_thread = (HANDLE) CreateThread (NULL, 0, babysitter,
sitter, 0, &sitter_thread_id);
if (sitter_thread == 0)
......
This diff is collapsed.
This diff is collapsed.
......@@ -40,20 +40,14 @@ extern void *_dbus_win_get_dll_hmodule (void);
#include <lm.h>
#include <io.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/"
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_warn_win_error (const char *message,
int code);
......
......@@ -518,7 +518,7 @@ _dbus_string_parse_int (const DBusString *str,
_dbus_string_get_length (str) - start);
end = NULL;
errno = 0;
_dbus_set_errno_to_zero ();
v = strtol (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
......@@ -557,7 +557,7 @@ _dbus_string_parse_uint (const DBusString *str,
_dbus_string_get_length (str) - start);
end = NULL;
errno = 0;
_dbus_set_errno_to_zero ();
v = strtoul (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
......@@ -699,7 +699,7 @@ ascii_strtod (const char *nptr,
/* Set errno to zero, so that we can distinguish zero results
and underflows */
errno = 0;
_dbus_set_errno_to_zero ();
if (decimal_point_pos)
{
......@@ -773,7 +773,7 @@ _dbus_string_parse_double (const DBusString *str,
return FALSE;
end = NULL;
errno = 0;
_dbus_set_errno_to_zero ();
v = ascii_strtod (p, &end);
if (end == NULL || end == p || errno != 0)
return FALSE;
......@@ -993,6 +993,17 @@ _dbus_error_from_errno (int error_number)
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
*/
......
......@@ -343,6 +343,7 @@ dbus_bool_t _dbus_generate_random_ascii (DBusString *str,
int n_bytes);
const char* _dbus_error_from_errno (int error_number);
const char* _dbus_error_from_system_errno (void);
void _dbus_set_errno_to_zero (void);
dbus_bool_t _dbus_get_is_errno_nonzero (void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment