]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: improve windows errno handling
authorAlex Brainman <alex.brainman@gmail.com>
Mon, 26 Jul 2010 00:24:48 +0000 (10:24 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Mon, 26 Jul 2010 00:24:48 +0000 (10:24 +1000)
R=rsc, Joe Poirier, PeterGo
CC=golang-dev
https://golang.org/cl/1872045

src/pkg/runtime/windows/thread.c
src/pkg/syscall/mksyscall_windows.sh
src/pkg/syscall/zsyscall_windows_386.go

index 5dd013f48366a1c7f8ad1d69d218147b223d26c9..7522d26433dce0aaa4e6e4e66fdfb0a9d838e320 100644 (file)
@@ -17,6 +17,7 @@ void *VirtualAlloc;
 void *LoadLibraryEx;
 void *GetProcAddress;
 void *GetLastError;
+void *SetLastError;
 
 static void *CreateEvent;
 static void *CreateThread;
@@ -65,6 +66,7 @@ osinit(void)
        WaitForSingleObject = get_proc_addr("kernel32.dll", "WaitForSingleObject");
        WriteFile = get_proc_addr("kernel32.dll", "WriteFile");
        GetLastError = get_proc_addr("kernel32.dll", "GetLastError");
+       SetLastError = get_proc_addr("kernel32.dll", "SetLastError");
 }
 
 // The arguments are strings.
@@ -285,6 +287,7 @@ void
 call_syscall(void *args)
 {
        StdcallParams *p = (StdcallParams*)args;
+       stdcall_raw(SetLastError, 1, 0);
        p->r = (uintptr)stdcall_raw((void*)p->fn, p->args[0], p->args[1], p->args[2], p->args[3], p->args[4], p->args[5], p->args[6], p->args[7], p->args[8], p->args[9], p->args[10], p->args[11]);
        p->err = (uintptr)stdcall_raw(GetLastError);
        return;
index ea35ba2b42911d6d139a7013cae4ee3fb21e3721..9b9fad03a646409ffb4e97db060321e48fb69921 100755 (executable)
@@ -229,7 +229,11 @@ while(<>) {
                if($name eq "errno") {
                        # Set errno to "last error" only if returned value indicate failure
                        $body .= "\tif $failexpr {\n";
-                       $body .= "\t\t$name = $type($reg);\n";
+                       $body .= "\t\tif $reg != 0 {\n";
+                       $body .= "\t\t\t$name = $type($reg);\n";
+                       $body .= "\t\t} else {\n";
+                       $body .= "\t\t\t$name = EINVAL;\n";
+                       $body .= "\t\t}\n";
                        $body .= "\t} else {\n";
                        $body .= "\t\t$name = 0;\n";
                        $body .= "\t}\n";
index 7c75d2b7739f061a4b794e6026417403742ac84d..f6e98dc167d727face73f13c16d1a0f91296637f 100644 (file)
@@ -1,4 +1,4 @@
-// mksyscall_windows.sh -l32 syscall_windows.go
+// mksyscall_windows.sh -l32 syscall_windows.go syscall_windows_386.go
 // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
 
 package syscall
@@ -76,7 +76,11 @@ func LoadLibrary(libname string) (handle uint32, errno int) {
        r0, _, e1 := Syscall(procLoadLibraryW, uintptr(unsafe.Pointer(StringToUTF16Ptr(libname))), 0, 0)
        handle = uint32(r0)
        if handle == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -87,7 +91,11 @@ func FreeLibrary(handle uint32) (ok bool, errno int) {
        r0, _, e1 := Syscall(procFreeLibrary, uintptr(handle), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -98,7 +106,11 @@ func GetProcAddress(module uint32, procname string) (proc uint32, errno int) {
        r0, _, e1 := Syscall(procGetProcAddress, uintptr(module), uintptr(unsafe.Pointer(StringBytePtr(procname))), 0)
        proc = uint32(r0)
        if proc == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -109,7 +121,11 @@ func GetVersion() (ver uint32, errno int) {
        r0, _, e1 := Syscall(procGetVersion, 0, 0, 0)
        ver = uint32(r0)
        if ver == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -124,7 +140,11 @@ func FormatMessage(flags uint32, msgsrc uint32, msgid uint32, langid uint32, buf
        r0, _, e1 := Syscall9(procFormatMessageW, uintptr(flags), uintptr(msgsrc), uintptr(msgid), uintptr(langid), uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), uintptr(unsafe.Pointer(args)), 0, 0)
        n = uint32(r0)
        if n == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -140,7 +160,11 @@ func CreateFile(name *uint16, access uint32, mode uint32, sa *byte, createmode u
        r0, _, e1 := Syscall9(procCreateFileW, uintptr(unsafe.Pointer(name)), uintptr(access), uintptr(mode), uintptr(unsafe.Pointer(sa)), uintptr(createmode), uintptr(attrs), uintptr(templatefile), 0, 0)
        handle = int32(r0)
        if handle == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -155,7 +179,11 @@ func ReadFile(handle int32, buf []byte, done *uint32, overlapped *Overlapped) (o
        r0, _, e1 := Syscall6(procReadFile, uintptr(handle), uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), uintptr(unsafe.Pointer(done)), uintptr(unsafe.Pointer(overlapped)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -170,7 +198,11 @@ func WriteFile(handle int32, buf []byte, done *uint32, overlapped *Overlapped) (
        r0, _, e1 := Syscall6(procWriteFile, uintptr(handle), uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), uintptr(unsafe.Pointer(done)), uintptr(unsafe.Pointer(overlapped)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -181,7 +213,11 @@ func SetFilePointer(handle int32, lowoffset int32, highoffsetptr *int32, whence
        r0, _, e1 := Syscall6(procSetFilePointer, uintptr(handle), uintptr(lowoffset), uintptr(unsafe.Pointer(highoffsetptr)), uintptr(whence), 0, 0)
        newlowoffset = uint32(r0)
        if newlowoffset == 0xffffffff {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -192,7 +228,11 @@ func CloseHandle(handle int32) (ok bool, errno int) {
        r0, _, e1 := Syscall(procCloseHandle, uintptr(handle), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -203,7 +243,11 @@ func GetStdHandle(stdhandle int32) (handle int32, errno int) {
        r0, _, e1 := Syscall(procGetStdHandle, uintptr(stdhandle), 0, 0)
        handle = int32(r0)
        if handle == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -214,7 +258,11 @@ func FindFirstFile(name *uint16, data *Win32finddata) (handle int32, errno int)
        r0, _, e1 := Syscall(procFindFirstFileW, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(data)), 0)
        handle = int32(r0)
        if handle == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -225,7 +273,11 @@ func FindNextFile(handle int32, data *Win32finddata) (ok bool, errno int) {
        r0, _, e1 := Syscall(procFindNextFileW, uintptr(handle), uintptr(unsafe.Pointer(data)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -236,7 +288,11 @@ func FindClose(handle int32) (ok bool, errno int) {
        r0, _, e1 := Syscall(procFindClose, uintptr(handle), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -247,7 +303,11 @@ func GetFileInformationByHandle(handle int32, data *ByHandleFileInformation) (ok
        r0, _, e1 := Syscall(procGetFileInformationByHandle, uintptr(handle), uintptr(unsafe.Pointer(data)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -258,7 +318,11 @@ func GetCurrentDirectory(buflen uint32, buf *uint16) (n uint32, errno int) {
        r0, _, e1 := Syscall(procGetCurrentDirectoryW, uintptr(buflen), uintptr(unsafe.Pointer(buf)), 0)
        n = uint32(r0)
        if n == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -269,7 +333,11 @@ func SetCurrentDirectory(path *uint16) (ok bool, errno int) {
        r0, _, e1 := Syscall(procSetCurrentDirectoryW, uintptr(unsafe.Pointer(path)), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -280,7 +348,11 @@ func CreateDirectory(path *uint16, sa *byte) (ok bool, errno int) {
        r0, _, e1 := Syscall(procCreateDirectoryW, uintptr(unsafe.Pointer(path)), uintptr(unsafe.Pointer(sa)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -291,7 +363,11 @@ func RemoveDirectory(path *uint16) (ok bool, errno int) {
        r0, _, e1 := Syscall(procRemoveDirectoryW, uintptr(unsafe.Pointer(path)), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -302,7 +378,11 @@ func DeleteFile(path *uint16) (ok bool, errno int) {
        r0, _, e1 := Syscall(procDeleteFileW, uintptr(unsafe.Pointer(path)), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -313,7 +393,11 @@ func MoveFile(from *uint16, to *uint16) (ok bool, errno int) {
        r0, _, e1 := Syscall(procMoveFileW, uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(to)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -324,7 +408,11 @@ func GetComputerName(buf *uint16, n *uint32) (ok bool, errno int) {
        r0, _, e1 := Syscall(procGetComputerNameW, uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(n)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -335,7 +423,11 @@ func SetEndOfFile(handle int32) (ok bool, errno int) {
        r0, _, e1 := Syscall(procSetEndOfFile, uintptr(handle), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -356,7 +448,11 @@ func GetTimeZoneInformation(tzi *Timezoneinformation) (rc uint32, errno int) {
        r0, _, e1 := Syscall(procGetTimeZoneInformation, uintptr(unsafe.Pointer(tzi)), 0, 0)
        rc = uint32(r0)
        if rc == 0xffffffff {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -367,7 +463,11 @@ func CreateIoCompletionPort(filehandle int32, cphandle int32, key uint32, thread
        r0, _, e1 := Syscall6(procCreateIoCompletionPort, uintptr(filehandle), uintptr(cphandle), uintptr(key), uintptr(threadcnt), 0, 0)
        handle = int32(r0)
        if handle == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -378,7 +478,11 @@ func GetQueuedCompletionStatus(cphandle int32, qty *uint32, key *uint32, overlap
        r0, _, e1 := Syscall6(procGetQueuedCompletionStatus, uintptr(cphandle), uintptr(unsafe.Pointer(qty)), uintptr(unsafe.Pointer(key)), uintptr(unsafe.Pointer(overlapped)), uintptr(timeout), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -395,7 +499,11 @@ func CreateProcess(appName *int16, commandLine *uint16, procSecurity *int16, thr
        r0, _, e1 := Syscall12(procCreateProcessW, uintptr(unsafe.Pointer(appName)), uintptr(unsafe.Pointer(commandLine)), uintptr(unsafe.Pointer(procSecurity)), uintptr(unsafe.Pointer(threadSecurity)), uintptr(_p0), uintptr(creationFlags), uintptr(unsafe.Pointer(env)), uintptr(unsafe.Pointer(currentDir)), uintptr(unsafe.Pointer(startupInfo)), uintptr(unsafe.Pointer(outProcInfo)), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -406,7 +514,11 @@ func GetStartupInfo(startupInfo *StartupInfo) (ok bool, errno int) {
        r0, _, e1 := Syscall(procGetStartupInfoW, uintptr(unsafe.Pointer(startupInfo)), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -417,7 +529,11 @@ func GetCurrentProcess() (pseudoHandle int32, errno int) {
        r0, _, e1 := Syscall(procGetCurrentProcess, 0, 0, 0)
        pseudoHandle = int32(r0)
        if pseudoHandle == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -434,7 +550,11 @@ func DuplicateHandle(hSourceProcessHandle int32, hSourceHandle int32, hTargetPro
        r0, _, e1 := Syscall9(procDuplicateHandle, uintptr(hSourceProcessHandle), uintptr(hSourceHandle), uintptr(hTargetProcessHandle), uintptr(unsafe.Pointer(lpTargetHandle)), uintptr(dwDesiredAccess), uintptr(_p0), uintptr(dwOptions), 0, 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -445,7 +565,11 @@ func WaitForSingleObject(handle int32, waitMilliseconds uint32) (event uint32, e
        r0, _, e1 := Syscall(procWaitForSingleObject, uintptr(handle), uintptr(waitMilliseconds), 0)
        event = uint32(r0)
        if event == 0xffffffff {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -456,7 +580,11 @@ func GetTempPath(buflen uint32, buf *uint16) (n uint32, errno int) {
        r0, _, e1 := Syscall(procGetTempPathW, uintptr(buflen), uintptr(unsafe.Pointer(buf)), 0)
        n = uint32(r0)
        if n == 0 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -467,7 +595,11 @@ func CryptAcquireContext(provhandle *uint32, container *uint16, provider *uint16
        r0, _, e1 := Syscall6(procCryptAcquireContextW, uintptr(unsafe.Pointer(provhandle)), uintptr(unsafe.Pointer(container)), uintptr(unsafe.Pointer(provider)), uintptr(provtype), uintptr(flags), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -478,7 +610,11 @@ func CryptReleaseContext(provhandle uint32, flags uint32) (ok bool, errno int) {
        r0, _, e1 := Syscall(procCryptReleaseContext, uintptr(provhandle), uintptr(flags), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -489,7 +625,11 @@ func CryptGenRandom(provhandle uint32, buflen uint32, buf *byte) (ok bool, errno
        r0, _, e1 := Syscall(procCryptGenRandom, uintptr(provhandle), uintptr(buflen), uintptr(unsafe.Pointer(buf)))
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -505,7 +645,11 @@ func WSAStartup(verreq uint32, data *WSAData) (sockerrno int) {
 func WSACleanup() (errno int) {
        r1, _, e1 := Syscall(procWSACleanup, 0, 0, 0)
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -516,7 +660,11 @@ func socket(af int32, typ int32, protocol int32) (handle int32, errno int) {
        r0, _, e1 := Syscall(procsocket, uintptr(af), uintptr(typ), uintptr(protocol))
        handle = int32(r0)
        if handle == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -526,7 +674,11 @@ func socket(af int32, typ int32, protocol int32) (handle int32, errno int) {
 func setsockopt(s int32, level int32, optname int32, optval *byte, optlen int32) (errno int) {
        r1, _, e1 := Syscall6(procsetsockopt, uintptr(s), uintptr(level), uintptr(optname), uintptr(unsafe.Pointer(optval)), uintptr(optlen), 0)
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -536,7 +688,11 @@ func setsockopt(s int32, level int32, optname int32, optval *byte, optlen int32)
 func bind(s int32, name uintptr, namelen int32) (errno int) {
        r1, _, e1 := Syscall(procbind, uintptr(s), uintptr(name), uintptr(namelen))
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -546,7 +702,11 @@ func bind(s int32, name uintptr, namelen int32) (errno int) {
 func connect(s int32, name uintptr, namelen int32) (errno int) {
        r1, _, e1 := Syscall(procconnect, uintptr(s), uintptr(name), uintptr(namelen))
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -556,7 +716,11 @@ func connect(s int32, name uintptr, namelen int32) (errno int) {
 func getsockname(s int32, rsa *RawSockaddrAny, addrlen *int32) (errno int) {
        r1, _, e1 := Syscall(procgetsockname, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)))
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -566,7 +730,11 @@ func getsockname(s int32, rsa *RawSockaddrAny, addrlen *int32) (errno int) {
 func getpeername(s int32, rsa *RawSockaddrAny, addrlen *int32) (errno int) {
        r1, _, e1 := Syscall(procgetpeername, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)))
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -576,7 +744,11 @@ func getpeername(s int32, rsa *RawSockaddrAny, addrlen *int32) (errno int) {
 func listen(s int32, backlog int32) (errno int) {
        r1, _, e1 := Syscall(proclisten, uintptr(s), uintptr(backlog), 0)
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -586,7 +758,11 @@ func listen(s int32, backlog int32) (errno int) {
 func shutdown(s int32, how int32) (errno int) {
        r1, _, e1 := Syscall(procshutdown, uintptr(s), uintptr(how), 0)
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -597,7 +773,11 @@ func AcceptEx(ls uint32, as uint32, buf *byte, rxdatalen uint32, laddrlen uint32
        r0, _, e1 := Syscall9(procAcceptEx, uintptr(ls), uintptr(as), uintptr(unsafe.Pointer(buf)), uintptr(rxdatalen), uintptr(laddrlen), uintptr(raddrlen), uintptr(unsafe.Pointer(recvd)), uintptr(unsafe.Pointer(overlapped)), 0)
        ok = bool(r0 != 0)
        if !ok {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -612,7 +792,11 @@ func GetAcceptExSockaddrs(buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen
 func WSARecv(s uint32, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, overlapped *Overlapped, croutine *byte) (errno int) {
        r1, _, e1 := Syscall9(procWSARecv, uintptr(s), uintptr(unsafe.Pointer(bufs)), uintptr(bufcnt), uintptr(unsafe.Pointer(recvd)), uintptr(unsafe.Pointer(flags)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)), 0, 0)
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }
@@ -622,7 +806,11 @@ func WSARecv(s uint32, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32
 func WSASend(s uint32, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, overlapped *Overlapped, croutine *byte) (errno int) {
        r1, _, e1 := Syscall9(procWSASend, uintptr(s), uintptr(unsafe.Pointer(bufs)), uintptr(bufcnt), uintptr(unsafe.Pointer(sent)), uintptr(flags), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)), 0, 0)
        if int(r1) == -1 {
-               errno = int(e1)
+               if e1 != 0 {
+                       errno = int(e1)
+               } else {
+                       errno = EINVAL
+               }
        } else {
                errno = 0
        }