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 <dalegaard@gmail.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Lasse Dalegaard 2021-03-01 15:49:18 +00:00 committed by Kevin O'Connor
parent 1b989b81e0
commit e0db9f3a6e
1 changed files with 3 additions and 2 deletions

View File

@ -3,7 +3,7 @@
# Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import logging, threading import logging, threading, os
import serial import serial
import msgproto, chelper, util import msgproto, chelper, util
@ -91,7 +91,8 @@ class SerialReader:
self.ser.rts = self.rts self.ser.rts = self.rts
self.ser.open() self.ser.open()
else: 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: except (OSError, IOError, serial.SerialException) as e:
logging.warn("Unable to open port: %s", e) logging.warn("Unable to open port: %s", e)
self.reactor.pause(connect_time + 5.) self.reactor.pause(connect_time + 5.)