Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 6 | #include <stddef.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <stdlib.h> |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 8 | #include <stdio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 10 | #include <termios.h> |
| 11 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "chan_user.h" |
Paolo 'Blaisorblade' Giarrusso | c13e569 | 2006-10-19 23:28:20 -0700 | [diff] [blame] | 13 | #include "um_malloc.h" |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 14 | #include "user.h" |
| 15 | #include "os.h" |
| 16 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | struct fd_chan { |
| 19 | int fd; |
| 20 | int raw; |
| 21 | struct termios tt; |
| 22 | char str[sizeof("1234567890\0")]; |
| 23 | }; |
| 24 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 25 | static void *fd_init(char *str, int device, const struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | struct fd_chan *data; |
| 28 | char *end; |
| 29 | int n; |
| 30 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 31 | if (*str != ':') { |
| 32 | printk(UM_KERN_ERR "fd_init : channel type 'fd' must specify a " |
| 33 | "file descriptor\n"); |
| 34 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } |
| 36 | str++; |
| 37 | n = strtoul(str, &end, 0); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 38 | if ((*end != '\0') || (end == str)) { |
| 39 | printk(UM_KERN_ERR "fd_init : couldn't parse file descriptor " |
| 40 | "'%s'\n", str); |
| 41 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 43 | |
Jeff Dike | e4c4bf99 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 44 | data = kmalloc(sizeof(*data), UM_GFP_KERNEL); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 45 | if(data == NULL) |
| 46 | return NULL; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | *data = ((struct fd_chan) { .fd = n, |
| 49 | .raw = opts->raw }); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 50 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int fd_open(int input, int output, int primary, void *d, char **dev_out) |
| 54 | { |
| 55 | struct fd_chan *data = d; |
| 56 | int err; |
| 57 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 58 | if (data->raw && isatty(data->fd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | CATCH_EINTR(err = tcgetattr(data->fd, &data->tt)); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 60 | if (err) |
| 61 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | err = raw(data->fd); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 64 | if (err) |
| 65 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | sprintf(data->str, "%d", data->fd); |
| 68 | *dev_out = data->str; |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 69 | return data->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void fd_close(int fd, void *d) |
| 73 | { |
| 74 | struct fd_chan *data = d; |
| 75 | int err; |
| 76 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame^] | 77 | if (!data->raw || !isatty(fd)) |
| 78 | return; |
| 79 | |
| 80 | CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt)); |
| 81 | if (err) |
| 82 | printk(UM_KERN_ERR "Failed to restore terminal state - " |
| 83 | "errno = %d\n", -err); |
| 84 | data->raw = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 87 | const struct chan_ops fd_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | .type = "fd", |
| 89 | .init = fd_init, |
| 90 | .open = fd_open, |
| 91 | .close = fd_close, |
| 92 | .read = generic_read, |
| 93 | .write = generic_write, |
Paolo 'Blaisorblade' Giarrusso | fd9bc53 | 2005-11-13 16:07:10 -0800 | [diff] [blame] | 94 | .console_write = generic_console_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | .window_size = generic_window_size, |
| 96 | .free = generic_free, |
| 97 | .winch = 1, |
| 98 | }; |