Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fcntl syscalls #1893

Merged
merged 7 commits into from
Jan 31, 2025
Merged

Implement fcntl syscalls #1893

merged 7 commits into from
Jan 31, 2025

Conversation

BCSharp
Copy link
Member

@BCSharp BCSharp commented Jan 29, 2025

This PR implements the main fcntl, plus flock and lockf and enables test_fcntl from the StdLib. ioctl remains still a mock.

The signature of syscal fcntl is variadic, but luckily this call is supported by Mono.Unix. It means that there is the extra translation step from the given fcntl command code to Mono's enum, and then back to the actual OS code. This step will also filter out (as unsupported) all commands that were not defined yet by the OS at the time Mono.Unix was built. For one thing, I miss F_GETPATH, which was added in Python 3.9. It is only on macOS, but on Linux one can get the path name from a descriptor by readlink /dev/fd/«fileno». Well, maybe I will write a workaround for this one at some point.

The calls flock and lockf are not supported by Mono.Unix, but their signature is simple enough that the P/Invoke prototypes are not a big deal. With them in place it is finally be possible to fix SQLite on macOS (and improve on Linux).

}

int argSize = arg.Count;
IntPtr ptr = Marshal.AllocHGlobal(argSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AllocHGlobal is one of those methods .NET doesn't want us using. There's a blurb about it in the docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really necessary. CPython uses 1024 bytes on the stack. I could do the same with stackalloc, but it looks to me like a lot of space to put on the stack. Or simply pin a byte array.

@@ -27,6 +35,91 @@ a file object.
""";


#region fcntl

[LightThrowing]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the light throwing path expected to occur in normal use? If not then I would consider making a regular exception.

Similar question for the other light throwing methods below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the command but yes, sometimes. Those errors are very inexpensive to return so the usage pattern for some commands is simply try/error. For example handling errors EAGAIN, EWOULDBLOCK., EACCES. Having LightThrowing makes it relatively inexpensive here too. Nota bene the code still throws regular exceptions for programming errors like invalid value. That's intentional so that the errors do not go unnoticed when these calls are done from C#.


#region flock

[DllImport("libc", SetLastError = true, EntryPoint = "flock")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder what happens on Alpine. 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will check 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works! However, Mono.Unix doesn't load, missing dependency ld-linux-aarch64.so.1, which makes ipy pretty unusable. Even when I installed extra package gcompat (GNU C Library compatibility layer for musl, providing ld), Mono.Unix was still failing with __sprintf_chk: symbol not found.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my comment was more of a joke, but still unfortunate that Mono.Unix doesn't work on it. 😢

@slozier
Copy link
Contributor

slozier commented Jan 30, 2025

For whatever reason github mobile app wouldn't let me add a comment along with my review. My points are just technicalities and not blockers on the PR. I did not hit the approve button because tests are failing on Windows for whatever reason.

@BCSharp
Copy link
Member Author

BCSharp commented Jan 30, 2025

The tests are failing not because of the fcntl implementation per se. Perhaps the failure is related to #245. In any case, this PR cannot be merged as is as long as the tests are failing. Since I can't fix it on a dime, I will likely just submit a workaround disabling the test on Windows.

@BCSharp BCSharp marked this pull request as draft January 30, 2025 05:40
@BCSharp
Copy link
Member Author

BCSharp commented Jan 30, 2025

This seems not to be an IronPython-specific problem, I am getting the same behaviour with CPython 3.4.4. Skipping the test on Windows looks like the right choice.

@BCSharp
Copy link
Member Author

BCSharp commented Jan 30, 2025

It is an IronPython-specific problem after all. The test should be skipped if it raises unittest.SkipTest exception. This is happening in several tests so perhaps it's best to fix it in CaseExecuter. I will open another PR for that.

@BCSharp BCSharp marked this pull request as ready for review January 31, 2025 06:30
Copy link
Contributor

@slozier slozier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@BCSharp BCSharp merged commit e7a937d into IronLanguages:main Jan 31, 2025
8 checks passed
@BCSharp BCSharp deleted the fcntl_syscall branch January 31, 2025 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants