Jump to content

system(), exec(), passthru(), backtick operator ????????


jrlooney

Recommended Posts

Ok, so here is what I am trying to do. I have this old Perl script which I cannot get rid of because it is part of a large content management system. It has an image uploader which currently uploads just about anything. I want to take advantage of OSX's "sips" command to resample the image and generate thumbnails. This is just a unix command, so I should be able to use one of the functions in the title above. However it doesn't work.

 

In Perl, there are also system() and bactick operators. So, in the Perl code, right after the image is uploaded, I add a call to a php script:

`/usr/bin/php /Users/php/acuweb_helpers/create_thumb.php $path $thumb_path`;

 

$path is the path to the image

$thumb_path is where I want to write the thumbnail file to

 

Now, the php script looks like this:

 

<?php
$path = $argv[1];
$thumb_path = $argv[2];

exec("/usr/bin/sips --resampleWidth 60 $path --out $thumb_path", $output, $ret);

$stuff = implode(',',$output)."\n";
$string = "/usr/bin/sips --resampleWidth 60 $path --out $thumb_path"." ,returns: ".$ret."\n\n";
$handle = fopen('/Users/php/acuweb_helpers/out.txt', 'a');
fwrite($handle, $string);
fwrite($handle, $stuff);
fclose($handle);
?>

 

All the extra stuff there is just me troubleshooting - I write to a text file, the command that I am passing to exec() so I can make sure it comes out right (which it does, everytime).

 

So, the problem is that the exec() fails. I have checked permissions and they are wide open. I even modifed the command to be like this (to try and force the command to be executed as root user):

sudo -u root -S /usr/bin/sips --resampleWidth 60 $path --out $thumb_path < /etc/root.secret

Link to comment
Share on other sites

Oh, and since I am printing out to that file (just so I can see the full command being sent to exec()), I can take the string from that file, put it in a php script and it works just fine.

 

In other words, if you look at my code above, the command contains variables that need to be interpolated.

 

However if I just run this script, it works fine:

 

<?php
exec("/usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg", $output, $ret);
?>

 

So, apparently I am set to run exec w/o trouble. Note that I have also used the backtick operator and passthru on different occasions. Any other thoughts on ways to troubleshoot? For example, does anyone know the significance of the return error codes for exec()? When I run this it always returns a 6.

Link to comment
Share on other sites

Thank you for replying.

 

Literally what the php script is getting is

 

$path = /Users/aw/www/images/uploads/1.jpg

$thumb_path = /Users/aw/www/images/uploads/thumbs/1.jpg

 

So the resulting command sent to exec:

exec("/usr/bin/sips --resampleWidth 60 $path --out $thumb_path", $output, $ret);

should interpolate to this:

exec("/usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg", $output, $ret);

 

I have read the man page on sips and it does not tell the error codes

http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/sips.1.html

 

And also, like I said if I just take the resulting command, stick it in another php script and run that script (either from browser or command line), it works fine:

<?php
exec("/usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg", $output, $ret);
?>

Link to comment
Share on other sites

Are warnings turned on in Perl?

What if you literally type those paths into the Perl--does it work?

Why not keep the code in Perl? I would use system, although, without knowing the return codes for sips, one cannot properly check for errors.

 

 

Link to comment
Share on other sites

Yeah, I tried just using backtick or system in Perl, but it failed there too. Since it's failing in PHP now too, I guess I might as well remove that link in the chain and just go right to sips from Perl.

 

My thinking is that it's some type of permissions issue between the user that the perl cgi runs as and apache or root. Though I even tried sudoing the command as root like so:

 

`sudo -u root -S /usr/bin/sips --resampleWidth 60 $path --out $thumb_path < /etc/root.secret`;

 

Well, if anyone else has further thoughts, I would appreciate it. Otherwise I'll go back to troubleshooting in Perl.

 

thanks again for your time.

Link to comment
Share on other sites

I don't see anything in the error log related to this script.

 

Yes, I can run other commands. In fact, a few steps before this part, I copy the file from it's temp location to it's live location:

system("/bin/cp -pr $pth $path");

and that works fine.

 

Sips is owned by root. here is the ls:

 

-r-xr-xr-x     1 root  wheel       174616 Aug 20  2006 sips

 

Apache runs as user "www"

Link to comment
Share on other sites

And everything is open for www on the web side? Interesting. I still think it's odd that sips will work manually--it still does, right?--but returns an exit code in Perl, and an undocumented one at that! Have you verified that everything works properly leading up to the sips line in the Perl?

 

When you successfully execute sips at the command line, what status does it return? Try echo $status for csh or echo $? for all others (I think).

Link to comment
Share on other sites

Yeah it's got me totally baffled. :)

 

Ok so I just tried the sips command on the cli, this one to be exact:

 

/usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg

 

Then I did echo $? and got back 0

 

So it's nice and happy on command line.

 

Here is another trick. Remember in my original post I was using a system call in the perl, to call a php script and it was the php script which was calling sips? Well I just ran that php script on the command line and it worked fine. Here's what I did:

 

php create_thumb.php /Users/aw/www/images/uploads/1.jpg /Users/aw/www/images/uploads/thumbs/1.jpg

 

That was when logged in as root. So, now I am really leaning towards it being a conflict between the "www" user (Apache) and root. And in fact, if I try to run that php script from a browser (passing the two paths on the $_GET) it also fails - so again, there it is running as "www". So, I am going to monkey around with the www user and see if i can assign it to the same groups as root, or maybe change onwership on sips itself. I'll let you know what I find.

 

** Although what kills me is I ran it using sudo as root and it did not work.  *pulls out hair*

Link to comment
Share on other sites

well i am at my wit's end. this is nuts. I can do all kinds of other commands via system() but this one tanks. I don't know what else to try. I changed ownership on sips. I added the www to the sudoers list so they can do this command with no problem. I checked all ther permissions on all involved files.

Command line works perfect, through browser/apache does not. >:(

Link to comment
Share on other sites

What does this give you?

 

<pre>
<?php

$path = '/Users/aw/www/images/uploads/thumbs';
$dirs = explode('/', $path);
foreach ($dirs as $dir) {
	$cur_dir .= $dir . '/';
	echo '<b>', $cur_dir, '</b><br/>';
	echo substr(sprintf('%o', fileperms($cur_dir)), -4), '/';
	echo is_executable($cur_dir), '/';
	echo fileowner($cur_dir), '/';
	echo filegroup($cur_dir);
	echo '<br/><br/>';
}

?>
</pre>

Link to comment
Share on other sites

oh and here is another clue. I happened to think about how I had experimented with sips via PHP on my older server. So I went back and found my script on the older machine and ran it - and it works fine through a browser.

 

So now I thinking it could also be version of PHP, because on the old machine I am running 4.3.2 and on the newer machine i am running 5.2.1

 

???

Link to comment
Share on other sites

addition to two posts above (re: older server)

I just checked everything on the older server that I had modified on the new server (apache is running as "www" user, "www" user is NOT in the sudoers list, permissions are same on sips) - and i see nothing glaring at me saying "Hey, here I am!!! here is the difference you are looking for!!"

 

Could it be a change in the php implementation of system()?

Link to comment
Share on other sites

Ok, maybe you can still help me. If I call my php script directly from the browser, then using the PHP builtin functions works. However, if I call it from the Perl script, then the php script fails at:

 

$src = imagecreatefromjpeg("$path");

 

So, here is the code. This one works fine when called from the browser directly as so:

 

http://domain.com/create_thumb.php?path=/Users/aw/www/images/uploads/1.jpg&path2=/Users/aw/www/images/uploads/thumbs/1.jpg

 

<?php
$path = $_GET['path'];
$thumb_path = $_GET['path2'];
$new_width = '60';
$new_height = '60';
list($width, $height) = getimagesize($path);
$src = imagecreatefromjpeg("$path");
$im = imagecreatetruecolor($new_width,$new_height); 
imagecopyresampled($im,$src,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($im, $thumb_path,75);
imagedestroy($im); 
?>

 

 

Now, it fails if from my perl script I call the php script like this:

 

`/usr/bin/php /Users/php/acuweb_helpers/create_thumb.php $path $thumb_path`;

 

 

<?php
$path = $argv[1];
$thumb_path = $argv[2];
$new_width = '60';
$new_height = '60';
list($width, $height) = getimagesize($path);
$src = imagecreatefromjpeg("$path");
$im = imagecreatetruecolor($new_width,$new_height); 
imagecopyresampled($im,$src,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($im, $thumb_path,75);
imagedestroy($im); 
?>

 

And note - I also have the php script writing some stuff to a text file so I can troubleshoot. for instance I did implode(',', $argv) and wrote that string to the text file and it shows that the 2 image paths are coming in correctly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.