[tor-commits] [torsocks/master] Try both symbol names before showing library error
ioerror at torproject.org
ioerror at torproject.org
Sun Jan 27 21:37:48 UTC 2013
commit 3f575e848eb77860215ba7ff39a6f629acfefc11
Author: Matthew Finkel <matthew.finkel at gmail.com>
Date: Sat Nov 10 23:24:19 2012 -0500
Try both symbol names before showing library error
The correct library we need to find may be the symbol sym or __sym or
something else entirely that we can't handle yet. We want to try to find
the library using both naming conventions, because they are the most
common, before we display a (very repetitive) warning.
Adjust macro name, check if dlerror returns null. strdup dlerror after sym
not found.
Give the macro a more descriptive name and add it to torsocks namespace.
Also use a default "Not Found" reason if both dysym and dlerror return
null.
Factor out common macro
---
src/common.h | 28 ++++++++++++++++++++++++++++
src/darwin_warts.c | 16 ++++------------
src/torsocks.c | 51 ++++++++++++++++++---------------------------------
3 files changed, 50 insertions(+), 45 deletions(-)
diff --git a/src/common.h b/src/common.h
index 1fc4589..f84a2f7 100644
--- a/src/common.h
+++ b/src/common.h
@@ -47,6 +47,34 @@
#define PREDICT_UNLIKELY(exp) (exp)
#endif
+/** Try to find the symbol that is either m or __m.
+ * If one of them exists, in that order, then save its address in r,
+ * otherwise we want to print a message at log level l stating that
+ * we could not find it.
+ */
+#define torsocks_find_library(m,l,r) \
+ do { \
+ char * dl_error_msg = ""; \
+ char * dl_error_msg2 = ""; \
+ dlerror(); \
+ if ((r = dlsym(RTLD_NEXT, m)) == NULL) { \
+ dl_error_msg = dlerror(); \
+ if (dl_error_msg != NULL) { \
+ dl_error_msg = strdup(dl_error_msg); \
+ } \
+ if ((r = dlsym(RTLD_NEXT, "__" m)) == NULL) { \
+ dl_error_msg2 = dlerror(); \
+ show_msg(l, "WARNING: The symbol %s() was not found in any shared " \
+ "library with the reported error: %s!\n" \
+ " Also, we failed to find the symbol %s() with the reported error:" \
+ " %s\n", m, (dl_error_msg ? dl_error_msg : "Not Found"), \
+ "__"m, (dl_error_msg2 ? dl_error_msg2 : "Not Found")); \
+ } \
+ if (dl_error_msg) \
+ free(dl_error_msg); \
+ } \
+ } while (0)
+
uint16_t get_uint16(const char *cp) ATTR_PURE ATTR_NONNULL((1));
uint32_t get_uint32(const char *cp) ATTR_PURE ATTR_NONNULL((1));
void set_uint16(char *cp, uint16_t v) ATTR_NONNULL((1));
diff --git a/src/darwin_warts.c b/src/darwin_warts.c
index 4cb86f0..65bdd04 100644
--- a/src/darwin_warts.c
+++ b/src/darwin_warts.c
@@ -32,18 +32,12 @@
#include <stddef.h>
#include <stdint.h>
+#include <stdlib.h>
#include <dlfcn.h>
+#include <string.h>
+#include <errno.h>
#include "common.h"
-#define LOAD_ERROR(s,l) { \
- char *error; \
- error = dlerror(); \
- show_msg(l, "The symbol %s() was not found in any shared " \
- "library. The error reported was: %s!\n", s, \
- (error)?error:"not found"); \
- dlerror(); \
- }
-
#define SELECT_SIGNATURE int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout
#define SELECT_ARGNAMES n, readfds, writefds, exceptfds, timeout
@@ -56,9 +50,7 @@ int torsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATU
int select(SELECT_SIGNATURE) {
if (!realselect) {
- dlerror();
- if ((realselect = dlsym(RTLD_NEXT, "select")) == NULL)
- LOAD_ERROR("select", MSGERR);
+ torsocks_find_library("select", MSGERR, realselect);
}
return torsocks_select_guts(SELECT_ARGNAMES, realselect);
}
diff --git a/src/torsocks.c b/src/torsocks.c
index 294e6dc..09d7bea 100644
--- a/src/torsocks.c
+++ b/src/torsocks.c
@@ -112,7 +112,6 @@ int res_init(void);
#include "expansion_table.h"
#undef PATCH_TABLE_EXPANSION
-
static int get_config();
static int get_environment();
static int deadpool_init(void);
@@ -121,14 +120,6 @@ static pthread_mutex_t torsocks_init_mutex = PTHREAD_MUTEX_INITIALIZER;
void torsocks_init(void)
{
-#define LOAD_ERROR(s,l) { \
- const char *error; \
- error = dlerror(); \
- if (error) \
- show_msg(l, "The symbol %s() was not found in any shared " \
- "library. The error reported was: %s!\n", s, error); \
- dlerror(); \
- }
pthread_mutex_lock(&torsocks_init_mutex);
show_msg(MSGDEBUG, "In torsocks_init \n");
@@ -150,11 +141,9 @@ void torsocks_init(void)
dlerror();
#ifndef USE_OLD_DLSYM
#ifdef SUPPORT_RES_API
- if (((realres_init = dlsym(RTLD_NEXT, "res_init")) == NULL) &&
- ((realres_init = dlsym(RTLD_NEXT, "__res_init")) == NULL))
- LOAD_ERROR("res_init", MSGERR);
+ torsocks_find_library("res_init", MSGERR, realres_init);
#endif
- #define PATCH_TABLE_EXPANSION(e,r,s,n,b,m) if (((real##n = dlsym(RTLD_NEXT, m)) == NULL) && ((real##n = dlsym(RTLD_NEXT, "__" m)) == NULL)) LOAD_ERROR(m, MSG##e);
+ #define PATCH_TABLE_EXPANSION(e,r,s,n,b,m) torsocks_find_library(m, MSG##e, real##n);
#include "expansion_table.h"
#undef PATCH_TABLE_EXPANSION
#else
@@ -244,9 +233,7 @@ static int get_config ()
#define PATCH_TABLE_EXPANSION(e,r,s,n,b,m) \
r n(s##SIGNATURE) { \
if (!real##n) { \
- dlerror(); \
- if ((real##n = dlsym(RTLD_NEXT, m)) == NULL) \
- LOAD_ERROR(m, MSG##e); \
+ torsocks_find_library(m, MSG##e, real##n);\
} \
return torsocks_##b##_guts(s##ARGNAMES, real##n); \
}
@@ -857,9 +844,9 @@ int res_init(void)
{
int rc;
- if (!realres_init && ((realres_init = dlsym(RTLD_NEXT, "res_init")) == NULL) &&
- ((realres_init = dlsym(RTLD_NEXT, "__res_init")) == NULL))
- LOAD_ERROR("res_init", MSGERR);
+ if (!realres_init) {
+ torsocks_find_library("res_init", MSGERR, realres_init);
+ }
show_msg(MSGTEST, "Got res_init request\n");
@@ -879,9 +866,9 @@ int EXPAND_GUTS_NAME(res_query)(RES_QUERY_SIGNATURE, int (*original_res_query)(R
{
int rc;
- if (!original_res_query && ((original_res_query = dlsym(RTLD_NEXT, "res_query")) == NULL) &&
- ((original_res_query = dlsym(RTLD_NEXT, "__res_query")) == NULL))
- LOAD_ERROR("res_query", MSGERR);
+ if (!original_res_query) {
+ torsocks_find_library("res_query", MSGERR, original_res_query);
+ }
show_msg(MSGTEST, "Got res_query request\n");
@@ -905,10 +892,9 @@ int EXPAND_GUTS_NAME(res_querydomain)(RES_QUERYDOMAIN_SIGNATURE, int (*original_
{
int rc;
- if (!original_res_querydomain &&
- ((original_res_querydomain = dlsym(RTLD_NEXT, "res_querydomain")) == NULL) &&
- ((original_res_querydomain = dlsym(RTLD_NEXT, "__res_querydomain")) == NULL))
- LOAD_ERROR("res_querydoimain", MSGERR);
+ if (!original_res_querydomain) {
+ torsocks_find_library("res_querydomain", MSGERR, original_res_querydomain);
+ }
show_msg(MSGDEBUG, "Got res_querydomain request\n");
@@ -932,10 +918,9 @@ int EXPAND_GUTS_NAME(res_search)(RES_SEARCH_SIGNATURE, int (*original_res_search
{
int rc;
- if (!original_res_search &&
- ((original_res_search = dlsym(RTLD_NEXT, "res_search")) == NULL) &&
- ((original_res_search = dlsym(RTLD_NEXT, "__res_search")) == NULL))
- LOAD_ERROR("res_search", MSGERR);
+ if (!original_res_search) {
+ torsocks_find_library("res_search", MSGERR, original_res_search);
+ }
show_msg(MSGTEST, "Got res_search request\n");
@@ -959,9 +944,9 @@ int EXPAND_GUTS_NAME(res_send)(RES_SEND_SIGNATURE, int (*original_res_send)(RES_
{
int rc;
- if (!original_res_send && ((original_res_send = dlsym(RTLD_NEXT, "res_send")) == NULL)
- && ((original_res_send = dlsym(RTLD_NEXT, "__res_send")) == NULL))
- LOAD_ERROR("res_send", MSGERR);
+ if (!original_res_send) {
+ torsocks_find_library("res_send", MSGERR, original_res_send);
+ }
show_msg(MSGTEST, "Got res_send request\n");
More information about the tor-commits
mailing list