Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - posix_spawnp diffifculties with file actions: (1 Item)
   
posix_spawnp diffifculties with file actions  
I'm currently using posix_spawnp() to create child processes. I'm using posix_spawnp() instead of  soawn() because I 
need the ability to start the child process with a specified uid:gid that is not the same as the parent process. I also 
want to "redirect" or open a specific file to rep[lace stdin, stdout, & stderr in the child process. 

Anybody out there an 'expert' on posix_spawn ? The code below doesn't seem to work for the file replacement.


The following is a snipit of code [left out error handling] that I'm am using that is not working for the file portion. 
The function is called simple_spawn(), parameters are the path/file of the process to spawn, and a pointer to a argv[] 
type of array.

simpleSpawn( char *file, char **argv )
{
    int rtn;
    pid_t childPid = -1;
    char * replFileName = "/var/log/mylog.txt";

    posix_spawnattr_t spawnAttrib;
    posix_spawn_file_actions_t spawnFileActions;

    // init & setup attrib object
    posix_spawnattr_init( &spawnAttrib );
    posix_spawnattr_setxflags( &spawnAttrib, POSIX_SPAWN_SETCRED );
    posix_spawnattr_setcred( &spawnAttrib, 0, 0 );

    // init & setup file actions object
    posix_spawn_file_actions_init( &spawnFileActions );
    // replace stdin
    posix_spawn_file_actions_addopen( &spawnFileActions, 0, replFileName, O_RDWR | O_CREAT | O_APPEND, 0600 )
    // replace stdout
    posix_spawn_file_actions_addopen( &spawnFileActions, 1, replFileName, O_RDWR | O_CREAT | O_APPEND, 0600 )
    // replace stderr
    posix_spawn_file_actions_addopen( &spawnFileActions, 2, replFileName, O_RDWR | O_CREAT | O_APPEND, 0600 )

    // spawn the child
    posix_spawnp( &childPid, file, &spawnFileActions, &spawnAttrib, argv, NULL )

    return childPid;
}