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