Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set proctitle by overwriting argv #32691

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions common/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ common_libs = [
'util.cc',
'i2c.cc',
'watchdog.cc',
'ratekeeper.cc'
'ratekeeper.cc',
'proc_title.cc'
]

if arch != "Darwin":
Expand All @@ -28,12 +29,13 @@ if GetOption('extras'):

# Cython bindings
params_python = envCython.Program('params_pyx.so', 'params_pyx.pyx', LIBS=envCython['LIBS'] + [_common, 'zmq', 'json11'])
proc_title_python = envCython.Program('proc_title_pyx.so', 'proc_title_pyx.pyx')

SConscript([
'transformations/SConscript',
])

Import('transformations_python')
common_python = [params_python, transformations_python]
common_python = [params_python, proc_title_python, transformations_python]

Export('common_python')
62 changes: 62 additions & 0 deletions common/proc_title.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <string>

extern char **environ;

char* argv = nullptr;
int len_argv;

void _init(int argc, char* argv0) {
char *ptr = environ[0] - 1;
char *limit = ptr - 2048;

for ( int i = argc - 1; i >= 1; i-- ) {
ptr--;
while ( *ptr && ptr > limit ) {
ptr--;
}

*ptr = ' ';

if ( ptr <= limit ) {
return;
}
}

ptr = ptr - strlen(argv0);

if ( strncmp(ptr, argv0, strlen(argv0)) ) {
return;
}

argv = ptr;
len_argv = environ[0] - 1 - ptr;
}

void setProcTitle(char* new_title) {
if (argv == nullptr) {
return;
}

int new_title_len = strlen(new_title);

if ( new_title_len > len_argv ) {
new_title_len = len_argv - 1;
}

strncpy(argv, new_title, new_title_len);
memset(argv + new_title_len, 0, len_argv - new_title_len);

return;
}

std::string getProcTitle() {
if (argv == nullptr) {
return "";
}

return argv;
}
31 changes: 31 additions & 0 deletions common/proc_title_pyx.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# distutils: language = c++
# cython: language_level = 3
import sys

from libcpp.string cimport string


cdef extern from "common/proc_title.cc":
void setProcTitle(char*)
cdef string getProcTitle()
void _init(int, char*)


cdef init():
argv = sys.orig_argv
argc = len(argv)
_init(argc, argv[0].encode())


def set_proc_title(title):
if sys.platform not in ('linux'):
return
setProcTitle(title.encode())


def get_proc_title():
if sys.platform not in ('linux'):
return 0
return getProcTitle().decode()

init()
59 changes: 59 additions & 0 deletions common/tests/test_proc_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest
import sys
import subprocess

import openpilot.common.proc_title_pyx as proc_title

def _run(script: str):
result = subprocess.run(
[sys.executable],
input=script,
capture_output=True,
text=True,
shell=True,
)

return result.stdout


@pytest.mark.skipif(sys.platform not in ('linux'), reason="only supported on linux")
class TestProcTitle:
def test_set_proc_title(self):
title = 'test'
script = f'''
from openpilot.common.proc_title_pyx import set_proc_title
import os

set_proc_title("{title}")
print(os.getpid())
print(os.popen("ps -x -o pid,command 2> /dev/null").read())
'''
out = _run(script)
lines = [line for line in out.splitlines() if line]
test_pid = lines.pop(0)
pids = dict([line.strip().split(None, 1) for line in lines])
assert pids[test_pid] == title


def test_truncate_long_title(self):
title_len = 1000
script = f'''
from openpilot.common.proc_title_pyx import set_proc_title
import os

set_proc_title(''.join(['a' for _ in range({title_len})]))
print(os.getpid())
print(os.popen("ps -x -o pid,command 2> /dev/null").read())
'''
out = _run(script)
lines = [line for line in out.splitlines() if line]
test_pid = lines.pop(0)
pids = dict([line.strip().split(None, 1) for line in lines])
assert len(pids[test_pid]) < title_len


def test_set_and_get(self):
proc_title.set_proc_title('abc')
assert proc_title.get_proc_title() == 'abc'
proc_title.set_proc_title('again')
assert proc_title.get_proc_title() == 'again'
Loading