Cosmetics, cosmetics and cosmetics.

This commit is contained in:
Godzil 2012-07-10 14:06:00 +02:00
parent 0678a66b3c
commit fd1f2a7374

View File

@ -104,7 +104,8 @@ void fusd_init()
{ {
static int fusd_init_needed = 1; static int fusd_init_needed = 1;
if (fusd_init_needed) { if (fusd_init_needed)
{
int i; int i;
fusd_init_needed = 0; fusd_init_needed = 0;
@ -128,7 +129,8 @@ int fusd_register(const char *name, const char* clazz, const char* devname, mode
fusd_init(); fusd_init();
/* make sure the name is valid and we have a valid set of fops... */ /* make sure the name is valid and we have a valid set of fops... */
if (name == NULL || fops == NULL) { if (name == NULL || fops == NULL)
{
fprintf(stderr, "fusd_register: invalid name or fops argument\n"); fprintf(stderr, "fusd_register: invalid name or fops argument\n");
retval = -EINVAL; retval = -EINVAL;
goto done; goto done;
@ -139,27 +141,33 @@ int fusd_register(const char *name, const char* clazz, const char* devname, mode
* to register are SKIP_PREFIX (usually "/dev/"), skip over them. * to register are SKIP_PREFIX (usually "/dev/"), skip over them.
*/ */
if (dev_root != NULL && strlen(name) > strlen(dev_root) && if (dev_root != NULL && strlen(name) > strlen(dev_root) &&
!strncmp(name, dev_root, strlen(dev_root))) { !strncmp(name, dev_root, strlen(dev_root)))
{
name += strlen(dev_root); name += strlen(dev_root);
} }
if (strlen(name) > FUSD_MAX_NAME_LENGTH) { if (strlen(name) > FUSD_MAX_NAME_LENGTH)
fprintf(stderr, "name '%s' too long, sorry :(", name); {
fprintf(stderr, "libfusd: name '%s' too long, sorry :(\n", name);
retval = -EINVAL; retval = -EINVAL;
goto done; goto done;
} }
/* open the fusd control channel */ /* open the fusd control channel */
if ((fd = open(FUSD_CONTROL_DEVNAME, O_RDWR | O_NONBLOCK)) < 0) { if ((fd = open(FUSD_CONTROL_DEVNAME, O_RDWR | O_NONBLOCK)) < 0)
{
/* if the problem is that /dev/fusd does not exist, return the /* if the problem is that /dev/fusd does not exist, return the
* message "Package not installed", which is hopefully more * message "Package not installed", which is hopefully more
* illuminating than "no such file or directory" */ * illuminating than "no such file or directory" */
if (errno == ENOENT) { if (errno == ENOENT)
{
fprintf(stderr, "libfusd: %s does not exist; ensure FUSD's kernel module is installed\n", fprintf(stderr, "libfusd: %s does not exist; ensure FUSD's kernel module is installed\n",
FUSD_CONTROL_DEVNAME); FUSD_CONTROL_DEVNAME);
retval = -ENOPKG; retval = -ENOPKG;
} else { }
else
{
perror("libfusd: trying to open FUSD control channel"); perror("libfusd: trying to open FUSD control channel");
retval = -errno; retval = -errno;
} }
@ -167,7 +175,8 @@ int fusd_register(const char *name, const char* clazz, const char* devname, mode
} }
/* fd in use? */ /* fd in use? */
if (FUSD_FD_VALID(fd)) { if (FUSD_FD_VALID(fd))
{
retval = -EBADF; retval = -EBADF;
goto done; goto done;
} }
@ -184,7 +193,8 @@ int fusd_register(const char *name, const char* clazz, const char* devname, mode
message.parm.register_msg.device_info = device_info; message.parm.register_msg.device_info = device_info;
/* make the request */ /* make the request */
if (write(fd, &message, sizeof(fusd_msg_t)) < 0) { if (write(fd, &message, sizeof(fusd_msg_t)) < 0)
{
retval = -errno; retval = -errno;
goto done; goto done;
} }
@ -195,34 +205,38 @@ int fusd_register(const char *name, const char* clazz, const char* devname, mode
/* success! */ /* success! */
done: done:
if (retval < 0) { if (retval < 0)
{
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
errno = -retval; errno = -retval;
retval = -1; retval = -1;
} else { }
else
{
errno = 0; errno = 0;
retval = fd; retval = fd;
} }
return retval; return retval;
} }
int fusd_unregister(int fd) int fusd_unregister(int fd)
{ {
if (FUSD_FD_VALID(fd)) { int ret = -1;
if (FUSD_FD_VALID(fd))
{
/* clear fd location */ /* clear fd location */
FUSD_SET_FOPS(fd, &null_fops); FUSD_SET_FOPS(fd, &null_fops);
FD_CLR(fd, &fusd_fds); FD_CLR(fd, &fusd_fds);
/* close */ /* close */
return close(fd); ret = close(fd);
}
else
{
errno = EBADF;
} }
else { return ret;
errno = EBADF;
return -1;
}
} }
@ -241,21 +255,25 @@ void fusd_run(void)
int i; int i;
/* locate maxmimum fd in use */ /* locate maxmimum fd in use */
for (maxfd=0, i=0; i < FD_SETSIZE; i++) { for (maxfd=0, i=0; i < FD_SETSIZE; i++)
if (FD_ISSET(i, &fusd_fds)) { {
if (FD_ISSET(i, &fusd_fds))
{
maxfd = i; maxfd = i;
} }
} }
maxfd++; maxfd++;
while (1) { while (1)
{
/* select */ /* select */
memmove(&tfds, &fusd_fds, sizeof(fd_set)); memmove(&tfds, &fusd_fds, sizeof(fd_set));
status = select(maxfd, &tfds, NULL, NULL, NULL); status = select(maxfd, &tfds, NULL, NULL, NULL);
/* error? */ /* error? */
if (status < 0) { if (status < 0)
{
perror("libfusd: fusd_run: error on select"); perror("libfusd: fusd_run: error on select");
continue; continue;
} }
@ -263,14 +281,13 @@ void fusd_run(void)
/* readable? */ /* readable? */
for (i = 0; i < maxfd; i++) for (i = 0; i < maxfd; i++)
if (FD_ISSET(i, &tfds)) if (FD_ISSET(i, &tfds))
fusd_dispatch(i); fusd_dispatch(i);
} }
} }
/************************************************************************/ /************************************************************************/
/* reads a fusd kernel-to-userspace message from fd, and puts a /* reads a fusd kernel-to-userspace message from fd, and puts a
* fusd_msg into the memory pointed to by msg (we assume we are passed * fusd_msg into the memory pointed to by msg (we assume we are passed
* a buffer managed by the caller). if there is a data portion to the * a buffer managed by the caller). if there is a data portion to the
@ -279,39 +296,51 @@ void fusd_run(void)
* managed by the caller. */ * managed by the caller. */
static int fusd_get_message(int fd, fusd_msg_t *msg) static int fusd_get_message(int fd, fusd_msg_t *msg)
{ {
int ret;
/* read the header part into the kernel */ /* read the header part into the kernel */
if (read(fd, msg, sizeof(fusd_msg_t)) < 0) { if (read(fd, msg, sizeof(fusd_msg_t)) < 0)
{
if (errno != EAGAIN) if (errno != EAGAIN)
perror("error talking to FUSD control channel on header read"); perror("error talking to FUSD control channel on header read");
return -errno; ret = -errno;
goto exit;
} }
msg->data = NULL; /* pointers in kernelspace have no meaning */ msg->data = NULL; /* pointers in kernelspace have no meaning */
if (msg->magic != FUSD_MSG_MAGIC) { if (msg->magic != FUSD_MSG_MAGIC)
fprintf(stderr, "libfusd magic number failure\n"); {
return -EINVAL; fprintf(stderr, "libfusd: magic number failure\n");
ret = -EINVAL;
goto exit;
} }
/* if there's a data part to the message, read it from the kernel. */ /* if there's a data part to the message, read it from the kernel. */
if (msg->datalen) { if (msg->datalen)
if ((msg->data = malloc(msg->datalen + 1)) == NULL) { {
if ((msg->data = malloc(msg->datalen + 1)) == NULL)
{
fprintf(stderr, "libfusd: can't allocate memory\n"); fprintf(stderr, "libfusd: can't allocate memory\n");
return -ENOMEM; /* this is bad, we are now unsynced */ ret = -ENOMEM; /* this is bad, we are now unsynced */
goto exit;
} }
if (read(fd, msg->data, msg->datalen) < 0) { if (read(fd, msg->data, msg->datalen) < 0)
{
perror("error talking to FUSD control channel on data read"); perror("error talking to FUSD control channel on data read");
free(msg->data); free(msg->data);
msg->data = NULL; msg->data = NULL;
return -EIO; ret = -EIO;
goto exit;
} }
/* For convenience, we now ensure that the byte *after* the buffer /* For convenience, we now ensure that the byte *after* the buffer
* is set to 0. (Note we malloc'd one extra byte above.) */ * is set to 0. (Note we malloc'd one extra byte above.) */
msg->data[msg->datalen] = '\0'; msg->data[msg->datalen] = '\0';
} }
ret = 0;
return 0; exit:
return ret;
} }
@ -323,10 +352,13 @@ void fusd_fdset_add(fd_set *set, int *max)
{ {
int i; int i;
for (i = 0; i < FD_SETSIZE; i++) { for (i = 0; i < FD_SETSIZE; i++)
if (FD_ISSET(i, &fusd_fds)) { {
if (FD_ISSET(i, &fusd_fds))
{
FD_SET(i, set); FD_SET(i, set);
if (i > *max) { if (i > *max)
{
*max = i; *max = i;
} }
} }
@ -345,6 +377,7 @@ void fusd_dispatch_fdset(fd_set *set)
for (i = 0; i < FD_SETSIZE; i++) for (i = 0; i < FD_SETSIZE; i++)
if (FD_ISSET(i, set) && FD_ISSET(i, &fusd_fds)) if (FD_ISSET(i, set) && FD_ISSET(i, &fusd_fds))
fusd_dispatch(i); fusd_dispatch(i);
} }
@ -366,14 +399,16 @@ static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
int user_retval = 0; /* returned to the user who made the syscall */ int user_retval = 0; /* returned to the user who made the syscall */
/* check for valid, look up ops */ /* check for valid, look up ops */
if (fops == NULL) { if (fops == NULL)
{
fprintf(stderr, "fusd_dispatch: no fops provided!\n"); fprintf(stderr, "fusd_dispatch: no fops provided!\n");
driver_retval = -EBADF; driver_retval = -EBADF;
goto out_noreply; goto out_noreply;
} }
/* allocate memory for fusd_msg_t */ /* allocate memory for fusd_msg_t */
if ((msg = malloc(sizeof(fusd_msg_t))) == NULL) { if ((msg = malloc(sizeof(fusd_msg_t))) == NULL)
{
driver_retval = -ENOMEM; driver_retval = -ENOMEM;
fprintf(stderr, "libfusd: can't allocate memory\n"); fprintf(stderr, "libfusd: can't allocate memory\n");
goto out_noreply; goto out_noreply;
@ -386,7 +421,8 @@ static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
/* allocate file info struct */ /* allocate file info struct */
file = malloc(sizeof(fusd_file_info_t)); file = malloc(sizeof(fusd_file_info_t));
if (NULL == file) { if (NULL == file)
{
fprintf(stderr, "libfusd: can't allocate memory\n"); fprintf(stderr, "libfusd: can't allocate memory\n");
driver_retval = -ENOMEM; driver_retval = -ENOMEM;
goto out_noreply; goto out_noreply;
@ -412,7 +448,8 @@ static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
/* right now we only handle fops requests */ /* right now we only handle fops requests */
if (msg->cmd != FUSD_FOPS_CALL && msg->cmd != FUSD_FOPS_NONBLOCK && if (msg->cmd != FUSD_FOPS_CALL && msg->cmd != FUSD_FOPS_NONBLOCK &&
msg->cmd != FUSD_FOPS_CALL_DROPREPLY) { msg->cmd != FUSD_FOPS_CALL_DROPREPLY)
{
fprintf(stderr, "libfusd: got unknown msg->cmd from kernel\n"); fprintf(stderr, "libfusd: got unknown msg->cmd from kernel\n");
user_retval = -EINVAL; user_retval = -EINVAL;
goto send_reply; goto send_reply;
@ -422,37 +459,47 @@ static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
user_retval = -ENOSYS; user_retval = -ENOSYS;
//printf("dispatch_one: subcmd: %d - ", msg->subcmd); //printf("dispatch_one: subcmd: %d - ", msg->subcmd);
switch (msg->subcmd) {
switch (msg->subcmd)
{
case FUSD_OPEN: case FUSD_OPEN:
//printf("FUSD_OPEN\n"); //printf("FUSD_OPEN\n");
if (fops && fops->open) if (fops && fops->open)
user_retval = fops->open(file); user_retval = fops->open(file);
break; break;
case FUSD_CLOSE: case FUSD_CLOSE:
//printf("FUSD_CLOSE\n"); //printf("FUSD_CLOSE\n");
if (fops && fops->close) if (fops && fops->close)
user_retval = fops->close(file); user_retval = fops->close(file);
break; break;
case FUSD_READ: case FUSD_READ:
//printf("FUSD_READ\n"); //printf("FUSD_READ\n");
/* allocate a buffer and make the call */ /* allocate a buffer and make the call */
if (fops && fops->read) { if (fops && fops->read)
if ((msg->data = malloc(msg->parm.fops_msg.length)) == NULL) { {
if ((msg->data = malloc(msg->parm.fops_msg.length)) == NULL)
{
user_retval = -ENOMEM; user_retval = -ENOMEM;
fprintf(stderr, "libfusd: can't allocate memory\n"); fprintf(stderr, "libfusd: can't allocate memory\n");
} else { }
else
{
msg->datalen = msg->parm.fops_msg.length; msg->datalen = msg->parm.fops_msg.length;
user_retval = fops->read(file, msg->data, msg->datalen, user_retval = fops->read(file, msg->data, msg->datalen,
&msg->parm.fops_msg.offset); &msg->parm.fops_msg.offset);
} }
} }
break; break;
case FUSD_WRITE: case FUSD_WRITE:
//printf("FUSD_WRITE\n"); //printf("FUSD_WRITE\n");
if (fops && fops->write) if (fops && fops->write)
user_retval = fops->write(file, msg->data, msg->datalen, user_retval = fops->write(file, msg->data, msg->datalen,
&msg->parm.fops_msg.offset); &msg->parm.fops_msg.offset);
break; break;
case FUSD_MMAP: case FUSD_MMAP:
//printf("FUSD_MMAP\n"); //printf("FUSD_MMAP\n");
if (fops && fops->mmap) if (fops && fops->mmap)
@ -461,16 +508,20 @@ static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
&msg->parm.fops_msg.arg.ptr_arg, &msg->parm.fops_msg.length); &msg->parm.fops_msg.arg.ptr_arg, &msg->parm.fops_msg.length);
} }
break; break;
case FUSD_IOCTL: case FUSD_IOCTL:
//printf("FUSD_IOCTL\n"); //printf("FUSD_IOCTL\n");
if (fops && fops->ioctl) { if (fops && fops->ioctl)
{
/* in the case of an ioctl read, allocate a buffer for the /* in the case of an ioctl read, allocate a buffer for the
* driver to write to, IF there isn't already a buffer. (there * driver to write to, IF there isn't already a buffer. (there
* might already be a buffer if this is a read+write) */ * might already be a buffer if this is a read+write) */
if ((_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) && if ((_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) &&
msg->data == NULL) { msg->data == NULL)
{
msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd); msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
if ((msg->data = malloc(msg->datalen)) == NULL) { if ((msg->data = malloc(msg->datalen)) == NULL)
{
user_retval = -ENOMEM; user_retval = -ENOMEM;
break; break;
} }
@ -517,20 +568,25 @@ static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
/* send_reply is only used for success */ /* send_reply is only used for success */
send_reply: send_reply:
if (-user_retval <= 0xff) { if (-user_retval <= 0xff)
{
/* 0xff is the maximum legal return value (?) - return val to user */ /* 0xff is the maximum legal return value (?) - return val to user */
driver_retval = fusd_return(file, user_retval); driver_retval = fusd_return(file, user_retval);
} else { }
else
{
/* if we got a FUSD_NOREPLY, don't free the msg structure */ /* if we got a FUSD_NOREPLY, don't free the msg structure */
driver_retval = 0; driver_retval = 0;
} }
/* this is common to both errors and success */ /* this is common to both errors and success */
done: done:
if (driver_retval < 0) { if (driver_retval < 0)
{
errno = -driver_retval; errno = -driver_retval;
driver_retval = -1; driver_retval = -1;
} }
return driver_retval; return driver_retval;
} }
@ -550,7 +606,8 @@ void fusd_dispatch(int fd)
fusd_file_operations_t *fops = NULL; fusd_file_operations_t *fops = NULL;
/* make sure we have a valid FD, and get its fops structure */ /* make sure we have a valid FD, and get its fops structure */
if (!FUSD_FD_VALID(fd)) { if (!FUSD_FD_VALID(fd))
{
errno = EBADF; errno = EBADF;
retval = -1; retval = -1;
fprintf(stderr, "libfusd: not a valid FUSD FD\n"); fprintf(stderr, "libfusd: not a valid FUSD FD\n");
@ -559,7 +616,8 @@ void fusd_dispatch(int fd)
fops = FUSD_GET_FOPS(fd); fops = FUSD_GET_FOPS(fd);
/* now keep dispatching until a dispatch returns an error */ /* now keep dispatching until a dispatch returns an error */
do { do
{
retval = fusd_dispatch_one(fd, fops); retval = fusd_dispatch_one(fd, fops);
if (retval >= 0) if (retval >= 0)
@ -569,7 +627,8 @@ void fusd_dispatch(int fd)
/* if we've dispatched at least one message successfully, and then /* if we've dispatched at least one message successfully, and then
* stopped because of EAGAIN - do not report an error. this is the * stopped because of EAGAIN - do not report an error. this is the
* common case. */ * common case. */
if (num_dispatches > 0 && errno == EAGAIN) { if (num_dispatches > 0 && errno == EAGAIN)
{
retval = 0; retval = 0;
errno = 0; errno = 0;
} }
@ -577,6 +636,7 @@ void fusd_dispatch(int fd)
out: out:
if (retval < 0 && errno != EPIPE) if (retval < 0 && errno != EPIPE)
fprintf(stderr, "libfusd: fusd_dispatch error on fd %d: [%d] %m\n", fd, retval); fprintf(stderr, "libfusd: fusd_dispatch error on fd %d: [%d] %m\n", fd, retval);
} }
@ -612,6 +672,7 @@ int fusd_return(fusd_file_info_t *file, ssize_t retval)
fusd_msg_t *msg = NULL; fusd_msg_t *msg = NULL;
int fd; int fd;
int driver_retval = 0; int driver_retval = 0;
int ret;
struct iovec iov[2]; struct iovec iov[2];
if (file == NULL) if (file == NULL)
@ -643,24 +704,29 @@ int fusd_return(fusd_file_info_t *file, ssize_t retval)
goto free_memory; goto free_memory;
/* do we copy data back to kernel? how much? */ /* do we copy data back to kernel? how much? */
switch(msg->subcmd) { switch(msg->subcmd)
{
case FUSD_READ: case FUSD_READ:
/* these operations can return data to userspace */ /* these operations can return data to userspace */
if (retval > 0) { if (retval > 0)
msg->datalen = MIN(retval, msg->parm.fops_msg.length); {
msg->datalen = MIN((int)retval, (int)msg->parm.fops_msg.length);
retval = msg->datalen; retval = msg->datalen;
} else { }
else
{
msg->datalen = 0; msg->datalen = 0;
} }
break; break;
case FUSD_IOCTL: case FUSD_IOCTL:
/* ioctl CAN (in read mode) return data to userspace */ /* ioctl CAN (in read mode) return data to userspace */
if ((retval == 0) && if (/*(retval == 0) && */ (_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) )
(_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ))
msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd); msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
else else
msg->datalen = 0; msg->datalen = 0;
break; break;
default: default:
/* open, close, write, etc. do not return data */ /* open, close, write, etc. do not return data */
msg->datalen = 0; msg->datalen = 0;
@ -676,7 +742,8 @@ int fusd_return(fusd_file_info_t *file, ssize_t retval)
/* pid is NOT copied back. */ /* pid is NOT copied back. */
/* send message to kernel */ /* send message to kernel */
if (msg->datalen && msg->data != NULL) { if (msg->datalen && msg->data != NULL)
{
//printf("(msg->datalen [%d] && msg->data != NULL [%p]", msg->datalen, msg->data); //printf("(msg->datalen [%d] && msg->data != NULL [%p]", msg->datalen, msg->data);
iov[0].iov_base = msg; iov[0].iov_base = msg;
iov[0].iov_len = sizeof(fusd_msg_t); iov[0].iov_len = sizeof(fusd_msg_t);
@ -684,20 +751,19 @@ int fusd_return(fusd_file_info_t *file, ssize_t retval)
iov[1].iov_len = msg->datalen; iov[1].iov_len = msg->datalen;
driver_retval = writev(fd, iov, 2); driver_retval = writev(fd, iov, 2);
} }
else { else
{
driver_retval = write(fd, msg, sizeof(fusd_msg_t)); driver_retval = write(fd, msg, sizeof(fusd_msg_t));
} }
free_memory: free_memory:
FILE_UNLOCK(file); FILE_UNLOCK(file);
free_memory:
fusd_destroy(file); fusd_destroy(file);
ret = 0; ret = 0;
if (driver_retval < 0) if (driver_retval < 0)
return -errno; ret = -errno;
else
return 0;
goto exit; goto exit;
exit_unlock: exit_unlock: