xref: /titanic_51/usr/src/cmd/scadm/sparc/mpxu/common/consolelog.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1*03831d35Sstevel /*
2*03831d35Sstevel  * CDDL HEADER START
3*03831d35Sstevel  *
4*03831d35Sstevel  * The contents of this file are subject to the terms of the
5*03831d35Sstevel  * Common Development and Distribution License (the "License").
6*03831d35Sstevel  * You may not use this file except in compliance with the License.
7*03831d35Sstevel  *
8*03831d35Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*03831d35Sstevel  * or http://www.opensolaris.org/os/licensing.
10*03831d35Sstevel  * See the License for the specific language governing permissions
11*03831d35Sstevel  * and limitations under the License.
12*03831d35Sstevel  *
13*03831d35Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
14*03831d35Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*03831d35Sstevel  * If applicable, add the following below this CDDL HEADER, with the
16*03831d35Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
17*03831d35Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
18*03831d35Sstevel  *
19*03831d35Sstevel  * CDDL HEADER END
20*03831d35Sstevel  */
21*03831d35Sstevel 
22*03831d35Sstevel /*
23*03831d35Sstevel  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*03831d35Sstevel  * Use is subject to license terms.
25*03831d35Sstevel  */
26*03831d35Sstevel 
27*03831d35Sstevel #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*03831d35Sstevel 
29*03831d35Sstevel /*
30*03831d35Sstevel  * consolelog.c: support for the scadm consolelog option (to display the
31*03831d35Sstevel  * service processor console log)
32*03831d35Sstevel  */
33*03831d35Sstevel 
34*03831d35Sstevel #include <libintl.h>
35*03831d35Sstevel #include <stdio.h>
36*03831d35Sstevel #include <string.h>
37*03831d35Sstevel #include <time.h>  /* required by librsc.h */
38*03831d35Sstevel #include <limits.h>
39*03831d35Sstevel 
40*03831d35Sstevel #include "librsc.h"
41*03831d35Sstevel #include "adm.h"
42*03831d35Sstevel 
43*03831d35Sstevel /* #define DEBUG */
44*03831d35Sstevel 
45*03831d35Sstevel void
46*03831d35Sstevel ADM_Process_console_log(int all)
47*03831d35Sstevel {
48*03831d35Sstevel 	rscp_msg_t		Message;
49*03831d35Sstevel 	struct timespec		Timeout;
50*03831d35Sstevel 	dp_get_console_log_r_t	*rscReply;
51*03831d35Sstevel 	rsci64			bytes_remaining, seqno;
52*03831d35Sstevel 	rsci16			request_size, response_size;
53*03831d35Sstevel 	dp_get_console_log_t	rscCmd;
54*03831d35Sstevel 
55*03831d35Sstevel 	ADM_Start();
56*03831d35Sstevel 
57*03831d35Sstevel 	/*
58*03831d35Sstevel 	 * Start by sending a zero-length request to ALOM, so that
59*03831d35Sstevel 	 * we can learn the length of the console log.  We expect
60*03831d35Sstevel 	 * ALOM to return the length of the entire log.  We get
61*03831d35Sstevel 	 * a snapshot of the length of the log here - it may however
62*03831d35Sstevel 	 * continue to grow as we're reading it.  We read only as
63*03831d35Sstevel 	 * much of the log as we get in this snapshot.
64*03831d35Sstevel 	 */
65*03831d35Sstevel 	rscCmd.start_seq = 0;
66*03831d35Sstevel 	rscCmd.length = 0;
67*03831d35Sstevel 	Message.type = DP_GET_CONSOLE_LOG;
68*03831d35Sstevel 	Message.len = sizeof (rscCmd);
69*03831d35Sstevel 	Message.data = (char *)&rscCmd;
70*03831d35Sstevel 	ADM_Send(&Message);
71*03831d35Sstevel 
72*03831d35Sstevel 	Timeout.tv_nsec = 0;
73*03831d35Sstevel 	Timeout.tv_sec  = ADM_TIMEOUT;
74*03831d35Sstevel 	ADM_Recv(&Message, &Timeout,
75*03831d35Sstevel 	    DP_GET_CONSOLE_LOG_R, sizeof (*rscReply));
76*03831d35Sstevel 
77*03831d35Sstevel 	rscReply = (dp_get_console_log_r_t *)Message.data;
78*03831d35Sstevel 
79*03831d35Sstevel 	/*
80*03831d35Sstevel 	 * If we do not want the whole log, and the log is bigger than
81*03831d35Sstevel 	 * the length limit, then fetch just the last ADM_DEFAULT_LOG_LENGTH
82*03831d35Sstevel 	 * bytes from the log.  Else just get the whole thing.
83*03831d35Sstevel 	 */
84*03831d35Sstevel 	if ((all == 0) && (rscReply->remaining_log_bytes >
85*03831d35Sstevel 	    ADM_DEFAULT_LOG_LENGTH)) {
86*03831d35Sstevel 		bytes_remaining = ADM_DEFAULT_LOG_LENGTH;
87*03831d35Sstevel 		seqno = (rscReply->remaining_log_bytes +
88*03831d35Sstevel 		    rscReply->next_seq) - bytes_remaining;
89*03831d35Sstevel 	} else {
90*03831d35Sstevel 		bytes_remaining = rscReply->remaining_log_bytes;
91*03831d35Sstevel 		seqno = rscReply->next_seq;
92*03831d35Sstevel 	}
93*03831d35Sstevel 	request_size = sizeof (rscReply->buffer);
94*03831d35Sstevel 	ADM_Free(&Message);
95*03831d35Sstevel 
96*03831d35Sstevel 	/*
97*03831d35Sstevel 	 * Timeout for RSC response.
98*03831d35Sstevel 	 */
99*03831d35Sstevel 	Timeout.tv_nsec = 0;
100*03831d35Sstevel 	Timeout.tv_sec  = ADM_TIMEOUT;
101*03831d35Sstevel 
102*03831d35Sstevel 	/*
103*03831d35Sstevel 	 * This loop runs as long as there is data in the log, or until
104*03831d35Sstevel 	 * we hit the default limit (above).  It's possible that ALOM may
105*03831d35Sstevel 	 * shrink the log - we need to account for this.  If ALOM returns
106*03831d35Sstevel 	 * no data, we bail out.
107*03831d35Sstevel 	 */
108*03831d35Sstevel 	while (bytes_remaining) {
109*03831d35Sstevel 		rscCmd.start_seq = seqno;
110*03831d35Sstevel 		rscCmd.length = (bytes_remaining < request_size) ?
111*03831d35Sstevel 		    bytes_remaining : request_size;
112*03831d35Sstevel 		Message.type = DP_GET_CONSOLE_LOG;
113*03831d35Sstevel 		Message.len = sizeof (rscCmd);
114*03831d35Sstevel 		Message.data = (char *)&rscCmd;
115*03831d35Sstevel 		ADM_Send(&Message);
116*03831d35Sstevel 
117*03831d35Sstevel 		ADM_Recv(&Message, &Timeout,
118*03831d35Sstevel 		    DP_GET_CONSOLE_LOG_R, sizeof (*rscReply));
119*03831d35Sstevel 
120*03831d35Sstevel 		rscReply = (dp_get_console_log_r_t *)Message.data;
121*03831d35Sstevel 
122*03831d35Sstevel 		/* If ALOM returns zero bytes, we're done. */
123*03831d35Sstevel 		response_size = rscReply->length;
124*03831d35Sstevel 		if (response_size == 0) {
125*03831d35Sstevel 			ADM_Free(&Message);
126*03831d35Sstevel 			break;
127*03831d35Sstevel 		}
128*03831d35Sstevel 		bytes_remaining -= response_size;
129*03831d35Sstevel 		if (rscReply->remaining_log_bytes < bytes_remaining) {
130*03831d35Sstevel 			bytes_remaining = rscReply->remaining_log_bytes;
131*03831d35Sstevel 		}
132*03831d35Sstevel 
133*03831d35Sstevel 		/*
134*03831d35Sstevel 		 * If the byte at the original sequence number is no
135*03831d35Sstevel 		 * longer in the log, print a message.
136*03831d35Sstevel 		 */
137*03831d35Sstevel 		if (rscReply->next_seq > seqno + response_size) {
138*03831d35Sstevel 			printf(gettext("\nscadm: lost %d bytes of log data\n"),
139*03831d35Sstevel 			    rscReply->next_seq - (seqno + response_size));
140*03831d35Sstevel 		}
141*03831d35Sstevel 		seqno = rscReply->next_seq;
142*03831d35Sstevel 
143*03831d35Sstevel 		/* Print the console log */
144*03831d35Sstevel 		if (fwrite(rscReply->buffer, sizeof (char), response_size,
145*03831d35Sstevel 		    stdout) != response_size) {
146*03831d35Sstevel 			perror(gettext("\ncouldn't write console log buffer"
147*03831d35Sstevel 			    " to stdout"));
148*03831d35Sstevel 			ADM_Free(&Message);
149*03831d35Sstevel 			break;
150*03831d35Sstevel 		}
151*03831d35Sstevel 		ADM_Free(&Message);
152*03831d35Sstevel 	}
153*03831d35Sstevel 	putchar('\n');
154*03831d35Sstevel }
155