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