1 /* 2 * CDDL HEADER START 3 * 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 7 * with the License. 8 * 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. 13 * 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] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/fcntl.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 #include <stdarg.h> 39 40 #include "extern.h" 41 #include "misc.h" 42 #include "msgs.h" 43 #include <sac.h> 44 #include "structs.h" 45 46 static FILE *Lfp; /* log file */ 47 #ifdef DEBUG 48 static FILE *Dfp; /* debug file */ 49 #endif 50 51 52 /* 53 * cons_printf - emit a message to the system console 54 */ 55 56 /*PRINTFLIKE1*/ 57 static void 58 cons_printf(const char *fmt, ...) 59 { 60 char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */ 61 int fd; 62 va_list ap; 63 64 va_start(ap, fmt); 65 (void) vsnprintf(buf, sizeof (buf), fmt, ap); 66 va_end(ap); 67 68 if ((fd = open("/dev/console", O_WRONLY|O_NOCTTY)) != -1) 69 (void) write(fd, buf, strlen(buf) + 1); 70 (void) close(fd); 71 } 72 73 /* 74 * openlog - open log file, sets global file pointer Lfp 75 */ 76 77 void 78 openlog() 79 { 80 if ((Lfp = fopen(LOGFILE, "a+")) == NULL) { 81 cons_printf("SAC: could not open logfile %s: %s\n", 82 LOGFILE, strerror(errno)); 83 exit(1); 84 } 85 86 /* 87 * lock logfile to indicate presence 88 */ 89 if (lockf(fileno(Lfp), F_LOCK, 0) < 0) { 90 cons_printf("SAC: could not lock logfile %s:%s\n", 91 LOGFILE, strerror(errno)); 92 exit(1); 93 } 94 } 95 96 97 /* 98 * log - put a message into the log file 99 * 100 * args: msg - message to be logged 101 */ 102 void 103 log(char *msg) 104 { 105 char *timestamp; /* current time in readable form */ 106 time_t clock; /* current time in seconds */ 107 char buf[SIZE]; /* scratch buffer */ 108 109 (void) time(&clock); 110 timestamp = ctime(&clock); 111 *(strchr(timestamp, '\n')) = '\0'; 112 (void) sprintf(buf, "%s; %ld; %s\n", timestamp, getpid(), msg); 113 (void) fprintf(Lfp, buf); 114 (void) fflush(Lfp); 115 } 116 117 118 /* 119 * error - put an error message into the log file and exit if indicated 120 * 121 * args: msgid - id of message to be output 122 * action - action to be taken (EXIT or not) 123 */ 124 125 126 void 127 error(int msgid, int action) 128 { 129 if (msgid < 0 || msgid > N_msgs) 130 return; 131 log(Msgs[msgid].e_str); 132 if (action == EXIT) { 133 log("*** SAC exiting ***"); 134 exit(Msgs[msgid].e_exitcode); 135 } 136 } 137 138 139 #ifdef DEBUG 140 141 /* 142 * opendebug - open debugging file, sets global file pointer Dfp 143 */ 144 145 146 void 147 opendebug() 148 { 149 FILE *fp; /* scratch file pointer for problems */ 150 151 if ((Dfp = fopen(DBGFILE, "a+")) == NULL) { 152 cons_printf("SAC: could not open debugfile %s: %s\n", 153 DBGFILE, strerror(errno)); 154 exit(1); 155 } 156 } 157 158 159 /* 160 * debug - put a message into debug file 161 * 162 * args: msg - message to be output 163 */ 164 165 166 void 167 debug(char *msg) 168 { 169 char *timestamp; /* current time in readable form */ 170 time_t clock; /* current time in seconds */ 171 char buf[SIZE]; /* scratch buffer */ 172 173 (void) time(&clock); 174 timestamp = ctime(&clock); 175 *(strchr(timestamp, '\n')) = '\0'; 176 (void) sprintf(buf, "%s; %ld; %s\n", timestamp, getpid(), msg); 177 (void) fprintf(Dfp, buf); 178 (void) fflush(Dfp); 179 } 180 181 #endif /* DEBUG */ 182