Move POSIX code to task-manager.c

The functions send_signal_to_pid() and set_priority_to_pid() are using
POSIX functions and “should” be safe in task-manager.c. Still keep the
enumeration as a bridge between virtual values and real values, there is
no benefit removing them (wasting minutes) and it won't make the code
look better (or worse).

The functions have been removed from the Linux and BSD implementations.
This commit is contained in:
Mike Massonnet
2010-05-16 12:07:17 +02:00
parent 8a56b552b5
commit b961822ba4
4 changed files with 90 additions and 126 deletions

View File

@@ -17,6 +17,7 @@
*/
#include <stdlib.h>
#include <err.h>
/* for getpwuid() */
#include <sys/types.h>
#include <pwd.h>
@@ -26,9 +27,6 @@
#include <sys/sysctl.h>
/* for swapctl() */
#include <sys/swap.h>
/* for kill() */
#include <signal.h>
#include <err.h>
/* for strlcpy() */
#include <string.h>
/* for getpagesize() */
@@ -195,39 +193,3 @@ gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64
return TRUE;
}
gboolean send_signal_to_pid (guint task_id, gint signal)
{
gint ret = 0;
if(task_id > 0 && signal != 0)
ret = kill(task_id, signal);
return (ret == 0) ? TRUE : FALSE;
}
gboolean set_priority_to_pid (guint task_id, gint prio)
{
gint res;
switch (prio)
{
case XTM_PRIORITY_VERY_LOW:
prio = 15;
break;
case XTM_PRIORITY_LOW:
prio = 5;
break;
case XTM_PRIORITY_NORMAL:
prio = 0;
break;
case XTM_PRIORITY_HIGH:
prio = -5;
break;
case XTM_PRIORITY_VERY_HIGH:
prio = -15;
break;
default:
return TRUE;
}
res = setpriority (PRIO_PROCESS, task_id, prio);
return (res == 0) ? TRUE : FALSE;
}

View File

@@ -14,8 +14,6 @@
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/resource.h>
#include <glib.h>
@@ -329,58 +327,3 @@ pid_is_sleeping (guint pid)
return (state[0] == 'T') ? TRUE : FALSE;
}
gboolean
send_signal_to_pid (guint pid, gint signal)
{
gint sig;
gint res;
switch (signal)
{
case XTM_SIGNAL_TERMINATE:
sig = SIGTERM;
break;
case XTM_SIGNAL_STOP:
sig = SIGSTOP;
break;
case XTM_SIGNAL_CONTINUE:
sig = SIGCONT;
break;
case XTM_SIGNAL_KILL:
sig = SIGKILL;
break;
default:
return TRUE;
}
res = kill (pid, sig);
return (res == 0) ? TRUE : FALSE;
}
gboolean
set_priority_to_pid (guint pid, gint priority)
{
gint prio;
gint res;
switch (priority)
{
case XTM_PRIORITY_VERY_LOW:
prio = 15;
break;
case XTM_PRIORITY_LOW:
prio = 5;
break;
case XTM_PRIORITY_NORMAL:
prio = 0;
break;
case XTM_PRIORITY_HIGH:
prio = -5;
break;
case XTM_PRIORITY_VERY_HIGH:
prio = -15;
break;
default:
return TRUE;
}
res = setpriority (PRIO_PROCESS, pid, prio);
return (res == 0) ? TRUE : FALSE;
}

View File

@@ -14,6 +14,8 @@
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <signal.h>
#include <sys/resource.h>
#include <glib-object.h>
#include <glib/gi18n.h>
@@ -92,34 +94,6 @@ _xtm_task_manager_set_model (XtmTaskManager *manager, GtkTreeModel *model)
manager->model = model;
}
void
get_owner_uid (guint *owner_uid, gchar **owner_uid_name)
{
uid_t uid;
struct passwd *pw;
gchar *username = NULL;
uid = getuid ();
pw = getpwuid (uid);
username = g_strdup ((pw != NULL) ? pw->pw_name : "nobody");
*owner_uid = (guint) uid;
*owner_uid_name = username;
}
gchar *
get_hostname ()
{
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif
char hostname[HOST_NAME_MAX];
if (gethostname (hostname, HOST_NAME_MAX))
return g_strdup ("(unknown)");
return g_strdup_printf ("%s", hostname);
}
static void
model_add_task (GtkTreeModel *model, Task *task)
{
@@ -372,3 +346,88 @@ xtm_task_manager_update_model (XtmTaskManager *manager)
return;
}
void
get_owner_uid (guint *owner_uid, gchar **owner_uid_name)
{
uid_t uid;
struct passwd *pw;
gchar *username = NULL;
uid = getuid ();
pw = getpwuid (uid);
username = g_strdup ((pw != NULL) ? pw->pw_name : "nobody");
*owner_uid = (guint) uid;
*owner_uid_name = username;
}
gchar *
get_hostname ()
{
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif
char hostname[HOST_NAME_MAX];
if (gethostname (hostname, HOST_NAME_MAX))
return g_strdup ("(unknown)");
return g_strdup_printf ("%s", hostname);
}
gboolean
send_signal_to_pid (guint pid, gint signal)
{
gint sig;
gint res;
switch (signal)
{
case XTM_SIGNAL_TERMINATE:
sig = SIGTERM;
break;
case XTM_SIGNAL_STOP:
sig = SIGSTOP;
break;
case XTM_SIGNAL_CONTINUE:
sig = SIGCONT;
break;
case XTM_SIGNAL_KILL:
sig = SIGKILL;
break;
default:
return TRUE;
}
res = kill (pid, sig);
return (res == 0) ? TRUE : FALSE;
}
gboolean
set_priority_to_pid (guint pid, gint priority)
{
gint prio;
gint res;
switch (priority)
{
case XTM_PRIORITY_VERY_LOW:
prio = 15;
break;
case XTM_PRIORITY_LOW:
prio = 5;
break;
case XTM_PRIORITY_NORMAL:
prio = 0;
break;
case XTM_PRIORITY_HIGH:
prio = -5;
break;
case XTM_PRIORITY_VERY_HIGH:
prio = -15;
break;
default:
return TRUE;
}
res = setpriority (PRIO_PROCESS, pid, prio);
return (res == 0) ? TRUE : FALSE;
}

View File

@@ -67,8 +67,6 @@ gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64
gboolean get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system);
gboolean get_task_list (GArray *task_list);
gboolean pid_is_sleeping (guint pid);
gboolean send_signal_to_pid (guint pid, gint signal);
gboolean set_priority_to_pid (guint pid, gint priority);
/**
* GObject class used to update the graphical widgets.
@@ -97,5 +95,7 @@ void xtm_task_manager_update_model (XtmTaskManager *manager);
void get_owner_uid (guint *owner_uid, gchar **owner_uid_name);
gchar * get_hostname ();
gboolean send_signal_to_pid (guint pid, gint signal);
gboolean set_priority_to_pid (guint pid, gint priority);
#endif /* !TASK_MANAGER_H */