Annotation of prex/usr/server/proc/tty.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 2007, Kohsuke Ohtani
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the author nor the names of any co-contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: /*
31: * tty.c - TTY signal support
32: */
33:
34: #include <prex/prex.h>
35: #include <server/proc.h>
36: #include <sys/list.h>
37:
38: #include <unistd.h>
39: #include <errno.h>
40: #include <stdlib.h>
41: #include <signal.h>
42: #include <termios.h>
43:
44: #include "proc.h"
45:
46: static device_t ttydev;
47:
48: /*
49: * Send TTY signal.
50: */
51: static void
52: tty_signal(int sig)
53: {
54: pid_t pid;
55:
56: /*
57: * Get the process group that was active when
58: * the TTY signal was invoked.
59: */
60: if (device_ioctl(ttydev, TIOCGPGRP, &pid) != 0)
61: return;
62:
63: DPRINTF(("proc: tty_signal sig=%d\n", sig));
64: kill_pg(pid, sig);
65: }
66:
67: /*
68: * Catch TTY related signals and forward them
69: * to the appropriate processes.
70: */
71: static void
72: exception_handler(int sig)
73: {
74:
75: /*
76: * Handle signals from tty input.
77: */
78: switch (sig) {
79: case SIGINT:
80: case SIGQUIT:
81: case SIGTSTP:
82: case SIGTTIN:
83: case SIGTTOU:
84: case SIGINFO:
85: case SIGWINCH:
86: case SIGIO:
87: if (ttydev != DEVICE_NULL)
88: tty_signal(sig);
89: break;
90: }
91: exception_return();
92: }
93:
94: /*
95: * Initialize TTY.
96: *
97: * Since we manage the process group only in the process
98: * server, the TTY driver can not know anything about the
99: * process group. However, POSIX specification requires TTY
100: * driver to send a signal to the specific process group.
101: * So, we will catch all TTY related signals by this server
102: * and forward them to the actual process or process group.
103: */
104: void
105: tty_init(void)
106: {
107: task_t self;
108:
109: /*
110: * Setup exception to receive signals from tty.
111: */
112: exception_setup(exception_handler);
113:
114: if (device_open("tty", 0, &ttydev) != 0)
115: ttydev = DEVICE_NULL;
116: else {
117: /*
118: * Notify the TTY driver to send all tty related
119: * signals in system to this task.
120: */
121: self = task_self();
122: device_ioctl(ttydev, TIOCSETSIGT, &self);
123: }
124: }
CVSweb