Unix Programming - Guaranteed Term Restore w/ atexit(3) No Good

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > Guaranteed Term Restore w/ atexit(3) No Good





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 Guaranteed Term Restore w/ atexit(3) No Good
Michael B Allen

2005-08-30, 2:56 am

I'm turning off ICANON and ECHO flags on my terminal and I was using
atexit(3) to call my "term_restore" function as a sort of insurance
policy that the terminal is restored to it's original state. But I have
since discovered two problems with this:

1) Atexit(3) is inherited by children so when the child exits the terminal
is prematurely restored.
2) The atexit(3) documentation claims it is only called during "normal"
program exit which suggests it may not be adequate to use in this
context (although it *does* appear to work in all abnormal cases I
have experienced so far).

Can someone suggest an alternative method to ensure that
"tcsetattr(STDIN_FILENO, TCSANOW, &restore);" is called when only the
parent process exits?

Thanks,
Mike

Maxim Yegorushkin

2005-08-30, 8:14 am


Michael B Allen wrote:
> I'm turning off ICANON and ECHO flags on my terminal and I was using
> atexit(3) to call my "term_restore" function as a sort of insurance
> policy that the terminal is restored to it's original state. But I have
> since discovered two problems with this:
>
> 1) Atexit(3) is inherited by children so when the child exits the terminal
> is prematurely restored.
> 2) The atexit(3) documentation claims it is only called during "normal"
> program exit which suggests it may not be adequate to use in this
> context (although it *does* appear to work in all abnormal cases I
> have experienced so far).
>
> Can someone suggest an alternative method to ensure that
> "tcsetattr(STDIN_FILENO, TCSANOW, &restore);" is called when only the
> parent process exits?


Catch all signals whose action is process termination. Execute your
code in the signal handler (you might want to be sure you call async
signal safe functions
http://www.opengroup.org/onlinepubs..._chap02_04.html)

Rich Teer

2005-08-30, 6:00 pm

On Tue, 30 Aug 2005, Michael B Allen wrote:

> Can someone suggest an alternative method to ensure that
> "tcsetattr(STDIN_FILENO, TCSANOW, &restore);" is called when only the
> parent process exits?


Not a particularly clever way, but how about making note of your
PID in the parent and then checking your PID in your exit handler,
calling tcsetattr only when getpid() == saved_parent_pid?

--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
Barry Margolin

2005-08-31, 2:52 am

In article <Pine.SOL.4.58.0508300919000.12892@zen.rite-group.com>,
Rich Teer <rich.teer@rite-group.com> wrote:

> On Tue, 30 Aug 2005, Michael B Allen wrote:
>
>
> Not a particularly clever way, but how about making note of your
> PID in the parent and then checking your PID in your exit handler,
> calling tcsetattr only when getpid() == saved_parent_pid?


A common technique is to call exit() only in the parent process, and
_exit() in the children.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com