Commit e99525f9 authored by Jeff Dike's avatar Jeff Dike Committed by Linus Torvalds

uml: console subsystem tidying

This does a lot of cleanup on the UML console system.  This patch should be
entirely non-functional.

The tidying is as follows:
	header cleanups - the includes should be closer to minimal and complete
	all printks now have a severity
	lots of style fixes
	fd_close is restructured a little in order to reduce the nesting
	some functions were calling the os_* wrappers when they can
call libc directly
	port_accept had a unnecessary variable
	it also tested a pid unecessarily before killing it
	some functions were made static
	xterm_free is gone, as it was identical to generic_free
Signed-off-by: default avatarJeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 79f66233
This diff is collapsed.
/* /*
* Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com) * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <termios.h>
#include <string.h>
#include <signal.h>
#include <sched.h> #include <sched.h>
#include <sys/stat.h> #include <signal.h>
#include <termios.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/socket.h>
#include "kern_util.h"
#include "chan_user.h" #include "chan_user.h"
#include "user.h"
#include "os.h" #include "os.h"
#include "choose-mode.h"
#include "mode.h"
#include "um_malloc.h" #include "um_malloc.h"
#include "user.h"
void generic_close(int fd, void *unused) void generic_close(int fd, void *unused)
{ {
...@@ -53,7 +47,7 @@ int generic_window_size(int fd, void *unused, unsigned short *rows_out, ...@@ -53,7 +47,7 @@ int generic_window_size(int fd, void *unused, unsigned short *rows_out,
struct winsize size; struct winsize size;
int ret; int ret;
if(ioctl(fd, TIOCGWINSZ, &size) < 0) if (ioctl(fd, TIOCGWINSZ, &size) < 0)
return -errno; return -errno;
ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col)); ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
...@@ -74,7 +68,7 @@ int generic_console_write(int fd, const char *buf, int n) ...@@ -74,7 +68,7 @@ int generic_console_write(int fd, const char *buf, int n)
struct termios save, new; struct termios save, new;
int err; int err;
if(isatty(fd)){ if (isatty(fd)) {
CATCH_EINTR(err = tcgetattr(fd, &save)); CATCH_EINTR(err = tcgetattr(fd, &save));
if (err) if (err)
goto error; goto error;
...@@ -90,11 +84,11 @@ int generic_console_write(int fd, const char *buf, int n) ...@@ -90,11 +84,11 @@ int generic_console_write(int fd, const char *buf, int n)
err = generic_write(fd, buf, n, NULL); err = generic_write(fd, buf, n, NULL);
/* Restore raw mode, in any case; we *must* ignore any error apart /* Restore raw mode, in any case; we *must* ignore any error apart
* EINTR, except for debug.*/ * EINTR, except for debug.*/
if(isatty(fd)) if (isatty(fd))
CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save)); CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
return(err); return err;
error: error:
return(-errno); return -errno;
} }
/* /*
...@@ -137,56 +131,62 @@ static int winch_thread(void *arg) ...@@ -137,56 +131,62 @@ static int winch_thread(void *arg)
pty_fd = data->pty_fd; pty_fd = data->pty_fd;
pipe_fd = data->pipe_fd; pipe_fd = data->pipe_fd;
count = os_write_file(pipe_fd, &c, sizeof(c)); count = os_write_file(pipe_fd, &c, sizeof(c));
if(count != sizeof(c)) if (count != sizeof(c))
printk("winch_thread : failed to write synchronization " printk(UM_KERN_ERR "winch_thread : failed to write "
"byte, err = %d\n", -count); "synchronization byte, err = %d\n", -count);
/* We are not using SIG_IGN on purpose, so don't fix it as I thought to /*
* We are not using SIG_IGN on purpose, so don't fix it as I thought to
* do! If using SIG_IGN, the sigsuspend() call below would not stop on * do! If using SIG_IGN, the sigsuspend() call below would not stop on
* SIGWINCH. */ * SIGWINCH.
*/
signal(SIGWINCH, winch_handler); signal(SIGWINCH, winch_handler);
sigfillset(&sigs); sigfillset(&sigs);
/* Block all signals possible. */ /* Block all signals possible. */
if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){ if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
printk("winch_thread : sigprocmask failed, errno = %d\n", printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
errno); "errno = %d\n", errno);
exit(1); exit(1);
} }
/* In sigsuspend(), block anything else than SIGWINCH. */ /* In sigsuspend(), block anything else than SIGWINCH. */
sigdelset(&sigs, SIGWINCH); sigdelset(&sigs, SIGWINCH);
if(setsid() < 0){ if (setsid() < 0) {
printk("winch_thread : setsid failed, errno = %d\n", errno); printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
errno);
exit(1); exit(1);
} }
err = os_new_tty_pgrp(pty_fd, os_getpid()); err = os_new_tty_pgrp(pty_fd, os_getpid());
if(err < 0){ if (err < 0) {
printk("winch_thread : new_tty_pgrp failed on fd %d, " printk(UM_KERN_ERR "winch_thread : new_tty_pgrp failed on "
"err = %d\n", pty_fd, -err); "fd %d err = %d\n", pty_fd, -err);
exit(1); exit(1);
} }
/* These are synchronization calls between various UML threads on the /*
* These are synchronization calls between various UML threads on the
* host - since they are not different kernel threads, we cannot use * host - since they are not different kernel threads, we cannot use
* kernel semaphores. We don't use SysV semaphores because they are * kernel semaphores. We don't use SysV semaphores because they are
* persistent. */ * persistent.
*/
count = os_read_file(pipe_fd, &c, sizeof(c)); count = os_read_file(pipe_fd, &c, sizeof(c));
if(count != sizeof(c)) if (count != sizeof(c))
printk("winch_thread : failed to read synchronization byte, " printk(UM_KERN_ERR "winch_thread : failed to read "
"err = %d\n", -count); "synchronization byte, err = %d\n", -count);
while(1){ while(1) {
/* This will be interrupted by SIGWINCH only, since /*
* This will be interrupted by SIGWINCH only, since
* other signals are blocked. * other signals are blocked.
*/ */
sigsuspend(&sigs); sigsuspend(&sigs);
count = os_write_file(pipe_fd, &c, sizeof(c)); count = os_write_file(pipe_fd, &c, sizeof(c));
if(count != sizeof(c)) if (count != sizeof(c))
printk("winch_thread : write failed, err = %d\n", printk(UM_KERN_ERR "winch_thread : write failed, "
-count); "err = %d\n", -count);
} }
} }
...@@ -198,36 +198,41 @@ static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out, ...@@ -198,36 +198,41 @@ static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out,
char c; char c;
err = os_pipe(fds, 1, 1); err = os_pipe(fds, 1, 1);
if(err < 0){ if (err < 0) {
printk("winch_tramp : os_pipe failed, err = %d\n", -err); printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
-err);
goto out; goto out;
} }
data = ((struct winch_data) { .pty_fd = fd, data = ((struct winch_data) { .pty_fd = fd,
.pipe_fd = fds[1] } ); .pipe_fd = fds[1] } );
/* CLONE_FILES so this thread doesn't hold open files which are open /*
* CLONE_FILES so this thread doesn't hold open files which are open
* now, but later closed in a different thread. This is a * now, but later closed in a different thread. This is a
* problem with /dev/net/tun, which if held open by this * problem with /dev/net/tun, which if held open by this
* thread, prevents the TUN/TAP device from being reused. * thread, prevents the TUN/TAP device from being reused.
*/ */
err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out); err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
if(err < 0){ if (err < 0) {
printk("fork of winch_thread failed - errno = %d\n", -err); printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
-err);
goto out_close; goto out_close;
} }
*fd_out = fds[0]; *fd_out = fds[0];
n = os_read_file(fds[0], &c, sizeof(c)); n = os_read_file(fds[0], &c, sizeof(c));
if(n != sizeof(c)){ if (n != sizeof(c)) {
printk("winch_tramp : failed to read synchronization byte\n"); printk(UM_KERN_ERR "winch_tramp : failed to read "
printk("read failed, err = %d\n", -n); "synchronization byte\n");
printk("fd %d will not support SIGWINCH\n", fd); printk(UM_KERN_ERR "read failed, err = %d\n", -n);
err = -EINVAL; printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
err = -EINVAL;
goto out_close; goto out_close;
} }
if (os_set_fd_block(*fd_out, 0)) { if (os_set_fd_block(*fd_out, 0)) {
printk("winch_tramp: failed to set thread_fd non-blocking.\n"); printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
"non-blocking.\n");
goto out_close; goto out_close;
} }
...@@ -246,7 +251,7 @@ void register_winch(int fd, struct tty_struct *tty) ...@@ -246,7 +251,7 @@ void register_winch(int fd, struct tty_struct *tty)
int pid, thread, count, thread_fd = -1; int pid, thread, count, thread_fd = -1;
char c = 1; char c = 1;
if(!isatty(fd)) if (!isatty(fd))
return; return;
pid = tcgetpgrp(fd); pid = tcgetpgrp(fd);
...@@ -259,8 +264,8 @@ void register_winch(int fd, struct tty_struct *tty) ...@@ -259,8 +264,8 @@ void register_winch(int fd, struct tty_struct *tty)
register_winch_irq(thread_fd, fd, thread, tty, stack); register_winch_irq(thread_fd, fd, thread, tty, stack);
count = os_write_file(thread_fd, &c, sizeof(c)); count = os_write_file(thread_fd, &c, sizeof(c));
if(count != sizeof(c)) if (count != sizeof(c))
printk("register_winch : failed to write " printk(UM_KERN_ERR "register_winch : failed to write "
"synchronization byte, err = %d\n", -count); "synchronization byte, err = %d\n", -count);
} }
} }
/* /*
* Copyright (C) 2001 Jeff Dike (jdike@karaya.com) * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
#include <stdio.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <stdio.h>
#include <termios.h>
#include <errno.h> #include <errno.h>
#include "user.h" #include <termios.h>
#include <unistd.h>
#include "chan_user.h" #include "chan_user.h"
#include "os.h"
#include "um_malloc.h" #include "um_malloc.h"
#include "user.h"
#include "os.h"
#include "kern_constants.h"
struct fd_chan { struct fd_chan {
int fd; int fd;
...@@ -26,22 +28,26 @@ static void *fd_init(char *str, int device, const struct chan_opts *opts) ...@@ -26,22 +28,26 @@ static void *fd_init(char *str, int device, const struct chan_opts *opts)
char *end; char *end;
int n; int n;
if(*str != ':'){ if (*str != ':') {
printk("fd_init : channel type 'fd' must specify a file " printk(UM_KERN_ERR "fd_init : channel type 'fd' must specify a "
"descriptor\n"); "file descriptor\n");
return(NULL); return NULL;
} }
str++; str++;
n = strtoul(str, &end, 0); n = strtoul(str, &end, 0);
if((*end != '\0') || (end == str)){ if ((*end != '\0') || (end == str)) {
printk("fd_init : couldn't parse file descriptor '%s'\n", str); printk(UM_KERN_ERR "fd_init : couldn't parse file descriptor "
return(NULL); "'%s'\n", str);
return NULL;
} }
data = kmalloc(sizeof(*data), UM_GFP_KERNEL); data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
if(data == NULL) return(NULL); if(data == NULL)
return NULL;
*data = ((struct fd_chan) { .fd = n, *data = ((struct fd_chan) { .fd = n,
.raw = opts->raw }); .raw = opts->raw });
return(data); return data;
} }
static int fd_open(int input, int output, int primary, void *d, char **dev_out) static int fd_open(int input, int output, int primary, void *d, char **dev_out)
...@@ -49,18 +55,18 @@ static int fd_open(int input, int output, int primary, void *d, char **dev_out) ...@@ -49,18 +55,18 @@ static int fd_open(int input, int output, int primary, void *d, char **dev_out)
struct fd_chan *data = d; struct fd_chan *data = d;
int err; int err;
if(data->raw && isatty(data->fd)){ if (data->raw && isatty(data->fd)) {
CATCH_EINTR(err = tcgetattr(data->fd, &data->tt)); CATCH_EINTR(err = tcgetattr(data->fd, &data->tt));
if(err) if (err)
return(err); return err;
err = raw(data->fd); err = raw(data->fd);
if(err) if (err)
return(err); return err;
} }
sprintf(data->str, "%d", data->fd); sprintf(data->str, "%d", data->fd);
*dev_out = data->str; *dev_out = data->str;
return(data->fd); return data->fd;
} }
static void fd_close(int fd, void *d) static void fd_close(int fd, void *d)
...@@ -68,13 +74,14 @@ static void fd_close(int fd, void *d) ...@@ -68,13 +74,14 @@ static void fd_close(int fd, void *d)
struct fd_chan *data = d; struct fd_chan *data = d;
int err; int err;
if(data->raw && isatty(fd)){ if (!data->raw || !isatty(fd))
CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt)); return;
if(err)
printk("Failed to restore terminal state - " CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt));
"errno = %d\n", -err); if (err)
data->raw = 0; printk(UM_KERN_ERR "Failed to restore terminal state - "
} "errno = %d\n", -err);
data->raw = 0;
} }
const struct chan_ops fd_ops = { const struct chan_ops fd_ops = {
...@@ -89,14 +96,3 @@ const struct chan_ops fd_ops = { ...@@ -89,14 +96,3 @@ const struct chan_ops fd_ops = {
.free = generic_free, .free = generic_free,
.winch = 1, .winch = 1,
}; };
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/
/* /*
* Copyright (C) 2002 Jeff Dike (jdike@karaya.com) * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
#include <stdlib.h> #include <stddef.h>
#include <errno.h> #include <errno.h>
#include "chan_user.h" #include <fcntl.h>
#include "os.h" #include "os.h"
#include "chan_user.h"
/* This address is used only as a unique identifer */ /* This address is used only as a unique identifer */
static int null_chan; static int null_chan;
static void *null_init(char *str, int device, const struct chan_opts *opts) static void *null_init(char *str, int device, const struct chan_opts *opts)
{ {
return(&null_chan); return &null_chan;
} }
static int null_open(int input, int output, int primary, void *d, static int null_open(int input, int output, int primary, void *d,
char **dev_out) char **dev_out)
{ {
int fd;
*dev_out = NULL; *dev_out = NULL;
return(os_open_file(DEV_NULL, of_rdwr(OPENFLAGS()), 0));
fd = open(DEV_NULL, O_RDWR);
return (fd < 0) ? -errno : fd;
} }
static int null_read(int fd, char *c_out, void *unused) static int null_read(int fd, char *c_out, void *unused)
{ {
return(-ENODEV); return -ENODEV;
} }
static void null_free(void *data) static void null_free(void *data)
...@@ -44,14 +49,3 @@ const struct chan_ops null_ops = { ...@@ -44,14 +49,3 @@ const struct chan_ops null_ops = {
.free = null_free, .free = null_free,
.winch = 0, .winch = 0,
}; };
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/
/* /*
* Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com) * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
#include "linux/list.h" #include "linux/completion.h"
#include "linux/sched.h"
#include "linux/slab.h"
#include "linux/interrupt.h" #include "linux/interrupt.h"
#include "linux/spinlock.h" #include "linux/list.h"
#include "linux/errno.h"
#include "asm/atomic.h" #include "asm/atomic.h"
#include "asm/semaphore.h"
#include "asm/errno.h"
#include "kern_util.h"
#include "kern.h"
#include "irq_user.h"
#include "irq_kern.h"
#include "port.h"
#include "init.h" #include "init.h"
#include "irq_kern.h"
#include "os.h" #include "os.h"
#include "port.h"
struct port_list { struct port_list {
struct list_head list; struct list_head list;
...@@ -53,8 +45,8 @@ static irqreturn_t pipe_interrupt(int irq, void *data) ...@@ -53,8 +45,8 @@ static irqreturn_t pipe_interrupt(int irq, void *data)
int fd; int fd;
fd = os_rcv_fd(conn->socket[0], &conn->helper_pid); fd = os_rcv_fd(conn->socket[0], &conn->helper_pid);
if(fd < 0){ if (fd < 0) {
if(fd == -EAGAIN) if (fd == -EAGAIN)
return IRQ_NONE; return IRQ_NONE;
printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n", printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n",
...@@ -81,18 +73,18 @@ static irqreturn_t pipe_interrupt(int irq, void *data) ...@@ -81,18 +73,18 @@ static irqreturn_t pipe_interrupt(int irq, void *data)
static int port_accept(struct port_list *port) static int port_accept(struct port_list *port)
{ {
struct connection *conn; struct connection *conn;
int fd, socket[2], pid, ret = 0; int fd, socket[2], pid;
fd = port_connection(port->fd, socket, &pid); fd = port_connection(port->fd, socket, &pid);
if(fd < 0){ if (fd < 0) {
if(fd != -EAGAIN) if (fd != -EAGAIN)
printk(KERN_ERR "port_accept : port_connection " printk(KERN_ERR "port_accept : port_connection "
"returned %d\n", -fd); "returned %d\n", -fd);
goto out; goto out;
} }
conn = kmalloc(sizeof(*conn), GFP_ATOMIC); conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
if(conn == NULL){ if (conn == NULL) {
printk(KERN_ERR "port_accept : failed to allocate " printk(KERN_ERR "port_accept : failed to allocate "
"connection\n"); "connection\n");
goto out_close; goto out_close;
...@@ -104,17 +96,17 @@ static int port_accept(struct port_list *port) ...@@ -104,17 +96,17 @@ static int port_accept(struct port_list *port)
.telnetd_pid = pid, .telnetd_pid = pid,
.port = port }); .port = port });
if(um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt, if (um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt,
IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
"telnetd", conn)){ "telnetd", conn)) {
printk(KERN_ERR "port_accept : failed to get IRQ for " printk(KERN_ERR "port_accept : failed to get IRQ for "
"telnetd\n"); "telnetd\n");
goto out_free; goto out_free;
} }
if(atomic_read(&port->wait_count) == 0){ if (atomic_read(&port->wait_count) == 0) {
os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG)); os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG));
printk("No one waiting for port\n"); printk(KERN_ERR "No one waiting for port\n");
} }
list_add(&conn->list, &port->pending); list_add(&conn->list, &port->pending);
return 1; return 1;
...@@ -123,28 +115,29 @@ static int port_accept(struct port_list *port) ...@@ -123,28 +115,29 @@ static int port_accept(struct port_list *port)
kfree(conn); kfree(conn);
out_close: out_close:
os_close_file(fd); os_close_file(fd);
if(pid != -1) os_kill_process(pid, 1);
os_kill_process(pid, 1);
out: out:
return ret; return 0;
} }
static DECLARE_MUTEX(ports_sem); static DECLARE_MUTEX(ports_sem);
static LIST_HEAD(ports); static LIST_HEAD(ports);
void port_work_proc(struct work_struct *unused) static void port_work_proc(struct work_struct *unused)
{ {
struct port_list *port; struct port_list *port;
struct list_head *ele; struct list_head *ele;
unsigned long flags; unsigned long flags;
local_irq_save(flags); local_irq_save(flags);
list_for_each(ele, &ports){ list_for_each(ele, &ports) {
port = list_entry(ele, struct port_list, list); port = list_entry(ele, struct port_list, list);
if(!port->has_connection) if (!port->has_connection)
continue; continue;
reactivate_fd(port->fd, ACCEPT_IRQ); reactivate_fd(port->fd, ACCEPT_IRQ);
while(port_accept(port)) ; while (port_accept(port))
;
port->has_connection = 0; port->has_connection = 0;
} }
local_irq_restore(flags); local_irq_restore(flags);
...@@ -169,25 +162,27 @@ void *port_data(int port_num) ...@@ -169,25 +162,27 @@ void *port_data(int port_num)
int fd; int fd;
down(&ports_sem); down(&ports_sem);
list_for_each(ele, &ports){ list_for_each(ele, &ports) {
port = list_entry(ele, struct port_list, list); port = list_entry(ele, struct port_list, list);
if(port->port == port_num) goto found; if (port->port == port_num)
goto found;
} }
port = kmalloc(sizeof(struct port_list), GFP_KERNEL); port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
if(port == NULL){ if (port == NULL) {
printk(KERN_ERR "Allocation of port list failed\n"); printk(KERN_ERR "Allocation of port list failed\n");
goto out; goto out;
} }
fd = port_listen_fd(port_num); fd = port_listen_fd(port_num);
if(fd < 0){ if (fd < 0) {
printk(KERN_ERR "binding to port %d failed, errno = %d\n", printk(KERN_ERR "binding to port %d failed, errno = %d\n",
port_num, -fd); port_num, -fd);
goto out_free; goto out_free;
} }
if(um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt,
if (um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt,
IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
"port", port)){ "port", port)) {
printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num); printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
goto out_close; goto out_close;
} }
...@@ -206,7 +201,7 @@ void *port_data(int port_num) ...@@ -206,7 +201,7 @@ void *port_data(int port_num)
found: found:
dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL); dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
if(dev == NULL){ if (dev == NULL) {
printk(KERN_ERR "Allocation of port device entry failed\n"); printk(KERN_ERR "Allocation of port device entry failed\n");
goto out; goto out;
} }
...@@ -233,9 +228,9 @@ int port_wait(void *data) ...@@ -233,9 +228,9 @@ int port_wait(void *data)
int fd; int fd;
atomic_inc(&port->wait_count); atomic_inc(&port->wait_count);
while(1){ while (1) {
fd = -ERESTARTSYS; fd = -ERESTARTSYS;
if(wait_for_completion_interruptible(&port->done)) if (wait_for_completion_interruptible(&port->done))
goto out; goto out;
spin_lock(&port->lock); spin_lock(&port->lock);
...@@ -258,7 +253,8 @@ int port_wait(void *data) ...@@ -258,7 +253,8 @@ int port_wait(void *data)
*/ */
free_irq(TELNETD_IRQ, conn); free_irq(TELNETD_IRQ, conn);
if(conn->fd >= 0) break; if (conn->fd >= 0)
break;
os_close_file(conn->fd); os_close_file(conn->fd);
kfree(conn); kfree(conn);
} }
...@@ -276,9 +272,9 @@ void port_remove_dev(void *d) ...@@ -276,9 +272,9 @@ void port_remove_dev(void *d)
{ {
struct port_dev *dev = d; struct port_dev *dev = d;
if(dev->helper_pid != -1) if (dev->helper_pid != -1)
os_kill_process(dev->helper_pid, 0); os_kill_process(dev->helper_pid, 0);
if(dev->telnetd_pid != -1) if (dev->telnetd_pid != -1)
os_kill_process(dev->telnetd_pid, 1); os_kill_process(dev->telnetd_pid, 1);
dev->helper_pid = -1; dev->helper_pid = -1;
dev->telnetd_pid = -1; dev->telnetd_pid = -1;
...@@ -297,7 +293,7 @@ static void free_port(void) ...@@ -297,7 +293,7 @@ static void free_port(void)
struct list_head *ele; struct list_head *ele;
struct port_list *port; struct port_list *port;
list_for_each(ele, &ports){ list_for_each(ele, &ports) {
port = list_entry(ele, struct port_list, list); port = list_entry(ele, struct port_list, list);
free_irq_by_fd(port->fd); free_irq_by_fd(port->fd);
os_close_file(port->fd); os_close_file(port->fd);
......
/* /*
* Copyright (C) 2001 Jeff Dike (jdike@karaya.com) * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
#include <stdio.h> #include <stdio.h>
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <termios.h> #include <termios.h>
#include <sys/socket.h> #include <unistd.h>
#include <sys/un.h>
#include <netinet/in.h> #include <netinet/in.h>
#include "kern_util.h"
#include "user.h"
#include "chan_user.h" #include "chan_user.h"
#include "port.h" #include "kern_constants.h"
#include "os.h" #include "os.h"
#include "port.h"
#include "um_malloc.h" #include "um_malloc.h"
#include "user.h"
struct port_chan { struct port_chan {
int raw; int raw;
...@@ -34,24 +30,25 @@ static void *port_init(char *str, int device, const struct chan_opts *opts) ...@@ -34,24 +30,25 @@ static void *port_init(char *str, int device, const struct chan_opts *opts)
char *end; char *end;
int port; int port;
if(*str != ':'){ if (*str != ':') {
printk("port_init : channel type 'port' must specify a " printk(UM_KERN_ERR "port_init : channel type 'port' must "
"port number\n"); "specify a port number\n");
return NULL; return NULL;
} }
str++; str++;
port = strtoul(str, &end, 0); port = strtoul(str, &end, 0);
if((*end != '\0') || (end == str)){ if ((*end != '\0') || (end == str)) {
printk("port_init : couldn't parse port '%s'\n", str); printk(UM_KERN_ERR "port_init : couldn't parse port '%s'\n",
str);
return NULL; return NULL;
} }
kern_data = port_data(port); kern_data = port_data(port);
if(kern_data == NULL) if (kern_data == NULL)
return NULL; return NULL;
data = kmalloc(sizeof(*data), UM_GFP_KERNEL); data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
if(data == NULL) if (data == NULL)
goto err; goto err;
*data = ((struct port_chan) { .raw = opts->raw, *data = ((struct port_chan) { .raw = opts->raw,
...@@ -79,13 +76,13 @@ static int port_open(int input, int output, int primary, void *d, ...@@ -79,13 +76,13 @@ static int port_open(int input, int output, int primary, void *d,
int fd, err; int fd, err;
fd = port_wait(data->kernel_data); fd = port_wait(data->kernel_data);
if((fd >= 0) && data->raw){ if ((fd >= 0) && data->raw) {
CATCH_EINTR(err = tcgetattr(fd, &data->tt)); CATCH_EINTR(err = tcgetattr(fd, &data->tt));
if(err) if (err)
return err; return err;
err = raw(fd); err = raw(fd);
if(err) if (err)
return err; return err;
} }
*dev_out = data->dev; *dev_out = data->dev;
...@@ -119,11 +116,11 @@ int port_listen_fd(int port) ...@@ -119,11 +116,11 @@ int port_listen_fd(int port)
int fd, err, arg; int fd, err, arg;
fd = socket(PF_INET, SOCK_STREAM, 0); fd = socket(PF_INET, SOCK_STREAM, 0);
if(fd == -1) if (fd == -1)
return -errno; return -errno;
arg = 1; arg = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0) {
err = -errno; err = -errno;
goto out; goto out;
} }
...@@ -131,23 +128,23 @@ int port_listen_fd(int port) ...@@ -131,23 +128,23 @@ int port_listen_fd(int port)
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){ if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
err = -errno; err = -errno;
goto out; goto out;
} }
if(listen(fd, 1) < 0){ if (listen(fd, 1) < 0) {
err = -errno; err = -errno;
goto out; goto out;
} }
err = os_set_fd_block(fd, 0); err = os_set_fd_block(fd, 0);
if(err < 0) if (err < 0)
goto out; goto out;
return fd; return fd;
out: out:
os_close_file(fd); close(fd);
return err; return err;
} }
...@@ -163,10 +160,10 @@ void port_pre_exec(void *arg) ...@@ -163,10 +160,10 @@ void port_pre_exec(void *arg)
dup2(data->sock_fd, 0); dup2(data->sock_fd, 0);
dup2(data->sock_fd, 1); dup2(data->sock_fd, 1);
dup2(data->sock_fd, 2); dup2(data->sock_fd, 2);
os_close_file(data->sock_fd); close(data->sock_fd);
dup2(data->pipe_fd, 3); dup2(data->pipe_fd, 3);
os_shutdown_socket(3, 1, 0); shutdown(3, SHUT_RD);
os_close_file(data->pipe_fd); close(data->pipe_fd);
} }
int port_connection(int fd, int *socket, int *pid_out) int port_connection(int fd, int *socket, int *pid_out)
...@@ -176,12 +173,12 @@ int port_connection(int fd, int *socket, int *pid_out) ...@@ -176,12 +173,12 @@ int port_connection(int fd, int *socket, int *pid_out)
"/usr/lib/uml/port-helper", NULL }; "/usr/lib/uml/port-helper", NULL };
struct port_pre_exec_data data; struct port_pre_exec_data data;
new = os_accept_connection(fd); new = accept(fd, NULL, 0);
if(new < 0) if (new < 0)
return new; return -errno;
err = os_pipe(socket, 0, 0); err = os_pipe(socket, 0, 0);
if(err < 0) if (err < 0)
goto out_close; goto out_close;
data = ((struct port_pre_exec_data) data = ((struct port_pre_exec_data)
...@@ -189,18 +186,18 @@ int port_connection(int fd, int *socket, int *pid_out) ...@@ -189,18 +186,18 @@ int port_connection(int fd, int *socket, int *pid_out)
.pipe_fd = socket[1] }); .pipe_fd = socket[1] });
err = run_helper(port_pre_exec, &data, argv); err = run_helper(port_pre_exec, &data, argv);
if(err < 0) if (err < 0)
goto out_shutdown; goto out_shutdown;
*pid_out = err; *pid_out = err;
return new; return new;
out_shutdown: out_shutdown:
os_shutdown_socket(socket[0], 1, 1); shutdown(socket[0], SHUT_RDWR);
os_close_file(socket[0]); close(socket[0]);
os_shutdown_socket(socket[1], 1, 1); shutdown(socket[1], SHUT_RDWR);
os_close_file(socket[1]); close(socket[1]);
out_close: out_close:
os_close_file(new); close(new);
return err; return err;
} }
...@@ -56,11 +56,11 @@ static int pts_open(int input, int output, int primary, void *d, ...@@ -56,11 +56,11 @@ static int pts_open(int input, int output, int primary, void *d,
if (data->raw) { if (data->raw) {
CATCH_EINTR(err = tcgetattr(fd, &data->tt)); CATCH_EINTR(err = tcgetattr(fd, &data->tt));
if (err) if (err)
return err; goto out_close;
err = raw(fd); err = raw(fd);
if (err) if (err)
return err; goto out_close;
} }
dev = ptsname(fd); dev = ptsname(fd);
...@@ -71,6 +71,10 @@ static int pts_open(int input, int output, int primary, void *d, ...@@ -71,6 +71,10 @@ static int pts_open(int input, int output, int primary, void *d,
(*data->announce)(dev, data->dev); (*data->announce)(dev, data->dev);
return fd; return fd;
out_close:
close(fd);
return err;
} }
static int getmaster(char *line) static int getmaster(char *line)
...@@ -119,10 +123,12 @@ static int pty_open(int input, int output, int primary, void *d, ...@@ -119,10 +123,12 @@ static int pty_open(int input, int output, int primary, void *d,
if (fd < 0) if (fd < 0)
return fd; return fd;
if(data->raw){ if (data->raw) {
err = raw(fd); err = raw(fd);
if (err) if (err) {
close(fd);
return err; return err;
}
} }
if (data->announce) if (data->announce)
......
/* /*
* Copyright (C) 2001 Jeff Dike (jdike@karaya.com) * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
#include <stdio.h>
#include <termios.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <fcntl.h>
#include <termios.h>
#include "chan_user.h" #include "chan_user.h"
#include "user.h" #include "kern_constants.h"
#include "os.h" #include "os.h"
#include "um_malloc.h" #include "um_malloc.h"
#include "user.h"
struct tty_chan { struct tty_chan {
char *dev; char *dev;
...@@ -22,15 +22,15 @@ static void *tty_chan_init(char *str, int device, const struct chan_opts *opts) ...@@ -22,15 +22,15 @@ static void *tty_chan_init(char *str, int device, const struct chan_opts *opts)
{ {
struct tty_chan *data; struct tty_chan *data;
if(*str != ':'){ if (*str != ':') {
printk("tty_init : channel type 'tty' must specify " printk(UM_KERN_ERR "tty_init : channel type 'tty' must specify "
"a device\n"); "a device\n");
return NULL; return NULL;
} }
str++; str++;
data = kmalloc(sizeof(*data), UM_GFP_KERNEL); data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
if(data == NULL) if (data == NULL)
return NULL; return NULL;
*data = ((struct tty_chan) { .dev = str, *data = ((struct tty_chan) { .dev = str,
.raw = opts->raw }); .raw = opts->raw });
...@@ -42,19 +42,26 @@ static int tty_open(int input, int output, int primary, void *d, ...@@ -42,19 +42,26 @@ static int tty_open(int input, int output, int primary, void *d,
char **dev_out) char **dev_out)
{ {
struct tty_chan *data = d; struct tty_chan *data = d;
int fd, err; int fd, err, mode = 0;
if (input && output)
mode = O_RDWR;
else if (input)
mode = O_RDONLY;
else if (output)
mode = O_WRONLY;
fd = os_open_file(data->dev, of_set_rw(OPENFLAGS(), input, output), 0); fd = open(data->dev, mode);
if(fd < 0) if (fd < 0)
return fd; return -errno;
if(data->raw){ if (data->raw) {
CATCH_EINTR(err = tcgetattr(fd, &data->tt)); CATCH_EINTR(err = tcgetattr(fd, &data->tt));
if(err) if (err)
return err; return err;
err = raw(fd); err = raw(fd);
if(err) if (err)
return err; return err;
} }
......
...@@ -3,18 +3,19 @@ ...@@ -3,18 +3,19 @@
* Licensed under the GPL * Licensed under the GPL
*/ */
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include <termios.h> #include <termios.h>
#include "chan_user.h" #include "chan_user.h"
#include "kern_constants.h"
#include "os.h" #include "os.h"
#include "init.h" #include "um_malloc.h"
#include "user.h" #include "user.h"
#include "xterm.h" #include "xterm.h"
#include "kern_constants.h"
struct xterm_chan { struct xterm_chan {
int pid; int pid;
...@@ -29,7 +30,7 @@ static void *xterm_init(char *str, int device, const struct chan_opts *opts) ...@@ -29,7 +30,7 @@ static void *xterm_init(char *str, int device, const struct chan_opts *opts)
{ {
struct xterm_chan *data; struct xterm_chan *data;
data = malloc(sizeof(*data)); data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
if (data == NULL) if (data == NULL)
return NULL; return NULL;
*data = ((struct xterm_chan) { .pid = -1, *data = ((struct xterm_chan) { .pid = -1,
...@@ -207,11 +208,6 @@ static void xterm_close(int fd, void *d) ...@@ -207,11 +208,6 @@ static void xterm_close(int fd, void *d)
os_close_file(fd); os_close_file(fd);
} }
static void xterm_free(void *d)
{
free(d);
}
const struct chan_ops xterm_ops = { const struct chan_ops xterm_ops = {
.type = "xterm", .type = "xterm",
.init = xterm_init, .init = xterm_init,
...@@ -221,6 +217,6 @@ const struct chan_ops xterm_ops = { ...@@ -221,6 +217,6 @@ const struct chan_ops xterm_ops = {
.write = generic_write, .write = generic_write,
.console_write = generic_console_write, .console_write = generic_console_write,
.window_size = generic_window_size, .window_size = generic_window_size,
.free = xterm_free, .free = generic_free,
.winch = 1, .winch = 1,
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment