From 92f7b80096c67c2a82b9c3c297c871c117939386 Mon Sep 17 00:00:00 2001 From: Janos Kovacs Date: Wed, 2 Oct 2013 13:39:04 +0300 Subject: [PATCH] support for resource.set.appid property for streams Change-Id: I7616c9fda571331506faab2f31de53998f1c93a0 --- murphy/classify.c | 101 ++++++++++++++++++++++++++++++++++++++++++++- murphy/module-murphy-ivi.c | 1 + murphy/murphy-ivi.lua | 2 +- murphy/userdata.h | 1 + 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/murphy/classify.c b/murphy/classify.c index 170c51c..fc96cb4 100644 --- a/murphy/classify.c +++ b/murphy/classify.c @@ -41,7 +41,9 @@ ot, write to the #include "node.h" #include "utils.h" + static int pid2exe(pid_t, char *, size_t); +static char *pid2appid(pid_t, char *, size_t); void pa_classify_node_by_card(mir_node *node, pa_card *card, @@ -290,6 +292,7 @@ mir_node_type pa_classify_guess_stream_node_type(struct userdata *u, const char *role; const char *bin; char buf[4096]; + char appid[PATH_MAX]; const char *pidstr; const char *name; int pid; @@ -299,10 +302,15 @@ mir_node_type pa_classify_guess_stream_node_type(struct userdata *u, do { + if (!(pidstr = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_ID)) || + (pid = strtol(pidstr, NULL, 10)) < 2) + { + pid = 0; + } + if ((bin = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_BINARY))) { if (!strcmp(bin, "threaded-ml") || !strcmp(bin, "WebProcess")) { - if (!(pidstr = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_ID)) || - (pid = strtol(pidstr, NULL, 10)) < 2) + if (!pid) break; if (aul_app_get_appid_bypid(pid, buf, sizeof(buf)) < 0 && @@ -342,6 +350,9 @@ mir_node_type pa_classify_guess_stream_node_type(struct userdata *u, } while (0); + if (pid2appid(pid, appid, sizeof(appid))) + pa_proplist_sets(pl, PA_PROP_RESOURCE_SET_APPID, appid); + if (resdef) *resdef = map ? map->resdef : NULL; @@ -462,6 +473,92 @@ static int pid2exe(pid_t pid, char *buf, size_t len) } +static char *get_binary(pid_t pid, char *buf, size_t size) +{ + char path[128]; + ssize_t len; + + snprintf(path, sizeof(path), "/proc/%u/exe", pid); + if ((len = readlink(path, buf, size - 1)) > 0) { + buf[len] = '\0'; + return buf; + } + else + return NULL; +} + + +static char *strprev(char *point, char c, char *base) +{ + while (point > base && *point != c) + point--; + + if (*point == c) + return point; + else + return NULL; +} + + +static char *pid2appid(pid_t pid, char *buf, size_t size) +{ + char binary[PATH_MAX]; + char path[PATH_MAX], *dir, *p, *base; + int len; + + if (!pid || !get_binary(pid, binary, sizeof(binary))) + return NULL; + + strncpy(path, binary, sizeof(path) - 1); + path[sizeof(path) - 1] = '\0'; + + /* fetch basename */ + if ((p = strrchr(path, '/')) == NULL || p == path) { + strncpy(buf, binary, size - 1); + buf[size - 1] = '\0'; + return buf; + } + + base = p-- + 1; + + /* fetch ../bin/ */ + if ((p = strprev(p, '/', path)) == NULL || p == path) + goto return_base; + + if (strncmp(p + 1, "bin/", 4) != 0) + goto return_base; + else + p--; + + /* fetch dir name above bin */ + if ((dir = strprev(p, '/', path)) == NULL || dir == path) + goto return_base; + + len = p - dir; + + /* fetch 'apps' dir */ + p = dir - 1; + + if ((p = strprev(p, '/', path)) == NULL) + goto return_base; + + if (strncmp(p + 1, "apps/", 5) != 0) + goto return_base; + + if (len + 1 <= size) { + strncpy(buf, dir + 1, len); + buf[len] = '\0'; + + return buf; + } + + return_base: + strncpy(buf, base, size - 1); + buf[size - 1] = '\0'; + return buf; +} + + mir_node_type pa_classify_guess_application_class(mir_node *node) { mir_node_type class; diff --git a/murphy/module-murphy-ivi.c b/murphy/module-murphy-ivi.c index 4b54753..7a0695a 100644 --- a/murphy/module-murphy-ivi.c +++ b/murphy/module-murphy-ivi.c @@ -63,6 +63,7 @@ #include "scripting.h" #include "extapi.h" #include "murphyif.h" +#include "classify.h" #ifndef DEFAULT_CONFIG_DIR #define DEFAULT_CONFIG_DIR "/etc/pulse" diff --git a/murphy/murphy-ivi.lua b/murphy/murphy-ivi.lua index 7384118..9a42b9a 100644 --- a/murphy/murphy-ivi.lua +++ b/murphy/murphy-ivi.lua @@ -133,7 +133,7 @@ audio_resource { attributes = { role = {"media.role", mdb.string, "music"}, pid = {"application.process.id", mdb.string, ""}, - appid = {"application.process.binary", mdb.string, ""} + appid = {"resource.set.appid", mdb.string, ""} } } diff --git a/murphy/userdata.h b/murphy/userdata.h index 731e9d9..af71604 100644 --- a/murphy/userdata.h +++ b/murphy/userdata.h @@ -42,6 +42,7 @@ #define PA_PROP_NODE_TYPE "node.type" #define PA_PROP_NODE_ROLE "node.role" #define PA_PROP_RESOURCE_SET_ID "resource.set.id" +#define PA_PROP_RESOURCE_SET_APPID "resource.set.appid" #define PA_PROP_RESOURCE_PRIORITY "resource.set.priority" #define PA_PROP_RESOURCE_SET_FLAGS "resource.set.flags" #define PA_PROP_RESOURCE_AUDIO_FLAGS "resource.audio.flags" -- 2.7.4