17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*3bb2c156SToomas Soome /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * error/logging/cleanup functions for ttymon.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <stdarg.h>
417c478bd9Sstevel@tonic-gate #include <time.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <syslog.h>
477c478bd9Sstevel@tonic-gate #include "ttymon.h"
487c478bd9Sstevel@tonic-gate #include "tmstruct.h"
497c478bd9Sstevel@tonic-gate #include "tmextern.h"
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate const char *appname = "ttymon";
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate void
openttymonlog(void)547c478bd9Sstevel@tonic-gate openttymonlog(void)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate int fd, ret;
577c478bd9Sstevel@tonic-gate char logfile[MAXPATHLEN];
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /* the log file resides in /var/saf/pmtag/ */
607c478bd9Sstevel@tonic-gate (void) snprintf(logfile, sizeof (logfile), "%s%s/%s", LOGDIR, Tag,
617c478bd9Sstevel@tonic-gate LOGFILE);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate Logfp = NULL;
64*3bb2c156SToomas Soome (void) close(0);
65*3bb2c156SToomas Soome if ((fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0444)) != -1)
667c478bd9Sstevel@tonic-gate if ((ret = fcntl(fd, F_DUPFD, 3)) == 3) {
677c478bd9Sstevel@tonic-gate /* set close-on-exec flag */
687c478bd9Sstevel@tonic-gate if (fcntl(ret, F_SETFD, FD_CLOEXEC) == 0) {
697c478bd9Sstevel@tonic-gate Logfp = fdopen(ret, "a+");
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate if (!Logfp) {
737c478bd9Sstevel@tonic-gate cons_printf("ttymon cannot create log file \"%s\": %s\n",
747c478bd9Sstevel@tonic-gate logfile, strerror(errno));
757c478bd9Sstevel@tonic-gate exit(1);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate log(" ");
787c478bd9Sstevel@tonic-gate log("********** ttymon starting **********");
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #ifdef DEBUG
817c478bd9Sstevel@tonic-gate log("fd(log)\t = %d", fileno(Logfp));
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate static void
roll_log(void)86*3bb2c156SToomas Soome roll_log(void)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate char logf[MAXPATHLEN];
897c478bd9Sstevel@tonic-gate char ologf[MAXPATHLEN];
907c478bd9Sstevel@tonic-gate char tlogf[MAXPATHLEN];
917c478bd9Sstevel@tonic-gate FILE *nlogfp;
927c478bd9Sstevel@tonic-gate struct stat buf;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate (void) fprintf(Logfp, "Restarting log file\n");
957c478bd9Sstevel@tonic-gate (void) snprintf(logf, sizeof (logf), "%s%s/%s", LOGDIR, Tag, LOGFILE);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate (void) snprintf(ologf, sizeof (ologf), "%s%s/%s", LOGDIR, Tag,
987c478bd9Sstevel@tonic-gate OLOGFILE);
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate (void) snprintf(tlogf, sizeof (tlogf), "%s%s/%s", LOGDIR, Tag,
1017c478bd9Sstevel@tonic-gate TLOGFILE);
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if (!stat(ologf, &buf) && rename(ologf, tlogf)) {
1047c478bd9Sstevel@tonic-gate (void) fprintf(Logfp, "rename old to tmp file failed\n");
1057c478bd9Sstevel@tonic-gate } else if (!stat(logf, &buf) && rename(logf, ologf)) {
1067c478bd9Sstevel@tonic-gate (void) fprintf(Logfp, "rename log to old file failed\n");
1077c478bd9Sstevel@tonic-gate /* Restore old log file */
1087c478bd9Sstevel@tonic-gate if (!stat(tlogf, &buf) && rename(tlogf, ologf))
1097c478bd9Sstevel@tonic-gate (void) fprintf(Logfp,
1107c478bd9Sstevel@tonic-gate "rename tmp to old file failed\n");
111*3bb2c156SToomas Soome } else if ((nlogfp = fopen(logf, "w")) != NULL) {
1127c478bd9Sstevel@tonic-gate (void) fclose(Logfp);
1137c478bd9Sstevel@tonic-gate Logfp = nlogfp;
1147c478bd9Sstevel@tonic-gate /* reset close-on-exec */
1157c478bd9Sstevel@tonic-gate (void) fcntl(fileno(Logfp), F_SETFD, 1);
1167c478bd9Sstevel@tonic-gate } else {
1177c478bd9Sstevel@tonic-gate (void) fprintf(Logfp, "log file open failed\n");
1187c478bd9Sstevel@tonic-gate /* Restore current and old log file */
1197c478bd9Sstevel@tonic-gate if (!stat(ologf, &buf) && rename(ologf, logf))
1207c478bd9Sstevel@tonic-gate (void) fprintf(Logfp,
1217c478bd9Sstevel@tonic-gate "rename old to log file failed\n");
1227c478bd9Sstevel@tonic-gate else if (!stat(tlogf, &buf) && rename(tlogf, ologf))
1237c478bd9Sstevel@tonic-gate (void) fprintf(Logfp,
1247c478bd9Sstevel@tonic-gate "rename tmp to old file failed\n");
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate (void) unlink(tlogf); /* remove any stale tmp logfile */
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * vlog(msg) - common message routine.
1337c478bd9Sstevel@tonic-gate * - if Logfp is NULL, write message to stderr or CONSOLE
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate static void
vlog(const char * fmt,va_list ap)1367c478bd9Sstevel@tonic-gate vlog(const char *fmt, va_list ap)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate char *timestamp; /* current time in readable form */
1397c478bd9Sstevel@tonic-gate time_t clock; /* current time in seconds */
1407c478bd9Sstevel@tonic-gate int fd;
1417c478bd9Sstevel@tonic-gate struct stat buf;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate if (Logfp) {
1447c478bd9Sstevel@tonic-gate if ((fstat(fileno(Logfp), &buf) != -1) &&
1457c478bd9Sstevel@tonic-gate (buf.st_size >= Logmaxsz) && !Splflag) {
1467c478bd9Sstevel@tonic-gate Splflag = 1;
1477c478bd9Sstevel@tonic-gate roll_log();
1487c478bd9Sstevel@tonic-gate Splflag = 0;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate (void) time(&clock);
1527c478bd9Sstevel@tonic-gate timestamp = ctime(&clock);
1537c478bd9Sstevel@tonic-gate *(strchr(timestamp, '\n')) = '\0';
1547c478bd9Sstevel@tonic-gate (void) fprintf(Logfp, "%s; %ld; ", timestamp, getpid());
1557c478bd9Sstevel@tonic-gate (void) vfprintf(Logfp, fmt, ap);
1567c478bd9Sstevel@tonic-gate if (fmt[strlen(fmt) - 1] != '\n')
1577c478bd9Sstevel@tonic-gate (void) fputc('\n', Logfp);
1587c478bd9Sstevel@tonic-gate (void) fflush(Logfp);
1597c478bd9Sstevel@tonic-gate } else if (isatty(STDERR_FILENO)) {
1607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", appname);
1617c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
1627c478bd9Sstevel@tonic-gate if (fmt[strlen(fmt) - 1] != '\n')
1637c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
1647c478bd9Sstevel@tonic-gate (void) fflush(stderr);
1657c478bd9Sstevel@tonic-gate } else if ((fd = open(CONSOLE, O_WRONLY|O_NOCTTY)) != -1) {
1667c478bd9Sstevel@tonic-gate FILE *f = fdopen(fd, "w");
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate (void) fprintf(f, "%s: ", appname);
1697c478bd9Sstevel@tonic-gate (void) vfprintf(f, fmt, ap);
1707c478bd9Sstevel@tonic-gate if (fmt[strlen(fmt) - 1] != '\n')
1717c478bd9Sstevel@tonic-gate (void) fputc('\n', f);
1727c478bd9Sstevel@tonic-gate (void) fclose(f);
1737c478bd9Sstevel@tonic-gate } else {
1747c478bd9Sstevel@tonic-gate vsyslog(LOG_CRIT, fmt, ap);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * log(fmt, ...) - put a message into the log file
1807c478bd9Sstevel@tonic-gate * - if Logfp is NULL, write message to stderr or CONSOLE
1817c478bd9Sstevel@tonic-gate */
1827c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1837c478bd9Sstevel@tonic-gate void
log(const char * fmt,...)1847c478bd9Sstevel@tonic-gate log(const char *fmt, ...)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate va_list ap;
1877c478bd9Sstevel@tonic-gate va_start(ap, fmt);
1887c478bd9Sstevel@tonic-gate vlog(fmt, ap);
1897c478bd9Sstevel@tonic-gate va_end(ap);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * fatal(fmt, ...) - put a message into the log file, then exit.
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1977c478bd9Sstevel@tonic-gate void
fatal(const char * fmt,...)1987c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate if (fmt) {
2017c478bd9Sstevel@tonic-gate va_list ap;
2027c478bd9Sstevel@tonic-gate va_start(ap, fmt);
2037c478bd9Sstevel@tonic-gate vlog(fmt, ap);
2047c478bd9Sstevel@tonic-gate va_end(ap);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate log("********** ttymon exiting ***********");
2077c478bd9Sstevel@tonic-gate exit(1);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
210*3bb2c156SToomas Soome #ifdef DEBUG
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * opendebug - open debugging file, sets global file pointer Debugfp
214*3bb2c156SToomas Soome * arg: getty - if TRUE, ttymon is in getty_mode and use a different
2157c478bd9Sstevel@tonic-gate * debug file
2167c478bd9Sstevel@tonic-gate */
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate void
opendebug(int getty_mode)2197c478bd9Sstevel@tonic-gate opendebug(int getty_mode)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate int fd, ret;
2227c478bd9Sstevel@tonic-gate char debugfile[BUFSIZ];
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate if (!getty_mode) {
225*3bb2c156SToomas Soome (void) strcpy(debugfile, LOGDIR);
226*3bb2c156SToomas Soome (void) strcat(debugfile, Tag);
227*3bb2c156SToomas Soome (void) strcat(debugfile, "/");
228*3bb2c156SToomas Soome (void) strcat(debugfile, DBGFILE);
2297c478bd9Sstevel@tonic-gate if ((Debugfp = fopen(debugfile, "a+")) == NULL)
2307c478bd9Sstevel@tonic-gate fatal("open debug file failed");
231*3bb2c156SToomas Soome } else {
2327c478bd9Sstevel@tonic-gate if ((fd = open(EX_DBG, O_WRONLY|O_APPEND|O_CREAT)) < 0)
2337c478bd9Sstevel@tonic-gate fatal("open %s failed: %s", EX_DBG, errno);
2347c478bd9Sstevel@tonic-gate
235*3bb2c156SToomas Soome if (fd >= 3) {
2367c478bd9Sstevel@tonic-gate ret = fd;
237*3bb2c156SToomas Soome } else {
2387c478bd9Sstevel@tonic-gate if ((ret = fcntl(fd, F_DUPFD, 3)) < 0)
2397c478bd9Sstevel@tonic-gate fatal("F_DUPFD fcntl failed: %s",
2407c478bd9Sstevel@tonic-gate strerror(errno));
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate if ((Debugfp = fdopen(ret, "a+")) == NULL)
2447c478bd9Sstevel@tonic-gate fatal("fdopen failed: %s", strerror(errno));
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (ret != fd)
247*3bb2c156SToomas Soome (void) close(fd);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate /* set close-on-exec flag */
2507c478bd9Sstevel@tonic-gate if (fcntl(fileno(Debugfp), F_SETFD, 1) == -1)
2517c478bd9Sstevel@tonic-gate fatal("F_SETFD fcntl failed: %s", strerror(errno));
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * debug(msg) - put a message into debug file
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate void
debug(const char * fmt,...)2597c478bd9Sstevel@tonic-gate debug(const char *fmt, ...)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate va_list ap;
2627c478bd9Sstevel@tonic-gate char *timestamp; /* current time in readable form */
2637c478bd9Sstevel@tonic-gate time_t clock; /* current time in seconds */
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate (void) time(&clock);
2667c478bd9Sstevel@tonic-gate timestamp = ctime(&clock);
2677c478bd9Sstevel@tonic-gate *(strchr(timestamp, '\n')) = '\0';
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate (void) fprintf(Debugfp, "%s; %ld; ", timestamp, getpid());
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate va_start(ap, fmt);
2727c478bd9Sstevel@tonic-gate (void) vfprintf(Debugfp, fmt, ap);
2737c478bd9Sstevel@tonic-gate va_end(ap);
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate (void) fprintf(Debugfp, "\n");
2767c478bd9Sstevel@tonic-gate (void) fflush(Debugfp);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate #endif
279