Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ endif

REDIS_SERVER_NAME=redis-server
REDIS_SENTINEL_NAME=redis-sentinel
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o gopher.o tracking.o connection.o tls.o sha256.o timeout.o

REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o redis_err.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o gopher.o tracking.o connection.o tls.o sha256.o timeout.o
REDIS_CLI_NAME=redis-cli
REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o ae.o crc64.o siphash.o crc16.o
REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o redis_err.o release.o ae.o crc64.o siphash.o crc16.o
REDIS_BENCHMARK_NAME=redis-benchmark
REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o siphash.o
REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o redis_err.o siphash.o

REDIS_CHECK_RDB_NAME=redis-check-rdb
REDIS_CHECK_AOF_NAME=redis-check-aof

Expand Down
3 changes: 2 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "server.h"
#include "cluster.h"
#include "redis_err.h"

#include <fcntl.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -550,7 +551,7 @@ void loadServerConfig(char *filename, char *options) {
} else {
if ((fp = fopen(filename,"r")) == NULL) {
serverLog(LL_WARNING,
"Fatal error, can't open config file '%s'", filename);
"Fatal error, can't open config file '%s', %s", filename, redisError(errno));
exit(1);
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/redis_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* strerror is not thread safe
* why oh why should I complain
* when strerror_r & _l behave differently
* Across the Unix domain
*
* after distinguishing between the two
* And allocating error strings all over
* You have ugly Marcos
* (and are still not async signal safe)
*
* Allocate a static array containing all possible error strings
*/

#include <errno.h>
#include <string.h>
#include <stdio.h>

#include "redis_err.h"
#include "zmalloc.h"

const char **ERROR_ARRAY;
/* sys_nerr is considered internal impl. detail, and isn't available on all OSes */
const unsigned int MAX_ERR = 256;
const char *UNKNOWN_STRING = "Unknown error";

/*
* Initialize the error array
*/
int initErrorStrings(){
ERROR_ARRAY = zmalloc(sizeof(char*)*(MAX_ERR));

unsigned int n = 0;
char *err;

for (; n < MAX_ERR; n++) {
err = strerror(n);
if (err == NULL
|| strncasecmp(err, UNKNOWN_STRING, 13) == 0)
{
ERROR_ARRAY[n] = UNKNOWN_STRING;
continue;
}
ERROR_ARRAY[n] = zstrdup(err);
}
return 0;
}


const char *redisError(unsigned int err) {
if (err > MAX_ERR) {
perror("Requested out of bounds error string");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think you should print anything. no one usually monitors stderr, and not sure there's a point in printing anyway since the returned string probably goes to the log.
what can be helpful is stack trace, in case the one that prints to the log doesn't have a distinct message.

return UNKNOWN_STRING;
}
return ERROR_ARRAY[err];
}


void freeErrorStrings() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redis doesn't bother to free memory.. you can keep this method, but don't bother to call it.

unsigned int n = 0;
for (;n < MAX_ERR; n++) {
if (ERROR_ARRAY[n] != UNKNOWN_STRING) {
zfree((void *)ERROR_ARRAY[n]);
}
}

}
13 changes: 13 additions & 0 deletions src/redis_err.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Created by uri on 6/7/19.
//

#ifndef REDIS_REDIS_ERR_H
#define REDIS_REDIS_ERR_H


int initErrorStrings ();
const char *redisError(unsigned int err);
void freeErrorStrings();

#endif //REDIS_REDIS_ERR_H
9 changes: 6 additions & 3 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "bio.h"
#include "latency.h"
#include "atomicvar.h"
#include "redis_err.h"

#include <time.h>
#include <signal.h>
Expand Down Expand Up @@ -1767,7 +1768,7 @@ void checkChildrenDone(void) {
if (pid == -1) {
serverLog(LL_WARNING,"wait3() returned an error: %s. "
"rdb_child_pid = %d, aof_child_pid = %d, module_child_pid = %d",
strerror(errno),
redisError(errno),
(int) server.rdb_child_pid,
(int) server.aof_child_pid,
(int) server.module_child_pid);
Expand Down Expand Up @@ -2843,7 +2844,7 @@ void initServer(void) {
O_WRONLY|O_APPEND|O_CREAT,0644);
if (server.aof_fd == -1) {
serverLog(LL_WARNING, "Can't open the append-only file: %s",
strerror(errno));
redisError(errno));
exit(1);
}
}
Expand Down Expand Up @@ -4773,7 +4774,7 @@ void loadDataFromDisk(void) {
selectDb(server.cached_master,rsi.repl_stream_db);
}
} else if (errno != ENOENT) {
serverLog(LL_WARNING,"Fatal error loading the DB: %s. Exiting.",strerror(errno));
serverLog(LL_WARNING,"Fatal error loading the DB: %s. Exiting.",redisError(errno));
exit(1);
}
}
Expand Down Expand Up @@ -4902,6 +4903,7 @@ int main(int argc, char **argv) {
setlocale(LC_COLLATE,"");
tzset(); /* Populates 'timezone' global. */
zmalloc_set_oom_handler(redisOutOfMemoryHandler);
initErrorStrings();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unit test code above may some day use this mechanism too (by calling a method that calls this). unlikely, but maybe you should consider moving this to the top.

srand(time(NULL)^getpid());
gettimeofday(&tv,NULL);

Expand Down Expand Up @@ -5073,6 +5075,7 @@ int main(int argc, char **argv) {
aeSetAfterSleepProc(server.el,afterSleep);
aeMain(server.el);
aeDeleteEventLoop(server.el);
freeErrorStrings();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually dead code.. the normal place where redis exists, it with a call to exit after calling prepareForShutdown. but anyway, redis doesn't bother to release memory or shutdown.. that's a waste of time. i suggest to remove this line.

return 0;
}

Expand Down