]>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
13 | */ | |
14 | ||
15 | #ifndef _LVM_DAEMON_SERVER_H | |
16 | #define _LVM_DAEMON_SERVER_H | |
17 | ||
18 | #include "libdaemon/client/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 | struct buffer buffer; | |
30 | } request; | |
31 | ||
32 | typedef struct { | |
33 | int error; | |
34 | struct dm_config_tree *cft; | |
35 | struct buffer buffer; | |
36 | } response; | |
37 | ||
38 | struct timeval; | |
39 | ||
40 | /* | |
41 | * is_idle: daemon implementation sets it to true when no background task | |
42 | * is running | |
43 | * max_timeouts: how many seconds do daemon allow to be idle before it shutdowns | |
44 | * ptimeout: internal variable passed to select(). has to be reset to 1 second | |
45 | * before each select | |
46 | */ | |
47 | typedef struct { | |
48 | volatile unsigned is_idle; | |
49 | unsigned max_timeouts; | |
50 | struct timespec *ptimeout; | |
51 | } daemon_idle; | |
52 | ||
53 | struct daemon_state; | |
54 | ||
55 | /* | |
56 | * Craft a simple reply, without the need to construct a config_tree. See | |
57 | * daemon_send_simple in daemon-client.h for the description of the parameters. | |
58 | */ | |
59 | response daemon_reply_simple(const char *id, ...); | |
60 | ||
61 | static inline int daemon_request_int(request r, const char *path, int def) { | |
62 | if (!r.cft) | |
63 | return def; | |
64 | return dm_config_find_int(r.cft->root, path, def); | |
65 | } | |
66 | ||
67 | static inline const char *daemon_request_str(request r, const char *path, const char *def) { | |
68 | if (!r.cft) | |
69 | return def; | |
70 | return dm_config_find_str(r.cft->root, path, def); | |
71 | } | |
72 | ||
73 | /* | |
74 | * The callback. Called once per request issued, in the respective client's | |
75 | * thread. It is presented by a parsed request (in the form of a config tree). | |
76 | * The output is a new config tree that is serialised and sent back to the | |
77 | * client. The client blocks until the request processing is done and reply is | |
78 | * sent. | |
79 | */ | |
80 | typedef response (*handle_request)(struct daemon_state s, client_handle h, request r); | |
81 | ||
82 | typedef struct { | |
83 | uint32_t log_config[32]; | |
84 | void *backend_state[32]; | |
85 | const char *name; | |
86 | } log_state; | |
87 | ||
88 | struct thread_state; | |
89 | ||
90 | typedef struct daemon_state { | |
91 | /* | |
92 | * The maximal stack size for individual daemon threads. This is | |
93 | * essential for daemons that need to be locked into memory, since | |
94 | * pthread's default is 10M per thread. | |
95 | */ | |
96 | int thread_stack_size; | |
97 | ||
98 | /* Flags & attributes affecting the behaviour of the daemon. */ | |
99 | unsigned avoid_oom:1; | |
100 | unsigned foreground:1; | |
101 | const char *name; | |
102 | const char *pidfile; | |
103 | const char *socket_path; | |
104 | const char *protocol; | |
105 | int protocol_version; | |
106 | ||
107 | handle_request handler; | |
108 | int (*daemon_init)(struct daemon_state *st); | |
109 | int (*daemon_fini)(struct daemon_state *st); | |
110 | int (*daemon_main)(struct daemon_state *st); | |
111 | ||
112 | /* Global runtime info maintained by the framework. */ | |
113 | int socket_fd; | |
114 | ||
115 | log_state *log; | |
116 | struct thread_state *threads; | |
117 | ||
118 | /* support for shutdown on idle */ | |
119 | daemon_idle *idle; | |
120 | ||
121 | void *private; /* the global daemon state */ | |
122 | } daemon_state; | |
123 | ||
124 | typedef struct thread_state { | |
125 | daemon_state s; | |
126 | client_handle client; | |
127 | struct thread_state *next; | |
128 | volatile int active; | |
129 | } thread_state; | |
130 | ||
131 | /* | |
132 | * Start serving the requests. This does all the daemonisation, socket setup | |
133 | * work and so on. This function takes over the process, and upon failure, it | |
134 | * will terminate execution. It may be called at most once. | |
135 | */ | |
136 | void daemon_start(daemon_state s); | |
137 | ||
138 | /* | |
139 | * Take over from an already running daemon. This function handles connecting | |
140 | * to the running daemon and telling it we are going to take over. The takeover | |
141 | * request may be customised by passing in a non-NULL request. | |
142 | * | |
143 | * The takeover sequence: the old daemon stops accepting new clients, then it | |
144 | * waits until all current client connections are closed. When that happens, it | |
145 | * serializes its current state and sends that as a reply, which is then | |
146 | * returned by this function (therefore, this function won't return until the | |
147 | * previous instance has shut down). | |
148 | * | |
149 | * The daemon, after calling daemon_takeover is expected to set up its | |
150 | * daemon_state using the reply from this function and call daemon_start as | |
151 | * usual. | |
152 | */ | |
153 | daemon_reply daemon_takeover(daemon_info i, daemon_request r); | |
154 | ||
155 | /* Call this to request a clean shutdown of the daemon. Async safe. */ | |
156 | void daemon_stop(void); | |
157 | ||
158 | enum { DAEMON_LOG_OUTLET_SYSLOG = 1, | |
159 | DAEMON_LOG_OUTLET_STDERR = 2, | |
160 | DAEMON_LOG_OUTLET_SOCKET = 4 }; | |
161 | ||
162 | /* Log a message of a given type. */ | |
163 | void daemon_log(log_state *s, int type, const char *message); | |
164 | ||
165 | /* Log a config (sub)tree, using a given message type, each line prefixed with "prefix". */ | |
166 | void daemon_log_cft(log_state *s, int type, const char *prefix, | |
167 | const struct dm_config_node *n); | |
168 | ||
169 | /* Log a multi-line block, prefixing each line with "prefix". */ | |
170 | void daemon_log_multi(log_state *s, int type, const char *prefix, const char *message); | |
171 | ||
172 | /* Log a formatted message as "type". See also daemon-log.h. */ | |
173 | void daemon_logf(log_state *s, int type, const char *format, ...) | |
174 | __attribute__ ((format(printf, 3, 4))); | |
175 | ||
176 | /* | |
177 | * Configure log_state to send messages of type "type" to the log outlet | |
178 | * "outlet", iff "enable" is true. | |
179 | */ | |
180 | void daemon_log_enable(log_state *s, int outlet, int type, int enable); | |
181 | ||
182 | /* | |
183 | * Set up logging on a given outlet using a list of message types (comma | |
184 | * separated) to log using that outlet. The list is expected to look like this, | |
185 | * "all,wire,debug". Returns 0 upon encountering an unknown message type. | |
186 | */ | |
187 | int daemon_log_parse(log_state *s, int outlet, const char *types, int enable); | |
188 | ||
189 | #endif |