📄 fhandler-tut.txt
字号:
fhandler tutorialThis document will show how to add a new "fhandler" to cygwin, byshowing an example of /dev/zero.Files to note:fhandler.h - must define a new derived class here and FH_*path.cc - to notice "/dev/zero" and mark itfhandler_zero.cc - newdtable.cc - to create the fhandler instanceOK, first we have to define what this new fhandler will do. In ourexample 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 besimulating 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).OK, let's start with fhandler.h.First, update the comment about which files are where. We're addingfhandler_dev_zero as FH_DEV_ZERO. We're adding this as a "fast"device (it will never block) so we have to adjust FH_NDEV also.Later in that file, we'll copy fhandler_dev_null and edit it to befhandler_dev_zero. I chose that one because it's small, but we'll addmore members as we go (since we're simulating the whole thing). Infact, let's copy the I/O methods from fhandler_windows since we'llneed all those anyway, even though we'll go through the full listlater.OK, next we need to edit path.cc to recognize when the user is tryingto open "/dev/zero". Look in get_device_number; there's a long listof cases, just add one (I added one after "null"). Also remember toadd an entry to the windows_device_names list in the right spot.To go along with that change, we'll need to change dtable.cc. Look forFH_NULL and add a case for FH_ZERO as well.Now we get to fhandler_zero.cc itself. Create the empty file and copythe "usual" header/copyright/includes from some other fhandler_*.ccsource file. Also, edit Makefile.in to build this new file. Add onenew entry to DLL_OFILES, and a new line for the winsup.h dependencies.Since we changed fhandler.h, when you type "make" it will rebuildeverything. 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 beable to link the new dll, as we haven't added all the methods for thenew fhandler class yet, but at least you'll get a lot of compilationout 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 backto the base class, along with the FH_ZERO value. We call set_cbbecause 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 realwindows devices like /dev/zero, but we ignore the name. We callset_flags to save the flags.write: writes are discarded; we return success.read: reads read NUL bytes, so fill the buffer with NULs and returnsuccess.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 inselect.cc for examples. The base fhandler's methods will do for now.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -