xref: /titanic_41/usr/src/lib/libunistat/common/spcs_log.c (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <values.h>
35 #include <locale.h>
36 #include <sys/stat.h>
37 #include <strings.h>
38 #include <stdarg.h>
39 #include <sys/param.h>
40 #include <sys/nsctl/nsctl.h>
41 
42 #include <sys/unistat/spcs_s.h>
43 #include <sys/unistat/spcs_s_u.h>
44 #include <sys/unistat/spcs_errors.h>
45 
46 #define	MAX_SESSION_LOG    (10 * 1024 * 1024)	/* allowable log file size */
47 
48 static char	sessionlog[]	 =  "/var/adm/ds.log";
49 static char	sessionlog_bak[] =  "/var/adm/ds.log.bak";
50 
51 static char *spcstime();
52 
53 void
54 spcs_log(const char *product, spcs_s_info_t *status, const char *format, ...)
55 {
56 	struct  stat st;
57 	FILE   *fp = NULL;
58 	struct flock lk;
59 	va_list	ap;
60 
61 	bzero(&lk, sizeof (lk));
62 
63 	/*
64 	 * check the file size, if > than MAX_SESSION_LOG bytes make a .bak
65 	 * and truncate
66 	 */
67 	if (stat(sessionlog, &st) == 0) {
68 		if (st.st_size > MAX_SESSION_LOG) {
69 			rename(sessionlog, sessionlog_bak);
70 		}
71 	}
72 
73 	va_start(ap, format);
74 	if ((fp = fopen(sessionlog, "a")) == (FILE *)NULL)
75 		goto fail;
76 	lk.l_type = F_WRLCK;
77 	lk.l_whence = SEEK_SET;
78 	lk.l_start = (off_t)0;
79 	lk.l_len = (off_t)0;
80 
81 	if (fcntl(fileno(fp), F_SETLKW, &lk) < 0)
82 		goto fail;
83 
84 
85 	fprintf(fp, "%s %s: ", spcstime(), product);
86 	(void) vfprintf(fp, format, ap);
87 	fputs("\n", fp);
88 	if (status)
89 		spcs_s_report(*status, fp);
90 
91 	fflush(fp);
92 
93 	lk.l_type = F_UNLCK;
94 
95 	(void) fcntl(fileno(fp), F_SETLKW, &lk);
96 
97 fail:
98 	if (fp)
99 		fclose(fp);
100 	va_end(ap);
101 }
102 
103 /*
104  * spcstime():  gets current time
105  */
106 static char *
107 spcstime()
108 {
109 	static char timeptr[20];
110 	time_t tnow;
111 
112 	tnow = time((time_t *)0);
113 	cftime(timeptr, "%b %d %T", &tnow);
114 	return (timeptr);
115 }
116