mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-01 10:18:00 +02:00
shared: move nm_utils_get_start_time_for_pid() to shared/nm-utils
We will also use it in nmcli later. It will be needed when we replace polkit_unix_process_new_for_owner(). Which is still far down the road.
This commit is contained in:
parent
313f47c915
commit
e5e8f86c3d
5 changed files with 83 additions and 80 deletions
|
|
@ -1195,3 +1195,82 @@ nm_utils_strv_make_deep_copied (const char **strv)
|
|||
|
||||
return (char **) strv;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
/**
|
||||
* nm_utils_get_start_time_for_pid:
|
||||
* @pid: the process identifier
|
||||
* @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)
|
||||
* and adjusted.
|
||||
*
|
||||
* Returns: the timestamp when the process started (by parsing /proc/$PID/stat).
|
||||
* If an error occurs (e.g. the process does not exist), 0 is returned.
|
||||
*
|
||||
* The returned start time counts since boot, in the unit HZ (with HZ usually being (1/100) seconds)
|
||||
**/
|
||||
guint64
|
||||
nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid)
|
||||
{
|
||||
guint64 start_time;
|
||||
char filename[256];
|
||||
gs_free gchar *contents = NULL;
|
||||
size_t length;
|
||||
gs_strfreev gchar **tokens = NULL;
|
||||
guint num_tokens;
|
||||
gchar *p;
|
||||
char state = ' ';
|
||||
gint64 ppid = 0;
|
||||
|
||||
start_time = 0;
|
||||
contents = NULL;
|
||||
|
||||
g_return_val_if_fail (pid > 0, 0);
|
||||
|
||||
nm_sprintf_buf (filename, "/proc/%"G_GUINT64_FORMAT"/stat", (guint64) pid);
|
||||
|
||||
if (!g_file_get_contents (filename, &contents, &length, NULL))
|
||||
goto fail;
|
||||
|
||||
/* start time is the token at index 19 after the '(process name)' entry - since only this
|
||||
* field can contain the ')' character, search backwards for this to avoid malicious
|
||||
* processes trying to fool us
|
||||
*/
|
||||
p = strrchr (contents, ')');
|
||||
if (p == NULL)
|
||||
goto fail;
|
||||
p += 2; /* skip ') ' */
|
||||
if (p - contents >= (int) length)
|
||||
goto fail;
|
||||
|
||||
state = p[0];
|
||||
|
||||
tokens = g_strsplit (p, " ", 0);
|
||||
|
||||
num_tokens = g_strv_length (tokens);
|
||||
|
||||
if (num_tokens < 20)
|
||||
goto fail;
|
||||
|
||||
if (out_ppid) {
|
||||
ppid = _nm_utils_ascii_str_to_int64 (tokens[1], 10, 1, G_MAXINT, 0);
|
||||
if (ppid == 0)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
start_time = _nm_utils_ascii_str_to_int64 (tokens[19], 10, 1, G_MAXINT64, 0);
|
||||
if (start_time == 0)
|
||||
goto fail;
|
||||
|
||||
NM_SET_OUT (out_state, state);
|
||||
NM_SET_OUT (out_ppid, ppid);
|
||||
return start_time;
|
||||
|
||||
fail:
|
||||
NM_SET_OUT (out_state, ' ');
|
||||
NM_SET_OUT (out_ppid, 0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -586,4 +586,8 @@ int nm_utils_fd_read_loop_exact (int fd, void *buf, size_t nbytes, bool do_poll)
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
guint64 nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid);
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
#endif /* __NM_SHARED_UTILS_H__ */
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "nm-dbus-manager.h"
|
||||
#include "NetworkManagerUtils.h"
|
||||
|
||||
enum {
|
||||
PROP_0,
|
||||
|
|
|
|||
|
|
@ -452,83 +452,6 @@ nm_utils_modprobe (GError **error, gboolean suppress_error_logging, const char *
|
|||
return exit_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* nm_utils_get_start_time_for_pid:
|
||||
* @pid: the process identifier
|
||||
* @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)
|
||||
* and adjusted.
|
||||
*
|
||||
* Returns: the timestamp when the process started (by parsing /proc/$PID/stat).
|
||||
* If an error occurs (e.g. the process does not exist), 0 is returned.
|
||||
*
|
||||
* The returned start time counts since boot, in the unit HZ (with HZ usually being (1/100) seconds)
|
||||
**/
|
||||
guint64
|
||||
nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid)
|
||||
{
|
||||
guint64 start_time;
|
||||
char filename[256];
|
||||
gs_free gchar *contents = NULL;
|
||||
size_t length;
|
||||
gs_strfreev gchar **tokens = NULL;
|
||||
guint num_tokens;
|
||||
gchar *p;
|
||||
char state = ' ';
|
||||
gint64 ppid = 0;
|
||||
|
||||
start_time = 0;
|
||||
contents = NULL;
|
||||
|
||||
g_return_val_if_fail (pid > 0, 0);
|
||||
|
||||
nm_sprintf_buf (filename, "/proc/%"G_GUINT64_FORMAT"/stat", (guint64) pid);
|
||||
|
||||
if (!g_file_get_contents (filename, &contents, &length, NULL))
|
||||
goto fail;
|
||||
|
||||
/* start time is the token at index 19 after the '(process name)' entry - since only this
|
||||
* field can contain the ')' character, search backwards for this to avoid malicious
|
||||
* processes trying to fool us
|
||||
*/
|
||||
p = strrchr (contents, ')');
|
||||
if (p == NULL)
|
||||
goto fail;
|
||||
p += 2; /* skip ') ' */
|
||||
if (p - contents >= (int) length)
|
||||
goto fail;
|
||||
|
||||
state = p[0];
|
||||
|
||||
tokens = g_strsplit (p, " ", 0);
|
||||
|
||||
num_tokens = g_strv_length (tokens);
|
||||
|
||||
if (num_tokens < 20)
|
||||
goto fail;
|
||||
|
||||
if (out_ppid) {
|
||||
ppid = _nm_utils_ascii_str_to_int64 (tokens[1], 10, 1, G_MAXINT, 0);
|
||||
if (ppid == 0)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
start_time = _nm_utils_ascii_str_to_int64 (tokens[19], 10, 1, G_MAXINT64, 0);
|
||||
if (start_time == 0)
|
||||
goto fail;
|
||||
|
||||
NM_SET_OUT (out_state, state);
|
||||
NM_SET_OUT (out_ppid, ppid);
|
||||
return start_time;
|
||||
|
||||
fail:
|
||||
NM_SET_OUT (out_state, ' ');
|
||||
NM_SET_OUT (out_ppid, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
|
|
|
|||
|
|
@ -178,8 +178,6 @@ nm_utils_ip_route_metric_penalize (int addr_family, guint32 metric, guint32 pena
|
|||
|
||||
int nm_utils_modprobe (GError **error, gboolean suppress_error_loggin, const char *arg1, ...) G_GNUC_NULL_TERMINATED;
|
||||
|
||||
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,
|
||||
const char *log_name, guint32 wait_before_kill_msec,
|
||||
guint32 sleep_duration_msec, guint32 max_wait_msec);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue