示例#1
0
int
close(int fd)
{
	int		ret;

	/* This is a cancelation point: */
	_thread_enter_cancellation_point();

	if ((fd < 0) || (fd >= _thread_max_fdtsize) ||
	    (fd == _thread_kern_pipe[0]) || (fd == _thread_kern_pipe[1])) {
		errno = EBADF;
		ret = -1;
	} else if ((ret = _FD_LOCK(fd, FD_RDWR_CLOSE, NULL)) != -1) {
		/*
		 * We need to hold the entry spinlock till after
		 * _thread_sys_close() to stop races caused by the
		 * fd state transition.
		 */
		_SPINLOCK(&_thread_fd_table[fd]->lock);

		_thread_fd_entry_close(fd);

		/* Close the file descriptor: */
		ret = _thread_sys_close(fd);

		_SPINUNLOCK(&_thread_fd_table[fd]->lock);

		_FD_UNLOCK(fd, FD_RDWR_CLOSE);
	}

	/* No longer in a cancellation point: */
	_thread_leave_cancellation_point();

	return (ret);
}
示例#2
0
int
_dup(int fd)
{
	int             ret;

	/* Lock the file descriptor: */
	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		/* Perform the 'dup' syscall: */
		if ((ret = __sys_dup(fd)) < 0) {
		}
		/* Initialise the file descriptor table entry: */
		else if (_thread_fd_table_init(ret) != 0) {
			/* Quietly close the file: */
			__sys_close(ret);

			/* Reset the file descriptor: */
			ret = -1;
		} else {
			/*
			 * Save the file open flags so that they can be
			 * checked later: 
			 */
			_thread_fd_setflags(ret, _thread_fd_getflags(fd));
		}

		/* Unlock the file descriptor: */
		_FD_UNLOCK(fd, FD_RDWR);
	}
	/* Return the completion status: */
	return (ret);
}
示例#3
0
ssize_t
_sendmsg(int fd, const struct msghdr *msg, int flags)
{
	struct pthread	*curthread = _get_curthread();
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
		while ((ret = __sys_sendmsg(fd, msg, flags)) < 0) {
			if (!(_thread_fd_getflags(fd) & O_NONBLOCK)
			    && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) {
				curthread->data.fd.fd = fd;

				/* Set the timeout: */
				_thread_kern_set_timeout(NULL);
				curthread->interrupted = 0;
				_thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__);

				/* Check if the operation was interrupted: */
				if (curthread->interrupted) {
					errno = EINTR;
					ret = -1;
					break;
				}
			} else {
				ret = -1;
				break;
			}
		}
		_FD_UNLOCK(fd, FD_WRITE);
	}
	return (ret);
}
示例#4
0
int
ioctl(int fd, unsigned long request,...)
{
	int             ret;
	int		*op;
	va_list         ap;

	/* Lock the file descriptor: */
	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		/* Initialise the variable argument list: */
		va_start(ap, request);

		switch( request) {
		case FIONBIO:
			/*
			 * descriptors must be non-blocking; we are only
			 * twiddling the flag based on the request
			 */
			op = va_arg(ap, int *);
			_thread_fd_table[fd]->status_flags->flags &= ~O_NONBLOCK;
			_thread_fd_table[fd]->status_flags->flags |= ((*op) ? O_NONBLOCK : 0);
			ret = 0;
			break;
		default:
			ret = _thread_sys_ioctl(fd, request, va_arg(ap, char *));
			break;
		}

		/* Free variable arguments: */
		va_end(ap);

		/* Unlock the file descriptor: */
		_FD_UNLOCK(fd, FD_RDWR);
	}
int
_getdirentries(int fd, char *buf, int nbytes, long *basep)
{
    int             ret;

    if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
        ret = __sys_getdirentries(fd, buf, nbytes, basep);
        _FD_UNLOCK(fd, FD_RDWR);
    }
    return (ret);
}
示例#6
0
int
_fchownat(int fd, const char *path, uid_t owner, gid_t group, int flags)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
		ret = __sys_fchownat(fd, path, owner, group, flags);
		_FD_UNLOCK(fd, FD_READ);
	}
	return (ret);
}
int
_getsockname(int s, struct sockaddr * name, socklen_t *namelen)
{
	int             ret;

	if ((ret = _FD_LOCK(s, FD_READ, NULL)) == 0) {
		ret = __sys_getsockname(s, name, namelen);
		_FD_UNLOCK(s, FD_READ);
	}
	return ret;
}
int
_getpeername(int fd, struct sockaddr * peer, socklen_t *paddrlen)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
		ret = __sys_getpeername(fd, peer, paddrlen);
		_FD_UNLOCK(fd, FD_READ);
	}
	return ret;
}
示例#9
0
int
_listen(int fd, int backlog)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		ret = __sys_listen(fd, backlog);
		_FD_UNLOCK(fd, FD_RDWR);
	}
	return (ret);
}
示例#10
0
int
bind(int fd, const struct sockaddr * name, socklen_t namelen)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		ret = _thread_sys_bind(fd, name, namelen);
		_FD_UNLOCK(fd, FD_RDWR);
	}
	return (ret);
}
示例#11
0
int
fchown(int fd, uid_t owner, gid_t group)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
		ret = _thread_sys_fchown(fd, owner, group);
		_FD_UNLOCK(fd, FD_WRITE);
	}
	return (ret);
}
示例#12
0
int
_fchmod(int fd, mode_t mode)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
		ret = __sys_fchmod(fd, mode);
		_FD_UNLOCK(fd, FD_WRITE);
	}
	return (ret);
}
int
___acl_aclcheck_fd(int fd, acl_type_t tp, acl_t acl)
{
	int	error;

	if ((error = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
		error = __sys___acl_aclcheck_fd(fd, tp, (struct acl *)acl);
		_FD_UNLOCK(fd, FD_READ);
	}
	return (error);
}
示例#14
0
int
_faccessat(int fd, const char *path, int mode, int flags)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
		ret = __sys_faccessat(fd, path, mode, flags);
		_FD_UNLOCK(fd, FD_READ);
	}
	return (ret);
}
示例#15
0
int
_flock(int fd, int operation)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		ret = __sys_flock(fd, operation);
		_FD_UNLOCK(fd, FD_RDWR);
	}
	return (ret);
}
示例#16
0
int
_fchflags(int fd, u_long flags)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
		ret = __sys_fchflags(fd, flags);
		_FD_UNLOCK(fd, FD_WRITE);
	}
	return (ret);
}
示例#17
0
long
fpathconf(int fd, int name)
{
	long	ret;

	if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
		ret = _thread_sys_fpathconf(fd, name);
		_FD_UNLOCK(fd, FD_READ);
	}
	return (ret);
}
示例#18
0
int
fchflags(int fd, unsigned int flags)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
		ret = _thread_sys_fchflags(fd, flags);
		_FD_UNLOCK(fd, FD_WRITE);
	}
	return (ret);
}
示例#19
0
ssize_t
recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr * from, socklen_t *from_len)
{
	struct pthread	*curthread = _get_curthread();
	ssize_t		ret;

	/* This is a cancellation point: */
	_thread_enter_cancellation_point();

	if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
		while ((ret = _thread_sys_recvfrom(fd, buf, len, flags, from, from_len)) < 0) {
			if (!(_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) &&
			    !(flags & MSG_DONTWAIT) &&
			    ((errno == EWOULDBLOCK) || (errno == EAGAIN))) {
				curthread->data.fd.fd = fd;

				/* Set the timeout: */
				_thread_kern_set_timeout(_FD_RCVTIMEO(fd));
				curthread->interrupted = 0;
				curthread->closing_fd = 0;
				curthread->timeout = 0;
				_thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__);

				/* Check if the wait was interrupted: */
				if (curthread->interrupted) {
					/* Return an error status: */
					errno = EINTR;
					ret = -1;
					break;
				} else if (curthread->closing_fd) {
					/* Return an error status: */
					errno = EBADF;
					ret = -1;
					break;
				} else if (curthread->timeout) {
					/* Return an error status: */
					errno = EWOULDBLOCK;
					ret = -1;
					break;
				}
			} else {
				ret = -1;
				break;
			}
		}
		_FD_UNLOCK(fd, FD_READ);
	}

	/* No longer in a cancellation point: */
	_thread_leave_cancellation_point();

	return (ret);
}
示例#20
0
int
_getsockopt(int fd, int level, int optname, void *optval, socklen_t
    *optlen)
{
	int             ret;

	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		ret = __sys_getsockopt(fd, level, optname, optval, optlen);
		_FD_UNLOCK(fd, FD_RDWR);
	}
	return ret;
}
示例#21
0
/*
 * This function provides 64-bit offset padding that
 * is not supplied by GCC 1.X but is supplied by GCC 2.X.
 */
int
ftruncate(int fd, off_t length)
{
	int retval;

	if (_FD_LOCK(fd, FD_RDWR, NULL) != 0) {
		retval = -1;
	} else {
		retval = __syscall((quad_t)SYS_ftruncate, fd, 0, length);
		_FD_UNLOCK(fd, FD_RDWR);
	}
	return retval;
}
示例#22
0
int
_fstatfs(int fd, struct statfs * buf)
{
	int             ret;

	/* Lock the file descriptor for read: */
	if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
		/* Get the file system status: */
		ret = __sys_fstatfs(fd, buf);
		/* Unlock the file descriptor: */
		_FD_UNLOCK(fd, FD_READ);
	}
	return (ret);
}
示例#23
0
int
dup(int fd)
{
	int             ret;

	ret = _FD_LOCK(fd, FD_RDWR, NULL);
	if (ret == 0) {
		ret = _thread_sys_dup(fd);
		if (ret != -1) {
			if (_thread_fd_table_init(ret, FD_INIT_DUP,
			    _thread_fd_table[fd]->status_flags) == -1) {
				_thread_sys_close(ret);
				ret = -1;
			}
		}
		_FD_UNLOCK(fd, FD_RDWR);
	}
	return (ret);
}
示例#24
0
ssize_t
read(int fd, void *buf, size_t nbytes)
{
    struct pthread	*curthread = _get_curthread();
    ssize_t	ret;
    int	type;

    /* This is a cancellation point: */
    _thread_enter_cancellation_point();

    /* POSIX says to do just this: */
    if (nbytes == 0)
        ret = 0;

    /* Lock the file descriptor for read: */
    else if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
        /* Get the read/write mode type: */
        type = _thread_fd_table[fd]->status_flags->flags & O_ACCMODE;

        /* Check if the file is not open for read: */
        if (type != O_RDONLY && type != O_RDWR) {
            /* File is not open for read: */
            errno = EBADF;
            ret = -1;
        }

        /* Perform a non-blocking read syscall: */
        else while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) {
                if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0 &&
                        (errno == EWOULDBLOCK || errno == EAGAIN)) {
                    curthread->data.fd.fd = fd;
                    _thread_kern_set_timeout(NULL);

                    /* Reset the interrupted operation flag: */
                    curthread->interrupted = 0;
                    curthread->closing_fd = 0;

                    _thread_kern_sched_state(PS_FDR_WAIT,
                                             __FILE__, __LINE__);

                    /*
                     * Check if the operation was
                     * interrupted by a signal or
                     * a closing fd.
                     */
                    if (curthread->interrupted) {
                        errno = EINTR;
                        ret = -1;
                        break;
                    } else if (curthread->closing_fd) {
                        errno = EBADF;
                        ret = -1;
                        break;
                    }
                } else {
                    break;
                }
            }
        _FD_UNLOCK(fd, FD_READ);
    }

    /* No longer in a cancellation point: */
    _thread_leave_cancellation_point();

    return (ret);
}
示例#25
0
int
connect(int fd, const struct sockaddr * name, socklen_t namelen)
{
	struct pthread	*curthread = _get_curthread();
	struct sockaddr tmpname;
	socklen_t	errnolen, tmpnamelen;
	int             ret;

	/* This is a cancellation point: */
	_thread_enter_cancellation_point();

	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		if ((ret = _thread_sys_connect(fd, name, namelen)) < 0) {
			if (!(_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) &&
			((errno == EWOULDBLOCK) || (errno == EINPROGRESS) ||
			 (errno == EALREADY) || (errno == EAGAIN))) {
				curthread->data.fd.fd = fd;

				/* Reset the interrupted operation flag: */
				curthread->interrupted = 0;
				curthread->closing_fd = 0;

				/* Set the timeout: */
				_thread_kern_set_timeout(NULL);
				_thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__);

				/*
				 * Check if the operation was
				 * interrupted by a signal or
				 * a closing fd.
				 */
				if (curthread->interrupted) {
					errno = EINTR;
					ret = -1;
				} else if (curthread->closing_fd) {
					errno = EBADF;
					ret = -1;
				} else {
					tmpnamelen = sizeof(tmpname);
					/* 0 now lets see if it really worked */
					if (((ret = _thread_sys_getpeername(fd, &tmpname, &tmpnamelen)) < 0) &&
					    (errno == ENOTCONN)) {

						/*
						 * Get the error, this function
						 * should not fail 
						 */
						errnolen = sizeof(errno);
						_thread_sys_getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, &errnolen);
					}
				}
			} else {
				ret = -1;
			}
		}
		_FD_UNLOCK(fd, FD_RDWR);
	}

	/* No longer in a cancellation point: */
	_thread_leave_cancellation_point();

	return (ret);
}
示例#26
0
ssize_t
writev(int fd, const struct iovec * iov, int iovcnt)
{
	struct pthread	*curthread = _get_curthread();
	int	blocking;
	int	idx = 0;
	int	type;
	ssize_t num = 0;
	size_t cnt;
	ssize_t n;
	ssize_t	ret;
	struct iovec liov[20];
	struct iovec *p_iov = liov;

	/* This is a cancellation point: */
	_thread_enter_cancellation_point();

	/* Check if the array size exceeds to compiled in size: */
	if (iovcnt > (int) (sizeof(liov) / sizeof(struct iovec))) {
		/* Allocate memory for the local array: */
		if ((p_iov = (struct iovec *)
		    malloc((size_t)iovcnt * sizeof(struct iovec))) == NULL) {
			/* Insufficient memory: */
			errno = ENOMEM;
			_thread_leave_cancellation_point();
			return (-1);
		}
	} else if (iovcnt <= 0) {
		errno = EINVAL;
		_thread_leave_cancellation_point();
		return (-1);
	}

	/* Copy the caller's array so that it can be modified locally: */
	memcpy(p_iov,iov,(size_t)iovcnt * sizeof(struct iovec));

	/* Lock the file descriptor for write: */
	if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
		/* Get the read/write mode type: */
		type = _thread_fd_table[fd]->status_flags->flags & O_ACCMODE;

		/* Check if the file is not open for write: */
		if (type != O_WRONLY && type != O_RDWR) {
			/* File is not open for write: */
			errno = EBADF;
			_FD_UNLOCK(fd, FD_WRITE);
			if (p_iov != liov)
				free(p_iov);
			_thread_leave_cancellation_point();
			return (-1);
		}

		/* Check if file operations are to block */
		blocking = ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0);

		/*
		 * Loop while no error occurs and until the expected number
		 * of bytes are written if performing a blocking write:
		 */
		while (ret == 0) {
			/* Perform a non-blocking write syscall: */
			n = _thread_sys_writev(fd, &p_iov[idx], iovcnt - idx);

			/* Check if one or more bytes were written: */
			if (n > 0) {
				/*
				 * Keep a count of the number of bytes
				 * written:
				 */
				num += n;

				/*
				 * Enter a loop to check if a short write
				 * occurred and move the index to the
				 * array entry where the short write
				 * ended:
				 */
				cnt = (size_t)n;
				while (cnt > 0 && idx < iovcnt) {
					/*
					 * If the residual count exceeds
					 * the size of this vector, then
					 * it was completely written:
					 */
					if (cnt >= p_iov[idx].iov_len)
						/*
						 * Decrement the residual
						 * count and increment the
						 * index to the next array
						 * entry:
						 */
						cnt -= p_iov[idx++].iov_len;
					else {
						/*
						 * This entry was only
						 * partially written, so
						 * adjust it's length
						 * and base pointer ready
						 * for the next write:
						 */
						p_iov[idx].iov_len -= cnt;
						p_iov[idx].iov_base =
						    (char *)p_iov[idx].iov_base
						    + (ptrdiff_t)cnt;
						cnt = 0;
					}
				}
			} else if (n == 0) {
				/*
				 * Avoid an infinite loop if the last iov_len is
				 * 0.
				 */
				while (idx < iovcnt && p_iov[idx].iov_len == 0)
					idx++;

				if (idx == iovcnt) {
					ret = num;
					break;
				}
			}

			/*
			 * If performing a blocking write, check if the
			 * write would have blocked or if some bytes
			 * were written but there are still more to
			 * write:
			 */
			if (blocking && ((n < 0 && (errno == EWOULDBLOCK ||
			    errno == EAGAIN)) || (n >= 0 && idx < iovcnt))) {
				curthread->data.fd.fd = fd;
				_thread_kern_set_timeout(NULL);

				/* Reset the interrupted operation flag: */
				curthread->interrupted = 0;
				curthread->closing_fd = 0;

				_thread_kern_sched_state(PS_FDW_WAIT,
				    __FILE__, __LINE__);

				/*
				 * Check if the operation was
				 * interrupted by a signal
				 */
				if (curthread->interrupted || curthread->closing_fd) {
					if (num > 0) {
						/* Return partial success: */
						ret = num;
					} else {
						/* Return an error: */
						if (curthread->closing_fd)
							errno = EBADF;
						else
							errno = EINTR;
						ret = -1;
					}
				}

			/*
			 * If performing a non-blocking write,
			 * just return whatever the write syscall did:
			 */
			} else if (!blocking) {
				/* A non-blocking call might return zero: */
				ret = n;
				break;

			/*
			 * If there was an error, return partial success
			 * (if any bytes were written) or else the error:
			 */
			} else if (n < 0) {
				if (num > 0)
					ret = num;
				else
					ret = n;

			/* Check if the write has completed: */
			} else if (idx == iovcnt)
				/* Return the number of bytes written: */
				ret = num;
		}
		_FD_UNLOCK(fd, FD_WRITE);
	}

	/* If memory was allocated for the array, free it: */
	if (p_iov != liov)
		free(p_iov);

	/* No longer in a cancellation point: */
	_thread_leave_cancellation_point();

	return (ret);
}
示例#27
0
int
closefrom(int fd)
{
	int ret = 0;
	int safe_fd;
	int lock_fd;
	int *flags;

	_thread_enter_cancellation_point();
	
	if (fd < 0 || fd >= _thread_max_fdtsize) {
		errno = EBADF;
		ret = -1;
	} else {
		safe_fd = _thread_kern_pipe[0] > _thread_kern_pipe[1] ?
			_thread_kern_pipe[0] : _thread_kern_pipe[1];

		/*
		 * close individual files until we get past the pipe
		 * fds.  Attempting to close a pipe fd is a no-op.
		 */
		for (safe_fd++; fd < safe_fd; fd++)
			close(fd);

		flags = calloc((size_t)_thread_max_fdtsize, sizeof *flags);
		if (flags == NULL) {
			/* use calloc errno */
			ret = -1;
		} else {
			/* Lock and record all fd entries */
			for (lock_fd = fd; lock_fd < _thread_max_fdtsize; lock_fd++) {
				if (_thread_fd_table[lock_fd] != NULL &&
			   	 _thread_fd_table[lock_fd]->state != FD_ENTRY_CLOSED) {
					ret = _FD_LOCK(lock_fd, FD_RDWR_CLOSE, NULL);
					if (ret != -1)
						flags[lock_fd] = 1;
					else
						break;
				}
			}

			if (ret != -1) {
				/*
				 * Close the entries and reset the non-bocking
				 * flag when needed.
				 */
				for (lock_fd = fd; lock_fd < _thread_max_fdtsize; lock_fd++) {
					if (flags[lock_fd] != 0) {
						_thread_fd_entry_close(lock_fd);
					}
				}
				/*
				 * Now let the system do its thing. It is not practical
				 * to try to prevent races with other threads that can
				 * create new file descriptors. We just have to assume
				 * the application is well behaved when using closefrom.
				 */
				ret = _thread_sys_closefrom(fd);
			}

			/*
			 * Unlock any locked entries.
			 */
			for (lock_fd = fd; lock_fd < _thread_max_fdtsize; lock_fd++) {
				if (flags[lock_fd] != 0) {
					_FD_UNLOCK(lock_fd, FD_RDWR_CLOSE);
				}
			}
			free(flags);
		}
	}

	_thread_leave_cancellation_point();

	return (ret);

}
示例#28
0
int
accept(int fd, struct sockaddr * name, socklen_t *namelen)
{
	struct pthread	*curthread = _get_curthread();
	int             ret;
	int		newfd;
	enum fd_entry_mode init_mode;

	/* This is a cancellation point: */
	_thread_enter_cancellation_point();

	/* Lock the file descriptor: */
	if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
		/* Enter a loop to wait for a connection request: */
		while ((ret = _thread_sys_accept(fd, name, namelen)) < 0) {
			/* Check if the socket is to block: */
			if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0 &&
			    (errno == EWOULDBLOCK || errno == EAGAIN)) {
				/* Save the socket file descriptor: */
				curthread->data.fd.fd = fd;
				curthread->data.fd.fname = __FILE__;
				curthread->data.fd.branch = __LINE__;

				/* Set the timeout: */
				_thread_kern_set_timeout(NULL);
				curthread->interrupted = 0;
				curthread->closing_fd = 0;

				/* Schedule the next thread: */
				_thread_kern_sched_state(PS_FDR_WAIT, __FILE__,
							 __LINE__);

				/* Check if the wait was interrupted: */
				if (curthread->interrupted) {
					/* Return an error status: */
					errno = EINTR;
					ret = -1;
					break;
				} else if (curthread->closing_fd) {
					/* Return an error status: */
					errno = EBADF;
					ret = -1;
					break;
				}
			} else {
				/*
				 * Another error has occurred, so exit the
				 * loop here: 
				 */
				break;
			}
		}

		/*
		 * If no errors initialize the file descriptor table
		 * for the new socket. If the client's view of the
		 * status_flags for fd is blocking, then force newfd
		 * to be viewed as blocking too.
		 */
		if (ret != -1) {
			newfd = ret;

			if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0)
				init_mode = FD_INIT_BLOCKING;
			else
				init_mode = FD_INIT_NEW;
			if((ret = _thread_fd_table_init(newfd, init_mode, NULL)) != -1)
				ret = newfd;
			else {
				/* quitely close the fd */
				_thread_sys_close(ret);
			}
		}

		/* Unlock the file descriptor: */
		_FD_UNLOCK(fd, FD_RDWR);
	}

	/* No longer in a cancellation point: */
	_thread_leave_cancellation_point();

	/* Return the socket file descriptor or -1 on error: */
	return (ret);
}