ocaml: Remove old entry from .gitignore file.
[libguestfs.git] / daemon / daemon.h
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #ifndef GUESTFSD_DAEMON_H
20 #define GUESTFSD_DAEMON_H
21
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include <rpc/types.h>
28 #include <rpc/xdr.h>
29
30 #include "../src/guestfs_protocol.h"
31
32 /*-- in guestfsd.c --*/
33 extern int verbose;
34
35 extern const char *sysroot;
36 extern int sysroot_len;
37
38 extern char *sysroot_path (const char *path);
39
40 extern int is_root_device (const char *device);
41
42 extern int xwrite (int sock, const void *buf, size_t len)
43   __attribute__((__warn_unused_result__));
44 extern int xread (int sock, void *buf, size_t len)
45   __attribute__((__warn_unused_result__));
46
47 extern int add_string (char ***argv, int *size, int *alloc, const char *str);
48 extern size_t count_strings (char *const *argv);
49 extern void sort_strings (char **argv, int len);
50 extern void free_strings (char **argv);
51 extern void free_stringslen (char **argv, int len);
52
53 extern int is_power_of_2 (unsigned long v);
54
55 #define command(out,err,name,...) commandf((out),(err),0,(name),__VA_ARGS__)
56 #define commandr(out,err,name,...) commandrf((out),(err),0,(name),__VA_ARGS__)
57 #define commandv(out,err,argv) commandvf((out),(err),0,(argv))
58 #define commandrv(out,err,argv) commandrvf((out),(err),0,(argv))
59
60 #define COMMAND_FLAG_FD_MASK                   (1024-1)
61 #define COMMAND_FLAG_FOLD_STDOUT_ON_STDERR     1024
62 #define COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN 2048
63
64 extern int commandf (char **stdoutput, char **stderror, int flags,
65                      const char *name, ...);
66 extern int commandrf (char **stdoutput, char **stderror, int flags,
67                       const char *name, ...);
68 extern int commandvf (char **stdoutput, char **stderror, int flags,
69                       char const *const *argv);
70 extern int commandrvf (char **stdoutput, char **stderror, int flags,
71                        char const* const *argv);
72
73 extern char **split_lines (char *str);
74
75 extern void trim (char *str);
76
77 extern int device_name_translation (char *device);
78
79 extern int prog_exists (const char *prog);
80
81 extern void udev_settle (void);
82
83 /* This just stops gcc from giving a warning about our custom
84  * printf formatters %Q and %R.  See HACKING file for more
85  * info about these.
86  */
87 static inline int
88 asprintf_nowarn (char **strp, const char *fmt, ...)
89 {
90   int r;
91   va_list args;
92
93   va_start (args, fmt);
94   r = vasprintf (strp, fmt, args);
95   va_end (args);
96   return r;
97 }
98
99 /*-- in names.c (auto-generated) --*/
100 extern const char *function_names[];
101
102 /*-- in proto.c --*/
103 extern int proc_nr;
104 extern int serial;
105
106 /*-- in mount.c --*/
107 extern int root_mounted;
108
109 /*-- in stubs.c (auto-generated) --*/
110 extern void dispatch_incoming_message (XDR *);
111 extern guestfs_int_lvm_pv_list *parse_command_line_pvs (void);
112 extern guestfs_int_lvm_vg_list *parse_command_line_vgs (void);
113 extern guestfs_int_lvm_lv_list *parse_command_line_lvs (void);
114
115 /*-- in optgroups.c (auto-generated) --*/
116 struct optgroup {
117   const char *group;            /* Name of the optional group. */
118   int (*available) (void);      /* Function to test availability. */
119 };
120 extern struct optgroup optgroups[];
121
122 /*-- in sync.c --*/
123 /* Use this as a replacement for sync(2). */
124 extern int sync_disks (void);
125
126 /*-- in proto.c --*/
127 extern void main_loop (int sock) __attribute__((noreturn));
128
129 /* ordinary daemon functions use these to indicate errors
130  * NB: you don't need to prefix the string with the current command,
131  * it is added automatically by the client-side RPC stubs.
132  */
133 extern void reply_with_error (const char *fs, ...)
134   __attribute__((format (printf,1,2)));
135 extern void reply_with_perror_errno (int err, const char *fs, ...)
136   __attribute__((format (printf,2,3)));
137 #define reply_with_perror(...) reply_with_perror_errno(errno, __VA_ARGS__)
138
139 /* daemon functions that receive files (FileIn) should call
140  * receive_file for each FileIn parameter.
141  */
142 typedef int (*receive_cb) (void *opaque, const void *buf, size_t len);
143 extern int receive_file (receive_cb cb, void *opaque);
144
145 /* daemon functions that receive files (FileIn) can call this
146  * to cancel incoming transfers (eg. if there is a local error).
147  *
148  * If and only if this function does NOT return -2, they MUST then
149  * call reply_with_*
150  * (see https://bugzilla.redhat.com/show_bug.cgi?id=576879#c5).
151  */
152 extern int cancel_receive (void);
153
154 /* daemon functions that return files (FileOut) should call
155  * reply, then send_file_* for each FileOut parameter.
156  * Note max write size if GUESTFS_MAX_CHUNK_SIZE.
157  */
158 extern int send_file_write (const void *buf, int len);
159 extern int send_file_end (int cancel);
160
161 /* only call this if there is a FileOut parameter */
162 extern void reply (xdrproc_t xdrp, char *ret);
163
164 /* Helper for functions that need a root filesystem mounted.
165  * NB. Cannot be used for FileIn functions.
166  */
167 #define NEED_ROOT(cancel_stmt,fail_stmt)                                \
168   do {                                                                  \
169     if (!root_mounted) {                                                \
170       if ((cancel_stmt) != -2)                                          \
171         reply_with_error ("%s: you must call 'mount' first to mount the root filesystem", __func__); \
172       fail_stmt;                                                        \
173     }                                                                   \
174   }                                                                     \
175   while (0)
176
177 /* Helper for functions that need an argument ("path") that is absolute.
178  * NB. Cannot be used for FileIn functions.
179  */
180 #define ABS_PATH(path,cancel_stmt,fail_stmt)                            \
181   do {                                                                  \
182     if ((path)[0] != '/') {                                             \
183       if ((cancel_stmt) != -2)                                          \
184         reply_with_error ("%s: path must start with a / character", __func__); \
185       fail_stmt;                                                        \
186     }                                                                   \
187   } while (0)
188
189 /* All functions that need an argument that is a device or partition name
190  * must call this macro.  It checks that the device exists and does
191  * device name translation (described in the guestfs(3) manpage).
192  * Note that the "path" argument may be modified.
193  *
194  * NB. Cannot be used for FileIn functions.
195  */
196 #define RESOLVE_DEVICE(path,cancel_stmt,fail_stmt)                      \
197   do {                                                                  \
198     if (STRNEQLEN ((path), "/dev/", 5)) {                               \
199       if ((cancel_stmt) != -2)                                          \
200         reply_with_error ("%s: %s: expecting a device name", __func__, (path)); \
201       fail_stmt;                                                        \
202     }                                                                   \
203     if (is_root_device (path))                                          \
204       reply_with_error ("%s: %s: device not found", __func__, path);    \
205     if (device_name_translation ((path)) == -1) {                       \
206       int err = errno;                                                  \
207       int r = cancel_stmt;                                              \
208       errno = err;                                                      \
209       if (r != -2)                                                      \
210         reply_with_perror ("%s: %s", __func__, path);                   \
211       fail_stmt;                                                        \
212     }                                                                   \
213   } while (0)
214
215 /* Helper for functions which need either an absolute path in the
216  * mounted filesystem, OR a /dev/ device which exists.
217  *
218  * NB. Cannot be used for FileIn functions.
219  *
220  * NB #2: Functions which mix filenames and device paths should be
221  * avoided, and existing functions should be deprecated.  This is
222  * because we intend in future to make device parameters a distinct
223  * type from filenames.
224  */
225 #define REQUIRE_ROOT_OR_RESOLVE_DEVICE(path,cancel_stmt,fail_stmt)      \
226   do {                                                                  \
227     if (STREQLEN ((path), "/dev/", 5))                                  \
228       RESOLVE_DEVICE ((path), cancel_stmt, fail_stmt);                  \
229     else {                                                              \
230       NEED_ROOT (cancel_stmt, fail_stmt);                               \
231       ABS_PATH ((path), cancel_stmt, fail_stmt);                        \
232     }                                                                   \
233   } while (0)
234
235 /* NB:
236  * (1) You must match CHROOT_IN and CHROOT_OUT even along error paths.
237  * (2) You must not change directory!  cwd must always be "/", otherwise
238  *     we can't escape our own chroot.
239  * (3) All paths specified must be absolute.
240  * (4) Neither macro affects errno.
241  */
242 #define CHROOT_IN                               \
243   do {                                          \
244     int __old_errno = errno;                    \
245     if (chroot (sysroot) == -1)                 \
246       perror ("CHROOT_IN: sysroot");            \
247     errno = __old_errno;                        \
248   } while (0)
249 #define CHROOT_OUT                              \
250   do {                                          \
251     int __old_errno = errno;                    \
252     if (chroot (".") == -1)                     \
253       perror ("CHROOT_OUT: .");                 \
254     errno = __old_errno;                        \
255   } while (0)
256
257 /* Marks functions which are not implemented.
258  * NB. Cannot be used for FileIn functions.
259  */
260 #define XXX_NOT_IMPL(errcode)                                           \
261   do {                                                                  \
262     reply_with_error ("%s: function not implemented", __func__);        \
263     return (errcode);                                                   \
264   }                                                                     \
265   while (0)
266
267 /* Marks functions which are not available.
268  * NB. Cannot be used for FileIn functions.
269  */
270 #define NOT_AVAILABLE(errcode)                                          \
271   do {                                                                  \
272     reply_with_error ("%s: function not available", __func__);          \
273     return (errcode);                                                   \
274   }                                                                     \
275   while (0)
276
277 #ifndef __attribute__
278 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
279 #  define __attribute__(x) /* empty */
280 # endif
281 #endif
282
283 #ifndef ATTRIBUTE_UNUSED
284 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
285 #endif
286
287 #define STREQ(a,b) (strcmp((a),(b)) == 0)
288 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
289 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
290 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
291 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
292 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
293 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
294 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
295 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
296
297 #endif /* GUESTFSD_DAEMON_H */