named pipe help 
Author Message
 named pipe help

Hello all,

Please forgive my "newbieness" if I sound confused here...

I'm trying to create a _temporary_ named pipe that will fool another
program into thinking it's accessing data from a real file.  Once all
the data has been read, the named pipe should disappear...I don't want
it to hang around or leave any zombie processes.  I know how to create
the named pipe (on Unix) and delete it when I'm done, but I'm having
trouble conceiving of a way to have the pipe accessed by an external
program (since the main script has to "stall" waiting for an access
request to the pipe) -- since this is 1)a temporary pipe & 2)I want to
have it accessed almost immediately after it's created,  I know I need
to create some sort of child process but I've never done it before & I'm
having a hard time with the facts & docs (& books).  The timing of the
processes is also critical, since i don't want the child process to
start reading until the named pipe is "blocked" by the main/parent
script.

The reason I need to use a named pipe instead of STDIN is because I'm
trying to compress multiple (appended) entries into a single file with
PGP and unless PGP is compressing from an actual file, all the
compressed "file names" PGP stores end up being named 'STDIN'.  I can't
write out a temporary *REAL* file since I don't want to store sensitive
data unencrypted even temporarily on a remote server that hosts my
site.  I do, however, need to name the files something descriptive since
I need to retrieve the single file (with multiple entries) and
decompress its contents without having all the entries overwrite
themselves (since they'd all be named 'STDIN').

I also would like to make this work in an NT environment for testing
(although I'm not sure what the equivalent of 'mnknod' or 'mkfifo' is on
NT) -- having this run on NT would be very helpful.

Here's what I have so far (a lot of nested if statements...sorry):
[I also attached the script...]

---- start code ----
#!/usr/bin/local/perl -w

chdir;
$FIFO = "$ENV{DOCUMENT_ROOT}/temp/tempfile.tmp";
$ENV{PATH} .= ":/etc:/sbin:$ENV{DOCUMENT_ROOT}/temp";
$data2enc = "This is the text we need to encrypt...grabbed from STDIN.";

$temp_encrypt_file = "$ENV{DOCUMENT_ROOT}/temp/temp_enc.tmp"; # need
this, since PGP won't use STDOUT without STDIN
$final_file = "$ENV{DOCUMENT_ROOT}/temp/maindata.txt";

$proc_id = fork;
if ($proc_id == 0) { # CHILD
  while (1) {
        unless (-p $FIFO) {
          unlink $FIFO;
          # I can't use exec here since I want a continuos block on
$FIFO
          system('mknod', $FIFO, 'p') || die "Can't mknod $FIFO: $!\n";
        }
    open (FIFO, "> $FIFO") || die "Can't write to $FIFO: $!\n";
    print FIFO "$data2enc";
    close(FIFO);
    sleep(2); # do I need this?
  }

Quote:
}

elsif (defined $proc_id) { # PARENT
  sleep(4); # is this dependable? - I need to be sure child has done
it's duty first
  # well, let me verify the $FIFO then...
  $waittime = 20;
  $waittime = time + $waittime;
      while (-e $FIFO eq "" && time < $waittime) {
        # do nothing? - just wait...
      }
  $pgp = "$ENV{DOCUMENT_ROOT}/apps/pgpe";

  $pubring = "$ENV{DOCUMENT_ROOT}/apps/pgp/pubring.pkr";
  # whatever the commandline needs to be...
  # will probably fail if $FIFO still doesn't exist after 24+ seconds
(long time...)
  open(PGP,"| $pgp -r $pgpid -aft --pubring=$pubring -o
$temp_encrypt_file $FIFO") || die "PGP failed: $!\n";
  close(PGP);
      if ($proc_id == 0) { # SWITCH TO CHILD AND KILL IT
        exit(0); # will this kill the child and its loop?
        unlink $FIFO;
        # $exitfifo = `rm $FIFO`; # would this be better?
      }
  $died = $pid; # get pid from parent
  $pid = wait; # get pid of child (should now be dead)
      # should I use 'eq' here? result can be (1) or ("")
      if ($pid == $died) { # CHILD IS DEAD & WE'RE BACK IN THE PARENT
         $result = "Child has died.\nFIFO exit results (if used \"rm
\$FIFO\"): $exitfifo\n";
      }
      else {
        $result = "Child lives?.\nFIFO exit results (if used \"rm
\$FIFO\"): $exitfifo\n";
      }
Quote:
}

else {
  die "Fork error: $!\n";

Quote:
}

open(FINALFILE, "<$final_file") || die "Can't open $final_file: $!\n";
open(TEMPFILE, ">>$temp_encrypt_file") || die "Can't open
$temp_encrypt_file: $!\n";
while (<TEMPFILE>) {
  print FINALFILE $_;
Quote:
}

close(FINALFILE);
close(TEMPFILE);
print "Here are the results...\n$result";
exit;
---- end code ----

Sloppy, I know...

Any help would be appreciated.  I think maybe I could reverse the
actions and have the PGP action run in the child, but then I don't know
how I would exit the loop in the parent.  Perhaps a signal would be
better in that instance?  I don't know much about signals...

Also, a tricky part is making sure that the child has created the $FIFO
file & is blocking it before the parent attempts to access it.  Is there
a fool-proof way to do this or is anything I do here a risk?

I keep getting an "internal server error" -- the syntax is OK, so I'm
probably not doing this right.  I don't have direct shell access to the
server (only CGI-BIN access) so it's hard to debug this thing and since
I'm not adept enought yet to port it to NT, I'm kinda stuck.

Oh, yeah, here's the breakdown on the server configuration (just in case
it's helpful):
SGI Origin200/ChallengeS Server
IRIX V6.4
Apache V1.2

Obviously, I'm not running as root.  I don't think my scripts run as
'nobody', I'm pretty sure they run under my user id on the system.
Anyway, like I said above, none of this code is dependent on CGI other
than the way I access/debug it.  That's not the issue.  The issue is how
I'm trying to deal with the process/pipe handling...

--
Jihad Battikha

http://www.*-*-*.com/

  n_pipe.pl
2K Download


Fri, 16 Jun 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Help with named named pipes and ipc!!!

2. Help with named pipes

3. Need Help with Named Pipes Please

4. Named pipes in Perl

5. Named Pipes

6. Atomic named pipe print?

7. Named Pipes and Perl, anyone done this?

8. Named Pipes and Perl, anyone done this?

9. Reading from named pipes

10. multiple processes accessing a named pipe

11. named pipes, and redirection woes

12. Named Pipes

 

 
Powered by phpBB® Forum Software