NFSv4: Add post-op attributes to nfs4_proc_link()
[linux-2.6/mini2440.git] / arch / um / drivers / chan_user.c
blobde3bce71aeb3e8f3b3d283da35a77949a6444022
1 /*
2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <termios.h>
10 #include <string.h>
11 #include <signal.h>
12 #include <sys/stat.h>
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
15 #include "kern_util.h"
16 #include "user_util.h"
17 #include "chan_user.h"
18 #include "user.h"
19 #include "helper.h"
20 #include "os.h"
21 #include "choose-mode.h"
22 #include "mode.h"
24 int generic_console_write(int fd, const char *buf, int n, void *unused)
26 struct termios save, new;
27 int err;
29 if(isatty(fd)){
30 CATCH_EINTR(err = tcgetattr(fd, &save));
31 if (err)
32 goto error;
33 new = save;
34 /* The terminal becomes a bit less raw, to handle \n also as
35 * "Carriage Return", not only as "New Line". Otherwise, the new
36 * line won't start at the first column.*/
37 new.c_oflag |= OPOST;
38 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
39 if (err)
40 goto error;
42 err = generic_write(fd, buf, n, NULL);
43 /* Restore raw mode, in any case; we *must* ignore any error apart
44 * EINTR, except for debug.*/
45 if(isatty(fd))
46 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
47 return(err);
48 error:
49 return(-errno);
53 * UML SIGWINCH handling
55 * The point of this is to handle SIGWINCH on consoles which have host ttys and
56 * relay them inside UML to whatever might be running on the console and cares
57 * about the window size (since SIGWINCH notifies about terminal size changes).
59 * So, we have a separate thread for each host tty attached to a UML device
60 * (side-issue - I'm annoyed that one thread can't have multiple controlling
61 * ttys for purposed of handling SIGWINCH, but I imagine there are other reasons
62 * that doesn't make any sense).
64 * SIGWINCH can't be received synchronously, so you have to set up to receive it
65 * as a signal. That being the case, if you are going to wait for it, it is
66 * convenient to sit in sigsuspend() and wait for the signal to bounce you out of
67 * it (see below for how we make sure to exit only on SIGWINCH).
70 static void winch_handler(int sig)
74 struct winch_data {
75 int pty_fd;
76 int pipe_fd;
77 int close_me;
80 static int winch_thread(void *arg)
82 struct winch_data *data = arg;
83 sigset_t sigs;
84 int pty_fd, pipe_fd;
85 int count, err;
86 char c = 1;
88 os_close_file(data->close_me);
89 pty_fd = data->pty_fd;
90 pipe_fd = data->pipe_fd;
91 count = os_write_file(pipe_fd, &c, sizeof(c));
92 if(count != sizeof(c))
93 printk("winch_thread : failed to write synchronization "
94 "byte, err = %d\n", -count);
96 /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
97 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
98 * SIGWINCH. */
100 signal(SIGWINCH, winch_handler);
101 sigfillset(&sigs);
102 /* Block all signals possible. */
103 if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
104 printk("winch_thread : sigprocmask failed, errno = %d\n",
105 errno);
106 exit(1);
108 /* In sigsuspend(), block anything else than SIGWINCH. */
109 sigdelset(&sigs, SIGWINCH);
111 if(setsid() < 0){
112 printk("winch_thread : setsid failed, errno = %d\n", errno);
113 exit(1);
116 err = os_new_tty_pgrp(pty_fd, os_getpid());
117 if(err < 0){
118 printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err);
119 exit(1);
122 /* These are synchronization calls between various UML threads on the
123 * host - since they are not different kernel threads, we cannot use
124 * kernel semaphores. We don't use SysV semaphores because they are
125 * persistant. */
126 count = os_read_file(pipe_fd, &c, sizeof(c));
127 if(count != sizeof(c))
128 printk("winch_thread : failed to read synchronization byte, "
129 "err = %d\n", -count);
131 while(1){
132 /* This will be interrupted by SIGWINCH only, since other signals
133 * are blocked.*/
134 sigsuspend(&sigs);
136 count = os_write_file(pipe_fd, &c, sizeof(c));
137 if(count != sizeof(c))
138 printk("winch_thread : write failed, err = %d\n",
139 -count);
143 static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out)
145 struct winch_data data;
146 unsigned long stack;
147 int fds[2], n, err;
148 char c;
150 err = os_pipe(fds, 1, 1);
151 if(err < 0){
152 printk("winch_tramp : os_pipe failed, err = %d\n", -err);
153 goto out;
156 data = ((struct winch_data) { .pty_fd = fd,
157 .pipe_fd = fds[1],
158 .close_me = fds[0] } );
159 err = run_helper_thread(winch_thread, &data, 0, &stack, 0);
160 if(err < 0){
161 printk("fork of winch_thread failed - errno = %d\n", errno);
162 goto out_close;
165 os_close_file(fds[1]);
166 *fd_out = fds[0];
167 n = os_read_file(fds[0], &c, sizeof(c));
168 if(n != sizeof(c)){
169 printk("winch_tramp : failed to read synchronization byte\n");
170 printk("read failed, err = %d\n", -n);
171 printk("fd %d will not support SIGWINCH\n", fd);
172 err = -EINVAL;
173 goto out_close1;
175 return err ;
177 out_close:
178 os_close_file(fds[1]);
179 out_close1:
180 os_close_file(fds[0]);
181 out:
182 return err;
185 void register_winch(int fd, struct tty_struct *tty)
187 int pid, thread, thread_fd = -1;
188 int count;
189 char c = 1;
191 if(!isatty(fd))
192 return;
194 pid = tcgetpgrp(fd);
195 if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
196 tty) && (pid == -1)){
197 thread = winch_tramp(fd, tty, &thread_fd);
198 if(thread > 0){
199 register_winch_irq(thread_fd, fd, thread, tty);
201 count = os_write_file(thread_fd, &c, sizeof(c));
202 if(count != sizeof(c))
203 printk("register_winch : failed to write "
204 "synchronization byte, err = %d\n",
205 -count);
211 * Overrides for Emacs so that we follow Linus's tabbing style.
212 * Emacs will notice this stuff at the end of the file and automatically
213 * adjust the settings for this buffer only. This must remain at the end
214 * of the file.
215 * ---------------------------------------------------------------------------
216 * Local variables:
217 * c-file-style: "linux"
218 * End: