-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpseudo_classic.cpp
More file actions
172 lines (147 loc) · 3 KB
/
Copy pathpseudo_classic.cpp
File metadata and controls
172 lines (147 loc) · 3 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
// Copyright Timothy Miller, 1999
#include "pseudo_classic.hpp"
#include <stdlib.h>
#include <unistd.h>
#ifndef __FreeBSD__
#include <stropts.h>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>
#ifdef USE_UTMP
#include <utmp.h>
#include <time.h>
#include <pwd.h>
#endif
# include <grp.h>
PseudoTerminal::PseudoTerminal():master(-1),slave(-1)
{
ptsname = new char[12];
}
PseudoTerminal::~PseudoTerminal()
{
done();
delete ptsname;
}
void PseudoTerminal::done()
{
if(master!=-1)
close(master);
if(slave!=-1)
close(slave);
if(master!=-1 || slave!=-1) {
#ifdef USE_UTMP
remove_utmp();
#endif
chown(ptsname, tty_stat.st_uid, tty_stat.st_gid);
chmod(ptsname, tty_stat.st_mode);
}
master = -1;
slave = -1;
}
bool PseudoTerminal::init()
{
/* init only once */
if (master!=-1 || slave!=-1)
return false;
int i;
const char *ptr;
struct stat statbuf;
const char ptychar[] = "pqrstuvwxyzPQRST";
const char hexdigit[] = "0123456789abcdef";
struct group *gptr; // for tty group look up
int gid; // for tty group id
for (ptr = ptychar; *ptr; ptr++) {
strcpy(ptsname, "/dev/ptyXY");
ptsname[8] = *ptr;
ptsname[9] = '0';
if (stat(ptsname, &statbuf) < 0) break;
for (i=0; i<16; i++) {
ptsname[9] = hexdigit[i];
master = open(ptsname, O_RDWR);
if (master >= 0)
goto master_ok;
}
}
goto finish_error;
master_ok:;
ptsname[5] = 't';
if ((gptr = getgrnam("tty")) != 0) {
gid = gptr->gr_gid;
} else {
gid = -1;
}
slave = open(ptsname, O_RDWR);
if(slave<0)
goto close_master;
if(chown(ptsname, getuid(), gid)<0 ||
chmod(ptsname, S_IRUSR|S_IWUSR|S_IWGRP)<0)
goto close_slave;
return true;
close_slave:
close(slave);
close_master:
close(master);
finish_error:
master = -1;
slave = -1;
return false;
}
bool PseudoTerminal::spawn(char *exe)
{
int pid;
pid = fork();
if (pid<0)
return false;
if (!pid)
{
close(master);
setuid(getuid());
setgid(getgid());
if (setsid()<0)
fprintf(stderr, "Could not set session leader\n");
if (ioctl(slave, TIOCSCTTY, NULL))
fprintf(stderr, "Could not set controlling tty\n");
dup2(slave, 0);
dup2(slave, 1);
dup2(slave, 2);
if (slave>2)
close(slave);
execl(exe, exe, NULL);
exit(0);
}
close(slave); // not necessary any more, comment out?
slave = -1;
this->pid = pid;
#ifdef USE_UTMP
add_utmp();
#endif
return true;
}
#ifdef USE_UTMP
void PseudoTerminal::add_utmp()
{
ut_entry.ut_type = USER_PROCESS;
ut_entry.ut_pid = pid;
strcpy(ut_entry.ut_line, ptsname+5);
memset(ut_entry.ut_id,' ',4);
time(&ut_entry.ut_time);
strcpy(ut_entry.ut_user, getpwuid(getuid())->pw_name);
strcpy(ut_entry.ut_host, getenv("DISPLAY"));
ut_entry.ut_addr = 0;
setutent();
pututline(&ut_entry);
endutent();
}
void PseudoTerminal::remove_utmp()
{
ut_entry.ut_type = DEAD_PROCESS;
memset(ut_entry.ut_line, 0, UT_LINESIZE);
ut_entry.ut_time = 0;
memset(ut_entry.ut_user, 0, UT_NAMESIZE);
setutent();
pututline(&ut_entry);
endutent();
}
#endif /* USE_UTMP */