| 327 | |
| 328 | |
| 329 | static redisConfig *getRedisConfig(const char *ip, int port, |
| 330 | const char *hostsocket) |
| 331 | { |
| 332 | redisConfig *cfg = zcalloc(sizeof(*cfg)); |
| 333 | if (!cfg) return NULL; |
| 334 | redisContext *c = NULL; |
| 335 | redisReply *reply = NULL, *sub_reply = NULL; |
| 336 | c = getRedisContext(ip, port, hostsocket); |
| 337 | if (c == NULL) { |
| 338 | freeRedisConfig(cfg); |
| 339 | return NULL; |
| 340 | } |
| 341 | redisAppendCommand(c, "CONFIG GET %s", "save"); |
| 342 | redisAppendCommand(c, "CONFIG GET %s", "appendonly"); |
| 343 | int i = 0; |
| 344 | void *r = NULL; |
| 345 | for (; i < 2; i++) { |
| 346 | int res = redisGetReply(c, &r); |
| 347 | if (reply) freeReplyObject(reply); |
| 348 | reply = res == REDIS_OK ? ((redisReply *) r) : NULL; |
| 349 | if (res != REDIS_OK || !r) goto fail; |
| 350 | if (reply->type == REDIS_REPLY_ERROR) { |
| 351 | fprintf(stderr, "ERROR: %s\n", reply->str); |
| 352 | goto fail; |
| 353 | } |
| 354 | if (reply->type != REDIS_REPLY_ARRAY || reply->elements < 2) goto fail; |
| 355 | sub_reply = reply->element[1]; |
| 356 | char *value = sub_reply->str; |
| 357 | if (!value) value = ""; |
| 358 | switch (i) { |
| 359 | case 0: cfg->save = sdsnew(value); break; |
| 360 | case 1: cfg->appendonly = sdsnew(value); break; |
| 361 | } |
| 362 | } |
| 363 | freeReplyObject(reply); |
| 364 | redisFree(c); |
| 365 | return cfg; |
| 366 | fail: |
| 367 | fprintf(stderr, "ERROR: failed to fetch CONFIG from "); |
| 368 | if (hostsocket == NULL) fprintf(stderr, "%s:%d\n", ip, port); |
| 369 | else fprintf(stderr, "%s\n", hostsocket); |
| 370 | int abort_test = 0; |
| 371 | if (reply && reply->type == REDIS_REPLY_ERROR && |
| 372 | (!strncmp(reply->str,"NOAUTH",5) || |
| 373 | !strncmp(reply->str,"WRONGPASS",9) || |
| 374 | !strncmp(reply->str,"NOPERM",5))) |
| 375 | abort_test = 1; |
| 376 | freeReplyObject(reply); |
| 377 | redisFree(c); |
| 378 | freeRedisConfig(cfg); |
| 379 | if (abort_test) exit(1); |
| 380 | return NULL; |
| 381 | } |
| 382 | static void freeRedisConfig(redisConfig *cfg) { |
| 383 | if (cfg->save) sdsfree(cfg->save); |
| 384 | if (cfg->appendonly) sdsfree(cfg->appendonly); |
no test coverage detected