4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * error/logging/cleanup functions for the network listener process.
38 /* system include files */
46 #include <sys/utsname.h>
47 #include <sys/param.h>
48 #include <sys/types.h>
55 /* listener include files */
57 #include "lsparam.h" /* listener parameters */
58 #include "listen.h" /* listener */
59 #include "lsfiles.h" /* listener files info */
60 #include "lserror.h" /* listener error codes */
63 extern char Lastmsg
[];
70 static char *stamp(char *);
71 void logmessage(char *s
);
72 void clean_up(int code
, int flag
, char *msg
);
75 * error handling and debug routines
76 * most routines take two args: code and exit.
77 * code is a #define in lserror.h.
78 * if EXIT bit in exitflag is non-zero, the routine exits. (see clean_up() )
79 * define COREDUMP to do the obvious.
84 * error: catastrophic error handler
88 error(int code
, int exitflag
)
92 if (!(exitflag
& NO_MSG
)) {
93 strcpy(scratch
, err_list
[code
].err_msg
);
94 clean_up(code
, exitflag
, scratch
);
96 clean_up(code
, exitflag
, NULL
);
100 * tli_error: Deal (appropriately) with an error in a TLI call
103 static char *tlirange
= "Unknown TLI error (t_errno > t_nerr)";
106 tli_error(int code
, int exitflag
)
111 int save_errno
= errno
;
113 p
= (t_errno
< t_nerr
? t_errlist
[t_errno
] : tlirange
);
115 (void) snprintf(scratch
, sizeof (scratch
), "%s: %s",
116 err_list
[code
].err_msg
, p
);
117 if (t_errno
== TSYSERR
) {
118 (void) strlcat(scratch
, ": ", sizeof (scratch
));
119 (void) strlcat(scratch
, strerror(save_errno
), sizeof (scratch
));
121 clean_up(code
, exitflag
, scratch
);
126 * sys_error: error in a system call
130 sys_error(int code
, int exitflag
)
134 (void) snprintf(scratch
, sizeof (scratch
), "%s: %s",
135 err_list
[code
].err_msg
, strerror(errno
));
136 clean_up(code
, exitflag
, scratch
);
141 * clean_up: if 'flag', and main listener is exiting, clean things
142 * up and exit. Dumps core if !(flag & NOCORE).
143 * Tries to send a message to someone if the listener
144 * is exiting due to an error. (Inherrently machine dependent.)
148 clean_up(int code
, int flag
, char *msg
)
150 extern int Dbf_entries
;
151 extern void logexit();
152 extern int NLPS_proc
, Nflag
;
154 extern dbf_t Dbfhead
;
155 dbf_t
*dbp
= &Dbfhead
;
157 if (!(flag
& EXIT
)) {
165 * unbind anything that we bound.
166 * Needs more intelligence.
170 for (i
=0;i
<Dbf_entries
;i
++) {
171 t_unbind(dbp
->dbf_fd
);
177 if (!(flag
& NOCORE
))
179 #endif /* COREDUMP */
181 logexit(err_list
[code
].err_code
, msg
);
186 logexit(exitcode
, msg
)
191 logmessage(msg
); /* put it in the log */
194 logmessage("*** listener terminating!!! ***");
204 debug(int level
, char *format
, ...)
209 va_start(ap
, format
);
210 (void) vsprintf(buf
, format
, ap
);
213 fprintf(Debugfp
, stamp(buf
));
222 * log: given a message number (code), write a message to the logfile
223 * logmessage: given a string, write a message to the logfile
229 logmessage(err_list
[code
].err_msg
);
233 static int nlogs
; /* maintains size of logfile */
246 * The listener may be maintaining the size of it's logfile.
247 * Nothing in here should make the listener abort.
248 * If it can't save the file, it rewinds the existing log.
249 * Note that the algorithm is not exact, child listener's
250 * messages do not affect the parent's count.
255 if (!NLPS_proc
&& Logmax
&& ( nlogs
>= Logmax
) && !Splflag
) {
257 fprintf(Logfp
, stamp("Restarting log file"));
258 sprintf(log
, "%s/%s/%s", ALTDIR
, Mytag
, LOGNAME
);
259 sprintf(olog
, "%s/%s/%s", ALTDIR
, Mytag
, OLOGNAME
);
260 DEBUG((1, "Logfile exceeds Logmax (%d) lines", Logmax
));
261 unlink(olog
); /* remove stale saved logfile */
262 if (rename(log
, olog
)) {
265 DEBUG((1,"errno %d renaming log to old logfile",errno
));
267 else if (nlogfp
= fopen(log
, "a+")) {
270 fcntl(fileno(Logfp
), F_SETFD
, 1); /* reset close-on-exec */
271 DEBUG((1, "logmessage: logfile saved successfully"));
275 DEBUG((1, "Lost the logfile, errno %d", errno
));
278 fprintf(Logfp
, stamp("Trouble saving the logfile"));
281 fprintf(Logfp
, stamp(s
));
295 tm_p
= (struct tm
*) localtime(&clock
);
296 tm_p
->tm_mon
++; /* since months are 0-11 */
297 sprintf(Lastmsg
, "%2.2d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d; %ld; %s\n",
298 tm_p
->tm_mon
, tm_p
->tm_mday
, (tm_p
->tm_year
% 100),
299 tm_p
->tm_hour
, tm_p
->tm_min
, tm_p
->tm_sec
, Pid
, msg
);