[minor] Implement opening and closing of file descriptors & flag handling