Page 1 of 1

PHP unable to access directory

Posted: Wed Jun 16, 2010 3:19 pm
by ghollins
Hi,

I'm having an issue where a PHP page running under apache alters destructively a directory's permissions, causing future attempts to access the directory to fail. A very simple script that lists a directory's contents seems to cause this:

Code: Select all

<?php
  $cmd = "ls -la /Volumes/ 2>&1";
  exec($cmd, $execOut, $exitCode);
  echo "<PRE>EXIT CODE = $exitCode<BR/>";
  foreach($execOut as $line) {
    echo "$line <BR/>";
  }
  echo "</PRE>";
?>
When I hit this page it usually succeeds, but it sometimes (randomly) produces this output:

Code: Select all

EXIT CODE = 1
ls: myshare: Permission denied 
total 8 
drwxrwxrwt@  4 root  admin   136 Jun 14 12:34 . 
drwxrwxr-t  30 root  admin  1088 Jun  4 13:09 .. 
lrwxr-xr-x   1 root  admin     1 Jun 11 16:05 galenhd -> /
When it succeeds it looks like:

Code: Select all

EXIT CODE = 0
total 40 
drwxrwxrwt@  4 root   admin    136 Jun 14 12:34 . 
drwxrwxr-t  30 root   admin   1088 Jun  4 13:09 .. 
drwx------   1 galen  staff  16384 Jun 14 09:28 myshare 
lrwxr-xr-x   1 root   admin      1 Jun 11 16:05 galenhd -> /
There should be no reason why consecutive hits of the same page should do something functionally different.
I'm pretty confident this bug/issue involves Apache to some degree, because when I take Apache out of the equation, for example by running PHP standalone, I don't see any problems.

OTHER DETAILS:
-- Running on mac Snow Leopard
-- using out-of-the-box Apache and PHP on mac
-- mounted myshare directory via Finder (i.e. smb://myserver/myshare via Go-->Connect to Server... menu)
-- If command is changed to remove "-l" option, it doesn't cause failure
-- SMB server is Windows 2003, although I've also tested this on a linux SMB server, and it also fails, so I don't think the server is at fault
-- Running the same PHP script from the command-line doesn't cause problem -- it only fails when run under Apache
-- Running "sudo ls -la /Volumes" repairs permissions on directory
-- Failure has been occurring for me roughly 1 out of every 20 page hits, but in a random fashion

Surely someone has run into this seemingly simple use case?

Thanks,
Galen

Re: Directory permissions changed when executing ls command?

Posted: Wed Jun 16, 2010 3:57 pm
by requinix
I don't see what was "altered destructively": ., .., and galenhd all have the same ownership and permission bits set in both of those outputs.

The error is probably about ls trying to stat that myshare directory. I think it's more of a problem with the mounting than with anything else - occasionally the share can't be located and thus ls can't get information about it. Often a "permission denied" error means merely "I'm pretty sure I should be able to do this but for one reason or another I can't".

Re: Directory permissions changed when executing ls command?

Posted: Wed Jun 16, 2010 4:22 pm
by ghollins
tasairis wrote:I don't see what was "altered destructively": ., .., and galenhd all have the same ownership and permission bits set in both of those outputs.

The error is probably about ls trying to stat that myshare directory. I think it's more of a problem with the mounting than with anything else - occasionally the share can't be located and thus ls can't get information about it. Often a "permission denied" error means merely "I'm pretty sure I should be able to do this but for one reason or another I can't".
Yes, you might be right about stat failing. What I meant by "destructively" is that something changes about the "myshare" directory when apache/mysql attempt to "ls -la" it, that causes failure to read/see the directory. Nothing bad seems to ever happen to galenhd, so you can ignore that directory. The question I have is "What is it about PHP under Apache that causes stat (or whatever it is) to not be able to read/see the directory?". I mean running the exact same PHP code from the command line doesn't result in this behavior. It looks like it's something to do with the Apache environment, or PHP is somehow conditionally calling a different set of lower-level code when running under apache. Or perhaps it has something to do with it running as the _www user??...

SOME MORE INFO:

When I substitute "file" instead of "ls -la" in the above code,
the error looks like:

Code: Select all

EXIT CODE (web) = 0
/Volumes/myshare: cannot open `/Volumes/myshare' (Permission denied)
When I substitute "stat" instead of "ls -la" in the above code,
the error looks like:

Code: Select all

EXIT CODE (web) = 0
stat: /Volumes/myshare: stat: Permission denied