how do I create a pseudo tty and bind it to ftp using popen?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
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.
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).
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?
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"?
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 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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.