Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_play / rpc / clnt_perror.c
1 #ifndef lint
2 static char sfs_clnt_perror_c_id[] = "@(#)clnt_perror.c     2.1     97/10/23";
3 #endif
4 /* @(#)clnt_perror.c    2.1 88/07/29 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * clnt_perror.c
57  *
58  * Copyright (C) 1984, Sun Microsystems, Inc.
59  *
60  */
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #include "rpc/rpc.h"
66
67 static char *auth_errmsg(enum auth_stat);
68
69 static char *
70 _buf()
71 {
72         static char *buf = NULL;
73         if (buf == NULL)
74                 buf = (char *)malloc(256);
75         return (buf);
76 }
77
78 /*
79  * Print reply error info
80  */
81 char *
82 clnt_sperror(
83         CLIENT *rpch,
84         char *s)
85 {
86         struct rpc_err e;
87         char *err;
88         char *str = _buf();
89         char *strstart = str;
90
91         if (str == 0)
92                 return (0);
93         CLNT_GETERR(rpch, &e);
94
95         (void) sprintf(str, "%s: ", s);  
96         str += strlen(str);
97
98         (void) strcpy(str, clnt_sperrno(e.re_status));  
99         str += strlen(str);
100
101         switch (e.re_status) {
102         case RPC_SUCCESS:
103         case RPC_CANTENCODEARGS:
104         case RPC_CANTDECODERES:
105         case RPC_TIMEDOUT:     
106         case RPC_PROGUNAVAIL:
107         case RPC_PROCUNAVAIL:
108         case RPC_CANTDECODEARGS:
109         case RPC_SYSTEMERROR:
110         case RPC_UNKNOWNHOST:
111         case RPC_UNKNOWNPROTO:
112         case RPC_PMAPFAILURE:
113         case RPC_PROGNOTREGISTERED:
114         case RPC_FAILED:
115                 break;
116
117         case RPC_CANTSEND:
118         case RPC_CANTRECV:
119                 (void) sprintf(str, "; errno = %s",
120                     strerror(e.re_errno)); 
121                 str += strlen(str);
122                 break;
123
124         case RPC_VERSMISMATCH:
125                 (void) sprintf(str,
126                         "; low version = %lu, high version = %lu", 
127                         e.re_vers.low, e.re_vers.high);
128                 str += strlen(str);
129                 break;
130
131         case RPC_AUTHERROR:
132                 err = auth_errmsg(e.re_why);
133                 (void) sprintf(str,"; why = ");
134                 str += strlen(str);
135                 if (err != NULL) {
136                         (void) sprintf(str, "%s",err);
137                 } else {
138                         (void) sprintf(str,
139                                 "(unknown authentication error - %d)",
140                                 (int) e.re_why);
141                 }
142                 str += strlen(str);
143                 break;
144
145         case RPC_PROGVERSMISMATCH:
146                 (void) sprintf(str, 
147                         "; low version = %lu, high version = %lu", 
148                         e.re_vers.low, e.re_vers.high);
149                 str += strlen(str);
150                 break;
151
152         default:        /* unknown */
153                 (void) sprintf(str, 
154                         "; s1 = %lu, s2 = %lu", 
155                         e.re_lb.s1, e.re_lb.s2);
156                 str += strlen(str);
157                 break;
158         }
159         (void) sprintf(str, "\n");
160         return(strstart) ;
161 }
162
163 void
164 clnt_perror(
165         CLIENT *rpch,
166         char *s)
167 {
168         (void) fprintf(stderr,"%s",clnt_sperror(rpch,s));
169 }
170
171
172 struct rpc_errtab {
173         enum clnt_stat status;
174         char *message;
175 };
176
177 static struct rpc_errtab  rpc_errlist[] = {
178         { RPC_SUCCESS, 
179                 "RPC: Success" }, 
180         { RPC_CANTENCODEARGS, 
181                 "RPC: Can't encode arguments" },
182         { RPC_CANTDECODERES, 
183                 "RPC: Can't decode result" },
184         { RPC_CANTSEND, 
185                 "RPC: Unable to send" },
186         { RPC_CANTRECV, 
187                 "RPC: Unable to receive" },
188         { RPC_TIMEDOUT, 
189                 "RPC: Timed out" },
190         { RPC_VERSMISMATCH, 
191                 "RPC: Incompatible versions of RPC" },
192         { RPC_AUTHERROR, 
193                 "RPC: Authentication error" },
194         { RPC_PROGUNAVAIL, 
195                 "RPC: Program unavailable" },
196         { RPC_PROGVERSMISMATCH, 
197                 "RPC: Program/version mismatch" },
198         { RPC_PROCUNAVAIL, 
199                 "RPC: Procedure unavailable" },
200         { RPC_CANTDECODEARGS, 
201                 "RPC: Server can't decode arguments" },
202         { RPC_SYSTEMERROR, 
203                 "RPC: Remote system error" },
204         { RPC_UNKNOWNHOST, 
205                 "RPC: Unknown host" },
206         { RPC_UNKNOWNPROTO,
207                 "RPC: Unknown protocol" },
208         { RPC_PMAPFAILURE, 
209                 "RPC: Port mapper failure" },
210         { RPC_PROGNOTREGISTERED, 
211                 "RPC: Program not registered"},
212         { RPC_FAILED, 
213                 "RPC: Failed (unspecified error)"}
214 };
215
216
217 /*
218  * This interface for use by clntrpc
219  */
220 char *
221 clnt_sperrno(
222         enum clnt_stat stat)
223 {
224         int i;
225
226         for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
227                 if (rpc_errlist[i].status == stat) {
228                         return (rpc_errlist[i].message);
229                 }
230         }
231         return ("RPC: (unknown error code)");
232 }
233
234 void
235 clnt_perrno(
236         enum clnt_stat num)
237 {
238         (void) fprintf(stderr,"%s",clnt_sperrno(num));
239 }
240
241
242 char *
243 clnt_spcreateerror(char *s)
244 {
245         char *sp;
246         char *str = _buf();
247
248         if (str == 0)
249                 return(0);
250         (void) sprintf(str, "%s: ", s);
251         (void) strcat(str, clnt_sperrno(rpc_createerr.cf_stat));
252         switch (rpc_createerr.cf_stat) {
253         case RPC_PMAPFAILURE:
254                 (void) strcat(str, " - ");
255                 (void) strcat(str,
256                     clnt_sperrno(rpc_createerr.cf_error.re_status));
257                 break;
258
259         case RPC_SYSTEMERROR:
260                 sp = strerror(rpc_createerr.cf_error.re_errno);
261                 (void) strcat(str, " - ");
262                 if (rpc_createerr.cf_error.re_errno > 0
263                     && sp != NULL)
264                         (void) strcat(str, sp);
265                 else
266                         (void) sprintf(&str[strlen(str)], "Error %d",
267                             rpc_createerr.cf_error.re_errno);
268                 break;
269         }
270         (void) strcat(str, "\n");
271         return (str);
272 }
273
274 void
275 clnt_pcreateerror(char *s)
276 {
277         (void) fprintf(stderr, "%s", clnt_spcreateerror(s));
278 }
279
280 struct auth_errtab {
281         enum auth_stat status;  
282         char *message;
283 };
284
285 static struct auth_errtab auth_errlist[] = {
286         { AUTH_OK,
287                 "Authentication OK" },
288         { AUTH_BADCRED,
289                 "Invalid client credential" },
290         { AUTH_REJECTEDCRED,
291                 "Server rejected credential" },
292         { AUTH_BADVERF,
293                 "Invalid client verifier" },
294         { AUTH_REJECTEDVERF,
295                 "Server rejected verifier" },
296         { AUTH_TOOWEAK,
297                 "Client credential too weak" },
298         { AUTH_INVALIDRESP,
299                 "Invalid server verifier" },
300         { AUTH_FAILED,
301                 "Failed (unspecified error)" },
302 };
303
304 static char *
305 auth_errmsg(
306         enum auth_stat stat)
307 {
308         int i;
309
310         for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) {
311                 if (auth_errlist[i].status == stat) {
312                         return(auth_errlist[i].message);
313                 }
314         }
315         return(NULL);
316 }