2000-02-18 03:38:33 +08:00
|
|
|
fhandler tutorial
|
|
|
|
|
|
|
|
This document will show how to add a new "fhandler" to cygwin, by
|
|
|
|
showing an example of /dev/zero.
|
|
|
|
|
|
|
|
Files to note:
|
|
|
|
|
|
|
|
fhandler.h - must define a new derived class here and FH_*
|
2005-04-20 12:26:34 +08:00
|
|
|
devices.in - to notice "/dev/zero" and mark it
|
2000-02-18 03:38:33 +08:00
|
|
|
fhandler_zero.cc - new
|
2000-08-19 03:52:31 +08:00
|
|
|
dtable.cc - to create the fhandler instance
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
OK, first we have to define what this new fhandler will do. In our
|
|
|
|
example case, we're going to implement the unix "/dev/zero" device,
|
|
|
|
which has the following characteristics:
|
|
|
|
|
|
|
|
* writes to /dev/zero are silently discarded
|
|
|
|
* reads from /dev/zero return all zero bytes
|
|
|
|
* mmap()ing /dev/zero maps a chunk of zero'd out memory.
|
|
|
|
|
|
|
|
Since windows doesn't have a device that acts like this, we'll be
|
|
|
|
simulating everything. Thus:
|
|
|
|
|
|
|
|
* writes simply return a success status
|
|
|
|
* reads memset() the buffer and return success
|
|
|
|
* we take advantage of the fact that CreateFileMapping can take a
|
|
|
|
handle of -1, which (1) maps swap memory, and (2) zeros it out for
|
|
|
|
us (at least, on NT).
|
|
|
|
|
2005-04-20 12:26:34 +08:00
|
|
|
OK, let's start with devices.h.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-04-20 12:26:34 +08:00
|
|
|
We have to create a new entry in the enum fh_devices. The new
|
|
|
|
devices must get a major and a minor ID. As a rule of thumb, just
|
|
|
|
copy the ones that are used on a linux system.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-04-20 12:26:34 +08:00
|
|
|
Now, let's continue with fhandler.h.
|
|
|
|
|
|
|
|
First, update the fhandler_union near the end of the file with a
|
|
|
|
line for the new device. Use existing members, in this case __dev_null
|
|
|
|
as a template. This union is sorted alphabetically.
|
|
|
|
|
|
|
|
Earlier in that file, we'll copy fhandler_dev_null and edit it to be
|
2000-02-18 03:38:33 +08:00
|
|
|
fhandler_dev_zero. I chose that one because it's small, but we'll add
|
|
|
|
more members as we go (since we're simulating the whole thing). In
|
|
|
|
fact, let's copy the I/O methods from fhandler_windows since we'll
|
|
|
|
need all those anyway, even though we'll go through the full list
|
|
|
|
later.
|
|
|
|
|
2005-04-20 12:26:34 +08:00
|
|
|
OK, next we need to edit devices.in. There is a section where each device
|
|
|
|
is listed with its cygwin path, its structure and its windows path.
|
|
|
|
"/dev/zero", FH_ZERO, "\\dev\\zero"
|
|
|
|
This is needed to recognize when the user is trying to open "/dev/zero".
|
|
|
|
You have to build devices.cc from devices.in now.
|
|
|
|
There is a script 'gendevices' in the winsup/cygwin directory which may
|
|
|
|
be called at some time in the future if you use 'make' to build the DLL.
|
|
|
|
This should rebuild the devices.cc file. You have to have shilka
|
|
|
|
available to do that; this is part of the cygwin cocom package.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-08-19 03:52:31 +08:00
|
|
|
To go along with that change, we'll need to change dtable.cc. Look for
|
2000-02-18 03:38:33 +08:00
|
|
|
FH_NULL and add a case for FH_ZERO as well.
|
|
|
|
|
|
|
|
Now we get to fhandler_zero.cc itself. Create the empty file and copy
|
|
|
|
the "usual" header/copyright/includes from some other fhandler_*.cc
|
|
|
|
source file. Also, edit Makefile.in to build this new file. Add one
|
|
|
|
new entry to DLL_OFILES, and a new line for the winsup.h dependencies.
|
|
|
|
|
|
|
|
Since we changed fhandler.h, when you type "make" it will rebuild
|
|
|
|
everything. Go ahead and do that when you get a chance to let it run,
|
|
|
|
since we're not changing the headers any more. Note that you won't be
|
|
|
|
able to link the new dll, as we haven't added all the methods for the
|
|
|
|
new fhandler class yet, but at least you'll get a lot of compilation
|
|
|
|
out of the way.
|
|
|
|
|
|
|
|
Next we start adding in the fhandler methods themselves.
|
|
|
|
|
|
|
|
Constructor: This takes a name, and all we do is pass that name back
|
|
|
|
to the base class, along with the FH_ZERO value. We call set_cb
|
|
|
|
because all fhandlers call this (it's for exec to copy the fd).
|
|
|
|
|
|
|
|
open: we override the one that takes a name because there are no real
|
|
|
|
windows devices like /dev/zero, but we ignore the name. We call
|
|
|
|
set_flags to save the flags.
|
|
|
|
|
|
|
|
write: writes are discarded; we return success.
|
|
|
|
|
|
|
|
read: reads read NUL bytes, so fill the buffer with NULs and return
|
|
|
|
success.
|
|
|
|
|
|
|
|
lseek/close: just return success.
|
|
|
|
|
|
|
|
dump: this is just for debugging, so we just print something.
|
|
|
|
|
|
|
|
select_*: we don't support this yet, see the myriad examples in
|
|
|
|
select.cc for examples. The base fhandler's methods will do for now.
|