File: dcap_error.c

package info (click to toggle)
dcap 2.47.6-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,400 kB
  • sloc: ansic: 14,239; makefile: 412; xml: 136; python: 75; sh: 58
file content (250 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 *   DCAP - dCache Access Protocol client interface
 *
 *   Copyright (C) 2000,2004 DESY Hamburg DMG-Division.
 *
 *   AUTHOR: Tigran Mkrtchayn (tigran.mkrtchyan@desy.de)
 *
 *   This program can be distributed under the terms of the GNU LGPL.
 *   See the file COPYING.LIB
 *
 */


/*
 * $Id: dcap_error.c,v 1.23 2006-09-22 13:25:46 tigran Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "sysdep.h"
#include "dcap.h"
#include "dcap_errno.h"
#include "system_io.h"
#ifdef WIN32
#    include "dcap_unix2win.h"
#endif

#define DC_MAX_SRV_ERR_MSG 1024

#ifdef _REENTRANT

static TKEY err_key;
static TKEY srvMessage_key;
static TKEY srvMessagePtr_key;
static MUTEX(kLock);
static int err_once = 0;
static int msg_once = 0;
static int msgPtr_once = 0;

/* Local function prototypes */
static char **__dc_srvMessage();
static char * dc_errno2srvMessage();


int *
__dc_errno()
{
	int *en;

	/* only one thread allowed to kreate key*/
	m_lock(&kLock);
	if(!err_once) {
		t_keycreate(&err_key, NULL);
		err_once++;
	}
	m_unlock(&kLock);

	t_getspecific(err_key, &en);
	if( en == NULL ) {
		en = calloc(1, sizeof(int));
		t_setspecific(err_key, (void *)en);
	}

	return en;
}

char **
__dc_srvMessage()
{
	char  **msg;

	/* only one thread allowed to create key*/
	m_lock(&kLock);
	if(!msg_once) {
		t_keycreate(&srvMessage_key, NULL);
		msg_once++;
	}
	m_unlock(&kLock);

	t_getspecific(srvMessage_key, &msg);
	if ( msg == NULL ) {
		msg = calloc(1,sizeof(char *));
		t_setspecific(srvMessage_key, (void *)msg);
	}

	return msg;
}

static char * dc_errno2srvMessage()
{
	char  *sPtr;

	/* only one thread allowed to kreate key*/
	m_lock(&kLock);
	if(!msgPtr_once) {
		t_keycreate(&srvMessagePtr_key, NULL);
		msgPtr_once++;
	}
	m_unlock(&kLock);

	t_getspecific(srvMessagePtr_key, &sPtr);
	if ( sPtr == NULL ) {
		sPtr = calloc(DC_MAX_SRV_ERR_MSG + 1,sizeof(char));
		strcat(sPtr,"Server Error Messages");
		sPtr[DC_MAX_SRV_ERR_MSG] = '\0';
		t_setspecific(srvMessagePtr_key, (void *)sPtr);
	}

	return sPtr;
}


#define dc_errno (*(__dc_errno()))
#define srvMessage (*(__dc_srvMessage()))

#else
int dc_errno = 0;
static char *srvMessage = NULL;
static char srvConstMessage[DC_MAX_SRV_ERR_MSG];
#endif

static const char *const dcap_errlist[] = {
	"OK",
	"Unexpected data on CONTROL connection",
	"Unexpected data on DATA connection",
	"Unexpected data on CONTROL or DATA connection",
	"Message not confirmed",
	"Message not acknowledged",
	"Unexpected message",
	"Remote server has closed connection",
	"Can not resolve server name",
	"Invalid configuration format",
	"Can not read configuration file",
	"Write access to existing file",
	"Can not read non existing file",
	"Not Pnfs file system",
	"Invalid file name",
	"Can not create entry in pNfs",
	"Can not get pNfs ID",
	"Can not create new node",
	"Fail to make a new CONTROL connection",
	"Incorrect config file format",
	"Parser error",
	"System error",
	"Can not open config file",
	"Config file not available",
	"Can not create socket",
	"Unable to connect to server",
	"Server rejected \"hello\"",
	"Bind failed",
	"Memory allocation failed",
	"Invalid flags passed to open",
	"Server error",
	"File not cached",
	"Not valid DCAP url",
	"Unsafe write operation failed",
	"Unrecoverable read error",
	""
};

void
dc_perror(const char *msg)
{
	const char * dc_strerror(int);
	char *se;

	if ((msg != NULL) && strlen(msg)) {
		system_write(2, msg, strlen(msg) );
		system_write(2, " : ", 3);
	}


	system_write(2, dc_strerror(dc_errno), strlen(dc_strerror(dc_errno)) );
	system_write(2, "\n", 1);
	if (errno) {
		se = strerror(errno);
		system_write(2, "System error: ", 14);
		system_write(2, se, strlen(se) );
		system_write(2, "\n", 1);
	}
#ifdef WIN32
	reportWinsockError();
#endif

}

void
dc_error(const char *msg)
{
	dc_perror(msg);
}

void dc_setServerError(const char *msg)
{
    char *p;
    int len;

	if(srvMessage != NULL) {
		free(srvMessage);
		srvMessage = NULL;
	}

	if(msg != NULL) {
		srvMessage = strdup(msg);
		dc_errno = DESRVMSG;
	}

	errno = EIO;

	/* 'p' will point to the buffer for error mesage */
#ifdef _REENTRANT
	p = dc_errno2srvMessage();
#else
	p = srvConstMessage;
#endif /* _REENTRANT */

	len = strlen(msg);
	if(len > DC_MAX_SRV_ERR_MSG) {
		len = DC_MAX_SRV_ERR_MSG;
	}

	strncpy(p, msg, len);
	p[len] = '\0';

	/* there is no need to free 'p', while we get it already allocated */
}


const char * dc_strerror(int errnum)
{
    const char *p;

	if( (errnum > DEMAXERRORNUM) || (errnum < DEOK) ) {
		return "Unknown error";
	}

	if(errnum == DESRVMSG) {
#ifdef _REENTRANT
	p = dc_errno2srvMessage();
#else
	p = srvConstMessage;
#endif /* _REENTRANT */
	}else{
		p = dcap_errlist[errnum];
	}

	return (const char *)p;
}