aboutsummaryrefslogtreecommitdiff
path: root/src/port/threads.c
blob: 659f5cd100cff2978561c38854e302c20fc294b7 (plain)
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
/*-------------------------------------------------------------------------
 *
 * threads.c
 *
 *		  Prototypes and macros around system calls, used to help make
 *		  threaded libraries reentrant and safe to use from threaded applications.
 *
 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
 *
 * $Id: threads.c,v 1.2 2003/08/04 00:43:33 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

/*
 * Wrapper around strerror and strerror_r to use the former if it is
 * available and also return a more useful value (the error string).
 */
char *
pqStrerror(int errnum, char *strerrbuf, size_t buflen)
{
#if defined(USE_THREADS) && defined(HAVE_STRERROR_R)
	/* reentrant strerror_r is available */
	/* some early standards had strerror_r returning char * */
	strerror_r(errnum, strerrbuf, buflen);
	return (strerrbuf);
#else
	/* no strerror_r() available, just use strerror */
	return strerror(errnum);
#endif
}

/*
 * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
 * behaviour, if it is not available.
 */
int
pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
		   size_t buflen, struct passwd ** result)
{
#if defined(USE_THREADS) && defined(HAVE_GETPWUID_R)

	/*
	 * broken (well early POSIX draft) getpwuid_r() which returns 'struct
	 * passwd *'
	 */
	*result = getpwuid_r(uid, resultbuf, buffer, buflen);
#else
	/* no getpwuid_r() available, just use getpwuid() */
	*result = getpwuid(uid);
#endif
	return (*result == NULL) ? -1 : 0;
}

/*
 * Wrapper around gethostbyname() or gethostbyname_r() to mimic
 * POSIX gethostbyname_r() behaviour, if it is not available.
 */
int
pqGethostbyname(const char *name,
				struct hostent * resbuf,
				char *buf, size_t buflen,
				struct hostent ** result,
				int *herrno)
{
#if defined(USE_THREADS) && defined(HAVE_GETHOSTBYNAME_R)

	/*
	 * broken (well early POSIX draft) gethostbyname_r() which returns
	 * 'struct hostent *'
	 */
	*result = gethostbyname_r(name, resbuf, buf, buflen, herrno);
	return (*result == NULL) ? -1 : 0;
#else
	/* no gethostbyname_r(), just use gethostbyname() */
	*result = gethostbyname(name);
	if (*result != NULL)
		return 0;
	else
	{
		*herrno = h_errno;
		return -1;
	}
#endif
}