]> code.delx.au - pulseaudio/blob - src/pulsecore/proplist-util.c
allow overwriting of process properties with environment variables
[pulseaudio] / src / pulsecore / proplist-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <locale.h>
28 #include <dlfcn.h>
29
30 #include <pulse/proplist.h>
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/util.h>
34
35 #include <pulsecore/core-util.h>
36
37 #include "proplist-util.h"
38
39 void pa_init_proplist(pa_proplist *p) {
40 #if !HAVE_DECL_ENVIRON
41 extern char **environ;
42 #endif
43 char **e;
44 const char *pp;
45
46 pa_assert(p);
47
48 if (environ) {
49
50 /* Some applications seem to reset environ to NULL for various
51 * reasons, hence we need to check for this explicitly. See
52 * rhbz #473080 */
53
54 for (e = environ; *e; e++) {
55
56 if (pa_startswith(*e, "PULSE_PROP_")) {
57 size_t kl = strcspn(*e+11, "=");
58 char *k;
59
60 if ((*e)[11+kl] != '=')
61 continue;
62
63 if (!pa_utf8_valid(*e+11+kl+1))
64 continue;
65
66 k = pa_xstrndup(*e+11, kl);
67
68 pa_proplist_sets(p, k, *e+11+kl+1);
69 pa_xfree(k);
70 }
71 }
72 }
73
74 if ((pp = getenv("PULSE_PROP"))) {
75 pa_proplist *t;
76
77 if ((t = pa_proplist_from_string(pp))) {
78 pa_proplist_update(p, PA_UPDATE_REPLACE, t);
79 pa_proplist_free(t);
80 }
81 }
82
83 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_ID)) {
84 char t[32];
85 pa_snprintf(t, sizeof(t), "%lu", (unsigned long) getpid());
86 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_ID, t);
87 }
88
89 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_USER)) {
90 char t[64];
91 if (pa_get_user_name(t, sizeof(t))) {
92 char *c = pa_utf8_filter(t);
93 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_USER, c);
94 pa_xfree(c);
95 }
96 }
97
98 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_HOST)) {
99 char t[64];
100 if (pa_get_host_name(t, sizeof(t))) {
101 char *c = pa_utf8_filter(t);
102 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_HOST, c);
103 pa_xfree(c);
104 }
105 }
106
107 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
108 char t[PATH_MAX];
109 if (pa_get_binary_name(t, sizeof(t))) {
110 char *c = pa_utf8_filter(t);
111 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
112 pa_xfree(c);
113 }
114 }
115
116 #ifdef RTLD_NOLOAD
117 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME)) {
118 void *dl;
119
120 if ((dl = dlopen("libglib-2.0", RTLD_NOLOAD))) {
121 const char *(*_g_get_application_name)(void);
122
123 if ((*(void**) &_g_get_application_name = dlsym(dl, "g_get_application_name")))
124 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, _g_get_application_name());
125
126 dlclose(dl);
127 }
128 }
129 #endif
130
131 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME)) {
132 const char *t;
133
134 if ((t = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
135 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
136 }
137
138 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_LANGUAGE)) {
139 const char *l;
140
141 if ((l = setlocale(LC_MESSAGES, NULL)))
142 pa_proplist_sets(p, PA_PROP_APPLICATION_LANGUAGE, l);
143 }
144
145 if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY)) {
146 const char *t;
147
148 if ((t = getenv("DISPLAY"))) {
149 char *c = pa_utf8_filter(t);
150 pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, c);
151 pa_xfree(c);
152 }
153 }
154
155 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID)) {
156 char *m;
157
158 if ((m = pa_machine_id())) {
159 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID, m);
160 pa_xfree(m);
161 }
162 }
163 }