#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <tiuser.h>
#include <sys/utsname.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <values.h>
#include <ctype.h>
#include <time.h>
#include "lsparam.h"
#include "listen.h"
#include "lsfiles.h"
#include "lserror.h"
#include "lsdbf.h"
extern char Lastmsg[];
extern int NLPS_proc;
extern char *Netspec;
extern FILE *Logfp;
extern FILE *Debugfp;
extern char Mytag[];
static char *stamp(char *);
void logmessage(char *s);
void clean_up(int code, int flag, char *msg);
void
error(int code, int exitflag)
{
char scratch[BUFSIZ];
if (!(exitflag & NO_MSG)) {
strcpy(scratch, err_list[code].err_msg);
clean_up(code, exitflag, scratch);
}
clean_up(code, exitflag, NULL);
}
static char *tlirange = "Unknown TLI error (t_errno > t_nerr)";
void
tli_error(int code, int exitflag)
{
void t_error();
char scratch[256];
const char *p;
int save_errno = errno;
p = (t_errno < t_nerr ? t_errlist[t_errno] : tlirange);
(void) snprintf(scratch, sizeof (scratch), "%s: %s",
err_list[code].err_msg, p);
if (t_errno == TSYSERR) {
(void) strlcat(scratch, ": ", sizeof (scratch));
(void) strlcat(scratch, strerror(save_errno), sizeof (scratch));
}
clean_up(code, exitflag, scratch);
}
void
sys_error(int code, int exitflag)
{
char scratch[256];
(void) snprintf(scratch, sizeof (scratch), "%s: %s",
err_list[code].err_msg, strerror(errno));
clean_up(code, exitflag, scratch);
}
void
clean_up(int code, int flag, char *msg)
{
extern int Dbf_entries;
extern void logexit();
extern int NLPS_proc, Nflag;
int i;
extern dbf_t Dbfhead;
dbf_t *dbp = &Dbfhead;
if (!(flag & EXIT)) {
logmessage(msg);
return;
}
if (!(NLPS_proc)) {
for (i=0;i<Dbf_entries;i++) {
t_unbind(dbp->dbf_fd);
dbp++;
}
}
#ifdef COREDUMP
if (!(flag & NOCORE))
abort();
#endif
logexit(err_list[code].err_code, msg);
}
void
logexit(exitcode, msg)
int exitcode;
char *msg;
{
if (msg) {
logmessage(msg);
}
if (!NLPS_proc)
logmessage("*** listener terminating!!! ***");
exit(exitcode);
}
#ifdef DEBUGMODE
int
debug(int level, char *format, ...)
{
char buf[256];
va_list ap;
va_start(ap, format);
(void) vsprintf(buf, format, ap);
va_end(ap);
fprintf(Debugfp, stamp(buf));
fflush(Debugfp);
}
#endif
void
log(int code)
{
logmessage(err_list[code].err_msg);
}
static int nlogs;
void
logmessage(char *s)
{
char log[BUFSIZ];
char olog[BUFSIZ];
int err = 0;
FILE *nlogfp;
extern int Logmax;
extern int Splflag;
if (!Logfp)
return;
if (!NLPS_proc && Logmax && ( nlogs >= Logmax ) && !Splflag) {
nlogs = 0;
fprintf(Logfp, stamp("Restarting log file"));
sprintf(log, "%s/%s/%s", ALTDIR, Mytag, LOGNAME);
sprintf(olog, "%s/%s/%s", ALTDIR, Mytag, OLOGNAME);
DEBUG((1, "Logfile exceeds Logmax (%d) lines", Logmax));
unlink(olog);
if (rename(log, olog)) {
++err;
rewind(Logfp);
DEBUG((1,"errno %d renaming log to old logfile",errno));
}
else if (nlogfp = fopen(log, "a+")) {
fclose(Logfp);
Logfp = nlogfp;
fcntl(fileno(Logfp), F_SETFD, 1);
DEBUG((1, "logmessage: logfile saved successfully"));
} else {
++err;
rewind(Logfp);
DEBUG((1, "Lost the logfile, errno %d", errno));
}
if (err)
fprintf(Logfp, stamp("Trouble saving the logfile"));
}
fprintf(Logfp, stamp(s));
fflush(Logfp);
++nlogs;
}
extern pid_t Pid;
static char *
stamp(char *msg)
{
time_t clock;
struct tm *tm_p;
(void)time(&clock);
tm_p = (struct tm *) localtime(&clock);
tm_p->tm_mon++;
sprintf(Lastmsg, "%2.2d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d; %ld; %s\n",
tm_p->tm_mon, tm_p->tm_mday, (tm_p->tm_year % 100),
tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec, Pid, msg);
return(Lastmsg);
}