From e0db9f3a6ec5caf619cba2e693be1b155df052f4 Mon Sep 17 00:00:00 2001 From: Lasse Dalegaard Date: Mon, 1 Mar 2021 15:49:18 +0000 Subject: [PATCH] serialhdl: prevent creation of controlling tty When `serialhdl` opens a terminal device it must prevent the device from becoming the controlling terminal of `klippy`, as such a terminal will send additional messages to the session leader, e.g. a SIGHUP, which would kill `klippy`. See e.g. #3981. pySerial already does this, but for e.g. `klipper_mcu` we were not doing this ourselves. On Linux a process must set `O_NOCTTY` when opening any file that could potentially be a terminal device, to avoid this. Earlier process daemonization tools prevent this from being an issue by double forking, but under `systemd` a process must take steps to avoid opening a controlling terminal. Signed-off-by: Lasse Dalegaard Signed-off-by: Kevin O'Connor --- klippy/serialhdl.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/klippy/serialhdl.py b/klippy/serialhdl.py index e9c645d0..9ef39dfd 100644 --- a/klippy/serialhdl.py +++ b/klippy/serialhdl.py @@ -3,7 +3,7 @@ # Copyright (C) 2016-2020 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import logging, threading +import logging, threading, os import serial import msgproto, chelper, util @@ -91,7 +91,8 @@ class SerialReader: self.ser.rts = self.rts self.ser.open() else: - self.ser = open(self.serialport, 'rb+', buffering=0) + fd = os.open(self.serialport, os.O_RDWR | os.O_NOCTTY) + self.ser = os.fdopen(fd, 'rb+', 0) except (OSError, IOError, serial.SerialException) as e: logging.warn("Unable to open port: %s", e) self.reactor.pause(connect_time + 5.)