|
Home > Archive > Unix Programming > November 2004 > Creating a tty in a C program
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Creating a tty in a C program
|
|
| Robert M. Gary 2004-11-23, 6:08 pm |
| Solaris 8
I have an application that runs as a daemon (no tty) and will occasionally
fork another process to drive i/o. My customer is trying to fork off ssh to
drive input. However, ssh is complaining that there is no tty available so
it does not work. Usually I just have customers run telnet localhost and
then run their app (since telnet creates a new tty). However, this customer
says they have disabled telnetd and the like for security.
So, how can I get a tty before I exec this process?
I was looking at man pty and see
#include <fcntl.h>
#include <sys/termios.h>
int fdm fds;
fdm = open("/dev/ptyp0, O_RDWR); /* open master */
fds = open("/dev/ttyp0, O_RDWR); /* open slave */
but the master hangs and if I comment it out the slave hangs when I try to
i/o to it. I've tried running it as root.
-Robert
| |
| Nils O. Selåsdal 2004-11-24, 8:12 am |
| Robert M. Gary wrote:
> Solaris 8
> I have an application that runs as a daemon (no tty) and will occasionally
> fork another process to drive i/o. My customer is trying to fork off ssh to
> drive input. However, ssh is complaining that there is no tty available so
> it does not work. Usually I just have customers run telnet localhost and
> then run their app (since telnet creates a new tty). However, this customer
> says they have disabled telnetd and the like for security.
> So, how can I get a tty before I exec this process?
> I was looking at man pty and see
> #include <fcntl.h>
> #include <sys/termios.h>
>
> int fdm fds;
> fdm = open("/dev/ptyp0, O_RDWR); /* open master */
> fds = open("/dev/ttyp0, O_RDWR); /* open slave */
Are you perhaps looking for the openpty(2) routine ?
| |
| Chuck Dillon 2004-11-24, 8:12 am |
| Robert M. Gary wrote:
> Solaris 8
> I have an application that runs as a daemon (no tty) and will occasionally
> fork another process to drive i/o. My customer is trying to fork off ssh to
> drive input. However, ssh is complaining that there is no tty available so
> it does not work. Usually I just have customers run telnet localhost and
> then run their app (since telnet creates a new tty). However, this customer
> says they have disabled telnetd and the like for security.
An alternative to using telnet would for the customer to install expect
(http://expect.nist.gov/) and launch his/her ssh session through it.
If you still want to implement ptys in your daemon consider using
libexpect which will give you a portable solution and make it simpler
to add features like terminal emulations if such requirements come along.
-- ced
> So, how can I get a tty before I exec this process?
> I was looking at man pty and see
> #include <fcntl.h>
> #include <sys/termios.h>
>
> int fdm fds;
> fdm = open("/dev/ptyp0, O_RDWR); /* open master */
> fds = open("/dev/ttyp0, O_RDWR); /* open slave */
>
>
> but the master hangs and if I comment it out the slave hangs when I try to
> i/o to it. I've tried running it as root.
>
> -Robert
>
>
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
| |
| Casper H.S. Dik 2004-11-24, 6:28 pm |
| "Robert M. Gary" <foobar@foobar.com> writes:
>Solaris 8
>I have an application that runs as a daemon (no tty) and will occasionally
>fork another process to drive i/o. My customer is trying to fork off ssh to
>drive input. However, ssh is complaining that there is no tty available so
>it does not work. Usually I just have customers run telnet localhost and
>then run their app (since telnet creates a new tty). However, this customer
>says they have disabled telnetd and the like for security.
>So, how can I get a tty before I exec this process?
>I was looking at man pty and see
> #include <fcntl.h>
> #include <sys/termios.h>
> int fdm fds;
> fdm = open("/dev/ptyp0, O_RDWR); /* open master */
> fds = open("/dev/ttyp0, O_RDWR); /* open slave */
>but the master hangs and if I comment it out the slave hangs when I try to
>i/o to it. I've tried running it as root.
Don't use "man pty". Check man pts for the proper, safe and
easy to use ptys:
int fdm fds;
char *slavename;
extern char *ptsname();
fdm = open("/dev/ptmx", O_RDWR); /* open master */
grantpt(fdm); /* change permission of slave */
unlockpt(fdm); /* unlock slave */
slavename = ptsname(fdm); /* get name of slave */
fds = open(slavename, O_RDWR); /* open slave */
ioctl(fds, I_PUSH, "ptem"); /* push ptem */
ioctl(fds, I_PUSH, "ldterm"); /* push ldterm*/
You will need to setup the I/O properly and make sure you don't
get into a deadlock situation.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| Robert M. Gary 2004-11-24, 6:28 pm |
| I'm not seeing that in the man pages. Do I need to install a Solaris package
for that? I'm running on Solaris 8 (although I have a few customers on 9).
"Nils O. Selåsdal" <NOS@Utel.no> wrote in message
news:_rZod.18419$Km6.223356@news4.e.nsc.no...
> Are you perhaps looking for the openpty(2) routine ?
| |
| Robert M. Gary 2004-11-24, 6:28 pm |
| Awesome! I'll give it a shot!
"Casper H.S. Dik" <Casper.Dik@Sun.COM> wrote in message
news:41a4b769$0$568$e4fe514c@news.xs4all.nl...
> "Robert M. Gary" <foobar@foobar.com> writes:
>
occasionally[vbcol=seagreen]
to[vbcol=seagreen]
so[vbcol=seagreen]
customer[vbcol=seagreen]
>
>
>
to[vbcol=seagreen]
>
> Don't use "man pty". Check man pts for the proper, safe and
> easy to use ptys:
>
> int fdm fds;
> char *slavename;
> extern char *ptsname();
>
> fdm = open("/dev/ptmx", O_RDWR); /* open master */
> grantpt(fdm); /* change permission of slave */
> unlockpt(fdm); /* unlock slave */
> slavename = ptsname(fdm); /* get name of slave */
> fds = open(slavename, O_RDWR); /* open slave */
> ioctl(fds, I_PUSH, "ptem"); /* push ptem */
> ioctl(fds, I_PUSH, "ldterm"); /* push ldterm*/
>
>
> You will need to setup the I/O properly and make sure you don't
> get into a deadlock situation.
>
> Casper
> --
> Expressed in this posting are my opinions. They are in no way related
> to opinions held by my employer, Sun Microsystems.
> Statements on Sun products included here are not gospel and may
> be fiction rather than truth.
| |
| Rich Teer 2004-11-24, 6:28 pm |
| On Tue, 23 Nov 2004, Robert M. Gary wrote:
> Solaris 8
> I have an application that runs as a daemon (no tty) and will occasionally
> fork another process to drive i/o. My customer is trying to fork off ssh to
> drive input. However, ssh is complaining that there is no tty available so
> it does not work. Usually I just have customers run telnet localhost and
> then run their app (since telnet creates a new tty). However, this customer
> says they have disabled telnetd and the like for security.
> So, how can I get a tty before I exec this process?
> I was looking at man pty and see
> #include <fcntl.h>
> #include <sys/termios.h>
>
> int fdm fds;
> fdm = open("/dev/ptyp0, O_RDWR); /* open master */
> fds = open("/dev/ttyp0, O_RDWR); /* open slave */
What you need is a pseudo tty. Chapter 23 of my book, Solaris Systems
Programming, explains all.
HTH,
--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
| |
| Casper H.S. Dik 2004-11-24, 6:28 pm |
| "Robert M. Gary" <foobar@foobar.com> writes:
>I'm not seeing that in the man pages. Do I need to install a Solaris package
>for that? I'm running on Solaris 8 (although I have a few customers on 9).
There's no openpty() in Solaris.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| Nick Maclaren 2004-11-24, 6:28 pm |
|
In article <41a4b769$0$568$e4fe514c@news.xs4all.nl>,
Casper H.S. Dik <Casper.Dik@Sun.COM> writes:
|>
|> Don't use "man pty". Check man pts for the proper, safe and
|> easy to use ptys: ...
Isn't that a bit misleading? Shouldn't "proper" be "modern"?
And you could probably add "standard" to that.
As I understand it, people gagged at the prospect of standardising
the old Berkeley pty mechanism, and designed the pts one. Or have
I misunderstood?
Regards,
Nick Maclaren.
| |
| Casper H.S. Dik 2004-11-24, 6:28 pm |
| nmm1@cus.cam.ac.uk (Nick Maclaren) writes:
>Isn't that a bit misleading? Shouldn't "proper" be "modern"?
>And you could probably add "standard" to that.
They're standard now, indeed. (and when linker with appropriate
options the I_PUSH isn't necessary.)
>As I understand it, people gagged at the prospect of standardising
>the old Berkeley pty mechanism, and designed the pts one. Or have
>I misunderstood?
No, they were done at AT&T and I think I remember smb telling
me that he did it, including the pt_chmod program (core of
grantpt()) which with all the other commands completely does
away with the awkward BSD way of allocating including the many security
and race condition bugs.
BSD ptys are a crock and so they standardized the AT&T ones.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| Nick Maclaren 2004-11-24, 6:28 pm |
| In article <41a4f606$0$34762$e4fe514c@news.xs4all.nl>,
Casper H.S. Dik <Casper.Dik@Sun.COM> wrote:
>
>
>No, they were done at AT&T and I think I remember smb telling
>me that he did it, including the pt_chmod program (core of
>grantpt()) which with all the other commands completely does
>away with the awkward BSD way of allocating including the many security
>and race condition bugs.
Ah. Thanks for the correction.
>BSD ptys are a crock and so they standardized the AT&T ones.
BSD ptys are, indeed, a software engineering disaster.
Regards,
Nick Maclaren.
| |
| SM Ryan 2004-11-25, 7:50 am |
| Casper H.S. Dik <Casper.Dik@Sun.COM> wrote:
# "Robert M. Gary" <foobar@foobar.com> writes:
#
# >I'm not seeing that in the man pages. Do I need to install a Solaris package
# >for that? I'm running on Solaris 8 (although I have a few customers on 9).
#
#
# There's no openpty() in Solaris.
Every unix does pseudo-ttys differently.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
If your job was as meaningless as theirs, wouldn't you go crazy too?
| |
| Boyd Adamson 2004-11-25, 5:53 pm |
| "Robert M. Gary" <foobar@foobar.com> writes:
> Solaris 8
> I have an application that runs as a daemon (no tty) and will occasionally
> fork another process to drive i/o. My customer is trying to fork off ssh to
> drive input. However, ssh is complaining that there is no tty available so
> it does not work. Usually I just have customers run telnet localhost and
> then run their app (since telnet creates a new tty). However, this customer
> says they have disabled telnetd and the like for security.
> So, how can I get a tty before I exec this process?
You may possibly be able to get what you're after with ssh -t -t
--
Boyd
| |
| Richard L. Hamilton 2004-11-26, 5:50 pm |
| In article <co2th0$cjo$1@gemini.csx.cam.ac.uk>,
nmm1@cus.cam.ac.uk (Nick Maclaren) writes:
> In article <41a4f606$0$34762$e4fe514c@news.xs4all.nl>,
> Casper H.S. Dik <Casper.Dik@Sun.COM> wrote:
>
> Ah. Thanks for the correction.
>
>
> BSD ptys are, indeed, a software engineering disaster.
>
although the TIOCPKT and TIOCREMOTE modes look kind of cool in terms
of offering differing sorts of control. I have some dim memory that
one or both of those had something to do with working with Crays, but
maybe I'm just losing it today...
--
mailto:rlhamil@smart.net http://www.smart.net/~rlhamil
Lasik/PRK theme music:
"In the Hall of the Mountain King", from "Peer Gynt"
|
|
|
|
|