summaryrefslogtreecommitdiff
path: root/src/pkg/syscall/syscall_mingw.go
blob: 99f649e4fc2aa9f31e52e603e7eec9a20075c731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// 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.

// Windows system calls.

package syscall

import (
	"unsafe"
	"utf16"
)

const OS = "mingw"

/*

small demo to detect version of windows you are running:

package main

import (
	"syscall"
)

func abort(funcname string, err int) {
	panic(funcname + " failed: " + syscall.Errstr(err))
}

func print_version(v uint32) {
	major := byte(v)
	minor := uint8(v >> 8)
	build := uint16(v >> 16)
	print("windows version ", major, ".", minor, " (Build ", build, ")\n")
}

func main() {
	h, err := syscall.LoadLibrary("kernel32.dll")
	if err != 0 {
		abort("LoadLibrary", err)
	}
	defer syscall.FreeLibrary(h)
	proc, err := syscall.GetProcAddress(h, "GetVersion")
	if err != 0 {
		abort("GetProcAddress", err)
	}
	r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0)
	print_version(uint32(r))
}

*/

// StringToUTF16 returns the UTF-16 encoding of the UTF-8 string s,
// with a terminating NUL added.
func StringToUTF16(s string) []uint16 { return utf16.Encode([]int(s + "\x00")) }

// UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,
// with a terminating NUL removed.
func UTF16ToString(s []uint16) string {
	for i, v := range s {
		if v == 0 {
			s = s[0:i]
			break
		}
	}
	return string(utf16.Decode(s))
}

// StringToUTF16Ptr returns pointer to the UTF-16 encoding of
// the UTF-8 string s, with a terminating NUL added.
func StringToUTF16Ptr(s string) *uint16 { return &StringToUTF16(s)[0] }

// dll helpers

// implemented in ../pkg/runtime/mingw/syscall.cgo
func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, lasterr uintptr)
func loadlibraryex(filename uintptr) (handle uint32)
func getprocaddress(handle uint32, procname uintptr) (proc uintptr)

func loadDll(fname string) uint32 {
	m := loadlibraryex(uintptr(unsafe.Pointer(StringBytePtr(fname))))
	if m == 0 {
		panic("syscall: could not LoadLibraryEx " + fname)
	}
	return m
}

func getSysProcAddr(m uint32, pname string) uintptr {
	p := getprocaddress(m, uintptr(unsafe.Pointer(StringBytePtr(pname))))
	if p == 0 {
		panic("syscall: could not GetProcAddress for " + pname)
	}
	return p
}

// windows api calls

//sys	GetLastError() (lasterrno int)
//sys	LoadLibrary(libname string) (handle uint32, errno int) = LoadLibraryW
//sys	FreeLibrary(handle uint32) (ok bool, errno int)
//sys	GetProcAddress(module uint32, procname string) (proc uint32, errno int)
//sys	GetVersion() (ver uint32, errno int)
//sys	FormatMessage(flags uint32, msgsrc uint32, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, errno int) = FormatMessageW
//sys	ExitProcess(exitcode uint32)
//sys	CreateFile(name *uint16, access uint32, mode uint32, sa *byte, createmode uint32, attrs uint32, templatefile int32) (handle int32, errno int) [failretval=-1] = CreateFileW
//sys	ReadFile(handle int32, buf []byte, done *uint32, overlapped *Overlapped) (ok bool, errno int)
//sys	WriteFile(handle int32, buf []byte, done *uint32, overlapped *Overlapped) (ok bool, errno int)
//sys	SetFilePointer(handle int32, lowoffset int32, highoffsetptr *int32, whence uint32) (newlowoffset uint32, errno int) [failretval=0xffffffff]
//sys	CloseHandle(handle int32) (ok bool, errno int)
//sys	GetStdHandle(stdhandle int32) (handle int32, errno int) [failretval=-1]
//sys	FindFirstFile(name *uint16, data *Win32finddata) (handle int32, errno int) [failretval=-1] = FindFirstFileW
//sys	FindNextFile(handle int32, data *Win32finddata) (ok bool, errno int) = FindNextFileW
//sys	FindClose(handle int32) (ok bool, errno int)

// syscall interface implementation for other packages

func Errstr(errno int) string {
	if errno == EMINGW {
		return "not supported by windows"
	}
	var b = make([]uint16, 300)
	n, err := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ARGUMENT_ARRAY, 0, uint32(errno), 0, b, nil)
	if err != 0 {
		return "error " + str(errno) + " (FormatMessage failed with err=" + str(err) + ")"
	}
	return string(utf16.Decode(b[0 : n-1]))
}

func Exit(code int) { ExitProcess(uint32(code)) }

func Open(path string, mode int, perm int) (fd int, errno int) {
	if len(path) == 0 {
		return -1, ERROR_FILE_NOT_FOUND
	}
	var access uint32
	switch mode & (O_RDONLY | O_WRONLY | O_RDWR) {
	case O_RDONLY:
		access = GENERIC_READ
	case O_WRONLY:
		access = GENERIC_WRITE
	case O_RDWR:
		access = GENERIC_READ | GENERIC_WRITE
	}
	if mode&O_CREAT != 0 {
		access |= GENERIC_WRITE
	}
	sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE)
	var createmode uint32
	switch {
	case mode&O_CREAT != 0:
		if mode&O_EXCL != 0 {
			createmode = CREATE_NEW
		} else {
			createmode = CREATE_ALWAYS
		}
	case mode&O_TRUNC != 0:
		createmode = TRUNCATE_EXISTING
	default:
		createmode = OPEN_EXISTING
	}
	h, e := CreateFile(StringToUTF16Ptr(path), access, sharemode, nil, createmode, FILE_ATTRIBUTE_NORMAL, 0)
	return int(h), int(e)
}

func Read(fd int, p []byte) (n int, errno int) {
	var done uint32
	if ok, e := ReadFile(int32(fd), p, &done, nil); !ok {
		return 0, e
	}
	return int(done), 0
}

// TODO(brainman): ReadFile/WriteFile change file offset, therefore
// i use Seek here to preserve semantics of unix pread/pwrite,
// not sure if I should do that

func Pread(fd int, p []byte, offset int64) (n int, errno int) {
	var o Overlapped
	o.OffsetHigh = uint32(offset >> 32)
	o.Offset = uint32(offset)
	curoffset, e := Seek(fd, 0, 1)
	if e != 0 {
		return 0, e
	}
	var done uint32
	if ok, e := ReadFile(int32(fd), p, &done, &o); !ok {
		return 0, e
	}
	_, e = Seek(fd, curoffset, 0)
	if e != 0 {
		return 0, e
	}
	return int(done), 0
}

func Write(fd int, p []byte) (n int, errno int) {
	var done uint32
	if ok, e := WriteFile(int32(fd), p, &done, nil); !ok {
		return 0, e
	}
	return int(done), 0
}

func Pwrite(fd int, p []byte, offset int64) (n int, errno int) {
	var o Overlapped
	o.OffsetHigh = uint32(offset >> 32)
	o.Offset = uint32(offset)
	curoffset, e := Seek(fd, 0, 1)
	if e != 0 {
		return 0, e
	}
	var done uint32
	if ok, e := WriteFile(int32(fd), p, &done, &o); !ok {
		return 0, e
	}
	_, e = Seek(fd, curoffset, 0)
	if e != 0 {
		return 0, e
	}
	return int(done), 0
}

func Seek(fd int, offset int64, whence int) (newoffset int64, errno int) {
	var w uint32
	switch whence {
	case 0:
		w = FILE_BEGIN
	case 1:
		w = FILE_CURRENT
	case 2:
		w = FILE_END
	}
	hi := int32(offset >> 32)
	lo := int32(offset)
	rlo, e := SetFilePointer(int32(fd), lo, &hi, w)
	if e != 0 {
		return 0, e
	}
	return int64(hi)<<32 + int64(rlo), 0
}

func Close(fd int) (errno int) {
	if ok, e := CloseHandle(int32(fd)); !ok {
		return e
	}
	return 0
}

var (
	Stdin  = getStdHandle(STD_INPUT_HANDLE)
	Stdout = getStdHandle(STD_OUTPUT_HANDLE)
	Stderr = getStdHandle(STD_ERROR_HANDLE)
)

func getStdHandle(h int32) (fd int) {
	r, _ := GetStdHandle(h)
	return int(r)
}

func Stat(path string, stat *Stat_t) (errno int) {
	h, e := FindFirstFile(StringToUTF16Ptr(path), &stat.Windata)
	if e != 0 {
		return e
	}
	defer FindClose(h)
	stat.Mode = 0
	return 0
}

func Lstat(path string, stat *Stat_t) (errno int) {
	// no links on windows, just call Stat
	return Stat(path, stat)
}

// TODO(brainman): fix all needed for os

const (
	SIGTRAP = 5
)

func Getpid() (pid int)   { return -1 }
func Getppid() (ppid int) { return -1 }

func Mkdir(path string, mode int) (errno int)             { return EMINGW }
func Fstat(fd int, stat *Stat_t) (errno int)              { return EMINGW }
func Chdir(path string) (errno int)                       { return EMINGW }
func Fchdir(fd int) (errno int)                           { return EMINGW }
func Unlink(path string) (errno int)                      { return EMINGW }
func Rmdir(path string) (errno int)                       { return EMINGW }
func Link(oldpath, newpath string) (errno int)            { return EMINGW }
func Symlink(path, link string) (errno int)               { return EMINGW }
func Readlink(path string, buf []byte) (n int, errno int) { return 0, EMINGW }
func Rename(oldpath, newpath string) (errno int)          { return EMINGW }
func Chmod(path string, mode int) (errno int)             { return EMINGW }
func Fchmod(fd int, mode int) (errno int)                 { return EMINGW }
func Chown(path string, uid int, gid int) (errno int)     { return EMINGW }
func Lchown(path string, uid int, gid int) (errno int)    { return EMINGW }
func Fchown(fd int, uid int, gid int) (errno int)         { return EMINGW }
func Truncate(name string, size int64) (errno int)        { return EMINGW }
func Ftruncate(fd int, length int64) (errno int)          { return EMINGW }

const ImplementsGetwd = true

func Getwd() (wd string, errno int)        { return "", EMINGW }
func Getuid() (uid int)                    { return -1 }
func Geteuid() (euid int)                  { return -1 }
func Getgid() (gid int)                    { return -1 }
func Getegid() (egid int)                  { return -1 }
func Getgroups() (gids []int, errno int)   { return nil, EMINGW }
func Gettimeofday(tv *Timeval) (errno int) { return EMINGW }

// TODO(brainman): fix all this meaningless code, it is here to compile exec.go

func Pipe(p []int) (errno int) { return EMINGW }

func read(fd int, buf *byte, nbuf int) (n int, errno int) {
	return 0, EMINGW
}

func fcntl(fd, cmd, arg int) (val int, errno int) {
	return 0, EMINGW
}

const (
	PTRACE_TRACEME = 1 + iota
	WNOHANG
	WSTOPPED
	SYS_CLOSE
	SYS_WRITE
	SYS_EXIT
	SYS_READ
)

type Rusage struct {
	Utime    Timeval
	Stime    Timeval
	Maxrss   int32
	Ixrss    int32
	Idrss    int32
	Isrss    int32
	Minflt   int32
	Majflt   int32
	Nswap    int32
	Inblock  int32
	Oublock  int32
	Msgsnd   int32
	Msgrcv   int32
	Nsignals int32
	Nvcsw    int32
	Nivcsw   int32
}

func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, errno int) {
	return 0, EMINGW
}

type WaitStatus uint32

func (WaitStatus) Exited() bool { return false }

func (WaitStatus) ExitStatus() int { return -1 }

func (WaitStatus) Signal() int { return -1 }

func (WaitStatus) CoreDump() bool { return false }

func (WaitStatus) Stopped() bool { return false }

func (WaitStatus) Continued() bool { return false }

func (WaitStatus) StopSignal() int { return -1 }

func (WaitStatus) Signaled() bool { return false }

func (WaitStatus) TrapCause() int { return -1 }