File 11_always_init_function_pointers.patch of Package tsocks

#! /bin/sh /usr/share/dpatch/dpatch-run
## 06_fallback.dpatch by Tamas SZERB <toma@rulez.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Be sure that the function pointers are correctly initialised
## DP: 
## DP: When a library is loaded before libtsocks, but wants to use the
## DP: functions wrapped by tsocks, the function pointers to these
## DP: wrapped functions will not yet be initialised (because libtsocks'
## DP: own _init() has not yet been called), thus causing segfaults.
## DP: 
## DP: Work around this by always calling the initialisation code from
## DP: all the wrapped functions.
## DP: 
## DP: Because _init() is special, move the initialisation code to
## DP: a new tsocks_init() function, called from _init() and all the
## DP: wrapped functions.
## DP: 
## DP: Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>

@DPATCH@
--- a/tsocks-orig.c	2016-05-02 21:07:58.804224930 +0200
+++ b/tsocks.c	2016-05-02 21:13:00.399657192 +0200
@@ -70,6 +70,7 @@
 
 /* Exported Function Prototypes */
 void _init(void);
+void tsocks_init(void);
 int connect(CONNECT_SIGNATURE);
 int select(SELECT_SIGNATURE);
 int poll(POLL_SIGNATURE);
@@ -103,10 +104,18 @@
 static int read_socksv5_auth(struct connreq *conn);
 
 void _init(void) {
+    tsocks_init();
+}
+void tsocks_init(void) {
+    static int init_done;
 #ifdef USE_OLD_DLSYM
 	void *lib;
 #endif
 
+    if (init_done)
+        return;
+    init_done = 1;
+
 	/* We could do all our initialization here, but to be honest */
 	/* most programs that are run won't use our services, so     */
 	/* we do our general initialization on first call            */
@@ -202,6 +211,8 @@
 	struct serverent *path;
    struct connreq *newconn;
 
+   tsocks_init();
+
    get_environment();
 
 	/* If the real connect doesn't exist, we're stuffed */
@@ -361,6 +372,8 @@
    struct connreq *conn, *nextconn;
    fd_set mywritefds, myreadfds, myexceptfds;
 
+   tsocks_init();
+
    /* If we're not currently managing any requests we can just 
     * leave here */
    if (!requests) {
@@ -545,6 +558,8 @@
    int monitoring = 0;
    struct connreq *conn, *nextconn;
 
+   tsocks_init();
+
    /* If we're not currently managing any requests we can just 
     * leave here */
    if (!requests)
@@ -699,6 +714,8 @@
    int rc;
    struct connreq *conn;
 
+   tsocks_init();
+
 	if (realclose == NULL) {
 		show_msg(MSGERR, "Unresolved symbol: close\n");
 		return(-1);
@@ -739,6 +756,8 @@
    struct connreq *conn;
    int rc;
 
+   tsocks_init();
+
     if (realgetpeername == NULL) {
         show_msg(MSGERR, "Unresolved symbol: getpeername\n");
         return(-1);
@@ -1227,6 +1246,8 @@
 int res_init(void) {
         int rc;
 
+   tsocks_init();
+
 	if (realresinit == NULL) {
 		show_msg(MSGERR, "Unresolved symbol: res_init\n");
 		return(-1);