Commit a79a9098 authored by Alex Brainman's avatar Alex Brainman

syscall: make sure go error numbers do not clash with windows system errors

R=rsc
CC=golang-dev
https://golang.org/cl/1857049
parent 5443bbe2
......@@ -154,7 +154,7 @@ windows_386)
mksyscall="./mksyscall_windows.sh -l32"
mksysnum=
mktypes=
mkerrors=
mkerrors="./mkerrors_windows.sh -f -m32"
;;
*)
echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
......
#!/usr/bin/env bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# Generate Go code listing errors and other #defined constant
# values (ENAMETOOLONG etc.), by asking the preprocessor
# about the definitions.
unset LANG
export LC_ALL=C
export LC_CTYPE=C
case "$GOARCH" in
arm)
GCC=arm-gcc
;;
*)
GCC=gcc
;;
esac
uname=$(uname)
includes_Linux='
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/epoll.h>
#include <linux/ptrace.h>
#include <linux/wait.h>
'
includes_Darwin='
#define __DARWIN_UNIX03 0
#define KERNEL
#define _DARWIN_USE_64_BIT_INODE
#include <sys/wait.h>
#include <sys/event.h>
'
includes_FreeBSD='
#include <sys/wait.h>
#include <sys/event.h>
'
includes='
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <sys/signal.h>
#include <signal.h>
'
ccflags=""
next=false
for i
do
if $next; then
ccflags="$ccflags $i"
next=false
elif [ "$i" = "-f" ]; then
next=true
fi
done
# Pull out just the error names for later.
errors=$(
echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
awk '
$1 != "#define" || $2 ~ /\(/ {next}
$2 ~ /^ENOTDIR$/ {next}
$2 ~ /^E[A-Z0-9_]+$/ { print $2 }
{next}
' | sort
)
echo '// mkerrors_windows.sh' "$@"
echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
echo
echo 'package syscall'
# Run C program to print error strings.
(
/bin/echo "
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
struct {
char *name;
int value;
} errors[] = {
"
for i in $errors
do
/bin/echo ' {"'$i'",' $i'},'
done
# Use /bin/echo to avoid builtin echo,
# which interprets \n itself
/bin/echo '
};
int
main(void)
{
int i, j, e, iota = 1;
char buf[1024];
printf("\nconst (\n");
for(i=0; i<nelem(errors); i++) {
e = errors[i].value;
strcpy(buf, strerror(e));
// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
buf[0] += a - A;
printf("\t%s", errors[i].name);
if(iota) {
printf(" = APPLICATION_ERROR + iota");
iota = !iota;
}
printf("\n");
}
printf("\tEWINDOWS\n");
printf(")\n");
printf("\n// Error table\n");
printf("var errors = [...]string {\n");
for(i=0; i<nelem(errors); i++) {
e = errors[i].value;
for(j=0; j<i; j++)
if(errors[j].value == e) // duplicate value
goto next;
strcpy(buf, strerror(e));
// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
buf[0] += a - A;
printf("\t%s - APPLICATION_ERROR: \"%s\",\n", errors[i].name, buf);
next:;
}
printf("\tEWINDOWS - APPLICATION_ERROR: \"not supported by windows\",\n");
printf("}\n\n");
return 0;
}
'
) >_errors.c
$GCC $ccflags -static -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors
......@@ -216,8 +216,10 @@ while(<>) {
$ret[$i] = sprintf("r%d", $i);
$ret[$i+1] = sprintf("r%d", $i+1);
}
my $rettype = $type;
if($type =~ /^\*/) {
$reg = "unsafe.Pointer($reg)";
$rettype = "($rettype)";
}
if($i == 0) {
if($type eq "bool") {
......@@ -241,7 +243,7 @@ while(<>) {
$body .= "\t\t$name = 0;\n";
$body .= "\t}\n";
} else {
$body .= "\t$name = ($type)($reg);\n";
$body .= "\t$name = $rettype($reg);\n";
}
push @pout, sprintf "\"%s=\", %s, ", $name, $name;
}
......
......@@ -147,9 +147,12 @@ func getSysProcAddr(m uint32, pname string) uintptr {
// syscall interface implementation for other packages
func Errstr(errno int) string {
if errno == EWINDOWS {
return "not supported by windows"
// deal with special go errors
e := errno - APPLICATION_ERROR
if 0 <= e && e < len(errors) {
return errors[e]
}
// ask windows for the remaining errors
var flags uint32 = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_IGNORE_INSERTS
b := make([]uint16, 300)
n, err := FormatMessage(flags, 0, uint32(errno), 0, b, nil)
......
This diff is collapsed.
......@@ -887,13 +887,13 @@ func GetServByName(name string, proto string) (s *Servent, errno int) {
func Ntohs(netshort uint16) (u uint16) {
r0, _, _ := Syscall(procntohs, uintptr(netshort), 0, 0)
u = (uint16)(r0)
u = uint16(r0)
return
}
func DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status uint32) {
r0, _, _ := Syscall6(procDnsQuery_W, uintptr(unsafe.Pointer(StringToUTF16Ptr(name))), uintptr(qtype), uintptr(options), uintptr(unsafe.Pointer(extra)), uintptr(unsafe.Pointer(qrs)), uintptr(unsafe.Pointer(pr)))
status = (uint32)(r0)
status = uint32(r0)
return
}
......
......@@ -17,6 +17,22 @@ const (
SizeofCmsghdr = 0xc
)
const (
// Windows errors.
ERROR_FILE_NOT_FOUND = 2
ERROR_NO_MORE_FILES = 18
ERROR_BROKEN_PIPE = 109
ERROR_INSUFFICIENT_BUFFER = 122
ERROR_MOD_NOT_FOUND = 126
ERROR_PROC_NOT_FOUND = 127
ERROR_DIRECTORY = 267
ERROR_IO_PENDING = 997
// Go names for Windows errors.
ENOTDIR = ERROR_DIRECTORY
// Windows reserves errors >= 1<<29 for application use.
APPLICATION_ERROR = 1 << 29
)
const (
// Invented values to support what package os expects.
O_RDONLY = 0x00000
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment