SUMMARY: su -c issued twice hangs process

From: Hugh Pritchard <Hugh.Pritchard_at_MCI.Com>
Date: Thu, 25 Sep 1997 17:14 -0400 (EDT)

The DBAs found that, yes, stty in .profile (or other initialization)
files hung a second su in a script. They chose to encapsulate all
commands requiring a controlling terminal (e.g. stty) with a

    tty -s && { stty ...; }

This will not prevent the script failing when run from a command
line, as Sebastian Bar pointed out, because there you *would* have a
controlling terminal, so stty runs, leading to su's hanging.
However, this script is intended to run from cron or as a shutdown
script, and it works that way.
-- 
     Hugh
     Mailto: Hugh.Pritchard_at_MCI.com
Original question:
In a proposed startup script there are two lines like
    su - oracle -c "command string"
The second one hangs.  Experimenting, we've found that
    su - oracle -c "ps; exit"
doesn't hang after the second one.
I know the -c something gets passed to the new shell (in this case,
ksh) by su.  I've also heard that this is a problem on HPUX machines.
Is this a known problem, with the workaround being to add the exit
to the command string?
If the exit command is ALWAYS needed in the command string, why does
the first su work and return?
Is this an su problem, or a ksh problem?
-- 
Replies:
Date: Wed, 24 Sep 1997 08:00 -0400 (EDT)
From: "Bar, Sebastian" <SBar_at_eragsm.com.pl>
To: "'Hugh Pritchard'" <Hugh.Pritchard_at_MCI.Com>
Subject: RE: su -c issued twice hangs process
Hi,
I've had simmilar problem once. It's something wrong with stty an ksh.
Look on which command it is hanging during second login. It was stty
command (executed from .profile) on my machine. I've found, that this
situation happens only if you use ksh as a shell. (your experiment is
also funny :-) ). I've made a workaround for that, ie. I've renamed stty
to stty.orig and instead of original stty command there is a script:
-- stty --
#!/bin/sh
if [ ! -e /tmp/nostty] ; then
    stty.orig $*
fi
-- stty --
Now, in scripts that are using several 'su - c ' commands I make
something like that:
touch /tmp/nostty
....
su -c '....
su -c '...
...
rm /tmp/nostty
Hanging happens only if you run the script from shell by typing it. If
it will be executed from cron job it will not hang - stty will only say
that tty is not a terminal and will not try to set attributes.
Date: Wed, 24 Sep 1997 06:28 -0400 (EDT)
From: "LBRO"<lbro_at_dscc.dk>
To: <Hugh.Pritchard_at_MCI.Com>
Subject: Re: su -c issued twice hangs process 
     Hi,
     
     I have many times been asked why shell scripts that use the su command 
     get blocked unexpectedly. First an example:
     
     The script below:
     
     su - ingres -c /bin/true
     su - ingres -c /bin/true
     
     Hangs in the "stty dec" command in ~ingres/profile when line 2 is 
     executed. This happens independently of what shell is used to run the 
     script.
     
     But if you type the commands at the shell prompt, they wont hang of 
     course.  
     
     The explanation is as follows:
     
     When a Korn shell runs a command from the tty, it assigns a process 
     group to that command and notifies the tty that that process group is 
     in the foreground. When the command terminates, the Korn shell makes 
     its own process group the foreground process group.
     
     When a korn shell runs commands from a script, it does not take any 
     special measures and of course assumes that the commands do not mess 
     around with the controlling tty.
     
     However in the script above, the "su -" command starts an 
     "interactive" shell that makes its own process group owner of the tty. 
     When this shell exits, the calling shell is supposed to reset the 
     ownership of the tty. Unfortunately, the caller is a script which does 
     not do anything.
     
     When the first process exits, a 0 is placed for "foreground process 
     group", since the process group is dead. The script does not do 
     anything and the next "su -" command will see itself as being in the 
     background. The first eg. stty command will hang.
     
     The script below:
     
     su - ingres -c /bin/true
     stty echoe
     
     Will also hang in the stty command because the first command fucks it 
     up.
     
     So, Don't use commands that mess around with the controlling tty and 
     such. If we HAVE TO run such stuff, make a new tty for each invocation 
     For example like this:
     
     rsh cindy su - ingres -c /bin/true
     rsh cindy su - ingres -c /bin/true
     
     The rsh makes a new pseudotty that the su fucks up. It fortunately 
     dies with the command so we need not care.
     
     When we have to su and make use of the environment, run 
     
     su ingres ". ~/.profile; /bin/true"
     
     Yours,
     Lars Bro
Received on Thu Sep 25 1997 - 23:31:58 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:36 NZDT