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

I'm writing a REST application which interacts with an Asterisk server. When I write a test script and run it as sudo root, unlink works fine. However, when I use WWW::Mechanize to run my code across port 80, presumably as www-data, it is able to write a call file to my queue directory with permissions:

drwxrwsr-x 2 www-data www-data 48 2009-09-01 03:18 queue
and then to move that call file to the outbound spool:

drwxrwxr-x 2 root dialer 48 2009-09-01 10:24 outgoing
(www-data is a member of the dialer group.)

That call file is picked up from the spool and used by asterisk (running as root, according to `ps`) to initiate an outbound call and connect it to an extension in my dialplan. The dialplan includes an AGI() call to a script (again running as root if my debug statements for $< and $> are to be believed), which initiates additional calls, staged in the pending queue, and successfully moved to the spool.

The issue arises when one of those calls is successfully connected and bridged to the first. At that point, any still pending call files are supposed to be moved aside before they also get moved to the outbound spool. I've tried unlink "$queue/$call_file" and rename "$queue/$call_file", "$queue/pending/$call_file", and neither seems to work as advertised. Neither seems to be able to successfully remove or move aside those pending call files and the calls keep coming.

Based on speculation last night in a cb conversation, I experimented unsuccessfully with adding the --no-lock option to File::Util->write_file() call used to write the call file originally, hoping that releasing it in that way might free it up for the next process to remove the file. But that did not work either.

Does anyone have any idea what might be missing here? I was at this issue for an hour or more last night without being able to move it forward.

-- Hugh


Thank you folks. I had been using a foreach to loop over the files returned by a call to File::Utils->list_dir(). The ->list_dir() was looking in the wrong directory, an issue pointed to by an empty debug log when I started looking for the $! output. Thank you kindly.

if( $lal && $lol ) { $life++; }
if( $insurance->rationing() ) { $people->die(); }

Replies are listed 'Best First'.
Re: root unable to unlink files
by ig (Vicar) on Sep 01, 2009 at 15:13 UTC

    A program running as www-data can't easily create a file in the outbound spool owned by root.dialer. There may be some error in your assumption about the web server process or which process is creating the file in the outbound spool.

    When unlink and rename fail, they return error indications. What is the return value and what is $! set to immediately after the calls to these functions? What are the values of "$queue/$call_file" when these functions are called?

    I usually call these functions as follows.

    unlink($path) or die "unlink: $path: $!"; rename($oldpath,$newpath) or die "rename($oldpath,$newpath): $!";

    Can you do similarly? Are errors reported?

    update: What makes you think that unlink and rename were executed?

    Maybe you can call them as follows:

    unlink($path) or die "unlink($path): $!"; warn "unlike($path) succeeded"; rename($oldpath,$newpath) or die "rename($oldpath,$newpath): $!"; warn "rename($oldpath,$newpath) succeeded";

    Then check your error output: you should have either an error indication that specifies the cause of the error or a positive indication that the function call was successful.

Re: root unable to unlink files
by leocharre (Priest) on Sep 01, 2009 at 14:59 UTC

    Also .. you mention " I've tried unlink "$queue/$call_file" and rename "$queue/$call_file", "$queue/pending/$call_file", and neither seems to work as advertised." ...

    Just how does it fail? Any error messages? Anything in $! ? Have you tried -f "$queue/$call_file" before you continue.. just to be extra super duper paranoid?

    Have any code example where you try to delete something and it fails?

    Why do you think it failed? Because afterwards things are not where they're supposed to be? Because.. yeah.. need to see some code.

Re: root unable to unlink files
by leocharre (Priest) on Sep 01, 2009 at 14:56 UTC
    I can think of one thing... The partition is corrupt. You are writing to it, and the os is changing/remounting it to read only. Check 'dmesg'