blob: fc20759418b7610e6328351b1ba860d788a2c3dc [file] [log] [blame]
Scott James Remnant8092e792008-04-29 23:36:16 +01001/* upstart
2 *
3 * system.c - core system functions
4 *
Scott James Remnant75123022009-05-22 13:27:56 +02005 * Copyright © 2009 Canonical Ltd.
Scott James Remnant7d5b2ea2009-05-22 15:20:12 +02006 * Author: Scott James Remnant <scott@netsplit.com>.
Scott James Remnant8092e792008-04-29 23:36:16 +01007 *
Scott James Remnant75123022009-05-22 13:27:56 +02008 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3, as
10 * published by the Free Software Foundation.
Scott James Remnant8092e792008-04-29 23:36:16 +010011 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
Scott James Remnant75123022009-05-22 13:27:56 +020018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Scott James Remnant8092e792008-04-29 23:36:16 +010019 */
Scott James Remnant75123022009-05-22 13:27:56 +020020 *
Scott James Remnant8092e792008-04-29 23:36:16 +010021
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif /* HAVE_CONFIG_H */
25
26#include <sys/types.h>
27#include <sys/ioctl.h>
28#include <sys/stat.h>
29
30#include <fcntl.h>
31#include <signal.h>
32#include <unistd.h>
33#include <termios.h>
34
35#include <nih/macros.h>
36#include <nih/error.h>
37#include <nih/logging.h>
38
39#include "paths.h"
40#include "system.h"
41#include "job_class.h"
42
43
44/**
45 * system_kill:
46 * @pid: process id of process,
47 * @force: force the death.
48 *
49 * Kill all processes in the same process group as @pid, which may not
50 * necessarily be the group leader.
51 *
52 * When @force is FALSE, the TERM signal is sent; when it is TRUE, KILL
53 * is sent instead.
54 *
55 * Returns: zero on success, negative value on raised error.
56 **/
57int
58system_kill (pid_t pid,
59 int force)
60{
61 int signal;
62 pid_t pgid;
63
64 nih_assert (pid > 0);
65
66 signal = (force ? SIGKILL : SIGTERM);
67
68 pgid = getpgid (pid);
69
70 if (kill (pgid > 0 ? -pgid : pid, signal) < 0)
71 nih_return_system_error (-1);
72
73 return 0;
74}
75
76
77/**
78 * system_setup_console:
79 * @type: console type,
80 * @reset: reset console to sane defaults.
81 *
82 * Set up the standard input, output and error file descriptors for the
83 * current process based on the console @type given. If @reset is TRUE then
84 * the console device will be reset to sane defaults.
85 *
86 * Returns: zero on success, negative value on raised error.
87 **/
88int
89system_setup_console (ConsoleType type,
90 int reset)
91{
92 int fd = -1, i;
93
94 /* Close the standard file descriptors since we're about to re-open
95 * them; it may be that some of these aren't already open, we get
96 * called in some very strange ways.
97 */
98 for (i = 0; i < 3; i++)
99 close (i);
100
101 /* Open the new first file descriptor, which should always become
102 * file zero.
103 */
104 switch (type) {
105 case CONSOLE_OUTPUT:
106 case CONSOLE_OWNER:
107 /* Ordinary console input and output */
108 fd = open (CONSOLE, O_RDWR | O_NOCTTY);
109 if (fd < 0)
110 nih_return_system_error (-1);
111
112 if (type == CONSOLE_OWNER)
113 ioctl (fd, TIOCSCTTY, 1);
114 break;
115 case CONSOLE_NONE:
116 /* No console really means /dev/null */
117 fd = open (DEV_NULL, O_RDWR | O_NOCTTY);
118 if (fd < 0)
119 nih_return_system_error (-1);
120 break;
121 }
122
123 /* Reset to sane defaults, cribbed from sysvinit, initng, etc. */
124 if (reset) {
125 struct termios tty;
126
127 tcgetattr (0, &tty);
128
129 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB
130 | PARENB | PARODD);
131 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
132
133 /* Set up usual keys */
134 tty.c_cc[VINTR] = 3; /* ^C */
135 tty.c_cc[VQUIT] = 28; /* ^\ */
136 tty.c_cc[VERASE] = 127;
137 tty.c_cc[VKILL] = 24; /* ^X */
138 tty.c_cc[VEOF] = 4; /* ^D */
139 tty.c_cc[VTIME] = 0;
140 tty.c_cc[VMIN] = 1;
141 tty.c_cc[VSTART] = 17; /* ^Q */
142 tty.c_cc[VSTOP] = 19; /* ^S */
143 tty.c_cc[VSUSP] = 26; /* ^Z */
144
145 /* Pre and post processing */
146 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
147 tty.c_oflag = (OPOST | ONLCR);
148 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL
149 | ECHOPRT | ECHOKE);
150
151 /* Set the terminal line and flush it */
152 tcsetattr (0, TCSANOW, &tty);
153 tcflush (0, TCIOFLUSH);
154 }
155
156 /* Copy to standard output and standard error */
157 while (dup (fd) < 2)
158 ;
159
160 return 0;
161}