diff options
Diffstat (limited to 'src/backend/port/win32')
-rw-r--r-- | src/backend/port/win32/error.c | 202 | ||||
-rw-r--r-- | src/backend/port/win32/security.c | 134 | ||||
-rw-r--r-- | src/backend/port/win32/sema.c | 8 | ||||
-rw-r--r-- | src/backend/port/win32/shmem.c | 6 | ||||
-rw-r--r-- | src/backend/port/win32/signal.c | 28 | ||||
-rw-r--r-- | src/backend/port/win32/socket.c | 332 | ||||
-rw-r--r-- | src/backend/port/win32/timer.c | 24 |
7 files changed, 455 insertions, 279 deletions
diff --git a/src/backend/port/win32/error.c b/src/backend/port/win32/error.c index 28bf7bc9d9d..e0eb2fab467 100644 --- a/src/backend/port/win32/error.c +++ b/src/backend/port/win32/error.c @@ -1,70 +1,166 @@ /*------------------------------------------------------------------------- * * error.c - * Map win32 error codes to errno values + * Map win32 error codes to errno values * * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/error.c,v 1.2 2004/08/29 04:12:46 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/error.c,v 1.3 2004/08/29 05:06:46 momjian Exp $ * *------------------------------------------------------------------------- */ #include "postgres.h" -static struct { DWORD winerr; int doserr;} doserrors[] = +static struct { - { ERROR_INVALID_FUNCTION, EINVAL }, - { ERROR_FILE_NOT_FOUND, ENOENT }, - { ERROR_PATH_NOT_FOUND, ENOENT }, - { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, - { ERROR_ACCESS_DENIED, EACCES }, - { ERROR_INVALID_HANDLE, EBADF }, - { ERROR_ARENA_TRASHED, ENOMEM }, - { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, - { ERROR_INVALID_BLOCK, ENOMEM }, - { ERROR_BAD_ENVIRONMENT, E2BIG }, - { ERROR_BAD_FORMAT, ENOEXEC }, - { ERROR_INVALID_ACCESS, EINVAL }, - { ERROR_INVALID_DATA, EINVAL }, - { ERROR_INVALID_DRIVE, ENOENT }, - { ERROR_CURRENT_DIRECTORY, EACCES }, - { ERROR_NOT_SAME_DEVICE, EXDEV }, - { ERROR_NO_MORE_FILES, ENOENT }, - { ERROR_LOCK_VIOLATION, EACCES }, - { ERROR_BAD_NETPATH, ENOENT }, - { ERROR_NETWORK_ACCESS_DENIED, EACCES }, - { ERROR_BAD_NET_NAME, ENOENT }, - { ERROR_FILE_EXISTS, EEXIST }, - { ERROR_CANNOT_MAKE, EACCES }, - { ERROR_FAIL_I24, EACCES }, - { ERROR_INVALID_PARAMETER, EINVAL }, - { ERROR_NO_PROC_SLOTS, EAGAIN }, - { ERROR_DRIVE_LOCKED, EACCES }, - { ERROR_BROKEN_PIPE, EPIPE }, - { ERROR_DISK_FULL, ENOSPC }, - { ERROR_INVALID_TARGET_HANDLE, EBADF }, - { ERROR_INVALID_HANDLE, EINVAL }, - { ERROR_WAIT_NO_CHILDREN, ECHILD }, - { ERROR_CHILD_NOT_COMPLETE, ECHILD }, - { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, - { ERROR_NEGATIVE_SEEK, EINVAL }, - { ERROR_SEEK_ON_DEVICE, EACCES }, - { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, - { ERROR_NOT_LOCKED, EACCES }, - { ERROR_BAD_PATHNAME, ENOENT }, - { ERROR_MAX_THRDS_REACHED, EAGAIN }, - { ERROR_LOCK_FAILED, EACCES }, - { ERROR_ALREADY_EXISTS, EEXIST }, - { ERROR_FILENAME_EXCED_RANGE, ENOENT }, - { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, - { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } + DWORD winerr; + int doserr; +} doserrors[] = + +{ + { + ERROR_INVALID_FUNCTION, EINVAL + }, + { + ERROR_FILE_NOT_FOUND, ENOENT + }, + { + ERROR_PATH_NOT_FOUND, ENOENT + }, + { + ERROR_TOO_MANY_OPEN_FILES, EMFILE + }, + { + ERROR_ACCESS_DENIED, EACCES + }, + { + ERROR_INVALID_HANDLE, EBADF + }, + { + ERROR_ARENA_TRASHED, ENOMEM + }, + { + ERROR_NOT_ENOUGH_MEMORY, ENOMEM + }, + { + ERROR_INVALID_BLOCK, ENOMEM + }, + { + ERROR_BAD_ENVIRONMENT, E2BIG + }, + { + ERROR_BAD_FORMAT, ENOEXEC + }, + { + ERROR_INVALID_ACCESS, EINVAL + }, + { + ERROR_INVALID_DATA, EINVAL + }, + { + ERROR_INVALID_DRIVE, ENOENT + }, + { + ERROR_CURRENT_DIRECTORY, EACCES + }, + { + ERROR_NOT_SAME_DEVICE, EXDEV + }, + { + ERROR_NO_MORE_FILES, ENOENT + }, + { + ERROR_LOCK_VIOLATION, EACCES + }, + { + ERROR_BAD_NETPATH, ENOENT + }, + { + ERROR_NETWORK_ACCESS_DENIED, EACCES + }, + { + ERROR_BAD_NET_NAME, ENOENT + }, + { + ERROR_FILE_EXISTS, EEXIST + }, + { + ERROR_CANNOT_MAKE, EACCES + }, + { + ERROR_FAIL_I24, EACCES + }, + { + ERROR_INVALID_PARAMETER, EINVAL + }, + { + ERROR_NO_PROC_SLOTS, EAGAIN + }, + { + ERROR_DRIVE_LOCKED, EACCES + }, + { + ERROR_BROKEN_PIPE, EPIPE + }, + { + ERROR_DISK_FULL, ENOSPC + }, + { + ERROR_INVALID_TARGET_HANDLE, EBADF + }, + { + ERROR_INVALID_HANDLE, EINVAL + }, + { + ERROR_WAIT_NO_CHILDREN, ECHILD + }, + { + ERROR_CHILD_NOT_COMPLETE, ECHILD + }, + { + ERROR_DIRECT_ACCESS_HANDLE, EBADF + }, + { + ERROR_NEGATIVE_SEEK, EINVAL + }, + { + ERROR_SEEK_ON_DEVICE, EACCES + }, + { + ERROR_DIR_NOT_EMPTY, ENOTEMPTY + }, + { + ERROR_NOT_LOCKED, EACCES + }, + { + ERROR_BAD_PATHNAME, ENOENT + }, + { + ERROR_MAX_THRDS_REACHED, EAGAIN + }, + { + ERROR_LOCK_FAILED, EACCES + }, + { + ERROR_ALREADY_EXISTS, EEXIST + }, + { + ERROR_FILENAME_EXCED_RANGE, ENOENT + }, + { + ERROR_NESTING_NOT_ALLOWED, EAGAIN + }, + { + ERROR_NOT_ENOUGH_QUOTA, ENOMEM + } }; -void _dosmaperr(unsigned long e) +void +_dosmaperr(unsigned long e) { - int i; + int i; if (e == 0) { @@ -72,21 +168,21 @@ void _dosmaperr(unsigned long e) return; } - for (i=0; i<sizeof(doserrors)/sizeof(doserrors[0]); i++) + for (i = 0; i < sizeof(doserrors) / sizeof(doserrors[0]); i++) { if (doserrors[i].winerr == e) { errno = doserrors[i].doserr; ereport(DEBUG5, (errmsg_internal("Mapped win32 error code %i to %i", - (int)e, errno))); + (int) e, errno))); return; } } ereport(DEBUG4, (errmsg_internal("Unknown win32 error code: %i", - (int)e))); + (int) e))); errno = EINVAL; return; } diff --git a/src/backend/port/win32/security.c b/src/backend/port/win32/security.c index 056e9caf533..f546b9cd8a0 100644 --- a/src/backend/port/win32/security.c +++ b/src/backend/port/win32/security.c @@ -1,12 +1,12 @@ /*------------------------------------------------------------------------- * * security.c - * Microsoft Windows Win32 Security Support Functions + * Microsoft Windows Win32 Security Support Functions * * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/security.c,v 1.3 2004/08/29 04:12:46 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/security.c,v 1.4 2004/08/29 05:06:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -24,24 +24,24 @@ int pgwin32_is_admin(void) { - HANDLE AccessToken; - char *InfoBuffer = NULL; + HANDLE AccessToken; + char *InfoBuffer = NULL; PTOKEN_GROUPS Groups; - DWORD InfoBufferSize; - PSID AdministratorsSid; - PSID PowerUsersSid; - SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY }; - UINT x; - BOOL success; - - if(!OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&AccessToken)) + DWORD InfoBufferSize; + PSID AdministratorsSid; + PSID PowerUsersSid; + SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY}; + UINT x; + BOOL success; + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) { write_stderr("failed to open process token: %d\n", - (int)GetLastError()); + (int) GetLastError()); exit(1); } - if (GetTokenInformation(AccessToken,TokenGroups,NULL,0,&InfoBufferSize)) + if (GetTokenInformation(AccessToken, TokenGroups, NULL, 0, &InfoBufferSize)) { write_stderr("failed to get token information - got zero size!\n"); exit(1); @@ -50,7 +50,7 @@ pgwin32_is_admin(void) if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { write_stderr("failed to get token information: %d\n", - (int)GetLastError()); + (int) GetLastError()); exit(1); } @@ -58,42 +58,42 @@ pgwin32_is_admin(void) if (!InfoBuffer) { write_stderr("failed to allocate %i bytes for token information!\n", - (int)InfoBufferSize); + (int) InfoBufferSize); exit(1); } - Groups = (PTOKEN_GROUPS)InfoBuffer; + Groups = (PTOKEN_GROUPS) InfoBuffer; - if (!GetTokenInformation(AccessToken,TokenGroups,InfoBuffer, + if (!GetTokenInformation(AccessToken, TokenGroups, InfoBuffer, InfoBufferSize, &InfoBufferSize)) { write_stderr("failed to get token information: %d\n", - (int)GetLastError()); + (int) GetLastError()); exit(1); } CloseHandle(AccessToken); - if(!AllocateAndInitializeSid(&NtAuthority, 2, - SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, - 0,&AdministratorsSid)) + if (!AllocateAndInitializeSid(&NtAuthority, 2, + SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, + 0, &AdministratorsSid)) { write_stderr("failed to get SID for Administrators group: %d\n", - (int)GetLastError()); + (int) GetLastError()); exit(1); } if (!AllocateAndInitializeSid(&NtAuthority, 2, - SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0, + SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0, 0, &PowerUsersSid)) { write_stderr("failed to get SID for PowerUsers group: %d\n", - (int)GetLastError()); + (int) GetLastError()); exit(1); } - + success = FALSE; - - for (x=0; x<Groups->GroupCount; x++) + + for (x = 0; x < Groups->GroupCount; x++) { if (EqualSid(AdministratorsSid, Groups->Groups[x].Sid) || EqualSid(PowerUsersSid, Groups->Groups[x].Sid)) @@ -102,7 +102,7 @@ pgwin32_is_admin(void) break; } } - + free(InfoBuffer); FreeSid(AdministratorsSid); FreeSid(PowerUsersSid); @@ -115,12 +115,12 @@ pgwin32_is_admin(void) * * 1) We are running as Local System (only used by services) * 2) Our token contains SECURITY_SERVICE_RID (automatically added to the - * process token by the SCM when starting a service) + * process token by the SCM when starting a service) * * Return values: - * 0 = Not service - * 1 = Service - * -1 = Error + * 0 = Not service + * 1 = Service + * -1 = Error * * Note: we can't report errors via either ereport (we're called too early) * or write_stderr (because that calls this). We are therefore reduced to @@ -129,43 +129,47 @@ pgwin32_is_admin(void) int pgwin32_is_service(void) { - static int _is_service = -1; - HANDLE AccessToken; - UCHAR InfoBuffer[1024]; - PTOKEN_GROUPS Groups = (PTOKEN_GROUPS)InfoBuffer; - PTOKEN_USER User = (PTOKEN_USER)InfoBuffer; - DWORD InfoBufferSize; - PSID ServiceSid; - PSID LocalSystemSid; - SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY }; - UINT x; + static int _is_service = -1; + HANDLE AccessToken; + UCHAR InfoBuffer[1024]; + PTOKEN_GROUPS Groups = (PTOKEN_GROUPS) InfoBuffer; + PTOKEN_USER User = (PTOKEN_USER) InfoBuffer; + DWORD InfoBufferSize; + PSID ServiceSid; + PSID LocalSystemSid; + SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY}; + UINT x; /* Only check the first time */ if (_is_service != -1) return _is_service; - - if (!OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&AccessToken)) { - fprintf(stderr,"failed to open process token: %d\n", - (int)GetLastError()); + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) + { + fprintf(stderr, "failed to open process token: %d\n", + (int) GetLastError()); return -1; } /* First check for local system */ - if (!GetTokenInformation(AccessToken,TokenUser,InfoBuffer,1024,&InfoBufferSize)) { - fprintf(stderr,"failed to get token information: %d\n", - (int)GetLastError()); + if (!GetTokenInformation(AccessToken, TokenUser, InfoBuffer, 1024, &InfoBufferSize)) + { + fprintf(stderr, "failed to get token information: %d\n", + (int) GetLastError()); return -1; } - - if (!AllocateAndInitializeSid(&NtAuthority,1, - SECURITY_LOCAL_SYSTEM_RID,0,0,0,0,0,0,0, - &LocalSystemSid)) { - fprintf(stderr,"failed to get SID for local system account\n"); + + if (!AllocateAndInitializeSid(&NtAuthority, 1, + SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, + &LocalSystemSid)) + { + fprintf(stderr, "failed to get SID for local system account\n"); CloseHandle(AccessToken); return -1; } - if (EqualSid(LocalSystemSid, User->User.Sid)) { + if (EqualSid(LocalSystemSid, User->User.Sid)) + { FreeSid(LocalSystemSid); CloseHandle(AccessToken); _is_service = 1; @@ -175,16 +179,18 @@ pgwin32_is_service(void) FreeSid(LocalSystemSid); /* Now check for group SID */ - if (!GetTokenInformation(AccessToken,TokenGroups,InfoBuffer,1024,&InfoBufferSize)) { - fprintf(stderr,"failed to get token information: %d\n", - (int)GetLastError()); + if (!GetTokenInformation(AccessToken, TokenGroups, InfoBuffer, 1024, &InfoBufferSize)) + { + fprintf(stderr, "failed to get token information: %d\n", + (int) GetLastError()); return -1; } - if (!AllocateAndInitializeSid(&NtAuthority,1, - SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, - &ServiceSid)) { - fprintf(stderr,"failed to get SID for service group\n"); + if (!AllocateAndInitializeSid(&NtAuthority, 1, + SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, + &ServiceSid)) + { + fprintf(stderr, "failed to get SID for service group\n"); CloseHandle(AccessToken); return -1; } @@ -192,7 +198,7 @@ pgwin32_is_service(void) _is_service = 0; for (x = 0; x < Groups->GroupCount; x++) { - if (EqualSid(ServiceSid, Groups->Groups[x].Sid)) + if (EqualSid(ServiceSid, Groups->Groups[x].Sid)) { _is_service = 1; break; diff --git a/src/backend/port/win32/sema.c b/src/backend/port/win32/sema.c index 43f11877453..71ab24c35c5 100644 --- a/src/backend/port/win32/sema.c +++ b/src/backend/port/win32/sema.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/sema.c,v 1.7 2004/08/29 04:12:46 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/sema.c,v 1.8 2004/08/29 05:06:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -228,12 +228,12 @@ semop(int semId, struct sembuf * sops, int nsops) if (sops[0].sem_op == -1) { DWORD ret; - HANDLE wh[2]; + HANDLE wh[2]; wh[0] = cur_handle; wh[1] = pgwin32_signal_event; - ret = WaitForMultipleObjects(2, wh, FALSE, (sops[0].sem_flg & IPC_NOWAIT)?0:INFINITE); + ret = WaitForMultipleObjects(2, wh, FALSE, (sops[0].sem_flg & IPC_NOWAIT) ? 0 : INFINITE); if (ret == WAIT_OBJECT_0) { @@ -241,7 +241,7 @@ semop(int semId, struct sembuf * sops, int nsops) sem_counts[sops[0].sem_num]--; return 0; } - else if (ret == WAIT_OBJECT_0+1) + else if (ret == WAIT_OBJECT_0 + 1) { /* Signal event is set - we have a signal to deliver */ pgwin32_dispatch_queued_signals(); diff --git a/src/backend/port/win32/shmem.c b/src/backend/port/win32/shmem.c index 229b29bae5f..ada954f5cb4 100644 --- a/src/backend/port/win32/shmem.c +++ b/src/backend/port/win32/shmem.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/shmem.c,v 1.7 2004/08/29 04:12:46 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/shmem.c,v 1.8 2004/08/29 05:06:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -22,7 +22,7 @@ static DWORD s_segsize = 0; int shmdt(const void *shmaddr) { - if (UnmapViewOfFile((LPCVOID*)shmaddr)) + if (UnmapViewOfFile((LPCVOID *) shmaddr)) return 0; else return -1; @@ -35,7 +35,7 @@ shmat(int memId, void *shmaddr, int flag) /* TODO -- shmat needs to count # attached to shared mem */ void *lpmem = MapViewOfFileEx((HANDLE) memId, FILE_MAP_WRITE | FILE_MAP_READ, - 0, 0, /* (DWORD)pshmdsc->segsize */ 0 /* s_segsize */, shmaddr); + 0, 0, /* (DWORD)pshmdsc->segsize */ 0 /* s_segsize */ , shmaddr); if (lpmem == NULL) { diff --git a/src/backend/port/win32/signal.c b/src/backend/port/win32/signal.c index 336bc59d95c..0a6b68e8fee 100644 --- a/src/backend/port/win32/signal.c +++ b/src/backend/port/win32/signal.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/signal.c,v 1.6 2004/08/29 04:12:46 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/signal.c,v 1.7 2004/08/29 05:06:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -33,8 +33,11 @@ static DWORD WINAPI pg_signal_thread(LPVOID param); static BOOL WINAPI pg_console_handler(DWORD dwCtrlType); /* Sleep function that can be interrupted by signals */ -void pgwin32_backend_usleep(long microsec) { - if (WaitForSingleObject(pgwin32_signal_event, (microsec < 500 ? 1 : (microsec + 500) / 1000)) == WAIT_OBJECT_0) { +void +pgwin32_backend_usleep(long microsec) +{ + if (WaitForSingleObject(pgwin32_signal_event, (microsec < 500 ? 1 : (microsec + 500) / 1000)) == WAIT_OBJECT_0) + { pgwin32_dispatch_queued_signals(); errno = EINTR; return; @@ -61,20 +64,20 @@ pgwin32_signal_initialize(void) /* Create the global event handle used to flag signals */ pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL); - if (pgwin32_signal_event == NULL) + if (pgwin32_signal_event == NULL) ereport(FATAL, - (errmsg_internal("failed to create signal event: %d", (int)GetLastError()))); + (errmsg_internal("failed to create signal event: %d", (int) GetLastError()))); /* Create thread for handling signals */ signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL); if (signal_thread_handle == NULL) ereport(FATAL, - (errmsg_internal("failed to create signal handler thread"))); + (errmsg_internal("failed to create signal handler thread"))); /* Create console control handle to pick up Ctrl-C etc */ - if (!SetConsoleCtrlHandler(pg_console_handler, TRUE)) + if (!SetConsoleCtrlHandler(pg_console_handler, TRUE)) ereport(FATAL, - (errmsg_internal("failed to set console control handler"))); + (errmsg_internal("failed to set console control handler"))); } @@ -246,13 +249,16 @@ pg_signal_thread(LPVOID param) } -/* Console control handler will execute on a thread created +/* Console control handler will execute on a thread created by the OS at the time of invocation */ -static BOOL WINAPI pg_console_handler(DWORD dwCtrlType) { +static BOOL WINAPI +pg_console_handler(DWORD dwCtrlType) +{ if (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT || dwCtrlType == CTRL_CLOSE_EVENT || - dwCtrlType == CTRL_SHUTDOWN_EVENT) { + dwCtrlType == CTRL_SHUTDOWN_EVENT) + { pg_queue_signal(SIGINT); return TRUE; } diff --git a/src/backend/port/win32/socket.c b/src/backend/port/win32/socket.c index 3609feeffe1..de84e45f2be 100644 --- a/src/backend/port/win32/socket.c +++ b/src/backend/port/win32/socket.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.3 2004/08/29 04:12:46 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.4 2004/08/29 05:06:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -26,19 +26,22 @@ */ /* - * Convert the last socket error code into errno + * Convert the last socket error code into errno */ -static void TranslateSocketError(void) { - switch (WSAGetLastError()) { - case WSANOTINITIALISED: - case WSAENETDOWN: - case WSAEINPROGRESS: - case WSAEINVAL: - case WSAESOCKTNOSUPPORT: - case WSAEFAULT: - case WSAEINVALIDPROVIDER: - case WSAEINVALIDPROCTABLE: - case WSAEMSGSIZE: +static void +TranslateSocketError(void) +{ + switch (WSAGetLastError()) + { + case WSANOTINITIALISED: + case WSAENETDOWN: + case WSAEINPROGRESS: + case WSAEINVAL: + case WSAESOCKTNOSUPPORT: + case WSAEFAULT: + case WSAEINVALIDPROVIDER: + case WSAEINVALIDPROCTABLE: + case WSAEMSGSIZE: errno = EINVAL; break; case WSAEAFNOSUPPORT: @@ -78,17 +81,20 @@ static void TranslateSocketError(void) { case WSAESHUTDOWN: case WSAECONNABORTED: case WSAEDISCON: - errno = ECONNREFUSED; /*ENOTCONN?*/ + errno = ECONNREFUSED; /* ENOTCONN? */ break; default: ereport(NOTICE, - (errmsg_internal("Unknown win32 socket error code: %i",WSAGetLastError()))); + (errmsg_internal("Unknown win32 socket error code: %i", WSAGetLastError()))); errno = EINVAL; } } -static int pgwin32_poll_signals(void) { - if (WaitForSingleObject(pgwin32_signal_event,0) == WAIT_OBJECT_0) { +static int +pgwin32_poll_signals(void) +{ + if (WaitForSingleObject(pgwin32_signal_event, 0) == WAIT_OBJECT_0) + { pgwin32_dispatch_queued_signals(); errno = EINTR; return 1; @@ -96,25 +102,28 @@ static int pgwin32_poll_signals(void) { return 0; } -static int pgwin32_waitforsinglesocket(SOCKET s, int what) { +static int +pgwin32_waitforsinglesocket(SOCKET s, int what) +{ static HANDLE waitevent = INVALID_HANDLE_VALUE; - HANDLE events[2]; - int r; + HANDLE events[2]; + int r; - if (waitevent == INVALID_HANDLE_VALUE) { + if (waitevent == INVALID_HANDLE_VALUE) + { waitevent = CreateEvent(NULL, TRUE, FALSE, NULL); if (waitevent == INVALID_HANDLE_VALUE) ereport(ERROR, - (errmsg_internal("Failed to create socket waiting event: %i",(int)GetLastError()))); + (errmsg_internal("Failed to create socket waiting event: %i", (int) GetLastError()))); } - else - if (!ResetEvent(waitevent)) - ereport(ERROR, - (errmsg_internal("Failed to reset socket waiting event: %i",(int)GetLastError()))); + else if (!ResetEvent(waitevent)) + ereport(ERROR, + (errmsg_internal("Failed to reset socket waiting event: %i", (int) GetLastError()))); + - - if (WSAEventSelect(s, waitevent, what) == SOCKET_ERROR) { + if (WSAEventSelect(s, waitevent, what) == SOCKET_ERROR) + { TranslateSocketError(); return 0; } @@ -123,32 +132,37 @@ static int pgwin32_waitforsinglesocket(SOCKET s, int what) { events[1] = waitevent; r = WaitForMultipleObjects(2, events, FALSE, INFINITE); - if (r == WAIT_OBJECT_0) { + if (r == WAIT_OBJECT_0) + { pgwin32_dispatch_queued_signals(); errno = EINTR; return 0; } - if (r == WAIT_OBJECT_0+1) + if (r == WAIT_OBJECT_0 + 1) return 1; ereport(ERROR, - (errmsg_internal("Bad return from WaitForMultipleObjects: %i (%i)",r,(int)GetLastError()))); + (errmsg_internal("Bad return from WaitForMultipleObjects: %i (%i)", r, (int) GetLastError()))); return 0; } /* - * Create a socket, setting it to overlapped and non-blocking + * Create a socket, setting it to overlapped and non-blocking */ -SOCKET pgwin32_socket(int af, int type, int protocol) { - SOCKET s; +SOCKET +pgwin32_socket(int af, int type, int protocol) +{ + SOCKET s; unsigned long on = 1; s = WSASocket(af, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED); - if (s == INVALID_SOCKET) { + if (s == INVALID_SOCKET) + { TranslateSocketError(); return INVALID_SOCKET; } - - if (ioctlsocket(s, FIONBIO, &on)) { + + if (ioctlsocket(s, FIONBIO, &on)) + { TranslateSocketError(); return INVALID_SOCKET; } @@ -158,15 +172,20 @@ SOCKET pgwin32_socket(int af, int type, int protocol) { } -SOCKET pgwin32_accept(SOCKET s, struct sockaddr* addr, int* addrlen) { - SOCKET rs; +SOCKET +pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen) +{ + SOCKET rs; - /* Poll for signals, but don't return with EINTR, since we don't - handle that in pqcomm.c */ + /* + * Poll for signals, but don't return with EINTR, since we don't + * handle that in pqcomm.c + */ pgwin32_poll_signals(); rs = WSAAccept(s, addr, addrlen, NULL, 0); - if (rs == INVALID_SOCKET) { + if (rs == INVALID_SOCKET) + { TranslateSocketError(); return INVALID_SOCKET; } @@ -175,30 +194,36 @@ SOCKET pgwin32_accept(SOCKET s, struct sockaddr* addr, int* addrlen) { /* No signal delivery during connect. */ -int pgwin32_connect(SOCKET s, const struct sockaddr *addr, int addrlen) { - int r; +int +pgwin32_connect(SOCKET s, const struct sockaddr * addr, int addrlen) +{ + int r; r = WSAConnect(s, addr, addrlen, NULL, NULL, NULL, NULL); if (r == 0) return 0; - if (WSAGetLastError() != WSAEWOULDBLOCK) { + if (WSAGetLastError() != WSAEWOULDBLOCK) + { TranslateSocketError(); return -1; } - while (pgwin32_waitforsinglesocket(s, FD_CONNECT) == 0) { + while (pgwin32_waitforsinglesocket(s, FD_CONNECT) == 0) + { /* Loop endlessly as long as we are just delivering signals */ } return 0; } -int pgwin32_recv(SOCKET s, char *buf, int len, int f) { - WSABUF wbuf; - int r; - DWORD b; - DWORD flags = f; +int +pgwin32_recv(SOCKET s, char *buf, int len, int f) +{ + WSABUF wbuf; + int r; + DWORD b; + DWORD flags = f; if (pgwin32_poll_signals()) return -1; @@ -206,34 +231,38 @@ int pgwin32_recv(SOCKET s, char *buf, int len, int f) { wbuf.len = len; wbuf.buf = buf; - r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL); - if (r != SOCKET_ERROR && b > 0) + r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL); + if (r != SOCKET_ERROR && b > 0) /* Read succeeded right away */ - return b; + return b; if (r == SOCKET_ERROR && - WSAGetLastError() != WSAEWOULDBLOCK) { + WSAGetLastError() != WSAEWOULDBLOCK) + { TranslateSocketError(); return -1; } /* No error, zero bytes (win2000+) or error+WSAEWOULDBLOCK (<=nt4) */ - if (pgwin32_waitforsinglesocket(s, FD_READ | FD_CLOSE | FD_ACCEPT) == 0) + if (pgwin32_waitforsinglesocket(s, FD_READ | FD_CLOSE | FD_ACCEPT) == 0) return -1; r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL); - if (r == SOCKET_ERROR) { + if (r == SOCKET_ERROR) + { TranslateSocketError(); return -1; } return b; } -int pgwin32_send(SOCKET s, char *buf, int len, int flags) { - WSABUF wbuf; - int r; - DWORD b; +int +pgwin32_send(SOCKET s, char *buf, int len, int flags) +{ + WSABUF wbuf; + int r; + DWORD b; if (pgwin32_poll_signals()) return -1; @@ -242,12 +271,13 @@ int pgwin32_send(SOCKET s, char *buf, int len, int flags) { wbuf.buf = buf; r = WSASend(s, &wbuf, 1, &b, flags, NULL, NULL); - if (r != SOCKET_ERROR && b > 0) + if (r != SOCKET_ERROR && b > 0) /* Write succeeded right away */ return b; - + if (r == SOCKET_ERROR && - WSAGetLastError() != WSAEWOULDBLOCK) { + WSAGetLastError() != WSAEWOULDBLOCK) + { TranslateSocketError(); return -1; } @@ -258,7 +288,8 @@ int pgwin32_send(SOCKET s, char *buf, int len, int flags) { return -1; r = WSASend(s, &wbuf, 1, &b, flags, NULL, NULL); - if (r == SOCKET_ERROR) { + if (r == SOCKET_ERROR) + { TranslateSocketError(); return -1; } @@ -268,22 +299,25 @@ int pgwin32_send(SOCKET s, char *buf, int len, int flags) { /* * Wait for activity on one or more sockets. - * While waiting, allow signals to run + * While waiting, allow signals to run * * NOTE! Currently does not implement exceptfds check, * since it is not used in postgresql! */ -int pgwin32_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const struct timeval* timeout) { - WSAEVENT events[FD_SETSIZE*2]; /* worst case is readfds totally different - * from writefds, so 2*FD_SETSIZE sockets */ - SOCKET sockets[FD_SETSIZE*2]; - int numevents=0; - int i; - int r; - DWORD timeoutval = WSA_INFINITE; - FD_SET outreadfds; - FD_SET outwritefds; - int nummatches = 0; +int +pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout) +{ + WSAEVENT events[FD_SETSIZE * 2]; /* worst case is readfds totally + * different from writefds, so + * 2*FD_SETSIZE sockets */ + SOCKET sockets[FD_SETSIZE * 2]; + int numevents = 0; + int i; + int r; + DWORD timeoutval = WSA_INFINITE; + FD_SET outreadfds; + FD_SET outwritefds; + int nummatches = 0; Assert(exceptfds == NULL); @@ -293,55 +327,70 @@ int pgwin32_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfd FD_ZERO(&outreadfds); FD_ZERO(&outwritefds); - /* Write FDs are different in the way that it is only flagged by - * WSASelectEvent() if we have tried to write to them first. So try - * an empty write */ - if (writefds) { - for (i = 0; i < writefds->fd_count; i++) { - char c; - WSABUF buf; - DWORD sent; + /* + * Write FDs are different in the way that it is only flagged by + * WSASelectEvent() if we have tried to write to them first. So try an + * empty write + */ + if (writefds) + { + for (i = 0; i < writefds->fd_count; i++) + { + char c; + WSABUF buf; + DWORD sent; buf.buf = &c; buf.len = 0; r = WSASend(writefds->fd_array[i], &buf, 1, &sent, 0, NULL, NULL); - if (r == 0) /* Completed - means things are fine! */ + if (r == 0) /* Completed - means things are fine! */ FD_SET(writefds->fd_array[i], &outwritefds); - else { /* Not completed */ + else + { /* Not completed */ if (WSAGetLastError() != WSAEWOULDBLOCK) - /* Not completed, and not just "would block", so - * an error occured */ + + /* + * Not completed, and not just "would block", so an + * error occured + */ FD_SET(writefds->fd_array[i], &outwritefds); } } - if (outwritefds.fd_count > 0) { - memcpy(writefds,&outwritefds,sizeof(fd_set)); + if (outwritefds.fd_count > 0) + { + memcpy(writefds, &outwritefds, sizeof(fd_set)); if (readfds) FD_ZERO(readfds); return outwritefds.fd_count; } } - + /* Now set up for an actual select */ - if (timeout != NULL) { + if (timeout != NULL) + { /* timeoutval is in milliseconds */ - timeoutval = timeout->tv_sec*1000 + timeout->tv_usec / 1000; + timeoutval = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; } - if (readfds != NULL) { - for (i=0; i < readfds->fd_count; i++) { + if (readfds != NULL) + { + for (i = 0; i < readfds->fd_count; i++) + { events[numevents] = WSACreateEvent(); sockets[numevents] = readfds->fd_array[i]; numevents++; } } - if (writefds != NULL) { - for (i=0; i < writefds->fd_count; i++) { + if (writefds != NULL) + { + for (i = 0; i < writefds->fd_count; i++) + { if (!readfds || - !FD_ISSET(writefds->fd_array[i], readfds)) { + !FD_ISSET(writefds->fd_array[i], readfds)) + { /* If the socket is not in the read list */ events[numevents] = WSACreateEvent(); sockets[numevents] = writefds->fd_array[i]; @@ -350,63 +399,74 @@ int pgwin32_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfd } } - for (i=0; i < numevents; i++) { - int flags = 0; + for (i = 0; i < numevents; i++) + { + int flags = 0; - if (readfds && FD_ISSET(sockets[i],readfds)) + if (readfds && FD_ISSET(sockets[i], readfds)) flags |= FD_READ | FD_ACCEPT | FD_CLOSE; - if (writefds && FD_ISSET(sockets[i],writefds)) + if (writefds && FD_ISSET(sockets[i], writefds)) flags |= FD_WRITE | FD_CLOSE; - - if (WSAEventSelect(sockets[i], events[i], flags) == SOCKET_ERROR) { + + if (WSAEventSelect(sockets[i], events[i], flags) == SOCKET_ERROR) + { TranslateSocketError(); - for (i = 0; i < numevents; i++) + for (i = 0; i < numevents; i++) WSACloseEvent(events[i]); return -1; } } events[numevents] = pgwin32_signal_event; - r = WaitForMultipleObjectsEx(numevents+1, events, FALSE, timeoutval, FALSE); - if (r != WSA_WAIT_TIMEOUT && r != (WAIT_OBJECT_0+numevents)) { - /* We scan all events, even those not signalled, in case more - * than one event has been tagged but Wait.. can only return one. + r = WaitForMultipleObjectsEx(numevents + 1, events, FALSE, timeoutval, FALSE); + if (r != WSA_WAIT_TIMEOUT && r != (WAIT_OBJECT_0 + numevents)) + { + /* + * We scan all events, even those not signalled, in case more than + * one event has been tagged but Wait.. can only return one. */ WSANETWORKEVENTS resEvents; - - for (i=0; i < numevents; i++) { - ZeroMemory(&resEvents,sizeof(resEvents)); - if (WSAEnumNetworkEvents(sockets[i],events[i],&resEvents) == SOCKET_ERROR) + + for (i = 0; i < numevents; i++) + { + ZeroMemory(&resEvents, sizeof(resEvents)); + if (WSAEnumNetworkEvents(sockets[i], events[i], &resEvents) == SOCKET_ERROR) ereport(FATAL, - (errmsg_internal("failed to enumerate network events: %i",(int)GetLastError()))); + (errmsg_internal("failed to enumerate network events: %i", (int) GetLastError()))); /* Read activity? */ - if (readfds && FD_ISSET(sockets[i], readfds)) { + if (readfds && FD_ISSET(sockets[i], readfds)) + { if ((resEvents.lNetworkEvents & FD_READ) || (resEvents.lNetworkEvents & FD_ACCEPT) || - (resEvents.lNetworkEvents & FD_CLOSE)) { - FD_SET(sockets[i],&outreadfds); + (resEvents.lNetworkEvents & FD_CLOSE)) + { + FD_SET(sockets[i], &outreadfds); nummatches++; } } /* Write activity? */ - if (writefds && FD_ISSET(sockets[i], writefds)) { + if (writefds && FD_ISSET(sockets[i], writefds)) + { if ((resEvents.lNetworkEvents & FD_WRITE) || - (resEvents.lNetworkEvents & FD_CLOSE)) { - FD_SET(sockets[i],&outwritefds); + (resEvents.lNetworkEvents & FD_CLOSE)) + { + FD_SET(sockets[i], &outwritefds); nummatches++; } } } } - + /* Clean up all handles */ - for (i = 0; i < numevents; i++) { + for (i = 0; i < numevents; i++) + { WSAEventSelect(sockets[i], events[i], 0); WSACloseEvent(events[i]); } - - if (r == WSA_WAIT_TIMEOUT) { + + if (r == WSA_WAIT_TIMEOUT) + { if (readfds) FD_ZERO(readfds); if (writefds) @@ -414,7 +474,8 @@ int pgwin32_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfd return 0; } - if (r == WAIT_OBJECT_0+numevents) { + if (r == WAIT_OBJECT_0 + numevents) + { pgwin32_dispatch_queued_signals(); errno = EINTR; if (readfds) @@ -433,9 +494,9 @@ int pgwin32_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfd } -/* +/* * Return win32 error string, since strerror can't - * handle winsock codes + * handle winsock codes */ static char wserrbuf[256]; const char * @@ -443,11 +504,12 @@ pgwin32_socket_strerror(int err) { static HANDLE handleDLL = INVALID_HANDLE_VALUE; - if (handleDLL == INVALID_HANDLE_VALUE) { + if (handleDLL == INVALID_HANDLE_VALUE) + { handleDLL = LoadLibraryEx("netmsg.dll", NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE); - if (handleDLL == NULL) + if (handleDLL == NULL) ereport(FATAL, - (errmsg_internal("Failed to load netmsg.dll: %i",(int)GetLastError()))); + (errmsg_internal("Failed to load netmsg.dll: %i", (int) GetLastError()))); } ZeroMemory(&wserrbuf, sizeof(wserrbuf)); @@ -456,11 +518,11 @@ pgwin32_socket_strerror(int err) err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), wserrbuf, - sizeof(wserrbuf)-1, - NULL) == 0) { + sizeof(wserrbuf) - 1, + NULL) == 0) + { /* Failed to get id */ - sprintf(wserrbuf,"Unknown winsock error %i",err); + sprintf(wserrbuf, "Unknown winsock error %i", err); } return wserrbuf; } - diff --git a/src/backend/port/win32/timer.c b/src/backend/port/win32/timer.c index 8dc6a829001..d764dac4b0b 100644 --- a/src/backend/port/win32/timer.c +++ b/src/backend/port/win32/timer.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/port/win32/timer.c,v 1.3 2004/08/29 04:12:46 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/port/win32/timer.c,v 1.4 2004/08/29 05:06:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -18,19 +18,23 @@ static HANDLE timerHandle = INVALID_HANDLE_VALUE; -static VOID CALLBACK timer_completion(LPVOID arg, DWORD timeLow, DWORD timeHigh) { +static VOID CALLBACK +timer_completion(LPVOID arg, DWORD timeLow, DWORD timeHigh) +{ pg_queue_signal(SIGALRM); } /* * Limitations of this implementation: - * + * * - Does not support setting ovalue * - Does not support interval timer (value->it_interval) * - Only supports ITIMER_REAL */ -int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue) { +int +setitimer(int which, const struct itimerval * value, struct itimerval * ovalue) +{ LARGE_INTEGER dueTime; Assert(ovalue == NULL); @@ -38,28 +42,30 @@ int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0); Assert(which == ITIMER_REAL); - if (timerHandle == INVALID_HANDLE_VALUE) { + if (timerHandle == INVALID_HANDLE_VALUE) + { /* First call in this backend, create new timer object */ timerHandle = CreateWaitableTimer(NULL, TRUE, NULL); if (timerHandle == NULL) ereport(FATAL, - (errmsg_internal("failed to create waitable timer: %i",(int)GetLastError()))); + (errmsg_internal("failed to create waitable timer: %i", (int) GetLastError()))); } if (value->it_value.tv_sec == 0 && - value->it_value.tv_usec == 0) { + value->it_value.tv_usec == 0) + { /* Turn timer off */ CancelWaitableTimer(timerHandle); return 0; } /* Negative time to SetWaitableTimer means relative time */ - dueTime.QuadPart = -(value->it_value.tv_usec*10 + value->it_value.tv_sec*10000000L); + dueTime.QuadPart = -(value->it_value.tv_usec * 10 + value->it_value.tv_sec * 10000000L); /* Turn timer on, or change timer */ if (!SetWaitableTimer(timerHandle, &dueTime, 0, timer_completion, NULL, FALSE)) ereport(FATAL, - (errmsg_internal("failed to set waitable timer: %i",(int)GetLastError()))); + (errmsg_internal("failed to set waitable timer: %i", (int) GetLastError()))); return 0; } |