def close_filenos(preserve):
    """ Close unprotected file descriptors

    Close all open file descriptors that are not in preserve.

    If ulimit -nofile is "unlimited", all is defined filenos <= 4096,
    else all is <= the output of resource.getrlimit().

    :param preserve: set with protected files
    :type preserve: set

    :return: None
    """
    maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
    if maxfd == resource.RLIM_INFINITY:
        maxfd = 4096
    for fileno in range(maxfd):
        if fileno not in preserve:
            try:
                os.close(fileno)
            except OSError as err:
                if not err.errno == errno.EBADF:
                    raise DaemonError(
                        'Failed to close file descriptor {0}: {1}'
                        .format(fileno, err))
Close unprotected file descriptors

    Close all open file descriptors that are not in preserve.

    If ulimit -nofile is "unlimited", all is defined filenos <= 4096,
    else all is <= the output of resource.getrlimit().

    :param preserve: set with protected files
    :type preserve: set

    :return: None