Skip to content

Commit 766ffc1

Browse files
authored
Merge pull request #13 from replit/th-multiple-log-files
Better logging
2 parents 25fe9a9 + e53ff6e commit 766ffc1

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

scripts/mypython

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# A python wrapper that uses LD_AUDIT with rtld loader for testing.
44

55
export PYTHONPATH=${REPL_HOME}/.pythonlibs/lib/python3.10/site-packages
6+
export REPLIT_RTLD_LOG_LEVEL=2
67
export LD_LIBRARY_PATH=
78
export LD_AUDIT=$REPL_HOME/rtld_loader.so
89
exec "python3" "$@"

src/logging.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "string_funs.h"
44
#include "syscalls.h"
55

6+
#define LOG_FILE_PREFIX "rtld_loader.log."
7+
#define LOG_FILE_PREFIX_LENGTH 16
8+
69
static int audit_log_fd = -1;
710
static int log_level;
811

@@ -17,11 +20,49 @@ void fprint_int(int fd, int num) {
1720
sys_write(fd, int_str, len);
1821
}
1922

23+
// For troubleshooting purposes: output contents of /proc/self/cmdline to the
24+
// log file
25+
void _output_cmdline() {
26+
char buf[1024];
27+
if (log_level < INFO) {
28+
return;
29+
}
30+
log_info("cmdline: ");
31+
int cmdline_fd = sys_open("/proc/self/cmdline", O_RDONLY, 0);
32+
while (1) {
33+
int bytes = sys_read(cmdline_fd, buf, sizeof(buf));
34+
if (bytes <= 0) {
35+
break;
36+
}
37+
sys_write(audit_log_fd, buf, bytes);
38+
}
39+
log_info("\n");
40+
sys_close(cmdline_fd);
41+
}
42+
2043
void log_init(int ll) {
2144
log_level = ll;
2245
if (log_level > 0) {
23-
audit_log_fd =
24-
sys_open("rtld_loader.log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
46+
// Generated a log file name with a numeric suffix, i.e. rtld_loader.log.1
47+
// if the file already exists, increment the suffix
48+
char log_filename[LOG_FILE_PREFIX_LENGTH + MAX_DECIMAL_INT_LEN + 1];
49+
my_strncpy(log_filename, LOG_FILE_PREFIX, LOG_FILE_PREFIX_LENGTH);
50+
int suffix = 1;
51+
while (1) {
52+
int suffix_len = itoa(suffix, log_filename + LOG_FILE_PREFIX_LENGTH, 10);
53+
log_filename[LOG_FILE_PREFIX_LENGTH + suffix_len] = '\0';
54+
if (sys_access(log_filename, R_OK) == 0) {
55+
// file already exists, try the next suffix
56+
suffix++;
57+
} else {
58+
// we can use this one
59+
audit_log_fd =
60+
sys_open(log_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
61+
break;
62+
}
63+
}
64+
65+
_output_cmdline();
2566
}
2667
}
2768

test/integration_tests.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def realize(pkgpath):
1616
realize(ace)
1717

1818
def test_ace_dynamic():
19+
subprocess.run(["bash", "-c", "rm rtld_loader.log.*"])
1920
code = "import ctypes; print(ctypes.cdll.LoadLibrary('libACE_ETCL.so'))"
2021

2122
env = rtld_env.copy()
@@ -26,8 +27,31 @@ def test_ace_dynamic():
2627
["%s/bin/python" % python_23_11, '-c', code],
2728
env = env
2829
)
29-
30+
31+
print(str(output, 'UTF-8'))
3032
assert str(output, 'UTF-8').startswith("<CDLL 'libACE_ETCL.so'")
3133
print('OK test_ace_dynamic')
3234

33-
test_ace_dynamic()
35+
def test_multiple_logs_for_subprocess():
36+
subprocess.run(["bash", "-c", "rm rtld_loader.log.*"])
37+
code = "import subprocess; subprocess.run(['echo', 'hello, world!'])"
38+
output = subprocess.check_output(
39+
["%s/bin/python" % python_23_11, '-c', code],
40+
env = {
41+
"LD_AUDIT": "%s/rtld_loader.so" % pwd,
42+
"REPLIT_RTLD_LOG_LEVEL": "3"
43+
}
44+
)
45+
assert os.path.exists("rtld_loader.log.1")
46+
assert os.path.exists("rtld_loader.log.2")
47+
with open("rtld_loader.log.1", "r") as f:
48+
content = f.read()
49+
assert "/bin/python" in content
50+
with open("rtld_loader.log.2", "r") as f:
51+
content = f.read()
52+
assert "echo" in content
53+
assert "hello, world!" in content
54+
print('OK test_multiple_logs_for_subprocess')
55+
56+
test_ace_dynamic()
57+
test_multiple_logs_for_subprocess()

0 commit comments

Comments
 (0)