ashisht has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks,

I came across this node for generation of temporary files, but I was wondering if another safe way to generate unique temporary files for CGI scripts would be to try to use the user's host name and process id to name the file.

Is there a way to obtain either the process id or the hostname through a Perl-CGI script? I would also appreciate any advice on whether the idea is workable.

Thanks,
Ashish.

Edit by castaway - swapped a-tag-pm-link for id://

  • Comment on Is it possible to generate unique temporary filenames using hostname/process id?

Replies are listed 'Best First'.
Re: Is it possible to generate unique temporary filenames using hostname/process id?
by tachyon (Chancellor) on Mar 13, 2004 at 02:34 UTC

    See Avoiding race condition with sysopen and the 15 odd line temp file function presented their which is the distilled guts of File::Temp (1800 lines). It also presents some args for not using File::Temp - provided you know what you are doing. Just using $$ or functions you roll yourself (unless you are an uber hacker) is quite possibly a recipe for disaster.

    cheers

    tachyon

Re: Is it possible to generate unique temporary filenames using hostname/process id?
by Happy-the-monk (Canon) on Mar 13, 2004 at 01:12 UTC

    $$ - see perldoc perlvar

    hostname is not a builtin - see perldoc -q hostname

    the combination of hostname and process id is however not a safe way to generate unique strings in general:
    One process will not change it's process id, but some former process will have had the same id before. Also a potential attacker could reasonably succesfully guess a future process id and create/manipulate such a file.

    Edit:
    There's this module on CPAN: File::Temp return name and handle of a temporary file safely

    Sören

Re: Is it possible to generate unique temporary filenames using hostname/process id?
by pbeckingham (Parson) on Mar 13, 2004 at 01:17 UTC
    The process id is available with the $$ variable, but you really should use File::Temp.
Re: Is it possible to generate unique temporary filenames using hostname/process id?
by bageler (Hermit) on Mar 13, 2004 at 02:25 UTC
    $$ and a microtime might be better, but still doesn't 100% ensure uniqueness.
      Ok, I know I run the risk of looking like a dumbass here but (assuming a Unix platform) what's wrong with $$ . time()? Is the risk that so many processes are created within one second that the PIDs roll over?
        Stick a long random number on the end and your odds are pretty good. Of course, there's no reason not to look for a name collision to guarantee it.
Re: Is it possible to generate unique temporary filenames using hostname/process id?
by simonm (Vicar) on Mar 13, 2004 at 19:03 UTC
    As an alternative, you could use Data::UUID (or the older UUID module]:
    use Data::UUID; my $uuid_gen = Data::UUID->new(); ... my $unique = $uuid_gen->create_str();

    The generated strings are 68 characters long, containing letters, numbers and hyphens, and essentially guaranteed to be unique.

Re: Is it possible to generate unique temporary filenames using hostname/process id?
by jockel (Beadle) on Mar 16, 2004 at 15:47 UTC

    Hi Ashish

    Just a thought.. if you are using mod_perl, the pid number
    will be the same for a "number of exections" (depending on how you've
    configured apache) or until apache is restarted.

    regards
    /jocke