i18n: remove unneeded files from POTFILES.in
[platform/upstream/pulseaudio.git] / src / modules / module-systemd-login.c
1 /***
2     This file is part of PulseAudio.
3
4     Copyright 2012 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 published
8     by the Free Software Foundation; either version 2.1 of the License,
9     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     General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public License
17     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 <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32
33 #include <systemd/sd-login.h>
34
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/module.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/hashmap.h>
40 #include <pulsecore/idxset.h>
41 #include <pulsecore/modargs.h>
42
43 #include "module-systemd-login-symdef.h"
44
45 PA_MODULE_AUTHOR("Lennart Poettering");
46 PA_MODULE_DESCRIPTION("Create a client for each login session of this user");
47 PA_MODULE_VERSION(PACKAGE_VERSION);
48 PA_MODULE_LOAD_ONCE(true);
49
50 static const char* const valid_modargs[] = {
51     NULL
52 };
53
54 struct session {
55     char *id;
56     pa_client *client;
57 };
58
59 struct userdata {
60     pa_module *module;
61     pa_core *core;
62     pa_hashmap *sessions, *previous_sessions;
63     sd_login_monitor *monitor;
64     pa_io_event *io;
65 };
66
67 static int add_session(struct userdata *u, const char *id) {
68     struct session *session;
69     pa_client_new_data data;
70
71     session = pa_xnew(struct session, 1);
72     session->id = pa_xstrdup(id);
73
74     pa_client_new_data_init(&data);
75     data.module = u->module;
76     data.driver = __FILE__;
77     pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "Login Session %s", id);
78     pa_proplist_sets(data.proplist, "systemd-login.session", id);
79     session->client = pa_client_new(u->core, &data);
80     pa_client_new_data_done(&data);
81
82     if (!session->client) {
83         pa_xfree(session->id);
84         pa_xfree(session);
85         return -1;
86     }
87
88     pa_hashmap_put(u->sessions, session->id, session);
89
90     pa_log_debug("Added new session %s", id);
91     return 0;
92 }
93
94 static void free_session(struct session *session) {
95     pa_assert(session);
96
97     pa_log_debug("Removing session %s", session->id);
98
99     pa_client_free(session->client);
100     pa_xfree(session->id);
101     pa_xfree(session);
102 }
103
104 static int get_session_list(struct userdata *u) {
105     int r;
106     char **sessions;
107     pa_hashmap *h;
108     struct session *o;
109
110     pa_assert(u);
111
112     r = sd_uid_get_sessions(getuid(), 0, &sessions);
113     if (r < 0)
114         return -1;
115
116     /* We copy all sessions that still exist from one hashmap to the
117      * other and then flush the remaining ones */
118
119     h = u->previous_sessions;
120     u->previous_sessions = u->sessions;
121     u->sessions = h;
122
123     if (sessions) {
124         char **s;
125
126         /* Note that the sessions array is allocated with libc's
127          * malloc()/free() calls, hence do not use pa_xfree() to free
128          * this here. */
129
130         for (s = sessions; *s; s++) {
131             o = pa_hashmap_remove(u->previous_sessions, *s);
132             if (o)
133                 pa_hashmap_put(u->sessions, o->id, o);
134             else
135                 add_session(u, *s);
136
137             free(*s);
138         }
139
140         free(sessions);
141     }
142
143     pa_hashmap_remove_all(u->previous_sessions, (pa_free_cb_t) free_session);
144
145     return 0;
146 }
147
148 static void monitor_cb(
149         pa_mainloop_api*a,
150         pa_io_event* e,
151         int fd,
152         pa_io_event_flags_t events,
153         void *userdata) {
154
155     struct userdata *u = userdata;
156
157     pa_assert(u);
158
159     sd_login_monitor_flush(u->monitor);
160     get_session_list(u);
161 }
162
163 int pa__init(pa_module *m) {
164     struct userdata *u = NULL;
165     pa_modargs *ma;
166     sd_login_monitor *monitor = NULL;
167     int r;
168
169     pa_assert(m);
170
171     /* If we are not actually running logind become a NOP */
172     if (access("/run/systemd/seats/", F_OK) < 0)
173         return 0;
174
175     ma = pa_modargs_new(m->argument, valid_modargs);
176     if (!ma) {
177         pa_log("Failed to parse module arguments");
178         goto fail;
179     }
180
181     r = sd_login_monitor_new("session", &monitor);
182     if (r < 0) {
183         pa_log("Failed to create session monitor: %s", strerror(-r));
184         goto fail;
185     }
186
187     m->userdata = u = pa_xnew0(struct userdata, 1);
188     u->core = m->core;
189     u->module = m;
190     u->sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
191     u->previous_sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
192     u->monitor = monitor;
193
194     u->io = u->core->mainloop->io_new(u->core->mainloop, sd_login_monitor_get_fd(monitor), PA_IO_EVENT_INPUT, monitor_cb, u);
195
196     if (get_session_list(u) < 0)
197         goto fail;
198
199     pa_modargs_free(ma);
200
201     return 0;
202
203 fail:
204     if (ma)
205         pa_modargs_free(ma);
206
207     pa__done(m);
208
209     return -1;
210 }
211
212 void pa__done(pa_module *m) {
213     struct userdata *u;
214
215     pa_assert(m);
216
217     u = m->userdata;
218     if (!u)
219         return;
220
221     if (u->sessions) {
222         pa_hashmap_free(u->sessions, (pa_free_cb_t) free_session);
223         pa_hashmap_free(u->previous_sessions, (pa_free_cb_t) free_session);
224     }
225
226     if (u->io)
227         m->core->mainloop->io_free(u->io);
228
229     if (u->monitor)
230         sd_login_monitor_unref(u->monitor);
231
232     pa_xfree(u);
233 }