blob: f5a981a16240e4d8dd92afca3fb710d9d4d84588 [file] [log] [blame]
Jeff Dikecb8fa612007-10-16 01:27:34 -07001/*
Jeff Dikee99525f2007-10-16 01:26:41 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dikee99525f2007-10-16 01:26:41 -07006#include <stdio.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -07007#include <stdlib.h>
8#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <errno.h>
Jeff Dikee99525f2007-10-16 01:26:41 -070010#include <termios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "chan_user.h"
Jeff Dikecb8fa612007-10-16 01:27:34 -070012#include "kern_constants.h"
13#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070014#include "um_malloc.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070015#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17struct fd_chan {
18 int fd;
19 int raw;
20 struct termios tt;
21 char str[sizeof("1234567890\0")];
22};
23
Jeff Dike5e7672e2006-09-27 01:50:33 -070024static void *fd_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 struct fd_chan *data;
27 char *end;
28 int n;
29
Jeff Dikee99525f2007-10-16 01:26:41 -070030 if (*str != ':') {
31 printk(UM_KERN_ERR "fd_init : channel type 'fd' must specify a "
32 "file descriptor\n");
33 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 }
35 str++;
36 n = strtoul(str, &end, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -070037 if ((*end != '\0') || (end == str)) {
38 printk(UM_KERN_ERR "fd_init : couldn't parse file descriptor "
39 "'%s'\n", str);
40 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
Jeff Dikee99525f2007-10-16 01:26:41 -070042
Jeff Dike43f5b302008-05-12 14:01:52 -070043 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070044 if (data == NULL)
Jeff Dikee99525f2007-10-16 01:26:41 -070045 return NULL;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *data = ((struct fd_chan) { .fd = n,
48 .raw = opts->raw });
Jeff Dikee99525f2007-10-16 01:26:41 -070049 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static int fd_open(int input, int output, int primary, void *d, char **dev_out)
53{
54 struct fd_chan *data = d;
55 int err;
56
Jeff Dikee99525f2007-10-16 01:26:41 -070057 if (data->raw && isatty(data->fd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 CATCH_EINTR(err = tcgetattr(data->fd, &data->tt));
Jeff Dikee99525f2007-10-16 01:26:41 -070059 if (err)
60 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 err = raw(data->fd);
Jeff Dikee99525f2007-10-16 01:26:41 -070063 if (err)
64 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66 sprintf(data->str, "%d", data->fd);
67 *dev_out = data->str;
Jeff Dikee99525f2007-10-16 01:26:41 -070068 return data->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71static void fd_close(int fd, void *d)
72{
73 struct fd_chan *data = d;
74 int err;
75
Jeff Dikee99525f2007-10-16 01:26:41 -070076 if (!data->raw || !isatty(fd))
77 return;
78
79 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt));
80 if (err)
81 printk(UM_KERN_ERR "Failed to restore terminal state - "
82 "errno = %d\n", -err);
83 data->raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Jeff Dike5e7672e2006-09-27 01:50:33 -070086const struct chan_ops fd_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .type = "fd",
88 .init = fd_init,
89 .open = fd_open,
90 .close = fd_close,
91 .read = generic_read,
92 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -080093 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .window_size = generic_window_size,
95 .free = generic_free,
96 .winch = 1,
97};