Move getpwuid() from backends to gui, use getpwuid_r(), call only once on task add. (bug 14401)

This commit is contained in:
rim
2018-05-29 03:11:15 +03:00
committed by Landry Breuil
parent 40a3e65803
commit 8e1b48f8de
7 changed files with 17 additions and 25 deletions

View File

@@ -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);

View File

@@ -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));

View File

@@ -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 */

View File

@@ -11,7 +11,6 @@
/* Add includes for system functions needed */
/* Example:
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
*/

View File

@@ -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);

View File

@@ -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)
{

View File

@@ -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);