From 04b6df21cd281e8cd540139f8d9ae85defc1961c Mon Sep 17 00:00:00 2001 From: ng0 Date: Tue, 10 Sep 2019 16:59:32 +0000 Subject: remove CYGWIN codeblocks, drop vendored Windows openvpn, drop win32 specific files. configures and builds okay. testsuite wasn't checked, will be checked. diff including the plibc removal is now around 14370 lines of code less. --- src/util/Makefile.am | 43 +- src/util/disk.c | 88 +- src/util/dnsstub.c | 6 +- src/util/gnunet-timeout-w32.c | 193 --- src/util/network.c | 866 +------------- src/util/os_installation.c | 78 +- src/util/os_network.c | 29 +- src/util/os_priority.c | 615 +--------- src/util/scheduler.c | 46 - src/util/service.c | 110 +- src/util/strings.c | 35 - src/util/test_common_logging_runtime_loglevels.c | 4 - src/util/test_configuration.c | 6 +- src/util/test_getopt.c | 4 +- src/util/test_resolver_api.c | 5 +- src/util/test_scheduler.c | 4 - src/util/test_service.c | 3 +- src/util/test_strings.c | 6 +- src/util/w32cat.c | 154 --- src/util/win.c | 1365 ---------------------- src/util/winproc.c | 340 ------ 21 files changed, 24 insertions(+), 3976 deletions(-) delete mode 100644 src/util/gnunet-timeout-w32.c delete mode 100644 src/util/w32cat.c delete mode 100644 src/util/win.c delete mode 100644 src/util/winproc.c (limited to 'src/util') diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 647f09224..c240053ff 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -13,27 +13,7 @@ dist_pkgcfg_DATA = \ pkgcfg_DATA = \ resolver.conf -if MINGW -noinst_LTLIBRARIES = \ - libgnunetutilwin.la -libgnunetutilwin_la_SOURCES = \ - win.c \ - winproc.c -libgnunetutilwin_la_LDFLAGS = \ - -no-undefined -Wl,--export-all-symbols -libgnunetutilwin_la_LIBADD = \ - -lshell32 -liconv -lstdc++ \ - -lcomdlg32 -lgdi32 -liphlpapi -WINLIB = libgnunetutilwin.la -W32CAT = w32cat -W32CONSOLEHELPER = gnunet-helper-w32-console -endif - -if !MINGW - TEST_CLIENT_UNIX_NC = test_client_unix.nc -else - TEST_CLIENT_UNIX_NC = -endif +TEST_CLIENT_UNIX_NC = test_client_unix.nc if USE_COVERAGE AM_CFLAGS = --coverage -O0 @@ -45,14 +25,6 @@ if ENABLE_BENCHMARK PTHREAD = -lpthread endif -w32cat_SOURCES = w32cat.c - -gnunet_helper_w32_console_SOURCES = \ - gnunet-helper-w32-console.c \ - gnunet-helper-w32-console.h -gnunet_helper_w32_console_LDADD = \ - libgnunetutil.la - gnunet_config_diff_SOURCES = \ gnunet-config-diff.c gnunet_config_diff_LDADD = \ @@ -202,8 +174,7 @@ lib_LTLIBRARIES = libgnunetutil.la libexec_PROGRAMS = \ gnunet-service-resolver \ - gnunet-timeout \ - $(W32CONSOLEHELPER) + gnunet-timeout bin_PROGRAMS = \ gnunet-resolver \ @@ -217,7 +188,6 @@ endif noinst_PROGRAMS = \ gnunet-config-diff \ - $(W32CAT) \ test_common_logging_dummy @@ -228,14 +198,8 @@ endif endif - -if !MINGW gnunet_timeout_SOURCES = \ gnunet-timeout.c -else -gnunet_timeout_SOURCES = \ - gnunet-timeout-w32.c -endif gnunet_service_resolver_SOURCES = \ gnunet-service-resolver.c @@ -393,8 +357,7 @@ test_os_start_process_SOURCES = \ test_os_start_process_LDADD = \ libgnunetutil.la test_os_start_process_DEPENDENCIES = \ - libgnunetutil.la \ - $(WINCAT) + libgnunetutil.la test_client_nc_SOURCES = \ test_client.c diff --git a/src/util/disk.c b/src/util/disk.c index 01d5efbe2..8b474542b 100644 --- a/src/util/disk.c +++ b/src/util/disk.c @@ -96,7 +96,6 @@ struct GetFileSizeData { }; -#ifndef MINGW /** * Translate GNUnet-internal permission bitmap to UNIX file * access permission bitmap. @@ -131,7 +130,6 @@ translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm) return mode; } -#endif /** @@ -191,11 +189,7 @@ getSizeRec(void *cls, const char *fn) int GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h) { -#ifdef MINGW - return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO; -#else return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; -#endif } /** @@ -236,26 +230,9 @@ GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, return GNUNET_SYSERR; } -#ifdef MINGW - LARGE_INTEGER li; - LARGE_INTEGER new_pos; - BOOL b; - - static DWORD t[] = { FILE_BEGIN, FILE_CURRENT, FILE_END }; - li.QuadPart = offset; - - b = SetFilePointerEx(h->h, li, &new_pos, t[whence]); - if (b == 0) - { - SetErrnoFromWinError(GetLastError()); - return GNUNET_SYSERR; - } - return (off_t)new_pos.QuadPart; -#else static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END }; return lseek(h->fd, offset, t[whence]); -#endif } @@ -1296,7 +1273,7 @@ GNUNET_DISK_file_open(const char *fn, expfn = GNUNET_STRINGS_filename_expand(fn); if (NULL == expfn) return NULL; -#ifndef MINGW + mode = 0; if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ @@ -1342,69 +1319,6 @@ GNUNET_DISK_file_open(const char *fn, GNUNET_free(expfn); return NULL; } -#else - access = 0; - disp = OPEN_ALWAYS; - - if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) - access = FILE_READ_DATA | FILE_WRITE_DATA; - else if (flags & GNUNET_DISK_OPEN_READ) - access = FILE_READ_DATA; - else if (flags & GNUNET_DISK_OPEN_WRITE) - access = FILE_WRITE_DATA; - - if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) - { - disp = CREATE_NEW; - } - else if (flags & GNUNET_DISK_OPEN_CREATE) - { - (void)GNUNET_DISK_directory_create_for_file(expfn); - if (flags & GNUNET_DISK_OPEN_TRUNCATE) - disp = CREATE_ALWAYS; - else - disp = OPEN_ALWAYS; - } - else if (flags & GNUNET_DISK_OPEN_TRUNCATE) - { - disp = TRUNCATE_EXISTING; - } - else - { - disp = OPEN_EXISTING; - } - - if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(expfn, wexpfn)) - h = CreateFileW(wexpfn, - access, - FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - disp, - FILE_ATTRIBUTE_NORMAL, - NULL); - else - h = INVALID_HANDLE_VALUE; - if (h == INVALID_HANDLE_VALUE) - { - int err; - SetErrnoFromWinError(GetLastError()); - err = errno; - LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_INFO, "open", expfn); - GNUNET_free(expfn); - errno = err; - return NULL; - } - - if (flags & GNUNET_DISK_OPEN_APPEND) - if (SetFilePointer(h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER) - { - SetErrnoFromWinError(GetLastError()); - LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn); - CloseHandle(h); - GNUNET_free(expfn); - return NULL; - } -#endif ret = GNUNET_new(struct GNUNET_DISK_FileHandle); diff --git a/src/util/dnsstub.c b/src/util/dnsstub.c index ab7a08318..fef4b5f5d 100644 --- a/src/util/dnsstub.c +++ b/src/util/dnsstub.c @@ -291,16 +291,12 @@ do_dns_read(struct GNUNET_DNSSTUB_RequestSocket *rs, ssize_t r; int len; -#ifndef MINGW if (0 != ioctl(GNUNET_NETWORK_get_fd(dnsout), FIONREAD, &len)) { /* conservative choice: */ len = UINT16_MAX; } -#else - /* port the code above? */ - len = UINT16_MAX; -#endif + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Receiving %d byte DNS reply\n", len); { unsigned char buf[len] GNUNET_ALIGN; diff --git a/src/util/gnunet-timeout-w32.c b/src/util/gnunet-timeout-w32.c deleted file mode 100644 index 3e579714c..000000000 --- a/src/util/gnunet-timeout-w32.c +++ /dev/null @@ -1,193 +0,0 @@ -/* - This file is part of GNUnet - Copyright (C) 2010 GNUnet e.V. - - GNUnet is free software: you can redistribute it and/or modify it - under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file src/util/gnunet-timeout-w32.c - * @brief small tool starting a child process, waiting that it terminates or killing it after a given timeout period - * @author LRN - */ - -#include -#include -#include - -int -main(int argc, char *argv[]) -{ - int i; - DWORD wait_result; - wchar_t *commandline; - wchar_t **wargv; - wchar_t *arg; - unsigned int cmdlen; - STARTUPINFOW start; - PROCESS_INFORMATION proc; - - wchar_t wpath[MAX_PATH + 1]; - - wchar_t *pathbuf; - DWORD pathbuf_len, alloc_len; - wchar_t *ptr; - wchar_t *non_const_filename; - wchar_t *wcmd; - int wargc; - int timeout = 0; - ssize_t wrote; - - HANDLE job; - - if (argc < 3) - { - printf - ("arg 1: timeout in sec., arg 2: executable, arg arguments\n"); - exit(1); - } - - timeout = atoi(argv[1]); - - if (timeout == 0) - timeout = 600; - - commandline = GetCommandLineW(); - if (commandline == NULL) - { - printf("Failed to get commandline: %lu\n", GetLastError()); - exit(2); - } - - wargv = CommandLineToArgvW(commandline, &wargc); - if (wargv == NULL || wargc <= 1) - { - printf("Failed to get parse commandline: %lu\n", GetLastError()); - exit(3); - } - - job = CreateJobObject(NULL, NULL); - if (job == NULL) - { - printf("Failed to create a job: %lu\n", GetLastError()); - exit(4); - } - - pathbuf_len = GetEnvironmentVariableW(L"PATH", (wchar_t *)&pathbuf, 0); - - alloc_len = pathbuf_len + 1; - - pathbuf = malloc(alloc_len * sizeof(wchar_t)); - - ptr = pathbuf; - - alloc_len = GetEnvironmentVariableW(L"PATH", ptr, pathbuf_len); - - cmdlen = wcslen(wargv[2]); - if (cmdlen < 5 || wcscmp(&wargv[2][cmdlen - 4], L".exe") != 0) - { - non_const_filename = malloc(sizeof(wchar_t) * (cmdlen + 5)); - swprintf(non_const_filename, cmdlen + 5, L"%S.exe", wargv[2]); - } - else - { - non_const_filename = wcsdup(wargv[2]); - } - - /* Check that this is the full path. If it isn't, search. */ - if (non_const_filename[1] == L':') - swprintf(wpath, sizeof(wpath) / sizeof(wchar_t), L"%S", non_const_filename); - else if (!SearchPathW - (pathbuf, non_const_filename, NULL, sizeof(wpath) / sizeof(wchar_t), - wpath, NULL)) - { - printf("Failed to get find executable: %lu\n", GetLastError()); - exit(5); - } - free(pathbuf); - free(non_const_filename); - - cmdlen = wcslen(wpath) + 4; - i = 3; - while (NULL != (arg = wargv[i++])) - cmdlen += wcslen(arg) + 4; - - wcmd = malloc(sizeof(wchar_t) * (cmdlen + 1)); - wrote = 0; - i = 2; - while (NULL != (arg = wargv[i++])) - { - /* This is to escape trailing slash */ - wchar_t arg_lastchar = arg[wcslen(arg) - 1]; - if (wrote == 0) - { - wrote += swprintf(&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\" ", wpath, - arg_lastchar == L'\\' ? L"\\" : L""); - } - else - { - if (wcschr(arg, L' ') != NULL) - wrote += swprintf(&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\"%S", arg, - arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" "); - else - wrote += swprintf(&wcmd[wrote], cmdlen + 1 - wrote, L"%S%S%S", arg, - arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" "); - } - } - - LocalFree(wargv); - - memset(&start, 0, sizeof(start)); - start.cb = sizeof(start); - - if (!CreateProcessW(wpath, wcmd, NULL, NULL, TRUE, CREATE_SUSPENDED, - NULL, NULL, &start, &proc)) - { - wprintf(L"Failed to get spawn process `%S' with arguments `%S': %lu\n", wpath, wcmd, GetLastError()); - exit(6); - } - - AssignProcessToJobObject(job, proc.hProcess); - - ResumeThread(proc.hThread); - CloseHandle(proc.hThread); - - free(wcmd); - - wait_result = WaitForSingleObject(proc.hProcess, timeout * 1000); - if (wait_result == WAIT_OBJECT_0) - { - DWORD status; - wait_result = GetExitCodeProcess(proc.hProcess, &status); - CloseHandle(proc.hProcess); - if (wait_result != 0) - { - printf("Test process exited with result %lu\n", status); - TerminateJobObject(job, status); - exit(status); - } - printf("Test process exited (failed to obtain exit status)\n"); - TerminateJobObject(job, 0); - exit(0); - } - printf("Child processes were killed after timeout of %u seconds\n", - timeout); - TerminateJobObject(job, 1); - CloseHandle(proc.hProcess); - exit(1); -} - -/* end of timeout_watchdog_w32.c */ diff --git a/src/util/network.c b/src/util/network.c index ec76424eb..6f8a8ff5a 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -759,10 +759,6 @@ GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc, flags, src_addr, addrlen); -#ifdef MINGW - if (SOCKET_ERROR == ret) - SetErrnoFromWinsockError(WSAGetLastError()); -#endif return ret; } @@ -792,10 +788,6 @@ GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, buffer, length, flags); -#ifdef MINGW - if (SOCKET_ERROR == ret) - SetErrnoFromWinsockError(WSAGetLastError()); -#endif return ret; } @@ -827,10 +819,6 @@ GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, buffer, length, flags); -#ifdef MINGW - if (SOCKET_ERROR == ret) - SetErrnoFromWinsockError(WSAGetLastError()); -#endif return ret; } @@ -865,10 +853,6 @@ GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc, flags |= MSG_NOSIGNAL; #endif ret = sendto(desc->fd, message, length, flags, dest_addr, dest_len); -#ifdef MINGW - if (SOCKET_ERROR == ret) - SetErrnoFromWinsockError(WSAGetLastError()); -#endif return ret; } @@ -897,10 +881,7 @@ GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, option_name, option_value, option_len); -#ifdef MINGW - if (SOCKET_ERROR == ret) - SetErrnoFromWinsockError(WSAGetLastError()); -#endif + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } @@ -950,10 +931,7 @@ GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, int ret; ret = shutdown(desc->fd, how); -#ifdef MINGW - if (0 != ret) - SetErrnoFromWinsockError(WSAGetLastError()); -#endif + return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR; } @@ -1054,7 +1032,6 @@ void GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst, const struct GNUNET_NETWORK_FDSet *src) { -#ifndef MINGW int nfds; for (nfds = src->nsds; nfds >= 0; nfds--) @@ -1062,31 +1039,6 @@ GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst, FD_SET(nfds, &dst->sds); dst->nsds = GNUNET_MAX(dst->nsds, src->nsds); -#else - /* This is MinGW32-specific implementation that relies on the code that - * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking - * that fd being added is not already in the set. - * Also relies on us knowing what's inside fd_set (fd_count and fd_array). - * - * NOTE: I don't understand why the UNIX-logic wouldn't work - * for the first part here as well. -CG - */ - unsigned int i; - - for (i = 0; i < src->sds.fd_count; i++) - FD_SET(src->sds.fd_array[i], - &dst->sds); - dst->nsds = GNUNET_MAX(src->nsds, - dst->nsds); - - /* also copy over `struct GNUNET_DISK_FileHandle` array */ - if (dst->handles_pos + src->handles_pos > dst->handles_size) - GNUNET_array_grow(dst->handles, - dst->handles_size, - ((dst->handles_pos + src->handles_pos) << 1)); - for (i = 0; i < src->handles_pos; i++) - dst->handles[dst->handles_pos++] = src->handles[i]; -#endif } @@ -1103,16 +1055,6 @@ GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to, FD_COPY(&from->sds, &to->sds); to->nsds = from->nsds; -#ifdef MINGW - if (from->handles_pos > to->handles_size) - GNUNET_array_grow(to->handles, - to->handles_size, - from->handles_pos * 2); - GNUNET_memcpy(to->handles, - from->handles, - from->handles_pos * sizeof(struct GNUNET_NETWORK_Handle *)); - to->handles_pos = from->handles_pos; -#endif } @@ -1217,13 +1159,6 @@ void GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h) { -#ifdef MINGW - if (fds->handles_pos == fds->handles_size) - GNUNET_array_grow(fds->handles, - fds->handles_size, - fds->handles_size * 2 + 2); - fds->handles[fds->handles_pos++] = h; -#else int fd; GNUNET_assert(GNUNET_OK == @@ -1234,7 +1169,6 @@ GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, &fds->sds); fds->nsds = GNUNET_MAX(fd + 1, fds->nsds); -#endif } @@ -1247,22 +1181,7 @@ void GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h) { -#ifdef MINGW - if (fds->handles_pos == fds->handles_size) - GNUNET_array_grow(fds->handles, - fds->handles_size, - fds->handles_size * 2 + 2); - fds->handles[fds->handles_pos] = h; - if (fds->handles[0] != h) - { - const struct GNUNET_DISK_FileHandle *bak = fds->handles[0]; - fds->handles[0] = h; - fds->handles[fds->handles_pos] = bak; - } - fds->handles_pos++; -#else GNUNET_NETWORK_fdset_handle_set(fds, h); -#endif } @@ -1277,42 +1196,11 @@ int GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h) { -#ifdef MINGW - unsigned int i; - - for (i = 0; i < fds->handles_pos; i++) - if (fds->handles[i] == h) - return GNUNET_YES; - return GNUNET_NO; -#else return FD_ISSET(h->fd, &fds->sds); -#endif } -#ifdef MINGW -/** - * Numerically compare pointers to sort them. - * Used to test for overlap in the arrays. - * - * @param p1 a pointer - * @param p2 a pointer - * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2. - */ -static int -ptr_cmp(const void *p1, - const void *p2) -{ - if (p1 == p2) - return 0; - if ((intptr_t)p1 < (intptr_t)p2) - return -1; - return 1; -} -#endif - - /** * Checks if two fd sets overlap * @@ -1324,7 +1212,6 @@ int GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1, const struct GNUNET_NETWORK_FDSet *fds2) { -#ifndef MINGW int nfds; nfds = GNUNET_MIN(fds1->nsds, @@ -1339,53 +1226,6 @@ GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1, return GNUNET_YES; } return GNUNET_NO; -#else - unsigned int i; - unsigned int j; - - /* This code is somewhat hacky, we are not supposed to know what's - * inside of fd_set; also the O(n^2) is really bad... */ - for (i = 0; i < fds1->sds.fd_count; i++) - for (j = 0; j < fds2->sds.fd_count; j++) - if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j]) - return GNUNET_YES; - - /* take a short cut if possible */ - if ((0 == fds1->handles_pos) || - (0 == fds2->handles_pos)) - return GNUNET_NO; - - /* Sort file handles array to avoid quadratic complexity when - checking for overlap */ - qsort(fds1->handles, - fds1->handles_pos, - sizeof(void *), - &ptr_cmp); - qsort(fds2->handles, - fds2->handles_pos, - sizeof(void *), - &ptr_cmp); - i = 0; - j = 0; - while ((i < fds1->handles_pos) && - (j < fds2->handles_pos)) - { - switch (ptr_cmp(fds1->handles[i], - fds2->handles[j])) - { - case -1: - i++; - break; - - case 0: - return GNUNET_YES; - - case 1: - j++; - } - } - return GNUNET_NO; -#endif } @@ -1413,192 +1253,10 @@ GNUNET_NETWORK_fdset_create() void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds) { -#ifdef MINGW - GNUNET_array_grow(fds->handles, - fds->handles_size, - 0); -#endif GNUNET_free(fds); } -#if MINGW -/** - * FIXME. - */ -struct _select_params { - /** - * Read set. - */ - fd_set *r; - - /** - * Write set. - */ - fd_set *w; - - /** - * Except set. - */ - fd_set *e; - - /** - * Timeout for select(). - */ - struct timeval *tv; - - /** - * FIXME. - */ - HANDLE wakeup; - - /** - * FIXME. - */ - HANDLE standby; - - /** - * FIXME. - */ - _win_socket wakeup_socket; - - /** - * Set to return value from select. - */ - int status; -}; - - -/** - * FIXME. - */ -static DWORD WINAPI -_selector(LPVOID p) -{ - struct _select_params *sp = p; - - while (1) - { - WaitForSingleObject(sp->standby, - INFINITE); - ResetEvent(sp->standby); - sp->status = select(1, - sp->r, - sp->w, - sp->e, - sp->tv); - if (FD_ISSET(sp->wakeup_socket, - sp->r)) - { - FD_CLR(sp->wakeup_socket, - sp->r); - sp->status -= 1; - } - SetEvent(sp->wakeup); - } - return 0; -} - - -static HANDLE hEventPipeWrite; - -static HANDLE hEventReadReady; - -static struct _select_params sp; - -static HANDLE select_thread; - -static HANDLE select_finished_event; - -static HANDLE select_standby_event; - -static _win_socket select_wakeup_socket = -1; - -static _win_socket select_send_socket = -1; - -static struct timeval select_timeout; - - -/** - * On W32, we actually use a thread to help with the - * event loop due to W32-API limitations. This function - * initializes that thread. - */ -static void -initialize_select_thread() -{ - _win_socket select_listening_socket = -1; - struct sockaddr_in s_in; - int alen; - int res; - unsigned long p; - - select_standby_event = CreateEvent(NULL, TRUE, FALSE, NULL); - select_finished_event = CreateEvent(NULL, TRUE, FALSE, NULL); - - select_wakeup_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - - select_listening_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - - p = 1; - res = ioctlsocket(select_wakeup_socket, FIONBIO, &p); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Select thread initialization: ioctlsocket() returns %d\n", - res); - - alen = sizeof(s_in); - s_in.sin_family = AF_INET; - s_in.sin_port = 0; - s_in.sin_addr.S_un.S_un_b.s_b1 = 127; - s_in.sin_addr.S_un.S_un_b.s_b2 = 0; - s_in.sin_addr.S_un.S_un_b.s_b3 = 0; - s_in.sin_addr.S_un.S_un_b.s_b4 = 1; - res = bind(select_listening_socket, - (const struct sockaddr *)&s_in, - sizeof(s_in)); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Select thread initialization: bind() returns %d\n", - res); - - res = getsockname(select_listening_socket, - (struct sockaddr *)&s_in, - &alen); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Select thread initialization: getsockname() returns %d\n", - res); - - res = listen(select_listening_socket, - SOMAXCONN); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Select thread initialization: listen() returns %d\n", - res); - res = connect(select_wakeup_socket, - (const struct sockaddr *)&s_in, - sizeof(s_in)); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Select thread initialization: connect() returns %d\n", - res); - - select_send_socket = accept(select_listening_socket, - (struct sockaddr *)&s_in, - &alen); - - closesocket(select_listening_socket); - - sp.wakeup = select_finished_event; - sp.standby = select_standby_event; - sp.wakeup_socket = select_wakeup_socket; - - select_thread = CreateThread(NULL, - 0, - _selector, - &sp, - 0, NULL); -} - - -#endif - /** * Test if the given @a port is available. * @@ -1657,7 +1315,6 @@ GNUNET_NETWORK_test_port_free(int ipproto, } -#ifndef MINGW /** * Check if sockets or pipes meet certain conditions * @@ -1714,523 +1371,4 @@ GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds, GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv); } - -#else -/* MINGW */ - - -/** - * Non-blocking test if a pipe is ready for reading. - * - * @param fh pipe handle - * @return #GNUNET_YES if the pipe is ready for reading - */ -static int -pipe_read_ready(const struct GNUNET_DISK_FileHandle *fh) -{ - DWORD error; - BOOL bret; - DWORD waitstatus = 0; - - SetLastError(0); - bret = PeekNamedPipe(fh->h, NULL, 0, NULL, &waitstatus, NULL); - error = GetLastError(); - if (0 == bret) - { - /* TODO: either add more errors to this condition, or eliminate it - * entirely (failed to peek -> pipe is in serious trouble, should - * be selected as readable). - */ - if ((error != ERROR_BROKEN_PIPE) && - (error != ERROR_INVALID_HANDLE)) - return GNUNET_NO; - } - else if (waitstatus <= 0) - return GNUNET_NO; - return GNUNET_YES; -} - - -/** - * Non-blocking test if a pipe is having an IO exception. - * - * @param fh pipe handle - * @return #GNUNET_YES if the pipe is having an IO exception. - */ -static int -pipe_except_ready(const struct GNUNET_DISK_FileHandle *fh) -{ - DWORD dwBytes; - - if (PeekNamedPipe(fh->h, NULL, 0, NULL, &dwBytes, NULL)) - return GNUNET_NO; - return GNUNET_YES; -} - - -/** - * Iterate over handles in fds, destructively rewrite the - * handles array contents of fds so that it starts with the - * handles that are ready, and update handles_pos accordingly. - * - * @param fds set of handles (usually pipes) to be checked for readiness - * @param except GNUNET_NO if fds should be checked for readiness to read, - * GNUNET_YES if fds should be checked for exceptions - * (there is no way to check for write-readiness - pipes are always write-ready) - * @param set_for_sure a HANDLE that is known to be set already, - * because WaitForMultipleObjects() returned its index. - * @return number of ready handles - */ -static int -check_handles_status(struct GNUNET_NETWORK_FDSet *fds, - int except, - HANDLE set_for_sure) -{ - const struct GNUNET_DISK_FileHandle *fh; - unsigned int roff; - unsigned int woff; - - for (woff = 0, roff = 0; roff < fds->handles_pos; roff++) - { - fh = fds->handles[roff]; - if (fh == set_for_sure) - { - fds->handles[woff++] = fh; - } - else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE) - { - if ((except && pipe_except_ready(fh)) || - (!except && pipe_read_ready(fh))) - fds->handles[woff++] = fh; - } - else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE) - { - if (!except) - fds->handles[woff++] = fh; - } - else - { - if (WAIT_OBJECT_0 == WaitForSingleObject(fh->h, 0)) - fds->handles[woff++] = fh; - } - } - fds->handles_pos = woff; - return woff; -} - - -/** - * Check if sockets or pipes meet certain conditions, version for W32. - * - * @param rfds set of sockets or pipes to be checked for readability - * @param wfds set of sockets or pipes to be checked for writability - * @param efds set of sockets or pipes to be checked for exceptions - * @param timeout relative value when to return - * @return number of selected sockets or pipes, #GNUNET_SYSERR on error - */ -int -GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds, - struct GNUNET_NETWORK_FDSet *wfds, - struct GNUNET_NETWORK_FDSet *efds, - const struct GNUNET_TIME_Relative timeout) -{ - const struct GNUNET_DISK_FileHandle *fh; - int nfds; - int handles; - unsigned int i; - int retcode; - uint64_t mcs_total; - DWORD ms_rounded; - int nhandles = 0; - int read_pipes_off; - HANDLE handle_array[FD_SETSIZE + 2]; - int returncode; - int returnedpos = 0; - int selectret; - fd_set aread; - fd_set awrite; - fd_set aexcept; - - nfds = 0; - handles = 0; - if (NULL != rfds) - { - nfds = GNUNET_MAX(nfds, rfds->nsds); - handles += rfds->handles_pos; - } - if (NULL != wfds) - { - nfds = GNUNET_MAX(nfds, wfds->nsds); - handles += wfds->handles_pos; - } - if (NULL != efds) - { - nfds = GNUNET_MAX(nfds, efds->nsds); - handles += efds->handles_pos; - } - - if ((0 == nfds) && - (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) && - (0 == handles)) - { - GNUNET_break(0); - LOG(GNUNET_ERROR_TYPE_ERROR, - _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), - "select"); - } -#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) - /* calculate how long we need to wait in microseconds */ - if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) - { - mcs_total = INFINITE; - ms_rounded = INFINITE; - } - else - { - mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us; - ms_rounded = (DWORD)(mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us); - if (mcs_total > 0 && ms_rounded == 0) - ms_rounded = 1; - } - /* select() may be used as a portable way to sleep */ - if (!(rfds || wfds || efds)) - { - Sleep(ms_rounded); - return 0; - } - - if (NULL == select_thread) - initialize_select_thread(); - - FD_ZERO(&aread); - FD_ZERO(&awrite); - FD_ZERO(&aexcept); - if (rfds) - FD_COPY(&rfds->sds, &aread); - if (wfds) - FD_COPY(&wfds->sds, &awrite); - if (efds) - FD_COPY(&efds->sds, &aexcept); - - /* Start by doing a fast check on sockets and pipes (without - waiting). It is cheap, and is sufficient most of the time. By - profiling we detected that to be true in 90% of the cases. - */ - - /* Do the select now */ - select_timeout.tv_sec = 0; - select_timeout.tv_usec = 0; - - /* Copy all the writes to the except, so we can detect connect() errors */ - for (i = 0; i < awrite.fd_count; i++) - FD_SET(awrite.fd_array[i], - &aexcept); - if ((aread.fd_count > 0) || - (awrite.fd_count > 0) || - (aexcept.fd_count > 0)) - selectret = select(1, - (NULL != rfds) ? &aread : NULL, - (NULL != wfds) ? &awrite : NULL, - &aexcept, - &select_timeout); - else - selectret = 0; - if (-1 == selectret) - { - /* Throw an error early on, while we still have the context. */ - LOG(GNUNET_ERROR_TYPE_ERROR, - "W32 select(%d, %d, %d) failed: %lu\n", - rfds ? aread.fd_count : 0, - wfds ? awrite.fd_count : 0, - aexcept.fd_count, - GetLastError()); - GNUNET_assert(0); - } - - /* Check aexcept, if something is in there and we copied that - FD before to detect connect() errors, add it back to the - write set to report errors. */ - if (NULL != wfds) - for (i = 0; i < aexcept.fd_count; i++) - if (FD_ISSET(aexcept.fd_array[i], - &wfds->sds)) - FD_SET(aexcept.fd_array[i], - &awrite); - - - /* If our select returned something or is a 0-timed request, then - also check the pipes and get out of here! */ - /* Sadly, it means code duplication :( */ - if ((selectret > 0) || (0 == mcs_total)) - { - retcode = 0; - - /* Read Pipes */ - if (rfds && (rfds->handles_pos > 0)) - retcode += check_handles_status(rfds, GNUNET_NO, NULL); - - /* wfds handles remain untouched, on W32 - we pretend our pipes are "always" write-ready */ - - /* except pipes */ - if (efds && (efds->handles_pos > 0)) - retcode += check_handles_status(efds, GNUNET_YES, NULL); - - if (rfds) - { - GNUNET_NETWORK_fdset_zero(rfds); - if (selectret != -1) - GNUNET_NETWORK_fdset_copy_native(rfds, &aread, selectret); - } - if (wfds) - { - GNUNET_NETWORK_fdset_zero(wfds); - if (selectret != -1) - GNUNET_NETWORK_fdset_copy_native(wfds, &awrite, selectret); - } - if (efds) - { - GNUNET_NETWORK_fdset_zero(efds); - if (selectret != -1) - GNUNET_NETWORK_fdset_copy_native(efds, &aexcept, selectret); - } - if (-1 == selectret) - return -1; - /* Add our select() FDs to the total return value */ - retcode += selectret; - return retcode; - } - - /* If we got this far, use slower implementation that is able to do a waiting select - on both sockets and pipes simultaneously */ - - /* Events for pipes */ - if (!hEventReadReady) - hEventReadReady = CreateEvent(NULL, TRUE, TRUE, NULL); - if (!hEventPipeWrite) - hEventPipeWrite = CreateEvent(NULL, TRUE, TRUE, NULL); - retcode = 0; - - FD_ZERO(&aread); - FD_ZERO(&awrite); - FD_ZERO(&aexcept); - if (rfds) - FD_COPY(&rfds->sds, &aread); - if (wfds) - FD_COPY(&wfds->sds, &awrite); - if (efds) - FD_COPY(&efds->sds, &aexcept); - /* We will first Add the PIPES to the events */ - /* Track how far in `handle_array` the read pipes go, - so we may by-pass them quickly if none of them - are selected. */ - read_pipes_off = 0; - if (rfds && (rfds->handles_pos > 0)) - { - for (i = 0; i < rfds->handles_pos; i++) - { - fh = rfds->handles[i]; - if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT) - { - handle_array[nhandles++] = fh->h; - continue; - } - if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE) - continue; - /* Read zero bytes to check the status of the pipe */ - if (!ReadFile(fh->h, NULL, 0, NULL, fh->oOverlapRead)) - { - DWORD error_code = GetLastError(); - - if (error_code == ERROR_IO_PENDING) - { - /* add as unready */ - handle_array[nhandles++] = fh->oOverlapRead->hEvent; - read_pipes_off++; - } - else - { - /* add as ready */ - handle_array[nhandles++] = hEventReadReady; - read_pipes_off++; - } - } - else - { - /* error also counts as ready */ - handle_array[nhandles++] = hEventReadReady; - read_pipes_off++; - } - } - } - - if (wfds && (wfds->handles_pos > 0)) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Adding the write ready event to the array as %d\n", - nhandles); - handle_array[nhandles++] = hEventPipeWrite; - } - - sp.status = 0; - if (nfds > 0) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Adding the socket event to the array as %d\n", - nhandles); - handle_array[nhandles++] = select_finished_event; - if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) - { - sp.tv = NULL; - } - else - { - select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us; - select_timeout.tv_usec = (timeout.rel_value_us - - (select_timeout.tv_sec * - GNUNET_TIME_UNIT_SECONDS.rel_value_us)); - sp.tv = &select_timeout; - } - FD_SET(select_wakeup_socket, &aread); - do - { - i = recv(select_wakeup_socket, - (char *)&returnedpos, - 1, - 0); - } - while (i == 1); - sp.r = &aread; - sp.w = &awrite; - sp.e = &aexcept; - /* Failed connections cause sockets to be set in errorfds on W32, - * but on POSIX it should set them in writefds. - * First copy all awrite sockets to aexcept, later we'll - * check aexcept and set its contents in awrite as well - * Sockets are also set in errorfds when OOB data is available, - * but we don't use OOB data. - */ - for (i = 0; i < awrite.fd_count; i++) - FD_SET(awrite.fd_array[i], - &aexcept); - ResetEvent(select_finished_event); - SetEvent(select_standby_event); - } - - /* NULL-terminate array */ - handle_array[nhandles] = NULL; - LOG(GNUNET_ERROR_TYPE_DEBUG, - "nfds: %d, handles: %d, will wait: %llu mcs\n", - nfds, - nhandles, - mcs_total); - if (nhandles) - { - returncode - = WaitForMultipleObjects(nhandles, - handle_array, - FALSE, - ms_rounded); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "WaitForMultipleObjects Returned: %d\n", - returncode); - } - else if (nfds > 0) - { - GNUNET_break(0); /* This branch shouldn't actually be executed...*/ - i = (int)WaitForSingleObject(select_finished_event, - INFINITE); - returncode = WAIT_TIMEOUT; - } - else - { - /* Shouldn't come this far. If it does - investigate. */ - GNUNET_assert(0); - } - - if (nfds > 0) - { - /* Don't wake up select-thread when delay is 0, it should return immediately - * and wake up by itself. - */ - if (0 != mcs_total) - i = send(select_send_socket, - (const char *)&returnedpos, - 1, - 0); - i = (int)WaitForSingleObject(select_finished_event, - INFINITE); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Finished waiting for the select thread: %d %d\n", - i, - sp.status); - if (0 != mcs_total) - { - do - { - i = recv(select_wakeup_socket, - (char *)&returnedpos, - 1, 0); - } - while (1 == i); - } - /* Check aexcept, add its contents to awrite */ - for (i = 0; i < aexcept.fd_count; i++) - FD_SET(aexcept.fd_array[i], &awrite); - } - - returnedpos = returncode - WAIT_OBJECT_0; - LOG(GNUNET_ERROR_TYPE_DEBUG, - "return pos is: %d\n", - returnedpos); - - if (rfds) - { - /* We queued a zero-long read on each pipe to check - * its state, now we must cancel these read operations. - * This must be done while rfds->handles_pos is still - * intact and matches the number of read handles that we - * got from the caller. - */ - for (i = 0; i < rfds->handles_pos; i++) - { - fh = rfds->handles[i]; - if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type) - CancelIo(fh->h); - } - - /* We may have some pipes ready for reading. */ - if (returnedpos < read_pipes_off) - retcode += check_handles_status(rfds, GNUNET_NO, handle_array[returnedpos]); - else - rfds->handles_pos = 0; - - if (-1 != sp.status) - GNUNET_NETWORK_fdset_copy_native(rfds, &aread, retcode); - } - if (wfds) - { - retcode += wfds->handles_pos; - /* wfds handles remain untouched */ - if (-1 != sp.status) - GNUNET_NETWORK_fdset_copy_native(wfds, &awrite, retcode); - } - if (efds) - { - retcode += check_handles_status(rfds, - GNUNET_YES, - returnedpos < nhandles ? handle_array[returnedpos] : NULL); - if (-1 != sp.status) - GNUNET_NETWORK_fdset_copy_native(efds, &aexcept, retcode); - } - - if (sp.status > 0) - retcode += sp.status; - - return retcode; -} - -/* MINGW */ -#endif - /* end of network.c */ diff --git a/src/util/os_installation.c b/src/util/os_installation.c index 104a98da6..c8590c8fb 100644 --- a/src/util/os_installation.c +++ b/src/util/os_installation.c @@ -764,26 +764,6 @@ GNUNET_OS_check_helper_binary(const char *binary, char *p; char *pf; -#ifdef MINGW - char *binaryexe; - - GNUNET_asprintf(&binaryexe, "%s.exe", binary); - if ((GNUNET_YES == - GNUNET_STRINGS_path_is_absolute(binaryexe, GNUNET_NO, NULL, NULL)) || - (0 == strncmp(binary, "./", 2))) - p = GNUNET_strdup(binaryexe); - else - { - p = get_path_from_PATH(binaryexe); - if (NULL != p) - { - GNUNET_asprintf(&pf, "%s/%s", p, binaryexe); - GNUNET_free(p); - p = pf; - } - } - GNUNET_free(binaryexe); -#else if ((GNUNET_YES == GNUNET_STRINGS_path_is_absolute(binary, GNUNET_NO, NULL, NULL)) || (0 == strncmp(binary, "./", 2))) @@ -800,7 +780,7 @@ GNUNET_OS_check_helper_binary(const char *binary, p = pf; } } -#endif + if (NULL == p) { LOG(GNUNET_ERROR_TYPE_INFO, @@ -814,14 +794,14 @@ GNUNET_OS_check_helper_binary(const char *binary, GNUNET_free(p); return GNUNET_SYSERR; } -#ifndef MINGW + if (0 == getuid()) { /* as we run as root, we don't insist on SUID */ GNUNET_free(p); return GNUNET_YES; } -#endif + if (0 != stat(p, &statbuf)) { LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", p); @@ -830,7 +810,6 @@ GNUNET_OS_check_helper_binary(const char *binary, } if (check_suid) { -#ifndef MINGW (void)params; if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid)) { @@ -841,57 +820,6 @@ GNUNET_OS_check_helper_binary(const char *binary, _("Binary `%s' exists, but is not SUID\n"), p); /* binary exists, but not SUID */ -#else - STARTUPINFO start; - char parameters[512]; - PROCESS_INFORMATION proc; - DWORD exit_value; - - GNUNET_snprintf(parameters, sizeof(parameters), "-d %s", params); - memset(&start, 0, sizeof(start)); - start.cb = sizeof(start); - memset(&proc, 0, sizeof(proc)); - - - // Start the child process. - if (!(CreateProcess( - p, // current windows (2k3 and up can handle / instead of \ in paths)) - parameters, // execute dryrun/priviliege checking mode - NULL, // Process handle not inheritable - NULL, // Thread handle not inheritable - FALSE, // Set handle inheritance to FALSE - CREATE_DEFAULT_ERROR_MODE, // No creation flags - NULL, // Use parent's environment block - NULL, // Use parent's starting directory - &start, // Pointer to STARTUPINFO structure - &proc) // Pointer to PROCESS_INFORMATION structure - )) - { - LOG(GNUNET_ERROR_TYPE_ERROR, - _("CreateProcess failed for binary %s (%d).\n"), - p, - GetLastError()); - return GNUNET_SYSERR; - } - - // Wait until child process exits. - WaitForSingleObject(proc.hProcess, INFINITE); - - if (!GetExitCodeProcess(proc.hProcess, &exit_value)) - { - LOG(GNUNET_ERROR_TYPE_ERROR, - _("GetExitCodeProcess failed for binary %s (%d).\n"), - p, - GetLastError()); - return GNUNET_SYSERR; - } - // Close process and thread handles. - CloseHandle(proc.hProcess); - CloseHandle(proc.hThread); - - if (!exit_value) - return GNUNET_YES; -#endif } GNUNET_free(p); return GNUNET_NO; diff --git a/src/util/os_network.c b/src/util/os_network.c index 4df37b9e5..526678047 100644 --- a/src/util/os_network.c +++ b/src/util/os_network.c @@ -35,7 +35,7 @@ #define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-os-network", syscall, filename) -#if !(HAVE_GETIFADDRS && HAVE_FREEIFADDRS) && !MINGW +#if !(HAVE_GETIFADDRS && HAVE_FREEIFADDRS) /** * Try to enumerate all network interfaces using 'ifconfig'. * @@ -388,32 +388,7 @@ void GNUNET_OS_network_interfaces_list(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls) { -#ifdef MINGW - int r; - int i; - struct EnumNICs3_results *results = NULL; - int results_count; - - r = EnumNICs3(&results, &results_count); - if (r != GNUNET_OK) - return; - - for (i = 0; i < results_count; i++) - { - if (GNUNET_OK != - proc(proc_cls, results[i].pretty_name, results[i].is_default, - (const struct sockaddr *)&results[i].address, - results[i]. - flags & ENUMNICS3_BCAST_OK ? - (const struct sockaddr *)&results[i].broadcast : NULL, - results[i].flags & ENUMNICS3_MASK_OK ? - (const struct sockaddr *)&results[i].mask : NULL, - results[i].addr_size)) - break; - } - EnumNICs3_free(results); - return; -#elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS +#if HAVE_GETIFADDRS && HAVE_FREEIFADDRS struct ifaddrs *ifa_first; struct ifaddrs *ifa_ptr; socklen_t alen; diff --git a/src/util/os_priority.c b/src/util/os_priority.c index ebe469b3b..e25623af5 100644 --- a/src/util/os_priority.c +++ b/src/util/os_priority.c @@ -350,7 +350,6 @@ start_process(int pipe_control, const char *filename, char *const argv[]) { -#ifndef MINGW pid_t ret; char fds[16]; struct GNUNET_OS_Process *gnunet_proc; @@ -464,7 +463,7 @@ start_process(int pipe_control, GNUNET_array_append(lscp, ls, -1); } #if DARWIN - /* see https://gnunet.org/vfork */ + /* see https://web.archive.org/web/20150924082249/gnunet.org/vfork */ ret = vfork(); #else ret = fork(); @@ -589,547 +588,6 @@ start_process(int pipe_control, execvp(filename, argv); LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "execvp", filename); _exit(1); -#else - struct GNUNET_DISK_FileHandle *childpipe_read; - struct GNUNET_DISK_FileHandle *childpipe_write; - HANDLE childpipe_read_handle; - char **arg; - char **non_const_argv; - unsigned int cmdlen; - char *cmd; - char *idx; - STARTUPINFOW start; - PROCESS_INFORMATION proc; - int argcount = 0; - struct GNUNET_OS_Process *gnunet_proc; - char path[MAX_PATH + 1]; - char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; - char *env_block = NULL; - char *pathbuf; - DWORD pathbuf_len; - DWORD alloc_len; - char *self_prefix; - char *bindir; - char *libdir; - char *ptr; - char *non_const_filename; - char win_path[MAX_PATH + 1]; - struct GNUNET_DISK_PipeHandle *lsocks_pipe; - const struct GNUNET_DISK_FileHandle *lsocks_write_fd; - HANDLE lsocks_read; - HANDLE lsocks_write; - wchar_t *wpath; - wchar_t *wcmd; - size_t wpath_len; - size_t wcmd_len; - int env_off; - int fail; - long lRet; - HANDLE stdin_handle; - HANDLE stdout_handle; - HANDLE stdih, stdoh, stdeh; - DWORD stdif, stdof, stdef; - BOOL bresult; - DWORD error_code; - DWORD create_no_window; - - if (GNUNET_SYSERR == - GNUNET_OS_check_helper_binary(filename, GNUNET_NO, NULL)) - return NULL; /* not executable */ - - /* Search in prefix dir (hopefully - the directory from which - * the current module was loaded), bindir and libdir, then in PATH - */ - self_prefix = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_SELF_PREFIX); - bindir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR); - libdir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LIBDIR); - - pathbuf_len = GetEnvironmentVariableA("PATH", (char *)&pathbuf, 0); - - alloc_len = pathbuf_len + 1 + strlen(self_prefix) + 1 + strlen(bindir) + 1 + - strlen(libdir); - - pathbuf = GNUNET_malloc(alloc_len * sizeof(char)); - - ptr = pathbuf; - ptr += sprintf(pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir); - GNUNET_free(self_prefix); - GNUNET_free(bindir); - GNUNET_free(libdir); - - alloc_len = GetEnvironmentVariableA("PATH", ptr, pathbuf_len); - if (alloc_len != pathbuf_len - 1) - { - GNUNET_free(pathbuf); - errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */ - return NULL; - } - - cmdlen = strlen(filename); - if ((cmdlen < 5) || (0 != strcmp(&filename[cmdlen - 4], ".exe"))) - GNUNET_asprintf(&non_const_filename, "%s.exe", filename); - else - GNUNET_asprintf(&non_const_filename, "%s", filename); - - /* It could be in POSIX form, convert it to a DOS path early on */ - if (ERROR_SUCCESS != - (lRet = plibc_conv_to_win_path(non_const_filename, win_path))) - { - SetErrnoFromWinError(lRet); - LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, - "plibc_conv_to_win_path", - non_const_filename); - GNUNET_free(non_const_filename); - GNUNET_free(pathbuf); - return NULL; - } - GNUNET_free(non_const_filename); - non_const_filename = GNUNET_strdup(win_path); - /* Check that this is the full path. If it isn't, search. */ - /* FIXME: convert it to wchar_t and use SearchPathW? - * Remember: arguments to _start_process() are technically in UTF-8... - */ - if (non_const_filename[1] == ':') - { - snprintf(path, sizeof(path) / sizeof(char), "%s", non_const_filename); - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Using path `%s' as-is. PATH is %s\n", - path, - ptr); - } - else if (!SearchPathA(pathbuf, - non_const_filename, - NULL, - sizeof(path) / sizeof(char), - path, - NULL)) - { - SetErrnoFromWinError(GetLastError()); - LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, - "SearchPath", - non_const_filename); - GNUNET_free(non_const_filename); - GNUNET_free(pathbuf); - return NULL; - } - else - LOG(GNUNET_ERROR_TYPE_DEBUG, "Found `%s' in PATH `%s'\n", path, pathbuf); - GNUNET_free(pathbuf); - GNUNET_free(non_const_filename); - - /* Count the number of arguments */ - arg = (char **)argv; - while (*arg) - { - arg++; - argcount++; - } - - /* Allocate a copy argv */ - non_const_argv = GNUNET_malloc(sizeof(char *) * (argcount + 1)); - - /* Copy all argv strings */ - argcount = 0; - arg = (char **)argv; - while (*arg) - { - if (arg == argv) - non_const_argv[argcount] = GNUNET_strdup(path); - else - non_const_argv[argcount] = GNUNET_strdup(*arg); - arg++; - argcount++; - } - non_const_argv[argcount] = NULL; - - /* Count cmd len */ - cmdlen = 1; - arg = non_const_argv; - while (*arg) - { - cmdlen = cmdlen + strlen(*arg) + 4; - arg++; - } - - /* Allocate and create cmd */ - cmd = idx = GNUNET_malloc(sizeof(char) * cmdlen); - arg = non_const_argv; - while (*arg) - { - char arg_last_char = (*arg)[strlen(*arg) - 1]; - idx += sprintf(idx, - "\"%s%s\"%s", - *arg, - arg_last_char == '\\' ? "\\" : "", - *(arg + 1) ? " " : ""); - arg++; - } - - while (argcount > 0) - GNUNET_free(non_const_argv[--argcount]); - GNUNET_free(non_const_argv); - - memset(&start, 0, sizeof(start)); - start.cb = sizeof(start); - if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0)) - start.dwFlags |= STARTF_USESTDHANDLES; - - stdih = GetStdHandle(STD_INPUT_HANDLE); - GetHandleInformation(stdih, &stdif); - if (pipe_stdin != NULL) - { - GNUNET_DISK_internal_file_handle_( - GNUNET_DISK_pipe_handle(pipe_stdin, GNUNET_DISK_PIPE_END_READ), - &stdin_handle, - sizeof(HANDLE)); - start.hStdInput = stdin_handle; - } - else if (stdih) - { - if (std_inheritance & GNUNET_OS_INHERIT_STD_IN) - { - SetHandleInformation(stdih, HANDLE_FLAG_INHERIT, 1); - if (pipe_stdin == NULL) - start.hStdInput = stdih; - } - else - SetHandleInformation(stdih, HANDLE_FLAG_INHERIT, 0); - } - - - stdoh = GetStdHandle(STD_OUTPUT_HANDLE); - GetHandleInformation(stdoh, &stdof); - if (NULL != pipe_stdout) - { - GNUNET_DISK_internal_file_handle_( - GNUNET_DISK_pipe_handle(pipe_stdout, GNUNET_DISK_PIPE_END_WRITE), - &stdout_handle, - sizeof(HANDLE)); - start.hStdOutput = stdout_handle; - } - else if (stdoh) - { - if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT) - { - SetHandleInformation(stdoh, HANDLE_FLAG_INHERIT, 1); - if (pipe_stdout == NULL) - start.hStdOutput = stdoh; - } - else - SetHandleInformation(stdoh, HANDLE_FLAG_INHERIT, 0); - } - - stdeh = GetStdHandle(STD_ERROR_HANDLE); - GetHandleInformation(stdeh, &stdef); - if (stdeh) - { - if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR) - { - SetHandleInformation(stdeh, HANDLE_FLAG_INHERIT, 1); - start.hStdError = stdeh; - } - else - SetHandleInformation(stdeh, HANDLE_FLAG_INHERIT, 0); - } - - if (GNUNET_YES == pipe_control) - { - struct GNUNET_DISK_PipeHandle *childpipe; - childpipe = GNUNET_DISK_pipe(GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO); - if (NULL == childpipe) - return NULL; - childpipe_read = - GNUNET_DISK_pipe_detach_end(childpipe, GNUNET_DISK_PIPE_END_READ); - childpipe_write = - GNUNET_DISK_pipe_detach_end(childpipe, GNUNET_DISK_PIPE_END_WRITE); - GNUNET_DISK_pipe_close(childpipe); - if ((NULL == childpipe_read) || (NULL == childpipe_write) || - (GNUNET_OK != GNUNET_DISK_internal_file_handle_(childpipe_read, - &childpipe_read_handle, - sizeof(HANDLE)))) - { - if (childpipe_read) - GNUNET_DISK_file_close(childpipe_read); - if (childpipe_write) - GNUNET_DISK_file_close(childpipe_write); - GNUNET_free(cmd); - return NULL; - } - /* Unlike *nix variant, we don't dup the handle, so can't close - * filehandle right now. - */ - SetHandleInformation(childpipe_read_handle, HANDLE_FLAG_INHERIT, 1); - } - else - { - childpipe_read = NULL; - childpipe_write = NULL; - } - - if (lsocks != NULL && lsocks[0] != INVALID_SOCKET) - { - lsocks_pipe = - GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); - - if (lsocks_pipe == NULL) - { - GNUNET_free(cmd); - GNUNET_DISK_pipe_close(lsocks_pipe); - if (GNUNET_YES == pipe_control) - { - GNUNET_DISK_file_close(childpipe_write); - GNUNET_DISK_file_close(childpipe_read); - } - return NULL; - } - lsocks_write_fd = - GNUNET_DISK_pipe_handle(lsocks_pipe, GNUNET_DISK_PIPE_END_WRITE); - GNUNET_DISK_internal_file_handle_(lsocks_write_fd, - &lsocks_write, - sizeof(HANDLE)); - GNUNET_DISK_internal_file_handle_( - GNUNET_DISK_pipe_handle(lsocks_pipe, GNUNET_DISK_PIPE_END_READ), - &lsocks_read, - sizeof(HANDLE)); - } - else - { - lsocks_pipe = NULL; - lsocks_write_fd = NULL; - } - - env_off = 0; - if (GNUNET_YES == pipe_control) - { - GNUNET_asprintf(&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE); - GNUNET_asprintf(&our_env[env_off++], "%p", childpipe_read_handle); - } - if ((lsocks != NULL) && (lsocks[0] != INVALID_SOCKET)) - { - /*This will tell the child that we're going to send lsocks over the pipe*/ - GNUNET_asprintf(&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS"); - GNUNET_asprintf(&our_env[env_off++], "%lu", lsocks_read); - } - our_env[env_off++] = NULL; - env_block = CreateCustomEnvTable(our_env); - while (0 > env_off) - GNUNET_free_non_null(our_env[--env_off]); - - wpath_len = 0; - if (NULL == - (wpath = - u8_to_u16((uint8_t *)path, 1 + strlen(path), NULL, &wpath_len))) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", - path, - errno); - GNUNET_free(env_block); - GNUNET_free(cmd); - if (lsocks_pipe) - GNUNET_DISK_pipe_close(lsocks_pipe); - if (GNUNET_YES == pipe_control) - { - GNUNET_DISK_file_close(childpipe_write); - GNUNET_DISK_file_close(childpipe_read); - } - return NULL; - } - - wcmd_len = 0; - if (NULL == - (wcmd = u8_to_u16((uint8_t *)cmd, 1 + strlen(cmd), NULL, &wcmd_len))) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", - cmd, - errno); - GNUNET_free(env_block); - GNUNET_free(cmd); - free(wpath); - if (lsocks_pipe) - GNUNET_DISK_pipe_close(lsocks_pipe); - if (GNUNET_YES == pipe_control) - { - GNUNET_DISK_file_close(childpipe_write); - GNUNET_DISK_file_close(childpipe_read); - } - return NULL; - } - - create_no_window = 0; - { - HANDLE console_input = CreateFile("CONIN$", - GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - OPEN_EXISTING, - 0, - NULL); - if (INVALID_HANDLE_VALUE == console_input) - create_no_window = CREATE_NO_WINDOW; - else - CloseHandle(console_input); - } - - bresult = CreateProcessW(wpath, - wcmd, - NULL, - NULL, - GNUNET_YES, - create_no_window | CREATE_SUSPENDED, - env_block, - NULL, - &start, - &proc); - error_code = GetLastError(); - - if ((NULL == pipe_stdin) && (stdih)) - SetHandleInformation(stdih, HANDLE_FLAG_INHERIT, stdif); - - - if ((NULL == pipe_stdout) && (stdoh)) - SetHandleInformation(stdoh, HANDLE_FLAG_INHERIT, stdof); - - if (stdeh) - SetHandleInformation(stdeh, HANDLE_FLAG_INHERIT, stdef); - - if (!bresult) - LOG(GNUNET_ERROR_TYPE_ERROR, - "CreateProcess(%s, %s) failed: %lu\n", - path, - cmd, - error_code); - - GNUNET_free(env_block); - GNUNET_free(cmd); - free(wpath); - free(wcmd); - if (GNUNET_YES == pipe_control) - { - GNUNET_DISK_file_close(childpipe_read); - } - - if (!bresult) - { - if (GNUNET_YES == pipe_control) - { - GNUNET_DISK_file_close(childpipe_write); - } - if (NULL != lsocks) - GNUNET_DISK_pipe_close(lsocks_pipe); - SetErrnoFromWinError(error_code); - return NULL; - } - - gnunet_proc = GNUNET_new(struct GNUNET_OS_Process); - gnunet_proc->pid = proc.dwProcessId; - gnunet_proc->handle = proc.hProcess; - gnunet_proc->control_pipe = childpipe_write; - - CreateThread(NULL, 64000, &child_wait_thread, (void *)gnunet_proc, 0, NULL); - - ResumeThread(proc.hThread); - CloseHandle(proc.hThread); - - if ((NULL == lsocks) || (INVALID_SOCKET == lsocks[0])) - return gnunet_proc; - - GNUNET_DISK_pipe_close_end(lsocks_pipe, GNUNET_DISK_PIPE_END_READ); - - /* This is a replacement for "goto error" that doesn't use goto */ - fail = 1; - do - { - ssize_t wrote; - uint64_t size; - uint64_t count; - unsigned int i; - - /* Tell the number of sockets */ - for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++) - ; - - wrote = GNUNET_DISK_file_write(lsocks_write_fd, &count, sizeof(count)); - if (sizeof(count) != wrote) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Failed to write %u count bytes to the child: %lu\n", - sizeof(count), - GetLastError()); - break; - } - for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++) - { - WSAPROTOCOL_INFOA pi; - /* Get a socket duplication info */ - if (SOCKET_ERROR == - WSADuplicateSocketA(lsocks[i], gnunet_proc->pid, &pi)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Failed to duplicate an socket[%u]: %lu\n", - i, - GetLastError()); - break; - } - /* Synchronous I/O is not nice, but we can't schedule this: - * lsocks will be closed/freed by the caller soon, and until - * the child creates a duplicate, closing a socket here will - * close it for good. - */ - /* Send the size of the structure - * (the child might be built with different headers...) - */ - size = sizeof(pi); - wrote = GNUNET_DISK_file_write(lsocks_write_fd, &size, sizeof(size)); - if (sizeof(size) != wrote) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Failed to write %u size[%u] bytes to the child: %lu\n", - sizeof(size), - i, - GetLastError()); - break; - } - /* Finally! Send the data */ - wrote = GNUNET_DISK_file_write(lsocks_write_fd, &pi, sizeof(pi)); - if (sizeof(pi) != wrote) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Failed to write %u socket[%u] bytes to the child: %lu\n", - sizeof(pi), - i, - GetLastError()); - break; - } - } - /* This will block us until the child makes a final read or closes - * the pipe (hence no 'wrote' check), since we have to wait for it - * to duplicate the last socket, before we return and start closing - * our own copies) - */ - wrote = GNUNET_DISK_file_write(lsocks_write_fd, &count, sizeof(count)); - fail = 0; - } - while (fail); - - GNUNET_DISK_file_sync(lsocks_write_fd); - GNUNET_DISK_pipe_close(lsocks_pipe); - - if (fail) - { - /* If we can't pass on the socket(s), the child will block forever, - * better put it out of its misery. - */ - SafeTerminateProcess(gnunet_proc->handle, 0, 0); - CloseHandle(gnunet_proc->handle); - if (NULL != gnunet_proc->control_pipe) - GNUNET_DISK_file_close(gnunet_proc->control_pipe); - GNUNET_free(gnunet_proc); - return NULL; - } - return gnunet_proc; -#endif } @@ -1439,7 +897,6 @@ process_status(struct GNUNET_OS_Process *proc, unsigned long *code, int options) { -#ifndef MINGW int status; int ret; @@ -1488,53 +945,6 @@ process_status(struct GNUNET_OS_Process *proc, *type = GNUNET_OS_PROCESS_UNKNOWN; *code = 0; } -#else -#ifndef WNOHANG -#define WNOHANG 42 /* just a flag for W32, purely internal at this point */ -#endif - - HANDLE h; - DWORD c, error_code, ret; - - h = proc->handle; - ret = proc->pid; - if (h == NULL || ret == 0) - { - LOG(GNUNET_ERROR_TYPE_WARNING, - "Invalid process information {%d, %08X}\n", - ret, - h); - return GNUNET_SYSERR; - } - if (h == NULL) - h = GetCurrentProcess(); - - if (WNOHANG != options) - { - if (WAIT_OBJECT_0 != WaitForSingleObject(h, INFINITE)) - { - SetErrnoFromWinError(GetLastError()); - return GNUNET_SYSERR; - } - } - SetLastError(0); - ret = GetExitCodeProcess(h, &c); - error_code = GetLastError(); - if (ret == 0 || error_code != NO_ERROR) - { - SetErrnoFromWinError(error_code); - LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess"); - return GNUNET_SYSERR; - } - if (STILL_ACTIVE == c) - { - *type = GNUNET_OS_PROCESS_RUNNING; - *code = 0; - return GNUNET_NO; - } - *type = GNUNET_OS_PROCESS_EXITED; - *code = c; -#endif return GNUNET_OK; } @@ -1589,7 +999,6 @@ GNUNET_OS_process_wait_status(struct GNUNET_OS_Process *proc, int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc) { -#ifndef MINGW pid_t pid = proc->pid; pid_t ret; @@ -1601,28 +1010,6 @@ GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc) return GNUNET_SYSERR; } return GNUNET_OK; -#else - HANDLE h; - - h = proc->handle; - if (NULL == h) - { - LOG(GNUNET_ERROR_TYPE_WARNING, - "Invalid process information {%d, %08X}\n", - proc->pid, - h); - return GNUNET_SYSERR; - } - if (NULL == h) - h = GetCurrentProcess(); - - if (WAIT_OBJECT_0 != WaitForSingleObject(h, INFINITE)) - { - SetErrnoFromWinError(GetLastError()); - return GNUNET_SYSERR; - } - return GNUNET_OK; -#endif } diff --git a/src/util/scheduler.c b/src/util/scheduler.c index 4bea5bd5d..f379bce42 100644 --- a/src/util/scheduler.c +++ b/src/util/scheduler.c @@ -108,7 +108,6 @@ struct GNUNET_SCHEDULER_Handle { struct GNUNET_SIGNAL_Context *shc_gterm; #endif -#ifndef MINGW /** * context of the SIGQUIT handler */ @@ -123,7 +122,6 @@ struct GNUNET_SCHEDULER_Handle { * context of hte SIGPIPE handler */ struct GNUNET_SIGNAL_Context *shc_pipe; -#endif }; @@ -621,13 +619,11 @@ static pid_t my_pid; /** * Signal handler called for SIGPIPE. */ -#ifndef MINGW static void sighandler_pipe() { return; } -#endif ///** @@ -1396,7 +1392,6 @@ check_fd(struct GNUNET_SCHEDULER_Task *t, int raw_fd) * @return unique task identifier for the job * only valid until @a task is started! */ -#ifndef MINGW static struct GNUNET_SCHEDULER_Task * add_without_sets(struct GNUNET_TIME_Relative delay, enum GNUNET_SCHEDULER_Priority priority, @@ -1446,7 +1441,6 @@ add_without_sets(struct GNUNET_TIME_Relative delay, init_backtrace(t); return t; } -#endif /** @@ -1586,22 +1580,6 @@ GNUNET_SCHEDULER_add_net_with_priority(struct GNUNET_TIME_Relative delay, { /* scheduler must be running */ GNUNET_assert(NULL != scheduler_driver); - -#if MINGW - struct GNUNET_NETWORK_FDSet *s; - struct GNUNET_SCHEDULER_Task * ret; - - GNUNET_assert(NULL != fd); - s = GNUNET_NETWORK_fdset_create(); - GNUNET_NETWORK_fdset_set(s, fd); - ret = GNUNET_SCHEDULER_add_select( - priority, delay, - on_read ? s : NULL, - on_write ? s : NULL, - task, task_cls); - GNUNET_NETWORK_fdset_destroy(s); - return ret; -#else GNUNET_assert(on_read || on_write); GNUNET_assert(GNUNET_NETWORK_get_fd(fd) >= 0); return add_without_sets(delay, priority, @@ -1610,7 +1588,6 @@ GNUNET_SCHEDULER_add_net_with_priority(struct GNUNET_TIME_Relative delay, NULL, NULL, task, task_cls); -#endif } @@ -1710,22 +1687,6 @@ GNUNET_SCHEDULER_add_file_with_priority(struct GNUNET_TIME_Relative delay, { /* scheduler must be running */ GNUNET_assert(NULL != scheduler_driver); - -#if MINGW - struct GNUNET_NETWORK_FDSet *s; - struct GNUNET_SCHEDULER_Task * ret; - - GNUNET_assert(NULL != fd); - s = GNUNET_NETWORK_fdset_create(); - GNUNET_NETWORK_fdset_handle_set(s, fd); - ret = GNUNET_SCHEDULER_add_select( - priority, delay, - on_read ? s : NULL, - on_write ? s : NULL, - task, task_cls); - GNUNET_NETWORK_fdset_destroy(s); - return ret; -#else GNUNET_assert(on_read || on_write); GNUNET_assert(fd->fd >= 0); return add_without_sets(delay, priority, @@ -1734,7 +1695,6 @@ GNUNET_SCHEDULER_add_file_with_priority(struct GNUNET_TIME_Relative delay, on_read ? fd : NULL, on_write ? fd : NULL, task, task_cls); -#endif } @@ -2218,14 +2178,12 @@ GNUNET_SCHEDULER_driver_init(const struct GNUNET_SCHEDULER_Driver *driver) sh->shc_gterm = GNUNET_SIGNAL_handler_install(GNUNET_TERM_SIG, &sighandler_shutdown); #endif -#ifndef MINGW sh->shc_pipe = GNUNET_SIGNAL_handler_install(SIGPIPE, &sighandler_pipe); sh->shc_quit = GNUNET_SIGNAL_handler_install(SIGQUIT, &sighandler_shutdown); sh->shc_hup = GNUNET_SIGNAL_handler_install(SIGHUP, &sighandler_shutdown); -#endif /* Setup initial tasks */ current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT; @@ -2282,11 +2240,9 @@ GNUNET_SCHEDULER_driver_done(struct GNUNET_SCHEDULER_Handle *sh) #if (SIGTERM != GNUNET_TERM_SIG) GNUNET_SIGNAL_handler_uninstall(sh->shc_gterm); #endif -#ifndef MINGW GNUNET_SIGNAL_handler_uninstall(sh->shc_pipe); GNUNET_SIGNAL_handler_uninstall(sh->shc_quit); GNUNET_SIGNAL_handler_uninstall(sh->shc_hup); -#endif GNUNET_DISK_pipe_close(shutdown_pipe_handle); shutdown_pipe_handle = NULL; scheduler_driver = NULL; @@ -2352,7 +2308,6 @@ select_loop(struct GNUNET_SCHEDULER_Handle *sh, LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "select"); -#ifndef MINGW #if USE_LSOF char lsof[512]; @@ -2366,7 +2321,6 @@ select_loop(struct GNUNET_SCHEDULER_Handle *sh, LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "system"); #endif -#endif #if DEBUG_FDS for (struct Scheduled *s = context->scheduled_head; NULL != s; diff --git a/src/util/service.c b/src/util/service.c index 880047a42..b0f4ea289 100644 --- a/src/util/service.c +++ b/src/util/service.c @@ -1354,93 +1354,6 @@ get_server_addresses(const char *service_name, } -#ifdef MINGW -/** - * Read listen sockets from the parent process (ARM). - * - * @param sh service context to initialize - * @return NULL-terminated array of sockets on success, - * NULL if not ok (must bind yourself) - */ -static struct GNUNET_NETWORK_Handle ** -receive_sockets_from_parent(struct GNUNET_SERVICE_Handle *sh) -{ - static struct GNUNET_NETWORK_Handle **lsocks; - const char *env_buf; - int fail; - uint64_t count; - uint64_t i; - HANDLE lsocks_pipe; - - env_buf = getenv("GNUNET_OS_READ_LSOCKS"); - if ((NULL == env_buf) || (strlen(env_buf) <= 0)) - return NULL; - /* Using W32 API directly here, because this pipe will - * never be used outside of this function, and it's just too much of a bother - * to create a GNUnet API that boxes a HANDLE (the way it is done with socks) - */ - lsocks_pipe = (HANDLE)strtoul(env_buf, NULL, 10); - if ((0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe)) - return NULL; - fail = 1; - do - { - int ret; - int fail2; - DWORD rd; - - ret = ReadFile(lsocks_pipe, &count, sizeof(count), &rd, NULL); - if ((0 == ret) || (sizeof(count) != rd) || (0 == count)) - break; - lsocks = GNUNET_new_array(count + 1, struct GNUNET_NETWORK_Handle *); - - fail2 = 1; - for (i = 0; i < count; i++) - { - WSAPROTOCOL_INFOA pi; - uint64_t size; - _win_socket s; - - ret = ReadFile(lsocks_pipe, &size, sizeof(size), &rd, NULL); - if ((0 == ret) || (sizeof(size) != rd) || (sizeof(pi) != size)) - break; - ret = ReadFile(lsocks_pipe, &pi, sizeof(pi), &rd, NULL); - if ((0 == ret) || (sizeof(pi) != rd)) - break; - s = WSASocketA(pi.iAddressFamily, - pi.iSocketType, - pi.iProtocol, - &pi, - 0, - WSA_FLAG_OVERLAPPED); - lsocks[i] = GNUNET_NETWORK_socket_box_native(s); - if (NULL == lsocks[i]) - break; - else if (i == count - 1) - fail2 = 0; - } - if (fail2) - break; - lsocks[count] = NULL; - fail = 0; - } - while (fail); - CloseHandle(lsocks_pipe); - - if (fail) - { - LOG(GNUNET_ERROR_TYPE_ERROR, - _("Could not access a pre-bound socket, will try to bind myself\n")); - for (i = 0; (i < count) && (NULL != lsocks[i]); i++) - GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(lsocks[i])); - GNUNET_free(lsocks); - return NULL; - } - return lsocks; -} -#endif - - /** * Create and initialize a listen socket for the server. * @@ -1557,13 +1470,10 @@ setup_service(struct GNUNET_SERVICE_Handle *sh) { int tolerant; struct GNUNET_NETWORK_Handle **lsocks; - -#ifndef MINGW const char *nfds; unsigned int cnt; int flags; char dummy[2]; -#endif if (GNUNET_CONFIGURATION_have_value(sh->cfg, sh->service_name, "TOLERANT")) { @@ -1583,7 +1493,7 @@ setup_service(struct GNUNET_SERVICE_Handle *sh) tolerant = GNUNET_NO; lsocks = NULL; -#ifndef MINGW + errno = 0; if ((NULL != (nfds = getenv("LISTEN_FDS"))) && (1 == sscanf(nfds, "%u%1s", &cnt, dummy)) && (cnt > 0) && @@ -1611,13 +1521,6 @@ setup_service(struct GNUNET_SERVICE_Handle *sh) } unsetenv("LISTEN_FDS"); } -#else - if (NULL != getenv("GNUNET_OS_READ_LSOCKS")) - { - lsocks = receive_sockets_from_parent(sh); - putenv("GNUNET_OS_READ_LSOCKS="); - } -#endif if (NULL != lsocks) { @@ -1723,7 +1626,7 @@ set_user_id(struct GNUNET_SERVICE_Handle *sh) if (NULL == (user = get_user_name(sh))) return GNUNET_OK; /* keep */ -#ifndef MINGW + struct passwd *pws; errno = 0; @@ -1754,7 +1657,7 @@ set_user_id(struct GNUNET_SERVICE_Handle *sh) return GNUNET_SYSERR; } } -#endif + GNUNET_free(user); return GNUNET_OK; } @@ -1808,7 +1711,6 @@ pid_file_delete(struct GNUNET_SERVICE_Handle *sh) static int detach_terminal(struct GNUNET_SERVICE_Handle *sh) { -#ifndef MINGW pid_t pid; int nullfd; int filedes[2]; @@ -1875,11 +1777,7 @@ detach_terminal(struct GNUNET_SERVICE_Handle *sh) if (-1 == pid) LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "setsid"); sh->ready_confirm_fd = filedes[1]; -#else - /* FIXME: we probably need to do something else - * elsewhere in order to fork the process itself... */ - FreeConsole(); -#endif + return GNUNET_OK; } diff --git a/src/util/strings.c b/src/util/strings.c index dc46ad33a..25b09f8e2 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -602,20 +602,13 @@ char * GNUNET_STRINGS_filename_expand(const char *fil) { char *buffer; - -#ifndef MINGW size_t len; char *fm; const char *fil_ptr; -#else - char *fn; - long lRet; -#endif if (fil == NULL) return NULL; -#ifndef MINGW if (fil[0] == DIR_SEPARATOR) /* absolute path, just copy */ return GNUNET_strdup(fil); @@ -677,34 +670,6 @@ GNUNET_STRINGS_filename_expand(const char *fil) fil_ptr); GNUNET_free(fm); return buffer; -#else - fn = GNUNET_malloc(MAX_PATH + 1); - - if ((lRet = plibc_conv_to_win_path(fil, fn)) != ERROR_SUCCESS) - { - SetErrnoFromWinError(lRet); - LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); - return NULL; - } - /* is the path relative? */ - if ((0 != strncmp(fn + 1, ":\\", 2)) && (0 != strncmp(fn, "\\\\", 2))) - { - char szCurDir[MAX_PATH + 1]; - - lRet = GetCurrentDirectory(MAX_PATH + 1, szCurDir); - if (lRet + strlen(fn) + 1 > (MAX_PATH + 1)) - { - SetErrnoFromWinError(ERROR_BUFFER_OVERFLOW); - LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory"); - return NULL; - } - GNUNET_asprintf(&buffer, "%s\\%s", szCurDir, fn); - GNUNET_free(fn); - fn = buffer; - } - - return fn; -#endif } diff --git a/src/util/test_common_logging_runtime_loglevels.c b/src/util/test_common_logging_runtime_loglevels.c index 07e2327c9..4ba7b544c 100644 --- a/src/util/test_common_logging_runtime_loglevels.c +++ b/src/util/test_common_logging_runtime_loglevels.c @@ -401,11 +401,7 @@ runone() proc = GNUNET_OS_start_process(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, pipe_stdout, NULL, -#if MINGW - "test_common_logging_dummy", -#else "./test_common_logging_dummy", -#endif "test_common_logging_dummy", NULL); GNUNET_assert(NULL != proc); putenv("GNUNET_FORCE_LOG="); diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c index 71a1ae5bd..b5b1e238c 100644 --- a/src/util/test_configuration.c +++ b/src/util/test_configuration.c @@ -323,12 +323,8 @@ testConfig() GNUNET_break(0); return 8; } -#ifndef MINGW + if (0 != strcmp(c, "/hello/world")) -#else -#define HI "\\hello\\world" - if (strstr(c, HI) != c + strlen(c) - strlen(HI)) -#endif { GNUNET_break(0); GNUNET_free(c); diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c index 620605f96..77b8c8b2f 100644 --- a/src/util/test_getopt.c +++ b/src/util/test_getopt.c @@ -163,9 +163,9 @@ main(int argc, char *argv[]) GNUNET_log_setup("test_getopt", "WARNING", NULL); /* suppress output from -h, -v options */ -#ifndef MINGW + GNUNET_break(0 == close(1)); -#endif + if (0 != testMinimal()) errCnt++; if (0 != testVerbose()) diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c index ea26c4ee6..0b27a2eff 100644 --- a/src/util/test_resolver_api.c +++ b/src/util/test_resolver_api.c @@ -307,11 +307,8 @@ run(void *cls, char *const *args, const char *cfgfile, #if HAVE_SOCKADDR_IN_SIN_LEN sa.sin_len = (u_char)sizeof(sa); #endif -#ifndef MINGW inet_aton(ROOTSERVER_IP, &sa.sin_addr); -#else - sa.sin_addr.S_un.S_addr = inet_addr(ROOTSERVER_IP); -#endif + GNUNET_RESOLVER_hostname_get((const struct sockaddr *)&sa, sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls); diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c index 236bb4e76..8952225c2 100644 --- a/src/util/test_scheduler.c +++ b/src/util/test_scheduler.c @@ -211,7 +211,6 @@ checkShutdown() } -#ifndef MINGW static void taskSig(void *cls) { @@ -244,7 +243,6 @@ checkSignal() GNUNET_SCHEDULER_run(&taskSig, &ok); return ok; } -#endif static void @@ -283,9 +281,7 @@ main(int argc, char *argv[]) GNUNET_log_setup("test_scheduler", "WARNING", NULL); ret += check(); ret += checkCancel(); -#ifndef MINGW ret += checkSignal(); -#endif ret += checkShutdown(); GNUNET_DISK_pipe_close(p); diff --git a/src/util/test_service.c b/src/util/test_service.c index c0b02872f..4edc38a0d 100644 --- a/src/util/test_service.c +++ b/src/util/test_service.c @@ -206,11 +206,10 @@ main(int argc, NULL); ret += check("test_service"); ret += check("test_service"); -#ifndef MINGW s = GNUNET_NETWORK_socket_create(PF_INET6, SOCK_STREAM, 0); -#endif + if (NULL == s) { if ((errno == ENOBUFS) || diff --git a/src/util/test_strings.c b/src/util/test_strings.c index a4e548e31..60887c587 100644 --- a/src/util/test_strings.c +++ b/src/util/test_strings.c @@ -67,11 +67,9 @@ main(int argc, char *argv[]) (GNUNET_TIME_UNIT_MILLISECONDS, 7 * 60 * 60 * 1000), GNUNET_YES); WANTNF(buf, bc); -#ifndef MINGW + hdir = getenv("HOME"); -#else - hdir = getenv("USERPROFILE"); -#endif + GNUNET_snprintf(buf, sizeof(buf), "%s%s", hdir, DIR_SEPARATOR_STR); b = GNUNET_STRINGS_filename_expand("~"); GNUNET_assert(b != NULL); diff --git a/src/util/w32cat.c b/src/util/w32cat.c deleted file mode 100644 index fa7637d38..000000000 --- a/src/util/w32cat.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - W32 version of 'cat' program - Copyright (C) 2012 LRN - - cat is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - cat is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with cat; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -#include -#include -#include -#include - -DWORD WINAPI -parent_control_thread(LPVOID lpParameter) -{ - HANDLE h = (HANDLE)lpParameter; - - while (TRUE) - { - DWORD dw; - BOOL b; - unsigned char c; - b = ReadFile(h, &c, 1, &dw, NULL); - if (!b) - { - ExitProcess(0); - } - raise((int)c); - } -} - -void -install_parent_control_handler() -{ - const char *env_buf; - char *env_buf_end; - uint64_t pipe_fd; - HANDLE pipe_handle; - - env_buf = getenv("GNUNET_OS_CONTROL_PIPE"); - if ((NULL == env_buf) || (strlen(env_buf) <= 0)) - return; - errno = 0; - pipe_fd = strtoull(env_buf, &env_buf_end, 16); - if ((0 != errno) || (env_buf == env_buf_end)) - return; - /* Gcc will issue a warning here. What to do with it? */ - pipe_handle = (HANDLE)(uintptr_t)pipe_fd; - CreateThread(NULL, 0, parent_control_thread, (LPVOID)pipe_handle, 0, NULL); -} - -int -main(int argc, char **argv) -{ - HANDLE stdi, stdo; - BOOL b; - wchar_t *commandlinew, **argvw; - int argcw; - int i; - - stdo = GetStdHandle(STD_OUTPUT_HANDLE); - if (stdo == INVALID_HANDLE_VALUE || stdo == NULL) - return 1; - - commandlinew = GetCommandLineW(); - argvw = CommandLineToArgvW(commandlinew, &argcw); - if (argvw == NULL) - return 1; - - install_parent_control_handler(); - - for (i = 1; i < argcw || argcw == 1; i++) - { - DWORD r, w; - int is_dash = wcscmp(NULL == argvw[i] ? L"-" : argvw[i], L"-") == 0; - if (argcw == 1 || is_dash) - { - stdi = GetStdHandle(STD_INPUT_HANDLE); - if (stdi == INVALID_HANDLE_VALUE) - { - fprintf(stderr, "cat: Failed to obtain stdin handle.\n"); - return 4; - } - if (stdi == NULL) - { - fprintf(stderr, "cat: Have no stdin.\n"); - return 5; - } - } - else - { - stdi = CreateFileW(argvw[i], GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); - if (stdi == INVALID_HANDLE_VALUE) - { - wchar_t *msgbuf; - DWORD le = GetLastError(); - if (0 < FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *)&msgbuf, 0, NULL)) - { - fprintf(stderr, "cat: Failed to open file `%S'. Error %lu.\n", argvw[i], le); - return 3; - } - fprintf(stderr, "cat: Failed to open file `%S'. Error %lu: %S\n", argvw[i], le, msgbuf); - if (msgbuf != NULL) - LocalFree(msgbuf); - return 2; - } - } - do - { - unsigned char c; - b = ReadFile(stdi, &c, 1, &r, NULL); - if (b && r > 0) - { - b = WriteFile(stdo, &c, 1, &w, NULL); - if (b == 0) - { - wchar_t *msgbuf; - DWORD le = GetLastError(); - if (0 < FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *)&msgbuf, 0, NULL)) - { - fprintf(stderr, "cat: Failed to write into stdout. Error %lu.\n", le); - return 3; - } - fprintf(stderr, "cat: Failed to write into stdout. Error %lu: %S\n", le, msgbuf); - if (msgbuf != NULL) - LocalFree(msgbuf); - return 6; - } - } - } - while (b && r > 0); - if (argcw == 1) - break; - if (!is_dash) - CloseHandle(stdi); - } - LocalFree(argvw); - return 0; -} diff --git a/src/util/win.c b/src/util/win.c deleted file mode 100644 index a2b0d08da..000000000 --- a/src/util/win.c +++ /dev/null @@ -1,1365 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 GNUnet e.V. - - GNUnet is free software: you can redistribute it and/or modify it - under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, - or (at your option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file util/win.c - * @brief Helper functions for MS Windows in C++ - * @author Nils Durner - */ - -#ifndef _WIN_C -#define _WIN_C - -#include "winproc.h" -#include "platform.h" -#include "gnunet_crypto_lib.h" -#include "gnunet_common.h" -#include "gnunet_connection_lib.h" - -#include -#ifndef INHERITED_ACE -#define INHERITED_ACE 0x10 -#endif - -int plibc_conv_to_win_path(const char *pszUnix, char *pszWindows); - -#define _IP_ADAPTER_UNICAST_ADDRESS_HEAD \ - union { \ - struct { \ - ULONG Length; \ - DWORD Flags; \ - }; \ - }; - -#define _IP_ADAPTER_UNICAST_ADDRESS_BASE \ - SOCKET_ADDRESS Address; \ - IP_PREFIX_ORIGIN PrefixOrigin; \ - IP_SUFFIX_ORIGIN SuffixOrigin; \ - IP_DAD_STATE DadState; \ - ULONG ValidLifetime; \ - ULONG PreferredLifetime; \ - ULONG LeaseLifetime; - -#define _IP_ADAPTER_UNICAST_ADDRESS_ADD_VISTA \ - UINT8 OnLinkPrefixLength; - - -#define _IP_ADAPTER_UNICAST_ADDRESS_DEFINE(suffix, addition) \ - typedef struct _IP_ADAPTER_UNICAST_ADDRESS ## suffix { \ - _IP_ADAPTER_UNICAST_ADDRESS_HEAD \ - struct _IP_ADAPTER_UNICAST_ADDRESS ## suffix *Next; \ - _IP_ADAPTER_UNICAST_ADDRESS_BASE \ - addition \ - } IP_ADAPTER_UNICAST_ADDRESS ## suffix, *PIP_ADAPTER_UNICAST_ADDRESS ## suffix; - -/* _IP_ADAPTER_UNICAST_ADDRESS_DEFINE(,) defined in w32api headers */ -_IP_ADAPTER_UNICAST_ADDRESS_DEFINE(_VISTA, _IP_ADAPTER_UNICAST_ADDRESS_ADD_VISTA) - - -#ifndef __MINGW64_VERSION_MAJOR -typedef struct _IP_ADAPTER_WINS_SERVER_ADDRESS { - union { - ULONGLONG Alignment; - struct { - ULONG Length; - DWORD Reserved; - }; - }; - struct _IP_ADAPTER_WINS_SERVER_ADDRESS *Next; - SOCKET_ADDRESS Address; -} IP_ADAPTER_WINS_SERVER_ADDRESS, *PIP_ADAPTER_WINS_SERVER_ADDRESS, *PIP_ADAPTER_WINS_SERVER_ADDRESS_LH; - -typedef struct _IP_ADAPTER_GATEWAY_ADDRESS { - union { - ULONGLONG Alignment; - struct { - ULONG Length; - DWORD Reserved; - }; - }; - struct _IP_ADAPTER_GATEWAY_ADDRESS *Next; - SOCKET_ADDRESS Address; -} IP_ADAPTER_GATEWAY_ADDRESS, *PIP_ADAPTER_GATEWAY_ADDRESS, *PIP_ADAPTER_GATEWAY_ADDRESS_LH; -#endif - -typedef UINT32 NET_IF_COMPARTMENT_ID; -typedef GUID NET_IF_NETWORK_GUID; - -#ifndef __MINGW64_VERSION_MAJOR -typedef enum _NET_IF_CONNECTION_TYPE { - NET_IF_CONNECTION_DEDICATED = 1, - NET_IF_CONNECTION_PASSIVE, - NET_IF_CONNECTION_DEMAND, - NET_IF_CONNECTION_MAXIMUM -} NET_IF_CONNECTION_TYPE, *PNET_IF_CONNECTION_TYPE; - -typedef enum { - TUNNEL_TYPE_NONE = 0, - TUNNEL_TYPE_OTHER, - TUNNEL_TYPE_DIRECT, - TUNNEL_TYPE_6TO4, - TUNNEL_TYPE_ISATAP, - TUNNEL_TYPE_TEREDO, - TUNNEL_TYPE_IPHTTPS -} TUNNEL_TYPE, *PTUNNEL_TYPE; -#endif - -/* - A DUID consists of a two-octet type code represented in network byte - order, followed by a variable number of octets that make up the - actual identifier. A DUID can be no more than 128 octets long (not - including the type code). - */ -#define MAX_DHCPV6_DUID_LENGTH 130 - -#ifndef __MINGW64_VERSION_MAJOR -typedef union _NET_LUID { - ULONG64 Value; - struct { - ULONG64 Reserved : 24; - ULONG64 NetLuidIndex : 24; - ULONG64 IfType : 16; - } Info; -} NET_LUID, *PNET_LUID, IF_LUID; - -#define MAX_DNS_SUFFIX_STRING_LENGTH 246 - -typedef struct _IP_ADAPTER_DNS_SUFFIX { - struct _IP_ADAPTER_DNS_SUFFIX *Next; - WCHAR String[MAX_DNS_SUFFIX_STRING_LENGTH]; -} IP_ADAPTER_DNS_SUFFIX, *PIP_ADAPTER_DNS_SUFFIX; -#endif - - - -#define _IP_ADAPTER_ADDRESSES_HEAD \ - union { \ - ULONGLONG Alignment; \ - struct { \ - ULONG Length; \ - DWORD IfIndex; \ - }; \ - }; - -#define _IP_ADAPTER_ADDRESSES_BASE \ - PCHAR AdapterName; \ - PIP_ADAPTER_UNICAST_ADDRESS FirstUnicastAddress; \ - PIP_ADAPTER_ANYCAST_ADDRESS FirstAnycastAddress; \ - PIP_ADAPTER_MULTICAST_ADDRESS FirstMulticastAddress; \ - PIP_ADAPTER_DNS_SERVER_ADDRESS FirstDnsServerAddress; \ - PWCHAR DnsSuffix; \ - PWCHAR Description; \ - PWCHAR FriendlyName; \ - BYTE PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH]; \ - DWORD PhysicalAddressLength; \ - DWORD Flags; \ - DWORD Mtu; \ - DWORD IfType; \ - IF_OPER_STATUS OperStatus; - -#define _IP_ADAPTER_ADDRESSES_ADD_XPSP1 \ - DWORD Ipv6IfIndex; \ - DWORD ZoneIndices[16]; \ - PIP_ADAPTER_PREFIX FirstPrefix; \ - - -#define _IP_ADAPTER_ADDRESSES_ADD_VISTA \ - _IP_ADAPTER_ADDRESSES_ADD_XPSP1 \ - ULONG64 TransmitLinkSpeed; \ - ULONG64 ReceiveLinkSpeed; \ - PIP_ADAPTER_WINS_SERVER_ADDRESS_LH FirstWinsServerAddress; \ - PIP_ADAPTER_GATEWAY_ADDRESS_LH FirstGatewayAddress; \ - ULONG Ipv4Metric; \ - ULONG Ipv6Metric; \ - IF_LUID Luid; \ - SOCKET_ADDRESS Dhcpv4Server; \ - NET_IF_COMPARTMENT_ID CompartmentId; \ - NET_IF_NETWORK_GUID NetworkGuid; \ - NET_IF_CONNECTION_TYPE ConnectionType; \ - TUNNEL_TYPE TunnelType; \ - SOCKET_ADDRESS Dhcpv6Server; \ - BYTE Dhcpv6ClientDuid[MAX_DHCPV6_DUID_LENGTH]; \ - ULONG Dhcpv6ClientDuidLength; \ - ULONG Dhcpv6Iaid; - -#define _IP_ADAPTER_ADDRESSES_ADD_2008_OR_VISTASP1 \ - _IP_ADAPTER_ADDRESSES_ADD_VISTA \ - PIP_ADAPTER_DNS_SUFFIX FirstDnsSuffix; - -#define _IP_ADAPTER_ADDRESSES_DEFINE(suffix, addition) \ - typedef struct _IP_ADAPTER_ADDRESSES ## suffix { \ - _IP_ADAPTER_ADDRESSES_HEAD \ - struct _IP_ADAPTER_ADDRESSES ## suffix *Next; \ - _IP_ADAPTER_ADDRESSES_BASE \ - addition \ - } IP_ADAPTER_ADDRESSES ## suffix, *PIP_ADAPTER_ADDRESSES ## suffix; - - -/* _IP_ADAPTER_ADDRESSES_DEFINE(,) defined in w32api headers */ -_IP_ADAPTER_ADDRESSES_DEFINE(_XPSP1, _IP_ADAPTER_ADDRESSES_ADD_XPSP1) -_IP_ADAPTER_ADDRESSES_DEFINE(_VISTA, _IP_ADAPTER_ADDRESSES_ADD_VISTA) -_IP_ADAPTER_ADDRESSES_DEFINE(_2008_OR_VISTASP1, _IP_ADAPTER_ADDRESSES_ADD_2008_OR_VISTASP1) - -static int -EnumNICs_IPv6_get_ifs_count(_win_socket s) -{ - DWORD dwret = 0, err; - int iret; - - iret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, 0, - &dwret, NULL, NULL); - err = GetLastError(); - if (iret == SOCKET_ERROR && err == WSAEFAULT) - return dwret; - else if (iret == 0) - return 0; - return GNUNET_SYSERR; -} - -static int -EnumNICs_IPv6_get_ifs(_win_socket s, SOCKET_ADDRESS_LIST *inf, int size) -{ - int iret; - DWORD dwret = 0; - - iret = WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0, inf, size, - &dwret, NULL, NULL); - - if (iret != 0 || dwret != size) - { - /* It's supposed to succeed! And size should be the same */ - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - -#undef GNUNET_malloc -#define GNUNET_malloc(a) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | \ - HEAP_GENERATE_EXCEPTIONS, a) - -#undef GNUNET_free -#define GNUNET_free(a) HeapFree(GetProcessHeap(), 0, a) - -#undef GNUNET_free_non_null -#define GNUNET_free_non_null(a) do { if ((a) != NULL) GNUNET_free (a); } while (0) - -static int -EnumNICs_IPv4_get_ifs(SOCKET s, INTERFACE_INFO **inf, int *size) -{ - int iret; - DWORD dwret = 0; - DWORD error; - INTERFACE_INFO *ii = NULL; - DWORD ii_size = sizeof(INTERFACE_INFO) * 15; - - while (TRUE) - { - if (ii_size >= sizeof(INTERFACE_INFO) * 1000) - return GNUNET_SYSERR; - ii = (INTERFACE_INFO *)GNUNET_malloc(ii_size); - dwret = 0; - iret = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, ii, ii_size, - &dwret, NULL, NULL); - error = GetLastError(); - if (iret == SOCKET_ERROR) - { - if (error == WSAEFAULT) - { - GNUNET_free(ii); - ii_size *= 2; - continue; - } - GNUNET_free(ii); - return GNUNET_SYSERR; - } - else - { - *inf = ii; - *size = dwret; - return GNUNET_OK; - } - } - return GNUNET_SYSERR; -} - -int -EnumNICs2(INTERFACE_INFO **ifs4, int *ifs4_len, SOCKET_ADDRESS_LIST **ifs6) -{ - int result = 0; - _win_socket s4; - _win_socket s6; - int ifs4len = 0; - int ifs6len = 0; - INTERFACE_INFO *interfaces4 = NULL; - SOCKET_ADDRESS_LIST *interfaces6 = NULL; - - SetLastError(0); - s4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - (void)GetLastError(); - SetLastError(0); - s6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); - (void)GetLastError(); - if (s6 != INVALID_SOCKET) - { - ifs6len = EnumNICs_IPv6_get_ifs_count(s6); - if (ifs6len > 0) - { - interfaces6 = (SOCKET_ADDRESS_LIST *)GNUNET_malloc(ifs6len); - result = EnumNICs_IPv6_get_ifs(s6, interfaces6, ifs6len) || result; - } - closesocket(s6); - s6 = INVALID_SOCKET; - } - - if (s4 != INVALID_SOCKET) - { - result = EnumNICs_IPv4_get_ifs(s4, &interfaces4, &ifs4len) || result; - closesocket(s4); - s4 = INVALID_SOCKET; - } - if (ifs6len + ifs4len == 0) - goto error; - - if (!result) - { - *ifs4 = interfaces4; - *ifs4_len = ifs4len; - *ifs6 = interfaces6; - return GNUNET_OK; - } -error: - if (interfaces4 != NULL) - GNUNET_free(interfaces4); - if (interfaces6 != NULL) - GNUNET_free(interfaces6); - if (s4 != INVALID_SOCKET) - closesocket(s4); - if (s6 != INVALID_SOCKET) - closesocket(s6); - return GNUNET_SYSERR; -} - - -/** - * @returns #GNUNET_OK on success, #GNUNET_SYSERR on error - */ -int -EnumNICs3(struct EnumNICs3_results **results, int *results_count) -{ - DWORD dwRetVal = 0; - int count = 0; - ULONG flags = /*GAA_FLAG_INCLUDE_PREFIX |*/ GAA_FLAG_SKIP_ANYCAST | - GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER; - struct sockaddr_in6 examplecom6; - IPAddr examplecom; - DWORD best_interface = 0; - DWORD best_interface6 = 0; - - int use_enum2 = 0; - INTERFACE_INFO *interfaces4 = NULL; - int interfaces4_len = 0; - SOCKET_ADDRESS_LIST *interfaces6 = NULL; - - unsigned long outBufLen = sizeof(IP_ADAPTER_ADDRESSES); - IP_ADAPTER_ADDRESSES *pCurrentAddress = NULL; - IP_ADAPTER_ADDRESSES *pAddresses = (IP_ADAPTER_ADDRESSES *)GNUNET_malloc(outBufLen); - - if (GetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAddresses, &outBufLen) - == ERROR_BUFFER_OVERFLOW) - { - GNUNET_free(pAddresses); - pAddresses = (IP_ADAPTER_ADDRESSES *)GNUNET_malloc(outBufLen); - } - - dwRetVal = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAddresses, &outBufLen); - - if (dwRetVal != NO_ERROR) - { - GNUNET_free(pAddresses); - return GNUNET_SYSERR; - } - - if (pAddresses->Length < sizeof(IP_ADAPTER_ADDRESSES_VISTA)) - { - use_enum2 = 1; - - /* Enumerate NICs using WSAIoctl() */ - if (GNUNET_OK != EnumNICs2(&interfaces4, &interfaces4_len, &interfaces6)) - { - GNUNET_free(pAddresses); - return GNUNET_SYSERR; - } - } - - examplecom = inet_addr("192.0.34.166"); /* www.example.com */ - if (GetBestInterface(examplecom, &best_interface) != NO_ERROR) - best_interface = 0; - - if (GNGetBestInterfaceEx != NULL) - { - examplecom6.sin6_family = AF_INET6; - examplecom6.sin6_port = 0; - examplecom6.sin6_flowinfo = 0; - examplecom6.sin6_scope_id = 0; - inet_pton(AF_INET6, "2001:500:88:200:0:0:0:10", - (struct sockaddr *)&examplecom6.sin6_addr); - dwRetVal = GNGetBestInterfaceEx((struct sockaddr *)&examplecom6, - &best_interface6); - if (dwRetVal != NO_ERROR) - best_interface6 = 0; - } - - /* Give IPv6 a priority */ - if (best_interface6 != 0) - best_interface = best_interface6; - - count = 0; - for (pCurrentAddress = pAddresses; - pCurrentAddress != NULL; pCurrentAddress = pCurrentAddress->Next) - { - if (pCurrentAddress->OperStatus == IfOperStatusUp) - { - IP_ADAPTER_UNICAST_ADDRESS *unicast = NULL; - for (unicast = pCurrentAddress->FirstUnicastAddress; unicast != NULL; - unicast = unicast->Next) - { - if ((unicast->Address.lpSockaddr->sa_family == AF_INET || - unicast->Address.lpSockaddr->sa_family == AF_INET6) && - (unicast->DadState == IpDadStateDeprecated || - unicast->DadState == IpDadStatePreferred)) - count += 1; - } - } - } - - if (count == 0) - { - *results = NULL; - *results_count = 0; - GNUNET_free(pAddresses); - GNUNET_free_non_null(interfaces4); - GNUNET_free_non_null(interfaces6); - return GNUNET_OK; - } - - *results = (struct EnumNICs3_results *)GNUNET_malloc( - sizeof(struct EnumNICs3_results) * count); - *results_count = count; - - count = 0; - for (pCurrentAddress = pAddresses; - pCurrentAddress != NULL; pCurrentAddress = pCurrentAddress->Next) - { - struct EnumNICs3_results *r; - IP_ADAPTER_UNICAST_ADDRESS *unicast = NULL; - if (pCurrentAddress->OperStatus != IfOperStatusUp) - continue; - for (unicast = pCurrentAddress->FirstUnicastAddress; unicast != NULL; - unicast = unicast->Next) - { - int i, j; - int mask_length = -1; - char dst[INET6_ADDRSTRLEN + 1]; - - if ((unicast->Address.lpSockaddr->sa_family != AF_INET && - unicast->Address.lpSockaddr->sa_family != AF_INET6) || - (unicast->DadState != IpDadStateDeprecated && - unicast->DadState != IpDadStatePreferred)) - continue; - - r = &(*results)[count]; - r->flags = 0; - if (pCurrentAddress->IfIndex > 0 && - pCurrentAddress->IfIndex == best_interface && - unicast->Address.lpSockaddr->sa_family == AF_INET) - r->is_default = 1; - else if (pCurrentAddress->Ipv6IfIndex > 0 && - pCurrentAddress->Ipv6IfIndex == best_interface6 && - unicast->Address.lpSockaddr->sa_family == AF_INET6) - r->is_default = 1; - else - r->is_default = 0; - - /* Don't choose default interface twice */ - if (r->is_default) - best_interface = best_interface6 = 0; - - if (!use_enum2) - { - GNUNET_memcpy(&r->address, unicast->Address.lpSockaddr, - unicast->Address.iSockaddrLength); - memset(&r->mask, 0, sizeof(struct sockaddr)); - mask_length = ((IP_ADAPTER_UNICAST_ADDRESS_VISTA *)unicast)-> - OnLinkPrefixLength; - /* OnLinkPrefixLength is the number of leading 1s in the mask. - * OnLinkPrefixLength is available on Vista and later (hence use_enum2). - */ - if (unicast->Address.lpSockaddr->sa_family == AF_INET) - { - struct sockaddr_in *m = (struct sockaddr_in *)&r->mask; - for (i = 0; i < mask_length; i++) - ((unsigned char *)&m->sin_addr)[i / 8] |= 0x80 >> (i % 8); - } - else if (unicast->Address.lpSockaddr->sa_family == AF_INET6) - { - struct sockaddr_in6 *m = (struct sockaddr_in6 *)&r->mask; - struct sockaddr_in6 *b = (struct sockaddr_in6 *)&r->broadcast; - for (i = 0; i < mask_length; i++) - ((unsigned char *)&m->sin6_addr)[i / 8] |= 0x80 >> (i % 8); - GNUNET_memcpy(&r->broadcast, &r->address, unicast->Address.iSockaddrLength); - for (i = mask_length; i < 128; i++) - ((unsigned char *)&b->sin6_addr)[i / 8] |= 0x80 >> (i % 8); - } - r->flags |= ENUMNICS3_MASK_OK; - } - else - { - int found = 0; - if (unicast->Address.lpSockaddr->sa_family == AF_INET) - { - for (i = 0; !found && i < interfaces4_len / sizeof(INTERFACE_INFO); i++) - { - struct sockaddr_in *m = (struct sockaddr_in *)&r->mask; - GNUNET_memcpy(&interfaces4[i].iiAddress.Address, - unicast->Address.lpSockaddr, - unicast->Address.iSockaddrLength); - found = 1; - GNUNET_memcpy(&r->address, &interfaces4[i].iiAddress.Address, - sizeof(struct sockaddr_in)); - GNUNET_memcpy(&r->mask, &interfaces4[i].iiNetmask.Address, - sizeof(struct sockaddr_in)); - for (mask_length = 0; - ((unsigned char *)&m->sin_addr)[mask_length / 8] & - 0x80 >> (mask_length % 8); mask_length++) - { - } - r->flags |= ENUMNICS3_MASK_OK; - } - } - else if (unicast->Address.lpSockaddr->sa_family == AF_INET6) - { - for (i = 0; - interfaces6 != NULL && !found && i < interfaces6->iAddressCount; - i++) - { - GNUNET_memcpy(interfaces6->Address[i].lpSockaddr, - unicast->Address.lpSockaddr, - unicast->Address.iSockaddrLength); - found = 1; - GNUNET_memcpy(&r->address, interfaces6->Address[i].lpSockaddr, - sizeof(struct sockaddr_in6)); - /* TODO: Find a way to reliably get network mask for IPv6 on XP */ - memset(&r->mask, 0, sizeof(struct sockaddr)); - r->flags &= ~ENUMNICS3_MASK_OK; - } - } - if (!found) - { - DebugBreak(); - } - } - if (unicast->Address.lpSockaddr->sa_family == AF_INET) - { - struct sockaddr_in *m = (struct sockaddr_in *)&r->mask; - struct sockaddr_in *a = (struct sockaddr_in *)&r->address; - /* copy address to broadcast, then flip all the trailing bits not - * falling under netmask to 1, - * so we get, 192.168.0.255 from, say, 192.168.0.43 with mask == 24. - */ - GNUNET_memcpy(&r->broadcast, &r->address, unicast->Address.iSockaddrLength); - for (i = mask_length; i < 32; i++) - ((unsigned char *)&m->sin_addr)[i / 8] |= 0x80 >> (i % 8); - r->flags |= ENUMNICS3_BCAST_OK; - r->addr_size = sizeof(struct sockaddr_in); - inet_ntop(AF_INET, &a->sin_addr, dst, INET_ADDRSTRLEN); - } - else if (unicast->Address.lpSockaddr->sa_family == AF_INET6) - { - struct sockaddr_in6 *a = (struct sockaddr_in6 *)&r->address; - /* for IPv6 broadcast is not defined, zero it down */ - memset(&r->broadcast, 0, sizeof(struct sockaddr)); - r->flags &= ~ENUMNICS3_BCAST_OK; - r->addr_size = sizeof(struct sockaddr_in6); - inet_ntop(AF_INET6, &a->sin6_addr, dst, INET6_ADDRSTRLEN); - } - - i = 0; - i += snprintf(&r->pretty_name[i], 1000 - i > 0 ? 1000 - i : 0, - "%S (%s", pCurrentAddress->FriendlyName, dst); - for (j = 0; j < pCurrentAddress->PhysicalAddressLength; j++) - i += snprintf(&r->pretty_name[i], 1000 - i > 0 ? 1000 - i : 0, - "%s%02X", j > 0 ? ":" : " - ", pCurrentAddress->PhysicalAddress[j]); - i += snprintf(&r->pretty_name[i], 1000 - i > 0 ? 1000 - i : 0, ")"); - r->pretty_name[1000] = '\0'; - count += 1; - } - } - - if (use_enum2) - { - GNUNET_free_non_null(interfaces4); - GNUNET_free_non_null(interfaces6); - } - - GNUNET_free(pAddresses); - return GNUNET_OK; -} - -void -EnumNICs3_free(struct EnumNICs3_results *r) -{ - GNUNET_free_non_null(r); -} - - -/** - * Lists all network interfaces in a combo box - * Used by the basic GTK configurator - * - * @param callback function to call for each NIC - * @param callback_cls closure for callback - */ -int -ListNICs(void (*callback)(void *, const char *, int), void * callback_cls) -{ - int r; - int i; - struct EnumNICs3_results *results = NULL; - int results_count; - - r = EnumNICs3(&results, &results_count); - if (r != GNUNET_OK) - return GNUNET_NO; - - for (i = 0; i < results_count; i++) - callback(callback_cls, results[i].pretty_name, results[i].is_default); - GNUNET_free_non_null(results); - return GNUNET_YES; -} - -/** - * @brief Installs the Windows service - * @param servicename name of the service as diplayed by the SCM - * @param application path to the application binary - * @param username the name of the service's user account - * @returns 0 on success - * 1 if the Windows version doesn't support services - * 2 if the SCM could not be opened - * 3 if the service could not be created - */ -int InstallAsService(char *servicename, char *application, char *username) -{ - SC_HANDLE hManager, hService; - char szEXE[_MAX_PATH + 17] = "\""; - char *user = NULL; - - if (!GNOpenSCManager) - return 1; - - plibc_conv_to_win_path(application, szEXE + 1); - strcat(szEXE, "\" --win-service"); - hManager = GNOpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); - if (!hManager) - return 2; - - if (username) - { - user = (char *)malloc(strlen(username) + 3); - sprintf(user, ".\\%s", username); - } - - hService = GNCreateService(hManager, (LPCTSTR)servicename, (LPCTSTR)servicename, 0, - SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, (LPCTSTR)szEXE, - NULL, NULL, NULL, (LPCTSTR)user, (LPCTSTR)username); - - if (user) - free(user); - - if (!hService) - return 3; - - GNCloseServiceHandle(hService); - - return 0; -} - - -/** - * @brief Uninstall Windows service - * @param servicename name of the service to delete - * @returns 0 on success - * 1 if the Windows version doesn't support services - * 2 if the SCM could not be openend - * 3 if the service cannot be accessed - * 4 if the service cannot be deleted - */ -int -UninstallService(char *servicename) -{ - SC_HANDLE hManager, hService; - - if (!GNOpenSCManager) - return 1; - - hManager = GNOpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); - if (!hManager) - return 2; - - if (!(hService = GNOpenService(hManager, (LPCTSTR)servicename, DELETE))) - { - if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST) - return 3; - else - goto closeSCM; - } - - if (!GNDeleteService(hService)) - if (GetLastError() != ERROR_SERVICE_MARKED_FOR_DELETE) - return 4; - -closeSCM: - GNCloseServiceHandle(hService); - - return 0; -} - -/** - * @author Scott Field, Microsoft - * @see http://support.microsoft.com/?scid=kb;en-us;132958 - * @date 12-Jul-95 - */ -void -_InitLsaString(PLSA_UNICODE_STRING LsaString, LPWSTR String) -{ - DWORD StringLength; - - if (String == NULL) - { - LsaString->Buffer = NULL; - LsaString->Length = 0; - LsaString->MaximumLength = 0; - return; - } - - StringLength = wcslen(String); - LsaString->Buffer = String; - LsaString->Length = (USHORT)StringLength * sizeof(WCHAR); - LsaString->MaximumLength = (USHORT)(StringLength + 1) * sizeof(WCHAR); -} - - -/** - * @author Scott Field, Microsoft - * @see http://support.microsoft.com/?scid=kb;en-us;132958 - * @date 12-Jul-95 - */ -NTSTATUS -_OpenPolicy(LPWSTR ServerName, DWORD DesiredAccess, PLSA_HANDLE PolicyHandle) -{ - LSA_OBJECT_ATTRIBUTES ObjectAttributes; - LSA_UNICODE_STRING ServerString; - PLSA_UNICODE_STRING Server = NULL; - - /* Always initialize the object attributes to all zeroes. */ - ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes)); - - if (ServerName != NULL) - { - /* Make a LSA_UNICODE_STRING out of the LPWSTR passed in */ - _InitLsaString(&ServerString, ServerName); - Server = &ServerString; - } - - /* Attempt to open the policy. */ - return GNLsaOpenPolicy(Server, - &ObjectAttributes, DesiredAccess, PolicyHandle); -} - -/** - * @brief Obtain a SID representing the supplied account on the supplied system - * @return TRUE on success, FALSE on failure - * @author Scott Field, Microsoft - * @date 12-Jul-95 - * @remarks A buffer is allocated which contains the SID representing the - * supplied account. This buffer should be freed when it is no longer - * needed by calling\n - * HeapFree(GetProcessHeap(), 0, buffer) - * @remarks Call GetLastError() to obtain extended error information. - * @see http://support.microsoft.com/?scid=kb;en-us;132958 - */ -BOOL -_GetAccountSid(LPCTSTR SystemName, LPCTSTR AccountName, PSID * Sid) -{ - LPTSTR ReferencedDomain = NULL; - DWORD cbSid = 128; /* initial allocation attempt */ - DWORD cchReferencedDomain = 16; /* initial allocation size */ - SID_NAME_USE peUse; - BOOL bSuccess = FALSE; /* assume this function will fail */ - - /* initial memory allocations */ - if ((*Sid = HeapAlloc(GetProcessHeap(), 0, cbSid)) == NULL) - return FALSE; - - if ((ReferencedDomain = (LPTSTR)HeapAlloc(GetProcessHeap(), - 0, - cchReferencedDomain * - sizeof(TCHAR))) == NULL) - return FALSE; - - /* Obtain the SID of the specified account on the specified system. */ - while (!GNLookupAccountName(SystemName, /* machine to lookup account on */ - AccountName, /* account to lookup */ - *Sid, /* SID of interest */ - &cbSid, /* size of SID */ - ReferencedDomain, /* domain account was found on */ - &cchReferencedDomain, &peUse)) - { - if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) - { - /* reallocate memory */ - if ((*Sid = HeapReAlloc(GetProcessHeap(), 0, *Sid, cbSid)) == NULL) - return FALSE; - - if ((ReferencedDomain = (LPTSTR)HeapReAlloc(GetProcessHeap(), - 0, - ReferencedDomain, - cchReferencedDomain - * sizeof(TCHAR))) == NULL) - return FALSE; - } - else - goto end; - } - - /* Indicate success. */ - bSuccess = TRUE; - -end: - /* Cleanup and indicate failure, if appropriate. */ - HeapFree(GetProcessHeap(), 0, ReferencedDomain); - - if (!bSuccess) - { - if (*Sid != NULL) - { - HeapFree(GetProcessHeap(), 0, *Sid); - *Sid = NULL; - } - } - - return bSuccess; -} - -/** - * @author Scott Field, Microsoft - * @see http://support.microsoft.com/?scid=kb;en-us;132958 - * @date 12-Jul-95 - */ -NTSTATUS -_SetPrivilegeOnAccount(LSA_HANDLE PolicyHandle,/* open policy handle */ - PSID AccountSid, /* SID to grant privilege to */ - LPWSTR PrivilegeName, /* privilege to grant (Unicode) */ - BOOL bEnable /* enable or disable */ - ) -{ - LSA_UNICODE_STRING PrivilegeString; - - /* Create a LSA_UNICODE_STRING for the privilege name. */ - _InitLsaString(&PrivilegeString, PrivilegeName); - - /* grant or revoke the privilege, accordingly */ - if (bEnable) - { - NTSTATUS i; - - i = GNLsaAddAccountRights(PolicyHandle, /* open policy handle */ - AccountSid, /* target SID */ - &PrivilegeString, /* privileges */ - 1 /* privilege count */ - ); - return i; - } - else - { - return GNLsaRemoveAccountRights(PolicyHandle, /* open policy handle */ - AccountSid, /* target SID */ - FALSE, /* do not disable all rights */ - &PrivilegeString, /* privileges */ - 1 /* privilege count */ - ); - } -} - -/** - * @brief Create a Windows service account - * @return 0 on success, > 0 otherwise - * @param pszName the name of the account - * @param pszDesc description of the account - */ -int -CreateServiceAccount(const char *pszName, - const char *pszDesc) -{ - USER_INFO_1 ui; - USER_INFO_1008 ui2; - NET_API_STATUS nStatus; - wchar_t wszName[MAX_NAME_LENGTH], wszDesc[MAX_NAME_LENGTH]; - LSA_HANDLE hPolicy; - PSID pSID; - - if (!GNNetUserAdd) - return 1; - mbstowcs(wszName, pszName, strlen(pszName) + 1); - mbstowcs(wszDesc, pszDesc, strlen(pszDesc) + 1); - - memset(&ui, 0, sizeof(ui)); - ui.usri1_name = wszName; - ui.usri1_password = wszName; /* account is locked anyway */ - ui.usri1_priv = USER_PRIV_USER; - ui.usri1_comment = wszDesc; - ui.usri1_flags = UF_SCRIPT; - - nStatus = GNNetUserAdd(NULL, 1, (LPBYTE)&ui, NULL); - - if (nStatus != NERR_Success && nStatus != NERR_UserExists) - return 2; - - ui2.usri1008_flags = UF_PASSWD_CANT_CHANGE | UF_DONT_EXPIRE_PASSWD; - GNNetUserSetInfo(NULL, wszName, 1008, (LPBYTE)&ui2, NULL); - - if (!NT_SUCCESS(_OpenPolicy(NULL, POLICY_ALL_ACCESS, &hPolicy))) - return 3; - - _GetAccountSid(NULL, (LPCTSTR)pszName, &pSID); - - if (!NT_SUCCESS(_SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR)L"SeServiceLogonRight", TRUE))) - return 4; - - _SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR)L"SeDenyInteractiveLogonRight", TRUE); - _SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR)L"SeDenyBatchLogonRight", TRUE); - _SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR)L"SeDenyNetworkLogonRight", TRUE); - - GNLsaClose(hPolicy); - - return 0; -} - -/** - * @brief Grant permission to a file - * @param lpszFileName the name of the file or directory - * @param lpszAccountName the user account - * @param dwAccessMask the desired access (e.g. GENERIC_ALL) - * @return TRUE on success - * @remark based on http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q102102& - */ -BOOL AddPathAccessRights(char *lpszFileName, char *lpszAccountName, - DWORD dwAccessMask) -{ - /* SID variables. */ - SID_NAME_USE snuType; - TCHAR * szDomain = NULL; - DWORD cbDomain = 0; - LPVOID pUserSID = NULL; - DWORD cbUserSID = 0; - - /* File SD variables. */ - PSECURITY_DESCRIPTOR pFileSD = NULL; - DWORD cbFileSD = 0; - - /* New SD variables. */ - SECURITY_DESCRIPTOR newSD; - - /* ACL variables. */ - PACL pACL = NULL; - BOOL fDaclPresent; - BOOL fDaclDefaulted; - ACL_SIZE_INFORMATION AclInfo; - - /* New ACL variables. */ - PACL pNewACL = NULL; - DWORD cbNewACL = 0; - - /* Temporary ACE. */ - LPVOID pTempAce = NULL; - UINT CurrentAceIndex = 0; - - UINT newAceIndex = 0; - - /* Assume function will fail. */ - BOOL fResult = FALSE; - BOOL fAPISuccess; - - SECURITY_INFORMATION secInfo = DACL_SECURITY_INFORMATION; - - /** - * STEP 1: Get SID of the account name specified. - */ - fAPISuccess = GNLookupAccountName(NULL, (LPCTSTR)lpszAccountName, - pUserSID, &cbUserSID, (LPTSTR)szDomain, &cbDomain, &snuType); - - /* API should have failed with insufficient buffer. */ - if (fAPISuccess) - goto end; - else if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) - { - goto end; - } - - pUserSID = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbUserSID); - if (!pUserSID) - { - goto end; - } - - szDomain = (TCHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbDomain * sizeof(TCHAR)); - if (!szDomain) - { - goto end; - } - - fAPISuccess = GNLookupAccountName(NULL, (LPCTSTR)lpszAccountName, - pUserSID, &cbUserSID, (LPTSTR)szDomain, &cbDomain, &snuType); - if (!fAPISuccess) - { - goto end; - } - - /** - * STEP 2: Get security descriptor (SD) of the file specified. - */ - fAPISuccess = GNGetFileSecurity((LPCTSTR)lpszFileName, - secInfo, pFileSD, 0, &cbFileSD); - - /* API should have failed with insufficient buffer. */ - if (fAPISuccess) - goto end; - else if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) - { - goto end; - } - - pFileSD = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, - cbFileSD); - if (!pFileSD) - { - goto end; - } - - fAPISuccess = GNGetFileSecurity((LPCTSTR)lpszFileName, - secInfo, pFileSD, cbFileSD, &cbFileSD); - if (!fAPISuccess) - { - goto end; - } - - /** - * STEP 3: Initialize new SD. - */ - if (!GNInitializeSecurityDescriptor(&newSD, - SECURITY_DESCRIPTOR_REVISION)) - { - goto end; - } - - /** - * STEP 4: Get DACL from the old SD. - */ - if (!GNGetSecurityDescriptorDacl(pFileSD, &fDaclPresent, &pACL, - &fDaclDefaulted)) - { - goto end; - } - - /** - * STEP 5: Get size information for DACL. - */ - AclInfo.AceCount = 0; // Assume NULL DACL. - AclInfo.AclBytesFree = 0; - AclInfo.AclBytesInUse = sizeof(ACL); - - if (pACL == NULL) - fDaclPresent = FALSE; - - /* If not NULL DACL, gather size information from DACL. */ - if (fDaclPresent) - { - if (!GNGetAclInformation(pACL, &AclInfo, - sizeof(ACL_SIZE_INFORMATION), AclSizeInformation)) - { - goto end; - } - } - - /** - * STEP 6: Compute size needed for the new ACL. - */ - cbNewACL = AclInfo.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE) - + GetLengthSid(pUserSID) - sizeof(DWORD); - - /** - * STEP 7: Allocate memory for new ACL. - */ - pNewACL = (PACL)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbNewACL); - if (!pNewACL) - { - goto end; - } - - /** - * STEP 8: Initialize the new ACL. - */ - if (!GNInitializeAcl(pNewACL, cbNewACL, ACL_REVISION2)) - { - goto end; - } - - /** - * STEP 9 If DACL is present, copy all the ACEs from the old DACL - * to the new DACL. - * - * The following code assumes that the old DACL is - * already in Windows 2000 preferred order. To conform - * to the new Windows 2000 preferred order, first we will - * copy all non-inherited ACEs from the old DACL to the - * new DACL, irrespective of the ACE type. - */ - - newAceIndex = 0; - - if (fDaclPresent && AclInfo.AceCount) - { - for (CurrentAceIndex = 0; - CurrentAceIndex < AclInfo.AceCount; - CurrentAceIndex++) - { - /** - * TEP 10: Get an ACE. - */ - if (!GNGetAce(pACL, CurrentAceIndex, &pTempAce)) - { - goto end; - } - - /** - * STEP 11: Check if it is a non-inherited ACE. - * If it is an inherited ACE, break from the loop so - * that the new access allowed non-inherited ACE can - * be added in the correct position, immediately after - * all non-inherited ACEs. - */ - if (((ACCESS_ALLOWED_ACE *)pTempAce)->Header.AceFlags - & INHERITED_ACE) - break; - - /** - * STEP 12: Skip adding the ACE, if the SID matches - * with the account specified, as we are going to - * add an access allowed ACE with a different access - * mask. - */ - if (GNEqualSid(pUserSID, - &(((ACCESS_ALLOWED_ACE *)pTempAce)->SidStart))) - continue; - - /** - * STEP 13: Add the ACE to the new ACL. - */ - if (!GNAddAce(pNewACL, ACL_REVISION, MAXDWORD, pTempAce, - ((PACE_HEADER)pTempAce)->AceSize)) - { - goto end; - } - - newAceIndex++; - } - } - - /** - * STEP 14: Add the access-allowed ACE to the new DACL. - * The new ACE added here will be in the correct position, - * immediately after all existing non-inherited ACEs. - */ - if (!GNAddAccessAllowedAce(pNewACL, ACL_REVISION2, dwAccessMask, - pUserSID)) - { - goto end; - } - - /** - * STEP 14.5: Make new ACE inheritable - */ - if (!GetAce(pNewACL, newAceIndex, &pTempAce)) - goto end; - ((ACCESS_ALLOWED_ACE *)pTempAce)->Header.AceFlags |= - (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE); - - /** - * STEP 15: To conform to the new Windows 2000 preferred order, - * we will now copy the rest of inherited ACEs from the - * old DACL to the new DACL. - */ - if (fDaclPresent && AclInfo.AceCount) - { - for (; - CurrentAceIndex < AclInfo.AceCount; - CurrentAceIndex++) - { - /** - * STEP 16: Get an ACE. - */ - if (!GNGetAce(pACL, CurrentAceIndex, &pTempAce)) - { - goto end; - } - - /** - * STEP 17: Add the ACE to the new ACL. - */ - if (!GNAddAce(pNewACL, ACL_REVISION, MAXDWORD, pTempAce, - ((PACE_HEADER)pTempAce)->AceSize)) - { - goto end; - } - } - } - - /** - * STEP 18: Set permissions - */ - if (GNSetNamedSecurityInfo((LPTSTR)lpszFileName, SE_FILE_OBJECT, - DACL_SECURITY_INFORMATION, NULL, NULL, pNewACL, NULL) != ERROR_SUCCESS) - { - goto end; - } - - fResult = TRUE; - -end: - - /** - * STEP 19: Free allocated memory - */ - if (pUserSID) - HeapFree(GetProcessHeap(), 0, pUserSID); - - if (szDomain) - HeapFree(GetProcessHeap(), 0, szDomain); - - if (pFileSD) - HeapFree(GetProcessHeap(), 0, pFileSD); - - if (pNewACL) - HeapFree(GetProcessHeap(), 0, pNewACL); - - return fResult; -} - -char *winErrorStr(const char *prefix, int dwErr) -{ - char *err, *ret; - int mem; - - if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, (DWORD)dwErr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&err, - 0, NULL)) - { - err = (char *)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, 1); - } - - mem = strlen(err) + strlen(prefix) + 20; - ret = (char *)malloc(mem); - - snprintf(ret, mem, "%s: %s (#%u)", prefix, err, dwErr); - - LocalFree(err); - - return ret; -} - -/** - * Terminate a process by creating a remote thread within it, - * which proceeds to call ExitProcess() inside that process. - * Safer than TerminateProcess (). - * - * Code is from From http://private-storm.de/2009/08/11/case-terminateprocess/ - * - * @param hProcess handle of a process to terminate - * @param uExitCode exit code to use for ExitProcess() - * @param dwTimeout number of ms to wait for the process to terminate - * @return TRUE on success, FALSE on failure (check last error for the code) - */ -BOOL -SafeTerminateProcess(HANDLE hProcess, UINT uExitCode, DWORD dwTimeout) -{ - DWORD dwTID, dwCode, dwErr = 0; - HANDLE hProcessDup = INVALID_HANDLE_VALUE; - HANDLE hRT = NULL; - HINSTANCE hKernel = GetModuleHandle("Kernel32"); - BOOL bSuccess = FALSE; - - BOOL bDup = DuplicateHandle(GetCurrentProcess(), hProcess, - GetCurrentProcess(), &hProcessDup, PROCESS_ALL_ACCESS, - FALSE, 0); - - /* Detect the special case where the process is - * already dead... - */ - if (GetExitCodeProcess(bDup ? hProcessDup : hProcess, &dwCode) && - (STILL_ACTIVE == dwCode)) - { - FARPROC pfnExitProc; - - pfnExitProc = GetProcAddress(hKernel, "ExitProcess"); - - hRT = CreateRemoteThread((bDup) ? hProcessDup : hProcess, NULL, 0, - (LPTHREAD_START_ROUTINE)pfnExitProc, (PVOID)uExitCode, 0, &dwTID); - - dwErr = GetLastError(); - } - else - { - dwErr = ERROR_PROCESS_ABORTED; - } - - if (hRT) - { - /* Must wait process to terminate to - * guarantee that it has exited... - */ - DWORD dwWaitResult = WaitForSingleObject((bDup) ? hProcessDup : hProcess, - dwTimeout); - if (dwWaitResult == WAIT_TIMEOUT) - dwErr = WAIT_TIMEOUT; - else - dwErr = GetLastError(); - - CloseHandle(hRT); - bSuccess = dwErr == NO_ERROR; - } - - if (bDup) - CloseHandle(hProcessDup); - - SetLastError(dwErr); - - return bSuccess; -} - -#endif diff --git a/src/util/winproc.c b/src/util/winproc.c deleted file mode 100644 index f2e4de4df..000000000 --- a/src/util/winproc.c +++ /dev/null @@ -1,340 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 GNUnet e.V. - - GNUnet is free software: you can redistribute it and/or modify it - under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, - or (at your option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file util/winproc.c - * @brief Functions for MS Windows - * @author Nils Durner - */ - -#include "platform.h" -#include "gnunet_crypto_lib.h" -#include "gnunet_common.h" - - -#ifdef MINGW - -static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi; -#ifdef W32_VEH -static void *GNWinVEH_handle = NULL; -#endif - -TNtQuerySystemInformation GNNtQuerySystemInformation; -TGetIfEntry GNGetIfEntry; -TGetIpAddrTable GNGetIpAddrTable; -TGetIfTable GNGetIfTable; -TOpenSCManager GNOpenSCManager; -TCreateService GNCreateService; -TCloseServiceHandle GNCloseServiceHandle; -TDeleteService GNDeleteService; -TRegisterServiceCtrlHandler GNRegisterServiceCtrlHandler; -TSetServiceStatus GNSetServiceStatus; -TStartServiceCtrlDispatcher GNStartServiceCtrlDispatcher; -TControlService GNControlService; -TOpenService GNOpenService; -TGetBestInterfaceEx GNGetBestInterfaceEx; -TGetAdaptersInfo GNGetAdaptersInfo; -TNetUserAdd GNNetUserAdd; -TNetUserSetInfo GNNetUserSetInfo; -TLsaOpenPolicy GNLsaOpenPolicy; -TLsaAddAccountRights GNLsaAddAccountRights; -TLsaRemoveAccountRights GNLsaRemoveAccountRights; -TLsaClose GNLsaClose; -TLookupAccountName GNLookupAccountName; -TGetFileSecurity GNGetFileSecurity; -TInitializeSecurityDescriptor GNInitializeSecurityDescriptor; -TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl; -TGetAclInformation GNGetAclInformation; -TInitializeAcl GNInitializeAcl; -TGetAce GNGetAce; -TEqualSid GNEqualSid; -TAddAce GNAddAce; -TAddAccessAllowedAce GNAddAccessAllowedAce; -TSetNamedSecurityInfo GNSetNamedSecurityInfo; - -#define LOG(kind, ...) GNUNET_log_from(kind, "util-winproc", __VA_ARGS__) -/** - * Log (panic) messages from PlibC - */ -void -plibc_panic(int err, char *msg) -{ - LOG(((err == INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR), - "%s", msg); -} - -#ifdef W32_VEH -/** - * Handles exceptions (useful for debugging). - * Issues a DebugBreak() call if the process is being debugged (not really - * useful - if the process is being debugged, this handler won't be invoked - * anyway). If it is not, runs a debugger from GNUNET_DEBUGGER env var, - * substituting first %u in it for PID, and the second one for the event, - * that should be set once the debugger attaches itself (otherwise the - * only way out of WaitForSingleObject() is to time out after 1 minute). - */ -LONG __stdcall -GNWinVEH(PEXCEPTION_POINTERS ExceptionInfo) -{ - char debugger[MAX_PATH + 1]; - char *debugger_env = NULL; - - if (IsDebuggerPresent()) - { - DebugBreak(); - return EXCEPTION_CONTINUE_EXECUTION; - } - debugger_env = getenv("GNUNET_DEBUGGER"); - if (debugger_env != NULL) - { - STARTUPINFO si; - PROCESS_INFORMATION pi; - HANDLE event; - SECURITY_ATTRIBUTES sa; - memset(&si, 0, sizeof(si)); - si.cb = sizeof(si); - memset(&pi, 0, sizeof(pi)); - memset(&sa, 0, sizeof(sa)); - sa.nLength = sizeof(sa); - sa.bInheritHandle = TRUE; - event = CreateEvent(&sa, FALSE, FALSE, NULL); - snprintf(debugger, MAX_PATH + 1, debugger_env, GetCurrentProcessId(), (uintptr_t)event); - debugger[MAX_PATH] = '\0'; - if (0 != CreateProcessA(NULL, debugger, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) - { - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - WaitForSingleObject(event, 60000); - CloseHandle(event); - if (IsDebuggerPresent()) - { - return EXCEPTION_CONTINUE_EXECUTION; - } - } - else - CloseHandle(event); - } - return EXCEPTION_CONTINUE_SEARCH; -} -#endif - -/** - * @brief Initialize PlibC and set up Windows environment - * @param logging context, NULL means stderr - * @return Error code from winerror.h, ERROR_SUCCESS on success - */ -int -GNInitWinEnv() -{ - int ret; - - plibc_initialized(); - plibc_set_panic_proc(plibc_panic); - ret = plibc_init_utf8("GNU", PACKAGE, 1); - plibc_set_stat_size_size(sizeof(((struct stat *)0)->st_size)); - plibc_set_stat_time_size(sizeof(((struct stat *)0)->st_mtime)); - /* don't load other DLLs twice */ - if (hNTDLL) - return ret; - -#ifdef W32_VEH - if (GNWinVEH_handle == NULL) - { - GNWinVEH_handle = AddVectoredExceptionHandler(1, &GNWinVEH); - if (GNWinVEH_handle == NULL) - { - /* This is bad, but what can we do? */ - printf("Failed to set up an exception handler!\n"); - } - } -#endif - - hNTDLL = LoadLibrary("ntdll.dll"); - - /* Function to get CPU usage under Win NT */ - if (hNTDLL) - { - GNNtQuerySystemInformation = - (TNtQuerySystemInformation)GetProcAddress(hNTDLL, - "NtQuerySystemInformation"); - } - else - { - GNNtQuerySystemInformation = NULL; - } - - /* Functions to get information about a network adapter */ - hIphlpapi = LoadLibrary("iphlpapi.dll"); - if (hIphlpapi) - { - GNGetIfEntry = (TGetIfEntry)GetProcAddress(hIphlpapi, "GetIfEntry"); - GNGetIpAddrTable = - (TGetIpAddrTable)GetProcAddress(hIphlpapi, "GetIpAddrTable"); - GNGetIfTable = (TGetIfTable)GetProcAddress(hIphlpapi, "GetIfTable"); - GNGetBestInterfaceEx = - (TGetBestInterfaceEx)GetProcAddress(hIphlpapi, "GetBestInterfaceEx"); - GNGetAdaptersInfo = - (TGetAdaptersInfo)GetProcAddress(hIphlpapi, "GetAdaptersInfo"); - } - else - { - GNGetIfEntry = NULL; - GNGetIpAddrTable = NULL; - GNGetIfTable = NULL; - GNGetBestInterfaceEx = NULL; - GNGetAdaptersInfo = NULL; - } - - /* Service & Account functions */ - hAdvapi = LoadLibrary("advapi32.dll"); - if (hAdvapi) - { - GNOpenSCManager = - (TOpenSCManager)GetProcAddress(hAdvapi, "OpenSCManagerA"); - GNCreateService = - (TCreateService)GetProcAddress(hAdvapi, "CreateServiceA"); - GNCloseServiceHandle = - (TCloseServiceHandle)GetProcAddress(hAdvapi, "CloseServiceHandle"); - GNDeleteService = - (TDeleteService)GetProcAddress(hAdvapi, "DeleteService"); - GNRegisterServiceCtrlHandler = - (TRegisterServiceCtrlHandler)GetProcAddress(hAdvapi, - "RegisterServiceCtrlHandlerA"); - GNSetServiceStatus = - (TSetServiceStatus)GetProcAddress(hAdvapi, "SetServiceStatus"); - GNStartServiceCtrlDispatcher = - (TStartServiceCtrlDispatcher)GetProcAddress(hAdvapi, - "StartServiceCtrlDispatcherA"); - GNControlService = - (TControlService)GetProcAddress(hAdvapi, "ControlService"); - GNOpenService = (TOpenService)GetProcAddress(hAdvapi, "OpenServiceA"); - - GNLsaOpenPolicy = - (TLsaOpenPolicy)GetProcAddress(hAdvapi, "LsaOpenPolicy"); - GNLsaAddAccountRights = - (TLsaAddAccountRights)GetProcAddress(hAdvapi, "LsaAddAccountRights"); - GNLsaRemoveAccountRights = - (TLsaRemoveAccountRights)GetProcAddress(hAdvapi, - "LsaRemoveAccountRights"); - GNLsaClose = (TLsaClose)GetProcAddress(hAdvapi, "LsaClose"); - GNLookupAccountName = - (TLookupAccountName)GetProcAddress(hAdvapi, "LookupAccountNameA"); - - GNGetFileSecurity = - (TGetFileSecurity)GetProcAddress(hAdvapi, "GetFileSecurityA"); - GNInitializeSecurityDescriptor = - (TInitializeSecurityDescriptor)GetProcAddress(hAdvapi, - "InitializeSecurityDescriptor"); - GNGetSecurityDescriptorDacl = - (TGetSecurityDescriptorDacl)GetProcAddress(hAdvapi, - "GetSecurityDescriptorDacl"); - GNGetAclInformation = - (TGetAclInformation)GetProcAddress(hAdvapi, "GetAclInformation"); - GNInitializeAcl = - (TInitializeAcl)GetProcAddress(hAdvapi, "InitializeAcl"); - GNGetAce = (TGetAce)GetProcAddress(hAdvapi, "GetAce"); - GNEqualSid = (TEqualSid)GetProcAddress(hAdvapi, "EqualSid"); - GNAddAce = (TAddAce)GetProcAddress(hAdvapi, "AddAce"); - GNAddAccessAllowedAce = - (TAddAccessAllowedAce)GetProcAddress(hAdvapi, "AddAccessAllowedAce"); - GNSetNamedSecurityInfo = - (TSetNamedSecurityInfo)GetProcAddress(hAdvapi, - "SetNamedSecurityInfoA"); - } - else - { - GNOpenSCManager = NULL; - GNCreateService = NULL; - GNCloseServiceHandle = NULL; - GNDeleteService = NULL; - GNRegisterServiceCtrlHandler = NULL; - GNSetServiceStatus = NULL; - GNStartServiceCtrlDispatcher = NULL; - GNControlService = NULL; - GNOpenService = NULL; - - GNLsaOpenPolicy = NULL; - GNLsaAddAccountRights = NULL; - GNLsaRemoveAccountRights = NULL; - GNLsaClose = NULL; - GNLookupAccountName = NULL; - - GNGetFileSecurity = NULL; - GNInitializeSecurityDescriptor = NULL; - GNGetSecurityDescriptorDacl = NULL; - GNGetAclInformation = NULL; - GNInitializeAcl = NULL; - GNGetAce = NULL; - GNEqualSid = NULL; - GNAddAce = NULL; - GNAddAccessAllowedAce = NULL; - GNSetNamedSecurityInfo = NULL; - } - - /* Account function */ - hNetapi = LoadLibrary("netapi32.dll"); - if (hNetapi) - { - GNNetUserAdd = (TNetUserAdd)GetProcAddress(hNetapi, "NetUserAdd"); - GNNetUserSetInfo = - (TNetUserSetInfo)GetProcAddress(hNetapi, "NetUserSetInfo"); - } - else - { - GNNetUserAdd = NULL; - GNNetUserSetInfo = NULL; - } - - return ret; -} - -/** - * Clean up Windows environment - */ -void -GNShutdownWinEnv() -{ - plibc_shutdown(); - -#ifdef W32_VEH - if (GNWinVEH_handle != NULL) - { - RemoveVectoredExceptionHandler(GNWinVEH_handle); - GNWinVEH_handle = NULL; - } -#endif - - FreeLibrary(hNTDLL); - FreeLibrary(hIphlpapi); - FreeLibrary(hAdvapi); - FreeLibrary(hNetapi); - - CoUninitialize(); -} - -#endif /* MINGW */ - -#if !HAVE_ATOLL -long long -atoll(const char *nptr) -{ - return atol(nptr); -} -#endif -- cgit v1.2.3