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"
J
Jeff Dike 已提交
15
#include "kern_constants.h"
J
Jeff Dike 已提交
16
#include "os.h"
17
#include "um_malloc.h"
J
Jeff Dike 已提交
18
#include "user.h"
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27

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 已提交
28
static void *pty_chan_init(char *str, int device, const struct chan_opts *opts)
L
Linus Torvalds 已提交
29 30 31
{
	struct pty_chan *data;

32
	data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
J
Jeff Dike 已提交
33 34 35
	if (data == NULL)
		return NULL;

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

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 已提交
50
	if (fd < 0) {
51
		err = -errno;
J
Jeff Dike 已提交
52
		printk(UM_KERN_ERR "open_pts : Failed to open pts\n");
53
		return err;
L
Linus Torvalds 已提交
54
	}
J
Jeff Dike 已提交
55 56

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

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

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

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

	return fd;
J
Jeff Dike 已提交
74 75 76 77

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

static int getmaster(char *line)
{
J
Jeff Dike 已提交
82
	struct stat buf;
L
Linus Torvalds 已提交
83 84 85 86 87 88 89
	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 已提交
90 91
		/* Did we hit the end ? */
		if ((stat(line, &buf) < 0) && (errno == ENOENT))
L
Linus Torvalds 已提交
92
			break;
J
Jeff Dike 已提交
93

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

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

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

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 已提交
123 124
	if (fd < 0)
		return fd;
L
Linus Torvalds 已提交
125

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

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

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

	return fd;
L
Linus Torvalds 已提交
141 142
}

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

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