harden ipc and frontend recovery
This commit is contained in:
59
srv.c
59
srv.c
@@ -2,30 +2,53 @@
|
||||
#include "fn.h"
|
||||
|
||||
static char adir[40];
|
||||
static Channel *clientc;
|
||||
|
||||
static void
|
||||
sendkey(int fd, u32int ks, u32int mod, int want)
|
||||
int
|
||||
srvreadkey(int fd, Keyreq *kr)
|
||||
{
|
||||
Keyreq kr;
|
||||
uchar req[Ipcreqsz];
|
||||
u32int ks, mod;
|
||||
int want;
|
||||
|
||||
kr.fd = fd;
|
||||
kr.ks = ks;
|
||||
kr.mod = mod;
|
||||
kr.want = want;
|
||||
chansend(keyc, &kr);
|
||||
if(ipcreadn(fd, req, sizeof req) < 0)
|
||||
return -1;
|
||||
ipcunpackreq(req, &want, &mod, &ks);
|
||||
kr->ks = ks;
|
||||
kr->mod = mod;
|
||||
kr->want = want;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
clientthread(void *arg)
|
||||
{
|
||||
Channel *reply;
|
||||
int fd;
|
||||
uchar req[4];
|
||||
Keyreq kr;
|
||||
Keyres res;
|
||||
uchar out[Ipcmaxresp];
|
||||
char commit[Maxutf], preedit[Maxutf];
|
||||
int n, ncommit, npreedit;
|
||||
uchar token;
|
||||
|
||||
fd = (int)(uintptr)arg;
|
||||
threadsetname("client %d", fd);
|
||||
while(read(fd, req, 4) == 4)
|
||||
sendkey(fd, req[2] | (req[3] << 8), req[1], req[0]);
|
||||
reply = chancreate(sizeof(Keyres), 0);
|
||||
kr.reply = reply;
|
||||
while(srvreadkey(fd, &kr) == 0){
|
||||
chansend(keyc, &kr);
|
||||
chanrecv(reply, &res);
|
||||
ncommit = stoutf(&res.commit, commit, sizeof commit);
|
||||
npreedit = stoutf(&res.preedit, preedit, sizeof preedit);
|
||||
n = ipcpackresp(out, sizeof out, res.eaten,
|
||||
commit, ncommit, preedit, npreedit, kr.want);
|
||||
if(n < 0 || ipcsend(fd, out, n) < 0)
|
||||
break;
|
||||
}
|
||||
chanfree(reply);
|
||||
close(fd);
|
||||
chanrecv(clientc, &token);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -33,7 +56,7 @@ srvinit(void)
|
||||
{
|
||||
char addr[64];
|
||||
|
||||
snprint(addr, sizeof(addr), "unix!/tmp/strans.%d", getuid());
|
||||
snprint(addr, sizeof(addr), "unix!" IPCPATH, getuid());
|
||||
remove(addr + 5);
|
||||
if(announce(addr, adir) < 0)
|
||||
die("announce: %r");
|
||||
@@ -44,13 +67,23 @@ srvthread(void*)
|
||||
{
|
||||
char ldir[40];
|
||||
int fd;
|
||||
uchar token;
|
||||
|
||||
threadsetname("srv");
|
||||
srvinit();
|
||||
token = 0;
|
||||
clientc = chancreate(sizeof token, Maxclients);
|
||||
for(;;){
|
||||
fd = listen(adir, ldir);
|
||||
if(fd < 0)
|
||||
continue;
|
||||
proccreate(clientthread, (void*)(uintptr)fd, 8192);
|
||||
if(channbsend(clientc, &token) <= 0){
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
if(proccreate(clientthread, (void*)(uintptr)fd, 8192) < 0){
|
||||
chanrecv(clientc, &token);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user