Bug 2014 - Locking file with fcntl fails on OSX when invoked through Mono.Unix
Summary: Locking file with fcntl fails on OSX when invoked through Mono.Unix
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: Mono.POSIX (show other bugs)
Version: 2.10.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2011-11-13 03:43 UTC by Kenneth
Modified: 2012-06-26 06:13 UTC (History)
2 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description Kenneth 2011-11-13 03:43:14 UTC
The fcntl() command with F_SETLK fails with ENOTTY on OSX when using Mono.Unix.Native.Syscall.fcntl() but works when invoked from C code.

The Mono.Unix.Native.Syscall.fcntl() call works fine on Linux (Debian at least).

I have made two files which I believe are equivalent using C and C#.
The output from the C program is:
fd: 3
Res 0, errno: 0

The output from the C# program is: 
fd 3
Res -1, errno: ENOTTY

Source code for C program:
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>

int main() {

    int fd = open("test", O_CREAT | O_RDWR, 666);
    printf("fd: %d\n", fd);

    struct flock fl;
    fl.l_type = F_WRLCK;
    fl.l_start = 0;
    fl.l_whence = SEEK_SET;
    fl.l_len = 0;
    fl.l_pid = getpid();

    int res = fcntl(fd, F_SETLK, &fl);
    printf("Res %d, errno: %d\n", res, errno);


Source code for C# program:
using System;
using Mono.Unix.Native;

public static class Test 
	public static void Main()
			Flock lck;
			lck.l_len = 0;
			lck.l_pid = Syscall.getpid();
			lck.l_start = 0;
			lck.l_type = LockType.F_WRLCK;
			lck.l_whence = SeekFlags.SEEK_SET;
			int fd = Syscall.open("test", OpenFlags.O_CREAT | OpenFlags.O_RDWR, FilePermissions.DEFFILEMODE);
			Console.WriteLine("fd {0}", fd);

			int res = Syscall.fcntl(fd, FcntlCommand.F_SETLK, ref lck);
			Console.WriteLine("Res {0}, errno: {1}", res, Syscall.GetLastError());
Comment 1 Robert Wilkens 2012-06-25 15:55:25 UTC
Note: When I run the csharp code on Linux, Ubuntu 12.04, mono I get an error as well:

fd 3
Res -1, errno: EINVAL

On a mac, I get similar failures to what you report.  I half-wonder if they're incarnations of the same problem.
Comment 2 Robert Wilkens 2012-06-25 17:38:30 UTC
I figured out, i believe, why it's failing on _64 bit_ linux, and if you have a 64-bit mac and 32-bit linux you're testing on that may explain it.

In "x86_64-linux-gnu/bits/fcntl.h" in /usr/include, F_SETLK has several variants..

The relevant things are:

F_SETLK is 6 under the condition where your C version is written...

But when it's run in mono, F_SETLK is using a variant where F_SETLK is F_SETLK64 which is "13" (but i believe that is only supposed to be the case on 32-bit systems).

If I set your C program to pass directly "13" in place of F_SETLK it fails.
Comment 3 Robert Wilkens 2012-06-25 19:09:10 UTC
Just for the record, while 64-bit linux the value should be 6, on 64-bit mac the value, i believe, should be 8.  But it's hard-coded to be 13.  This needs to be made more platform specific (The FcntlCommand enum in Syscall.c under Mono.Posix/Mono.Unix.Native).
Comment 4 Robert Wilkens 2012-06-25 20:23:59 UTC
Submitted patch:

No guarantee it will be accepted, but it's simple enough and it should work.  I tested on 64-bit linux and Mac OSX Lion.
Comment 5 Robert Wilkens 2012-06-26 06:13:39 UTC
I'm not sure which version the fixes will be in, but in the latest github source, the patch has now been merged in.

Note You need to log in before you can comment on or make changes to this bug.