in reply to Re^4: fcntl failure after eval
in thread fcntl failure after eval

Many thanks for the responses. I'm not super-confident with pack or C... My man fcntl says:

struct flock { ... short l_type; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK */ short l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_start; /* Starting offset for lock */ off_t l_len; /* Number of bytes to lock */ pid_t l_pid; /* PID of process blocking our lock (F_GETLK only) */ ... };

So you're saying that the size of off_t, which appears from /usr/include to be a long, depends on the system I'm running on, and the only way to be sure is to compile a program which includes the definition of this type, and see what size it is in the result.

Does perl not know what this type is, as it's been compiled for the system on which it's running? If not (or there's no way for my script to get the information from the interpreter), I guess there's no portable pure perl solution? That's a bit of a pain - I need to lock files on NFS, flock doesn't work there and manually managing lockfiles seems a bit of a crummy solution.

On my systems, doing or not doing the eval consistenly makes the program work or not work - is this just some fluke side effect of uninitialised memory?

Thanks again,

Pete

Replies are listed 'Best First'.
Re^6: fcntl failure after eval
by Anonymous Monk on Aug 12, 2011 at 10:54 UTC

    Does perl not know what this type is, as it's been compiled for the system on which it's running? If not (or there's no way for my script to get the information from the interpreter),

    $ perl -V:ldflags ldflags='-s -L"c:\perl\5.14.1\lib\MSWin32-x86-multi-thread\CORE" -L"C: +\MinGW\lib"'; $ perl -V:archlib archlib='c:\perl\5.14.1\lib\MSWin32-x86-multi-thread'; $ grep -ri "define off_t " C:\perl\5.14.1\lib\MSWin32-x86-multi-thread +\CORE C:\perl\5.14.1\lib\MSWin32-x86-multi-thread\CORE/config.h:#define Off_ +t long long /* <offset> type */ C:\perl\5.14.1\lib\MSWin32-x86-multi-thread\CORE/perl.h:# define + Off_t off64_t C:\perl\5.14.1\lib\MSWin32-x86-multi-thread\CORE/uconfig.h:#define Off +_t int /* <offset> type */
    You want to read $Config::Config{archlib}/CORE/config.h
    /* Off_t: * This symbol holds the type used to declare offsets in the kernel +. * It can be int, long, off_t, etc... It may be necessary to includ +e * <sys/types.h> to get any typedef'ed information. */ /* LSEEKSIZE: * This symbol holds the number of bytes used by the Off_t. */ /* Off_t_size: * This symbol holds the number of bytes used by the Off_t. */ #define Off_t long long /* <offset> type */ #define LSEEKSIZE 8 /* <offset> size */ #define Off_t_size 8 /* <offset> size */
    $ perl "-V:.+?seek.+?" d_fseeko='undef'; d_lseekproto='define'; d_seekdir='define'; lseeksize='8'; lseektype='long long';
    See also Convert::Binary::C/ccconfig
Re^6: fcntl failure after eval
by ikegami (Patriarch) on Aug 12, 2011 at 19:22 UTC

    man fcntl says the even the order of the fields if system specific. Padding between the fields is compiler and system specific. The size of some of these fields is system specific.

    Does perl not know what this type is

    Perl surely does #include <fcntl.h>, so it does know.

    I guess there's no portable pure perl solution?

    fcntl itself is not portable.

    Aside from that, it's most definitely possible.

    You're asking for a Pure Perl way of getting the structure your C compiler would created from the info the system provides in fcntl.h.

    Perl (via Config) does provide the info you need to launch the C compiler using system, although Inline::C will simplify the process.

    This could even be done by your installer.

    You could even do this at development time. Perl (via Config) does specify which compiler and system it's running on, so you could look up the right structure at run-time.

    That's a bit of a pain

    That the system specifies its input as compiler-specific C code? yeah.

      So the reason you don't hit this problem with most system/library calls is that they take simple datatypes rather than structs?

      If so, how come this isn't an issue with readdir()? The manpage says that needs a structure also...
        The argument of the readdir system call is a file descriptor, and Perl passes that to it. The last argument of fcntl system call is a pointer to a series of bytes of no defined format. Perl allows you to pass that to it.
Re^6: fcntl failure after eval
by flipper (Beadle) on Aug 12, 2011 at 10:49 UTC
    I've just found File::NFSLock on CPAN - it seems to use manual lockfiles, you have to specify timeouts for stale locks - not a great solution.