Skip to content

Commit

Permalink
DSO: Support hot-swapping, enabled with new flag DL_HOTSWAP.
Browse files Browse the repository at this point in the history
- lib/dl_lib.c:
   - Extract function `DL_invoke()` from `DL_func()`
   - Add functions `DL_get_hotswap()` and `DL_func_hotswap`
      for reloading DSO libraries when `DL_HOTSWAP` is enabled.
- include/dao.h: Add declaration of functions `DL_get_hotswap()` and `DL_func_hotswap`.

- maple/: menu.c: `domenu()`, popupmenu.c: `do_cmd()` & xover.c: `xover()`:
   Do not modify the callback table when `DL_HOTSWAP` is enabled.

- include/cppdef.h: Add macro `DL_HOTSWAP_SCOPE` for declaring DSO object pointers.
- maple/: Do not preserve DSO function pointers when `DL_HOTSWAP` is enabled.

- sample/dreambbs.conf: Add build config `DL_HOTSWAP` for enabling hot-swapping.
- include/global.h: Add `MODULE_DL_HOTSWAP` for `module_flags`.
- so/xyz.c: `x_siteinfo()`: Add information for hot-swapping enabling information.
  • Loading branch information
IepIweidieng committed Feb 13, 2020
1 parent 9d72e6c commit b662e9d
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 18 deletions.
22 changes: 20 additions & 2 deletions include/cppdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ template <class T>

/* Macros for managing loading of dynamic libraries */

#include "config.h"

#if NO_SO

#define DL_NAME(module_str, obj) (&obj)
Expand All @@ -185,12 +187,20 @@ template <class T>
#define DL_NAME_GET(module_str, obj) (&obj)
#define DL_NAME_CALL(module_str, func) func

#undef DL_HOTSWAP

#else // #if NO_SO

#define DL_NAME(module_str, obj) \
BINARY_SUFFIX module_str ":" CPP_STR(CPP_UNPAREN_OPT(obj))
#define DL_GET(dl_name) DL_get(dl_name)
#define DL_CALL(dl_name) DL_func((dl_name), CPP_APPEND_CLOSEPAREN

#ifdef DL_HOTSWAP
#define DL_GET(dl_name) DL_get_hotswap(dl_name)
#define DL_CALL(dl_name) DL_func_hotswap((dl_name), CPP_APPEND_CLOSEPAREN
#else
#define DL_GET(dl_name) DL_get(dl_name)
#define DL_CALL(dl_name) DL_func((dl_name), CPP_APPEND_CLOSEPAREN
#endif

#ifdef CPP_TYPEOF
#define DL_NAME_GET(module_str, obj) \
Expand All @@ -207,6 +217,14 @@ template <class T>

#endif // #if NO_SO

/* IID.20200103: For making dynamic libraries hot-swappable. */
#ifdef DL_HOTSWAP
#define DL_HOTSWAP_SCOPE /* Function local */
#else
#define DL_HOTSWAP_SCOPE static
#endif


/* Macros for emitting warnings */

#define CPP_PRAGMA(arg) _Pragma(CPP_STR(arg))
Expand Down
2 changes: 2 additions & 0 deletions include/dao.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ void archiv32(time_t chrono, char *fname);
void archiv32m(time_t chrono, char *fname);
/* dl_lib.c */
void *DL_get(const char *name);
void *DL_get_hotswap(const char *name);
int DL_func(const char *name, ...);
int DL_func_hotswap(const char *name, ...);
/* record.c */
int rec_add(const char *fpath, const void *data, int size);
int rec_bot(const char *fpath, const void *data, int size);
Expand Down
2 changes: 2 additions & 0 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ VAR const int build_langver INI(IF_DEF(__cplusplus, (int)__cplusplus, (int
#define MODULE_GRAYOUT (1U << 3)
#define MODULE_M3_USE_BBSLUA (1U << 4)
#define MODULE_M3_USE_BBSRUBY (1U << 5)
#define MODULE_DL_HOTSWAP (1U << 6)

#ifdef HAVE_BBSLUA
VAR const char bbslua_version_str[] INI(
Expand All @@ -564,6 +565,7 @@ VAR const unsigned int module_flags INI(
| MODULE_STATUS(GRAYOUT)
| MODULE_STATUS(M3_USE_BBSLUA)
| MODULE_STATUS(M3_USE_BBSRUBY)
| MODULE_STATUS(DL_HOTSWAP)
);
#undef MODULE_STATUS

Expand Down
32 changes: 30 additions & 2 deletions lib/dl_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,29 @@ void *DL_get(const char *name)
return dlsym(p->handle, t+1);
}

int DL_func(const char *name, ...)
void *DL_get_hotswap(const char *name)
{
const char *t = DL_name_delim(name);
DL_list *p;
if (!t)
return NULL;

p = DL_insert(name, t - name);
if (p->handle) /* Unload the library to load a updated one */
dlclose(p->handle);

p->handle = dlopen(p->path, DL_OPEN_FLAGS);
if (!p->handle)
return NULL;

return dlsym(p->handle, t+1);
}

/* Dynamic library function invokers */

static int DL_invoke(int (*f)(va_list), ...)
{
va_list args;
int (*f)(va_list) = (int (*)(va_list)) DL_get(name);
int ret;

if (!f) /* not get func */
Expand All @@ -107,3 +126,12 @@ int DL_func(const char *name, ...)

return ret;
}

int DL_func(const char *name, ...)
{
return DL_invoke((int (*)(va_list)) DL_get(name));
}
int DL_func_hotswap(const char *name, ...)
{
return DL_invoke((int (*)(va_list)) DL_get_hotswap(name));
}
4 changes: 1 addition & 3 deletions maple/acct.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,6 @@ void acct_setup(ACCT * u, int adm)
ACCT x;
int y, c;

int (*sm) (char *mail);

int i, num, tmp, mode;
FILE *flog;
char *str, buf[80], pass[PLAINPASSLEN];
Expand All @@ -998,7 +996,6 @@ void acct_setup(ACCT * u, int adm)
acct_show(u, (adm) ? 3 : 0);

memcpy(&x, u, sizeof(ACCT));
sm = NULL;

if (((u->userlevel & PERM_SYSOP) && strcmp(cuser.userid, u->userid))
&& !check_admin(cuser.userid))
Expand Down Expand Up @@ -1083,6 +1080,7 @@ void acct_setup(ACCT * u, int adm)
}
if (adm == '3')
{
DL_HOTSWAP_SCOPE int (*sm) (char *mail) = NULL;
switch (vans("使用程序 i)內部 o)外部 q)取消:[q]"))
{
case 'i':
Expand Down
2 changes: 1 addition & 1 deletion maple/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,6 @@ vedit(
{
textline *vln, *tmp;
int cc, col, mode, margin, pos;
static void (*input_tool)(void);

/* --------------------------------------------------- */
/* 初始設定:載入檔案、引用文章、設定編輯模式 */
Expand Down Expand Up @@ -2044,6 +2043,7 @@ vedit(
#ifdef HAVE_INPUT_TOOLS
if (cc == VE_INPUTOOL)
{
DL_HOTSWAP_SCOPE void (*input_tool)(void) = NULL;
if (!input_tool)
{
input_tool = DL_NAME_GET("ascii.so", input_tools);
Expand Down
2 changes: 1 addition & 1 deletion maple/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,7 @@ static int
mbox_gem(
XO *xo)
{
static void (*mgp)(void);
DL_HOTSWAP_SCOPE void (*mgp)(void) = NULL;
if (!HAS_PERM(PERM_MBOX))
return XO_NONE;
if (!mgp)
Expand Down
2 changes: 2 additions & 0 deletions maple/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,10 @@ domenu(
mitem.func = (int (*)(void)) DL_GET(mitem.dlfunc);
if (!mitem.func) break;
mmode = -mmode;
#ifndef DL_HOTSWAP
mptr->item = mitem;
mptr->umode = mmode;
#endif
}
#endif
utmp_mode(mmode /* = mptr->umode*/);
Expand Down
4 changes: 4 additions & 0 deletions maple/popupmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ do_cmd(MENU *mptr, XO *xo, int x, int y)
if (!mitem.xofunc)
return 0;
mmode = -mmode;
#ifndef DL_HOTSWAP
mptr->item = mitem;
mptr->umode = mmode;
#endif
}
#endif

Expand All @@ -84,8 +86,10 @@ do_cmd(MENU *mptr, XO *xo, int x, int y)
scr_free(&old_screen);
return 0;
}
#ifndef DL_HOTSWAP
mptr->item = mitem;
mptr->umode = POPUP_FUN;
#endif
mode = (*mitem.func) ();
break;
#endif
Expand Down
8 changes: 2 additions & 6 deletions maple/talk.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ typedef struct
char ship[46];
} PAL_SHIP;

#ifdef HAVE_PIP_FIGHT
void (*p)(void);
#endif



static int pal_count;
static int *pal_pool;
static int bmw_modetype;
Expand Down Expand Up @@ -2953,6 +2947,7 @@ talk_page(
#ifdef HAVE_PIP_FIGHT
else if (ans == 'c')
{
DL_HOTSWAP_SCOPE void (*p)(void) = NULL;
if (!p)
p = DL_NAME_GET("pip.so", pip_vf_fight);
if (p)
Expand Down Expand Up @@ -4366,6 +4361,7 @@ talk_rqst(void)
#ifdef HAVE_PIP_FIGHT
else if (ans == 'c')
{
DL_HOTSWAP_SCOPE void (*p)(void) = NULL;
if (!p)
p = DL_NAME_GET("pip.so", pip_vf_fight);
strcpy(cutmp->mateid, up->userid);
Expand Down
9 changes: 7 additions & 2 deletions maple/xover.c
Original file line number Diff line number Diff line change
Expand Up @@ -1682,11 +1682,16 @@ xover(
if (p)
{
#ifdef HAVE_HASH_KEYFUNCLIST
#ifdef DL_HOTSWAP
xcmd->erase(num);
cb = xcmd->insert({cmd, {p}}).first;
#endif
#else
#ifdef DL_HOTSWAP
cb->second.func = p;
pos = cb->first = cmd;
cb->first = cmd;
#endif
pos = cmd;
#endif
}
else
Expand Down Expand Up @@ -1929,7 +1934,7 @@ xover(
#ifdef HAVE_MAILGEM
else if (cmd == 'G' && HAS_PERM(PERM_MBOX))
{
static int (*mgp)(XO *xo);
DL_HOTSWAP_SCOPE int (*mgp)(XO *xo) = NULL;
if (!mgp)
{
mgp = DL_NAME_GET("mailgem.so", mailgem_gather);
Expand Down
5 changes: 5 additions & 0 deletions sample/dreambbs.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"\033[1;32m�� Origin: \033[35m�ڦR�毸 \033[33m<news.clam.ml> \033[31m�� From:\033[36m %-34.34s \033[m\n"


/* �}�� DSO �Ҳժ� hot swapping Enable hot swapping for DSO modules */
/* �}�ҫᤣ�����s�n�J�Y�i�M�� DSO �Ҳժ���s */
/* Re-login is not required to reload DSO modules when enabled */
//#define DL_HOTSWAP

/* �}�� pmore �\Ū������ */
//#define M3_USE_PMORE /* piaip's more 2007+ */

Expand Down
4 changes: 3 additions & 1 deletion so/xyz.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ x_siteinfo(void)
prints("\x1b[1;33mInternet Technology Lab\x1b[37m, Institute of CCE, National Cheng Kung University.\x1b[m\n");
prints("\n");
#ifdef Modules
prints("Modules & Plug-in: \x1b[m\n\n");
#define CHECK_CONF(conf) ((bool)(module_flags & (MODULE_ ## conf)))
prints("Modules & Plug-in: %s\x1b[m\n\n",
CHECK_CONF(DL_HOTSWAP) ? "[\x1b[1;32mHotswap enabled\x1b[m]" : "[\x1b[1;31mHotswap disabled\x1b[m]");

//模組化的放在這邊
#define ONLINE_STR "\x1b[1;32monline \x1b[m"
Expand Down

0 comments on commit b662e9d

Please sign in to comment.