liquid_feedback_core

view lf_update.c @ 624:82387194519b

Added code to suppress compiler warning (writing one byte into region of size 0)
author jbe
date Fri Feb 05 13:50:45 2021 +0100 (2 months ago)
parents aa23fa17604d
children
line source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <libpq-fe.h>
7 #define exec_sql_error(message) do { \
8 fprintf(stderr, message ": %s\n%s", command, PQresultErrorMessage(res)); \
9 goto exec_sql_error_clear; \
10 } while (0)
12 int exec_sql(PGconn *db, PGresult **resptr, int *errptr, int onerow, char *command) {
13 int count = 0;
14 PGresult *res = PQexec(db, command);
15 if (!res) {
16 fprintf(stderr, "Error in pqlib while sending the following SQL command: %s\n", command);
17 goto exec_sql_error_exit;
18 }
19 if (
20 PQresultStatus(res) != PGRES_COMMAND_OK &&
21 PQresultStatus(res) != PGRES_TUPLES_OK
22 ) exec_sql_error("Error while executing the following SQL command");
23 if (resptr) {
24 if (PQresultStatus(res) != PGRES_TUPLES_OK) exec_sql_error("The following SQL command returned no result");
25 count = PQntuples(res);
26 if (count < 0) exec_sql_error("The following SQL command returned too many rows");
27 if (onerow) {
28 if (count < 1) exec_sql_error("The following SQL command returned less than one row");
29 else if (count > 1) exec_sql_error("The following SQL command returned more than one row");
30 }
31 *resptr = res;
32 } else {
33 PQclear(res);
34 }
35 return count;
36 exec_sql_error_clear:
37 PQclear(res);
38 exec_sql_error_exit:
39 if (resptr) *resptr = NULL;
40 if (errptr) *errptr = 1;
41 return -1;
42 }
44 int main(int argc, char **argv) {
46 // variable declarations:
47 int err = 0; /* set to 1 if any error occured */
48 int admission_failed = 0; /* set to 1 if error occurred during admission */
49 int i, count;
50 char *conninfo;
51 PGconn *db;
52 PGresult *res;
54 // parse command line:
55 if (argc == 0) return 1;
56 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
57 FILE *out;
58 out = argc == 1 ? stderr : stdout;
59 fprintf(out, "\n");
60 fprintf(out, "Usage: %s <conninfo>\n", argv[0]);
61 fprintf(out, "\n");
62 fprintf(out, "<conninfo> is specified by PostgreSQL's libpq,\n");
63 fprintf(out, "see http://www.postgresql.org/docs/9.6/static/libpq-connect.html\n");
64 fprintf(out, "\n");
65 fprintf(out, "Example: %s dbname=liquid_feedback\n", argv[0]);
66 fprintf(out, "\n");
67 return argc == 1 ? 1 : 0;
68 }
69 {
70 size_t len = 0, seglen;
71 for (i=1; i<argc; i++) {
72 seglen = strlen(argv[i]) + 1;
73 if (seglen >= SIZE_MAX/2 || len >= SIZE_MAX/2) {
74 fprintf(stderr, "Error: Command line arguments too long\n");
75 return 1;
76 }
77 len += seglen;
78 }
79 if (!len) len = 1; // not needed but suppresses compiler warning
80 conninfo = malloc(len * sizeof(char));
81 if (!conninfo) {
82 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
83 return 1;
84 }
85 conninfo[0] = 0;
86 for (i=1; i<argc; i++) {
87 if (i>1) strcat(conninfo, " ");
88 strcat(conninfo, argv[i]);
89 }
90 }
92 // connect to database:
93 db = PQconnectdb(conninfo);
94 if (!db) {
95 fprintf(stderr, "Error: Could not create database handle\n");
96 return 1;
97 }
98 if (PQstatus(db) != CONNECTION_OK) {
99 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
100 return 1;
101 }
103 // delete expired sessions:
104 exec_sql(db, NULL, &err, 0, "DELETE FROM \"expired_session\"");
106 // delete expired tokens and authorization codes:
107 exec_sql(db, NULL, &err, 0, "DELETE FROM \"expired_token\"");
109 // delete unused snapshots:
110 exec_sql(db, NULL, &err, 0, "DELETE FROM \"unused_snapshot\"");
112 // check member activity:
113 exec_sql(db, NULL, &err, 0, "SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SELECT \"check_activity\"()");
115 // calculate member counts:
116 exec_sql(db, NULL, &err, 0, "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SELECT \"calculate_member_counts\"()");
118 // issue admission:
119 count = exec_sql(db, &res, &err, 0, "SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SELECT \"id\" FROM \"area_with_unaccepted_issues\"");
120 if (!res) admission_failed = 1;
121 else {
122 char *area_id, *escaped_area_id, *cmd;
123 PGresult *res2;
124 for (i=0; i<count; i++) {
125 area_id = PQgetvalue(res, i, 0);
126 escaped_area_id = PQescapeLiteral(db, area_id, strlen(area_id));
127 if (!escaped_area_id) {
128 fprintf(stderr, "Could not escape literal in memory.\n");
129 err = admission_failed = 1;
130 continue;
131 }
132 if (asprintf(&cmd, "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SELECT \"take_snapshot\"(NULL, %s)", escaped_area_id) < 0) {
133 fprintf(stderr, "Could not prepare query string in memory.\n");
134 err = admission_failed = 1;
135 PQfreemem(escaped_area_id);
136 continue;
137 }
138 exec_sql(db, &res2, &err, 1, cmd);
139 free(cmd);
140 if (!res2) admission_failed = 1;
141 else {
142 char *snapshot_id, *escaped_snapshot_id;
143 int j, count2;
144 snapshot_id = PQgetvalue(res2, 0, 0);
145 escaped_snapshot_id = PQescapeLiteral(db, snapshot_id, strlen(snapshot_id));
146 PQclear(res2);
147 if (!escaped_snapshot_id) {
148 fprintf(stderr, "Could not escape literal in memory.\n");
149 err = admission_failed = 1;
150 goto area_admission_cleanup;
151 }
152 if (asprintf(&cmd, "SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SELECT \"issue_id\" FROM \"snapshot_issue\" WHERE \"snapshot_id\" = %s", escaped_snapshot_id) < 0) {
153 fprintf(stderr, "Could not prepare query string in memory.\n");
154 err = admission_failed = 1;
155 PQfreemem(escaped_snapshot_id);
156 goto area_admission_cleanup;
157 }
158 PQfreemem(escaped_snapshot_id);
159 count2 = exec_sql(db, &res2, &err, 0, cmd);
160 free(cmd);
161 if (!res2) admission_failed = 1;
162 else {
163 char *issue_id, *escaped_issue_id;
164 for (j=0; j<count2; j++) {
165 issue_id = PQgetvalue(res2, j, 0);
166 escaped_issue_id = PQescapeLiteral(db, issue_id, strlen(issue_id));
167 if (!escaped_issue_id) {
168 fprintf(stderr, "Could not escape literal in memory.\n");
169 err = admission_failed = 1;
170 continue;
171 }
172 if (asprintf(&cmd, "SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SELECT \"finish_snapshot\"(%s)", escaped_issue_id) < 0) {
173 fprintf(stderr, "Could not prepare query string in memory.\n");
174 err = admission_failed = 1;
175 PQfreemem(escaped_issue_id);
176 continue;
177 }
178 PQfreemem(escaped_issue_id);
179 if (exec_sql(db, NULL, &err, 0, cmd) < 0) admission_failed = 1;
180 free(cmd);
181 }
182 PQclear(res2);
183 }
184 if (!admission_failed) {
185 if (asprintf(&cmd, "SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SELECT \"issue_admission\"(%s)", escaped_area_id) < 0) {
186 fprintf(stderr, "Could not prepare query string in memory.\n");
187 err = admission_failed = 1;
188 goto area_admission_cleanup;
189 }
190 }
191 while (1) {
192 exec_sql(db, &res2, &err, 1, cmd);
193 if (!res2) {
194 admission_failed = 1;
195 break;
196 }
197 if (PQgetvalue(res2, 0, 0)[0] != 't') {
198 PQclear(res2);
199 break;
200 }
201 PQclear(res2);
202 }
203 }
204 area_admission_cleanup:
205 PQfreemem(escaped_area_id);
206 }
207 PQclear(res);
208 }
210 // update open issues:
211 count = exec_sql(
212 db, &res, &err, 0,
213 admission_failed ?
214 "SELECT \"id\" FROM \"open_issue\" WHERE \"state\" != 'admission'::\"issue_state\"" :
215 "SELECT \"id\" FROM \"open_issue\""
216 );
217 for (i=0; i<count; i++) {
218 char *issue_id, *escaped_issue_id;
219 PGresult *res2, *old_res2;
220 int j;
221 issue_id = PQgetvalue(res, i, 0);
222 escaped_issue_id = PQescapeLiteral(db, issue_id, strlen(issue_id));
223 if (!escaped_issue_id) {
224 fprintf(stderr, "Could not escape literal in memory.\n");
225 err = 1;
226 continue;
227 }
228 old_res2 = NULL;
229 for (j=0; ; j++) {
230 if (j >= 20) { // safety to avoid endless loops
231 fprintf(stderr, "Function \"check_issue\"(...) returned non-null value too often.\n");
232 err = 1;
233 if (j > 0) PQclear(old_res2);
234 break;
235 }
236 if (j == 0) {
237 char *cmd;
238 if (asprintf(&cmd, "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SELECT \"check_issue\"(%s, NULL)", escaped_issue_id) < 0) {
239 fprintf(stderr, "Could not prepare query string in memory.\n");
240 err = 1;
241 break;
242 }
243 exec_sql(db, &res2, &err, 1, cmd);
244 free(cmd);
245 } else {
246 char *persist, *escaped_persist, *cmd;
247 persist = PQgetvalue(old_res2, 0, 0);
248 escaped_persist = PQescapeLiteral(db, persist, strlen(persist));
249 if (!escaped_persist) {
250 fprintf(stderr, "Could not escape literal in memory.\n");
251 err = 1;
252 PQclear(old_res2);
253 break;
254 }
255 if (asprintf(&cmd, "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SELECT \"check_issue\"(%s, %s::\"check_issue_persistence\")", escaped_issue_id, escaped_persist) < 0) {
256 PQfreemem(escaped_persist);
257 fprintf(stderr, "Could not prepare query string in memory.\n");
258 err = 1;
259 PQclear(old_res2);
260 break;
261 }
262 PQfreemem(escaped_persist);
263 exec_sql(db, &res2, &err, 1, cmd);
264 free(cmd);
265 PQclear(old_res2);
266 }
267 if (!res2) break;
268 if (PQgetisnull(res2, 0, 0)) {
269 PQclear(res2);
270 break;
271 }
272 old_res2 = res2;
273 }
274 PQfreemem(escaped_issue_id);
275 }
276 if (res) PQclear(res);
278 // delete unused snapshots:
279 exec_sql(db, NULL, &err, 0, "DELETE FROM \"unused_snapshot\"");
281 // cleanup and exit:
282 PQfinish(db);
283 return err;
285 }

Impressum / About Us