allow AF_UNIX socket path to be overriden by an environment variable, or

by a configuration file option, falls back silently to the default if
neither is present or connection fails.
pull/13/head
John Tsiombikas 2022-02-06 14:46:47 +02:00
rodzic 55d975c624
commit 0d33c4f7ac
3 zmienionych plików z 45 dodań i 10 usunięć

Wyświetl plik

@ -18,7 +18,7 @@ ifeq ($(shell uname -s), Darwin)
shared = -dynamiclib shared = -dynamiclib
else else
so_major = 0 so_major = 0
so_minor = 1 so_minor = 2
devlink = lib$(name).so devlink = lib$(name).so
soname = $(devlink).$(so_major) soname = $(devlink).$(so_major)
lib_so = $(soname).$(so_minor) lib_so = $(soname).$(so_minor)
@ -54,8 +54,9 @@ install: $(lib_a) $(lib_so) $(hdr)
cp $(lib_so) $(DESTDIR)$(PREFIX)/$(libdir)/$(lib_so) cp $(lib_so) $(DESTDIR)$(PREFIX)/$(libdir)/$(lib_so)
[ -n "$(soname)" ] && \ [ -n "$(soname)" ] && \
rm -f $(DESTDIR)$(PREFIX)/$(libdir)/$(soname) $(DESTDIR)$(PREFIX)/$(libdir)/$(devlink) && \ rm -f $(DESTDIR)$(PREFIX)/$(libdir)/$(soname) $(DESTDIR)$(PREFIX)/$(libdir)/$(devlink) && \
ln -s $(DESTDIR)$(PREFIX)/$(libdir)/$(lib_so) $(DESTDIR)$(PREFIX)/$(libdir)/$(soname) && \ cd $(DESTDIR)$(PREFIX)/$(libdir) && \
ln -s $(DESTDIR)$(PREFIX)/$(libdir)/$(soname) $(DESTDIR)$(PREFIX)/$(libdir)/$(devlink) || \ ln -s $(lib_so) $(soname) && \
ln -s $(soname) $(devlink) || \
true true
for h in $(hdr); do cp -p $(srcdir)/$$h $(DESTDIR)$(PREFIX)/include/; done for h in $(hdr); do cp -p $(srcdir)/$$h $(DESTDIR)$(PREFIX)/include/; done

46
spnav.c
Wyświetl plik

@ -1,6 +1,6 @@
/* /*
This file is part of libspnav, part of the spacenav project (spacenav.sf.net) This file is part of libspnav, part of the spacenav project (spacenav.sf.net)
Copyright (C) 2007-2020 John Tsiombikas <nuclear@member.fsf.org> Copyright (C) 2007-2022 John Tsiombikas <nuclear@member.fsf.org>
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
@ -27,6 +27,7 @@ OF SUCH DAMAGE.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
@ -70,11 +71,22 @@ static struct event_node *ev_queue, *ev_queue_tail;
/* AF_UNIX socket used for alternative communication with daemon */ /* AF_UNIX socket used for alternative communication with daemon */
static int sock = -1; static int sock = -1;
static int connect_afunix(int s, const char *path)
{
struct sockaddr_un addr = {0};
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof addr.sun_path - 1);
return connect(s, (struct sockaddr*)&addr, sizeof addr);
}
int spnav_open(void) int spnav_open(void)
{ {
int s; int s;
struct sockaddr_un addr; char *path;
FILE *fp;
char buf[256], *ptr;
if(IS_OPEN) { if(IS_OPEN) {
return -1; return -1;
@ -90,16 +102,38 @@ int spnav_open(void)
return -1; return -1;
} }
memset(&addr, 0, sizeof addr); /* heed SPNAV_SOCKET environment variable if it's defined */
addr.sun_family = AF_UNIX; if((path = getenv("SPNAV_SOCKET"))) {
strncpy(addr.sun_path, SPNAV_SOCK_PATH, sizeof(addr.sun_path)); if(connect_afunix(s, path) == 0) goto success;
}
/* hacky config file parser, to look for socket = <path> in /etc/spnavrc */
if((fp = fopen("/etc/spnavrc", "rb"))) {
path = 0;
while(fgets(buf, sizeof buf, fp)) {
ptr = buf;
while(*ptr && isspace(*ptr)) ptr++;
if(!*ptr || *ptr == '#') continue; /* comment or empty line */
if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) { if(memcmp(ptr, "socket", 6) == 0 && (ptr = strchr(ptr, '='))) {
while(*++ptr && isspace(*ptr));
if(!*ptr) continue;
path = ptr;
ptr += strlen(ptr) - 1;
while(ptr > path && isspace(*ptr)) *ptr-- = 0;
break;
}
}
if(path && connect_afunix(s, path) == 0) goto success;
}
/* by default use SPNAV_SOCK_PATH (see top of this file) */
if(connect_afunix(s, SPNAV_SOCK_PATH) == -1) {
close(s); close(s);
return -1; return -1;
} }
success:
sock = s; sock = s;
return 0; return 0;
} }

Wyświetl plik

@ -1,6 +1,6 @@
/* /*
This file is part of libspnav, part of the spacenav project (spacenav.sf.net) This file is part of libspnav, part of the spacenav project (spacenav.sf.net)
Copyright (C) 2007-2010 John Tsiombikas <nuclear@member.fsf.org> Copyright (C) 2007-2022 John Tsiombikas <nuclear@member.fsf.org>
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met: