srv, ibus: remove the endpoints on a signal too
A stopped daemon left its socket and IBus address file behind, since plan9port ends a process on SIGTERM without running exit handlers. Both files now go on a note as well as on exit, and only while they are still the ones this daemon made, so a successor is never robbed of its own.
This commit is contained in:
39
srv.c
39
srv.c
@@ -5,8 +5,32 @@
|
||||
#include <sys/un.h>
|
||||
|
||||
static char adir[256];
|
||||
static char sockpath[sizeof(((struct sockaddr_un*)0)->sun_path)];
|
||||
static dev_t sockdev;
|
||||
static ino_t sockino;
|
||||
static Channel *clientc;
|
||||
|
||||
/* Removes the socket only while it is still the one we announced. */
|
||||
static void
|
||||
srvunlink(void)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if(sockpath[0] != '\0' && lstat(sockpath, &st) == 0 &&
|
||||
st.st_dev == sockdev && st.st_ino == sockino)
|
||||
unlink(sockpath);
|
||||
sockpath[0] = '\0';
|
||||
}
|
||||
|
||||
static int
|
||||
srvnote(void *v, char *note)
|
||||
{
|
||||
USED(v);
|
||||
USED(note);
|
||||
srvunlink();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
srvreadreq(int fd, Keyreq *kr, int *want)
|
||||
{
|
||||
@@ -90,18 +114,23 @@ clientthread(void *arg)
|
||||
void
|
||||
srvinit(void)
|
||||
{
|
||||
char *addr, path[sizeof(((struct sockaddr_un*)0)->sun_path)];
|
||||
struct stat st;
|
||||
char *addr;
|
||||
|
||||
if(ipcpath(path, sizeof path) < 0)
|
||||
if(ipcpath(sockpath, sizeof sockpath) < 0)
|
||||
die("IPC path is too long");
|
||||
addr = smprint("unix!%s", path);
|
||||
addr = smprint("unix!%s", sockpath);
|
||||
if(addr == nil)
|
||||
die("out of memory");
|
||||
if(announce(addr, adir) < 0)
|
||||
die("IPC endpoint is already in use: %r");
|
||||
free(addr);
|
||||
if(chmod(path, 0600) < 0)
|
||||
die("can't protect IPC endpoint: %s", path);
|
||||
if(chmod(sockpath, 0600) < 0 || lstat(sockpath, &st) < 0)
|
||||
die("can't protect IPC endpoint: %s", sockpath);
|
||||
sockdev = st.st_dev;
|
||||
sockino = st.st_ino;
|
||||
atexit(srvunlink);
|
||||
threadnotify(srvnote, 1);
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user