authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 23:55:43-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-08-02 23:55:43-07:00
log5687323cd2a759dfdf9533e46e3de8cddb1b55b7
tree38bb75a506c0163606064f6dc84951c66e0c1c7e
parent57830e43ee79dd0ceafc96fdad6c52e73edf1420
parent08251fbc544aa03c77d4c311e267689592432282
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2994 from euantorano/fix/1626-os-getRandomBytesDevURandom

Check if /dev/urandom is a character device

5 files changed, 125 insertions(+), 0 deletions(-)

std/os.zig+5
...@@ -133,6 +133,11 @@ fn getRandomBytesDevURandom(buf: []u8) !void {...@@ -133,6 +133,11 @@ fn getRandomBytesDevURandom(buf: []u8) !void {
133 const fd = try openC(c"/dev/urandom", O_RDONLY | O_CLOEXEC, 0);133 const fd = try openC(c"/dev/urandom", O_RDONLY | O_CLOEXEC, 0);
134 defer close(fd);134 defer close(fd);
135135
136 const st = try fstat(fd);
137 if (!S_ISCHR(st.mode)) {
138 return error.NoDevice;
139 }
140
136 const stream = &std.fs.File.openHandle(fd).inStream().stream;141 const stream = &std.fs.File.openHandle(fd).inStream().stream;
137 stream.readNoEof(buf) catch return error.Unexpected;142 stream.readNoEof(buf) catch return error.Unexpected;
138}143}
std/os/bits/darwin.zig+59
...@@ -1116,3 +1116,62 @@ pub const stack_t = extern struct {...@@ -1116,3 +1116,62 @@ pub const stack_t = extern struct {
1116 ss_size: isize,1116 ss_size: isize,
1117 ss_flags: i32,1117 ss_flags: i32,
1118};1118};
1119
1120pub const S_IFMT = 0o170000;
1121
1122pub const S_IFIFO = 0o010000;
1123pub const S_IFCHR = 0o020000;
1124pub const S_IFDIR = 0o040000;
1125pub const S_IFBLK = 0o060000;
1126pub const S_IFREG = 0o100000;
1127pub const S_IFLNK = 0o120000;
1128pub const S_IFSOCK = 0o140000;
1129pub const S_IFWHT = 0o160000;
1130
1131pub const S_ISUID = 0o4000;
1132pub const S_ISGID = 0o2000;
1133pub const S_ISVTX = 0o1000;
1134pub const S_IRWXU = 0o700;
1135pub const S_IRUSR = 0o400;
1136pub const S_IWUSR = 0o200;
1137pub const S_IXUSR = 0o100;
1138pub const S_IRWXG = 0o070;
1139pub const S_IRGRP = 0o040;
1140pub const S_IWGRP = 0o020;
1141pub const S_IXGRP = 0o010;
1142pub const S_IRWXO = 0o007;
1143pub const S_IROTH = 0o004;
1144pub const S_IWOTH = 0o002;
1145pub const S_IXOTH = 0o001;
1146
1147pub fn S_ISFIFO(m: u32) bool {
1148 return m & S_IFMT == S_IFIFO;
1149}
1150
1151pub fn S_ISCHR(m: u32) bool {
1152 return m & S_IFMT == S_IFCHR;
1153}
1154
1155pub fn S_ISDIR(m: u32) bool {
1156 return m & S_IFMT == S_IFDIR;
1157}
1158
1159pub fn S_ISBLK(m: u32) bool {
1160 return m & S_IFMT == S_IFBLK;
1161}
1162
1163pub fn S_ISREG(m: u32) bool {
1164 return m & S_IFMT == S_IFREG;
1165}
1166
1167pub fn S_ISLNK(m: u32) bool {
1168 return m & S_IFMT == S_IFLNK;
1169}
1170
1171pub fn S_ISSOCK(m: u32) bool {
1172 return m & S_IFMT == S_IFSOCK;
1173}
1174
1175pub fn S_IWHT(m: u32) bool {
1176 return m & S_IFMT == S_IFWHT;
1177}
std/os/bits/freebsd.zig+59
...@@ -876,3 +876,62 @@ pub const stack_t = extern struct {...@@ -876,3 +876,62 @@ pub const stack_t = extern struct {
876 ss_size: isize,876 ss_size: isize,
877 ss_flags: i32,877 ss_flags: i32,
878};878};
879
880pub const S_IFMT = 0o170000;
881
882pub const S_IFIFO = 0o010000;
883pub const S_IFCHR = 0o020000;
884pub const S_IFDIR = 0o040000;
885pub const S_IFBLK = 0o060000;
886pub const S_IFREG = 0o100000;
887pub const S_IFLNK = 0o120000;
888pub const S_IFSOCK = 0o140000;
889pub const S_IFWHT = 0o160000;
890
891pub const S_ISUID = 0o4000;
892pub const S_ISGID = 0o2000;
893pub const S_ISVTX = 0o1000;
894pub const S_IRWXU = 0o700;
895pub const S_IRUSR = 0o400;
896pub const S_IWUSR = 0o200;
897pub const S_IXUSR = 0o100;
898pub const S_IRWXG = 0o070;
899pub const S_IRGRP = 0o040;
900pub const S_IWGRP = 0o020;
901pub const S_IXGRP = 0o010;
902pub const S_IRWXO = 0o007;
903pub const S_IROTH = 0o004;
904pub const S_IWOTH = 0o002;
905pub const S_IXOTH = 0o001;
906
907pub fn S_ISFIFO(m: u32) bool {
908 return m & S_IFMT == S_IFIFO;
909}
910
911pub fn S_ISCHR(m: u32) bool {
912 return m & S_IFMT == S_IFCHR;
913}
914
915pub fn S_ISDIR(m: u32) bool {
916 return m & S_IFMT == S_IFDIR;
917}
918
919pub fn S_ISBLK(m: u32) bool {
920 return m & S_IFMT == S_IFBLK;
921}
922
923pub fn S_ISREG(m: u32) bool {
924 return m & S_IFMT == S_IFREG;
925}
926
927pub fn S_ISLNK(m: u32) bool {
928 return m & S_IFMT == S_IFLNK;
929}
930
931pub fn S_ISSOCK(m: u32) bool {
932 return m & S_IFMT == S_IFSOCK;
933}
934
935pub fn S_IWHT(m: u32) bool {
936 return m & S_IFMT == S_IFWHT;
937}
std/os/darwin.zig+1
...@@ -5,3 +5,4 @@ pub const is_the_target = switch (builtin.os) {...@@ -5,3 +5,4 @@ pub const is_the_target = switch (builtin.os) {
5 else => false,5 else => false,
6};6};
7pub usingnamespace std.c;7pub usingnamespace std.c;
8pub usingnamespace @import("bits.zig");
\ No newline at end of file
std/os/freebsd.zig+1
...@@ -2,3 +2,4 @@ const std = @import("../std.zig");...@@ -2,3 +2,4 @@ const std = @import("../std.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3pub const is_the_target = builtin.os == .freebsd;3pub const is_the_target = builtin.os == .freebsd;
4pub usingnamespace std.c;4pub usingnamespace std.c;
5pub usingnamespace @import("bits.zig");
\ No newline at end of file