UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2005-07-14 07:48 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: tcgetaddr() replacement when running from a daemon?

Hi all, here is a rather esoteric question for you: 

I have some code that launches a child process, and uses the /dev/tty and /dev/pty devices to communicate with the child process via its stdin and stdout streams.  This allows my software to interact with interactive command line apps in much the same way as a user would, and is very useful e.g. when I want to make a user-friendly GUI or network-accessible interface for a third-party command line app.

This all works pretty well, except when my process is running in the background (as a daemon).  One of the steps in setting up the communication with the child process is to query the current terminal parameters, so I can replicate them for the child process, like this:

struct termios orig_termios;
struct winsize size;
if (tcgetattr(STDIN_FILENO, &orig_termios) < 0)
{
   // handle error?
}
if (ioctl(STDIN_FILENO, TIOCGWINSZ, (char *) &size) < 0)
{
   // handle error?
}

... but of course when my code is running as a daemon, it doesn't have a current terminal, so these calls fail.  My current "solution" for this problem is to then fill in the winsize and termios structures with some hard-coded (empirically determined) default data, which sort of works -- but often it gives me "interesting" results, such as all the text from the child process's stdout being received in my process in UPPER CASE, etc.

So my question is, in the case where I can't get info about the terminal settings for the current process, is there some other call I can make to collect some "reasonable" settings to use instead?

Offline

#2 2005-07-15 01:10 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: tcgetaddr() replacement when running from a daemon?

One simple work-around is to obtain the terminal settings before daemonizing, and
keep them around in static variables...  Of course, if you happen to be started with
no terminal at all (or, something bogus like /dev/null), you'll still have a problem...

Your system may define macros like TTYDEF_IFLAG, TTYDEF_OFLAG, etc.,
which you can use to initialize a termios struct with...  Look in <termios.h> (or,
possibly some other included header, like <sys/ttydefaults.h>)...

Offline

#3 2005-07-19 05:21 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: tcgetaddr() replacement when running from a daemon?

Offline

Board footer

Powered by FluxBB