Sponsored Content
Top Forums Shell Programming and Scripting File Locking with fcntl on Darwin Mac OSX Post 302578027 by flagman5 on Wednesday 30th of November 2011 02:18:08 PM
Old 11-30-2011
File Locking with fcntl on Darwin Mac OSX

Hello

I have a Perl script that works on non-darwin Mac OS X environments and I think I have narrowed down the issue to a file locking problem.

In other linux environments, the flock struct is defined differently. I have adjusted this via the reference for Mac OS X fcntl(2) man page. The struct is defined as:
Code:
struct flock {
                 off_t       l_start;    /* starting offset */
                 off_t       l_len;      /* len = 0 means until end of file */
                 pid_t       l_pid;      /* lock owner */
                 short       l_type;     /* lock type: read/write, etc. */
                 short       l_whence;   /* type of l_start */
             };

I am using the flock as the following:

Code:
my $msg = fcntl( $LOCKF, F_SETLK, pack("lliss",0,0,0,F_WRLCK,0) ) or die "Something wrong: $!";

The adjustment works on a separate test script that I extracted parts of the script out for testing. However when I deploy it to the real script, it doesnt work anymore. I then did a trial-error sequence and found that after certain 'additions' to the code, anything I add, such as a simple print "hello" statement, would result in the fcntl line not working. (The error outputted is Invalid Argument supplied).

Is it because the offset, len, l_whence are not defined correctly?

Please help. Thanks for your help.
 

10 More Discussions You Might Find Interesting

1. Cybersecurity

ssh and Mac OSX

Please help if you are familiar with Mac OSX. I downloaded OpenSSH for a newer version of SSH than what comes with OS 10.1. What a mistake! Now every time I try to make a connection to my remote server I get an message that ssh was built against version such and such and I have version such and... (2 Replies)
Discussion started by: glfisfn
2 Replies

2. UNIX Desktop Questions & Answers

Mac Osx.2

I finally broke down and decided to buy a new piece of hardware. I think I made the right decision when I chose an Apple iBook - OSX is incredible! I haven't used a Mac since System7.5, and 10.2 is just blowing me away! Best of all, it's easy to use for people who are not used to Mac, but if I... (5 Replies)
Discussion started by: LivinFree
5 Replies

3. UNIX for Dummies Questions & Answers

Make for Mac OS X (Darwin)

Can anyone tell me where to find "make". I have performed an exhaustive search of my system using find - to no avail. (2 Replies)
Discussion started by: garb
2 Replies

4. Windows & DOS: Issues & Discussions

win-xp/mac-osx

I'm currently looking for an emulation program that would allow me to open and run osx app.s and programs on a windows xp based system. if not is there a unix/linux/lindows program that may do the same? (3 Replies)
Discussion started by: area51nstk
3 Replies

5. Programming

Problem with curses library on Mac OS 10.2 darwin

Hello, I am trying to write a simple program with functions in the ncurses library, on a Mac running OSX 10.2.8, with the compiler and libraries that were included in the Dec 2002 Developer's tools release (the last one that runs on Jaguar, as far as I know). When I try to compile, I get... (2 Replies)
Discussion started by: marks
2 Replies

6. UNIX for Dummies Questions & Answers

Upgrading bash on Darwin (osx)

Hi, I have installed bash 3.2 via darwin ports, however when I try and change the shell i.e. chsh -s /opt/local/bin/bash is says its a non-standard shell? but if i run ./bash i get a new bash prompt with version 3.2? Thanks (3 Replies)
Discussion started by: c19h28O2
3 Replies

7. Filesystems, Disks and Memory

unix executable file problem on MAC OSX ??? please help

I've got this problem. My computers and external hard drives are converting many of my files to a Unix Executable File which has a grey terminal looking icon. I don't understand what is causing this to happen. It is happening to a large number of my image file of different formats and also... (1 Reply)
Discussion started by: chadb
1 Replies

8. OS X (Apple)

Mac OSX kernel

is there anyway of looking at, and if possible, modifying it? (2 Replies)
Discussion started by: cleansing_flame
2 Replies

9. Programming

fcntl works in linux but not in mac os x

Hi, Unless I am missing some serious differences in Mac and linux in terms of C programming, I dont know why this would happen. Please take a look at the following piece of code fragment: bool add_input_to_db(Cons *new_data) { // Set the attributes of the lock struct flock fl =... (3 Replies)
Discussion started by: newhere
3 Replies

10. UNIX for Advanced & Expert Users

UML on MAC OSX

Hey guyz, Is it possible to build user-mode linux kernel on MAC OSX? Please I need a reply asap as I have an assignment that I need to do. Thanks! Adel (1 Reply)
Discussion started by: aje02
1 Replies
FCNTL(2)							System Calls Manual							  FCNTL(2)

NAME
fcntl - miscellaneous file descriptor control functions SYNOPSIS
#include <fcntl.h> int fcntl(int fd, int *cmd, [data]) DESCRIPTION
Fcntl() performs several file descriptor related functions, like duplicating a file descriptor, setting the "close on exec" attribute, etc. The fd argument is the file descriptor to operate on, cmd is the command code of the operation to perform, and data is an optional argument to give or receive parameters. The command codes and other symbols and types are declared in <fcntl.h>. The commands are: fcntl(fd, F_DUPFD, int fd2) Returns a new file descriptor that is a duplicate of file descriptor fd. It shares the same file pointer and the same file status flags, but has separate file descriptor flags that are initially off. The value of the duplicate file descriptor is the first free file descriptor greater than or equal to fd2. fcntl(fd, F_GETFD) Returns the file descriptor flags associated with file descriptor fd. The flags are the "close on exec" flag FD_CLOEXEC that, when set, causes the file descriptor to be closed when the process executes another program. The Minix-vmd specific FD_ASYNCHIO flag marks a file descriptor for asynchronous I/O operation. fcntl(fd, F_SETFD, int flags) Set the file descriptor flags of fd to flags. fcntl(fd, F_GETFL) Return the file status flags and file access modes associated with the file associated with file descriptor fd. The file status flags are O_NONBLOCK (non blocking I/O) and O_APPEND (append mode). The file access modes are O_RDONLY (read-only), O_WRONLY (write-only) and O_RDWR (read-write). These flags are also used in the second argument of open(2). fcntl(fd, F_SETFL, int flags) Set the file status flags of the file referenced by fd to flags. Only O_NONBLOCK and O_APPEND may be changed. Access mode flags are ignored. The next four commands use a parameter of type struct flock that is defined in <fcntl.h> as: struct flock { short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */ short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */ off_t l_start; /* byte offset to start of segment */ off_t l_len; /* length of segment */ pid_t l_pid; /* process id of the locks' owner */ }; This structure describes a segment of a file. L_type is the lock operation performed on the file segment: F_RDLCK to set a read lock, F_WRLCK to set a write lock, and F_UNLCK to remove a lock. Several processes may have a read lock on a segment, but only one process can have a write lock. L_whence tells if the l_start offset must be interpreted from the start of the file (SEEK_SET), the current file posi- tion (SEEK_CUR), or the end of the file (SEEK_END). This is analogous to the third parameter of lseek(2). These SEEK_* symbols are declared in <unistd.h>. L_start is the starting offset of the segment of the file. L_end is the length of the segment. If zero then the segment extends until end of file. L_pid is the process-id of the process currently holding a lock on the segment. It is returned by F_GETLK. fcntl(fd, F_GETLK, struct flock *lkp) Find out if some other process has a lock on a segment of the file associated by file descriptor fd that overlaps with the segment described by the flock structure pointed to by lkp. If the segment is not locked then l_type is set to F_UNLCK. Otherwise an flock structure is returned through lkp that describes the lock held by the other process. L_start is set relative to the start of the file. fcntl(fd, F_SETLK, struct flock *lkp) Register a lock on a segment of the file associated with file descriptor fd. The file segment is described by the struct flock pointed to by lkp. This call returns an error if any part of the segment is already locked. fcntl(fd, F_SETLKW, struct flock *lkp) Register a lock on a segment of the file associated with file descriptor fd. The file segment is described by the struct flock pointed to by lkp. This call blocks waiting for the lock to be released if any part of the segment is already locked. fcntl(fd, F_FREESP, struct flock *lkp) Free a segment of disk space occupied by the file associated with file descriptor fd. The segment is described by the struct flock pointed to by lkp. The file is truncated in length to the byte position indicated by l_start if l_len is zero. If l_len is nonzero then the file keeps its size, but the freed bytes now read as zeros. (Other than sharing the flock structure, this call has nothing to do with locking.) fcntl(fd, F_SEEK, u64_t pos) This Minix-vmd specific call sets the file position of the file associated with file descriptor fd to the byte offset indicated by the 64-bit number pos. This is analogous to the call lseek(fd, pos, SEEK_SET) except that F_SEEK can be used on devices larger than 4 gigabyte. SEE ALSO
open(2), dup(2), lseek(2), ftruncate(3), int64(3). DIAGNOSTICS
Fcntl returns a file descriptor, flags, or 0 to indicate success. On error -1 is returned, with errno set to the appropriate error code. The most notable errors are: EINTR If a blocked F_SETLKW operation is interrupted by a signal that is caught. EAGAIN By F_SETLK if a segment cannot be locked. EBADF A bad file descriptor in general, or an attempt to place a write lock on a file that is not open for writing, etc. ENOLCK No locks available, the file system code has run out of internal table space. AUTHOR
Kees J. Bot (kjb@cs.vu.nl) FCNTL(2)
All times are GMT -4. The time now is 05:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy