blob: a13a427b996bad391eba6fcac6b60a0baa03a850 [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"
Al Viro37185b32012-10-08 03:27:32 +010012#include <os.h>
13#include <um_malloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15struct fd_chan {
16 int fd;
17 int raw;
18 struct termios tt;
19 char str[sizeof("1234567890\0")];
20};
21
Jeff Dike5e7672e2006-09-27 01:50:33 -070022static void *fd_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 struct fd_chan *data;
25 char *end;
26 int n;
27
Jeff Dikee99525f2007-10-16 01:26:41 -070028 if (*str != ':') {
29 printk(UM_KERN_ERR "fd_init : channel type 'fd' must specify a "
30 "file descriptor\n");
31 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 }
33 str++;
34 n = strtoul(str, &end, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -070035 if ((*end != '\0') || (end == str)) {
36 printk(UM_KERN_ERR "fd_init : couldn't parse file descriptor "
37 "'%s'\n", str);
38 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
Jeff Dikee99525f2007-10-16 01:26:41 -070040
Jeff Dike43f5b302008-05-12 14:01:52 -070041 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070042 if (data == NULL)
Jeff Dikee99525f2007-10-16 01:26:41 -070043 return NULL;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *data = ((struct fd_chan) { .fd = n,
46 .raw = opts->raw });
Jeff Dikee99525f2007-10-16 01:26:41 -070047 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50static int fd_open(int input, int output, int primary, void *d, char **dev_out)
51{
52 struct fd_chan *data = d;
53 int err;
54
Jeff Dikee99525f2007-10-16 01:26:41 -070055 if (data->raw && isatty(data->fd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 CATCH_EINTR(err = tcgetattr(data->fd, &data->tt));
Jeff Dikee99525f2007-10-16 01:26:41 -070057 if (err)
58 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 err = raw(data->fd);
Jeff Dikee99525f2007-10-16 01:26:41 -070061 if (err)
62 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64 sprintf(data->str, "%d", data->fd);
65 *dev_out = data->str;
Jeff Dikee99525f2007-10-16 01:26:41 -070066 return data->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69static void fd_close(int fd, void *d)
70{
71 struct fd_chan *data = d;
72 int err;
73
Jeff Dikee99525f2007-10-16 01:26:41 -070074 if (!data->raw || !isatty(fd))
75 return;
76
77 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt));
78 if (err)
79 printk(UM_KERN_ERR "Failed to restore terminal state - "
80 "errno = %d\n", -err);
81 data->raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Jeff Dike5e7672e2006-09-27 01:50:33 -070084const struct chan_ops fd_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .type = "fd",
86 .init = fd_init,
87 .open = fd_open,
88 .close = fd_close,
89 .read = generic_read,
90 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -080091 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .window_size = generic_window_size,
93 .free = generic_free,
94 .winch = 1,
95};