Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
Jeff Dike | 24fa6c0 | 2007-05-06 14:51:09 -0700 | [diff] [blame] | 7 | #include <stdlib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 10 | #include <fcntl.h> |
| 11 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <termios.h> |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 13 | #include <sys/stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "chan_user.h" |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 15 | #include "os.h" |
Paolo 'Blaisorblade' Giarrusso | c13e569 | 2006-10-19 23:28:20 -0700 | [diff] [blame] | 16 | #include "um_malloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | struct pty_chan { |
| 19 | void (*announce)(char *dev_name, int dev); |
| 20 | int dev; |
| 21 | int raw; |
| 22 | struct termios tt; |
| 23 | char dev_name[sizeof("/dev/pts/0123456\0")]; |
| 24 | }; |
| 25 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 26 | static void *pty_chan_init(char *str, int device, const struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | struct pty_chan *data; |
| 29 | |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 30 | data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 31 | if (data == NULL) |
| 32 | return NULL; |
| 33 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 34 | *data = ((struct pty_chan) { .announce = opts->announce, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | .dev = device, |
| 36 | .raw = opts->raw }); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 37 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static int pts_open(int input, int output, int primary, void *d, |
| 41 | char **dev_out) |
| 42 | { |
| 43 | struct pty_chan *data = d; |
| 44 | char *dev; |
| 45 | int fd, err; |
| 46 | |
| 47 | fd = get_pty(); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 48 | if (fd < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 49 | err = -errno; |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 50 | printk(UM_KERN_ERR "open_pts : Failed to open pts\n"); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 51 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 53 | |
| 54 | if (data->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | CATCH_EINTR(err = tcgetattr(fd, &data->tt)); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 56 | if (err) |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 57 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | err = raw(fd); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 60 | if (err) |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 61 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | dev = ptsname(fd); |
| 65 | sprintf(data->dev_name, "%s", dev); |
| 66 | *dev_out = data->dev_name; |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (data->announce) |
| 69 | (*data->announce)(dev, data->dev); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 70 | |
| 71 | return fd; |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 72 | |
| 73 | out_close: |
| 74 | close(fd); |
| 75 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static int getmaster(char *line) |
| 79 | { |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 80 | struct stat buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | char *pty, *bank, *cp; |
| 82 | int master, err; |
| 83 | |
| 84 | pty = &line[strlen("/dev/ptyp")]; |
| 85 | for (bank = "pqrs"; *bank; bank++) { |
| 86 | line[strlen("/dev/pty")] = *bank; |
| 87 | *pty = '0'; |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 88 | /* Did we hit the end ? */ |
| 89 | if ((stat(line, &buf) < 0) && (errno == ENOENT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | break; |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | for (cp = "0123456789abcdef"; *cp; cp++) { |
| 93 | *pty = *cp; |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 94 | master = open(line, O_RDWR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if (master >= 0) { |
| 96 | char *tp = &line[strlen("/dev/")]; |
| 97 | |
| 98 | /* verify slave side is usable */ |
| 99 | *tp = 't'; |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 100 | err = access(line, R_OK | W_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | *tp = 'p'; |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 102 | if (!err) |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 103 | return master; |
| 104 | close(master); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 108 | |
| 109 | printk(UM_KERN_ERR "getmaster - no usable host pty devices\n"); |
| 110 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int pty_open(int input, int output, int primary, void *d, |
| 114 | char **dev_out) |
| 115 | { |
| 116 | struct pty_chan *data = d; |
| 117 | int fd, err; |
| 118 | char dev[sizeof("/dev/ptyxx\0")] = "/dev/ptyxx"; |
| 119 | |
| 120 | fd = getmaster(dev); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 121 | if (fd < 0) |
| 122 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 124 | if (data->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | err = raw(fd); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 126 | if (err) { |
| 127 | close(fd); |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 128 | return err; |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 131 | |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 132 | if (data->announce) |
| 133 | (*data->announce)(dev, data->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | sprintf(data->dev_name, "%s", dev); |
| 136 | *dev_out = data->dev_name; |
Jeff Dike | 75886f2 | 2007-07-15 23:38:53 -0700 | [diff] [blame] | 137 | |
| 138 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 141 | const struct chan_ops pty_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .type = "pty", |
| 143 | .init = pty_chan_init, |
| 144 | .open = pty_open, |
| 145 | .close = generic_close, |
| 146 | .read = generic_read, |
| 147 | .write = generic_write, |
Paolo 'Blaisorblade' Giarrusso | fd9bc53 | 2005-11-13 16:07:10 -0800 | [diff] [blame] | 148 | .console_write = generic_console_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | .window_size = generic_window_size, |
| 150 | .free = generic_free, |
| 151 | .winch = 0, |
| 152 | }; |
| 153 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 154 | const struct chan_ops pts_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | .type = "pts", |
| 156 | .init = pty_chan_init, |
| 157 | .open = pts_open, |
| 158 | .close = generic_close, |
| 159 | .read = generic_read, |
| 160 | .write = generic_write, |
Paolo 'Blaisorblade' Giarrusso | fd9bc53 | 2005-11-13 16:07:10 -0800 | [diff] [blame] | 161 | .console_write = generic_console_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | .window_size = generic_window_size, |
| 163 | .free = generic_free, |
| 164 | .winch = 0, |
| 165 | }; |