in reply to printing fcntl( fd, F_GETFL,0 ) return value changes its representation

When I do "perldoc -f fcntl" I see:

use Fcntl; fcntl($filehandle, F_GETFL, $packed_return_buffer) or die "can't fcntl F_GETFL: $!";
which shows that F_GETFL "gets" the value into the third argument. I think that when you do:
$cur_opts = fcntl( FH, &F_GETFL, 0 );
Perl passes the address of the constant string, "0", to fcntl(). But fcntl() was expecting the address of a properly sized buffer for it to write the "FL" that you wanted to "GET" into. So fcntl() overwrites the 'constant' string, "0", which is stuff that Perl wasn't expected to be overwritten.

The design of fcntl() (Unix design, not Perl design) requires that the caller know what types are argument(s) to pass for each command code and doesn't provie any good ways to check/enforce that. You didn't obey that rule. This corrupted some data and what Perl does after that could be nearly anything.

                - tye

Replies are listed 'Best First'.
Re: Re: printing fcntl( fd, F_GETFL,0 ) return value changes its representation (usage)
by weinstev (Initiate) on Jun 06, 2003 at 18:00 UTC
    If you look at man 2 fcntl, the actual Unix C function does not require a third argument. However, Fcntl::fcntl does. If you look at perldoc -f ioctl, you will find an example of fcntl using 0 as the third parameter. If you look at my original post, I have verified the validity of the syntax
    $flags = fcntl( FH, F_GETFL, 0 ); $rc = fcntl( FH, F_SETFL, $flags );
    However, what causes the call to fail is if I print the value returned from the F_GETFL call
    $flags = fcntl( FH, F_GETFL, 0 ); print "current flags -> $flags\n"; ## now we've got trouble $rc = fcntl( FH, F_SETFL, $flags );
    I am not sure why the ioctl documentation and fcntl documentation are out of synch, but the third argument is NOT required by the Unix C function; I have verified that, in the absence of the print statement, my call syntax is not the issue. Thanks for your response, feel free to correct my logic as necessary.
    Victor

      I never said your syntax was incorrect. You should not be passing 0 as the third argument for F_GETFL.

      Update: isotope mentioned to me that newer versions of the ioctl POD do exactly as you have done. Well, I get suspicious of an examples in ioclt POD that are for the wrong function, and it didn't look like anyone was trying what I suggested. So I tried the different methods of doing F_GETFL (one from fcntl, one from ioctl) on an ancient Unix system that was handy and the one from ioctl was correct (on that Unix system), but it also didn't exhibit the original problems in this thread. So I suspect that the usage of F_GETFL varies between versions of Unix.

      In any case, one or both of the fcntl/ioctl PODs could use some updates when we figure this out. (:

                      - tye