blob: 4123fe1fca985aa9cbe94d2fd1cc703a70252ff2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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>
9#include <string.h>
Jeff Dike75886f22007-07-15 23:38:53 -070010#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <errno.h>
12#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "os.h"
Jeff Dike75886f22007-07-15 23:38:53 -070016#include "user.h"
17#include "kern_constants.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070018#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20struct 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 Dike5e7672e2006-09-27 01:50:33 -070028static void *pty_chan_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 struct pty_chan *data;
31
Jeff Dikee4c4bf92007-07-15 23:38:56 -070032 data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dike75886f22007-07-15 23:38:53 -070033 if (data == NULL)
34 return NULL;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *data = ((struct pty_chan) { .announce = opts->announce,
37 .dev = device,
38 .raw = opts->raw });
Jeff Dike75886f22007-07-15 23:38:53 -070039 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42static 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 Dike75886f22007-07-15 23:38:53 -070050 if (fd < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -070051 err = -errno;
Jeff Dike75886f22007-07-15 23:38:53 -070052 printk(UM_KERN_ERR "open_pts : Failed to open pts\n");
Jeff Dikeb4fd3102005-09-16 19:27:49 -070053 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
Jeff Dike75886f22007-07-15 23:38:53 -070055
56 if (data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
Jeff Dike75886f22007-07-15 23:38:53 -070058 if (err)
Jeff Dikee99525f2007-10-16 01:26:41 -070059 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 err = raw(fd);
Jeff Dike75886f22007-07-15 23:38:53 -070062 if (err)
Jeff Dikee99525f2007-10-16 01:26:41 -070063 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
66 dev = ptsname(fd);
67 sprintf(data->dev_name, "%s", dev);
68 *dev_out = data->dev_name;
Jeff Dike75886f22007-07-15 23:38:53 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (data->announce)
71 (*data->announce)(dev, data->dev);
Jeff Dike75886f22007-07-15 23:38:53 -070072
73 return fd;
Jeff Dikee99525f2007-10-16 01:26:41 -070074
75out_close:
76 close(fd);
77 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static int getmaster(char *line)
81{
Jeff Dike75886f22007-07-15 23:38:53 -070082 struct stat buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 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 Dike75886f22007-07-15 23:38:53 -070090 /* Did we hit the end ? */
91 if ((stat(line, &buf) < 0) && (errno == ENOENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 break;
Jeff Dike75886f22007-07-15 23:38:53 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 for (cp = "0123456789abcdef"; *cp; cp++) {
95 *pty = *cp;
Jeff Dike75886f22007-07-15 23:38:53 -070096 master = open(line, O_RDWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (master >= 0) {
98 char *tp = &line[strlen("/dev/")];
99
100 /* verify slave side is usable */
101 *tp = 't';
Jeff Dike75886f22007-07-15 23:38:53 -0700102 err = access(line, R_OK | W_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *tp = 'p';
Jeff Dike75886f22007-07-15 23:38:53 -0700104 if(!err)
105 return master;
106 close(master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 }
109 }
Jeff Dike75886f22007-07-15 23:38:53 -0700110
111 printk(UM_KERN_ERR "getmaster - no usable host pty devices\n");
112 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115static 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 Dike75886f22007-07-15 23:38:53 -0700123 if (fd < 0)
124 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jeff Dikee99525f2007-10-16 01:26:41 -0700126 if (data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 err = raw(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -0700128 if (err) {
129 close(fd);
Jeff Dike75886f22007-07-15 23:38:53 -0700130 return err;
Jeff Dikee99525f2007-10-16 01:26:41 -0700131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Jeff Dike75886f22007-07-15 23:38:53 -0700134 if (data->announce)
135 (*data->announce)(dev, data->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 sprintf(data->dev_name, "%s", dev);
138 *dev_out = data->dev_name;
Jeff Dike75886f22007-07-15 23:38:53 -0700139
140 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Jeff Dike5e7672e2006-09-27 01:50:33 -0700143const struct chan_ops pty_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .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' Giarrussofd9bc532005-11-13 16:07:10 -0800150 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .window_size = generic_window_size,
152 .free = generic_free,
153 .winch = 0,
154};
155
Jeff Dike5e7672e2006-09-27 01:50:33 -0700156const struct chan_ops pts_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .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' Giarrussofd9bc532005-11-13 16:07:10 -0800163 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 .window_size = generic_window_size,
165 .free = generic_free,
166 .winch = 0,
167};