How to call "ioctl" in Rust? And interface the Linux "tun" driver - linux-kernel

How can I the function "ioctl" in Rust? Should I find a wrapper for it somewhere? Is there a de-facto wrapper? Or maybe it's already included in the standard Rust library? I've not found it, though.
Or, more generally, I need an interface the Linux "tun" driver.

You can find ioctl in nix, which contains convenience wrapper of *nix functionalities.

Related

What does nanotime do on MacOS?

I am currently trying to understand how nanotime is implemented for MacOS in Go.
On Linux (see link to implementation here), it seems to calls clock_gettime.
On Windows (see link to implementation here), it seems to call QueryPerformanceCounter.
But I cannot find what it does on MacOS and what kind of equivalent system function it calls. What does it do on MacOS?
Note: link to the source code are very welcome.
Here is the nanotime implementation for Darwin. It calls into nanotime_trampoline, implemented in Go assembly which in turns seems to call mach_absolute_time, defined in macOS's libSystem

Convert HANDLE to Handle

I want to create a annonymous pipe on windows using the CreatePipe of the WinAPI via the FFI. This will give me a HANDLE (type from the Win32 haskell package), but I'd like to get an ordinary haskell Handle such that I can use the standard haskell IO functions on it. So I need a function of type:
win32handleToStandardHandle :: HANDLE -> IO Handle
How can I implement this?
On linux, I can use System.Posix.IO's fdToHandle function to convert between the FD type used by the linux system calls and the standard haskell type. But there seems to be no such function for windows.
Even on Windows the standard implementation of files in System.IO uses a file descriptor, not a win32 HANDLE. These file descriptors are provided by the C runtime, not Windows. To convert a HANDLE to a file descriptor the runtime provides the _open_osfhandle function. GHC uses MinGW on Windows but MinGW doesn't provide its own C runtime, so Haskell EXEs use msvcrt.dll.
Use the FFI to import _open_osfhandle. Use this to convert your HANDLE to a file descriptor and then call fdToHandle, which seems to live in GHC.IO.Handle.FD.
I haven't actually tried this.

Using windows.pas with Lazarus on OS X

I was wondering if it's possible somehow to use windows.pas on OS X with Lazarus?
I need to use special library in my project, and one of key-files uses windows.pas :( Any ideas?
Windows.pas only works on Windows. You will have to edit the library to put an IFDEF around it in the uses clause, and then provide alternatives for any functionality that is then broken. Or contact the library author and see if there is already a non-Windows version available.
You certainly cannot use Windows.pas under OSX. Because Windows.pas exposes the functionality of the Win32 library.
If you need to execute Win32 code on OSX pretty much your only option is Wine.
A more plausible solution is that you find an alternative to this "special" library to which you refer.
Windows.pas is mostly a wrapper around different DLLs contained in the Windows operating system. As it is unlikely that you will find those DLLs in OSX I guess you are out of luck.
You could check the library's source code and try to identify the constants, procedures and functions that are used in windows.pas. If it is not too much code you could try to modify the library so that it uses corresponding Carbon functions instead.
While the various answers are correct, and the vast bulk of unit windows is not portable, some functionality IS abstracted. Structures like interlockedincrement, Rect and ColorRef, and some message related functionality. Have a look at types and lcltype and the system unit interface of FPC.
A lot of Delphi code still uses Windows for that functionality, while e.g. unit types already exists since D6.
Some other things are abstracted, but not using the same (windows unit) calls. Better explain what exactly you need in a separate post.

How to use __sync_fetch_and_add for a Linux userspace program on beagleboard/gumstix

I am looking to use the __sync_fetch_and_xxx functions for thread safe shared memory access on my Linux application with a beagleboard and gumstix. I can't seem to find the correct header to include. Are these functions only available for kernel development?
Thanks
These are compiler builtins. They are available for user development. You need no header to include, if gcc on your architecture supports them, it will produce correct assembler, if no, then it will produce an error.

Reading from keyboard with Ruby

I'm using Windows.
Is there a Ruby Way to make a dll call? I want to use the GetAsyncKeyState() function.
Haven't been a Windows user in a long time, but there's a Win32API library. Maybe this article helps:
http://www.rubytips.org/2008/05/13/accessing-windows-api-from-ruby-using-win32api-library/
You might want to look at the WIN32OLE library, or the DL library. They are designed for DLL access and Windows programing in Ruby. (NOTE: Although the librarys work, dynamic C access from Ruby does not seem to be an easy task, so use them only for sparse calls to the API. A Win32 program will do better in C, C++, C#, or .NET)
You may want to give FFI a look. It provides a fairly seamless interface to native libraries.

Resources