Skip to content

Commit 693b043

Browse files
edgarpettijohnomar-polo
authored andcommitted
Lazy connect for socketmap.
1 parent 5a8aca5 commit 693b043

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

table_socketmap.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
#include "log.h"
3535
#include "table_stdio.h"
3636

37+
#define REPLYBUFFERSIZE 100000
38+
3739
static char *config;
3840
static int sock = -1;
41+
static int connected = 0;
3942
static FILE *sockstream;
40-
#define REPLYBUFFERSIZE 100000
4143
static char repbuffer[REPLYBUFFERSIZE+1];
4244

4345
enum socketmap_reply{
@@ -76,6 +78,8 @@ table_socketmap_connect(const char *s)
7678
goto err;
7779
}
7880

81+
connected = 1;
82+
7983
return 1;
8084

8185
err:
@@ -94,6 +98,13 @@ table_socketmap_query(const char *name, const char *key)
9498
ssize_t len;
9599
int ret = SM_PERM;
96100

101+
if (!connected) {
102+
if (!table_socketmap_connect(config)) {
103+
log_warnx("error connecting to %s", config);
104+
return ret;
105+
}
106+
}
107+
97108
memset(repbuffer, 0, sizeof repbuffer);
98109
fprintf(sockstream, "%s %s\n", name, key);
99110
fflush(sockstream);
@@ -224,9 +235,6 @@ main(int argc, char **argv)
224235

225236
config = argv[0];
226237

227-
if (table_socketmap_connect(config) == 0)
228-
fatalx("error connecting to %s", config);
229-
230238
table_api_on_update(table_socketmap_update);
231239
table_api_on_check(table_socketmap_check);
232240
table_api_on_lookup(table_socketmap_lookup);

0 commit comments

Comments
 (0)