winsup roadmap

DJ Delorie
Wed Jan 5 18:51:00 GMT 2000

I've been working on (a little at a time) a "roadmap" to the
cygwin/winsup sources.  Attached is a preliminary version for review
and comments.  Note that the intention is to give just enough overview
of which files do what, that a programmer will be able to find where
things are in the sources, kind of a starting point for understanding
the cygwin/winsup sources.



The purpose of this document is to give the briefest overview of how
the various parts of cygwin work together and where everything can be
found.  The intended audience is people developing the cygwin dll
itself.  Comments to

=== cygwin1.dll source files

- overhead
.h	winsup autoload debug external shared sync
.cc	assert dcrt0 debug external init ntea registry security
	shared smallprint strace sync
.din	cygwin
.rc	winver
.sgml	external shared

- processes
.h	sigproc
.cc	exec fork pinfo resource signal sigproc spawn wait

- signals
.cc	exceptions window

- files and I/O
.h	delqueue fhandler path select
.cc	delqueue dir fhandler* hinfo path pipe select tty
.sgml	hinfo path

- common unix functions
.h	dll_init tz_posixrules
.cc	dlfcn dll_init environ errno fcntl flog grp ioctl localtime
	malloc passwd scandir strsep syscalls sysconf syslog termios
.c	longjmp setjmp
.sgml	dll_init

- unix emulation
.cc	heap mmap net times unifo uname

--- if MT_SAFE
.h	thread
.cc	pthread thread

--- from other places
../newlib/* (libc)

=== libcygwin.a source files

=== gmon (profiling, -pg)

gmon.c		gmon.h
profil.c	profil.h

=== entry points

- normal cygwin program

newlib/libc/sys/cygwin/crt0.c has mainCRTStartup() and calls cygwin_crt0() has cygwin_crt0() and calls dll_crt0() - has dll_crt0()

Note: has nothing to do with initializing the cygwin dll.
It initializes the dlls you have dl_open'd.

- cygwin-built dll - has a macro for wrapping your dll startup function
	(equivalent of DllMain()) in such a way that you get your
	cygwin environment set up automatically when your dll is
	loaded. - has empty DllMain() in case you don't have your own

- manually loading cygwin1.dll - has dll_entry() which is called by the OS when the dll is
	loaded.  It doesn't do much except note if you linked
	cygwin1.dll or are manually loading it.

=== About "fhandlers"

An fhandler is a file type handler.  This is where the unix device
emulation happens. maps posix file descriptors to a table of file handlers (type
fhandler) in the dll.  It's mostly concerned with managing the table
of descriptors (open, dup, fork, select).  Most of the posix I/O
system calls ( use the hinfo table to call the right
fhandler directly. is the base class; specific types are derived as
appropriate (see fhandler.h). is in charge of selecting and
creating a suitable fhandler when you open a file. handles
emulated files in /dev (like /dev/null) by returning an FH_* value
from get_device_number (which calls in hinfo::build_fhandler).

Note: if you're looking for read() and write(), they call _read() and
_write() in  The non-underscored ones are in
newlib/libc/syscalls and just call the underscored ones.

=== How "fork" works

It all starts in fork() in

Set up a pid in the shared memory area for the new child.  Use
setjmp() to capture state.  First time (parent), set up some stuff and
use CreateProcess to run a second copy of the same executable.  The
second copy will note in the shared memory area that it's a fork, and
do the longjmp.  They sync up and the parent copies all it's program
memory to the child's address space.  There's also code to reload
dlls, map shared memory and mmap'd files, etc.

Handling the special startup for the child is done in in many
places.  This case is triggered by a special StartupInfo structure
that's passed from the parent to the child in CreateProcessA.

More information about the Cygwin-developers mailing list