[Mono-list] Writing daemons in C#

Paolo Molaro lupus@ximian.com
Wed, 20 Nov 2002 11:49:39 +0100


On 11/19/02 Adam Keys wrote:
> On Tue, 2002-11-19 at 19:31, Miguel de Icaza wrote:
> > There are various sites online with the POSIX APIs.
> 
> Googling revealed a few fairly uninformative sites.  However, an
> effort[1] to implement POSIX APIs in Java did show up.  However, they
> did not have code, just a (useful to me) list of functions in the POSIX
> standard.

Have also a look at the POSIX perl module: just do man POSIX if you have
perl installed.

> > It should be easily doable for a new programmer, but you must be ready
> > to rewrite a few chunks based on the input that you will very likely
> > receive ;-)
> 
> I'm presuming DllImport and P/Invoke aren't the preferred methods here? 
> I looked at System.Net.Socket, since that would appear to be a task
> similar to implementing POSIX APIs.  It uses InternalCall, which I'm
> presuming (C# In A Nutshell is not so clear on this) is implemented
> within the interpreter.  I poked through mono and found the file
> mono/io-layer/sockets.c which appears to be the internal implementation
> of the Sockets API.  Would the POSIX implementation look similar to
> this?  Is there someplace all this is documented or am I on my own?

My idea is to implement it using DllImport, mostly. Many of the calls
are dangerous anyway, so requiring p/invoke permissions is just about
right. The socket stuff in System is done using internal calls because
it has different permission requirements and it needs to tie to some
runtime features like the handles.
Consider chwon:

	int chown (const char *path, uid_t owner, gid_t group);

in C# it becomes:

	[DllImport ("libc")]
	public static int chown (string path, int owner, int group);

Now, that is the bare-bones stuff and it's enough to get started.
Next, I want to write easy to use wrappers, like:

	public static int chown (string path, string owner, string group) {
		int uid = owner == null? -1: get_uid_from_name (owner);
		int gid = group == null? -1: get_gid_from_name (group);
		// check for errors
		return chown (path, uid, gid);
	}

So that I can do things like:

	POSIX.chown ("/etc/passwd", "lupus", "evil");

Third, I want a version of the API that raises exceptions on error, so
that I don't need to do all the checking myself.

After that I want it to have better integration with the mono runtime
and the base assemblies: this involves internal calls. This is needed,
say, to get a FileStream back from a file descriptor used in the POSIX
API and vice versa.

Hope that gives a better idea of what I'd like the Mono.POSIX assembly
to look like:

1) Massive use of DllImport (a little boring, but easy to do).
2) Easy to use interfaces (requires good taste for APIs:-).
3) Exception-raising variants (again, a little boring).
4) Requires internal calls for better integration.

lupus

-- 
-----------------------------------------------------------------
lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better