summaryrefslogtreecommitdiff
path: root/arch/um/os-Linux
diff options
context:
space:
mode:
Diffstat (limited to 'arch/um/os-Linux')
-rw-r--r--arch/um/os-Linux/Makefile13
-rw-r--r--arch/um/os-Linux/irq.c162
-rw-r--r--arch/um/os-Linux/sigio.c323
-rw-r--r--arch/um/os-Linux/start_up.c128
-rw-r--r--arch/um/os-Linux/tt.c10
-rw-r--r--arch/um/os-Linux/tty_log.c218
-rw-r--r--arch/um/os-Linux/umid.c33
7 files changed, 872 insertions, 15 deletions
diff --git a/arch/um/os-Linux/Makefile b/arch/um/os-Linux/Makefile
index 08a4e628b24c..1659386b42bb 100644
--- a/arch/um/os-Linux/Makefile
+++ b/arch/um/os-Linux/Makefile
@@ -3,14 +3,17 @@
# Licensed under the GPL
#
-obj-y = aio.o elf_aux.o file.o helper.o main.o mem.o process.o signal.o \
- start_up.o time.o trap.o tt.o tty.o uaccess.o umid.o user_syms.o \
- util.o drivers/ sys-$(SUBARCH)/
+obj-y = aio.o elf_aux.o file.o helper.o irq.o main.o mem.o process.o sigio.o \
+ signal.o start_up.o time.o trap.o tt.o tty.o uaccess.o umid.o \
+ user_syms.o util.o drivers/ sys-$(SUBARCH)/
obj-$(CONFIG_MODE_SKAS) += skas/
+obj-$(CONFIG_TTY_LOG) += tty_log.o
+user-objs-$(CONFIG_TTY_LOG) += tty_log.o
-USER_OBJS := aio.o elf_aux.o file.o helper.o main.o mem.o process.o signal.o \
- start_up.o time.o trap.o tt.o tty.o uaccess.o umid.o util.o
+USER_OBJS := $(user-objs-y) aio.o elf_aux.o file.o helper.o irq.o main.o mem.o \
+ process.o sigio.o signal.o start_up.o time.o trap.o tt.o tty.o \
+ uaccess.o umid.o util.o
elf_aux.o: $(ARCH_DIR)/kernel-offsets.h
CFLAGS_elf_aux.o += -I$(objtree)/arch/um
diff --git a/arch/um/os-Linux/irq.c b/arch/um/os-Linux/irq.c
new file mode 100644
index 000000000000..e599be423da1
--- /dev/null
+++ b/arch/um/os-Linux/irq.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
+ * Licensed under the GPL
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+#include <string.h>
+#include <sys/poll.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include "user_util.h"
+#include "kern_util.h"
+#include "user.h"
+#include "process.h"
+#include "sigio.h"
+#include "irq_user.h"
+#include "os.h"
+
+static struct pollfd *pollfds = NULL;
+static int pollfds_num = 0;
+static int pollfds_size = 0;
+
+int os_waiting_for_events(struct irq_fd *active_fds)
+{
+ struct irq_fd *irq_fd;
+ int i, n, err;
+
+ n = poll(pollfds, pollfds_num, 0);
+ if(n < 0){
+ err = -errno;
+ if(errno != EINTR)
+ printk("sigio_handler: os_waiting_for_events:"
+ " poll returned %d, errno = %d\n", n, errno);
+ return err;
+ }
+
+ if(n == 0)
+ return 0;
+
+ irq_fd = active_fds;
+
+ for(i = 0; i < pollfds_num; i++){
+ if(pollfds[i].revents != 0){
+ irq_fd->current_events = pollfds[i].revents;
+ pollfds[i].fd = -1;
+ }
+ irq_fd = irq_fd->next;
+ }
+ return n;
+}
+
+int os_isatty(int fd)
+{
+ return(isatty(fd));
+}
+
+int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
+{
+ if (pollfds_num == pollfds_size) {
+ if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
+ /* return min size needed for new pollfds area */
+ return((pollfds_size + 1) * sizeof(pollfds[0]));
+ }
+
+ if(pollfds != NULL){
+ memcpy(tmp_pfd, pollfds,
+ sizeof(pollfds[0]) * pollfds_size);
+ /* remove old pollfds */
+ kfree(pollfds);
+ }
+ pollfds = tmp_pfd;
+ pollfds_size++;
+ } else {
+ /* remove not used tmp_pfd */
+ if (tmp_pfd != NULL)
+ kfree(tmp_pfd);
+ }
+
+ pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
+ .events = events,
+ .revents = 0 });
+ pollfds_num++;
+
+ return(0);
+}
+
+void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
+ struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
+{
+ struct irq_fd **prev;
+ int i = 0;
+
+ prev = &active_fds;
+ while(*prev != NULL){
+ if((*test)(*prev, arg)){
+ struct irq_fd *old_fd = *prev;
+ if((pollfds[i].fd != -1) &&
+ (pollfds[i].fd != (*prev)->fd)){
+ printk("os_free_irq_by_cb - mismatch between "
+ "active_fds and pollfds, fd %d vs %d\n",
+ (*prev)->fd, pollfds[i].fd);
+ goto out;
+ }
+
+ pollfds_num--;
+
+ /* This moves the *whole* array after pollfds[i]
+ * (though it doesn't spot as such)!
+ */
+
+ memmove(&pollfds[i], &pollfds[i + 1],
+ (pollfds_num - i) * sizeof(pollfds[0]));
+ if(*last_irq_ptr2 == &old_fd->next)
+ *last_irq_ptr2 = prev;
+
+ *prev = (*prev)->next;
+ if(old_fd->type == IRQ_WRITE)
+ ignore_sigio_fd(old_fd->fd);
+ kfree(old_fd);
+ continue;
+ }
+ prev = &(*prev)->next;
+ i++;
+ }
+ out:
+ return;
+}
+
+
+int os_get_pollfd(int i)
+{
+ return(pollfds[i].fd);
+}
+
+void os_set_pollfd(int i, int fd)
+{
+ pollfds[i].fd = fd;
+}
+
+void os_set_ioignore(void)
+{
+ set_handler(SIGIO, SIG_IGN, 0, -1);
+}
+
+void init_irq_signals(int on_sigstack)
+{
+ __sighandler_t h;
+ int flags;
+
+ flags = on_sigstack ? SA_ONSTACK : 0;
+ if(timer_irq_inited) h = (__sighandler_t) alarm_handler;
+ else h = boot_timer_handler;
+
+ set_handler(SIGVTALRM, h, flags | SA_RESTART,
+ SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
+ set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
+ SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
+ signal(SIGWINCH, SIG_IGN);
+}
diff --git a/arch/um/os-Linux/sigio.c b/arch/um/os-Linux/sigio.c
new file mode 100644
index 000000000000..9ba942947146
--- /dev/null
+++ b/arch/um/os-Linux/sigio.c
@@ -0,0 +1,323 @@
+/*
+ * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
+ * Licensed under the GPL
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <pty.h>
+#include <signal.h>
+#include <errno.h>
+#include <string.h>
+#include <sched.h>
+#include <sys/socket.h>
+#include <sys/poll.h>
+#include "init.h"
+#include "user.h"
+#include "kern_util.h"
+#include "user_util.h"
+#include "sigio.h"
+#include "os.h"
+
+/* Protected by sigio_lock(), also used by sigio_cleanup, which is an
+ * exitcall.
+ */
+static int write_sigio_pid = -1;
+
+/* These arrays are initialized before the sigio thread is started, and
+ * the descriptors closed after it is killed. So, it can't see them change.
+ * On the UML side, they are changed under the sigio_lock.
+ */
+#define SIGIO_FDS_INIT {-1, -1}
+
+static int write_sigio_fds[2] = SIGIO_FDS_INIT;
+static int sigio_private[2] = SIGIO_FDS_INIT;
+
+struct pollfds {
+ struct pollfd *poll;
+ int size;
+ int used;
+};
+
+/* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
+ * synchronizes with it.
+ */
+struct pollfds current_poll = {
+ .poll = NULL,
+ .size = 0,
+ .used = 0
+};
+
+struct pollfds next_poll = {
+ .poll = NULL,
+ .size = 0,
+ .used = 0
+};
+
+static int write_sigio_thread(void *unused)
+{
+ struct pollfds *fds, tmp;
+ struct pollfd *p;
+ int i, n, respond_fd;
+ char c;
+
+ signal(SIGWINCH, SIG_IGN);
+ fds = &current_poll;
+ while(1){
+ n = poll(fds->poll, fds->used, -1);
+ if(n < 0){
+ if(errno == EINTR) continue;
+ printk("write_sigio_thread : poll returned %d, "
+ "errno = %d\n", n, errno);
+ }
+ for(i = 0; i < fds->used; i++){
+ p = &fds->poll[i];
+ if(p->revents == 0) continue;
+ if(p->fd == sigio_private[1]){
+ n = os_read_file(sigio_private[1], &c, sizeof(c));
+ if(n != sizeof(c))
+ printk("write_sigio_thread : "
+ "read failed, err = %d\n", -n);
+ tmp = current_poll;
+ current_poll = next_poll;
+ next_poll = tmp;
+ respond_fd = sigio_private[1];
+ }
+ else {
+ respond_fd = write_sigio_fds[1];
+ fds->used--;
+ memmove(&fds->poll[i], &fds->poll[i + 1],
+ (fds->used - i) * sizeof(*fds->poll));
+ }
+
+ n = os_write_file(respond_fd, &c, sizeof(c));
+ if(n != sizeof(c))
+ printk("write_sigio_thread : write failed, "
+ "err = %d\n", -n);
+ }
+ }
+
+ return 0;
+}
+
+static int need_poll(int n)
+{
+ if(n <= next_poll.size){
+ next_poll.used = n;
+ return(0);
+ }
+ kfree(next_poll.poll);
+ next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
+ if(next_poll.poll == NULL){
+ printk("need_poll : failed to allocate new pollfds\n");
+ next_poll.size = 0;
+ next_poll.used = 0;
+ return(-1);
+ }
+ next_poll.size = n;
+ next_poll.used = n;
+ return(0);
+}
+
+/* Must be called with sigio_lock held, because it's needed by the marked
+ * critical section. */
+static void update_thread(void)
+{
+ unsigned long flags;
+ int n;
+ char c;
+
+ flags = set_signals(0);
+ n = os_write_file(sigio_private[0], &c, sizeof(c));
+ if(n != sizeof(c)){
+ printk("update_thread : write failed, err = %d\n", -n);
+ goto fail;
+ }
+
+ n = os_read_file(sigio_private[0], &c, sizeof(c));
+ if(n != sizeof(c)){
+ printk("update_thread : read failed, err = %d\n", -n);
+ goto fail;
+ }
+
+ set_signals(flags);
+ return;
+ fail:
+ /* Critical section start */
+ if(write_sigio_pid != -1)
+ os_kill_process(write_sigio_pid, 1);
+ write_sigio_pid = -1;
+ close(sigio_private[0]);
+ close(sigio_private[1]);
+ close(write_sigio_fds[0]);
+ close(write_sigio_fds[1]);
+ /* Critical section end */
+ set_signals(flags);
+}
+
+int add_sigio_fd(int fd, int read)
+{
+ int err = 0, i, n, events;
+
+ sigio_lock();
+ for(i = 0; i < current_poll.used; i++){
+ if(current_poll.poll[i].fd == fd)
+ goto out;
+ }
+
+ n = current_poll.used + 1;
+ err = need_poll(n);
+ if(err)
+ goto out;
+
+ for(i = 0; i < current_poll.used; i++)
+ next_poll.poll[i] = current_poll.poll[i];
+
+ if(read) events = POLLIN;
+ else events = POLLOUT;
+
+ next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
+ .events = events,
+ .revents = 0 });
+ update_thread();
+ out:
+ sigio_unlock();
+ return(err);
+}
+
+int ignore_sigio_fd(int fd)
+{
+ struct pollfd *p;
+ int err = 0, i, n = 0;
+
+ sigio_lock();
+ for(i = 0; i < current_poll.used; i++){
+ if(current_poll.poll[i].fd == fd) break;
+ }
+ if(i == current_poll.used)
+ goto out;
+
+ err = need_poll(current_poll.used - 1);
+ if(err)
+ goto out;
+
+ for(i = 0; i < current_poll.used; i++){
+ p = &current_poll.poll[i];
+ if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
+ }
+ if(n == i){
+ printk("ignore_sigio_fd : fd %d not found\n", fd);
+ err = -1;
+ goto out;
+ }
+
+ update_thread();
+ out:
+ sigio_unlock();
+ return(err);
+}
+
+static struct pollfd *setup_initial_poll(int fd)
+{
+ struct pollfd *p;
+
+ p = um_kmalloc(sizeof(struct pollfd));
+ if (p == NULL) {
+ printk("setup_initial_poll : failed to allocate poll\n");
+ return NULL;
+ }
+ *p = ((struct pollfd) { .fd = fd,
+ .events = POLLIN,
+ .revents = 0 });
+ return p;
+}
+
+void write_sigio_workaround(void)
+{
+ unsigned long stack;
+ struct pollfd *p;
+ int err;
+ int l_write_sigio_fds[2];
+ int l_sigio_private[2];
+ int l_write_sigio_pid;
+
+ /* We call this *tons* of times - and most ones we must just fail. */
+ sigio_lock();
+ l_write_sigio_pid = write_sigio_pid;
+ sigio_unlock();
+
+ if (l_write_sigio_pid != -1)
+ return;
+
+ err = os_pipe(l_write_sigio_fds, 1, 1);
+ if(err < 0){
+ printk("write_sigio_workaround - os_pipe 1 failed, "
+ "err = %d\n", -err);
+ return;
+ }
+ err = os_pipe(l_sigio_private, 1, 1);
+ if(err < 0){
+ printk("write_sigio_workaround - os_pipe 2 failed, "
+ "err = %d\n", -err);
+ goto out_close1;
+ }
+
+ p = setup_initial_poll(l_sigio_private[1]);
+ if(!p)
+ goto out_close2;
+
+ sigio_lock();
+
+ /* Did we race? Don't try to optimize this, please, it's not so likely
+ * to happen, and no more than once at the boot. */
+ if(write_sigio_pid != -1)
+ goto out_free;
+
+ current_poll = ((struct pollfds) { .poll = p,
+ .used = 1,
+ .size = 1 });
+
+ if (write_sigio_irq(l_write_sigio_fds[0]))
+ goto out_clear_poll;
+
+ memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
+ memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
+
+ write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
+ CLONE_FILES | CLONE_VM, &stack, 0);
+
+ if (write_sigio_pid < 0)
+ goto out_clear;
+
+ sigio_unlock();
+ return;
+
+out_clear:
+ write_sigio_pid = -1;
+ write_sigio_fds[0] = -1;
+ write_sigio_fds[1] = -1;
+ sigio_private[0] = -1;
+ sigio_private[1] = -1;
+out_clear_poll:
+ current_poll = ((struct pollfds) { .poll = NULL,
+ .size = 0,
+ .used = 0 });
+out_free:
+ kfree(p);
+ sigio_unlock();
+out_close2:
+ close(l_sigio_private[0]);
+ close(l_sigio_private[1]);
+out_close1:
+ close(l_write_sigio_fds[0]);
+ close(l_write_sigio_fds[1]);
+}
+
+void sigio_cleanup(void)
+{
+ if(write_sigio_pid != -1){
+ os_kill_process(write_sigio_pid, 1);
+ write_sigio_pid = -1;
+ }
+}
diff --git a/arch/um/os-Linux/start_up.c b/arch/um/os-Linux/start_up.c
index 829d6b0d8b02..32753131f8d8 100644
--- a/arch/um/os-Linux/start_up.c
+++ b/arch/um/os-Linux/start_up.c
@@ -3,6 +3,7 @@
* Licensed under the GPL
*/
+#include <pty.h>
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
@@ -539,3 +540,130 @@ int __init parse_iomem(char *str, int *add)
return(1);
}
+
+/* Changed during early boot */
+int pty_output_sigio = 0;
+int pty_close_sigio = 0;
+
+/* Used as a flag during SIGIO testing early in boot */
+static volatile int got_sigio = 0;
+
+static void __init handler(int sig)
+{
+ got_sigio = 1;
+}
+
+struct openpty_arg {
+ int master;
+ int slave;
+ int err;
+};
+
+static void openpty_cb(void *arg)
+{
+ struct openpty_arg *info = arg;
+
+ info->err = 0;
+ if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
+ info->err = -errno;
+}
+
+static void __init check_one_sigio(void (*proc)(int, int))
+{
+ struct sigaction old, new;
+ struct openpty_arg pty = { .master = -1, .slave = -1 };
+ int master, slave, err;
+
+ initial_thread_cb(openpty_cb, &pty);
+ if(pty.err){
+ printk("openpty failed, errno = %d\n", -pty.err);
+ return;
+ }
+
+ master = pty.master;
+ slave = pty.slave;
+
+ if((master == -1) || (slave == -1)){
+ printk("openpty failed to allocate a pty\n");
+ return;
+ }
+
+ /* Not now, but complain so we now where we failed. */
+ err = raw(master);
+ if (err < 0)
+ panic("check_sigio : __raw failed, errno = %d\n", -err);
+
+ err = os_sigio_async(master, slave);
+ if(err < 0)
+ panic("tty_fds : sigio_async failed, err = %d\n", -err);
+
+ if(sigaction(SIGIO, NULL, &old) < 0)
+ panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
+ new = old;
+ new.sa_handler = handler;
+ if(sigaction(SIGIO, &new, NULL) < 0)
+ panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
+
+ got_sigio = 0;
+ (*proc)(master, slave);
+
+ close(master);
+ close(slave);
+
+ if(sigaction(SIGIO, &old, NULL) < 0)
+ panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
+}
+
+static void tty_output(int master, int slave)
+{
+ int n;
+ char buf[512];
+
+ printk("Checking that host ptys support output SIGIO...");
+
+ memset(buf, 0, sizeof(buf));
+
+ while(os_write_file(master, buf, sizeof(buf)) > 0) ;
+ if(errno != EAGAIN)
+ panic("check_sigio : write failed, errno = %d\n", errno);
+ while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
+
+ if(got_sigio){
+ printk("Yes\n");
+ pty_output_sigio = 1;
+ }
+ else if(n == -EAGAIN) printk("No, enabling workaround\n");
+ else panic("check_sigio : read failed, err = %d\n", n);
+}
+
+static void tty_close(int master, int slave)
+{
+ printk("Checking that host ptys support SIGIO on close...");
+
+ close(slave);
+ if(got_sigio){
+ printk("Yes\n");
+ pty_close_sigio = 1;
+ }
+ else printk("No, enabling workaround\n");
+}
+
+void __init check_sigio(void)
+{
+ if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
+ (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
+ printk("No pseudo-terminals available - skipping pty SIGIO "
+ "check\n");
+ return;
+ }
+ check_one_sigio(tty_output);
+ check_one_sigio(tty_close);
+}
+
+void os_check_bugs(void)
+{
+ check_ptrace();
+ check_sigio();
+ check_devanon();
+}
+
diff --git a/arch/um/os-Linux/tt.c b/arch/um/os-Linux/tt.c
index 919d19f11537..5461a065bbb9 100644
--- a/arch/um/os-Linux/tt.c
+++ b/arch/um/os-Linux/tt.c
@@ -110,6 +110,16 @@ int wait_for_stop(int pid, int sig, int cont_type, void *relay)
}
}
+void forward_ipi(int fd, int pid)
+{
+ int err;
+
+ err = os_set_owner(fd, pid);
+ if(err < 0)
+ printk("forward_ipi: set_owner failed, fd = %d, me = %d, "
+ "target = %d, err = %d\n", fd, os_getpid(), pid, -err);
+}
+
/*
*-------------------------
* only for tt mode (will be deleted in future...)
diff --git a/arch/um/os-Linux/tty_log.c b/arch/um/os-Linux/tty_log.c
new file mode 100644
index 000000000000..c6ba56c1560f
--- /dev/null
+++ b/arch/um/os-Linux/tty_log.c
@@ -0,0 +1,218 @@
+/*
+ * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) and
+ * geoffrey hing <ghing@net.ohio-state.edu>
+ * Licensed under the GPL
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include "init.h"
+#include "user.h"
+#include "kern_util.h"
+#include "os.h"
+
+#define TTY_LOG_DIR "./"
+
+/* Set early in boot and then unchanged */
+static char *tty_log_dir = TTY_LOG_DIR;
+static int tty_log_fd = -1;
+
+#define TTY_LOG_OPEN 1
+#define TTY_LOG_CLOSE 2
+#define TTY_LOG_WRITE 3
+#define TTY_LOG_EXEC 4
+
+#define TTY_READ 1
+#define TTY_WRITE 2
+
+struct tty_log_buf {
+ int what;
+ unsigned long tty;
+ int len;
+ int direction;
+ unsigned long sec;
+ unsigned long usec;
+};
+
+int open_tty_log(void *tty, void *current_tty)
+{
+ struct timeval tv;
+ struct tty_log_buf data;
+ char buf[strlen(tty_log_dir) + sizeof("01234567890-01234567\0")];
+ int fd;
+
+ gettimeofday(&tv, NULL);
+ if(tty_log_fd != -1){
+ data = ((struct tty_log_buf) { .what = TTY_LOG_OPEN,
+ .tty = (unsigned long) tty,
+ .len = sizeof(current_tty),
+ .direction = 0,
+ .sec = tv.tv_sec,
+ .usec = tv.tv_usec } );
+ os_write_file(tty_log_fd, &data, sizeof(data));
+ os_write_file(tty_log_fd, &current_tty, data.len);
+ return(tty_log_fd);
+ }
+
+ sprintf(buf, "%s/%0u-%0u", tty_log_dir, (unsigned int) tv.tv_sec,
+ (unsigned int) tv.tv_usec);
+
+ fd = os_open_file(buf, of_append(of_create(of_rdwr(OPENFLAGS()))),
+ 0644);
+ if(fd < 0){
+ printk("open_tty_log : couldn't open '%s', errno = %d\n",
+ buf, -fd);
+ }
+ return(fd);
+}
+
+void close_tty_log(int fd, void *tty)
+{
+ struct tty_log_buf data;
+ struct timeval tv;
+
+ if(tty_log_fd != -1){
+ gettimeofday(&tv, NULL);
+ data = ((struct tty_log_buf) { .what = TTY_LOG_CLOSE,
+ .tty = (unsigned long) tty,
+ .len = 0,
+ .direction = 0,
+ .sec = tv.tv_sec,
+ .usec = tv.tv_usec } );
+ os_write_file(tty_log_fd, &data, sizeof(data));
+ return;
+ }
+ os_close_file(fd);
+}
+
+static int log_chunk(int fd, const char *buf, int len)
+{
+ int total = 0, try, missed, n;
+ char chunk[64];
+
+ while(len > 0){
+ try = (len > sizeof(chunk)) ? sizeof(chunk) : len;
+ missed = copy_from_user_proc(chunk, (char *) buf, try);
+ try -= missed;
+ n = os_write_file(fd, chunk, try);
+ if(n != try) {
+ if(n < 0)
+ return(n);
+ return(-EIO);
+ }
+ if(missed != 0)
+ return(-EFAULT);
+
+ len -= try;
+ total += try;
+ buf += try;
+ }
+
+ return(total);
+}
+
+int write_tty_log(int fd, const char *buf, int len, void *tty, int is_read)
+{
+ struct timeval tv;
+ struct tty_log_buf data;
+ int direction;
+
+ if(fd == tty_log_fd){
+ gettimeofday(&tv, NULL);
+ direction = is_read ? TTY_READ : TTY_WRITE;
+ data = ((struct tty_log_buf) { .what = TTY_LOG_WRITE,
+ .tty = (unsigned long) tty,
+ .len = len,
+ .direction = direction,
+ .sec = tv.tv_sec,
+ .usec = tv.tv_usec } );
+ os_write_file(tty_log_fd, &data, sizeof(data));
+ }
+
+ return(log_chunk(fd, buf, len));
+}
+
+void log_exec(char **argv, void *tty)
+{
+ struct timeval tv;
+ struct tty_log_buf data;
+ char **ptr,*arg;
+ int len;
+
+ if(tty_log_fd == -1) return;
+
+ gettimeofday(&tv, NULL);
+
+ len = 0;
+ for(ptr = argv; ; ptr++){
+ if(copy_from_user_proc(&arg, ptr, sizeof(arg)))
+ return;
+ if(arg == NULL) break;
+ len += strlen_user_proc(arg);
+ }
+
+ data = ((struct tty_log_buf) { .what = TTY_LOG_EXEC,
+ .tty = (unsigned long) tty,
+ .len = len,
+ .direction = 0,
+ .sec = tv.tv_sec,
+ .usec = tv.tv_usec } );
+ os_write_file(tty_log_fd, &data, sizeof(data));
+
+ for(ptr = argv; ; ptr++){
+ if(copy_from_user_proc(&arg, ptr, sizeof(arg)))
+ return;
+ if(arg == NULL) break;
+ log_chunk(tty_log_fd, arg, strlen_user_proc(arg));
+ }
+}
+
+extern void register_tty_logger(int (*opener)(void *, void *),
+ int (*writer)(int, const char *, int,
+ void *, int),
+ void (*closer)(int, void *));
+
+static int register_logger(void)
+{
+ register_tty_logger(open_tty_log, write_tty_log, close_tty_log);
+ return(0);
+}
+
+__uml_initcall(register_logger);
+
+static int __init set_tty_log_dir(char *name, int *add)
+{
+ tty_log_dir = name;
+ return 0;
+}
+
+__uml_setup("tty_log_dir=", set_tty_log_dir,
+"tty_log_dir=<directory>\n"
+" This is used to specify the directory where the logs of all pty\n"
+" data from this UML machine will be written.\n\n"
+);
+
+static int __init set_tty_log_fd(char *name, int *add)
+{
+ char *end;
+
+ tty_log_fd = strtoul(name, &end, 0);
+ if((*end != '\0') || (end == name)){
+ printf("set_tty_log_fd - strtoul failed on '%s'\n", name);
+ tty_log_fd = -1;
+ }
+
+ *add = 0;
+ return 0;
+}
+
+__uml_setup("tty_log_fd=", set_tty_log_fd,
+"tty_log_fd=<fd>\n"
+" This is used to specify a preconfigured file descriptor to which all\n"
+" tty data will be written. Preconfigure the descriptor with something\n"
+" like '10>tty_log tty_log_fd=10'.\n\n"
+);
diff --git a/arch/um/os-Linux/umid.c b/arch/um/os-Linux/umid.c
index ecf107ae5ac8..198e59163288 100644
--- a/arch/um/os-Linux/umid.c
+++ b/arch/um/os-Linux/umid.c
@@ -143,8 +143,10 @@ static int not_dead_yet(char *dir)
goto out_close;
}
- if((kill(p, 0) == 0) || (errno != ESRCH))
+ if((kill(p, 0) == 0) || (errno != ESRCH)){
+ printk("umid \"%s\" is already in use by pid %d\n", umid, p);
return 1;
+ }
err = actually_do_remove(dir);
if(err)
@@ -234,33 +236,44 @@ int __init make_umid(void)
err = mkdir(tmp, 0777);
if(err < 0){
err = -errno;
- if(errno != EEXIST)
+ if(err != -EEXIST)
goto err;
- if(not_dead_yet(tmp) < 0)
+ /* 1 -> this umid is already in use
+ * < 0 -> we couldn't remove the umid directory
+ * In either case, we can't use this umid, so return -EEXIST.
+ */
+ if(not_dead_yet(tmp) != 0)
goto err;
err = mkdir(tmp, 0777);
}
- if(err < 0){
- printk("Failed to create '%s' - err = %d\n", umid, err);
- goto err_rmdir;
+ if(err){
+ err = -errno;
+ printk("Failed to create '%s' - err = %d\n", umid, -errno);
+ goto err;
}
umid_setup = 1;
create_pid_file();
- return 0;
-
- err_rmdir:
- rmdir(tmp);
+ err = 0;
err:
return err;
}
static int __init make_umid_init(void)
{
+ if(!make_umid())
+ return 0;
+
+ /* If initializing with the given umid failed, then try again with
+ * a random one.
+ */
+ printk("Failed to initialize umid \"%s\", trying with a random umid\n",
+ umid);
+ *umid = '\0';
make_umid();
return 0;