mirror of
https://github.com/systemd/systemd
synced 2025-11-19 08:44:44 +01:00
Compare commits
No commits in common. "f03b49b079fd4ecb6407e74a116995ded906325a" and "f59f4ac2a7c30256ea54900fddbb113d754afb38" have entirely different histories.
f03b49b079
...
f59f4ac2a7
@ -172,19 +172,18 @@ static int update_argv(const char name[], size_t l) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rename_process_full(const char *comm, const char *invocation) {
|
int rename_process(const char name[]) {
|
||||||
bool truncated = false;
|
bool truncated = false;
|
||||||
|
|
||||||
/* This is a like a poor man's setproctitle(). It changes the comm field by the name specified by
|
/* This is a like a poor man's setproctitle(). It changes the comm field, argv[0], and also the glibc's
|
||||||
* 'comm', and changes argv[0] and the glibc's internally used names of the process
|
* internally used name of the process. For the first one a limit of 16 chars applies; to the second one in
|
||||||
* (program_invocation_name and program_invocation_short_name) by the name specified by 'invocation'.
|
* many cases one of 10 (i.e. length of "/sbin/init") — however if we have CAP_SYS_RESOURCES it is unbounded;
|
||||||
* For the first one a limit of 16 chars applies; to the second one in many cases one of 10 (i.e.
|
* to the third one 7 (i.e. the length of "systemd". If you pass a longer string it will likely be
|
||||||
* length of "/sbin/init") — however if we have CAP_SYS_RESOURCES it is unbounded; to the third one
|
* truncated.
|
||||||
* 7 (i.e. the length of "systemd". If you pass a longer string it will likely be truncated.
|
|
||||||
*
|
*
|
||||||
* Returns 0 if a name was set but truncated, > 0 if it was set but not truncated. */
|
* Returns 0 if a name was set but truncated, > 0 if it was set but not truncated. */
|
||||||
|
|
||||||
if (isempty(comm))
|
if (isempty(name))
|
||||||
return -EINVAL; /* let's not confuse users unnecessarily with an empty name */
|
return -EINVAL; /* let's not confuse users unnecessarily with an empty name */
|
||||||
|
|
||||||
if (!is_main_thread())
|
if (!is_main_thread())
|
||||||
@ -192,27 +191,21 @@ int rename_process_full(const char *comm, const char *invocation) {
|
|||||||
* cache things without locking, and we make assumptions that PR_SET_NAME sets the
|
* cache things without locking, and we make assumptions that PR_SET_NAME sets the
|
||||||
* process name that isn't correct on any other threads */
|
* process name that isn't correct on any other threads */
|
||||||
|
|
||||||
size_t l = strlen(comm);
|
size_t l = strlen(name);
|
||||||
|
|
||||||
/* First step, change the comm field. The main thread's comm is identical to the process comm. This means we
|
/* First step, change the comm field. The main thread's comm is identical to the process comm. This means we
|
||||||
* can use PR_SET_NAME, which sets the thread name for the calling thread. */
|
* can use PR_SET_NAME, which sets the thread name for the calling thread. */
|
||||||
if (prctl(PR_SET_NAME, comm) < 0)
|
if (prctl(PR_SET_NAME, name) < 0)
|
||||||
log_debug_errno(errno, "PR_SET_NAME failed: %m");
|
log_debug_errno(errno, "PR_SET_NAME failed: %m");
|
||||||
if (l >= TASK_COMM_LEN) /* Linux userspace process names can be 15 chars at max */
|
if (l >= TASK_COMM_LEN) /* Linux userspace process names can be 15 chars at max */
|
||||||
truncated = true;
|
truncated = true;
|
||||||
|
|
||||||
/* If nothing specified, fall back to comm. */
|
|
||||||
if (isempty(invocation))
|
|
||||||
invocation = comm;
|
|
||||||
|
|
||||||
l = strlen(invocation);
|
|
||||||
|
|
||||||
/* Second step, change glibc's ID of the process name. */
|
/* Second step, change glibc's ID of the process name. */
|
||||||
if (program_invocation_name) {
|
if (program_invocation_name) {
|
||||||
size_t k;
|
size_t k;
|
||||||
|
|
||||||
k = strlen(program_invocation_name);
|
k = strlen(program_invocation_name);
|
||||||
strncpy(program_invocation_name, invocation, k);
|
strncpy(program_invocation_name, name, k);
|
||||||
if (l > k)
|
if (l > k)
|
||||||
truncated = true;
|
truncated = true;
|
||||||
|
|
||||||
@ -224,7 +217,7 @@ int rename_process_full(const char *comm, const char *invocation) {
|
|||||||
/* Third step, completely replace the argv[] array the kernel maintains for us. This requires privileges, but
|
/* Third step, completely replace the argv[] array the kernel maintains for us. This requires privileges, but
|
||||||
* has the advantage that the argv[] array is exactly what we want it to be, and not filled up with zeros at
|
* has the advantage that the argv[] array is exactly what we want it to be, and not filled up with zeros at
|
||||||
* the end. This is the best option for changing /proc/self/cmdline. */
|
* the end. This is the best option for changing /proc/self/cmdline. */
|
||||||
(void) update_argv(invocation, l);
|
(void) update_argv(name, l);
|
||||||
|
|
||||||
/* Fourth step: in all cases we'll also update the original argv[], so that our own code gets it right too if
|
/* Fourth step: in all cases we'll also update the original argv[], so that our own code gets it right too if
|
||||||
* it still looks here */
|
* it still looks here */
|
||||||
@ -233,7 +226,7 @@ int rename_process_full(const char *comm, const char *invocation) {
|
|||||||
size_t k;
|
size_t k;
|
||||||
|
|
||||||
k = strlen(saved_argv[0]);
|
k = strlen(saved_argv[0]);
|
||||||
strncpy(saved_argv[0], invocation, k);
|
strncpy(saved_argv[0], name, k);
|
||||||
if (l > k)
|
if (l > k)
|
||||||
truncated = true;
|
truncated = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,4 @@ bool invoked_as(char *argv[], const char *token);
|
|||||||
bool invoked_by_systemd(void);
|
bool invoked_by_systemd(void);
|
||||||
bool argv_looks_like_help(int argc, char **argv);
|
bool argv_looks_like_help(int argc, char **argv);
|
||||||
|
|
||||||
int rename_process_full(const char *comm, const char *invocation);
|
int rename_process(const char name[]);
|
||||||
static inline int rename_process(const char *name) {
|
|
||||||
return rename_process_full(name, name);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1490,30 +1490,35 @@ fail:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void rename_process_from_path(const char *path) {
|
static void rename_process_from_path(const char *path) {
|
||||||
int r;
|
_cleanup_free_ char *buf = NULL;
|
||||||
|
const char *p;
|
||||||
|
|
||||||
assert(path);
|
assert(path);
|
||||||
|
|
||||||
_cleanup_free_ char *buf = NULL;
|
/* This resulting string must fit in 10 chars (i.e. the length of "/sbin/init") to look pretty in
|
||||||
r = path_extract_filename(path, &buf);
|
* /bin/ps */
|
||||||
if (r < 0) {
|
|
||||||
log_debug_errno(r, "Failed to extract file name from '%s', ignoring: %m", path);
|
if (path_extract_filename(path, &buf) < 0) {
|
||||||
return (void) rename_process("(...)");
|
rename_process("(...)");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t len = strlen(buf);
|
size_t l = strlen(buf);
|
||||||
char comm[TASK_COMM_LEN], *p = comm;
|
if (l > 8) {
|
||||||
*p++ = '(';
|
/* The end of the process name is usually more interesting, since the first bit might just be
|
||||||
p = mempcpy(p, buf + LESS_BY(len, (size_t) (TASK_COMM_LEN - 3)), MIN(len, (size_t) (TASK_COMM_LEN - 3)));
|
* "systemd-" */
|
||||||
*p++ = ')';
|
p = buf + l - 8;
|
||||||
*p = '\0';
|
l = 8;
|
||||||
|
} else
|
||||||
|
p = buf;
|
||||||
|
|
||||||
size_t len_invocation = program_invocation_name ? strlen(program_invocation_name) : SIZE_MAX;
|
char process_name[11];
|
||||||
_cleanup_free_ char *invocation = strjoin("(", buf + LESS_BY(len, len_invocation - 2), ")");
|
process_name[0] = '(';
|
||||||
if (!invocation)
|
memcpy(process_name+1, p, l);
|
||||||
log_oom_debug();
|
process_name[1+l] = ')';
|
||||||
|
process_name[1+l+1] = 0;
|
||||||
|
|
||||||
(void) rename_process_full(comm, invocation ?: comm);
|
(void) rename_process(process_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool context_has_address_families(const ExecContext *c) {
|
static bool context_has_address_families(const ExecContext *c) {
|
||||||
|
|||||||
@ -19,7 +19,7 @@ mkdir -p /run/systemd/system/systemd-resolved.service.d/
|
|||||||
cat >/run/systemd/system/systemd-resolved.service.d/99-start-limit.conf <<EOF
|
cat >/run/systemd/system/systemd-resolved.service.d/99-start-limit.conf <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
StartLimitBurst=5
|
StartLimitBurst=5
|
||||||
StartLimitInterval=100
|
StartLimitInterval=30
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStopPost=sleep 10
|
ExecStopPost=sleep 10
|
||||||
@ -39,5 +39,4 @@ for i in {1..5}; do
|
|||||||
journalctl -o short-monotonic --no-hostname --no-pager -u systemd-resolved.service -n 15
|
journalctl -o short-monotonic --no-hostname --no-pager -u systemd-resolved.service -n 15
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
systemctl is-active systemd-resolved.service
|
|
||||||
done
|
done
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user