summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Jon McCann <mccann@jhu.edu>2007-02-26 15:18:49 -0500
committerWilliam Jon McCann <mccann@jhu.edu>2007-02-26 15:18:49 -0500
commit35b022fde7836d0edb5819d4f8be29bd1b9a20d9 (patch)
tree9bbcd8afc63999cf9f079564c6a56e78fcf77f2c
parent8fa6f59953fbcae779008c7cdf12441e995b1ca3 (diff)
rename some items in proc.h api; add some more error checking
Make the proc.h API more coherent. Convert some warnings to debug statements. Fix two leaks. Add more error checking.
-rw-r--r--src/ck-manager.c104
-rw-r--r--src/proc-linux.c42
-rw-r--r--src/proc.h17
3 files changed, 99 insertions, 64 deletions
diff --git a/src/ck-manager.c b/src/ck-manager.c
index 63561b3..71734d8 100644
--- a/src/ck-manager.c
+++ b/src/ck-manager.c
@@ -300,7 +300,7 @@ get_caller_info (CkManager *manager,
G_TYPE_INVALID,
G_TYPE_UINT, calling_uid,
G_TYPE_INVALID)) {
- g_warning ("GetConnectionUnixUser() failed: %s", error->message);
+ ck_debug ("GetConnectionUnixUser() failed: %s", error->message);
g_error_free (error);
goto out;
}
@@ -310,7 +310,7 @@ get_caller_info (CkManager *manager,
G_TYPE_INVALID,
G_TYPE_UINT, calling_pid,
G_TYPE_INVALID)) {
- g_warning ("GetConnectionUnixProcessID() failed: %s", error->message);
+ ck_debug ("GetConnectionUnixProcessID() failed: %s", error->message);
g_error_free (error);
goto out;
}
@@ -452,18 +452,19 @@ create_session_for_caller (CkManager *manager,
const GPtrArray *parameters,
GError **error)
{
- char *ssid;
- proc_t *stat;
- char *cmd;
- char *xdisplay;
- char *tty;
- CkSession *session;
- CkSeat *seat;
- char *cookie;
- pid_t pid;
- uid_t uid;
- gboolean res;
- LeaderInfo *leader_info;
+ char *ssid;
+ proc_stat_t *stat;
+ char *cmd;
+ char *xdisplay;
+ char *tty;
+ CkSession *session;
+ CkSeat *seat;
+ char *cookie;
+ pid_t pid;
+ uid_t uid;
+ gboolean res;
+ LeaderInfo *leader_info;
+ GError *local_error;
res = get_caller_info (manager,
sender,
@@ -488,7 +489,7 @@ create_session_for_caller (CkManager *manager,
parameters);
if (session == NULL) {
- g_warning ("Unable to create new session");
+ ck_debug ("Unable to create new session");
g_free (cookie);
cookie = NULL;
g_set_error (error,
@@ -498,11 +499,25 @@ create_session_for_caller (CkManager *manager,
goto out;
}
- proc_stat_pid (pid, &stat);
- tty = proc_get_tty (stat);
- cmd = proc_get_cmd (stat);
+ local_error = NULL;
+ res = proc_stat_new_for_pid (pid, &stat, &local_error);
+ if (! res) {
+ g_set_error (error,
+ CK_MANAGER_ERROR,
+ CK_MANAGER_ERROR_GENERAL,
+ _("Unable to lookup information about calling process '%d'"),
+ pid);
+ if (local_error != NULL) {
+ ck_debug ("stat on pid %d failed: %s", pid, local_error->message);
+ g_error_free (local_error);
+ }
+ return FALSE;
+ }
+
+ tty = proc_stat_get_tty (stat);
+ cmd = proc_stat_get_cmd (stat);
xdisplay = NULL;
- proc_free (stat);
+ proc_stat_free (stat);
/* If the parameters are not set then try to get them */
if (parameters == NULL) {
@@ -565,14 +580,15 @@ ck_manager_get_session_for_cookie (CkManager *manager,
const char *cookie,
DBusGMethodInvocation *context)
{
- gboolean res;
- char *sender;
- uid_t calling_uid;
- pid_t calling_pid;
- proc_t *stat;
- char *ssid;
- CkSession *session;
- LeaderInfo *leader_info;
+ gboolean res;
+ char *sender;
+ uid_t calling_uid;
+ pid_t calling_pid;
+ proc_stat_t *stat;
+ char *ssid;
+ CkSession *session;
+ LeaderInfo *leader_info;
+ GError *local_error;
ssid = NULL;
@@ -594,20 +610,26 @@ ck_manager_get_session_for_cookie (CkManager *manager,
return FALSE;
}
- res = proc_stat_pid (calling_pid, &stat);
+ local_error = NULL;
+ res = proc_stat_new_for_pid (calling_pid, &stat, &local_error);
if (! res) {
GError *error;
error = g_error_new (CK_MANAGER_ERROR,
CK_MANAGER_ERROR_GENERAL,
_("Unable to lookup information about calling process '%d'"),
calling_pid);
- g_warning ("stat on pid %d failed", calling_pid);
+ if (local_error != NULL) {
+ ck_debug ("stat on pid %d failed: %s", calling_pid, local_error->message);
+ g_error_free (local_error);
+ }
+
dbus_g_method_return_error (context, error);
g_error_free (error);
return FALSE;
}
/* FIXME: should we restrict this by uid? */
+ proc_stat_free (stat);
leader_info = g_hash_table_lookup (manager->priv->leaders, cookie);
if (leader_info == NULL) {
@@ -648,7 +670,7 @@ get_cookie_for_pid (CkManager *manager,
/* FIXME: need a better way to get the cookie */
- cookie = proc_get_env (pid, "XDG_SESSION_COOKIE");
+ cookie = proc_pid_get_env (pid, "XDG_SESSION_COOKIE");
return cookie;
}
@@ -665,12 +687,13 @@ ck_manager_get_session_for_unix_process (CkManager *manager,
guint pid,
DBusGMethodInvocation *context)
{
- gboolean res;
- char *sender;
- uid_t calling_uid;
- pid_t calling_pid;
- proc_t *stat;
- char *cookie;
+ gboolean res;
+ char *sender;
+ uid_t calling_uid;
+ pid_t calling_pid;
+ proc_stat_t *stat;
+ char *cookie;
+ GError *error;
sender = dbus_g_method_get_sender (context);
@@ -690,10 +713,11 @@ ck_manager_get_session_for_unix_process (CkManager *manager,
return FALSE;
}
- res = proc_stat_pid (calling_pid, &stat);
+ error = NULL;
+ res = proc_stat_new_for_pid (calling_pid, &stat, &error);
if (! res) {
GError *error;
- g_warning ("stat on pid %d failed", calling_pid);
+ ck_debug ("stat on pid %d failed", calling_pid);
error = g_error_new (CK_MANAGER_ERROR,
CK_MANAGER_ERROR_GENERAL,
_("Unable to lookup information about calling process '%d'"),
@@ -703,6 +727,10 @@ ck_manager_get_session_for_unix_process (CkManager *manager,
return FALSE;
}
+ /* FIXME: check stuff? */
+
+ proc_stat_free (stat);
+
cookie = get_cookie_for_pid (manager, pid);
if (cookie == NULL) {
GError *error;
diff --git a/src/proc-linux.c b/src/proc-linux.c
index 997825e..080293e 100644
--- a/src/proc-linux.c
+++ b/src/proc-linux.c
@@ -30,7 +30,8 @@
#include "proc.h"
-struct _proc_t
+/* adapted from procps */
+struct _proc_stat_t
{
int pid;
int ppid; /* stat,status pid of parent process */
@@ -258,14 +259,14 @@ link_name (guint maj,
}
char *
-proc_get_cmd (proc_t *stat)
+proc_stat_get_cmd (proc_stat_t *stat)
{
return g_strdup (stat->cmd);
}
/* adapted from procps */
char *
-proc_get_tty (proc_t *stat)
+proc_stat_get_tty (proc_stat_t *stat)
{
guint dev;
char *tty;
@@ -311,8 +312,8 @@ proc_get_tty (proc_t *stat)
#define KLF "l"
/* adapted from procps */
static void
-stat2proc (const char *S,
- proc_t *P)
+stat2proc (const char *S,
+ proc_stat_t *P)
{
unsigned num;
char * tmp;
@@ -375,15 +376,16 @@ stat2proc (const char *S,
}
gboolean
-proc_stat_pid (pid_t pid,
- proc_t **stat)
+proc_stat_new_for_pid (pid_t pid,
+ proc_stat_t **stat,
+ GError **error)
{
- char *path;
- char *contents;
- gsize length;
- gboolean res;
- GError *error;
- proc_t *proc;
+ char *path;
+ char *contents;
+ gsize length;
+ gboolean res;
+ GError *local_error;
+ proc_stat_t *proc;
if (stat == NULL) {
return FALSE;
@@ -392,15 +394,19 @@ proc_stat_pid (pid_t pid,
path = g_strdup_printf ("/proc/%d/stat", pid);
contents = NULL;
+ local_error = NULL;
res = g_file_get_contents (path,
&contents,
&length,
- &error);
+ &local_error);
if (res) {
- proc = g_new0 (proc_t, 1);
+ proc = g_new0 (proc_stat_t, 1);
proc->pid = pid;
stat2proc (contents, proc);
*stat = proc;
+ } else {
+ g_propagate_error (error, local_error);
+ *stat = NULL;
}
g_free (contents);
@@ -410,14 +416,14 @@ proc_stat_pid (pid_t pid,
}
void
-proc_free (proc_t *stat)
+proc_stat_free (proc_stat_t *stat)
{
g_free (stat);
}
char *
-proc_get_env (pid_t pid,
- const char *var)
+proc_pid_get_env (pid_t pid,
+ const char *var)
{
char *path;
gboolean res;
diff --git a/src/proc.h b/src/proc.h
index fb7ee94..4ab4ef3 100644
--- a/src/proc.h
+++ b/src/proc.h
@@ -25,16 +25,17 @@
G_BEGIN_DECLS
-typedef struct _proc_t proc_t;
+typedef struct _proc_stat_t proc_stat_t;
-gboolean proc_stat_pid (pid_t pid,
- proc_t **stat);
-char *proc_get_tty (proc_t *stat);
-char *proc_get_cmd (proc_t *stat);
-void proc_free (proc_t *stat);
+gboolean proc_stat_new_for_pid (pid_t pid,
+ proc_stat_t **stat,
+ GError **error);
+char *proc_stat_get_tty (proc_stat_t *stat);
+char *proc_stat_get_cmd (proc_stat_t *stat);
+void proc_stat_free (proc_stat_t *stat);
-char *proc_get_env (pid_t pid,
- const char *var);
+char *proc_pid_get_env (pid_t pid,
+ const char *var);
G_END_DECLS