Skip to content

Commit

Permalink
Add public method to gnome-desktop-utils to return a best-guess
Browse files Browse the repository at this point in the history
session user.
  • Loading branch information
mtwebster committed May 21, 2015
1 parent 773ec9b commit afb166b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
36 changes: 4 additions & 32 deletions libcinnamon-desktop/gnome-desktop-thumbnail.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
#include <string.h>
#include <glib.h>
#include <stdio.h>
#include <pwd.h>
#include <errno.h>

#define GDK_PIXBUF_ENABLE_BACKEND
#include <gdk-pixbuf/gdk-pixbuf.h>

#define GNOME_DESKTOP_USE_UNSTABLE_API
#include "gnome-desktop-thumbnail.h"
#include "gnome-desktop-utils.h"
#include <glib/gstdio.h>
#include <libgsystem.h>

Expand Down Expand Up @@ -73,7 +73,6 @@ static const char *appname = "gnome-thumbnail-factory";

static void gnome_desktop_thumbnail_factory_init (GnomeDesktopThumbnailFactory *factory);
static void gnome_desktop_thumbnail_factory_class_init (GnomeDesktopThumbnailFactoryClass *class);
static struct passwd *get_session_user_pwent (void);

G_DEFINE_TYPE (GnomeDesktopThumbnailFactory,
gnome_desktop_thumbnail_factory,
Expand Down Expand Up @@ -822,7 +821,7 @@ get_user_info (GnomeDesktopThumbnailFactory *factory,
{
struct passwd *pwent;

pwent = get_session_user_pwent ();
pwent = gnome_desktop_get_session_user_pwent ();

*uid = pwent->pw_uid;
*gid = pwent->pw_gid;
Expand Down Expand Up @@ -1875,33 +1874,6 @@ check_subfolder_permissions_only (const gchar *path, uid_t uid, gid_t gid)
return ret;
}

static struct passwd *
get_session_user_pwent (void)
{
struct passwd *pwent = NULL;

if (getuid () != geteuid ()) {
gint uid = getuid ();
pwent = getpwuid (uid);
} else if (g_getenv ("SUDO_UID") != NULL) {
gint uid = (int) g_ascii_strtoll (g_getenv ("SUDO_UID"), NULL, 10);
pwent = getpwuid (uid);
} else if (g_getenv ("PKEXEC_UID") != NULL) {
gint uid = (int) g_ascii_strtoll (g_getenv ("PKEXEC_UID"), NULL, 10);
pwent = getpwuid (uid);
} else if (g_getenv ("USERNAME") != NULL) {
pwent = getpwnam (g_getenv ("USERNAME"));
} else if (g_getenv ("USER") != NULL) {
pwent = getpwnam (g_getenv ("USER"));
}

if (!pwent) {
return getpwuid (getuid ());
}

return pwent;
}

/**
* gnome_desktop_cache_fix_permissions:
*
Expand All @@ -1916,7 +1888,7 @@ gnome_desktop_thumbnail_cache_fix_permissions (void)
{
struct passwd *pwent;

pwent = get_session_user_pwent ();
pwent = gnome_desktop_get_session_user_pwent ();

gchar *cache_dir = g_build_filename (pwent->pw_dir, ".cache", "thumbnails", NULL);

Expand Down Expand Up @@ -1953,7 +1925,7 @@ gnome_desktop_thumbnail_cache_check_permissions (GnomeDesktopThumbnailFactory *f
gboolean checks_out = TRUE;

struct passwd *pwent;
pwent = get_session_user_pwent ();
pwent = gnome_desktop_get_session_user_pwent ();

gchar *cache_dir = g_build_filename (pwent->pw_dir, ".cache", "thumbnails", NULL);

Expand Down
40 changes: 39 additions & 1 deletion libcinnamon-desktop/gnome-desktop-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,42 @@ gnome_desktop_get_media_key_string (gint type)
g_return_val_if_fail (type >= 0 && type < G_N_ELEMENTS (media_keys), NULL);

return media_keys[type];
}
}

/**
* gnome_desktop_get_session_user_pwent: (skip)
*
* Description: Makes a best effort to retrieve the currently
* logged-in user's passwd struct (containing uid, gid, home, etc...)
* based on the process uid and various environment variables.
*
* Returns: (transfer none): the passwd struct corresponding to the
* session user (or, as a last resort, the user returned by getuid())
**/

struct passwd *
gnome_desktop_get_session_user_pwent (void)
{
struct passwd *pwent = NULL;

if (getuid () != geteuid ()) {
gint uid = getuid ();
pwent = getpwuid (uid);
} else if (g_getenv ("SUDO_UID") != NULL) {
gint uid = (int) g_ascii_strtoll (g_getenv ("SUDO_UID"), NULL, 10);
pwent = getpwuid (uid);
} else if (g_getenv ("PKEXEC_UID") != NULL) {
gint uid = (int) g_ascii_strtoll (g_getenv ("PKEXEC_UID"), NULL, 10);
pwent = getpwuid (uid);
} else if (g_getenv ("USERNAME") != NULL) {
pwent = getpwnam (g_getenv ("USERNAME"));
} else if (g_getenv ("USER") != NULL) {
pwent = getpwnam (g_getenv ("USER"));
}

if (!pwent) {
return getpwuid (getuid ());
}

return pwent;
}
2 changes: 2 additions & 0 deletions libcinnamon-desktop/gnome-desktop-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@

#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <pwd.h>

G_BEGIN_DECLS

/* prepend the terminal command to a vector */
void gnome_desktop_prepend_terminal_to_vector (int *argc, char ***argv);

const char *gnome_desktop_get_media_key_string (gint type);
struct passwd *gnome_desktop_get_session_user_pwent (void);

G_END_DECLS

Expand Down

0 comments on commit afb166b

Please sign in to comment.