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