rbb 99/10/16 07:07:01
Modified: src/lib/apr/file_io/unix dir.c
src/lib/apr/inc apr_macro.h
src/lib/apr/network_io/unix networkio.h sockets.c sockopt.c
Log:
Put mutex's around non-thread-safe functions in APR.
Revision Changes Path
1.9 +10 -6 apache-2.0/src/lib/apr/file_io/unix/dir.c
Index: dir.c
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/file_io/unix/dir.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- dir.c 1999/10/12 06:14:42 1.8
+++ dir.c 1999/10/16 14:06:58 1.9
@@ -58,11 +58,15 @@
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
+#include "apr_macro.h"
#include "fileio.h"
#include "apr_file_io.h"
#include "apr_lib.h"
+#include "apr_lock.h"
#include "apr_portable.h"
+static ap_lock_t *lock_readdir = NULL;
+
static ap_status_t dir_cleanup(void *thedir)
{
struct dir_t *dir = thedir;
@@ -88,7 +92,7 @@
(*new)->cntxt = cont;
(*new)->dirname = strdup(dirname);
(*new)->dirstruct = opendir(dirname);
- (*new)->entry = NULL;
+ (*new)->entry = ap_pcalloc(cont, sizeof(struct dirent));
if ((*new)->dirstruct == NULL) {
return errno;
@@ -124,11 +128,11 @@
*/
ap_status_t ap_readdir(struct dir_t *thedir)
{
- thedir->entry = readdir(thedir->dirstruct);
- if (thedir->entry == NULL) {
- return errno;
- }
- return APR_SUCCESS;
+ ap_status_t status;
+ SAFETY_LOCK(readdir, "readdir_file");
+ READDIR(thedir->dirstruct, thedir->entry, status);
+ SAFETY_UNLOCK(readdir);
+ return status;
}
/* ***APRDOC********************************************************
1.3 +55 -7 apache-2.0/src/lib/apr/inc/apr_macro.h
Index: apr_macro.h
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/inc/apr_macro.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- apr_macro.h 1999/10/16 12:47:59 1.2
+++ apr_macro.h 1999/10/16 14:06:59 1.3
@@ -62,18 +62,20 @@
extern "C" {
#endif
+#include "apr_config.h"
+
#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
-#define SAFETY_LOCK(func_name, name_str) \
+#define SAFETY_LOCK(funcname, namestr) \
{ \
if (lock_##func_name == NULL) \
if (ap_create_lock(&lock_##func_name, APR_MUTEX, APR_INTRAPROCESS, \
name_str, NULL) != APR_SUCCESS) \
return APR_NOTTHREADSAFE; \
- if (ap_lock(lock_##func_name) != APR_SUCCESS) \
+ if (ap_lock(lock_##funcname) != APR_SUCCESS) \
return APR_NOTTHREADSAFE; \
}
#else
-#define SAFETY_LOCK(func_name, name_str)
+#define SAFETY_LOCK(funcname, namestr)
#endif
#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
@@ -86,15 +88,61 @@
#endif
#ifdef HAVE_GMTIME_R
-#define GMTIME_R(x, y) gmtime_r(x, y)
+#define GMTIME_R(x, y) gmtime_r(x, y);
#else
-#define GMTIME_R(x, y) memcpy(y, gmtime(x), sizeof(y))
+#define GMTIME_R(x, y) memcpy(y, gmtime(x), sizeof(y));
#endif
#ifdef HAVE_LOCALTIME_R
-#define LOCALTIME_R(x, y) localtime_r(x, y)
+#define LOCALTIME_R(x, y) localtime_r(x, y);
+#else
+#define LOCALTIME_R(x, y) memcpy(y, localtime(x), sizeof(y));
+#endif
+
+#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
+#define GETHOSTBYNAME(x, y, z) z = gethostbyname(x);
+#else
+#define GETHOSTBYNAME(x, y, z) \
+ y = gethostbyname(x); \
+ z = ap_palloc(NULL, sizeof(struct hostent)); \
+ memcpy(z, y, sizeof(struct hostent));
+#endif
+
+#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
+#define GETHOSTBYADDR(x, y, z) z = gethostbyaddr(x, sizeof(struct in_addr),
AF_INET);
+#else
+#define GETHOSTBYADDR(x, y, z) \
+ y = gethostbyaddr(x, sizeof(struct in_addr), AF_INET); \
+ z = ap_palloc(NULL, sizeof(struct hostent)); \
+ memcpy(z, y, sizeof(struct hostent));
+#endif
+
+#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
+#define INET_NTOA(x, y, len) ap_cpystrn(y, inet_ntoa(x), len);
+#else
+#define INET_NTOA(x, y, len) ap_cpystrn(y, inet_ntoa(x), len);
+#endif
+
+#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
+#define READDIR(x, y, z) y = readdir(x); \
+ if (y == NULL) { \
+ z = errno; \
+ } \
+ else { \
+ z = APR_SUCCESS; \
+ }
#else
-#define LOCALTIME_R(x, y) memcpy(y, localtime(x), sizeof(y))
+#define READDIR(x, y, z) \
+ { \
+ struct dirent *temp = readdir(x); \
+ if (temp == NULL) { \
+ z = errno; \
+ } \
+ else { \
+ memcpy(y, temp, sizeof(struct dirent)); \
+ z = APR_SUCCESS; \
+ } \
+ }
#endif
#ifdef __cplusplus
1.7 +1 -0 apache-2.0/src/lib/apr/network_io/unix/networkio.h
Index: networkio.h
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/network_io/unix/networkio.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- networkio.h 1999/10/10 17:08:00 1.6
+++ networkio.h 1999/10/16 14:07:00 1.7
@@ -58,6 +58,7 @@
#include "apr_network_io.h"
#include "apr_general.h"
+#include "apr_lock.h"
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
1.17 +15 -5 apache-2.0/src/lib/apr/network_io/unix/sockets.c
Index: sockets.c
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/network_io/unix/sockets.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- sockets.c 1999/10/15 14:20:13 1.16
+++ sockets.c 1999/10/16 14:07:00 1.17
@@ -58,6 +58,7 @@
#include "apr_general.h"
#include "apr_portable.h"
#include "apr_lib.h"
+#include "apr_macro.h"
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
@@ -66,6 +67,11 @@
#include <arpa/inet.h>
#include <netdb.h>
+#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
+ap_lock_t *lock_network = NULL;
+ap_lock_t *lock_inet = NULL;
+#endif
+
static ap_status_t socket_cleanup(void *sock)
{
struct socket_t *thesocket = sock;
@@ -204,8 +210,9 @@
ap_status_t ap_getipaddr(char *addr, ap_ssize_t len,
const struct socket_t *sock)
{
- char *temp = inet_ntoa(sock->addr->sin_addr);
- ap_cpystrn(addr,temp,len-1);
+ SAFETY_LOCK(inet, "inetfile");
+ INET_NTOA(sock->addr->sin_addr, addr, len - 1);
+ SAFETY_UNLOCK(inet);
return APR_SUCCESS;
}
@@ -286,21 +293,24 @@
ap_status_t ap_connect(struct socket_t *sock, char *hostname)
{
struct hostent *hp;
+ struct hostent *hp_safe;
if (hostname != NULL) {
- hp = gethostbyname(hostname);
+ SAFETY_LOCK(network, "net_file");
+ GETHOSTBYNAME(hostname, hp, hp_safe);
+ SAFETY_UNLOCK(network);
if ((sock->socketdes < 0) || (!sock->addr)) {
return APR_ENOTSOCK;
}
- if (!hp) {
+ if (!hp_safe) {
if (h_errno == TRY_AGAIN) {
return EAGAIN;
}
return h_errno;
}
- memcpy((char *)&sock->addr->sin_addr, hp->h_addr_list[0],
hp->h_length);
+ memcpy((char *)&sock->addr->sin_addr, hp_safe->h_addr_list[0],
hp_safe->h_length);
sock->addr_len = sizeof(*sock->addr);
}
1.9 +9 -0 apache-2.0/src/lib/apr/network_io/unix/sockopt.c
Index: sockopt.c
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/network_io/unix/sockopt.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- sockopt.c 1999/10/10 20:34:57 1.8
+++ sockopt.c 1999/10/16 14:07:00 1.9
@@ -57,6 +57,7 @@
#include "apr_network_io.h"
#include "apr_general.h"
#include "apr_lib.h"
+#include "apr_macro.h"
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
@@ -66,6 +67,10 @@
#include <fcntl.h>
#include <netdb.h>
+#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
+extern ap_lock_t *lock_network;
+#endif
+
static ap_status_t soblock(int sd)
{
int fd_flags;
@@ -203,9 +208,13 @@
ap_status_t ap_get_remote_hostname(char **name, struct socket_t *sock)
{
struct hostent *hptr;
+ struct hostent *hp_safe;
+ SAFETY_LOCK(network, "net_file");
+ GETHOSTBYADDR((char *)&(sock->addr->sin_addr), hptr, hp_safe);
hptr = gethostbyaddr((char *)&(sock->addr->sin_addr),
sizeof(struct in_addr), AF_INET);
+ SAFETY_UNLOCK(network);
if (hptr != NULL) {
*name = ap_pstrdup(sock->cntxt, hptr->h_name);
if (*name) {