#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <murphy/common.h>
#define AUL_APPLICATION_TABLE_NAME "aul_applications"
#define DB_BUF_SIZE 1024
+#define MAX_CMD_BUFSZ 1024
+
+#define DEFAULT_USER "app"
+#define DEFAULT_UID 5000
/* IMPORTANT: You need to have glib plugin loaded in order for this to work! */
return 0;
}
-static ail_cb_ret_e handle_appinfo(const ail_appinfo_h appinfo, void *user_data)
+static ail_cb_ret_e handle_appinfo(const ail_appinfo_h appinfo,
+ void *user_data, uid_t uid)
{
char *val = NULL;
char **str = user_data;
+ MRP_UNUSED(uid);
ail_appinfo_get_str(appinfo, AIL_PROP_CATEGORIES_STR, &val);
return AIL_CB_RET_CANCEL;
}
-static char *get_category(const char *appid)
+static char *get_category(const char *appid, uid_t uid)
{
ail_error_e e;
char *value = NULL;
if (e != AIL_ERROR_OK)
goto end;
- e = ail_filter_list_appinfo_foreach(f, handle_appinfo, &value);
+ e = ail_filter_list_usr_appinfo_foreach(f, handle_appinfo, &value, uid);
if (e != AIL_ERROR_OK)
goto end;
return value;
}
+uid_t get_usr_by_pid(int pid)
+{
+ char buf[MAX_CMD_BUFSZ];
+ int ret;
+ uid_t uid;
+ struct stat dirstat;
+ snprintf(buf, sizeof(buf), "/proc/%d", pid);
+ ret = stat(buf, &dirstat);
+ if (ret < 0)
+ uid = (uid_t)-1;
+ else
+ uid = dirstat.st_uid;
+ return uid;
+}
+
+
+static int get_default_usr(void)
+{
+ static uid_t uid = 0;
+ struct passwd *pwd;
+
+ if (uid != 0)
+ return uid;
+
+ if ((pwd = getpwnam(DEFAULT_USER)) != NULL)
+ uid = pwd->pw_uid;
+ else
+ uid = DEFAULT_UID;
+
+ mrp_log_info("User '%s' resolved to UID %d.", DEFAULT_USER, uid);
+
+ return uid;
+}
+
+
static int aul_launch_signal(int pid, void *user_data)
{
char appid[512];
char *category;
+ uid_t uid;
MRP_UNUSED(user_data);
mrp_log_info("tracker: launched app %i", pid);
- if (aul_app_get_appid_bypid(pid, appid, 511) < 0) {
+ if (aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1) < 0) {
appid[0] = '\0';
}
- category = get_category(appid);
+ uid = get_usr_by_pid(pid);
+ if(uid == (uid_t)-1) {
+ uid = get_default_usr();
+ mrp_log_info("Unable to get launching user for process %i, "
+ "falling back to '%s' (%d).", pid, DEFAULT_USER, uid);
+ }
+ category = get_category(appid, uid);
add_app_to_db(appid, pid, category);
static int aul_iter_app_info(const aul_app_info *ai, void *user_data)
{
char *category;
-
+ uid_t uid;
MRP_UNUSED(user_data);
mrp_log_info("tracker: app info pid %i, appid: %s, pkg_name %s", ai->pid,
ai->appid ? ai->appid : "NULL", ai->pkg_name);
- category = get_category(ai->appid);
+ uid = get_usr_by_pid(ai->pid);
+ if(uid == (uid_t)-1) {
+ uid = get_default_usr();
+ mrp_log_info("Unable to get launching user for process %i, "
+ "falling back to '%s' (%d).", ai->pid, DEFAULT_USER, uid);
+ }
+ category = get_category(ai->appid, uid);
add_app_to_db(ai->appid, ai->pid, category);