df6b9bbcd01a383a021a770ea19e8533ee21d4aaJosef 'Jeff' Sipek#ifndef FD_UTIL_H
df6b9bbcd01a383a021a770ea19e8533ee21d4aaJosef 'Jeff' Sipek#define FD_UTIL_H
df6b9bbcd01a383a021a770ea19e8533ee21d4aaJosef 'Jeff' Sipek
fa26ad32932ab2ce1b7e3b2b121bb2df886f5736Josef 'Jeff' Sipek/* Change close-on-exec flag of fd. */
fa26ad32932ab2ce1b7e3b2b121bb2df886f5736Josef 'Jeff' Sipekvoid fd_close_on_exec(int fd, bool set);
fa26ad32932ab2ce1b7e3b2b121bb2df886f5736Josef 'Jeff' Sipek
fa26ad32932ab2ce1b7e3b2b121bb2df886f5736Josef 'Jeff' Sipek/* Verify that fds in given range don't exist. */
fa26ad32932ab2ce1b7e3b2b121bb2df886f5736Josef 'Jeff' Sipekvoid fd_debug_verify_leaks(int first_fd, int last_fd);
fa26ad32932ab2ce1b7e3b2b121bb2df886f5736Josef 'Jeff' Sipek
184d3a07c99f4622c16d99efc08d6516c2c8b48aJosef 'Jeff' Sipek/* Set file descriptor to blocking/nonblocking state */
184d3a07c99f4622c16d99efc08d6516c2c8b48aJosef 'Jeff' Sipekvoid fd_set_nonblock(int fd, bool nonblock);
184d3a07c99f4622c16d99efc08d6516c2c8b48aJosef 'Jeff' Sipek
83b799ebdc3297b4b2fb7e7b54b3ea9fa2e5de27Josef 'Jeff' Sipek/* Close fd_in and fd_out, unless they're already -1. They can point to the
83b799ebdc3297b4b2fb7e7b54b3ea9fa2e5de27Josef 'Jeff' Sipek same fd, in which case they're closed only once. If they point to stdin
83b799ebdc3297b4b2fb7e7b54b3ea9fa2e5de27Josef 'Jeff' Sipek or stdout, they're replaced with /dev/null. */
83b799ebdc3297b4b2fb7e7b54b3ea9fa2e5de27Josef 'Jeff' Sipekvoid fd_close_maybe_stdio(int *fd_in, int *fd_out);
83b799ebdc3297b4b2fb7e7b54b3ea9fa2e5de27Josef 'Jeff' Sipek
713e3ec4daebf5e0a4057f925ed1aeb5766debd8Josef 'Jeff' Sipek/* Close the fd and set it to -1. This assert-crashes if fd == 0, and is a
713e3ec4daebf5e0a4057f925ed1aeb5766debd8Josef 'Jeff' Sipek no-op if fd == -1. Normally fd == 0 would happen only if an uninitialized
713e3ec4daebf5e0a4057f925ed1aeb5766debd8Josef 'Jeff' Sipek fd is attempted to be closed, which is a bug. */
e5f373495009b0dafb3670d759137070dd9b807fJosef 'Jeff' Sipekvoid i_close_fd_path(int *fd, const char *path, const char *arg,
e5f373495009b0dafb3670d759137070dd9b807fJosef 'Jeff' Sipek const char *func, const char *file, int line);
e5f373495009b0dafb3670d759137070dd9b807fJosef 'Jeff' Sipek#define i_close_fd_path(fd, path) i_close_fd_path((fd), (path), #fd, __func__, __FILE__, __LINE__)
e5f373495009b0dafb3670d759137070dd9b807fJosef 'Jeff' Sipek#define i_close_fd(fd) i_close_fd_path((fd), NULL)
713e3ec4daebf5e0a4057f925ed1aeb5766debd8Josef 'Jeff' Sipek
df6b9bbcd01a383a021a770ea19e8533ee21d4aaJosef 'Jeff' Sipek#endif