22#include <stdlib.h>
33#include <dlfcn.h>
44#include <signal.h>
5+ #include <unistd.h>
56
67#include <jemalloc/jemalloc.h>
78
89#include "server.h"
910
1011#define LIB_REDIS_PATH "./redis-server.so"
12+ #define LIB_REDIS_BUSY_PATH "./redis-server.so.busy"
1113
1214#define malloc (size ) je_malloc(size)
13- #define calloc (count ,size ) je_calloc(count,size)
14- #define realloc (ptr ,size ) je_realloc(ptr,size)
1515#define free (ptr ) je_free(ptr)
16- #define mallocx (size ,flags ) je_mallocx(size,flags)
17- #define dallocx (ptr ,flags ) je_dallocx(ptr,flags)
1816
1917typedef int (* ENTRY )(int , char * * , struct wrapperContext * );
2018typedef int (* RELOAD )(int );
21- typedef char * (* GETDB )();
2219
2320int main (int argc , char * * argv )
2421{
@@ -28,6 +25,7 @@ int main(int argc, char ** argv)
2825 RELOAD reload = NULL ;
2926 int ret ;
3027 char * mem = NULL ;
28+ int err = 0 ;
3129
3230 ctx .reload = 0 ;
3331
@@ -42,9 +40,30 @@ int main(int argc, char ** argv)
4240 }
4341 free (mem );
4442
43+ /* check so file */
44+ if (access (LIB_REDIS_BUSY_PATH , R_OK ) != 0 ) {
45+ err = errno ;
46+ fprintf (stderr , "file %s access error:%s\n" , LIB_REDIS_BUSY_PATH , strerror (err ));
47+
48+ if (access (LIB_REDIS_PATH , R_OK ) != 0 ) {
49+ err = errno ;
50+ fprintf (stderr , "file %s access error:%s\n" , LIB_REDIS_PATH , strerror (err ));
51+ fprintf (stderr , "dynamic library file not exists, please check.\n" );
52+ exit (EXIT_FAILURE );
53+ } else {
54+ /* rename so file to busy */
55+ if (rename (LIB_REDIS_PATH , LIB_REDIS_BUSY_PATH ) != 0 ) {
56+ err = errno ;
57+ fprintf (stderr , "rename file failed:%s\n" , strerror (err ));
58+ exit (EXIT_FAILURE );
59+ }
60+ }
61+ }
62+
63+
4564 for (;;) {
4665 fprintf (stderr , "load dynamic library\n" );
47- handle = dlopen (LIB_REDIS_PATH , RTLD_LAZY );
66+ handle = dlopen (LIB_REDIS_BUSY_PATH , RTLD_LAZY );
4867 if (!handle ) {
4968 fprintf (stderr , "%s\n" , dlerror ());
5069 exit (EXIT_FAILURE );
@@ -71,6 +90,19 @@ int main(int argc, char ** argv)
7190 }
7291 ctx .reload = 1 ;
7392 ctx .reloadTimes ++ ;
93+
94+ /* use new so file */
95+ if (access (LIB_REDIS_PATH , R_OK ) == 0 ) {
96+ /* rename so file */
97+ if (rename (LIB_REDIS_PATH , LIB_REDIS_BUSY_PATH ) != 0 ) {
98+ err = errno ;
99+ fprintf (stderr , "rename %s to %s failed: %s" , LIB_REDIS_PATH ,
100+ LIB_REDIS_BUSY_PATH , strerror (err ));
101+ fprintf (stderr , "use old so file instead" );
102+ }
103+ } else {
104+ fprintf (stderr , "no newer so file found, using old so file instead" );
105+ }
74106 }
75107 exit (EXIT_SUCCESS );
76108}
0 commit comments