-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_proc.c
More file actions
191 lines (172 loc) · 4.22 KB
/
script_proc.c
File metadata and controls
191 lines (172 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Basic implementation of script(1).
* Runs $SHELL under a pseudoterminal slave, the master then outputs
* all input (where echoed) and output to the specified file.
* This updated version uses cat and tee programs, instead of
* handling IO redirection ourselves via select.
*/
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#define BUFSZ 100
static struct termios tios_restore;
static volatile sig_atomic_t winch;
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
static void restore_tios() {
tcsetattr(STDOUT_FILENO, TCSADRAIN, &tios_restore);
}
static void run_child(char *sname) {
int slave;
if (setsid() == (pid_t)-1)
exit_err("setsid");
if ((slave = open(sname, O_RDWR)) == -1)
exit_err("open pt slave");
if (ioctl(slave, TIOCSCTTY) == -1)
exit_err("TIOCSTTY");
if (dup2(slave, STDIN_FILENO) == -1)
exit_err("dup2 STDIN");
if (dup2(slave, STDOUT_FILENO) == -1)
exit_err("dup2 STDOUT");
if (dup2(slave, STDERR_FILENO) == -1) {
puts("dup2 STDERR");
exit_err("dup2 STDERR");
}
close(slave);
execl("/bin/bash", "bash", (char*)NULL);
exit_err("exec");
}
static int log_time(int ofile, int finished) {
time_t t;
char *str, *intro;
size_t len;
intro = finished ? "Script finished on: " : "Script started on: ";
if (time(&t) == (time_t)-1)
return -1;
if ((str = ctime(&t)) == NULL)
return -1;
len = strlen(intro);
if (write(ofile, intro, len) != (ssize_t)len)
return -1;
len = strlen(str);
if (write(ofile, str, len) != (ssize_t)len)
return -1;
}
static void handle(int sig) {
winch = 1;
}
static int setup_winch() {
struct sigaction sigact;
memset(&sigact, 0, sizeof (sigact));
sigact.sa_handler = handle;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
if (sigaction(SIGWINCH, &sigact, NULL) == -1)
return -1;
}
static int winchpt(int master) {
struct winsize sz;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &sz) == -1)
return -1;
if (ioctl(master, TIOCSWINSZ, &sz) == -1)
return -1;
}
int main(int argc, char **argv) {
int master, ofile, result, wstat;
char *sname, buf[BUFSZ];
struct pollfd fds[2];
struct termios raw;
ssize_t nread;
pid_t tee, cat, wpid;
if (argc < 2)
exit_err("usage: ./script <output_file>");
if (setup_winch() == -1)
exit_err("setup sigwinch");
if ((ofile = open(argv[1], O_WRONLY | O_CREAT | O_APPEND, S_IRWXU)) == -1)
exit_err("failed to open output file");
if ((master = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
exit_err("posix_openpt");
if (grantpt(master) == -1)
exit_err("grantpt");
if ((sname = ptsname(master)) == NULL)
exit_err("ptsname");
if (unlockpt(master) == -1)
exit_err("unlockpt");
switch(fork()) {
case -1:
exit_err("fork");
case 0:
run_child(sname);
default:
break;
}
if (tcgetattr(STDOUT_FILENO, &tios_restore) == -1)
exit_err("tcgetattr");
if (atexit(restore_tios) != 0)
exit_err("atexit");
raw = tios_restore;
raw.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
raw.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |
INPCK | ISTRIP | IXON | PARMRK);
raw.c_oflag &= ~OPOST;
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
if (tcsetattr(STDOUT_FILENO, TCSADRAIN, &raw) == -1)
exit_err("tcsetattr raw");
log_time(ofile, 0);
fsync(ofile);
switch ((tee = fork())) {
case -1:
exit_err("fork");
case 0:
dup2(master, STDIN_FILENO);
execl("/bin/tee", "tee", "-a", argv[1], (char*)NULL);
exit_err("tee");
default:
break;
}
switch ((cat = fork())) {
case -1:
exit_err("cat");
case 0:
dup2(master, STDOUT_FILENO);
execl("/bin/cat", "cat", (char*)NULL);
exit_err("cat");
default:
break;
}
// Monitor our child procs
for (;;) {
if ((wpid = wait(&wstat)) == -1) {
switch (errno) {
case ECHILD:
fsync(ofile);
log_time(ofile, 1);
exit(EXIT_SUCCESS);
case EINTR:
continue;
default:
kill(cat, SIGINT);
kill(tee, SIGINT);
exit_err("wait");
}
// Tee should be the first to quit, but just in case.
} else if (wpid == cat) {
kill(tee, SIGINT);
} else if (wpid == tee) {
kill(cat, SIGINT);
}
}
}