[Xfce4-commits] [apps/xfce4-taskmanager] 04/20: Move getpwuid() from backends to gui, use getpwuid_r(), call only once on task add. (bug 14401)

noreply at xfce.org noreply at xfce.org
Wed May 30 22:00:18 CEST 2018


This is an automated email from the git hooks/post-receive script.

l   a   n   d   r   y       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository apps/xfce4-taskmanager.

commit 8e1b48f8def94d7b1b72464aaf495d761d46176a
Author: rim <rozhuk.im at gmail.com>
Date:   Tue May 29 03:11:15 2018 +0300

    Move getpwuid() from backends to gui, use getpwuid_r(), call only once on task add. (bug 14401)
---
 src/task-manager-bsd.c     |  7 -------
 src/task-manager-freebsd.c |  4 ----
 src/task-manager-linux.c   |  5 -----
 src/task-manager-skel.c    |  1 -
 src/task-manager-solaris.c |  4 ----
 src/task-manager.c         | 19 ++++++++++++++++---
 src/task-manager.h         |  2 +-
 7 files changed, 17 insertions(+), 25 deletions(-)

diff --git a/src/task-manager-bsd.c b/src/task-manager-bsd.c
index 0107d20..e921c64 100644
--- a/src/task-manager-bsd.c
+++ b/src/task-manager-bsd.c
@@ -18,9 +18,7 @@
 
 #include <stdlib.h>
 #include <err.h>
-/* for getpwuid() */
 #include <sys/types.h>
-#include <pwd.h>
 /* for sysctl() */
 #include <sys/param.h>
 #include <sys/sched.h>
@@ -54,7 +52,6 @@ gboolean get_task_list (GArray *task_list)
 	struct kinfo_proc2 *kp;
 #endif
 	Task t;
-	struct passwd *passwdp;
 	char **args;
 	gchar* buf;
 	int nproc, i;
@@ -143,10 +140,6 @@ gboolean get_task_list (GArray *task_list)
 
 		t.cpu_user = (100.0 * ((double) p.p_pctcpu / FSCALE));
 		t.cpu_system = 0.0f; /* TODO ? */
-		/* get username from uid */
-		passwdp = getpwuid(t.uid);
-		if(passwdp != NULL && passwdp->pw_name != NULL)
-			g_strlcpy(t.uid_name, passwdp->pw_name, sizeof t.uid_name);
 		g_array_append_val(task_list, t);
 	}
 	free(kp);
diff --git a/src/task-manager-freebsd.c b/src/task-manager-freebsd.c
index 0438b97..ac49064 100644
--- a/src/task-manager-freebsd.c
+++ b/src/task-manager-freebsd.c
@@ -15,7 +15,6 @@
 #include <sys/sysctl.h>
 #include <sys/user.h>
 #include <sys/proc.h>
-#include <pwd.h>
 #include <fcntl.h>
 #include <paths.h>
 #include <unistd.h>
@@ -120,7 +119,6 @@ get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system)
 static gboolean
 get_task_details (struct kinfo_proc *kp, Task *task)
 {
-	struct passwd *pw;
 	char buf[1024], *p;
 	size_t bufsz;
 	int i, oid[4];
@@ -132,9 +130,7 @@ get_task_details (struct kinfo_proc *kp, Task *task)
 	task->cpu_system = 0.0f;
 	task->vsz = kp->ki_size;
 	task->rss = kp->ki_rssize * getpagesize ();
-	pw = getpwuid (kp->ki_uid);
 	task->uid = kp->ki_uid;
-	g_strlcpy (task->uid_name, (pw != NULL) ? pw->pw_name : "nobody", sizeof (task->uid_name));
 	task->prio = (gushort)kp->ki_nice;
 	g_strlcpy (task->name, kp->ki_comm, sizeof(task->name));
 
diff --git a/src/task-manager-linux.c b/src/task-manager-linux.c
index a62c5a0..441a234 100644
--- a/src/task-manager-linux.c
+++ b/src/task-manager-linux.c
@@ -11,7 +11,6 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <pwd.h>
 #include <unistd.h>
 #include <string.h>
 
@@ -294,7 +293,6 @@ get_task_details (GPid pid, Task *task)
 
 	/* Parse the status file: it contains the UIDs */
 	{
-		struct passwd *pw;
 		guint dummy;
 
 		snprintf(filename, sizeof (filename), "/proc/%d/status", pid);
@@ -307,9 +305,6 @@ get_task_details (GPid pid, Task *task)
 				break;
 		}
 		fclose (file);
-
-		pw = getpwuid (task->uid);
-		g_strlcpy (task->uid_name, (pw != NULL) ? pw->pw_name : "nobody", sizeof (task->uid_name));
 	}
 
 	/* Read the full command line */
diff --git a/src/task-manager-skel.c b/src/task-manager-skel.c
index f2f82a1..cfca284 100644
--- a/src/task-manager-skel.c
+++ b/src/task-manager-skel.c
@@ -11,7 +11,6 @@
 /* Add includes for system functions needed */
 /* Example:
 #include <stdio.h>
-#include <pwd.h>
 #include <unistd.h>
 #include <string.h>
 */
diff --git a/src/task-manager-solaris.c b/src/task-manager-solaris.c
index cc9defc..37279b0 100644
--- a/src/task-manager-solaris.c
+++ b/src/task-manager-solaris.c
@@ -9,7 +9,6 @@
  */
 
 #include <kstat.h>
-#include <pwd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
@@ -173,7 +172,6 @@ get_task_details (GPid pid, Task *task)
 {
 	FILE *file;
 	gchar filename[96];
-	struct passwd *pw;
 	psinfo_t process;
 
 	snprintf (filename, sizeof(filename), "/proc/%d/psinfo", pid);
@@ -195,9 +193,7 @@ get_task_details (GPid pid, Task *task)
 	task->vsz = (guint64)process.pr_size * 1024;
 	task->rss = (guint64)process.pr_rssize * 1024;
 	task->prio = (gushort)process.pr_lwp.pr_pri;
-	pw = getpwuid (process.pr_uid);
 	task->uid = (guint)process.pr_uid;
-	g_strlcpy (task->uid_name, (pw != NULL) ? pw->pw_name : "nobody", sizeof (task->uid_name));
 	get_cpu_percent (task->pid, (process.pr_time.tv_sec * 1000 + process.pr_time.tv_nsec / 100000), &task->cpu_user, 0, &task->cpu_system);
 
 	fclose (file);
diff --git a/src/task-manager.c b/src/task-manager.c
index 015112f..243f0c1 100644
--- a/src/task-manager.c
+++ b/src/task-manager.c
@@ -166,17 +166,17 @@ model_add_task (XtmTaskManager *manager, Task *task, glong timestamp)
 {
 	GtkTreeIter iter;
 	GtkTreeModel *model = manager->model;
+	gchar *uid_name = get_uid_name (task->uid);
 
 	gtk_list_store_append (GTK_LIST_STORE (model), &iter);
 	gtk_list_store_set (GTK_LIST_STORE (model), &iter,
 		XTM_PTV_COLUMN_PID, task->pid,
 		XTM_PTV_COLUMN_STATE, task->state,
 		XTM_PTV_COLUMN_UID, task->uid,
-		XTM_PTV_COLUMN_UID_STR, task->uid_name,
-		XTM_PTV_COLUMN_BACKGROUND, NULL,
-		XTM_PTV_COLUMN_FOREGROUND, NULL,
+		XTM_PTV_COLUMN_UID_STR, uid_name,
 		XTM_PTV_COLUMN_TIMESTAMP, timestamp,
 		-1);
+	g_free(uid_name);
 	model_update_tree_iter (manager, &iter, timestamp, TRUE, task);
 }
 
@@ -524,6 +524,19 @@ get_hostname (void)
 	return g_strdup_printf ("%s", hostname);
 }
 
+gchar *
+get_uid_name (guint uid)
+{
+	int error;
+	struct passwd *pw = NULL, pwd_buf;
+	char buf[4096];
+
+	bzero(buf, sizeof(buf));
+	error = getpwuid_r(uid, &pwd_buf, buf, sizeof(buf), &pw);
+
+	return (g_strdup ((0 == error && pw != NULL) ? pw->pw_name : "nobody"));
+}
+
 gboolean
 send_signal_to_pid (GPid pid, gint xtm_signal)
 {
diff --git a/src/task-manager.h b/src/task-manager.h
index b6d5052..f62f9eb 100644
--- a/src/task-manager.h
+++ b/src/task-manager.h
@@ -25,7 +25,6 @@ typedef struct _Task Task;
 struct _Task
 {
 	guint		uid;
-	gchar		uid_name[256];
 	GPid		pid;
 	GPid		ppid;
 	gchar		name[256];
@@ -94,6 +93,7 @@ enum
 
 void		get_owner_uid		(guint *owner_uid, gchar **owner_uid_name);
 gchar *		get_hostname		(void);
+gchar *		get_uid_name		(guint uid);
 gboolean	send_signal_to_pid	(GPid pid, gint xtm_signal);
 gint		task_pid_compare_fn	(gconstpointer a, gconstpointer b);
 gboolean	set_priority_to_pid	(GPid pid, gint priority);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list