Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Why aren't system() calls working?

by hotyopa (Scribe)
on Dec 15, 2000 at 11:35 UTC ( [id://46804]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,

As an addendum to this question:

Writing a file as someone else

I have written the following admittedly dangerous script:

#!/usr/bin/perl use CGI; $q = new CGI; $directory = '/home/httpd/html/antonh/testdir'; print $q->header, $q->start_html, "Removing $directory<br>"; $result = system("rm -r $directory"); print -W $directory,",", -o $directory,",",$result;
This is the result I get on the screen:

Removing /home/httpd/html/antonh/testdir
1,1,256

This script does work from the command line, but for some reason the system call is failing. I had similar problems trying to do a chmod in the same way.

Any ideas why this might be the case?

Replies are listed 'Best First'.
Re: Why aren't system() calls working?
by ChOas (Curate) on Dec 15, 2000 at 11:46 UTC
    Hey...

    The return of the system call is in the upper 8 bits of
    the 16 returned, so you might be better off with something like:
    system("rm -r $directory"); $result=$? >> 8;
    and *cough* running with strict, -w and -T *cough*
    *double cough* you could have tried perldoc -f system *double cough*

    GreetZ!,
      ChOas

    :wq

    And I should keep my mouth shut, coz I didn't answer the REAL question,
    sorry... you might wanna check the permissions of files/directory/parent dir
    rm -rf might force it
Re: Why aren't system() calls working?
by chipmunk (Parson) on Dec 15, 2000 at 20:15 UTC
    I wonder why you are using system() and not Perl's builtin rmdir() function.
    $result = rmdir($directory); if ($result) { print "Directory '$directory' removed.\n"; } else { print "Directory '$directory' not removed: $!\n"; }
    Or, if you need to do a recursive rm, you can use File::Path's rmtree method.

    If your code works from the command line but not via the web server, there must be some difference between the users or the environments. Note that, in order to delete a directory, you must have write access to that directory and its parent directory.

Re: Why aren't system() calls working?
by eg (Friar) on Dec 15, 2000 at 11:47 UTC

    My guess is that the user your web-server is running as (nobody or www-data, most likely) doesn't have the proper permissions to do anything to your test directory. If you chmod 777 it from your command line, then the script ought to work from a browser.

    Note, perl itself has a rmdir command, although you'd have to clear out the directory first (using File::Find or something.)


    Huh? I thought you said the script ran fine from the command line, therefore I would hope that you somehow have permissions to play around with the test directory.

    Nevertheless, a second thing you might check is to make sure that rm is in the script's path. Better yet, use a full path in your system call.

    Have you checked the server's error log? What (if anything) does it say?

      As per the earlier question, I can't chmod it from the command line because I'm not the owner and don't have root access...

      Also, the UID and GID that the script are running under are the owners of the directory and all files within.

Re: Why aren't system() calls working?
by Fastolfe (Vicar) on Dec 15, 2000 at 20:39 UTC
    As another poster mentioned, you're not quite fetching the error status correctly. As it is now, if $result is non-zero, it's indicating a failure (counter to the usual behavior). In addition, your server's error logs should contain a textual description of why the 'rm' command failed (as it would have printed its message to STDERR, which would have gone to the server's error logs). While you're debugging, you may want to make use of CGI::Carp to send your errors straight to the browser:
    use CGI::Carp 'fatalsToBrowser';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://46804]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found