344 Commits

Author SHA1 Message Date
Andrew Kelley
61850f8883 std: Windows: WSASocketW ensures WSAStartup
When WSASocketW gets WSANOTINITIALISED, now it will lock a mutex to
safely call WSAStartup and then try again one time.

This implementation:
 * Does not use recursion
 * Contains a detailed doc comment explaining why things are how they are
 * Is careful about which errors are surfaced in the respective error
   sets. `std.os.socket` intentionally does not have "not initialised"
   as one of the possible errors.
2021-05-20 12:28:30 -07:00
Bxil
85c2ffc9ba std.os: WSAStartup is now called upon socket creation when needed 2021-05-20 11:22:55 -07:00
Andrew Kelley
615d45da77 Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
 * src/codegen/spirv.zig
 * src/link/SpirV.zig

We're going to want to improve the stage2 test harness to print
the source file name when a compile error occurs otherwise std lib
contributors are going to see some confusing CI failures when they cause
stage2 AstGen compile errors.
2021-05-17 19:30:38 -07:00
Jonathan Marler
5042a47682 add missing EBADF error code for openat 2021-05-16 22:36:50 +02:00
Andrew Kelley
c9cc09a3bf Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
 * lib/std/os/linux.zig
 * lib/std/os/windows/bits.zig
 * src/Module.zig
 * src/Sema.zig
 * test/stage2/test.zig

Mainly I wanted Jakub's new macOS code for respecting stack size, since
we now depend on it for debug builds able to pass one of the test cases
for recursive comptime function calls with `@setEvalBranchQuota`.

The conflicts were all trivial.
2021-05-12 16:41:20 -07:00
LemonBoy
c065e12dbe std: Add more tests, fix broken code
It turns out that nothing in the test suite was exercising
preadv/pwritev and so the previous commits silently broke them.

Adding tests revealed readvAll and preadvAll were also broken and not
covered by any test.
2021-05-11 12:33:48 +02:00
LemonBoy
31f1cc9a0d std: Harmonize use of off_t between libc and Zig impls
Let's follow the libc/kernel convention and use an i64 for offsets, we
bitcast as needed and avoid the useless conditional casts.
2021-05-11 12:33:48 +02:00
LemonBoy
f4a05286d3 std: Fix cast that's sometimes invalid 2021-05-11 12:33:48 +02:00
LemonBoy
a909db6aea std: Prefer 64bit libc functions where possible
While musl decided to hard-wire off_t to a 64bit quantity, glibc is much
older and defaults to 32bit offsets and offers some -64 suffixed
versions of I/O functions.

There's a weird mix-up of types: sometimes off_t is used, sometimes not,
sometimes it's defined as a signed quantity and sometimes as an unsigned
one, but we'll sort this problem later.
2021-05-11 12:33:47 +02:00
lithdew
6d41e08664 x/os, os: fix setsockopt on windows, simplify Socket.setOption error set 2021-05-10 19:22:31 +09:00
lithdew
3d946ef5eb x/os, x/net, os:, fix typos/errors, add missing constants/docs
Add missing constants to ws2_32 such as SIO_BASE_HANDLE.

Made all declared constants in ws2_32 comptime_int, and fixed a few
broken constants (i.e. GUID's).

Fixed a few syscalls not having callconv(WINAPI) in ws2_32.

Fixed a typo in std.x.net.tcp.Client.

Removed unnecessary @alignCast's for socket addresses in
std.x.net.Socket.

Added a warning on using timeout methods for std.x.net.Socket.

Fixed compilation error spotted by CI in std.os that references
std.os.windows.ws2_32.SD_RECEIVE.

Renamed std.x.os.Socket.setOption()'s parameter `name: u32` to `code:
u32`.
2021-05-10 19:22:31 +09:00
lithdew
d7b601b35e std/os, x/os/socket: windows support, socket helpers, getpeername()
Socket I/O methods such as read, readv, write, writev, send, recv,
sendmsg, recvmsg have been generalized to read(buf, flags), write(buf,
flags), readVectorized(vectors, flags), and writeVectorized(vectors,
flags). There is still some work left to be done abstracting both
readVectorized and writeVectorized properly across platforms, which is
work to be done in a future PR.

Support for setting the linger timeout of a socket, querying the remote
address of a socket, setting whether or not keep-alive messages are to
be sent through a connection-oriented socket periodically depending on
host operating system settings has been added.

`std.io.Reader` and `std.io.Writer` wrappers around `Socket` has been
implemented, which wrap around Socket.read(buf, flags) and
Socket.write(buf, flags). Both wrappers may be provided flags which are
passed to Socket.read / Socket.write accordingly.

Cross-platform support for `getpeername()` has been implemented.

Windows support for the new `std.x.os.Socket` has been implemented. To
accomplish this, a full refactor of `std.os.windows.ws2_32` has been
done to supply any missing definitions and constants based on
auto-generated Windows syscall bindings by @marler8997.

`std.x.net.TCP.Listener.setQuickACK` has been moved to
`std.x.net.TCP.Client.setQuickACK`.

Windows support for resolving the scope ID of an interface name
specified in an IPv6 address has been provided.

`sockaddr_storage` definitions have been provided for Windows, Linux,
and Darwin. `sockaddr_storage` is used to allocate space before any
socket addresses are queried via. calls such as accept(), getsockname(),
and getpeername().

Zig-friendly wrappers for GetQueuedCompletionStatusEx(), getpeername(),
SetConsoleCtrlHandler(), SetFileCompletionNotificationModes() syscalls
on Windows have been provided.

Socket.setOption() was provided to set the value of a socket option in
place of os.setsockopt. Socket.getOption() will be provided in a future
PR.

There is still further work to be done regarding querying socket option
values on Windows, which is to be done in a subsequent PR.
2021-05-10 19:22:31 +09:00
Frank Denis
c3675d9351 Shorten 2021-05-09 18:17:09 +02:00
Frank Denis
4e399ef62c Initialize the Stat structure
The system `stat` structure includes padding, and, on some
operating systems such as all BSDs, "spare" bytes at the end.

We can't reliably compare two `Stat` values if these are
uninitialized, while being later compared.

This is what was causing the `fstatat` test to fail on FreeBSD
since the update to LLVM 12. It was previously only passing by
accident.
2021-05-09 18:15:42 +02:00
Andrew Kelley
dc28f5c3ec Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
	lib/std/crypto/25519/field.zig
	lib/std/crypto/poly1305.zig

I had resolved those by removing `comptime` but master branch decided to
make the parameters `comptime`.

This also pulls in the updated default `zig build` install directory.
2021-04-30 23:13:17 -07:00
Devin Bayer
d3f55782b0
rename doc refs to deprecated functions like openC (#8467) 2021-04-30 21:31:01 +02:00
Yorhel
f79b487337 Handle EPERM and ELOOP in os.fstatat() 2021-04-30 15:08:07 +02:00
Andrew Kelley
429cd2b5dd std: change @import("builtin") to std.builtin 2021-04-15 19:06:39 -07:00
lithdew
81adcd533e os/posix: handle ECONNRESET for write/writev 2021-04-14 16:40:06 -07:00
lithdew
2bfc6d14d5 os/linux: return error on EALREADY for connect() and getsockoptError()
When a connected socket file descriptor on Linux is re-acquired
    after being closed, through fuzz testing, it appears that a
    subsequent attempt to establish a connection with the file
    descriptor causes EALREADY to be reported.

    Instead of panicking, choose to return error.ConnectionPending
    to allow for users to handle this fairly rare case.
2021-04-06 11:30:20 +02:00
lithdew
59035ae3e9 os: handle ECONNRESET for connect() syscall 2021-04-01 14:18:49 +02:00
lithdew
4ce212739b os/linux: handle ECONNRESET for recv 2021-03-25 12:41:51 -07:00
jacob gw
83d0c2ed67 std: make all errors in os.zig public 2021-03-16 14:48:31 -07:00
Sébastien Marie
89e522b935 make std.c.getErrno() return same type as _errno() aka c_int
adjust std.os.unexpectedErrno() to be correct for all std.os.system.errno (c_int, u12, usize, ...)
2021-03-12 15:04:36 +01:00
daurnimator
1f17221bc4 std: add sendmsg 2021-02-28 21:57:43 +02:00
Andrew Kelley
4360f45d7f std.os: remove special case for haiku
This is an accident from a merge conflict introduced in
7edb204edfa41e11776ac009da5a20fb1c907f5f.

The new pipe2 code I believe is supposed to work for all posix-like
systems. If haiku needs special handling here, it should be
re-introduced.
2021-02-25 17:42:14 -07:00
Andrew Kelley
37a1d08de2 haiku: minor fixups
* no isHaiku() function since there is not more than one os tag that
   this applies to.
 * clean up some control flow into a switch
 * add some TODO comments to investigate panics that suspiciously look
   like they should be compile errors (see #363)
2021-02-25 16:54:32 -07:00
Al Hoang
c17396691c initial support for haiku sync update
* add cpu count
* use haiku find_directory
* add definitions and exports for building in haiku
2021-02-25 16:41:42 -07:00
Al Hoang
6b0372229d initial support for haiku continue clean up
* remove unused definitions
* setup os specific blocks
2021-02-25 16:41:42 -07:00
Al Hoang
025635c3f8 initial support for haiku past stage0 2021-02-25 16:41:42 -07:00
Andrew Kelley
5f35dc0c0d zig fmt the std lib 2021-02-24 21:29:23 -07:00
Tau
840331ee48 Rebase link(at) properly 2021-02-21 12:04:40 +02:00
Joran Dirk Greef
881ecdc72f Add MAX_RW_COUNT limit to std.os.pread()
Fixes: https://github.com/ziglang/zig/issues/7805
2021-01-25 10:41:38 -08:00
LemonBoy
134f5fd3d6 std: Update test "" to test where it makes sense 2021-01-22 15:46:58 +01:00
LemonBoy
6418f9ae91 std: Add missing cast when calling fcntl w/ constant args
comptime_int arguments are a big no no.
2021-01-18 18:02:09 +01:00
LemonBoy
9d18df142c std: Fixed pipe2 fallback
Use both F_SETFD and F_SETFL depending on what flag we're setting.

Closes #7760
2021-01-18 14:52:35 +01:00
Bill Nagel
2c79d669a7 add missing ECONNRESET from getsockoptError 2021-01-12 18:11:58 -08:00
Andrew Kelley
3fdffe85c0
Merge pull request #7720 from Snektron/sockopt
Some sockopt stuff
2021-01-07 23:50:34 -08:00
Jay Petacat
a9b505fa77 Reduce use of deprecated IO types
Related: #4917
2021-01-07 23:48:58 -08:00
Robin Voetter
ec7adeda2b Add EPERM to std.os.setsockopt 2021-01-08 02:34:42 +01:00
Felix (xq) Queißner
819f2a01a1 Fixes missing error prong in std.os.send. 2021-01-04 14:03:39 -08:00
Andrew Kelley
974c008a0e convert more {} to {d} and {s} 2021-01-02 19:03:14 -07:00
Frank Denis
6c2e0c2046 Year++ 2020-12-31 15:45:24 -08:00
Andrew Kelley
717cf00fe0 std.ChildProcess: improvements to collectOutputPosix
* read directly into the ArrayList buffers.
 * respect max_output_bytes

 * std.ArrayList:
   - make `allocatedSlice` public.
   - add `unusedCapacitySlice`.

I removed the Windows implementation of this stuff; I am doing a partial
merge of LemonBoy's patch with the understanding that a later patch can
add the Windows implementation after it is vetted.
2020-12-29 11:13:00 -07:00
LemonBoy
b297c2eae9 std: Fix compilation on FreeBSD/Darwin 2020-12-29 10:40:26 -07:00
Andrew Kelley
3f9588ca29 std: do not call malloc() between fork() and execv()
We were violating the POSIX standard which resulted in a deadlock on
musl v1.1.24 on aarch64 alpine linux, uncovered with the new ThreadPool
usage in the stage2 compiler.

std.os execv functions that accept an Allocator parameter are removed
because they are footguns. The POSIX standard does not allow calls to
malloc() between fork() and execv() and since it is common to both
(1) call execv() after fork() and (2) use std.heap.c_allocator,
Programmers are encouraged to go through the `std.process` API
instead, causing some dissonance when combined with `std.os` APIs.

I also slapped a big warning message on all the relevant doc comments.
2020-12-26 13:50:26 -07:00
luna
6a75cfd0f6
cast sendto to SendError inside send (#7481)
* cast sendto to SendError inside send

* remove WouldBlock from SendToError

* add missing ENOTCONN mapping

* remove SystemResources duplicate

* move NetworkUnreachable to SendError

* add NetworkSubsystemFailed to SendError

* Use zig's implicit error set casting

Co-authored-by: Andrew Kelley <andrew@ziglang.org>
2020-12-22 16:47:11 -05:00
Andrew Kelley
53987c932c std.crypto.random: introduce fork safety
Everybody gets what they want!

 * AT_RANDOM is completely ignored.
 * On Linux, MADV_WIPEONFORK is used to provide fork safety.
 * On pthread systems, `pthread_atfork` is used to provide fork safety.
 * For systems that do not have the capability to provide fork safety,
   the implementation falls back to calling getrandom() every time.
 * If madvise is unavailable or returns an error, or pthread_atfork
   fails for whatever reason, it falls back to calling getrandom() every
   time.
 * Applications may choose to opt-out of fork safety.
 * Applications may choose to opt-in to unconditionally calling
   getrandom() for every call to std.crypto.random.fillFn.
 * Added `std.meta.globalOption`.
 * Added `std.os.madvise` and related bits.
 * Bumped up the size of the main thread TLS buffer. See the comment
   there for justification.
 * Simpler hot path in TLS initialization.
2020-12-18 15:54:01 -07:00
luna
0c33624a45
create SendToError (#7417)
* add SendToError

* remove error catch

* add missing SendToError entries

* add mappings to new errors for posix

* map windows sendto() errors
2020-12-15 14:56:42 -05:00
LemonBoy
beae3cea17 std: Improve sigaction interface
Add a smoke test to prevent regressions.
2020-12-12 13:57:25 +01:00