Re: libct ct_connect thread_safe

[email protected] (Christos Zoulas)
Newsgroups gmane.comp.db.tds.freetds
Organization Astron Software
Message-ID <[email protected]>
On Dec 15,  8:13am, [email protected] (Mitch Freed) wrote:
-- Subject: Re: [freetds] libct ct_connect thread_safe

| I have 2 threads each with their *own* connection to different servers. 
| The problem is
| 
| static SSL_CTX *ssl_ctx
| 
| inside net.c and the way it is being initialized.

This is easily fixed as follows... I added some othe minor fixes as a christmas
bonus.

christos

Index: include/tds.h
===================================================================
RCS file: /src/twosigma/cvsroot/external/public/freetds/include/tds.h,v
retrieving revision 1.19
diff -u -u -r1.19 tds.h
--- include/tds.h	29 Sep 2011 20:13:05 -0000	1.19
+++ include/tds.h	15 Dec 2011 16:01:49 -0000
@@ -1009,6 +1009,7 @@
 	int internal_sp_called;
 
 	int option_value;
+	void *ssl_ctx;
 };
 
 #define tds_conn(tds) (&(tds)->conn)
@@ -1330,9 +1331,9 @@
 
 #define IS_TDSDEAD(x) (((x) == NULL) || TDS_IS_SOCKET_INVALID(tds_conn(x)->s))
 
-/** Check if product is Sybase (such as Adaptive Server Enterrprice). x should be a TDS_SOCKET*. */
+/** Check if product is Sybase (such as Adaptive Server Enterrprice). x should be a TDSSOCKET*. */
 #define TDS_IS_SYBASE(x) (!(tds_conn(x)->product_version & 0x80000000u))
-/** Check if product is Microsft SQL Server. x should be a TDS_SOCKET*. */
+/** Check if product is Microsft SQL Server. x should be a TDSSOCKET*. */
 #define TDS_IS_MSSQL(x) ((tds_conn(x)->product_version & 0x80000000u)!=0)
 
 /** Calc a version number for mssql. Use with TDS_MS_VER(7,0,842).
Index: src/apps/bsqldb.c
===================================================================
RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/bsqldb.c,v
retrieving revision 1.5
diff -u -u -r1.5 bsqldb.c
--- src/apps/bsqldb.c	29 Sep 2011 16:37:18 -0000	1.5
+++ src/apps/bsqldb.c	15 Dec 2011 16:01:49 -0000
@@ -543,7 +543,7 @@
 							perror("could not write to output file");
 							exit(EXIT_FAILURE);
 						}
-						fprintf(stdout, metadata[c].format_string); /* col/row separator */
+						fprintf(stdout, "%s", metadata[c].format_string); /* col/row separator */
 						continue;
 					}
 					switch (data[c].status) { /* handle nulls */
Index: src/apps/fisql/fisql.c
===================================================================
RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/fisql/fisql.c,v
retrieving revision 1.3
diff -u -u -r1.3 fisql.c
--- src/apps/fisql/fisql.c	23 May 2011 18:29:08 -0000	1.3
+++ src/apps/fisql/fisql.c	15 Dec 2011 16:01:49 -0000
@@ -542,9 +542,20 @@
 				add_history(line);
 			}
 			if (!(strncasecmp(line, "!!", 2))) {
+				int rv;
 				cp = line + 2;
-				system(cp);
-				continue;
+				switch (rv = system(cp)) {
+				case 0:
+					continue;
+				case -1:
+					fprintf(stderr,
+					    "Failed to execute `%s'\n", cp);
+					continue;
+				default:
+					fprintf(stderr, "Command `%s' exited "
+					    "with code %d\n", cp, rv);
+					continue;
+				}
 			}
 			/* XXX: isql off-by-one line count error for :r not duplicated */
 			if (!(strncasecmp(line, ":r", 2))) {
@@ -553,7 +564,7 @@
 				for (; *cp && !(isspace((unsigned char) *cp)); cp++);
 				*cp = '\0';
 				if ((fp = fopen(tfn, "r")) == NULL) {
-					printf("Operating system error: Failed to open %s.\n", tfn);
+					fprintf(stderr, "Operating system error: Failed to open %s.\n", tfn);
 					continue;
 				}
 				tmpfp = rl_instream;
Index: src/ctlib/ct.c
===================================================================
RCS file: /src/twosigma/cvsroot/external/public/freetds/src/ctlib/ct.c,v
retrieving revision 1.18
diff -u -u -r1.18 ct.c
--- src/ctlib/ct.c	29 Sep 2011 16:37:19 -0000	1.18
+++ src/ctlib/ct.c	15 Dec 2011 16:01:49 -0000
@@ -396,7 +396,7 @@
 			break;
 		case CS_SERVERADDR: {
 			/* Format of this property: "[hostname] [port]" */
-			char *host, *port, *lasts;
+			char *host, *port, *lasts = NULL;
 			int portno;
 			host= strtok_r(set_buffer, " ", &lasts);
 			port= strtok_r(NULL, " ", &lasts);
Index: src/tds/net.c
===================================================================
RCS file: /src/twosigma/cvsroot/external/public/freetds/src/tds/net.c,v
retrieving revision 1.7
diff -u -u -r1.7 net.c
--- src/tds/net.c	29 Sep 2011 16:37:20 -0000	1.7
+++ src/tds/net.c	15 Dec 2011 16:01:49 -0000
@@ -1237,14 +1237,14 @@
 	tdsdump_log(TDS_DBG_INFO1, "GNUTLS: level %d:\n  %s", level, s);
 }
 
-static int tls_initialized = 0;
-
 #ifdef TDS_ATTRIBUTE_DESTRUCTOR
 static void __attribute__((destructor))
 tds_tls_deinit(void)
 {
+#ifdef notyet
 	if (tls_initialized)
 		gnutls_global_deinit();
+#endif
 }
 #endif
 
@@ -1288,12 +1288,12 @@
 
 	/* FIXME place somewhere else, deinit at end */
 	ret = 0;
-	if (!tls_initialized) {
+	if (tds->ssl_ctx == NULL) {
 		tds_gcry_init();
 		ret = gnutls_global_init();
 	}
 	if (ret == 0) {
-		tls_initialized = 1;
+		tds->ssl_ctx = (void *)1;
 
 		gnutls_global_set_log_level(11);
 		gnutls_global_set_log_function(tds_tls_log);
@@ -1403,29 +1403,31 @@
 	NULL,
 };
 
-static SSL_CTX *ssl_ctx;
 
-static int
+static SSL_CTX *
 tds_init_openssl(void)
 {
+	SSL_CTX *ssl_ctx;
 	SSL_METHOD *meth;
 
 	SSL_library_init ();
 	meth = TLSv1_client_method ();
 	if (meth == NULL)
-		return 1;
+		return NULL;
 	ssl_ctx = SSL_CTX_new (meth);
 	if (ssl_ctx == NULL)
-		return 1;
-	return 0;
+		return NULL;
+	return ssl_ctx;
 }
 
 #ifdef TDS_ATTRIBUTE_DESTRUCTOR
 static void __attribute__((destructor))
 tds_tls_deinit(void)
 {
+#ifdef notyet
 	if (ssl_ctx)
 		SSL_CTX_free (ssl_ctx);
+#endif
 }
 #endif
 
@@ -1451,13 +1453,13 @@
 
 	/* FIXME place somewhere else, deinit at end */
 	ret = 0;
-	if (!ssl_ctx)
-		ret = tds_init_openssl();
+	if (tds->ssl_ctx == NULL)
+		tds->ssl_ctx = tds_init_openssl();
 
-	if (ret == 0) {
+	if (tds->ssl_ctx) {
 		/* Initialize TLS session */
 		tls_msg = "initializing session";
-		con = SSL_new(ssl_ctx);
+		con = SSL_new(tds->ssl_ctx);
 	}
 	
 	if (con) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.