@@ -22,6 +22,10 @@ typedef struct DTMConnData
22
22
int sock ;
23
23
}DTMConnData ;
24
24
25
+ static char * dtmhost = NULL ;
26
+ static int dtmport = 0 ;
27
+ static char * dtm_unix_sock_dir ;
28
+
25
29
typedef unsigned long long xid_t ;
26
30
27
31
// Returns true if the write was successful.
@@ -151,51 +155,73 @@ static bool dtm_read_status(DTMConn dtm, XidStatus *s)
151
155
// Connects to the specified DTM.
152
156
static DTMConn DtmConnect (char * host ,int port )
153
157
{
154
- struct addrinfo * addrs = NULL ;
155
- struct addrinfo hint ;
156
- char portstr [6 ];
157
- struct addrinfo * a ;
158
-
159
- memset (& hint ,0 ,sizeof (hint ));
160
- hint .ai_socktype = SOCK_STREAM ;
161
- hint .ai_family = AF_INET ;
162
- snprintf (portstr ,6 ,"%d" ,port );
163
- hint .ai_protocol = getprotobyname ("tcp" )-> p_proto ;
164
- if (getaddrinfo (host ,portstr ,& hint ,& addrs ))
165
- {
166
- perror ("resolve address" );
167
- return NULL ;
168
- }
169
-
170
- for (a = addrs ;a != NULL ;a = a -> ai_next )
171
- {
172
- DTMConn dtm ;
173
- int one = 1 ;
174
- int sock = socket (a -> ai_family ,a -> ai_socktype ,a -> ai_protocol );
175
- if (sock == -1 )
176
- {
177
- perror ("failed to create a socket" );
178
- continue ;
179
- }
180
- setsockopt (sock ,IPPROTO_TCP ,TCP_NODELAY ,& one ,sizeof (one ));
181
-
182
- if (connect (sock ,a -> ai_addr ,a -> ai_addrlen )== -1 )
183
- {
184
- perror ("failed to connect to an address" );
185
- close (sock );
186
- continue ;
187
- }
188
-
189
- // success
190
- freeaddrinfo (addrs );
191
- dtm = malloc (sizeof (DTMConnData ));
192
- dtm -> sock = sock ;
193
- return dtm ;
194
- }
195
-
196
- freeaddrinfo (addrs );
197
- fprintf (stderr ,"could not connect\n" );
198
- return NULL ;
158
+ DTMConn dtm ;
159
+ int sd ;
160
+
161
+ if (strcmp (host ,"localhost" )== 0 ) {
162
+ struct sockaddr sock ;
163
+ int len = offsetof(struct sockaddr ,sa_data )+ snprintf (sock .sa_data ,sizeof (sock .sa_data ),"%s/p%u" ,dtm_unix_sock_dir ,port );
164
+ sock .sa_family = AF_UNIX ;
165
+
166
+ sd = socket (AF_UNIX ,SOCK_STREAM ,0 );
167
+ if (sd == -1 )
168
+ {
169
+ perror ("failed to create a unix socket" );
170
+ }
171
+ if (connect (sd ,& sock ,len )== -1 )
172
+ {
173
+ perror ("failed to connect to an address" );
174
+ close (sd );
175
+ return NULL ;
176
+ }
177
+ dtm = malloc (sizeof (DTMConnData ));
178
+ dtm -> sock = sd ;
179
+ return dtm ;
180
+ }else {
181
+ struct addrinfo * addrs = NULL ;
182
+ struct addrinfo hint ;
183
+ char portstr [6 ];
184
+ struct addrinfo * a ;
185
+
186
+ memset (& hint ,0 ,sizeof (hint ));
187
+ hint .ai_socktype = SOCK_STREAM ;
188
+ hint .ai_family = AF_INET ;
189
+ snprintf (portstr ,6 ,"%d" ,port );
190
+ hint .ai_protocol = getprotobyname ("tcp" )-> p_proto ;
191
+ if (getaddrinfo (host ,portstr ,& hint ,& addrs ))
192
+ {
193
+ perror ("resolve address" );
194
+ return NULL ;
195
+ }
196
+
197
+ for (a = addrs ;a != NULL ;a = a -> ai_next )
198
+ {
199
+ int one = 1 ;
200
+ sd = socket (a -> ai_family ,a -> ai_socktype ,a -> ai_protocol );
201
+ if (sd == -1 )
202
+ {
203
+ perror ("failed to create a socket" );
204
+ continue ;
205
+ }
206
+ setsockopt (sd ,IPPROTO_TCP ,TCP_NODELAY ,& one ,sizeof (one ));
207
+
208
+ if (connect (sd ,a -> ai_addr ,a -> ai_addrlen )== -1 )
209
+ {
210
+ perror ("failed to connect to an address" );
211
+ close (sd );
212
+ continue ;
213
+ }
214
+
215
+ // success
216
+ freeaddrinfo (addrs );
217
+ dtm = malloc (sizeof (DTMConnData ));
218
+ dtm -> sock = sd ;
219
+ return dtm ;
220
+ }
221
+ freeaddrinfo (addrs );
222
+ }
223
+ fprintf (stderr ,"could not connect\n" );
224
+ return NULL ;
199
225
}
200
226
201
227
/*
@@ -231,16 +257,14 @@ static bool dtm_query(DTMConn dtm, char cmd, int argc, ...)
231
257
return true;
232
258
}
233
259
234
- static char * dtmhost = NULL ;
235
- static int dtmport = 0 ;
236
-
237
- void TuneToDtm (char * host ,int port ) {
260
+ void DtmGlobalConfig (char * host ,int port ,char * sock_dir ) {
238
261
if (dtmhost ) {
239
262
free (dtmhost );
240
263
dtmhost = NULL ;
241
264
}
242
265
dtmhost = strdup (host );
243
266
dtmport = port ;
267
+ dtm_unix_sock_dir = sock_dir ;
244
268
}
245
269
246
270
static DTMConn GetConnection ()
@@ -255,7 +279,7 @@ static DTMConn GetConnection()
255
279
elog (ERROR ,"Failed to connect to DTMD %s:%d" ,dtmhost ,dtmport );
256
280
}
257
281
}else {
258
- elog (ERROR ,"DTMD address not specified" );
282
+ /* elog(ERROR, "DTMD address not specified"); */
259
283
}
260
284
}
261
285
return dtm ;