This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
forked from begriffs/pg_listen
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg_listen.c
225 lines (193 loc) · 4.47 KB
/
pg_listen.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
#include <errno.h>
#include <libpq-fe.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
void listen_forever(PGconn *, const char *, const char *, char **);
int reset_if_necessary(PGconn *);
void clean_and_die(PGconn *);
void begin_listen(PGconn *, const char *);
int print_log(const char *, const char *,...);
int exec_pipe(const char *cmd, char **cmd_argv, const char *input);
#define BUFSZ 512
int
main(int argc, char **argv)
{
PGconn *conn;
char *chan;
if (argc < 3)
{
fprintf(stderr,
"USAGE: %s db-url channel [/path/to/program] [args]\n",
argv[0]);
return EXIT_FAILURE;
}
/* if no command given, print payload with line buffering */
if (argc == 3)
setvbuf(stdout, NULL, _IOLBF, 0);
conn = PQconnectdb(argv[1]);
if (PQstatus(conn) != CONNECTION_OK)
{
print_log("CRITICAL", PQerrorMessage(conn));
clean_and_die(conn);
}
chan = PQescapeIdentifier(conn, argv[2], BUFSZ);
if (chan == NULL)
{
print_log("CRITICAL", PQerrorMessage(conn));
clean_and_die(conn);
}
/* safe since argv[argc] == NULL by C99 5.1.2.2.1 */
listen_forever(conn, chan, argv[3], argv + 3);
/* should never get here */
PQfreemem(chan);
PQfinish(conn);
return EXIT_SUCCESS;
}
int
exec_pipe(const char *cmd, char **cmd_argv, const char *input)
{
int pipefds[2];
/* we'll send "input" through pipe to stdin */
if (errno = 0, pipe(pipefds) < 0)
{
print_log("ERROR", "pipe(): %s", strerror(errno));
return 0;
}
switch (errno = 0, fork())
{
case -1:
print_log("ERROR", "fork(): %s", strerror(errno));
close(pipefds[0]);
close(pipefds[1]);
return 0;
case 0: /* Child - reads from pipe */
/* Write end is unused */
close(pipefds[1]);
/* read from pipe as stdin */
if (errno = 0, dup2(pipefds[0], STDIN_FILENO) < 0)
{
print_log("ERROR",
"Unable to assign stdin to pipe: %s",
strerror(errno));
close(pipefds[0]);
exit(EXIT_FAILURE);
}
if (errno = 0, execv(cmd, cmd_argv) < 0)
{
print_log("ERROR", "execv(%s): %s",
cmd, strerror(errno));
close(pipefds[0]);
exit(EXIT_FAILURE);
}
/* should not get here */
break;
default: /* Parent - writes to pipe */
close(pipefds[0]); /* Read end is unused */
write(pipefds[1], input, strlen(input));
close(pipefds[1]);
break;
}
return 1;
}
void
listen_forever(PGconn *conn, const char *chan, const char *cmd, char **cmd_argv)
{
int sock;
PGnotify *notify;
struct pollfd pfd[1];
begin_listen(conn, chan);
while (1)
{
if (reset_if_necessary(conn))
begin_listen(conn, chan);
sock = PQsocket(conn);
if (sock < 0)
{
print_log("CRITICAL",
"Failed to get libpq socket: %s\n",
PQerrorMessage(conn));
clean_and_die(conn);
}
pfd[0].fd = sock;
pfd[0].events = POLLIN;
if (errno = 0, poll(pfd, 1, -1) < 0)
{
print_log("CRITICAL", "poll(): %s", strerror(errno));
clean_and_die(conn);
}
PQconsumeInput(conn);
while ((notify = PQnotifies(conn)) != NULL)
{
if (!cmd)
fputs(notify->extra, stdout);
else if (!exec_pipe(cmd, cmd_argv, notify->extra))
{
PQfreemem(notify);
clean_and_die(conn);
}
PQfreemem(notify);
}
}
}
int
reset_if_necessary(PGconn *conn)
{
unsigned int seconds = 0;
if (PQstatus(conn) == CONNECTION_OK)
return 0;
do
{
if (seconds == 0)
seconds = 1;
else
{
print_log("ERROR", "Connection failed.\nSleeping %u seconds.", seconds);
sleep(seconds);
seconds *= 2;
}
print_log("INFO", "Reconnecting to database...");
PQreset(conn);
} while (PQstatus(conn) != CONNECTION_OK);
return 1;
}
void
begin_listen(PGconn *conn, const char *chan)
{
PGresult *res;
char sql[7 + BUFSZ + 1];
print_log("INFO", "Listening on channel %s", chan);
snprintf(sql, 7 + BUFSZ + 1, "LISTEN %s", chan);
res = PQexec(conn, sql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
print_log("CRITICAL", "LISTEN command failed: %s", PQerrorMessage(conn));
PQclear(res);
clean_and_die(conn);
}
PQclear(res);
}
void
clean_and_die(PGconn *conn)
{
PQfinish(conn);
exit(EXIT_FAILURE);
}
int
print_log(const char *sev, const char *fmt,...)
{
va_list ap;
time_t now = time(NULL);
char timestamp[128];
int res;
strftime(timestamp, sizeof timestamp, "%Y-%m-%dT%H:%M:%S", gmtime(&now));
res = fprintf(stderr, "%s - pg_listen - %s - ", timestamp, sev);
va_start(ap, fmt);
res += vfprintf(stderr, fmt, ap);
va_end(ap);
return res + fprintf(stderr, "\n");
}