Diffstat (limited to 'core/apps/embeddedkonsole/MyPty.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/apps/embeddedkonsole/MyPty.cpp | 99 |
1 files changed, 50 insertions, 49 deletions
diff --git a/core/apps/embeddedkonsole/MyPty.cpp b/core/apps/embeddedkonsole/MyPty.cpp index 3622d48..6421ab0 100644 --- a/core/apps/embeddedkonsole/MyPty.cpp +++ b/core/apps/embeddedkonsole/MyPty.cpp | |||
@@ -1,27 +1,27 @@ | |||
1 | /* -------------------------------------------------------------------------- */ | 1 | /* -------------------------------------------------------------------------- */ |
2 | /* */ | 2 | /* */ |
3 | /* [MyPty.C] Pseudo Terminal Device */ | 3 | /* [MyPty.C] Pseudo Terminal Device */ |
4 | /* */ | 4 | /* */ |
5 | /* -------------------------------------------------------------------------- */ | 5 | /* -------------------------------------------------------------------------- */ |
6 | /* */ | 6 | /* */ |
7 | /* Copyright (c) 1997,1998 by Lars Doelle <lars.doelle@on-line.de> */ | 7 | /* Copyright (c) 1997,1998 by Lars Doelle <lars.doelle@on-line.de> */ |
8 | /* */ | 8 | /* */ |
9 | /* This file is part of Konsole - an X terminal for KDE */ | 9 | /* This file is part of Konsole - an X terminal for KDE */ |
10 | /* -------------------------------------------------------------------------- */ | 10 | /* -------------------------------------------------------------------------- */ |
11 | /* */ | 11 | /* */ |
12 | /* Ported Konsole to Qt/Embedded */ | 12 | /* Ported Konsole to Qt/Embedded */ |
13 | /* */ | 13 | /* */ |
14 | /* Copyright (C) 2000 by John Ryland <jryland@trolltech.com> */ | 14 | /* Copyright (C) 2000 by John Ryland <jryland@trolltech.com> */ |
15 | /* */ | 15 | /* */ |
16 | /* -------------------------------------------------------------------------- */ | 16 | /* -------------------------------------------------------------------------- */ |
17 | 17 | ||
18 | /* If you're compiling konsole on non-Linux platforms and find | 18 | /* If you're compiling konsole on non-Linux platforms and find |
19 | problems that you can track down to this file, please have | 19 | problems that you can track down to this file, please have |
20 | a look into ../README.ports, too. | 20 | a look into ../README.ports, too. |
21 | */ | 21 | */ |
22 | 22 | ||
23 | /*! \file | 23 | /*! \file |
24 | */ | 24 | */ |
25 | 25 | ||
26 | /*! \class TEPty | 26 | /*! \class TEPty |
27 | 27 | ||
@@ -104,124 +104,125 @@ void MyPty::setSize(int lines, int columns) | |||
104 | ioctl(fd,TIOCSWINSZ,(char *)&wsize); | 104 | ioctl(fd,TIOCSWINSZ,(char *)&wsize); |
105 | } | 105 | } |
106 | 106 | ||
107 | 107 | ||
108 | void MyPty::donePty() | 108 | void MyPty::donePty() |
109 | { | 109 | { |
110 | // This is code from the Qt DumbTerminal example | 110 | // This is code from the Qt DumbTerminal example |
111 | int status = 0; | 111 | int status = 0; |
112 | 112 | ||
113 | ::close(fd); | 113 | ::close(fd); |
114 | 114 | ||
115 | if (cpid) { | 115 | if (cpid) { |
116 | kill(cpid, SIGHUP); | 116 | kill(cpid, SIGHUP); |
117 | waitpid(cpid, &status, 0); | 117 | waitpid(cpid, &status, 0); |
118 | } | 118 | } |
119 | 119 | ||
120 | emit done(status); | 120 | emit done(status); |
121 | } | 121 | } |
122 | 122 | ||
123 | 123 | ||
124 | const char* MyPty::deviceName() | 124 | const char* MyPty::deviceName() |
125 | { | 125 | { |
126 | return ttynam; | 126 | return ttynam; |
127 | } | 127 | } |
128 | 128 | ||
129 | 129 | ||
130 | void MyPty::error() | 130 | void MyPty::error() |
131 | { | 131 | { |
132 | // This is code from the Qt DumbTerminal example | 132 | // This is code from the Qt DumbTerminal example |
133 | donePty(); | 133 | donePty(); |
134 | } | 134 | } |
135 | 135 | ||
136 | 136 | ||
137 | /*! | 137 | /*! |
138 | start the client program. | 138 | start the client program. |
139 | */ | 139 | */ |
140 | int MyPty::run(const char* cmd, QStrList &, const char*, int) | 140 | int MyPty::run(const char* cmd, QStrList &, const char*, int) |
141 | { | 141 | { |
142 | // This is code from the Qt DumbTerminal example | 142 | // This is code from the Qt DumbTerminal example |
143 | cpid = fork(); | 143 | cpid = fork(); |
144 | 144 | ||
145 | if ( !cpid ) { | 145 | if ( !cpid ) { |
146 | // child - exec shell on tty | 146 | // child - exec shell on tty |
147 | for (int sig = 1; sig < NSIG; sig++) signal(sig,SIG_DFL); | 147 | for (int sig = 1; sig < NSIG; sig++) signal(sig,SIG_DFL); |
148 | int ttyfd = open(ttynam, O_RDWR); | 148 | int ttyfd = open(ttynam, O_RDWR); |
149 | dup2(ttyfd, STDIN_FILENO); | 149 | dup2(ttyfd, STDIN_FILENO); |
150 | dup2(ttyfd, STDOUT_FILENO); | 150 | dup2(ttyfd, STDOUT_FILENO); |
151 | dup2(ttyfd, STDERR_FILENO); | 151 | dup2(ttyfd, STDERR_FILENO); |
152 | // should be done with tty, so close it | 152 | // should be done with tty, so close it |
153 | close(ttyfd); | 153 | close(ttyfd); |
154 | static struct termios ttmode; | 154 | static struct termios ttmode; |
155 | if ( setsid() < 0 ) | 155 | if ( setsid() < 0 ) |
156 | perror( "failed to set process group" ); | 156 | perror( "failed to set process group" ); |
157 | #if defined (TIOCSCTTY) | 157 | #if defined (TIOCSCTTY) |
158 | // grabbed from APUE by Stevens | 158 | // grabbed from APUE by Stevens |
159 | ioctl(STDIN_FILENO, TIOCSCTTY, 0); | 159 | ioctl(STDIN_FILENO, TIOCSCTTY, 0); |
160 | #endif | 160 | #endif |
161 | tcgetattr( STDIN_FILENO, &ttmode ); | 161 | tcgetattr( STDIN_FILENO, &ttmode ); |
162 | ttmode.c_cc[VINTR] = 3; | 162 | ttmode.c_cc[VINTR] = 3; |
163 | ttmode.c_cc[VERASE] = 8; | 163 | ttmode.c_cc[VERASE] = 8; |
164 | tcsetattr( STDIN_FILENO, TCSANOW, &ttmode ); | 164 | tcsetattr( STDIN_FILENO, TCSANOW, &ttmode ); |
165 | setenv("TERM","vt100",1); | 165 | setenv("TERM","vt100",1); |
166 | setenv("COLORTERM","0",1); | 166 | setenv("COLORTERM","0",1); |
167 | 167 | ||
168 | if (getuid() == 0) { | 168 | if (getuid() == 0) { |
169 | char msg[] = "WARNING: You are running this shell as root!\n"; | 169 | char msg[] = "WARNING: You are running this shell as root!\n"; |
170 | write(ttyfd, msg, sizeof(msg)); | 170 | write(ttyfd, msg, sizeof(msg)); |
171 | } | 171 | } |
172 | execl(cmd, cmd, 0); | 172 | ; //creates a login shell |
173 | 173 | execl(cmd, cmd, "--login", 0); | |
174 | donePty(); | 174 | |
175 | exit(-1); | 175 | donePty(); |
176 | exit(-1); | ||
176 | } | 177 | } |
177 | 178 | ||
178 | // parent - continue as a widget | 179 | // parent - continue as a widget |
179 | QSocketNotifier* sn_r = new QSocketNotifier(fd,QSocketNotifier::Read,this); | 180 | QSocketNotifier* sn_r = new QSocketNotifier(fd,QSocketNotifier::Read,this); |
180 | QSocketNotifier* sn_e = new QSocketNotifier(fd,QSocketNotifier::Exception,this); | 181 | QSocketNotifier* sn_e = new QSocketNotifier(fd,QSocketNotifier::Exception,this); |
181 | connect(sn_r,SIGNAL(activated(int)),this,SLOT(readPty())); | 182 | connect(sn_r,SIGNAL(activated(int)),this,SLOT(readPty())); |
182 | connect(sn_e,SIGNAL(activated(int)),this,SLOT(error())); | 183 | connect(sn_e,SIGNAL(activated(int)),this,SLOT(error())); |
183 | 184 | ||
184 | return 0; | 185 | return 0; |
185 | } | 186 | } |
186 | 187 | ||
187 | int MyPty::openPty() | 188 | int MyPty::openPty() |
188 | { | 189 | { |
189 | // This is code from the Qt DumbTerminal example | 190 | // This is code from the Qt DumbTerminal example |
190 | int ptyfd = -1; | 191 | int ptyfd = -1; |
191 | 192 | ||
192 | #ifdef HAVE_OPENPTY | 193 | #ifdef HAVE_OPENPTY |
193 | int ttyfd; | 194 | int ttyfd; |
194 | if ( openpty(&ptyfd,&ttyfd,ttynam,0,0) ) | 195 | if ( openpty(&ptyfd,&ttyfd,ttynam,0,0) ) |
195 | ptyfd = -1; | 196 | ptyfd = -1; |
196 | else | 197 | else |
197 | close(ttyfd); // we open the ttynam ourselves. | 198 | close(ttyfd); // we open the ttynam ourselves. |
198 | #else | 199 | #else |
199 | for (const char* c0 = "pqrstuvwxyzabcde"; ptyfd < 0 && *c0 != 0; c0++) { | 200 | for (const char* c0 = "pqrstuvwxyzabcde"; ptyfd < 0 && *c0 != 0; c0++) { |
200 | for (const char* c1 = "0123456789abcdef"; ptyfd < 0 && *c1 != 0; c1++) { | 201 | for (const char* c1 = "0123456789abcdef"; ptyfd < 0 && *c1 != 0; c1++) { |
201 | sprintf(ptynam,"/dev/pty%c%c",*c0,*c1); | 202 | sprintf(ptynam,"/dev/pty%c%c",*c0,*c1); |
202 | sprintf(ttynam,"/dev/tty%c%c",*c0,*c1); | 203 | sprintf(ttynam,"/dev/tty%c%c",*c0,*c1); |
203 | if ((ptyfd = ::open(ptynam,O_RDWR)) >= 0) { | 204 | if ((ptyfd = ::open(ptynam,O_RDWR)) >= 0) { |
204 | if (geteuid() != 0 && !access(ttynam,R_OK|W_OK) == 0) { | 205 | if (geteuid() != 0 && !access(ttynam,R_OK|W_OK) == 0) { |
205 | ::close(ptyfd); | 206 | ::close(ptyfd); |
206 | ptyfd = -1; | 207 | ptyfd = -1; |
207 | } | 208 | } |
208 | } | 209 | } |
209 | } | 210 | } |
210 | } | 211 | } |
211 | #endif | 212 | #endif |
212 | 213 | ||
213 | if ( ptyfd < 0 ) { | 214 | if ( ptyfd < 0 ) { |
214 | qApp->exit(1); | 215 | qApp->exit(1); |
215 | return -1; | 216 | return -1; |
216 | } | 217 | } |
217 | 218 | ||
218 | return ptyfd; | 219 | return ptyfd; |
219 | } | 220 | } |
220 | 221 | ||
221 | /*! | 222 | /*! |
222 | Create an instance. | 223 | Create an instance. |
223 | */ | 224 | */ |
224 | MyPty::MyPty() : cpid(0) | 225 | MyPty::MyPty() : cpid(0) |
225 | { | 226 | { |
226 | fd = openPty(); | 227 | fd = openPty(); |
227 | } | 228 | } |
@@ -240,25 +241,25 @@ MyPty::~MyPty() | |||
240 | /*! sends len bytes through the line */ | 241 | /*! sends len bytes through the line */ |
241 | void MyPty::send_bytes(const char* s, int len) | 242 | void MyPty::send_bytes(const char* s, int len) |
242 | { | 243 | { |
243 | 244 | ||
244 | #ifdef VERBOSE_DEBUG | 245 | #ifdef VERBOSE_DEBUG |
245 | // verbose debug | 246 | // verbose debug |
246 | printf("sending bytes:\n"); | 247 | printf("sending bytes:\n"); |
247 | for (int i = 0; i < len; i++) | 248 | for (int i = 0; i < len; i++) |
248 | printf("%c", s[i]); | 249 | printf("%c", s[i]); |
249 | printf("\n"); | 250 | printf("\n"); |
250 | #endif | 251 | #endif |
251 | 252 | ||
252 | ::write(fd, s, len); | 253 | ::write(fd, s, len); |
253 | } | 254 | } |
254 | 255 | ||
255 | /*! indicates that a block of data is received */ | 256 | /*! indicates that a block of data is received */ |
256 | void MyPty::readPty() | 257 | void MyPty::readPty() |
257 | { | 258 | { |
258 | char buf[4096]; | 259 | char buf[4096]; |
259 | 260 | ||
260 | int len = ::read( fd, buf, 4096 ); | 261 | int len = ::read( fd, buf, 4096 ); |
261 | 262 | ||
262 | if (len == -1) | 263 | if (len == -1) |
263 | donePty(); | 264 | donePty(); |
264 | 265 | ||