2 * Copyright (C) 2009-2011 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_api_versions
28 open Generator_optgroups
29 open Generator_actions
30 open Generator_structs
35 type optarg_proto = Dots | VA | Argv
37 (* Generate a C function prototype. *)
38 let rec generate_prototype ?(extern = true) ?(static = false)
40 ?(single_line = false) ?(indent = "") ?(newline = false)
42 ?(prefix = "") ?(suffix = "")
44 ?(optarg_proto = Dots)
45 name (ret, args, optargs) =
47 if extern then pr "extern ";
48 if static then pr "static ";
54 if single_line then pr " " else pr "\n%s" indent
57 if single_line then pr " " else pr "\n%s" indent
58 | RConstString _ | RConstOptString _ ->
60 if not single_line then pr "\n%s" indent
61 | RString _ | RBufferOut _ ->
63 if not single_line then pr "\n%s" indent
64 | RStringList _ | RHashtable _ ->
66 if not single_line then pr "\n%s" indent
68 if not in_daemon then pr "struct guestfs_%s *" typ
69 else pr "guestfs_int_%s *" typ;
70 if not single_line then pr "\n%s" indent
71 | RStructList (_, typ) ->
72 if not in_daemon then pr "struct guestfs_%s_list *" typ
73 else pr "guestfs_int_%s_list *" typ;
74 if not single_line then pr "\n%s" indent
76 let is_RBufferOut = match ret with RBufferOut _ -> true | _ -> false in
77 pr "%s%s%s (" prefix name suffix;
78 if handle = None && args = [] && optargs = [] && not is_RBufferOut then
81 let comma = ref false in
84 | Some handle -> pr "guestfs_h *%s" handle; comma := true
88 if single_line then pr ", "
90 let namelen = String.length prefix + String.length name +
91 String.length suffix + 2 in
92 pr ",\n%s%s" indent (spaces namelen)
100 | Device n | Dev_or_Path n
105 pr "const char *%s" n
106 | StringList n | DeviceList n ->
108 pr "char *const *%s" n
109 | Bool n -> next (); pr "int %s" n
110 | Int n -> next (); pr "int %s" n
111 | Int64 n -> next (); pr "int64_t %s" n
114 if not in_daemon then (next (); pr "const char *%s" n)
117 pr "const char *%s" n;
119 pr "size_t %s_size" n
124 if is_RBufferOut then (next (); pr "size_t *size_r");
125 if optargs <> [] then (
127 match optarg_proto with
129 | VA -> pr "va_list args"
130 | Argv -> pr "const struct guestfs_%s_argv *optargs" name
134 if semicolon then pr ";";
135 if newline then pr "\n"
137 (* Generate C call arguments, eg "(handle, foo, bar)" *)
138 and generate_c_call_args ?handle (ret, args, optargs) =
140 let comma = ref false in
142 if !comma then pr ", ";
147 | Some handle -> pr "%s" handle; comma := true
156 pr "%s" (name_of_argt arg)
158 (* For RBufferOut calls, add implicit &size parameter. *)
165 (* For calls with optional arguments, add implicit optargs parameter. *)
166 if optargs <> [] then (
172 (* Generate the pod documentation for the C API. *)
173 and generate_actions_pod () =
175 fun (shortname, (ret, args, optargs as style), _, flags, _, _, longdesc) ->
176 if not (List.mem NotInDocs flags) then (
177 let name = "guestfs_" ^ shortname in
178 pr "=head2 %s\n\n" name;
179 generate_prototype ~extern:false ~indent:" " ~handle:"g" name style;
182 let uc_shortname = String.uppercase shortname in
183 if optargs <> [] then (
184 pr "You may supply a list of optional arguments to this call.\n";
185 pr "Use zero or more of the following pairs of parameters,\n";
186 pr "and terminate the list with C<-1> on its own.\n";
187 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
190 let n = name_of_argt argt in
191 let uc_n = String.uppercase n in
192 pr " GUESTFS_%s_%s, " uc_shortname uc_n;
194 | Bool n -> pr "int %s,\n" n
195 | Int n -> pr "int %s,\n" n
196 | Int64 n -> pr "int64_t %s,\n" n
197 | String n -> pr "const char *%s,\n" n
203 pr "%s\n\n" longdesc;
204 let ret, args, optargs = style in
207 pr "This function returns 0 on success or -1 on error.\n\n"
209 pr "On error this function returns -1.\n\n"
211 pr "On error this function returns -1.\n\n"
213 pr "This function returns a C truth value on success or -1 on error.\n\n"
215 pr "This function returns a string, or NULL on error.
216 The string is owned by the guest handle and must I<not> be freed.\n\n"
217 | RConstOptString _ ->
218 pr "This function returns a string which may be NULL.
219 There is no way to return an error from this function.
220 The string is owned by the guest handle and must I<not> be freed.\n\n"
222 pr "This function returns a string, or NULL on error.
223 I<The caller must free the returned string after use>.\n\n"
225 pr "This function returns a NULL-terminated array of strings
226 (like L<environ(3)>), or NULL if there was an error.
227 I<The caller must free the strings and the array after use>.\n\n"
228 | RStruct (_, typ) ->
229 pr "This function returns a C<struct guestfs_%s *>,
230 or NULL if there was an error.
231 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
232 | RStructList (_, typ) ->
233 pr "This function returns a C<struct guestfs_%s_list *>,
234 or NULL if there was an error.
235 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
237 pr "This function returns a NULL-terminated array of
238 strings, or NULL if there was an error.
239 The array of strings will always have length C<2n+1>, where
240 C<n> keys and values alternate, followed by the trailing NULL entry.
241 I<The caller must free the strings and the array after use>.\n\n"
243 pr "This function returns a buffer, or NULL on error.
244 The size of the returned buffer is written to C<*size_r>.
245 I<The caller must free the returned buffer after use>.\n\n"
247 if List.mem Progress flags then
248 pr "%s\n\n" progress_message;
249 if List.mem ProtocolLimitWarning flags then
250 pr "%s\n\n" protocol_limit_warning;
251 if List.mem DangerWillRobinson flags then
252 pr "%s\n\n" danger_will_robinson;
253 if List.exists (function Key _ -> true | _ -> false) (args@optargs) then
254 pr "This function takes a key or passphrase parameter which
255 could contain sensitive material. Read the section
256 L</KEYS AND PASSPHRASES> for more information.\n\n";
257 (match deprecation_notice flags with
259 | Some txt -> pr "%s\n\n" txt
261 (match lookup_api_version name with
262 | Some version -> pr "(Added in %s)\n\n" version
266 (* Handling of optional argument variants. *)
267 if optargs <> [] then (
268 pr "=head2 %s_va\n\n" name;
269 generate_prototype ~extern:false ~indent:" " ~handle:"g"
270 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
273 pr "This is the \"va_list variant\" of L</%s>.\n\n" name;
274 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
275 pr "=head2 %s_argv\n\n" name;
276 generate_prototype ~extern:false ~indent:" " ~handle:"g"
277 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
280 pr "This is the \"argv variant\" of L</%s>.\n\n" name;
281 pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
284 ) all_functions_sorted
286 and generate_structs_pod () =
287 (* Structs documentation. *)
290 pr "=head2 guestfs_%s\n" typ;
292 pr " struct guestfs_%s {\n" typ;
295 | name, FChar -> pr " char %s;\n" name
296 | name, FUInt32 -> pr " uint32_t %s;\n" name
297 | name, FInt32 -> pr " int32_t %s;\n" name
298 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
299 | name, FInt64 -> pr " int64_t %s;\n" name
300 | name, FString -> pr " char *%s;\n" name
302 pr " /* The next two fields describe a byte array. */\n";
303 pr " uint32_t %s_len;\n" name;
304 pr " char *%s;\n" name
306 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
307 pr " char %s[32];\n" name
308 | name, FOptPercent ->
309 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
310 pr " float %s;\n" name
314 pr " struct guestfs_%s_list {\n" typ;
315 pr " uint32_t len; /* Number of elements in list. */\n";
316 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
319 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
320 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
325 and generate_availability_pod () =
326 (* Availability documentation. *)
330 fun (group, functions) ->
331 pr "=item B<%s>\n" group;
333 pr "The following functions:\n";
334 List.iter (pr "L</guestfs_%s>\n") functions;
340 (* Generate the guestfs.h file. *)
341 and generate_guestfs_h () =
342 generate_header CStyle LGPLv2plus;
345 /* ---------- IMPORTANT NOTE ----------
347 * All API documentation is in the manpage, 'guestfs(3)'.
348 * To read it, type: man 3 guestfs
349 * Or read it online here: http://libguestfs.org/guestfs.3.html
351 * Go and read it now, I'll be right here waiting for you
352 * when you come back.
354 * ------------------------------------
369 # define GUESTFS_GCC_VERSION \\
370 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
374 #ifndef GUESTFS_TYPEDEF_H
375 #define GUESTFS_TYPEDEF_H 1
376 typedef struct guestfs_h guestfs_h;
379 /* Connection management. */
380 extern guestfs_h *guestfs_create (void);
381 extern void guestfs_close (guestfs_h *g);
383 /* Error handling. */
384 extern const char *guestfs_last_error (guestfs_h *g);
385 #define LIBGUESTFS_HAVE_LAST_ERRNO 1
386 extern int guestfs_last_errno (guestfs_h *g);
388 #ifndef GUESTFS_TYPEDEF_ERROR_HANDLER_CB
389 #define GUESTFS_TYPEDEF_ERROR_HANDLER_CB 1
390 typedef void (*guestfs_error_handler_cb) (guestfs_h *g, void *opaque, const char *msg);
393 #ifndef GUESTFS_TYPEDEF_ABORT_CB
394 #define GUESTFS_TYPEDEF_ABORT_CB 1
395 typedef void (*guestfs_abort_cb) (void) __attribute__((__noreturn__));
398 extern void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *opaque);
399 extern guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **opaque_rtn);
401 extern void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb);
402 extern guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g);
408 fun (name, bitmask) ->
409 pr "#define GUESTFS_EVENT_%-16s 0x%04x\n"
410 (String.uppercase name) bitmask
412 pr "#define GUESTFS_EVENT_%-16s UINT64_MAX\n" "ALL";
416 #ifndef GUESTFS_TYPEDEF_EVENT_CALLBACK
417 #define GUESTFS_TYPEDEF_EVENT_CALLBACK 1
418 typedef void (*guestfs_event_callback) (
424 const char *buf, size_t buf_len,
425 const uint64_t *array, size_t array_len);
428 #define LIBGUESTFS_HAVE_SET_EVENT_CALLBACK 1
429 int guestfs_set_event_callback (guestfs_h *g,
430 guestfs_event_callback cb,
431 uint64_t event_bitmask,
434 #define LIBGUESTFS_HAVE_DELETE_EVENT_CALLBACK 1
435 void guestfs_delete_event_callback (guestfs_h *g, int event_handle);
437 /* Old-style event handling. In new code use guestfs_set_event_callback. */
438 #ifndef GUESTFS_TYPEDEF_LOG_MESSAGE_CB
439 #define GUESTFS_TYPEDEF_LOG_MESSAGE_CB 1
440 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque, char *buf, int len);
443 #ifndef GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB
444 #define GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB 1
445 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
448 #ifndef GUESTFS_TYPEDEF_LAUNCH_DONE_CB
449 #define GUESTFS_TYPEDEF_LAUNCH_DONE_CB 1
450 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
453 #ifndef GUESTFS_TYPEDEF_CLOSE_CB
454 #define GUESTFS_TYPEDEF_CLOSE_CB 1
455 typedef void (*guestfs_close_cb) (guestfs_h *g, void *opaque);
458 #ifndef GUESTFS_TYPEDEF_PROGRESS_CB
459 #define GUESTFS_TYPEDEF_PROGRESS_CB 1
460 typedef void (*guestfs_progress_cb) (guestfs_h *g, void *opaque, int proc_nr, int serial, uint64_t position, uint64_t total);
463 extern void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque);
464 extern void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque);
465 extern void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque);
466 #define LIBGUESTFS_HAVE_SET_CLOSE_CALLBACK 1
467 extern void guestfs_set_close_callback (guestfs_h *g, guestfs_close_cb cb, void *opaque);
468 #define LIBGUESTFS_HAVE_SET_PROGRESS_CALLBACK 1
469 extern void guestfs_set_progress_callback (guestfs_h *g, guestfs_progress_cb cb, void *opaque);
471 /* Private data area. */
472 #define LIBGUESTFS_HAVE_SET_PRIVATE 1
473 extern void guestfs_set_private (guestfs_h *g, const char *key, void *data);
474 #define LIBGUESTFS_HAVE_GET_PRIVATE 1
475 extern void *guestfs_get_private (guestfs_h *g, const char *key);
476 #define LIBGUESTFS_HAVE_FIRST_PRIVATE 1
477 extern void *guestfs_first_private (guestfs_h *g, const char **key_rtn);
478 #define LIBGUESTFS_HAVE_NEXT_PRIVATE 1
479 extern void *guestfs_next_private (guestfs_h *g, const char **key_rtn);
484 (* The structures are carefully written to have exactly the same
485 * in-memory format as the XDR structures that we use on the wire to
486 * the daemon. The reason for creating copies of these structures
487 * here is just so we don't have to export the whole of
488 * guestfs_protocol.h (which includes much unrelated and
489 * XDR-dependent stuff that we don't want to be public, or required
492 * To reiterate, we will pass these structures to and from the client
493 * with a simple assignment or memcpy, so the format must be
494 * identical to what rpcgen / the RFC defines.
497 (* Public structures. *)
500 pr "struct guestfs_%s {\n" typ;
503 | name, FChar -> pr " char %s;\n" name
504 | name, FString -> pr " char *%s;\n" name
506 pr " uint32_t %s_len;\n" name;
507 pr " char *%s;\n" name
508 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
509 | name, FUInt32 -> pr " uint32_t %s;\n" name
510 | name, FInt32 -> pr " int32_t %s;\n" name
511 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
512 | name, FInt64 -> pr " int64_t %s;\n" name
513 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
517 pr "struct guestfs_%s_list {\n" typ;
518 pr " uint32_t len;\n";
519 pr " struct guestfs_%s *val;\n" typ;
522 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
523 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
532 fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
534 List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
536 String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
538 String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
539 if not deprecated && not test0 && not debug then
540 pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
542 generate_prototype ~single_line:true ~newline:true ~handle:"g"
543 ~prefix:"guestfs_" shortname style;
545 if optargs <> [] then (
546 generate_prototype ~single_line:true ~newline:true ~handle:"g"
547 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
550 pr "struct guestfs_%s_argv {\n" shortname;
551 pr " uint64_t bitmask;\n";
558 | Int64 n -> "int64_t "
559 | String n -> "const char *"
560 | _ -> assert false (* checked in generator_checks *) in
561 let uc_shortname = String.uppercase shortname in
562 let n = name_of_argt argt in
563 let uc_n = String.uppercase n in
564 pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
565 pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
566 pr "/* The field below is only valid in this struct if the\n";
567 pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
568 pr " * in the bitmask above, otherwise the contents are ignored.\n";
570 pr " %s%s;\n" c_type n
574 generate_prototype ~single_line:true ~newline:true ~handle:"g"
575 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
580 ) all_functions_sorted;
584 /* Private functions.
586 * These are NOT part of the public, stable API, and can change at any
587 * time! We export them because they are used by some of the language
590 extern void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes);
591 extern void *guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s);
592 extern const char *guestfs_tmpdir (void);
593 #ifdef GUESTFS_PRIVATE_FOR_EACH_DISK
594 extern int guestfs___for_each_disk (guestfs_h *g, virDomainPtr dom, int (*)(guestfs_h *g, const char *filename, const char *format, void *data), void *data);
596 /* End of private functions. */
602 #endif /* GUESTFS_H_ */
605 (* Generate the guestfs-internal-actions.h file. *)
606 and generate_internal_actions_h () =
607 generate_header CStyle LGPLv2plus;
609 fun (shortname, style, _, _, _, _, _) ->
610 generate_prototype ~single_line:true ~newline:true ~handle:"g"
611 ~prefix:"guestfs__" ~optarg_proto:Argv
613 ) non_daemon_functions
615 (* Generate the client-side dispatch stubs. *)
616 and generate_client_actions () =
617 generate_header CStyle LGPLv2plus;
624 #include <inttypes.h>
626 #include <sys/types.h>
627 #include <sys/stat.h>
630 #include \"guestfs.h\"
631 #include \"guestfs-internal.h\"
632 #include \"guestfs-internal-actions.h\"
633 #include \"guestfs_protocol.h\"
634 #include \"errnostring.h\"
636 /* Check the return message from a call for validity. */
638 check_reply_header (guestfs_h *g,
639 const struct guestfs_message_header *hdr,
640 unsigned int proc_nr, unsigned int serial)
642 if (hdr->prog != GUESTFS_PROGRAM) {
643 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
646 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
647 error (g, \"wrong protocol version (%%d/%%d)\",
648 hdr->vers, GUESTFS_PROTOCOL_VERSION);
651 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
652 error (g, \"unexpected message direction (%%d/%%d)\",
653 hdr->direction, GUESTFS_DIRECTION_REPLY);
656 if (hdr->proc != proc_nr) {
657 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
660 if (hdr->serial != serial) {
661 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
668 /* Check we are in the right state to run a high-level action. */
670 check_state (guestfs_h *g, const char *caller)
672 if (!guestfs__is_ready (g)) {
673 if (guestfs__is_config (g) || guestfs__is_launching (g))
674 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
677 error (g, \"%%s called from the wrong state, %%d != READY\",
678 caller, guestfs__get_state (g));
684 /* Convenience wrapper for tracing. */
686 trace_open (guestfs_h *g)
688 assert (g->trace_fp == NULL);
691 g->trace_fp = open_memstream (&g->trace_buf, &g->trace_len);
699 trace_send_line (guestfs_h *g)
705 fclose (g->trace_fp);
708 /* The callback might invoke other libguestfs calls, so keep
709 * a copy of the pointer to the buffer and length.
714 guestfs___call_callbacks_message (g, GUESTFS_EVENT_TRACE, buf, len);
722 (* Generate code to check String-like parameters are not passed in
723 * as NULL (returning an error if they are).
725 let check_null_strings shortname (ret, args, optargs) =
726 let pr_newline = ref false in
729 (* parameters which should not be NULL *)
741 pr " if (%s == NULL) {\n" n;
742 pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
743 pr " \"%s\", \"%s\");\n" shortname n;
745 match errcode_of_ret ret with
746 | `CannotReturnError ->
747 if shortname = "test0rconstoptstring" then (* XXX hack *)
751 "%s: RConstOptString function has invalid parameter '%s'"
753 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
754 pr " return %s;\n" (string_of_errcode errcode);
767 (* For optional arguments. *)
771 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
772 (String.uppercase shortname) (String.uppercase n);
773 pr " optargs->%s == NULL) {\n" n;
774 pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
775 pr " \"%s\", \"%s\");\n" shortname n;
777 match errcode_of_ret ret with
778 | `CannotReturnError -> assert false
779 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
780 pr " return %s;\n" (string_of_errcode errcode);
785 | Bool _ | Int _ | Int64 _ -> ()
790 if !pr_newline then pr "\n";
793 (* Generate code to reject optargs we don't know about. *)
794 let reject_unknown_optargs shortname = function
797 let len = List.length optargs in
798 let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
799 pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
800 pr " error (g, \"%%s: unknown option in guestfs_%%s_argv->bitmask (this can happen if a program is compiled against a newer version of libguestfs, then dynamically linked to an older version)\",\n";
801 pr " \"%s\", \"%s\");\n" shortname shortname;
803 match errcode_of_ret ret with
804 | `CannotReturnError -> assert false
805 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
806 pr " return %s;\n" (string_of_errcode errcode);
811 (* Generate code to generate guestfish call traces. *)
812 let trace_call shortname (ret, args, optargs) =
813 pr " if (trace_flag) {\n";
816 List.exists (function
817 | StringList _ | DeviceList _ -> true
818 | _ -> false) args in
824 pr " trace_fp = trace_open (g);\n";
826 pr " fprintf (trace_fp, \"%%s\", \"%s\");\n" shortname;
828 (* Required arguments. *)
831 | String n (* strings *)
837 (* guestfish doesn't support string escaping, so neither do we *)
838 pr " fprintf (trace_fp, \" \\\"%%s\\\"\", %s);\n" n
840 (* don't print keys *)
841 pr " fprintf (trace_fp, \" \\\"***\\\"\");\n"
842 | OptString n -> (* string option *)
843 pr " if (%s) fprintf (trace_fp, \" \\\"%%s\\\"\", %s);\n" n n;
844 pr " else fprintf (trace_fp, \" null\");\n"
846 | DeviceList n -> (* string list *)
847 pr " fputc (' ', trace_fp);\n";
848 pr " fputc ('\"', trace_fp);\n";
849 pr " for (i = 0; %s[i]; ++i) {\n" n;
850 pr " if (i > 0) fputc (' ', trace_fp);\n";
851 pr " fputs (%s[i], trace_fp);\n" n;
853 pr " fputc ('\"', trace_fp);\n";
854 | Bool n -> (* boolean *)
855 pr " fputs (%s ? \" true\" : \" false\", trace_fp);\n" n
857 pr " fprintf (trace_fp, \" %%d\", %s);\n" n
859 pr " fprintf (trace_fp, \" %%\" PRIi64, %s);\n" n
860 | BufferIn n -> (* RHBZ#646822 *)
861 pr " fputc (' ', trace_fp);\n";
862 pr " guestfs___print_BufferIn (trace_fp, %s, %s_size);\n" n n
864 pr " fprintf (trace_fp, \" (%s)%%p\", %s);\n" t n
867 (* Optional arguments. *)
870 let n = name_of_argt argt in
871 let uc_shortname = String.uppercase shortname in
872 let uc_n = String.uppercase n in
873 pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
877 pr " fprintf (trace_fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
879 pr " fprintf (trace_fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
881 pr " fprintf (trace_fp, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
883 pr " fprintf (trace_fp, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
888 pr " trace_send_line (g);\n";
893 let trace_return ?(indent = 2) shortname (ret, _, _) rv =
894 let indent = spaces indent in
896 pr "%sif (trace_flag) {\n" indent;
900 | RStringList _ | RHashtable _ -> true
903 pr "%s size_t i;\n" indent;
907 pr "%s trace_fp = trace_open (g);\n" indent;
909 pr "%s fprintf (trace_fp, \"%%s = \", \"%s\");\n" indent shortname;
912 | RErr | RInt _ | RBool _ ->
913 pr "%s fprintf (trace_fp, \"%%d\", %s);\n" indent rv
915 pr "%s fprintf (trace_fp, \"%%\" PRIi64, %s);\n" indent rv
916 | RConstString _ | RString _ ->
917 pr "%s fprintf (trace_fp, \"\\\"%%s\\\"\", %s);\n" indent rv
918 | RConstOptString _ ->
919 pr "%s fprintf (trace_fp, \"\\\"%%s\\\"\", %s != NULL ? %s : \"NULL\");\n"
922 pr "%s guestfs___print_BufferOut (trace_fp, %s, *size_r);\n" indent rv
923 | RStringList _ | RHashtable _ ->
924 pr "%s fputs (\"[\", trace_fp);\n" indent;
925 pr "%s for (i = 0; %s[i]; ++i) {\n" indent rv;
926 pr "%s if (i > 0) fputs (\", \", trace_fp);\n" indent;
927 pr "%s fputs (\"\\\"\", trace_fp);\n" indent;
928 pr "%s fputs (%s[i], trace_fp);\n" indent rv;
929 pr "%s fputs (\"\\\"\", trace_fp);\n" indent;
931 pr "%s fputs (\"]\", trace_fp);\n" indent;
932 | RStruct (_, typ) ->
933 (* XXX There is code generated for guestfish for printing
934 * these structures. We need to make it generally available
937 pr "%s fprintf (trace_fp, \"<struct guestfs_%s *>\");\n"
939 | RStructList (_, typ) ->
940 pr "%s fprintf (trace_fp, \"<struct guestfs_%s_list *>\");\n"
943 pr "%s trace_send_line (g);\n" indent;
948 let trace_return_error ?(indent = 2) shortname (ret, _, _) errcode =
949 let indent = spaces indent in
951 pr "%sif (trace_flag)\n" indent;
953 pr "%s guestfs___trace (g, \"%%s = %%s (error)\",\n" indent;
954 pr "%s \"%s\", \"%s\");\n"
955 indent shortname (string_of_errcode errcode)
958 (* For non-daemon functions, generate a wrapper around each function. *)
960 fun (shortname, (ret, _, optargs as style), _, _, _, _, _) ->
962 generate_prototype ~extern:false ~semicolon:false ~newline:true
963 ~handle:"g" ~prefix:"guestfs_"
966 generate_prototype ~extern:false ~semicolon:false ~newline:true
967 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
970 pr " int trace_flag = g->trace;\n";
971 pr " FILE *trace_fp;\n";
973 | RErr | RInt _ | RBool _ ->
978 pr " const char *r;\n"
979 | RConstOptString _ ->
980 pr " const char *r;\n"
981 | RString _ | RBufferOut _ ->
983 | RStringList _ | RHashtable _ ->
985 | RStruct (_, typ) ->
986 pr " struct guestfs_%s *r;\n" typ
987 | RStructList (_, typ) ->
988 pr " struct guestfs_%s_list *r;\n" typ
991 check_null_strings shortname style;
992 reject_unknown_optargs shortname style;
993 trace_call shortname style;
994 pr " r = guestfs__%s " shortname;
995 generate_c_call_args ~handle:"g" style;
998 (match errcode_of_ret ret with
999 | (`ErrorIsMinusOne | `ErrorIsNULL) as errcode ->
1000 pr " if (r != %s) {\n" (string_of_errcode errcode);
1001 trace_return ~indent:4 shortname style "r";
1003 trace_return_error ~indent:4 shortname style errcode;
1005 | `CannotReturnError ->
1006 trace_return shortname style "r";
1012 ) non_daemon_functions;
1014 (* Client-side stubs for each function. *)
1016 fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
1017 let name = "guestfs_" ^ shortname in
1019 match errcode_of_ret ret with
1020 | `CannotReturnError -> assert false
1021 | (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
1023 (* Generate the action stub. *)
1024 if optargs = [] then
1025 generate_prototype ~extern:false ~semicolon:false ~newline:true
1026 ~handle:"g" ~prefix:"guestfs_" shortname style
1028 generate_prototype ~extern:false ~semicolon:false ~newline:true
1029 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv"
1030 ~optarg_proto:Argv shortname style;
1036 | _ -> pr " struct %s_args args;\n" name
1039 pr " guestfs_message_header hdr;\n";
1040 pr " guestfs_message_error err;\n";
1044 | RConstString _ | RConstOptString _ ->
1045 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
1047 | RBool _ | RString _ | RStringList _
1048 | RStruct _ | RStructList _
1049 | RHashtable _ | RBufferOut _ ->
1050 pr " struct %s_ret ret;\n" name;
1053 pr " int serial;\n";
1055 pr " int trace_flag = g->trace;\n";
1056 pr " FILE *trace_fp;\n";
1058 | RErr | RInt _ | RBool _ ->
1061 pr " int64_t ret_v;\n"
1062 | RConstString _ | RConstOptString _ ->
1063 pr " const char *ret_v;\n"
1064 | RString _ | RBufferOut _ ->
1065 pr " char *ret_v;\n"
1066 | RStringList _ | RHashtable _ ->
1067 pr " char **ret_v;\n"
1068 | RStruct (_, typ) ->
1069 pr " struct guestfs_%s *ret_v;\n" typ
1070 | RStructList (_, typ) ->
1071 pr " struct guestfs_%s_list *ret_v;\n" typ
1075 List.exists (function FileIn _ -> true | _ -> false) args in
1076 if has_filein then (
1077 pr " uint64_t progress_hint = 0;\n";
1078 pr " struct stat progress_stat;\n";
1080 pr " const uint64_t progress_hint = 0;\n";
1083 check_null_strings shortname style;
1084 reject_unknown_optargs shortname style;
1085 trace_call shortname style;
1087 (* Calculate the total size of all FileIn arguments to pass
1088 * as a progress bar hint.
1093 pr " if (stat (%s, &progress_stat) == 0 &&\n" n;
1094 pr " S_ISREG (progress_stat.st_mode))\n";
1095 pr " progress_hint += progress_stat.st_size;\n";
1100 (* Check we are in the right state for sending a request. *)
1101 pr " if (check_state (g, \"%s\") == -1) {\n" shortname;
1102 trace_return_error ~indent:4 shortname style errcode;
1103 pr " return %s;\n" (string_of_errcode errcode);
1105 pr " guestfs___set_busy (g);\n";
1108 (* Send the main header and arguments. *)
1109 if args = [] && optargs = [] then (
1110 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, progress_hint, 0,\n"
1111 (String.uppercase shortname);
1112 pr " NULL, NULL);\n"
1116 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
1117 pr " args.%s = (char *) %s;\n" n n
1119 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
1120 | StringList n | DeviceList n ->
1121 pr " args.%s.%s_val = (char **) %s;\n" n n n;
1122 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
1124 pr " args.%s = %s;\n" n n
1126 pr " args.%s = %s;\n" n n
1128 pr " args.%s = %s;\n" n n
1129 | FileIn _ | FileOut _ -> ()
1131 pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
1132 pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
1133 trace_return_error ~indent:4 shortname style errcode;
1134 pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
1136 pr " guestfs___end_busy (g);\n";
1137 pr " return %s;\n" (string_of_errcode errcode);
1139 pr " args.%s.%s_val = (char *) %s;\n" n n n;
1140 pr " args.%s.%s_len = %s_size;\n" n n n
1141 | Pointer _ -> assert false
1146 let n = name_of_argt argt in
1147 let uc_shortname = String.uppercase shortname in
1148 let uc_n = String.uppercase n in
1149 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK))\n"
1155 pr " args.%s = optargs->%s;\n" n n;
1157 pr " args.%s = 0;\n" n
1159 pr " args.%s = (char *) optargs->%s;\n" n n;
1161 pr " args.%s = (char *) \"\";\n" n
1166 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
1167 (String.uppercase shortname);
1168 pr " progress_hint, %s,\n"
1169 (if optargs <> [] then "optargs->bitmask" else "0");
1170 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
1173 pr " if (serial == -1) {\n";
1174 pr " guestfs___end_busy (g);\n";
1175 trace_return_error ~indent:4 shortname style errcode;
1176 pr " return %s;\n" (string_of_errcode errcode);
1180 (* Send any additional files (FileIn) requested. *)
1181 let need_read_reply_label = ref false in
1185 pr " r = guestfs___send_file (g, %s);\n" n;
1186 pr " if (r == -1) {\n";
1187 pr " guestfs___end_busy (g);\n";
1188 trace_return_error ~indent:4 shortname style errcode;
1189 pr " /* daemon will send an error reply which we discard */\n";
1190 pr " guestfs___recv_discard (g, \"%s\");\n" shortname;
1191 pr " return %s;\n" (string_of_errcode errcode);
1193 pr " if (r == -2) /* daemon cancelled */\n";
1194 pr " goto read_reply;\n";
1195 need_read_reply_label := true;
1200 (* Wait for the reply from the remote end. *)
1201 if !need_read_reply_label then pr " read_reply:\n";
1202 pr " memset (&hdr, 0, sizeof hdr);\n";
1203 pr " memset (&err, 0, sizeof err);\n";
1204 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
1206 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
1210 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
1213 pr " if (r == -1) {\n";
1214 pr " guestfs___end_busy (g);\n";
1215 trace_return_error ~indent:4 shortname style errcode;
1216 pr " return %s;\n" (string_of_errcode errcode);
1220 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
1221 (String.uppercase shortname);
1222 pr " guestfs___end_busy (g);\n";
1223 trace_return_error ~indent:4 shortname style errcode;
1224 pr " return %s;\n" (string_of_errcode errcode);
1228 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
1229 trace_return_error ~indent:4 shortname style errcode;
1230 pr " int errnum = 0;\n";
1231 pr " if (err.errno_string[0] != '\\0')\n";
1232 pr " errnum = guestfs___string_to_errno (err.errno_string);\n";
1233 pr " if (errnum <= 0)\n";
1234 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n"
1237 pr " guestfs_error_errno (g, errnum, \"%%s: %%s\", \"%s\",\n"
1239 pr " err.error_message);\n";
1240 pr " free (err.error_message);\n";
1241 pr " free (err.errno_string);\n";
1242 pr " guestfs___end_busy (g);\n";
1243 pr " return %s;\n" (string_of_errcode errcode);
1247 (* Expecting to receive further files (FileOut)? *)
1251 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
1252 pr " guestfs___end_busy (g);\n";
1253 trace_return_error ~indent:4 shortname style errcode;
1254 pr " return %s;\n" (string_of_errcode errcode);
1260 pr " guestfs___end_busy (g);\n";
1265 | RInt n | RInt64 n | RBool n ->
1266 pr " ret_v = ret.%s;\n" n
1267 | RConstString _ | RConstOptString _ ->
1268 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
1270 pr " ret_v = ret.%s; /* caller will free */\n" n
1271 | RStringList n | RHashtable n ->
1272 pr " /* caller will free this, but we need to add a NULL entry */\n";
1273 pr " ret.%s.%s_val =\n" n n;
1274 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
1275 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
1277 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
1278 pr " ret_v = ret.%s.%s_val;\n" n n
1280 pr " /* caller will free this */\n";
1281 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1282 | RStructList (n, _) ->
1283 pr " /* caller will free this */\n";
1284 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1286 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
1287 pr " * _val might be NULL here. To make the API saner for\n";
1288 pr " * callers, we turn this case into a unique pointer (using\n";
1289 pr " * malloc(1)).\n";
1291 pr " if (ret.%s.%s_len > 0) {\n" n n;
1292 pr " *size_r = ret.%s.%s_len;\n" n n;
1293 pr " ret_v = ret.%s.%s_val; /* caller will free */\n" n n;
1295 pr " free (ret.%s.%s_val);\n" n n;
1296 pr " char *p = safe_malloc (g, 1);\n";
1297 pr " *size_r = ret.%s.%s_len;\n" n n;
1301 trace_return shortname style "ret_v";
1302 pr " return ret_v;\n";
1306 (* Functions to free structures. *)
1307 pr "/* Structure-freeing functions. These rely on the fact that the\n";
1308 pr " * structure format is identical to the XDR format. See note in\n";
1309 pr " * generator.ml.\n";
1316 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
1318 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
1324 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
1326 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
1333 (* Functions which have optional arguments have two generated variants. *)
1336 | shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
1337 let uc_shortname = String.uppercase shortname in
1339 (* Get the name of the last regular argument. *)
1343 | args -> name_of_argt (List.hd (List.rev args)) in
1347 | RErr | RInt _ | RBool _ -> "int "
1348 | RInt64 _ -> "int64_t "
1349 | RConstString _ | RConstOptString _ -> "const char *"
1350 | RString _ | RBufferOut _ -> "char *"
1351 | RStringList _ | RHashtable _ -> "char **"
1352 | RStruct (_, typ) -> sprintf "struct guestfs_%s *" typ
1353 | RStructList (_, typ) ->
1354 sprintf "struct guestfs_%s_list *" typ in
1356 (* The regular variable args function, just calls the _va variant. *)
1357 generate_prototype ~extern:false ~semicolon:false ~newline:true
1358 ~handle:"g" ~prefix:"guestfs_" shortname style;
1360 pr " va_list optargs;\n";
1362 pr " va_start (optargs, %s);\n" last_arg;
1363 pr " %sr = guestfs_%s_va " rtype shortname;
1364 generate_c_call_args ~handle:"g" style;
1366 pr " va_end (optargs);\n";
1371 generate_prototype ~extern:false ~semicolon:false ~newline:true
1372 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
1375 pr " struct guestfs_%s_argv optargs_s;\n" shortname;
1376 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
1379 pr " optargs_s.bitmask = 0;\n";
1381 pr " while ((i = va_arg (args, int)) >= 0) {\n";
1382 pr " switch (i) {\n";
1386 let n = name_of_argt argt in
1387 let uc_n = String.uppercase n in
1388 pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
1389 pr " optargs_s.%s = va_arg (args, " n;
1391 | Bool _ | Int _ -> pr "int"
1392 | Int64 _ -> pr "int64_t"
1393 | String _ -> pr "const char *"
1401 match errcode_of_ret ret with
1402 | `CannotReturnError -> assert false
1403 | (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
1406 pr " error (g, \"%%s: unknown option %%d (this can happen if a program is compiled against a newer version of libguestfs, then dynamically linked to an older version)\",\n";
1407 pr " \"%s\", i);\n" shortname;
1408 pr " return %s;\n" (string_of_errcode errcode);
1411 pr " uint64_t i_mask = UINT64_C(1) << i;\n";
1412 pr " if (optargs_s.bitmask & i_mask) {\n";
1413 pr " error (g, \"%%s: same optional argument specified more than once\",\n";
1414 pr " \"%s\");\n" shortname;
1415 pr " return %s;\n" (string_of_errcode errcode);
1417 pr " optargs_s.bitmask |= i_mask;\n";
1420 pr " return guestfs_%s_argv " shortname;
1421 generate_c_call_args ~handle:"g" style;
1425 ) all_functions_sorted
1427 (* Generate the linker script which controls the visibility of
1428 * symbols in the public ABI and ensures no other symbols get
1429 * exported accidentally.
1431 and generate_linker_script () =
1432 generate_header HashStyle GPLv2plus;
1437 "guestfs_delete_event_callback";
1438 "guestfs_first_private";
1439 "guestfs_get_error_handler";
1440 "guestfs_get_out_of_memory_handler";
1441 "guestfs_get_private";
1442 "guestfs_last_errno";
1443 "guestfs_last_error";
1444 "guestfs_next_private";
1445 "guestfs_set_close_callback";
1446 "guestfs_set_error_handler";
1447 "guestfs_set_event_callback";
1448 "guestfs_set_launch_done_callback";
1449 "guestfs_set_log_message_callback";
1450 "guestfs_set_out_of_memory_handler";
1451 "guestfs_set_private";
1452 "guestfs_set_progress_callback";
1453 "guestfs_set_subprocess_quit_callback";
1455 (* Unofficial parts of the API: the bindings code use these
1456 * functions, so it is useful to export them.
1458 "guestfs_safe_calloc";
1459 "guestfs_safe_malloc";
1460 "guestfs_safe_strdup";
1461 "guestfs_safe_memdup";
1463 "guestfs___for_each_disk";
1469 | name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
1470 | name, (_, _, _), _, _, _, _, _ ->
1472 "guestfs_" ^ name ^ "_va";
1473 "guestfs_" ^ name ^ "_argv"]
1478 List.map (fun (typ, _) ->
1479 ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
1482 let globals = List.sort compare (globals @ functions @ structs) in
1486 List.iter (pr " %s;\n") globals;
1493 and generate_max_proc_nr () =
1494 pr "%d\n" max_proc_nr