1*6a54128fSAndroid Build Coastguard Worker\input texinfo @c -*-texinfo-*- 2*6a54128fSAndroid Build Coastguard Worker 3*6a54128fSAndroid Build Coastguard Worker@c $Header$ 4*6a54128fSAndroid Build Coastguard Worker@c $Source$ 5*6a54128fSAndroid Build Coastguard Worker@c $Locker$ 6*6a54128fSAndroid Build Coastguard Worker 7*6a54128fSAndroid Build Coastguard Worker@c Note that although this source file is in texinfo format (more 8*6a54128fSAndroid Build Coastguard Worker@c or less), it is not yet suitable for turning into an ``info'' 9*6a54128fSAndroid Build Coastguard Worker@c file. Sorry, maybe next time. 10*6a54128fSAndroid Build Coastguard Worker@c 11*6a54128fSAndroid Build Coastguard Worker@c In order to produce hardcopy documentation from a texinfo file, 12*6a54128fSAndroid Build Coastguard Worker@c run ``tex com_err.texinfo'' which will load in texinfo.tex, 13*6a54128fSAndroid Build Coastguard Worker@c provided in this distribution. (texinfo.tex is from the Free 14*6a54128fSAndroid Build Coastguard Worker@c Software Foundation, and is under different copyright restrictions 15*6a54128fSAndroid Build Coastguard Worker@c from the rest of this package.) 16*6a54128fSAndroid Build Coastguard Worker 17*6a54128fSAndroid Build Coastguard Worker@setfilename com_err.info 18*6a54128fSAndroid Build Coastguard Worker@settitle A Common Error Description Library for UNIX 19*6a54128fSAndroid Build Coastguard Worker 20*6a54128fSAndroid Build Coastguard Worker@ifinfo 21*6a54128fSAndroid Build Coastguard Worker@dircategory Development 22*6a54128fSAndroid Build Coastguard Worker@direntry 23*6a54128fSAndroid Build Coastguard Worker* Com_err: (com_err). A Common Error Description Library for UNIX. 24*6a54128fSAndroid Build Coastguard Worker@end direntry 25*6a54128fSAndroid Build Coastguard Worker@end ifinfo 26*6a54128fSAndroid Build Coastguard Worker 27*6a54128fSAndroid Build Coastguard Worker@c smallbook 28*6a54128fSAndroid Build Coastguard Worker 29*6a54128fSAndroid Build Coastguard Worker@iftex 30*6a54128fSAndroid Build Coastguard Worker@finalout 31*6a54128fSAndroid Build Coastguard Worker@end iftex 32*6a54128fSAndroid Build Coastguard Worker 33*6a54128fSAndroid Build Coastguard Worker@ifinfo 34*6a54128fSAndroid Build Coastguard WorkerThis file documents the use of the Common Error Description library. 35*6a54128fSAndroid Build Coastguard Worker 36*6a54128fSAndroid Build Coastguard WorkerCopyright (C) 1987, 1988 Student Information Processing Board of the 37*6a54128fSAndroid Build Coastguard WorkerMassachusetts Institute of Technology. 38*6a54128fSAndroid Build Coastguard Worker 39*6a54128fSAndroid Build Coastguard WorkerPermission to use, copy, modify, and distribute this software and its 40*6a54128fSAndroid Build Coastguard Workerdocumentation for any purpose and without fee is hereby granted, provided 41*6a54128fSAndroid Build Coastguard Workerthat the above copyright notice appear in all copies and that both that 42*6a54128fSAndroid Build Coastguard Workercopyright notice and this permission notice appear in supporting 43*6a54128fSAndroid Build Coastguard Workerdocumentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be 44*6a54128fSAndroid Build Coastguard Workerused in advertising or publicity pertaining to distribution of the software 45*6a54128fSAndroid Build Coastguard Workerwithout specific, written prior permission. M.I.T. and the M.I.T. S.I.P.B. 46*6a54128fSAndroid Build Coastguard Workermake no representations about the suitability of this software for any 47*6a54128fSAndroid Build Coastguard Workerpurpose. It is provided "as is" without express or implied warranty. 48*6a54128fSAndroid Build Coastguard Worker 49*6a54128fSAndroid Build Coastguard WorkerNote that the file texinfo.tex, provided with this distribution, is from 50*6a54128fSAndroid Build Coastguard Workerthe Free Software Foundation, and is under different copyright restrictions 51*6a54128fSAndroid Build Coastguard Workerfrom the remainder of this package. 52*6a54128fSAndroid Build Coastguard Worker 53*6a54128fSAndroid Build Coastguard Worker@ignore 54*6a54128fSAndroid Build Coastguard WorkerPermission is granted to process this file through Tex and print the 55*6a54128fSAndroid Build Coastguard Workerresults, provided the printed document carries copying permission 56*6a54128fSAndroid Build Coastguard Workernotice identical to this one except for the removal of this paragraph 57*6a54128fSAndroid Build Coastguard Worker(this paragraph not being relevant to the printed manual). 58*6a54128fSAndroid Build Coastguard Worker 59*6a54128fSAndroid Build Coastguard Worker@end ignore 60*6a54128fSAndroid Build Coastguard Worker@end ifinfo 61*6a54128fSAndroid Build Coastguard Worker 62*6a54128fSAndroid Build Coastguard Worker@setchapternewpage odd 63*6a54128fSAndroid Build Coastguard Worker 64*6a54128fSAndroid Build Coastguard Worker@titlepage 65*6a54128fSAndroid Build Coastguard Worker@center @titlefont{A Common Error Description} 66*6a54128fSAndroid Build Coastguard Worker@center @titlefont{Library for UNIX} 67*6a54128fSAndroid Build Coastguard Worker@sp 2 68*6a54128fSAndroid Build Coastguard Worker@center Ken Raeburn 69*6a54128fSAndroid Build Coastguard Worker@center Bill Sommerfeld 70*6a54128fSAndroid Build Coastguard Worker@sp 1 71*6a54128fSAndroid Build Coastguard Worker@center MIT Student Information Processing Board 72*6a54128fSAndroid Build Coastguard Worker@sp 3 73*6a54128fSAndroid Build Coastguard Worker@center last updated 1 January 1989 74*6a54128fSAndroid Build Coastguard Worker@center for version 1.2 75*6a54128fSAndroid Build Coastguard Worker@center ***DRAFT COPY ONLY*** 76*6a54128fSAndroid Build Coastguard Worker 77*6a54128fSAndroid Build Coastguard Worker@vskip 2in 78*6a54128fSAndroid Build Coastguard Worker 79*6a54128fSAndroid Build Coastguard Worker@center @b{Abstract} 80*6a54128fSAndroid Build Coastguard Worker 81*6a54128fSAndroid Build Coastguard WorkerUNIX has always had a clean and simple system call interface, with a 82*6a54128fSAndroid Build Coastguard Workerstandard set of error codes passed between the kernel and user 83*6a54128fSAndroid Build Coastguard Workerprograms. Unfortunately, the same cannot be said of many of the 84*6a54128fSAndroid Build Coastguard Workerlibraries layered on top of the primitives provided by the kernel. 85*6a54128fSAndroid Build Coastguard WorkerTypically, each one has used a different style of indicating errors to 86*6a54128fSAndroid Build Coastguard Workertheir callers, leading to a total hodgepodge of error handling, and 87*6a54128fSAndroid Build Coastguard Workerconsiderable amounts of work for the programmer. This paper describes 88*6a54128fSAndroid Build Coastguard Workera library and associated utilities which allows a more uniform way for 89*6a54128fSAndroid Build Coastguard Workerlibraries to return errors to their callers, and for programs to 90*6a54128fSAndroid Build Coastguard Workerdescribe errors and exceptional conditions to their users. 91*6a54128fSAndroid Build Coastguard Worker 92*6a54128fSAndroid Build Coastguard Worker@page 93*6a54128fSAndroid Build Coastguard Worker@vskip 0pt plus 1filll 94*6a54128fSAndroid Build Coastguard Worker 95*6a54128fSAndroid Build Coastguard WorkerCopyright @copyright{} 1987, 1988 by the Student Information Processing 96*6a54128fSAndroid Build Coastguard WorkerBoard of the Massachusetts Institute of Technology. 97*6a54128fSAndroid Build Coastguard Worker 98*6a54128fSAndroid Build Coastguard WorkerPermission to use, copy, modify, and distribute this software and its 99*6a54128fSAndroid Build Coastguard Workerdocumentation for any purpose and without fee is hereby granted, provided 100*6a54128fSAndroid Build Coastguard Workerthat the above copyright notice appear in all copies and that both that 101*6a54128fSAndroid Build Coastguard Workercopyright notice and this permission notice appear in supporting 102*6a54128fSAndroid Build Coastguard Workerdocumentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be 103*6a54128fSAndroid Build Coastguard Workerused in advertising or publicity pertaining to distribution of the software 104*6a54128fSAndroid Build Coastguard Workerwithout specific, written prior permission. M.I.T. and the M.I.T. S.I.P.B. 105*6a54128fSAndroid Build Coastguard Workermake no representations about the suitability of this software for any 106*6a54128fSAndroid Build Coastguard Workerpurpose. It is provided "as is" without express or implied warranty. 107*6a54128fSAndroid Build Coastguard Worker 108*6a54128fSAndroid Build Coastguard WorkerNote that the file texinfo.tex, provided with this distribution, is from 109*6a54128fSAndroid Build Coastguard Workerthe Free Software Foundation, and is under different copyright restrictions 110*6a54128fSAndroid Build Coastguard Workerfrom the remainder of this package. 111*6a54128fSAndroid Build Coastguard Worker 112*6a54128fSAndroid Build Coastguard Worker@end titlepage 113*6a54128fSAndroid Build Coastguard Worker 114*6a54128fSAndroid Build Coastguard Worker@node Top, Why com_err?, (dir), (dir) 115*6a54128fSAndroid Build Coastguard Worker 116*6a54128fSAndroid Build Coastguard Worker@top A Common Error Description Library for UNIX 117*6a54128fSAndroid Build Coastguard Worker 118*6a54128fSAndroid Build Coastguard WorkerThis manual documents the com_err library. 119*6a54128fSAndroid Build Coastguard Worker 120*6a54128fSAndroid Build Coastguard Worker@menu 121*6a54128fSAndroid Build Coastguard Worker* Why com_err?:: 122*6a54128fSAndroid Build Coastguard Worker* Error codes:: 123*6a54128fSAndroid Build Coastguard Worker* Error table source file:: 124*6a54128fSAndroid Build Coastguard Worker* The error-table compiler:: 125*6a54128fSAndroid Build Coastguard Worker* Run-time support routines:: 126*6a54128fSAndroid Build Coastguard Worker* Coding Conventions:: 127*6a54128fSAndroid Build Coastguard Worker* Building and Installation:: 128*6a54128fSAndroid Build Coastguard Worker* Bug Reports:: 129*6a54128fSAndroid Build Coastguard Worker* Acknowledgements:: 130*6a54128fSAndroid Build Coastguard Worker@end menu 131*6a54128fSAndroid Build Coastguard Worker 132*6a54128fSAndroid Build Coastguard Worker@page 133*6a54128fSAndroid Build Coastguard Worker 134*6a54128fSAndroid Build Coastguard Worker@node Why com_err?, Error codes, Top, Top 135*6a54128fSAndroid Build Coastguard Worker@chapter Why com_err? 136*6a54128fSAndroid Build Coastguard Worker 137*6a54128fSAndroid Build Coastguard WorkerIn building application software packages, a programmer often has to 138*6a54128fSAndroid Build Coastguard Workerdeal with a number of libraries, each of which can use a different 139*6a54128fSAndroid Build Coastguard Workererror-reporting mechanism. Sometimes one of two values is returned, 140*6a54128fSAndroid Build Coastguard Workerindicating simply SUCCESS or FAILURE, with no description of errors 141*6a54128fSAndroid Build Coastguard Workerencountered. Sometimes it is an index into a table of text strings, 142*6a54128fSAndroid Build Coastguard Workerwhere the name of the table used is dependent on the library being 143*6a54128fSAndroid Build Coastguard Workerused when the error is generated; since each table starts numbering at 144*6a54128fSAndroid Build Coastguard Worker0 or 1, additional information as to the source of the error code is 145*6a54128fSAndroid Build Coastguard Workerneeded to determine which table to look at. Sometimes no text messages are 146*6a54128fSAndroid Build Coastguard Workersupplied at all, and the programmer must supply them at any point at which 147*6a54128fSAndroid Build Coastguard Workerhe may wish to report error conditions. 148*6a54128fSAndroid Build Coastguard WorkerOften, a global variable is assigned some value describing the error, but 149*6a54128fSAndroid Build Coastguard Workerthe programmer has to know in each case whether to look at @code{errno}, 150*6a54128fSAndroid Build Coastguard Worker@code{h_errno}, the return value from @code{hes_err()}, or whatever other 151*6a54128fSAndroid Build Coastguard Workervariables or routines are specified. 152*6a54128fSAndroid Build Coastguard WorkerAnd what happens if something 153*6a54128fSAndroid Build Coastguard Workerin the procedure of 154*6a54128fSAndroid Build Coastguard Workerexamining or reporting the error changes the same variable? 155*6a54128fSAndroid Build Coastguard Worker 156*6a54128fSAndroid Build Coastguard WorkerThe package we have developed is an attempt to present a common 157*6a54128fSAndroid Build Coastguard Workererror-handling mechanism to manipulate the most common form of error code 158*6a54128fSAndroid Build Coastguard Workerin a fashion that does not have the problems listed above. 159*6a54128fSAndroid Build Coastguard Worker 160*6a54128fSAndroid Build Coastguard WorkerA list of up to 256 text messages is supplied to a translator we have 161*6a54128fSAndroid Build Coastguard Workerwritten, along with the three- to four-character ``name'' of the error 162*6a54128fSAndroid Build Coastguard Workertable. The library using this error table need only call a routine 163*6a54128fSAndroid Build Coastguard Workergenerated from this error-table source to make the table ``known'' to the 164*6a54128fSAndroid Build Coastguard Workercom_err library, and any error code the library generates can be converted 165*6a54128fSAndroid Build Coastguard Workerto the corresponding error message. There is also a default format for 166*6a54128fSAndroid Build Coastguard Workererror codes accidentally returned before making the table known, which is 167*6a54128fSAndroid Build Coastguard Workerof the form @samp{unknown code foo 32}, where @samp{foo} would be the name 168*6a54128fSAndroid Build Coastguard Workerof the table. 169*6a54128fSAndroid Build Coastguard Worker 170*6a54128fSAndroid Build Coastguard Worker@node Error codes, Error table source file, Why com_err?, Top 171*6a54128fSAndroid Build Coastguard Worker@chapter Error codes 172*6a54128fSAndroid Build Coastguard Worker 173*6a54128fSAndroid Build Coastguard WorkerError codes themselves are 32 bit (signed) integers, of which the high 174*6a54128fSAndroid Build Coastguard Workerorder 24 bits are an identifier of which error table the error code is 175*6a54128fSAndroid Build Coastguard Workerfrom, and the low order 8 bits are a sequential error number within 176*6a54128fSAndroid Build Coastguard Workerthe table. An error code may thus be easily decomposed into its component 177*6a54128fSAndroid Build Coastguard Workerparts. Only the lowest 32 bits of an error code are considered significant 178*6a54128fSAndroid Build Coastguard Workeron systems which support wider values. 179*6a54128fSAndroid Build Coastguard Worker 180*6a54128fSAndroid Build Coastguard WorkerError table 0 is defined to match the UNIX system call error table 181*6a54128fSAndroid Build Coastguard Worker(@code{sys_errlist}); this allows @code{errno} values to be used directly 182*6a54128fSAndroid Build Coastguard Workerin the library (assuming that @code{errno} is of a type with the same width 183*6a54128fSAndroid Build Coastguard Workeras @t{long}). Other error table numbers are formed by compacting together 184*6a54128fSAndroid Build Coastguard Workerthe first four characters of the error table name. The mapping between 185*6a54128fSAndroid Build Coastguard Workercharacters in the name and numeric values in the error code are defined in 186*6a54128fSAndroid Build Coastguard Workera system-independent fashion, so that two systems that can pass integral 187*6a54128fSAndroid Build Coastguard Workervalues between them can reliably pass error codes without loss of meaning; 188*6a54128fSAndroid Build Coastguard Workerthis should work even if the character sets used are not the same. 189*6a54128fSAndroid Build Coastguard Worker(However, if this is to be done, error table 0 should be avoided, since the 190*6a54128fSAndroid Build Coastguard Workerlocal system call error tables may differ.) 191*6a54128fSAndroid Build Coastguard Worker 192*6a54128fSAndroid Build Coastguard WorkerAny variable which is to contain an error code should be declared @t{long}. 193*6a54128fSAndroid Build Coastguard WorkerThe draft proposed American National Standard for C (as of May, 1988) 194*6a54128fSAndroid Build Coastguard Workerrequires that @t{long} variables be at least 32 bits; any system which does 195*6a54128fSAndroid Build Coastguard Workernot support 32-bit @t{long} values cannot make use of this package (nor 196*6a54128fSAndroid Build Coastguard Workermuch other software that assumes an ANSI-C environment base) without 197*6a54128fSAndroid Build Coastguard Workersignificant effort. 198*6a54128fSAndroid Build Coastguard Worker 199*6a54128fSAndroid Build Coastguard Worker@node Error table source file, The error-table compiler, Error codes, Top 200*6a54128fSAndroid Build Coastguard Worker@chapter Error table source file 201*6a54128fSAndroid Build Coastguard Worker 202*6a54128fSAndroid Build Coastguard WorkerThe error table source file begins with the declaration of the table name, 203*6a54128fSAndroid Build Coastguard Workeras 204*6a54128fSAndroid Build Coastguard Worker 205*6a54128fSAndroid Build Coastguard Worker@example 206*6a54128fSAndroid Build Coastguard Workererror_table @var{tablename} 207*6a54128fSAndroid Build Coastguard Worker@end example 208*6a54128fSAndroid Build Coastguard Worker 209*6a54128fSAndroid Build Coastguard WorkerIndividual error codes are 210*6a54128fSAndroid Build Coastguard Workerspecified with 211*6a54128fSAndroid Build Coastguard Worker 212*6a54128fSAndroid Build Coastguard Worker@example 213*6a54128fSAndroid Build Coastguard Workererror_code @var{ERROR_NAME}, @var{"text message"} 214*6a54128fSAndroid Build Coastguard Worker@end example 215*6a54128fSAndroid Build Coastguard Worker 216*6a54128fSAndroid Build Coastguard Workerwhere @samp{ec} can also be used as a short form of @samp{error_code}. To 217*6a54128fSAndroid Build Coastguard Workerindicate the end of the table, use @samp{end}. Thus, a (short) sample 218*6a54128fSAndroid Build Coastguard Workererror table might be: 219*6a54128fSAndroid Build Coastguard Worker 220*6a54128fSAndroid Build Coastguard Worker@example 221*6a54128fSAndroid Build Coastguard Worker 222*6a54128fSAndroid Build Coastguard Worker error_table dsc 223*6a54128fSAndroid Build Coastguard Worker 224*6a54128fSAndroid Build Coastguard Worker error_code DSC_DUP_MTG_NAME, 225*6a54128fSAndroid Build Coastguard Worker "Meeting already exists" 226*6a54128fSAndroid Build Coastguard Worker 227*6a54128fSAndroid Build Coastguard Worker ec DSC_BAD_PATH, 228*6a54128fSAndroid Build Coastguard Worker "A bad meeting pathname was given" 229*6a54128fSAndroid Build Coastguard Worker 230*6a54128fSAndroid Build Coastguard Worker ec DSC_BAD_MODES, 231*6a54128fSAndroid Build Coastguard Worker "Invalid mode for this access control list" 232*6a54128fSAndroid Build Coastguard Worker 233*6a54128fSAndroid Build Coastguard Worker end 234*6a54128fSAndroid Build Coastguard Worker 235*6a54128fSAndroid Build Coastguard Worker@end example 236*6a54128fSAndroid Build Coastguard Worker 237*6a54128fSAndroid Build Coastguard Worker@node The error-table compiler, Run-time support routines, Error table source file, Top 238*6a54128fSAndroid Build Coastguard Worker@chapter The error-table compiler 239*6a54128fSAndroid Build Coastguard Worker 240*6a54128fSAndroid Build Coastguard WorkerThe error table compiler is named @code{compile_et}. It takes one 241*6a54128fSAndroid Build Coastguard Workerargument, the pathname of a file (ending in @samp{.et}, e.g., 242*6a54128fSAndroid Build Coastguard Worker@samp{dsc_err.et}) containing an error table source file. It parses the 243*6a54128fSAndroid Build Coastguard Workererror table, and generates two output files -- a C header file 244*6a54128fSAndroid Build Coastguard Worker(@samp{discuss_err.h}) which contains definitions of the numerical values 245*6a54128fSAndroid Build Coastguard Workerof the error codes defined in the error table, and a C source file which 246*6a54128fSAndroid Build Coastguard Workershould be compiled and linked with the executable. The header file must be 247*6a54128fSAndroid Build Coastguard Workerincluded in the source of a module which wishes to reference the error 248*6a54128fSAndroid Build Coastguard Workercodes defined; the object module generated from the C code may be linked in 249*6a54128fSAndroid Build Coastguard Workerto a program which wishes to use the printed forms of the error codes. 250*6a54128fSAndroid Build Coastguard Worker 251*6a54128fSAndroid Build Coastguard Worker@node Run-time support routines, Coding Conventions, The error-table compiler, Top 252*6a54128fSAndroid Build Coastguard Worker@chapter Run-time support routines 253*6a54128fSAndroid Build Coastguard Worker 254*6a54128fSAndroid Build Coastguard WorkerAny source file which uses the routines supplied with or produced by the 255*6a54128fSAndroid Build Coastguard Workercom_err package should include the header file @file{<com_err.h>}. It 256*6a54128fSAndroid Build Coastguard Workercontains declarations and definitions which may be needed on some systems. 257*6a54128fSAndroid Build Coastguard Worker(Some functions cannot be referenced properly without the return type 258*6a54128fSAndroid Build Coastguard Workerdeclarations in this file. Some functions may work properly on most 259*6a54128fSAndroid Build Coastguard Workerarchitectures even without the header file, but relying on this is not 260*6a54128fSAndroid Build Coastguard Workerrecommended.) 261*6a54128fSAndroid Build Coastguard Worker 262*6a54128fSAndroid Build Coastguard WorkerThe run-time support routines and variables provided via this package 263*6a54128fSAndroid Build Coastguard Workerinclude the following: 264*6a54128fSAndroid Build Coastguard Worker 265*6a54128fSAndroid Build Coastguard Worker@example 266*6a54128fSAndroid Build Coastguard Workervoid initialize_@var{xxxx}_error_table (void); 267*6a54128fSAndroid Build Coastguard Worker@end example 268*6a54128fSAndroid Build Coastguard Worker 269*6a54128fSAndroid Build Coastguard WorkerOne of these routines is built by the error compiler for each error table. 270*6a54128fSAndroid Build Coastguard WorkerIt makes the @var{xxxx} error table ``known'' to the error reporting 271*6a54128fSAndroid Build Coastguard Workersystem. By convention, this routine should be called in the initialization 272*6a54128fSAndroid Build Coastguard Workerroutine of the @var{xxxx} library. If the library has no initialization 273*6a54128fSAndroid Build Coastguard Workerroutine, some combination of routines which form the core of the library 274*6a54128fSAndroid Build Coastguard Workershould ensure that this routine is called. It is not advised to leave it 275*6a54128fSAndroid Build Coastguard Workerthe caller to make this call. 276*6a54128fSAndroid Build Coastguard Worker 277*6a54128fSAndroid Build Coastguard WorkerThere is no harm in calling this routine more than once. 278*6a54128fSAndroid Build Coastguard Worker 279*6a54128fSAndroid Build Coastguard Worker@example 280*6a54128fSAndroid Build Coastguard Worker#define ERROR_TABLE_BASE_@var{xxxx} @var{nnnnn}L 281*6a54128fSAndroid Build Coastguard Worker@end example 282*6a54128fSAndroid Build Coastguard Worker 283*6a54128fSAndroid Build Coastguard WorkerThis symbol contains the value of the first error code entry in the 284*6a54128fSAndroid Build Coastguard Workerspecified table. 285*6a54128fSAndroid Build Coastguard WorkerThis rarely needs be used by the 286*6a54128fSAndroid Build Coastguard Workerprogrammer. 287*6a54128fSAndroid Build Coastguard Worker 288*6a54128fSAndroid Build Coastguard Worker@deftypefun const char *error_message (long @var{code}); 289*6a54128fSAndroid Build Coastguard Worker 290*6a54128fSAndroid Build Coastguard WorkerThis routine returns the character string error message associated 291*6a54128fSAndroid Build Coastguard Workerwith @code{code}; if this is associated with an unknown error table, or 292*6a54128fSAndroid Build Coastguard Workerif the code is associated with a known error table but the code is not 293*6a54128fSAndroid Build Coastguard Workerin the table, a string of the form @samp{Unknown code @var{xxxx nn}} is 294*6a54128fSAndroid Build Coastguard Workerreturned, where @var{xxxx} is the error table name produced by 295*6a54128fSAndroid Build Coastguard Workerreversing the compaction performed on the error table number implied 296*6a54128fSAndroid Build Coastguard Workerby that error code, and @var{nn} is the offset from that base value. 297*6a54128fSAndroid Build Coastguard Worker 298*6a54128fSAndroid Build Coastguard WorkerAlthough this routine is available for use when needed, its use should be 299*6a54128fSAndroid Build Coastguard Workerleft to circumstances which render @code{com_err} (below) unusable. 300*6a54128fSAndroid Build Coastguard Worker 301*6a54128fSAndroid Build Coastguard Worker@end deftypefun 302*6a54128fSAndroid Build Coastguard Worker 303*6a54128fSAndroid Build Coastguard Worker@deftypefun void com_err (const char *@var{whoami}, long @var{error_code}, const char *@var{format}, ...); 304*6a54128fSAndroid Build Coastguard Worker 305*6a54128fSAndroid Build Coastguard WorkerThis routine provides an alternate way to print error messages to 306*6a54128fSAndroid Build Coastguard Workerstandard error; it allows the error message to be passed in as a 307*6a54128fSAndroid Build Coastguard Workerparameter, rather than in an external variable. @emph{Provide grammatical 308*6a54128fSAndroid Build Coastguard Workercontext for ``message.''} 309*6a54128fSAndroid Build Coastguard Worker 310*6a54128fSAndroid Build Coastguard WorkerThe module reporting the error should be passed in via @var{whoami}. 311*6a54128fSAndroid Build Coastguard WorkerIf @var{format} is @code{(char *)NULL}, the formatted message will not be 312*6a54128fSAndroid Build Coastguard Workerprinted. @var{format} may not be omitted. 313*6a54128fSAndroid Build Coastguard Worker 314*6a54128fSAndroid Build Coastguard Worker@end deftypefun 315*6a54128fSAndroid Build Coastguard Worker 316*6a54128fSAndroid Build Coastguard Worker@deftypefun void com_err_va (const char *@var{whoami}, long @var{error_code}, const char *@var{format}, va_list @var{args}); 317*6a54128fSAndroid Build Coastguard Worker 318*6a54128fSAndroid Build Coastguard WorkerThis routine provides an interface, equivalent to @code{com_err} above, 319*6a54128fSAndroid Build Coastguard Workerwhich may be used by higher-level variadic functions (functions which 320*6a54128fSAndroid Build Coastguard Workeraccept variable numbers of arguments). 321*6a54128fSAndroid Build Coastguard Worker 322*6a54128fSAndroid Build Coastguard Worker@end deftypefun 323*6a54128fSAndroid Build Coastguard Worker 324*6a54128fSAndroid Build Coastguard Worker@deftypefun void *set_com_err_hook (void (*@var{proc}) (const char *@var{whoami}, long @var{error_code}, va_list @var{args}) (const char *@var{whoami}, long @var{error_code}, va_list @var{args})); 325*6a54128fSAndroid Build Coastguard Worker 326*6a54128fSAndroid Build Coastguard Worker@deftypefunx void reset_com_err_hook (); 327*6a54128fSAndroid Build Coastguard Worker 328*6a54128fSAndroid Build Coastguard WorkerThese two routines allow a routine to be dynamically substituted for 329*6a54128fSAndroid Build Coastguard Worker@samp{com_err}. After @samp{set_com_err_hook} has been called, 330*6a54128fSAndroid Build Coastguard Workercalls to @samp{com_err} will turn into calls to the new hook routine. 331*6a54128fSAndroid Build Coastguard Worker@samp{reset_com_err_hook} turns off this hook. This may intended to 332*6a54128fSAndroid Build Coastguard Workerbe used in daemons (to use a routine which calls @cite{syslog(3)}), or 333*6a54128fSAndroid Build Coastguard Workerin a window system application (which could pop up a dialogue box). 334*6a54128fSAndroid Build Coastguard Worker 335*6a54128fSAndroid Build Coastguard WorkerIf a program is to be used in an environment in which simply printing 336*6a54128fSAndroid Build Coastguard Workermessages to the @code{stderr} stream would be inappropriate (such as in a 337*6a54128fSAndroid Build Coastguard Workerdaemon program which runs without a terminal attached), 338*6a54128fSAndroid Build Coastguard Worker@code{set_com_err_hook} may be used to redirect output from @code{com_err}. 339*6a54128fSAndroid Build Coastguard WorkerThe following is an example of an error handler which uses @cite{syslog(3)} 340*6a54128fSAndroid Build Coastguard Workeras supplied in BSD 4.3: 341*6a54128fSAndroid Build Coastguard Worker 342*6a54128fSAndroid Build Coastguard Worker@example 343*6a54128fSAndroid Build Coastguard Worker#include <stdio.h> 344*6a54128fSAndroid Build Coastguard Worker#include <stdarg.h> 345*6a54128fSAndroid Build Coastguard Worker#include <syslog.h> 346*6a54128fSAndroid Build Coastguard Worker 347*6a54128fSAndroid Build Coastguard Worker/* extern openlog (const char * name, int logopt, int facility); */ 348*6a54128fSAndroid Build Coastguard Worker/* extern syslog (int priority, char * message, ...); */ 349*6a54128fSAndroid Build Coastguard Worker 350*6a54128fSAndroid Build Coastguard Workervoid hook (const char * whoami, long code, 351*6a54128fSAndroid Build Coastguard Worker const char * format, va_list args) 352*6a54128fSAndroid Build Coastguard Worker@{ 353*6a54128fSAndroid Build Coastguard Worker char buffer[BUFSIZ]; 354*6a54128fSAndroid Build Coastguard Worker static int initialized = 0; 355*6a54128fSAndroid Build Coastguard Worker if (!initialized) @{ 356*6a54128fSAndroid Build Coastguard Worker openlog (whoami, 357*6a54128fSAndroid Build Coastguard Worker LOG_NOWAIT|LOG_CONS|LOG_PID|LOG_NDELAY, 358*6a54128fSAndroid Build Coastguard Worker LOG_DAEMON); 359*6a54128fSAndroid Build Coastguard Worker initialized = 1; 360*6a54128fSAndroid Build Coastguard Worker @} 361*6a54128fSAndroid Build Coastguard Worker vsprintf (buffer, format, args); 362*6a54128fSAndroid Build Coastguard Worker syslog (LOG_ERR, "%s %s", error_message (code), buffer); 363*6a54128fSAndroid Build Coastguard Worker@} 364*6a54128fSAndroid Build Coastguard Worker@end example 365*6a54128fSAndroid Build Coastguard Worker 366*6a54128fSAndroid Build Coastguard WorkerAfter making the call 367*6a54128fSAndroid Build Coastguard Worker@code{set_com_err_hook (hook);}, 368*6a54128fSAndroid Build Coastguard Workerany calls to @code{com_err} will result in messages being sent to the 369*6a54128fSAndroid Build Coastguard Worker@var{syslogd} daemon for logging. 370*6a54128fSAndroid Build Coastguard WorkerThe name of the program, @samp{whoami}, is supplied to the 371*6a54128fSAndroid Build Coastguard Worker@samp{openlog()} call, and the message is formatted into a buffer and 372*6a54128fSAndroid Build Coastguard Workerpassed to @code{syslog}. 373*6a54128fSAndroid Build Coastguard Worker 374*6a54128fSAndroid Build Coastguard WorkerNote that since the extra arguments to @code{com_err} are passed by 375*6a54128fSAndroid Build Coastguard Workerreference via the @code{va_list} value @code{args}, the hook routine may 376*6a54128fSAndroid Build Coastguard Workerplace any form of interpretation on them, including ignoring them. For 377*6a54128fSAndroid Build Coastguard Workerconsistency, @code{printf}-style interpretation is suggested, via 378*6a54128fSAndroid Build Coastguard Worker@code{vsprintf} (or @code{_doprnt} on BSD systems without full support for 379*6a54128fSAndroid Build Coastguard Workerthe ANSI C library). 380*6a54128fSAndroid Build Coastguard Worker 381*6a54128fSAndroid Build Coastguard Worker@end deftypefun 382*6a54128fSAndroid Build Coastguard Worker 383*6a54128fSAndroid Build Coastguard Worker@node Coding Conventions, Building and Installation, Run-time support routines, Top 384*6a54128fSAndroid Build Coastguard Worker@chapter Coding Conventions 385*6a54128fSAndroid Build Coastguard Worker 386*6a54128fSAndroid Build Coastguard WorkerThe following conventions are just some general stylistic conventions 387*6a54128fSAndroid Build Coastguard Workerto follow when writing robust libraries and programs. Conventions 388*6a54128fSAndroid Build Coastguard Workersimilar to this are generally followed inside the UNIX kernel and most 389*6a54128fSAndroid Build Coastguard Workerroutines in the Multics operating system. In general, a routine 390*6a54128fSAndroid Build Coastguard Workereither succeeds (returning a zero error code, and doing some side 391*6a54128fSAndroid Build Coastguard Workereffects in the process), or it fails, doing minimal side effects; in 392*6a54128fSAndroid Build Coastguard Workerany event, any invariant which the library assumes must be maintained. 393*6a54128fSAndroid Build Coastguard Worker 394*6a54128fSAndroid Build Coastguard WorkerIn general, it is not in the domain of non user-interface library 395*6a54128fSAndroid Build Coastguard Workerroutines to write error messages to the user's terminal, or halt the 396*6a54128fSAndroid Build Coastguard Workerprocess. Such forms of ``error handling'' should be reserved for 397*6a54128fSAndroid Build Coastguard Workerfailures of internal invariants and consistency checks only, as it 398*6a54128fSAndroid Build Coastguard Workerprovides the user of the library no way to clean up for himself in the 399*6a54128fSAndroid Build Coastguard Workerevent of total failure. 400*6a54128fSAndroid Build Coastguard Worker 401*6a54128fSAndroid Build Coastguard WorkerLibrary routines which can fail should be set up to return an error 402*6a54128fSAndroid Build Coastguard Workercode. This should usually be done as the return value of the 403*6a54128fSAndroid Build Coastguard Workerfunction; if this is not acceptable, the routine should return a 404*6a54128fSAndroid Build Coastguard Worker``null'' value, and put the error code into a parameter passed by 405*6a54128fSAndroid Build Coastguard Workerreference. 406*6a54128fSAndroid Build Coastguard Worker 407*6a54128fSAndroid Build Coastguard WorkerRoutines which use the first style of interface can be used from 408*6a54128fSAndroid Build Coastguard Workeruser-interface levels of a program as follows: 409*6a54128fSAndroid Build Coastguard Worker 410*6a54128fSAndroid Build Coastguard Worker@example 411*6a54128fSAndroid Build Coastguard Worker@{ 412*6a54128fSAndroid Build Coastguard Worker if ((code = initialize_world(getuid(), random())) != 0) @{ 413*6a54128fSAndroid Build Coastguard Worker com_err("demo", code, 414*6a54128fSAndroid Build Coastguard Worker "when trying to initialize world"); 415*6a54128fSAndroid Build Coastguard Worker exit(1); 416*6a54128fSAndroid Build Coastguard Worker @} 417*6a54128fSAndroid Build Coastguard Worker if ((database = open_database("my_secrets", &code))==NULL) @{ 418*6a54128fSAndroid Build Coastguard Worker com_err("demo", code, 419*6a54128fSAndroid Build Coastguard Worker "while opening my_secrets"); 420*6a54128fSAndroid Build Coastguard Worker exit(1); 421*6a54128fSAndroid Build Coastguard Worker @} 422*6a54128fSAndroid Build Coastguard Worker@} 423*6a54128fSAndroid Build Coastguard Worker@end example 424*6a54128fSAndroid Build Coastguard Worker 425*6a54128fSAndroid Build Coastguard WorkerA caller which fails to check the return status is in error. It is 426*6a54128fSAndroid Build Coastguard Workerpossible to look for code which ignores error returns by using lint; 427*6a54128fSAndroid Build Coastguard Workerlook for error messages of the form ``foobar returns value which is 428*6a54128fSAndroid Build Coastguard Workersometimes ignored'' or ``foobar returns value which is always 429*6a54128fSAndroid Build Coastguard Workerignored.'' 430*6a54128fSAndroid Build Coastguard Worker 431*6a54128fSAndroid Build Coastguard WorkerSince libraries may be built out of other libraries, it is often necessary 432*6a54128fSAndroid Build Coastguard Workerfor the success of one routine to depend on another. When a lower level 433*6a54128fSAndroid Build Coastguard Workerroutine returns an error code, the middle level routine has a few possible 434*6a54128fSAndroid Build Coastguard Workeroptions. It can simply return the error code to its caller after doing 435*6a54128fSAndroid Build Coastguard Workersome form of cleanup, it can substitute one of its own, or it can take 436*6a54128fSAndroid Build Coastguard Workercorrective action of its own and continue normally. For instance, a 437*6a54128fSAndroid Build Coastguard Workerlibrary routine which makes a ``connect'' system call to make a network 438*6a54128fSAndroid Build Coastguard Workerconnection may reflect the system error code @code{ECONNREFUSED} 439*6a54128fSAndroid Build Coastguard Worker(Connection refused) to its caller, or it may return a ``server not 440*6a54128fSAndroid Build Coastguard Workeravailable, try again later,'' or it may try a different server. 441*6a54128fSAndroid Build Coastguard Worker 442*6a54128fSAndroid Build Coastguard WorkerCleanup which is typically necessary may include, but not be limited 443*6a54128fSAndroid Build Coastguard Workerto, freeing allocated memory which will not be needed any more, 444*6a54128fSAndroid Build Coastguard Workerunlocking concurrency locks, dropping reference counts, closing file 445*6a54128fSAndroid Build Coastguard Workerdescriptors, or otherwise undoing anything which the procedure did up 446*6a54128fSAndroid Build Coastguard Workerto this point. When there are a lot of things which can go wrong, it 447*6a54128fSAndroid Build Coastguard Workeris generally good to write one block of error-handling code which is 448*6a54128fSAndroid Build Coastguard Workerbranched to, using a goto, in the event of failure. A common source 449*6a54128fSAndroid Build Coastguard Workerof errors in UNIX programs is failing to close file descriptors on 450*6a54128fSAndroid Build Coastguard Workererror returns; this leaves a number of ``zombied'' file descriptors 451*6a54128fSAndroid Build Coastguard Workeropen, which eventually causes the process to run out of file 452*6a54128fSAndroid Build Coastguard Workerdescriptors and fall over. 453*6a54128fSAndroid Build Coastguard Worker 454*6a54128fSAndroid Build Coastguard Worker@example 455*6a54128fSAndroid Build Coastguard Worker@{ 456*6a54128fSAndroid Build Coastguard Worker FILE *f1=NULL, *f2=NULL, *f3=NULL; 457*6a54128fSAndroid Build Coastguard Worker int status = 0; 458*6a54128fSAndroid Build Coastguard Worker 459*6a54128fSAndroid Build Coastguard Worker if ( (f1 = fopen(FILE1, "r")) == NULL) @{ 460*6a54128fSAndroid Build Coastguard Worker status = errno; 461*6a54128fSAndroid Build Coastguard Worker goto error; 462*6a54128fSAndroid Build Coastguard Worker @} 463*6a54128fSAndroid Build Coastguard Worker 464*6a54128fSAndroid Build Coastguard Worker /* 465*6a54128fSAndroid Build Coastguard Worker * Crunch for a while 466*6a54128fSAndroid Build Coastguard Worker */ 467*6a54128fSAndroid Build Coastguard Worker 468*6a54128fSAndroid Build Coastguard Worker if ( (f2 = fopen(FILE2, "w")) == NULL) @{ 469*6a54128fSAndroid Build Coastguard Worker status = errno; 470*6a54128fSAndroid Build Coastguard Worker goto error; 471*6a54128fSAndroid Build Coastguard Worker @} 472*6a54128fSAndroid Build Coastguard Worker 473*6a54128fSAndroid Build Coastguard Worker if ( (f3 = fopen(FILE3, "a+")) == NULL) @{ 474*6a54128fSAndroid Build Coastguard Worker status = errno; 475*6a54128fSAndroid Build Coastguard Worker goto error; 476*6a54128fSAndroid Build Coastguard Worker @} 477*6a54128fSAndroid Build Coastguard Worker 478*6a54128fSAndroid Build Coastguard Worker /* 479*6a54128fSAndroid Build Coastguard Worker * Do more processing. 480*6a54128fSAndroid Build Coastguard Worker */ 481*6a54128fSAndroid Build Coastguard Worker fclose(f1); 482*6a54128fSAndroid Build Coastguard Worker fclose(f2); 483*6a54128fSAndroid Build Coastguard Worker fclose(f3); 484*6a54128fSAndroid Build Coastguard Worker return 0; 485*6a54128fSAndroid Build Coastguard Worker 486*6a54128fSAndroid Build Coastguard Workererror: 487*6a54128fSAndroid Build Coastguard Worker if (f1) fclose(f1); 488*6a54128fSAndroid Build Coastguard Worker if (f2) fclose(f2); 489*6a54128fSAndroid Build Coastguard Worker if (f3) fclose(f3); 490*6a54128fSAndroid Build Coastguard Worker return status; 491*6a54128fSAndroid Build Coastguard Worker@} 492*6a54128fSAndroid Build Coastguard Worker@end example 493*6a54128fSAndroid Build Coastguard Worker 494*6a54128fSAndroid Build Coastguard Worker@node Building and Installation, Bug Reports, Coding Conventions, Top 495*6a54128fSAndroid Build Coastguard Worker@chapter Building and Installation 496*6a54128fSAndroid Build Coastguard Worker 497*6a54128fSAndroid Build Coastguard WorkerThe distribution of this package will probably be done as a compressed 498*6a54128fSAndroid Build Coastguard Worker``tar''-format file available via anonymous FTP from SIPB.MIT.EDU. 499*6a54128fSAndroid Build Coastguard WorkerRetrieve @samp{pub/com_err.tar.Z} and extract the contents. A subdirectory 500*6a54128fSAndroid Build Coastguard Worker@t{profiled} should be created to hold objects compiled for profiling. 501*6a54128fSAndroid Build Coastguard WorkerRunning ``make all'' should then be sufficient to build the library and 502*6a54128fSAndroid Build Coastguard Workererror-table compiler. The files @samp{libcom_err.a}, 503*6a54128fSAndroid Build Coastguard Worker@samp{libcom_err_p.a}, @samp{com_err.h}, and @samp{compile_et} should be 504*6a54128fSAndroid Build Coastguard Workerinstalled for use; @samp{com_err.3} and @samp{compile_et.1} can also be 505*6a54128fSAndroid Build Coastguard Workerinstalled as manual pages. 506*6a54128fSAndroid Build Coastguard Worker 507*6a54128fSAndroid Build Coastguard Worker@node Bug Reports, Acknowledgements, Building and Installation, Top 508*6a54128fSAndroid Build Coastguard Worker@chapter Bug Reports 509*6a54128fSAndroid Build Coastguard Worker 510*6a54128fSAndroid Build Coastguard WorkerThe principal author of this library is: Ken 511*6a54128fSAndroid Build Coastguard WorkerRaeburn, @t{raeburn@@MIT.EDU}. 512*6a54128fSAndroid Build Coastguard Worker 513*6a54128fSAndroid Build Coastguard WorkerThis version of the com_err library is being maintained by Theodore 514*6a54128fSAndroid Build Coastguard WorkerTs'o, and so bugs and comments should be sent to @t{tytso@@thunk.org}. 515*6a54128fSAndroid Build Coastguard Worker 516*6a54128fSAndroid Build Coastguard Worker 517*6a54128fSAndroid Build Coastguard Worker@node Acknowledgements, , Bug Reports, Top 518*6a54128fSAndroid Build Coastguard Worker@chapter Acknowledgements 519*6a54128fSAndroid Build Coastguard Worker 520*6a54128fSAndroid Build Coastguard WorkerI would like to thank: Bill Sommerfeld, for his help with some of this 521*6a54128fSAndroid Build Coastguard Workerdocumentation, and catching some of the bugs the first time around; 522*6a54128fSAndroid Build Coastguard WorkerHoneywell Information Systems, for not killing off the @emph{Multics} 523*6a54128fSAndroid Build Coastguard Workeroperating system before I had an opportunity to use it; Honeywell's 524*6a54128fSAndroid Build Coastguard Workercustomers, who persuaded them not to do so, for a while; Ted Anderson of 525*6a54128fSAndroid Build Coastguard WorkerCMU, for catching some problems before version 1.2 left the nest; Stan 526*6a54128fSAndroid Build Coastguard WorkerZanarotti and several others of MIT's Student Information Processing Board, 527*6a54128fSAndroid Build Coastguard Workerfor getting us started with ``discuss,'' for which this package was 528*6a54128fSAndroid Build Coastguard Workeroriginally written; and everyone I've talked into --- I mean, asked to read 529*6a54128fSAndroid Build Coastguard Workerthis document and the ``man'' pages. 530*6a54128fSAndroid Build Coastguard Worker 531*6a54128fSAndroid Build Coastguard Worker@contents 532*6a54128fSAndroid Build Coastguard Worker@bye 533