gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

libc_darwin_amd64.go (11107B)


      1 // Copyright 2020 The Libc Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package libc // import "modernc.org/libc"
      6 
      7 import (
      8 	"strings"
      9 	"unsafe"
     10 
     11 	"golang.org/x/sys/unix"
     12 	"modernc.org/libc/fcntl"
     13 	"modernc.org/libc/signal"
     14 	"modernc.org/libc/sys/types"
     15 	"modernc.org/libc/utime"
     16 )
     17 
     18 // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
     19 func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
     20 	var kact, koldact uintptr
     21 	if act != 0 {
     22 		sz := int(unsafe.Sizeof(signal.X__sigaction{}))
     23 		kact = t.Alloc(sz)
     24 		defer t.Free(sz)
     25 		(*signal.X__sigaction)(unsafe.Pointer(kact)).F__sigaction_u.F__sa_handler = (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_u.F__sa_handler
     26 		(*signal.X__sigaction)(unsafe.Pointer(kact)).Fsa_flags = (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags
     27 		Xmemcpy(t, kact+unsafe.Offsetof(signal.X__sigaction{}.Fsa_mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(signal.Sigset_t(0))))
     28 	}
     29 	if oldact != 0 {
     30 		panic(todo(""))
     31 	}
     32 
     33 	if _, _, err := unix.Syscall6(unix.SYS_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(signal.Sigset_t(0)), 0, 0); err != 0 {
     34 		t.setErrno(err)
     35 		return -1
     36 	}
     37 
     38 	if oldact != 0 {
     39 		panic(todo(""))
     40 	}
     41 
     42 	return 0
     43 }
     44 
     45 // int fcntl(int fd, int cmd, ... /* arg */ );
     46 func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) (r int32) {
     47 	var err error
     48 	var p uintptr
     49 	var i int
     50 	switch cmd {
     51 	case fcntl.F_GETLK, fcntl.F_SETLK:
     52 		p = *(*uintptr)(unsafe.Pointer(args))
     53 		err = unix.FcntlFlock(uintptr(fd), int(cmd), (*unix.Flock_t)(unsafe.Pointer(p)))
     54 	case fcntl.F_GETFL, fcntl.F_FULLFSYNC:
     55 		i, err = unix.FcntlInt(uintptr(fd), int(cmd), 0)
     56 		r = int32(i)
     57 	case fcntl.F_SETFD, fcntl.F_SETFL:
     58 		arg := *(*int32)(unsafe.Pointer(args))
     59 		_, err = unix.FcntlInt(uintptr(fd), int(cmd), int(arg))
     60 	default:
     61 		panic(todo("%v: %v %v", origin(1), fd, cmd))
     62 	}
     63 	if err != nil {
     64 		if dmesgs {
     65 			dmesg("%v: fd %v cmd %v p %#x: %v FAIL", origin(1), fcntlCmdStr(fd), cmd, p, err)
     66 		}
     67 		t.setErrno(err)
     68 		return -1
     69 	}
     70 
     71 	if dmesgs {
     72 		dmesg("%v: %d %s %#x: ok", origin(1), fd, fcntlCmdStr(cmd), p)
     73 	}
     74 	return r
     75 }
     76 
     77 // int lstat(const char *pathname, struct stat *statbuf);
     78 func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
     79 	if err := unix.Lstat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
     80 		if dmesgs {
     81 			dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
     82 		}
     83 		t.setErrno(err)
     84 		return -1
     85 	}
     86 
     87 	if dmesgs {
     88 		dmesg("%v: %q: ok", origin(1), GoString(pathname))
     89 	}
     90 	return 0
     91 }
     92 
     93 // int stat(const char *pathname, struct stat *statbuf);
     94 func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
     95 	if err := unix.Stat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
     96 		if dmesgs {
     97 			dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
     98 		}
     99 		t.setErrno(err)
    100 		return -1
    101 	}
    102 
    103 	if dmesgs {
    104 		dmesg("%v: %q: ok", origin(1), GoString(pathname))
    105 	}
    106 	return 0
    107 }
    108 
    109 // int fstatfs(int fd, struct statfs *buf);
    110 func Xfstatfs(t *TLS, fd int32, buf uintptr) int32 {
    111 	if err := unix.Fstatfs(int(fd), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
    112 		if dmesgs {
    113 			dmesg("%v: %v: %v FAIL", origin(1), fd, err)
    114 		}
    115 		t.setErrno(err)
    116 		return -1
    117 	}
    118 
    119 	if dmesgs {
    120 		dmesg("%v: %v: ok", origin(1), fd)
    121 	}
    122 	return 0
    123 }
    124 
    125 // int statfs(const char *path, struct statfs *buf);
    126 func Xstatfs(t *TLS, path uintptr, buf uintptr) int32 {
    127 	if err := unix.Statfs(GoString(path), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
    128 		if dmesgs {
    129 			dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
    130 		}
    131 		t.setErrno(err)
    132 		return -1
    133 	}
    134 
    135 	if dmesgs {
    136 		dmesg("%v: %q: ok", origin(1), GoString(path))
    137 	}
    138 	return 0
    139 }
    140 
    141 // int fstat(int fd, struct stat *statbuf);
    142 func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
    143 	if err := unix.Fstat(int(fd), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
    144 		if dmesgs {
    145 			dmesg("%v: fd %d: %v FAIL", origin(1), fd, err)
    146 		}
    147 		t.setErrno(err)
    148 		return -1
    149 	}
    150 
    151 	if dmesgs {
    152 		dmesg("%v: fd %d: ok", origin(1), fd)
    153 	}
    154 	return 0
    155 }
    156 
    157 // off64_t lseek64(int fd, off64_t offset, int whence);
    158 func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
    159 	n, err := unix.Seek(int(fd), int64(offset), int(whence))
    160 	if err != nil {
    161 		if dmesgs {
    162 			dmesg("%v: %v FAIL", origin(1), err)
    163 		}
    164 		t.setErrno(err)
    165 		return -1
    166 	}
    167 
    168 	if dmesgs {
    169 		dmesg("%v: ok", origin(1))
    170 	}
    171 	return types.Off_t(n)
    172 }
    173 
    174 // int utime(const char *filename, const struct utimbuf *times);
    175 func Xutime(t *TLS, filename, times uintptr) int32 {
    176 	var a []unix.Timeval
    177 	if times != 0 {
    178 		a = make([]unix.Timeval, 2)
    179 		a[0].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Factime
    180 		a[1].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Fmodtime
    181 	}
    182 	if err := unix.Utimes(GoString(filename), a); err != nil {
    183 		if dmesgs {
    184 			dmesg("%v: %v FAIL", origin(1), err)
    185 		}
    186 		t.setErrno(err)
    187 		return -1
    188 	}
    189 
    190 	if dmesgs {
    191 		dmesg("%v: ok", origin(1))
    192 	}
    193 	return 0
    194 }
    195 
    196 // unsigned int alarm(unsigned int seconds);
    197 func Xalarm(t *TLS, seconds uint32) uint32 {
    198 	panic(todo(""))
    199 	// n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
    200 	// if err != 0 {
    201 	// 	panic(todo(""))
    202 	// }
    203 
    204 	// return uint32(n)
    205 }
    206 
    207 // time_t time(time_t *tloc);
    208 func Xtime(t *TLS, tloc uintptr) types.Time_t {
    209 	panic(todo(""))
    210 	// n := time.Now().UTC().Unix()
    211 	// if tloc != 0 {
    212 	// 	*(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
    213 	// }
    214 	// return types.Time_t(n)
    215 }
    216 
    217 // // int getrlimit(int resource, struct rlimit *rlim);
    218 // func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
    219 // 	if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
    220 // 		t.setErrno(err)
    221 // 		return -1
    222 // 	}
    223 //
    224 // 	return 0
    225 // }
    226 
    227 // int mkdir(const char *path, mode_t mode);
    228 func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
    229 	if err := unix.Mkdir(GoString(path), uint32(mode)); err != nil {
    230 		if dmesgs {
    231 			dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
    232 		}
    233 		t.setErrno(err)
    234 		return -1
    235 	}
    236 
    237 	if dmesgs {
    238 		dmesg("%v: %q: ok", origin(1), GoString(path))
    239 	}
    240 	return 0
    241 }
    242 
    243 // int symlink(const char *target, const char *linkpath);
    244 func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
    245 	if err := unix.Symlink(GoString(target), GoString(linkpath)); err != nil {
    246 		if dmesgs {
    247 			dmesg("%v: %v FAIL", origin(1), err)
    248 		}
    249 		t.setErrno(err)
    250 		return -1
    251 	}
    252 
    253 	if dmesgs {
    254 		dmesg("%v: ok", origin(1))
    255 	}
    256 	return 0
    257 }
    258 
    259 // int chmod(const char *pathname, mode_t mode)
    260 func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
    261 	if err := unix.Chmod(GoString(pathname), uint32(mode)); err != nil {
    262 		if dmesgs {
    263 			dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err)
    264 		}
    265 		t.setErrno(err)
    266 		return -1
    267 	}
    268 
    269 	if dmesgs {
    270 		dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
    271 	}
    272 	return 0
    273 }
    274 
    275 // int utimes(const char *filename, const struct timeval times[2]);
    276 func Xutimes(t *TLS, filename, times uintptr) int32 {
    277 	var a []unix.Timeval
    278 	if times != 0 {
    279 		a = make([]unix.Timeval, 2)
    280 		a[0] = *(*unix.Timeval)(unsafe.Pointer(times))
    281 		a[1] = *(*unix.Timeval)(unsafe.Pointer(times + unsafe.Sizeof(unix.Timeval{})))
    282 	}
    283 	if err := unix.Utimes(GoString(filename), a); err != nil {
    284 		if dmesgs {
    285 			dmesg("%v: %v FAIL", origin(1), err)
    286 		}
    287 		t.setErrno(err)
    288 		return -1
    289 	}
    290 
    291 	if dmesgs {
    292 		dmesg("%v: ok", origin(1))
    293 	}
    294 	return 0
    295 }
    296 
    297 // int unlink(const char *pathname);
    298 func Xunlink(t *TLS, pathname uintptr) int32 {
    299 	if err := unix.Unlink(GoString(pathname)); err != nil {
    300 		if dmesgs {
    301 			dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
    302 		}
    303 		t.setErrno(err)
    304 		return -1
    305 	}
    306 
    307 	if dmesgs {
    308 		dmesg("%v: ok", origin(1))
    309 	}
    310 	return 0
    311 }
    312 
    313 // int access(const char *pathname, int mode);
    314 func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
    315 	if err := unix.Access(GoString(pathname), uint32(mode)); err != nil {
    316 		if dmesgs {
    317 			dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err)
    318 		}
    319 		t.setErrno(err)
    320 		return -1
    321 	}
    322 
    323 	if dmesgs {
    324 		dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
    325 	}
    326 	return 0
    327 }
    328 
    329 // int rename(const char *oldpath, const char *newpath);
    330 func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
    331 	if err := unix.Rename(GoString(oldpath), GoString(newpath)); err != nil {
    332 		if dmesgs {
    333 			dmesg("%v: %v FAIL", origin(1), err)
    334 		}
    335 		t.setErrno(err)
    336 		return -1
    337 	}
    338 
    339 	if dmesgs {
    340 		dmesg("%v: ok", origin(1))
    341 	}
    342 	return 0
    343 }
    344 
    345 // int mknod(const char *pathname, mode_t mode, dev_t dev);
    346 func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
    347 	panic(todo(""))
    348 	// if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 {
    349 	// 	t.setErrno(err)
    350 	// 	return -1
    351 	// }
    352 
    353 	// return 0
    354 }
    355 
    356 // int chown(const char *pathname, uid_t owner, gid_t group);
    357 func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
    358 	panic(todo(""))
    359 	// if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 {
    360 	// 	t.setErrno(err)
    361 	// 	return -1
    362 	// }
    363 
    364 	// return 0
    365 }
    366 
    367 // int link(const char *oldpath, const char *newpath);
    368 func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
    369 	panic(todo(""))
    370 	// if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
    371 	// 	t.setErrno(err)
    372 	// 	return -1
    373 	// }
    374 
    375 	// return 0
    376 }
    377 
    378 // int dup2(int oldfd, int newfd);
    379 func Xdup2(t *TLS, oldfd, newfd int32) int32 {
    380 	n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
    381 	if err != 0 {
    382 		t.setErrno(err)
    383 		return -1
    384 	}
    385 
    386 	return int32(n)
    387 }
    388 
    389 // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
    390 func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
    391 	var n int
    392 	var err error
    393 	switch {
    394 	case buf == 0 || bufsize == 0:
    395 		n, err = unix.Readlink(GoString(path), nil)
    396 	default:
    397 		n, err = unix.Readlink(GoString(path), (*RawMem)(unsafe.Pointer(buf))[:bufsize:bufsize])
    398 	}
    399 	if err != nil {
    400 		if dmesgs {
    401 			dmesg("%v: %v FAIL", err)
    402 		}
    403 		t.setErrno(err)
    404 		return -1
    405 	}
    406 
    407 	if dmesgs {
    408 		dmesg("%v: ok")
    409 	}
    410 	return types.Ssize_t(n)
    411 }
    412 
    413 // FILE *fopen64(const char *pathname, const char *mode);
    414 func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
    415 	m := strings.ReplaceAll(GoString(mode), "b", "")
    416 	var flags int
    417 	switch m {
    418 	case "r":
    419 		flags = fcntl.O_RDONLY
    420 	case "r+":
    421 		flags = fcntl.O_RDWR
    422 	case "w":
    423 		flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_TRUNC
    424 	case "w+":
    425 		flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_TRUNC
    426 	case "a":
    427 		flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_APPEND
    428 	case "a+":
    429 		flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_APPEND
    430 	default:
    431 		panic(m)
    432 	}
    433 	fd, err := unix.Open(GoString(pathname), int(flags), 0666)
    434 	if err != nil {
    435 		if dmesgs {
    436 			dmesg("%v: %q %q: %v FAIL", origin(1), GoString(pathname), GoString(mode), err)
    437 		}
    438 		t.setErrno(err)
    439 		return 0
    440 	}
    441 
    442 	if dmesgs {
    443 		dmesg("%v: %q %q: fd %v", origin(1), GoString(pathname), GoString(mode), fd)
    444 	}
    445 	if p := newFile(t, int32(fd)); p != 0 {
    446 		return p
    447 	}
    448 
    449 	panic("OOM")
    450 }