Hi all, I have something that I'm working on, and with it, I believe I need to fork a process, and retrieve the exit status and PID. A command is exec'ed in the background that essentially is an rsync process

I'm working on a Browser based Rsync backup process: Ajax <-> Perl.cgi -> SETUID C Prog ->Perl(Rsync)

This C program is passed a username and the script to run, and it changes it's effective UID before running the Perl script, so that the rsync command can be run as an ssh login session sudo'ed to the user with environment, and subsequent key pairs, to enable a non-interactive process.

All works so far as I can tell, except for the very last bit - I'm trying to decide on the best approach to launch process, retrieve PID, and Exit Status before writing JSON back to the browser about what happened, and continue to seek read chunks from logfile and also write back to web browser in separate process

When I open pipe as a way to fork and get PID, I get success (on open pipe) even if wrapper process fails. If I exec, I get no PID etc. I know I can use 'pidof' and/or parse 'ps' command etc., but perhaps I'm not seeing a less-convoluted way to approach this?

Any awesome suggestions are welcome

### run backup command ### my @system_params = ( "-o", "log/nix_backup_wrap", "$var{usern +ame}", "./backup_run.plX" ); my $pid = open my $cmd, "-|", './nix_backup_wrap', @system_pa +rams or warn "$!\n"; my $backup_process_status = $? >> 8; my @backup_status_message; print "BACKUP PROCESS STATUS: $backup_process_status\n"; if ( $backup_process_status == 0 ) { $msglog->write( "Success starting backup process!" ); push @backup_status_message, "Success starting backup +process!"; open 'PID', '>', $PID_FILE or die "Status $var{status} +: Tried to open $PID_FILE $!\n"; print PID "$pid\n"; close PID; } else { $err_msglog->write( "Error starting backup process!" ) +; push @backup_status_message, "Error starting backup pr +ocess!"; } $jsonObject = { msgdata => "BACKUP STATUS MESSAGE: @backup_status_message with P +ID: $pid", pid => $pid, lposition => $var{lposition}, count => $var{count}, segment => $var{segment}, status => $var{status}, backup_process_status => $backup_process_status }; my $jsonEncoded = encode_json $jsonObject; print WB_OUT $cgi->header(-type => "application/json", -charset => + "utf-8"); print WB_OUT $jsonEncoded;
From Rsync Script
eval { alarm 5; $msglog->write( "/usr/bin/rsync -avz $var{username}\@$IP:/tmp/$var{ +username}/test /tmp/ | tee -a $TMPLOG" ); exec( "/usr/bin/rsync -avz $var{username}\@$IP:/tmp/$var{username}/ +test /tmp/ | tee -a $TMPLOG") or $err_msglog->write( "STATUS: $var{status}: Couldn't initiate bac +kup $!\n" ); alarm 0; };
From C Program
/* Set UID to user */ setuid( new_uid ); _user = custom_command( command_whoami ); printf("User after setuid(%d): %s\n", new_uid,_user); /* Now run the actual ".cgi" file */ strcpy( _HOME, "HOME=/home/e-smith/files/users/" ); strcat( _HOME, username ); strcpy( _LOGNAME, "LOGNAME=" ); strcat( _LOGNAME, username ); int ret; char *cmd[] = { script_name, username, (char *)0 }; char *env[] = { _HOME, _LOGNAME, "SHELL=/bin/bash", "SSH_TTY=/dev/pts/ +1", (char *)0 }; ret = execve (script_name, cmd, env); return(0); }

In reply to Open pipe and fork or system command to retrieve PID and exit status - Best approach? by altTesting

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.