Skip to content

Commit b64af78

Browse files
committed
Some aesthetic fixes in libdtm.
1 parent 3f13c27 commit b64af78

File tree

1 file changed

+74
-70
lines changed

1 file changed

+74
-70
lines changed

contrib/pg_dtm/libdtm.c

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ static bool dtm_read_snapshot(DTMConn dtm, Snapshot s)
126126
static bool dtm_read_status(DTMConn dtm, XidStatus *s)
127127
{
128128
char statuschar;
129-
if (!dtm_read_char(dtm, &statuschar)) {
129+
if (!dtm_read_char(dtm, &statuschar))
130+
{
130131
fprintf(stderr, "dtm_read_status: failed to get char\n");
131132
return false;
132133
}
@@ -155,73 +156,76 @@ static bool dtm_read_status(DTMConn dtm, XidStatus *s)
155156
// Connects to the specified DTM.
156157
static DTMConn DtmConnect(char *host, int port)
157158
{
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;
159+
DTMConn dtm;
160+
int sd;
161+
162+
if (strcmp(host, "localhost") == 0)
163+
{
164+
struct sockaddr sock;
165+
int len = offsetof(struct sockaddr, sa_data) + snprintf(sock.sa_data, sizeof(sock.sa_data), "%s/p%u", dtm_unix_sock_dir, port);
166+
sock.sa_family = AF_UNIX;
167+
168+
sd = socket(AF_UNIX, SOCK_STREAM, 0);
169+
if (sd == -1)
170+
{
171+
perror("failed to create a unix socket");
172+
}
173+
if (connect(sd, &sock, len) == -1)
174+
{
175+
perror("failed to connect to the address");
176+
close(sd);
177+
return NULL;
178+
}
179+
dtm = malloc(sizeof(DTMConnData));
180+
dtm->sock = sd;
181+
return dtm;
182+
}
183+
else
184+
{
185+
struct addrinfo *addrs = NULL;
186+
struct addrinfo hint;
187+
char portstr[6];
188+
struct addrinfo *a;
189+
190+
memset(&hint, 0, sizeof(hint));
191+
hint.ai_socktype = SOCK_STREAM;
192+
hint.ai_family = AF_INET;
193+
snprintf(portstr, 6, "%d", port);
194+
hint.ai_protocol = getprotobyname("tcp")->p_proto;
195+
if (getaddrinfo(host, portstr, &hint, &addrs))
196+
{
197+
perror("failed to resolve address");
198+
return NULL;
199+
}
200+
201+
for (a = addrs; a != NULL; a = a->ai_next)
202+
{
203+
int one = 1;
204+
sd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
205+
if (sd == -1)
206+
{
207+
perror("failed to create a socket");
208+
continue;
209+
}
210+
setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
211+
212+
if (connect(sd, a->ai_addr, a->ai_addrlen) == -1)
213+
{
214+
perror("failed to connect to an address");
215+
close(sd);
216+
continue;
217+
}
218+
219+
// success
220+
freeaddrinfo(addrs);
221+
dtm = malloc(sizeof(DTMConnData));
222+
dtm->sock = sd;
223+
return dtm;
224+
}
225+
freeaddrinfo(addrs);
226+
}
227+
fprintf(stderr, "could not connect\n");
228+
return NULL;
225229
}
226230

227231
/*
@@ -264,7 +268,7 @@ void DtmGlobalConfig(char *host, int port, char* sock_dir) {
264268
}
265269
dtmhost = strdup(host);
266270
dtmport = port;
267-
dtm_unix_sock_dir = sock_dir;
271+
dtm_unix_sock_dir = sock_dir;
268272
}
269273

270274
static DTMConn GetConnection()
@@ -409,7 +413,7 @@ XidStatus DtmGlobalSetTransStatus(TransactionId xid, XidStatus status, bool wait
409413
fprintf(
410414
stderr,
411415
"DtmGlobalSetTransStatus: failed to vote"
412-
" %s the transaction status for xid = %d\n",
416+
" %s the transaction xid = %d\n",
413417
(status == TRANSACTION_STATUS_COMMITTED) ? "for" : "against",
414418
xid
415419
);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy