Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / usr / server / proc / proc_tty.c @ 03e9c04a

History | View | Annotate | Download (3.22 KB)

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 <sys/prex.h>
35
#include <ipc/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 pgid;
55

    
56
        /*
57
         * Get the process group that was active when
58
         * the TTY signal was invoked.
59
         */
60
        if (device_ioctl(ttydev, TIOCGPGRP, &pgid) != 0)
61
                return;
62

    
63
        DPRINTF(("proc: tty_signal pgid=%d sig=%d\n", pgid, sig));
64
        kill_pg(pgid, 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 != NODEV)
88
                        tty_signal(sig);
89
                break;
90
        }
91
        exception_return();
92
}
93

    
94

    
95
/*
96
 * Initialize TTY.
97
 *
98
 * Since we manage the process group only in the process
99
 * server, the TTY driver can not know anything about the
100
 * process group.  However, POSIX specification requires TTY
101
 * driver to send a signal to the specific process group.
102
 * So, we will catch all TTY related signals by this server
103
 * and forward them to the actual process or process group.
104
 */
105
void
106
tty_init(void)
107
{
108
        task_t self;
109

    
110
        /*
111
         * Setup exception to receive signals from tty.
112
         */
113
        exception_setup(exception_handler);
114

    
115
        if (device_open("tty", 0, &ttydev) != 0) {
116
                DPRINTF(("proc: no tty found\n"));
117
                ttydev = NODEV;
118
        } else {
119
                /*
120
                 * Notify the TTY driver to send all tty related
121
                 * signals in system to this task.
122
                 */
123
                self = task_self();
124
                device_ioctl(ttydev, TIOCSETSIGT, &self);
125
        }
126
}