zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit aa043a633904b6685a201000a0ab2a62fc3bdb91 (tree)
parent 96c9a9bdb3f7b10aa1ce4833bf6adb0af1c82dc9
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Wed,  7 Feb 2018 17:27:30 -0500

Merge remote-tracking branch 'origin/master' into llvm6

Diffstat:
MCMakeLists.txt | 18+++++++++---------
Mdoc/langref.html.in | 144+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
Msrc/all_types.hpp | 9+++++++++
Msrc/analyze.cpp | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Msrc/analyze.hpp | 2+-
Msrc/ir.cpp | 37++++++++++++++++++++++++++++++-------
Mstd/c/linux.zig | 2+-
Mstd/fmt/index.zig | 16+++++++++++++++-
Mstd/index.zig | 1+
Mstd/io.zig | 2+-
Mstd/os/index.zig | 55++++++++++++++++++++++++++++++++++++++-----------------
Dstd/os/linux.zig | 796-------------------------------------------------------------------------------
Rstd/os/linux_errno.zig -> std/os/linux/errno.zig | 0
Astd/os/linux/i386.zig | 505+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astd/os/linux/index.zig | 796+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astd/os/linux/test.zig | 38++++++++++++++++++++++++++++++++++++++
Astd/os/linux/x86_64.zig | 490+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dstd/os/linux_i386.zig | 504-------------------------------------------------------------------------------
Dstd/os/linux_test.zig | 38--------------------------------------
Dstd/os/linux_x86_64.zig | 489-------------------------------------------------------------------------------
Mtest/compile_errors.zig | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
21 files changed, 2193 insertions(+), 1915 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -201,29 +201,29 @@ else() COMPILE_FLAGS ${ZIG_LLD_COMPILE_FLAGS} LINK_FLAGS " " ) - target_include_directories(embedded_lld_lib PUBLIC + target_include_directories(embedded_lld_lib PRIVATE "${CMAKE_SOURCE_DIR}/deps/lld/include" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt" ) - target_include_directories(embedded_lld_elf PUBLIC + target_include_directories(embedded_lld_elf PRIVATE "${CMAKE_SOURCE_DIR}/deps/lld/ELF" "${CMAKE_SOURCE_DIR}/deps/lld/include" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/ELF" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt" ) - target_include_directories(embedded_lld_coff PUBLIC + target_include_directories(embedded_lld_coff PRIVATE "${CMAKE_SOURCE_DIR}/deps/lld/COFF" "${CMAKE_SOURCE_DIR}/deps/lld/include" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/COFF" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt" ) - target_include_directories(embedded_lld_mingw PUBLIC + target_include_directories(embedded_lld_mingw PRIVATE "${CMAKE_SOURCE_DIR}/deps/lld/MinGW" "${CMAKE_SOURCE_DIR}/deps/lld/include" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/MinGW" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt" ) - target_include_directories(embedded_lld_wasm PUBLIC + target_include_directories(embedded_lld_wasm PRIVATE "${CMAKE_SOURCE_DIR}/deps/lld/wasm" "${CMAKE_SOURCE_DIR}/deps/lld/include" "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/wasm" @@ -482,10 +482,10 @@ set(ZIG_STD_FILES "os/darwin_errno.zig" "os/get_user_id.zig" "os/index.zig" - "os/linux.zig" - "os/linux_errno.zig" - "os/linux_i386.zig" - "os/linux_x86_64.zig" + "os/linux/index.zig" + "os/linux/errno.zig" + "os/linux/i386.zig" + "os/linux/x86_64.zig" "os/path.zig" "os/windows/error.zig" "os/windows/index.zig" diff --git a/doc/langref.html.in b/doc/langref.html.in @@ -36,25 +36,47 @@ code { font-size: 12pt; } - @media screen and (min-width: 28.75em) { + pre > code { + display: block; + overflow: auto; + } + .table-wrapper { + width: 100%; + overflow-y: auto; + } + /* Desktop */ + @media screen and (min-width: 56.25em) { #nav { width: 20em; height: 100%; - overflow-y: scroll; position: fixed; + overflow-y: scroll; left: 0; top: 0; + padding-left: 1em; } #contents { - max-width: 50em; + max-width: 60em; padding-left: 22em; + padding: 1em; + padding-left: 24em; + } + } + /* Mobile */ + @media screen and (max-width: 56.25em) { + body, code { + font-size: small; + } + #nav { + border-bottom: 1px solid grey; } } </style> </head> <body> <div id="nav"> - {#nav#} + <h3>Index</h3> + {#nav#} </div> <div id="contents"> {#header_open|Introduction#} @@ -112,16 +134,6 @@ pub fn main() void { </p> {#see_also|Values|@import|Errors|Root Source File#} {#header_close#} - {#header_open|Source Encoding#} - <p>Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.</p> - <p>Throughout all zig source code (including in comments), some codepoints are never allowed:</p> - <ul> - <li>Ascii control characters, except for U+000a (LF): U+0000 - U+0009, U+000b - U+0001f, U+007f. (Note that Windows line endings (CRLF) are not allowed, and hard tabs are not allowed.)</li> - <li>Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).</li> - </ul> - <p>The codepoint U+000a (LF) (which is encoded as the single-byte value 0x0a) is the line terminator character. This character always terminates a line of zig source code (except possbly the last line of the file).</p> - <p>For some discussion on the rationale behind these design decisions, see <a href="https://github.com/zig-lang/zig/issues/663">issue #663</a></p> - {#header_close#} {#header_open|Values#} {#code_begin|exe|values#} const std = @import("std"); @@ -173,6 +185,7 @@ pub fn main() %void { } {#code_end#} {#header_open|Primitive Types#} + <div class="table-wrapper"> <table> <tr> <th> @@ -398,9 +411,11 @@ pub fn main() %void { <td>an error code</td> </tr> </table> + </div> {#see_also|Integers|Floats|void|Errors#} {#header_close#} {#header_open|Primitive Values#} + <div class="table-wrapper"> <table> <tr> <th> @@ -427,6 +442,7 @@ pub fn main() %void { <td>refers to the thing in immediate scope</td> </tr> </table> + </div> {#see_also|Nullables|this#} {#header_close#} {#header_open|String Literals#} @@ -451,6 +467,7 @@ test "string literals" { {#code_end#} {#see_also|Arrays|Zig Test#} {#header_open|Escape Sequences#} + <div class="table-wrapper"> <table> <tr> <th> @@ -497,6 +514,7 @@ test "string literals" { <td>hexadecimal 24-bit Unicode character code UTF-8 encoded (6 digits)</td> </tr> </table> + </div> <p>Note that the maximum valid Unicode point is <code>0x10ffff</code>.</p> {#header_close#} {#header_open|Multiline String Literals#} @@ -674,6 +692,7 @@ pub fn main() %void { {#header_close#} {#header_open|Operators#} {#header_open|Table of Operators#} + <div class="table-wrapper"> <table> <tr> <th> @@ -1246,6 +1265,7 @@ const ptr = &amp;x; </td> </tr> </table> + </div> {#header_close#} {#header_open|Precedence#} <pre><code>x() x[] x.y @@ -2755,6 +2775,16 @@ test "implicitly cast to const pointer" { use the C calling convention may pass structs and unions by value. </p> {#header_close#} + {#header_open|Function Reflection#} + {#code_begin|test#} +const assert = @import("std").debug.assert; + +test "fn reflection" { + assert(@typeOf(assert).ReturnType == void); + assert(@typeOf(assert).is_var_args == false); +} + {#code_end#} + {#header_close#} {#header_close#} {#header_open|Errors#} <p> @@ -2968,6 +2998,31 @@ fn createFoo(param: i32) %Foo { </li> </ul> {#see_also|defer|if|switch#} + {#header_open|Error Union Type#} + <p>An error union is created by putting a <code>%</code> in front of a type. + You can use compile-time reflection to access the child type of an error union:</p> + {#code_begin|test#} +const assert = @import("std").debug.assert; + +error SomeError; + +test "error union" { + var foo: %i32 = undefined; + + // Implicitly cast from child type of an error union: + foo = 1234; + + // Implicitly cast from an error set: + foo = error.SomeError; + + // Use compile-time reflection to access the child type of an error union: + comptime assert(@typeOf(foo).Child == i32); +} + {#code_end#} + {#header_close#} + {#header_open|Error Set Type#} + <p>TODO</p> + {#header_close#} {#header_close#} {#header_open|Nullables#} <p> @@ -3069,6 +3124,24 @@ fn doAThing(nullable_foo: ?&Foo) void { The optimizer can sometimes make better decisions knowing that pointer arguments cannot be null. </p> + {#header_open|Nullable Type#} + <p>A nullable is created by putting <code>?</code> in front of a type. You can use compile-time + reflection to access the child type of a nullable:</p> + {#code_begin|test#} +const assert = @import("std").debug.assert; + +test "nullable type" { + // Declare a nullable and implicitly cast from null: + var foo: ?i32 = null; + + // Implicitly cast from child type of a nullable + foo = 1234; + + // Use compile-time reflection to access the child type of the nullable: + comptime assert(@typeOf(foo).Child == i32); +} + {#code_end#} + {#header_close#} {#header_close#} {#header_open|Casting#} <p>TODO: explain implicit vs explicit casting</p> @@ -3804,6 +3877,17 @@ comptime { <code>@cInclude</code>, <code>@cDefine</code>, and <code>@cUndef</code> work within this expression, appending to a temporary buffer which is then parsed as C code. </p> + <p> + Usually you should only have one <code>@cImport</code> in your entire application, because it saves the compiler + from invoking clang multiple times, and prevents inline functions from being duplicated. + </p> + <p> + Reasons for having multiple <code>@cImport</code> expressions would be: + </p> + <ul> + <li>To avoid a symbol collision, for example if foo.h and bar.h both <code>#define CONNECTION_COUNT</code></li> + <li>To analyze the C code with different preprocessor defines</li> + </ul> {#see_also|Import from C Header File|@cInclude|@cDefine|@cUndef#} {#header_close#} {#header_open|@cInclude#} @@ -4133,17 +4217,28 @@ fn add(a: i32, b: i32) i32 { return a + b; } {#header_open|@memberCount#} <pre><code class="zig">@memberCount(comptime T: type) -&gt; (number literal)</code></pre> <p> - This function returns the number of enum values in an enum type. + This function returns the number of members in a struct, enum, or union type. </p> <p> The result is a compile time constant. </p> + <p> + It does not include functions, variables, or constants. + </p> {#header_close#} {#header_open|@memberName#} - <p>TODO</p> + <pre><code class="zig">@memberName(comptime T: type, comptime index: usize) -&gt; [N]u8</code></pre> + <p>Returns the field name of a struct, union, or enum.</p> + <p> + The result is a compile time constant. + </p> + <p> + It does not include functions, variables, or constants. + </p> {#header_close#} {#header_open|@memberType#} - <p>TODO</p> + <pre><code class="zig">@memberType(comptime T: type, comptime index: usize) -&gt; type</code></pre> + <p>Returns the field type of a struct or union.</p> {#header_close#} {#header_open|@memcpy#} <pre><code class="zig">@memcpy(noalias dest: &u8, noalias source: &const u8, byte_count: usize)</code></pre> @@ -5533,6 +5628,16 @@ fn readU32Be() u32 {} </p> {#header_close#} {#header_close#} + {#header_open|Source Encoding#} + <p>Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.</p> + <p>Throughout all zig source code (including in comments), some codepoints are never allowed:</p> + <ul> + <li>Ascii control characters, except for U+000a (LF): U+0000 - U+0009, U+000b - U+0001f, U+007f. (Note that Windows line endings (CRLF) are not allowed, and hard tabs are not allowed.)</li> + <li>Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).</li> + </ul> + <p>The codepoint U+000a (LF) (which is encoded as the single-byte value 0x0a) is the line terminator character. This character always terminates a line of zig source code (except possbly the last line of the file).</p> + <p>For some discussion on the rationale behind these design decisions, see <a href="https://github.com/zig-lang/zig/issues/663">issue #663</a></p> + {#header_close#} {#header_open|Grammar#} <pre><code class="nohighlight">Root = many(TopLevelItem) EOF @@ -5701,9 +5806,6 @@ ContainerDecl = option("extern" | "packed") <li>Together we serve end users.</li> </ul> {#header_close#} - {#header_open|TODO#} - <p>TODO: document changes from a31b23c46ba2a8c28df01adc1aa0b4d878b9a5cf (compile time reflection additions)</p> - {#header_close#} </div> <script> /*! highlight.js v9.12.0 | BSD3 License | git.io/hljslicense */ @@ -5765,7 +5867,7 @@ hljs.registerLanguage("zig", function(t) { a = t.IR + "\\s*\\(", c = { keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong", - built_in: "breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchg fence divExact truncate", + built_in: "breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchg fence divExact truncate", literal: "true false null undefined" }, n = [e, t.CLCM, t.CBCM, s, r]; diff --git a/src/all_types.hpp b/src/all_types.hpp @@ -331,12 +331,14 @@ struct TypeEnumField { Buf *name; BigInt value; uint32_t decl_index; + AstNode *decl_node; }; struct TypeUnionField { Buf *name; TypeEnumField *enum_field; TypeTableEntry *type_entry; + AstNode *decl_node; uint32_t gen_index; }; @@ -961,6 +963,7 @@ struct TypeStructField { size_t packed_bits_offset; size_t packed_bits_size; size_t unaligned_bit_count; + AstNode *decl_node; }; struct TypeTableEntryStruct { AstNode *decl_node; @@ -982,6 +985,8 @@ struct TypeTableEntryStruct { bool zero_bits_loop_flag; bool zero_bits_known; uint32_t abi_alignment; // also figured out with zero_bits pass + + HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name; }; struct TypeTableEntryMaybe { @@ -1013,6 +1018,8 @@ struct TypeTableEntryEnum { bool generate_name_table; LLVMValueRef name_table; + + HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name; }; uint32_t type_ptr_hash(const TypeTableEntry *ptr); @@ -1045,6 +1052,8 @@ struct TypeTableEntryUnion { uint32_t union_size_bytes; TypeTableEntry *most_aligned_union_member; + + HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name; }; struct FnGenParamInfo { diff --git a/src/analyze.cpp b/src/analyze.cpp @@ -633,20 +633,27 @@ TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t static void slice_type_common_init(CodeGen *g, TypeTableEntry *pointer_type, TypeTableEntry *entry) { unsigned element_count = 2; + Buf *ptr_field_name = buf_create_from_str("ptr"); + Buf *len_field_name = buf_create_from_str("len"); + entry->data.structure.layout = ContainerLayoutAuto; entry->data.structure.is_slice = true; entry->data.structure.src_field_count = element_count; entry->data.structure.gen_field_count = element_count; entry->data.structure.fields = allocate<TypeStructField>(element_count); - entry->data.structure.fields[slice_ptr_index].name = buf_create_from_str("ptr"); + entry->data.structure.fields_by_name.init(element_count); + entry->data.structure.fields[slice_ptr_index].name = ptr_field_name; entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type; entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index; entry->data.structure.fields[slice_ptr_index].gen_index = 0; - entry->data.structure.fields[slice_len_index].name = buf_create_from_str("len"); + entry->data.structure.fields[slice_len_index].name = len_field_name; entry->data.structure.fields[slice_len_index].type_entry = g->builtin_types.entry_usize; entry->data.structure.fields[slice_len_index].src_index = slice_len_index; entry->data.structure.fields[slice_len_index].gen_index = 1; + entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]); + entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]); + assert(type_has_zero_bits_known(pointer_type->data.pointer.child_type)); if (pointer_type->data.pointer.child_type->zero_bits) { entry->data.structure.gen_field_count = 1; @@ -1555,6 +1562,7 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f struct_type->data.structure.zero_bits_known = true; struct_type->data.structure.complete = true; struct_type->data.structure.fields = allocate<TypeStructField>(field_count); + struct_type->data.structure.fields_by_name.init(field_count); ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(field_count); LLVMTypeRef *element_types = allocate<LLVMTypeRef>(field_count); @@ -1566,6 +1574,9 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f field->type_entry = field_types[i]; field->src_index = i; field->gen_index = i; + + auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field); + assert(prev_entry == nullptr); } struct_type->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), type_name); @@ -2102,6 +2113,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { enum_type->data.enumeration.src_field_count = field_count; enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count); + enum_type->data.enumeration.fields_by_name.init(field_count); Scope *scope = &enum_type->data.enumeration.decls_scope->base; @@ -2139,6 +2151,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_i]; type_enum_field->name = field_node->data.struct_field.name; type_enum_field->decl_index = field_i; + type_enum_field->decl_node = field_node; if (field_node->data.struct_field.type != nullptr) { ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.type, @@ -2147,6 +2160,15 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { buf_sprintf("consider 'union(enum)' here")); } + auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field); + if (field_entry != nullptr) { + ErrorMsg *msg = add_node_error(g, field_node, + buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name))); + add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); + enum_type->data.enumeration.is_invalid = true; + continue; + } + AstNode *tag_value = field_node->data.struct_field.value; // In this first pass we resolve explicit tag values. @@ -2242,6 +2264,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { size_t field_count = decl_node->data.container_decl.fields.length; struct_type->data.structure.src_field_count = (uint32_t)field_count; struct_type->data.structure.fields = allocate<TypeStructField>(field_count); + struct_type->data.structure.fields_by_name.init(field_count); Scope *scope = &struct_type->data.structure.decls_scope->base; @@ -2250,6 +2273,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { AstNode *field_node = decl_node->data.container_decl.fields.at(i); TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; type_struct_field->name = field_node->data.struct_field.name; + type_struct_field->decl_node = field_node; if (field_node->data.struct_field.type == nullptr) { add_node_error(g, field_node, buf_sprintf("struct field missing type")); @@ -2257,6 +2281,15 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { continue; } + auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field); + if (field_entry != nullptr) { + ErrorMsg *msg = add_node_error(g, field_node, + buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name))); + add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); + struct_type->data.structure.is_invalid = true; + continue; + } + TypeTableEntry *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type); type_struct_field->type_entry = field_type; type_struct_field->src_index = i; @@ -2344,6 +2377,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { } union_type->data.unionation.src_field_count = field_count; union_type->data.unionation.fields = allocate<TypeUnionField>(field_count); + union_type->data.unionation.fields_by_name.init(field_count); uint32_t biggest_align_bytes = 0; @@ -2395,6 +2429,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { tag_type->data.enumeration.layout = ContainerLayoutAuto; tag_type->data.enumeration.src_field_count = field_count; tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count); + tag_type->data.enumeration.fields_by_name.init(field_count); tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope; tag_type->data.enumeration.complete = true; } else if (enum_type_node != nullptr) { @@ -2424,6 +2459,16 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { Buf *field_name = field_node->data.struct_field.name; TypeUnionField *union_field = &union_type->data.unionation.fields[i]; union_field->name = field_node->data.struct_field.name; + union_field->decl_node = field_node; + + auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field); + if (field_entry != nullptr) { + ErrorMsg *msg = add_node_error(g, field_node, + buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name))); + add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); + union_type->data.unionation.is_invalid = true; + continue; + } TypeTableEntry *field_type; if (field_node->data.struct_field.type == nullptr) { @@ -2456,6 +2501,10 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { union_field->enum_field = &tag_type->data.enumeration.fields[i]; union_field->enum_field->name = field_name; union_field->enum_field->decl_index = i; + union_field->enum_field->decl_node = field_node; + + auto prev_entry = tag_type->data.enumeration.fields_by_name.put_unique(union_field->enum_field->name, union_field->enum_field); + assert(prev_entry == nullptr); // caught by union de-duplicator above AstNode *tag_value = field_node->data.struct_field.value; // In this first pass we resolve explicit tag values. @@ -3246,6 +3295,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { is_const, init_val, &tld_var->base); tld_var->var->linkage = linkage; + if (implicit_type != nullptr && type_is_invalid(implicit_type)) { + tld_var->var->value->type = g->builtin_types.entry_invalid; + } + if (var_decl->align_expr != nullptr) { if (!analyze_const_align(g, tld_var->base.parent_scope, var_decl->align_expr, &tld_var->var->align_bytes)) { tld_var->var->value->type = g->builtin_types.entry_invalid; @@ -3327,11 +3380,7 @@ bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry * } // slice const - if (expected_type->id == TypeTableEntryIdStruct && - actual_type->id == TypeTableEntryIdStruct && - expected_type->data.structure.is_slice && - actual_type->data.structure.is_slice) - { + if (is_slice(expected_type) && is_slice(actual_type)) { TypeTableEntry *actual_ptr_type = actual_type->data.structure.fields[slice_ptr_index].type_entry; TypeTableEntry *expected_ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry; if ((!actual_ptr_type->data.pointer.is_const || expected_ptr_type->data.pointer.is_const) && @@ -3499,37 +3548,35 @@ FnTableEntry *scope_get_fn_if_root(Scope *scope) { } TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name) { - for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) { - TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i]; - if (buf_eql_buf(type_enum_field->name, name)) { - return type_enum_field; - } - } - return nullptr; + assert(enum_type->id == TypeTableEntryIdEnum); + if (enum_type->data.enumeration.src_field_count == 0) + return nullptr; + auto entry = enum_type->data.enumeration.fields_by_name.maybe_get(name); + if (entry == nullptr) + return nullptr; + return entry->value; } TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name) { assert(type_entry->id == TypeTableEntryIdStruct); assert(type_entry->data.structure.complete); - for (uint32_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) { - TypeStructField *field = &type_entry->data.structure.fields[i]; - if (buf_eql_buf(field->name, name)) { - return field; - } - } - return nullptr; + if (type_entry->data.structure.src_field_count == 0) + return nullptr; + auto entry = type_entry->data.structure.fields_by_name.maybe_get(name); + if (entry == nullptr) + return nullptr; + return entry->value; } TypeUnionField *find_union_type_field(TypeTableEntry *type_entry, Buf *name) { assert(type_entry->id == TypeTableEntryIdUnion); assert(type_entry->data.unionation.zero_bits_known); - for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) { - TypeUnionField *field = &type_entry->data.unionation.fields[i]; - if (buf_eql_buf(field->enum_field->name, name)) { - return field; - } - } - return nullptr; + if (type_entry->data.unionation.src_field_count == 0) + return nullptr; + auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name); + if (entry == nullptr) + return nullptr; + return entry->value; } TypeUnionField *find_union_field_by_tag(TypeTableEntry *type_entry, const BigInt *tag) { diff --git a/src/analyze.hpp b/src/analyze.hpp @@ -60,8 +60,8 @@ bool type_is_complete(TypeTableEntry *type_entry); bool type_is_invalid(TypeTableEntry *type_entry); bool type_has_zero_bits_known(TypeTableEntry *type_entry); void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry); -TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name); ScopeDecls *get_container_scope(TypeTableEntry *type_entry); +TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name); TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name); TypeUnionField *find_union_type_field(TypeTableEntry *type_entry, Buf *name); TypeEnumField *find_enum_field_by_tag(TypeTableEntry *enum_type, const BigInt *tag); diff --git a/src/ir.cpp b/src/ir.cpp @@ -6193,6 +6193,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc if (other_type->id == TypeTableEntryIdFloat) { return true; } else if (other_type->id == TypeTableEntryIdInt && const_val_is_int) { + if (!other_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) { + Buf *val_buf = buf_alloc(); + bigint_append_buf(val_buf, &const_val->data.x_bigint, 10); + ir_add_error(ira, instruction, + buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'", + buf_ptr(val_buf), + buf_ptr(&other_type->name))); + return false; + } if (bigint_fits_in_bits(&const_val->data.x_bigint, other_type->data.integral.bit_count, other_type->data.integral.is_signed)) { @@ -6205,6 +6214,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc if (const_val_fits_in_num_lit(const_val, child_type)) { return true; } else if (child_type->id == TypeTableEntryIdInt && const_val_is_int) { + if (!child_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) { + Buf *val_buf = buf_alloc(); + bigint_append_buf(val_buf, &const_val->data.x_bigint, 10); + ir_add_error(ira, instruction, + buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'", + buf_ptr(val_buf), + buf_ptr(&child_type->name))); + return false; + } if (bigint_fits_in_bits(&const_val->data.x_bigint, child_type->data.integral.bit_count, child_type->data.integral.is_signed)) @@ -6351,10 +6369,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira, } // implicit [N]T to []const T - if (expected_type->id == TypeTableEntryIdStruct && - expected_type->data.structure.is_slice && - actual_type->id == TypeTableEntryIdArray) - { + if (is_slice(expected_type) && actual_type->id == TypeTableEntryIdArray) { TypeTableEntry *ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry; assert(ptr_type->id == TypeTableEntryIdPointer); @@ -6366,8 +6381,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira, } // implicit &const [N]T to []const T - if (expected_type->id == TypeTableEntryIdStruct && - expected_type->data.structure.is_slice && + if (is_slice(expected_type) && actual_type->id == TypeTableEntryIdPointer && actual_type->data.pointer.is_const && actual_type->data.pointer.child_type->id == TypeTableEntryIdArray) @@ -7009,7 +7023,7 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio if (pointee_type->id == TypeTableEntryIdMetaType) { TypeTableEntry *type_entry = pointee->data.x_type; if (type_entry->id == TypeTableEntryIdUnreachable) { - ir_add_error(ira, instruction, buf_sprintf("pointer to unreachable not allowed")); + ir_add_error(ira, instruction, buf_sprintf("pointer to noreturn not allowed")); return ira->codegen->invalid_instruction; } @@ -9837,6 +9851,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal AstNode *fn_proto_node = fn_entry ? fn_entry->proto_node : nullptr;; + if (fn_type_id->cc == CallingConventionNaked) { + ErrorMsg *msg = ir_add_error(ira, fn_ref, buf_sprintf("unable to call function with naked calling convention")); + if (fn_proto_node) { + add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("declared here")); + } + return ira->codegen->builtin_types.entry_invalid; + } + + if (fn_type_id->is_var_args) { if (call_param_count < src_param_count) { ErrorMsg *msg = ir_add_error_node(ira, source_node, diff --git a/std/c/linux.zig b/std/c/linux.zig @@ -1,4 +1,4 @@ -pub use @import("../os/linux_errno.zig"); +pub use @import("../os/linux/errno.zig"); pub extern "c" fn getrandom(buf_ptr: &u8, buf_len: usize, flags: c_uint) c_int; extern "c" fn __errno_location() &c_int; diff --git a/std/fmt/index.zig b/std/fmt/index.zig @@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) { return output(context, (*value)[0..]); } else { - @compileError("Unable to format type '" ++ @typeName(T) ++ "'"); + return format(context, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value)); } }, else => if (@canImplicitCast([]const u8, value)) { @@ -546,6 +546,12 @@ test "parse unsigned comptime" { } } +// Dummy field because of https://github.com/zig-lang/zig/issues/557. +// At top level because of https://github.com/zig-lang/zig/issues/675. +const Struct = struct { + unused: u8, +}; + test "fmt.format" { { var buf1: [32]u8 = undefined; @@ -577,6 +583,14 @@ test "fmt.format" { const result = try bufPrint(buf1[0..], "u3: {}\n", value); assert(mem.eql(u8, result, "u3: 5\n")); } + { + var buf1: [32]u8 = undefined; + const value = Struct { + .unused = 42, + }; + const result = try bufPrint(buf1[0..], "pointer: {}\n", &value); + assert(mem.startsWith(u8, result, "pointer: Struct@")); + } // TODO get these tests passing in release modes // https://github.com/zig-lang/zig/issues/564 diff --git a/std/index.zig b/std/index.zig @@ -6,6 +6,7 @@ pub const Buffer = @import("buffer.zig").Buffer; pub const BufferOutStream = @import("buffer.zig").BufferOutStream; pub const HashMap = @import("hash_map.zig").HashMap; pub const LinkedList = @import("linked_list.zig").LinkedList; +pub const IntrusiveLinkedList = @import("linked_list.zig").IntrusiveLinkedList; pub const base64 = @import("base64.zig"); pub const build = @import("build.zig"); diff --git a/std/io.zig b/std/io.zig @@ -2,7 +2,7 @@ const std = @import("index.zig"); const builtin = @import("builtin"); const Os = builtin.Os; const system = switch(builtin.os) { - Os.linux => @import("os/linux.zig"), + Os.linux => @import("os/linux/index.zig"), Os.macosx, Os.ios => @import("os/darwin.zig"), Os.windows => @import("os/windows/index.zig"), else => @compileError("Unsupported OS"), diff --git a/std/os/index.zig b/std/os/index.zig @@ -6,7 +6,7 @@ const os = this; pub const windows = @import("windows/index.zig"); pub const darwin = @import("darwin.zig"); -pub const linux = @import("linux.zig"); +pub const linux = @import("linux/index.zig"); pub const zen = @import("zen.zig"); pub const posix = switch(builtin.os) { Os.linux => linux, @@ -82,18 +82,24 @@ pub fn getRandomBytes(buf: []u8) %void { // See #397 const err = posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)); if (err > 0) { - return switch (err) { + switch (err) { posix.EINVAL => unreachable, posix.EFAULT => unreachable, posix.EINTR => continue, - else => unexpectedErrorPosix(err), - }; + posix.ENOSYS => { + const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0); + defer close(fd); + + try posixRead(fd, buf); + return; + }, + else => return unexpectedErrorPosix(err), + } } return; }, Os.macosx, Os.ios => { - const fd = try posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, - 0, null); + const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0); defer close(fd); try posixRead(fd, buf); @@ -183,10 +189,15 @@ pub fn close(handle: FileHandle) void { /// Calls POSIX read, and keeps trying if it gets interrupted. pub fn posixRead(fd: i32, buf: []u8) %void { + // Linux can return EINVAL when read amount is > 0x7ffff000 + // See https://github.com/zig-lang/zig/pull/743#issuecomment-363158274 + const max_buf_len = 0x7ffff000; + var index: usize = 0; while (index < buf.len) { - const amt_written = posix.read(fd, &buf[index], buf.len - index); - const err = posix.getErrno(amt_written); + const want_to_read = math.min(buf.len - index, usize(max_buf_len)); + const rc = posix.read(fd, &buf[index], want_to_read); + const err = posix.getErrno(rc); if (err > 0) { return switch (err) { posix.EINTR => continue, @@ -199,7 +210,7 @@ pub fn posixRead(fd: i32, buf: []u8) %void { else => unexpectedErrorPosix(err), }; } - index += amt_written; + index += rc; } } @@ -214,9 +225,15 @@ error BrokenPipe; /// Calls POSIX write, and keeps trying if it gets interrupted. pub fn posixWrite(fd: i32, bytes: []const u8) %void { - while (true) { - const write_ret = posix.write(fd, bytes.ptr, bytes.len); - const write_err = posix.getErrno(write_ret); + // Linux can return EINVAL when write amount is > 0x7ffff000 + // See https://github.com/zig-lang/zig/pull/743#issuecomment-363165856 + const max_bytes_len = 0x7ffff000; + + var index: usize = 0; + while (index < bytes.len) { + const amt_to_write = math.min(bytes.len - index, usize(max_bytes_len)); + const rc = posix.write(fd, &bytes[index], amt_to_write); + const write_err = posix.getErrno(rc); if (write_err > 0) { return switch (write_err) { posix.EINTR => continue, @@ -233,7 +250,7 @@ pub fn posixWrite(fd: i32, bytes: []const u8) %void { else => unexpectedErrorPosix(write_err), }; } - return; + index += rc; } } @@ -262,8 +279,12 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al mem.copy(u8, path0, file_path); path0[file_path.len] = 0; + return posixOpenC(path0.ptr, flags, perm); +} + +pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) %i32 { while (true) { - const result = posix.open(path0.ptr, flags, perm); + const result = posix.open(file_path, flags, perm); const err = posix.getErrno(result); if (err > 0) { return switch (err) { @@ -1495,10 +1516,10 @@ test "std.os" { _ = @import("darwin_errno.zig"); _ = @import("darwin.zig"); _ = @import("get_user_id.zig"); - _ = @import("linux_errno.zig"); + _ = @import("linux/errno.zig"); //_ = @import("linux_i386.zig"); - _ = @import("linux_x86_64.zig"); - _ = @import("linux.zig"); + _ = @import("linux/x86_64.zig"); + _ = @import("linux/index.zig"); _ = @import("path.zig"); _ = @import("windows/index.zig"); } diff --git a/std/os/linux.zig b/std/os/linux.zig @@ -1,796 +0,0 @@ -const std = @import("../index.zig"); -const assert = std.debug.assert; -const builtin = @import("builtin"); -const arch = switch (builtin.arch) { - builtin.Arch.x86_64 => @import("linux_x86_64.zig"), - builtin.Arch.i386 => @import("linux_i386.zig"), - else => @compileError("unsupported arch"), -}; -pub use @import("linux_errno.zig"); - -pub const PATH_MAX = 4096; - -pub const STDIN_FILENO = 0; -pub const STDOUT_FILENO = 1; -pub const STDERR_FILENO = 2; - -pub const PROT_NONE = 0; -pub const PROT_READ = 1; -pub const PROT_WRITE = 2; -pub const PROT_EXEC = 4; -pub const PROT_GROWSDOWN = 0x01000000; -pub const PROT_GROWSUP = 0x02000000; - -pub const MAP_FAILED = @maxValue(usize); -pub const MAP_SHARED = 0x01; -pub const MAP_PRIVATE = 0x02; -pub const MAP_TYPE = 0x0f; -pub const MAP_FIXED = 0x10; -pub const MAP_ANONYMOUS = 0x20; -pub const MAP_NORESERVE = 0x4000; -pub const MAP_GROWSDOWN = 0x0100; -pub const MAP_DENYWRITE = 0x0800; -pub const MAP_EXECUTABLE = 0x1000; -pub const MAP_LOCKED = 0x2000; -pub const MAP_POPULATE = 0x8000; -pub const MAP_NONBLOCK = 0x10000; -pub const MAP_STACK = 0x20000; -pub const MAP_HUGETLB = 0x40000; -pub const MAP_FILE = 0; - -pub const WNOHANG = 1; -pub const WUNTRACED = 2; -pub const WSTOPPED = 2; -pub const WEXITED = 4; -pub const WCONTINUED = 8; -pub const WNOWAIT = 0x1000000; - -pub const SA_NOCLDSTOP = 1; -pub const SA_NOCLDWAIT = 2; -pub const SA_SIGINFO = 4; -pub const SA_ONSTACK = 0x08000000; -pub const SA_RESTART = 0x10000000; -pub const SA_NODEFER = 0x40000000; -pub const SA_RESETHAND = 0x80000000; -pub const SA_RESTORER = 0x04000000; - -pub const SIGHUP = 1; -pub const SIGINT = 2; -pub const SIGQUIT = 3; -pub const SIGILL = 4; -pub const SIGTRAP = 5; -pub const SIGABRT = 6; -pub const SIGIOT = SIGABRT; -pub const SIGBUS = 7; -pub const SIGFPE = 8; -pub const SIGKILL = 9; -pub const SIGUSR1 = 10; -pub const SIGSEGV = 11; -pub const SIGUSR2 = 12; -pub const SIGPIPE = 13; -pub const SIGALRM = 14; -pub const SIGTERM = 15; -pub const SIGSTKFLT = 16; -pub const SIGCHLD = 17; -pub const SIGCONT = 18; -pub const SIGSTOP = 19; -pub const SIGTSTP = 20; -pub const SIGTTIN = 21; -pub const SIGTTOU = 22; -pub const SIGURG = 23; -pub const SIGXCPU = 24; -pub const SIGXFSZ = 25; -pub const SIGVTALRM = 26; -pub const SIGPROF = 27; -pub const SIGWINCH = 28; -pub const SIGIO = 29; -pub const SIGPOLL = 29; -pub const SIGPWR = 30; -pub const SIGSYS = 31; -pub const SIGUNUSED = SIGSYS; - -pub const O_RDONLY = 0o0; -pub const O_WRONLY = 0o1; -pub const O_RDWR = 0o2; - -pub const O_CREAT = arch.O_CREAT; -pub const O_EXCL = arch.O_EXCL; -pub const O_NOCTTY = arch.O_NOCTTY; -pub const O_TRUNC = arch.O_TRUNC; -pub const O_APPEND = arch.O_APPEND; -pub const O_NONBLOCK = arch.O_NONBLOCK; -pub const O_DSYNC = arch.O_DSYNC; -pub const O_SYNC = arch.O_SYNC; -pub const O_RSYNC = arch.O_RSYNC; -pub const O_DIRECTORY = arch.O_DIRECTORY; -pub const O_NOFOLLOW = arch.O_NOFOLLOW; -pub const O_CLOEXEC = arch.O_CLOEXEC; - -pub const O_ASYNC = arch.O_ASYNC; -pub const O_DIRECT = arch.O_DIRECT; -pub const O_LARGEFILE = arch.O_LARGEFILE; -pub const O_NOATIME = arch.O_NOATIME; -pub const O_PATH = arch.O_PATH; -pub const O_TMPFILE = arch.O_TMPFILE; -pub const O_NDELAY = arch.O_NDELAY; - -pub const SEEK_SET = 0; -pub const SEEK_CUR = 1; -pub const SEEK_END = 2; - -pub const SIG_BLOCK = 0; -pub const SIG_UNBLOCK = 1; -pub const SIG_SETMASK = 2; - -pub const SOCK_STREAM = 1; -pub const SOCK_DGRAM = 2; -pub const SOCK_RAW = 3; -pub const SOCK_RDM = 4; -pub const SOCK_SEQPACKET = 5; -pub const SOCK_DCCP = 6; -pub const SOCK_PACKET = 10; -pub const SOCK_CLOEXEC = 0o2000000; -pub const SOCK_NONBLOCK = 0o4000; - - -pub const PROTO_ip = 0o000; -pub const PROTO_icmp = 0o001; -pub const PROTO_igmp = 0o002; -pub const PROTO_ggp = 0o003; -pub const PROTO_ipencap = 0o004; -pub const PROTO_st = 0o005; -pub const PROTO_tcp = 0o006; -pub const PROTO_egp = 0o010; -pub const PROTO_pup = 0o014; -pub const PROTO_udp = 0o021; -pub const PROTO_hmp = 0o024; -pub const PROTO_xns_idp = 0o026; -pub const PROTO_rdp = 0o033; -pub const PROTO_iso_tp4 = 0o035; -pub const PROTO_xtp = 0o044; -pub const PROTO_ddp = 0o045; -pub const PROTO_idpr_cmtp = 0o046; -pub const PROTO_ipv6 = 0o051; -pub const PROTO_ipv6_route = 0o053; -pub const PROTO_ipv6_frag = 0o054; -pub const PROTO_idrp = 0o055; -pub const PROTO_rsvp = 0o056; -pub const PROTO_gre = 0o057; -pub const PROTO_esp = 0o062; -pub const PROTO_ah = 0o063; -pub const PROTO_skip = 0o071; -pub const PROTO_ipv6_icmp = 0o072; -pub const PROTO_ipv6_nonxt = 0o073; -pub const PROTO_ipv6_opts = 0o074; -pub const PROTO_rspf = 0o111; -pub const PROTO_vmtp = 0o121; -pub const PROTO_ospf = 0o131; -pub const PROTO_ipip = 0o136; -pub const PROTO_encap = 0o142; -pub const PROTO_pim = 0o147; -pub const PROTO_raw = 0o377; - -pub const PF_UNSPEC = 0; -pub const PF_LOCAL = 1; -pub const PF_UNIX = PF_LOCAL; -pub const PF_FILE = PF_LOCAL; -pub const PF_INET = 2; -pub const PF_AX25 = 3; -pub const PF_IPX = 4; -pub const PF_APPLETALK = 5; -pub const PF_NETROM = 6; -pub const PF_BRIDGE = 7; -pub const PF_ATMPVC = 8; -pub const PF_X25 = 9; -pub const PF_INET6 = 10; -pub const PF_ROSE = 11; -pub const PF_DECnet = 12; -pub const PF_NETBEUI = 13; -pub const PF_SECURITY = 14; -pub const PF_KEY = 15; -pub const PF_NETLINK = 16; -pub const PF_ROUTE = PF_NETLINK; -pub const PF_PACKET = 17; -pub const PF_ASH = 18; -pub const PF_ECONET = 19; -pub const PF_ATMSVC = 20; -pub const PF_RDS = 21; -pub const PF_SNA = 22; -pub const PF_IRDA = 23; -pub const PF_PPPOX = 24; -pub const PF_WANPIPE = 25; -pub const PF_LLC = 26; -pub const PF_IB = 27; -pub const PF_MPLS = 28; -pub const PF_CAN = 29; -pub const PF_TIPC = 30; -pub const PF_BLUETOOTH = 31; -pub const PF_IUCV = 32; -pub const PF_RXRPC = 33; -pub const PF_ISDN = 34; -pub const PF_PHONET = 35; -pub const PF_IEEE802154 = 36; -pub const PF_CAIF = 37; -pub const PF_ALG = 38; -pub const PF_NFC = 39; -pub const PF_VSOCK = 40; -pub const PF_MAX = 41; - -pub const AF_UNSPEC = PF_UNSPEC; -pub const AF_LOCAL = PF_LOCAL; -pub const AF_UNIX = AF_LOCAL; -pub const AF_FILE = AF_LOCAL; -pub const AF_INET = PF_INET; -pub const AF_AX25 = PF_AX25; -pub const AF_IPX = PF_IPX; -pub const AF_APPLETALK = PF_APPLETALK; -pub const AF_NETROM = PF_NETROM; -pub const AF_BRIDGE = PF_BRIDGE; -pub const AF_ATMPVC = PF_ATMPVC; -pub const AF_X25 = PF_X25; -pub const AF_INET6 = PF_INET6; -pub const AF_ROSE = PF_ROSE; -pub const AF_DECnet = PF_DECnet; -pub const AF_NETBEUI = PF_NETBEUI; -pub const AF_SECURITY = PF_SECURITY; -pub const AF_KEY = PF_KEY; -pub const AF_NETLINK = PF_NETLINK; -pub const AF_ROUTE = PF_ROUTE; -pub const AF_PACKET = PF_PACKET; -pub const AF_ASH = PF_ASH; -pub const AF_ECONET = PF_ECONET; -pub const AF_ATMSVC = PF_ATMSVC; -pub const AF_RDS = PF_RDS; -pub const AF_SNA = PF_SNA; -pub const AF_IRDA = PF_IRDA; -pub const AF_PPPOX = PF_PPPOX; -pub const AF_WANPIPE = PF_WANPIPE; -pub const AF_LLC = PF_LLC; -pub const AF_IB = PF_IB; -pub const AF_MPLS = PF_MPLS; -pub const AF_CAN = PF_CAN; -pub const AF_TIPC = PF_TIPC; -pub const AF_BLUETOOTH = PF_BLUETOOTH; -pub const AF_IUCV = PF_IUCV; -pub const AF_RXRPC = PF_RXRPC; -pub const AF_ISDN = PF_ISDN; -pub const AF_PHONET = PF_PHONET; -pub const AF_IEEE802154 = PF_IEEE802154; -pub const AF_CAIF = PF_CAIF; -pub const AF_ALG = PF_ALG; -pub const AF_NFC = PF_NFC; -pub const AF_VSOCK = PF_VSOCK; -pub const AF_MAX = PF_MAX; - -pub const DT_UNKNOWN = 0; -pub const DT_FIFO = 1; -pub const DT_CHR = 2; -pub const DT_DIR = 4; -pub const DT_BLK = 6; -pub const DT_REG = 8; -pub const DT_LNK = 10; -pub const DT_SOCK = 12; -pub const DT_WHT = 14; - - -pub const TCGETS = 0x5401; -pub const TCSETS = 0x5402; -pub const TCSETSW = 0x5403; -pub const TCSETSF = 0x5404; -pub const TCGETA = 0x5405; -pub const TCSETA = 0x5406; -pub const TCSETAW = 0x5407; -pub const TCSETAF = 0x5408; -pub const TCSBRK = 0x5409; -pub const TCXONC = 0x540A; -pub const TCFLSH = 0x540B; -pub const TIOCEXCL = 0x540C; -pub const TIOCNXCL = 0x540D; -pub const TIOCSCTTY = 0x540E; -pub const TIOCGPGRP = 0x540F; -pub const TIOCSPGRP = 0x5410; -pub const TIOCOUTQ = 0x5411; -pub const TIOCSTI = 0x5412; -pub const TIOCGWINSZ = 0x5413; -pub const TIOCSWINSZ = 0x5414; -pub const TIOCMGET = 0x5415; -pub const TIOCMBIS = 0x5416; -pub const TIOCMBIC = 0x5417; -pub const TIOCMSET = 0x5418; -pub const TIOCGSOFTCAR = 0x5419; -pub const TIOCSSOFTCAR = 0x541A; -pub const FIONREAD = 0x541B; -pub const TIOCINQ = FIONREAD; -pub const TIOCLINUX = 0x541C; -pub const TIOCCONS = 0x541D; -pub const TIOCGSERIAL = 0x541E; -pub const TIOCSSERIAL = 0x541F; -pub const TIOCPKT = 0x5420; -pub const FIONBIO = 0x5421; -pub const TIOCNOTTY = 0x5422; -pub const TIOCSETD = 0x5423; -pub const TIOCGETD = 0x5424; -pub const TCSBRKP = 0x5425; -pub const TIOCSBRK = 0x5427; -pub const TIOCCBRK = 0x5428; -pub const TIOCGSID = 0x5429; -pub const TIOCGRS485 = 0x542E; -pub const TIOCSRS485 = 0x542F; -pub const TIOCGPTN = 0x80045430; -pub const TIOCSPTLCK = 0x40045431; -pub const TIOCGDEV = 0x80045432; -pub const TCGETX = 0x5432; -pub const TCSETX = 0x5433; -pub const TCSETXF = 0x5434; -pub const TCSETXW = 0x5435; -pub const TIOCSIG = 0x40045436; -pub const TIOCVHANGUP = 0x5437; -pub const TIOCGPKT = 0x80045438; -pub const TIOCGPTLCK = 0x80045439; -pub const TIOCGEXCL = 0x80045440; - -pub const EPOLL_CTL_ADD = 1; -pub const EPOLL_CTL_DEL = 2; -pub const EPOLL_CTL_MOD = 3; - -pub const EPOLLIN = 0x001; -pub const EPOLLPRI = 0x002; -pub const EPOLLOUT = 0x004; -pub const EPOLLRDNORM = 0x040; -pub const EPOLLRDBAND = 0x080; -pub const EPOLLWRNORM = 0x100; -pub const EPOLLWRBAND = 0x200; -pub const EPOLLMSG = 0x400; -pub const EPOLLERR = 0x008; -pub const EPOLLHUP = 0x010; -pub const EPOLLRDHUP = 0x2000; -pub const EPOLLEXCLUSIVE = (u32(1) << 28); -pub const EPOLLWAKEUP = (u32(1) << 29); -pub const EPOLLONESHOT = (u32(1) << 30); -pub const EPOLLET = (u32(1) << 31); - -pub const CLOCK_REALTIME = 0; -pub const CLOCK_MONOTONIC = 1; -pub const CLOCK_PROCESS_CPUTIME_ID = 2; -pub const CLOCK_THREAD_CPUTIME_ID = 3; -pub const CLOCK_MONOTONIC_RAW = 4; -pub const CLOCK_REALTIME_COARSE = 5; -pub const CLOCK_MONOTONIC_COARSE = 6; -pub const CLOCK_BOOTTIME = 7; -pub const CLOCK_REALTIME_ALARM = 8; -pub const CLOCK_BOOTTIME_ALARM = 9; -pub const CLOCK_SGI_CYCLE = 10; -pub const CLOCK_TAI = 11; - -pub const TFD_NONBLOCK = O_NONBLOCK; -pub const TFD_CLOEXEC = O_CLOEXEC; - -pub const TFD_TIMER_ABSTIME = 1; -pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1); - -fn unsigned(s: i32) u32 { return @bitCast(u32, s); } -fn signed(s: u32) i32 { return @bitCast(i32, s); } -pub fn WEXITSTATUS(s: i32) i32 { return signed((unsigned(s) & 0xff00) >> 8); } -pub fn WTERMSIG(s: i32) i32 { return signed(unsigned(s) & 0x7f); } -pub fn WSTOPSIG(s: i32) i32 { return WEXITSTATUS(s); } -pub fn WIFEXITED(s: i32) bool { return WTERMSIG(s) == 0; } -pub fn WIFSTOPPED(s: i32) bool { return (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00; } -pub fn WIFSIGNALED(s: i32) bool { return (unsigned(s)&0xffff)-%1 < 0xff; } - - -pub const winsize = extern struct { - ws_row: u16, - ws_col: u16, - ws_xpixel: u16, - ws_ypixel: u16, -}; - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) usize { - const signed_r = @bitCast(isize, r); - return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0; -} - -pub fn dup2(old: i32, new: i32) usize { - return arch.syscall2(arch.SYS_dup2, usize(old), usize(new)); -} - -pub fn chdir(path: &const u8) usize { - return arch.syscall1(arch.SYS_chdir, @ptrToInt(path)); -} - -pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize { - return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); -} - -pub fn fork() usize { - return arch.syscall0(arch.SYS_fork); -} - -pub fn getcwd(buf: &u8, size: usize) usize { - return arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size); -} - -pub fn getdents(fd: i32, dirp: &u8, count: usize) usize { - return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count); -} - -pub fn isatty(fd: i32) bool { - var wsz: winsize = undefined; - return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; -} - -pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize { - return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); -} - -pub fn mkdir(path: &const u8, mode: u32) usize { - return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode); -} - -pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize { - return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), - @bitCast(usize, offset)); -} - -pub fn munmap(address: &u8, length: usize) usize { - return arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length); -} - -pub fn read(fd: i32, buf: &u8, count: usize) usize { - return arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count); -} - -pub fn rmdir(path: &const u8) usize { - return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)); -} - -pub fn symlink(existing: &const u8, new: &const u8) usize { - return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); -} - -pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize { - return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset); -} - -pub fn pipe(fd: &[2]i32) usize { - return pipe2(fd, 0); -} - -pub fn pipe2(fd: &[2]i32, flags: usize) usize { - return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags); -} - -pub fn write(fd: i32, buf: &const u8, count: usize) usize { - return arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count); -} - -pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) usize { - return arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset); -} - -pub fn rename(old: &const u8, new: &const u8) usize { - return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)); -} - -pub fn open(path: &const u8, flags: u32, perm: usize) usize { - return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm); -} - -pub fn create(path: &const u8, perm: usize) usize { - return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm); -} - -pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize { - return arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode); -} - -pub fn close(fd: i32) usize { - return arch.syscall1(arch.SYS_close, usize(fd)); -} - -pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize { - return arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos); -} - -pub fn exit(status: i32) noreturn { - _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn getrandom(buf: &u8, count: usize, flags: u32) usize { - return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags)); -} - -pub fn kill(pid: i32, sig: i32) usize { - return arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)); -} - -pub fn unlink(path: &const u8) usize { - return arch.syscall1(arch.SYS_unlink, @ptrToInt(path)); -} - -pub fn waitpid(pid: i32, status: &i32, options: i32) usize { - return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); -} - -pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize { - return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); -} - -pub fn setuid(uid: u32) usize { - return arch.syscall1(arch.SYS_setuid, uid); -} - -pub fn setgid(gid: u32) usize { - return arch.syscall1(arch.SYS_setgid, gid); -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return arch.syscall2(arch.SYS_setreuid, ruid, euid); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return arch.syscall2(arch.SYS_setregid, rgid, egid); -} - -pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize { - return arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8); -} - -pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize { - assert(sig >= 1); - assert(sig != SIGKILL); - assert(sig != SIGSTOP); - var ksa = k_sigaction { - .handler = act.handler, - .flags = act.flags | SA_RESTORER, - .mask = undefined, - .restorer = @ptrCast(extern fn()void, arch.restore_rt), - }; - var ksa_old: k_sigaction = undefined; - @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8); - const result = arch.syscall4(arch.SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); - const err = getErrno(result); - if (err != 0) { - return result; - } - if (oact) |old| { - old.handler = ksa_old.handler; - old.flags = @truncate(u32, ksa_old.flags); - @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); - } - return 0; -} - -const NSIG = 65; -const sigset_t = [128 / @sizeOf(usize)]usize; -const all_mask = []usize{@maxValue(usize)}; -const app_mask = []usize{0xfffffffc7fffffff}; - -const k_sigaction = extern struct { - handler: extern fn(i32)void, - flags: usize, - restorer: extern fn()void, - mask: [2]u32, -}; - -/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. -pub const Sigaction = struct { - handler: extern fn(i32)void, - mask: sigset_t, - flags: u32, -}; - -pub const SIG_ERR = @intToPtr(extern fn(i32)void, @maxValue(usize)); -pub const SIG_DFL = @intToPtr(extern fn(i32)void, 0); -pub const SIG_IGN = @intToPtr(extern fn(i32)void, 1); -pub const empty_sigset = []usize{0} ** sigset_t.len; - -pub fn raise(sig: i32) usize { - var set: sigset_t = undefined; - blockAppSignals(&set); - const tid = i32(arch.syscall0(arch.SYS_gettid)); - const ret = arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig)); - restoreSignals(&set); - return ret; -} - -fn blockAllSignals(set: &sigset_t) void { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8); -} - -fn blockAppSignals(set: &sigset_t) void { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8); -} - -fn restoreSignals(set: &sigset_t) void { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8); -} - -pub fn sigaddset(set: &sigset_t, sig: u6) void { - const s = sig - 1; - (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1)); -} - -pub fn sigismember(set: &const sigset_t, sig: u6) bool { - const s = sig - 1; - return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0; -} - - -pub const sa_family_t = u16; -pub const socklen_t = u32; -pub const in_addr = u32; -pub const in6_addr = [16]u8; - -pub const sockaddr = extern struct { - family: sa_family_t, - port: u16, - data: [12]u8, -}; - -pub const sockaddr_in = extern struct { - family: sa_family_t, - port: u16, - addr: in_addr, - zero: [8]u8, -}; - -pub const sockaddr_in6 = extern struct { - family: sa_family_t, - port: u16, - flowinfo: u32, - addr: in6_addr, - scope_id: u32, -}; - -pub const iovec = extern struct { - iov_base: &u8, - iov_len: usize, -}; - -pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { - return arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { - return arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn socket(domain: i32, socket_type: i32, protocol: i32) usize { - return arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)); -} - -pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) usize { - return arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen)); -} - -pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) usize { - return arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen)); -} - -pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) usize { - return arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags); -} - -pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) usize { - return arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len)); -} - -pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) usize { - return arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags); -} - -pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, - noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize -{ - return arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); -} - -pub fn shutdown(fd: i32, how: i32) usize { - return arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)); -} - -pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) usize { - return arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len)); -} - -pub fn listen(fd: i32, backlog: i32) usize { - return arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)); -} - -pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) usize { - return arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen)); -} - -pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { - return arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0])); -} - -pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { - return accept4(fd, addr, len, 0); -} - -pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) usize { - return arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags); -} - -// error NameTooLong; -// error SystemResources; -// error Io; -// -// pub fn if_nametoindex(name: []u8) %u32 { -// var ifr: ifreq = undefined; -// -// if (name.len >= ifr.ifr_name.len) { -// return error.NameTooLong; -// } -// -// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); -// const socket_err = getErrno(socket_ret); -// if (socket_err > 0) { -// return error.SystemResources; -// } -// const socket_fd = i32(socket_ret); -// @memcpy(&ifr.ifr_name[0], &name[0], name.len); -// ifr.ifr_name[name.len] = 0; -// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); -// close(socket_fd); -// const ioctl_err = getErrno(ioctl_ret); -// if (ioctl_err > 0) { -// return error.Io; -// } -// return ifr.ifr_ifindex; -// } - -pub const Stat = arch.Stat; -pub const timespec = arch.timespec; - -pub fn fstat(fd: i32, stat_buf: &Stat) usize { - return arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf)); -} - -pub const epoll_data = u64; - -pub const epoll_event = extern struct { - events: u32, - data: epoll_data -}; - -pub fn epoll_create() usize { - return arch.syscall1(arch.SYS_epoll_create, usize(1)); -} - -pub fn epoll_ctl(epoll_fd: i32, op: i32, fd: i32, ev: &epoll_event) usize { - return arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev)); -} - -pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: i32, timeout: i32) usize { - return arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout)); -} - -pub fn timerfd_create(clockid: i32, flags: u32) usize { - return arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags)); -} - -pub const itimerspec = extern struct { - it_interval: timespec, - it_value: timespec -}; - -pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize { - return arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value)); -} - -pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) usize { - return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value)); -} - -test "import linux_test" { - // TODO lazy analysis should prevent this test from being compiled on windows, but - // it is still compiled on windows - if (builtin.os == builtin.Os.linux) { - _ = @import("linux_test.zig"); - } -} diff --git a/std/os/linux_errno.zig b/std/os/linux/errno.zig diff --git a/std/os/linux/i386.zig b/std/os/linux/i386.zig @@ -0,0 +1,505 @@ +const std = @import("../../index.zig"); +const linux = std.os.linux; +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; + +pub const SYS_restart_syscall = 0; +pub const SYS_exit = 1; +pub const SYS_fork = 2; +pub const SYS_read = 3; +pub const SYS_write = 4; +pub const SYS_open = 5; +pub const SYS_close = 6; +pub const SYS_waitpid = 7; +pub const SYS_creat = 8; +pub const SYS_link = 9; +pub const SYS_unlink = 10; +pub const SYS_execve = 11; +pub const SYS_chdir = 12; +pub const SYS_time = 13; +pub const SYS_mknod = 14; +pub const SYS_chmod = 15; +pub const SYS_lchown = 16; +pub const SYS_break = 17; +pub const SYS_oldstat = 18; +pub const SYS_lseek = 19; +pub const SYS_getpid = 20; +pub const SYS_mount = 21; +pub const SYS_umount = 22; +pub const SYS_setuid = 23; +pub const SYS_getuid = 24; +pub const SYS_stime = 25; +pub const SYS_ptrace = 26; +pub const SYS_alarm = 27; +pub const SYS_oldfstat = 28; +pub const SYS_pause = 29; +pub const SYS_utime = 30; +pub const SYS_stty = 31; +pub const SYS_gtty = 32; +pub const SYS_access = 33; +pub const SYS_nice = 34; +pub const SYS_ftime = 35; +pub const SYS_sync = 36; +pub const SYS_kill = 37; +pub const SYS_rename = 38; +pub const SYS_mkdir = 39; +pub const SYS_rmdir = 40; +pub const SYS_dup = 41; +pub const SYS_pipe = 42; +pub const SYS_times = 43; +pub const SYS_prof = 44; +pub const SYS_brk = 45; +pub const SYS_setgid = 46; +pub const SYS_getgid = 47; +pub const SYS_signal = 48; +pub const SYS_geteuid = 49; +pub const SYS_getegid = 50; +pub const SYS_acct = 51; +pub const SYS_umount2 = 52; +pub const SYS_lock = 53; +pub const SYS_ioctl = 54; +pub const SYS_fcntl = 55; +pub const SYS_mpx = 56; +pub const SYS_setpgid = 57; +pub const SYS_ulimit = 58; +pub const SYS_oldolduname = 59; +pub const SYS_umask = 60; +pub const SYS_chroot = 61; +pub const SYS_ustat = 62; +pub const SYS_dup2 = 63; +pub const SYS_getppid = 64; +pub const SYS_getpgrp = 65; +pub const SYS_setsid = 66; +pub const SYS_sigaction = 67; +pub const SYS_sgetmask = 68; +pub const SYS_ssetmask = 69; +pub const SYS_setreuid = 70; +pub const SYS_setregid = 71; +pub const SYS_sigsuspend = 72; +pub const SYS_sigpending = 73; +pub const SYS_sethostname = 74; +pub const SYS_setrlimit = 75; +pub const SYS_getrlimit = 76; +pub const SYS_getrusage = 77; +pub const SYS_gettimeofday = 78; +pub const SYS_settimeofday = 79; +pub const SYS_getgroups = 80; +pub const SYS_setgroups = 81; +pub const SYS_select = 82; +pub const SYS_symlink = 83; +pub const SYS_oldlstat = 84; +pub const SYS_readlink = 85; +pub const SYS_uselib = 86; +pub const SYS_swapon = 87; +pub const SYS_reboot = 88; +pub const SYS_readdir = 89; +pub const SYS_mmap = 90; +pub const SYS_munmap = 91; +pub const SYS_truncate = 92; +pub const SYS_ftruncate = 93; +pub const SYS_fchmod = 94; +pub const SYS_fchown = 95; +pub const SYS_getpriority = 96; +pub const SYS_setpriority = 97; +pub const SYS_profil = 98; +pub const SYS_statfs = 99; +pub const SYS_fstatfs = 100; +pub const SYS_ioperm = 101; +pub const SYS_socketcall = 102; +pub const SYS_syslog = 103; +pub const SYS_setitimer = 104; +pub const SYS_getitimer = 105; +pub const SYS_stat = 106; +pub const SYS_lstat = 107; +pub const SYS_fstat = 108; +pub const SYS_olduname = 109; +pub const SYS_iopl = 110; +pub const SYS_vhangup = 111; +pub const SYS_idle = 112; +pub const SYS_vm86old = 113; +pub const SYS_wait4 = 114; +pub const SYS_swapoff = 115; +pub const SYS_sysinfo = 116; +pub const SYS_ipc = 117; +pub const SYS_fsync = 118; +pub const SYS_sigreturn = 119; +pub const SYS_clone = 120; +pub const SYS_setdomainname = 121; +pub const SYS_uname = 122; +pub const SYS_modify_ldt = 123; +pub const SYS_adjtimex = 124; +pub const SYS_mprotect = 125; +pub const SYS_sigprocmask = 126; +pub const SYS_create_module = 127; +pub const SYS_init_module = 128; +pub const SYS_delete_module = 129; +pub const SYS_get_kernel_syms = 130; +pub const SYS_quotactl = 131; +pub const SYS_getpgid = 132; +pub const SYS_fchdir = 133; +pub const SYS_bdflush = 134; +pub const SYS_sysfs = 135; +pub const SYS_personality = 136; +pub const SYS_afs_syscall = 137; +pub const SYS_setfsuid = 138; +pub const SYS_setfsgid = 139; +pub const SYS__llseek = 140; +pub const SYS_getdents = 141; +pub const SYS__newselect = 142; +pub const SYS_flock = 143; +pub const SYS_msync = 144; +pub const SYS_readv = 145; +pub const SYS_writev = 146; +pub const SYS_getsid = 147; +pub const SYS_fdatasync = 148; +pub const SYS__sysctl = 149; +pub const SYS_mlock = 150; +pub const SYS_munlock = 151; +pub const SYS_mlockall = 152; +pub const SYS_munlockall = 153; +pub const SYS_sched_setparam = 154; +pub const SYS_sched_getparam = 155; +pub const SYS_sched_setscheduler = 156; +pub const SYS_sched_getscheduler = 157; +pub const SYS_sched_yield = 158; +pub const SYS_sched_get_priority_max = 159; +pub const SYS_sched_get_priority_min = 160; +pub const SYS_sched_rr_get_interval = 161; +pub const SYS_nanosleep = 162; +pub const SYS_mremap = 163; +pub const SYS_setresuid = 164; +pub const SYS_getresuid = 165; +pub const SYS_vm86 = 166; +pub const SYS_query_module = 167; +pub const SYS_poll = 168; +pub const SYS_nfsservctl = 169; +pub const SYS_setresgid = 170; +pub const SYS_getresgid = 171; +pub const SYS_prctl = 172; +pub const SYS_rt_sigreturn = 173; +pub const SYS_rt_sigaction = 174; +pub const SYS_rt_sigprocmask = 175; +pub const SYS_rt_sigpending = 176; +pub const SYS_rt_sigtimedwait = 177; +pub const SYS_rt_sigqueueinfo = 178; +pub const SYS_rt_sigsuspend = 179; +pub const SYS_pread64 = 180; +pub const SYS_pwrite64 = 181; +pub const SYS_chown = 182; +pub const SYS_getcwd = 183; +pub const SYS_capget = 184; +pub const SYS_capset = 185; +pub const SYS_sigaltstack = 186; +pub const SYS_sendfile = 187; +pub const SYS_getpmsg = 188; +pub const SYS_putpmsg = 189; +pub const SYS_vfork = 190; +pub const SYS_ugetrlimit = 191; +pub const SYS_mmap2 = 192; +pub const SYS_truncate64 = 193; +pub const SYS_ftruncate64 = 194; +pub const SYS_stat64 = 195; +pub const SYS_lstat64 = 196; +pub const SYS_fstat64 = 197; +pub const SYS_lchown32 = 198; +pub const SYS_getuid32 = 199; +pub const SYS_getgid32 = 200; +pub const SYS_geteuid32 = 201; +pub const SYS_getegid32 = 202; +pub const SYS_setreuid32 = 203; +pub const SYS_setregid32 = 204; +pub const SYS_getgroups32 = 205; +pub const SYS_setgroups32 = 206; +pub const SYS_fchown32 = 207; +pub const SYS_setresuid32 = 208; +pub const SYS_getresuid32 = 209; +pub const SYS_setresgid32 = 210; +pub const SYS_getresgid32 = 211; +pub const SYS_chown32 = 212; +pub const SYS_setuid32 = 213; +pub const SYS_setgid32 = 214; +pub const SYS_setfsuid32 = 215; +pub const SYS_setfsgid32 = 216; +pub const SYS_pivot_root = 217; +pub const SYS_mincore = 218; +pub const SYS_madvise = 219; +pub const SYS_madvise1 = 219; +pub const SYS_getdents64 = 220; +pub const SYS_fcntl64 = 221; +pub const SYS_gettid = 224; +pub const SYS_readahead = 225; +pub const SYS_setxattr = 226; +pub const SYS_lsetxattr = 227; +pub const SYS_fsetxattr = 228; +pub const SYS_getxattr = 229; +pub const SYS_lgetxattr = 230; +pub const SYS_fgetxattr = 231; +pub const SYS_listxattr = 232; +pub const SYS_llistxattr = 233; +pub const SYS_flistxattr = 234; +pub const SYS_removexattr = 235; +pub const SYS_lremovexattr = 236; +pub const SYS_fremovexattr = 237; +pub const SYS_tkill = 238; +pub const SYS_sendfile64 = 239; +pub const SYS_futex = 240; +pub const SYS_sched_setaffinity = 241; +pub const SYS_sched_getaffinity = 242; +pub const SYS_set_thread_area = 243; +pub const SYS_get_thread_area = 244; +pub const SYS_io_setup = 245; +pub const SYS_io_destroy = 246; +pub const SYS_io_getevents = 247; +pub const SYS_io_submit = 248; +pub const SYS_io_cancel = 249; +pub const SYS_fadvise64 = 250; +pub const SYS_exit_group = 252; +pub const SYS_lookup_dcookie = 253; +pub const SYS_epoll_create = 254; +pub const SYS_epoll_ctl = 255; +pub const SYS_epoll_wait = 256; +pub const SYS_remap_file_pages = 257; +pub const SYS_set_tid_address = 258; +pub const SYS_timer_create = 259; +pub const SYS_timer_settime = SYS_timer_create+1; +pub const SYS_timer_gettime = SYS_timer_create+2; +pub const SYS_timer_getoverrun = SYS_timer_create+3; +pub const SYS_timer_delete = SYS_timer_create+4; +pub const SYS_clock_settime = SYS_timer_create+5; +pub const SYS_clock_gettime = SYS_timer_create+6; +pub const SYS_clock_getres = SYS_timer_create+7; +pub const SYS_clock_nanosleep = SYS_timer_create+8; +pub const SYS_statfs64 = 268; +pub const SYS_fstatfs64 = 269; +pub const SYS_tgkill = 270; +pub const SYS_utimes = 271; +pub const SYS_fadvise64_64 = 272; +pub const SYS_vserver = 273; +pub const SYS_mbind = 274; +pub const SYS_get_mempolicy = 275; +pub const SYS_set_mempolicy = 276; +pub const SYS_mq_open = 277; +pub const SYS_mq_unlink = SYS_mq_open+1; +pub const SYS_mq_timedsend = SYS_mq_open+2; +pub const SYS_mq_timedreceive = SYS_mq_open+3; +pub const SYS_mq_notify = SYS_mq_open+4; +pub const SYS_mq_getsetattr = SYS_mq_open+5; +pub const SYS_kexec_load = 283; +pub const SYS_waitid = 284; +pub const SYS_add_key = 286; +pub const SYS_request_key = 287; +pub const SYS_keyctl = 288; +pub const SYS_ioprio_set = 289; +pub const SYS_ioprio_get = 290; +pub const SYS_inotify_init = 291; +pub const SYS_inotify_add_watch = 292; +pub const SYS_inotify_rm_watch = 293; +pub const SYS_migrate_pages = 294; +pub const SYS_openat = 295; +pub const SYS_mkdirat = 296; +pub const SYS_mknodat = 297; +pub const SYS_fchownat = 298; +pub const SYS_futimesat = 299; +pub const SYS_fstatat64 = 300; +pub const SYS_unlinkat = 301; +pub const SYS_renameat = 302; +pub const SYS_linkat = 303; +pub const SYS_symlinkat = 304; +pub const SYS_readlinkat = 305; +pub const SYS_fchmodat = 306; +pub const SYS_faccessat = 307; +pub const SYS_pselect6 = 308; +pub const SYS_ppoll = 309; +pub const SYS_unshare = 310; +pub const SYS_set_robust_list = 311; +pub const SYS_get_robust_list = 312; +pub const SYS_splice = 313; +pub const SYS_sync_file_range = 314; +pub const SYS_tee = 315; +pub const SYS_vmsplice = 316; +pub const SYS_move_pages = 317; +pub const SYS_getcpu = 318; +pub const SYS_epoll_pwait = 319; +pub const SYS_utimensat = 320; +pub const SYS_signalfd = 321; +pub const SYS_timerfd_create = 322; +pub const SYS_eventfd = 323; +pub const SYS_fallocate = 324; +pub const SYS_timerfd_settime = 325; +pub const SYS_timerfd_gettime = 326; +pub const SYS_signalfd4 = 327; +pub const SYS_eventfd2 = 328; +pub const SYS_epoll_create1 = 329; +pub const SYS_dup3 = 330; +pub const SYS_pipe2 = 331; +pub const SYS_inotify_init1 = 332; +pub const SYS_preadv = 333; +pub const SYS_pwritev = 334; +pub const SYS_rt_tgsigqueueinfo = 335; +pub const SYS_perf_event_open = 336; +pub const SYS_recvmmsg = 337; +pub const SYS_fanotify_init = 338; +pub const SYS_fanotify_mark = 339; +pub const SYS_prlimit64 = 340; +pub const SYS_name_to_handle_at = 341; +pub const SYS_open_by_handle_at = 342; +pub const SYS_clock_adjtime = 343; +pub const SYS_syncfs = 344; +pub const SYS_sendmmsg = 345; +pub const SYS_setns = 346; +pub const SYS_process_vm_readv = 347; +pub const SYS_process_vm_writev = 348; +pub const SYS_kcmp = 349; +pub const SYS_finit_module = 350; +pub const SYS_sched_setattr = 351; +pub const SYS_sched_getattr = 352; +pub const SYS_renameat2 = 353; +pub const SYS_seccomp = 354; +pub const SYS_getrandom = 355; +pub const SYS_memfd_create = 356; +pub const SYS_bpf = 357; +pub const SYS_execveat = 358; +pub const SYS_socket = 359; +pub const SYS_socketpair = 360; +pub const SYS_bind = 361; +pub const SYS_connect = 362; +pub const SYS_listen = 363; +pub const SYS_accept4 = 364; +pub const SYS_getsockopt = 365; +pub const SYS_setsockopt = 366; +pub const SYS_getsockname = 367; +pub const SYS_getpeername = 368; +pub const SYS_sendto = 369; +pub const SYS_sendmsg = 370; +pub const SYS_recvfrom = 371; +pub const SYS_recvmsg = 372; +pub const SYS_shutdown = 373; +pub const SYS_userfaultfd = 374; +pub const SYS_membarrier = 375; +pub const SYS_mlock2 = 376; + + +pub const O_CREAT = 0o100; +pub const O_EXCL = 0o200; +pub const O_NOCTTY = 0o400; +pub const O_TRUNC = 0o1000; +pub const O_APPEND = 0o2000; +pub const O_NONBLOCK = 0o4000; +pub const O_DSYNC = 0o10000; +pub const O_SYNC = 0o4010000; +pub const O_RSYNC = 0o4010000; +pub const O_DIRECTORY = 0o200000; +pub const O_NOFOLLOW = 0o400000; +pub const O_CLOEXEC = 0o2000000; + +pub const O_ASYNC = 0o20000; +pub const O_DIRECT = 0o40000; +pub const O_LARGEFILE = 0o100000; +pub const O_NOATIME = 0o1000000; +pub const O_PATH = 0o10000000; +pub const O_TMPFILE = 0o20200000; +pub const O_NDELAY = O_NONBLOCK; + +pub const F_DUPFD = 0; +pub const F_GETFD = 1; +pub const F_SETFD = 2; +pub const F_GETFL = 3; +pub const F_SETFL = 4; + +pub const F_SETOWN = 8; +pub const F_GETOWN = 9; +pub const F_SETSIG = 10; +pub const F_GETSIG = 11; + +pub const F_GETLK = 12; +pub const F_SETLK = 13; +pub const F_SETLKW = 14; + +pub const F_SETOWN_EX = 15; +pub const F_GETOWN_EX = 16; + +pub const F_GETOWNER_UIDS = 17; + +pub inline fn syscall0(number: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number)) +} + +pub inline fn syscall1(number: usize, arg1: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1)) +} + +pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2)) +} + +pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3)) +} + +pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4)) +} + +pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, + arg4: usize, arg5: usize) -> usize +{ + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4), + [arg5] "{edi}" (arg5)) +} + +pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, + arg4: usize, arg5: usize, arg6: usize) -> usize +{ + asm volatile ("int $0x80" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4), + [arg5] "{edi}" (arg5), + [arg6] "{ebp}" (arg6)) +} + +pub nakedcc fn restore() void { + asm volatile ( + \\popl %%eax + \\movl $119, %%eax + \\int $0x80 + : + : + : "rcx", "r11") +} + +pub nakedcc fn restore_rt() void { + asm volatile ("int $0x80" + : + : [number] "{eax}" (usize(SYS_rt_sigreturn)) + : "rcx", "r11") +} diff --git a/std/os/linux/index.zig b/std/os/linux/index.zig @@ -0,0 +1,796 @@ +const std = @import("../../index.zig"); +const assert = std.debug.assert; +const builtin = @import("builtin"); +const arch = switch (builtin.arch) { + builtin.Arch.x86_64 => @import("x86_64.zig"), + builtin.Arch.i386 => @import("i386.zig"), + else => @compileError("unsupported arch"), +}; +pub use @import("errno.zig"); + +pub const PATH_MAX = 4096; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT_NONE = 0; +pub const PROT_READ = 1; +pub const PROT_WRITE = 2; +pub const PROT_EXEC = 4; +pub const PROT_GROWSDOWN = 0x01000000; +pub const PROT_GROWSUP = 0x02000000; + +pub const MAP_FAILED = @maxValue(usize); +pub const MAP_SHARED = 0x01; +pub const MAP_PRIVATE = 0x02; +pub const MAP_TYPE = 0x0f; +pub const MAP_FIXED = 0x10; +pub const MAP_ANONYMOUS = 0x20; +pub const MAP_NORESERVE = 0x4000; +pub const MAP_GROWSDOWN = 0x0100; +pub const MAP_DENYWRITE = 0x0800; +pub const MAP_EXECUTABLE = 0x1000; +pub const MAP_LOCKED = 0x2000; +pub const MAP_POPULATE = 0x8000; +pub const MAP_NONBLOCK = 0x10000; +pub const MAP_STACK = 0x20000; +pub const MAP_HUGETLB = 0x40000; +pub const MAP_FILE = 0; + +pub const WNOHANG = 1; +pub const WUNTRACED = 2; +pub const WSTOPPED = 2; +pub const WEXITED = 4; +pub const WCONTINUED = 8; +pub const WNOWAIT = 0x1000000; + +pub const SA_NOCLDSTOP = 1; +pub const SA_NOCLDWAIT = 2; +pub const SA_SIGINFO = 4; +pub const SA_ONSTACK = 0x08000000; +pub const SA_RESTART = 0x10000000; +pub const SA_NODEFER = 0x40000000; +pub const SA_RESETHAND = 0x80000000; +pub const SA_RESTORER = 0x04000000; + +pub const SIGHUP = 1; +pub const SIGINT = 2; +pub const SIGQUIT = 3; +pub const SIGILL = 4; +pub const SIGTRAP = 5; +pub const SIGABRT = 6; +pub const SIGIOT = SIGABRT; +pub const SIGBUS = 7; +pub const SIGFPE = 8; +pub const SIGKILL = 9; +pub const SIGUSR1 = 10; +pub const SIGSEGV = 11; +pub const SIGUSR2 = 12; +pub const SIGPIPE = 13; +pub const SIGALRM = 14; +pub const SIGTERM = 15; +pub const SIGSTKFLT = 16; +pub const SIGCHLD = 17; +pub const SIGCONT = 18; +pub const SIGSTOP = 19; +pub const SIGTSTP = 20; +pub const SIGTTIN = 21; +pub const SIGTTOU = 22; +pub const SIGURG = 23; +pub const SIGXCPU = 24; +pub const SIGXFSZ = 25; +pub const SIGVTALRM = 26; +pub const SIGPROF = 27; +pub const SIGWINCH = 28; +pub const SIGIO = 29; +pub const SIGPOLL = 29; +pub const SIGPWR = 30; +pub const SIGSYS = 31; +pub const SIGUNUSED = SIGSYS; + +pub const O_RDONLY = 0o0; +pub const O_WRONLY = 0o1; +pub const O_RDWR = 0o2; + +pub const O_CREAT = arch.O_CREAT; +pub const O_EXCL = arch.O_EXCL; +pub const O_NOCTTY = arch.O_NOCTTY; +pub const O_TRUNC = arch.O_TRUNC; +pub const O_APPEND = arch.O_APPEND; +pub const O_NONBLOCK = arch.O_NONBLOCK; +pub const O_DSYNC = arch.O_DSYNC; +pub const O_SYNC = arch.O_SYNC; +pub const O_RSYNC = arch.O_RSYNC; +pub const O_DIRECTORY = arch.O_DIRECTORY; +pub const O_NOFOLLOW = arch.O_NOFOLLOW; +pub const O_CLOEXEC = arch.O_CLOEXEC; + +pub const O_ASYNC = arch.O_ASYNC; +pub const O_DIRECT = arch.O_DIRECT; +pub const O_LARGEFILE = arch.O_LARGEFILE; +pub const O_NOATIME = arch.O_NOATIME; +pub const O_PATH = arch.O_PATH; +pub const O_TMPFILE = arch.O_TMPFILE; +pub const O_NDELAY = arch.O_NDELAY; + +pub const SEEK_SET = 0; +pub const SEEK_CUR = 1; +pub const SEEK_END = 2; + +pub const SIG_BLOCK = 0; +pub const SIG_UNBLOCK = 1; +pub const SIG_SETMASK = 2; + +pub const SOCK_STREAM = 1; +pub const SOCK_DGRAM = 2; +pub const SOCK_RAW = 3; +pub const SOCK_RDM = 4; +pub const SOCK_SEQPACKET = 5; +pub const SOCK_DCCP = 6; +pub const SOCK_PACKET = 10; +pub const SOCK_CLOEXEC = 0o2000000; +pub const SOCK_NONBLOCK = 0o4000; + + +pub const PROTO_ip = 0o000; +pub const PROTO_icmp = 0o001; +pub const PROTO_igmp = 0o002; +pub const PROTO_ggp = 0o003; +pub const PROTO_ipencap = 0o004; +pub const PROTO_st = 0o005; +pub const PROTO_tcp = 0o006; +pub const PROTO_egp = 0o010; +pub const PROTO_pup = 0o014; +pub const PROTO_udp = 0o021; +pub const PROTO_hmp = 0o024; +pub const PROTO_xns_idp = 0o026; +pub const PROTO_rdp = 0o033; +pub const PROTO_iso_tp4 = 0o035; +pub const PROTO_xtp = 0o044; +pub const PROTO_ddp = 0o045; +pub const PROTO_idpr_cmtp = 0o046; +pub const PROTO_ipv6 = 0o051; +pub const PROTO_ipv6_route = 0o053; +pub const PROTO_ipv6_frag = 0o054; +pub const PROTO_idrp = 0o055; +pub const PROTO_rsvp = 0o056; +pub const PROTO_gre = 0o057; +pub const PROTO_esp = 0o062; +pub const PROTO_ah = 0o063; +pub const PROTO_skip = 0o071; +pub const PROTO_ipv6_icmp = 0o072; +pub const PROTO_ipv6_nonxt = 0o073; +pub const PROTO_ipv6_opts = 0o074; +pub const PROTO_rspf = 0o111; +pub const PROTO_vmtp = 0o121; +pub const PROTO_ospf = 0o131; +pub const PROTO_ipip = 0o136; +pub const PROTO_encap = 0o142; +pub const PROTO_pim = 0o147; +pub const PROTO_raw = 0o377; + +pub const PF_UNSPEC = 0; +pub const PF_LOCAL = 1; +pub const PF_UNIX = PF_LOCAL; +pub const PF_FILE = PF_LOCAL; +pub const PF_INET = 2; +pub const PF_AX25 = 3; +pub const PF_IPX = 4; +pub const PF_APPLETALK = 5; +pub const PF_NETROM = 6; +pub const PF_BRIDGE = 7; +pub const PF_ATMPVC = 8; +pub const PF_X25 = 9; +pub const PF_INET6 = 10; +pub const PF_ROSE = 11; +pub const PF_DECnet = 12; +pub const PF_NETBEUI = 13; +pub const PF_SECURITY = 14; +pub const PF_KEY = 15; +pub const PF_NETLINK = 16; +pub const PF_ROUTE = PF_NETLINK; +pub const PF_PACKET = 17; +pub const PF_ASH = 18; +pub const PF_ECONET = 19; +pub const PF_ATMSVC = 20; +pub const PF_RDS = 21; +pub const PF_SNA = 22; +pub const PF_IRDA = 23; +pub const PF_PPPOX = 24; +pub const PF_WANPIPE = 25; +pub const PF_LLC = 26; +pub const PF_IB = 27; +pub const PF_MPLS = 28; +pub const PF_CAN = 29; +pub const PF_TIPC = 30; +pub const PF_BLUETOOTH = 31; +pub const PF_IUCV = 32; +pub const PF_RXRPC = 33; +pub const PF_ISDN = 34; +pub const PF_PHONET = 35; +pub const PF_IEEE802154 = 36; +pub const PF_CAIF = 37; +pub const PF_ALG = 38; +pub const PF_NFC = 39; +pub const PF_VSOCK = 40; +pub const PF_MAX = 41; + +pub const AF_UNSPEC = PF_UNSPEC; +pub const AF_LOCAL = PF_LOCAL; +pub const AF_UNIX = AF_LOCAL; +pub const AF_FILE = AF_LOCAL; +pub const AF_INET = PF_INET; +pub const AF_AX25 = PF_AX25; +pub const AF_IPX = PF_IPX; +pub const AF_APPLETALK = PF_APPLETALK; +pub const AF_NETROM = PF_NETROM; +pub const AF_BRIDGE = PF_BRIDGE; +pub const AF_ATMPVC = PF_ATMPVC; +pub const AF_X25 = PF_X25; +pub const AF_INET6 = PF_INET6; +pub const AF_ROSE = PF_ROSE; +pub const AF_DECnet = PF_DECnet; +pub const AF_NETBEUI = PF_NETBEUI; +pub const AF_SECURITY = PF_SECURITY; +pub const AF_KEY = PF_KEY; +pub const AF_NETLINK = PF_NETLINK; +pub const AF_ROUTE = PF_ROUTE; +pub const AF_PACKET = PF_PACKET; +pub const AF_ASH = PF_ASH; +pub const AF_ECONET = PF_ECONET; +pub const AF_ATMSVC = PF_ATMSVC; +pub const AF_RDS = PF_RDS; +pub const AF_SNA = PF_SNA; +pub const AF_IRDA = PF_IRDA; +pub const AF_PPPOX = PF_PPPOX; +pub const AF_WANPIPE = PF_WANPIPE; +pub const AF_LLC = PF_LLC; +pub const AF_IB = PF_IB; +pub const AF_MPLS = PF_MPLS; +pub const AF_CAN = PF_CAN; +pub const AF_TIPC = PF_TIPC; +pub const AF_BLUETOOTH = PF_BLUETOOTH; +pub const AF_IUCV = PF_IUCV; +pub const AF_RXRPC = PF_RXRPC; +pub const AF_ISDN = PF_ISDN; +pub const AF_PHONET = PF_PHONET; +pub const AF_IEEE802154 = PF_IEEE802154; +pub const AF_CAIF = PF_CAIF; +pub const AF_ALG = PF_ALG; +pub const AF_NFC = PF_NFC; +pub const AF_VSOCK = PF_VSOCK; +pub const AF_MAX = PF_MAX; + +pub const DT_UNKNOWN = 0; +pub const DT_FIFO = 1; +pub const DT_CHR = 2; +pub const DT_DIR = 4; +pub const DT_BLK = 6; +pub const DT_REG = 8; +pub const DT_LNK = 10; +pub const DT_SOCK = 12; +pub const DT_WHT = 14; + + +pub const TCGETS = 0x5401; +pub const TCSETS = 0x5402; +pub const TCSETSW = 0x5403; +pub const TCSETSF = 0x5404; +pub const TCGETA = 0x5405; +pub const TCSETA = 0x5406; +pub const TCSETAW = 0x5407; +pub const TCSETAF = 0x5408; +pub const TCSBRK = 0x5409; +pub const TCXONC = 0x540A; +pub const TCFLSH = 0x540B; +pub const TIOCEXCL = 0x540C; +pub const TIOCNXCL = 0x540D; +pub const TIOCSCTTY = 0x540E; +pub const TIOCGPGRP = 0x540F; +pub const TIOCSPGRP = 0x5410; +pub const TIOCOUTQ = 0x5411; +pub const TIOCSTI = 0x5412; +pub const TIOCGWINSZ = 0x5413; +pub const TIOCSWINSZ = 0x5414; +pub const TIOCMGET = 0x5415; +pub const TIOCMBIS = 0x5416; +pub const TIOCMBIC = 0x5417; +pub const TIOCMSET = 0x5418; +pub const TIOCGSOFTCAR = 0x5419; +pub const TIOCSSOFTCAR = 0x541A; +pub const FIONREAD = 0x541B; +pub const TIOCINQ = FIONREAD; +pub const TIOCLINUX = 0x541C; +pub const TIOCCONS = 0x541D; +pub const TIOCGSERIAL = 0x541E; +pub const TIOCSSERIAL = 0x541F; +pub const TIOCPKT = 0x5420; +pub const FIONBIO = 0x5421; +pub const TIOCNOTTY = 0x5422; +pub const TIOCSETD = 0x5423; +pub const TIOCGETD = 0x5424; +pub const TCSBRKP = 0x5425; +pub const TIOCSBRK = 0x5427; +pub const TIOCCBRK = 0x5428; +pub const TIOCGSID = 0x5429; +pub const TIOCGRS485 = 0x542E; +pub const TIOCSRS485 = 0x542F; +pub const TIOCGPTN = 0x80045430; +pub const TIOCSPTLCK = 0x40045431; +pub const TIOCGDEV = 0x80045432; +pub const TCGETX = 0x5432; +pub const TCSETX = 0x5433; +pub const TCSETXF = 0x5434; +pub const TCSETXW = 0x5435; +pub const TIOCSIG = 0x40045436; +pub const TIOCVHANGUP = 0x5437; +pub const TIOCGPKT = 0x80045438; +pub const TIOCGPTLCK = 0x80045439; +pub const TIOCGEXCL = 0x80045440; + +pub const EPOLL_CTL_ADD = 1; +pub const EPOLL_CTL_DEL = 2; +pub const EPOLL_CTL_MOD = 3; + +pub const EPOLLIN = 0x001; +pub const EPOLLPRI = 0x002; +pub const EPOLLOUT = 0x004; +pub const EPOLLRDNORM = 0x040; +pub const EPOLLRDBAND = 0x080; +pub const EPOLLWRNORM = 0x100; +pub const EPOLLWRBAND = 0x200; +pub const EPOLLMSG = 0x400; +pub const EPOLLERR = 0x008; +pub const EPOLLHUP = 0x010; +pub const EPOLLRDHUP = 0x2000; +pub const EPOLLEXCLUSIVE = (u32(1) << 28); +pub const EPOLLWAKEUP = (u32(1) << 29); +pub const EPOLLONESHOT = (u32(1) << 30); +pub const EPOLLET = (u32(1) << 31); + +pub const CLOCK_REALTIME = 0; +pub const CLOCK_MONOTONIC = 1; +pub const CLOCK_PROCESS_CPUTIME_ID = 2; +pub const CLOCK_THREAD_CPUTIME_ID = 3; +pub const CLOCK_MONOTONIC_RAW = 4; +pub const CLOCK_REALTIME_COARSE = 5; +pub const CLOCK_MONOTONIC_COARSE = 6; +pub const CLOCK_BOOTTIME = 7; +pub const CLOCK_REALTIME_ALARM = 8; +pub const CLOCK_BOOTTIME_ALARM = 9; +pub const CLOCK_SGI_CYCLE = 10; +pub const CLOCK_TAI = 11; + +pub const TFD_NONBLOCK = O_NONBLOCK; +pub const TFD_CLOEXEC = O_CLOEXEC; + +pub const TFD_TIMER_ABSTIME = 1; +pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1); + +fn unsigned(s: i32) u32 { return @bitCast(u32, s); } +fn signed(s: u32) i32 { return @bitCast(i32, s); } +pub fn WEXITSTATUS(s: i32) i32 { return signed((unsigned(s) & 0xff00) >> 8); } +pub fn WTERMSIG(s: i32) i32 { return signed(unsigned(s) & 0x7f); } +pub fn WSTOPSIG(s: i32) i32 { return WEXITSTATUS(s); } +pub fn WIFEXITED(s: i32) bool { return WTERMSIG(s) == 0; } +pub fn WIFSTOPPED(s: i32) bool { return (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00; } +pub fn WIFSIGNALED(s: i32) bool { return (unsigned(s)&0xffff)-%1 < 0xff; } + + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +/// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) usize { + const signed_r = @bitCast(isize, r); + return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0; +} + +pub fn dup2(old: i32, new: i32) usize { + return arch.syscall2(arch.SYS_dup2, usize(old), usize(new)); +} + +pub fn chdir(path: &const u8) usize { + return arch.syscall1(arch.SYS_chdir, @ptrToInt(path)); +} + +pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize { + return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); +} + +pub fn fork() usize { + return arch.syscall0(arch.SYS_fork); +} + +pub fn getcwd(buf: &u8, size: usize) usize { + return arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size); +} + +pub fn getdents(fd: i32, dirp: &u8, count: usize) usize { + return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count); +} + +pub fn isatty(fd: i32) bool { + var wsz: winsize = undefined; + return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; +} + +pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize { + return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); +} + +pub fn mkdir(path: &const u8, mode: u32) usize { + return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode); +} + +pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize { + return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), + @bitCast(usize, offset)); +} + +pub fn munmap(address: &u8, length: usize) usize { + return arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length); +} + +pub fn read(fd: i32, buf: &u8, count: usize) usize { + return arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count); +} + +pub fn rmdir(path: &const u8) usize { + return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)); +} + +pub fn symlink(existing: &const u8, new: &const u8) usize { + return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); +} + +pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize { + return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset); +} + +pub fn pipe(fd: &[2]i32) usize { + return pipe2(fd, 0); +} + +pub fn pipe2(fd: &[2]i32, flags: usize) usize { + return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags); +} + +pub fn write(fd: i32, buf: &const u8, count: usize) usize { + return arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count); +} + +pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) usize { + return arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset); +} + +pub fn rename(old: &const u8, new: &const u8) usize { + return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)); +} + +pub fn open(path: &const u8, flags: u32, perm: usize) usize { + return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm); +} + +pub fn create(path: &const u8, perm: usize) usize { + return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm); +} + +pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize { + return arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode); +} + +pub fn close(fd: i32) usize { + return arch.syscall1(arch.SYS_close, usize(fd)); +} + +pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize { + return arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos); +} + +pub fn exit(status: i32) noreturn { + _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn getrandom(buf: &u8, count: usize, flags: u32) usize { + return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags)); +} + +pub fn kill(pid: i32, sig: i32) usize { + return arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)); +} + +pub fn unlink(path: &const u8) usize { + return arch.syscall1(arch.SYS_unlink, @ptrToInt(path)); +} + +pub fn waitpid(pid: i32, status: &i32, options: i32) usize { + return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); +} + +pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize { + return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); +} + +pub fn setuid(uid: u32) usize { + return arch.syscall1(arch.SYS_setuid, uid); +} + +pub fn setgid(gid: u32) usize { + return arch.syscall1(arch.SYS_setgid, gid); +} + +pub fn setreuid(ruid: u32, euid: u32) usize { + return arch.syscall2(arch.SYS_setreuid, ruid, euid); +} + +pub fn setregid(rgid: u32, egid: u32) usize { + return arch.syscall2(arch.SYS_setregid, rgid, egid); +} + +pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize { + return arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8); +} + +pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize { + assert(sig >= 1); + assert(sig != SIGKILL); + assert(sig != SIGSTOP); + var ksa = k_sigaction { + .handler = act.handler, + .flags = act.flags | SA_RESTORER, + .mask = undefined, + .restorer = @ptrCast(extern fn()void, arch.restore_rt), + }; + var ksa_old: k_sigaction = undefined; + @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8); + const result = arch.syscall4(arch.SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); + const err = getErrno(result); + if (err != 0) { + return result; + } + if (oact) |old| { + old.handler = ksa_old.handler; + old.flags = @truncate(u32, ksa_old.flags); + @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); + } + return 0; +} + +const NSIG = 65; +const sigset_t = [128 / @sizeOf(usize)]usize; +const all_mask = []usize{@maxValue(usize)}; +const app_mask = []usize{0xfffffffc7fffffff}; + +const k_sigaction = extern struct { + handler: extern fn(i32)void, + flags: usize, + restorer: extern fn()void, + mask: [2]u32, +}; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = struct { + handler: extern fn(i32)void, + mask: sigset_t, + flags: u32, +}; + +pub const SIG_ERR = @intToPtr(extern fn(i32)void, @maxValue(usize)); +pub const SIG_DFL = @intToPtr(extern fn(i32)void, 0); +pub const SIG_IGN = @intToPtr(extern fn(i32)void, 1); +pub const empty_sigset = []usize{0} ** sigset_t.len; + +pub fn raise(sig: i32) usize { + var set: sigset_t = undefined; + blockAppSignals(&set); + const tid = i32(arch.syscall0(arch.SYS_gettid)); + const ret = arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig)); + restoreSignals(&set); + return ret; +} + +fn blockAllSignals(set: &sigset_t) void { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8); +} + +fn blockAppSignals(set: &sigset_t) void { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8); +} + +fn restoreSignals(set: &sigset_t) void { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8); +} + +pub fn sigaddset(set: &sigset_t, sig: u6) void { + const s = sig - 1; + (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1)); +} + +pub fn sigismember(set: &const sigset_t, sig: u6) bool { + const s = sig - 1; + return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0; +} + + +pub const sa_family_t = u16; +pub const socklen_t = u32; +pub const in_addr = u32; +pub const in6_addr = [16]u8; + +pub const sockaddr = extern struct { + family: sa_family_t, + port: u16, + data: [12]u8, +}; + +pub const sockaddr_in = extern struct { + family: sa_family_t, + port: u16, + addr: in_addr, + zero: [8]u8, +}; + +pub const sockaddr_in6 = extern struct { + family: sa_family_t, + port: u16, + flowinfo: u32, + addr: in6_addr, + scope_id: u32, +}; + +pub const iovec = extern struct { + iov_base: &u8, + iov_len: usize, +}; + +pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { + return arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { + return arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn socket(domain: i32, socket_type: i32, protocol: i32) usize { + return arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)); +} + +pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) usize { + return arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen)); +} + +pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) usize { + return arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen)); +} + +pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) usize { + return arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags); +} + +pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) usize { + return arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len)); +} + +pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) usize { + return arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags); +} + +pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, + noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize +{ + return arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); +} + +pub fn shutdown(fd: i32, how: i32) usize { + return arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)); +} + +pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) usize { + return arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len)); +} + +pub fn listen(fd: i32, backlog: i32) usize { + return arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)); +} + +pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) usize { + return arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen)); +} + +pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { + return arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0])); +} + +pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize { + return accept4(fd, addr, len, 0); +} + +pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) usize { + return arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags); +} + +// error NameTooLong; +// error SystemResources; +// error Io; +// +// pub fn if_nametoindex(name: []u8) %u32 { +// var ifr: ifreq = undefined; +// +// if (name.len >= ifr.ifr_name.len) { +// return error.NameTooLong; +// } +// +// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); +// const socket_err = getErrno(socket_ret); +// if (socket_err > 0) { +// return error.SystemResources; +// } +// const socket_fd = i32(socket_ret); +// @memcpy(&ifr.ifr_name[0], &name[0], name.len); +// ifr.ifr_name[name.len] = 0; +// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); +// close(socket_fd); +// const ioctl_err = getErrno(ioctl_ret); +// if (ioctl_err > 0) { +// return error.Io; +// } +// return ifr.ifr_ifindex; +// } + +pub const Stat = arch.Stat; +pub const timespec = arch.timespec; + +pub fn fstat(fd: i32, stat_buf: &Stat) usize { + return arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf)); +} + +pub const epoll_data = u64; + +pub const epoll_event = extern struct { + events: u32, + data: epoll_data +}; + +pub fn epoll_create() usize { + return arch.syscall1(arch.SYS_epoll_create, usize(1)); +} + +pub fn epoll_ctl(epoll_fd: i32, op: i32, fd: i32, ev: &epoll_event) usize { + return arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev)); +} + +pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: i32, timeout: i32) usize { + return arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout)); +} + +pub fn timerfd_create(clockid: i32, flags: u32) usize { + return arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags)); +} + +pub const itimerspec = extern struct { + it_interval: timespec, + it_value: timespec +}; + +pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize { + return arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value)); +} + +pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) usize { + return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value)); +} + +test "import linux test" { + // TODO lazy analysis should prevent this test from being compiled on windows, but + // it is still compiled on windows + if (builtin.os == builtin.Os.linux) { + _ = @import("test.zig"); + } +} diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig @@ -0,0 +1,38 @@ +const std = @import("../../index.zig"); +const linux = std.os.linux; +const assert = std.debug.assert; + +test "timer" { + const epoll_fd = linux.epoll_create(); + var err = linux.getErrno(epoll_fd); + assert(err == 0); + + const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0); + assert(linux.getErrno(timer_fd) == 0); + + const time_interval = linux.timespec { + .tv_sec = 0, + .tv_nsec = 2000000 + }; + + const new_time = linux.itimerspec { + .it_interval = time_interval, + .it_value = time_interval + }; + + err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null); + assert(err == 0); + + var event = linux.epoll_event { + .events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET, + .data = 0 + }; + + err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event); + assert(err == 0); + + const events_one: linux.epoll_event = undefined; + var events = []linux.epoll_event{events_one} ** 8; + + err = linux.epoll_wait(i32(epoll_fd), &events[0], 8, -1); +} diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig @@ -0,0 +1,490 @@ +const std = @import("../../index.zig"); +const linux = std.os.linux; +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; + +pub const SYS_read = 0; +pub const SYS_write = 1; +pub const SYS_open = 2; +pub const SYS_close = 3; +pub const SYS_stat = 4; +pub const SYS_fstat = 5; +pub const SYS_lstat = 6; +pub const SYS_poll = 7; +pub const SYS_lseek = 8; +pub const SYS_mmap = 9; +pub const SYS_mprotect = 10; +pub const SYS_munmap = 11; +pub const SYS_brk = 12; +pub const SYS_rt_sigaction = 13; +pub const SYS_rt_sigprocmask = 14; +pub const SYS_rt_sigreturn = 15; +pub const SYS_ioctl = 16; +pub const SYS_pread = 17; +pub const SYS_pwrite = 18; +pub const SYS_readv = 19; +pub const SYS_writev = 20; +pub const SYS_access = 21; +pub const SYS_pipe = 22; +pub const SYS_select = 23; +pub const SYS_sched_yield = 24; +pub const SYS_mremap = 25; +pub const SYS_msync = 26; +pub const SYS_mincore = 27; +pub const SYS_madvise = 28; +pub const SYS_shmget = 29; +pub const SYS_shmat = 30; +pub const SYS_shmctl = 31; +pub const SYS_dup = 32; +pub const SYS_dup2 = 33; +pub const SYS_pause = 34; +pub const SYS_nanosleep = 35; +pub const SYS_getitimer = 36; +pub const SYS_alarm = 37; +pub const SYS_setitimer = 38; +pub const SYS_getpid = 39; +pub const SYS_sendfile = 40; +pub const SYS_socket = 41; +pub const SYS_connect = 42; +pub const SYS_accept = 43; +pub const SYS_sendto = 44; +pub const SYS_recvfrom = 45; +pub const SYS_sendmsg = 46; +pub const SYS_recvmsg = 47; +pub const SYS_shutdown = 48; +pub const SYS_bind = 49; +pub const SYS_listen = 50; +pub const SYS_getsockname = 51; +pub const SYS_getpeername = 52; +pub const SYS_socketpair = 53; +pub const SYS_setsockopt = 54; +pub const SYS_getsockopt = 55; +pub const SYS_clone = 56; +pub const SYS_fork = 57; +pub const SYS_vfork = 58; +pub const SYS_execve = 59; +pub const SYS_exit = 60; +pub const SYS_wait4 = 61; +pub const SYS_kill = 62; +pub const SYS_uname = 63; +pub const SYS_semget = 64; +pub const SYS_semop = 65; +pub const SYS_semctl = 66; +pub const SYS_shmdt = 67; +pub const SYS_msgget = 68; +pub const SYS_msgsnd = 69; +pub const SYS_msgrcv = 70; +pub const SYS_msgctl = 71; +pub const SYS_fcntl = 72; +pub const SYS_flock = 73; +pub const SYS_fsync = 74; +pub const SYS_fdatasync = 75; +pub const SYS_truncate = 76; +pub const SYS_ftruncate = 77; +pub const SYS_getdents = 78; +pub const SYS_getcwd = 79; +pub const SYS_chdir = 80; +pub const SYS_fchdir = 81; +pub const SYS_rename = 82; +pub const SYS_mkdir = 83; +pub const SYS_rmdir = 84; +pub const SYS_creat = 85; +pub const SYS_link = 86; +pub const SYS_unlink = 87; +pub const SYS_symlink = 88; +pub const SYS_readlink = 89; +pub const SYS_chmod = 90; +pub const SYS_fchmod = 91; +pub const SYS_chown = 92; +pub const SYS_fchown = 93; +pub const SYS_lchown = 94; +pub const SYS_umask = 95; +pub const SYS_gettimeofday = 96; +pub const SYS_getrlimit = 97; +pub const SYS_getrusage = 98; +pub const SYS_sysinfo = 99; +pub const SYS_times = 100; +pub const SYS_ptrace = 101; +pub const SYS_getuid = 102; +pub const SYS_syslog = 103; +pub const SYS_getgid = 104; +pub const SYS_setuid = 105; +pub const SYS_setgid = 106; +pub const SYS_geteuid = 107; +pub const SYS_getegid = 108; +pub const SYS_setpgid = 109; +pub const SYS_getppid = 110; +pub const SYS_getpgrp = 111; +pub const SYS_setsid = 112; +pub const SYS_setreuid = 113; +pub const SYS_setregid = 114; +pub const SYS_getgroups = 115; +pub const SYS_setgroups = 116; +pub const SYS_setresuid = 117; +pub const SYS_getresuid = 118; +pub const SYS_setresgid = 119; +pub const SYS_getresgid = 120; +pub const SYS_getpgid = 121; +pub const SYS_setfsuid = 122; +pub const SYS_setfsgid = 123; +pub const SYS_getsid = 124; +pub const SYS_capget = 125; +pub const SYS_capset = 126; +pub const SYS_rt_sigpending = 127; +pub const SYS_rt_sigtimedwait = 128; +pub const SYS_rt_sigqueueinfo = 129; +pub const SYS_rt_sigsuspend = 130; +pub const SYS_sigaltstack = 131; +pub const SYS_utime = 132; +pub const SYS_mknod = 133; +pub const SYS_uselib = 134; +pub const SYS_personality = 135; +pub const SYS_ustat = 136; +pub const SYS_statfs = 137; +pub const SYS_fstatfs = 138; +pub const SYS_sysfs = 139; +pub const SYS_getpriority = 140; +pub const SYS_setpriority = 141; +pub const SYS_sched_setparam = 142; +pub const SYS_sched_getparam = 143; +pub const SYS_sched_setscheduler = 144; +pub const SYS_sched_getscheduler = 145; +pub const SYS_sched_get_priority_max = 146; +pub const SYS_sched_get_priority_min = 147; +pub const SYS_sched_rr_get_interval = 148; +pub const SYS_mlock = 149; +pub const SYS_munlock = 150; +pub const SYS_mlockall = 151; +pub const SYS_munlockall = 152; +pub const SYS_vhangup = 153; +pub const SYS_modify_ldt = 154; +pub const SYS_pivot_root = 155; +pub const SYS__sysctl = 156; +pub const SYS_prctl = 157; +pub const SYS_arch_prctl = 158; +pub const SYS_adjtimex = 159; +pub const SYS_setrlimit = 160; +pub const SYS_chroot = 161; +pub const SYS_sync = 162; +pub const SYS_acct = 163; +pub const SYS_settimeofday = 164; +pub const SYS_mount = 165; +pub const SYS_umount2 = 166; +pub const SYS_swapon = 167; +pub const SYS_swapoff = 168; +pub const SYS_reboot = 169; +pub const SYS_sethostname = 170; +pub const SYS_setdomainname = 171; +pub const SYS_iopl = 172; +pub const SYS_ioperm = 173; +pub const SYS_create_module = 174; +pub const SYS_init_module = 175; +pub const SYS_delete_module = 176; +pub const SYS_get_kernel_syms = 177; +pub const SYS_query_module = 178; +pub const SYS_quotactl = 179; +pub const SYS_nfsservctl = 180; +pub const SYS_getpmsg = 181; +pub const SYS_putpmsg = 182; +pub const SYS_afs_syscall = 183; +pub const SYS_tuxcall = 184; +pub const SYS_security = 185; +pub const SYS_gettid = 186; +pub const SYS_readahead = 187; +pub const SYS_setxattr = 188; +pub const SYS_lsetxattr = 189; +pub const SYS_fsetxattr = 190; +pub const SYS_getxattr = 191; +pub const SYS_lgetxattr = 192; +pub const SYS_fgetxattr = 193; +pub const SYS_listxattr = 194; +pub const SYS_llistxattr = 195; +pub const SYS_flistxattr = 196; +pub const SYS_removexattr = 197; +pub const SYS_lremovexattr = 198; +pub const SYS_fremovexattr = 199; +pub const SYS_tkill = 200; +pub const SYS_time = 201; +pub const SYS_futex = 202; +pub const SYS_sched_setaffinity = 203; +pub const SYS_sched_getaffinity = 204; +pub const SYS_set_thread_area = 205; +pub const SYS_io_setup = 206; +pub const SYS_io_destroy = 207; +pub const SYS_io_getevents = 208; +pub const SYS_io_submit = 209; +pub const SYS_io_cancel = 210; +pub const SYS_get_thread_area = 211; +pub const SYS_lookup_dcookie = 212; +pub const SYS_epoll_create = 213; +pub const SYS_epoll_ctl_old = 214; +pub const SYS_epoll_wait_old = 215; +pub const SYS_remap_file_pages = 216; +pub const SYS_getdents64 = 217; +pub const SYS_set_tid_address = 218; +pub const SYS_restart_syscall = 219; +pub const SYS_semtimedop = 220; +pub const SYS_fadvise64 = 221; +pub const SYS_timer_create = 222; +pub const SYS_timer_settime = 223; +pub const SYS_timer_gettime = 224; +pub const SYS_timer_getoverrun = 225; +pub const SYS_timer_delete = 226; +pub const SYS_clock_settime = 227; +pub const SYS_clock_gettime = 228; +pub const SYS_clock_getres = 229; +pub const SYS_clock_nanosleep = 230; +pub const SYS_exit_group = 231; +pub const SYS_epoll_wait = 232; +pub const SYS_epoll_ctl = 233; +pub const SYS_tgkill = 234; +pub const SYS_utimes = 235; +pub const SYS_vserver = 236; +pub const SYS_mbind = 237; +pub const SYS_set_mempolicy = 238; +pub const SYS_get_mempolicy = 239; +pub const SYS_mq_open = 240; +pub const SYS_mq_unlink = 241; +pub const SYS_mq_timedsend = 242; +pub const SYS_mq_timedreceive = 243; +pub const SYS_mq_notify = 244; +pub const SYS_mq_getsetattr = 245; +pub const SYS_kexec_load = 246; +pub const SYS_waitid = 247; +pub const SYS_add_key = 248; +pub const SYS_request_key = 249; +pub const SYS_keyctl = 250; +pub const SYS_ioprio_set = 251; +pub const SYS_ioprio_get = 252; +pub const SYS_inotify_init = 253; +pub const SYS_inotify_add_watch = 254; +pub const SYS_inotify_rm_watch = 255; +pub const SYS_migrate_pages = 256; +pub const SYS_openat = 257; +pub const SYS_mkdirat = 258; +pub const SYS_mknodat = 259; +pub const SYS_fchownat = 260; +pub const SYS_futimesat = 261; +pub const SYS_newfstatat = 262; +pub const SYS_unlinkat = 263; +pub const SYS_renameat = 264; +pub const SYS_linkat = 265; +pub const SYS_symlinkat = 266; +pub const SYS_readlinkat = 267; +pub const SYS_fchmodat = 268; +pub const SYS_faccessat = 269; +pub const SYS_pselect6 = 270; +pub const SYS_ppoll = 271; +pub const SYS_unshare = 272; +pub const SYS_set_robust_list = 273; +pub const SYS_get_robust_list = 274; +pub const SYS_splice = 275; +pub const SYS_tee = 276; +pub const SYS_sync_file_range = 277; +pub const SYS_vmsplice = 278; +pub const SYS_move_pages = 279; +pub const SYS_utimensat = 280; +pub const SYS_epoll_pwait = 281; +pub const SYS_signalfd = 282; +pub const SYS_timerfd_create = 283; +pub const SYS_eventfd = 284; +pub const SYS_fallocate = 285; +pub const SYS_timerfd_settime = 286; +pub const SYS_timerfd_gettime = 287; +pub const SYS_accept4 = 288; +pub const SYS_signalfd4 = 289; +pub const SYS_eventfd2 = 290; +pub const SYS_epoll_create1 = 291; +pub const SYS_dup3 = 292; +pub const SYS_pipe2 = 293; +pub const SYS_inotify_init1 = 294; +pub const SYS_preadv = 295; +pub const SYS_pwritev = 296; +pub const SYS_rt_tgsigqueueinfo = 297; +pub const SYS_perf_event_open = 298; +pub const SYS_recvmmsg = 299; +pub const SYS_fanotify_init = 300; +pub const SYS_fanotify_mark = 301; +pub const SYS_prlimit64 = 302; +pub const SYS_name_to_handle_at = 303; +pub const SYS_open_by_handle_at = 304; +pub const SYS_clock_adjtime = 305; +pub const SYS_syncfs = 306; +pub const SYS_sendmmsg = 307; +pub const SYS_setns = 308; +pub const SYS_getcpu = 309; +pub const SYS_process_vm_readv = 310; +pub const SYS_process_vm_writev = 311; +pub const SYS_kcmp = 312; +pub const SYS_finit_module = 313; +pub const SYS_sched_setattr = 314; +pub const SYS_sched_getattr = 315; +pub const SYS_renameat2 = 316; +pub const SYS_seccomp = 317; +pub const SYS_getrandom = 318; +pub const SYS_memfd_create = 319; +pub const SYS_kexec_file_load = 320; +pub const SYS_bpf = 321; +pub const SYS_execveat = 322; +pub const SYS_userfaultfd = 323; +pub const SYS_membarrier = 324; +pub const SYS_mlock2 = 325; + +pub const O_CREAT = 0o100; +pub const O_EXCL = 0o200; +pub const O_NOCTTY = 0o400; +pub const O_TRUNC = 0o1000; +pub const O_APPEND = 0o2000; +pub const O_NONBLOCK = 0o4000; +pub const O_DSYNC = 0o10000; +pub const O_SYNC = 0o4010000; +pub const O_RSYNC = 0o4010000; +pub const O_DIRECTORY = 0o200000; +pub const O_NOFOLLOW = 0o400000; +pub const O_CLOEXEC = 0o2000000; + +pub const O_ASYNC = 0o20000; +pub const O_DIRECT = 0o40000; +pub const O_LARGEFILE = 0; +pub const O_NOATIME = 0o1000000; +pub const O_PATH = 0o10000000; +pub const O_TMPFILE = 0o20200000; +pub const O_NDELAY = O_NONBLOCK; + +pub const F_DUPFD = 0; +pub const F_GETFD = 1; +pub const F_SETFD = 2; +pub const F_GETFL = 3; +pub const F_SETFL = 4; + +pub const F_SETOWN = 8; +pub const F_GETOWN = 9; +pub const F_SETSIG = 10; +pub const F_GETSIG = 11; + +pub const F_GETLK = 5; +pub const F_SETLK = 6; +pub const F_SETLKW = 7; + +pub const F_SETOWN_EX = 15; +pub const F_GETOWN_EX = 16; + +pub const F_GETOWNER_UIDS = 17; + +pub fn syscall0(number: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number) + : "rcx", "r11"); +} + +pub fn syscall1(number: usize, arg1: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1) + : "rcx", "r11"); +} + +pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2) + : "rcx", "r11"); +} + +pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3) + : "rcx", "r11"); +} + +pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4) + : "rcx", "r11"); +} + +pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize { + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4), + [arg5] "{r8}" (arg5) + : "rcx", "r11"); +} + +pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, + arg5: usize, arg6: usize) usize +{ + return asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4), + [arg5] "{r8}" (arg5), + [arg6] "{r9}" (arg6) + : "rcx", "r11"); +} + +pub nakedcc fn restore_rt() void { + return asm volatile ("syscall" + : + : [number] "{rax}" (usize(SYS_rt_sigreturn)) + : "rcx", "r11"); +} + + +pub const msghdr = extern struct { + msg_name: &u8, + msg_namelen: socklen_t, + msg_iov: &iovec, + msg_iovlen: i32, + __pad1: i32, + msg_control: &u8, + msg_controllen: socklen_t, + __pad2: socklen_t, + msg_flags: i32, +}; + +/// Renamed to Stat to not conflict with the stat function. +pub const Stat = extern struct { + dev: u64, + ino: u64, + nlink: usize, + + mode: u32, + uid: u32, + gid: u32, + __pad0: u32, + rdev: u64, + size: i64, + blksize: isize, + blocks: i64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + __unused: [3]isize, +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; diff --git a/std/os/linux_i386.zig b/std/os/linux_i386.zig @@ -1,504 +0,0 @@ -const linux = @import("linux.zig"); -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; - -pub const SYS_restart_syscall = 0; -pub const SYS_exit = 1; -pub const SYS_fork = 2; -pub const SYS_read = 3; -pub const SYS_write = 4; -pub const SYS_open = 5; -pub const SYS_close = 6; -pub const SYS_waitpid = 7; -pub const SYS_creat = 8; -pub const SYS_link = 9; -pub const SYS_unlink = 10; -pub const SYS_execve = 11; -pub const SYS_chdir = 12; -pub const SYS_time = 13; -pub const SYS_mknod = 14; -pub const SYS_chmod = 15; -pub const SYS_lchown = 16; -pub const SYS_break = 17; -pub const SYS_oldstat = 18; -pub const SYS_lseek = 19; -pub const SYS_getpid = 20; -pub const SYS_mount = 21; -pub const SYS_umount = 22; -pub const SYS_setuid = 23; -pub const SYS_getuid = 24; -pub const SYS_stime = 25; -pub const SYS_ptrace = 26; -pub const SYS_alarm = 27; -pub const SYS_oldfstat = 28; -pub const SYS_pause = 29; -pub const SYS_utime = 30; -pub const SYS_stty = 31; -pub const SYS_gtty = 32; -pub const SYS_access = 33; -pub const SYS_nice = 34; -pub const SYS_ftime = 35; -pub const SYS_sync = 36; -pub const SYS_kill = 37; -pub const SYS_rename = 38; -pub const SYS_mkdir = 39; -pub const SYS_rmdir = 40; -pub const SYS_dup = 41; -pub const SYS_pipe = 42; -pub const SYS_times = 43; -pub const SYS_prof = 44; -pub const SYS_brk = 45; -pub const SYS_setgid = 46; -pub const SYS_getgid = 47; -pub const SYS_signal = 48; -pub const SYS_geteuid = 49; -pub const SYS_getegid = 50; -pub const SYS_acct = 51; -pub const SYS_umount2 = 52; -pub const SYS_lock = 53; -pub const SYS_ioctl = 54; -pub const SYS_fcntl = 55; -pub const SYS_mpx = 56; -pub const SYS_setpgid = 57; -pub const SYS_ulimit = 58; -pub const SYS_oldolduname = 59; -pub const SYS_umask = 60; -pub const SYS_chroot = 61; -pub const SYS_ustat = 62; -pub const SYS_dup2 = 63; -pub const SYS_getppid = 64; -pub const SYS_getpgrp = 65; -pub const SYS_setsid = 66; -pub const SYS_sigaction = 67; -pub const SYS_sgetmask = 68; -pub const SYS_ssetmask = 69; -pub const SYS_setreuid = 70; -pub const SYS_setregid = 71; -pub const SYS_sigsuspend = 72; -pub const SYS_sigpending = 73; -pub const SYS_sethostname = 74; -pub const SYS_setrlimit = 75; -pub const SYS_getrlimit = 76; -pub const SYS_getrusage = 77; -pub const SYS_gettimeofday = 78; -pub const SYS_settimeofday = 79; -pub const SYS_getgroups = 80; -pub const SYS_setgroups = 81; -pub const SYS_select = 82; -pub const SYS_symlink = 83; -pub const SYS_oldlstat = 84; -pub const SYS_readlink = 85; -pub const SYS_uselib = 86; -pub const SYS_swapon = 87; -pub const SYS_reboot = 88; -pub const SYS_readdir = 89; -pub const SYS_mmap = 90; -pub const SYS_munmap = 91; -pub const SYS_truncate = 92; -pub const SYS_ftruncate = 93; -pub const SYS_fchmod = 94; -pub const SYS_fchown = 95; -pub const SYS_getpriority = 96; -pub const SYS_setpriority = 97; -pub const SYS_profil = 98; -pub const SYS_statfs = 99; -pub const SYS_fstatfs = 100; -pub const SYS_ioperm = 101; -pub const SYS_socketcall = 102; -pub const SYS_syslog = 103; -pub const SYS_setitimer = 104; -pub const SYS_getitimer = 105; -pub const SYS_stat = 106; -pub const SYS_lstat = 107; -pub const SYS_fstat = 108; -pub const SYS_olduname = 109; -pub const SYS_iopl = 110; -pub const SYS_vhangup = 111; -pub const SYS_idle = 112; -pub const SYS_vm86old = 113; -pub const SYS_wait4 = 114; -pub const SYS_swapoff = 115; -pub const SYS_sysinfo = 116; -pub const SYS_ipc = 117; -pub const SYS_fsync = 118; -pub const SYS_sigreturn = 119; -pub const SYS_clone = 120; -pub const SYS_setdomainname = 121; -pub const SYS_uname = 122; -pub const SYS_modify_ldt = 123; -pub const SYS_adjtimex = 124; -pub const SYS_mprotect = 125; -pub const SYS_sigprocmask = 126; -pub const SYS_create_module = 127; -pub const SYS_init_module = 128; -pub const SYS_delete_module = 129; -pub const SYS_get_kernel_syms = 130; -pub const SYS_quotactl = 131; -pub const SYS_getpgid = 132; -pub const SYS_fchdir = 133; -pub const SYS_bdflush = 134; -pub const SYS_sysfs = 135; -pub const SYS_personality = 136; -pub const SYS_afs_syscall = 137; -pub const SYS_setfsuid = 138; -pub const SYS_setfsgid = 139; -pub const SYS__llseek = 140; -pub const SYS_getdents = 141; -pub const SYS__newselect = 142; -pub const SYS_flock = 143; -pub const SYS_msync = 144; -pub const SYS_readv = 145; -pub const SYS_writev = 146; -pub const SYS_getsid = 147; -pub const SYS_fdatasync = 148; -pub const SYS__sysctl = 149; -pub const SYS_mlock = 150; -pub const SYS_munlock = 151; -pub const SYS_mlockall = 152; -pub const SYS_munlockall = 153; -pub const SYS_sched_setparam = 154; -pub const SYS_sched_getparam = 155; -pub const SYS_sched_setscheduler = 156; -pub const SYS_sched_getscheduler = 157; -pub const SYS_sched_yield = 158; -pub const SYS_sched_get_priority_max = 159; -pub const SYS_sched_get_priority_min = 160; -pub const SYS_sched_rr_get_interval = 161; -pub const SYS_nanosleep = 162; -pub const SYS_mremap = 163; -pub const SYS_setresuid = 164; -pub const SYS_getresuid = 165; -pub const SYS_vm86 = 166; -pub const SYS_query_module = 167; -pub const SYS_poll = 168; -pub const SYS_nfsservctl = 169; -pub const SYS_setresgid = 170; -pub const SYS_getresgid = 171; -pub const SYS_prctl = 172; -pub const SYS_rt_sigreturn = 173; -pub const SYS_rt_sigaction = 174; -pub const SYS_rt_sigprocmask = 175; -pub const SYS_rt_sigpending = 176; -pub const SYS_rt_sigtimedwait = 177; -pub const SYS_rt_sigqueueinfo = 178; -pub const SYS_rt_sigsuspend = 179; -pub const SYS_pread64 = 180; -pub const SYS_pwrite64 = 181; -pub const SYS_chown = 182; -pub const SYS_getcwd = 183; -pub const SYS_capget = 184; -pub const SYS_capset = 185; -pub const SYS_sigaltstack = 186; -pub const SYS_sendfile = 187; -pub const SYS_getpmsg = 188; -pub const SYS_putpmsg = 189; -pub const SYS_vfork = 190; -pub const SYS_ugetrlimit = 191; -pub const SYS_mmap2 = 192; -pub const SYS_truncate64 = 193; -pub const SYS_ftruncate64 = 194; -pub const SYS_stat64 = 195; -pub const SYS_lstat64 = 196; -pub const SYS_fstat64 = 197; -pub const SYS_lchown32 = 198; -pub const SYS_getuid32 = 199; -pub const SYS_getgid32 = 200; -pub const SYS_geteuid32 = 201; -pub const SYS_getegid32 = 202; -pub const SYS_setreuid32 = 203; -pub const SYS_setregid32 = 204; -pub const SYS_getgroups32 = 205; -pub const SYS_setgroups32 = 206; -pub const SYS_fchown32 = 207; -pub const SYS_setresuid32 = 208; -pub const SYS_getresuid32 = 209; -pub const SYS_setresgid32 = 210; -pub const SYS_getresgid32 = 211; -pub const SYS_chown32 = 212; -pub const SYS_setuid32 = 213; -pub const SYS_setgid32 = 214; -pub const SYS_setfsuid32 = 215; -pub const SYS_setfsgid32 = 216; -pub const SYS_pivot_root = 217; -pub const SYS_mincore = 218; -pub const SYS_madvise = 219; -pub const SYS_madvise1 = 219; -pub const SYS_getdents64 = 220; -pub const SYS_fcntl64 = 221; -pub const SYS_gettid = 224; -pub const SYS_readahead = 225; -pub const SYS_setxattr = 226; -pub const SYS_lsetxattr = 227; -pub const SYS_fsetxattr = 228; -pub const SYS_getxattr = 229; -pub const SYS_lgetxattr = 230; -pub const SYS_fgetxattr = 231; -pub const SYS_listxattr = 232; -pub const SYS_llistxattr = 233; -pub const SYS_flistxattr = 234; -pub const SYS_removexattr = 235; -pub const SYS_lremovexattr = 236; -pub const SYS_fremovexattr = 237; -pub const SYS_tkill = 238; -pub const SYS_sendfile64 = 239; -pub const SYS_futex = 240; -pub const SYS_sched_setaffinity = 241; -pub const SYS_sched_getaffinity = 242; -pub const SYS_set_thread_area = 243; -pub const SYS_get_thread_area = 244; -pub const SYS_io_setup = 245; -pub const SYS_io_destroy = 246; -pub const SYS_io_getevents = 247; -pub const SYS_io_submit = 248; -pub const SYS_io_cancel = 249; -pub const SYS_fadvise64 = 250; -pub const SYS_exit_group = 252; -pub const SYS_lookup_dcookie = 253; -pub const SYS_epoll_create = 254; -pub const SYS_epoll_ctl = 255; -pub const SYS_epoll_wait = 256; -pub const SYS_remap_file_pages = 257; -pub const SYS_set_tid_address = 258; -pub const SYS_timer_create = 259; -pub const SYS_timer_settime = SYS_timer_create+1; -pub const SYS_timer_gettime = SYS_timer_create+2; -pub const SYS_timer_getoverrun = SYS_timer_create+3; -pub const SYS_timer_delete = SYS_timer_create+4; -pub const SYS_clock_settime = SYS_timer_create+5; -pub const SYS_clock_gettime = SYS_timer_create+6; -pub const SYS_clock_getres = SYS_timer_create+7; -pub const SYS_clock_nanosleep = SYS_timer_create+8; -pub const SYS_statfs64 = 268; -pub const SYS_fstatfs64 = 269; -pub const SYS_tgkill = 270; -pub const SYS_utimes = 271; -pub const SYS_fadvise64_64 = 272; -pub const SYS_vserver = 273; -pub const SYS_mbind = 274; -pub const SYS_get_mempolicy = 275; -pub const SYS_set_mempolicy = 276; -pub const SYS_mq_open = 277; -pub const SYS_mq_unlink = SYS_mq_open+1; -pub const SYS_mq_timedsend = SYS_mq_open+2; -pub const SYS_mq_timedreceive = SYS_mq_open+3; -pub const SYS_mq_notify = SYS_mq_open+4; -pub const SYS_mq_getsetattr = SYS_mq_open+5; -pub const SYS_kexec_load = 283; -pub const SYS_waitid = 284; -pub const SYS_add_key = 286; -pub const SYS_request_key = 287; -pub const SYS_keyctl = 288; -pub const SYS_ioprio_set = 289; -pub const SYS_ioprio_get = 290; -pub const SYS_inotify_init = 291; -pub const SYS_inotify_add_watch = 292; -pub const SYS_inotify_rm_watch = 293; -pub const SYS_migrate_pages = 294; -pub const SYS_openat = 295; -pub const SYS_mkdirat = 296; -pub const SYS_mknodat = 297; -pub const SYS_fchownat = 298; -pub const SYS_futimesat = 299; -pub const SYS_fstatat64 = 300; -pub const SYS_unlinkat = 301; -pub const SYS_renameat = 302; -pub const SYS_linkat = 303; -pub const SYS_symlinkat = 304; -pub const SYS_readlinkat = 305; -pub const SYS_fchmodat = 306; -pub const SYS_faccessat = 307; -pub const SYS_pselect6 = 308; -pub const SYS_ppoll = 309; -pub const SYS_unshare = 310; -pub const SYS_set_robust_list = 311; -pub const SYS_get_robust_list = 312; -pub const SYS_splice = 313; -pub const SYS_sync_file_range = 314; -pub const SYS_tee = 315; -pub const SYS_vmsplice = 316; -pub const SYS_move_pages = 317; -pub const SYS_getcpu = 318; -pub const SYS_epoll_pwait = 319; -pub const SYS_utimensat = 320; -pub const SYS_signalfd = 321; -pub const SYS_timerfd_create = 322; -pub const SYS_eventfd = 323; -pub const SYS_fallocate = 324; -pub const SYS_timerfd_settime = 325; -pub const SYS_timerfd_gettime = 326; -pub const SYS_signalfd4 = 327; -pub const SYS_eventfd2 = 328; -pub const SYS_epoll_create1 = 329; -pub const SYS_dup3 = 330; -pub const SYS_pipe2 = 331; -pub const SYS_inotify_init1 = 332; -pub const SYS_preadv = 333; -pub const SYS_pwritev = 334; -pub const SYS_rt_tgsigqueueinfo = 335; -pub const SYS_perf_event_open = 336; -pub const SYS_recvmmsg = 337; -pub const SYS_fanotify_init = 338; -pub const SYS_fanotify_mark = 339; -pub const SYS_prlimit64 = 340; -pub const SYS_name_to_handle_at = 341; -pub const SYS_open_by_handle_at = 342; -pub const SYS_clock_adjtime = 343; -pub const SYS_syncfs = 344; -pub const SYS_sendmmsg = 345; -pub const SYS_setns = 346; -pub const SYS_process_vm_readv = 347; -pub const SYS_process_vm_writev = 348; -pub const SYS_kcmp = 349; -pub const SYS_finit_module = 350; -pub const SYS_sched_setattr = 351; -pub const SYS_sched_getattr = 352; -pub const SYS_renameat2 = 353; -pub const SYS_seccomp = 354; -pub const SYS_getrandom = 355; -pub const SYS_memfd_create = 356; -pub const SYS_bpf = 357; -pub const SYS_execveat = 358; -pub const SYS_socket = 359; -pub const SYS_socketpair = 360; -pub const SYS_bind = 361; -pub const SYS_connect = 362; -pub const SYS_listen = 363; -pub const SYS_accept4 = 364; -pub const SYS_getsockopt = 365; -pub const SYS_setsockopt = 366; -pub const SYS_getsockname = 367; -pub const SYS_getpeername = 368; -pub const SYS_sendto = 369; -pub const SYS_sendmsg = 370; -pub const SYS_recvfrom = 371; -pub const SYS_recvmsg = 372; -pub const SYS_shutdown = 373; -pub const SYS_userfaultfd = 374; -pub const SYS_membarrier = 375; -pub const SYS_mlock2 = 376; - - -pub const O_CREAT = 0o100; -pub const O_EXCL = 0o200; -pub const O_NOCTTY = 0o400; -pub const O_TRUNC = 0o1000; -pub const O_APPEND = 0o2000; -pub const O_NONBLOCK = 0o4000; -pub const O_DSYNC = 0o10000; -pub const O_SYNC = 0o4010000; -pub const O_RSYNC = 0o4010000; -pub const O_DIRECTORY = 0o200000; -pub const O_NOFOLLOW = 0o400000; -pub const O_CLOEXEC = 0o2000000; - -pub const O_ASYNC = 0o20000; -pub const O_DIRECT = 0o40000; -pub const O_LARGEFILE = 0o100000; -pub const O_NOATIME = 0o1000000; -pub const O_PATH = 0o10000000; -pub const O_TMPFILE = 0o20200000; -pub const O_NDELAY = O_NONBLOCK; - -pub const F_DUPFD = 0; -pub const F_GETFD = 1; -pub const F_SETFD = 2; -pub const F_GETFL = 3; -pub const F_SETFL = 4; - -pub const F_SETOWN = 8; -pub const F_GETOWN = 9; -pub const F_SETSIG = 10; -pub const F_GETSIG = 11; - -pub const F_GETLK = 12; -pub const F_SETLK = 13; -pub const F_SETLKW = 14; - -pub const F_SETOWN_EX = 15; -pub const F_GETOWN_EX = 16; - -pub const F_GETOWNER_UIDS = 17; - -pub inline fn syscall0(number: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number)) -} - -pub inline fn syscall1(number: usize, arg1: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1)) -} - -pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2)) -} - -pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3)) -} - -pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4)) -} - -pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, - arg4: usize, arg5: usize) -> usize -{ - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4), - [arg5] "{edi}" (arg5)) -} - -pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, - arg4: usize, arg5: usize, arg6: usize) -> usize -{ - asm volatile ("int $0x80" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4), - [arg5] "{edi}" (arg5), - [arg6] "{ebp}" (arg6)) -} - -pub nakedcc fn restore() void { - asm volatile ( - \\popl %%eax - \\movl $119, %%eax - \\int $0x80 - : - : - : "rcx", "r11") -} - -pub nakedcc fn restore_rt() void { - asm volatile ("int $0x80" - : - : [number] "{eax}" (usize(SYS_rt_sigreturn)) - : "rcx", "r11") -} diff --git a/std/os/linux_test.zig b/std/os/linux_test.zig @@ -1,38 +0,0 @@ -const std = @import("std"); -const linux = std.os.linux; -const assert = std.debug.assert; - -test "timer" { - const epoll_fd = linux.epoll_create(); - var err = linux.getErrno(epoll_fd); - assert(err == 0); - - const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0); - assert(linux.getErrno(timer_fd) == 0); - - const time_interval = linux.timespec { - .tv_sec = 0, - .tv_nsec = 2000000 - }; - - const new_time = linux.itimerspec { - .it_interval = time_interval, - .it_value = time_interval - }; - - err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null); - assert(err == 0); - - var event = linux.epoll_event { - .events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET, - .data = 0 - }; - - err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event); - assert(err == 0); - - const events_one: linux.epoll_event = undefined; - var events = []linux.epoll_event{events_one} ** 8; - - err = linux.epoll_wait(i32(epoll_fd), &events[0], 8, -1); -} diff --git a/std/os/linux_x86_64.zig b/std/os/linux_x86_64.zig @@ -1,489 +0,0 @@ -const linux = @import("linux.zig"); -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; - -pub const SYS_read = 0; -pub const SYS_write = 1; -pub const SYS_open = 2; -pub const SYS_close = 3; -pub const SYS_stat = 4; -pub const SYS_fstat = 5; -pub const SYS_lstat = 6; -pub const SYS_poll = 7; -pub const SYS_lseek = 8; -pub const SYS_mmap = 9; -pub const SYS_mprotect = 10; -pub const SYS_munmap = 11; -pub const SYS_brk = 12; -pub const SYS_rt_sigaction = 13; -pub const SYS_rt_sigprocmask = 14; -pub const SYS_rt_sigreturn = 15; -pub const SYS_ioctl = 16; -pub const SYS_pread = 17; -pub const SYS_pwrite = 18; -pub const SYS_readv = 19; -pub const SYS_writev = 20; -pub const SYS_access = 21; -pub const SYS_pipe = 22; -pub const SYS_select = 23; -pub const SYS_sched_yield = 24; -pub const SYS_mremap = 25; -pub const SYS_msync = 26; -pub const SYS_mincore = 27; -pub const SYS_madvise = 28; -pub const SYS_shmget = 29; -pub const SYS_shmat = 30; -pub const SYS_shmctl = 31; -pub const SYS_dup = 32; -pub const SYS_dup2 = 33; -pub const SYS_pause = 34; -pub const SYS_nanosleep = 35; -pub const SYS_getitimer = 36; -pub const SYS_alarm = 37; -pub const SYS_setitimer = 38; -pub const SYS_getpid = 39; -pub const SYS_sendfile = 40; -pub const SYS_socket = 41; -pub const SYS_connect = 42; -pub const SYS_accept = 43; -pub const SYS_sendto = 44; -pub const SYS_recvfrom = 45; -pub const SYS_sendmsg = 46; -pub const SYS_recvmsg = 47; -pub const SYS_shutdown = 48; -pub const SYS_bind = 49; -pub const SYS_listen = 50; -pub const SYS_getsockname = 51; -pub const SYS_getpeername = 52; -pub const SYS_socketpair = 53; -pub const SYS_setsockopt = 54; -pub const SYS_getsockopt = 55; -pub const SYS_clone = 56; -pub const SYS_fork = 57; -pub const SYS_vfork = 58; -pub const SYS_execve = 59; -pub const SYS_exit = 60; -pub const SYS_wait4 = 61; -pub const SYS_kill = 62; -pub const SYS_uname = 63; -pub const SYS_semget = 64; -pub const SYS_semop = 65; -pub const SYS_semctl = 66; -pub const SYS_shmdt = 67; -pub const SYS_msgget = 68; -pub const SYS_msgsnd = 69; -pub const SYS_msgrcv = 70; -pub const SYS_msgctl = 71; -pub const SYS_fcntl = 72; -pub const SYS_flock = 73; -pub const SYS_fsync = 74; -pub const SYS_fdatasync = 75; -pub const SYS_truncate = 76; -pub const SYS_ftruncate = 77; -pub const SYS_getdents = 78; -pub const SYS_getcwd = 79; -pub const SYS_chdir = 80; -pub const SYS_fchdir = 81; -pub const SYS_rename = 82; -pub const SYS_mkdir = 83; -pub const SYS_rmdir = 84; -pub const SYS_creat = 85; -pub const SYS_link = 86; -pub const SYS_unlink = 87; -pub const SYS_symlink = 88; -pub const SYS_readlink = 89; -pub const SYS_chmod = 90; -pub const SYS_fchmod = 91; -pub const SYS_chown = 92; -pub const SYS_fchown = 93; -pub const SYS_lchown = 94; -pub const SYS_umask = 95; -pub const SYS_gettimeofday = 96; -pub const SYS_getrlimit = 97; -pub const SYS_getrusage = 98; -pub const SYS_sysinfo = 99; -pub const SYS_times = 100; -pub const SYS_ptrace = 101; -pub const SYS_getuid = 102; -pub const SYS_syslog = 103; -pub const SYS_getgid = 104; -pub const SYS_setuid = 105; -pub const SYS_setgid = 106; -pub const SYS_geteuid = 107; -pub const SYS_getegid = 108; -pub const SYS_setpgid = 109; -pub const SYS_getppid = 110; -pub const SYS_getpgrp = 111; -pub const SYS_setsid = 112; -pub const SYS_setreuid = 113; -pub const SYS_setregid = 114; -pub const SYS_getgroups = 115; -pub const SYS_setgroups = 116; -pub const SYS_setresuid = 117; -pub const SYS_getresuid = 118; -pub const SYS_setresgid = 119; -pub const SYS_getresgid = 120; -pub const SYS_getpgid = 121; -pub const SYS_setfsuid = 122; -pub const SYS_setfsgid = 123; -pub const SYS_getsid = 124; -pub const SYS_capget = 125; -pub const SYS_capset = 126; -pub const SYS_rt_sigpending = 127; -pub const SYS_rt_sigtimedwait = 128; -pub const SYS_rt_sigqueueinfo = 129; -pub const SYS_rt_sigsuspend = 130; -pub const SYS_sigaltstack = 131; -pub const SYS_utime = 132; -pub const SYS_mknod = 133; -pub const SYS_uselib = 134; -pub const SYS_personality = 135; -pub const SYS_ustat = 136; -pub const SYS_statfs = 137; -pub const SYS_fstatfs = 138; -pub const SYS_sysfs = 139; -pub const SYS_getpriority = 140; -pub const SYS_setpriority = 141; -pub const SYS_sched_setparam = 142; -pub const SYS_sched_getparam = 143; -pub const SYS_sched_setscheduler = 144; -pub const SYS_sched_getscheduler = 145; -pub const SYS_sched_get_priority_max = 146; -pub const SYS_sched_get_priority_min = 147; -pub const SYS_sched_rr_get_interval = 148; -pub const SYS_mlock = 149; -pub const SYS_munlock = 150; -pub const SYS_mlockall = 151; -pub const SYS_munlockall = 152; -pub const SYS_vhangup = 153; -pub const SYS_modify_ldt = 154; -pub const SYS_pivot_root = 155; -pub const SYS__sysctl = 156; -pub const SYS_prctl = 157; -pub const SYS_arch_prctl = 158; -pub const SYS_adjtimex = 159; -pub const SYS_setrlimit = 160; -pub const SYS_chroot = 161; -pub const SYS_sync = 162; -pub const SYS_acct = 163; -pub const SYS_settimeofday = 164; -pub const SYS_mount = 165; -pub const SYS_umount2 = 166; -pub const SYS_swapon = 167; -pub const SYS_swapoff = 168; -pub const SYS_reboot = 169; -pub const SYS_sethostname = 170; -pub const SYS_setdomainname = 171; -pub const SYS_iopl = 172; -pub const SYS_ioperm = 173; -pub const SYS_create_module = 174; -pub const SYS_init_module = 175; -pub const SYS_delete_module = 176; -pub const SYS_get_kernel_syms = 177; -pub const SYS_query_module = 178; -pub const SYS_quotactl = 179; -pub const SYS_nfsservctl = 180; -pub const SYS_getpmsg = 181; -pub const SYS_putpmsg = 182; -pub const SYS_afs_syscall = 183; -pub const SYS_tuxcall = 184; -pub const SYS_security = 185; -pub const SYS_gettid = 186; -pub const SYS_readahead = 187; -pub const SYS_setxattr = 188; -pub const SYS_lsetxattr = 189; -pub const SYS_fsetxattr = 190; -pub const SYS_getxattr = 191; -pub const SYS_lgetxattr = 192; -pub const SYS_fgetxattr = 193; -pub const SYS_listxattr = 194; -pub const SYS_llistxattr = 195; -pub const SYS_flistxattr = 196; -pub const SYS_removexattr = 197; -pub const SYS_lremovexattr = 198; -pub const SYS_fremovexattr = 199; -pub const SYS_tkill = 200; -pub const SYS_time = 201; -pub const SYS_futex = 202; -pub const SYS_sched_setaffinity = 203; -pub const SYS_sched_getaffinity = 204; -pub const SYS_set_thread_area = 205; -pub const SYS_io_setup = 206; -pub const SYS_io_destroy = 207; -pub const SYS_io_getevents = 208; -pub const SYS_io_submit = 209; -pub const SYS_io_cancel = 210; -pub const SYS_get_thread_area = 211; -pub const SYS_lookup_dcookie = 212; -pub const SYS_epoll_create = 213; -pub const SYS_epoll_ctl_old = 214; -pub const SYS_epoll_wait_old = 215; -pub const SYS_remap_file_pages = 216; -pub const SYS_getdents64 = 217; -pub const SYS_set_tid_address = 218; -pub const SYS_restart_syscall = 219; -pub const SYS_semtimedop = 220; -pub const SYS_fadvise64 = 221; -pub const SYS_timer_create = 222; -pub const SYS_timer_settime = 223; -pub const SYS_timer_gettime = 224; -pub const SYS_timer_getoverrun = 225; -pub const SYS_timer_delete = 226; -pub const SYS_clock_settime = 227; -pub const SYS_clock_gettime = 228; -pub const SYS_clock_getres = 229; -pub const SYS_clock_nanosleep = 230; -pub const SYS_exit_group = 231; -pub const SYS_epoll_wait = 232; -pub const SYS_epoll_ctl = 233; -pub const SYS_tgkill = 234; -pub const SYS_utimes = 235; -pub const SYS_vserver = 236; -pub const SYS_mbind = 237; -pub const SYS_set_mempolicy = 238; -pub const SYS_get_mempolicy = 239; -pub const SYS_mq_open = 240; -pub const SYS_mq_unlink = 241; -pub const SYS_mq_timedsend = 242; -pub const SYS_mq_timedreceive = 243; -pub const SYS_mq_notify = 244; -pub const SYS_mq_getsetattr = 245; -pub const SYS_kexec_load = 246; -pub const SYS_waitid = 247; -pub const SYS_add_key = 248; -pub const SYS_request_key = 249; -pub const SYS_keyctl = 250; -pub const SYS_ioprio_set = 251; -pub const SYS_ioprio_get = 252; -pub const SYS_inotify_init = 253; -pub const SYS_inotify_add_watch = 254; -pub const SYS_inotify_rm_watch = 255; -pub const SYS_migrate_pages = 256; -pub const SYS_openat = 257; -pub const SYS_mkdirat = 258; -pub const SYS_mknodat = 259; -pub const SYS_fchownat = 260; -pub const SYS_futimesat = 261; -pub const SYS_newfstatat = 262; -pub const SYS_unlinkat = 263; -pub const SYS_renameat = 264; -pub const SYS_linkat = 265; -pub const SYS_symlinkat = 266; -pub const SYS_readlinkat = 267; -pub const SYS_fchmodat = 268; -pub const SYS_faccessat = 269; -pub const SYS_pselect6 = 270; -pub const SYS_ppoll = 271; -pub const SYS_unshare = 272; -pub const SYS_set_robust_list = 273; -pub const SYS_get_robust_list = 274; -pub const SYS_splice = 275; -pub const SYS_tee = 276; -pub const SYS_sync_file_range = 277; -pub const SYS_vmsplice = 278; -pub const SYS_move_pages = 279; -pub const SYS_utimensat = 280; -pub const SYS_epoll_pwait = 281; -pub const SYS_signalfd = 282; -pub const SYS_timerfd_create = 283; -pub const SYS_eventfd = 284; -pub const SYS_fallocate = 285; -pub const SYS_timerfd_settime = 286; -pub const SYS_timerfd_gettime = 287; -pub const SYS_accept4 = 288; -pub const SYS_signalfd4 = 289; -pub const SYS_eventfd2 = 290; -pub const SYS_epoll_create1 = 291; -pub const SYS_dup3 = 292; -pub const SYS_pipe2 = 293; -pub const SYS_inotify_init1 = 294; -pub const SYS_preadv = 295; -pub const SYS_pwritev = 296; -pub const SYS_rt_tgsigqueueinfo = 297; -pub const SYS_perf_event_open = 298; -pub const SYS_recvmmsg = 299; -pub const SYS_fanotify_init = 300; -pub const SYS_fanotify_mark = 301; -pub const SYS_prlimit64 = 302; -pub const SYS_name_to_handle_at = 303; -pub const SYS_open_by_handle_at = 304; -pub const SYS_clock_adjtime = 305; -pub const SYS_syncfs = 306; -pub const SYS_sendmmsg = 307; -pub const SYS_setns = 308; -pub const SYS_getcpu = 309; -pub const SYS_process_vm_readv = 310; -pub const SYS_process_vm_writev = 311; -pub const SYS_kcmp = 312; -pub const SYS_finit_module = 313; -pub const SYS_sched_setattr = 314; -pub const SYS_sched_getattr = 315; -pub const SYS_renameat2 = 316; -pub const SYS_seccomp = 317; -pub const SYS_getrandom = 318; -pub const SYS_memfd_create = 319; -pub const SYS_kexec_file_load = 320; -pub const SYS_bpf = 321; -pub const SYS_execveat = 322; -pub const SYS_userfaultfd = 323; -pub const SYS_membarrier = 324; -pub const SYS_mlock2 = 325; - -pub const O_CREAT = 0o100; -pub const O_EXCL = 0o200; -pub const O_NOCTTY = 0o400; -pub const O_TRUNC = 0o1000; -pub const O_APPEND = 0o2000; -pub const O_NONBLOCK = 0o4000; -pub const O_DSYNC = 0o10000; -pub const O_SYNC = 0o4010000; -pub const O_RSYNC = 0o4010000; -pub const O_DIRECTORY = 0o200000; -pub const O_NOFOLLOW = 0o400000; -pub const O_CLOEXEC = 0o2000000; - -pub const O_ASYNC = 0o20000; -pub const O_DIRECT = 0o40000; -pub const O_LARGEFILE = 0; -pub const O_NOATIME = 0o1000000; -pub const O_PATH = 0o10000000; -pub const O_TMPFILE = 0o20200000; -pub const O_NDELAY = O_NONBLOCK; - -pub const F_DUPFD = 0; -pub const F_GETFD = 1; -pub const F_SETFD = 2; -pub const F_GETFL = 3; -pub const F_SETFL = 4; - -pub const F_SETOWN = 8; -pub const F_GETOWN = 9; -pub const F_SETSIG = 10; -pub const F_GETSIG = 11; - -pub const F_GETLK = 5; -pub const F_SETLK = 6; -pub const F_SETLKW = 7; - -pub const F_SETOWN_EX = 15; -pub const F_GETOWN_EX = 16; - -pub const F_GETOWNER_UIDS = 17; - -pub fn syscall0(number: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number) - : "rcx", "r11"); -} - -pub fn syscall1(number: usize, arg1: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1) - : "rcx", "r11"); -} - -pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2) - : "rcx", "r11"); -} - -pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3) - : "rcx", "r11"); -} - -pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4) - : "rcx", "r11"); -} - -pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize { - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4), - [arg5] "{r8}" (arg5) - : "rcx", "r11"); -} - -pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, - arg5: usize, arg6: usize) usize -{ - return asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4), - [arg5] "{r8}" (arg5), - [arg6] "{r9}" (arg6) - : "rcx", "r11"); -} - -pub nakedcc fn restore_rt() void { - return asm volatile ("syscall" - : - : [number] "{rax}" (usize(SYS_rt_sigreturn)) - : "rcx", "r11"); -} - - -pub const msghdr = extern struct { - msg_name: &u8, - msg_namelen: socklen_t, - msg_iov: &iovec, - msg_iovlen: i32, - __pad1: i32, - msg_control: &u8, - msg_controllen: socklen_t, - __pad2: socklen_t, - msg_flags: i32, -}; - -/// Renamed to Stat to not conflict with the stat function. -pub const Stat = extern struct { - dev: u64, - ino: u64, - nlink: usize, - - mode: u32, - uid: u32, - gid: u32, - __pad0: u32, - rdev: u64, - size: i64, - blksize: isize, - blocks: i64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - __unused: [3]isize, -}; - -pub const timespec = extern struct { - tv_sec: isize, - tv_nsec: isize, -}; diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -1,6 +1,65 @@ const tests = @import("tests.zig"); pub fn addCases(cases: &tests.CompileErrorContext) void { + cases.add("cast negative integer literal to usize", + \\export fn entry() void { + \\ const x = usize(-10); + \\} + , ".tmp_source.zig:2:21: error: cannot cast negative value -10 to unsigned integer type 'usize'"); + + cases.add("use invalid number literal as array index", + \\var v = 25; + \\export fn entry() void { + \\ var arr: [v]u8 = undefined; + \\} + , ".tmp_source.zig:1:1: error: unable to infer variable type"); + + cases.add("duplicate struct field", + \\const Foo = struct { + \\ Bar: i32, + \\ Bar: usize, + \\}; + \\export fn entry() void { + \\ const a: Foo = undefined; + \\} + , + ".tmp_source.zig:3:5: error: duplicate struct field: 'Bar'", + ".tmp_source.zig:2:5: note: other field here"); + + cases.add("duplicate union field", + \\const Foo = union { + \\ Bar: i32, + \\ Bar: usize, + \\}; + \\export fn entry() void { + \\ const a: Foo = undefined; + \\} + , + ".tmp_source.zig:3:5: error: duplicate union field: 'Bar'", + ".tmp_source.zig:2:5: note: other field here"); + + cases.add("duplicate enum field", + \\const Foo = enum { + \\ Bar, + \\ Bar, + \\}; + \\ + \\export fn entry() void { + \\ const a: Foo = undefined; + \\} + , + ".tmp_source.zig:3:5: error: duplicate enum field: 'Bar'", + ".tmp_source.zig:2:5: note: other field here"); + + cases.add("calling function with naked calling convention", + \\export fn entry() void { + \\ foo(); + \\} + \\nakedcc fn foo() void { } + , + ".tmp_source.zig:2:5: error: unable to call function with naked calling convention", + ".tmp_source.zig:4:9: note: declared here"); + cases.add("function with invalid return type", \\export fn foo() boid {} , ".tmp_source.zig:1:17: error: use of undeclared identifier 'boid'"); @@ -326,10 +385,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) void { \\export fn entry() void { _ = a(); } , ".tmp_source.zig:1:8: error: use of undeclared identifier 'bogus'"); - cases.add("pointer to unreachable", + cases.add("pointer to noreturn", \\fn a() &noreturn {} \\export fn entry() void { _ = a(); } - , ".tmp_source.zig:1:9: error: pointer to unreachable not allowed"); + , ".tmp_source.zig:1:9: error: pointer to noreturn not allowed"); cases.add("unreachable code", \\export fn a() void {