~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to ext/pty/pty.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include        "config.h"
 
1
#include        "ruby/config.h"
2
2
#ifdef RUBY_EXTCONF_H
3
3
#include RUBY_EXTCONF_H
4
4
#endif
25
25
#endif
26
26
#include <ctype.h>
27
27
 
28
 
#include "ruby.h"
29
 
#include "rubyio.h"
30
 
#include "util.h"
 
28
#include "ruby/ruby.h"
 
29
#include "ruby/io.h"
 
30
#include "ruby/util.h"
31
31
 
32
32
#include <signal.h>
33
33
#ifdef HAVE_SYS_STROPTS_H
42
42
 
43
43
#if !defined(HAVE_OPENPTY)
44
44
#if defined(__hpux)
45
 
static
46
 
char    *MasterDevice = "/dev/ptym/pty%s",
47
 
        *SlaveDevice =  "/dev/pty/tty%s",
48
 
        *deviceNo[] = {
 
45
static const
 
46
char    MasterDevice[] = "/dev/ptym/pty%s",
 
47
        SlaveDevice[] =  "/dev/pty/tty%s",
 
48
        *const deviceNo[] = {
49
49
                "p0","p1","p2","p3","p4","p5","p6","p7",
50
50
                "p8","p9","pa","pb","pc","pd","pe","pf",
51
51
                "q0","q1","q2","q3","q4","q5","q6","q7",
65
65
                0,
66
66
        };
67
67
#elif defined(_IBMESA)  /* AIX/ESA */
68
 
static
69
 
char    *MasterDevice = "/dev/ptyp%s",
70
 
        *SlaveDevice = "/dev/ttyp%s",
71
 
        *deviceNo[] = {
 
68
static const
 
69
char    MasterDevice[] = "/dev/ptyp%s",
 
70
        SlaveDevice[] = "/dev/ttyp%s",
 
71
        *const deviceNo[] = {
72
72
"00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
73
73
"10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
74
74
"20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
87
87
"f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff",
88
88
                };
89
89
#elif !defined(HAVE_PTSNAME)
90
 
static
91
 
char    *MasterDevice = "/dev/pty%s",
92
 
        *SlaveDevice = "/dev/tty%s",
93
 
        *deviceNo[] = {
 
90
static const
 
91
char    MasterDevice[] = "/dev/pty%s",
 
92
        SlaveDevice[] = "/dev/tty%s",
 
93
        *const deviceNo[] = {
94
94
                "p0","p1","p2","p3","p4","p5","p6","p7",
95
95
                "p8","p9","pa","pb","pc","pd","pe","pf",
96
96
                "q0","q1","q2","q3","q4","q5","q6","q7",
104
104
#endif
105
105
#endif /* !defined(HAVE_OPENPTY) */
106
106
 
107
 
static char SlaveName[DEVICELEN];
108
 
 
109
107
#ifndef HAVE_SETEUID
110
108
# ifdef HAVE_SETREUID
111
109
#  define seteuid(e)    setreuid(-1, (e))
154
152
        cpid = rb_waitpid(info->child_pid, &status, WUNTRACED);
155
153
        if (cpid == -1) return Qnil;
156
154
 
157
 
#if defined(IF_STOPPED)
158
 
        if (IF_STOPPED(status)) { /* suspend */
159
 
            raise_from_wait("stopped", info);
160
 
        }
161
 
#elif defined(WIFSTOPPED)
162
 
        if (WIFSTOPPED(status)) { /* suspend */
163
 
            raise_from_wait("stopped", info);
164
 
        }
 
155
#if defined(WIFSTOPPED)
 
156
#elif defined(IF_STOPPED)
 
157
#define WIFSTOPPED(status) IF_STOPPED(status)
165
158
#else
166
159
---->> Either IF_STOPPED or WIFSTOPPED is needed <<----
167
160
#endif /* WIFSTOPPED | IF_STOPPED */
 
161
        if (WIFSTOPPED(status)) { /* suspend */
 
162
            raise_from_wait("stopped", info);
 
163
        }
168
164
        else if (kill(info->child_pid, 0) == 0) {
169
165
            raise_from_wait("changed", info);
170
166
        }
175
171
    }
176
172
}
177
173
 
178
 
static void getDevice(int*, int*);
 
174
static void getDevice(int*, int*, char [DEVICELEN]);
179
175
 
180
176
struct exec_info {
181
177
    int argc;
190
186
}
191
187
 
192
188
static void
193
 
establishShell(int argc, VALUE *argv, struct pty_info *info)
 
189
establishShell(int argc, VALUE *argv, struct pty_info *info,
 
190
               char SlaveName[DEVICELEN])
194
191
{
195
 
    int                 i,master,slave;
 
192
    int                 master,slave;
196
193
    rb_pid_t            pid;
197
 
    char                *p,*getenv();
 
194
    char                *p, tmp, *getenv();
198
195
    struct passwd       *pwent;
199
196
    VALUE               v;
200
197
    struct exec_info    arg;
217
214
        argc = 1;
218
215
        argv = &v;
219
216
    }
220
 
    getDevice(&master,&slave);
 
217
    getDevice(&master, &slave, SlaveName);
221
218
 
222
219
    info->thread = rb_thread_current();
223
220
    if ((pid = fork()) < 0) {
240
237
#  else /* SETGRP_VOID */
241
238
        if (setpgrp(0, getpid()) == -1)
242
239
            rb_sys_fail("setpgrp()");
243
 
        if ((i = open("/dev/tty", O_RDONLY)) < 0)
244
 
            rb_sys_fail("/dev/tty");
245
 
        else {
 
240
        {
 
241
            int i = open("/dev/tty", O_RDONLY);
 
242
            if (i < 0) rb_sys_fail("/dev/tty");
246
243
            if (ioctl(i, TIOCNOTTY, (char *)0))
247
244
                perror("ioctl(TIOCNOTTY)");
248
245
            close(i);
267
264
        }
268
265
        close(master);
269
266
#endif
 
267
        write(slave, "", 1);
270
268
        dup2(slave,0);
271
269
        dup2(slave,1);
272
270
        dup2(slave,2);
282
280
        _exit(1);
283
281
    }
284
282
 
 
283
    read(master, &tmp, 1);
285
284
    close(slave);
286
285
 
287
286
    info->child_pid = pid;
298
297
}
299
298
 
300
299
static int
301
 
get_device_once(int *master, int *slave, int fail)
 
300
get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int fail)
302
301
{
303
302
#if defined HAVE_OPENPTY
304
303
/*
321
320
    }
322
321
 
323
322
    *slave = open(name, O_RDWR);
324
 
    strcpy(SlaveName, name);
 
323
    strlcpy(SlaveName, name, sizeof SlaveName);
325
324
 
326
325
    return 0;
327
326
#else /* HAVE__GETPTY */
345
344
#if defined I_PUSH && !defined linux
346
345
                        if(ioctl(j, I_PUSH, "ptem") != -1) {
347
346
                            if(ioctl(j, I_PUSH, "ldterm") != -1) {
 
347
                                ioctl(j, I_PUSH, "ttcompat");
348
348
#endif
349
349
                                *master = i;
350
350
                                *slave = j;
351
 
                                strcpy(SlaveName, pn);
 
351
                                strlcpy(SlaveName, pn, sizeof SlaveName);
352
352
                                return 0;
353
353
#if defined I_PUSH && !defined linux
354
354
                            }
367
367
    char MasterName[DEVICELEN];
368
368
 
369
369
    for (p = deviceNo; *p != NULL; p++) {
370
 
        sprintf(MasterName,MasterDevice,*p);
 
370
        snprintf(MasterName, sizeof MasterName, MasterDevice, *p);
371
371
        if ((i = open(MasterName,O_RDWR,0)) >= 0) {
372
372
            *master = i;
373
 
            sprintf(SlaveName,SlaveDevice,*p);
 
373
            snprintf(SlaveName, sizeof SlaveName, SlaveDevice, *p);
374
374
            if ((j = open(SlaveName,O_RDWR,0)) >= 0) {
375
375
                *slave = j;
376
376
                chown(SlaveName, getuid(), getgid());
387
387
}
388
388
 
389
389
static void
390
 
getDevice(int *master, int *slave)
 
390
getDevice(int *master, int *slave, char SlaveName[DEVICELEN])
391
391
{
392
 
    if (get_device_once(master, slave, 0)) {
 
392
    if (get_device_once(master, slave, SlaveName, 0)) {
393
393
        rb_gc();
394
 
        get_device_once(master, slave, 1);
 
394
        get_device_once(master, slave, SlaveName, 1);
395
395
    }
396
396
}
397
397
 
405
405
    rb_io_t *wfptr,*rfptr;
406
406
    VALUE rport = rb_obj_alloc(rb_cFile);
407
407
    VALUE wport = rb_obj_alloc(rb_cFile);
 
408
    char SlaveName[DEVICELEN];
408
409
 
409
410
    MakeOpenFile(rport, rfptr);
410
411
    MakeOpenFile(wport, wfptr);
411
412
 
412
 
    establishShell(argc, argv, &info);
 
413
    establishShell(argc, argv, &info, SlaveName);
413
414
 
414
415
    rfptr->mode = rb_io_mode_flags("r");
415
416
    rfptr->fd = info.fd;