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