blob: cff2b75d31fd7b3bf53be49c75f6a887f8159129 [file] [log] [blame]
Jeff Dikecb8fa612007-10-16 01:27:34 -07001/*
Jeff Dike75886f22007-07-15 23:38:53 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdio.h>
Jeff Dike24fa6c02007-05-06 14:51:09 -07007#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <errno.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070010#include <fcntl.h>
11#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <termios.h>
Jeff Dike75886f22007-07-15 23:38:53 -070013#include <sys/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "chan_user.h"
Jeff Dikecb8fa612007-10-16 01:27:34 -070015#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070016#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18struct 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 Dike5e7672e2006-09-27 01:50:33 -070026static void *pty_chan_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 struct pty_chan *data;
29
Jeff Dike43f5b302008-05-12 14:01:52 -070030 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dike75886f22007-07-15 23:38:53 -070031 if (data == NULL)
32 return NULL;
33
Jeff Dikecb8fa612007-10-16 01:27:34 -070034 *data = ((struct pty_chan) { .announce = opts->announce,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .dev = device,
36 .raw = opts->raw });
Jeff Dike75886f22007-07-15 23:38:53 -070037 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40static 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 Dike75886f22007-07-15 23:38:53 -070048 if (fd < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -070049 err = -errno;
Jeff Dike75886f22007-07-15 23:38:53 -070050 printk(UM_KERN_ERR "open_pts : Failed to open pts\n");
Jeff Dikeb4fd3102005-09-16 19:27:49 -070051 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
Jeff Dike75886f22007-07-15 23:38:53 -070053
54 if (data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
Jeff Dike75886f22007-07-15 23:38:53 -070056 if (err)
Jeff Dikee99525f2007-10-16 01:26:41 -070057 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 err = raw(fd);
Jeff Dike75886f22007-07-15 23:38:53 -070060 if (err)
Jeff Dikee99525f2007-10-16 01:26:41 -070061 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
64 dev = ptsname(fd);
65 sprintf(data->dev_name, "%s", dev);
66 *dev_out = data->dev_name;
Jeff Dike75886f22007-07-15 23:38:53 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (data->announce)
69 (*data->announce)(dev, data->dev);
Jeff Dike75886f22007-07-15 23:38:53 -070070
71 return fd;
Jeff Dikee99525f2007-10-16 01:26:41 -070072
73out_close:
74 close(fd);
75 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78static int getmaster(char *line)
79{
Jeff Dike75886f22007-07-15 23:38:53 -070080 struct stat buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 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 Dike75886f22007-07-15 23:38:53 -070088 /* Did we hit the end ? */
89 if ((stat(line, &buf) < 0) && (errno == ENOENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
Jeff Dike75886f22007-07-15 23:38:53 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 for (cp = "0123456789abcdef"; *cp; cp++) {
93 *pty = *cp;
Jeff Dike75886f22007-07-15 23:38:53 -070094 master = open(line, O_RDWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (master >= 0) {
96 char *tp = &line[strlen("/dev/")];
97
98 /* verify slave side is usable */
99 *tp = 't';
Jeff Dike75886f22007-07-15 23:38:53 -0700100 err = access(line, R_OK | W_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *tp = 'p';
Jeff Dikecb8fa612007-10-16 01:27:34 -0700102 if (!err)
Jeff Dike75886f22007-07-15 23:38:53 -0700103 return master;
104 close(master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106 }
107 }
Jeff Dike75886f22007-07-15 23:38:53 -0700108
109 printk(UM_KERN_ERR "getmaster - no usable host pty devices\n");
110 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static 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 Dike75886f22007-07-15 23:38:53 -0700121 if (fd < 0)
122 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jeff Dikee99525f2007-10-16 01:26:41 -0700124 if (data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 err = raw(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -0700126 if (err) {
127 close(fd);
Jeff Dike75886f22007-07-15 23:38:53 -0700128 return err;
Jeff Dikee99525f2007-10-16 01:26:41 -0700129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700131
Jeff Dike75886f22007-07-15 23:38:53 -0700132 if (data->announce)
133 (*data->announce)(dev, data->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 sprintf(data->dev_name, "%s", dev);
136 *dev_out = data->dev_name;
Jeff Dike75886f22007-07-15 23:38:53 -0700137
138 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Jeff Dike5e7672e2006-09-27 01:50:33 -0700141const struct chan_ops pty_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .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' Giarrussofd9bc532005-11-13 16:07:10 -0800148 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .window_size = generic_window_size,
150 .free = generic_free,
151 .winch = 0,
152};
153
Jeff Dike5e7672e2006-09-27 01:50:33 -0700154const struct chan_ops pts_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .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' Giarrussofd9bc532005-11-13 16:07:10 -0800161 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .window_size = generic_window_size,
163 .free = generic_free,
164 .winch = 0,
165};