Skip to content

Commit d9a4f87

Browse files
author
David T Lewis
committed
Let ptyForkAndExec return the pid of the child process used for tty_login
such that primitives can inform the image of the child pid. This is to support coordination of SIGCHLD handling between PseudoTTYPlugin and OSProcess. Also let "declining responsibility for child processes" be a debug message rather than stderr message in anticipation of better OSProcess integration. This update based on the more recent version in squeakvm.org SVN with cosmetic updates from original author (to keep code bases in sync).
1 parent 8508f9f commit d9a4f87

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

platforms/unix/plugins/PseudoTTYPlugin/sqUnixPseudoTTYPlugin.c

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* Author: Ian Piumarta <[email protected]>
44
* Version: 1.1
5-
* Last edited: 2003-09-03 18:06:13 by piumarta on emilia.inria.fr
5+
* Last edited: 2009-08-19 04:25:56 by piumarta on emilia-2.local
66
*
77
* This plugin extends AsynchFilePlugin with support for Unix98-style
88
* pseudo ttys. See the PseudoTTY and PseudoTTYPlugin class comments
@@ -31,12 +31,12 @@
3131
*
3232
* This file is part of Unix Squeak.
3333
*
34-
* Permission is hereby granted, free of charge, to any person obtaining a
35-
* copy of this software and associated documentation files (the "Software"),
36-
* to deal in the Software without restriction, including without limitation
37-
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
38-
* and/or sell copies of the Software, and to permit persons to whom the
39-
* Software is furnished to do so, subject to the following conditions:
34+
* Permission is hereby granted, free of charge, to any person obtaining a copy
35+
* of this software and associated documentation files (the "Software"), to deal
36+
* in the Software without restriction, including without limitation the rights
37+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38+
* copies of the Software, and to permit persons to whom the Software is
39+
* furnished to do so, subject to the following conditions:
4040
*
4141
* The above copyright notice and this permission notice shall be included in
4242
* all copies or substantial portions of the Software.
@@ -45,11 +45,12 @@
4545
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4646
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4747
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49-
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50-
* DEALINGS IN THE SOFTWARE.
48+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50+
* SOFTWARE.
5151
*/
5252

53+
#include "config.h"
5354
#include "sq.h"
5455
#include "PseudoTTYPlugin.h"
5556

@@ -69,9 +70,9 @@
6970
#include "openpty.h" /* hide the gory details ;) */
7071

7172
#if 0
72-
# define DPRINTF(ARGS) printf ARGS
73+
# define debugf(ARGS) printf ARGS
7374
#else
74-
# define DPRINTF(ARGS)
75+
# define debugf(ARGS)
7576
#endif
7677

7778

@@ -123,20 +124,20 @@ static void sigchld(int signum)
123124
/* close(zombie->pty->fd); */
124125
zombie->pty->rd.status= -2;
125126
signalSemaphoreWithIndex(zombie->pty->sema);
126-
DPRINTF(("closed pty for pid %d\n", pid));
127+
debugf(("closed pty for pid %d\n", pid));
127128
}
128129
}
129130

130131

131132
int ptyInit(void)
132133
{
133-
DPRINTF(("ptyInit: AsyncFileSession is %d\n", sqUnixAsyncFileSessionID));
134+
debugf(("ptyInit: AsyncFileSession is %d\n", sqUnixAsyncFileSessionID));
134135
vm= sqGetInterpreterProxy();
135136
slaves= 0;
136137
prevchld= signal(SIGCHLD, sigchld);
137138
if ((prevchld != SIG_DFL) && (prevchld != SIG_IGN))
138139
{
139-
fprintf(stderr, "declining responsibility for child processes!\n");
140+
debugf(("ptyInit: declining responsibility for child processes!\n"));
140141
signal(SIGCHLD, prevchld);
141142
reaping= 0;
142143
}
@@ -178,30 +179,30 @@ int ptyShutdown(void)
178179
#include <time.h>
179180

180181

181-
int ptyForkAndExec(AsyncFile *f, int semaIndex,
182+
pid_t ptyForkAndExec(AsyncFile *f, int semaIndex,
182183
char *cmdIndex, int cmdLen, sqInt *argIndex, int argLen)
183184
{
184185
int ptm= -1, pts= -1;
185186
char tty[32];
186187
FilePtr fp= 0;
187188

188-
DPRINTF(("ptyForkAndExec\n"));
189+
debugf(("ptyForkAndExec\n"));
189190

190191
/* Module init must succeed in loading the AsyncFile plugin */
191192
if (sqUnixAsyncFileSessionID == 0)
192193
{
193194
vm->primitiveFail();
194-
return 0;
195+
return -1;
195196
}
196197

197-
DPRINTF(("AsyncFileSession is %d\n", sqUnixAsyncFileSessionID));
198+
debugf(("AsyncFileSession is %d\n", sqUnixAsyncFileSessionID));
198199

199200
if (openpty(&ptm, &pts, tty, 0, 0) == -1)
200201
{
201202
perror("pty: openpty");
202203
goto failDetached;
203204
}
204-
DPRINTF(("pty: using %s (ptm %d pts %d)\n", tty, ptm, pts));
205+
debugf(("pty: using %s (ptm %d pts %d)\n", tty, ptm, pts));
205206

206207
if ((fp= asyncFileAttach(f, ptm, semaIndex)) == 0)
207208
goto failDetached;
@@ -213,9 +214,11 @@ int ptyForkAndExec(AsyncFile *f, int semaIndex,
213214
char **argv= (char **)alloca(sizeof(char *) * (argLen + 2));
214215
int i= 0;
215216
SlavePtr slave= 0;
217+
pid_t childPid;
218+
216219
memcpy((void *)cmd, cmdIndex, cmdLen);
217220
cmd[cmdLen]= '\0';
218-
DPRINTF(("pty: command: %s\n", cmd));
221+
debugf(("pty: command: %s\n", cmd));
219222
argv[0]= cmd;
220223
for (i= 1; i <= argLen; ++i)
221224
{
@@ -224,12 +227,12 @@ int ptyForkAndExec(AsyncFile *f, int semaIndex,
224227
int len= 0;
225228
if (!vm->isBytes(argOop)) goto fail;
226229
len= vm->stSizeOf(argOop);
227-
DPRINTF(("pty: arg %d len %d\n", i, len));
230+
debugf(("pty: arg %d len %d\n", i, len));
228231
arg= (char *)alloca(len + 1);
229232
memcpy((void *)arg, (void *)vm->firstIndexableField(argOop), len);
230233
arg[len]= '\0';
231234
argv[i]= arg;
232-
DPRINTF(("pty: argv[%d]: %s\n", i, argv[i]));
235+
debugf(("pty: argv[%d]: %s\n", i, argv[i]));
233236
}
234237
argv[argLen+1]= 0; /* argv terminator */
235238

@@ -239,9 +242,9 @@ int ptyForkAndExec(AsyncFile *f, int semaIndex,
239242
slaves= slave;
240243
slave->pts= pts;
241244
slave->pty= fp;
242-
slave->pid= fork();
243-
244-
switch (slave->pid)
245+
childPid= fork();
246+
slave->pid= childPid;
247+
switch (childPid)
245248
{
246249
case -1: /* error */
247250
slaves= slaves->next;
@@ -263,7 +266,7 @@ int ptyForkAndExec(AsyncFile *f, int semaIndex,
263266
close(pts);
264267
break;
265268
}
266-
return 0;
269+
return childPid;
267270
}
268271

269272
fail:
@@ -274,7 +277,7 @@ int ptyForkAndExec(AsyncFile *f, int semaIndex,
274277
if (ptm >= 0) close(ptm);
275278
if (pts >= 0) close(pts);
276279
vm->primitiveFail();
277-
return 0;
280+
return -1;
278281
}
279282

280283

@@ -283,14 +286,14 @@ int ptyClose(AsyncFile *f)
283286
SlavePtr slave= 0, prev= 0;
284287
FilePtr pty= (FilePtr)f->state;
285288
validate(f);
286-
DPRINTF(("pty: close %d\n", pty->fd));
289+
debugf(("pty: close %d\n", pty->fd));
287290
if (pty->fd >= 0)
288291
{
289292
for (prev= 0, slave= slaves; slave; prev= slave, slave= slave->next)
290293
if (slave->pty == pty)
291294
{
292295
int pid= slave->pid;
293-
DPRINTF(("killing pid %d connected to pts %d\n", pid, slave->pts));
296+
debugf(("killing pid %d connected to pts %d\n", pid, slave->pts));
294297
/* terminate with increasing degrees of violence... */
295298
kill(pid, SIGTERM);
296299
usleep(200*1000);
@@ -318,7 +321,7 @@ int ptyWindowSize(AsyncFile *f, int cols, int rows)
318321
struct winsize sz;
319322
FilePtr pty= (FilePtr)f->state;
320323
validate(f);
321-
DPRINTF(("pty %d size %d %d\n", pty->fd, cols, rows));
324+
debugf(("pty %d size %d %d\n", pty->fd, cols, rows));
322325
sz.ws_col= cols;
323326
sz.ws_row= rows;
324327
sz.ws_xpixel= sz.ws_ypixel= 0;

0 commit comments

Comments
 (0)