LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2009, 10:46 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
how do I create a pseudo tty and bind it to ftp using popen?


I want to read the stream of the ftp application in C++ using for example popen.

But nothing comes from the stream. I have since found that the ftp outputs to stdout but buffers this until an input is received.

I have found given it an input does release this buffer.

Although the suggestions on the web is to use a pseudo tty. How do I open ftp with popen using a pseudo tty?
 
Old 09-22-2009, 02:49 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I wrote a program in C which uses ftp, and I use that program regularly. Most of the program you can ignore, but the part which initializes the pty and the part that uses ftp is something you might want to check out.

Porting it to C++ should be trivial.

At least through 15 October, you will be able to download it from here.

By the way, it doesn't use popen(). You should use that when you need a pty for either writing or reading but not both. With ftp, you need both. So I didn't use it in this program.

Hope this helps.
 
Old 09-23-2009, 09:12 AM   #3
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Fantastic! I will investigate your source, cheers.

Since I posted this question I have found that I need to use forkpty() - I wonder if your code uses this? Although I have not implemented it as yet.
 
Old 09-23-2009, 09:25 AM   #4
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
LOL - you do use forkpty() - you wont believe the google'n I have done to find this (well, perhaps you do!).

Code looks promising.

Question when I spawn the child and execv an ftp, I am finding the execv not failing but the ftp dies immediately, i.e. ps shows ftp <defunct>. Any ideas?
 
Old 09-23-2009, 01:53 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by DEF. View Post
the ftp dies immediately, i.e. ps shows ftp <defunct>. Any ideas?
When a process is defunct, there's not much of that process left. Basically, what's left is the exit status of the process. The parent process should get that via waitpid(); see the man page, and read that man page carefully, because there are some non-intuitive bits in there.

Once you've used waitpid() to harvest the exit status, you will find that the defunct process has disappeared.

Incidentally, before you do any of that, are there any characters of data available as input on the pty? Those could contain a quite useful error message.
 
Old 09-25-2009, 07:22 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by DEF. View Post
LOL - you do use forkpty() - you wont believe the google'n I have done to find this (well, perhaps you do!).
Once you discover info libc you'll wonder how you lived without it before. Browsable in Konqueror with info:libc, even...
Kevin Barry
 
Old 09-29-2009, 03:56 AM   #7
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Thanks guys. I have a solution now using forkpty().

However, how do I set the forkpty() to open a device with O_NON_BLOCK? I tried the fcntl() method after the forkpty() and it has no affect.

Also... (sorry to drag this out) but can I use seek/tell to get number of bytes to read on a pty as it doesn't seem to work?

Progress is slow, but at least it's progress huh!
 
Old 09-29-2009, 03:01 PM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by DEF. View Post
how do I set the forkpty() to open a device with O_NON_BLOCK? I tried the fcntl() method after the forkpty() and it has no affect.
You don't. You use select() to read from the pty when it has data for you, and to write to it when you have data ready for it and it can absorb data immediately.
Quote:
Originally Posted by DEF. View Post
can I use seek/tell to get number of bytes to read on a pty
I've never used it for that, and have no idea whether it works or not. I read one byte at a time, assumbing select() tells me there's something available.

Of course, if select() tells me something's available, and I read(), and the result of the function call is zero instead of one, that means that there's no more data on the pty (which probably means that the object process has logged out).
 
Old 09-30-2009, 03:21 AM   #9
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Yes I have currently implemented with poll (poll is superior to select - but not as portable, yet) however reading one byte at a time seems a little 'tiresome'. Hence if I could read the length then I could just grab what is there. Alternatively, to also read non-blocking as well as poll will allow poll to let me know when data is available and read could then attempt to read larger chucks, say 255 bytes without blocking.

Some time later... OK, OK, fcntl does work if you use the correct flags!

Thank you all for your help.

wje_lq: would you require a c++ wrapper for your code?

Last edited by DEF.; 09-30-2009 at 03:22 AM.
 
Old 09-30-2009, 05:56 AM   #10
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by DEF. View Post
wje_lq: would you require a c++ wrapper for your code?
I'm afraid I don't understand the question. If by "your code" you mean the sample program whose source I put online, it's all written in C. And what do you mean by "would you require"?

Sorry, I'm stumped here.
 
Old 10-04-2009, 03:00 AM   #11
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
I was asking if you would like me to provide some C++ classes to provide a C++ interface to the ftp pty as used it your C sample source code?
 
Old 10-04-2009, 06:34 PM   #12
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by DEF. View Post
I was asking if you would like me to provide some C++ classes to provide a C++ interface to the ftp pty as used it your C sample source code?
Actually, I'm good as things are. :)

It's a program I use as is periodically (to pick up mail). I'm not likely to change it any time soon, much less translate it to C++. I don't use pty's in any other context at this point, and other projects are competing for my attention.

I am nonetheless grateful for your offer.
 
Old 10-25-2009, 11:57 AM   #13
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
hey wje_lq, btw: using the forkpty nicely, but...

I found that the file descriptor for the pty is failing to be closed. I didn't notice for a while due to incorrect error handling until the system runs out of file descriptors!.

Where in your code example do you close the pty file descriptor?
 
Old 10-25-2009, 12:10 PM   #14
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Perhaps I'll re-phrase... Can I use close() to close the pty file descriptor? In my program I forkpty() repetitively and thus need to close for every fork.
 
Old 10-25-2009, 05:35 PM   #15
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by DEF. View Post
Where in your code example do you close the pty file descriptor?
Oops.
Quote:
Originally Posted by DEF. View Post
Can I use close() to close the pty file descriptor?
Yes.
Quote:
Originally Posted by DEF. View Post
In my program I forkpty() repetitively and thus need to close for every fork.
Good idea.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Konsole - which pseudo-tty per session? theNbomr Linux - Software 5 11-07-2006 04:39 PM
Regarding Pseudo tty, Pseudo terminals ? mqureshi Programming 0 07-30-2005 10:51 AM
Pseudo tty configuration RH 7.3 Dalma Linux - Networking 1 06-17-2003 12:53 PM
Eterm pseudo-tty? jpbarto Linux From Scratch 4 04-14-2003 11:51 AM
Pseudo-TTY Control Under Linux nitr0gen Programming 0 03-21-2002 12:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration