Example #1
0
int
cloudabi_sys_fd_stat_get(struct thread *td,
    struct cloudabi_sys_fd_stat_get_args *uap)
{
	cloudabi_fdstat_t fsb = {};
	struct filedesc *fdp;
	struct file *fp;
	seq_t seq;
	cap_rights_t rights;
	int error, oflags;
	bool modified;

	/* Obtain file descriptor properties. */
	fdp = td->td_proc->p_fd;
	do {
		error = fget_unlocked(fdp, uap->fd, cap_rights_init(&rights),
		    &fp, &seq);
		if (error != 0)
			return (error);
		if (fp->f_ops == &badfileops) {
			fdrop(fp, td);
			return (EBADF);
		}

		rights = *cap_rights(fdp, uap->fd);
		oflags = OFLAGS(fp->f_flag);
		fsb.fs_filetype = cloudabi_convert_filetype(fp);

		modified = fd_modified(fdp, uap->fd, seq);
		fdrop(fp, td);
	} while (modified);

	/* Convert file descriptor flags. */
	if (oflags & O_APPEND)
		fsb.fs_flags |= CLOUDABI_FDFLAG_APPEND;
	if (oflags & O_NONBLOCK)
		fsb.fs_flags |= CLOUDABI_FDFLAG_NONBLOCK;
	if (oflags & O_SYNC)
		fsb.fs_flags |= CLOUDABI_FDFLAG_SYNC;

	/* Convert capabilities to CloudABI rights. */
	convert_capabilities(&rights, fsb.fs_filetype,
	    &fsb.fs_rights_base, &fsb.fs_rights_inheriting);
	return (copyout(&fsb, (void *)uap->buf, sizeof(fsb)));
}
Example #2
0
/*
 * Convert a user file descriptor to a kernel file entry and check if required
 * capability rights are present.
 * A reference on the file entry is held upon returning.
 */
int
getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
    struct file **fpp, u_int *fflagp)
{
	struct file *fp;
	int error;

	error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
	if (error != 0)
		return (error);
	if (fp->f_type != DTYPE_SOCKET) {
		fdrop(fp, td);
		return (ENOTSOCK);
	}
	if (fflagp != NULL)
		*fflagp = fp->f_flag;
	*fpp = fp;
	return (0);
}