pty.c 3.1 KB
Newer Older
J
Jeff Dike 已提交
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include <stdio.h>
7
#include <stdlib.h>
L
Linus Torvalds 已提交
8 9
#include <unistd.h>
#include <errno.h>
J
Jeff Dike 已提交
10 11
#include <fcntl.h>
#include <string.h>
L
Linus Torvalds 已提交
12
#include <termios.h>
J
Jeff Dike 已提交
13
#include <sys/stat.h>
L
Linus Torvalds 已提交
14
#include "chan_user.h"
15 16
#include <os.h>
#include <um_malloc.h>
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24 25

struct pty_chan {
	void (*announce)(char *dev_name, int dev);
	int dev;
	int raw;
	struct termios tt;
	char dev_name[sizeof("/dev/pts/0123456\0")];
};

J
Jeff Dike 已提交
26
static void *pty_chan_init(char *str, int device, const struct chan_opts *opts)
L
Linus Torvalds 已提交
27 28 29
{
	struct pty_chan *data;

J
Jeff Dike 已提交
30
	data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
J
Jeff Dike 已提交
31 32 33
	if (data == NULL)
		return NULL;

J
Jeff Dike 已提交
34
	*data = ((struct pty_chan) { .announce  	= opts->announce,
L
Linus Torvalds 已提交
35 36
				     .dev  		= device,
				     .raw  		= opts->raw });
J
Jeff Dike 已提交
37
	return data;
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47
}

static int pts_open(int input, int output, int primary, void *d,
		    char **dev_out)
{
	struct pty_chan *data = d;
	char *dev;
	int fd, err;

	fd = get_pty();
J
Jeff Dike 已提交
48
	if (fd < 0) {
49
		err = -errno;
J
Jeff Dike 已提交
50
		printk(UM_KERN_ERR "open_pts : Failed to open pts\n");
51
		return err;
L
Linus Torvalds 已提交
52
	}
J
Jeff Dike 已提交
53 54

	if (data->raw) {
L
Linus Torvalds 已提交
55
		CATCH_EINTR(err = tcgetattr(fd, &data->tt));
J
Jeff Dike 已提交
56
		if (err)
J
Jeff Dike 已提交
57
			goto out_close;
L
Linus Torvalds 已提交
58 59

		err = raw(fd);
J
Jeff Dike 已提交
60
		if (err)
J
Jeff Dike 已提交
61
			goto out_close;
L
Linus Torvalds 已提交
62 63 64 65 66
	}

	dev = ptsname(fd);
	sprintf(data->dev_name, "%s", dev);
	*dev_out = data->dev_name;
J
Jeff Dike 已提交
67

L
Linus Torvalds 已提交
68 69
	if (data->announce)
		(*data->announce)(dev, data->dev);
J
Jeff Dike 已提交
70 71

	return fd;
J
Jeff Dike 已提交
72 73 74 75

out_close:
	close(fd);
	return err;
L
Linus Torvalds 已提交
76 77 78 79
}

static int getmaster(char *line)
{
J
Jeff Dike 已提交
80
	struct stat buf;
L
Linus Torvalds 已提交
81 82 83 84 85 86 87
	char *pty, *bank, *cp;
	int master, err;

	pty = &line[strlen("/dev/ptyp")];
	for (bank = "pqrs"; *bank; bank++) {
		line[strlen("/dev/pty")] = *bank;
		*pty = '0';
J
Jeff Dike 已提交
88 89
		/* Did we hit the end ? */
		if ((stat(line, &buf) < 0) && (errno == ENOENT))
L
Linus Torvalds 已提交
90
			break;
J
Jeff Dike 已提交
91

L
Linus Torvalds 已提交
92 93
		for (cp = "0123456789abcdef"; *cp; cp++) {
			*pty = *cp;
J
Jeff Dike 已提交
94
			master = open(line, O_RDWR);
L
Linus Torvalds 已提交
95 96 97 98 99
			if (master >= 0) {
				char *tp = &line[strlen("/dev/")];

				/* verify slave side is usable */
				*tp = 't';
J
Jeff Dike 已提交
100
				err = access(line, R_OK | W_OK);
L
Linus Torvalds 已提交
101
				*tp = 'p';
J
Jeff Dike 已提交
102
				if (!err)
J
Jeff Dike 已提交
103 104
					return master;
				close(master);
L
Linus Torvalds 已提交
105 106 107
			}
		}
	}
J
Jeff Dike 已提交
108 109 110

	printk(UM_KERN_ERR "getmaster - no usable host pty devices\n");
	return -ENOENT;
L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119 120
}

static int pty_open(int input, int output, int primary, void *d,
		    char **dev_out)
{
	struct pty_chan *data = d;
	int fd, err;
	char dev[sizeof("/dev/ptyxx\0")] = "/dev/ptyxx";

	fd = getmaster(dev);
J
Jeff Dike 已提交
121 122
	if (fd < 0)
		return fd;
L
Linus Torvalds 已提交
123

J
Jeff Dike 已提交
124
	if (data->raw) {
L
Linus Torvalds 已提交
125
		err = raw(fd);
J
Jeff Dike 已提交
126 127
		if (err) {
			close(fd);
J
Jeff Dike 已提交
128
			return err;
J
Jeff Dike 已提交
129
		}
L
Linus Torvalds 已提交
130
	}
J
Jeff Dike 已提交
131

J
Jeff Dike 已提交
132 133
	if (data->announce)
		(*data->announce)(dev, data->dev);
L
Linus Torvalds 已提交
134 135 136

	sprintf(data->dev_name, "%s", dev);
	*dev_out = data->dev_name;
J
Jeff Dike 已提交
137 138

	return fd;
L
Linus Torvalds 已提交
139 140
}

J
Jeff Dike 已提交
141
const struct chan_ops pty_ops = {
L
Linus Torvalds 已提交
142 143 144 145 146 147
	.type		= "pty",
	.init		= pty_chan_init,
	.open		= pty_open,
	.close		= generic_close,
	.read		= generic_read,
	.write		= generic_write,
148
	.console_write	= generic_console_write,
L
Linus Torvalds 已提交
149 150 151 152 153
	.window_size	= generic_window_size,
	.free		= generic_free,
	.winch		= 0,
};

J
Jeff Dike 已提交
154
const struct chan_ops pts_ops = {
L
Linus Torvalds 已提交
155 156 157 158 159 160
	.type		= "pts",
	.init		= pty_chan_init,
	.open		= pts_open,
	.close		= generic_close,
	.read		= generic_read,
	.write		= generic_write,
161
	.console_write	= generic_console_write,
L
Linus Torvalds 已提交
162 163 164 165
	.window_size	= generic_window_size,
	.free		= generic_free,
	.winch		= 0,
};