triggerhappy icon indicating copy to clipboard operation
triggerhappy copied to clipboard

systemd socket type for triggerhappy should be datagram

Open holdenc opened this issue 7 years ago • 1 comments

Hi,

I'm using triggerhappy on a RaspberryPi 3 and the daemon starts fine:

holden@caledonian:~/src/triggerhappy $ systemctl status triggerhappy.service 
● triggerhappy.service - triggerhappy global hotkey daemon
   Loaded: loaded (/lib/systemd/system/triggerhappy.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-08-06 22:12:59 BST; 23h ago
 Main PID: 312 (thd)
   CGroup: /system.slice/triggerhappy.service
           └─312 /usr/sbin/thd --triggers /etc/triggerhappy/triggers.d/ --socket /run/thd.socket --user nobody --deviceglob /dev/input/event*

Aug 06 22:12:59 caledonian systemd[1]: Starting triggerhappy global hotkey daemon...
Aug 06 22:12:59 caledonian thd[312]: Found socket passed from systemd
Aug 06 22:12:59 caledonian systemd[1]: Started triggerhappy global hotkey daemon.

but I get the following error in syslog at startup:

Aug  6 22:12:59 caledonian systemd-udevd[144]: Process '/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev' failed with exit code 1.

I had a quick look at the source code and I can see triggerhappy uses only datagram sockets:

holden@caledonian:~/src/triggerhappy $ grep SOCK_ *.c
cmdsocket.c:	cmd_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
cmdsocket.c:	fd = socket(AF_UNIX, SOCK_DGRAM, 0);

but the .socket file for systemd configures a stream socket and not a datagram socket:

holden@caledonian:~/src/triggerhappy $ cat /lib/systemd/system/triggerhappy.socket
[Socket]
ListenStream=/run/thd.socket

[Install]
WantedBy=sockets.target

holden@caledonian:~/src/triggerhappy $ systemctl status triggerhappy.socket 
● triggerhappy.socket
   Loaded: loaded (/lib/systemd/system/triggerhappy.socket; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-08-06 22:12:59 BST; 23h ago
   Listen: /run/thd.socket (Stream)

Aug 06 22:12:59 caledonian systemd[1]: Listening on triggerhappy.socket.

The following patch should fix the issue:

diff --git a/systemd/triggerhappy.socket b/systemd/triggerhappy.socket
index 30a2c69..da37594 100644
--- a/systemd/triggerhappy.socket
+++ b/systemd/triggerhappy.socket
@@ -1,5 +1,5 @@
 [Socket]
-ListenStream=/run/thd.socket
+ListenDatagram=/run/thd.socket
 
 [Install]
 WantedBy=sockets.target

Also, the last argument of bind() and connect() calls is probably wrong and should be changed:

diff --git a/cmdsocket.c b/cmdsocket.c
index 1ba3af5..9f97dba 100644
--- a/cmdsocket.c
+++ b/cmdsocket.c
@@ -44,8 +44,7 @@ int bind_cmdsocket( char *name ) {
        cmd_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
        strcpy(addr.sun_path, name);
        addr.sun_family = AF_UNIX;
-       bind (cmd_fd, (struct sockaddr *) &addr, 
-               strlen(addr.sun_path) + sizeof (addr.sun_family));
+       bind (cmd_fd, (struct sockaddr *) &addr, sizeof(addr));
        return cmd_fd;
 }
 
@@ -55,8 +54,7 @@ int connect_cmdsocket( char *name ) {
        fd = socket(AF_UNIX, SOCK_DGRAM, 0);
        strcpy(server.sun_path, name);
        server.sun_family = AF_UNIX;
-       connect(fd, (struct sockaddr *) &server,
-               strlen(server.sun_path) + sizeof (server.sun_family));
+       connect(fd, (struct sockaddr *) &server, sizeof(server));
        return fd;
 }

and the return value from this call checked for errors (connect() is currently failing and returning EPROTOTYPE (Protocol wrong type for socket)).

All the above is almost certainly the cause of issue #22 and errors like this.

holdenc avatar Aug 07 '18 21:08 holdenc

I can confirm that just the change to triggerhappy.socket fixes device hot plug on my Raspberry Pi 3.

Thank you @holdenc

Looking at the age of this ticket, I am wondering if it's time for a fork and new release, but, little things.

zelch avatar Oct 04 '21 01:10 zelch