File: liberror.c

package info (click to toggle)
gfarm 2.4.1-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 12,024 kB
  • sloc: ansic: 99,583; sh: 14,214; java: 6,866; makefile: 2,627; python: 771; perl: 325; sql: 130; xml: 50; asm: 37; csh: 2
file content (727 lines) | stat: -rw-r--r-- 22,793 bytes parent folder | download
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
 * $Id: liberror.c 4527 2010-03-12 10:14:44Z n-soda $
 */

#include <pthread.h>
#include <sys/param.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <netdb.h>

#include <gfarm/gfarm.h>

#include "gfutil.h"

#include "liberror.h"
#include "gfpath.h"

static const char *errcode_string[GFARM_ERR_NUMBER] = {
	"success",

	/* classic errno (1..10) */
	"operation not permitted",
	"no such file or directory",
	"no such process",
	"interrupted system call",
	"input/output error",
	"device not configured",
	"argument list too long",
	"exec format error",
	"bad file descriptor",
	"no child process",

	/* non classic, posix (eagain == ewouldblock) */
	"resource temporarily unavailable",

	/* classic errno (12..34) */
	"no memory",
	"permission denied", /* prohibited by access control */
	"bad address",
	"block device required", /* non posix, non x/open */
	"device busy",
	"already exists",
	"cross device link",
	"operation not supported by device",
	"not a directory",
	"is a directory",
	"invalid argument",
	"too many open files in system",
	"too many open files",
	"inappropriate ioctl for device",
	"text file busy", /* non posix */
	"file too large",
	"no space",
	"illegal seek",
	"read only file system",
	"too many links",
	"broken pipe",
	"numerical argument out of domain",
	"result out of range",

	/* ISO-C standard amendment, wide/multibyte-character handling */
	"illegal byte sequence",

	/* POSIX */
	"resource deadlock avoided",
	"file name too long",
	"directory not empty",
	"no locks available",
	"function not implemented",

	/* X/Open */
	"operation now in progress",
	"operation already in progress",
	/* X/Open - ipc/network software -- argument errors */
	"socket operation on non socket",
	"destination address required",
	"message too long",
	"protocol wrong type for socket",
	"protocol not available",
	"protocol not supported",
	"operation not supported",
	"address family not supported by protocol family",
	"address already in use",
	"cannot assign requested address",
	/* X/Open - ipc/network software -- operational errors */
	"network is down",
	"network is unreachable",
	"connection aborted",
	"connection reset by peer",
	"no buffer space available",
	"socket is already connected",
	"socket is not connected",
	"operation timed out",
	"connection refused",
	"no route to host",
	/* X/Open */
	"too many levels of symbolic link",
	"disk quota exceeded",
	"stale file handle",
	/* X/Open - system-v ipc */
	"identifier removed",
	"no message of desired type",
	"value too large to be stored in data type",

	/* ipc/network errors */
	"authentication error",
	"expired",
	"protocol error",
	"unknown host",
	"cannot resolve an IP address into a hostname",

	/* gfarm specific errors */
	"no such object",
	"can't open",
	"unexpected EOF",
	"gfarm URL prefix is missing",
	"too many jobs",
	"file migrated",
	"not a symbolic link",
	"is a symbolic link",
	"unknown error",
	"invalid file replica",
	"no such user",
	"cannot remove the last replica",
	"no such group",
	"username is missing in a Gfarm URL",
	"hostname is missing in a Gfarm URL",
	"port number is missing in a Gfarm URL",
	"port number is invalid in a Gfarm URL",
	"file busy",
};

static const char *errmsg_string[GFARM_ERRMSG_END - GFARM_ERRMSG_BEGIN] = {
	"inconsistent metadata fixed, try again",
	"libgfarm internal error: too many error domain",

	"unknown keyword",
	"unknown authentication method",

	/* refered only from gfarm/hostspec.c */
	"hostname or IP address expected",
	"invalid character in hostname",
	"invalid character in IP address",
	"too big byte value in IP address",
	"IP address expected",
	"IP address less than 4 bytes",
	"too big netmask",
	"invalid name record",
	"reverse-lookup-name isn't resolvable",
	"reverse-lookup-name doesn't match, possibly forged",

	/* refered only from gfarm/param.c */
	"value isn't specified",
	"value is empty",
	"value isn't allowed for boolean",
	"invalid character in value",
	"unknown parameter",

	/* refered only from gfarm/sockopt.c */
	"getprotobyname(3) failed",
	"unknown socket option",

	/* refered only from gfarm/config.c */
	"missing argument",
	"too many arguments",
	"integer expected",
	"floating point number expected",
	"invalid character",
	"\"enable\" or \"disable\" is expected",
	"invalid syslog priority level",
	"local user name redefined",
	"global user name redefined",
	"local group name redefined",
	"global group name redefined",
	"missing second field (local user)",
	"inconsistent configuration, LDAP is specified as metadata backend before",
	"inconsistent configuration, PostgreSQL is specified as metadata backend before",
	"inconsistent configuration, localfs is specified as metadata backend before",
	"unterminated single quote",
	"unterminated double quote",
	"incomplete escape: \\",
	"missing 1st(auth-command) argument",
	"missing 2nd(auth-method) argument",
	"missing 3rd(host-spec) argument",
	"unknown auth subcommand",
	"missing 1st(netparam-option) argument",
	"missing 1st(sockopt-option) argument",
	"missing <address> argument",
	"missing <user map file> argument",
	"missing <group map file> argument",
	"missing 1st(architecture) argument",
	"missing 2nd(host-spec) argument",
	"cannot open gfarm2.conf",

	/* refered only from gfarm/gfp_xdr.c */
	"gfp_xdr_send: invalid format character",
	"gfp_xdr_recv: invalid format character",
	"gfp_xdr_vrpc: missing result in format string",
	"gfp_xdr_vrpc: invalid format character",

	/* refered only from gfarm/auth_common.c */
	"gfarm sharedsecret key - invalid expire field",
	"gfarm sharedsecret key - invalid key field",
	"gfarm sharedsecret key - key file not exist",

	/* refered only from gfarm/auth_client.c */
	"gfarm auth method isn't available for the host",
	"usable auth-method isn't configured",
	"gfarm_auth_request_sharedsecret: implementaton error",
	"gfarm_auth_request: implementation error",
	"gfarm_auth_request_sharedsecret_multiplexed: implementaton error",
	"gfarm_auth_request_multiplexed: implementation error",

	/* refered only from gfarm/auth_server.c */
	"gfarm_auth_sharedsecret_md5_response: key mismatch, continue",

	/* refered only from gfarm/auth_client_gsi.c */
	"GSI credential initialization failed",
	"GSI initialization failed",
	"cannot initiate GSI connection",
	"cannot acquire client-side GSI credential",

	/* refered only from gfarm/auth_config.c */
	"unknown cred_type",

	/* refered only from gfarm/io_gfsl.c */
	"GSI delegated credential doesn't exist",
	"cannot export GSI delegated credential",

	/* refered only from gfarm/gfs_client.c */
	"gfsd aborted",
	"illegal gfsd descriptor",
	"unknown gfsd reply",
	"GFS_PROTO_PREAD: protocol error",
	"GFS_PROTO_WRITE: protocol error",

	/* refered from gfarm/gfs_pio.c and related modules */
	"internal error: end of file with gfs_pio",

	/* refered only from gfarm/glob.c */
	"gfs_glob(): gfarm library isn't properly initialized",

	/* refered only from gfarm/schedule.c */
	"no filesystem node",

	/* refered only from gfarm/auth_common_gsi.c */
	"cred_type is not set, but cred_name is set",
	"cred_type is not set, but cred_service is set",
	"internal error: missing GSS_C_NO_CREDENTIAL check",
	"cred_type is \"no-name\", but cred_name is set",
	"cred_type is \"no-name\", but cred_service is set",
	"cred_type is \"mechanism-specific\", but cred_name is not set",
	"cred_type is \"mechanism-specific\", but cred_service is set",
	"cred_type is \"user\", but cred_service is set",
	"cred_type is \"self\", but cred_name is set",
	"cred_type is \"self\", but cred_service is set",
	"cred_type is \"self\", but not initialized as an initiator",
	"internal error - invalid cred_type",
	"invalid credential configuration",

	/* refered only from gfarm/import_help.c */
	"hostname expected",
	"empty file",
};

/*
 * NOTE: The order of the following table is important.
 *
 * For cases that multiple GFARM_ERRs are mapped into same UNIX errno.
 * first map entry will be used to map UNIX errno to GFARM_ERR,
 * The implementation of gfarm_errno_to_error_initialize() and
 * gfarm_errno_to_error() ensures this.
 *
 * It is ok that different gfarm_error_t's are mapped into same errno
 * (because gfarm_error_t is more detailed than errno),
 * but it isn't ok that different errno's are mapped into same gfarm_error_t
 * (because that means errno is more detailed than gfarm_error_t).
 */

static struct gfarm_errno_error_map {
	int unix_errno;
	gfarm_error_t gfarm_error;
} gfarm_errno_error_map_table[] = {
	{ 0,		GFARM_ERR_NO_ERROR },

	/* classic errno (1..10) */
	{ EPERM,	GFARM_ERR_OPERATION_NOT_PERMITTED },
	{ ENOENT,	GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY },
	{ ESRCH,	GFARM_ERR_NO_SUCH_PROCESS },
	{ EINTR,	GFARM_ERR_INTERRUPTED_SYSTEM_CALL },
	{ EIO,		GFARM_ERR_INPUT_OUTPUT },
	{ ENXIO,	GFARM_ERR_DEVICE_NOT_CONFIGURED },
	{ E2BIG,	GFARM_ERR_ARGUMENT_LIST_TOO_LONG },
	{ ENOEXEC,	GFARM_ERR_EXEC_FORMAT },
	{ EBADF,	GFARM_ERR_BAD_FILE_DESCRIPTOR },
	{ ECHILD,	GFARM_ERR_NO_CHILD_PROCESS },

	/* non classic, POSIX (EAGAIN == EWOULDBLOCK) */
	{ EAGAIN,	GFARM_ERR_RESOURCE_TEMPORARILY_UNAVAILABLE },

	/* classic errno (12..34) */
	{ ENOMEM,	GFARM_ERR_NO_MEMORY },
	{ EACCES,	GFARM_ERR_PERMISSION_DENIED },
	{ EFAULT,	GFARM_ERR_BAD_ADDRESS },
	{ ENOTBLK,	GFARM_ERR_BLOCK_DEVICE_REQUIRED },
	{ EBUSY,	GFARM_ERR_DEVICE_BUSY },
	{ EEXIST,	GFARM_ERR_ALREADY_EXISTS },
	{ EXDEV,	GFARM_ERR_CROSS_DEVICE_LINK },
	{ ENODEV,	GFARM_ERR_OPERATION_NOT_SUPPORTED_BY_DEVICE },
	{ ENOTDIR,	GFARM_ERR_NOT_A_DIRECTORY },
	{ EISDIR,	GFARM_ERR_IS_A_DIRECTORY },
	{ EINVAL,	GFARM_ERR_INVALID_ARGUMENT },
	{ ENFILE,	GFARM_ERR_TOO_MANY_OPEN_FILES_IN_SYSTEM },
	{ EMFILE,	GFARM_ERR_TOO_MANY_OPEN_FILES },
	{ ENOTTY,	GFARM_ERR_INAPPROPRIATE_IOCTL_FOR_DEVICE },
	{ ETXTBSY,	GFARM_ERR_TEXT_FILE_BUSY },
	{ EFBIG,	GFARM_ERR_FILE_TOO_LARGE },
	{ ENOSPC,	GFARM_ERR_NO_SPACE },
	{ ESPIPE,	GFARM_ERR_ILLEGAL_SEEK },
	{ EROFS,	GFARM_ERR_READ_ONLY_FILE_SYSTEM },
	{ EMLINK,	GFARM_ERR_TOO_MANY_LINKS },
	{ EPIPE,	GFARM_ERR_BROKEN_PIPE },
	{ EDOM,		GFARM_ERR_NUMERICAL_ARGUMENT_OUT_OF_DOMAIN },
	{ ERANGE,	GFARM_ERR_RESULT_OUT_OF_RANGE },

	/* ISO/IEC 9899 amendment1:1995, wide/multibyte-character handling */
	{ EILSEQ,	GFARM_ERR_ILLEGAL_BYTE_SEQUENCE },

	/* POSIX */
	{ EDEADLK,	GFARM_ERR_RESOURCE_DEADLOCK_AVOIDED },
	{ ENAMETOOLONG,	GFARM_ERR_FILE_NAME_TOO_LONG },
	{ ENOTEMPTY,	GFARM_ERR_DIRECTORY_NOT_EMPTY },
	{ ENOLCK,	GFARM_ERR_NO_LOCKS_AVAILABLE },
	{ ENOSYS,	GFARM_ERR_FUNCTION_NOT_IMPLEMENTED },

	/* X/Open */
	{ EINPROGRESS,	GFARM_ERR_OPERATION_NOW_IN_PROGRESS },
	{ EALREADY,	GFARM_ERR_OPERATION_ALREADY_IN_PROGRESS },
	/* X/Open - ipc/network software -- argument errors */
	{ ENOTSOCK,	GFARM_ERR_SOCKET_OPERATION_ON_NON_SOCKET },
	{ EDESTADDRREQ,	GFARM_ERR_DESTINATION_ADDRESS_REQUIRED },
	{ EMSGSIZE,	GFARM_ERR_MESSAGE_TOO_LONG },
	{ EPROTOTYPE,	GFARM_ERR_PROTOCOL_WRONG_TYPE_FOR_SOCKET },
	{ ENOPROTOOPT,	GFARM_ERR_PROTOCOL_NOT_AVAILABLE },
	{ EPROTONOSUPPORT, GFARM_ERR_PROTOCOL_NOT_SUPPORTED },
	{ EOPNOTSUPP,	GFARM_ERR_OPERATION_NOT_SUPPORTED },
	{ EAFNOSUPPORT,	GFARM_ERR_ADDRESS_FAMILY_NOT_SUPPORTED_BY_PROTOCOL_FAMILY },
	{ EADDRINUSE,	GFARM_ERR_ADDRESS_ALREADY_IN_USE },
	{ EADDRNOTAVAIL,GFARM_ERR_CANNOT_ASSIGN_REQUESTED_ADDRESS },
	/* X/Open - ipc/network software -- operational errors */
	{ ENETDOWN,	GFARM_ERR_NETWORK_IS_DOWN },
	{ ENETUNREACH,	GFARM_ERR_NETWORK_IS_UNREACHABLE },
	{ ECONNABORTED,	GFARM_ERR_CONNECTION_ABORTED },
	{ ECONNRESET,	GFARM_ERR_CONNECTION_RESET_BY_PEER },
	{ ENOBUFS,	GFARM_ERR_NO_BUFFER_SPACE_AVAILABLE },
	{ EISCONN,	GFARM_ERR_SOCKET_IS_ALREADY_CONNECTED },
	{ ENOTCONN,	GFARM_ERR_SOCKET_IS_NOT_CONNECTED },
	{ ETIMEDOUT,	GFARM_ERR_OPERATION_TIMED_OUT },
	{ ECONNREFUSED,	GFARM_ERR_CONNECTION_REFUSED },
	{ EHOSTUNREACH,	GFARM_ERR_NO_ROUTE_TO_HOST },
	/* X/Open */
	{ ELOOP,	GFARM_ERR_TOO_MANY_LEVELS_OF_SYMBOLIC_LINK },
	{ EDQUOT,	GFARM_ERR_DISK_QUOTA_EXCEEDED },
	{ ESTALE,	GFARM_ERR_STALE_FILE_HANDLE },
	/* X/Open - System-V IPC */
	{ EIDRM,	GFARM_ERR_IDENTIFIER_REMOVED },
	{ ENOMSG,	GFARM_ERR_NO_MESSAGE_OF_DESIRED_TYPE },
	{ EOVERFLOW,	GFARM_ERR_VALUE_TOO_LARGE_TO_BE_STORED_IN_DATA_TYPE },

	/* ipc/network errors */
#ifdef EAUTH
	{ EAUTH,	GFARM_ERR_AUTHENTICATION }, /* BSD */
#else
	{ EPERM,	GFARM_ERR_AUTHENTICATION }, /* "<-" only */
#endif
#ifdef ETIME
	{ ETIME,	GFARM_ERR_EXPIRED }, /* Linux */
#endif
#ifdef EPROTO
	{ EPROTO,	GFARM_ERR_PROTOCOL },  /* SVR4, Linux */
#else
	{ EPROTONOSUPPORT, GFARM_ERR_PROTOCOL }, /* "<-" only */
#endif

	/* gfarm specific errors */
	/*		GFARM_ERR_UNKNOWN_HOST */
	/*	      GFARM_ERR_CANNOT_RESOLVE_AN_IP_ADDRESS_INTO_A_HOSTNAME */
	/*		GFARM_ERR_NO_SUCH_OBJECT */
	/*		GFARM_ERR_CANT_OPEN */
#ifdef EPROTO
        { EPROTO,	GFARM_ERR_UNEXPECTED_EOF },
#else
        { ECONNABORTED,	GFARM_ERR_UNEXPECTED_EOF },
#endif
	/*		GFARM_ERR_GFARM_URL_PREFIX_IS_MISSING */
	{ EAGAIN,	GFARM_ERR_TOO_MANY_JOBS },
	/*		GFARM_ERR_FILE_MIGRATED */
	{ EOPNOTSUPP,	GFARM_ERR_NOT_A_SYMBOLIC_LINK },
	{ EOPNOTSUPP,	GFARM_ERR_IS_A_SYMBOLIC_LINK },
	/*		GFARM_ERR_UNKNOWN */
	/*		GFARM_ERR_INVALID_FILE_REPLICA */
	/*		GFARM_ERR_NO_SUCH_USER */
	{ EPERM,	GFARM_ERR_CANNOT_REMOVE_LAST_REPLICA },
	/*		GFARM_ERR_NO_SUCH_GROUP */
	/*		GFARM_ERR_GFARM_URL_USER_IS_MISSING */
	/*		GFARM_ERR_GFARM_URL_HOST_IS_MISSING */
	/*		GFARM_ERR_GFARM_URL_PORT_IS_MISSING */
	/*		GFARM_ERR_GFARM_URL_PORT_IS_INVALID */
	{ EBUSY,	GFARM_ERR_FILE_BUSY },
};

struct gfarm_error_domain {
	gfarm_error_t offset;
	int domerror_min, domerror_number;
	gfarm_error_t *map_to_gfarm;
	const char *(*domerror_to_message)(void *, int);
	void *domerror_to_message_cookie;

	/* only used by gfarm_error_range_alloc() case */
	struct gfarm_error_domain *next;
};

#define MAX_ERROR_DOMAINS	10

static int gfarm_error_domain_number = 0;
static struct gfarm_error_domain gfarm_error_domains[MAX_ERROR_DOMAINS];
static struct gfarm_error_domain *gfarm_error_ranges = NULL;

/*
 * allocate an error_domain
 * for a domain-specific error range [domerror_min, domerror_max]
 *
 * the corresponding gfarm error range will be:
 *	[domain->offset, domain->offset + domain->domerror_number - 1]
 */
gfarm_error_t
gfarm_error_domain_alloc(int domerror_min, int domerror_max,
	const char *(*de_to_m)(void *, int), void *cookie,
	struct gfarm_error_domain **domainp)
{
	int next_error;
	struct gfarm_error_domain *last, *new;

	if (gfarm_error_domain_number >= MAX_ERROR_DOMAINS) {
		gflog_debug(GFARM_MSG_1000845,
		    "gfarm_error_domain_alloc: too many error domains");
		return (GFARM_ERRMSG_TOO_MANY_ERROR_DOMAIN);
	}
	if (gfarm_error_domain_number == 0) {
		next_error = GFARM_ERR_FOREIGN_BEGIN;
	} else {
		last = &gfarm_error_domains[gfarm_error_domain_number - 1];
		next_error = last->offset + last->domerror_number;
	}
	if (next_error + domerror_max - domerror_min
	    >= GFARM_ERR_PRIVATE_BEGIN) {
		gflog_debug(GFARM_MSG_1000846,
		    "gfarm_error_domain_alloc: too large domain [%d, %d]",
		    domerror_min, domerror_max);
		return (GFARM_ERR_NUMERICAL_ARGUMENT_OUT_OF_DOMAIN);
	}
	new = &gfarm_error_domains[gfarm_error_domain_number];
	new->offset = next_error;
	new->domerror_min = domerror_min;
	new->domerror_number = domerror_max - domerror_min + 1;
	new->map_to_gfarm = NULL; /* may be allocated on demand */
	new->domerror_to_message = de_to_m;
	new->domerror_to_message_cookie = cookie;
	new->next = NULL;
	*domainp = new;
	gfarm_error_domain_number++;
	return (GFARM_ERR_NO_ERROR);
}

/*
 * allocate an error range [domerror_min, domerror_max]
 * within the gfarm error number space
 *
 * the corresponding gfarm error range will be:
 *	[domain->offset, domain->offset + domain->domerror_number - 1]
 *		==
 *	[domerror_min, domerror_max]
 */
gfarm_error_t
gfarm_error_range_alloc(int domerror_min, int domerror_max,
	const char *(*de_to_m)(void *, int), void *cookie,
	struct gfarm_error_domain **domainp)
{
	struct gfarm_error_domain *dom, *new;

	/* sanity checks */
	if (domerror_min < GFARM_ERR_PRIVATE_BEGIN ||
	    domerror_max < domerror_min) {
		gflog_debug(GFARM_MSG_1000847,
		    "gfarm_error_range_alloc: invalid range [%d, %d]",
		    domerror_min, domerror_max);
		return (GFARM_ERR_NUMERICAL_ARGUMENT_OUT_OF_DOMAIN);
	}
	for (dom = gfarm_error_ranges; dom != NULL; dom = dom->next) {
		if (domerror_max >= dom->offset &&
		    domerror_min < dom->offset + dom->domerror_number - 1) {
			gflog_debug(GFARM_MSG_1000848,
			    "gfarm_error_range_alloc: [%d, %d] is "
			    "overlapping with an existing range [%d, %d]",
			    domerror_min, domerror_max, dom->offset,
			    dom->offset + dom->domerror_number - 1);
			return (GFARM_ERR_NUMERICAL_ARGUMENT_OUT_OF_DOMAIN);
		}
	}

	GFARM_MALLOC(new);
	if (new == NULL) {
		gflog_debug(GFARM_MSG_1000849,
		    "gfarm_error_range_alloc: no memory");
		return (GFARM_ERR_NO_MEMORY);
	}
	new->offset = domerror_min;
	new->domerror_min = domerror_min;
	new->domerror_number = domerror_max - domerror_min + 1;
	new->map_to_gfarm = NULL;
	new->domerror_to_message = de_to_m;
	new->domerror_to_message_cookie = cookie;
	new->next = gfarm_error_ranges;
	gfarm_error_ranges = new;
	*domainp = new;
	return (GFARM_ERR_NO_ERROR);
}

gfarm_error_t
gfarm_error_domain_add_map(struct gfarm_error_domain *domain,
	int domerror, gfarm_error_t error)
{
	int i;

	if (domerror < domain->domerror_min ||
	    domerror >= domain->domerror_min + domain->domerror_number) {
		gflog_debug(GFARM_MSG_1000850,
		    "gfarm_error_domain_add_map: "
		    "error code %d is out of [%d, %d]",
		    domerror, domain->domerror_min,
		    domain->domerror_min + domain->domerror_number - 1);
		return (GFARM_ERR_NUMERICAL_ARGUMENT_OUT_OF_DOMAIN);
	}
	if (domain->map_to_gfarm == NULL) {
		GFARM_MALLOC_ARRAY(domain->map_to_gfarm,
		    domain->domerror_number);
		if (domain->map_to_gfarm == NULL) {
			gflog_debug(GFARM_MSG_1000851,
			    "gfarm_error_domain_add_map: no memory for %d",
			    domain->domerror_number);
			return (GFARM_ERR_NO_MEMORY);
		}
		for (i = 0; i < domain->domerror_number; i++)
			domain->map_to_gfarm[i] = GFARM_ERR_UNKNOWN;
	}
	domain->map_to_gfarm[domerror - domain->domerror_min] = error;
	return (GFARM_ERR_NO_ERROR);
}

gfarm_error_t
gfarm_error_domain_map(struct gfarm_error_domain *domain, int domerror)
{
	if (domerror < domain->domerror_min ||
	    domerror >= domain->domerror_min + domain->domerror_number)
		return (GFARM_ERR_UNKNOWN);
	domerror -= domain->domerror_min;
	if (domain->map_to_gfarm != NULL &&
	    domain->map_to_gfarm[domerror] != GFARM_ERR_UNKNOWN)
		return (domain->map_to_gfarm[domerror]);
	return (domain->offset + domerror);
}

static struct gfarm_error_domain *
gfarm_error_domain_search(gfarm_error_t error)
{
	int i;
	struct gfarm_error_domain *domain;

	for (i = 0; i < gfarm_error_domain_number; i++) {
		domain = &gfarm_error_domains[i];
		if (error < domain->offset)
			return (NULL);
		if (error < domain->offset + domain->domerror_number)
			return (domain);
	}
	for (domain = gfarm_error_ranges; domain != NULL;
	     domain = domain->next) {
		if (domain->offset <= error &&
		    error < domain->offset + domain->domerror_number)
			return (domain);
	}
	return (NULL);
}

const char *
gfarm_error_string(gfarm_error_t error)
{
	struct gfarm_error_domain *domain;

	if (error < 0)
		return (errcode_string[GFARM_ERR_UNKNOWN]);
	if (error < GFARM_ERR_NUMBER) {
		if (error >= GFARM_ARRAY_LENGTH(errcode_string)) {
			gflog_warning(GFARM_MSG_1000852,
			    "gfarm_error_string: missing errcode_string: %d",
			    error);
			return (errcode_string[GFARM_ERR_UNKNOWN]);
		}
		return (errcode_string[error]);
	}

	if (error < GFARM_ERRMSG_BEGIN)
		return (errcode_string[GFARM_ERR_UNKNOWN]);
	if (error < GFARM_ERRMSG_END)
		return (errmsg_string[error - GFARM_ERRMSG_BEGIN]);

	if ((domain = gfarm_error_domain_search(error)) != NULL) {
		error -= domain->offset;
#if 0 /* to make this return original error message instead of mapped one */
		/* this shouldn't happen, usually */
		if (domain->map_to_gfarm != NULL &&
		    domain->map_to_gfarm[error] != GFARM_ERR_UNKNOWN)
			return (errcode_string[domain->map_to_gfarm[error]]);
#endif
		return ((*domain->domerror_to_message)(
		    domain->domerror_to_message_cookie,
		    domain->domerror_min + error));
	}
	return (errcode_string[GFARM_ERR_UNKNOWN]);
}

#if defined(HAVE_SYS_NERR)
# define ERRNO_NUMBER sys_nerr
#elif defined(ELAST)
# define ERRNO_NUMBER (ELAST + 1)
#else
# define ERRNO_NUMBER 256 /* XXX */
#endif

static struct gfarm_error_domain *gfarm_errno_domain = NULL;

const char *
gfarm_errno_to_string(void *cookie, int eno)
{
	return (strerror(eno));
}

static void
gfarm_errno_to_error_initialize(void)
{
	gfarm_error_t e;
	int i;
	struct gfarm_errno_error_map *map;

	/* Solaris calls this function more than once with non-pthread apps */
	if (gfarm_errno_domain != NULL)
		return;

	e = gfarm_error_domain_alloc(0, ERRNO_NUMBER,
	    gfarm_errno_to_string, NULL,
	    &gfarm_errno_domain);
	if (e != GFARM_ERR_NO_ERROR) /* really fatal problem */
		gflog_fatal(GFARM_MSG_1000007,
		    "libgfarm: cannot allocate error domain for errno");

	for (i = 0; i < GFARM_ARRAY_LENGTH(gfarm_errno_error_map_table); i++) {
		map = &gfarm_errno_error_map_table[i];
		if (gfarm_error_domain_map(gfarm_errno_domain, map->unix_errno)
		    < GFARM_ERR_NUMBER)
			continue; /* a mapping is already registered */
		if ((e = gfarm_error_domain_add_map(gfarm_errno_domain,
		    map->unix_errno, map->gfarm_error)) != GFARM_ERR_NO_ERROR){
			gflog_fatal(GFARM_MSG_1000853,
			    "libgfarm: initializing error map "
			    "(%d -> %d): %s (%d)",
			    map->unix_errno, map->gfarm_error,
			    e == GFARM_ERR_NO_MEMORY ?
			    "no memory" : "unexpected error", e);
		}
	}
}

gfarm_error_t
gfarm_errno_to_error(int eno)
{
	static pthread_once_t gfarm_errno_to_error_initialized =
	    PTHREAD_ONCE_INIT;

	pthread_once(&gfarm_errno_to_error_initialized,
	    gfarm_errno_to_error_initialize);

	return (gfarm_error_domain_map(gfarm_errno_domain, eno));
}

static int gfarm_error_to_errno_map[GFARM_ERR_NUMBER];

static void
gfarm_error_to_errno_initialize(void)
{
	int i;
	struct gfarm_errno_error_map *map;

	for (i = 0; i < GFARM_ARRAY_LENGTH(gfarm_errno_error_map_table); i++) {
		map = &gfarm_errno_error_map_table[i];
		gfarm_error_to_errno_map[map->gfarm_error] = map->unix_errno;
	}
	for (i = 1; i < GFARM_ARRAY_LENGTH(gfarm_error_to_errno_map); i++) {
		if (gfarm_error_to_errno_map[i] == 0)
			gfarm_error_to_errno_map[i] = EINVAL;
	}
}

int
gfarm_error_to_errno(gfarm_error_t error)
{
	static pthread_once_t gfarm_error_to_errno_initialized =
	    PTHREAD_ONCE_INIT;

	pthread_once(&gfarm_error_to_errno_initialized,
	    gfarm_error_to_errno_initialize);
	if (error < GFARM_ERR_NUMBER)
		return (gfarm_error_to_errno_map[error]);
	return (EINVAL);
}