blob: 640bcdaad9f4f14e1f6a186257a7ace64f63a463 [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
32 data = um_kmalloc(sizeof(*data));
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)
59 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 err = raw(fd);
Jeff Dike75886f22007-07-15 23:38:53 -070062 if (err)
63 return err;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static int getmaster(char *line)
77{
Jeff Dike75886f22007-07-15 23:38:53 -070078 struct stat buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 char *pty, *bank, *cp;
80 int master, err;
81
82 pty = &line[strlen("/dev/ptyp")];
83 for (bank = "pqrs"; *bank; bank++) {
84 line[strlen("/dev/pty")] = *bank;
85 *pty = '0';
Jeff Dike75886f22007-07-15 23:38:53 -070086 /* Did we hit the end ? */
87 if ((stat(line, &buf) < 0) && (errno == ENOENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 break;
Jeff Dike75886f22007-07-15 23:38:53 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 for (cp = "0123456789abcdef"; *cp; cp++) {
91 *pty = *cp;
Jeff Dike75886f22007-07-15 23:38:53 -070092 master = open(line, O_RDWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (master >= 0) {
94 char *tp = &line[strlen("/dev/")];
95
96 /* verify slave side is usable */
97 *tp = 't';
Jeff Dike75886f22007-07-15 23:38:53 -070098 err = access(line, R_OK | W_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *tp = 'p';
Jeff Dike75886f22007-07-15 23:38:53 -0700100 if(!err)
101 return master;
102 close(master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 }
105 }
Jeff Dike75886f22007-07-15 23:38:53 -0700106
107 printk(UM_KERN_ERR "getmaster - no usable host pty devices\n");
108 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static int pty_open(int input, int output, int primary, void *d,
112 char **dev_out)
113{
114 struct pty_chan *data = d;
115 int fd, err;
116 char dev[sizeof("/dev/ptyxx\0")] = "/dev/ptyxx";
117
118 fd = getmaster(dev);
Jeff Dike75886f22007-07-15 23:38:53 -0700119 if (fd < 0)
120 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if(data->raw){
123 err = raw(fd);
Jeff Dike75886f22007-07-15 23:38:53 -0700124 if (err)
125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
Jeff Dike75886f22007-07-15 23:38:53 -0700128 if (data->announce)
129 (*data->announce)(dev, data->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 sprintf(data->dev_name, "%s", dev);
132 *dev_out = data->dev_name;
Jeff Dike75886f22007-07-15 23:38:53 -0700133
134 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Jeff Dike5e7672e2006-09-27 01:50:33 -0700137const struct chan_ops pty_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .type = "pty",
139 .init = pty_chan_init,
140 .open = pty_open,
141 .close = generic_close,
142 .read = generic_read,
143 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -0800144 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .window_size = generic_window_size,
146 .free = generic_free,
147 .winch = 0,
148};
149
Jeff Dike5e7672e2006-09-27 01:50:33 -0700150const struct chan_ops pts_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .type = "pts",
152 .init = pty_chan_init,
153 .open = pts_open,
154 .close = generic_close,
155 .read = generic_read,
156 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -0800157 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .window_size = generic_window_size,
159 .free = generic_free,
160 .winch = 0,
161};