-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpglater.c
332 lines (297 loc) · 8.51 KB
/
pglater.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>
#include <libpq-fe.h>
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#define EXIT_BADCONN 2
#define EXIT_USER 3
// Yay, C++ finger memory
#ifndef bool
typedef char bool;
#endif
#ifndef true
#define true ((bool) 1)
#endif
#ifndef false
#define false ((bool) 0)
#endif
#define PING_INTERVAL 300
// i18n
#define _(X) X
struct opts {
char *dbname;
char *host;
char *port;
char *username;
bool no_password;
bool verbose;
char *listen;
};
const char *progname = "pglater";
static void parse_options(int argc, char *argv[], struct opts *options);
static void usage();
static void showVersion();
int main(int argc, char *argv[])
{
struct opts options;
char *password = NULL;
char *password_prompt = NULL;
PGconn *db;
bool new_pass;
PGresult *res;
PGnotify *notify;
parse_options(argc, argv, &options);
if (options.username == NULL) {
password_prompt = strdup("Password: ");
} else {
char *prompt_template = "Password for user %s: ";
password_prompt = malloc(strlen(prompt_template) - 2
+ strlen(options.username) - 1);
sprintf(password_prompt, prompt_template, options.username);
}
do {
#define PARAMS_ARRAY_SIZE 7
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
keywords[0] = "host";
values[0] = options.host;
keywords[1] = "port";
values[1] = options.port;
keywords[2] = "user";
values[2] = options.username;
keywords[3] = "password";
values[3] = password;
keywords[4] = "dbname";
values[4] = options.dbname;
keywords[5] = "fallback_application_name";
values[5] = progname;
keywords[6] = NULL;
values[6] = NULL;
new_pass = false;
db = PQconnectdbParams(keywords, values, 1);
free(keywords);
free(values);
if (PQstatus(db) == CONNECTION_BAD &&
PQconnectionNeedsPassword(db) &&
!options.no_password) {
PQfinish(db);
if (password != NULL) {
free(password);
}
password = getpass(password_prompt);
}
} while(new_pass);
free(password);
free(password_prompt);
if (PQstatus(db) == CONNECTION_BAD) {
fprintf(stderr, "%s: %s", progname, PQerrorMessage(db));
PQfinish(db);
exit(EXIT_BADCONN);
}
if (options.listen == NULL) {
options.listen = "pglater";
}
if (options.verbose) {
fprintf(stderr, "Listening on channel %s\n", options.listen);
}
char *id = PQescapeIdentifier(db, options.listen, strlen(options.listen));
if (id == NULL) {
fprintf(stderr, "Failed to escape '%s': %s\n",
options.listen, PQerrorMessage(db));
PQfinish(db);
exit(EXIT_FAILURE);
}
char *listen = malloc(strlen(id) + 7);
sprintf(listen, "listen %s", id);
PQfreemem(id);
res = PQexec(db, listen);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Listen command failed: %s\n", PQerrorMessage(db));
PQclear(res);
PQfinish(db);
exit(EXIT_FAILURE);
}
PQclear(res);
time_t callback_time = 0;
char *callback_command = 0;
for (;;) {
int sock;
fd_set input_mask;
struct timeval timeout;
sock = PQsocket(db);
if (sock < 0) {
fprintf(stderr, "Failed to retrieve socket\n");
PQfinish(db);
exit(EXIT_FAILURE);
}
FD_ZERO(&input_mask);
FD_SET(sock, &input_mask);
time_t now = time(0);
if (callback_time == 0 || callback_time > now + PING_INTERVAL) {
timeout.tv_sec = PING_INTERVAL;
} else if(callback_time <= now) {
timeout.tv_sec = 0;
} else {
timeout.tv_sec = callback_time - now;
}
timeout.tv_usec = 0;
if (options.verbose) {
fprintf(stderr, "Waiting for up to %ld seconds\n", timeout.tv_sec);
}
switch (select(sock + 1, &input_mask, NULL, NULL, &timeout)) {
case -1:
/* error */
fprintf(stderr, "select() failed: %s\n", strerror(errno));
PQfinish(db);
exit(EXIT_FAILURE);
break;
case 0:
/* timeout */
now = time(0);
if (callback_time && callback_time <= now) {
if (options.verbose) {
fprintf(stderr, "Calling %s\n", callback_command);
}
res = PQexec(db, callback_command);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Callback command failed: %s\nCommand: %s\n", PQerrorMessage(db), callback_command);
PQfinish(db);
exit(EXIT_FAILURE);
}
PQclear(res);
callback_time = 0;
free(callback_command);
callback_command = 0;
} else {
if (options.verbose) {
fprintf(stderr, "Pinging database\n");
}
res = PQexec(db, "select 1");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Ping command failed: %s\n", PQerrorMessage(db));
PQfinish(db);
exit(EXIT_BADCONN);
}
PQclear(res);
}
break;
default:
break;
}
PQconsumeInput(db);
while ((notify = PQnotifies(db)) != NULL) {
int callback_delay;
int callback_starts;
if (options.verbose) {
fprintf(stderr, "Received notification: %s\n", notify->extra);
}
if (1 == sscanf(notify->extra, "%d %n", &callback_delay, &callback_starts)) {
if (callback_command) {
free(callback_command);
}
callback_command = strdup(notify->extra + callback_starts);
callback_time = time(0) + callback_delay;
} else {
fprintf(stderr, "Unable to parse notification '%s'\n", notify->extra);
PQfinish(db);
exit(EXIT_FAILURE);
}
PQfreemem(notify);
}
}
}
static void parse_options(int argc, char *argv[], struct opts * options)
{
static struct option long_options[] =
{
{"dbname", required_argument, NULL, 'd'},
{"host", required_argument, NULL, 'h'},
{"listen", required_argument, NULL, 'l'},
{"port", required_argument, NULL, 'p'},
{"quiet", no_argument, NULL, 'q'},
{"verbose", no_argument, NULL, 'v'},
{"username", required_argument, NULL, 'U'},
{"version", no_argument, NULL, 'V'},
{"no-password", no_argument, NULL, 'w'},
{"help", no_argument, NULL, '?'}
};
int optindex;
extern char *optarg;
extern int optind;
int c;
memset(options, 0, sizeof *options);
while ((c = getopt_long(argc, argv, "d:h:l:p:qU:Vvw?", long_options, &optindex)) != -1) {
switch (c)
{
case 'd':
options->dbname = optarg;
break;
case 'h':
options->host = optarg;
break;
case 'l':
options->listen = optarg;
break;
case 'p':
options->port = optarg;
break;
case 'q':
break;
case 'U':
options->username = optarg;
break;
case 'V':
showVersion();
exit(EXIT_SUCCESS);
break;
case 'v':
options->verbose = true;
break;
case 'w':
options->no_password = true;
break;
case '?':
if (strcmp(argv[optind-1], "-?") == 0
|| strcmp(argv[optind-1], "--help") == 0) {
usage();
exit(EXIT_SUCCESS);
}
/* FALLTHROUGH */
default:
fprintf(stderr, "Try \"%s --help\" for more information.\n",
progname);
exit(EXIT_FAILURE);
}
}
}
static void usage()
{
printf(_("Usage:\n"));
printf(_(" %s [OPTION]... [DBNAME [USERNAME]]\n\n"), progname);
printf(_(" -l, --listen=CHANNEL listen for notifications on this channel (default pglater)\n"));
printf(_(" --help show this help, then exit\n"));
printf(_(" -v, --verbose log activity to stderr\n"));
printf(_(" --version output version information, then exit\n"));
printf(_("\nConnection options:\n"));
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
printf(_(" -p, --port=PORT database server port\n"));
printf(_(" -U, --username=USERNAME database user name\n"));
printf(_(" -d, --dbname=DBNAME database name to connect to\n"));
printf(_(" -w, --no-password never prompt for password\n"));
}
static void showVersion()
{
printf(_("Version goes here\n"));
}