title: How to reacquire stdin in a restarted process?
body: <p>I have written a C application that is started in a (gnome) terminal with 'sudo' and has an interactive CLI, to get user commands and print back feedback. It must be started under sudo as some of the internal operations require that.</p>
  
  <p>A possible command is to do (self) 'sw-upgrade' from a *.tar.gz file.
  Since the machine's memory is limited, I cannot use fork() as this leads to doubling the required memory. So I've written a bash script, invoked from the application by calling <code>posix_spawn(/bin/bash -c sw_load.sh)</code>, to kill the original application and start the new one.
  It seems to be working except for...</p>
  
  <p>Issue#1: Ideally I would like to restart the application in the background (with an &amp; at the end) but then I loose stdin and the user cannot enter any commands (the CLI engine reports FALSE for <code>isatty(fileno(stdin))</code> as <code>ttyname(fileno(stdin))</code> is empty.
  Is there a way to pass the terminal's stdin (or maybe the TTY/PTY?) to a background process? Or even better, have the application get it by itself?</p>
  
  <p>Issue#2: So, due to issue#1, I'm <strong>not</strong> restarting the application in the <strong>background</strong>, but in the foreground.
  I've added a call to <code>setsid()</code> right at the beginning of <code>main()</code>, to become a session leader (I can leave the sw_load.sh running or kill it, it does not matter...). Now I have some stdin working, but it gets <strong>mixed</strong> up with the bash prompt: Sometimes I get the application prompt and sometimes (or after some  keys) the bash prompt! Sometimes a typed in command is parsed by the application's CLI engine and sometime by bash. Could not find any pattern when one or the other. Can anyone shed some light on why this happens and how to fix this?</p>
  
  <p><em>Note: I've running this either in a CentOS6.7 or Ubuntu12.04 VirtualBox VM</em></p>
  
  <p>Thanks!</p>