You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					68 lines
				
				1.8 KiB
			
		
		
			
		
	
	
					68 lines
				
				1.8 KiB
			| 
											2 years ago
										 | import errno
 | ||
|  | import fcntl
 | ||
| 
											5 years ago
										 | import os
 | ||
|  | import sys
 | ||
| 
											2 years ago
										 | import pathlib
 | ||
| 
											2 years ago
										 | import shutil
 | ||
| 
											2 years ago
										 | import signal
 | ||
| 
											2 years ago
										 | import subprocess
 | ||
|  | import tempfile
 | ||
|  | import threading
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											2 years ago
										 | from openpilot.common.basedir import BASEDIR
 | ||
|  | from openpilot.common.params import Params
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 | def unblock_stdout() -> None:
 | ||
| 
											5 years ago
										 |   # get a non-blocking stdout
 | ||
|  |   child_pid, child_pty = os.forkpty()
 | ||
|  |   if child_pid != 0:  # parent
 | ||
|  | 
 | ||
|  |     # child is in its own process group, manually pass kill signals
 | ||
|  |     signal.signal(signal.SIGINT, lambda signum, frame: os.kill(child_pid, signal.SIGINT))
 | ||
|  |     signal.signal(signal.SIGTERM, lambda signum, frame: os.kill(child_pid, signal.SIGTERM))
 | ||
|  | 
 | ||
|  |     fcntl.fcntl(sys.stdout, fcntl.F_SETFL, fcntl.fcntl(sys.stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
 | ||
|  | 
 | ||
|  |     while True:
 | ||
|  |       try:
 | ||
|  |         dat = os.read(child_pty, 4096)
 | ||
|  |       except OSError as e:
 | ||
|  |         if e.errno == errno.EIO:
 | ||
|  |           break
 | ||
|  |         continue
 | ||
|  | 
 | ||
|  |       if not dat:
 | ||
|  |         break
 | ||
|  | 
 | ||
|  |       try:
 | ||
|  |         sys.stdout.write(dat.decode('utf8'))
 | ||
| 
											4 years ago
										 |       except (OSError, UnicodeDecodeError):
 | ||
| 
											5 years ago
										 |         pass
 | ||
|  | 
 | ||
|  |     # os.wait() returns a tuple with the pid and a 16 bit value
 | ||
| 
											3 years ago
										 |     # whose low byte is the signal number and whose high byte is the exit status
 | ||
| 
											5 years ago
										 |     exit_status = os.wait()[1] >> 8
 | ||
|  |     os._exit(exit_status)
 | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | def write_onroad_params(started, params):
 | ||
|  |   params.put_bool("IsOnroad", started)
 | ||
|  |   params.put_bool("IsOffroad", not started)
 | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | def save_bootlog():
 | ||
|  |   # copy current params
 | ||
|  |   tmp = tempfile.mkdtemp()
 | ||
| 
											2 years ago
										 |   params_dirname = pathlib.Path(Params().get_param_path()).name
 | ||
|  |   params_dir = os.path.join(tmp, params_dirname)
 | ||
|  |   shutil.copytree(Params().get_param_path(), params_dir, dirs_exist_ok=True)
 | ||
| 
											2 years ago
										 | 
 | ||
| 
											2 years ago
										 |   def fn(tmpdir):
 | ||
|  |     env = os.environ.copy()
 | ||
| 
											2 years ago
										 |     env['PARAMS_COPY_PATH'] = tmpdir
 | ||
| 
											2 years ago
										 |     subprocess.call("./bootlog", cwd=os.path.join(BASEDIR, "system/loggerd"), env=env)
 | ||
|  |     shutil.rmtree(tmpdir)
 | ||
|  |   t = threading.Thread(target=fn, args=(tmp, ))
 | ||
| 
											2 years ago
										 |   t.daemon = True
 | ||
|  |   t.start()
 |