authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-07 12:11:15-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-07 12:11:15-05:00
log7f4cce3345baab10ff51e898c3810ee58e6bd88b
treef767f931f7f974739cd2e03bdf9e9c0ef70dc641
parent71873e71338fcdecf248d0fb994918ee943cd2e6

add fcntl support on darwin


3 files changed, 161 insertions(+), 9 deletions(-)

lib/std/c.zig+1
...@@ -121,6 +121,7 @@ pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*u...@@ -121,6 +121,7 @@ pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*u
121pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;121pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
122pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int;122pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int;
123pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int;123pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int;
124pub extern "c" fn fcntl(fd: fd_t, cmd: c_int, ...) c_int;
124125
125pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;126pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
126pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;127pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
lib/std/os.zig+2-9
...@@ -2529,15 +2529,8 @@ pub fn pipe() PipeError![2]fd_t {...@@ -2529,15 +2529,8 @@ pub fn pipe() PipeError![2]fd_t {
25292529
2530pub fn pipe2(flags: u32) PipeError![2]fd_t {2530pub fn pipe2(flags: u32) PipeError![2]fd_t {
2531 if (comptime std.Target.current.isDarwin()) {2531 if (comptime std.Target.current.isDarwin()) {
2532 var fds: [2]fd_t = undefined;2532 var fds: [2]fd_t = try pipe();
2533 switch (errno(system.pipe(&fds, flags))) {2533 if (flags == 0) return fds;
2534 0 => {},
2535 EINVAL => unreachable, // Invalid flags
2536 EFAULT => unreachable, // Invalid fds pointer
2537 ENFILE => return error.SystemFdQuotaExceeded,
2538 EMFILE => return error.ProcessFdQuotaExceeded,
2539 else => |err| return unexpectedErrno(err),
2540 }
2541 errdefer {2534 errdefer {
2542 close(fds[0]);2535 close(fds[0]);
2543 close(fds[1]);2536 close(fds[1]);
lib/std/os/bits/darwin.zig+158
...@@ -1224,3 +1224,161 @@ pub const RTLD_NEXT = @intToPtr(*c_void, ~maxInt(usize));...@@ -1224,3 +1224,161 @@ pub const RTLD_NEXT = @intToPtr(*c_void, ~maxInt(usize));
1224pub const RTLD_DEFAULT = @intToPtr(*c_void, ~maxInt(usize) - 1);1224pub const RTLD_DEFAULT = @intToPtr(*c_void, ~maxInt(usize) - 1);
1225pub const RTLD_SELF = @intToPtr(*c_void, ~maxInt(usize) - 2);1225pub const RTLD_SELF = @intToPtr(*c_void, ~maxInt(usize) - 2);
1226pub const RTLD_MAIN_ONLY = @intToPtr(*c_void, ~maxInt(usize) - 4);1226pub const RTLD_MAIN_ONLY = @intToPtr(*c_void, ~maxInt(usize) - 4);
1227
1228/// duplicate file descriptor
1229pub const F_DUPFD = 0;
1230
1231/// get file descriptor flags
1232pub const F_GETFD = 1;
1233
1234/// set file descriptor flags
1235pub const F_SETFD = 2;
1236
1237/// get file status flags
1238pub const F_GETFL = 3;
1239
1240/// set file status flags
1241pub const F_SETFL = 4;
1242
1243/// get SIGIO/SIGURG proc/pgrp
1244pub const F_GETOWN = 5;
1245
1246/// set SIGIO/SIGURG proc/pgrp
1247pub const F_SETOWN = 6;
1248
1249/// get record locking information
1250pub const F_GETLK = 7;
1251
1252/// set record locking information
1253pub const F_SETLK = 8;
1254
1255/// F_SETLK; wait if blocked
1256pub const F_SETLKW = 9;
1257
1258/// F_SETLK; wait if blocked, return on timeout
1259pub const F_SETLKWTIMEOUT = 10;
1260pub const F_FLUSH_DATA = 40;
1261
1262/// Used for regression test
1263pub const F_CHKCLEAN = 41;
1264
1265/// Preallocate storage
1266pub const F_PREALLOCATE = 42;
1267
1268/// Truncate a file without zeroing space
1269pub const F_SETSIZE = 43;
1270
1271/// Issue an advisory read async with no copy to user
1272pub const F_RDADVISE = 44;
1273
1274/// turn read ahead off/on for this fd
1275pub const F_RDAHEAD = 45;
1276
1277/// turn data caching off/on for this fd
1278pub const F_NOCACHE = 48;
1279
1280/// file offset to device offset
1281pub const F_LOG2PHYS = 49;
1282
1283/// return the full path of the fd
1284pub const F_GETPATH = 50;
1285
1286/// fsync + ask the drive to flush to the media
1287pub const F_FULLFSYNC = 51;
1288
1289/// find which component (if any) is a package
1290pub const F_PATHPKG_CHECK = 52;
1291
1292/// "freeze" all fs operations
1293pub const F_FREEZE_FS = 53;
1294
1295/// "thaw" all fs operations
1296pub const F_THAW_FS = 54;
1297
1298/// turn data caching off/on (globally) for this file
1299pub const F_GLOBAL_NOCACHE = 55;
1300
1301/// add detached signatures
1302pub const F_ADDSIGS = 59;
1303
1304/// add signature from same file (used by dyld for shared libs)
1305pub const F_ADDFILESIGS = 61;
1306
1307/// used in conjunction with F_NOCACHE to indicate that DIRECT, synchonous writes
1308/// should not be used (i.e. its ok to temporaily create cached pages)
1309pub const F_NODIRECT = 62;
1310
1311///Get the protection class of a file from the EA, returns int
1312pub const F_GETPROTECTIONCLASS = 63;
1313
1314///Set the protection class of a file for the EA, requires int
1315pub const F_SETPROTECTIONCLASS = 64;
1316
1317///file offset to device offset, extended
1318pub const F_LOG2PHYS_EXT = 65;
1319
1320///get record locking information, per-process
1321pub const F_GETLKPID = 66;
1322
1323///Mark the file as being the backing store for another filesystem
1324pub const F_SETBACKINGSTORE = 70;
1325
1326///return the full path of the FD, but error in specific mtmd circumstances
1327pub const F_GETPATH_MTMINFO = 71;
1328
1329///Returns the code directory, with associated hashes, to the caller
1330pub const F_GETCODEDIR = 72;
1331
1332///No SIGPIPE generated on EPIPE
1333pub const F_SETNOSIGPIPE = 73;
1334
1335///Status of SIGPIPE for this fd
1336pub const F_GETNOSIGPIPE = 74;
1337
1338///For some cases, we need to rewrap the key for AKS/MKB
1339pub const F_TRANSCODEKEY = 75;
1340
1341///file being written to a by single writer... if throttling enabled, writes
1342///may be broken into smaller chunks with throttling in between
1343pub const F_SINGLE_WRITER = 76;
1344
1345///Get the protection version number for this filesystem
1346pub const F_GETPROTECTIONLEVEL = 77;
1347
1348///Add detached code signatures (used by dyld for shared libs)
1349pub const F_FINDSIGS = 78;
1350
1351///Add signature from same file, only if it is signed by Apple (used by dyld for simulator)
1352pub const F_ADDFILESIGS_FOR_DYLD_SIM = 83;
1353
1354///fsync + issue barrier to drive
1355pub const F_BARRIERFSYNC = 85;
1356
1357///Add signature from same file, return end offset in structure on success
1358pub const F_ADDFILESIGS_RETURN = 97;
1359
1360///Check if Library Validation allows this Mach-O file to be mapped into the calling process
1361pub const F_CHECK_LV = 98;
1362
1363///Deallocate a range of the file
1364pub const F_PUNCHHOLE = 99;
1365
1366///Trim an active file
1367pub const F_TRIM_ACTIVE_FILE = 100;
1368
1369pub const FCNTL_FS_SPECIFIC_BASE = 0x00010000;
1370
1371///mark the dup with FD_CLOEXEC
1372pub const F_DUPFD_CLOEXEC = 67;
1373
1374///close-on-exec flag
1375pub const FD_CLOEXEC = 1;
1376
1377/// shared or read lock
1378pub const F_RDLCK = 1;
1379
1380/// unlock
1381pub const F_UNLCK = 2;
1382
1383/// exclusive or write lock
1384pub const F_WRLCK = 3;