1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the settings storage
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/types.h>
36 #include "dbus/helpers.h"
37 #include "dbus/error.h"
38 #include "dbus_constants.h"
39 #include "common/catdup.h"
40 #include "common/dirhelpers.h"
42 #define STORAGE_BASE_DIR "/.ladish/conf/"
44 extern const struct dbus_interface_descriptor g_interface
;
46 static const char * g_dbus_unique_name
;
47 static dbus_object_path g_object
;
52 struct list_head siblings
;
59 struct list_head g_pairs
;
61 static bool connect_dbus(void)
65 dbus_error_init(&g_dbus_error
);
67 g_dbus_connection
= dbus_bus_get(DBUS_BUS_SESSION
, &g_dbus_error
);
68 if (dbus_error_is_set(&g_dbus_error
))
70 log_error("Failed to get bus: %s", g_dbus_error
.message
);
71 dbus_error_free(&g_dbus_error
);
75 g_dbus_unique_name
= dbus_bus_get_unique_name(g_dbus_connection
);
76 if (g_dbus_unique_name
== NULL
)
78 log_error("Failed to read unique bus name");
79 goto unref_connection
;
82 log_info("Connected to local session bus, unique name is \"%s\"", g_dbus_unique_name
);
84 ret
= dbus_bus_request_name(g_dbus_connection
, CONF_SERVICE_NAME
, DBUS_NAME_FLAG_DO_NOT_QUEUE
, &g_dbus_error
);
87 log_error("Failed to acquire bus name: %s", g_dbus_error
.message
);
88 dbus_error_free(&g_dbus_error
);
89 goto unref_connection
;
92 if (ret
== DBUS_REQUEST_NAME_REPLY_EXISTS
)
94 log_error("Requested connection name already exists");
95 goto unref_connection
;
98 g_object
= dbus_object_path_new(CONF_OBJECT_PATH
, &g_interface
, NULL
, NULL
);
101 goto unref_connection
;
104 if (!dbus_object_path_register(g_dbus_connection
, g_object
))
106 goto destroy_control_object
;
111 destroy_control_object
:
112 dbus_object_path_destroy(g_dbus_connection
, g_object
);
114 dbus_connection_unref(g_dbus_connection
);
120 static void disconnect_dbus(void)
122 dbus_object_path_destroy(g_dbus_connection
, g_object
);
123 dbus_connection_unref(g_dbus_connection
);
126 void term_signal_handler(int signum
)
128 log_info("Caught signal %d (%s), terminating", signum
, strsignal(signum
));
132 bool install_term_signal_handler(int signum
, bool ignore_if_already_ignored
)
136 sigh
= signal(signum
, term_signal_handler
);
139 log_error("signal() failed to install handler function for signal %d.", signum
);
143 if (sigh
== SIG_IGN
&& ignore_if_already_ignored
)
145 signal(SIGTERM
, SIG_IGN
);
151 int main(int argc
, char ** argv
)
155 if (getenv("HOME") == NULL
)
157 log_error("Environment variable HOME not set");
161 INIT_LIST_HEAD(&g_pairs
);
163 install_term_signal_handler(SIGTERM
, false);
164 install_term_signal_handler(SIGINT
, true);
168 log_error("Failed to connect to D-Bus");
174 dbus_connection_read_write_dispatch(g_dbus_connection
, 50);
183 static struct pair
* create_pair(const char * key
, const char * value
)
185 struct pair
* pair_ptr
;
187 pair_ptr
= malloc(sizeof(struct pair
));
188 if (pair_ptr
== NULL
)
190 log_error("malloc() failed to allocate memory for pair struct");
194 pair_ptr
->key
= strdup(key
);
195 if (pair_ptr
->key
== NULL
)
197 log_error("strdup(\"%s\") failed for key", key
);
204 pair_ptr
->value
= strdup(value
);
205 if (pair_ptr
->value
== NULL
)
207 log_error("strdup(\"%s\") failed for value", value
);
215 /* Caller will fill this shortly */
216 pair_ptr
->value
= NULL
;
219 pair_ptr
->version
= 1;
220 pair_ptr
->stored
= false;
222 list_add_tail(&pair_ptr
->siblings
, &g_pairs
);
227 static bool store_pair(struct pair
* pair_ptr
)
235 dirpath
= catdupv(getenv("HOME"), STORAGE_BASE_DIR
, pair_ptr
->key
, NULL
);
241 if (!ensure_dir_exist(dirpath
, 0700))
247 filepath
= catdup(dirpath
, "/value");
249 if (filepath
== NULL
)
254 fd
= creat(filepath
, 0700);
257 log_error("Failed to create \"%s\": %d (%s)", filepath
, errno
, strerror(errno
));
262 len
= strlen(pair_ptr
->value
);
264 written
= write(fd
, pair_ptr
->value
, len
);
267 log_error("Failed to write() to \"%s\": %d (%s)", filepath
, errno
, strerror(errno
));
272 if ((size_t)written
!= len
)
274 log_error("write() to \"%s\" returned %zd instead of %zu", filepath
, written
, len
);
282 pair_ptr
->stored
= true;
287 static struct pair
* load_pair(const char * key
)
289 struct pair
* pair_ptr
;
296 path
= catdupv(getenv("HOME"), STORAGE_BASE_DIR
, key
, "/value", NULL
);
302 if (stat(path
, &st
) != 0)
304 log_error("Failed to stat \"%s\": %d (%s)", path
, errno
, strerror(errno
));
309 if (!S_ISREG(st
.st_mode
))
311 log_error("\"%s\" is not a regular file.", path
);
316 fd
= open(path
, O_RDONLY
);
319 log_error("Failed to open \"%s\": %d (%s)", path
, errno
, strerror(errno
));
324 buffer
= malloc((size_t)st
.st_size
+ 1);
327 log_error("malloc() failed to allocate %zu bytes of memory for value", (size_t)st
.st_size
+ 1);
333 bytes_read
= read(fd
, buffer
, st
.st_size
);
336 log_error("Failed to read() from \"%s\": %d (%s)", path
, errno
, strerror(errno
));
343 if (bytes_read
!= st
.st_size
)
345 log_error("read() from \"%s\" returned %zd instead of %llu", path
, bytes_read
, (unsigned long long)st
.st_size
);
352 buffer
[st
.st_size
] = 0;
354 pair_ptr
= create_pair(key
, NULL
);
355 if (pair_ptr
== NULL
)
363 pair_ptr
->value
= buffer
;
371 static struct pair
* find_pair(const char * key
)
373 struct list_head
* node_ptr
;
374 struct pair
* pair_ptr
;
376 list_for_each(node_ptr
, &g_pairs
)
378 pair_ptr
= list_entry(node_ptr
, struct pair
, siblings
);
379 if (strcmp(pair_ptr
->key
, key
) == 0)
388 static void emit_changed(struct pair
* pair_ptr
)
401 /***************************************************************************/
402 /* D-Bus interface implementation */
404 static void conf_set(struct dbus_method_call
* call_ptr
)
408 struct pair
* pair_ptr
;
412 if (!dbus_message_get_args(
415 DBUS_TYPE_STRING
, &key
,
416 DBUS_TYPE_STRING
, &value
,
419 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\": %s", call_ptr
->method_name
, g_dbus_error
.message
);
420 dbus_error_free(&g_dbus_error
);
424 log_info("set '%s' <- '%s'", key
, value
);
426 pair_ptr
= find_pair(key
);
427 if (pair_ptr
== NULL
)
429 pair_ptr
= create_pair(key
, value
);
430 if (pair_ptr
== NULL
)
432 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "Memory allocation failed");
436 emit_changed(pair_ptr
);
442 store
= strcmp(pair_ptr
->value
, value
) != 0;
445 buffer
= strdup(value
);
448 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "Memory allocation failed. strdup(\"%s\") failed for value", value
);
451 free(pair_ptr
->value
);
452 pair_ptr
->value
= buffer
;
454 pair_ptr
->stored
= false; /* mark that new value was not stored on disk yet */
456 emit_changed(pair_ptr
);
458 else if (!pair_ptr
->stored
) /* if store to disk failed last time, retry */
466 if (!store_pair(pair_ptr
))
468 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "Storing the value of key '%s' to disk failed", pair_ptr
->key
);
473 method_return_new_single(call_ptr
, DBUS_TYPE_UINT64
, &pair_ptr
->version
);
476 static void conf_get(struct dbus_method_call
* call_ptr
)
479 struct pair
* pair_ptr
;
481 if (!dbus_message_get_args(
484 DBUS_TYPE_STRING
, &key
,
487 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\": %s", call_ptr
->method_name
, g_dbus_error
.message
);
488 dbus_error_free(&g_dbus_error
);
492 pair_ptr
= find_pair(key
);
493 if (pair_ptr
== NULL
)
495 pair_ptr
= load_pair(key
);
496 if (pair_ptr
== NULL
)
498 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_KEY_NOT_FOUND
, "Key '%s' not found", key
);
503 log_info("get '%s' -> '%s'", key
, pair_ptr
->value
);
505 method_return_new_valist(
507 DBUS_TYPE_STRING
, &pair_ptr
->value
,
508 DBUS_TYPE_UINT64
, &pair_ptr
->version
,
512 static void conf_exit(struct dbus_method_call
* call_ptr
)
514 log_info("Exit command received through D-Bus");
516 method_return_new_void(call_ptr
);
519 METHOD_ARGS_BEGIN(set
, "Set conf value")
520 METHOD_ARG_DESCRIBE_IN("key", DBUS_TYPE_STRING_AS_STRING
, "")
521 METHOD_ARG_DESCRIBE_IN("value", DBUS_TYPE_STRING_AS_STRING
, "")
522 METHOD_ARG_DESCRIBE_OUT("version", DBUS_TYPE_UINT64_AS_STRING
, "")
525 METHOD_ARGS_BEGIN(get
, "Get conf value")
526 METHOD_ARG_DESCRIBE_IN("key", DBUS_TYPE_STRING_AS_STRING
, "")
527 METHOD_ARG_DESCRIBE_OUT("value", DBUS_TYPE_STRING_AS_STRING
, "")
528 METHOD_ARG_DESCRIBE_OUT("version", DBUS_TYPE_UINT64_AS_STRING
, "")
531 METHOD_ARGS_BEGIN(exit
, "Tell conf D-Bus service to exit")
535 METHOD_DESCRIBE(set
, conf_set
)
536 METHOD_DESCRIBE(get
, conf_get
)
537 METHOD_DESCRIBE(exit
, conf_exit
)
540 SIGNAL_ARGS_BEGIN(changed
, "")
541 SIGNAL_ARG_DESCRIBE("key", DBUS_TYPE_STRING_AS_STRING
, "")
542 SIGNAL_ARG_DESCRIBE("value", DBUS_TYPE_STRING_AS_STRING
, "")
543 SIGNAL_ARG_DESCRIBE("version", DBUS_TYPE_UINT64_AS_STRING
, "")
547 SIGNAL_DESCRIBE(changed
)
550 INTERFACE_BEGIN(g_interface
, CONF_IFACE
)
551 INTERFACE_DEFAULT_HANDLER
552 INTERFACE_EXPOSE_METHODS
553 INTERFACE_EXPOSE_SIGNALS