comparison host/tools/Utils/common/Subprocess.cpp @ 2094:a22e535e2a4c

* common/Subprocess.cpp (CSubprocess::CreateProcess): Try the Unix98 PTY scheme in addition to the BSD scheme. Recent Linux kernels may be built without legacy PTY support.
author jld
date Wed, 02 Nov 2005 08:39:18 +0000
parents 4c750ce71ae3
children 74dbf4c3f2e1
comparison
equal deleted inserted replaced
2093:105ae1490488 2094:a22e535e2a4c
1 //####COPYRIGHTBEGIN#### 1 //####COPYRIGHTBEGIN####
2 // 2 //
3 // ---------------------------------------------------------------------------- 3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. 4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 // Copyright (C) 2004 eCosCentric Limited
5 // 6 //
6 // This program is part of the eCos host tools. 7 // This program is part of the eCos host tools.
7 // 8 //
8 // This program is free software; you can redistribute it and/or modify it 9 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free 10 // under the terms of the GNU General Public License as published by the Free
40 // Usage: 41 // Usage:
41 // 42 //
42 //####DESCRIPTIONEND#### 43 //####DESCRIPTIONEND####
43 // 44 //
44 //=========================================================================== 45 //===========================================================================
46
47 // define _GNU_SOURCE to ensure that stdlib.h provides Unix98 PTY declarations
48 #define _GNU_SOURCE
49
45 #include "eCosTrace.h" 50 #include "eCosTrace.h"
46 #include "Subprocess.h" 51 #include "Subprocess.h"
47 #ifdef _WIN32 52 #ifdef _WIN32
48 #include <tlhelp32.h> 53 #include <tlhelp32.h>
49 54
329 bool CSubprocess::CreateProcess(LPCTSTR pszCmdline) 334 bool CSubprocess::CreateProcess(LPCTSTR pszCmdline)
330 { 335 {
331 m_idProcess=0; 336 m_idProcess=0;
332 int fdchild=-1; // the file descriptor for the child (slave) half of the pseudo-tty pair 337 int fdchild=-1; // the file descriptor for the child (slave) half of the pseudo-tty pair
333 338
334 // Get a free /dev/ptyp0 (master) and /dev/ttyp0 (slave) tty pair 339 // Try the Unix98 scheme to get a pseudo-tty pair
335 String strMasterTty,strChildTty; 340 String strMasterTty("/dev/ptmx"), strChildTty;
336 for(unsigned int c=0;c<64;c++){ 341 m_tty=open(strMasterTty, O_RDWR | O_NOCTTY);
337 strMasterTty.Format("/dev/pty%c%x",'p'+c/16,c%16); 342 if (-1!=m_tty) {
343 if ((0 == grantpt(m_tty)) && (0 == unlockpt(m_tty))) {
344 strChildTty = ptsname(m_tty);
345 if (!strChildTty.empty()) {
346 fdchild = open(strChildTty, O_RDWR);
347 }
348 }
349 if (-1==fdchild) {
350 close(m_tty);
351 m_tty=fdchild=-1;
352 } else {
353 VTRACE("opened %s - fd=%d\n",(LPCTSTR)strMasterTty,m_tty);
354 }
355 }
356
357 if (-1==m_tty) {
358 // Try the BSD scheme to get a free /dev/ptyp0 (master) and /dev/ttyp0 (slave) tty pair
359 for(unsigned int c=0;c<64;c++){
360 strMasterTty.Format("/dev/pty%c%x",'p'+c/16,c%16);
338 361
339 m_tty=open(strMasterTty, O_RDWR | O_NOCTTY); 362 m_tty=open(strMasterTty, O_RDWR | O_NOCTTY);
340 if (-1!=m_tty) { 363 if (-1!=m_tty) {
341 strChildTty.Format("/dev/tty%c%x",'p'+c/16,c%16); 364 strChildTty.Format("/dev/tty%c%x",'p'+c/16,c%16);
342 365
343 fdchild = open(strChildTty, O_RDWR); 366 fdchild = open(strChildTty, O_RDWR);
344 if (-1==fdchild) { 367 if (-1==fdchild) {
345 close(m_tty); 368 close(m_tty);
346 m_tty=fdchild=-1; 369 m_tty=fdchild=-1;
347 } else { 370 } else {
348 VTRACE("opened %s - fd=%d\n",(LPCTSTR)strMasterTty,m_tty); 371 VTRACE("opened %s - fd=%d\n",(LPCTSTR)strMasterTty,m_tty);
349 break; 372 break;
373 }
350 } 374 }
351 } 375 }
352 } 376 }
353 377
354 if(-1==m_tty){ 378 if(-1==m_tty){