📄 how-fhandlers-work.txt
字号:
Copyright 2001 Red Hat Inc., Robert Collinsfhandlers are the core mechanism by which cygwin provides a file descripter (fd)interface to things such as a random number generated, winsock sockets, raw diskdevices, the clipboard, the console and so on. Under unix access to all suchdevices is via a combination of IOCTL's and open/close/read/write calls. Somespecial functions do exist - such as bind () and listen () for sockets, butthese consistently operate on fd's. Under Win32 there are disparate interfacesthat have little in common with each other. See for example Direct Sound andthe Clipboard.The fhandler class provides all open,read,write,close, ioctl and fork()/exec()functionality for the fd interface. The base class operates on win32 backedfiles. The various derived classes utilise win32 primitives to provide theirspecific functionality.When a file is opened - not necesarily via open() a fd is assigned to it. The fdincludes a pointer to the actual fhandler that operates this specific file. Allfile-oriented system calls then operate off this basic structure.For example, lets examine lseek ().extern "C" off_t_lseek (int fd, off_t pos, int dir){ off_t res; sigframe thisframe (mainthread); if (dir != SEEK_SET && dir != SEEK_CUR && dir != SEEK_END) { set_errno (EINVAL); res = -1; } else if (cygheap->fdtab.not_open (fd)) { set_errno (EBADF); res = -1; } else { res = cygheap->fdtab[fd]->lseek (pos, dir); } syscall_printf ("%d = lseek (%d, %d, %d)", res, fd, pos, dir); return res;}The sigframe thisframe (mainthread); is signal related - see"how_signals_work.txt".The if, else if, else tests (in order)* the validity of the dir parameter,* is the fd being passed actually open? (cannot seek on a closed fd)* call the lseek virtual function in the associated fhandler.So as you can see, there is no code that attempts to understand the nature ofthe fhandler.fhandlers that make cross-function-call use of win32 objects that are notinheritable cross-process need to implement fixup-after-fork and recreate thoseobjects. HANDLES can be inherited, but memory mapped regions (for example)cannot.For an example step-by-step to create a new fhandler, see../doc/fhandler-tut.txtNote: In most case, it is safe to assume that using new/delete (ormalloc/free) in an fhandler is dangerous and to be avoided. The reasonfor this is that memory allocated to fhandlers is copied between execedprocesses in the cygwin heap. Memory allocated in new/delete is onlycopied to forked processes. So use cmalloc/cfree.Obviously it is possible to use new/delete in some situations but if you'reseeing strange core dumps with usages like cat < /my/newfhandler then theabove may well be the culprit.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -