On Mon, Sep 28 at 11:24, Stephen Pelc wrote:
> I need to determine if my Linux program is running detached, 
> e.g. if it was launched by
>   vfxlin
> or
>   vfxlin &
Uhm the second process isn't detached it's backgrounded which
isn't the same thing, it will still have a controlling terminal.
If your question was "Is my program in foreground or background?"
the following C demo should help.  It works by comparing the
programs process group with that currently selected foreground
group on the controlling terminal.  Also demos basic detached
opertion detection along the way.
> Is there a standard way for the vfxlin program to determine 
> this? It would also be useful to know if input or output is 
> being piped.
fstat() on the file descriptor should help.  The st_mode field
should help you distingush between plain files, character
devices (tty or /dev/null etc) and fifo/pipes.
/*
 *    Work out if a program is in foreground or background.
 *
 *    Report result via syslog for detached process, and
 *    via stderr for attached.
 */
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define INIT_PID    1
int
main( int argc, char *argv[] )
{
    int fd;
    pid_t ppid;
    pid_t mypgrp;
    pid_t ctpgrp;
    char *myname;
    int i;
    /* Get the program name for reports */
    myname = strrchr( argv[0],'/');
    if ( myname )
        myname++;
    else
        myname = argv[0];
    /* Try to open the controlling tty */
    fd = open("/dev/tty", O_RDONLY );
    if ( fd < 0 )
    {
        /* No controlling tty, report via syslog */
        ppid = getppid();
        if ( ppid == INIT_PID )
        syslog( LOG_INFO,"%s is detached child of init.", myname );
        else
        syslog( LOG_INFO,"%s is detached child of PID %d.", myname, ppid );
        exit( 0 );
    }
    /* Get our process group */
    mypgrp = getpgrp();
    /* Now report our state 10 times to allow playing for fg/bg settings */
    for ( i = 0 ; i < 10 ; i++ )
    {
        /* Get the controlling terminal group */
        ctpgrp = tcgetpgrp( fd );
        /* Report */
        if ( ctpgrp == mypgrp )
        fprintf( stderr,"%s is in foreground. (%d)\n", myname, mypgrp );
        else
        fprintf( stderr,"%s is in background. (%d != %d)\n", myname, mypgrp, ctpgrp );
        sleep( 2 );
    }
    close( fd );
    exit( 0 );
}
-- 
        Bob Dunlop