Linux 2.6.17.7
[linux/fpc-iii.git] / arch / um / os-Linux / tty_log.c
blobc6ba56c1560f9c95af005f199d15324b1de360eb
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 */
7 #include <errno.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/time.h>
13 #include "init.h"
14 #include "user.h"
15 #include "kern_util.h"
16 #include "os.h"
18 #define TTY_LOG_DIR "./"
20 /* Set early in boot and then unchanged */
21 static char *tty_log_dir = TTY_LOG_DIR;
22 static int tty_log_fd = -1;
24 #define TTY_LOG_OPEN 1
25 #define TTY_LOG_CLOSE 2
26 #define TTY_LOG_WRITE 3
27 #define TTY_LOG_EXEC 4
29 #define TTY_READ 1
30 #define TTY_WRITE 2
32 struct tty_log_buf {
33 int what;
34 unsigned long tty;
35 int len;
36 int direction;
37 unsigned long sec;
38 unsigned long usec;
41 int open_tty_log(void *tty, void *current_tty)
43 struct timeval tv;
44 struct tty_log_buf data;
45 char buf[strlen(tty_log_dir) + sizeof("01234567890-01234567\0")];
46 int fd;
48 gettimeofday(&tv, NULL);
49 if(tty_log_fd != -1){
50 data = ((struct tty_log_buf) { .what = TTY_LOG_OPEN,
51 .tty = (unsigned long) tty,
52 .len = sizeof(current_tty),
53 .direction = 0,
54 .sec = tv.tv_sec,
55 .usec = tv.tv_usec } );
56 os_write_file(tty_log_fd, &data, sizeof(data));
57 os_write_file(tty_log_fd, &current_tty, data.len);
58 return(tty_log_fd);
61 sprintf(buf, "%s/%0u-%0u", tty_log_dir, (unsigned int) tv.tv_sec,
62 (unsigned int) tv.tv_usec);
64 fd = os_open_file(buf, of_append(of_create(of_rdwr(OPENFLAGS()))),
65 0644);
66 if(fd < 0){
67 printk("open_tty_log : couldn't open '%s', errno = %d\n",
68 buf, -fd);
70 return(fd);
73 void close_tty_log(int fd, void *tty)
75 struct tty_log_buf data;
76 struct timeval tv;
78 if(tty_log_fd != -1){
79 gettimeofday(&tv, NULL);
80 data = ((struct tty_log_buf) { .what = TTY_LOG_CLOSE,
81 .tty = (unsigned long) tty,
82 .len = 0,
83 .direction = 0,
84 .sec = tv.tv_sec,
85 .usec = tv.tv_usec } );
86 os_write_file(tty_log_fd, &data, sizeof(data));
87 return;
89 os_close_file(fd);
92 static int log_chunk(int fd, const char *buf, int len)
94 int total = 0, try, missed, n;
95 char chunk[64];
97 while(len > 0){
98 try = (len > sizeof(chunk)) ? sizeof(chunk) : len;
99 missed = copy_from_user_proc(chunk, (char *) buf, try);
100 try -= missed;
101 n = os_write_file(fd, chunk, try);
102 if(n != try) {
103 if(n < 0)
104 return(n);
105 return(-EIO);
107 if(missed != 0)
108 return(-EFAULT);
110 len -= try;
111 total += try;
112 buf += try;
115 return(total);
118 int write_tty_log(int fd, const char *buf, int len, void *tty, int is_read)
120 struct timeval tv;
121 struct tty_log_buf data;
122 int direction;
124 if(fd == tty_log_fd){
125 gettimeofday(&tv, NULL);
126 direction = is_read ? TTY_READ : TTY_WRITE;
127 data = ((struct tty_log_buf) { .what = TTY_LOG_WRITE,
128 .tty = (unsigned long) tty,
129 .len = len,
130 .direction = direction,
131 .sec = tv.tv_sec,
132 .usec = tv.tv_usec } );
133 os_write_file(tty_log_fd, &data, sizeof(data));
136 return(log_chunk(fd, buf, len));
139 void log_exec(char **argv, void *tty)
141 struct timeval tv;
142 struct tty_log_buf data;
143 char **ptr,*arg;
144 int len;
146 if(tty_log_fd == -1) return;
148 gettimeofday(&tv, NULL);
150 len = 0;
151 for(ptr = argv; ; ptr++){
152 if(copy_from_user_proc(&arg, ptr, sizeof(arg)))
153 return;
154 if(arg == NULL) break;
155 len += strlen_user_proc(arg);
158 data = ((struct tty_log_buf) { .what = TTY_LOG_EXEC,
159 .tty = (unsigned long) tty,
160 .len = len,
161 .direction = 0,
162 .sec = tv.tv_sec,
163 .usec = tv.tv_usec } );
164 os_write_file(tty_log_fd, &data, sizeof(data));
166 for(ptr = argv; ; ptr++){
167 if(copy_from_user_proc(&arg, ptr, sizeof(arg)))
168 return;
169 if(arg == NULL) break;
170 log_chunk(tty_log_fd, arg, strlen_user_proc(arg));
174 extern void register_tty_logger(int (*opener)(void *, void *),
175 int (*writer)(int, const char *, int,
176 void *, int),
177 void (*closer)(int, void *));
179 static int register_logger(void)
181 register_tty_logger(open_tty_log, write_tty_log, close_tty_log);
182 return(0);
185 __uml_initcall(register_logger);
187 static int __init set_tty_log_dir(char *name, int *add)
189 tty_log_dir = name;
190 return 0;
193 __uml_setup("tty_log_dir=", set_tty_log_dir,
194 "tty_log_dir=<directory>\n"
195 " This is used to specify the directory where the logs of all pty\n"
196 " data from this UML machine will be written.\n\n"
199 static int __init set_tty_log_fd(char *name, int *add)
201 char *end;
203 tty_log_fd = strtoul(name, &end, 0);
204 if((*end != '\0') || (end == name)){
205 printf("set_tty_log_fd - strtoul failed on '%s'\n", name);
206 tty_log_fd = -1;
209 *add = 0;
210 return 0;
213 __uml_setup("tty_log_fd=", set_tty_log_fd,
214 "tty_log_fd=<fd>\n"
215 " This is used to specify a preconfigured file descriptor to which all\n"
216 " tty data will be written. Preconfigure the descriptor with something\n"
217 " like '10>tty_log tty_log_fd=10'.\n\n"