1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-27 10:25:37 +03:00

Merge pull request #32961 from YHNdnzj/starttime-main

core/service: try to query for new main process's starttime
This commit is contained in:
Yu Watanabe 2024-06-12 19:12:37 +09:00 committed by GitHub
commit 21f51d877f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 12 deletions

View File

@ -56,6 +56,7 @@
#include "string-table.h" #include "string-table.h"
#include "string-util.h" #include "string-util.h"
#include "terminal-util.h" #include "terminal-util.h"
#include "time-util.h"
#include "user-util.h" #include "user-util.h"
#include "utf8.h" #include "utf8.h"
@ -731,7 +732,7 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
return 0; return 0;
} }
int pid_get_start_time(pid_t pid, uint64_t *ret) { int pid_get_start_time(pid_t pid, usec_t *ret) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
const char *p; const char *p;
int r; int r;
@ -751,13 +752,12 @@ int pid_get_start_time(pid_t pid, uint64_t *ret) {
p = strrchr(line, ')'); p = strrchr(line, ')');
if (!p) if (!p)
return -EIO; return -EIO;
p++; p++;
unsigned long llu; unsigned long llu;
if (sscanf(p, " " if (sscanf(p, " "
"%*c " /* state */ "%*c " /* state */
"%*u " /* ppid */ "%*u " /* ppid */
"%*u " /* pgrp */ "%*u " /* pgrp */
"%*u " /* session */ "%*u " /* session */
@ -781,13 +781,13 @@ int pid_get_start_time(pid_t pid, uint64_t *ret) {
return -EIO; return -EIO;
if (ret) if (ret)
*ret = llu; *ret = jiffies_to_usec(llu); /* CLOCK_BOOTTIME */
return 0; return 0;
} }
int pidref_get_start_time(const PidRef *pid, uint64_t *ret) { int pidref_get_start_time(const PidRef *pid, usec_t *ret) {
uint64_t t; usec_t t;
int r; int r;
if (!pidref_is_set(pid)) if (!pidref_is_set(pid))

View File

@ -54,8 +54,8 @@ int get_process_cwd(pid_t pid, char **ret);
int get_process_root(pid_t pid, char **ret); int get_process_root(pid_t pid, char **ret);
int get_process_environ(pid_t pid, char **ret); int get_process_environ(pid_t pid, char **ret);
int get_process_ppid(pid_t pid, pid_t *ret); int get_process_ppid(pid_t pid, pid_t *ret);
int pid_get_start_time(pid_t pid, uint64_t *ret); int pid_get_start_time(pid_t pid, usec_t *ret);
int pidref_get_start_time(const PidRef* pid, uint64_t *ret); int pidref_get_start_time(const PidRef* pid, usec_t *ret);
int get_process_umask(pid_t pid, mode_t *ret); int get_process_umask(pid_t pid, mode_t *ret);
int container_get_leader(const char *machine, pid_t *pid); int container_get_leader(const char *machine, pid_t *pid);

View File

@ -214,6 +214,16 @@ static int service_set_main_pidref(Service *s, PidRef pidref_consume, const dual
if (!pidref_equal(&s->main_pid, &pidref)) { if (!pidref_equal(&s->main_pid, &pidref)) {
service_unwatch_main_pid(s); service_unwatch_main_pid(s);
dual_timestamp pid_start_time;
if (!start_timestamp) {
usec_t t;
if (pidref_get_start_time(&pidref, &t) >= 0)
start_timestamp = dual_timestamp_from_boottime(&pid_start_time, t);
}
exec_status_start(&s->main_exec_status, pidref.pid, start_timestamp); exec_status_start(&s->main_exec_status, pidref.pid, start_timestamp);
} }

View File

@ -951,18 +951,18 @@ TEST(pid_get_start_time) {
assert_se(pidref_set_self(&pidref) >= 0); assert_se(pidref_set_self(&pidref) >= 0);
uint64_t start_time; usec_t start_time;
assert_se(pidref_get_start_time(&pidref, &start_time) >= 0); assert_se(pidref_get_start_time(&pidref, &start_time) >= 0);
log_info("our starttime: %" PRIu64, start_time); log_info("our starttime: " USEC_FMT, start_time);
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL; _cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
assert_se(pidref_safe_fork("(stub)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &child) >= 0); assert_se(pidref_safe_fork("(stub)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &child) >= 0);
uint64_t start_time2; usec_t start_time2;
assert_se(pidref_get_start_time(&child, &start_time2) >= 0); assert_se(pidref_get_start_time(&child, &start_time2) >= 0);
log_info("child starttime: %" PRIu64, start_time2); log_info("child starttime: " USEC_FMT, start_time2);
assert_se(start_time2 >= start_time); assert_se(start_time2 >= start_time);
} }