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

Hi all, I have some code I'm porting to mod_perl2, running on ubuntu jaunty. The code is below. In a request handler, I'm simply opening a file, writing a little data, closing it. Simplicity itself. This code ran for years and years on mod_perl1. I ported it unchanged (this snippet, unchanged -- I made all the appropriate mod_perl2 conversions), and now running it causes intermittent "inappropriate ioctl for device" errors. I read somewhere that this may have to do with mod_perl2 needing to retie filehandles, but that would seem like a big change from mp1. Any thoughts?
my $file = "/tmp/foobar.txt"; #dir perms 777 open (FILE, ">$file") or die "ERROR: can't create $file: $!\n"; print FILE "the quick brown fox, etc.\n" or die "ERROR: $!\n"; close FILE or die "ERROR: closing $file $!\n";

Replies are listed 'Best First'.
Re: The old inappropriate ioctl bugaboo again, in mod_perl2
by almut (Canon) on Apr 27, 2010 at 21:59 UTC

    Maybe the print is intermittently failing for some reason...  I think print doesn't necessarily set $! when returning false, so when you effectively execute the following code (in case of error), you'd get

    my $file = "/tmp/foobar.txt"; #dir perms 777 open (FILE, ">$file") or die "ERROR: can't create $file: $!\n"; 0 or die "ERROR: $!\n"; # <--- dies with "ERROR: Inappropriate ioc +tl for device"

    In other words, what you see might be just the last errno that occurred, which is the regular (nothing-to-worry-about) ENOTTY from the ioctl belonging to the preceding open:

    $ strace ./837195.pl ... open("/tmp/foobar.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fffa2ebce90) = -1 ENOTTY (I +nappropriate ioctl for device) lseek(3, 0, SEEK_CUR) = 0 fstat(3, {st_mode=S_IFREG|0600, st_size=0, ...}) = 0 fcntl(3, F_SETFD, FD_CLOEXEC) = 0 write(3, "the quick brown fox, etc.\n", 26) = 26 close(3) = 0 exit_group(0) = ?

    Of course, you'd still have to find the cause for the failing print, but it might have nothing to do with ioctl...

Re: The old inappropriate ioctl bugaboo again, in mod_perl2
by Corion (Patriarch) on Apr 27, 2010 at 21:24 UTC

    Personally, I would try lexical filehandles instead of global filehandles, especially as you don't seem to be localizing FILE. If circumstances collude, you might or might not close the FILE filehandle for some other routine while you're doing your logging.

    Alternatively, maybe it's a hardware problem and the device in question is (going) bad and has intermittent errors?

Re: The old inappropriate ioctl bugaboo again, in mod_perl2
by ikegami (Patriarch) on Apr 27, 2010 at 21:26 UTC

    it causes intermittent "inappropriate ioctl for device" errors.

    Perl regularly makes calls that return that error, but that's not a problem. Do you mean "it dies with intermittent 'inappropriate ioctl for device' errors."?

Re: The old inappropriate ioctl bugaboo again, in mod_perl2
by swartz (Beadle) on May 12, 2010 at 22:43 UTC
    Might be related to the longstanding Apache/mod_perl 2 bug in which it closes STDOUT but doesn't reopen it. See the thread at http://marc.info/?l=apache-modperl&m=126296015910250&w=2