ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / kernel / tty_log.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) and 
3  * geoffrey hing <ghing@net.ohio-state.edu>
4  * Licensed under the GPL
5  */
6
7 #include <errno.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/time.h>
14 #include "init.h"
15 #include "user.h"
16 #include "os.h"
17
18 #define TTY_LOG_DIR "./"
19
20 /* Set early in boot and then unchanged */
21 static char *tty_log_dir = TTY_LOG_DIR;
22 static int tty_log_fd = -1;
23
24 #define TTY_LOG_OPEN 1
25 #define TTY_LOG_CLOSE 2
26 #define TTY_LOG_WRITE 3
27
28 struct tty_log_buf {
29         int what;
30         unsigned long tty;
31         int len;
32 };
33
34 int open_tty_log(void *tty)
35 {
36         struct timeval tv;
37         struct tty_log_buf data;
38         char buf[strlen(tty_log_dir) + sizeof("01234567890-01234567\0")];
39         int fd;
40
41         if(tty_log_fd != -1){
42                 data = ((struct tty_log_buf) { what :   TTY_LOG_OPEN,
43                                                tty : (unsigned long) tty,
44                                                len : 0 });
45                 write(tty_log_fd, &data, sizeof(data));
46                 return(tty_log_fd);
47         }
48
49         gettimeofday(&tv, NULL);
50         sprintf(buf, "%s/%0u-%0u", tty_log_dir, (unsigned int) tv.tv_sec, 
51                 (unsigned int) tv.tv_usec);
52
53         fd = os_open_file(buf, of_append(of_create(of_rdwr(OPENFLAGS()))),
54                           0644);
55         if(fd < 0){
56                 printk("open_tty_log : couldn't open '%s', errno = %d\n",
57                        buf, -fd);
58         }
59         return(fd);
60 }
61
62 void close_tty_log(int fd, void *tty)
63 {
64         struct tty_log_buf data;
65
66         if(tty_log_fd != -1){
67                 data = ((struct tty_log_buf) { what :   TTY_LOG_CLOSE,
68                                                tty : (unsigned long) tty,
69                                                len : 0 });
70                 write(tty_log_fd, &data, sizeof(data));
71                 return;
72         }
73         close(fd);
74 }
75
76 int write_tty_log(int fd, char *buf, int len, void *tty)
77 {
78         struct tty_log_buf data;
79
80         if(fd == tty_log_fd){
81                 data = ((struct tty_log_buf) { what :   TTY_LOG_WRITE,
82                                                tty : (unsigned long) tty,
83                                                len : len });
84                 write(tty_log_fd, &data, sizeof(data));
85         }
86         return(write(fd, buf, len));
87 }
88
89 static int __init set_tty_log_dir(char *name, int *add)
90 {
91         tty_log_dir = name;
92         return 0;
93 }
94
95 __uml_setup("tty_log_dir=", set_tty_log_dir,
96 "tty_log_dir=<directory>\n"
97 "    This is used to specify the directory where the logs of all pty\n"
98 "    data from this UML machine will be written.\n\n"
99 );
100
101 static int __init set_tty_log_fd(char *name, int *add)
102 {
103         char *end;
104
105         tty_log_fd = strtoul(name, &end, 0);
106         if((*end != '\0') || (end == name)){
107                 printk("set_tty_log_fd - strtoul failed on '%s'\n", name);
108                 tty_log_fd = -1;
109         }
110         return 0;
111 }
112
113 __uml_setup("tty_log_fd=", set_tty_log_fd,
114 "tty_log_fd=<fd>\n"
115 "    This is used to specify a preconfigured file descriptor to which all\n"
116 "    tty data will be written.  Preconfigure the descriptor with something\n"
117 "    like '10>tty_log tty_log_fd=10'.\n\n"
118 );
119
120
121 /*
122  * Overrides for Emacs so that we follow Linus's tabbing style.
123  * Emacs will notice this stuff at the end of the file and automatically
124  * adjust the settings for this buffer only.  This must remain at the end
125  * of the file.
126  * ---------------------------------------------------------------------------
127  * Local variables:
128  * c-file-style: "linux"
129  * End:
130  */