From b15991043ef1d0eb0e4527b0d1bd38b4bab2d047 Mon Sep 17 00:00:00 2001 From: jun7 Date: Sun, 3 Jun 2018 17:13:12 +0900 Subject: [PATCH] Add file monitor to reboot automatically when exe is updated --- makefile | 2 +- wyebrun.c | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/makefile b/makefile index 29c6423..d6c7368 100644 --- a/makefile +++ b/makefile @@ -29,7 +29,7 @@ librun.o: wyebrun.c wyebrun.h makefile testrun: wyebrun.c wyebrun.h makefile $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< \ - `pkg-config --cflags --libs glib-2.0` \ + `pkg-config --cflags --libs glib-2.0 gio-2.0` \ -DDEBUG=1 clean: diff --git a/wyebrun.c b/wyebrun.c index 7a250aa..3d6f628 100644 --- a/wyebrun.c +++ b/wyebrun.c @@ -25,6 +25,9 @@ along with wyebrun. If not, see . //flock #include +//monitor +#include + #include "wyebrun.h" #define ROOTNAME "wyebrun" @@ -60,8 +63,6 @@ typedef enum { - - //shared static void fatal(int i) { @@ -134,9 +135,11 @@ static GSource *ipcwatch(char *exe, char *name, GMainContext *ctx) { static char *svrexe = NULL; static GMainLoop *sloop = NULL; static wyebdataf dataf = NULL; +static GHashTable *orders = NULL; + static gboolean quit(gpointer p) { - DD(\nsvr quits\n) + DD(SVR QUITS\n) g_main_loop_quit(sloop); return false; } @@ -156,7 +159,19 @@ static gpointer pingt(gpointer p) g_main_loop_run(g_main_loop_new(ctx, true)); return NULL; } - +static gboolean quitif(gpointer p) +{ +DD(quitif) + static int cnt = 0; + if (cnt++ > 30 /*3 sec*/ || !g_hash_table_size(orders)) quit(NULL); + return true; +} +static void monitorcb(GFileMonitor *m, GFile *f, GFile *o, GFileMonitorEvent e, + gpointer p) +{ + if (e != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) return; + g_timeout_add(100, quitif, NULL); +} void wyebwatch(char *exe, char *caller, wyebdataf func) { svrexe = exe; @@ -173,6 +188,16 @@ void wyebwatch(char *exe, char *caller, wyebdataf func) static gboolean svrinit(char *caller) { wyebwatch(svrexe, caller, dataf); + + char path[PATH_MAX]; + readlink("/proc/self/exe", path, PATH_MAX); + D(exepath %s, path) + GFile *gf = g_file_new_for_path(path); + GFileMonitor *gm = g_file_monitor_file( + gf, G_FILE_MONITOR_NONE, NULL, NULL); + g_signal_connect(gm, "changed", G_CALLBACK(monitorcb), NULL); + g_object_unref(gf); + return false; } @@ -474,7 +499,6 @@ void wyebclient(char *exe) //@ipccb -static GHashTable *orders = NULL; gboolean ipccb(GIOChannel *ch, GIOCondition c, gpointer p) { if (!orders) @@ -495,7 +519,7 @@ gboolean ipccb(GIOChannel *ch, GIOCondition c, gpointer p) #if DEBUG static int i = 0; - D(ipccb%d %c/%s/%s;, i++, type ,id ,arg) + D(%c ipccb%d %c/%s/%s;, svrexe ? 'S':'C', i++, type ,id ,arg) #endif static int lastuntil = DUNTIL; @@ -549,10 +573,6 @@ int main(int argc, char **argv) // gint64 now = g_get_monotonic_time(); // D(time %ld %ld, now - start, now) - //char path[PATH_MAX] = {0}; - //readlink("/proc/self/exe", path, PATH_MAX); - //D(progrname %s, path) - if (!wyebsvr(argc, argv, testdata)) wyebclient(argv[0]);