Dear Monks. I developped a simple script in Perl that let's me verify if USB key is mounted or not and eventually unmount it.

I am not sure if problem is related to programming or configuration...

Basic issue is: When I execute those scripts in terminal using `Perl` it works perfectly fine, althou when I do it in the browser using `mod_perl` it shows some pretty odd behaviour.

Sources

panelmin.pl

This program simply outputs a message that indicates if the system device `/dev/sda1` is mounted in the system.

#!/usr/bin/perl use strict; use warnings; print "Content-type:text/html\n\n"; print "<html><head><title>USB test</title>"; print "</head><body>"; my $mounted = `df -h | grep /dev/sda1`; if ($mounted eq '') { print '<h1>USB device not connected</h1>'; print $mounted; } else { print '<h1>Device is connected</h1>'; } print '</body></html>';

umount.pl

This program umounts the `/dev/sda1` device from the system.

#!/usr/bin/perl print "Content-type:text/html\n\n"; print "<html><head><title>Umount</title></head><body>"; system("sudo", "umount", "/dev/sda1"); print "</body></html>";

Test scenario

1. Manually mounting the device `/dev/sda1`, (it is declared in `fstab`):

$ mount -a

Making sure the device is mounted in the system:

$ mount /dev/sda1 on /mnt/usbstick type vfat (rw,relatime,uid=1000,gid=100 +0,fmask=0137,dmask=0027,codepage=437,iocharset=ascii,shortname=mixed, +utf8,errors=remount-ro)

2. Executing `panelmin.pl` in the web browser (I will use curl for the purpose of clean output):

$ curl http://localhost/cgi-bin/admin/Q/panel/panelmin.pl <html><head><title>USB test</title></head><body><h1>Device is conn +ected</h1></body></html>

As we can see, the output is correct. It detected the through linux command `df -h` that the device `/dev/sda1` is mounted in the system.

2. Executing `umount.pl` in the web browser in order to umount the device:

$ curl http://localhost/cgi-bin/admin/Q/panel/umount.pl <html><head><title>Umount</title></head><body></body></html>

3. Verifying if the device is umounted using both `panelmin.pl` script in the web browser and linux command line.

$ curl http://localhost/cgi-bin/admin/Q/panel/panelmin.pl <html><head><title>USB test</title></head><body><h1>USB device not + connected</h1></body></html>

Seems to be correct, but let's verify it manually with `df -h` command:

$ df -h | grep /dev/sda1 /dev/sda1 15G 366M 15G 3% /mnt/usbstick

As we can see the device is still mounted in the system.

4. Let's retry the whole process but this time instead of executing scripts in the browser we will launch them manually with `Perl` in the terminal. First lets umount the device. This will also show that the user is in sudoers and script can umount it.

$ sudo umount /dev/sda1 $ df -h | grep /dev/sda1

Let's repeat the process.

$ mount -a (as superuser) $ df -h | grep /dev/sda1 /dev/sda1 15G 366M 15G 3% /mnt/usbstick

And finally the test:

$ perl panelmin.pl Content-type:text/html <html><head><title>USB test</title></head><body><h1>Device is conn +ected</h1></body>
$ perl umount.pl Content-type:text/html <html><head><title>Umount</title></head><body></body></html>
$ perl panelmin.pl Content-type:text/html <html><head><title>USB test</title></head><body><h1>USB device not + connected</h1></body></html>
$ df -h | grep /dev/sda1
Now `df -h | grep /dev/sda1` returned empty string, it has proven that `umount.pl` managed to umount the device from the system, but only if executed in the shell with `Perl`.

Attempts to solve the issue

Interpretation

The fact it works with `Perl` and not with `mod_perl` is not that surprising, what I find the most strange is with `mod_perl` the script `panelmin.pl` seems to works fine with it's `system()` launch and suddenly after executing `umount.pl` it does not work correctly anymore. For now I am out of ideas, I find this behaviour odd and I count on you guys. I hope someone knows what should I do. Thanks.


In reply to mod_perl odd behaviour with system() or exec() call or backticks by pucek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.