Beispiel #1
0
void print_connections() {
    struct connection *node = connection_tbl[0]->next;
    // get the current position
    FILE *file = fopen("connections.out","w");
    fflush(file);
    if (fgetpos(file,&pos) == -1)
    {
        fprintf(file,"Unable to refresh screen\n");
        fclose(file);
    }
    fprintf(file,"------------------------\n");
    fprintf(file,"Printing Connection Info\n");
    fprintf(file,"------------------------\n");
    fprintf(file,"Connection: FD \t STAT \t APPID \n");
    while(node->next != NULL) {
        fprintf(file,"Connection: %d \t %s \t %d \n", node->sockfd,  convert_status(node->status), node->app_guid);
        node = node->next;
    }
    fprintf(file, "------------------------\n");
    if (fsetpos(file,&pos) == -1)
    {
        fprintf(file,"Unable to refresh screen\n");
        fclose(file);
    }
    fclose(file);
}
Beispiel #2
0
nfsstat3 set_fattr(nfs_fh3 object, const sattr3 *attrs) {
	nfsstat3 status = NFS3_OK;
	int error = 0;
	const char *path = NULL;
	const kfsfilesystem_t *filesystem = get_filesystem(object, &path, NULL);
	if (filesystem) {
		dlog("\t%s (path, setattr)", path);

		// check for resize
		if (status == NFS3_OK && attrs->size.set_it) {
			if (!filesystem->truncate(path, attrs->size.set_size3_u.size, &error, filesystem->context)) {
				status = convert_status(error, NFS3ERR_NOENT); // truncate failed
			}
		}

		// check for mode change
		if (status == NFS3_OK && attrs->mode.set_it) {
			kfsmode_t mode = 0;
			if (attrs->mode.set_mode3_u.mode & NFS_IRUSR) { mode |= KFS_IRUSR; }
			if (attrs->mode.set_mode3_u.mode & NFS_IWUSR) { mode |= KFS_IWUSR; }
			if (attrs->mode.set_mode3_u.mode & NFS_IXUSR) { mode |= KFS_IXUSR; }
			if (attrs->mode.set_mode3_u.mode & NFS_IRGRP) { mode |= KFS_IRGRP; }
			if (attrs->mode.set_mode3_u.mode & NFS_IWGRP) { mode |= KFS_IWGRP; }
			if (attrs->mode.set_mode3_u.mode & NFS_IXGRP) { mode |= KFS_IXGRP; }
			if (attrs->mode.set_mode3_u.mode & NFS_IROTH) { mode |= KFS_IROTH; }
			if (attrs->mode.set_mode3_u.mode & NFS_IWOTH) { mode |= KFS_IWOTH; }
			if (attrs->mode.set_mode3_u.mode & NFS_IXOTH) { mode |= KFS_IXOTH; }
			
			if (!filesystem->chmod(path, mode, &error, filesystem->context)) {
				status = convert_status(error, NFS3ERR_NOENT); // chmod failed
			}
		}
		
		// set times
		if (status == NFS3_OK && (attrs->atime.set_it || attrs->mtime.set_it)) {
			kfstime_t *atime = attrs->atime.set_it ? &(kfstime_t){
				.sec = attrs->atime.set_atime_u.atime.seconds,
				.nsec = attrs->atime.set_atime_u.atime.nseconds
			} : NULL;
			kfstime_t *mtime = attrs->mtime.set_it ? &(kfstime_t){
				.sec = attrs->mtime.set_mtime_u.mtime.seconds,
				.nsec = attrs->mtime.set_mtime_u.mtime.nseconds
			} : NULL;
Beispiel #3
0
nfsstat3 get_fattr(nfs_fh3 object, fattr3 *result) {
	nfsstat3 status = NFS3_OK;
	uint64_t identifier = 0;
	int error = 0;
	const char *path = NULL;
	const kfsfilesystem_t *filesystem = get_filesystem(object, &path, &identifier);
	if (filesystem) {
		dlog("\t%s (path, getattr)", path);
		
		kfsstat_t sbuf = {};
		if (filesystem->stat(path, &sbuf, &error, filesystem->context)) {
			*result = (fattr3){};
			
			if (0) {}
			else if (sbuf.type == KFS_REG) { result->type = NF3REG; }
			else if (sbuf.type == KFS_DIR) { result->type = NF3DIR; }
			else if (sbuf.type == KFS_BLK) { result->type = NF3BLK; }
			else if (sbuf.type == KFS_CHR) { result->type = NF3CHR; }
			else if (sbuf.type == KFS_LNK) { result->type = NF3LNK; }
			else if (sbuf.type == KFS_SOCK) { result->type = NF3SOCK; }
			else if (sbuf.type == KFS_FIFO) { result->type = NF3FIFO; }
			
			if (sbuf.mode & KFS_IRUSR) { result->mode |= NFS_IRUSR; }
			if (sbuf.mode & KFS_IWUSR) { result->mode |= NFS_IWUSR; }
			if (sbuf.mode & KFS_IXUSR) { result->mode |= NFS_IXUSR; }
			if (sbuf.mode & KFS_IRGRP) { result->mode |= NFS_IRGRP; }
			if (sbuf.mode & KFS_IWGRP) { result->mode |= NFS_IWGRP; }
			if (sbuf.mode & KFS_IXGRP) { result->mode |= NFS_IXGRP; }
			if (sbuf.mode & KFS_IROTH) { result->mode |= NFS_IROTH; }
			if (sbuf.mode & KFS_IWOTH) { result->mode |= NFS_IWOTH; }
			if (sbuf.mode & KFS_IXOTH) { result->mode |= NFS_IXOTH; }
			
			result->nlink = 1;
			result->uid = getuid();
			result->gid = getgid();
			result->size = sbuf.size;
			result->used = sbuf.used;
			result->rdev = (specdata3){ 0, 0 };
			result->fsid = 0;
			result->fileid = kfs_fileid(identifier, path);
			result->atime = (nfstime3){ sbuf.atime.sec, sbuf.atime.nsec };
			result->mtime = (nfstime3){ sbuf.mtime.sec, sbuf.mtime.nsec };
			result->ctime = (nfstime3){ sbuf.ctime.sec, sbuf.ctime.nsec };
		} else { // stat failed
			status = convert_status(error, NFS3ERR_NOENT);
		}
	} else { // no filesystem
		status = NFS3ERR_BADHANDLE;
	}

	return status;
}
Beispiel #4
0
static int
xspi_ioctl(struct inode *inode, struct file *filp,
	   unsigned int cmd, unsigned long arg)
{
	struct xspi_instance *dev = filp->private_data;

	/* paranoia check */
	if (!dev)
		return -ENODEV;

	switch (cmd) {
	case XSPI_IOC_GETSLAVESELECT:
		{
			int i;

			i = ffs(XSpi_GetSlaveSelect(&dev->Spi)) - 1;
			return put_user(i, (int *) arg);	/* -1 means nothing selected */
		}
		break;
	case XSPI_IOC_SETSLAVESELECT:
		{
			int i;
			int retval;

			if (get_user(i, (int *) arg) != 0)
				return -EFAULT;

			if (i < -1 || i > 31)
				return -EINVAL;

			/* Lock the device. */
			if (down_interruptible(&dev->sem))
				return -ERESTARTSYS;

			if (i == -1)
				retval =
				    convert_status(XSpi_SetSlaveSelect
						   (&dev->Spi, 0));
			else
				retval =
				    convert_status(XSpi_SetSlaveSelect
						   (&dev->Spi, (u32) 1 << i));

			/* Unlock the device. */
			up(&dev->sem);

			return retval;
		}
		break;
	case XSPI_IOC_GETOPTS:
		{
			struct xspi_ioc_options xspi_opts;
			u32 xspi_options;

			xspi_options = XSpi_GetOptions(&dev->Spi);

			memset(&xspi_opts, 0, sizeof (xspi_opts));
			if (dev->Spi.HasFifos)
				xspi_opts.has_fifo = 1;
			if (xspi_options & XSP_CLK_ACTIVE_LOW_OPTION)
				xspi_opts.clk_level = 1;
			if (xspi_options & XSP_CLK_PHASE_1_OPTION)
				xspi_opts.clk_phase = 1;
			if (xspi_options & XSP_LOOPBACK_OPTION)
				xspi_opts.loopback = 1;
			xspi_opts.slave_selects = dev->Spi.NumSlaveBits;

			return put_user(xspi_opts,
					(struct xspi_ioc_options *) arg);
		}
		break;
	case XSPI_IOC_SETOPTS:
		{
			struct xspi_ioc_options xspi_opts;
			u32 xspi_options;
			int retval;

			if (copy_from_user(&xspi_opts,
					   (struct xspi_ioc_options *) arg,
					   sizeof (struct xspi_ioc_options)) !=
			    0)
				return -EFAULT;

			/* Lock the device. */
			if (down_interruptible(&dev->sem))
				return -ERESTARTSYS;

			/* Read current settings and set the changeable ones. */
			xspi_options = XSpi_GetOptions(&dev->Spi)
			    & ~XSPI_CHANGEABLE_OPTIONS;
			if (xspi_opts.clk_level != 0)
				xspi_options |= XSP_CLK_ACTIVE_LOW_OPTION;
			if (xspi_opts.clk_phase != 0)
				xspi_options |= XSP_CLK_PHASE_1_OPTION;
			if (xspi_opts.loopback != 0)
				xspi_options |= XSP_LOOPBACK_OPTION;

			retval =
			    convert_status(XSpi_SetOptions
					   (&dev->Spi, xspi_options));

			/* Unlock the device. */
			up(&dev->sem);

			return retval;
		}
		break;
	case XSPI_IOC_TRANSFER:
		{
			struct xspi_ioc_transfer_data trans_data;
			int retval;

			if (copy_from_user(&trans_data,
					   (struct xspi_ioc_transfer_data *)
					   arg,
					   sizeof (struct
						   xspi_ioc_transfer_data)) !=
			    0)
				return -EFAULT;

			/* Transfer the data. */
			retval = xspi_transfer(dev, trans_data.write_buf,
					       trans_data.read_buf,
					       trans_data.count,
					       trans_data.slave_index);
			if (retval > 0)
				return 0;
			else
				return retval;
		}
		break;
	default:
		return -ENOTTY;	/* redundant */
	}			/* switch(cmd) */

	return -ENOTTY;
}
Beispiel #5
0
/*
 * To be called from xspi_ioctl(), xspi_read(), and xspi_write().
 *
 * xspi_ioctl() uses both wr_buf and rd_buf.
 * xspi_read() doesn't care of what is sent, and sets wr_buf to NULL.
 * xspi_write() doesn't care of what it receives, and sets rd_buf to NULL.
 *
 * Set slave_ind to negative value if the currently selected SPI slave
 * device is to be used.
 *
 * Returns the number of bytes transferred (0 or positive value)
 * or error code (negative value).
 */
static int xspi_transfer(struct xspi_instance *dev, const char *wr_buf,
			 char *rd_buf, int count, int slave_ind)
{
	int retval;
	unsigned char *tmp_buf;

	if (count <= 0)
		return 0;

	/* Limit the count value to the small enough one.
	   This prevents a denial-of-service attack by using huge count values
	   thus making everything to be swapped out to free the space
	   for this huge buffer */
	if (count > 8192)
		count = 8192;

	/* Allocate buffer in the kernel space (it is first filled with
	   the data to send, then these data are overwritten with the
	   received data) */
	tmp_buf = kmalloc(count, GFP_KERNEL);
	if (tmp_buf == NULL)
		return -ENOMEM;

	/* Fill the buffer with data to send */
	if (wr_buf == NULL) {
		/* zero the buffer not to expose the kernel data */
		memset(tmp_buf, 0, count);
	} else {
		if (copy_from_user(tmp_buf, wr_buf, count) != 0) {
			kfree(tmp_buf);
			return -EFAULT;
		}
	}

	/* Lock the device */
	if (down_interruptible(&dev->sem)) {
		kfree(tmp_buf);
		return -ERESTARTSYS;
	}

	/* The while cycle below never loops - this is just a convenient
	   way to handle the errors */
	while (TRUE) {
		/* Select the proper slave if requested to do so */
		if (slave_ind >= 0) {
			retval =
			    convert_status(XSpi_SetSlaveSelect
					   (&dev->Spi,
					    0x00000001 << slave_ind));
			if (retval != 0)
				break;
		}

		/* Initiate transfer */
		dev->completion_status = 0;
		retval = convert_status(XSpi_Transfer(&dev->Spi, tmp_buf,
						      (rd_buf ==
						       NULL) ? NULL : tmp_buf,
						      count));
		if (retval != 0)
			break;

		/* Put the process to sleep */
		if (wait_event_interruptible(dev->waitq,
					     dev->completion_status != 0) !=
		    0) {
			/* ... woken up by the signal */
			retval = -ERESTARTSYS;
			break;
		}
		/* ... woken up by the transfer completed interrupt */
		if (dev->completion_status != XST_SPI_TRANSFER_DONE) {
			retval = -EIO;
			break;
		}

		/* Copy the received data to user if rd_buf != NULL */
		if (rd_buf != NULL &&
		    copy_to_user(rd_buf, tmp_buf, dev->tx_count) != 0) {
			retval = -EFAULT;
			break;
		}

		retval = dev->tx_count;
		break;
	}			/* while(TRUE) */

	/* Unlock the device, free the buffer and return */
	up(&dev->sem);
	kfree(tmp_buf);
	return retval;
}