harden ipc and frontend recovery

This commit is contained in:
2026-08-11 19:34:47 +09:00
parent 4bfb659b6d
commit 9dc116a035
14 changed files with 495 additions and 245 deletions

62
ibus.c
View File

@@ -8,6 +8,7 @@
#include <sys/stat.h>
#include <poll.h>
#include <dbus/dbus.h>
#include <xkbcommon/xkbcommon.h>
enum
{
@@ -30,6 +31,7 @@ static DBusServer *srv;
static char addrfile[256];
static int icctr;
static int busctr;
static Channel *replyc;
static DBusHandlerResult onmsg(DBusConnection*, DBusMessage*, void*);
@@ -182,9 +184,14 @@ togglewatch(DBusWatch *w, void *_)
static u32int
kget(u32int sym)
{
u32int c;
c = xkb_keysym_to_utf32(sym);
if(c >= ' ' && c != 0x7f)
return c;
if(sym >= 0xff00 && sym <= 0xffff)
return Kspec + (sym - 0xff00);
return sym;
return c;
}
static u32int
@@ -201,53 +208,24 @@ mget(u32int state)
}
static void
sendkey(u32int ks, u32int mod, char *com, int csz, char *pre, int psz,
int *eaten)
sendkey(u32int ks, u32int mod, Keyres *res)
{
Keyreq kr;
int p[2];
uchar hdr[2];
uchar pl;
int n;
*eaten = 0;
com[0] = '\0';
pre[0] = '\0';
if(pipe(p) < 0)
return;
kr.fd = p[1];
kr.ks = ks;
kr.mod = mod;
kr.want = 1;
kr.reply = replyc;
chansend(keyc, &kr);
if(read(p[0], hdr, 2) != 2)
goto out;
*eaten = hdr[0];
n = hdr[1];
if(n > 0 && n < csz){
if(read(p[0], com, n) != n)
goto out;
com[n] = '\0';
}
if(read(p[0], &pl, 1) != 1)
goto out;
if(pl > 0 && pl < psz){
if(read(p[0], pre, pl) != pl)
goto out;
pre[pl] = '\0';
}
out:
close(p[0]);
close(p[1]);
chanrecv(replyc, res);
}
static void
sendreset(void)
{
char com[64], pre[256];
int eaten;
Keyres res;
sendkey(Kesc, 0, com, sizeof(com), pre, sizeof(pre), &eaten);
sendkey(Kesc, 0, &res);
}
static void
@@ -305,7 +283,7 @@ emitpreedit(DBusConnection *c, const char *path, const char *text)
return;
dbus_message_iter_init_append(sig, &it);
appendibustext(&it, text);
cursor = utflen(text);
cursor = utflen((char*)text);
visible = text[0] != '\0' ? TRUE : FALSE;
dbus_message_iter_append_basic(&it, DBUS_TYPE_UINT32, &cursor);
dbus_message_iter_append_basic(&it, DBUS_TYPE_BOOLEAN, &visible);
@@ -353,8 +331,8 @@ handlekey(DBusConnection *c, DBusMessage *m)
DBusMessage *r;
DBusError err;
dbus_uint32_t sym, code, state;
char commit[256], preedit[256];
int eaten;
Keyres res;
char commit[Maxutf], preedit[Maxutf];
dbus_bool_t b;
u32int ks, mod;
@@ -378,13 +356,14 @@ handlekey(DBusConnection *c, DBusMessage *m)
}
ks = kget(sym);
mod = mget(state);
sendkey(ks, mod, commit, sizeof(commit), preedit, sizeof(preedit),
&eaten);
sendkey(ks, mod, &res);
stoutf(&res.commit, commit, sizeof commit);
stoutf(&res.preedit, preedit, sizeof preedit);
if(commit[0] != '\0')
emitcommit(c, dbus_message_get_path(m), commit);
emitpreedit(c, dbus_message_get_path(m), preedit);
r = dbus_message_new_method_return(m);
b = eaten ? TRUE : FALSE;
b = res.eaten ? TRUE : FALSE;
dbus_message_append_args(r, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
dbus_connection_send(c, r, nil);
dbus_message_unref(r);
@@ -581,6 +560,7 @@ ibusthread(void *_)
threadsetname("ibus");
if(ibusinit() < 0)
return;
replyc = chancreate(sizeof(Keyres), 0);
for(;;){
n = 0;
for(i = 0; i < nwatches && n < Maxwatches; i++){