2018-05-19 15:51:50 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 jun7@hush.mail
|
|
|
|
|
|
|
|
This file is part of wyebrun.
|
|
|
|
|
|
|
|
wyebrun is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
wyebrun is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with wyebrun. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//getpid
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
//flock
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
2018-06-03 08:13:12 +00:00
|
|
|
//monitor
|
|
|
|
#include <gio/gio.h>
|
|
|
|
|
2018-05-19 15:51:50 +00:00
|
|
|
#include "wyebrun.h"
|
|
|
|
|
|
|
|
#define ROOTNAME "wyebrun"
|
2018-06-05 09:34:05 +00:00
|
|
|
#define CLIDIR "clients"
|
2018-05-19 15:51:50 +00:00
|
|
|
#define PREFIX WYEBPREFIX
|
2018-06-05 09:34:05 +00:00
|
|
|
#define KEEPS WYEBKEEPSEC
|
2018-05-19 15:51:50 +00:00
|
|
|
#define INPUT "wyebinput"
|
|
|
|
#define PING "wyebping"
|
|
|
|
#define DPINGTIME 1000
|
|
|
|
|
|
|
|
#define P(f, ...) g_print(#f"\n", __VA_ARGS__);
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
static gint64 start;
|
2018-05-20 04:17:11 +00:00
|
|
|
# define D(f, ...) g_print("#"#f"\n", __VA_ARGS__);
|
|
|
|
# define DD(a) g_print("#"#a"\n");
|
2018-05-19 15:51:50 +00:00
|
|
|
#else
|
|
|
|
# define D(...) ;
|
|
|
|
# define DD(a) ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
//to svr
|
|
|
|
CSuntil = 'u',
|
|
|
|
CSdata = 'd',
|
|
|
|
CSping = 'p',
|
|
|
|
|
|
|
|
//to client
|
|
|
|
CCwoke = 'w',
|
|
|
|
CCret = 'r', //retrun data
|
|
|
|
CClost = 'l', //we lost the req
|
|
|
|
} Com;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//shared
|
|
|
|
static void fatal(int i)
|
|
|
|
{
|
|
|
|
P(\n!!! fatal %d !!!\n, i)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
static void mkdirif(char *path)
|
|
|
|
{
|
|
|
|
char *dir = g_path_get_dirname(path);
|
|
|
|
if (!g_file_test(dir, G_FILE_TEST_EXISTS))
|
|
|
|
g_mkdir_with_parents(dir, 0700);
|
|
|
|
|
|
|
|
g_free(dir);
|
|
|
|
}
|
|
|
|
static char *ipcpath(char *exe, char *name)
|
|
|
|
{
|
|
|
|
return g_build_filename(g_get_user_runtime_dir(),
|
|
|
|
ROOTNAME,exe, name, NULL);
|
|
|
|
}
|
|
|
|
static char *preparepp(char *exe, char *name)
|
|
|
|
{
|
|
|
|
char *path = ipcpath(exe, name);
|
|
|
|
if (!g_file_test(path, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
mkdirif(path);
|
|
|
|
mkfifo(path, 0600);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ipcsend(char *exe, char *name,
|
|
|
|
Com type, char *caller, char *data)
|
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
static GMutex m;
|
|
|
|
g_mutex_lock(&m);
|
2018-06-04 20:54:27 +00:00
|
|
|
|
2018-05-19 15:51:50 +00:00
|
|
|
//D(ipcsend exe:%s name:%s, exe, name)
|
|
|
|
char *path = preparepp(exe, name);
|
|
|
|
|
|
|
|
char *esc = g_strescape(data ?: "", "");
|
|
|
|
char *line = g_strdup_printf("%c%s:%s\n", type, caller ?: "", esc);
|
|
|
|
g_free(esc);
|
|
|
|
|
|
|
|
int pp = open(path, O_WRONLY | O_NONBLOCK);
|
|
|
|
|
|
|
|
bool ret = write(pp, line, strlen(line)) != -1;
|
|
|
|
g_free(line);
|
|
|
|
|
|
|
|
close(pp);
|
|
|
|
|
|
|
|
g_free(path);
|
2018-06-04 20:54:27 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_unlock(&m);
|
2018-05-19 15:51:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean ipccb(GIOChannel *ch, GIOCondition c, gpointer p);
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
static GSource *ipcwatch(char *exe, char *name, GMainContext *ctx, gpointer p)
|
|
|
|
{
|
2018-05-19 15:51:50 +00:00
|
|
|
char *path = preparepp(exe, name);
|
|
|
|
|
|
|
|
GIOChannel *io = g_io_channel_new_file(path, "r+", NULL);
|
|
|
|
GSource *watch = g_io_create_watch(io, G_IO_IN);
|
|
|
|
g_io_channel_unref(io);
|
2018-06-05 09:34:05 +00:00
|
|
|
g_source_set_callback(watch, (GSourceFunc)ipccb, p, NULL);
|
2018-05-19 15:51:50 +00:00
|
|
|
g_source_attach(watch, ctx);
|
|
|
|
|
|
|
|
g_free(path);
|
|
|
|
return watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//@server
|
|
|
|
static char *svrexe = NULL;
|
|
|
|
static GMainLoop *sloop = NULL;
|
|
|
|
static wyebdataf dataf = NULL;
|
2018-06-03 08:13:12 +00:00
|
|
|
static GHashTable *orders = NULL;
|
2018-06-04 20:54:27 +00:00
|
|
|
static GMutex ordersm;
|
2018-06-03 08:13:12 +00:00
|
|
|
|
2018-06-04 20:54:27 +00:00
|
|
|
static gboolean quitif(gpointer p)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-04 20:54:27 +00:00
|
|
|
g_mutex_lock(&ordersm);
|
|
|
|
if (!g_hash_table_size(orders))
|
|
|
|
{
|
|
|
|
DD(SVR QUITS\n)
|
|
|
|
g_main_loop_quit(sloop);
|
|
|
|
}
|
|
|
|
g_mutex_unlock(&ordersm);
|
|
|
|
return true;
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
static void until(int sec)
|
|
|
|
{
|
|
|
|
if (!sloop) return;
|
|
|
|
|
|
|
|
static guint last = 0;
|
2018-06-05 09:34:05 +00:00
|
|
|
static GMutex m;
|
|
|
|
g_mutex_lock(&m);
|
2018-05-19 15:51:50 +00:00
|
|
|
if (last)
|
|
|
|
g_source_remove(last);
|
2018-06-04 20:54:27 +00:00
|
|
|
last = g_timeout_add_full(G_PRIORITY_LOW * 2, sec * 1000, quitif, NULL, NULL);
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_unlock(&m);
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
static gpointer pingt(gpointer p)
|
|
|
|
{
|
|
|
|
GMainContext *ctx = g_main_context_new();
|
2018-06-05 09:34:05 +00:00
|
|
|
ipcwatch(svrexe, PING, ctx, NULL);
|
2018-05-19 15:51:50 +00:00
|
|
|
g_main_loop_run(g_main_loop_new(ctx, true));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void wyebwatch(char *exe, char *caller, wyebdataf func)
|
|
|
|
{
|
|
|
|
svrexe = exe;
|
|
|
|
dataf = func;
|
2018-06-04 03:32:27 +00:00
|
|
|
orders = g_hash_table_new(g_str_hash, g_str_equal);
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
until(KEEPS);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
g_thread_unref(g_thread_new("ping", pingt, NULL));
|
|
|
|
ipcwatch(exe, INPUT, g_main_context_default(), NULL);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
if (!ipcsend(CLIDIR, caller, CCwoke, "", NULL))
|
2018-05-19 15:51:50 +00:00
|
|
|
fatal(1);
|
|
|
|
}
|
|
|
|
|
2018-06-04 03:32:27 +00:00
|
|
|
static void monitorcb(GFileMonitor *m, GFile *f, GFile *o, GFileMonitorEvent e,
|
|
|
|
gpointer p)
|
|
|
|
{
|
|
|
|
if (e == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
|
|
|
|
g_timeout_add(100, quitif, NULL);
|
|
|
|
}
|
2018-05-19 15:51:50 +00:00
|
|
|
static gboolean svrinit(char *caller)
|
|
|
|
{
|
|
|
|
wyebwatch(svrexe, caller, dataf);
|
2018-06-03 08:13:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-05-19 15:51:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wyebsvr(int argc, char **argv, wyebdataf func)
|
|
|
|
{
|
|
|
|
if (argc < 2 || !g_str_has_prefix(argv[1], PREFIX)) return false;
|
|
|
|
|
|
|
|
svrexe = argv[0];
|
|
|
|
dataf = func;
|
|
|
|
sloop = g_main_loop_new(NULL, false);
|
|
|
|
g_idle_add((GSourceFunc)svrinit, argv[1]);
|
|
|
|
g_main_loop_run(sloop);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-04 20:54:27 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *caller;
|
2018-06-05 09:34:05 +00:00
|
|
|
char *data;
|
2018-06-04 20:54:27 +00:00
|
|
|
} Dataargs;
|
2018-06-05 09:34:05 +00:00
|
|
|
static void getdata(gpointer p, gpointer ap)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-04 20:54:27 +00:00
|
|
|
Dataargs *args = p;
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-04 20:54:27 +00:00
|
|
|
g_mutex_lock(&ordersm);
|
|
|
|
g_hash_table_add(orders, args->caller);
|
|
|
|
g_mutex_unlock(&ordersm);
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
char *data = dataf(args->data);
|
|
|
|
if (*args->caller && !ipcsend(CLIDIR, args->caller, CCret, "", data)) fatal(2);
|
2018-05-19 15:51:50 +00:00
|
|
|
g_free(data);
|
2018-06-04 20:54:27 +00:00
|
|
|
|
|
|
|
g_mutex_lock(&ordersm);
|
|
|
|
g_hash_table_remove(orders, args->caller);
|
|
|
|
g_mutex_unlock(&ordersm);
|
|
|
|
|
|
|
|
g_free(args->caller);
|
2018-06-05 09:34:05 +00:00
|
|
|
g_free(args->data);
|
2018-06-04 20:54:27 +00:00
|
|
|
g_free(args);
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//@client
|
2018-06-05 09:34:05 +00:00
|
|
|
typedef struct {
|
|
|
|
GMutex retm;
|
|
|
|
GMainContext *wctx;
|
|
|
|
GMainLoop *loop;
|
2018-06-06 04:18:17 +00:00
|
|
|
GSource *watch;
|
2018-06-05 09:34:05 +00:00
|
|
|
char *pid;
|
|
|
|
char *retdata;
|
|
|
|
char *pppath;
|
|
|
|
char *exe; //do not free. this is tmp
|
|
|
|
} Client;
|
2018-06-06 04:18:17 +00:00
|
|
|
|
|
|
|
static GSList *rmpath = NULL;
|
2018-06-06 15:59:42 +00:00
|
|
|
static GMutex rmm;
|
2018-06-06 04:18:17 +00:00
|
|
|
static void __attribute__((destructor)) removepp()
|
|
|
|
{
|
|
|
|
for (; rmpath; rmpath = rmpath->next)
|
|
|
|
remove(rmpath->data);
|
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
static Client *makecli()
|
|
|
|
{
|
|
|
|
Client *cli = g_new0(Client, 1);
|
|
|
|
g_mutex_init(&cli->retm);
|
|
|
|
cli->pid = g_strdup_printf(PREFIX"%d-%d",
|
|
|
|
getpid(), GPOINTER_TO_INT(g_thread_self()));
|
2018-06-06 04:18:17 +00:00
|
|
|
|
|
|
|
cli->wctx = g_main_context_new();
|
|
|
|
cli->loop = g_main_loop_new(cli->wctx, true);
|
|
|
|
cli->watch = ipcwatch(CLIDIR, cli->pid, cli->wctx, cli);
|
|
|
|
cli->pppath = ipcpath( CLIDIR, cli->pid);
|
2018-06-06 15:59:42 +00:00
|
|
|
g_mutex_lock(&rmm);
|
2018-06-06 04:18:17 +00:00
|
|
|
rmpath = g_slist_prepend(rmpath, cli->pppath);
|
2018-06-06 15:59:42 +00:00
|
|
|
g_mutex_unlock(&rmm);
|
2018-06-06 04:18:17 +00:00
|
|
|
|
|
|
|
g_thread_unref(g_thread_new("wait", (GThreadFunc)g_main_loop_run, cli->loop));
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
return cli;
|
|
|
|
}
|
|
|
|
static void freecli(Client *cli)
|
|
|
|
{
|
|
|
|
g_main_loop_quit(cli->loop);
|
2018-06-06 04:18:17 +00:00
|
|
|
g_source_unref(cli->watch);
|
|
|
|
g_main_loop_unref(cli->loop);
|
|
|
|
g_main_context_unref(cli->wctx);
|
|
|
|
g_mutex_clear(&cli->retm);
|
2018-06-05 09:34:05 +00:00
|
|
|
g_free(cli->pid);
|
|
|
|
g_free(cli->retdata);
|
|
|
|
remove(cli->pppath);
|
2018-06-06 15:59:42 +00:00
|
|
|
g_mutex_lock(&rmm);
|
2018-06-06 04:18:17 +00:00
|
|
|
rmpath = g_slist_remove(rmpath, cli->pppath);
|
2018-06-06 15:59:42 +00:00
|
|
|
g_mutex_unlock(&rmm);
|
2018-06-05 09:34:05 +00:00
|
|
|
g_free(cli->pppath);
|
|
|
|
}
|
|
|
|
static Client *getcli()
|
|
|
|
{
|
|
|
|
static GMutex m;
|
|
|
|
g_mutex_lock(&m);
|
|
|
|
static GPrivate pc = G_PRIVATE_INIT((GDestroyNotify)freecli);
|
|
|
|
Client *cli = g_private_get(&pc);
|
|
|
|
if (!cli)
|
|
|
|
{
|
|
|
|
cli = makecli();
|
|
|
|
g_private_set(&pc, cli);
|
|
|
|
}
|
|
|
|
g_mutex_unlock(&m);
|
|
|
|
return cli;
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
static GHashTable *lastsec = NULL;
|
|
|
|
static GMutex lastm;
|
|
|
|
static void setsec(char *exe, int sec)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_lock(&lastm);
|
|
|
|
if (!lastsec) lastsec = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
|
|
g_hash_table_replace(lastsec, g_strdup(exe), g_strdup_printf("%d", sec));
|
|
|
|
g_mutex_unlock(&lastm);
|
|
|
|
}
|
|
|
|
static char *keepstr(char *exe)
|
|
|
|
{
|
|
|
|
g_mutex_lock(&lastm);
|
|
|
|
char *ret = g_hash_table_lookup(lastsec, exe);
|
|
|
|
g_mutex_unlock(&lastm);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
#define Z(v) #v
|
|
|
|
return ret ?: Z(KEEPS);
|
|
|
|
#undef Z
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
|
|
|
|
static gboolean pingloop(Client *cli)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
if (!ipcsend(cli->exe, PING, CSping, cli->pid, keepstr(cli->exe)))
|
|
|
|
g_mutex_unlock(&cli->retm);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
static gboolean timeoutcb(Client *cli)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_unlock(&cli->retm);
|
2018-05-19 15:51:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//don't free
|
2018-06-05 09:34:05 +00:00
|
|
|
static char *request(char *exe, Com type, bool caller, char *data)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
Client *cli = getcli();
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
if (caller)
|
2018-06-05 09:34:05 +00:00
|
|
|
{
|
|
|
|
g_mutex_lock(&cli->retm);
|
|
|
|
g_free(cli->retdata);
|
|
|
|
cli->retdata = NULL;
|
|
|
|
}
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
if (!ipcsend(exe, INPUT, type, caller ? cli->pid : NULL, data))
|
2018-05-19 15:51:50 +00:00
|
|
|
{ //svr is not running
|
|
|
|
char *path = ipcpath(exe, "lock");
|
|
|
|
if (!g_file_test(path, G_FILE_TEST_EXISTS))
|
|
|
|
mkdirif(path);
|
|
|
|
|
|
|
|
int lock = open(path, O_RDONLY | O_CREAT, S_IRUSR);
|
|
|
|
g_free(path);
|
2018-05-29 16:00:21 +00:00
|
|
|
flock(lock, LOCK_EX);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
//retry in single proc
|
2018-06-05 09:34:05 +00:00
|
|
|
if (!ipcsend(exe, INPUT, type, caller ? cli->pid : NULL, data))
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
|
|
|
if (!caller)
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_lock(&cli->retm);
|
|
|
|
|
|
|
|
GSource *tout = g_timeout_source_new(KEEPS * 1000);
|
|
|
|
g_source_set_callback(tout, (GSourceFunc)timeoutcb, cli, NULL);
|
|
|
|
g_source_attach(tout, cli->wctx);
|
|
|
|
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
char **argv = g_new0(char*, 3);
|
|
|
|
argv[0] = exe;
|
|
|
|
argv[1] = cli->pid;
|
|
|
|
GError *err = NULL;
|
|
|
|
if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
|
|
|
|
NULL, NULL, NULL, &err))
|
|
|
|
{
|
|
|
|
g_print("err %s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
|
|
|
g_free(argv);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
|
|
|
|
g_mutex_lock(&cli->retm);
|
|
|
|
g_mutex_unlock(&cli->retm);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
g_source_destroy(tout);
|
|
|
|
g_source_unref(tout);
|
|
|
|
|
|
|
|
//wyebloop doesn't know svr quits
|
2018-06-05 09:34:05 +00:00
|
|
|
wyebkeep(exe, 0);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
if (caller)
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_lock(&cli->retm);
|
|
|
|
if (!ipcsend(exe, INPUT, type, caller ? cli->pid : NULL, data))
|
2018-06-07 05:29:52 +00:00
|
|
|
P(Spawning %s failed !!, exe)
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
close(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (caller)
|
|
|
|
{
|
|
|
|
GSource *ping = g_timeout_source_new(DPINGTIME);
|
2018-06-05 09:34:05 +00:00
|
|
|
cli->exe = exe;
|
|
|
|
g_source_set_callback(ping, (GSourceFunc)pingloop, cli, NULL);
|
|
|
|
g_source_attach(ping, cli->wctx); //attach to ping thread
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_lock(&cli->retm);
|
|
|
|
g_mutex_unlock(&cli->retm);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
g_source_destroy(ping);
|
|
|
|
g_source_unref(ping);
|
|
|
|
}
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
return cli->retdata;
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
char *wyebget(char *exe, char *data)
|
2018-05-20 01:19:51 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
return request(exe, CSdata, true, data);
|
2018-05-20 01:19:51 +00:00
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
void wyebsend(char *exe, char *data)
|
2018-05-22 05:24:54 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
request(exe, CSdata, false, data);
|
2018-05-22 05:24:54 +00:00
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
static gboolean keepcb(char *exe)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
request(exe, CSuntil, false, keepstr(exe));
|
2018-05-22 05:24:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
void wyebkeep(char *exe, int sec)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
if (sec) setsec(exe, sec);
|
|
|
|
g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc)keepcb,
|
|
|
|
g_strdup(exe), g_free);
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
2018-05-22 05:24:54 +00:00
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
static gboolean loopcb(char *exe)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
keepcb(exe);
|
2018-05-19 15:51:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
guint wyebloop(char *exe, int sec)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
if (sec) setsec(exe, sec);
|
|
|
|
loopcb(exe);
|
|
|
|
return g_timeout_add_full(G_PRIORITY_DEFAULT, sec * 300,
|
|
|
|
(GSourceFunc)loopcb, g_strdup(exe), g_free);
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
static void testget(gpointer p, gpointer ap)
|
|
|
|
{
|
2018-06-06 15:59:42 +00:00
|
|
|
D(ret %s - %s, wyebget(ap, p), (char *)p)
|
2018-06-05 09:34:05 +00:00
|
|
|
g_free(p);
|
|
|
|
}
|
|
|
|
#endif
|
2018-05-19 15:51:50 +00:00
|
|
|
static gboolean tcinputcb(GIOChannel *ch, GIOCondition c, char *exe)
|
|
|
|
{
|
|
|
|
char *line;
|
2018-05-29 06:06:35 +00:00
|
|
|
if (c == G_IO_HUP || G_IO_STATUS_EOF ==
|
|
|
|
g_io_channel_read_line(ch, &line, NULL, NULL, NULL))
|
2018-05-29 05:57:19 +00:00
|
|
|
exit(0);
|
|
|
|
|
2018-05-19 15:51:50 +00:00
|
|
|
if (!line) return true;
|
|
|
|
|
|
|
|
g_strstrip(line);
|
|
|
|
if (!strlen(line))
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
#if DEBUG
|
2018-05-22 03:02:56 +00:00
|
|
|
if (g_str_has_prefix(line, "l"))
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
2018-06-06 15:59:42 +00:00
|
|
|
GThreadPool *pool = g_thread_pool_new(testget, exe, 44, false, NULL);
|
2018-06-05 09:34:05 +00:00
|
|
|
|
2018-05-19 15:51:50 +00:00
|
|
|
start = g_get_monotonic_time();
|
2018-06-06 15:59:42 +00:00
|
|
|
for (int i = 0; i < 100000; i++)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
|
|
|
char *is = g_strdup_printf("l%d", i);
|
2018-06-05 09:34:05 +00:00
|
|
|
//g_print("loop %d ret %s\n", i, wyebget(exe, is));
|
2018-05-22 03:02:56 +00:00
|
|
|
if (*(line + 1) == 's')
|
|
|
|
wyebsend(exe, is);
|
|
|
|
else if (*(line + 1) == 'p')
|
|
|
|
D(pint %s, is)
|
|
|
|
else
|
2018-06-05 09:34:05 +00:00
|
|
|
g_thread_pool_push(pool, g_strdup(is), NULL);
|
|
|
|
// wyebget(exe, is);
|
2018-05-19 15:51:50 +00:00
|
|
|
g_free(is);
|
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
g_thread_pool_free(pool, false, true);
|
|
|
|
|
2018-05-19 15:51:50 +00:00
|
|
|
gint64 now = g_get_monotonic_time();
|
2018-06-05 09:34:05 +00:00
|
|
|
D(time %f, (now - start) / 1000000.0)
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
else
|
2018-06-05 09:34:05 +00:00
|
|
|
g_print("RET is %s\n", wyebget(exe, line));
|
2018-05-19 15:51:50 +00:00
|
|
|
#else
|
2018-06-05 09:34:05 +00:00
|
|
|
g_print("%s\n", wyebget(exe, line)); //don't free
|
2018-05-19 15:51:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
g_free(line);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static gboolean tcinit(char *exe)
|
|
|
|
{
|
2018-06-04 20:54:27 +00:00
|
|
|
#if DEBUG
|
2018-06-05 09:34:05 +00:00
|
|
|
wyebloop(exe, 300);
|
2018-06-04 20:54:27 +00:00
|
|
|
#else
|
2018-06-05 09:34:05 +00:00
|
|
|
wyebloop(exe, 2);
|
2018-06-04 20:54:27 +00:00
|
|
|
#endif
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
GIOChannel *io = g_io_channel_unix_new(fileno(stdin));
|
2018-05-29 06:06:35 +00:00
|
|
|
g_io_add_watch(io, G_IO_IN | G_IO_HUP, (GIOFunc)tcinputcb, exe);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void wyebclient(char *exe)
|
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
GMainLoop *loop = g_main_loop_new(NULL, false);
|
2018-05-19 15:51:50 +00:00
|
|
|
g_idle_add((GSourceFunc)tcinit, exe);
|
2018-06-05 09:34:05 +00:00
|
|
|
g_main_loop_run(loop);
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//@ipccb
|
|
|
|
gboolean ipccb(GIOChannel *ch, GIOCondition c, gpointer p)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
g_io_channel_read_line(ch, &line, NULL, NULL, NULL);
|
|
|
|
if (!line) return true;
|
|
|
|
|
2018-06-06 04:18:17 +00:00
|
|
|
Com type = *line;
|
|
|
|
char *id = line + 1;
|
|
|
|
char *data = strchr(line, ':');
|
2018-06-05 09:34:05 +00:00
|
|
|
*data++ = '\0';
|
2018-06-06 04:18:17 +00:00
|
|
|
g_strchomp(data);
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
#if DEBUG
|
2018-06-05 09:34:05 +00:00
|
|
|
// static int i = 0;
|
|
|
|
// D(%c ipccb%d %c/%s/%s;, svrexe ? 'S':'C', i++, type ,id ,data)
|
2018-05-19 15:51:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
//server
|
|
|
|
case CSdata:
|
2018-06-04 20:54:27 +00:00
|
|
|
{
|
|
|
|
Dataargs *args = g_new(Dataargs, 1);
|
|
|
|
args->caller = g_strdup(id);
|
2018-06-06 04:18:17 +00:00
|
|
|
args->data = g_strcompress(data);
|
2018-06-05 09:34:05 +00:00
|
|
|
|
|
|
|
static GThreadPool *pool = NULL;
|
|
|
|
if (!pool) pool = g_thread_pool_new(getdata, NULL, -1, false, NULL);
|
|
|
|
g_thread_pool_push(pool, args, NULL);
|
2018-05-19 15:51:50 +00:00
|
|
|
break;
|
2018-06-04 20:54:27 +00:00
|
|
|
}
|
2018-05-19 15:51:50 +00:00
|
|
|
case CSping:
|
2018-06-04 20:54:27 +00:00
|
|
|
g_mutex_lock(&ordersm);
|
2018-05-19 15:51:50 +00:00
|
|
|
if (!g_hash_table_lookup(orders, id))
|
2018-06-05 09:34:05 +00:00
|
|
|
ipcsend(CLIDIR, id, CClost, NULL, NULL);
|
2018-06-04 20:54:27 +00:00
|
|
|
g_mutex_unlock(&ordersm);
|
2018-06-05 09:34:05 +00:00
|
|
|
case CSuntil:
|
|
|
|
until(atoi(data));
|
2018-05-19 15:51:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
//client
|
|
|
|
case CCret:
|
|
|
|
case CClost:
|
|
|
|
case CCwoke:
|
2018-06-05 09:34:05 +00:00
|
|
|
{
|
|
|
|
Client *cli = p;
|
|
|
|
if (type == CCret)
|
2018-06-06 04:18:17 +00:00
|
|
|
cli->retdata = g_strcompress(data);
|
2018-06-05 09:34:05 +00:00
|
|
|
|
2018-06-04 20:54:27 +00:00
|
|
|
//for the case pinging at same time of ret
|
2018-06-05 09:34:05 +00:00
|
|
|
g_mutex_trylock(&cli->retm);
|
|
|
|
g_mutex_unlock(&cli->retm);
|
2018-05-19 15:51:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-06-05 09:34:05 +00:00
|
|
|
}
|
2018-05-19 15:51:50 +00:00
|
|
|
|
2018-06-06 04:18:17 +00:00
|
|
|
g_free(line);
|
2018-05-19 15:51:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//test
|
|
|
|
#if DEBUG
|
2018-06-05 09:34:05 +00:00
|
|
|
static char *testdata(char *data)
|
2018-05-19 15:51:50 +00:00
|
|
|
{
|
|
|
|
//sleep(9);
|
2018-06-05 09:34:05 +00:00
|
|
|
//g_free(data); //makes crash
|
2018-05-19 15:51:50 +00:00
|
|
|
|
|
|
|
static int i = 0;
|
2018-06-05 09:34:05 +00:00
|
|
|
return g_strdup_printf("%d th test data. req is %s", ++i, data);
|
2018-05-19 15:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
start = g_get_monotonic_time();
|
|
|
|
// gint64 now = g_get_monotonic_time();
|
|
|
|
// D(time %ld %ld, now - start, now)
|
|
|
|
|
|
|
|
if (!wyebsvr(argc, argv, testdata))
|
|
|
|
wyebclient(argv[0]);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif
|