* implementor.texinfo (Compiler and OS Requirements): New section
[krb5.git] / doc / implementor.texinfo
1 \input texinfo @c -*-texinfo-*-
2 @c
3 @c Note: the above texinfo file must include the "doubleleftarrow"
4 @c definitions added by jcb.
5 @c %**start of header
6 @c guide
7 @setfilename krb5-implement.info
8 @settitle Kerberos V5 Installation Guide
9 @setchapternewpage odd                  @c chapter begins on next odd page
10 @c @setchapternewpage on                   @c chapter begins on next page
11 @c @smallbook                              @c Format for 7" X 9.25" paper
12 @c %**end of header
13
14 @paragraphindent 0
15 @iftex
16 @parskip 6pt plus 6pt
17 @end iftex
18
19 @include definitions.texinfo
20 @set EDITION b7-1
21
22 @finalout                               @c don't print black warning boxes
23
24 @titlepage
25 @title @value{PRODUCT} Implementor's Guide
26 @subtitle Release:  @value{RELEASE}
27 @subtitle Document Edition:  @value{EDITION}
28 @subtitle Last updated:  @value{UPDATED}
29 @author @value{COMPANY}
30
31 @page
32 @vskip 0pt plus 1filll
33
34 @iftex
35 @include copyright.texinfo
36 @end iftex
37 @end titlepage
38
39 @node Top, Introduction, (dir), (dir)
40 @comment  node-name,  next,  previous,  up
41
42 @ifinfo
43 This file contains internal implementor's information for the
44 @value{RELEASE} release of @value{PRODUCT}.  
45
46 @include copyright.texinfo
47
48 @end ifinfo
49
50 @c The master menu is updated using emacs19's M-x texinfo-all-menus-update
51 @c function.  Don't forget to run M-x texinfo-every-node-update after
52 @c you add a new section or subsection, or after you've rearranged the
53 @c order of sections or subsections.  Also, don't forget to add an @node
54 @c comand before each @section or @subsection!  All you need to enter
55 @c is:
56 @c
57 @c @node New Section Name
58
59 @c @section New Section Name
60 @c
61 @c M-x texinfo-every-node-update will take care of calculating the
62 @c node's forward and back pointers.
63 @c
64 @c ---------------------------------------------------------------------
65
66 @menu
67 * Introduction::                
68 * Compiler and OS Requirements::  
69 * Socket API::                  
70 * IPv6 Support::                
71 * Local Addresses::             
72 * Host Address Lookup::         
73 * Thread Safety::               
74 * Shared Libraries::            
75 @end menu
76
77 @node Introduction, Compiler and OS Requirements, Top, Top
78 @chapter Introduction
79
80 This file contains internal implementor's information for
81 @value{PRODUCT}.  It is currently contains information that was removed
82 from install.texi; eventually it will have more detailed information on
83 the internals of the @value{PRODUCT}.
84
85 @node Compiler and OS Requirements, Socket API, Introduction, Top
86 @chapter Compiler and OS Requirements
87
88 The basic Kerberos libraries are entirely written in C.
89 However, we do assume full ANSI C (1989) support, typical 32- or
90 64-bit twos-complement architectures (@code{char} is 8 bits,
91 @code{short} is 16 bits, @code{int} is 32 bits, byte order is 1234 or
92 4321), and a few aspects of ISO C 1999:
93
94 @itemize @bullet
95 @item
96 support for inline functions, even if the keyword isn't @code{inline}
97 @item
98 64-bit arithmetic types (needed for sequence numbers in GSSAPI)
99 @end itemize
100
101 These are handled through the internal header file
102 @file{k5-platform.h}.
103
104 We also conditionally tailor code for the GNU C compiler in a few
105 places where it helps performance or debugging, but the code should
106 still work fine with other C compilers.
107
108 On UNIX platforms, ... @i{(should outline what POSIX stuff we
109 require)}.
110
111 See also @ref{Advanced Shared Library Requirements}, for UNIX and
112 Windows systems.
113
114 Our makefiles are intended to support building from the top level with
115 a POSIX-compliant version of @code{make}, and parallel builds using
116 GNU @code{make}.  This sometimes comes at the cost of efficiency with
117 non-GNU versions; for example, some targets in some directories will
118 always be rebuilt with certain versions of @code{make}, even though
119 the real dependencies are not out of date, because some versions of
120 @code{make} don't understand how we're using phony intermediate
121 targets to manage building in subdirectories in parallel builds.
122 (Actually, this is more my view of how we've been doing things than
123 official policy.  ---Ken)
124
125 @node Socket API, IPv6 Support, Compiler and OS Requirements, Top
126 @chapter Socket API
127
128 Someone should describe the API subset we're allowed to use with
129 sockets, how and when to use @code{SOCKET_ERRNO}, @i{etc}.
130
131 Note that all new code doing hostname and address translation should
132 use @code{getaddrinfo} and friends.  (@xref{Host Address Lookup}.)
133
134 @node IPv6 Support, Local Addresses, Socket API, Top
135 @chapter IPv6 Support
136
137 Most of the IPv6 support is keyed on the macro @code{KRB5_USE_INET6}.
138 If this macro is not defined, there should be no references to
139 @code{AF_INET6}, @code{struct sockaddr_in6}, @i{etc}.
140
141 The @code{configure} scripts will check for the existence of various
142 functions, macros and structure types to decide whether to enable the
143 IPv6 support.  You can also use the @samp{--enable-ipv6} or
144 @samp{--disable-ipv6} options to override this decision.
145
146 Regardless of the setting of @code{KRB5_USE_INET6}, some aspects of
147 the new APIs devised for IPv6 are used throughout the code, because it
148 would be too difficult maintain code for the IPv6 APIs and for the old
149 APIs at the same time.  But for backwards compatibility, we try to
150 fake them if the system libraries don't provide them, at least for
151 now.  This means we sometimes use slightly modified versions of the
152 APIs, but we try to keep the modifications as non-intrusive as
153 possible.  Macros are used to rename struct tags and function names,
154 so don't @code{#undef} any of these names.
155
156 @table @code
157
158 @item getaddrinfo
159 @itemx getnameinfo
160 @itemx freeaddrinfo
161 @itemx gai_strerror
162 @itemx struct addrinfo
163 Always include the header file @file{fake-addrinfo.h} before using
164 these.  If the native system doesn't provide them, the header file
165 will, using static functions that will call @code{gethostbyname} and
166 the like in the native libraries.  (This also happens to be the way
167 the Winsock 2 headers work, depending on some of the predefined macros
168 indicating the target OS version.)
169
170 We also provide ``wrapper'' versions on some systems where a native
171 implementation exists but the data it returns is broken in some way.
172
173 So these may not always be thread-safe, and they may not always
174 provide IPv6 support, but the API will be consistent.
175
176 @item struct sockaddr_storage
177 @itemx socklen_t
178 These are provided by @file{socket-utils.h}, if the native headers
179 don't provide them.  @code{sockaddr_storage} contains a
180 @code{sockaddr_in}, so by definition it's big enough to hold one; it
181 also has some extra padding which will probably make it big enough to
182 hold a @code{sockaddr_in6} if the resulting binary should get run on a
183 kernel with IPv6 support.
184
185 Question: Should these simply be moved into @file{port-sockets.h}?
186
187 @end table
188
189 IRIX 6.5.7 has no IPv6 support.  Of the systems most actively in the
190 MIT's Athena environment (used by MIT's Kerberos UNIX developers),
191 this is the only one without built-in IPv6 support.  In another year
192 or so we probably won't be using those systems any more, and we may
193 consider dropping support for systems without IPv6 support.
194
195 Somewhere between IRIX 6.5.14 and 6.5.16, partial IPv6 support was
196 introduced to the extent that the configuration system detects the
197 IPv6 support and attempts to use it. Code compiles, but then upon
198 linking, one discovers that ``in6addr_any'' is not defined in any
199 system library. A work around the header file @file{fake-addrinfo.h}
200 is provided by providing a static copy. This run time IPv6 code has
201 still not been tested.
202
203 Some utility functions or macros are also provided to give a
204 convenient shorthand for some operations, and to retain compile-time
205 type checking when possible (generally using inline functions but only
206 when compiling with GCC).
207
208 @table @code
209
210 @item socklen(struct sockaddr *)
211 Returns the length of the @code{sockaddr} structure, by looking at the
212 @code{sa_len} field if it exists, or by returning the known sizes of
213 @code{AF_INET} and @code{AF_INET6} address structures.
214
215 @item sa2sin(struct sockaddr *)
216 @itemx sa2sin6(struct sockaddr *)
217 @itemx ss2sa(struct sockaddr_storage *)
218 @itemx ss2sin(struct sockaddr_storage *)
219 @itemx ss2sin6(struct sockaddr_storage *)
220 Pointer type conversions.  Use these instead of plain casts, to get
221 type checking under GCC.
222
223 @end table
224
225 @node Local Addresses, Host Address Lookup, IPv6 Support, Top
226 @chapter Local Addresses
227
228 (Last update: 2002-03-13.)
229
230 Different systems have different ways of finding the local network
231 addresses.
232
233 On Windows, @code{gethostbyname} is called on the local host name to get a
234 set of addresses.  If that fails, a UDP socket is ``connected'' to a
235 particular IPv4 address, and the local socket name is retrieved, its
236 address being treated as the one local network address.  Future
237 versions of the Windows code should be able to actually examine local
238 interfaces.
239
240 On Mac OS 9 and earlier, a Mac-specific interface is used to look up
241 local addresses.  Presumably, on Mac OS X we'll use that or the
242 general UNIX code.
243
244 On (most?) UNIX systems, there is an @code{ioctl} called
245 @code{SIOCGIFCONF} which gets interface configuration information.
246 The behavior of this @code{ioctl} varies across UNIX systems though.
247 It takes as input a buffer to fill with data structures, but if the
248 buffer isn't big enough, the behavior isn't well defined.  Sometimes
249 you get an error, sometimes you get incomplete data.  Sometimes you
250 get a clear indication that more space was needed, sometimes not.  A
251 couple of systems have additional @code{ioctl}s that can be used to
252 determine or at least estimate the correct size for the buffer.
253 Solaris has introduced @code{SIOCGLIFCONF} for querying IPv6
254 addresses, and restricts @code{SIOCGIFCONF} to IPv4 only.  (** We
255 should actually check if that's true.)
256
257 We (Ken Raeburn in particular) ran some tests on various systems to
258 see what would happen with buffers of various sizes from much smaller
259 to much larger than needed for the actual data.  The buffers were
260 filled with specific byte values, and then checked to see how much of
261 the buffer was actually written to.  The "largest gap" values listed
262 below are the largest number of bytes we've seen left unused at the
263 end of the supplied buffer when there were more entries to return.
264 These values may of coures be dependent on the configurations of the
265 particular systems we wre testing with.  (See
266 @file{lib/krb5/os/t_gifconf.c} for the test program.)
267
268 NetBSD 1.5-alpha: The returned @code{ifc_len} is the desired amount of
269 space, always.  The returned list may be truncated if there isn't
270 enough room; no overrun.  Largest gap: 43.  However, NetBSD has
271 @code{getifaddrs}, which hides all the ugliness within the C library.
272
273 BSD/OS 4.0.1 (courtesy djm): The returned @code{ifc_len} is equal to
274 or less than the supplied @code{ifc_len}.  Sometimes the entire buffer
275 is used; sometimes N-1 bytes; occasionally, the buffer must have quite
276 a bit of extra room before the next structure will be added.  Largest
277 gap: 39.
278
279 Solaris 7,8: Return @code{EINVAL} if the buffer space is too small for
280 all the data to be returned, including when @code{ifc_len} is 0.
281 Solaris is the only system I've found so far that actually returns an
282 error.  No gap.  However, @code{SIOCGIFNUM} may be used to query the
283 number of interfaces.
284
285 Linux 2.2.12 (Red Hat 6.1 distribution, x86), 2.4.9 (RH 7.1, x86): The
286 buffer is filled in with as many entries as will fit, and the size
287 used is returned in @code{ifc_len}.  The list is truncated if needed,
288 with no indication.  Largest gap: 31.  @emph{However}, this interface
289 does not return any IPv6 addresses.  They must be read from a file
290 under @file{/proc}.  (This appears to be what the @samp{ifconfig}
291 program does.)
292
293 IRIX 6.5.7: The buffer is filled in with as many entries as will fit
294 in N-1 bytes, and the size used is returned in @code{ifc_len}.
295 Providing exactly the desired number of bytes is inadequate; the
296 buffer must be @emph{bigger} than needed.  (E.g., 32->0, 33->32.)  The
297 returned @code{ifc_len} is always less than the supplied one.  Largest
298 gap: 32.
299
300 AIX 4.3.3: Sometimes the returned @code{ifc_len} is bigger than the
301 supplied one, but it may not be big enough for @emph{all} the
302 interfaces.  Sometimes it's smaller than the supplied value, even if
303 the returned list is truncated.  The list is filled in with as many
304 entries as will fit; no overrun.  Largest gap: 143.
305
306 Older AIX: We're told by W. David Shambroom (DShambroom@@gte.com) in
307 PR krb5-kdc/919 that older versions of AIX have a bug in the
308 @code{SIOCGIFCONF} @code{ioctl} which can cause them to overrun the
309 supplied buffer.  However, we don't yet have details as to which
310 version, whether the overrun amount was bounded (e.g., one
311 @code{ifreq}'s worth) or not, whether it's a real buffer overrun or
312 someone assuming it was because @code{ifc_len} was increased, etc.
313 Once we've got details, we can try to work around the problem.
314
315 Digital UNIX 4.0F: If input @code{ifc_len} is zero, return an
316 @code{ifc_len} that's big enough to include all entries.  (Actually,
317 on our system, it appears to be larger than that by 32.)  If input
318 @code{ifc_len} is nonzero, fill in as many entries as will fit, and
319 set @code{ifc_len} accordingly.  (Tested only with buffer previously
320 filled with zeros.)
321
322 Tru64 UNIX 5.1A: Like Digital UNIX 4.0F, except the ``extra'' space
323 indicated when the input @code{ifc_len} is zero is larger.  (We got
324 400 out when 320 appeared to be needed.)
325
326 So... if the returned @code{ifc_len} is bigger than the supplied one,
327 we'll need at least that much space -- but possibly more -- to hold
328 all the results.  If the returned value is smaller or the same, we may
329 still need more space.
330
331 The heuristic we're using on most systems now is to keep growing the
332 buffer until the unused space is larger than an @code{ifreq} structure
333 by some safe margin.
334
335 @node Host Address Lookup, Thread Safety, Local Addresses, Top
336 @chapter Host Address Lookup
337
338 The traditional @code{gethostbyname} function is not thread-safe, and
339 does not support looking up IPv6 addresses, both of which are becoming
340 more important.  New standards have been in development that should
341 address both of these problems.  The most promising is
342 @code{getaddrinfo} and friends, which is part of the Austin Group and
343 UNIX 98(?) specifications.  Code in the MIT tree is gradually being
344 converted to use this interface.
345
346 @quotation
347 (Question: What about @code{inet_ntop} and @code{inet_pton}?  We're
348 not using them at the moment, but some bits of code would be
349 simplified if we were to do so, when plain addresses and not socket
350 addresses are already presented to us.)
351 @end quotation
352
353 The @code{getaddrinfo} function takes a host name and service name and
354 returns a linked list of structures indicating the address family,
355 length, and actual data in ``sockaddr'' form.  (That is, it includes a
356 pointer to a @code{sockaddr_in} or @code{sockaddr_in6} structure.)
357 Depending on options set via the @code{hints} input argument, the results
358 can be limited to a single address family (@i{e.g.}, for IPv4
359 applications), and the canonical name of the indicated host can be
360 returned.  Either the host or service can be a null pointer, in which
361 case only the other is looked up; they can also be expressed in
362 numeric form.  This interface is extensible to additional address
363 families in the future.  The returned linked list can be freed with
364 the @code{freeaddrinfo} function.
365
366 The @code{getnameinfo} function does the reverse -- given an address
367 in ``sockaddr'' form, it converts the address and port values into
368 printable forms.
369
370 Errors returned by either of these functions -- as return values, not
371 global variables -- can be translated into printable form with the
372 @code{gai_strerror} function.
373
374 Some vendors are starting to implement @code{getaddrinfo} and friends,
375 however, some of the implementations are deficient in one way or
376 another.
377
378 @table @asis
379
380 @item AIX
381 As of AIX 4.3.3, @code{getaddrinfo} returns sockaddr structures
382 without the family and length fields filled in.
383
384 @item GNU libc
385 The GNU C library, used on GNU/Linux systems, has had a few problems
386 in this area.  One version would drop some IPv4 addresses for some
387 hosts that had multiple IPv4 and IPv6 addresses.
388
389 In GNU libc 2.2.4, when the DNS is used, the name referred to by PTR
390 records for each of the addresses is looked up and stored in the
391 @code{ai_canonname} field, or the printed numeric form of the address
392 is, both of which are wrong.
393
394 @item IRIX
395 No known bugs here, but as of IRIX 6.5.7, the version we're using at
396 MIT, these functions had not been implemented.
397
398 @item NetBSD
399 As of NetBSD 1.5, this function is not thread-safe.  In 1.5X
400 (intermediate code snapshot between 1.5 and 1.6 releases), the
401 @code{ai_canonname} field can be empty, even if the
402 @code{AI_CANONNAME} flag was passed.  In particular, this can happen
403 if a numeric host address string is provided.  Also, numeric service
404 names appear not to work unless the stream type is given; specifying
405 the TCP protocol is not enough.
406
407 @item Tru64 UNIX
408 In Tru64 UNIX 5.0, @code{getaddrinfo} is available, but requires that
409 @code{<netdb.h>} be included before its use; that header file defines
410 @code{getaddrinfo} as a macro expanding to either @code{ogetaddrinfo}
411 or @code{ngetaddrinfo}, and apparently the symbol @code{getaddrinfo}
412 is not present in the system library, causing the @code{configure}
413 test for it to fail.  Technically speaking, I [Ken] think Compaq has
414 it wrong here, I think the symbol is supposed to be available even if
415 the application uses @code{#undef}, but I have not confirmed it in the
416 spec.
417
418 @item Windows
419 According to Windows documentation, the returned @code{ai_canonname}
420 field can be null even if the @code{AI_CANONNAME} flag is given.
421
422 @end table
423
424 For most systems where @code{getaddrinfo} returns incorrect data,
425 we've provided wrapper versions that call the system version and then
426 try to fix up the returned data.
427
428 For systems that don't provide these functions at all, we've provided
429 replacement versions that neither are thread-safe nor support IPv6,
430 but will allow us to convert the rest of our code to assume the
431 availability of @code{getaddrinfo}, rather than having to use two
432 branches everywhere, one for @code{getaddrinfo} and one for
433 @code{gethostbyname}.  These replacement functions do use
434 @code{gethostbyname} and the like; for some systems it would be
435 possible to use @code{gethostbyname2} or @code{gethostbyname_r} or
436 other such functions, to provide thread safety or IPv6 support, but
437 this has not been a priority for us, since most modern systems have
438 these functions anyways.  And if they don't, they probably don't have
439 real IPv6 support either.
440
441 Including @file{fake-addrinfo.h} will enable the wrapper or
442 replacement versions when needed.  Depending on the system
443 configuration, this header file may define several static functions
444 (and declare them @code{inline} under GNU C), and leave it to the
445 compiler to discard any unused code.  This may produce warnings on
446 some systems, and if the compiler isn't being too clever, may cause
447 several kilobytes of excess storage to be consumed on these backwards
448 systems.
449
450 Do not assume that @code{ai_canonname} will be set when the
451 @code{AI_CANONNAME} flag is set.  Check for a null pointer before
452 using it.
453
454 @node Thread Safety, Shared Libraries, Host Address Lookup, Top
455 @chapter Thread Safety
456
457 Work is still needed as this section is being written.  However, we've
458 made a lot of progress.
459
460 @menu
461 * Kerberos API Thread Safety::  
462 * Thread System Requirements::  
463 * Internal Thread API::         
464 @end menu
465
466 @node Kerberos API Thread Safety, Thread System Requirements, Thread Safety, Thread Safety
467 @section Kerberos API Thread Safety
468
469 We assume that a @code{krb5_context} or a @code{krb5_auth_context}
470 will be used in only one thread at a time, and any non-opaque object
471 clearly being modified by the application code (@i{e.g.}, a
472 @code{krb5_principal} having a field replaced) is not being used in
473 another thread at the same time.
474
475 A credentials cache, key table, or replay cache object, once the C
476 object is created, may be used in multiple threads simultaneously;
477 internal locking is done by the implementations of those objects.
478 (Iterators?  Probably okay now, but needs review.)  However, this
479 doesn't mean that we've fixed any problems there may be regarding
480 simultaneous access to on-disk files from multiple processes, and in
481 fact if a process opens a disk file multiple times, the same problems
482 may come up.
483
484 Any file locking issues may become worse, actually.  UNIX file locking
485 with @code{flock} is done on a per-process basis, and closing a file
486 descriptor that was opened on a file releases any locks the process
487 may have on that file, even if they were obtained using other,
488 still-open file descriptors.
489
490 We MAY implement --- but haven't yet --- a ``fix'' whereby open files
491 are tracked by name (and per object type), and a new attempt to open
492 one gets a handle that uses the same open file descriptor, even if it
493 appears as two objects to the application.  This won't address the
494 problem of getting the same file via two names that look different,
495 but it may be ``good enough.''
496
497 GSSAPI ....
498
499 @node Thread System Requirements, Internal Thread API, Kerberos API Thread Safety, Thread Safety
500 @section Thread System Requirements
501
502 We support a few types of environments with regard to thread support:
503
504 @itemize @bullet
505
506 @item
507 Windows native threads.  The objects used by the Windows thread
508 support functions generally need run-time initialization; this is done
509 through the library initialization function.  (@xref{Advanced Shared
510 Library Requirements}.)
511
512 @item
513 POSIX threads, with weak reference support so we can tell whether the
514 thread code was actually linked into the current executable.  If the
515 functions aren't available, we assume the process is single-threaded
516 and ignore locks.  (We do assume that the thread support functions
517 won't show up half-way through execution of the program.)  In order to
518 support single-threaded programs wanting to load Kerberos or GSSAPI
519 modules through a plug-in mechanism, we don't list the pthread library
520 in the dependencies of our shared libraries.
521
522 @item
523 POSIX threads, with the library functions always available, even if
524 they're stub versions that behave normally but don't permit the
525 creation of new threads.
526
527 On AIX 4.3.3, we do not get weak references or useful stub functions,
528 and calling @code{dlopen} apparently causes the pthread library to get
529 loaded, so we've decided to link against the pthread library always.
530
531 On Tru64 UNIX 5.1, we again do not get weak references or useful stub
532 functions.  Rather than look for yet another approach for this one
533 platform, we decided to always link against the pthread library on
534 this platform as well.  This may break single-threaded applications
535 that load the Kerberos libraries after startup.  A clean solution,
536 even if platform-dependent, would be welcome.
537
538 @item
539 Single-threaded.  No locking is performed, any ``thread-local''
540 storage is in fact global, @i{etc}.
541
542 @end itemize
543
544 If @code{pthread_once} is not provided in functional form in the
545 default libraries, and weak references are not supported, we always
546 link against the pthread libraries.  (Tru64, AIX.)
547
548 System routines: getaddrinfo (not always implemented thread-safe),
549 gethostbyname_r, gmtime_r, getpwnam_r, res_nsearch.
550
551 Unsafe system routines: setenv, setlocale.
552
553 @node Internal Thread API,  , Thread System Requirements, Thread Safety
554 @section Internal Thread API
555
556 Some ideas were discussed on the @samp{krbdev} mailing list, and while
557 the current implementation does largely resemble the scheme Ken
558 Raeburn proposed.
559
560 The following macros in @file{k5-thread.h} implement a locking scheme
561 similar to POSIX threads, with fewer features.
562
563 @deftp {Data type} k5_mutex_t
564 This is the type of a mutex to be used by the Kerberos libraries.  Any
565 object of this type needs initialization.  If the object is
566 dynamically allocated, @code{k5_mutex_init} must be used; if the
567 object is allocated statically, it should be initialized at compile
568 time with @code{K5_MUTEX_PARTIAL_INITIALIZER} and then
569 @code{k5_mutex_finish_init} should be called at run time.  (In
570 general, one of these will do the work, and the other will do nothing
571 interesting, depending on the platform.  When the debugging code is
572 turned on, it will check that both were done.  However, as far as I
573 know, it should work to use just @code{k5_mutex_init} on a mutex in
574 static storage.)
575
576 The mutex may be used only within the current process.  It should not
577 be created in memory shared between processes.  (Will it work in a
578 child process after @code{fork()}?  I think so.)
579
580 Depending on compile-time options, the @code{k5_mutex_t} object may
581 contain more than an operating-system mutex; it may also contain
582 debugging information such as the file and line number in the Kerberos
583 code where the last mutex operation was performed, information for
584 gathering statistics on mutex usage, @i{etc}.
585
586 This type @emph{is not} a simple typedef for the native OS mutex
587 object, to prevent programmers from accidentally assuming that
588 arbitrary features of the native thread system will always be
589 available.  (If someone wishes to make use of native thread system
590 features in random library code, they'll have to go further out of
591 their way to do it, and such changes probably won't be accepted in the
592 main Kerberos code base at MIT.)
593 @end deftp
594
595 @defvr Macro K5_MUTEX_PARTIAL_INITIALIZER
596 Value to be used for compile-time initialization of a mutex in static
597 storage.
598 @end defvr
599
600 @deftypefn Macro int k5_mutex_finish_init (k5_mutex_t *@var{m})
601 Finishes run-time initialization, if such is needed, of a mutex that
602 was initialized with @code{K5_MUTEX_PARTIAL_INITIALIZER}.  This macro
603 must be called before the mutex can be locked; usually this is done
604 from library initialization functions.
605 @end deftypefn
606
607 @deftypefn Macro int k5_mutex_init (k5_mutex_t *@var{m})
608 Initializes a mutex.
609 @end deftypefn
610
611 @deftypefn Macro int k5_mutex_destroy (k5_mutex_t *@var{m})
612 Destroys a mutex, whether allocated in static or heap storage.  All
613 mutexes should be destroyed before the containing storage is freed, in
614 case additional system resources have been allocated to manage them.
615 @end deftypefn
616
617 @deftypefn Macro int k5_mutex_lock (k5_mutex_t *@var{m})
618 @deftypefnx Macro int k5_mutex_unlock (k5_mutex_t *@var{m})
619 Lock or unlock a mutex, returning a system error code if an error
620 happened, or zero for success.  (Typically, the return code from
621 @code{k5_mutex_unlock} is ignored.)
622 @end deftypefn
623
624 @deftypefn Macro void k5_mutex_assert_locked (k5_mutex_t *@var{m})
625 @deftypefnx Macro void k5_mutex_assert_unlocked (k5_mutex_t *@var{m})
626 These macros may be used in functions that require that a certain
627 mutex be locked by the current thread, or not, at certain points
628 (typically on entry to the function).  They may generate error
629 messages or debugger traps, or abort the program, if the mutex is not
630 in the expected state.  Or, they may simply do nothing.
631
632 It is not required that the OS mutex interface let the application
633 code determine the state of a mutex; hence these are not specified as
634 a single macro returning the current state, to be checked with
635 @code{assert}.
636 @end deftypefn
637
638 Mutexes are assumed not to be recursive (@i{i.e.}, if a thread has the
639 mutex locked already, attempting to lock it again is an error).  There
640 is also no support assumed for ``trylock'' or ``lock with timeout''
641 operations.
642
643 The operating system interface is similar to the above interface, with
644 @code{k5_os_} names used for the OS mutex manipulation code.  The type
645 and macros indicated above are wrappers that optionally add debugging
646 code and other stuff.  So the Kerberos library code should use the
647 macros above, and ports to new thread systems should be done through
648 the @code{k5_os_} layer.
649
650 Thread-local storage is managed through another interface layer
651
652 @deftp {Enumerator} k5_key_t
653 This is an enumeration type which indicates which of the per-thread
654 data objects is to be referenced.
655 @end deftp
656
657 @deftypefn Macro int k5_key_register (k5_key_t @var{key}, void (*@var{destructor})(void*))
658 Registers a thread-local storage key and a function to destroy a
659 stored object if the thread exits.  This function must be called
660 before @code{k5_setspecific} can be used.  Currently @var{destructor}
661 must not be a null pointer; note, however, that the standard library
662 function @code{free} is of the correct type to be used here if the
663 allocated data doesn't require any special cleanup besides releasing
664 one block of storage.
665 @end deftypefn
666
667 @deftypefn Macro void *k5_getspecific (k5_key_t @var{key})
668 @deftypefnx Macro int k5_setspecific (k5_key_t @var{key}, void *@var{value})
669 As with the POSIX interface, retrieve or store the value for the
670 current thread.  Storing a value may return an error indication.  If
671 an error occurs retrieving a value, @code{NULL} is returned.
672 @end deftypefn
673
674 @deftypefn Macro int k5_key_delete (k5_key_t @var{key})
675 Called to indicate that the key value will no longer be used, for
676 example if the library is in the process of being unloaded.  The
677 destructor function should be called on objects of this type currently
678 allocated in any thread.  (XXX Not implemented yet.)
679 @end deftypefn
680
681 If support functions are needed to implement any of these macros,
682 they'll be in the Kerberos support library, and any exported symbols
683 will use the @code{krb5int_} prefix.  The shorter @code{k5_} prefix is
684 just for convenience, and should not be visible to any application
685 code.
686
687 @node Shared Libraries,  , Thread Safety, Top
688 @chapter Shared Libraries
689
690 (These sections are old -- they should get updated.)
691
692 @menu
693 * Shared Library Theory::       
694 * Advanced Shared Library Requirements::  
695 * Operating System Notes for Shared Libraries::  
696 @end menu
697
698 @node Shared Library Theory, Advanced Shared Library Requirements, Shared Libraries, Shared Libraries
699 @section Theory of How Shared Libraries are Used
700
701 An explanation of how shared libraries are implemented on a given
702 platform is too broad a topic for this manual. Instead this will touch
703 on some of the issues that the Kerberos V5 tree uses to support version
704 numbering and alternate install locations.
705
706 Normally when one builds a shared library and then links with it, the
707 name of the shared library is stored in the object
708 (i.e. libfoo.so). Most operating systems allows one to change name that
709 is referenced and we have done so, placing the version number into the
710 shared library (i.e. libfoo.so.0.1). At link time, one would reference
711 libfoo.so, but when one executes the program, the shared library loader
712 would then look for the shared library with the alternate name.  Hence
713 multiple versions of shared libraries may be supported relatively
714 easily. @footnote{Under AIX for the RISC/6000, multiple versions of
715 shared libraries are supported by combining two or more versions of the
716 shared library into one file.  The Kerberos build procedure produces
717 shared libraries with version numbers in the internal module names, so
718 that the shared libraries are compatible with this scheme.
719 Unfortunately, combining two shared libraries requires internal
720 knowledge of the AIX shared library system beyond the scope of this
721 document.  Practically speaking, only one version of AIX shared libraries
722 can be supported on a system, unless the multi-version library is
723 constructed by a programmer familiar with the AIX internals.}
724
725 All operating systems (that we have seen) provide a means for programs
726 to specify the location of shared libraries. On different operating
727 systems, this is either specified when creating the shared library, and
728 link time, or both.@footnote{Both are necessary sometimes as the shared
729 libraries are dependent on other shared libraries} The build process
730 will hardwire a path to the installed destination.
731
732 @node Advanced Shared Library Requirements, Operating System Notes for Shared Libraries, Shared Library Theory, Shared Libraries
733 @section Advanced Shared Library Requirements
734
735 In order to better support some multithreading models, and permit the
736 libraries to clean up internally maintained caches of information,
737 we've imposed new requirements on the OS shared library support.
738
739 Specifically, we want the ability to run certain bits of code in a
740 thread-safe manner at library load time, on multithreading platforms
741 not supporting @code{pthread_once}, and we want the ability to run
742 cleanup code when the library is unloaded.
743
744 In general, where platforms have initialization functions, we don't
745 always get an opportunity to return an error from them.  However, the
746 system functions we call can return errors.  So a framework has been
747 built that attempts to combine the @code{pthread_once} and load-time
748 initialization approaches, and add the ability to store an error code
749 indicating success or failure of the initialization routine.
750
751 The main implementation of this framework is in @file{k5-platform.h}.
752 Some additional information, specifically the names of the
753 initialization and finalization functions, are stored in the makefiles
754 used to generate each of the UNIX libraries, in @file{win_glue.c}, and
755 somewhere in the Mac OS X support (XXX not added yet?).  How the
756 information is used depends on the platform:
757
758 @itemize @bullet
759
760 @item
761 On platforms without any thread support, a simple flag is used, on the
762 assumption that the library code will have sole control over the
763 process execution until the initialization function returns.  (It's
764 generally a bad idea to call any ``interesting'' function like
765 @code{longjmp} or Kerberos functions from signal handlers; now it's a
766 slightly worse idea.)
767
768 @item
769 On platforms supporting @code{pthread_once}, library initialization is
770 generally delayed until the point where the library code needs to
771 verify that the initialization succeeded.  If @code{pthread_once} may
772 not have been linked into the executable and we can tell (for example,
773 with weak symbol references), this is combined with the simple-flag
774 approach above.
775
776 @item
777 On Windows, the library initialization function is run from
778 @file{win_glue.c} at load time; it should complete before the
779 library's symbol table is made accessible to the calling process.
780
781 @end itemize
782
783 The library finalization code is similarly platform-dependent.  If the
784 compiler or linker lets us specify that a function should be called as
785 a finalization function (for example, @code{gcc}'s ``destructor''
786 attribute), we use it.
787
788 The internal interface currently used within the code of the Kerberos
789 libraries consists of four macros:
790
791 @defmac MAKE_INIT_FUNCTION (@var{fname})
792 Used at the top level of the file (@i{i.e.}, not within a function),
793 with a semicolon after it, declares @var{fname}, a function taking no
794 arguments and returning @code{int}, to be an initialization function.
795 This macro must be used before the function is declared, and it must
796 be defined in the current file as:
797 @example
798 int @var{fname} (void) @{ ... @}
799 @end example
800 This macro will define additional data and possibly function objects,
801 and will declare @var{fname}, though it may or may not declare
802 @var{fname} as @code{static}.  (Under C rules, the declaration above
803 is compatible with a declaration of the function as @code{static}, and
804 @code{static} does apply, as long as the @code{static} declaration
805 comes first.)
806
807 When the function is invoked, the return value --- zero or an error
808 code --- will be saved away, and returned any time
809 @code{CALL_INIT_FUNCTION} is used.
810
811 There may be only one initialization function declared this way in
812 each UNIX library, currently.
813 @end defmac
814
815 @defmac MAKE_FINI_FUNCTION (@var{fname})
816 This is similar to @code{MAKE_INIT_FUNCTION} except that @var{fname}
817 is to be a library finalization function, called when the library is
818 no longer in use and is being unloaded from the address space.
819 @example
820 void @var{fname} (void) @{ ... @}
821 @end example
822
823 There may be only one finalization function declared this way in each
824 UNIX library, currently.
825 @end defmac
826
827 @deftypefn Macro int CALL_INIT_FUNCTION (@var{fname})
828 This macro ensures that the initialization function @var{fname} is
829 called at this point, if it has not been called already.  The macro
830 returns an error code that indicates success (zero), an error in the
831 OS support (@i{e.g.}, if @code{pthread_once} returns an error), or an
832 error returned by the initialization function.
833
834 Currently, all uses of @code{CALL_INIT_FUNCTION} must be in the same
835 file as the use of @code{MAKE_INIT_FUNCTION}, and must come after it.
836 @end deftypefn
837
838 @deftypefn Macro int INITIALIZER_RAN (@var{fname})
839 This macro returns non-zero iff the initialization function designated
840 by @var{fname} (and previously declared in the current file with
841 @code{MAKE_INIT_FUNCTION}) has been run, and returned no error
842 indication.
843
844 Since the finalization function might always be invoked through linker
845 support and initialization functions only sometimes invoked via
846 @code{pthread_once} in other functions that may not ever be called,
847 finalization functions should check whether the objects to be
848 destroyed have actually been created.  This macro provides one way of
849 doing that.
850 @end deftypefn
851
852 Note that all of this assumes shared libraries.  If static linking is
853 done, our options are a bit more limited.  We assume
854 @code{pthread_once} is available if there is any thread support
855 (@i{i.e.}, we don't support static linking on Windows), and we assume
856 that finalization code would be called only when the process is
857 exiting, at which point all resources should be freed up anyways, so
858 it doesn't really matter whether our cleanup code gets called.  In
859 fact, it should be more efficient if it does not.
860
861 While one of our goals is to be able to repeatedly load, use, and
862 unload the MIT Kerberos libraries under a plugin architecture without
863 memory or other resource leaks, the main goal was to provide hooks
864 through which the library threading support could be properly
865 initialized on various platforms.  The hooks we've added should be
866 sufficient for each library to free up any internal caches of
867 information at unload time, and we have added some of that support,
868 but it is not complete at this time.
869
870
871 We have also started limiting the list of exported symbols from shared
872 libraries on some UNIX platforms, and intend to start doing symbol
873 versioning on platforms that support it.  The symbol lists we use for
874 UNIX at the moment are fairly all-inclusive, because we need more
875 symbols exported than are in the lists used for Windows and Mac
876 platforms, and we have not yet narrowed them down.  The current lists
877 should not be taken as an indication of what we intend to export and
878 support in the future; see @file{krb5.h} for that.
879
880 The export lists are stored in the directories in which each UNIX
881 library is built, and the commands set up at configuration time by
882 @file{shlib.conf} can specify any processing to be done on those files
883 (@i{e.g.}, insertion of leading underscores or linker command-line
884 arguments.
885
886 (updated 7/20/2004)
887
888 @node Operating System Notes for Shared Libraries,  , Advanced Shared Library Requirements, Shared Libraries
889 @section Operating System Notes for Shared Libraries
890
891 From time to time users or developers suggest using GNU @code{Libtool}
892 or some other mechanism to  generate shared libraries.  Experience
893 with other packages suggests that Libtool tends to be difficult to
894 debug and when it works incorrectly, patches are required to generated
895 scripts to work around problems.  So far, the Kerberos shared library
896 build mechanism, which sets a variety of makefile variables based on
897 operating system type and then uses those variables in the build
898 process has proven to be easier to debug and adequate to the task of
899 building shared libraries for Kerberos.
900
901 @menu
902 * AIX Shared Library Support::  
903 * Alpha OSF/1 Shared Library Support::  
904 @end menu
905
906 @node AIX Shared Library Support, Alpha OSF/1 Shared Library Support, Operating System Notes for Shared Libraries, Operating System Notes for Shared Libraries
907 @subsection AIX Shared Library Support
908
909         AIX specifies shared library versions by combining multiple
910 versions into a single file.  Because of the complexity of this process,
911 no automatic procedure for building multi-versioned shared libraries is
912 provided. Therefore, supporting multiple versions of the Kerberos shared
913 libraries under AIX will require significant work on the part of a
914 programmer famiiliar with AIX internals.  
915
916         AIX allows a single library to be used both as a static library
917 and as a shared library.  For this reason, the @samp{--enable-shared}
918 switch to configure builds only shared libraries.  On other operating
919 systems, both shared and static libraries are built when this switch is
920 specified.  As with all other operating systems, only non-shared static
921 libraries are built when @samp{--enable-shared} is not specified.
922
923         The AIX 3.2.5 linker dumps core trying to build a shared
924 @samp{libkrb5.a} produced with the GNU C compiler.  The native AIX
925 compiler works fine.  In addition, the AIX 4.1 linker is able to build a
926 shared @samp{libkrb5.a} when GNU C is used.
927
928
929 @node Alpha OSF/1 Shared Library Support,  , AIX Shared Library Support, Operating System Notes for Shared Libraries
930 @subsection Alpha OSF/1 Shared Library Support
931
932 Shared library support has been tested with V2.1 and higher of the
933 operating system. Shared libraries may be compiled both with GCC and the
934 native compiler.
935
936 One of the nice features on this platform is that the paths to the
937 shared libraries is specified in the library itself without requiring
938 that one specify the same at link time. 
939
940 We are using the @samp{-rpath} option to @samp{ld} to place the library
941 load path into the executables. The one disadvantage of this is during
942 testing where we want to make sure that we are using the build tree
943 instead of a possibly installed library. The loader uses the contents of
944 @samp{-rpath} before LD_LIBRARY_PATH so we must specify a dummy _RLD_ROOT
945 and complete LD_LIBRARY_PATH in our tests.
946
947 The one disadvantage with the method we are using....
948
949 @contents
950 @bye