core: return parent pid from nm_utils_get_start_time_for_pid()

This commit is contained in:
Thomas Haller
2015-06-30 14:51:42 +02:00
parent a396ad2ac1
commit 7c9eefa767
4 changed files with 13 additions and 6 deletions

View File

@@ -358,6 +358,7 @@ nm_utils_modprobe (GError **error, gboolean suppress_error_logging, const char *
* nm_utils_get_start_time_for_pid: * nm_utils_get_start_time_for_pid:
* @pid: the process identifier * @pid: the process identifier
* @out_state: return the state character, like R, S, Z. See `man 5 proc`. * @out_state: return the state character, like R, S, Z. See `man 5 proc`.
* @out_ppid: parent process id
* *
* Originally copied from polkit source (src/polkit/polkitunixprocess.c) * Originally copied from polkit source (src/polkit/polkitunixprocess.c)
* and adjusted. * and adjusted.
@@ -368,7 +369,7 @@ nm_utils_modprobe (GError **error, gboolean suppress_error_logging, const char *
* The returned start time counts since boot, in the unit HZ (with HZ usually being (1/100) seconds) * The returned start time counts since boot, in the unit HZ (with HZ usually being (1/100) seconds)
**/ **/
guint64 guint64
nm_utils_get_start_time_for_pid (pid_t pid, char *out_state) nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid)
{ {
guint64 start_time; guint64 start_time;
gs_free gchar *filename = NULL; gs_free gchar *filename = NULL;
@@ -379,6 +380,7 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state)
gchar *p; gchar *p;
gchar *endp; gchar *endp;
char state = '\0'; char state = '\0';
gint64 ppid = 0;
start_time = 0; start_time = 0;
contents = NULL; contents = NULL;
@@ -410,6 +412,9 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state)
if (num_tokens < 20) if (num_tokens < 20)
goto out; goto out;
if (out_ppid)
ppid = _nm_utils_ascii_str_to_int64 (tokens[1], 10, 1, G_MAXINT, 0);
errno = 0; errno = 0;
start_time = strtoull (tokens[19], &endp, 10); start_time = strtoull (tokens[19], &endp, 10);
if (*endp != '\0' || errno != 0) if (*endp != '\0' || errno != 0)
@@ -418,6 +423,8 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state)
out: out:
if (out_state) if (out_state)
*out_state = state; *out_state = state;
if (out_ppid)
*out_ppid = ppid;
return start_time; return start_time;
} }
@@ -895,7 +902,7 @@ nm_utils_kill_process_sync (pid_t pid, guint64 start_time, int sig, NMLogDomain
g_return_if_fail (log_name != NULL); g_return_if_fail (log_name != NULL);
g_return_if_fail (wait_before_kill_msec > 0); g_return_if_fail (wait_before_kill_msec > 0);
start_time0 = nm_utils_get_start_time_for_pid (pid, &p_state); start_time0 = nm_utils_get_start_time_for_pid (pid, &p_state, NULL);
if (start_time0 == 0) { if (start_time0 == 0) {
nm_log_dbg (log_domain, LOG_NAME_PROCESS_FMT ": cannot kill process %ld because it seems already gone", nm_log_dbg (log_domain, LOG_NAME_PROCESS_FMT ": cannot kill process %ld because it seems already gone",
LOG_NAME_ARGS, (long int) pid); LOG_NAME_ARGS, (long int) pid);
@@ -948,7 +955,7 @@ nm_utils_kill_process_sync (pid_t pid, guint64 start_time, int sig, NMLogDomain
max_wait_until = 0; max_wait_until = 0;
while (TRUE) { while (TRUE) {
start_time = nm_utils_get_start_time_for_pid (pid, &p_state); start_time = nm_utils_get_start_time_for_pid (pid, &p_state, NULL);
if (start_time != start_time0) { if (start_time != start_time0) {
nm_log_dbg (log_domain, LOG_NAME_PROCESS_FMT ": process is gone after sending signal %s%s", nm_log_dbg (log_domain, LOG_NAME_PROCESS_FMT ": process is gone after sending signal %s%s",

View File

@@ -69,7 +69,7 @@ str_if_set (const char *str, const char *fallback)
return str ? str : fallback; return str ? str : fallback;
} }
guint64 nm_utils_get_start_time_for_pid (pid_t pid, char *out_state); guint64 nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid);
void nm_utils_kill_process_sync (pid_t pid, guint64 start_time, int sig, guint64 log_domain, void nm_utils_kill_process_sync (pid_t pid, guint64 start_time, int sig, guint64 log_domain,
const char *log_name, guint32 wait_before_kill_msec, const char *log_name, guint32 wait_before_kill_msec,

View File

@@ -592,7 +592,7 @@ nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name)
const char *exe; const char *exe;
/* Ensure the process is a DHCP client */ /* Ensure the process is a DHCP client */
start_time = nm_utils_get_start_time_for_pid (tmp, NULL); start_time = nm_utils_get_start_time_for_pid (tmp, NULL, NULL);
proc_path = g_strdup_printf ("/proc/%ld/cmdline", tmp); proc_path = g_strdup_printf ("/proc/%ld/cmdline", tmp);
if ( start_time if ( start_time
&& g_file_get_contents (proc_path, &proc_contents, NULL, NULL)) { && g_file_get_contents (proc_path, &proc_contents, NULL, NULL)) {

View File

@@ -360,7 +360,7 @@ constructed (GObject *object)
if (!priv->unix_process.dbus_sender || !*priv->unix_process.dbus_sender) if (!priv->unix_process.dbus_sender || !*priv->unix_process.dbus_sender)
break; break;
priv->unix_process.start_time = nm_utils_get_start_time_for_pid (priv->unix_process.pid, NULL); priv->unix_process.start_time = nm_utils_get_start_time_for_pid (priv->unix_process.pid, NULL, NULL);
if (!priv->unix_process.start_time) { if (!priv->unix_process.start_time) {
/* could not detect the process start time. The subject is invalid, but don't /* could not detect the process start time. The subject is invalid, but don't