1 #!/usr/bin/ocamlrun ocaml
3 * Copyright (C) 2009 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * This script generates a large amount of code and documentation for
20 * all the daemon actions. To add a new action there are only two
21 * files you need to change, this one to describe the interface, and
22 * daemon/<somefile>.c to write the implementation.
29 type style = ret * args
31 (* "Err" as a return value means an int used as a simple error
32 * indication, ie. 0 or -1.
36 (* 0 arguments, 1 argument, etc. The guestfs_h param is implicit. *)
41 | String of string (* const char *name, cannot be NULL *)
44 ("mount", (Err, P2 (String "device", String "mountpoint")), 1,
45 "Mount a guest disk at a position in the filesystem",
47 Mount a guest disk at a position in the filesystem. Block devices
48 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
49 the guest. If those block devices contain partitions, they will have
50 the usual names (eg. C</dev/sda1>). Also LVM C</dev/VG/LV>-style
53 The rules are the same as for L<mount(2)>: A filesystem must
54 first be mounted on C</> before others can be mounted. Other
55 filesystems can only be mounted on directories which already
58 ("sync", (Err, P0), 2,
59 "Sync disks, writes are flushed through to the disk image",
61 This syncs the disk, so that any writes are flushed through to the
62 underlying disk image.
64 You should always call this if you have modified a disk image, before
65 calling C<guestfs_close>.");
67 ("touch", (Err, P1 (String "path")), 3,
68 "Update file timestamps or create a new file",
70 Touch acts like the L<touch(1)> command. It can be used to
71 update the filesystems on a file, or, if the file does not exist,
72 to create a new zero-length file.");
75 (* 'pr' prints to the current output file. *)
77 let pr fs = ksprintf (output_string !chan) fs
79 let iter_args f = function
82 | P2 (arg1, arg2) -> f arg1; f arg2
84 type comment_style = CStyle | HashStyle | OCamlStyle
85 type license = GPLv2 | LGPLv2
87 (* Generate a header block in a number of standard styles. *)
88 let rec generate_header comment license =
89 let c = match comment with
90 | CStyle -> pr "/* "; " *"
91 | HashStyle -> pr "# "; "#"
92 | OCamlStyle -> pr "(* "; " *" in
93 pr "libguestfs generated file\n";
94 pr "%s WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.\n" c;
95 pr "%s ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.\n" c;
97 pr "%s Copyright (C) 2009 Red Hat Inc.\n" c;
101 pr "%s This program is free software; you can redistribute it and/or modify\n" c;
102 pr "%s it under the terms of the GNU General Public License as published by\n" c;
103 pr "%s the Free Software Foundation; either version 2 of the License, or\n" c;
104 pr "%s (at your option) any later version.\n" c;
106 pr "%s This program is distributed in the hope that it will be useful,\n" c;
107 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
108 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" c;
109 pr "%s GNU General Public License for more details.\n" c;
111 pr "%s You should have received a copy of the GNU General Public License along\n" c;
112 pr "%s with this program; if not, write to the Free Software Foundation, Inc.,\n" c;
113 pr "%s 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n" c;
116 pr "%s This library is free software; you can redistribute it and/or\n" c;
117 pr "%s modify it under the terms of the GNU Lesser General Public\n" c;
118 pr "%s License as published by the Free Software Foundation; either\n" c;
119 pr "%s version 2 of the License, or (at your option) any later version.\n" c;
121 pr "%s This library is distributed in the hope that it will be useful,\n" c;
122 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
123 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" c;
124 pr "%s Lesser General Public License for more details.\n" c;
126 pr "%s You should have received a copy of the GNU Lesser General Public\n" c;
127 pr "%s License along with this library; if not, write to the Free Software\n" c;
128 pr "%s Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" c;
131 | CStyle -> pr " */\n"
133 | OCamlStyle -> pr " *)\n"
137 (* Generate the pod documentation for the C API. *)
138 and generate_pod () =
140 fun (shortname, style, _, _, longdesc) ->
141 let name = "guestfs_" ^ shortname in
142 pr "=head2 %s\n\n" name;
144 generate_prototype ~extern:false ~handle:"handle" name style;
146 pr "%s\n\n" longdesc;
149 pr "This function return 0 on success or -1 on error.\n\n"
153 (* Generate the protocol (XDR) file. *)
154 and generate_xdr () =
155 generate_header CStyle LGPLv2;
158 fun (shortname, style, _, _, _) ->
159 let name = "guestfs_" ^ shortname in
160 pr "/* %s */\n\n" name;
164 pr "struct %s_args {\n" name;
167 | String name -> pr " string %s<>;\n" name
173 (* | ... -> pr "struct %s_ret ...\n" name; *)
177 (* Table of procedure numbers. *)
178 pr "enum guestfs_procedure {\n";
180 fun (shortname, _, proc_nr, _, _) ->
181 pr " GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
183 pr " GUESTFS_PROC_dummy\n"; (* so we don't have a "hanging comma" *)
187 (* Having to choose a maximum message size is annoying for several
188 * reasons (it limits what we can do in the API), but it (a) makes
189 * the protocol a lot simpler, and (b) provides a bound on the size
190 * of the daemon which operates in limited memory space. For large
191 * file transfers you should use FTP.
193 pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
196 (* Message header, etc. *)
198 const GUESTFS_PROGRAM = 0x2000F5F5;
199 const GUESTFS_PROTOCOL_VERSION = 1;
201 enum guestfs_message_direction {
202 GUESTFS_DIRECTION_CALL = 0, /* client -> daemon */
203 GUESTFS_DIRECTION_REPLY = 1 /* daemon -> client */
206 enum guestfs_message_status {
207 GUESTFS_STATUS_OK = 0,
208 GUESTFS_STATUS_ERROR = 1
211 struct guestfs_message_header {
212 unsigned prog; /* GUESTFS_PROGRAM */
213 unsigned vers; /* GUESTFS_PROTOCOL_VERSION */
214 guestfs_procedure proc; /* GUESTFS_PROC_x */
215 guestfs_message_direction direction;
216 unsigned serial; /* message serial number */
217 guestfs_message_status status;
221 (* Generate the guestfs-actions.h file. *)
222 and generate_actions_h () =
223 generate_header CStyle LGPLv2;
225 fun (shortname, style, _, _, _) ->
226 let name = "guestfs_" ^ shortname in
227 generate_prototype ~single_line:true ~newline:true ~handle:"handle"
231 (* Generate the client-side dispatch stubs. *)
232 and generate_client_actions () =
233 generate_header CStyle LGPLv2;
235 fun (shortname, style, _, _, _) ->
236 let name = "guestfs_" ^ shortname in
238 (* Generate the return value struct. *)
239 pr "struct %s_rv {\n" shortname;
240 pr " int err_code; /* 0 OK or -1 error */\n";
241 pr " int serial; /* serial number of reply */\n";
242 pr " char err_str[256]; /* error from daemon */\n";
245 (* | _ -> pr " struct %s_ret ret;\n" name; *)
249 (* Generate the callback function. *)
250 pr "static void %s_cb (guestfs_h *g, void *data, XDR *xdr)\n" shortname;
252 pr " struct %s_rv *rv = (struct %s_rv *) data;\n" shortname shortname;
254 pr " /* XXX */ rv->err_code = 0;\n";
255 pr " /* XXX rv->serial = ?; */\n";
256 pr " main_loop.main_loop_quit (g);\n";
259 (* Generate the action stub. *)
260 generate_prototype ~extern:false ~semicolon:false ~newline:true
261 ~handle:"g" name style;
265 | (Err, _) -> "-1" in
271 | _ -> pr " struct %s_args args;\n" name
274 pr " struct %s_rv rv;\n" shortname;
277 pr " if (g->state != READY) {\n";
278 pr " error (g, \"%s called from the wrong state, %%d != READY\",\n"
281 pr " return %s;\n" error_code;
286 pr " serial = dispatch (g, GUESTFS_PROC_%s, NULL, NULL);\n"
287 (String.uppercase shortname)
292 | String name -> pr " args.%s = (char *) %s;\n" name name
294 pr " serial = dispatch (g, GUESTFS_PROC_%s,\n"
295 (String.uppercase shortname);
296 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
299 pr " if (serial == -1)\n";
300 pr " return %s;\n" error_code;
303 pr " rv.err_code = 42;\n";
304 pr " g->reply_cb_internal = %s_cb;\n" shortname;
305 pr " g->reply_cb_internal_data = &rv;\n";
306 pr " main_loop.main_loop_run (g);\n";
307 pr " g->reply_cb_internal = NULL;\n";
308 pr " g->reply_cb_internal_data = NULL;\n";
309 pr " if (rv.err_code == 42) { /* callback wasn't called */\n";
310 pr " error (g, \"%s failed, see earlier error messages\");\n" name;
311 pr " return %s;\n" error_code;
313 pr " else if (rv.err_code == -1) { /* error from remote end */\n";
314 pr " error (g, \"%%s\", rv.err_str);\n";
315 pr " return %s;\n" error_code;
319 pr " /* XXX check serial number agrees */\n\n";
322 | (Err, _) -> pr " return 0;\n"
328 (* Generate daemon/actions.h. *)
329 and generate_daemon_actions_h () =
330 generate_header CStyle GPLv2;
332 fun (name, style, _, _, _) ->
333 generate_prototype ~single_line:true ~newline:true ("do_" ^ name) style;
336 (* Generate the server-side stubs. *)
337 and generate_daemon_actions () =
338 generate_header CStyle GPLv2;
340 pr "#include <rpc/types.h>\n";
341 pr "#include <rpc/xdr.h>\n";
342 pr "#include \"daemon.h\"\n";
343 pr "#include \"../src/guestfs_protocol.h\"\n";
344 pr "#include \"actions.h\"\n";
348 fun (name, style, _, _, _) ->
349 (* Generate server-side stubs. *)
350 pr "static void %s_stub (XDR *xdr_in)\n" name;
354 | (Err, _) -> pr " int r;\n"; "-1" in
358 pr " struct guestfs_%s_args args;\n" name;
361 | String name -> pr " const char *%s;\n" name
369 pr " if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
370 pr " reply_with_error (\"%s: daemon failed to decode procedure arguments\");\n" name;
375 | String name -> pr " %s = args.%s;\n" name name
380 pr " r = do_%s " name;
381 generate_call_args style;
384 pr " if (r == %s)\n" error_code;
385 pr " /* do_%s has already called reply_with_error, so just return */\n" name;
390 | (Err, _) -> pr " reply (NULL, NULL);\n"
396 (* Dispatch function. *)
397 pr "void dispatch_incoming_message (XDR *xdr_in)\n";
399 pr " switch (proc_nr) {\n";
402 fun (name, style, _, _, _) ->
403 pr " case GUESTFS_PROC_%s:\n" (String.uppercase name);
404 pr " %s_stub (xdr_in);\n" name;
409 pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d\", proc_nr);\n";
413 (* Generate a C function prototype. *)
414 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
415 ?(single_line = false) ?(newline = false)
417 if extern then pr "extern ";
418 if static then pr "static ";
420 | (Err, _) -> pr "int "
423 let comma = ref false in
426 | Some handle -> pr "guestfs_h *%s" handle; comma := true
430 if single_line then pr ", " else pr ",\n\t\t"
436 | String name -> next (); pr "const char *%s" name
439 if semicolon then pr ";";
440 if newline then pr "\n"
442 (* Generate C call arguments, eg "(handle, foo, bar)" *)
443 and generate_call_args ?handle style =
445 let comma = ref false in
448 | Some handle -> pr "%s" handle; comma := true
452 if !comma then pr ", ";
455 | String name -> pr "%s" name
459 let output_to filename =
460 let filename_new = filename ^ ".new" in
461 chan := open_out filename_new;
465 Unix.rename filename_new filename;
466 printf "written %s\n%!" filename;
472 let close = output_to "src/guestfs_protocol.x" in
476 let close = output_to "src/guestfs-actions.h" in
477 generate_actions_h ();
480 let close = output_to "src/guestfs-actions.c" in
481 generate_client_actions ();
484 let close = output_to "daemon/actions.h" in
485 generate_daemon_actions_h ();
488 let close = output_to "daemon/stubs.c" in
489 generate_daemon_actions ();
492 let close = output_to "guestfs-actions.pod" in