authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-07 17:27:30-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-07 17:27:30-05:00
logaa043a633904b6685a201000a0ab2a62fc3bdb91
tree24520ed49575e88a6f6ed9592c1166efd1c074dd
parent96c9a9bdb3f7b10aa1ce4833bf6adb0af1c82dc9
parent79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5

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


22 files changed, 2339 insertions(+), 2061 deletions(-)

CMakeLists.txt+9-9
......@@ -201,29 +201,29 @@ else()
201201 COMPILE_FLAGS ${ZIG_LLD_COMPILE_FLAGS}
202202 LINK_FLAGS " "
203203 )
204 target_include_directories(embedded_lld_lib PUBLIC
204 target_include_directories(embedded_lld_lib PRIVATE
205205 "${CMAKE_SOURCE_DIR}/deps/lld/include"
206206 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt"
207207 )
208 target_include_directories(embedded_lld_elf PUBLIC
208 target_include_directories(embedded_lld_elf PRIVATE
209209 "${CMAKE_SOURCE_DIR}/deps/lld/ELF"
210210 "${CMAKE_SOURCE_DIR}/deps/lld/include"
211211 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/ELF"
212212 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt"
213213 )
214 target_include_directories(embedded_lld_coff PUBLIC
214 target_include_directories(embedded_lld_coff PRIVATE
215215 "${CMAKE_SOURCE_DIR}/deps/lld/COFF"
216216 "${CMAKE_SOURCE_DIR}/deps/lld/include"
217217 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/COFF"
218218 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt"
219219 )
220 target_include_directories(embedded_lld_mingw PUBLIC
220 target_include_directories(embedded_lld_mingw PRIVATE
221221 "${CMAKE_SOURCE_DIR}/deps/lld/MinGW"
222222 "${CMAKE_SOURCE_DIR}/deps/lld/include"
223223 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/MinGW"
224224 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt"
225225 )
226 target_include_directories(embedded_lld_wasm PUBLIC
226 target_include_directories(embedded_lld_wasm PRIVATE
227227 "${CMAKE_SOURCE_DIR}/deps/lld/wasm"
228228 "${CMAKE_SOURCE_DIR}/deps/lld/include"
229229 "${CMAKE_SOURCE_DIR}/deps/lld-prebuilt/wasm"
......@@ -482,10 +482,10 @@ set(ZIG_STD_FILES
482482 "os/darwin_errno.zig"
483483 "os/get_user_id.zig"
484484 "os/index.zig"
485 "os/linux.zig"
486 "os/linux_errno.zig"
487 "os/linux_i386.zig"
488 "os/linux_x86_64.zig"
485 "os/linux/index.zig"
486 "os/linux/errno.zig"
487 "os/linux/i386.zig"
488 "os/linux/x86_64.zig"
489489 "os/path.zig"
490490 "os/windows/error.zig"
491491 "os/windows/index.zig"
doc/langref.html.in+123-21
......@@ -36,25 +36,47 @@
3636 code {
3737 font-size: 12pt;
3838 }
39 @media screen and (min-width: 28.75em) {
39 pre > code {
40 display: block;
41 overflow: auto;
42 }
43 .table-wrapper {
44 width: 100%;
45 overflow-y: auto;
46 }
47 /* Desktop */
48 @media screen and (min-width: 56.25em) {
4049 #nav {
4150 width: 20em;
4251 height: 100%;
43 overflow-y: scroll;
4452 position: fixed;
53 overflow-y: scroll;
4554 left: 0;
4655 top: 0;
56 padding-left: 1em;
4757 }
4858 #contents {
49 max-width: 50em;
59 max-width: 60em;
5060 padding-left: 22em;
61 padding: 1em;
62 padding-left: 24em;
63 }
64 }
65 /* Mobile */
66 @media screen and (max-width: 56.25em) {
67 body, code {
68 font-size: small;
69 }
70 #nav {
71 border-bottom: 1px solid grey;
5172 }
5273 }
5374 </style>
5475 </head>
5576 <body>
5677 <div id="nav">
57 {#nav#}
78 <h3>Index</h3>
79 {#nav#}
5880 </div>
5981 <div id="contents">
6082 {#header_open|Introduction#}
......@@ -112,16 +134,6 @@ pub fn main() void {
112134 </p>
113135 {#see_also|Values|@import|Errors|Root Source File#}
114136 {#header_close#}
115 {#header_open|Source Encoding#}
116 <p>Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.</p>
117 <p>Throughout all zig source code (including in comments), some codepoints are never allowed:</p>
118 <ul>
119 <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>
120 <li>Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).</li>
121 </ul>
122 <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>
123 <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>
124 {#header_close#}
125137 {#header_open|Values#}
126138 {#code_begin|exe|values#}
127139const std = @import("std");
......@@ -173,6 +185,7 @@ pub fn main() %void {
173185}
174186 {#code_end#}
175187 {#header_open|Primitive Types#}
188 <div class="table-wrapper">
176189 <table>
177190 <tr>
178191 <th>
......@@ -398,9 +411,11 @@ pub fn main() %void {
398411 <td>an error code</td>
399412 </tr>
400413 </table>
414 </div>
401415 {#see_also|Integers|Floats|void|Errors#}
402416 {#header_close#}
403417 {#header_open|Primitive Values#}
418 <div class="table-wrapper">
404419 <table>
405420 <tr>
406421 <th>
......@@ -427,6 +442,7 @@ pub fn main() %void {
427442 <td>refers to the thing in immediate scope</td>
428443 </tr>
429444 </table>
445 </div>
430446 {#see_also|Nullables|this#}
431447 {#header_close#}
432448 {#header_open|String Literals#}
......@@ -451,6 +467,7 @@ test "string literals" {
451467 {#code_end#}
452468 {#see_also|Arrays|Zig Test#}
453469 {#header_open|Escape Sequences#}
470 <div class="table-wrapper">
454471 <table>
455472 <tr>
456473 <th>
......@@ -497,6 +514,7 @@ test "string literals" {
497514 <td>hexadecimal 24-bit Unicode character code UTF-8 encoded (6 digits)</td>
498515 </tr>
499516 </table>
517 </div>
500518 <p>Note that the maximum valid Unicode point is <code>0x10ffff</code>.</p>
501519 {#header_close#}
502520 {#header_open|Multiline String Literals#}
......@@ -674,6 +692,7 @@ pub fn main() %void {
674692 {#header_close#}
675693 {#header_open|Operators#}
676694 {#header_open|Table of Operators#}
695 <div class="table-wrapper">
677696 <table>
678697 <tr>
679698 <th>
......@@ -1246,6 +1265,7 @@ const ptr = &amp;x;
12461265 </td>
12471266 </tr>
12481267 </table>
1268 </div>
12491269 {#header_close#}
12501270 {#header_open|Precedence#}
12511271 <pre><code>x() x[] x.y
......@@ -2755,6 +2775,16 @@ test "implicitly cast to const pointer" {
27552775 use the C calling convention may pass structs and unions by value.
27562776 </p>
27572777 {#header_close#}
2778 {#header_open|Function Reflection#}
2779 {#code_begin|test#}
2780const assert = @import("std").debug.assert;
2781
2782test "fn reflection" {
2783 assert(@typeOf(assert).ReturnType == void);
2784 assert(@typeOf(assert).is_var_args == false);
2785}
2786 {#code_end#}
2787 {#header_close#}
27582788 {#header_close#}
27592789 {#header_open|Errors#}
27602790 <p>
......@@ -2968,6 +2998,31 @@ fn createFoo(param: i32) %Foo {
29682998 </li>
29692999 </ul>
29703000 {#see_also|defer|if|switch#}
3001 {#header_open|Error Union Type#}
3002 <p>An error union is created by putting a <code>%</code> in front of a type.
3003 You can use compile-time reflection to access the child type of an error union:</p>
3004 {#code_begin|test#}
3005const assert = @import("std").debug.assert;
3006
3007error SomeError;
3008
3009test "error union" {
3010 var foo: %i32 = undefined;
3011
3012 // Implicitly cast from child type of an error union:
3013 foo = 1234;
3014
3015 // Implicitly cast from an error set:
3016 foo = error.SomeError;
3017
3018 // Use compile-time reflection to access the child type of an error union:
3019 comptime assert(@typeOf(foo).Child == i32);
3020}
3021 {#code_end#}
3022 {#header_close#}
3023 {#header_open|Error Set Type#}
3024 <p>TODO</p>
3025 {#header_close#}
29713026 {#header_close#}
29723027 {#header_open|Nullables#}
29733028 <p>
......@@ -3069,6 +3124,24 @@ fn doAThing(nullable_foo: ?&Foo) void {
30693124 The optimizer can sometimes make better decisions knowing that pointer arguments
30703125 cannot be null.
30713126 </p>
3127 {#header_open|Nullable Type#}
3128 <p>A nullable is created by putting <code>?</code> in front of a type. You can use compile-time
3129 reflection to access the child type of a nullable:</p>
3130 {#code_begin|test#}
3131const assert = @import("std").debug.assert;
3132
3133test "nullable type" {
3134 // Declare a nullable and implicitly cast from null:
3135 var foo: ?i32 = null;
3136
3137 // Implicitly cast from child type of a nullable
3138 foo = 1234;
3139
3140 // Use compile-time reflection to access the child type of the nullable:
3141 comptime assert(@typeOf(foo).Child == i32);
3142}
3143 {#code_end#}
3144 {#header_close#}
30723145 {#header_close#}
30733146 {#header_open|Casting#}
30743147 <p>TODO: explain implicit vs explicit casting</p>
......@@ -3804,6 +3877,17 @@ comptime {
38043877 <code>@cInclude</code>, <code>@cDefine</code>, and <code>@cUndef</code> work
38053878 within this expression, appending to a temporary buffer which is then parsed as C code.
38063879 </p>
3880 <p>
3881 Usually you should only have one <code>@cImport</code> in your entire application, because it saves the compiler
3882 from invoking clang multiple times, and prevents inline functions from being duplicated.
3883 </p>
3884 <p>
3885 Reasons for having multiple <code>@cImport</code> expressions would be:
3886 </p>
3887 <ul>
3888 <li>To avoid a symbol collision, for example if foo.h and bar.h both <code>#define CONNECTION_COUNT</code></li>
3889 <li>To analyze the C code with different preprocessor defines</li>
3890 </ul>
38073891 {#see_also|Import from C Header File|@cInclude|@cDefine|@cUndef#}
38083892 {#header_close#}
38093893 {#header_open|@cInclude#}
......@@ -4133,17 +4217,28 @@ fn add(a: i32, b: i32) i32 { return a + b; }
41334217 {#header_open|@memberCount#}
41344218 <pre><code class="zig">@memberCount(comptime T: type) -&gt; (number literal)</code></pre>
41354219 <p>
4136 This function returns the number of enum values in an enum type.
4220 This function returns the number of members in a struct, enum, or union type.
41374221 </p>
41384222 <p>
41394223 The result is a compile time constant.
41404224 </p>
4225 <p>
4226 It does not include functions, variables, or constants.
4227 </p>
41414228 {#header_close#}
41424229 {#header_open|@memberName#}
4143 <p>TODO</p>
4230 <pre><code class="zig">@memberName(comptime T: type, comptime index: usize) -&gt; [N]u8</code></pre>
4231 <p>Returns the field name of a struct, union, or enum.</p>
4232 <p>
4233 The result is a compile time constant.
4234 </p>
4235 <p>
4236 It does not include functions, variables, or constants.
4237 </p>
41444238 {#header_close#}
41454239 {#header_open|@memberType#}
4146 <p>TODO</p>
4240 <pre><code class="zig">@memberType(comptime T: type, comptime index: usize) -&gt; type</code></pre>
4241 <p>Returns the field type of a struct or union.</p>
41474242 {#header_close#}
41484243 {#header_open|@memcpy#}
41494244 <pre><code class="zig">@memcpy(noalias dest: &u8, noalias source: &const u8, byte_count: usize)</code></pre>
......@@ -5533,6 +5628,16 @@ fn readU32Be() u32 {}
55335628 </p>
55345629 {#header_close#}
55355630 {#header_close#}
5631 {#header_open|Source Encoding#}
5632 <p>Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.</p>
5633 <p>Throughout all zig source code (including in comments), some codepoints are never allowed:</p>
5634 <ul>
5635 <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>
5636 <li>Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).</li>
5637 </ul>
5638 <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>
5639 <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>
5640 {#header_close#}
55365641 {#header_open|Grammar#}
55375642 <pre><code class="nohighlight">Root = many(TopLevelItem) EOF
55385643
......@@ -5701,9 +5806,6 @@ ContainerDecl = option("extern" | "packed")
57015806 <li>Together we serve end users.</li>
57025807 </ul>
57035808 {#header_close#}
5704 {#header_open|TODO#}
5705 <p>TODO: document changes from a31b23c46ba2a8c28df01adc1aa0b4d878b9a5cf (compile time reflection additions)</p>
5706 {#header_close#}
57075809 </div>
57085810 <script>
57095811/*! highlight.js v9.12.0 | BSD3 License | git.io/hljslicense */
......@@ -5765,7 +5867,7 @@ hljs.registerLanguage("zig", function(t) {
57655867 a = t.IR + "\\s*\\(",
57665868 c = {
57675869 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",
5768 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",
5870 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",
57695871 literal: "true false null undefined"
57705872 },
57715873 n = [e, t.CLCM, t.CBCM, s, r];
src/all_types.hpp+9
......@@ -331,12 +331,14 @@ struct TypeEnumField {
331331 Buf *name;
332332 BigInt value;
333333 uint32_t decl_index;
334 AstNode *decl_node;
334335};
335336
336337struct TypeUnionField {
337338 Buf *name;
338339 TypeEnumField *enum_field;
339340 TypeTableEntry *type_entry;
341 AstNode *decl_node;
340342 uint32_t gen_index;
341343};
342344
......@@ -961,6 +963,7 @@ struct TypeStructField {
961963 size_t packed_bits_offset;
962964 size_t packed_bits_size;
963965 size_t unaligned_bit_count;
966 AstNode *decl_node;
964967};
965968struct TypeTableEntryStruct {
966969 AstNode *decl_node;
......@@ -982,6 +985,8 @@ struct TypeTableEntryStruct {
982985 bool zero_bits_loop_flag;
983986 bool zero_bits_known;
984987 uint32_t abi_alignment; // also figured out with zero_bits pass
988
989 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
985990};
986991
987992struct TypeTableEntryMaybe {
......@@ -1013,6 +1018,8 @@ struct TypeTableEntryEnum {
10131018
10141019 bool generate_name_table;
10151020 LLVMValueRef name_table;
1021
1022 HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name;
10161023};
10171024
10181025uint32_t type_ptr_hash(const TypeTableEntry *ptr);
......@@ -1045,6 +1052,8 @@ struct TypeTableEntryUnion {
10451052
10461053 uint32_t union_size_bytes;
10471054 TypeTableEntry *most_aligned_union_member;
1055
1056 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
10481057};
10491058
10501059struct FnGenParamInfo {
src/analyze.cpp+75-28
......@@ -633,20 +633,27 @@ TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t
633633
634634static void slice_type_common_init(CodeGen *g, TypeTableEntry *pointer_type, TypeTableEntry *entry) {
635635 unsigned element_count = 2;
636 Buf *ptr_field_name = buf_create_from_str("ptr");
637 Buf *len_field_name = buf_create_from_str("len");
638
636639 entry->data.structure.layout = ContainerLayoutAuto;
637640 entry->data.structure.is_slice = true;
638641 entry->data.structure.src_field_count = element_count;
639642 entry->data.structure.gen_field_count = element_count;
640643 entry->data.structure.fields = allocate<TypeStructField>(element_count);
641 entry->data.structure.fields[slice_ptr_index].name = buf_create_from_str("ptr");
644 entry->data.structure.fields_by_name.init(element_count);
645 entry->data.structure.fields[slice_ptr_index].name = ptr_field_name;
642646 entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type;
643647 entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index;
644648 entry->data.structure.fields[slice_ptr_index].gen_index = 0;
645 entry->data.structure.fields[slice_len_index].name = buf_create_from_str("len");
649 entry->data.structure.fields[slice_len_index].name = len_field_name;
646650 entry->data.structure.fields[slice_len_index].type_entry = g->builtin_types.entry_usize;
647651 entry->data.structure.fields[slice_len_index].src_index = slice_len_index;
648652 entry->data.structure.fields[slice_len_index].gen_index = 1;
649653
654 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
655 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);
656
650657 assert(type_has_zero_bits_known(pointer_type->data.pointer.child_type));
651658 if (pointer_type->data.pointer.child_type->zero_bits) {
652659 entry->data.structure.gen_field_count = 1;
......@@ -1555,6 +1562,7 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f
15551562 struct_type->data.structure.zero_bits_known = true;
15561563 struct_type->data.structure.complete = true;
15571564 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
1565 struct_type->data.structure.fields_by_name.init(field_count);
15581566
15591567 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(field_count);
15601568 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(field_count);
......@@ -1566,6 +1574,9 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f
15661574 field->type_entry = field_types[i];
15671575 field->src_index = i;
15681576 field->gen_index = i;
1577
1578 auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field);
1579 assert(prev_entry == nullptr);
15691580 }
15701581
15711582 struct_type->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), type_name);
......@@ -2102,6 +2113,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
21022113
21032114 enum_type->data.enumeration.src_field_count = field_count;
21042115 enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
2116 enum_type->data.enumeration.fields_by_name.init(field_count);
21052117
21062118 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
21072119
......@@ -2139,6 +2151,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
21392151 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_i];
21402152 type_enum_field->name = field_node->data.struct_field.name;
21412153 type_enum_field->decl_index = field_i;
2154 type_enum_field->decl_node = field_node;
21422155
21432156 if (field_node->data.struct_field.type != nullptr) {
21442157 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) {
21472160 buf_sprintf("consider 'union(enum)' here"));
21482161 }
21492162
2163 auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field);
2164 if (field_entry != nullptr) {
2165 ErrorMsg *msg = add_node_error(g, field_node,
2166 buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name)));
2167 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2168 enum_type->data.enumeration.is_invalid = true;
2169 continue;
2170 }
2171
21502172 AstNode *tag_value = field_node->data.struct_field.value;
21512173
21522174 // In this first pass we resolve explicit tag values.
......@@ -2242,6 +2264,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
22422264 size_t field_count = decl_node->data.container_decl.fields.length;
22432265 struct_type->data.structure.src_field_count = (uint32_t)field_count;
22442266 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
2267 struct_type->data.structure.fields_by_name.init(field_count);
22452268
22462269 Scope *scope = &struct_type->data.structure.decls_scope->base;
22472270
......@@ -2250,6 +2273,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
22502273 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
22512274 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
22522275 type_struct_field->name = field_node->data.struct_field.name;
2276 type_struct_field->decl_node = field_node;
22532277
22542278 if (field_node->data.struct_field.type == nullptr) {
22552279 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) {
22572281 continue;
22582282 }
22592283
2284 auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field);
2285 if (field_entry != nullptr) {
2286 ErrorMsg *msg = add_node_error(g, field_node,
2287 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));
2288 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2289 struct_type->data.structure.is_invalid = true;
2290 continue;
2291 }
2292
22602293 TypeTableEntry *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
22612294 type_struct_field->type_entry = field_type;
22622295 type_struct_field->src_index = i;
......@@ -2344,6 +2377,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
23442377 }
23452378 union_type->data.unionation.src_field_count = field_count;
23462379 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);
2380 union_type->data.unionation.fields_by_name.init(field_count);
23472381
23482382 uint32_t biggest_align_bytes = 0;
23492383
......@@ -2395,6 +2429,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
23952429 tag_type->data.enumeration.layout = ContainerLayoutAuto;
23962430 tag_type->data.enumeration.src_field_count = field_count;
23972431 tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
2432 tag_type->data.enumeration.fields_by_name.init(field_count);
23982433 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;
23992434 tag_type->data.enumeration.complete = true;
24002435 } else if (enum_type_node != nullptr) {
......@@ -2424,6 +2459,16 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
24242459 Buf *field_name = field_node->data.struct_field.name;
24252460 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
24262461 union_field->name = field_node->data.struct_field.name;
2462 union_field->decl_node = field_node;
2463
2464 auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field);
2465 if (field_entry != nullptr) {
2466 ErrorMsg *msg = add_node_error(g, field_node,
2467 buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name)));
2468 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2469 union_type->data.unionation.is_invalid = true;
2470 continue;
2471 }
24272472
24282473 TypeTableEntry *field_type;
24292474 if (field_node->data.struct_field.type == nullptr) {
......@@ -2456,6 +2501,10 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
24562501 union_field->enum_field = &tag_type->data.enumeration.fields[i];
24572502 union_field->enum_field->name = field_name;
24582503 union_field->enum_field->decl_index = i;
2504 union_field->enum_field->decl_node = field_node;
2505
2506 auto prev_entry = tag_type->data.enumeration.fields_by_name.put_unique(union_field->enum_field->name, union_field->enum_field);
2507 assert(prev_entry == nullptr); // caught by union de-duplicator above
24592508
24602509 AstNode *tag_value = field_node->data.struct_field.value;
24612510 // In this first pass we resolve explicit tag values.
......@@ -3246,6 +3295,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32463295 is_const, init_val, &tld_var->base);
32473296 tld_var->var->linkage = linkage;
32483297
3298 if (implicit_type != nullptr && type_is_invalid(implicit_type)) {
3299 tld_var->var->value->type = g->builtin_types.entry_invalid;
3300 }
3301
32493302 if (var_decl->align_expr != nullptr) {
32503303 if (!analyze_const_align(g, tld_var->base.parent_scope, var_decl->align_expr, &tld_var->var->align_bytes)) {
32513304 tld_var->var->value->type = g->builtin_types.entry_invalid;
......@@ -3327,11 +3380,7 @@ bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *
33273380 }
33283381
33293382 // slice const
3330 if (expected_type->id == TypeTableEntryIdStruct &&
3331 actual_type->id == TypeTableEntryIdStruct &&
3332 expected_type->data.structure.is_slice &&
3333 actual_type->data.structure.is_slice)
3334 {
3383 if (is_slice(expected_type) && is_slice(actual_type)) {
33353384 TypeTableEntry *actual_ptr_type = actual_type->data.structure.fields[slice_ptr_index].type_entry;
33363385 TypeTableEntry *expected_ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry;
33373386 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) {
34993548}
35003549
35013550TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name) {
3502 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
3503 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
3504 if (buf_eql_buf(type_enum_field->name, name)) {
3505 return type_enum_field;
3506 }
3507 }
3508 return nullptr;
3551 assert(enum_type->id == TypeTableEntryIdEnum);
3552 if (enum_type->data.enumeration.src_field_count == 0)
3553 return nullptr;
3554 auto entry = enum_type->data.enumeration.fields_by_name.maybe_get(name);
3555 if (entry == nullptr)
3556 return nullptr;
3557 return entry->value;
35093558}
35103559
35113560TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name) {
35123561 assert(type_entry->id == TypeTableEntryIdStruct);
35133562 assert(type_entry->data.structure.complete);
3514 for (uint32_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
3515 TypeStructField *field = &type_entry->data.structure.fields[i];
3516 if (buf_eql_buf(field->name, name)) {
3517 return field;
3518 }
3519 }
3520 return nullptr;
3563 if (type_entry->data.structure.src_field_count == 0)
3564 return nullptr;
3565 auto entry = type_entry->data.structure.fields_by_name.maybe_get(name);
3566 if (entry == nullptr)
3567 return nullptr;
3568 return entry->value;
35213569}
35223570
35233571TypeUnionField *find_union_type_field(TypeTableEntry *type_entry, Buf *name) {
35243572 assert(type_entry->id == TypeTableEntryIdUnion);
35253573 assert(type_entry->data.unionation.zero_bits_known);
3526 for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {
3527 TypeUnionField *field = &type_entry->data.unionation.fields[i];
3528 if (buf_eql_buf(field->enum_field->name, name)) {
3529 return field;
3530 }
3531 }
3532 return nullptr;
3574 if (type_entry->data.unionation.src_field_count == 0)
3575 return nullptr;
3576 auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name);
3577 if (entry == nullptr)
3578 return nullptr;
3579 return entry->value;
35333580}
35343581
35353582TypeUnionField *find_union_field_by_tag(TypeTableEntry *type_entry, const BigInt *tag) {
src/analyze.hpp+1-1
......@@ -60,8 +60,8 @@ bool type_is_complete(TypeTableEntry *type_entry);
6060bool type_is_invalid(TypeTableEntry *type_entry);
6161bool type_has_zero_bits_known(TypeTableEntry *type_entry);
6262void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry);
63TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name);
6463ScopeDecls *get_container_scope(TypeTableEntry *type_entry);
64TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name);
6565TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name);
6666TypeUnionField *find_union_type_field(TypeTableEntry *type_entry, Buf *name);
6767TypeEnumField *find_enum_field_by_tag(TypeTableEntry *enum_type, const BigInt *tag);
src/ir.cpp+30-7
......@@ -6193,6 +6193,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
61936193 if (other_type->id == TypeTableEntryIdFloat) {
61946194 return true;
61956195 } else if (other_type->id == TypeTableEntryIdInt && const_val_is_int) {
6196 if (!other_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) {
6197 Buf *val_buf = buf_alloc();
6198 bigint_append_buf(val_buf, &const_val->data.x_bigint, 10);
6199 ir_add_error(ira, instruction,
6200 buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'",
6201 buf_ptr(val_buf),
6202 buf_ptr(&other_type->name)));
6203 return false;
6204 }
61966205 if (bigint_fits_in_bits(&const_val->data.x_bigint, other_type->data.integral.bit_count,
61976206 other_type->data.integral.is_signed))
61986207 {
......@@ -6205,6 +6214,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
62056214 if (const_val_fits_in_num_lit(const_val, child_type)) {
62066215 return true;
62076216 } else if (child_type->id == TypeTableEntryIdInt && const_val_is_int) {
6217 if (!child_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) {
6218 Buf *val_buf = buf_alloc();
6219 bigint_append_buf(val_buf, &const_val->data.x_bigint, 10);
6220 ir_add_error(ira, instruction,
6221 buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'",
6222 buf_ptr(val_buf),
6223 buf_ptr(&child_type->name)));
6224 return false;
6225 }
62086226 if (bigint_fits_in_bits(&const_val->data.x_bigint,
62096227 child_type->data.integral.bit_count,
62106228 child_type->data.integral.is_signed))
......@@ -6351,10 +6369,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
63516369 }
63526370
63536371 // implicit [N]T to []const T
6354 if (expected_type->id == TypeTableEntryIdStruct &&
6355 expected_type->data.structure.is_slice &&
6356 actual_type->id == TypeTableEntryIdArray)
6357 {
6372 if (is_slice(expected_type) && actual_type->id == TypeTableEntryIdArray) {
63586373 TypeTableEntry *ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry;
63596374 assert(ptr_type->id == TypeTableEntryIdPointer);
63606375
......@@ -6366,8 +6381,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
63666381 }
63676382
63686383 // implicit &const [N]T to []const T
6369 if (expected_type->id == TypeTableEntryIdStruct &&
6370 expected_type->data.structure.is_slice &&
6384 if (is_slice(expected_type) &&
63716385 actual_type->id == TypeTableEntryIdPointer &&
63726386 actual_type->data.pointer.is_const &&
63736387 actual_type->data.pointer.child_type->id == TypeTableEntryIdArray)
......@@ -7009,7 +7023,7 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio
70097023 if (pointee_type->id == TypeTableEntryIdMetaType) {
70107024 TypeTableEntry *type_entry = pointee->data.x_type;
70117025 if (type_entry->id == TypeTableEntryIdUnreachable) {
7012 ir_add_error(ira, instruction, buf_sprintf("pointer to unreachable not allowed"));
7026 ir_add_error(ira, instruction, buf_sprintf("pointer to noreturn not allowed"));
70137027 return ira->codegen->invalid_instruction;
70147028 }
70157029
......@@ -9837,6 +9851,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
98379851
98389852 AstNode *fn_proto_node = fn_entry ? fn_entry->proto_node : nullptr;;
98399853
9854 if (fn_type_id->cc == CallingConventionNaked) {
9855 ErrorMsg *msg = ir_add_error(ira, fn_ref, buf_sprintf("unable to call function with naked calling convention"));
9856 if (fn_proto_node) {
9857 add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("declared here"));
9858 }
9859 return ira->codegen->builtin_types.entry_invalid;
9860 }
9861
9862
98409863 if (fn_type_id->is_var_args) {
98419864 if (call_param_count < src_param_count) {
98429865 ErrorMsg *msg = ir_add_error_node(ira, source_node,
std/c/linux.zig+1-1
......@@ -1,4 +1,4 @@
1pub use @import("../os/linux_errno.zig");
1pub use @import("../os/linux/errno.zig");
22
33pub extern "c" fn getrandom(buf_ptr: &u8, buf_len: usize, flags: c_uint) c_int;
44extern "c" fn __errno_location() &c_int;
std/fmt/index.zig+15-1
......@@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons
228228 if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {
229229 return output(context, (*value)[0..]);
230230 } else {
231 @compileError("Unable to format type '" ++ @typeName(T) ++ "'");
231 return format(context, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
232232 }
233233 },
234234 else => if (@canImplicitCast([]const u8, value)) {
......@@ -546,6 +546,12 @@ test "parse unsigned comptime" {
546546 }
547547}
548548
549// Dummy field because of https://github.com/zig-lang/zig/issues/557.
550// At top level because of https://github.com/zig-lang/zig/issues/675.
551const Struct = struct {
552 unused: u8,
553};
554
549555test "fmt.format" {
550556 {
551557 var buf1: [32]u8 = undefined;
......@@ -577,6 +583,14 @@ test "fmt.format" {
577583 const result = try bufPrint(buf1[0..], "u3: {}\n", value);
578584 assert(mem.eql(u8, result, "u3: 5\n"));
579585 }
586 {
587 var buf1: [32]u8 = undefined;
588 const value = Struct {
589 .unused = 42,
590 };
591 const result = try bufPrint(buf1[0..], "pointer: {}\n", &value);
592 assert(mem.startsWith(u8, result, "pointer: Struct@"));
593 }
580594
581595 // TODO get these tests passing in release modes
582596 // https://github.com/zig-lang/zig/issues/564
std/index.zig+1
......@@ -6,6 +6,7 @@ pub const Buffer = @import("buffer.zig").Buffer;
66pub const BufferOutStream = @import("buffer.zig").BufferOutStream;
77pub const HashMap = @import("hash_map.zig").HashMap;
88pub const LinkedList = @import("linked_list.zig").LinkedList;
9pub const IntrusiveLinkedList = @import("linked_list.zig").IntrusiveLinkedList;
910
1011pub const base64 = @import("base64.zig");
1112pub const build = @import("build.zig");
std/io.zig+1-1
......@@ -2,7 +2,7 @@ const std = @import("index.zig");
22const builtin = @import("builtin");
33const Os = builtin.Os;
44const system = switch(builtin.os) {
5 Os.linux => @import("os/linux.zig"),
5 Os.linux => @import("os/linux/index.zig"),
66 Os.macosx, Os.ios => @import("os/darwin.zig"),
77 Os.windows => @import("os/windows/index.zig"),
88 else => @compileError("Unsupported OS"),
std/os/index.zig+38-17
......@@ -6,7 +6,7 @@ const os = this;
66
77pub const windows = @import("windows/index.zig");
88pub const darwin = @import("darwin.zig");
9pub const linux = @import("linux.zig");
9pub const linux = @import("linux/index.zig");
1010pub const zen = @import("zen.zig");
1111pub const posix = switch(builtin.os) {
1212 Os.linux => linux,
......@@ -82,18 +82,24 @@ pub fn getRandomBytes(buf: []u8) %void {
8282 // See #397
8383 const err = posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0));
8484 if (err > 0) {
85 return switch (err) {
85 switch (err) {
8686 posix.EINVAL => unreachable,
8787 posix.EFAULT => unreachable,
8888 posix.EINTR => continue,
89 else => unexpectedErrorPosix(err),
90 };
89 posix.ENOSYS => {
90 const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0);
91 defer close(fd);
92
93 try posixRead(fd, buf);
94 return;
95 },
96 else => return unexpectedErrorPosix(err),
97 }
9198 }
9299 return;
93100 },
94101 Os.macosx, Os.ios => {
95 const fd = try posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC,
96 0, null);
102 const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0);
97103 defer close(fd);
98104
99105 try posixRead(fd, buf);
......@@ -183,10 +189,15 @@ pub fn close(handle: FileHandle) void {
183189
184190/// Calls POSIX read, and keeps trying if it gets interrupted.
185191pub fn posixRead(fd: i32, buf: []u8) %void {
192 // Linux can return EINVAL when read amount is > 0x7ffff000
193 // See https://github.com/zig-lang/zig/pull/743#issuecomment-363158274
194 const max_buf_len = 0x7ffff000;
195
186196 var index: usize = 0;
187197 while (index < buf.len) {
188 const amt_written = posix.read(fd, &buf[index], buf.len - index);
189 const err = posix.getErrno(amt_written);
198 const want_to_read = math.min(buf.len - index, usize(max_buf_len));
199 const rc = posix.read(fd, &buf[index], want_to_read);
200 const err = posix.getErrno(rc);
190201 if (err > 0) {
191202 return switch (err) {
192203 posix.EINTR => continue,
......@@ -199,7 +210,7 @@ pub fn posixRead(fd: i32, buf: []u8) %void {
199210 else => unexpectedErrorPosix(err),
200211 };
201212 }
202 index += amt_written;
213 index += rc;
203214 }
204215}
205216
......@@ -214,9 +225,15 @@ error BrokenPipe;
214225
215226/// Calls POSIX write, and keeps trying if it gets interrupted.
216227pub fn posixWrite(fd: i32, bytes: []const u8) %void {
217 while (true) {
218 const write_ret = posix.write(fd, bytes.ptr, bytes.len);
219 const write_err = posix.getErrno(write_ret);
228 // Linux can return EINVAL when write amount is > 0x7ffff000
229 // See https://github.com/zig-lang/zig/pull/743#issuecomment-363165856
230 const max_bytes_len = 0x7ffff000;
231
232 var index: usize = 0;
233 while (index < bytes.len) {
234 const amt_to_write = math.min(bytes.len - index, usize(max_bytes_len));
235 const rc = posix.write(fd, &bytes[index], amt_to_write);
236 const write_err = posix.getErrno(rc);
220237 if (write_err > 0) {
221238 return switch (write_err) {
222239 posix.EINTR => continue,
......@@ -233,7 +250,7 @@ pub fn posixWrite(fd: i32, bytes: []const u8) %void {
233250 else => unexpectedErrorPosix(write_err),
234251 };
235252 }
236 return;
253 index += rc;
237254 }
238255}
239256
......@@ -262,8 +279,12 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al
262279 mem.copy(u8, path0, file_path);
263280 path0[file_path.len] = 0;
264281
282 return posixOpenC(path0.ptr, flags, perm);
283}
284
285pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) %i32 {
265286 while (true) {
266 const result = posix.open(path0.ptr, flags, perm);
287 const result = posix.open(file_path, flags, perm);
267288 const err = posix.getErrno(result);
268289 if (err > 0) {
269290 return switch (err) {
......@@ -1495,10 +1516,10 @@ test "std.os" {
14951516 _ = @import("darwin_errno.zig");
14961517 _ = @import("darwin.zig");
14971518 _ = @import("get_user_id.zig");
1498 _ = @import("linux_errno.zig");
1519 _ = @import("linux/errno.zig");
14991520 //_ = @import("linux_i386.zig");
1500 _ = @import("linux_x86_64.zig");
1501 _ = @import("linux.zig");
1521 _ = @import("linux/x86_64.zig");
1522 _ = @import("linux/index.zig");
15021523 _ = @import("path.zig");
15031524 _ = @import("windows/index.zig");
15041525}
std/os/linux.zig deleted-796
......@@ -1,796 +0,0 @@
1const std = @import("../index.zig");
2const assert = std.debug.assert;
3const builtin = @import("builtin");
4const arch = switch (builtin.arch) {
5 builtin.Arch.x86_64 => @import("linux_x86_64.zig"),
6 builtin.Arch.i386 => @import("linux_i386.zig"),
7 else => @compileError("unsupported arch"),
8};
9pub use @import("linux_errno.zig");
10
11pub const PATH_MAX = 4096;
12
13pub const STDIN_FILENO = 0;
14pub const STDOUT_FILENO = 1;
15pub const STDERR_FILENO = 2;
16
17pub const PROT_NONE = 0;
18pub const PROT_READ = 1;
19pub const PROT_WRITE = 2;
20pub const PROT_EXEC = 4;
21pub const PROT_GROWSDOWN = 0x01000000;
22pub const PROT_GROWSUP = 0x02000000;
23
24pub const MAP_FAILED = @maxValue(usize);
25pub const MAP_SHARED = 0x01;
26pub const MAP_PRIVATE = 0x02;
27pub const MAP_TYPE = 0x0f;
28pub const MAP_FIXED = 0x10;
29pub const MAP_ANONYMOUS = 0x20;
30pub const MAP_NORESERVE = 0x4000;
31pub const MAP_GROWSDOWN = 0x0100;
32pub const MAP_DENYWRITE = 0x0800;
33pub const MAP_EXECUTABLE = 0x1000;
34pub const MAP_LOCKED = 0x2000;
35pub const MAP_POPULATE = 0x8000;
36pub const MAP_NONBLOCK = 0x10000;
37pub const MAP_STACK = 0x20000;
38pub const MAP_HUGETLB = 0x40000;
39pub const MAP_FILE = 0;
40
41pub const WNOHANG = 1;
42pub const WUNTRACED = 2;
43pub const WSTOPPED = 2;
44pub const WEXITED = 4;
45pub const WCONTINUED = 8;
46pub const WNOWAIT = 0x1000000;
47
48pub const SA_NOCLDSTOP = 1;
49pub const SA_NOCLDWAIT = 2;
50pub const SA_SIGINFO = 4;
51pub const SA_ONSTACK = 0x08000000;
52pub const SA_RESTART = 0x10000000;
53pub const SA_NODEFER = 0x40000000;
54pub const SA_RESETHAND = 0x80000000;
55pub const SA_RESTORER = 0x04000000;
56
57pub const SIGHUP = 1;
58pub const SIGINT = 2;
59pub const SIGQUIT = 3;
60pub const SIGILL = 4;
61pub const SIGTRAP = 5;
62pub const SIGABRT = 6;
63pub const SIGIOT = SIGABRT;
64pub const SIGBUS = 7;
65pub const SIGFPE = 8;
66pub const SIGKILL = 9;
67pub const SIGUSR1 = 10;
68pub const SIGSEGV = 11;
69pub const SIGUSR2 = 12;
70pub const SIGPIPE = 13;
71pub const SIGALRM = 14;
72pub const SIGTERM = 15;
73pub const SIGSTKFLT = 16;
74pub const SIGCHLD = 17;
75pub const SIGCONT = 18;
76pub const SIGSTOP = 19;
77pub const SIGTSTP = 20;
78pub const SIGTTIN = 21;
79pub const SIGTTOU = 22;
80pub const SIGURG = 23;
81pub const SIGXCPU = 24;
82pub const SIGXFSZ = 25;
83pub const SIGVTALRM = 26;
84pub const SIGPROF = 27;
85pub const SIGWINCH = 28;
86pub const SIGIO = 29;
87pub const SIGPOLL = 29;
88pub const SIGPWR = 30;
89pub const SIGSYS = 31;
90pub const SIGUNUSED = SIGSYS;
91
92pub const O_RDONLY = 0o0;
93pub const O_WRONLY = 0o1;
94pub const O_RDWR = 0o2;
95
96pub const O_CREAT = arch.O_CREAT;
97pub const O_EXCL = arch.O_EXCL;
98pub const O_NOCTTY = arch.O_NOCTTY;
99pub const O_TRUNC = arch.O_TRUNC;
100pub const O_APPEND = arch.O_APPEND;
101pub const O_NONBLOCK = arch.O_NONBLOCK;
102pub const O_DSYNC = arch.O_DSYNC;
103pub const O_SYNC = arch.O_SYNC;
104pub const O_RSYNC = arch.O_RSYNC;
105pub const O_DIRECTORY = arch.O_DIRECTORY;
106pub const O_NOFOLLOW = arch.O_NOFOLLOW;
107pub const O_CLOEXEC = arch.O_CLOEXEC;
108
109pub const O_ASYNC = arch.O_ASYNC;
110pub const O_DIRECT = arch.O_DIRECT;
111pub const O_LARGEFILE = arch.O_LARGEFILE;
112pub const O_NOATIME = arch.O_NOATIME;
113pub const O_PATH = arch.O_PATH;
114pub const O_TMPFILE = arch.O_TMPFILE;
115pub const O_NDELAY = arch.O_NDELAY;
116
117pub const SEEK_SET = 0;
118pub const SEEK_CUR = 1;
119pub const SEEK_END = 2;
120
121pub const SIG_BLOCK = 0;
122pub const SIG_UNBLOCK = 1;
123pub const SIG_SETMASK = 2;
124
125pub const SOCK_STREAM = 1;
126pub const SOCK_DGRAM = 2;
127pub const SOCK_RAW = 3;
128pub const SOCK_RDM = 4;
129pub const SOCK_SEQPACKET = 5;
130pub const SOCK_DCCP = 6;
131pub const SOCK_PACKET = 10;
132pub const SOCK_CLOEXEC = 0o2000000;
133pub const SOCK_NONBLOCK = 0o4000;
134
135
136pub const PROTO_ip = 0o000;
137pub const PROTO_icmp = 0o001;
138pub const PROTO_igmp = 0o002;
139pub const PROTO_ggp = 0o003;
140pub const PROTO_ipencap = 0o004;
141pub const PROTO_st = 0o005;
142pub const PROTO_tcp = 0o006;
143pub const PROTO_egp = 0o010;
144pub const PROTO_pup = 0o014;
145pub const PROTO_udp = 0o021;
146pub const PROTO_hmp = 0o024;
147pub const PROTO_xns_idp = 0o026;
148pub const PROTO_rdp = 0o033;
149pub const PROTO_iso_tp4 = 0o035;
150pub const PROTO_xtp = 0o044;
151pub const PROTO_ddp = 0o045;
152pub const PROTO_idpr_cmtp = 0o046;
153pub const PROTO_ipv6 = 0o051;
154pub const PROTO_ipv6_route = 0o053;
155pub const PROTO_ipv6_frag = 0o054;
156pub const PROTO_idrp = 0o055;
157pub const PROTO_rsvp = 0o056;
158pub const PROTO_gre = 0o057;
159pub const PROTO_esp = 0o062;
160pub const PROTO_ah = 0o063;
161pub const PROTO_skip = 0o071;
162pub const PROTO_ipv6_icmp = 0o072;
163pub const PROTO_ipv6_nonxt = 0o073;
164pub const PROTO_ipv6_opts = 0o074;
165pub const PROTO_rspf = 0o111;
166pub const PROTO_vmtp = 0o121;
167pub const PROTO_ospf = 0o131;
168pub const PROTO_ipip = 0o136;
169pub const PROTO_encap = 0o142;
170pub const PROTO_pim = 0o147;
171pub const PROTO_raw = 0o377;
172
173pub const PF_UNSPEC = 0;
174pub const PF_LOCAL = 1;
175pub const PF_UNIX = PF_LOCAL;
176pub const PF_FILE = PF_LOCAL;
177pub const PF_INET = 2;
178pub const PF_AX25 = 3;
179pub const PF_IPX = 4;
180pub const PF_APPLETALK = 5;
181pub const PF_NETROM = 6;
182pub const PF_BRIDGE = 7;
183pub const PF_ATMPVC = 8;
184pub const PF_X25 = 9;
185pub const PF_INET6 = 10;
186pub const PF_ROSE = 11;
187pub const PF_DECnet = 12;
188pub const PF_NETBEUI = 13;
189pub const PF_SECURITY = 14;
190pub const PF_KEY = 15;
191pub const PF_NETLINK = 16;
192pub const PF_ROUTE = PF_NETLINK;
193pub const PF_PACKET = 17;
194pub const PF_ASH = 18;
195pub const PF_ECONET = 19;
196pub const PF_ATMSVC = 20;
197pub const PF_RDS = 21;
198pub const PF_SNA = 22;
199pub const PF_IRDA = 23;
200pub const PF_PPPOX = 24;
201pub const PF_WANPIPE = 25;
202pub const PF_LLC = 26;
203pub const PF_IB = 27;
204pub const PF_MPLS = 28;
205pub const PF_CAN = 29;
206pub const PF_TIPC = 30;
207pub const PF_BLUETOOTH = 31;
208pub const PF_IUCV = 32;
209pub const PF_RXRPC = 33;
210pub const PF_ISDN = 34;
211pub const PF_PHONET = 35;
212pub const PF_IEEE802154 = 36;
213pub const PF_CAIF = 37;
214pub const PF_ALG = 38;
215pub const PF_NFC = 39;
216pub const PF_VSOCK = 40;
217pub const PF_MAX = 41;
218
219pub const AF_UNSPEC = PF_UNSPEC;
220pub const AF_LOCAL = PF_LOCAL;
221pub const AF_UNIX = AF_LOCAL;
222pub const AF_FILE = AF_LOCAL;
223pub const AF_INET = PF_INET;
224pub const AF_AX25 = PF_AX25;
225pub const AF_IPX = PF_IPX;
226pub const AF_APPLETALK = PF_APPLETALK;
227pub const AF_NETROM = PF_NETROM;
228pub const AF_BRIDGE = PF_BRIDGE;
229pub const AF_ATMPVC = PF_ATMPVC;
230pub const AF_X25 = PF_X25;
231pub const AF_INET6 = PF_INET6;
232pub const AF_ROSE = PF_ROSE;
233pub const AF_DECnet = PF_DECnet;
234pub const AF_NETBEUI = PF_NETBEUI;
235pub const AF_SECURITY = PF_SECURITY;
236pub const AF_KEY = PF_KEY;
237pub const AF_NETLINK = PF_NETLINK;
238pub const AF_ROUTE = PF_ROUTE;
239pub const AF_PACKET = PF_PACKET;
240pub const AF_ASH = PF_ASH;
241pub const AF_ECONET = PF_ECONET;
242pub const AF_ATMSVC = PF_ATMSVC;
243pub const AF_RDS = PF_RDS;
244pub const AF_SNA = PF_SNA;
245pub const AF_IRDA = PF_IRDA;
246pub const AF_PPPOX = PF_PPPOX;
247pub const AF_WANPIPE = PF_WANPIPE;
248pub const AF_LLC = PF_LLC;
249pub const AF_IB = PF_IB;
250pub const AF_MPLS = PF_MPLS;
251pub const AF_CAN = PF_CAN;
252pub const AF_TIPC = PF_TIPC;
253pub const AF_BLUETOOTH = PF_BLUETOOTH;
254pub const AF_IUCV = PF_IUCV;
255pub const AF_RXRPC = PF_RXRPC;
256pub const AF_ISDN = PF_ISDN;
257pub const AF_PHONET = PF_PHONET;
258pub const AF_IEEE802154 = PF_IEEE802154;
259pub const AF_CAIF = PF_CAIF;
260pub const AF_ALG = PF_ALG;
261pub const AF_NFC = PF_NFC;
262pub const AF_VSOCK = PF_VSOCK;
263pub const AF_MAX = PF_MAX;
264
265pub const DT_UNKNOWN = 0;
266pub const DT_FIFO = 1;
267pub const DT_CHR = 2;
268pub const DT_DIR = 4;
269pub const DT_BLK = 6;
270pub const DT_REG = 8;
271pub const DT_LNK = 10;
272pub const DT_SOCK = 12;
273pub const DT_WHT = 14;
274
275
276pub const TCGETS = 0x5401;
277pub const TCSETS = 0x5402;
278pub const TCSETSW = 0x5403;
279pub const TCSETSF = 0x5404;
280pub const TCGETA = 0x5405;
281pub const TCSETA = 0x5406;
282pub const TCSETAW = 0x5407;
283pub const TCSETAF = 0x5408;
284pub const TCSBRK = 0x5409;
285pub const TCXONC = 0x540A;
286pub const TCFLSH = 0x540B;
287pub const TIOCEXCL = 0x540C;
288pub const TIOCNXCL = 0x540D;
289pub const TIOCSCTTY = 0x540E;
290pub const TIOCGPGRP = 0x540F;
291pub const TIOCSPGRP = 0x5410;
292pub const TIOCOUTQ = 0x5411;
293pub const TIOCSTI = 0x5412;
294pub const TIOCGWINSZ = 0x5413;
295pub const TIOCSWINSZ = 0x5414;
296pub const TIOCMGET = 0x5415;
297pub const TIOCMBIS = 0x5416;
298pub const TIOCMBIC = 0x5417;
299pub const TIOCMSET = 0x5418;
300pub const TIOCGSOFTCAR = 0x5419;
301pub const TIOCSSOFTCAR = 0x541A;
302pub const FIONREAD = 0x541B;
303pub const TIOCINQ = FIONREAD;
304pub const TIOCLINUX = 0x541C;
305pub const TIOCCONS = 0x541D;
306pub const TIOCGSERIAL = 0x541E;
307pub const TIOCSSERIAL = 0x541F;
308pub const TIOCPKT = 0x5420;
309pub const FIONBIO = 0x5421;
310pub const TIOCNOTTY = 0x5422;
311pub const TIOCSETD = 0x5423;
312pub const TIOCGETD = 0x5424;
313pub const TCSBRKP = 0x5425;
314pub const TIOCSBRK = 0x5427;
315pub const TIOCCBRK = 0x5428;
316pub const TIOCGSID = 0x5429;
317pub const TIOCGRS485 = 0x542E;
318pub const TIOCSRS485 = 0x542F;
319pub const TIOCGPTN = 0x80045430;
320pub const TIOCSPTLCK = 0x40045431;
321pub const TIOCGDEV = 0x80045432;
322pub const TCGETX = 0x5432;
323pub const TCSETX = 0x5433;
324pub const TCSETXF = 0x5434;
325pub const TCSETXW = 0x5435;
326pub const TIOCSIG = 0x40045436;
327pub const TIOCVHANGUP = 0x5437;
328pub const TIOCGPKT = 0x80045438;
329pub const TIOCGPTLCK = 0x80045439;
330pub const TIOCGEXCL = 0x80045440;
331
332pub const EPOLL_CTL_ADD = 1;
333pub const EPOLL_CTL_DEL = 2;
334pub const EPOLL_CTL_MOD = 3;
335
336pub const EPOLLIN = 0x001;
337pub const EPOLLPRI = 0x002;
338pub const EPOLLOUT = 0x004;
339pub const EPOLLRDNORM = 0x040;
340pub const EPOLLRDBAND = 0x080;
341pub const EPOLLWRNORM = 0x100;
342pub const EPOLLWRBAND = 0x200;
343pub const EPOLLMSG = 0x400;
344pub const EPOLLERR = 0x008;
345pub const EPOLLHUP = 0x010;
346pub const EPOLLRDHUP = 0x2000;
347pub const EPOLLEXCLUSIVE = (u32(1) << 28);
348pub const EPOLLWAKEUP = (u32(1) << 29);
349pub const EPOLLONESHOT = (u32(1) << 30);
350pub const EPOLLET = (u32(1) << 31);
351
352pub const CLOCK_REALTIME = 0;
353pub const CLOCK_MONOTONIC = 1;
354pub const CLOCK_PROCESS_CPUTIME_ID = 2;
355pub const CLOCK_THREAD_CPUTIME_ID = 3;
356pub const CLOCK_MONOTONIC_RAW = 4;
357pub const CLOCK_REALTIME_COARSE = 5;
358pub const CLOCK_MONOTONIC_COARSE = 6;
359pub const CLOCK_BOOTTIME = 7;
360pub const CLOCK_REALTIME_ALARM = 8;
361pub const CLOCK_BOOTTIME_ALARM = 9;
362pub const CLOCK_SGI_CYCLE = 10;
363pub const CLOCK_TAI = 11;
364
365pub const TFD_NONBLOCK = O_NONBLOCK;
366pub const TFD_CLOEXEC = O_CLOEXEC;
367
368pub const TFD_TIMER_ABSTIME = 1;
369pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1);
370
371fn unsigned(s: i32) u32 { return @bitCast(u32, s); }
372fn signed(s: u32) i32 { return @bitCast(i32, s); }
373pub fn WEXITSTATUS(s: i32) i32 { return signed((unsigned(s) & 0xff00) >> 8); }
374pub fn WTERMSIG(s: i32) i32 { return signed(unsigned(s) & 0x7f); }
375pub fn WSTOPSIG(s: i32) i32 { return WEXITSTATUS(s); }
376pub fn WIFEXITED(s: i32) bool { return WTERMSIG(s) == 0; }
377pub fn WIFSTOPPED(s: i32) bool { return (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00; }
378pub fn WIFSIGNALED(s: i32) bool { return (unsigned(s)&0xffff)-%1 < 0xff; }
379
380
381pub const winsize = extern struct {
382 ws_row: u16,
383 ws_col: u16,
384 ws_xpixel: u16,
385 ws_ypixel: u16,
386};
387
388/// Get the errno from a syscall return value, or 0 for no error.
389pub fn getErrno(r: usize) usize {
390 const signed_r = @bitCast(isize, r);
391 return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
392}
393
394pub fn dup2(old: i32, new: i32) usize {
395 return arch.syscall2(arch.SYS_dup2, usize(old), usize(new));
396}
397
398pub fn chdir(path: &const u8) usize {
399 return arch.syscall1(arch.SYS_chdir, @ptrToInt(path));
400}
401
402pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize {
403 return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp));
404}
405
406pub fn fork() usize {
407 return arch.syscall0(arch.SYS_fork);
408}
409
410pub fn getcwd(buf: &u8, size: usize) usize {
411 return arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size);
412}
413
414pub fn getdents(fd: i32, dirp: &u8, count: usize) usize {
415 return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count);
416}
417
418pub fn isatty(fd: i32) bool {
419 var wsz: winsize = undefined;
420 return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
421}
422
423pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize {
424 return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
425}
426
427pub fn mkdir(path: &const u8, mode: u32) usize {
428 return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode);
429}
430
431pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize {
432 return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd),
433 @bitCast(usize, offset));
434}
435
436pub fn munmap(address: &u8, length: usize) usize {
437 return arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length);
438}
439
440pub fn read(fd: i32, buf: &u8, count: usize) usize {
441 return arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count);
442}
443
444pub fn rmdir(path: &const u8) usize {
445 return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path));
446}
447
448pub fn symlink(existing: &const u8, new: &const u8) usize {
449 return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new));
450}
451
452pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize {
453 return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset);
454}
455
456pub fn pipe(fd: &[2]i32) usize {
457 return pipe2(fd, 0);
458}
459
460pub fn pipe2(fd: &[2]i32, flags: usize) usize {
461 return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags);
462}
463
464pub fn write(fd: i32, buf: &const u8, count: usize) usize {
465 return arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count);
466}
467
468pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) usize {
469 return arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset);
470}
471
472pub fn rename(old: &const u8, new: &const u8) usize {
473 return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new));
474}
475
476pub fn open(path: &const u8, flags: u32, perm: usize) usize {
477 return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm);
478}
479
480pub fn create(path: &const u8, perm: usize) usize {
481 return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm);
482}
483
484pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize {
485 return arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode);
486}
487
488pub fn close(fd: i32) usize {
489 return arch.syscall1(arch.SYS_close, usize(fd));
490}
491
492pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
493 return arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos);
494}
495
496pub fn exit(status: i32) noreturn {
497 _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status)));
498 unreachable;
499}
500
501pub fn getrandom(buf: &u8, count: usize, flags: u32) usize {
502 return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags));
503}
504
505pub fn kill(pid: i32, sig: i32) usize {
506 return arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig));
507}
508
509pub fn unlink(path: &const u8) usize {
510 return arch.syscall1(arch.SYS_unlink, @ptrToInt(path));
511}
512
513pub fn waitpid(pid: i32, status: &i32, options: i32) usize {
514 return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
515}
516
517pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
518 return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));
519}
520
521pub fn setuid(uid: u32) usize {
522 return arch.syscall1(arch.SYS_setuid, uid);
523}
524
525pub fn setgid(gid: u32) usize {
526 return arch.syscall1(arch.SYS_setgid, gid);
527}
528
529pub fn setreuid(ruid: u32, euid: u32) usize {
530 return arch.syscall2(arch.SYS_setreuid, ruid, euid);
531}
532
533pub fn setregid(rgid: u32, egid: u32) usize {
534 return arch.syscall2(arch.SYS_setregid, rgid, egid);
535}
536
537pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize {
538 return arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8);
539}
540
541pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
542 assert(sig >= 1);
543 assert(sig != SIGKILL);
544 assert(sig != SIGSTOP);
545 var ksa = k_sigaction {
546 .handler = act.handler,
547 .flags = act.flags | SA_RESTORER,
548 .mask = undefined,
549 .restorer = @ptrCast(extern fn()void, arch.restore_rt),
550 };
551 var ksa_old: k_sigaction = undefined;
552 @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8);
553 const result = arch.syscall4(arch.SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask)));
554 const err = getErrno(result);
555 if (err != 0) {
556 return result;
557 }
558 if (oact) |old| {
559 old.handler = ksa_old.handler;
560 old.flags = @truncate(u32, ksa_old.flags);
561 @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask)));
562 }
563 return 0;
564}
565
566const NSIG = 65;
567const sigset_t = [128 / @sizeOf(usize)]usize;
568const all_mask = []usize{@maxValue(usize)};
569const app_mask = []usize{0xfffffffc7fffffff};
570
571const k_sigaction = extern struct {
572 handler: extern fn(i32)void,
573 flags: usize,
574 restorer: extern fn()void,
575 mask: [2]u32,
576};
577
578/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
579pub const Sigaction = struct {
580 handler: extern fn(i32)void,
581 mask: sigset_t,
582 flags: u32,
583};
584
585pub const SIG_ERR = @intToPtr(extern fn(i32)void, @maxValue(usize));
586pub const SIG_DFL = @intToPtr(extern fn(i32)void, 0);
587pub const SIG_IGN = @intToPtr(extern fn(i32)void, 1);
588pub const empty_sigset = []usize{0} ** sigset_t.len;
589
590pub fn raise(sig: i32) usize {
591 var set: sigset_t = undefined;
592 blockAppSignals(&set);
593 const tid = i32(arch.syscall0(arch.SYS_gettid));
594 const ret = arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig));
595 restoreSignals(&set);
596 return ret;
597}
598
599fn blockAllSignals(set: &sigset_t) void {
600 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8);
601}
602
603fn blockAppSignals(set: &sigset_t) void {
604 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8);
605}
606
607fn restoreSignals(set: &sigset_t) void {
608 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8);
609}
610
611pub fn sigaddset(set: &sigset_t, sig: u6) void {
612 const s = sig - 1;
613 (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
614}
615
616pub fn sigismember(set: &const sigset_t, sig: u6) bool {
617 const s = sig - 1;
618 return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
619}
620
621
622pub const sa_family_t = u16;
623pub const socklen_t = u32;
624pub const in_addr = u32;
625pub const in6_addr = [16]u8;
626
627pub const sockaddr = extern struct {
628 family: sa_family_t,
629 port: u16,
630 data: [12]u8,
631};
632
633pub const sockaddr_in = extern struct {
634 family: sa_family_t,
635 port: u16,
636 addr: in_addr,
637 zero: [8]u8,
638};
639
640pub const sockaddr_in6 = extern struct {
641 family: sa_family_t,
642 port: u16,
643 flowinfo: u32,
644 addr: in6_addr,
645 scope_id: u32,
646};
647
648pub const iovec = extern struct {
649 iov_base: &u8,
650 iov_len: usize,
651};
652
653pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
654 return arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len));
655}
656
657pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
658 return arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len));
659}
660
661pub fn socket(domain: i32, socket_type: i32, protocol: i32) usize {
662 return arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol));
663}
664
665pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) usize {
666 return arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen));
667}
668
669pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) usize {
670 return arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen));
671}
672
673pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) usize {
674 return arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags);
675}
676
677pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) usize {
678 return arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len));
679}
680
681pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) usize {
682 return arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags);
683}
684
685pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32,
686 noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize
687{
688 return arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
689}
690
691pub fn shutdown(fd: i32, how: i32) usize {
692 return arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how));
693}
694
695pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) usize {
696 return arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len));
697}
698
699pub fn listen(fd: i32, backlog: i32) usize {
700 return arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog));
701}
702
703pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) usize {
704 return arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen));
705}
706
707pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
708 return arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0]));
709}
710
711pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
712 return accept4(fd, addr, len, 0);
713}
714
715pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) usize {
716 return arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags);
717}
718
719// error NameTooLong;
720// error SystemResources;
721// error Io;
722//
723// pub fn if_nametoindex(name: []u8) %u32 {
724// var ifr: ifreq = undefined;
725//
726// if (name.len >= ifr.ifr_name.len) {
727// return error.NameTooLong;
728// }
729//
730// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
731// const socket_err = getErrno(socket_ret);
732// if (socket_err > 0) {
733// return error.SystemResources;
734// }
735// const socket_fd = i32(socket_ret);
736// @memcpy(&ifr.ifr_name[0], &name[0], name.len);
737// ifr.ifr_name[name.len] = 0;
738// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr);
739// close(socket_fd);
740// const ioctl_err = getErrno(ioctl_ret);
741// if (ioctl_err > 0) {
742// return error.Io;
743// }
744// return ifr.ifr_ifindex;
745// }
746
747pub const Stat = arch.Stat;
748pub const timespec = arch.timespec;
749
750pub fn fstat(fd: i32, stat_buf: &Stat) usize {
751 return arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf));
752}
753
754pub const epoll_data = u64;
755
756pub const epoll_event = extern struct {
757 events: u32,
758 data: epoll_data
759};
760
761pub fn epoll_create() usize {
762 return arch.syscall1(arch.SYS_epoll_create, usize(1));
763}
764
765pub fn epoll_ctl(epoll_fd: i32, op: i32, fd: i32, ev: &epoll_event) usize {
766 return arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev));
767}
768
769pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: i32, timeout: i32) usize {
770 return arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout));
771}
772
773pub fn timerfd_create(clockid: i32, flags: u32) usize {
774 return arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags));
775}
776
777pub const itimerspec = extern struct {
778 it_interval: timespec,
779 it_value: timespec
780};
781
782pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize {
783 return arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value));
784}
785
786pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) usize {
787 return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value));
788}
789
790test "import linux_test" {
791 // TODO lazy analysis should prevent this test from being compiled on windows, but
792 // it is still compiled on windows
793 if (builtin.os == builtin.Os.linux) {
794 _ = @import("linux_test.zig");
795 }
796}
std/os/linux/errno.zig created+146
......@@ -0,0 +1,146 @@
1pub const EPERM = 1; /// Operation not permitted
2pub const ENOENT = 2; /// No such file or directory
3pub const ESRCH = 3; /// No such process
4pub const EINTR = 4; /// Interrupted system call
5pub const EIO = 5; /// I/O error
6pub const ENXIO = 6; /// No such device or address
7pub const E2BIG = 7; /// Arg list too long
8pub const ENOEXEC = 8; /// Exec format error
9pub const EBADF = 9; /// Bad file number
10pub const ECHILD = 10; /// No child processes
11pub const EAGAIN = 11; /// Try again
12pub const ENOMEM = 12; /// Out of memory
13pub const EACCES = 13; /// Permission denied
14pub const EFAULT = 14; /// Bad address
15pub const ENOTBLK = 15; /// Block device required
16pub const EBUSY = 16; /// Device or resource busy
17pub const EEXIST = 17; /// File exists
18pub const EXDEV = 18; /// Cross-device link
19pub const ENODEV = 19; /// No such device
20pub const ENOTDIR = 20; /// Not a directory
21pub const EISDIR = 21; /// Is a directory
22pub const EINVAL = 22; /// Invalid argument
23pub const ENFILE = 23; /// File table overflow
24pub const EMFILE = 24; /// Too many open files
25pub const ENOTTY = 25; /// Not a typewriter
26pub const ETXTBSY = 26; /// Text file busy
27pub const EFBIG = 27; /// File too large
28pub const ENOSPC = 28; /// No space left on device
29pub const ESPIPE = 29; /// Illegal seek
30pub const EROFS = 30; /// Read-only file system
31pub const EMLINK = 31; /// Too many links
32pub const EPIPE = 32; /// Broken pipe
33pub const EDOM = 33; /// Math argument out of domain of func
34pub const ERANGE = 34; /// Math result not representable
35pub const EDEADLK = 35; /// Resource deadlock would occur
36pub const ENAMETOOLONG = 36; /// File name too long
37pub const ENOLCK = 37; /// No record locks available
38pub const ENOSYS = 38; /// Function not implemented
39pub const ENOTEMPTY = 39; /// Directory not empty
40pub const ELOOP = 40; /// Too many symbolic links encountered
41pub const EWOULDBLOCK = EAGAIN; /// Operation would block
42pub const ENOMSG = 42; /// No message of desired type
43pub const EIDRM = 43; /// Identifier removed
44pub const ECHRNG = 44; /// Channel number out of range
45pub const EL2NSYNC = 45; /// Level 2 not synchronized
46pub const EL3HLT = 46; /// Level 3 halted
47pub const EL3RST = 47; /// Level 3 reset
48pub const ELNRNG = 48; /// Link number out of range
49pub const EUNATCH = 49; /// Protocol driver not attached
50pub const ENOCSI = 50; /// No CSI structure available
51pub const EL2HLT = 51; /// Level 2 halted
52pub const EBADE = 52; /// Invalid exchange
53pub const EBADR = 53; /// Invalid request descriptor
54pub const EXFULL = 54; /// Exchange full
55pub const ENOANO = 55; /// No anode
56pub const EBADRQC = 56; /// Invalid request code
57pub const EBADSLT = 57; /// Invalid slot
58
59pub const EBFONT = 59; /// Bad font file format
60pub const ENOSTR = 60; /// Device not a stream
61pub const ENODATA = 61; /// No data available
62pub const ETIME = 62; /// Timer expired
63pub const ENOSR = 63; /// Out of streams resources
64pub const ENONET = 64; /// Machine is not on the network
65pub const ENOPKG = 65; /// Package not installed
66pub const EREMOTE = 66; /// Object is remote
67pub const ENOLINK = 67; /// Link has been severed
68pub const EADV = 68; /// Advertise error
69pub const ESRMNT = 69; /// Srmount error
70pub const ECOMM = 70; /// Communication error on send
71pub const EPROTO = 71; /// Protocol error
72pub const EMULTIHOP = 72; /// Multihop attempted
73pub const EDOTDOT = 73; /// RFS specific error
74pub const EBADMSG = 74; /// Not a data message
75pub const EOVERFLOW = 75; /// Value too large for defined data type
76pub const ENOTUNIQ = 76; /// Name not unique on network
77pub const EBADFD = 77; /// File descriptor in bad state
78pub const EREMCHG = 78; /// Remote address changed
79pub const ELIBACC = 79; /// Can not access a needed shared library
80pub const ELIBBAD = 80; /// Accessing a corrupted shared library
81pub const ELIBSCN = 81; /// .lib section in a.out corrupted
82pub const ELIBMAX = 82; /// Attempting to link in too many shared libraries
83pub const ELIBEXEC = 83; /// Cannot exec a shared library directly
84pub const EILSEQ = 84; /// Illegal byte sequence
85pub const ERESTART = 85; /// Interrupted system call should be restarted
86pub const ESTRPIPE = 86; /// Streams pipe error
87pub const EUSERS = 87; /// Too many users
88pub const ENOTSOCK = 88; /// Socket operation on non-socket
89pub const EDESTADDRREQ = 89; /// Destination address required
90pub const EMSGSIZE = 90; /// Message too long
91pub const EPROTOTYPE = 91; /// Protocol wrong type for socket
92pub const ENOPROTOOPT = 92; /// Protocol not available
93pub const EPROTONOSUPPORT = 93; /// Protocol not supported
94pub const ESOCKTNOSUPPORT = 94; /// Socket type not supported
95pub const EOPNOTSUPP = 95; /// Operation not supported on transport endpoint
96pub const EPFNOSUPPORT = 96; /// Protocol family not supported
97pub const EAFNOSUPPORT = 97; /// Address family not supported by protocol
98pub const EADDRINUSE = 98; /// Address already in use
99pub const EADDRNOTAVAIL = 99; /// Cannot assign requested address
100pub const ENETDOWN = 100; /// Network is down
101pub const ENETUNREACH = 101; /// Network is unreachable
102pub const ENETRESET = 102; /// Network dropped connection because of reset
103pub const ECONNABORTED = 103; /// Software caused connection abort
104pub const ECONNRESET = 104; /// Connection reset by peer
105pub const ENOBUFS = 105; /// No buffer space available
106pub const EISCONN = 106; /// Transport endpoint is already connected
107pub const ENOTCONN = 107; /// Transport endpoint is not connected
108pub const ESHUTDOWN = 108; /// Cannot send after transport endpoint shutdown
109pub const ETOOMANYREFS = 109; /// Too many references: cannot splice
110pub const ETIMEDOUT = 110; /// Connection timed out
111pub const ECONNREFUSED = 111; /// Connection refused
112pub const EHOSTDOWN = 112; /// Host is down
113pub const EHOSTUNREACH = 113; /// No route to host
114pub const EALREADY = 114; /// Operation already in progress
115pub const EINPROGRESS = 115; /// Operation now in progress
116pub const ESTALE = 116; /// Stale NFS file handle
117pub const EUCLEAN = 117; /// Structure needs cleaning
118pub const ENOTNAM = 118; /// Not a XENIX named type file
119pub const ENAVAIL = 119; /// No XENIX semaphores available
120pub const EISNAM = 120; /// Is a named type file
121pub const EREMOTEIO = 121; /// Remote I/O error
122pub const EDQUOT = 122; /// Quota exceeded
123
124pub const ENOMEDIUM = 123; /// No medium found
125pub const EMEDIUMTYPE = 124; /// Wrong medium type
126
127// nameserver query return codes
128pub const ENSROK = 0; /// DNS server returned answer with no data
129pub const ENSRNODATA = 160; /// DNS server returned answer with no data
130pub const ENSRFORMERR = 161; /// DNS server claims query was misformatted
131pub const ENSRSERVFAIL = 162; /// DNS server returned general failure
132pub const ENSRNOTFOUND = 163; /// Domain name not found
133pub const ENSRNOTIMP = 164; /// DNS server does not implement requested operation
134pub const ENSRREFUSED = 165; /// DNS server refused query
135pub const ENSRBADQUERY = 166; /// Misformatted DNS query
136pub const ENSRBADNAME = 167; /// Misformatted domain name
137pub const ENSRBADFAMILY = 168; /// Unsupported address family
138pub const ENSRBADRESP = 169; /// Misformatted DNS reply
139pub const ENSRCONNREFUSED = 170; /// Could not contact DNS servers
140pub const ENSRTIMEOUT = 171; /// Timeout while contacting DNS servers
141pub const ENSROF = 172; /// End of file
142pub const ENSRFILE = 173; /// Error reading file
143pub const ENSRNOMEM = 174; /// Out of memory
144pub const ENSRDESTRUCTION = 175; /// Application terminated lookup
145pub const ENSRQUERYDOMAINTOOLONG = 176; /// Domain name is too long
146pub const ENSRCNAMELOOP = 177; /// Domain name is too long
std/os/linux/i386.zig created+505
......@@ -0,0 +1,505 @@
1const std = @import("../../index.zig");
2const linux = std.os.linux;
3const socklen_t = linux.socklen_t;
4const iovec = linux.iovec;
5
6pub const SYS_restart_syscall = 0;
7pub const SYS_exit = 1;
8pub const SYS_fork = 2;
9pub const SYS_read = 3;
10pub const SYS_write = 4;
11pub const SYS_open = 5;
12pub const SYS_close = 6;
13pub const SYS_waitpid = 7;
14pub const SYS_creat = 8;
15pub const SYS_link = 9;
16pub const SYS_unlink = 10;
17pub const SYS_execve = 11;
18pub const SYS_chdir = 12;
19pub const SYS_time = 13;
20pub const SYS_mknod = 14;
21pub const SYS_chmod = 15;
22pub const SYS_lchown = 16;
23pub const SYS_break = 17;
24pub const SYS_oldstat = 18;
25pub const SYS_lseek = 19;
26pub const SYS_getpid = 20;
27pub const SYS_mount = 21;
28pub const SYS_umount = 22;
29pub const SYS_setuid = 23;
30pub const SYS_getuid = 24;
31pub const SYS_stime = 25;
32pub const SYS_ptrace = 26;
33pub const SYS_alarm = 27;
34pub const SYS_oldfstat = 28;
35pub const SYS_pause = 29;
36pub const SYS_utime = 30;
37pub const SYS_stty = 31;
38pub const SYS_gtty = 32;
39pub const SYS_access = 33;
40pub const SYS_nice = 34;
41pub const SYS_ftime = 35;
42pub const SYS_sync = 36;
43pub const SYS_kill = 37;
44pub const SYS_rename = 38;
45pub const SYS_mkdir = 39;
46pub const SYS_rmdir = 40;
47pub const SYS_dup = 41;
48pub const SYS_pipe = 42;
49pub const SYS_times = 43;
50pub const SYS_prof = 44;
51pub const SYS_brk = 45;
52pub const SYS_setgid = 46;
53pub const SYS_getgid = 47;
54pub const SYS_signal = 48;
55pub const SYS_geteuid = 49;
56pub const SYS_getegid = 50;
57pub const SYS_acct = 51;
58pub const SYS_umount2 = 52;
59pub const SYS_lock = 53;
60pub const SYS_ioctl = 54;
61pub const SYS_fcntl = 55;
62pub const SYS_mpx = 56;
63pub const SYS_setpgid = 57;
64pub const SYS_ulimit = 58;
65pub const SYS_oldolduname = 59;
66pub const SYS_umask = 60;
67pub const SYS_chroot = 61;
68pub const SYS_ustat = 62;
69pub const SYS_dup2 = 63;
70pub const SYS_getppid = 64;
71pub const SYS_getpgrp = 65;
72pub const SYS_setsid = 66;
73pub const SYS_sigaction = 67;
74pub const SYS_sgetmask = 68;
75pub const SYS_ssetmask = 69;
76pub const SYS_setreuid = 70;
77pub const SYS_setregid = 71;
78pub const SYS_sigsuspend = 72;
79pub const SYS_sigpending = 73;
80pub const SYS_sethostname = 74;
81pub const SYS_setrlimit = 75;
82pub const SYS_getrlimit = 76;
83pub const SYS_getrusage = 77;
84pub const SYS_gettimeofday = 78;
85pub const SYS_settimeofday = 79;
86pub const SYS_getgroups = 80;
87pub const SYS_setgroups = 81;
88pub const SYS_select = 82;
89pub const SYS_symlink = 83;
90pub const SYS_oldlstat = 84;
91pub const SYS_readlink = 85;
92pub const SYS_uselib = 86;
93pub const SYS_swapon = 87;
94pub const SYS_reboot = 88;
95pub const SYS_readdir = 89;
96pub const SYS_mmap = 90;
97pub const SYS_munmap = 91;
98pub const SYS_truncate = 92;
99pub const SYS_ftruncate = 93;
100pub const SYS_fchmod = 94;
101pub const SYS_fchown = 95;
102pub const SYS_getpriority = 96;
103pub const SYS_setpriority = 97;
104pub const SYS_profil = 98;
105pub const SYS_statfs = 99;
106pub const SYS_fstatfs = 100;
107pub const SYS_ioperm = 101;
108pub const SYS_socketcall = 102;
109pub const SYS_syslog = 103;
110pub const SYS_setitimer = 104;
111pub const SYS_getitimer = 105;
112pub const SYS_stat = 106;
113pub const SYS_lstat = 107;
114pub const SYS_fstat = 108;
115pub const SYS_olduname = 109;
116pub const SYS_iopl = 110;
117pub const SYS_vhangup = 111;
118pub const SYS_idle = 112;
119pub const SYS_vm86old = 113;
120pub const SYS_wait4 = 114;
121pub const SYS_swapoff = 115;
122pub const SYS_sysinfo = 116;
123pub const SYS_ipc = 117;
124pub const SYS_fsync = 118;
125pub const SYS_sigreturn = 119;
126pub const SYS_clone = 120;
127pub const SYS_setdomainname = 121;
128pub const SYS_uname = 122;
129pub const SYS_modify_ldt = 123;
130pub const SYS_adjtimex = 124;
131pub const SYS_mprotect = 125;
132pub const SYS_sigprocmask = 126;
133pub const SYS_create_module = 127;
134pub const SYS_init_module = 128;
135pub const SYS_delete_module = 129;
136pub const SYS_get_kernel_syms = 130;
137pub const SYS_quotactl = 131;
138pub const SYS_getpgid = 132;
139pub const SYS_fchdir = 133;
140pub const SYS_bdflush = 134;
141pub const SYS_sysfs = 135;
142pub const SYS_personality = 136;
143pub const SYS_afs_syscall = 137;
144pub const SYS_setfsuid = 138;
145pub const SYS_setfsgid = 139;
146pub const SYS__llseek = 140;
147pub const SYS_getdents = 141;
148pub const SYS__newselect = 142;
149pub const SYS_flock = 143;
150pub const SYS_msync = 144;
151pub const SYS_readv = 145;
152pub const SYS_writev = 146;
153pub const SYS_getsid = 147;
154pub const SYS_fdatasync = 148;
155pub const SYS__sysctl = 149;
156pub const SYS_mlock = 150;
157pub const SYS_munlock = 151;
158pub const SYS_mlockall = 152;
159pub const SYS_munlockall = 153;
160pub const SYS_sched_setparam = 154;
161pub const SYS_sched_getparam = 155;
162pub const SYS_sched_setscheduler = 156;
163pub const SYS_sched_getscheduler = 157;
164pub const SYS_sched_yield = 158;
165pub const SYS_sched_get_priority_max = 159;
166pub const SYS_sched_get_priority_min = 160;
167pub const SYS_sched_rr_get_interval = 161;
168pub const SYS_nanosleep = 162;
169pub const SYS_mremap = 163;
170pub const SYS_setresuid = 164;
171pub const SYS_getresuid = 165;
172pub const SYS_vm86 = 166;
173pub const SYS_query_module = 167;
174pub const SYS_poll = 168;
175pub const SYS_nfsservctl = 169;
176pub const SYS_setresgid = 170;
177pub const SYS_getresgid = 171;
178pub const SYS_prctl = 172;
179pub const SYS_rt_sigreturn = 173;
180pub const SYS_rt_sigaction = 174;
181pub const SYS_rt_sigprocmask = 175;
182pub const SYS_rt_sigpending = 176;
183pub const SYS_rt_sigtimedwait = 177;
184pub const SYS_rt_sigqueueinfo = 178;
185pub const SYS_rt_sigsuspend = 179;
186pub const SYS_pread64 = 180;
187pub const SYS_pwrite64 = 181;
188pub const SYS_chown = 182;
189pub const SYS_getcwd = 183;
190pub const SYS_capget = 184;
191pub const SYS_capset = 185;
192pub const SYS_sigaltstack = 186;
193pub const SYS_sendfile = 187;
194pub const SYS_getpmsg = 188;
195pub const SYS_putpmsg = 189;
196pub const SYS_vfork = 190;
197pub const SYS_ugetrlimit = 191;
198pub const SYS_mmap2 = 192;
199pub const SYS_truncate64 = 193;
200pub const SYS_ftruncate64 = 194;
201pub const SYS_stat64 = 195;
202pub const SYS_lstat64 = 196;
203pub const SYS_fstat64 = 197;
204pub const SYS_lchown32 = 198;
205pub const SYS_getuid32 = 199;
206pub const SYS_getgid32 = 200;
207pub const SYS_geteuid32 = 201;
208pub const SYS_getegid32 = 202;
209pub const SYS_setreuid32 = 203;
210pub const SYS_setregid32 = 204;
211pub const SYS_getgroups32 = 205;
212pub const SYS_setgroups32 = 206;
213pub const SYS_fchown32 = 207;
214pub const SYS_setresuid32 = 208;
215pub const SYS_getresuid32 = 209;
216pub const SYS_setresgid32 = 210;
217pub const SYS_getresgid32 = 211;
218pub const SYS_chown32 = 212;
219pub const SYS_setuid32 = 213;
220pub const SYS_setgid32 = 214;
221pub const SYS_setfsuid32 = 215;
222pub const SYS_setfsgid32 = 216;
223pub const SYS_pivot_root = 217;
224pub const SYS_mincore = 218;
225pub const SYS_madvise = 219;
226pub const SYS_madvise1 = 219;
227pub const SYS_getdents64 = 220;
228pub const SYS_fcntl64 = 221;
229pub const SYS_gettid = 224;
230pub const SYS_readahead = 225;
231pub const SYS_setxattr = 226;
232pub const SYS_lsetxattr = 227;
233pub const SYS_fsetxattr = 228;
234pub const SYS_getxattr = 229;
235pub const SYS_lgetxattr = 230;
236pub const SYS_fgetxattr = 231;
237pub const SYS_listxattr = 232;
238pub const SYS_llistxattr = 233;
239pub const SYS_flistxattr = 234;
240pub const SYS_removexattr = 235;
241pub const SYS_lremovexattr = 236;
242pub const SYS_fremovexattr = 237;
243pub const SYS_tkill = 238;
244pub const SYS_sendfile64 = 239;
245pub const SYS_futex = 240;
246pub const SYS_sched_setaffinity = 241;
247pub const SYS_sched_getaffinity = 242;
248pub const SYS_set_thread_area = 243;
249pub const SYS_get_thread_area = 244;
250pub const SYS_io_setup = 245;
251pub const SYS_io_destroy = 246;
252pub const SYS_io_getevents = 247;
253pub const SYS_io_submit = 248;
254pub const SYS_io_cancel = 249;
255pub const SYS_fadvise64 = 250;
256pub const SYS_exit_group = 252;
257pub const SYS_lookup_dcookie = 253;
258pub const SYS_epoll_create = 254;
259pub const SYS_epoll_ctl = 255;
260pub const SYS_epoll_wait = 256;
261pub const SYS_remap_file_pages = 257;
262pub const SYS_set_tid_address = 258;
263pub const SYS_timer_create = 259;
264pub const SYS_timer_settime = SYS_timer_create+1;
265pub const SYS_timer_gettime = SYS_timer_create+2;
266pub const SYS_timer_getoverrun = SYS_timer_create+3;
267pub const SYS_timer_delete = SYS_timer_create+4;
268pub const SYS_clock_settime = SYS_timer_create+5;
269pub const SYS_clock_gettime = SYS_timer_create+6;
270pub const SYS_clock_getres = SYS_timer_create+7;
271pub const SYS_clock_nanosleep = SYS_timer_create+8;
272pub const SYS_statfs64 = 268;
273pub const SYS_fstatfs64 = 269;
274pub const SYS_tgkill = 270;
275pub const SYS_utimes = 271;
276pub const SYS_fadvise64_64 = 272;
277pub const SYS_vserver = 273;
278pub const SYS_mbind = 274;
279pub const SYS_get_mempolicy = 275;
280pub const SYS_set_mempolicy = 276;
281pub const SYS_mq_open = 277;
282pub const SYS_mq_unlink = SYS_mq_open+1;
283pub const SYS_mq_timedsend = SYS_mq_open+2;
284pub const SYS_mq_timedreceive = SYS_mq_open+3;
285pub const SYS_mq_notify = SYS_mq_open+4;
286pub const SYS_mq_getsetattr = SYS_mq_open+5;
287pub const SYS_kexec_load = 283;
288pub const SYS_waitid = 284;
289pub const SYS_add_key = 286;
290pub const SYS_request_key = 287;
291pub const SYS_keyctl = 288;
292pub const SYS_ioprio_set = 289;
293pub const SYS_ioprio_get = 290;
294pub const SYS_inotify_init = 291;
295pub const SYS_inotify_add_watch = 292;
296pub const SYS_inotify_rm_watch = 293;
297pub const SYS_migrate_pages = 294;
298pub const SYS_openat = 295;
299pub const SYS_mkdirat = 296;
300pub const SYS_mknodat = 297;
301pub const SYS_fchownat = 298;
302pub const SYS_futimesat = 299;
303pub const SYS_fstatat64 = 300;
304pub const SYS_unlinkat = 301;
305pub const SYS_renameat = 302;
306pub const SYS_linkat = 303;
307pub const SYS_symlinkat = 304;
308pub const SYS_readlinkat = 305;
309pub const SYS_fchmodat = 306;
310pub const SYS_faccessat = 307;
311pub const SYS_pselect6 = 308;
312pub const SYS_ppoll = 309;
313pub const SYS_unshare = 310;
314pub const SYS_set_robust_list = 311;
315pub const SYS_get_robust_list = 312;
316pub const SYS_splice = 313;
317pub const SYS_sync_file_range = 314;
318pub const SYS_tee = 315;
319pub const SYS_vmsplice = 316;
320pub const SYS_move_pages = 317;
321pub const SYS_getcpu = 318;
322pub const SYS_epoll_pwait = 319;
323pub const SYS_utimensat = 320;
324pub const SYS_signalfd = 321;
325pub const SYS_timerfd_create = 322;
326pub const SYS_eventfd = 323;
327pub const SYS_fallocate = 324;
328pub const SYS_timerfd_settime = 325;
329pub const SYS_timerfd_gettime = 326;
330pub const SYS_signalfd4 = 327;
331pub const SYS_eventfd2 = 328;
332pub const SYS_epoll_create1 = 329;
333pub const SYS_dup3 = 330;
334pub const SYS_pipe2 = 331;
335pub const SYS_inotify_init1 = 332;
336pub const SYS_preadv = 333;
337pub const SYS_pwritev = 334;
338pub const SYS_rt_tgsigqueueinfo = 335;
339pub const SYS_perf_event_open = 336;
340pub const SYS_recvmmsg = 337;
341pub const SYS_fanotify_init = 338;
342pub const SYS_fanotify_mark = 339;
343pub const SYS_prlimit64 = 340;
344pub const SYS_name_to_handle_at = 341;
345pub const SYS_open_by_handle_at = 342;
346pub const SYS_clock_adjtime = 343;
347pub const SYS_syncfs = 344;
348pub const SYS_sendmmsg = 345;
349pub const SYS_setns = 346;
350pub const SYS_process_vm_readv = 347;
351pub const SYS_process_vm_writev = 348;
352pub const SYS_kcmp = 349;
353pub const SYS_finit_module = 350;
354pub const SYS_sched_setattr = 351;
355pub const SYS_sched_getattr = 352;
356pub const SYS_renameat2 = 353;
357pub const SYS_seccomp = 354;
358pub const SYS_getrandom = 355;
359pub const SYS_memfd_create = 356;
360pub const SYS_bpf = 357;
361pub const SYS_execveat = 358;
362pub const SYS_socket = 359;
363pub const SYS_socketpair = 360;
364pub const SYS_bind = 361;
365pub const SYS_connect = 362;
366pub const SYS_listen = 363;
367pub const SYS_accept4 = 364;
368pub const SYS_getsockopt = 365;
369pub const SYS_setsockopt = 366;
370pub const SYS_getsockname = 367;
371pub const SYS_getpeername = 368;
372pub const SYS_sendto = 369;
373pub const SYS_sendmsg = 370;
374pub const SYS_recvfrom = 371;
375pub const SYS_recvmsg = 372;
376pub const SYS_shutdown = 373;
377pub const SYS_userfaultfd = 374;
378pub const SYS_membarrier = 375;
379pub const SYS_mlock2 = 376;
380
381
382pub const O_CREAT = 0o100;
383pub const O_EXCL = 0o200;
384pub const O_NOCTTY = 0o400;
385pub const O_TRUNC = 0o1000;
386pub const O_APPEND = 0o2000;
387pub const O_NONBLOCK = 0o4000;
388pub const O_DSYNC = 0o10000;
389pub const O_SYNC = 0o4010000;
390pub const O_RSYNC = 0o4010000;
391pub const O_DIRECTORY = 0o200000;
392pub const O_NOFOLLOW = 0o400000;
393pub const O_CLOEXEC = 0o2000000;
394
395pub const O_ASYNC = 0o20000;
396pub const O_DIRECT = 0o40000;
397pub const O_LARGEFILE = 0o100000;
398pub const O_NOATIME = 0o1000000;
399pub const O_PATH = 0o10000000;
400pub const O_TMPFILE = 0o20200000;
401pub const O_NDELAY = O_NONBLOCK;
402
403pub const F_DUPFD = 0;
404pub const F_GETFD = 1;
405pub const F_SETFD = 2;
406pub const F_GETFL = 3;
407pub const F_SETFL = 4;
408
409pub const F_SETOWN = 8;
410pub const F_GETOWN = 9;
411pub const F_SETSIG = 10;
412pub const F_GETSIG = 11;
413
414pub const F_GETLK = 12;
415pub const F_SETLK = 13;
416pub const F_SETLKW = 14;
417
418pub const F_SETOWN_EX = 15;
419pub const F_GETOWN_EX = 16;
420
421pub const F_GETOWNER_UIDS = 17;
422
423pub inline fn syscall0(number: usize) usize {
424 asm volatile ("int $0x80"
425 : [ret] "={eax}" (-> usize)
426 : [number] "{eax}" (number))
427}
428
429pub inline fn syscall1(number: usize, arg1: usize) usize {
430 asm volatile ("int $0x80"
431 : [ret] "={eax}" (-> usize)
432 : [number] "{eax}" (number),
433 [arg1] "{ebx}" (arg1))
434}
435
436pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize {
437 asm volatile ("int $0x80"
438 : [ret] "={eax}" (-> usize)
439 : [number] "{eax}" (number),
440 [arg1] "{ebx}" (arg1),
441 [arg2] "{ecx}" (arg2))
442}
443
444pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
445 asm volatile ("int $0x80"
446 : [ret] "={eax}" (-> usize)
447 : [number] "{eax}" (number),
448 [arg1] "{ebx}" (arg1),
449 [arg2] "{ecx}" (arg2),
450 [arg3] "{edx}" (arg3))
451}
452
453pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
454 asm volatile ("int $0x80"
455 : [ret] "={eax}" (-> usize)
456 : [number] "{eax}" (number),
457 [arg1] "{ebx}" (arg1),
458 [arg2] "{ecx}" (arg2),
459 [arg3] "{edx}" (arg3),
460 [arg4] "{esi}" (arg4))
461}
462
463pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
464 arg4: usize, arg5: usize) -> usize
465{
466 asm volatile ("int $0x80"
467 : [ret] "={eax}" (-> usize)
468 : [number] "{eax}" (number),
469 [arg1] "{ebx}" (arg1),
470 [arg2] "{ecx}" (arg2),
471 [arg3] "{edx}" (arg3),
472 [arg4] "{esi}" (arg4),
473 [arg5] "{edi}" (arg5))
474}
475
476pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize,
477 arg4: usize, arg5: usize, arg6: usize) -> usize
478{
479 asm volatile ("int $0x80"
480 : [ret] "={eax}" (-> usize)
481 : [number] "{eax}" (number),
482 [arg1] "{ebx}" (arg1),
483 [arg2] "{ecx}" (arg2),
484 [arg3] "{edx}" (arg3),
485 [arg4] "{esi}" (arg4),
486 [arg5] "{edi}" (arg5),
487 [arg6] "{ebp}" (arg6))
488}
489
490pub nakedcc fn restore() void {
491 asm volatile (
492 \\popl %%eax
493 \\movl $119, %%eax
494 \\int $0x80
495 :
496 :
497 : "rcx", "r11")
498}
499
500pub nakedcc fn restore_rt() void {
501 asm volatile ("int $0x80"
502 :
503 : [number] "{eax}" (usize(SYS_rt_sigreturn))
504 : "rcx", "r11")
505}
std/os/linux/index.zig created+796
......@@ -0,0 +1,796 @@
1const std = @import("../../index.zig");
2const assert = std.debug.assert;
3const builtin = @import("builtin");
4const arch = switch (builtin.arch) {
5 builtin.Arch.x86_64 => @import("x86_64.zig"),
6 builtin.Arch.i386 => @import("i386.zig"),
7 else => @compileError("unsupported arch"),
8};
9pub use @import("errno.zig");
10
11pub const PATH_MAX = 4096;
12
13pub const STDIN_FILENO = 0;
14pub const STDOUT_FILENO = 1;
15pub const STDERR_FILENO = 2;
16
17pub const PROT_NONE = 0;
18pub const PROT_READ = 1;
19pub const PROT_WRITE = 2;
20pub const PROT_EXEC = 4;
21pub const PROT_GROWSDOWN = 0x01000000;
22pub const PROT_GROWSUP = 0x02000000;
23
24pub const MAP_FAILED = @maxValue(usize);
25pub const MAP_SHARED = 0x01;
26pub const MAP_PRIVATE = 0x02;
27pub const MAP_TYPE = 0x0f;
28pub const MAP_FIXED = 0x10;
29pub const MAP_ANONYMOUS = 0x20;
30pub const MAP_NORESERVE = 0x4000;
31pub const MAP_GROWSDOWN = 0x0100;
32pub const MAP_DENYWRITE = 0x0800;
33pub const MAP_EXECUTABLE = 0x1000;
34pub const MAP_LOCKED = 0x2000;
35pub const MAP_POPULATE = 0x8000;
36pub const MAP_NONBLOCK = 0x10000;
37pub const MAP_STACK = 0x20000;
38pub const MAP_HUGETLB = 0x40000;
39pub const MAP_FILE = 0;
40
41pub const WNOHANG = 1;
42pub const WUNTRACED = 2;
43pub const WSTOPPED = 2;
44pub const WEXITED = 4;
45pub const WCONTINUED = 8;
46pub const WNOWAIT = 0x1000000;
47
48pub const SA_NOCLDSTOP = 1;
49pub const SA_NOCLDWAIT = 2;
50pub const SA_SIGINFO = 4;
51pub const SA_ONSTACK = 0x08000000;
52pub const SA_RESTART = 0x10000000;
53pub const SA_NODEFER = 0x40000000;
54pub const SA_RESETHAND = 0x80000000;
55pub const SA_RESTORER = 0x04000000;
56
57pub const SIGHUP = 1;
58pub const SIGINT = 2;
59pub const SIGQUIT = 3;
60pub const SIGILL = 4;
61pub const SIGTRAP = 5;
62pub const SIGABRT = 6;
63pub const SIGIOT = SIGABRT;
64pub const SIGBUS = 7;
65pub const SIGFPE = 8;
66pub const SIGKILL = 9;
67pub const SIGUSR1 = 10;
68pub const SIGSEGV = 11;
69pub const SIGUSR2 = 12;
70pub const SIGPIPE = 13;
71pub const SIGALRM = 14;
72pub const SIGTERM = 15;
73pub const SIGSTKFLT = 16;
74pub const SIGCHLD = 17;
75pub const SIGCONT = 18;
76pub const SIGSTOP = 19;
77pub const SIGTSTP = 20;
78pub const SIGTTIN = 21;
79pub const SIGTTOU = 22;
80pub const SIGURG = 23;
81pub const SIGXCPU = 24;
82pub const SIGXFSZ = 25;
83pub const SIGVTALRM = 26;
84pub const SIGPROF = 27;
85pub const SIGWINCH = 28;
86pub const SIGIO = 29;
87pub const SIGPOLL = 29;
88pub const SIGPWR = 30;
89pub const SIGSYS = 31;
90pub const SIGUNUSED = SIGSYS;
91
92pub const O_RDONLY = 0o0;
93pub const O_WRONLY = 0o1;
94pub const O_RDWR = 0o2;
95
96pub const O_CREAT = arch.O_CREAT;
97pub const O_EXCL = arch.O_EXCL;
98pub const O_NOCTTY = arch.O_NOCTTY;
99pub const O_TRUNC = arch.O_TRUNC;
100pub const O_APPEND = arch.O_APPEND;
101pub const O_NONBLOCK = arch.O_NONBLOCK;
102pub const O_DSYNC = arch.O_DSYNC;
103pub const O_SYNC = arch.O_SYNC;
104pub const O_RSYNC = arch.O_RSYNC;
105pub const O_DIRECTORY = arch.O_DIRECTORY;
106pub const O_NOFOLLOW = arch.O_NOFOLLOW;
107pub const O_CLOEXEC = arch.O_CLOEXEC;
108
109pub const O_ASYNC = arch.O_ASYNC;
110pub const O_DIRECT = arch.O_DIRECT;
111pub const O_LARGEFILE = arch.O_LARGEFILE;
112pub const O_NOATIME = arch.O_NOATIME;
113pub const O_PATH = arch.O_PATH;
114pub const O_TMPFILE = arch.O_TMPFILE;
115pub const O_NDELAY = arch.O_NDELAY;
116
117pub const SEEK_SET = 0;
118pub const SEEK_CUR = 1;
119pub const SEEK_END = 2;
120
121pub const SIG_BLOCK = 0;
122pub const SIG_UNBLOCK = 1;
123pub const SIG_SETMASK = 2;
124
125pub const SOCK_STREAM = 1;
126pub const SOCK_DGRAM = 2;
127pub const SOCK_RAW = 3;
128pub const SOCK_RDM = 4;
129pub const SOCK_SEQPACKET = 5;
130pub const SOCK_DCCP = 6;
131pub const SOCK_PACKET = 10;
132pub const SOCK_CLOEXEC = 0o2000000;
133pub const SOCK_NONBLOCK = 0o4000;
134
135
136pub const PROTO_ip = 0o000;
137pub const PROTO_icmp = 0o001;
138pub const PROTO_igmp = 0o002;
139pub const PROTO_ggp = 0o003;
140pub const PROTO_ipencap = 0o004;
141pub const PROTO_st = 0o005;
142pub const PROTO_tcp = 0o006;
143pub const PROTO_egp = 0o010;
144pub const PROTO_pup = 0o014;
145pub const PROTO_udp = 0o021;
146pub const PROTO_hmp = 0o024;
147pub const PROTO_xns_idp = 0o026;
148pub const PROTO_rdp = 0o033;
149pub const PROTO_iso_tp4 = 0o035;
150pub const PROTO_xtp = 0o044;
151pub const PROTO_ddp = 0o045;
152pub const PROTO_idpr_cmtp = 0o046;
153pub const PROTO_ipv6 = 0o051;
154pub const PROTO_ipv6_route = 0o053;
155pub const PROTO_ipv6_frag = 0o054;
156pub const PROTO_idrp = 0o055;
157pub const PROTO_rsvp = 0o056;
158pub const PROTO_gre = 0o057;
159pub const PROTO_esp = 0o062;
160pub const PROTO_ah = 0o063;
161pub const PROTO_skip = 0o071;
162pub const PROTO_ipv6_icmp = 0o072;
163pub const PROTO_ipv6_nonxt = 0o073;
164pub const PROTO_ipv6_opts = 0o074;
165pub const PROTO_rspf = 0o111;
166pub const PROTO_vmtp = 0o121;
167pub const PROTO_ospf = 0o131;
168pub const PROTO_ipip = 0o136;
169pub const PROTO_encap = 0o142;
170pub const PROTO_pim = 0o147;
171pub const PROTO_raw = 0o377;
172
173pub const PF_UNSPEC = 0;
174pub const PF_LOCAL = 1;
175pub const PF_UNIX = PF_LOCAL;
176pub const PF_FILE = PF_LOCAL;
177pub const PF_INET = 2;
178pub const PF_AX25 = 3;
179pub const PF_IPX = 4;
180pub const PF_APPLETALK = 5;
181pub const PF_NETROM = 6;
182pub const PF_BRIDGE = 7;
183pub const PF_ATMPVC = 8;
184pub const PF_X25 = 9;
185pub const PF_INET6 = 10;
186pub const PF_ROSE = 11;
187pub const PF_DECnet = 12;
188pub const PF_NETBEUI = 13;
189pub const PF_SECURITY = 14;
190pub const PF_KEY = 15;
191pub const PF_NETLINK = 16;
192pub const PF_ROUTE = PF_NETLINK;
193pub const PF_PACKET = 17;
194pub const PF_ASH = 18;
195pub const PF_ECONET = 19;
196pub const PF_ATMSVC = 20;
197pub const PF_RDS = 21;
198pub const PF_SNA = 22;
199pub const PF_IRDA = 23;
200pub const PF_PPPOX = 24;
201pub const PF_WANPIPE = 25;
202pub const PF_LLC = 26;
203pub const PF_IB = 27;
204pub const PF_MPLS = 28;
205pub const PF_CAN = 29;
206pub const PF_TIPC = 30;
207pub const PF_BLUETOOTH = 31;
208pub const PF_IUCV = 32;
209pub const PF_RXRPC = 33;
210pub const PF_ISDN = 34;
211pub const PF_PHONET = 35;
212pub const PF_IEEE802154 = 36;
213pub const PF_CAIF = 37;
214pub const PF_ALG = 38;
215pub const PF_NFC = 39;
216pub const PF_VSOCK = 40;
217pub const PF_MAX = 41;
218
219pub const AF_UNSPEC = PF_UNSPEC;
220pub const AF_LOCAL = PF_LOCAL;
221pub const AF_UNIX = AF_LOCAL;
222pub const AF_FILE = AF_LOCAL;
223pub const AF_INET = PF_INET;
224pub const AF_AX25 = PF_AX25;
225pub const AF_IPX = PF_IPX;
226pub const AF_APPLETALK = PF_APPLETALK;
227pub const AF_NETROM = PF_NETROM;
228pub const AF_BRIDGE = PF_BRIDGE;
229pub const AF_ATMPVC = PF_ATMPVC;
230pub const AF_X25 = PF_X25;
231pub const AF_INET6 = PF_INET6;
232pub const AF_ROSE = PF_ROSE;
233pub const AF_DECnet = PF_DECnet;
234pub const AF_NETBEUI = PF_NETBEUI;
235pub const AF_SECURITY = PF_SECURITY;
236pub const AF_KEY = PF_KEY;
237pub const AF_NETLINK = PF_NETLINK;
238pub const AF_ROUTE = PF_ROUTE;
239pub const AF_PACKET = PF_PACKET;
240pub const AF_ASH = PF_ASH;
241pub const AF_ECONET = PF_ECONET;
242pub const AF_ATMSVC = PF_ATMSVC;
243pub const AF_RDS = PF_RDS;
244pub const AF_SNA = PF_SNA;
245pub const AF_IRDA = PF_IRDA;
246pub const AF_PPPOX = PF_PPPOX;
247pub const AF_WANPIPE = PF_WANPIPE;
248pub const AF_LLC = PF_LLC;
249pub const AF_IB = PF_IB;
250pub const AF_MPLS = PF_MPLS;
251pub const AF_CAN = PF_CAN;
252pub const AF_TIPC = PF_TIPC;
253pub const AF_BLUETOOTH = PF_BLUETOOTH;
254pub const AF_IUCV = PF_IUCV;
255pub const AF_RXRPC = PF_RXRPC;
256pub const AF_ISDN = PF_ISDN;
257pub const AF_PHONET = PF_PHONET;
258pub const AF_IEEE802154 = PF_IEEE802154;
259pub const AF_CAIF = PF_CAIF;
260pub const AF_ALG = PF_ALG;
261pub const AF_NFC = PF_NFC;
262pub const AF_VSOCK = PF_VSOCK;
263pub const AF_MAX = PF_MAX;
264
265pub const DT_UNKNOWN = 0;
266pub const DT_FIFO = 1;
267pub const DT_CHR = 2;
268pub const DT_DIR = 4;
269pub const DT_BLK = 6;
270pub const DT_REG = 8;
271pub const DT_LNK = 10;
272pub const DT_SOCK = 12;
273pub const DT_WHT = 14;
274
275
276pub const TCGETS = 0x5401;
277pub const TCSETS = 0x5402;
278pub const TCSETSW = 0x5403;
279pub const TCSETSF = 0x5404;
280pub const TCGETA = 0x5405;
281pub const TCSETA = 0x5406;
282pub const TCSETAW = 0x5407;
283pub const TCSETAF = 0x5408;
284pub const TCSBRK = 0x5409;
285pub const TCXONC = 0x540A;
286pub const TCFLSH = 0x540B;
287pub const TIOCEXCL = 0x540C;
288pub const TIOCNXCL = 0x540D;
289pub const TIOCSCTTY = 0x540E;
290pub const TIOCGPGRP = 0x540F;
291pub const TIOCSPGRP = 0x5410;
292pub const TIOCOUTQ = 0x5411;
293pub const TIOCSTI = 0x5412;
294pub const TIOCGWINSZ = 0x5413;
295pub const TIOCSWINSZ = 0x5414;
296pub const TIOCMGET = 0x5415;
297pub const TIOCMBIS = 0x5416;
298pub const TIOCMBIC = 0x5417;
299pub const TIOCMSET = 0x5418;
300pub const TIOCGSOFTCAR = 0x5419;
301pub const TIOCSSOFTCAR = 0x541A;
302pub const FIONREAD = 0x541B;
303pub const TIOCINQ = FIONREAD;
304pub const TIOCLINUX = 0x541C;
305pub const TIOCCONS = 0x541D;
306pub const TIOCGSERIAL = 0x541E;
307pub const TIOCSSERIAL = 0x541F;
308pub const TIOCPKT = 0x5420;
309pub const FIONBIO = 0x5421;
310pub const TIOCNOTTY = 0x5422;
311pub const TIOCSETD = 0x5423;
312pub const TIOCGETD = 0x5424;
313pub const TCSBRKP = 0x5425;
314pub const TIOCSBRK = 0x5427;
315pub const TIOCCBRK = 0x5428;
316pub const TIOCGSID = 0x5429;
317pub const TIOCGRS485 = 0x542E;
318pub const TIOCSRS485 = 0x542F;
319pub const TIOCGPTN = 0x80045430;
320pub const TIOCSPTLCK = 0x40045431;
321pub const TIOCGDEV = 0x80045432;
322pub const TCGETX = 0x5432;
323pub const TCSETX = 0x5433;
324pub const TCSETXF = 0x5434;
325pub const TCSETXW = 0x5435;
326pub const TIOCSIG = 0x40045436;
327pub const TIOCVHANGUP = 0x5437;
328pub const TIOCGPKT = 0x80045438;
329pub const TIOCGPTLCK = 0x80045439;
330pub const TIOCGEXCL = 0x80045440;
331
332pub const EPOLL_CTL_ADD = 1;
333pub const EPOLL_CTL_DEL = 2;
334pub const EPOLL_CTL_MOD = 3;
335
336pub const EPOLLIN = 0x001;
337pub const EPOLLPRI = 0x002;
338pub const EPOLLOUT = 0x004;
339pub const EPOLLRDNORM = 0x040;
340pub const EPOLLRDBAND = 0x080;
341pub const EPOLLWRNORM = 0x100;
342pub const EPOLLWRBAND = 0x200;
343pub const EPOLLMSG = 0x400;
344pub const EPOLLERR = 0x008;
345pub const EPOLLHUP = 0x010;
346pub const EPOLLRDHUP = 0x2000;
347pub const EPOLLEXCLUSIVE = (u32(1) << 28);
348pub const EPOLLWAKEUP = (u32(1) << 29);
349pub const EPOLLONESHOT = (u32(1) << 30);
350pub const EPOLLET = (u32(1) << 31);
351
352pub const CLOCK_REALTIME = 0;
353pub const CLOCK_MONOTONIC = 1;
354pub const CLOCK_PROCESS_CPUTIME_ID = 2;
355pub const CLOCK_THREAD_CPUTIME_ID = 3;
356pub const CLOCK_MONOTONIC_RAW = 4;
357pub const CLOCK_REALTIME_COARSE = 5;
358pub const CLOCK_MONOTONIC_COARSE = 6;
359pub const CLOCK_BOOTTIME = 7;
360pub const CLOCK_REALTIME_ALARM = 8;
361pub const CLOCK_BOOTTIME_ALARM = 9;
362pub const CLOCK_SGI_CYCLE = 10;
363pub const CLOCK_TAI = 11;
364
365pub const TFD_NONBLOCK = O_NONBLOCK;
366pub const TFD_CLOEXEC = O_CLOEXEC;
367
368pub const TFD_TIMER_ABSTIME = 1;
369pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1);
370
371fn unsigned(s: i32) u32 { return @bitCast(u32, s); }
372fn signed(s: u32) i32 { return @bitCast(i32, s); }
373pub fn WEXITSTATUS(s: i32) i32 { return signed((unsigned(s) & 0xff00) >> 8); }
374pub fn WTERMSIG(s: i32) i32 { return signed(unsigned(s) & 0x7f); }
375pub fn WSTOPSIG(s: i32) i32 { return WEXITSTATUS(s); }
376pub fn WIFEXITED(s: i32) bool { return WTERMSIG(s) == 0; }
377pub fn WIFSTOPPED(s: i32) bool { return (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00; }
378pub fn WIFSIGNALED(s: i32) bool { return (unsigned(s)&0xffff)-%1 < 0xff; }
379
380
381pub const winsize = extern struct {
382 ws_row: u16,
383 ws_col: u16,
384 ws_xpixel: u16,
385 ws_ypixel: u16,
386};
387
388/// Get the errno from a syscall return value, or 0 for no error.
389pub fn getErrno(r: usize) usize {
390 const signed_r = @bitCast(isize, r);
391 return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
392}
393
394pub fn dup2(old: i32, new: i32) usize {
395 return arch.syscall2(arch.SYS_dup2, usize(old), usize(new));
396}
397
398pub fn chdir(path: &const u8) usize {
399 return arch.syscall1(arch.SYS_chdir, @ptrToInt(path));
400}
401
402pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize {
403 return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp));
404}
405
406pub fn fork() usize {
407 return arch.syscall0(arch.SYS_fork);
408}
409
410pub fn getcwd(buf: &u8, size: usize) usize {
411 return arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size);
412}
413
414pub fn getdents(fd: i32, dirp: &u8, count: usize) usize {
415 return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count);
416}
417
418pub fn isatty(fd: i32) bool {
419 var wsz: winsize = undefined;
420 return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
421}
422
423pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize {
424 return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
425}
426
427pub fn mkdir(path: &const u8, mode: u32) usize {
428 return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode);
429}
430
431pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) usize {
432 return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd),
433 @bitCast(usize, offset));
434}
435
436pub fn munmap(address: &u8, length: usize) usize {
437 return arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length);
438}
439
440pub fn read(fd: i32, buf: &u8, count: usize) usize {
441 return arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count);
442}
443
444pub fn rmdir(path: &const u8) usize {
445 return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path));
446}
447
448pub fn symlink(existing: &const u8, new: &const u8) usize {
449 return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new));
450}
451
452pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize {
453 return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset);
454}
455
456pub fn pipe(fd: &[2]i32) usize {
457 return pipe2(fd, 0);
458}
459
460pub fn pipe2(fd: &[2]i32, flags: usize) usize {
461 return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags);
462}
463
464pub fn write(fd: i32, buf: &const u8, count: usize) usize {
465 return arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count);
466}
467
468pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) usize {
469 return arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset);
470}
471
472pub fn rename(old: &const u8, new: &const u8) usize {
473 return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new));
474}
475
476pub fn open(path: &const u8, flags: u32, perm: usize) usize {
477 return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm);
478}
479
480pub fn create(path: &const u8, perm: usize) usize {
481 return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm);
482}
483
484pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize {
485 return arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode);
486}
487
488pub fn close(fd: i32) usize {
489 return arch.syscall1(arch.SYS_close, usize(fd));
490}
491
492pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
493 return arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos);
494}
495
496pub fn exit(status: i32) noreturn {
497 _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status)));
498 unreachable;
499}
500
501pub fn getrandom(buf: &u8, count: usize, flags: u32) usize {
502 return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags));
503}
504
505pub fn kill(pid: i32, sig: i32) usize {
506 return arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig));
507}
508
509pub fn unlink(path: &const u8) usize {
510 return arch.syscall1(arch.SYS_unlink, @ptrToInt(path));
511}
512
513pub fn waitpid(pid: i32, status: &i32, options: i32) usize {
514 return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
515}
516
517pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
518 return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));
519}
520
521pub fn setuid(uid: u32) usize {
522 return arch.syscall1(arch.SYS_setuid, uid);
523}
524
525pub fn setgid(gid: u32) usize {
526 return arch.syscall1(arch.SYS_setgid, gid);
527}
528
529pub fn setreuid(ruid: u32, euid: u32) usize {
530 return arch.syscall2(arch.SYS_setreuid, ruid, euid);
531}
532
533pub fn setregid(rgid: u32, egid: u32) usize {
534 return arch.syscall2(arch.SYS_setregid, rgid, egid);
535}
536
537pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize {
538 return arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8);
539}
540
541pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
542 assert(sig >= 1);
543 assert(sig != SIGKILL);
544 assert(sig != SIGSTOP);
545 var ksa = k_sigaction {
546 .handler = act.handler,
547 .flags = act.flags | SA_RESTORER,
548 .mask = undefined,
549 .restorer = @ptrCast(extern fn()void, arch.restore_rt),
550 };
551 var ksa_old: k_sigaction = undefined;
552 @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8);
553 const result = arch.syscall4(arch.SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask)));
554 const err = getErrno(result);
555 if (err != 0) {
556 return result;
557 }
558 if (oact) |old| {
559 old.handler = ksa_old.handler;
560 old.flags = @truncate(u32, ksa_old.flags);
561 @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask)));
562 }
563 return 0;
564}
565
566const NSIG = 65;
567const sigset_t = [128 / @sizeOf(usize)]usize;
568const all_mask = []usize{@maxValue(usize)};
569const app_mask = []usize{0xfffffffc7fffffff};
570
571const k_sigaction = extern struct {
572 handler: extern fn(i32)void,
573 flags: usize,
574 restorer: extern fn()void,
575 mask: [2]u32,
576};
577
578/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
579pub const Sigaction = struct {
580 handler: extern fn(i32)void,
581 mask: sigset_t,
582 flags: u32,
583};
584
585pub const SIG_ERR = @intToPtr(extern fn(i32)void, @maxValue(usize));
586pub const SIG_DFL = @intToPtr(extern fn(i32)void, 0);
587pub const SIG_IGN = @intToPtr(extern fn(i32)void, 1);
588pub const empty_sigset = []usize{0} ** sigset_t.len;
589
590pub fn raise(sig: i32) usize {
591 var set: sigset_t = undefined;
592 blockAppSignals(&set);
593 const tid = i32(arch.syscall0(arch.SYS_gettid));
594 const ret = arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig));
595 restoreSignals(&set);
596 return ret;
597}
598
599fn blockAllSignals(set: &sigset_t) void {
600 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8);
601}
602
603fn blockAppSignals(set: &sigset_t) void {
604 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8);
605}
606
607fn restoreSignals(set: &sigset_t) void {
608 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8);
609}
610
611pub fn sigaddset(set: &sigset_t, sig: u6) void {
612 const s = sig - 1;
613 (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
614}
615
616pub fn sigismember(set: &const sigset_t, sig: u6) bool {
617 const s = sig - 1;
618 return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
619}
620
621
622pub const sa_family_t = u16;
623pub const socklen_t = u32;
624pub const in_addr = u32;
625pub const in6_addr = [16]u8;
626
627pub const sockaddr = extern struct {
628 family: sa_family_t,
629 port: u16,
630 data: [12]u8,
631};
632
633pub const sockaddr_in = extern struct {
634 family: sa_family_t,
635 port: u16,
636 addr: in_addr,
637 zero: [8]u8,
638};
639
640pub const sockaddr_in6 = extern struct {
641 family: sa_family_t,
642 port: u16,
643 flowinfo: u32,
644 addr: in6_addr,
645 scope_id: u32,
646};
647
648pub const iovec = extern struct {
649 iov_base: &u8,
650 iov_len: usize,
651};
652
653pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
654 return arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len));
655}
656
657pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
658 return arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len));
659}
660
661pub fn socket(domain: i32, socket_type: i32, protocol: i32) usize {
662 return arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol));
663}
664
665pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) usize {
666 return arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen));
667}
668
669pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) usize {
670 return arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen));
671}
672
673pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) usize {
674 return arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags);
675}
676
677pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) usize {
678 return arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len));
679}
680
681pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) usize {
682 return arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags);
683}
684
685pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32,
686 noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize
687{
688 return arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
689}
690
691pub fn shutdown(fd: i32, how: i32) usize {
692 return arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how));
693}
694
695pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) usize {
696 return arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len));
697}
698
699pub fn listen(fd: i32, backlog: i32) usize {
700 return arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog));
701}
702
703pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) usize {
704 return arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen));
705}
706
707pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
708 return arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0]));
709}
710
711pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
712 return accept4(fd, addr, len, 0);
713}
714
715pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) usize {
716 return arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags);
717}
718
719// error NameTooLong;
720// error SystemResources;
721// error Io;
722//
723// pub fn if_nametoindex(name: []u8) %u32 {
724// var ifr: ifreq = undefined;
725//
726// if (name.len >= ifr.ifr_name.len) {
727// return error.NameTooLong;
728// }
729//
730// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
731// const socket_err = getErrno(socket_ret);
732// if (socket_err > 0) {
733// return error.SystemResources;
734// }
735// const socket_fd = i32(socket_ret);
736// @memcpy(&ifr.ifr_name[0], &name[0], name.len);
737// ifr.ifr_name[name.len] = 0;
738// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr);
739// close(socket_fd);
740// const ioctl_err = getErrno(ioctl_ret);
741// if (ioctl_err > 0) {
742// return error.Io;
743// }
744// return ifr.ifr_ifindex;
745// }
746
747pub const Stat = arch.Stat;
748pub const timespec = arch.timespec;
749
750pub fn fstat(fd: i32, stat_buf: &Stat) usize {
751 return arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf));
752}
753
754pub const epoll_data = u64;
755
756pub const epoll_event = extern struct {
757 events: u32,
758 data: epoll_data
759};
760
761pub fn epoll_create() usize {
762 return arch.syscall1(arch.SYS_epoll_create, usize(1));
763}
764
765pub fn epoll_ctl(epoll_fd: i32, op: i32, fd: i32, ev: &epoll_event) usize {
766 return arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev));
767}
768
769pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: i32, timeout: i32) usize {
770 return arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout));
771}
772
773pub fn timerfd_create(clockid: i32, flags: u32) usize {
774 return arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags));
775}
776
777pub const itimerspec = extern struct {
778 it_interval: timespec,
779 it_value: timespec
780};
781
782pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize {
783 return arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value));
784}
785
786pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) usize {
787 return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value));
788}
789
790test "import linux test" {
791 // TODO lazy analysis should prevent this test from being compiled on windows, but
792 // it is still compiled on windows
793 if (builtin.os == builtin.Os.linux) {
794 _ = @import("test.zig");
795 }
796}
std/os/linux/test.zig created+38
......@@ -0,0 +1,38 @@
1const std = @import("../../index.zig");
2const linux = std.os.linux;
3const assert = std.debug.assert;
4
5test "timer" {
6 const epoll_fd = linux.epoll_create();
7 var err = linux.getErrno(epoll_fd);
8 assert(err == 0);
9
10 const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0);
11 assert(linux.getErrno(timer_fd) == 0);
12
13 const time_interval = linux.timespec {
14 .tv_sec = 0,
15 .tv_nsec = 2000000
16 };
17
18 const new_time = linux.itimerspec {
19 .it_interval = time_interval,
20 .it_value = time_interval
21 };
22
23 err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null);
24 assert(err == 0);
25
26 var event = linux.epoll_event {
27 .events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET,
28 .data = 0
29 };
30
31 err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event);
32 assert(err == 0);
33
34 const events_one: linux.epoll_event = undefined;
35 var events = []linux.epoll_event{events_one} ** 8;
36
37 err = linux.epoll_wait(i32(epoll_fd), &events[0], 8, -1);
38}
std/os/linux/x86_64.zig created+490
......@@ -0,0 +1,490 @@
1const std = @import("../../index.zig");
2const linux = std.os.linux;
3const socklen_t = linux.socklen_t;
4const iovec = linux.iovec;
5
6pub const SYS_read = 0;
7pub const SYS_write = 1;
8pub const SYS_open = 2;
9pub const SYS_close = 3;
10pub const SYS_stat = 4;
11pub const SYS_fstat = 5;
12pub const SYS_lstat = 6;
13pub const SYS_poll = 7;
14pub const SYS_lseek = 8;
15pub const SYS_mmap = 9;
16pub const SYS_mprotect = 10;
17pub const SYS_munmap = 11;
18pub const SYS_brk = 12;
19pub const SYS_rt_sigaction = 13;
20pub const SYS_rt_sigprocmask = 14;
21pub const SYS_rt_sigreturn = 15;
22pub const SYS_ioctl = 16;
23pub const SYS_pread = 17;
24pub const SYS_pwrite = 18;
25pub const SYS_readv = 19;
26pub const SYS_writev = 20;
27pub const SYS_access = 21;
28pub const SYS_pipe = 22;
29pub const SYS_select = 23;
30pub const SYS_sched_yield = 24;
31pub const SYS_mremap = 25;
32pub const SYS_msync = 26;
33pub const SYS_mincore = 27;
34pub const SYS_madvise = 28;
35pub const SYS_shmget = 29;
36pub const SYS_shmat = 30;
37pub const SYS_shmctl = 31;
38pub const SYS_dup = 32;
39pub const SYS_dup2 = 33;
40pub const SYS_pause = 34;
41pub const SYS_nanosleep = 35;
42pub const SYS_getitimer = 36;
43pub const SYS_alarm = 37;
44pub const SYS_setitimer = 38;
45pub const SYS_getpid = 39;
46pub const SYS_sendfile = 40;
47pub const SYS_socket = 41;
48pub const SYS_connect = 42;
49pub const SYS_accept = 43;
50pub const SYS_sendto = 44;
51pub const SYS_recvfrom = 45;
52pub const SYS_sendmsg = 46;
53pub const SYS_recvmsg = 47;
54pub const SYS_shutdown = 48;
55pub const SYS_bind = 49;
56pub const SYS_listen = 50;
57pub const SYS_getsockname = 51;
58pub const SYS_getpeername = 52;
59pub const SYS_socketpair = 53;
60pub const SYS_setsockopt = 54;
61pub const SYS_getsockopt = 55;
62pub const SYS_clone = 56;
63pub const SYS_fork = 57;
64pub const SYS_vfork = 58;
65pub const SYS_execve = 59;
66pub const SYS_exit = 60;
67pub const SYS_wait4 = 61;
68pub const SYS_kill = 62;
69pub const SYS_uname = 63;
70pub const SYS_semget = 64;
71pub const SYS_semop = 65;
72pub const SYS_semctl = 66;
73pub const SYS_shmdt = 67;
74pub const SYS_msgget = 68;
75pub const SYS_msgsnd = 69;
76pub const SYS_msgrcv = 70;
77pub const SYS_msgctl = 71;
78pub const SYS_fcntl = 72;
79pub const SYS_flock = 73;
80pub const SYS_fsync = 74;
81pub const SYS_fdatasync = 75;
82pub const SYS_truncate = 76;
83pub const SYS_ftruncate = 77;
84pub const SYS_getdents = 78;
85pub const SYS_getcwd = 79;
86pub const SYS_chdir = 80;
87pub const SYS_fchdir = 81;
88pub const SYS_rename = 82;
89pub const SYS_mkdir = 83;
90pub const SYS_rmdir = 84;
91pub const SYS_creat = 85;
92pub const SYS_link = 86;
93pub const SYS_unlink = 87;
94pub const SYS_symlink = 88;
95pub const SYS_readlink = 89;
96pub const SYS_chmod = 90;
97pub const SYS_fchmod = 91;
98pub const SYS_chown = 92;
99pub const SYS_fchown = 93;
100pub const SYS_lchown = 94;
101pub const SYS_umask = 95;
102pub const SYS_gettimeofday = 96;
103pub const SYS_getrlimit = 97;
104pub const SYS_getrusage = 98;
105pub const SYS_sysinfo = 99;
106pub const SYS_times = 100;
107pub const SYS_ptrace = 101;
108pub const SYS_getuid = 102;
109pub const SYS_syslog = 103;
110pub const SYS_getgid = 104;
111pub const SYS_setuid = 105;
112pub const SYS_setgid = 106;
113pub const SYS_geteuid = 107;
114pub const SYS_getegid = 108;
115pub const SYS_setpgid = 109;
116pub const SYS_getppid = 110;
117pub const SYS_getpgrp = 111;
118pub const SYS_setsid = 112;
119pub const SYS_setreuid = 113;
120pub const SYS_setregid = 114;
121pub const SYS_getgroups = 115;
122pub const SYS_setgroups = 116;
123pub const SYS_setresuid = 117;
124pub const SYS_getresuid = 118;
125pub const SYS_setresgid = 119;
126pub const SYS_getresgid = 120;
127pub const SYS_getpgid = 121;
128pub const SYS_setfsuid = 122;
129pub const SYS_setfsgid = 123;
130pub const SYS_getsid = 124;
131pub const SYS_capget = 125;
132pub const SYS_capset = 126;
133pub const SYS_rt_sigpending = 127;
134pub const SYS_rt_sigtimedwait = 128;
135pub const SYS_rt_sigqueueinfo = 129;
136pub const SYS_rt_sigsuspend = 130;
137pub const SYS_sigaltstack = 131;
138pub const SYS_utime = 132;
139pub const SYS_mknod = 133;
140pub const SYS_uselib = 134;
141pub const SYS_personality = 135;
142pub const SYS_ustat = 136;
143pub const SYS_statfs = 137;
144pub const SYS_fstatfs = 138;
145pub const SYS_sysfs = 139;
146pub const SYS_getpriority = 140;
147pub const SYS_setpriority = 141;
148pub const SYS_sched_setparam = 142;
149pub const SYS_sched_getparam = 143;
150pub const SYS_sched_setscheduler = 144;
151pub const SYS_sched_getscheduler = 145;
152pub const SYS_sched_get_priority_max = 146;
153pub const SYS_sched_get_priority_min = 147;
154pub const SYS_sched_rr_get_interval = 148;
155pub const SYS_mlock = 149;
156pub const SYS_munlock = 150;
157pub const SYS_mlockall = 151;
158pub const SYS_munlockall = 152;
159pub const SYS_vhangup = 153;
160pub const SYS_modify_ldt = 154;
161pub const SYS_pivot_root = 155;
162pub const SYS__sysctl = 156;
163pub const SYS_prctl = 157;
164pub const SYS_arch_prctl = 158;
165pub const SYS_adjtimex = 159;
166pub const SYS_setrlimit = 160;
167pub const SYS_chroot = 161;
168pub const SYS_sync = 162;
169pub const SYS_acct = 163;
170pub const SYS_settimeofday = 164;
171pub const SYS_mount = 165;
172pub const SYS_umount2 = 166;
173pub const SYS_swapon = 167;
174pub const SYS_swapoff = 168;
175pub const SYS_reboot = 169;
176pub const SYS_sethostname = 170;
177pub const SYS_setdomainname = 171;
178pub const SYS_iopl = 172;
179pub const SYS_ioperm = 173;
180pub const SYS_create_module = 174;
181pub const SYS_init_module = 175;
182pub const SYS_delete_module = 176;
183pub const SYS_get_kernel_syms = 177;
184pub const SYS_query_module = 178;
185pub const SYS_quotactl = 179;
186pub const SYS_nfsservctl = 180;
187pub const SYS_getpmsg = 181;
188pub const SYS_putpmsg = 182;
189pub const SYS_afs_syscall = 183;
190pub const SYS_tuxcall = 184;
191pub const SYS_security = 185;
192pub const SYS_gettid = 186;
193pub const SYS_readahead = 187;
194pub const SYS_setxattr = 188;
195pub const SYS_lsetxattr = 189;
196pub const SYS_fsetxattr = 190;
197pub const SYS_getxattr = 191;
198pub const SYS_lgetxattr = 192;
199pub const SYS_fgetxattr = 193;
200pub const SYS_listxattr = 194;
201pub const SYS_llistxattr = 195;
202pub const SYS_flistxattr = 196;
203pub const SYS_removexattr = 197;
204pub const SYS_lremovexattr = 198;
205pub const SYS_fremovexattr = 199;
206pub const SYS_tkill = 200;
207pub const SYS_time = 201;
208pub const SYS_futex = 202;
209pub const SYS_sched_setaffinity = 203;
210pub const SYS_sched_getaffinity = 204;
211pub const SYS_set_thread_area = 205;
212pub const SYS_io_setup = 206;
213pub const SYS_io_destroy = 207;
214pub const SYS_io_getevents = 208;
215pub const SYS_io_submit = 209;
216pub const SYS_io_cancel = 210;
217pub const SYS_get_thread_area = 211;
218pub const SYS_lookup_dcookie = 212;
219pub const SYS_epoll_create = 213;
220pub const SYS_epoll_ctl_old = 214;
221pub const SYS_epoll_wait_old = 215;
222pub const SYS_remap_file_pages = 216;
223pub const SYS_getdents64 = 217;
224pub const SYS_set_tid_address = 218;
225pub const SYS_restart_syscall = 219;
226pub const SYS_semtimedop = 220;
227pub const SYS_fadvise64 = 221;
228pub const SYS_timer_create = 222;
229pub const SYS_timer_settime = 223;
230pub const SYS_timer_gettime = 224;
231pub const SYS_timer_getoverrun = 225;
232pub const SYS_timer_delete = 226;
233pub const SYS_clock_settime = 227;
234pub const SYS_clock_gettime = 228;
235pub const SYS_clock_getres = 229;
236pub const SYS_clock_nanosleep = 230;
237pub const SYS_exit_group = 231;
238pub const SYS_epoll_wait = 232;
239pub const SYS_epoll_ctl = 233;
240pub const SYS_tgkill = 234;
241pub const SYS_utimes = 235;
242pub const SYS_vserver = 236;
243pub const SYS_mbind = 237;
244pub const SYS_set_mempolicy = 238;
245pub const SYS_get_mempolicy = 239;
246pub const SYS_mq_open = 240;
247pub const SYS_mq_unlink = 241;
248pub const SYS_mq_timedsend = 242;
249pub const SYS_mq_timedreceive = 243;
250pub const SYS_mq_notify = 244;
251pub const SYS_mq_getsetattr = 245;
252pub const SYS_kexec_load = 246;
253pub const SYS_waitid = 247;
254pub const SYS_add_key = 248;
255pub const SYS_request_key = 249;
256pub const SYS_keyctl = 250;
257pub const SYS_ioprio_set = 251;
258pub const SYS_ioprio_get = 252;
259pub const SYS_inotify_init = 253;
260pub const SYS_inotify_add_watch = 254;
261pub const SYS_inotify_rm_watch = 255;
262pub const SYS_migrate_pages = 256;
263pub const SYS_openat = 257;
264pub const SYS_mkdirat = 258;
265pub const SYS_mknodat = 259;
266pub const SYS_fchownat = 260;
267pub const SYS_futimesat = 261;
268pub const SYS_newfstatat = 262;
269pub const SYS_unlinkat = 263;
270pub const SYS_renameat = 264;
271pub const SYS_linkat = 265;
272pub const SYS_symlinkat = 266;
273pub const SYS_readlinkat = 267;
274pub const SYS_fchmodat = 268;
275pub const SYS_faccessat = 269;
276pub const SYS_pselect6 = 270;
277pub const SYS_ppoll = 271;
278pub const SYS_unshare = 272;
279pub const SYS_set_robust_list = 273;
280pub const SYS_get_robust_list = 274;
281pub const SYS_splice = 275;
282pub const SYS_tee = 276;
283pub const SYS_sync_file_range = 277;
284pub const SYS_vmsplice = 278;
285pub const SYS_move_pages = 279;
286pub const SYS_utimensat = 280;
287pub const SYS_epoll_pwait = 281;
288pub const SYS_signalfd = 282;
289pub const SYS_timerfd_create = 283;
290pub const SYS_eventfd = 284;
291pub const SYS_fallocate = 285;
292pub const SYS_timerfd_settime = 286;
293pub const SYS_timerfd_gettime = 287;
294pub const SYS_accept4 = 288;
295pub const SYS_signalfd4 = 289;
296pub const SYS_eventfd2 = 290;
297pub const SYS_epoll_create1 = 291;
298pub const SYS_dup3 = 292;
299pub const SYS_pipe2 = 293;
300pub const SYS_inotify_init1 = 294;
301pub const SYS_preadv = 295;
302pub const SYS_pwritev = 296;
303pub const SYS_rt_tgsigqueueinfo = 297;
304pub const SYS_perf_event_open = 298;
305pub const SYS_recvmmsg = 299;
306pub const SYS_fanotify_init = 300;
307pub const SYS_fanotify_mark = 301;
308pub const SYS_prlimit64 = 302;
309pub const SYS_name_to_handle_at = 303;
310pub const SYS_open_by_handle_at = 304;
311pub const SYS_clock_adjtime = 305;
312pub const SYS_syncfs = 306;
313pub const SYS_sendmmsg = 307;
314pub const SYS_setns = 308;
315pub const SYS_getcpu = 309;
316pub const SYS_process_vm_readv = 310;
317pub const SYS_process_vm_writev = 311;
318pub const SYS_kcmp = 312;
319pub const SYS_finit_module = 313;
320pub const SYS_sched_setattr = 314;
321pub const SYS_sched_getattr = 315;
322pub const SYS_renameat2 = 316;
323pub const SYS_seccomp = 317;
324pub const SYS_getrandom = 318;
325pub const SYS_memfd_create = 319;
326pub const SYS_kexec_file_load = 320;
327pub const SYS_bpf = 321;
328pub const SYS_execveat = 322;
329pub const SYS_userfaultfd = 323;
330pub const SYS_membarrier = 324;
331pub const SYS_mlock2 = 325;
332
333pub const O_CREAT = 0o100;
334pub const O_EXCL = 0o200;
335pub const O_NOCTTY = 0o400;
336pub const O_TRUNC = 0o1000;
337pub const O_APPEND = 0o2000;
338pub const O_NONBLOCK = 0o4000;
339pub const O_DSYNC = 0o10000;
340pub const O_SYNC = 0o4010000;
341pub const O_RSYNC = 0o4010000;
342pub const O_DIRECTORY = 0o200000;
343pub const O_NOFOLLOW = 0o400000;
344pub const O_CLOEXEC = 0o2000000;
345
346pub const O_ASYNC = 0o20000;
347pub const O_DIRECT = 0o40000;
348pub const O_LARGEFILE = 0;
349pub const O_NOATIME = 0o1000000;
350pub const O_PATH = 0o10000000;
351pub const O_TMPFILE = 0o20200000;
352pub const O_NDELAY = O_NONBLOCK;
353
354pub const F_DUPFD = 0;
355pub const F_GETFD = 1;
356pub const F_SETFD = 2;
357pub const F_GETFL = 3;
358pub const F_SETFL = 4;
359
360pub const F_SETOWN = 8;
361pub const F_GETOWN = 9;
362pub const F_SETSIG = 10;
363pub const F_GETSIG = 11;
364
365pub const F_GETLK = 5;
366pub const F_SETLK = 6;
367pub const F_SETLKW = 7;
368
369pub const F_SETOWN_EX = 15;
370pub const F_GETOWN_EX = 16;
371
372pub const F_GETOWNER_UIDS = 17;
373
374pub fn syscall0(number: usize) usize {
375 return asm volatile ("syscall"
376 : [ret] "={rax}" (-> usize)
377 : [number] "{rax}" (number)
378 : "rcx", "r11");
379}
380
381pub fn syscall1(number: usize, arg1: usize) usize {
382 return asm volatile ("syscall"
383 : [ret] "={rax}" (-> usize)
384 : [number] "{rax}" (number),
385 [arg1] "{rdi}" (arg1)
386 : "rcx", "r11");
387}
388
389pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize {
390 return asm volatile ("syscall"
391 : [ret] "={rax}" (-> usize)
392 : [number] "{rax}" (number),
393 [arg1] "{rdi}" (arg1),
394 [arg2] "{rsi}" (arg2)
395 : "rcx", "r11");
396}
397
398pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
399 return asm volatile ("syscall"
400 : [ret] "={rax}" (-> usize)
401 : [number] "{rax}" (number),
402 [arg1] "{rdi}" (arg1),
403 [arg2] "{rsi}" (arg2),
404 [arg3] "{rdx}" (arg3)
405 : "rcx", "r11");
406}
407
408pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
409 return asm volatile ("syscall"
410 : [ret] "={rax}" (-> usize)
411 : [number] "{rax}" (number),
412 [arg1] "{rdi}" (arg1),
413 [arg2] "{rsi}" (arg2),
414 [arg3] "{rdx}" (arg3),
415 [arg4] "{r10}" (arg4)
416 : "rcx", "r11");
417}
418
419pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
420 return asm volatile ("syscall"
421 : [ret] "={rax}" (-> usize)
422 : [number] "{rax}" (number),
423 [arg1] "{rdi}" (arg1),
424 [arg2] "{rsi}" (arg2),
425 [arg3] "{rdx}" (arg3),
426 [arg4] "{r10}" (arg4),
427 [arg5] "{r8}" (arg5)
428 : "rcx", "r11");
429}
430
431pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize,
432 arg5: usize, arg6: usize) usize
433{
434 return asm volatile ("syscall"
435 : [ret] "={rax}" (-> usize)
436 : [number] "{rax}" (number),
437 [arg1] "{rdi}" (arg1),
438 [arg2] "{rsi}" (arg2),
439 [arg3] "{rdx}" (arg3),
440 [arg4] "{r10}" (arg4),
441 [arg5] "{r8}" (arg5),
442 [arg6] "{r9}" (arg6)
443 : "rcx", "r11");
444}
445
446pub nakedcc fn restore_rt() void {
447 return asm volatile ("syscall"
448 :
449 : [number] "{rax}" (usize(SYS_rt_sigreturn))
450 : "rcx", "r11");
451}
452
453
454pub const msghdr = extern struct {
455 msg_name: &u8,
456 msg_namelen: socklen_t,
457 msg_iov: &iovec,
458 msg_iovlen: i32,
459 __pad1: i32,
460 msg_control: &u8,
461 msg_controllen: socklen_t,
462 __pad2: socklen_t,
463 msg_flags: i32,
464};
465
466/// Renamed to Stat to not conflict with the stat function.
467pub const Stat = extern struct {
468 dev: u64,
469 ino: u64,
470 nlink: usize,
471
472 mode: u32,
473 uid: u32,
474 gid: u32,
475 __pad0: u32,
476 rdev: u64,
477 size: i64,
478 blksize: isize,
479 blocks: i64,
480
481 atim: timespec,
482 mtim: timespec,
483 ctim: timespec,
484 __unused: [3]isize,
485};
486
487pub const timespec = extern struct {
488 tv_sec: isize,
489 tv_nsec: isize,
490};
std/os/linux_errno.zig deleted-146
......@@ -1,146 +0,0 @@
1pub const EPERM = 1; /// Operation not permitted
2pub const ENOENT = 2; /// No such file or directory
3pub const ESRCH = 3; /// No such process
4pub const EINTR = 4; /// Interrupted system call
5pub const EIO = 5; /// I/O error
6pub const ENXIO = 6; /// No such device or address
7pub const E2BIG = 7; /// Arg list too long
8pub const ENOEXEC = 8; /// Exec format error
9pub const EBADF = 9; /// Bad file number
10pub const ECHILD = 10; /// No child processes
11pub const EAGAIN = 11; /// Try again
12pub const ENOMEM = 12; /// Out of memory
13pub const EACCES = 13; /// Permission denied
14pub const EFAULT = 14; /// Bad address
15pub const ENOTBLK = 15; /// Block device required
16pub const EBUSY = 16; /// Device or resource busy
17pub const EEXIST = 17; /// File exists
18pub const EXDEV = 18; /// Cross-device link
19pub const ENODEV = 19; /// No such device
20pub const ENOTDIR = 20; /// Not a directory
21pub const EISDIR = 21; /// Is a directory
22pub const EINVAL = 22; /// Invalid argument
23pub const ENFILE = 23; /// File table overflow
24pub const EMFILE = 24; /// Too many open files
25pub const ENOTTY = 25; /// Not a typewriter
26pub const ETXTBSY = 26; /// Text file busy
27pub const EFBIG = 27; /// File too large
28pub const ENOSPC = 28; /// No space left on device
29pub const ESPIPE = 29; /// Illegal seek
30pub const EROFS = 30; /// Read-only file system
31pub const EMLINK = 31; /// Too many links
32pub const EPIPE = 32; /// Broken pipe
33pub const EDOM = 33; /// Math argument out of domain of func
34pub const ERANGE = 34; /// Math result not representable
35pub const EDEADLK = 35; /// Resource deadlock would occur
36pub const ENAMETOOLONG = 36; /// File name too long
37pub const ENOLCK = 37; /// No record locks available
38pub const ENOSYS = 38; /// Function not implemented
39pub const ENOTEMPTY = 39; /// Directory not empty
40pub const ELOOP = 40; /// Too many symbolic links encountered
41pub const EWOULDBLOCK = EAGAIN; /// Operation would block
42pub const ENOMSG = 42; /// No message of desired type
43pub const EIDRM = 43; /// Identifier removed
44pub const ECHRNG = 44; /// Channel number out of range
45pub const EL2NSYNC = 45; /// Level 2 not synchronized
46pub const EL3HLT = 46; /// Level 3 halted
47pub const EL3RST = 47; /// Level 3 reset
48pub const ELNRNG = 48; /// Link number out of range
49pub const EUNATCH = 49; /// Protocol driver not attached
50pub const ENOCSI = 50; /// No CSI structure available
51pub const EL2HLT = 51; /// Level 2 halted
52pub const EBADE = 52; /// Invalid exchange
53pub const EBADR = 53; /// Invalid request descriptor
54pub const EXFULL = 54; /// Exchange full
55pub const ENOANO = 55; /// No anode
56pub const EBADRQC = 56; /// Invalid request code
57pub const EBADSLT = 57; /// Invalid slot
58
59pub const EBFONT = 59; /// Bad font file format
60pub const ENOSTR = 60; /// Device not a stream
61pub const ENODATA = 61; /// No data available
62pub const ETIME = 62; /// Timer expired
63pub const ENOSR = 63; /// Out of streams resources
64pub const ENONET = 64; /// Machine is not on the network
65pub const ENOPKG = 65; /// Package not installed
66pub const EREMOTE = 66; /// Object is remote
67pub const ENOLINK = 67; /// Link has been severed
68pub const EADV = 68; /// Advertise error
69pub const ESRMNT = 69; /// Srmount error
70pub const ECOMM = 70; /// Communication error on send
71pub const EPROTO = 71; /// Protocol error
72pub const EMULTIHOP = 72; /// Multihop attempted
73pub const EDOTDOT = 73; /// RFS specific error
74pub const EBADMSG = 74; /// Not a data message
75pub const EOVERFLOW = 75; /// Value too large for defined data type
76pub const ENOTUNIQ = 76; /// Name not unique on network
77pub const EBADFD = 77; /// File descriptor in bad state
78pub const EREMCHG = 78; /// Remote address changed
79pub const ELIBACC = 79; /// Can not access a needed shared library
80pub const ELIBBAD = 80; /// Accessing a corrupted shared library
81pub const ELIBSCN = 81; /// .lib section in a.out corrupted
82pub const ELIBMAX = 82; /// Attempting to link in too many shared libraries
83pub const ELIBEXEC = 83; /// Cannot exec a shared library directly
84pub const EILSEQ = 84; /// Illegal byte sequence
85pub const ERESTART = 85; /// Interrupted system call should be restarted
86pub const ESTRPIPE = 86; /// Streams pipe error
87pub const EUSERS = 87; /// Too many users
88pub const ENOTSOCK = 88; /// Socket operation on non-socket
89pub const EDESTADDRREQ = 89; /// Destination address required
90pub const EMSGSIZE = 90; /// Message too long
91pub const EPROTOTYPE = 91; /// Protocol wrong type for socket
92pub const ENOPROTOOPT = 92; /// Protocol not available
93pub const EPROTONOSUPPORT = 93; /// Protocol not supported
94pub const ESOCKTNOSUPPORT = 94; /// Socket type not supported
95pub const EOPNOTSUPP = 95; /// Operation not supported on transport endpoint
96pub const EPFNOSUPPORT = 96; /// Protocol family not supported
97pub const EAFNOSUPPORT = 97; /// Address family not supported by protocol
98pub const EADDRINUSE = 98; /// Address already in use
99pub const EADDRNOTAVAIL = 99; /// Cannot assign requested address
100pub const ENETDOWN = 100; /// Network is down
101pub const ENETUNREACH = 101; /// Network is unreachable
102pub const ENETRESET = 102; /// Network dropped connection because of reset
103pub const ECONNABORTED = 103; /// Software caused connection abort
104pub const ECONNRESET = 104; /// Connection reset by peer
105pub const ENOBUFS = 105; /// No buffer space available
106pub const EISCONN = 106; /// Transport endpoint is already connected
107pub const ENOTCONN = 107; /// Transport endpoint is not connected
108pub const ESHUTDOWN = 108; /// Cannot send after transport endpoint shutdown
109pub const ETOOMANYREFS = 109; /// Too many references: cannot splice
110pub const ETIMEDOUT = 110; /// Connection timed out
111pub const ECONNREFUSED = 111; /// Connection refused
112pub const EHOSTDOWN = 112; /// Host is down
113pub const EHOSTUNREACH = 113; /// No route to host
114pub const EALREADY = 114; /// Operation already in progress
115pub const EINPROGRESS = 115; /// Operation now in progress
116pub const ESTALE = 116; /// Stale NFS file handle
117pub const EUCLEAN = 117; /// Structure needs cleaning
118pub const ENOTNAM = 118; /// Not a XENIX named type file
119pub const ENAVAIL = 119; /// No XENIX semaphores available
120pub const EISNAM = 120; /// Is a named type file
121pub const EREMOTEIO = 121; /// Remote I/O error
122pub const EDQUOT = 122; /// Quota exceeded
123
124pub const ENOMEDIUM = 123; /// No medium found
125pub const EMEDIUMTYPE = 124; /// Wrong medium type
126
127// nameserver query return codes
128pub const ENSROK = 0; /// DNS server returned answer with no data
129pub const ENSRNODATA = 160; /// DNS server returned answer with no data
130pub const ENSRFORMERR = 161; /// DNS server claims query was misformatted
131pub const ENSRSERVFAIL = 162; /// DNS server returned general failure
132pub const ENSRNOTFOUND = 163; /// Domain name not found
133pub const ENSRNOTIMP = 164; /// DNS server does not implement requested operation
134pub const ENSRREFUSED = 165; /// DNS server refused query
135pub const ENSRBADQUERY = 166; /// Misformatted DNS query
136pub const ENSRBADNAME = 167; /// Misformatted domain name
137pub const ENSRBADFAMILY = 168; /// Unsupported address family
138pub const ENSRBADRESP = 169; /// Misformatted DNS reply
139pub const ENSRCONNREFUSED = 170; /// Could not contact DNS servers
140pub const ENSRTIMEOUT = 171; /// Timeout while contacting DNS servers
141pub const ENSROF = 172; /// End of file
142pub const ENSRFILE = 173; /// Error reading file
143pub const ENSRNOMEM = 174; /// Out of memory
144pub const ENSRDESTRUCTION = 175; /// Application terminated lookup
145pub const ENSRQUERYDOMAINTOOLONG = 176; /// Domain name is too long
146pub const ENSRCNAMELOOP = 177; /// Domain name is too long
std/os/linux_i386.zig deleted-504
......@@ -1,504 +0,0 @@
1const linux = @import("linux.zig");
2const socklen_t = linux.socklen_t;
3const iovec = linux.iovec;
4
5pub const SYS_restart_syscall = 0;
6pub const SYS_exit = 1;
7pub const SYS_fork = 2;
8pub const SYS_read = 3;
9pub const SYS_write = 4;
10pub const SYS_open = 5;
11pub const SYS_close = 6;
12pub const SYS_waitpid = 7;
13pub const SYS_creat = 8;
14pub const SYS_link = 9;
15pub const SYS_unlink = 10;
16pub const SYS_execve = 11;
17pub const SYS_chdir = 12;
18pub const SYS_time = 13;
19pub const SYS_mknod = 14;
20pub const SYS_chmod = 15;
21pub const SYS_lchown = 16;
22pub const SYS_break = 17;
23pub const SYS_oldstat = 18;
24pub const SYS_lseek = 19;
25pub const SYS_getpid = 20;
26pub const SYS_mount = 21;
27pub const SYS_umount = 22;
28pub const SYS_setuid = 23;
29pub const SYS_getuid = 24;
30pub const SYS_stime = 25;
31pub const SYS_ptrace = 26;
32pub const SYS_alarm = 27;
33pub const SYS_oldfstat = 28;
34pub const SYS_pause = 29;
35pub const SYS_utime = 30;
36pub const SYS_stty = 31;
37pub const SYS_gtty = 32;
38pub const SYS_access = 33;
39pub const SYS_nice = 34;
40pub const SYS_ftime = 35;
41pub const SYS_sync = 36;
42pub const SYS_kill = 37;
43pub const SYS_rename = 38;
44pub const SYS_mkdir = 39;
45pub const SYS_rmdir = 40;
46pub const SYS_dup = 41;
47pub const SYS_pipe = 42;
48pub const SYS_times = 43;
49pub const SYS_prof = 44;
50pub const SYS_brk = 45;
51pub const SYS_setgid = 46;
52pub const SYS_getgid = 47;
53pub const SYS_signal = 48;
54pub const SYS_geteuid = 49;
55pub const SYS_getegid = 50;
56pub const SYS_acct = 51;
57pub const SYS_umount2 = 52;
58pub const SYS_lock = 53;
59pub const SYS_ioctl = 54;
60pub const SYS_fcntl = 55;
61pub const SYS_mpx = 56;
62pub const SYS_setpgid = 57;
63pub const SYS_ulimit = 58;
64pub const SYS_oldolduname = 59;
65pub const SYS_umask = 60;
66pub const SYS_chroot = 61;
67pub const SYS_ustat = 62;
68pub const SYS_dup2 = 63;
69pub const SYS_getppid = 64;
70pub const SYS_getpgrp = 65;
71pub const SYS_setsid = 66;
72pub const SYS_sigaction = 67;
73pub const SYS_sgetmask = 68;
74pub const SYS_ssetmask = 69;
75pub const SYS_setreuid = 70;
76pub const SYS_setregid = 71;
77pub const SYS_sigsuspend = 72;
78pub const SYS_sigpending = 73;
79pub const SYS_sethostname = 74;
80pub const SYS_setrlimit = 75;
81pub const SYS_getrlimit = 76;
82pub const SYS_getrusage = 77;
83pub const SYS_gettimeofday = 78;
84pub const SYS_settimeofday = 79;
85pub const SYS_getgroups = 80;
86pub const SYS_setgroups = 81;
87pub const SYS_select = 82;
88pub const SYS_symlink = 83;
89pub const SYS_oldlstat = 84;
90pub const SYS_readlink = 85;
91pub const SYS_uselib = 86;
92pub const SYS_swapon = 87;
93pub const SYS_reboot = 88;
94pub const SYS_readdir = 89;
95pub const SYS_mmap = 90;
96pub const SYS_munmap = 91;
97pub const SYS_truncate = 92;
98pub const SYS_ftruncate = 93;
99pub const SYS_fchmod = 94;
100pub const SYS_fchown = 95;
101pub const SYS_getpriority = 96;
102pub const SYS_setpriority = 97;
103pub const SYS_profil = 98;
104pub const SYS_statfs = 99;
105pub const SYS_fstatfs = 100;
106pub const SYS_ioperm = 101;
107pub const SYS_socketcall = 102;
108pub const SYS_syslog = 103;
109pub const SYS_setitimer = 104;
110pub const SYS_getitimer = 105;
111pub const SYS_stat = 106;
112pub const SYS_lstat = 107;
113pub const SYS_fstat = 108;
114pub const SYS_olduname = 109;
115pub const SYS_iopl = 110;
116pub const SYS_vhangup = 111;
117pub const SYS_idle = 112;
118pub const SYS_vm86old = 113;
119pub const SYS_wait4 = 114;
120pub const SYS_swapoff = 115;
121pub const SYS_sysinfo = 116;
122pub const SYS_ipc = 117;
123pub const SYS_fsync = 118;
124pub const SYS_sigreturn = 119;
125pub const SYS_clone = 120;
126pub const SYS_setdomainname = 121;
127pub const SYS_uname = 122;
128pub const SYS_modify_ldt = 123;
129pub const SYS_adjtimex = 124;
130pub const SYS_mprotect = 125;
131pub const SYS_sigprocmask = 126;
132pub const SYS_create_module = 127;
133pub const SYS_init_module = 128;
134pub const SYS_delete_module = 129;
135pub const SYS_get_kernel_syms = 130;
136pub const SYS_quotactl = 131;
137pub const SYS_getpgid = 132;
138pub const SYS_fchdir = 133;
139pub const SYS_bdflush = 134;
140pub const SYS_sysfs = 135;
141pub const SYS_personality = 136;
142pub const SYS_afs_syscall = 137;
143pub const SYS_setfsuid = 138;
144pub const SYS_setfsgid = 139;
145pub const SYS__llseek = 140;
146pub const SYS_getdents = 141;
147pub const SYS__newselect = 142;
148pub const SYS_flock = 143;
149pub const SYS_msync = 144;
150pub const SYS_readv = 145;
151pub const SYS_writev = 146;
152pub const SYS_getsid = 147;
153pub const SYS_fdatasync = 148;
154pub const SYS__sysctl = 149;
155pub const SYS_mlock = 150;
156pub const SYS_munlock = 151;
157pub const SYS_mlockall = 152;
158pub const SYS_munlockall = 153;
159pub const SYS_sched_setparam = 154;
160pub const SYS_sched_getparam = 155;
161pub const SYS_sched_setscheduler = 156;
162pub const SYS_sched_getscheduler = 157;
163pub const SYS_sched_yield = 158;
164pub const SYS_sched_get_priority_max = 159;
165pub const SYS_sched_get_priority_min = 160;
166pub const SYS_sched_rr_get_interval = 161;
167pub const SYS_nanosleep = 162;
168pub const SYS_mremap = 163;
169pub const SYS_setresuid = 164;
170pub const SYS_getresuid = 165;
171pub const SYS_vm86 = 166;
172pub const SYS_query_module = 167;
173pub const SYS_poll = 168;
174pub const SYS_nfsservctl = 169;
175pub const SYS_setresgid = 170;
176pub const SYS_getresgid = 171;
177pub const SYS_prctl = 172;
178pub const SYS_rt_sigreturn = 173;
179pub const SYS_rt_sigaction = 174;
180pub const SYS_rt_sigprocmask = 175;
181pub const SYS_rt_sigpending = 176;
182pub const SYS_rt_sigtimedwait = 177;
183pub const SYS_rt_sigqueueinfo = 178;
184pub const SYS_rt_sigsuspend = 179;
185pub const SYS_pread64 = 180;
186pub const SYS_pwrite64 = 181;
187pub const SYS_chown = 182;
188pub const SYS_getcwd = 183;
189pub const SYS_capget = 184;
190pub const SYS_capset = 185;
191pub const SYS_sigaltstack = 186;
192pub const SYS_sendfile = 187;
193pub const SYS_getpmsg = 188;
194pub const SYS_putpmsg = 189;
195pub const SYS_vfork = 190;
196pub const SYS_ugetrlimit = 191;
197pub const SYS_mmap2 = 192;
198pub const SYS_truncate64 = 193;
199pub const SYS_ftruncate64 = 194;
200pub const SYS_stat64 = 195;
201pub const SYS_lstat64 = 196;
202pub const SYS_fstat64 = 197;
203pub const SYS_lchown32 = 198;
204pub const SYS_getuid32 = 199;
205pub const SYS_getgid32 = 200;
206pub const SYS_geteuid32 = 201;
207pub const SYS_getegid32 = 202;
208pub const SYS_setreuid32 = 203;
209pub const SYS_setregid32 = 204;
210pub const SYS_getgroups32 = 205;
211pub const SYS_setgroups32 = 206;
212pub const SYS_fchown32 = 207;
213pub const SYS_setresuid32 = 208;
214pub const SYS_getresuid32 = 209;
215pub const SYS_setresgid32 = 210;
216pub const SYS_getresgid32 = 211;
217pub const SYS_chown32 = 212;
218pub const SYS_setuid32 = 213;
219pub const SYS_setgid32 = 214;
220pub const SYS_setfsuid32 = 215;
221pub const SYS_setfsgid32 = 216;
222pub const SYS_pivot_root = 217;
223pub const SYS_mincore = 218;
224pub const SYS_madvise = 219;
225pub const SYS_madvise1 = 219;
226pub const SYS_getdents64 = 220;
227pub const SYS_fcntl64 = 221;
228pub const SYS_gettid = 224;
229pub const SYS_readahead = 225;
230pub const SYS_setxattr = 226;
231pub const SYS_lsetxattr = 227;
232pub const SYS_fsetxattr = 228;
233pub const SYS_getxattr = 229;
234pub const SYS_lgetxattr = 230;
235pub const SYS_fgetxattr = 231;
236pub const SYS_listxattr = 232;
237pub const SYS_llistxattr = 233;
238pub const SYS_flistxattr = 234;
239pub const SYS_removexattr = 235;
240pub const SYS_lremovexattr = 236;
241pub const SYS_fremovexattr = 237;
242pub const SYS_tkill = 238;
243pub const SYS_sendfile64 = 239;
244pub const SYS_futex = 240;
245pub const SYS_sched_setaffinity = 241;
246pub const SYS_sched_getaffinity = 242;
247pub const SYS_set_thread_area = 243;
248pub const SYS_get_thread_area = 244;
249pub const SYS_io_setup = 245;
250pub const SYS_io_destroy = 246;
251pub const SYS_io_getevents = 247;
252pub const SYS_io_submit = 248;
253pub const SYS_io_cancel = 249;
254pub const SYS_fadvise64 = 250;
255pub const SYS_exit_group = 252;
256pub const SYS_lookup_dcookie = 253;
257pub const SYS_epoll_create = 254;
258pub const SYS_epoll_ctl = 255;
259pub const SYS_epoll_wait = 256;
260pub const SYS_remap_file_pages = 257;
261pub const SYS_set_tid_address = 258;
262pub const SYS_timer_create = 259;
263pub const SYS_timer_settime = SYS_timer_create+1;
264pub const SYS_timer_gettime = SYS_timer_create+2;
265pub const SYS_timer_getoverrun = SYS_timer_create+3;
266pub const SYS_timer_delete = SYS_timer_create+4;
267pub const SYS_clock_settime = SYS_timer_create+5;
268pub const SYS_clock_gettime = SYS_timer_create+6;
269pub const SYS_clock_getres = SYS_timer_create+7;
270pub const SYS_clock_nanosleep = SYS_timer_create+8;
271pub const SYS_statfs64 = 268;
272pub const SYS_fstatfs64 = 269;
273pub const SYS_tgkill = 270;
274pub const SYS_utimes = 271;
275pub const SYS_fadvise64_64 = 272;
276pub const SYS_vserver = 273;
277pub const SYS_mbind = 274;
278pub const SYS_get_mempolicy = 275;
279pub const SYS_set_mempolicy = 276;
280pub const SYS_mq_open = 277;
281pub const SYS_mq_unlink = SYS_mq_open+1;
282pub const SYS_mq_timedsend = SYS_mq_open+2;
283pub const SYS_mq_timedreceive = SYS_mq_open+3;
284pub const SYS_mq_notify = SYS_mq_open+4;
285pub const SYS_mq_getsetattr = SYS_mq_open+5;
286pub const SYS_kexec_load = 283;
287pub const SYS_waitid = 284;
288pub const SYS_add_key = 286;
289pub const SYS_request_key = 287;
290pub const SYS_keyctl = 288;
291pub const SYS_ioprio_set = 289;
292pub const SYS_ioprio_get = 290;
293pub const SYS_inotify_init = 291;
294pub const SYS_inotify_add_watch = 292;
295pub const SYS_inotify_rm_watch = 293;
296pub const SYS_migrate_pages = 294;
297pub const SYS_openat = 295;
298pub const SYS_mkdirat = 296;
299pub const SYS_mknodat = 297;
300pub const SYS_fchownat = 298;
301pub const SYS_futimesat = 299;
302pub const SYS_fstatat64 = 300;
303pub const SYS_unlinkat = 301;
304pub const SYS_renameat = 302;
305pub const SYS_linkat = 303;
306pub const SYS_symlinkat = 304;
307pub const SYS_readlinkat = 305;
308pub const SYS_fchmodat = 306;
309pub const SYS_faccessat = 307;
310pub const SYS_pselect6 = 308;
311pub const SYS_ppoll = 309;
312pub const SYS_unshare = 310;
313pub const SYS_set_robust_list = 311;
314pub const SYS_get_robust_list = 312;
315pub const SYS_splice = 313;
316pub const SYS_sync_file_range = 314;
317pub const SYS_tee = 315;
318pub const SYS_vmsplice = 316;
319pub const SYS_move_pages = 317;
320pub const SYS_getcpu = 318;
321pub const SYS_epoll_pwait = 319;
322pub const SYS_utimensat = 320;
323pub const SYS_signalfd = 321;
324pub const SYS_timerfd_create = 322;
325pub const SYS_eventfd = 323;
326pub const SYS_fallocate = 324;
327pub const SYS_timerfd_settime = 325;
328pub const SYS_timerfd_gettime = 326;
329pub const SYS_signalfd4 = 327;
330pub const SYS_eventfd2 = 328;
331pub const SYS_epoll_create1 = 329;
332pub const SYS_dup3 = 330;
333pub const SYS_pipe2 = 331;
334pub const SYS_inotify_init1 = 332;
335pub const SYS_preadv = 333;
336pub const SYS_pwritev = 334;
337pub const SYS_rt_tgsigqueueinfo = 335;
338pub const SYS_perf_event_open = 336;
339pub const SYS_recvmmsg = 337;
340pub const SYS_fanotify_init = 338;
341pub const SYS_fanotify_mark = 339;
342pub const SYS_prlimit64 = 340;
343pub const SYS_name_to_handle_at = 341;
344pub const SYS_open_by_handle_at = 342;
345pub const SYS_clock_adjtime = 343;
346pub const SYS_syncfs = 344;
347pub const SYS_sendmmsg = 345;
348pub const SYS_setns = 346;
349pub const SYS_process_vm_readv = 347;
350pub const SYS_process_vm_writev = 348;
351pub const SYS_kcmp = 349;
352pub const SYS_finit_module = 350;
353pub const SYS_sched_setattr = 351;
354pub const SYS_sched_getattr = 352;
355pub const SYS_renameat2 = 353;
356pub const SYS_seccomp = 354;
357pub const SYS_getrandom = 355;
358pub const SYS_memfd_create = 356;
359pub const SYS_bpf = 357;
360pub const SYS_execveat = 358;
361pub const SYS_socket = 359;
362pub const SYS_socketpair = 360;
363pub const SYS_bind = 361;
364pub const SYS_connect = 362;
365pub const SYS_listen = 363;
366pub const SYS_accept4 = 364;
367pub const SYS_getsockopt = 365;
368pub const SYS_setsockopt = 366;
369pub const SYS_getsockname = 367;
370pub const SYS_getpeername = 368;
371pub const SYS_sendto = 369;
372pub const SYS_sendmsg = 370;
373pub const SYS_recvfrom = 371;
374pub const SYS_recvmsg = 372;
375pub const SYS_shutdown = 373;
376pub const SYS_userfaultfd = 374;
377pub const SYS_membarrier = 375;
378pub const SYS_mlock2 = 376;
379
380
381pub const O_CREAT = 0o100;
382pub const O_EXCL = 0o200;
383pub const O_NOCTTY = 0o400;
384pub const O_TRUNC = 0o1000;
385pub const O_APPEND = 0o2000;
386pub const O_NONBLOCK = 0o4000;
387pub const O_DSYNC = 0o10000;
388pub const O_SYNC = 0o4010000;
389pub const O_RSYNC = 0o4010000;
390pub const O_DIRECTORY = 0o200000;
391pub const O_NOFOLLOW = 0o400000;
392pub const O_CLOEXEC = 0o2000000;
393
394pub const O_ASYNC = 0o20000;
395pub const O_DIRECT = 0o40000;
396pub const O_LARGEFILE = 0o100000;
397pub const O_NOATIME = 0o1000000;
398pub const O_PATH = 0o10000000;
399pub const O_TMPFILE = 0o20200000;
400pub const O_NDELAY = O_NONBLOCK;
401
402pub const F_DUPFD = 0;
403pub const F_GETFD = 1;
404pub const F_SETFD = 2;
405pub const F_GETFL = 3;
406pub const F_SETFL = 4;
407
408pub const F_SETOWN = 8;
409pub const F_GETOWN = 9;
410pub const F_SETSIG = 10;
411pub const F_GETSIG = 11;
412
413pub const F_GETLK = 12;
414pub const F_SETLK = 13;
415pub const F_SETLKW = 14;
416
417pub const F_SETOWN_EX = 15;
418pub const F_GETOWN_EX = 16;
419
420pub const F_GETOWNER_UIDS = 17;
421
422pub inline fn syscall0(number: usize) usize {
423 asm volatile ("int $0x80"
424 : [ret] "={eax}" (-> usize)
425 : [number] "{eax}" (number))
426}
427
428pub inline fn syscall1(number: usize, arg1: usize) usize {
429 asm volatile ("int $0x80"
430 : [ret] "={eax}" (-> usize)
431 : [number] "{eax}" (number),
432 [arg1] "{ebx}" (arg1))
433}
434
435pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize {
436 asm volatile ("int $0x80"
437 : [ret] "={eax}" (-> usize)
438 : [number] "{eax}" (number),
439 [arg1] "{ebx}" (arg1),
440 [arg2] "{ecx}" (arg2))
441}
442
443pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
444 asm volatile ("int $0x80"
445 : [ret] "={eax}" (-> usize)
446 : [number] "{eax}" (number),
447 [arg1] "{ebx}" (arg1),
448 [arg2] "{ecx}" (arg2),
449 [arg3] "{edx}" (arg3))
450}
451
452pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
453 asm volatile ("int $0x80"
454 : [ret] "={eax}" (-> usize)
455 : [number] "{eax}" (number),
456 [arg1] "{ebx}" (arg1),
457 [arg2] "{ecx}" (arg2),
458 [arg3] "{edx}" (arg3),
459 [arg4] "{esi}" (arg4))
460}
461
462pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
463 arg4: usize, arg5: usize) -> usize
464{
465 asm volatile ("int $0x80"
466 : [ret] "={eax}" (-> usize)
467 : [number] "{eax}" (number),
468 [arg1] "{ebx}" (arg1),
469 [arg2] "{ecx}" (arg2),
470 [arg3] "{edx}" (arg3),
471 [arg4] "{esi}" (arg4),
472 [arg5] "{edi}" (arg5))
473}
474
475pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize,
476 arg4: usize, arg5: usize, arg6: usize) -> usize
477{
478 asm volatile ("int $0x80"
479 : [ret] "={eax}" (-> usize)
480 : [number] "{eax}" (number),
481 [arg1] "{ebx}" (arg1),
482 [arg2] "{ecx}" (arg2),
483 [arg3] "{edx}" (arg3),
484 [arg4] "{esi}" (arg4),
485 [arg5] "{edi}" (arg5),
486 [arg6] "{ebp}" (arg6))
487}
488
489pub nakedcc fn restore() void {
490 asm volatile (
491 \\popl %%eax
492 \\movl $119, %%eax
493 \\int $0x80
494 :
495 :
496 : "rcx", "r11")
497}
498
499pub nakedcc fn restore_rt() void {
500 asm volatile ("int $0x80"
501 :
502 : [number] "{eax}" (usize(SYS_rt_sigreturn))
503 : "rcx", "r11")
504}
std/os/linux_test.zig deleted-38
......@@ -1,38 +0,0 @@
1const std = @import("std");
2const linux = std.os.linux;
3const assert = std.debug.assert;
4
5test "timer" {
6 const epoll_fd = linux.epoll_create();
7 var err = linux.getErrno(epoll_fd);
8 assert(err == 0);
9
10 const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0);
11 assert(linux.getErrno(timer_fd) == 0);
12
13 const time_interval = linux.timespec {
14 .tv_sec = 0,
15 .tv_nsec = 2000000
16 };
17
18 const new_time = linux.itimerspec {
19 .it_interval = time_interval,
20 .it_value = time_interval
21 };
22
23 err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null);
24 assert(err == 0);
25
26 var event = linux.epoll_event {
27 .events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET,
28 .data = 0
29 };
30
31 err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event);
32 assert(err == 0);
33
34 const events_one: linux.epoll_event = undefined;
35 var events = []linux.epoll_event{events_one} ** 8;
36
37 err = linux.epoll_wait(i32(epoll_fd), &events[0], 8, -1);
38}
std/os/linux_x86_64.zig deleted-489
......@@ -1,489 +0,0 @@
1const linux = @import("linux.zig");
2const socklen_t = linux.socklen_t;
3const iovec = linux.iovec;
4
5pub const SYS_read = 0;
6pub const SYS_write = 1;
7pub const SYS_open = 2;
8pub const SYS_close = 3;
9pub const SYS_stat = 4;
10pub const SYS_fstat = 5;
11pub const SYS_lstat = 6;
12pub const SYS_poll = 7;
13pub const SYS_lseek = 8;
14pub const SYS_mmap = 9;
15pub const SYS_mprotect = 10;
16pub const SYS_munmap = 11;
17pub const SYS_brk = 12;
18pub const SYS_rt_sigaction = 13;
19pub const SYS_rt_sigprocmask = 14;
20pub const SYS_rt_sigreturn = 15;
21pub const SYS_ioctl = 16;
22pub const SYS_pread = 17;
23pub const SYS_pwrite = 18;
24pub const SYS_readv = 19;
25pub const SYS_writev = 20;
26pub const SYS_access = 21;
27pub const SYS_pipe = 22;
28pub const SYS_select = 23;
29pub const SYS_sched_yield = 24;
30pub const SYS_mremap = 25;
31pub const SYS_msync = 26;
32pub const SYS_mincore = 27;
33pub const SYS_madvise = 28;
34pub const SYS_shmget = 29;
35pub const SYS_shmat = 30;
36pub const SYS_shmctl = 31;
37pub const SYS_dup = 32;
38pub const SYS_dup2 = 33;
39pub const SYS_pause = 34;
40pub const SYS_nanosleep = 35;
41pub const SYS_getitimer = 36;
42pub const SYS_alarm = 37;
43pub const SYS_setitimer = 38;
44pub const SYS_getpid = 39;
45pub const SYS_sendfile = 40;
46pub const SYS_socket = 41;
47pub const SYS_connect = 42;
48pub const SYS_accept = 43;
49pub const SYS_sendto = 44;
50pub const SYS_recvfrom = 45;
51pub const SYS_sendmsg = 46;
52pub const SYS_recvmsg = 47;
53pub const SYS_shutdown = 48;
54pub const SYS_bind = 49;
55pub const SYS_listen = 50;
56pub const SYS_getsockname = 51;
57pub const SYS_getpeername = 52;
58pub const SYS_socketpair = 53;
59pub const SYS_setsockopt = 54;
60pub const SYS_getsockopt = 55;
61pub const SYS_clone = 56;
62pub const SYS_fork = 57;
63pub const SYS_vfork = 58;
64pub const SYS_execve = 59;
65pub const SYS_exit = 60;
66pub const SYS_wait4 = 61;
67pub const SYS_kill = 62;
68pub const SYS_uname = 63;
69pub const SYS_semget = 64;
70pub const SYS_semop = 65;
71pub const SYS_semctl = 66;
72pub const SYS_shmdt = 67;
73pub const SYS_msgget = 68;
74pub const SYS_msgsnd = 69;
75pub const SYS_msgrcv = 70;
76pub const SYS_msgctl = 71;
77pub const SYS_fcntl = 72;
78pub const SYS_flock = 73;
79pub const SYS_fsync = 74;
80pub const SYS_fdatasync = 75;
81pub const SYS_truncate = 76;
82pub const SYS_ftruncate = 77;
83pub const SYS_getdents = 78;
84pub const SYS_getcwd = 79;
85pub const SYS_chdir = 80;
86pub const SYS_fchdir = 81;
87pub const SYS_rename = 82;
88pub const SYS_mkdir = 83;
89pub const SYS_rmdir = 84;
90pub const SYS_creat = 85;
91pub const SYS_link = 86;
92pub const SYS_unlink = 87;
93pub const SYS_symlink = 88;
94pub const SYS_readlink = 89;
95pub const SYS_chmod = 90;
96pub const SYS_fchmod = 91;
97pub const SYS_chown = 92;
98pub const SYS_fchown = 93;
99pub const SYS_lchown = 94;
100pub const SYS_umask = 95;
101pub const SYS_gettimeofday = 96;
102pub const SYS_getrlimit = 97;
103pub const SYS_getrusage = 98;
104pub const SYS_sysinfo = 99;
105pub const SYS_times = 100;
106pub const SYS_ptrace = 101;
107pub const SYS_getuid = 102;
108pub const SYS_syslog = 103;
109pub const SYS_getgid = 104;
110pub const SYS_setuid = 105;
111pub const SYS_setgid = 106;
112pub const SYS_geteuid = 107;
113pub const SYS_getegid = 108;
114pub const SYS_setpgid = 109;
115pub const SYS_getppid = 110;
116pub const SYS_getpgrp = 111;
117pub const SYS_setsid = 112;
118pub const SYS_setreuid = 113;
119pub const SYS_setregid = 114;
120pub const SYS_getgroups = 115;
121pub const SYS_setgroups = 116;
122pub const SYS_setresuid = 117;
123pub const SYS_getresuid = 118;
124pub const SYS_setresgid = 119;
125pub const SYS_getresgid = 120;
126pub const SYS_getpgid = 121;
127pub const SYS_setfsuid = 122;
128pub const SYS_setfsgid = 123;
129pub const SYS_getsid = 124;
130pub const SYS_capget = 125;
131pub const SYS_capset = 126;
132pub const SYS_rt_sigpending = 127;
133pub const SYS_rt_sigtimedwait = 128;
134pub const SYS_rt_sigqueueinfo = 129;
135pub const SYS_rt_sigsuspend = 130;
136pub const SYS_sigaltstack = 131;
137pub const SYS_utime = 132;
138pub const SYS_mknod = 133;
139pub const SYS_uselib = 134;
140pub const SYS_personality = 135;
141pub const SYS_ustat = 136;
142pub const SYS_statfs = 137;
143pub const SYS_fstatfs = 138;
144pub const SYS_sysfs = 139;
145pub const SYS_getpriority = 140;
146pub const SYS_setpriority = 141;
147pub const SYS_sched_setparam = 142;
148pub const SYS_sched_getparam = 143;
149pub const SYS_sched_setscheduler = 144;
150pub const SYS_sched_getscheduler = 145;
151pub const SYS_sched_get_priority_max = 146;
152pub const SYS_sched_get_priority_min = 147;
153pub const SYS_sched_rr_get_interval = 148;
154pub const SYS_mlock = 149;
155pub const SYS_munlock = 150;
156pub const SYS_mlockall = 151;
157pub const SYS_munlockall = 152;
158pub const SYS_vhangup = 153;
159pub const SYS_modify_ldt = 154;
160pub const SYS_pivot_root = 155;
161pub const SYS__sysctl = 156;
162pub const SYS_prctl = 157;
163pub const SYS_arch_prctl = 158;
164pub const SYS_adjtimex = 159;
165pub const SYS_setrlimit = 160;
166pub const SYS_chroot = 161;
167pub const SYS_sync = 162;
168pub const SYS_acct = 163;
169pub const SYS_settimeofday = 164;
170pub const SYS_mount = 165;
171pub const SYS_umount2 = 166;
172pub const SYS_swapon = 167;
173pub const SYS_swapoff = 168;
174pub const SYS_reboot = 169;
175pub const SYS_sethostname = 170;
176pub const SYS_setdomainname = 171;
177pub const SYS_iopl = 172;
178pub const SYS_ioperm = 173;
179pub const SYS_create_module = 174;
180pub const SYS_init_module = 175;
181pub const SYS_delete_module = 176;
182pub const SYS_get_kernel_syms = 177;
183pub const SYS_query_module = 178;
184pub const SYS_quotactl = 179;
185pub const SYS_nfsservctl = 180;
186pub const SYS_getpmsg = 181;
187pub const SYS_putpmsg = 182;
188pub const SYS_afs_syscall = 183;
189pub const SYS_tuxcall = 184;
190pub const SYS_security = 185;
191pub const SYS_gettid = 186;
192pub const SYS_readahead = 187;
193pub const SYS_setxattr = 188;
194pub const SYS_lsetxattr = 189;
195pub const SYS_fsetxattr = 190;
196pub const SYS_getxattr = 191;
197pub const SYS_lgetxattr = 192;
198pub const SYS_fgetxattr = 193;
199pub const SYS_listxattr = 194;
200pub const SYS_llistxattr = 195;
201pub const SYS_flistxattr = 196;
202pub const SYS_removexattr = 197;
203pub const SYS_lremovexattr = 198;
204pub const SYS_fremovexattr = 199;
205pub const SYS_tkill = 200;
206pub const SYS_time = 201;
207pub const SYS_futex = 202;
208pub const SYS_sched_setaffinity = 203;
209pub const SYS_sched_getaffinity = 204;
210pub const SYS_set_thread_area = 205;
211pub const SYS_io_setup = 206;
212pub const SYS_io_destroy = 207;
213pub const SYS_io_getevents = 208;
214pub const SYS_io_submit = 209;
215pub const SYS_io_cancel = 210;
216pub const SYS_get_thread_area = 211;
217pub const SYS_lookup_dcookie = 212;
218pub const SYS_epoll_create = 213;
219pub const SYS_epoll_ctl_old = 214;
220pub const SYS_epoll_wait_old = 215;
221pub const SYS_remap_file_pages = 216;
222pub const SYS_getdents64 = 217;
223pub const SYS_set_tid_address = 218;
224pub const SYS_restart_syscall = 219;
225pub const SYS_semtimedop = 220;
226pub const SYS_fadvise64 = 221;
227pub const SYS_timer_create = 222;
228pub const SYS_timer_settime = 223;
229pub const SYS_timer_gettime = 224;
230pub const SYS_timer_getoverrun = 225;
231pub const SYS_timer_delete = 226;
232pub const SYS_clock_settime = 227;
233pub const SYS_clock_gettime = 228;
234pub const SYS_clock_getres = 229;
235pub const SYS_clock_nanosleep = 230;
236pub const SYS_exit_group = 231;
237pub const SYS_epoll_wait = 232;
238pub const SYS_epoll_ctl = 233;
239pub const SYS_tgkill = 234;
240pub const SYS_utimes = 235;
241pub const SYS_vserver = 236;
242pub const SYS_mbind = 237;
243pub const SYS_set_mempolicy = 238;
244pub const SYS_get_mempolicy = 239;
245pub const SYS_mq_open = 240;
246pub const SYS_mq_unlink = 241;
247pub const SYS_mq_timedsend = 242;
248pub const SYS_mq_timedreceive = 243;
249pub const SYS_mq_notify = 244;
250pub const SYS_mq_getsetattr = 245;
251pub const SYS_kexec_load = 246;
252pub const SYS_waitid = 247;
253pub const SYS_add_key = 248;
254pub const SYS_request_key = 249;
255pub const SYS_keyctl = 250;
256pub const SYS_ioprio_set = 251;
257pub const SYS_ioprio_get = 252;
258pub const SYS_inotify_init = 253;
259pub const SYS_inotify_add_watch = 254;
260pub const SYS_inotify_rm_watch = 255;
261pub const SYS_migrate_pages = 256;
262pub const SYS_openat = 257;
263pub const SYS_mkdirat = 258;
264pub const SYS_mknodat = 259;
265pub const SYS_fchownat = 260;
266pub const SYS_futimesat = 261;
267pub const SYS_newfstatat = 262;
268pub const SYS_unlinkat = 263;
269pub const SYS_renameat = 264;
270pub const SYS_linkat = 265;
271pub const SYS_symlinkat = 266;
272pub const SYS_readlinkat = 267;
273pub const SYS_fchmodat = 268;
274pub const SYS_faccessat = 269;
275pub const SYS_pselect6 = 270;
276pub const SYS_ppoll = 271;
277pub const SYS_unshare = 272;
278pub const SYS_set_robust_list = 273;
279pub const SYS_get_robust_list = 274;
280pub const SYS_splice = 275;
281pub const SYS_tee = 276;
282pub const SYS_sync_file_range = 277;
283pub const SYS_vmsplice = 278;
284pub const SYS_move_pages = 279;
285pub const SYS_utimensat = 280;
286pub const SYS_epoll_pwait = 281;
287pub const SYS_signalfd = 282;
288pub const SYS_timerfd_create = 283;
289pub const SYS_eventfd = 284;
290pub const SYS_fallocate = 285;
291pub const SYS_timerfd_settime = 286;
292pub const SYS_timerfd_gettime = 287;
293pub const SYS_accept4 = 288;
294pub const SYS_signalfd4 = 289;
295pub const SYS_eventfd2 = 290;
296pub const SYS_epoll_create1 = 291;
297pub const SYS_dup3 = 292;
298pub const SYS_pipe2 = 293;
299pub const SYS_inotify_init1 = 294;
300pub const SYS_preadv = 295;
301pub const SYS_pwritev = 296;
302pub const SYS_rt_tgsigqueueinfo = 297;
303pub const SYS_perf_event_open = 298;
304pub const SYS_recvmmsg = 299;
305pub const SYS_fanotify_init = 300;
306pub const SYS_fanotify_mark = 301;
307pub const SYS_prlimit64 = 302;
308pub const SYS_name_to_handle_at = 303;
309pub const SYS_open_by_handle_at = 304;
310pub const SYS_clock_adjtime = 305;
311pub const SYS_syncfs = 306;
312pub const SYS_sendmmsg = 307;
313pub const SYS_setns = 308;
314pub const SYS_getcpu = 309;
315pub const SYS_process_vm_readv = 310;
316pub const SYS_process_vm_writev = 311;
317pub const SYS_kcmp = 312;
318pub const SYS_finit_module = 313;
319pub const SYS_sched_setattr = 314;
320pub const SYS_sched_getattr = 315;
321pub const SYS_renameat2 = 316;
322pub const SYS_seccomp = 317;
323pub const SYS_getrandom = 318;
324pub const SYS_memfd_create = 319;
325pub const SYS_kexec_file_load = 320;
326pub const SYS_bpf = 321;
327pub const SYS_execveat = 322;
328pub const SYS_userfaultfd = 323;
329pub const SYS_membarrier = 324;
330pub const SYS_mlock2 = 325;
331
332pub const O_CREAT = 0o100;
333pub const O_EXCL = 0o200;
334pub const O_NOCTTY = 0o400;
335pub const O_TRUNC = 0o1000;
336pub const O_APPEND = 0o2000;
337pub const O_NONBLOCK = 0o4000;
338pub const O_DSYNC = 0o10000;
339pub const O_SYNC = 0o4010000;
340pub const O_RSYNC = 0o4010000;
341pub const O_DIRECTORY = 0o200000;
342pub const O_NOFOLLOW = 0o400000;
343pub const O_CLOEXEC = 0o2000000;
344
345pub const O_ASYNC = 0o20000;
346pub const O_DIRECT = 0o40000;
347pub const O_LARGEFILE = 0;
348pub const O_NOATIME = 0o1000000;
349pub const O_PATH = 0o10000000;
350pub const O_TMPFILE = 0o20200000;
351pub const O_NDELAY = O_NONBLOCK;
352
353pub const F_DUPFD = 0;
354pub const F_GETFD = 1;
355pub const F_SETFD = 2;
356pub const F_GETFL = 3;
357pub const F_SETFL = 4;
358
359pub const F_SETOWN = 8;
360pub const F_GETOWN = 9;
361pub const F_SETSIG = 10;
362pub const F_GETSIG = 11;
363
364pub const F_GETLK = 5;
365pub const F_SETLK = 6;
366pub const F_SETLKW = 7;
367
368pub const F_SETOWN_EX = 15;
369pub const F_GETOWN_EX = 16;
370
371pub const F_GETOWNER_UIDS = 17;
372
373pub fn syscall0(number: usize) usize {
374 return asm volatile ("syscall"
375 : [ret] "={rax}" (-> usize)
376 : [number] "{rax}" (number)
377 : "rcx", "r11");
378}
379
380pub fn syscall1(number: usize, arg1: usize) usize {
381 return asm volatile ("syscall"
382 : [ret] "={rax}" (-> usize)
383 : [number] "{rax}" (number),
384 [arg1] "{rdi}" (arg1)
385 : "rcx", "r11");
386}
387
388pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize {
389 return asm volatile ("syscall"
390 : [ret] "={rax}" (-> usize)
391 : [number] "{rax}" (number),
392 [arg1] "{rdi}" (arg1),
393 [arg2] "{rsi}" (arg2)
394 : "rcx", "r11");
395}
396
397pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
398 return asm volatile ("syscall"
399 : [ret] "={rax}" (-> usize)
400 : [number] "{rax}" (number),
401 [arg1] "{rdi}" (arg1),
402 [arg2] "{rsi}" (arg2),
403 [arg3] "{rdx}" (arg3)
404 : "rcx", "r11");
405}
406
407pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
408 return asm volatile ("syscall"
409 : [ret] "={rax}" (-> usize)
410 : [number] "{rax}" (number),
411 [arg1] "{rdi}" (arg1),
412 [arg2] "{rsi}" (arg2),
413 [arg3] "{rdx}" (arg3),
414 [arg4] "{r10}" (arg4)
415 : "rcx", "r11");
416}
417
418pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
419 return asm volatile ("syscall"
420 : [ret] "={rax}" (-> usize)
421 : [number] "{rax}" (number),
422 [arg1] "{rdi}" (arg1),
423 [arg2] "{rsi}" (arg2),
424 [arg3] "{rdx}" (arg3),
425 [arg4] "{r10}" (arg4),
426 [arg5] "{r8}" (arg5)
427 : "rcx", "r11");
428}
429
430pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize,
431 arg5: usize, arg6: usize) usize
432{
433 return asm volatile ("syscall"
434 : [ret] "={rax}" (-> usize)
435 : [number] "{rax}" (number),
436 [arg1] "{rdi}" (arg1),
437 [arg2] "{rsi}" (arg2),
438 [arg3] "{rdx}" (arg3),
439 [arg4] "{r10}" (arg4),
440 [arg5] "{r8}" (arg5),
441 [arg6] "{r9}" (arg6)
442 : "rcx", "r11");
443}
444
445pub nakedcc fn restore_rt() void {
446 return asm volatile ("syscall"
447 :
448 : [number] "{rax}" (usize(SYS_rt_sigreturn))
449 : "rcx", "r11");
450}
451
452
453pub const msghdr = extern struct {
454 msg_name: &u8,
455 msg_namelen: socklen_t,
456 msg_iov: &iovec,
457 msg_iovlen: i32,
458 __pad1: i32,
459 msg_control: &u8,
460 msg_controllen: socklen_t,
461 __pad2: socklen_t,
462 msg_flags: i32,
463};
464
465/// Renamed to Stat to not conflict with the stat function.
466pub const Stat = extern struct {
467 dev: u64,
468 ino: u64,
469 nlink: usize,
470
471 mode: u32,
472 uid: u32,
473 gid: u32,
474 __pad0: u32,
475 rdev: u64,
476 size: i64,
477 blksize: isize,
478 blocks: i64,
479
480 atim: timespec,
481 mtim: timespec,
482 ctim: timespec,
483 __unused: [3]isize,
484};
485
486pub const timespec = extern struct {
487 tv_sec: isize,
488 tv_nsec: isize,
489};
test/compile_errors.zig+61-2
......@@ -1,6 +1,65 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: &tests.CompileErrorContext) void {
4 cases.add("cast negative integer literal to usize",
5 \\export fn entry() void {
6 \\ const x = usize(-10);
7 \\}
8 , ".tmp_source.zig:2:21: error: cannot cast negative value -10 to unsigned integer type 'usize'");
9
10 cases.add("use invalid number literal as array index",
11 \\var v = 25;
12 \\export fn entry() void {
13 \\ var arr: [v]u8 = undefined;
14 \\}
15 , ".tmp_source.zig:1:1: error: unable to infer variable type");
16
17 cases.add("duplicate struct field",
18 \\const Foo = struct {
19 \\ Bar: i32,
20 \\ Bar: usize,
21 \\};
22 \\export fn entry() void {
23 \\ const a: Foo = undefined;
24 \\}
25 ,
26 ".tmp_source.zig:3:5: error: duplicate struct field: 'Bar'",
27 ".tmp_source.zig:2:5: note: other field here");
28
29 cases.add("duplicate union field",
30 \\const Foo = union {
31 \\ Bar: i32,
32 \\ Bar: usize,
33 \\};
34 \\export fn entry() void {
35 \\ const a: Foo = undefined;
36 \\}
37 ,
38 ".tmp_source.zig:3:5: error: duplicate union field: 'Bar'",
39 ".tmp_source.zig:2:5: note: other field here");
40
41 cases.add("duplicate enum field",
42 \\const Foo = enum {
43 \\ Bar,
44 \\ Bar,
45 \\};
46 \\
47 \\export fn entry() void {
48 \\ const a: Foo = undefined;
49 \\}
50 ,
51 ".tmp_source.zig:3:5: error: duplicate enum field: 'Bar'",
52 ".tmp_source.zig:2:5: note: other field here");
53
54 cases.add("calling function with naked calling convention",
55 \\export fn entry() void {
56 \\ foo();
57 \\}
58 \\nakedcc fn foo() void { }
59 ,
60 ".tmp_source.zig:2:5: error: unable to call function with naked calling convention",
61 ".tmp_source.zig:4:9: note: declared here");
62
463 cases.add("function with invalid return type",
564 \\export fn foo() boid {}
665 , ".tmp_source.zig:1:17: error: use of undeclared identifier 'boid'");
......@@ -326,10 +385,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
326385 \\export fn entry() void { _ = a(); }
327386 , ".tmp_source.zig:1:8: error: use of undeclared identifier 'bogus'");
328387
329 cases.add("pointer to unreachable",
388 cases.add("pointer to noreturn",
330389 \\fn a() &noreturn {}
331390 \\export fn entry() void { _ = a(); }
332 , ".tmp_source.zig:1:9: error: pointer to unreachable not allowed");
391 , ".tmp_source.zig:1:9: error: pointer to noreturn not allowed");
333392
334393 cases.add("unreachable code",
335394 \\export fn a() void {