2 * Copyright (C) 2009-2010 Red Hat Inc.
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (* Please read generator/README first. *)
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
31 (* Generate the protocol (XDR) file, 'guestfs_protocol.x' and
32 * indirectly 'guestfs_protocol.h' and 'guestfs_protocol.c'.
34 * We have to use an underscore instead of a dash because otherwise
35 * rpcgen generates incorrect code.
37 * This header is NOT exported to clients, but see also generate_structs_h.
40 generate_header CStyle LGPLv2plus;
42 (* This has to be defined to get around a limitation in Sun's rpcgen. *)
43 pr "typedef string guestfs_str<>;\n";
46 (* Internal structures. *)
50 pr "struct guestfs_int_%s {\n" typ;
52 | name, FChar -> pr " char %s;\n" name
53 | name, FString -> pr " string %s<>;\n" name
54 | name, FBuffer -> pr " opaque %s<>;\n" name
55 | name, FUUID -> pr " opaque %s[32];\n" name
56 | name, (FInt32|FUInt32) -> pr " int %s;\n" name
57 | name, (FInt64|FUInt64|FBytes) -> pr " hyper %s;\n" name
58 | name, FOptPercent -> pr " float %s;\n" name
62 pr "typedef struct guestfs_int_%s guestfs_int_%s_list<>;\n" typ typ;
67 fun (shortname, style, _, _, _, _, _) ->
68 let name = "guestfs_" ^ shortname in
73 pr "struct %s_args {\n" name;
76 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
77 pr " string %s<>;\n" n
78 | OptString n -> pr " guestfs_str *%s;\n" n
79 | StringList n | DeviceList n -> pr " guestfs_str %s<>;\n" n
80 | Bool n -> pr " bool %s;\n" n
81 | Int n -> pr " int %s;\n" n
82 | Int64 n -> pr " hyper %s;\n" n
84 pr " opaque %s<>;\n" n
85 | FileIn _ | FileOut _ -> ()
92 pr "struct %s_ret {\n" name;
96 pr "struct %s_ret {\n" name;
100 pr "struct %s_ret {\n" name;
103 | RConstString _ | RConstOptString _ ->
104 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
106 pr "struct %s_ret {\n" name;
107 pr " string %s<>;\n" n;
110 pr "struct %s_ret {\n" name;
111 pr " guestfs_str %s<>;\n" n;
113 | RStruct (n, typ) ->
114 pr "struct %s_ret {\n" name;
115 pr " guestfs_int_%s %s;\n" typ n;
117 | RStructList (n, typ) ->
118 pr "struct %s_ret {\n" name;
119 pr " guestfs_int_%s_list %s;\n" typ n;
122 pr "struct %s_ret {\n" name;
123 pr " guestfs_str %s<>;\n" n;
126 pr "struct %s_ret {\n" name;
127 pr " opaque %s<>;\n" n;
132 (* Table of procedure numbers. *)
133 pr "enum guestfs_procedure {\n";
135 fun (shortname, _, proc_nr, _, _, _, _) ->
136 pr " GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
138 pr " GUESTFS_PROC_NR_PROCS\n";
142 (* Having to choose a maximum message size is annoying for several
143 * reasons (it limits what we can do in the API), but it (a) makes
144 * the protocol a lot simpler, and (b) provides a bound on the size
145 * of the daemon which operates in limited memory space.
147 pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
150 (* Message header, etc. *)
152 /* The communication protocol is now documented in the guestfs(3)
156 const GUESTFS_PROGRAM = 0x2000F5F5;
157 const GUESTFS_PROTOCOL_VERSION = 2;
159 /* These constants must be larger than any possible message length. */
160 const GUESTFS_LAUNCH_FLAG = 0xf5f55ff5;
161 const GUESTFS_CANCEL_FLAG = 0xffffeeee;
162 const GUESTFS_PROGRESS_FLAG = 0xffff5555;
164 enum guestfs_message_direction {
165 GUESTFS_DIRECTION_CALL = 0, /* client -> daemon */
166 GUESTFS_DIRECTION_REPLY = 1 /* daemon -> client */
169 enum guestfs_message_status {
170 GUESTFS_STATUS_OK = 0,
171 GUESTFS_STATUS_ERROR = 1
176 pr "const GUESTFS_ERROR_LEN = %d;\n" (64 * 1024);
180 struct guestfs_message_error {
181 int linux_errno; /* Linux errno if available. */
182 string error_message<GUESTFS_ERROR_LEN>;
185 struct guestfs_message_header {
186 unsigned prog; /* GUESTFS_PROGRAM */
187 unsigned vers; /* GUESTFS_PROTOCOL_VERSION */
188 guestfs_procedure proc; /* GUESTFS_PROC_x */
189 guestfs_message_direction direction;
190 unsigned serial; /* message serial number */
191 guestfs_message_status status;
194 const GUESTFS_MAX_CHUNK_SIZE = 8192;
196 struct guestfs_chunk {
197 int cancel; /* if non-zero, transfer is cancelled */
198 /* data size is 0 bytes if the transfer has finished successfully */
199 opaque data<GUESTFS_MAX_CHUNK_SIZE>;
202 /* Progress notifications. Daemon self-limits these messages to
203 * at most one per second. The daemon can send these messages
204 * at any time, and the caller should discard unexpected messages.
205 * 'position' and 'total' have undefined units; however they may
206 * have meaning for some calls.
208 * NB. guestfs___recv_from_daemon assumes the XDR-encoded
209 * structure is 24 bytes long.
211 struct guestfs_progress {
212 guestfs_procedure proc; /* @0: GUESTFS_PROC_x */
213 unsigned serial; /* @4: message serial number */
214 unsigned hyper position; /* @8: 0 <= position <= total */
215 unsigned hyper total; /* @16: total size of operation */
216 /* @24: size of structure */