static int table_proc_fetch(void *arg, enum table_service s, union lookup *lk) { struct table_proc_priv *priv = arg; struct ibuf *buf; int r; buf = imsg_create(&priv->ibuf, PROC_TABLE_FETCH, 0, 0, sizeof(s)); if (buf == NULL) return (-1); if (imsg_add(buf, &s, sizeof(s)) == -1) return (-1); imsg_close(&priv->ibuf, buf); table_proc_call(priv); table_proc_read(&r, sizeof(r)); if (r == 1) { if (rlen == 0) { log_warnx("warn: table-proc: empty response"); fatalx("table-proc: exiting"); } if (rdata[rlen - 1] != '\0') { log_warnx("warn: table-proc: not NUL-terminated"); fatalx("table-proc: exiting"); } r = table_parse_lookup(s, NULL, rdata, lk); table_proc_read(NULL, rlen); } table_proc_end(); return (r); }
static int table_static_fetch(void *hdl, enum table_service service, union lookup *lk) { struct table *t = hdl; const char *k; if (! dict_iter(&t->t_dict, &t->t_iter, &k, (void **)NULL)) { t->t_iter = NULL; if (! dict_iter(&t->t_dict, &t->t_iter, &k, (void **)NULL)) return 0; } if (lk == NULL) return 1; return table_parse_lookup(service, NULL, k, lk); }
static int table_static_lookup(void *hdl, const char *key, enum table_service service, union lookup *lk) { struct table *m = hdl; char *line; int ret; int (*match)(const char *, const char *) = NULL; size_t i; void *iter; const char *k; char *v; for (i = 0; i < nitems(keycmp); ++i) if (keycmp[i].service == service) match = keycmp[i].func; line = NULL; iter = NULL; ret = 0; while (dict_iter(&m->t_dict, &iter, &k, (void **)&v)) { if (match) { if (match(key, k)) { line = v; ret = 1; } } else { if (strcmp(key, k) == 0) { line = v; ret = 1; } } if (ret) break; } if (lk == NULL) return ret ? 1 : 0; if (ret == 0) return 0; return table_parse_lookup(service, key, line, lk); }
static int table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_service s, union lookup *lk) { struct table_proc_priv *priv = arg; struct ibuf *buf; int r; buf = imsg_create(&priv->ibuf, lk ? PROC_TABLE_LOOKUP : PROC_TABLE_CHECK, 0, 0, sizeof(s) + strlen(k) + 1); if (buf == NULL) return (-1); if (imsg_add(buf, &s, sizeof(s)) == -1) return (-1); if (imsg_add_params(buf, params) == -1) return (-1); if (imsg_add(buf, k, strlen(k) + 1) == -1) return (-1); imsg_close(&priv->ibuf, buf); table_proc_call(priv); table_proc_read(&r, sizeof(r)); if (r == 1 && lk) { if (rlen == 0) { log_warnx("warn: table-proc: empty response"); fatalx("table-proc: exiting"); } if (rdata[rlen - 1] != '\0') { log_warnx("warn: table-proc: not NUL-terminated"); fatalx("table-proc: exiting"); } r = table_parse_lookup(s, k, rdata, lk); table_proc_read(NULL, rlen); } table_proc_end(); return (r); }
static int table_db_lookup(void *hdl, const char *key, enum table_service service, union lookup *lk) { struct dbhandle *handle = hdl; struct table *table = NULL; char *line; size_t len = 0; int ret; int (*match)(const char *, const char *) = NULL; size_t i; struct stat sb; if (stat(handle->pathname, &sb) < 0) return -1; /* DB has changed, close and reopen */ if (sb.st_mtime != handle->mtime) { table = handle->table; table_db_update(handle->table); handle = table->t_handle; } for (i = 0; i < nitems(keycmp); ++i) if (keycmp[i].service == service) match = keycmp[i].func; if (match == NULL) line = table_db_get_entry(handle, key, &len); else line = table_db_get_entry_match(handle, key, &len, match); if (line == NULL) return 0; ret = 1; if (lk) ret = table_parse_lookup(service, key, line, lk); free(line); return ret; }
static int table_db_fetch(void *hdl, enum table_service service, union lookup *lk) { struct dbhandle *handle = hdl; struct table *table = handle->table; DBT dbk; DBT dbd; int r; if (table->t_iter == NULL) r = handle->db->seq(handle->db, &dbk, &dbd, R_FIRST); else r = handle->db->seq(handle->db, &dbk, &dbd, R_NEXT); table->t_iter = handle->db; if (!r) { r = handle->db->seq(handle->db, &dbk, &dbd, R_FIRST); if (!r) return 0; } return table_parse_lookup(service, NULL, dbk.data, lk); }