17c478bd9Sstevel@tonic-gate /* 2ba7b222eSGlenn Barry * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate 77c478bd9Sstevel@tonic-gate /* 87c478bd9Sstevel@tonic-gate * k5-platform.h 97c478bd9Sstevel@tonic-gate * 10159d09a2SMark Phalan * Copyright 2003, 2004, 2005 Massachusetts Institute of Technology. 117c478bd9Sstevel@tonic-gate * All Rights Reserved. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may 147c478bd9Sstevel@tonic-gate * require a specific license from the United States Government. 157c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating 167c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting. 17*55fea89dSDan Cross * 187c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 197c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and 207c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright 217c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and 227c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that 237c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining 247c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior 257c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label 267c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a 277c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software. 287c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of 297c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express 307c478bd9Sstevel@tonic-gate * or implied warranty. 31*55fea89dSDan Cross * 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * Some platform-dependent definitions to sync up the C support level. 347c478bd9Sstevel@tonic-gate * Some to a C99-ish level, some related utility code. 357c478bd9Sstevel@tonic-gate * 36505d05c7Sgtb * Currently: 37505d05c7Sgtb * + make "static inline" work 38505d05c7Sgtb * + 64-bit types and load/store code 39505d05c7Sgtb * + SIZE_MAX 40505d05c7Sgtb * + shared library init/fini hooks 41159d09a2SMark Phalan * + consistent getpwnam/getpwuid interfaces 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #ifndef K5_PLATFORM_H 457c478bd9Sstevel@tonic-gate #define K5_PLATFORM_H 467c478bd9Sstevel@tonic-gate 47159d09a2SMark Phalan /* Solaris Kerberos */ 48505d05c7Sgtb #ifndef _KERNEL 49159d09a2SMark Phalan #include <sys/types.h> 50159d09a2SMark Phalan 517c478bd9Sstevel@tonic-gate #include "autoconf.h" 527c478bd9Sstevel@tonic-gate 53505d05c7Sgtb /* Initialization and finalization function support for libraries. 54505d05c7Sgtb 55505d05c7Sgtb At top level, before the functions are defined or even declared: 56505d05c7Sgtb MAKE_INIT_FUNCTION(init_fn); 57505d05c7Sgtb MAKE_FINI_FUNCTION(fini_fn); 58159d09a2SMark Phalan Then: 59505d05c7Sgtb int init_fn(void) { ... } 60505d05c7Sgtb void fini_fn(void) { if (INITIALIZER_RAN(init_fn)) ... } 61505d05c7Sgtb In code, in the same file: 62505d05c7Sgtb err = CALL_INIT_FUNCTION(init_fn); 63505d05c7Sgtb 64505d05c7Sgtb To trigger or verify the initializer invocation from another file, 65159d09a2SMark Phalan a helper function must be created. 66159d09a2SMark Phalan 67159d09a2SMark Phalan This model handles both the load-time execution (Windows) and 68159d09a2SMark Phalan delayed execution (pthread_once) approaches, and should be able to 69159d09a2SMark Phalan guarantee in both cases that the init function is run once, in one 70159d09a2SMark Phalan thread, before other stuff in the library is done; furthermore, the 71159d09a2SMark Phalan finalization code should only run if the initialization code did. 72159d09a2SMark Phalan (Maybe I could've made the "if INITIALIZER_RAN" test implicit, via 73159d09a2SMark Phalan another function hidden in macros, but this is hairy enough 74159d09a2SMark Phalan already.) 75505d05c7Sgtb 76505d05c7Sgtb The init_fn and fini_fn names should be chosen such that any 77505d05c7Sgtb exported names staring with those names, and optionally followed by 78505d05c7Sgtb additional characters, fits in with any namespace constraints on 79505d05c7Sgtb the library in question. 80505d05c7Sgtb 81505d05c7Sgtb 82159d09a2SMark Phalan There's also PROGRAM_EXITING() currently always defined as zero. 83159d09a2SMark Phalan If there's some trivial way to find out if the fini function is 84159d09a2SMark Phalan being called because the program that the library is linked into is 85159d09a2SMark Phalan exiting, we can just skip all the work because the resources are 86159d09a2SMark Phalan about to be freed up anyways. Generally this is likely to be the 87159d09a2SMark Phalan same as distinguishing whether the library was loaded dynamically 88159d09a2SMark Phalan while the program was running, or loaded as part of program 89159d09a2SMark Phalan startup. On most platforms, I don't think we can distinguish these 90159d09a2SMark Phalan cases easily, and it's probably not worth expending any significant 91159d09a2SMark Phalan effort. (Note in particular that atexit() won't do, because if the 92159d09a2SMark Phalan library is explicitly loaded and unloaded, it would have to be able 93159d09a2SMark Phalan to deregister the atexit callback function. Also, the system limit 94159d09a2SMark Phalan on atexit callbacks may be small.) 95159d09a2SMark Phalan 96159d09a2SMark Phalan 97505d05c7Sgtb Implementation outline: 98505d05c7Sgtb 99505d05c7Sgtb Windows: MAKE_FINI_FUNCTION creates a symbol with a magic name that 100505d05c7Sgtb is sought at library build time, and code is added to invoke the 101505d05c7Sgtb function when the library is unloaded. MAKE_INIT_FUNCTION does 102505d05c7Sgtb likewise, but the function is invoked when the library is loaded, 103505d05c7Sgtb and an extra variable is declared to hold an error code and a "yes 104505d05c7Sgtb the initializer ran" flag. CALL_INIT_FUNCTION blows up if the flag 105505d05c7Sgtb isn't set, otherwise returns the error code. 106505d05c7Sgtb 107505d05c7Sgtb UNIX: MAKE_INIT_FUNCTION creates and initializes a variable with a 108505d05c7Sgtb name derived from the function name, containing a k5_once_t 109505d05c7Sgtb (pthread_once_t or int), an error code, and a pointer to the 110505d05c7Sgtb function. The function itself is declared static, but the 111505d05c7Sgtb associated variable has external linkage. CALL_INIT_FUNCTION 112505d05c7Sgtb ensures thath the function is called exactly once (pthread_once or 113505d05c7Sgtb just check the flag) and returns the stored error code (or the 114505d05c7Sgtb pthread_once error). 115505d05c7Sgtb 116159d09a2SMark Phalan (That's the basic idea. With some debugging assert() calls and 117159d09a2SMark Phalan such, it's a bit more complicated. And we also need to handle 118159d09a2SMark Phalan doing the pthread test at run time on systems where that works, so 119159d09a2SMark Phalan we use the k5_once_t stuff instead.) 120159d09a2SMark Phalan 121505d05c7Sgtb UNIX, with compiler support: MAKE_FINI_FUNCTION declares the 122505d05c7Sgtb function as a destructor, and the run time linker support or 123505d05c7Sgtb whatever will cause it to be invoked when the library is unloaded, 124505d05c7Sgtb the program ends, etc. 125505d05c7Sgtb 126505d05c7Sgtb UNIX, with linker support: MAKE_FINI_FUNCTION creates a symbol with 127505d05c7Sgtb a magic name that is sought at library build time, and linker 128505d05c7Sgtb options are used to mark it as a finalization function for the 129505d05c7Sgtb library. The symbol must be exported. 130505d05c7Sgtb 131505d05c7Sgtb UNIX, no library finalization support: The finalization function 132505d05c7Sgtb never runs, and we leak memory. Tough. 133505d05c7Sgtb 134159d09a2SMark Phalan DELAY_INITIALIZER will be defined by the configure script if we 135159d09a2SMark Phalan want to use k5_once instead of load-time initialization. That'll 136159d09a2SMark Phalan be the preferred method on most systems except Windows, where we 137159d09a2SMark Phalan have to initialize some mutexes. 138159d09a2SMark Phalan 139159d09a2SMark Phalan 140505d05c7Sgtb 141505d05c7Sgtb 142505d05c7Sgtb For maximum flexibility in defining the macros, the function name 143505d05c7Sgtb parameter should be a simple name, not even a macro defined as 144505d05c7Sgtb another name. The function should have a unique name, and should 145505d05c7Sgtb conform to whatever namespace is used by the library in question. 146159d09a2SMark Phalan (We do have export lists, but (1) they're not used for all 147159d09a2SMark Phalan platforms, and (2) they're not used for static libraries.) 148505d05c7Sgtb 149505d05c7Sgtb If the macro expansion needs the function to have been declared, it 150505d05c7Sgtb must include a declaration. If it is not necessary for the symbol 151505d05c7Sgtb name to be exported from the object file, the macro should declare 152505d05c7Sgtb it as "static". Hence the signature must exactly match "void 153505d05c7Sgtb foo(void)". (ANSI C allows a static declaration followed by a 154505d05c7Sgtb non-static one; the result is internal linkage.) The macro 155505d05c7Sgtb expansion has to come before the function, because gcc apparently 156505d05c7Sgtb won't act on "__attribute__((constructor))" if it comes after the 157505d05c7Sgtb function definition. 158505d05c7Sgtb 159505d05c7Sgtb This is going to be compiler- and environment-specific, and may 160505d05c7Sgtb require some support at library build time, and/or "asm" 161159d09a2SMark Phalan statements. But through macro expansion and auxiliary functions, 162159d09a2SMark Phalan we should be able to handle most things except #pragma. 163505d05c7Sgtb 164505d05c7Sgtb It's okay for this code to require that the library be built 165505d05c7Sgtb with the same compiler and compiler options throughout, but 166505d05c7Sgtb we shouldn't require that the library and application use the 167505d05c7Sgtb same compiler. 168505d05c7Sgtb 169505d05c7Sgtb For static libraries, we don't really care about cleanup too much, 170505d05c7Sgtb since it's all memory handling and mutex allocation which will all 171505d05c7Sgtb be cleaned up when the program exits. Thus, it's okay if gcc-built 172505d05c7Sgtb static libraries don't play nicely with cc-built executables when 173505d05c7Sgtb it comes to static constructors, just as long as it doesn't cause 174505d05c7Sgtb linking to fail. 175505d05c7Sgtb 176505d05c7Sgtb For dynamic libraries on UNIX, we'll use pthread_once-type support 177505d05c7Sgtb to do delayed initialization, so if finalization can't be made to 178505d05c7Sgtb work, we'll only have memory leaks in a load/use/unload cycle. If 179505d05c7Sgtb anyone (like, say, the OS vendor) complains about this, they can 180505d05c7Sgtb tell us how to get a shared library finalization function invoked 181159d09a2SMark Phalan automatically. 182159d09a2SMark Phalan 183159d09a2SMark Phalan Currently there's --disable-delayed-initialization for preventing 184159d09a2SMark Phalan the initialization from being delayed on UNIX, but that's mainly 185159d09a2SMark Phalan just for testing the linker options for initialization, and will 186159d09a2SMark Phalan probably be removed at some point. */ 187505d05c7Sgtb 188505d05c7Sgtb /* Helper macros. */ 189505d05c7Sgtb 190505d05c7Sgtb # define JOIN__2_2(A,B) A ## _ ## _ ## B 191505d05c7Sgtb # define JOIN__2(A,B) JOIN__2_2(A,B) 192505d05c7Sgtb 193505d05c7Sgtb /* XXX Should test USE_LINKER_INIT_OPTION early, and if it's set, 194505d05c7Sgtb always provide a function by the expected name, even if we're 195505d05c7Sgtb delaying initialization. */ 196505d05c7Sgtb 197505d05c7Sgtb #if defined(DELAY_INITIALIZER) 198505d05c7Sgtb 199505d05c7Sgtb /* Run the initialization code during program execution, at the latest 200505d05c7Sgtb possible moment. This means multiple threads may be active. */ 201505d05c7Sgtb # include "k5-thread.h" 202505d05c7Sgtb typedef struct { k5_once_t once; int error, did_run; void (*fn)(void); } k5_init_t; 203505d05c7Sgtb # ifdef USE_LINKER_INIT_OPTION 204505d05c7Sgtb # define MAYBE_DUMMY_INIT(NAME) \ 205505d05c7Sgtb void JOIN__2(NAME, auxinit) () { } 206505d05c7Sgtb # else 207505d05c7Sgtb # define MAYBE_DUMMY_INIT(NAME) 208505d05c7Sgtb # endif 209159d09a2SMark Phalan # ifdef __GNUC__ 210159d09a2SMark Phalan /* Do it in macro form so we get the file/line of the invocation if 211159d09a2SMark Phalan the assertion fails. */ 212159d09a2SMark Phalan # define k5_call_init_function(I) \ 213159d09a2SMark Phalan (__extension__ ({ \ 214159d09a2SMark Phalan k5_init_t *k5int_i = (I); \ 215159d09a2SMark Phalan int k5int_err = k5_once(&k5int_i->once, k5int_i->fn); \ 216159d09a2SMark Phalan (k5int_err \ 217159d09a2SMark Phalan ? k5int_err \ 218159d09a2SMark Phalan : (assert(k5int_i->did_run != 0), k5int_i->error)); \ 219159d09a2SMark Phalan })) 220159d09a2SMark Phalan # define MAYBE_DEFINE_CALLINIT_FUNCTION 221159d09a2SMark Phalan # else 222159d09a2SMark Phalan # define MAYBE_DEFINE_CALLINIT_FUNCTION \ 223159d09a2SMark Phalan static int k5_call_init_function(k5_init_t *i) \ 224159d09a2SMark Phalan { \ 225159d09a2SMark Phalan int err; \ 226159d09a2SMark Phalan err = k5_once(&i->once, i->fn); \ 227159d09a2SMark Phalan if (err) \ 228159d09a2SMark Phalan return err; \ 229159d09a2SMark Phalan assert (i->did_run != 0); \ 230159d09a2SMark Phalan return i->error; \ 231159d09a2SMark Phalan } 232159d09a2SMark Phalan # endif 233505d05c7Sgtb # define MAKE_INIT_FUNCTION(NAME) \ 234505d05c7Sgtb static int NAME(void); \ 235505d05c7Sgtb MAYBE_DUMMY_INIT(NAME) \ 236505d05c7Sgtb /* forward declaration for use in initializer */ \ 237505d05c7Sgtb static void JOIN__2(NAME, aux) (void); \ 238505d05c7Sgtb static k5_init_t JOIN__2(NAME, once) = \ 239505d05c7Sgtb { K5_ONCE_INIT, 0, 0, JOIN__2(NAME, aux) }; \ 240159d09a2SMark Phalan MAYBE_DEFINE_CALLINIT_FUNCTION \ 241505d05c7Sgtb static void JOIN__2(NAME, aux) (void) \ 242505d05c7Sgtb { \ 243505d05c7Sgtb JOIN__2(NAME, once).did_run = 1; \ 244505d05c7Sgtb JOIN__2(NAME, once).error = NAME(); \ 245505d05c7Sgtb } \ 246505d05c7Sgtb /* so ';' following macro use won't get error */ \ 247505d05c7Sgtb static int NAME(void) 248505d05c7Sgtb # define CALL_INIT_FUNCTION(NAME) \ 249505d05c7Sgtb k5_call_init_function(& JOIN__2(NAME, once)) 250505d05c7Sgtb /* This should be called in finalization only, so we shouldn't have 251505d05c7Sgtb multiple active threads mucking around in our library at this 252505d05c7Sgtb point. So ignore the once_t object and just look at the flag. 253505d05c7Sgtb 254159d09a2SMark Phalan XXX Could we have problems with memory coherence between processors 255159d09a2SMark Phalan if we don't invoke mutex/once routines? Probably not, the 256159d09a2SMark Phalan application code should already be coordinating things such that 257159d09a2SMark Phalan the library code is not in use by this point, and memory 258159d09a2SMark Phalan synchronization will be needed there. */ 259505d05c7Sgtb # define INITIALIZER_RAN(NAME) \ 260505d05c7Sgtb (JOIN__2(NAME, once).did_run && JOIN__2(NAME, once).error == 0) 261505d05c7Sgtb 262505d05c7Sgtb # define PROGRAM_EXITING() (0) 263505d05c7Sgtb 264505d05c7Sgtb #elif defined(__GNUC__) && !defined(_WIN32) && defined(CONSTRUCTOR_ATTR_WORKS) 265505d05c7Sgtb 266505d05c7Sgtb /* Run initializer at load time, via GCC/C++ hook magic. */ 267505d05c7Sgtb 268505d05c7Sgtb # ifdef USE_LINKER_INIT_OPTION 269159d09a2SMark Phalan /* Both gcc and linker option?? Favor gcc. */ 270505d05c7Sgtb # define MAYBE_DUMMY_INIT(NAME) \ 271505d05c7Sgtb void JOIN__2(NAME, auxinit) () { } 272505d05c7Sgtb # else 273505d05c7Sgtb # define MAYBE_DUMMY_INIT(NAME) 274505d05c7Sgtb # endif 275505d05c7Sgtb 276505d05c7Sgtb typedef struct { int error; unsigned char did_run; } k5_init_t; 277505d05c7Sgtb # define MAKE_INIT_FUNCTION(NAME) \ 278505d05c7Sgtb MAYBE_DUMMY_INIT(NAME) \ 279505d05c7Sgtb static k5_init_t JOIN__2(NAME, ran) \ 280505d05c7Sgtb = { 0, 2 }; \ 281505d05c7Sgtb static void JOIN__2(NAME, aux)(void) \ 282505d05c7Sgtb __attribute__((constructor)); \ 283505d05c7Sgtb static int NAME(void); \ 284505d05c7Sgtb static void JOIN__2(NAME, aux)(void) \ 285505d05c7Sgtb { \ 286505d05c7Sgtb JOIN__2(NAME, ran).error = NAME(); \ 287505d05c7Sgtb JOIN__2(NAME, ran).did_run = 3; \ 288505d05c7Sgtb } \ 289505d05c7Sgtb static int NAME(void) 290505d05c7Sgtb # define CALL_INIT_FUNCTION(NAME) \ 291505d05c7Sgtb (JOIN__2(NAME, ran).did_run == 3 \ 292505d05c7Sgtb ? JOIN__2(NAME, ran).error \ 293505d05c7Sgtb : (abort(),0)) 294505d05c7Sgtb # define INITIALIZER_RAN(NAME) (JOIN__2(NAME,ran).did_run == 3 && JOIN__2(NAME, ran).error == 0) 295505d05c7Sgtb 296159d09a2SMark Phalan # define PROGRAM_EXITING() (0) 297159d09a2SMark Phalan 298505d05c7Sgtb #elif defined(USE_LINKER_INIT_OPTION) || defined(_WIN32) 299505d05c7Sgtb 300505d05c7Sgtb /* Run initializer at load time, via linker magic, or in the 301505d05c7Sgtb case of WIN32, win_glue.c hard-coded knowledge. */ 302505d05c7Sgtb typedef struct { int error; unsigned char did_run; } k5_init_t; 303505d05c7Sgtb # define MAKE_INIT_FUNCTION(NAME) \ 304505d05c7Sgtb static k5_init_t JOIN__2(NAME, ran) \ 305505d05c7Sgtb = { 0, 2 }; \ 306505d05c7Sgtb static int NAME(void); \ 307505d05c7Sgtb void JOIN__2(NAME, auxinit)() \ 308505d05c7Sgtb { \ 309505d05c7Sgtb JOIN__2(NAME, ran).error = NAME(); \ 310505d05c7Sgtb JOIN__2(NAME, ran).did_run = 3; \ 311505d05c7Sgtb } \ 312505d05c7Sgtb static int NAME(void) 313505d05c7Sgtb # define CALL_INIT_FUNCTION(NAME) \ 314505d05c7Sgtb (JOIN__2(NAME, ran).did_run == 3 \ 315505d05c7Sgtb ? JOIN__2(NAME, ran).error \ 316505d05c7Sgtb : (abort(),0)) 317505d05c7Sgtb # define INITIALIZER_RAN(NAME) \ 318505d05c7Sgtb (JOIN__2(NAME, ran).error == 0) 319505d05c7Sgtb 320505d05c7Sgtb # define PROGRAM_EXITING() (0) 321505d05c7Sgtb 322505d05c7Sgtb #else 323505d05c7Sgtb 324505d05c7Sgtb # error "Don't know how to do load-time initializers for this configuration." 325505d05c7Sgtb 326505d05c7Sgtb # define PROGRAM_EXITING() (0) 327505d05c7Sgtb 328505d05c7Sgtb #endif 329505d05c7Sgtb 330505d05c7Sgtb 331505d05c7Sgtb 332505d05c7Sgtb #if defined(USE_LINKER_FINI_OPTION) || defined(_WIN32) 333505d05c7Sgtb /* If we're told the linker option will be used, it doesn't really 334505d05c7Sgtb matter what compiler we're using. Do it the same way 335505d05c7Sgtb regardless. */ 336505d05c7Sgtb 337159d09a2SMark Phalan # ifdef __hpux 338159d09a2SMark Phalan 339159d09a2SMark Phalan /* On HP-UX, we need this auxiliary function. At dynamic load or 340159d09a2SMark Phalan unload time (but *not* program startup and termination for 341159d09a2SMark Phalan link-time specified libraries), the linker-indicated function 342159d09a2SMark Phalan is called with a handle on the library and a flag indicating 343159d09a2SMark Phalan whether it's being loaded or unloaded. 344159d09a2SMark Phalan 345159d09a2SMark Phalan The "real" fini function doesn't need to be exported, so 346159d09a2SMark Phalan declare it static. 347159d09a2SMark Phalan 348159d09a2SMark Phalan As usual, the final declaration is just for syntactic 349159d09a2SMark Phalan convenience, so the top-level invocation of this macro can be 350159d09a2SMark Phalan followed by a semicolon. */ 351159d09a2SMark Phalan 352159d09a2SMark Phalan # include <dl.h> 353159d09a2SMark Phalan # define MAKE_FINI_FUNCTION(NAME) \ 354159d09a2SMark Phalan static void NAME(void); \ 355159d09a2SMark Phalan void JOIN__2(NAME, auxfini)(shl_t, int); /* silence gcc warnings */ \ 356159d09a2SMark Phalan void JOIN__2(NAME, auxfini)(shl_t h, int l) { if (!l) NAME(); } \ 357159d09a2SMark Phalan static void NAME(void) 358159d09a2SMark Phalan 359159d09a2SMark Phalan # else /* not hpux */ 360159d09a2SMark Phalan 361159d09a2SMark Phalan # define MAKE_FINI_FUNCTION(NAME) \ 362505d05c7Sgtb void NAME(void) 363505d05c7Sgtb 364159d09a2SMark Phalan # endif 365159d09a2SMark Phalan 366505d05c7Sgtb #elif defined(__GNUC__) && defined(DESTRUCTOR_ATTR_WORKS) 367505d05c7Sgtb /* If we're using gcc, if the C++ support works, the compiler should 368505d05c7Sgtb build executables and shared libraries that support the use of 369505d05c7Sgtb static constructors and destructors. The C compiler supports a 370505d05c7Sgtb function attribute that makes use of the same facility as C++. 371505d05c7Sgtb 372505d05c7Sgtb XXX How do we know if the C++ support actually works? */ 373505d05c7Sgtb # define MAKE_FINI_FUNCTION(NAME) \ 374505d05c7Sgtb static void NAME(void) __attribute__((destructor)) 375505d05c7Sgtb 376505d05c7Sgtb #elif !defined(SHARED) 377505d05c7Sgtb 378505d05c7Sgtb /* In this case, we just don't care about finalization. 379505d05c7Sgtb 380505d05c7Sgtb The code will still define the function, but we won't do anything 381505d05c7Sgtb with it. Annoying: This may generate unused-function warnings. */ 382505d05c7Sgtb 383505d05c7Sgtb # define MAKE_FINI_FUNCTION(NAME) \ 384505d05c7Sgtb static void NAME(void) 385505d05c7Sgtb 386159d09a2SMark Phalan #else 387505d05c7Sgtb 388505d05c7Sgtb # error "Don't know how to do unload-time finalization for this configuration." 389505d05c7Sgtb 390159d09a2SMark Phalan #endif 391505d05c7Sgtb 392505d05c7Sgtb #endif /* !_KERNEL */ 393505d05c7Sgtb 394505d05c7Sgtb 395505d05c7Sgtb /* 64-bit support: krb5_ui_8 and krb5_int64. 396505d05c7Sgtb 3977c478bd9Sstevel@tonic-gate This should move to krb5.h eventually, but without the namespace 3987c478bd9Sstevel@tonic-gate pollution from the autoconf macros. */ 3997c478bd9Sstevel@tonic-gate #if defined(HAVE_STDINT_H) || defined(HAVE_INTTYPES_H) 4007c478bd9Sstevel@tonic-gate # ifdef HAVE_STDINT_H 4017c478bd9Sstevel@tonic-gate # include <stdint.h> 4027c478bd9Sstevel@tonic-gate # endif 4037c478bd9Sstevel@tonic-gate # ifdef HAVE_INTTYPES_H 4047c478bd9Sstevel@tonic-gate # include <inttypes.h> 4057c478bd9Sstevel@tonic-gate # endif 4067c478bd9Sstevel@tonic-gate # define INT64_TYPE int64_t 4077c478bd9Sstevel@tonic-gate # define UINT64_TYPE uint64_t 4087c478bd9Sstevel@tonic-gate #elif defined(_WIN32) 4097c478bd9Sstevel@tonic-gate # define INT64_TYPE signed __int64 4107c478bd9Sstevel@tonic-gate # define UINT64_TYPE unsigned __int64 4117c478bd9Sstevel@tonic-gate #else /* not Windows, and neither stdint.h nor inttypes.h */ 4127c478bd9Sstevel@tonic-gate # define INT64_TYPE signed long long 4137c478bd9Sstevel@tonic-gate # define UINT64_TYPE unsigned long long 4147c478bd9Sstevel@tonic-gate #endif 4157c478bd9Sstevel@tonic-gate 416505d05c7Sgtb #ifndef _KERNEL 417505d05c7Sgtb #include <limits.h> 418505d05c7Sgtb #endif /* !_KERNEL */ 4197c478bd9Sstevel@tonic-gate #ifndef SIZE_MAX 4207c478bd9Sstevel@tonic-gate # define SIZE_MAX ((size_t)((size_t)0 - 1)) 4217c478bd9Sstevel@tonic-gate #endif 4227c478bd9Sstevel@tonic-gate 423505d05c7Sgtb 4247c478bd9Sstevel@tonic-gate /* Read and write integer values as (unaligned) octet strings in 4257c478bd9Sstevel@tonic-gate specific byte orders. 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate Add per-platform optimizations later if needed. (E.g., maybe x86 4287c478bd9Sstevel@tonic-gate unaligned word stores and gcc/asm instructions for byte swaps, 4297c478bd9Sstevel@tonic-gate etc.) */ 4307c478bd9Sstevel@tonic-gate 431159d09a2SMark Phalan /* Solaris Kerberos: To avoid problems with lint the following 432159d09a2SMark Phalan functions can be found in separate header files. */ 433159d09a2SMark Phalan #if 0 434159d09a2SMark Phalan static void 4357c478bd9Sstevel@tonic-gate store_16_be (unsigned int val, unsigned char *p) 4367c478bd9Sstevel@tonic-gate { 4377c478bd9Sstevel@tonic-gate p[0] = (val >> 8) & 0xff; 4387c478bd9Sstevel@tonic-gate p[1] = (val ) & 0xff; 4397c478bd9Sstevel@tonic-gate } 440159d09a2SMark Phalan static void 4417c478bd9Sstevel@tonic-gate store_16_le (unsigned int val, unsigned char *p) 4427c478bd9Sstevel@tonic-gate { 4437c478bd9Sstevel@tonic-gate p[1] = (val >> 8) & 0xff; 4447c478bd9Sstevel@tonic-gate p[0] = (val ) & 0xff; 4457c478bd9Sstevel@tonic-gate } 446159d09a2SMark Phalan static void 4477c478bd9Sstevel@tonic-gate store_32_be (unsigned int val, unsigned char *p) 4487c478bd9Sstevel@tonic-gate { 4497c478bd9Sstevel@tonic-gate p[0] = (val >> 24) & 0xff; 4507c478bd9Sstevel@tonic-gate p[1] = (val >> 16) & 0xff; 4517c478bd9Sstevel@tonic-gate p[2] = (val >> 8) & 0xff; 4527c478bd9Sstevel@tonic-gate p[3] = (val ) & 0xff; 4537c478bd9Sstevel@tonic-gate } 454159d09a2SMark Phalan static void 4557c478bd9Sstevel@tonic-gate store_32_le (unsigned int val, unsigned char *p) 4567c478bd9Sstevel@tonic-gate { 4577c478bd9Sstevel@tonic-gate p[3] = (val >> 24) & 0xff; 4587c478bd9Sstevel@tonic-gate p[2] = (val >> 16) & 0xff; 4597c478bd9Sstevel@tonic-gate p[1] = (val >> 8) & 0xff; 4607c478bd9Sstevel@tonic-gate p[0] = (val ) & 0xff; 4617c478bd9Sstevel@tonic-gate } 462159d09a2SMark Phalan static void 4637c478bd9Sstevel@tonic-gate store_64_be (UINT64_TYPE val, unsigned char *p) 4647c478bd9Sstevel@tonic-gate { 4657c478bd9Sstevel@tonic-gate p[0] = (unsigned char)((val >> 56) & 0xff); 4667c478bd9Sstevel@tonic-gate p[1] = (unsigned char)((val >> 48) & 0xff); 4677c478bd9Sstevel@tonic-gate p[2] = (unsigned char)((val >> 40) & 0xff); 4687c478bd9Sstevel@tonic-gate p[3] = (unsigned char)((val >> 32) & 0xff); 4697c478bd9Sstevel@tonic-gate p[4] = (unsigned char)((val >> 24) & 0xff); 4707c478bd9Sstevel@tonic-gate p[5] = (unsigned char)((val >> 16) & 0xff); 4717c478bd9Sstevel@tonic-gate p[6] = (unsigned char)((val >> 8) & 0xff); 4727c478bd9Sstevel@tonic-gate p[7] = (unsigned char)((val ) & 0xff); 4737c478bd9Sstevel@tonic-gate } 474159d09a2SMark Phalan static void 4757c478bd9Sstevel@tonic-gate store_64_le (UINT64_TYPE val, unsigned char *p) 4767c478bd9Sstevel@tonic-gate { 4777c478bd9Sstevel@tonic-gate p[7] = (unsigned char)((val >> 56) & 0xff); 4787c478bd9Sstevel@tonic-gate p[6] = (unsigned char)((val >> 48) & 0xff); 4797c478bd9Sstevel@tonic-gate p[5] = (unsigned char)((val >> 40) & 0xff); 4807c478bd9Sstevel@tonic-gate p[4] = (unsigned char)((val >> 32) & 0xff); 4817c478bd9Sstevel@tonic-gate p[3] = (unsigned char)((val >> 24) & 0xff); 4827c478bd9Sstevel@tonic-gate p[2] = (unsigned char)((val >> 16) & 0xff); 4837c478bd9Sstevel@tonic-gate p[1] = (unsigned char)((val >> 8) & 0xff); 4847c478bd9Sstevel@tonic-gate p[0] = (unsigned char)((val ) & 0xff); 4857c478bd9Sstevel@tonic-gate } 486159d09a2SMark Phalan static unsigned short 4877c478bd9Sstevel@tonic-gate load_16_be (unsigned char *p) 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate return (p[1] | (p[0] << 8)); 4907c478bd9Sstevel@tonic-gate } 491159d09a2SMark Phalan static unsigned short 4927c478bd9Sstevel@tonic-gate load_16_le (unsigned char *p) 4937c478bd9Sstevel@tonic-gate { 4947c478bd9Sstevel@tonic-gate return (p[0] | (p[1] << 8)); 4957c478bd9Sstevel@tonic-gate } 496505d05c7Sgtb static unsigned int 4977c478bd9Sstevel@tonic-gate load_32_be (unsigned char *p) 4987c478bd9Sstevel@tonic-gate { 4997c478bd9Sstevel@tonic-gate return (p[3] | (p[2] << 8) | (p[1] << 16) | (p[0] << 24)); 5007c478bd9Sstevel@tonic-gate } 501505d05c7Sgtb static unsigned int 5027c478bd9Sstevel@tonic-gate load_32_le (unsigned char *p) 5037c478bd9Sstevel@tonic-gate { 5047c478bd9Sstevel@tonic-gate return (p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24)); 5057c478bd9Sstevel@tonic-gate } 506159d09a2SMark Phalan static UINT64_TYPE 5077c478bd9Sstevel@tonic-gate load_64_be (unsigned char *p) 5087c478bd9Sstevel@tonic-gate { 5097c478bd9Sstevel@tonic-gate return ((UINT64_TYPE)load_32_be(p) << 32) | load_32_be(p+4); 5107c478bd9Sstevel@tonic-gate } 511159d09a2SMark Phalan static UINT64_TYPE 5127c478bd9Sstevel@tonic-gate load_64_le (unsigned char *p) 5137c478bd9Sstevel@tonic-gate { 5147c478bd9Sstevel@tonic-gate return ((UINT64_TYPE)load_32_le(p+4) << 32) | load_32_le(p); 5157c478bd9Sstevel@tonic-gate } 516159d09a2SMark Phalan #endif 517ab9b2e15Sgtb 518ab9b2e15Sgtb /* Make the interfaces to getpwnam and getpwuid consistent. 519ab9b2e15Sgtb Model the wrappers on the POSIX thread-safe versions, but 520ab9b2e15Sgtb use the unsafe system versions if the safe ones don't exist 521ab9b2e15Sgtb or we can't figure out their interfaces. */ 522ab9b2e15Sgtb /* SUNW15resync - just have Solaris relevant ones */ 523ab9b2e15Sgtb 524ab9b2e15Sgtb #define k5_getpwnam_r(NAME, REC, BUF, BUFSIZE, OUT) \ 525ab9b2e15Sgtb (*(OUT) = getpwnam_r(NAME,REC,BUF,BUFSIZE), *(OUT) == NULL ? -1 : 0) 526ab9b2e15Sgtb 527ab9b2e15Sgtb #define k5_getpwuid_r(UID, REC, BUF, BUFSIZE, OUT) \ 528ab9b2e15Sgtb (*(OUT) = getpwuid_r(UID,REC,BUF,BUFSIZE), *(OUT) == NULL ? -1 : 0) 529ab9b2e15Sgtb 530ba7b222eSGlenn Barry /* Return true if the snprintf return value RESULT reflects a buffer 531ba7b222eSGlenn Barry overflow for the buffer size SIZE. 532ba7b222eSGlenn Barry 533ba7b222eSGlenn Barry We cast the result to unsigned int for two reasons. First, old 534ba7b222eSGlenn Barry implementations of snprintf (such as the one in Solaris 9 and 535ba7b222eSGlenn Barry prior) return -1 on a buffer overflow. Casting the result to -1 536ba7b222eSGlenn Barry will convert that value to UINT_MAX, which should compare larger 537ba7b222eSGlenn Barry than any reasonable buffer size. Second, comparing signed and 538ba7b222eSGlenn Barry unsigned integers will generate warnings with some compilers, and 539ba7b222eSGlenn Barry can have unpredictable results, particularly when the relative 540ba7b222eSGlenn Barry widths of the types is not known (size_t may be the same width as 541ba7b222eSGlenn Barry int or larger). 542ba7b222eSGlenn Barry */ 543ba7b222eSGlenn Barry #define SNPRINTF_OVERFLOW(result, size) \ 544ba7b222eSGlenn Barry ((unsigned int)(result) >= (size_t)(size)) 545ab9b2e15Sgtb 5467c478bd9Sstevel@tonic-gate #endif /* K5_PLATFORM_H */ 547