summaryrefslogtreecommitdiff
path: root/src/modules/ecore/system/systemd/ecore_system_systemd.c
blob: ab19ff36cd21b38fc0019caf5e02f5b5e4fbc9e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eldbus.h>
#include <Ecore.h>
#include <locale.h>

static int _log_dom = -1;
static Eldbus_Connection *_conn = NULL;

static Eina_List *_objs = NULL;
static Eina_List *_proxies = NULL;
static Eina_List *_eldbus_pending = NULL;

#ifdef CRI
#undef CRI
#endif
#define CRI(...) EINA_LOG_DOM_CRIT(_log_dom, __VA_ARGS__)

#ifdef ERR
#undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)

#ifdef WRN
#undef WRN
#endif
#define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)

#ifdef DBG
#undef DBG
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)


static void
_props_changed_hostname(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
   Eldbus_Message_Iter *changed, *entry, *invalidated;
   const char *iface, *prop;

   if (!eldbus_message_arguments_get(msg, "sa{sv}as",
                                     &iface, &changed, &invalidated))
     {
        ERR("Error getting data from properties changed signal.");
        return;
     }

   while (eldbus_message_iter_get_and_next(changed, 'e', &entry))
     {
        const void *key;
        Eldbus_Message_Iter *var;
        if (!eldbus_message_iter_arguments_get(entry, "sv", &key, &var))
          continue;
        if (strcmp(key, "Hostname") == 0)
          goto changed_hostname;
     }

   while (eldbus_message_iter_get_and_next(invalidated, 's', &prop))
     {
        if (strcmp(prop, "Hostname") == 0)
          goto changed_hostname;
     }

   return;

 changed_hostname:
   ecore_event_add(ECORE_EVENT_HOSTNAME_CHANGED, NULL, NULL, NULL);
}

static void
_props_changed_timedate(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
   Eldbus_Message_Iter *changed, *entry, *invalidated;
   const char *iface, *prop;

   if (!eldbus_message_arguments_get(msg, "sa{sv}as",
                                     &iface, &changed, &invalidated))
     {
        ERR("Error getting data from properties changed signal.");
        return;
     }

   while (eldbus_message_iter_get_and_next(changed, 'e', &entry))
     {
        const void *key;
        Eldbus_Message_Iter *var;
        if (!eldbus_message_iter_arguments_get(entry, "sv", &key, &var))
          continue;
        if (strcmp(key, "Timezone") == 0)
          goto changed_timedate;
     }

   while (eldbus_message_iter_get_and_next(invalidated, 's', &prop))
     {
        if (strcmp(prop, "Timezone") == 0)
          goto changed_timedate;
     }

   return;

 changed_timedate:
   ecore_event_add(ECORE_EVENT_SYSTEM_TIMEDATE_CHANGED, NULL, NULL, NULL);
}

static void _locale_envs_unset(void)
{
   unsetenv("LC_CTYPE");
   unsetenv("LC_NUMERIC");
   unsetenv("LC_TIME");
   unsetenv("LC_COLLATE");
   unsetenv("LC_MONETARY");
   unsetenv("LC_MESSAGES");
   unsetenv("LC_ALL");
   unsetenv("LC_PAPER");
   unsetenv("LC_NAME");
   unsetenv("LC_ADDRESS");
   unsetenv("LC_TELEPHONE");
   unsetenv("LC_MEASUREMENT");
   unsetenv("LC_IDENTIFICATION");
}

static void _locale_get(void *data EINA_UNUSED, const Eldbus_Message *msg,
                        Eldbus_Pending *pending)
{
   Eldbus_Message_Iter *variant, *array;
   const char *errname, *errmsg, *val;

   _eldbus_pending = eina_list_remove(_eldbus_pending, pending);
   if (eldbus_message_error_get(msg, &errname, &errmsg))
     {
        ERR("Message error %s - %s", errname, errmsg);
        goto end;
     }
   if (!eldbus_message_arguments_get(msg, "v", &variant))
     {
        ERR("Error getting arguments.");
        goto end;
     }

   if (!eldbus_message_iter_get_and_next(variant, 'a', &array))
     {
        ERR("Error getting array.");
        goto end;
     }

   _locale_envs_unset();

   while (eldbus_message_iter_get_and_next(array, 's', &val))
     {
        char buf[1024], *value, *type;

        snprintf(buf, sizeof(buf), "%s", val);

        type = buf;

        value = strchr(buf, '=');
        if (!value)
          continue;
        *value = 0;
        value++;

        setenv(type, value, 1);
     }
   setlocale(LC_ALL, "");

 end:
   ecore_event_add(ECORE_EVENT_LOCALE_CHANGED, NULL, NULL, NULL);
}

static void
_props_changed_locale(void *data, const Eldbus_Message *msg)
{
   Eldbus_Proxy *proxy = data;
   Eldbus_Message_Iter *changed, *entry, *invalidated;
   const char *iface, *prop;
   Eldbus_Pending *pend;

   if (!eldbus_message_arguments_get(msg, "sa{sv}as",
                                     &iface, &changed, &invalidated))
     {
        ERR("Error getting data from properties changed signal.");
        return;
     }

   while (eldbus_message_iter_get_and_next(changed, 'e', &entry))
     {
        const void *key;
        Eldbus_Message_Iter *var;
        if (!eldbus_message_iter_arguments_get(entry, "sv", &key, &var))
          continue;
        if (strcmp(key, "Locale") == 0)
          goto changed_locale;
     }

   while (eldbus_message_iter_get_and_next(invalidated, 's', &prop))
     {
        if (strcmp(prop, "Locale") == 0)
          goto changed_locale;
     }

   return;

 changed_locale:
   pend = eldbus_proxy_property_get(proxy, "Locale", _locale_get, NULL);
   _eldbus_pending = eina_list_append(_eldbus_pending, pend);
}

static Eina_Bool
_property_change_monitor(const char *name,
                         const char *path,
                         const char *iface,
                         Eldbus_Signal_Cb cb)
{
   Eldbus_Object *o;
   Eldbus_Proxy *p;
   Eldbus_Signal_Handler *s;

   o = eldbus_object_get(_conn, name, path);
   if (!o)
     {
        ERR("could not get object name=%s, path=%s", name, path);
        return EINA_FALSE;
     }

   p = eldbus_proxy_get(o, iface);
   if (!p)
     {
        ERR("could not get proxy interface=%s, name=%s, path=%s",
            iface, name, path);
        eldbus_object_unref(o);
        return EINA_FALSE;
     }

   s = eldbus_proxy_properties_changed_callback_add(p, cb, p);
   if (!s)
     {
        ERR("could not add signal handler for properties changed for proxy "
            "interface=%s, name=%s, path=%s", iface, name, path);
        eldbus_proxy_unref(p);
        eldbus_object_unref(o);
        return EINA_FALSE;
     }

   _objs = eina_list_append(_objs, o);
   _proxies = eina_list_append(_proxies, p);
   return EINA_TRUE;
}

static void _ecore_system_systemd_shutdown(void);
static Eina_Bool _ecore_system_systemd_init(void);
static unsigned int reseting = 0;

static void
_ecore_system_systemd_reset()
{
   reseting = 1;
   _ecore_system_systemd_shutdown();
   _ecore_system_systemd_init();
   reseting = 0;
}
static Eina_Bool
_ecore_system_systemd_init(void)
{
   eldbus_init();
   if (!reseting)
     ecore_fork_reset_callback_add(_ecore_system_systemd_reset, NULL);

   _log_dom = eina_log_domain_register("ecore_system_systemd", NULL);
   if (_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register log domain: ecore_system_systemd");
        goto error;
     }

   _conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SYSTEM);

   if (!_property_change_monitor("org.freedesktop.hostname1",
                                 "/org/freedesktop/hostname1",
                                 "org.freedesktop.hostname1",
                                 _props_changed_hostname))
     goto error;

   if (!_property_change_monitor("org.freedesktop.timedate1",
                                 "/org/freedesktop/timedate1",
                                 "org.freedesktop.timedate1",
                                 _props_changed_timedate))
     goto error;

   if (!_property_change_monitor("org.freedesktop.locale1",
                                 "/org/freedesktop/locale1",
                                 "org.freedesktop.locale1",
                                 _props_changed_locale))
     goto error;

   DBG("ecore system 'systemd' loaded");
   return EINA_TRUE;

 error:
   _ecore_system_systemd_shutdown();
   return EINA_FALSE;
}

static void
_ecore_system_systemd_shutdown(void)
{
   Eldbus_Pending *pend;

   DBG("ecore system 'systemd' unloaded");
   if (!reseting)
     ecore_fork_reset_callback_del(_ecore_system_systemd_reset, NULL);

   while (_proxies)
     {
        eldbus_proxy_unref(_proxies->data);
        _proxies = eina_list_remove_list(_proxies, _proxies);
     }

   while (_objs)
     {
        eldbus_object_unref(_objs->data);
        _objs = eina_list_remove_list(_objs, _objs);
     }

   if (_conn)
     {
        eldbus_connection_unref(_conn);
        _conn = NULL;
     }

   if (_log_dom > 0)
     {
        eina_log_domain_unregister(_log_dom);
        _log_dom = -1;
     }

   EINA_LIST_FREE(_eldbus_pending, pend)
     {
        eldbus_pending_cancel(pend);
     }

   eldbus_shutdown();
}

EINA_MODULE_INIT(_ecore_system_systemd_init);
EINA_MODULE_SHUTDOWN(_ecore_system_systemd_shutdown);