201d60ca088b898dc2d873959dc3ac06288e5e58
[libguestfs.git] / src / guestfs.h
1 /* libguestfs
2  * Copyright (C) 2009 Red Hat Inc. 
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef GUESTFS_H_
20 #define GUESTFS_H_
21
22 /* IMPORTANT NOTE!
23  * All API documentation is in the manual page --> guestfs(3) <--
24  * Go and read it now, I'll wait.
25  */
26
27 #include <rpc/xdr.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 typedef struct guestfs_h guestfs_h;
34 typedef struct guestfs_main_loop guestfs_main_loop;
35
36 /* Connection management. */
37 extern guestfs_h *guestfs_create (void);
38 extern void guestfs_close (guestfs_h *g);
39
40 /* Error handling. */
41 extern const char *guestfs_last_error (guestfs_h *g);
42
43 typedef void (*guestfs_error_handler_cb) (guestfs_h *g, void *data, const char *msg);
44 typedef void (*guestfs_abort_cb) (void);
45
46 extern void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data);
47 extern guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **data_rtn);
48
49 extern void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb);
50 extern guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g);
51
52 #include <guestfs-structs.h>
53 #include <guestfs-actions.h>
54
55 /* Free up return values. */
56 extern void guestfs_free_int_bool (struct guestfs_int_bool *);
57 extern void guestfs_free_lvm_pv_list (struct guestfs_lvm_pv_list *);
58 extern void guestfs_free_lvm_vg_list (struct guestfs_lvm_vg_list *);
59 extern void guestfs_free_lvm_lv_list (struct guestfs_lvm_lv_list *);
60 extern void guestfs_free_dirent_list (struct guestfs_dirent_list *);
61
62 /* Low-level event API. */
63 typedef void (*guestfs_send_cb) (guestfs_h *g, void *data);
64 typedef void (*guestfs_reply_cb) (guestfs_h *g, void *data, XDR *xdr);
65 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *data, char *buf, int len);
66 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *data);
67 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *data);
68
69 extern void guestfs_set_send_callback (guestfs_h *g, guestfs_send_cb cb, void *opaque);
70 extern void guestfs_set_reply_callback (guestfs_h *g, guestfs_reply_cb cb, void *opaque);
71 extern void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque);
72 extern void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque);
73 extern void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque);
74
75 extern void guestfs_error (guestfs_h *g, const char *fs, ...)
76   __attribute__((format (printf,2,3)));
77 extern void guestfs_perrorf (guestfs_h *g, const char *fs, ...)
78   __attribute__((format (printf,2,3)));
79 extern void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes);
80 extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes);
81 extern char *guestfs_safe_strdup (guestfs_h *g, const char *str);
82 extern void *guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size);
83
84 extern int guestfs__switch_to_sending (guestfs_h *g);
85 extern int guestfs__switch_to_receiving (guestfs_h *g);
86
87 /* These *_sync calls wait until the action is performed, using the
88  * main loop.  We should implement asynchronous versions too.
89  */
90 extern int guestfs__send_sync (guestfs_h *g, int proc_nr, xdrproc_t xdrp, char *args);
91 extern int guestfs__send_file_sync (guestfs_h *g, const char *filename);
92 extern int guestfs__receive_file_sync (guestfs_h *g, const char *filename);
93
94 /* Main loop. */
95 #define GUESTFS_HANDLE_READABLE 0x1
96 #define GUESTFS_HANDLE_WRITABLE 0x2
97 #define GUESTFS_HANDLE_HANGUP   0x4
98 #define GUESTFS_HANDLE_ERROR    0x8
99
100 typedef void (*guestfs_handle_event_cb) (guestfs_main_loop *ml, guestfs_h *g, void *data, int watch, int fd, int events);
101 typedef int (*guestfs_add_handle_cb) (guestfs_main_loop *ml, guestfs_h *g, int fd, int events, guestfs_handle_event_cb cb, void *data);
102 typedef int (*guestfs_remove_handle_cb) (guestfs_main_loop *ml, guestfs_h *g, int watch);
103 typedef void (*guestfs_handle_timeout_cb) (guestfs_main_loop *ml, guestfs_h *g, void *data, int timer);
104 typedef int (*guestfs_add_timeout_cb) (guestfs_main_loop *ml, guestfs_h *g, int interval, guestfs_handle_timeout_cb cb, void *data);
105 typedef int (*guestfs_remove_timeout_cb) (guestfs_main_loop *ml, guestfs_h *g, int timer);
106 typedef int (*guestfs_main_loop_run_cb) (guestfs_main_loop *ml, guestfs_h *g);
107 typedef int (*guestfs_main_loop_quit_cb) (guestfs_main_loop *ml, guestfs_h *g);
108
109 /* This is the head of the main loop structure.  Concrete implementations
110  * use additional private data after this struct.
111  */
112 struct guestfs_main_loop {
113   guestfs_add_handle_cb add_handle;
114   guestfs_remove_handle_cb remove_handle;
115   guestfs_add_timeout_cb add_timeout;
116   guestfs_remove_timeout_cb remove_timeout;
117   guestfs_main_loop_run_cb main_loop_run;
118   guestfs_main_loop_quit_cb main_loop_quit;
119 };
120
121 extern void guestfs_set_main_loop (guestfs_h *handle, guestfs_main_loop *main_loop);
122 extern guestfs_main_loop *guestfs_get_main_loop (guestfs_h *handle);
123 extern guestfs_main_loop *guestfs_get_default_main_loop (void);
124
125 extern guestfs_main_loop *guestfs_create_main_loop (void);
126 extern void guestfs_free_main_loop (guestfs_main_loop *);
127
128 #ifdef __cplusplus
129 }
130 #endif
131
132 #endif /* GUESTFS_H_ */