Skip to content

Commit f2f66bb

Browse files
authored
Merge pull request OpenInterpreter#239 from rbrisita/fix_dmesg
Better Handle `dmesg` Log File
2 parents b3e1655 + 471b5f8 commit f2f66bb

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

software/source/server/utils/kernel.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
import asyncio
66
import subprocess
77
import platform
8+
import os
9+
import shutil
810

911
from .logs import setup_logging
1012
from .logs import logger
1113

1214
setup_logging()
1315

16+
# dmesg process created at boot time
17+
dmesg_proc = None
18+
1419

1520
def get_kernel_messages():
1621
"""
@@ -25,12 +30,37 @@ def get_kernel_messages():
2530
output, _ = process.communicate()
2631
return output.decode("utf-8")
2732
elif current_platform == "Linux":
28-
with open("/var/log/dmesg", "r") as file:
33+
log_path = get_dmesg_log_path()
34+
with open(log_path, 'r') as file:
2935
return file.read()
3036
else:
3137
logger.info("Unsupported platform.")
3238

3339

40+
def get_dmesg_log_path():
41+
"""
42+
Check for the existence of a readable dmesg log file and return its path.
43+
Create an accessible path if not found.
44+
"""
45+
if os.access('/var/log/dmesg', os.F_OK | os.R_OK):
46+
return '/var/log/dmesg'
47+
48+
global dmesg_proc
49+
dmesg_log_path = '/tmp/dmesg'
50+
if dmesg_proc:
51+
return dmesg_log_path
52+
53+
logger.info("Created /tmp/dmesg.")
54+
subprocess.run(['touch', dmesg_log_path])
55+
dmesg_path = shutil.which('dmesg')
56+
if dmesg_path:
57+
logger.info(f"Writing to {dmesg_log_path} from dmesg.")
58+
dmesg_proc = subprocess.Popen([dmesg_path, '--follow'], text=True, stdout=subprocess.PIPE)
59+
subprocess.Popen(['tee', dmesg_log_path], text=True, stdin=dmesg_proc.stdout, stdout=subprocess.DEVNULL)
60+
61+
return dmesg_log_path
62+
63+
3464
def custom_filter(message):
3565
# Check for {TO_INTERPRETER{ message here }TO_INTERPRETER} pattern
3666
if "{TO_INTERPRETER{" in message and "}TO_INTERPRETER}" in message:

0 commit comments

Comments
 (0)