nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1diff --git a/src/proto_unix.c b/src/proto_unix.c
2index 998f234..d38452c 100644
3--- a/src/proto_unix.c
4+++ b/src/proto_unix.c
5@@ -36,11 +36,14 @@ enum {
6
7 static int lsock = -1;
8
9+static char *spath = NULL;
10+
11 int init_unix(void)
12 {
13 int s;
14 mode_t prev_umask;
15 struct sockaddr_un addr;
16+ char *sock_path;
17
18 if(lsock >= 0) return 0;
19
20@@ -49,16 +52,18 @@ int init_unix(void)
21 return -1;
22 }
23
24- unlink(SOCK_NAME); /* in case it already exists */
25+ sock_path = socket_path();
26+
27+ unlink(sock_path); /* in case it already exists */
28
29 memset(&addr, 0, sizeof addr);
30 addr.sun_family = AF_UNIX;
31- strcpy(addr.sun_path, SOCK_NAME);
32+ strcpy(addr.sun_path, sock_path);
33
34 prev_umask = umask(0);
35
36 if(bind(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
37- logmsg(LOG_ERR, "failed to bind unix socket: %s: %s\n", SOCK_NAME, strerror(errno));
38+ logmsg(LOG_ERR, "failed to bind unix socket: %s: %s\n", sock_path, strerror(errno));
39 close(s);
40 return -1;
41 }
42@@ -68,7 +73,7 @@ int init_unix(void)
43 if(listen(s, 8) == -1) {
44 logmsg(LOG_ERR, "listen failed: %s\n", strerror(errno));
45 close(s);
46- unlink(SOCK_NAME);
47+ unlink(sock_path);
48 return -1;
49 }
50
51@@ -82,7 +87,7 @@ void close_unix(void)
52 close(lsock);
53 lsock = -1;
54
55- unlink(SOCK_NAME);
56+ unlink(socket_path());
57 }
58 }
59
60@@ -173,3 +178,19 @@ int handle_uevents(fd_set *rset)
61
62 return 0;
63 }
64+
65+char *socket_path(void)
66+{
67+ char *xdg_runtime_dir;
68+ if((xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"))) {
69+ if ( spath == NULL ) {
70+ spath = malloc(strlen(xdg_runtime_dir) + strlen("/spnav.sock") + 1);
71+ if ( spath != NULL ) {
72+ sprintf(spath, "%s/spnav.sock", xdg_runtime_dir);
73+ }
74+ };
75+ return spath;
76+ } else {
77+ return DEFAULT_SOCK_NAME;
78+ }
79+}
80diff --git a/src/proto_unix.h b/src/proto_unix.h
81index 045b379..ec4509c 100644
82--- a/src/proto_unix.h
83+++ b/src/proto_unix.h
84@@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
85 #include "event.h"
86 #include "client.h"
87
88+char *socket_path(void);
89 int init_unix(void);
90 void close_unix(void);
91 int get_unix_socket(void);
92diff --git a/src/spnavd.c b/src/spnavd.c
93index cbea191..03080da 100644
94--- a/src/spnavd.c
95+++ b/src/spnavd.c
96@@ -344,7 +344,7 @@ static int find_running_daemon(void)
97 }
98 memset(&addr, 0, sizeof addr);
99 addr.sun_family = AF_UNIX;
100- strncpy(addr.sun_path, SOCK_NAME, sizeof addr.sun_path);
101+ strncpy(addr.sun_path, socket_path(), sizeof addr.sun_path);
102
103 if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
104 close(s);
105diff --git a/src/spnavd.h b/src/spnavd.h
106index fa0a916..2d1c48b 100644
107--- a/src/spnavd.h
108+++ b/src/spnavd.h
109@@ -26,8 +26,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
110 #define DEF_CFGFILE "/etc/spnavrc"
111 #define DEF_LOGFILE "/var/log/spnavd.log"
112
113-#define SOCK_NAME "/var/run/spnav.sock"
114 #define PIDFILE "/var/run/spnavd.pid"
115+#define DEFAULT_SOCK_NAME "/run/spnav.sock"
116 #define SYSLOG_ID "spnavd"
117
118 /* Multiple devices support */