]> sourceware.org Git - lvm2.git/commitdiff
Various improvements to the daemon-common code, including automated response
authorPetr Rockai <prokai@redhat.com>
Mon, 18 Jul 2011 14:46:54 +0000 (14:46 +0000)
committerPetr Rockai <prokai@redhat.com>
Mon, 18 Jul 2011 14:46:54 +0000 (14:46 +0000)
formatting from config trees provided by the daemon implementation.

daemons/common/daemon-client.h
daemons/common/daemon-server.c
daemons/common/daemon-server.h

index 124892d60fe540f23d8424b4791824a6714dc91f..adb1c96f414191ebfee3c0a32b3b2a7dcc05f82b 100644 (file)
@@ -12,6 +12,7 @@
  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include "libdevmapper.h" // for dm_list, needed by config.h
 #include "config.h" // should become part of libdevmapper later
 
 #ifndef _LVM_DAEMON_COMMON_CLIENT_H
index 27496980d1521c97d6f1ea69abfc97b5d18b895a..c1668118aeb0b43d91e8aeffde058b5cca6adf23 100644 (file)
@@ -218,6 +218,22 @@ struct thread_baton {
        client_handle client;
 };
 
+int buffer_rewrite(char **buf, const char *format, const char *string) {
+       char *old = *buf;
+       dm_asprintf(buf, format, *buf, string);
+       dm_free(old);
+       return 0;
+}
+
+int buffer_line(const char *line, void *baton) {
+       response *r = baton;
+       if (r->buffer)
+               buffer_rewrite(&r->buffer, "%s\n%s", line);
+       else
+               dm_asprintf(&r->buffer, "%s\n", line);
+       return 0;
+}
+
 void *client_thread(void *baton)
 {
        struct thread_baton *b = baton;
@@ -227,12 +243,16 @@ void *client_thread(void *baton)
                        goto fail;
 
                req.cft = create_config_tree_from_string(req.buffer);
+               if (!req.cft)
+                       fprintf(stderr, "error parsing request:\n %s\n", req.buffer);
                response res = b->s.handler(b->s, b->client, req);
-               destroy_config_tree(req.cft);
+               if (req.cft)
+                       destroy_config_tree(req.cft);
                dm_free(req.buffer);
 
                if (!res.buffer) {
-                       /* TODO fill in the buffer from res.cft */
+                       write_config_node(res.cft->root, buffer_line, &res);
+                       buffer_rewrite(&res.buffer, "%s\n\n", NULL);
                }
 
                write_buffer(b->client.socket_fd, res.buffer, strlen(res.buffer));
@@ -318,6 +338,9 @@ void daemon_start(daemon_state s)
        if (!s.foreground)
                kill(getppid(), SIGTERM);
 
+       if (s.daemon_init)
+               s.daemon_init(&s);
+
        while (!_shutdown_requested && !failed) {
                int status;
                fd_set in;
@@ -333,6 +356,9 @@ void daemon_start(daemon_state s)
        if (s.socket_fd >= 0)
                unlink(s.socket_path);
 
+       if (s.daemon_fini)
+               s.daemon_fini(&s);
+
        syslog(LOG_NOTICE, "%s shutting down", s.name);
        closelog();
        remove_lockfile(s.pidfile);
index 92f354b02f24b7aeb8cda321ddc35fae4a0c7abe..a789fe31ef1e3283e4c6c0ced683773d98b00c7f 100644 (file)
@@ -45,10 +45,14 @@ struct daemon_state;
 response daemon_reply_simple(char *id, ...);
 
 static inline int daemon_request_int(request r, const char *path, int def) {
+       if (!r.cft)
+               return def;
        return find_config_int(r.cft->root, path, def);
 }
 
 static inline const char *daemon_request_str(request r, const char *path, const char *def) {
+       if (!r.cft)
+               return def;
        return find_config_str(r.cft->root, path, def);
 }
 
@@ -77,7 +81,8 @@ typedef struct daemon_state {
        const char *socket_path;
        int log_level;
        handle_request handler;
-       int (*setup_post)(struct daemon_state *st);
+       int (*daemon_init)(struct daemon_state *st);
+       int (*daemon_fini)(struct daemon_state *st);
 
        /* Global runtime info maintained by the framework. */
        int socket_fd;
This page took 0.0420469999999999 seconds and 5 git commands to generate.