]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2011-2012 Red Hat, Inc. | |
3 | * | |
4 | * This file is part of LVM2. | |
5 | * | |
6 | * This copyrighted material is made available to anyone wishing to use, | |
7 | * modify, copy, or redistribute it subject to the terms and conditions | |
8 | * of the GNU Lesser General Public License v.2.1. | |
9 | * | |
10 | * You should have received a copy of the GNU Lesser General Public License | |
11 | * along with this program; if not, write to the Free Software Foundation, | |
12 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
13 | */ | |
14 | ||
15 | #ifndef _LVM_DAEMON_COMMON_SERVER_H | |
16 | #define _LVM_DAEMON_COMMON_SERVER_H | |
17 | ||
18 | #include "daemon-client.h" | |
19 | ||
20 | typedef struct { | |
21 | int socket_fd; /* the fd we use to talk to the client */ | |
22 | pthread_t thread_id; | |
23 | char *read_buf; | |
24 | void *private; /* this holds per-client state */ | |
25 | } client_handle; | |
26 | ||
27 | typedef struct { | |
28 | struct dm_config_tree *cft; | |
29 | char *buffer; | |
30 | } request; | |
31 | ||
32 | typedef struct { | |
33 | int error; | |
34 | struct dm_config_tree *cft; | |
35 | char *buffer; | |
36 | } response; | |
37 | ||
38 | struct daemon_state; | |
39 | ||
40 | /* | |
41 | * Craft a simple reply, without the need to construct a config_tree. See | |
42 | * daemon_send_simple in daemon-client.h for the description of the parameters. | |
43 | */ | |
44 | response daemon_reply_simple(const char *id, ...); | |
45 | ||
46 | static inline int daemon_request_int(request r, const char *path, int def) { | |
47 | if (!r.cft) | |
48 | return def; | |
49 | return dm_config_find_int(r.cft->root, path, def); | |
50 | } | |
51 | ||
52 | static inline const char *daemon_request_str(request r, const char *path, const char *def) { | |
53 | if (!r.cft) | |
54 | return def; | |
55 | return dm_config_find_str(r.cft->root, path, def); | |
56 | } | |
57 | ||
58 | /* | |
59 | * The callback. Called once per request issued, in the respective client's | |
60 | * thread. It is presented by a parsed request (in the form of a config tree). | |
61 | * The output is a new config tree that is serialised and sent back to the | |
62 | * client. The client blocks until the request processing is done and reply is | |
63 | * sent. | |
64 | */ | |
65 | typedef response (*handle_request)(struct daemon_state s, client_handle h, request r); | |
66 | ||
67 | typedef struct daemon_state { | |
68 | /* | |
69 | * The maximal stack size for individual daemon threads. This is | |
70 | * essential for daemons that need to be locked into memory, since | |
71 | * pthread's default is 10M per thread. | |
72 | */ | |
73 | int thread_stack_size; | |
74 | ||
75 | /* Flags & attributes affecting the behaviour of the daemon. */ | |
76 | unsigned avoid_oom:1; | |
77 | unsigned foreground:1; | |
78 | const char *name; | |
79 | const char *pidfile; | |
80 | const char *socket_path; | |
81 | const char *protocol; | |
82 | int protocol_version; | |
83 | ||
84 | int log_level; | |
85 | handle_request handler; | |
86 | int (*daemon_init)(struct daemon_state *st); | |
87 | int (*daemon_fini)(struct daemon_state *st); | |
88 | ||
89 | /* Global runtime info maintained by the framework. */ | |
90 | int socket_fd; | |
91 | ||
92 | void *private; /* the global daemon state */ | |
93 | } daemon_state; | |
94 | ||
95 | /* | |
96 | * Start serving the requests. This does all the daemonisation, socket setup | |
97 | * work and so on. This function takes over the process, and upon failure, it | |
98 | * will terminate execution. It may be called at most once. | |
99 | */ | |
100 | void daemon_start(daemon_state s); | |
101 | ||
102 | /* | |
103 | * Take over from an already running daemon. This function handles connecting | |
104 | * to the running daemon and telling it we are going to take over. The takeover | |
105 | * request may be customised by passing in a non-NULL request. | |
106 | * | |
107 | * The takeover sequence: the old daemon stops accepting new clients, then it | |
108 | * waits until all current client connections are closed. When that happens, it | |
109 | * serializes its current state and sends that as a reply, which is then | |
110 | * returned by this function (therefore, this function won't return until the | |
111 | * previous instance has shut down). | |
112 | * | |
113 | * The daemon, after calling daemon_takeover is expected to set up its | |
114 | * daemon_state using the reply from this function and call daemon_start as | |
115 | * usual. | |
116 | */ | |
117 | daemon_reply daemon_takeover(daemon_info i, daemon_request r); | |
118 | ||
119 | /* Call this to request a clean shutdown of the daemon. Async safe. */ | |
120 | void daemon_stop(void); | |
121 | ||
122 | #endif |