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