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 #ifndef GUESTFS_TYPEDEF_H
370 #define GUESTFS_TYPEDEF_H 1
371 typedef struct guestfs_h guestfs_h;
374 /* Connection management. */
375 extern guestfs_h *guestfs_create (void);
376 extern void guestfs_close (guestfs_h *g);
378 /* Error handling. */
379 extern const char *guestfs_last_error (guestfs_h *g);
380 #define LIBGUESTFS_HAVE_LAST_ERRNO 1
381 extern int guestfs_last_errno (guestfs_h *g);
383 #ifndef GUESTFS_TYPEDEF_ERROR_HANDLER_CB
384 #define GUESTFS_TYPEDEF_ERROR_HANDLER_CB 1
385 typedef void (*guestfs_error_handler_cb) (guestfs_h *g, void *opaque, const char *msg);
388 #ifndef GUESTFS_TYPEDEF_ABORT_CB
389 #define GUESTFS_TYPEDEF_ABORT_CB 1
390 typedef void (*guestfs_abort_cb) (void) __attribute__((__noreturn__));
393 extern void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *opaque);
394 extern guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **opaque_rtn);
396 extern void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb);
397 extern guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g);
403 fun (name, bitmask) ->
404 pr "#define GUESTFS_EVENT_%-16s 0x%04x\n"
405 (String.uppercase name) bitmask
407 pr "#define GUESTFS_EVENT_%-16s UINT64_MAX\n" "ALL";
411 #ifndef GUESTFS_TYPEDEF_EVENT_CALLBACK
412 #define GUESTFS_TYPEDEF_EVENT_CALLBACK 1
413 typedef void (*guestfs_event_callback) (
419 const char *buf, size_t buf_len,
420 const uint64_t *array, size_t array_len);
423 #define LIBGUESTFS_HAVE_SET_EVENT_CALLBACK 1
424 int guestfs_set_event_callback (guestfs_h *g,
425 guestfs_event_callback cb,
426 uint64_t event_bitmask,
429 #define LIBGUESTFS_HAVE_DELETE_EVENT_CALLBACK 1
430 void guestfs_delete_event_callback (guestfs_h *g, int event_handle);
432 /* Old-style event handling. In new code use guestfs_set_event_callback. */
433 #ifndef GUESTFS_TYPEDEF_LOG_MESSAGE_CB
434 #define GUESTFS_TYPEDEF_LOG_MESSAGE_CB 1
435 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque, char *buf, int len);
438 #ifndef GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB
439 #define GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB 1
440 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
443 #ifndef GUESTFS_TYPEDEF_LAUNCH_DONE_CB
444 #define GUESTFS_TYPEDEF_LAUNCH_DONE_CB 1
445 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
448 #ifndef GUESTFS_TYPEDEF_CLOSE_CB
449 #define GUESTFS_TYPEDEF_CLOSE_CB 1
450 typedef void (*guestfs_close_cb) (guestfs_h *g, void *opaque);
453 #ifndef GUESTFS_TYPEDEF_PROGRESS_CB
454 #define GUESTFS_TYPEDEF_PROGRESS_CB 1
455 typedef void (*guestfs_progress_cb) (guestfs_h *g, void *opaque, int proc_nr, int serial, uint64_t position, uint64_t total);
458 extern void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque);
459 extern void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque);
460 extern void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque);
461 #define LIBGUESTFS_HAVE_SET_CLOSE_CALLBACK 1
462 extern void guestfs_set_close_callback (guestfs_h *g, guestfs_close_cb cb, void *opaque);
463 #define LIBGUESTFS_HAVE_SET_PROGRESS_CALLBACK 1
464 extern void guestfs_set_progress_callback (guestfs_h *g, guestfs_progress_cb cb, void *opaque);
466 /* Private data area. */
467 #define LIBGUESTFS_HAVE_SET_PRIVATE 1
468 extern void guestfs_set_private (guestfs_h *g, const char *key, void *data);
469 #define LIBGUESTFS_HAVE_GET_PRIVATE 1
470 extern void *guestfs_get_private (guestfs_h *g, const char *key);
471 #define LIBGUESTFS_HAVE_FIRST_PRIVATE 1
472 extern void *guestfs_first_private (guestfs_h *g, const char **key_rtn);
473 #define LIBGUESTFS_HAVE_NEXT_PRIVATE 1
474 extern void *guestfs_next_private (guestfs_h *g, const char **key_rtn);
479 (* The structures are carefully written to have exactly the same
480 * in-memory format as the XDR structures that we use on the wire to
481 * the daemon. The reason for creating copies of these structures
482 * here is just so we don't have to export the whole of
483 * guestfs_protocol.h (which includes much unrelated and
484 * XDR-dependent stuff that we don't want to be public, or required
487 * To reiterate, we will pass these structures to and from the client
488 * with a simple assignment or memcpy, so the format must be
489 * identical to what rpcgen / the RFC defines.
492 (* Public structures. *)
495 pr "struct guestfs_%s {\n" typ;
498 | name, FChar -> pr " char %s;\n" name
499 | name, FString -> pr " char *%s;\n" name
501 pr " uint32_t %s_len;\n" name;
502 pr " char *%s;\n" name
503 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
504 | name, FUInt32 -> pr " uint32_t %s;\n" name
505 | name, FInt32 -> pr " int32_t %s;\n" name
506 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
507 | name, FInt64 -> pr " int64_t %s;\n" name
508 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
512 pr "struct guestfs_%s_list {\n" typ;
513 pr " uint32_t len;\n";
514 pr " struct guestfs_%s *val;\n" typ;
517 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
518 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
527 fun (shortname, (ret, args, optargs as style), _, flags, _, _, _) ->
529 List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
531 String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
533 String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
534 if not deprecated && not test0 && not debug then
535 pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
537 generate_prototype ~single_line:true ~newline:true ~handle:"g"
538 ~prefix:"guestfs_" shortname style;
540 if optargs <> [] then (
541 generate_prototype ~single_line:true ~newline:true ~handle:"g"
542 ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
545 pr "struct guestfs_%s_argv {\n" shortname;
546 pr " uint64_t bitmask;\n";
553 | Int64 n -> "int64_t "
554 | String n -> "const char *"
555 | _ -> assert false (* checked in generator_checks *) in
556 let uc_shortname = String.uppercase shortname in
557 let n = name_of_argt argt in
558 let uc_n = String.uppercase n in
559 pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
560 pr "#define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
561 pr "/* The field below is only valid in this struct if the\n";
562 pr " * GUESTFS_%s_%s_BITMASK bit is set\n" uc_shortname uc_n;
563 pr " * in the bitmask above, otherwise the contents are ignored.\n";
565 pr " %s%s;\n" c_type n
569 generate_prototype ~single_line:true ~newline:true ~handle:"g"
570 ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
573 ) all_functions_sorted;
577 /* Private functions.
579 * These are NOT part of the public, stable API, and can change at any
580 * time! We export them because they are used by some of the language
583 extern void *guestfs_safe_malloc (guestfs_h *g, size_t nbytes);
584 extern void *guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s);
585 extern const char *guestfs_tmpdir (void);
586 #ifdef GUESTFS_PRIVATE_FOR_EACH_DISK
587 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);
589 /* End of private functions. */
595 #endif /* GUESTFS_H_ */
598 (* Generate the guestfs-internal-actions.h file. *)
599 and generate_internal_actions_h () =
600 generate_header CStyle LGPLv2plus;
602 fun (shortname, style, _, _, _, _, _) ->
603 generate_prototype ~single_line:true ~newline:true ~handle:"g"
604 ~prefix:"guestfs__" ~optarg_proto:Argv
606 ) non_daemon_functions
608 (* Generate the client-side dispatch stubs. *)
609 and generate_client_actions () =
610 generate_header CStyle LGPLv2plus;
617 #include <inttypes.h>
619 #include <sys/types.h>
620 #include <sys/stat.h>
623 #include \"guestfs.h\"
624 #include \"guestfs-internal.h\"
625 #include \"guestfs-internal-actions.h\"
626 #include \"guestfs_protocol.h\"
627 #include \"errnostring.h\"
629 /* Check the return message from a call for validity. */
631 check_reply_header (guestfs_h *g,
632 const struct guestfs_message_header *hdr,
633 unsigned int proc_nr, unsigned int serial)
635 if (hdr->prog != GUESTFS_PROGRAM) {
636 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
639 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
640 error (g, \"wrong protocol version (%%d/%%d)\",
641 hdr->vers, GUESTFS_PROTOCOL_VERSION);
644 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
645 error (g, \"unexpected message direction (%%d/%%d)\",
646 hdr->direction, GUESTFS_DIRECTION_REPLY);
649 if (hdr->proc != proc_nr) {
650 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
653 if (hdr->serial != serial) {
654 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
661 /* Check we are in the right state to run a high-level action. */
663 check_state (guestfs_h *g, const char *caller)
665 if (!guestfs__is_ready (g)) {
666 if (guestfs__is_config (g) || guestfs__is_launching (g))
667 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
670 error (g, \"%%s called from the wrong state, %%d != READY\",
671 caller, guestfs__get_state (g));
677 /* Convenience wrapper for tracing. */
679 trace_open (guestfs_h *g)
681 assert (g->trace_fp == NULL);
684 g->trace_fp = open_memstream (&g->trace_buf, &g->trace_len);
692 trace_send_line (guestfs_h *g)
698 fclose (g->trace_fp);
701 /* The callback might invoke other libguestfs calls, so keep
702 * a copy of the pointer to the buffer and length.
707 guestfs___call_callbacks_message (g, GUESTFS_EVENT_TRACE, buf, len);
715 (* Generate code to check String-like parameters are not passed in
716 * as NULL (returning an error if they are).
718 let check_null_strings shortname (ret, args, optargs) =
719 let pr_newline = ref false in
722 (* parameters which should not be NULL *)
734 pr " if (%s == NULL) {\n" n;
735 pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
736 pr " \"%s\", \"%s\");\n" shortname n;
738 match errcode_of_ret ret with
739 | `CannotReturnError ->
740 if shortname = "test0rconstoptstring" then (* XXX hack *)
744 "%s: RConstOptString function has invalid parameter '%s'"
746 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
747 pr " return %s;\n" (string_of_errcode errcode);
760 (* For optional arguments. *)
764 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
765 (String.uppercase shortname) (String.uppercase n);
766 pr " optargs->%s == NULL) {\n" n;
767 pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
768 pr " \"%s\", \"%s\");\n" shortname n;
770 match errcode_of_ret ret with
771 | `CannotReturnError -> assert false
772 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
773 pr " return %s;\n" (string_of_errcode errcode);
778 | Bool _ | Int _ | Int64 _ -> ()
783 if !pr_newline then pr "\n";
786 (* Generate code to reject optargs we don't know about. *)
787 let reject_unknown_optargs shortname = function
790 let len = List.length optargs in
791 let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
792 pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
793 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";
794 pr " \"%s\", \"%s\");\n" shortname shortname;
796 match errcode_of_ret ret with
797 | `CannotReturnError -> assert false
798 | (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
799 pr " return %s;\n" (string_of_errcode errcode);
804 (* Generate code to generate guestfish call traces. *)
805 let trace_call shortname (ret, args, optargs) =
806 pr " if (trace_flag) {\n";
809 List.exists (function
810 | StringList _ | DeviceList _ -> true
811 | _ -> false) args in
817 pr " trace_fp = trace_open (g);\n";
819 pr " fprintf (trace_fp, \"%%s\", \"%s\");\n" shortname;
821 (* Required arguments. *)
824 | String n (* strings *)
830 (* guestfish doesn't support string escaping, so neither do we *)
831 pr " fprintf (trace_fp, \" \\\"%%s\\\"\", %s);\n" n
833 (* don't print keys *)
834 pr " fprintf (trace_fp, \" \\\"***\\\"\");\n"
835 | OptString n -> (* string option *)
836 pr " if (%s) fprintf (trace_fp, \" \\\"%%s\\\"\", %s);\n" n n;
837 pr " else fprintf (trace_fp, \" null\");\n"
839 | DeviceList n -> (* string list *)
840 pr " fputc (' ', trace_fp);\n";
841 pr " fputc ('\"', trace_fp);\n";
842 pr " for (i = 0; %s[i]; ++i) {\n" n;
843 pr " if (i > 0) fputc (' ', trace_fp);\n";
844 pr " fputs (%s[i], trace_fp);\n" n;
846 pr " fputc ('\"', trace_fp);\n";
847 | Bool n -> (* boolean *)
848 pr " fputs (%s ? \" true\" : \" false\", trace_fp);\n" n
850 pr " fprintf (trace_fp, \" %%d\", %s);\n" n
852 pr " fprintf (trace_fp, \" %%\" PRIi64, %s);\n" n
853 | BufferIn n -> (* RHBZ#646822 *)
854 pr " fputc (' ', trace_fp);\n";
855 pr " guestfs___print_BufferIn (trace_fp, %s, %s_size);\n" n n
857 pr " fprintf (trace_fp, \" (%s)%%p\", %s);\n" t n
860 (* Optional arguments. *)
863 let n = name_of_argt argt in
864 let uc_shortname = String.uppercase shortname in
865 let uc_n = String.uppercase n in
866 pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK)\n"
870 pr " fprintf (trace_fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
872 pr " fprintf (trace_fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
874 pr " fprintf (trace_fp, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
876 pr " fprintf (trace_fp, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
881 pr " trace_send_line (g);\n";
886 let trace_return ?(indent = 2) shortname (ret, _, _) rv =
887 let indent = spaces indent in
889 pr "%sif (trace_flag) {\n" indent;
893 | RStringList _ | RHashtable _ -> true
896 pr "%s size_t i;\n" indent;
900 pr "%s trace_fp = trace_open (g);\n" indent;
902 pr "%s fprintf (trace_fp, \"%%s = \", \"%s\");\n" indent shortname;
905 | RErr | RInt _ | RBool _ ->
906 pr "%s fprintf (trace_fp, \"%%d\", %s);\n" indent rv
908 pr "%s fprintf (trace_fp, \"%%\" PRIi64, %s);\n" indent rv
909 | RConstString _ | RString _ ->
910 pr "%s fprintf (trace_fp, \"\\\"%%s\\\"\", %s);\n" indent rv
911 | RConstOptString _ ->
912 pr "%s fprintf (trace_fp, \"\\\"%%s\\\"\", %s != NULL ? %s : \"NULL\");\n"
915 pr "%s guestfs___print_BufferOut (trace_fp, %s, *size_r);\n" indent rv
916 | RStringList _ | RHashtable _ ->
917 pr "%s fputs (\"[\\\"\", trace_fp);\n" indent;
918 pr "%s for (i = 0; %s[i]; ++i) {\n" indent rv;
919 pr "%s if (i > 0) fputs (\"\\\", \\\"\", trace_fp);\n" indent;
920 pr "%s fputs (%s[i], trace_fp);\n" indent rv;
922 pr "%s fputs (\"\\\"]\", trace_fp);\n" indent;
923 | RStruct (_, typ) ->
924 (* XXX There is code generated for guestfish for printing
925 * these structures. We need to make it generally available
928 pr "%s fprintf (trace_fp, \"<struct guestfs_%s *>\");\n"
930 | RStructList (_, typ) ->
931 pr "%s fprintf (trace_fp, \"<struct guestfs_%s_list *>\");\n"
934 pr "%s trace_send_line (g);\n" indent;
939 let trace_return_error ?(indent = 2) shortname (ret, _, _) errcode =
940 let indent = spaces indent in
942 pr "%sif (trace_flag)\n" indent;
944 pr "%s guestfs___trace (g, \"%%s = %%s (error)\",\n" indent;
945 pr "%s \"%s\", \"%s\");\n"
946 indent shortname (string_of_errcode errcode)
949 (* For non-daemon functions, generate a wrapper around each function. *)
951 fun (shortname, (ret, _, optargs as style), _, _, _, _, _) ->
953 generate_prototype ~extern:false ~semicolon:false ~newline:true
954 ~handle:"g" ~prefix:"guestfs_"
957 generate_prototype ~extern:false ~semicolon:false ~newline:true
958 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
961 pr " int trace_flag = g->trace;\n";
962 pr " FILE *trace_fp;\n";
964 | RErr | RInt _ | RBool _ ->
969 pr " const char *r;\n"
970 | RConstOptString _ ->
971 pr " const char *r;\n"
972 | RString _ | RBufferOut _ ->
974 | RStringList _ | RHashtable _ ->
976 | RStruct (_, typ) ->
977 pr " struct guestfs_%s *r;\n" typ
978 | RStructList (_, typ) ->
979 pr " struct guestfs_%s_list *r;\n" typ
982 check_null_strings shortname style;
983 reject_unknown_optargs shortname style;
984 trace_call shortname style;
985 pr " r = guestfs__%s " shortname;
986 generate_c_call_args ~handle:"g" style;
989 (match errcode_of_ret ret with
990 | (`ErrorIsMinusOne | `ErrorIsNULL) as errcode ->
991 pr " if (r != %s) {\n" (string_of_errcode errcode);
992 trace_return ~indent:4 shortname style "r";
994 trace_return_error ~indent:4 shortname style errcode;
996 | `CannotReturnError ->
997 trace_return shortname style "r";
1003 ) non_daemon_functions;
1005 (* Client-side stubs for each function. *)
1007 fun (shortname, (ret, args, optargs as style), _, _, _, _, _) ->
1008 let name = "guestfs_" ^ shortname in
1010 match errcode_of_ret ret with
1011 | `CannotReturnError -> assert false
1012 | (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
1014 (* Generate the action stub. *)
1015 if optargs = [] then
1016 generate_prototype ~extern:false ~semicolon:false ~newline:true
1017 ~handle:"g" ~prefix:"guestfs_" shortname style
1019 generate_prototype ~extern:false ~semicolon:false ~newline:true
1020 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv"
1021 ~optarg_proto:Argv shortname style;
1027 | _ -> pr " struct %s_args args;\n" name
1030 pr " guestfs_message_header hdr;\n";
1031 pr " guestfs_message_error err;\n";
1035 | RConstString _ | RConstOptString _ ->
1036 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
1038 | RBool _ | RString _ | RStringList _
1039 | RStruct _ | RStructList _
1040 | RHashtable _ | RBufferOut _ ->
1041 pr " struct %s_ret ret;\n" name;
1044 pr " int serial;\n";
1046 pr " int trace_flag = g->trace;\n";
1047 pr " FILE *trace_fp;\n";
1049 | RErr | RInt _ | RBool _ ->
1052 pr " int64_t ret_v;\n"
1053 | RConstString _ | RConstOptString _ ->
1054 pr " const char *ret_v;\n"
1055 | RString _ | RBufferOut _ ->
1056 pr " char *ret_v;\n"
1057 | RStringList _ | RHashtable _ ->
1058 pr " char **ret_v;\n"
1059 | RStruct (_, typ) ->
1060 pr " struct guestfs_%s *ret_v;\n" typ
1061 | RStructList (_, typ) ->
1062 pr " struct guestfs_%s_list *ret_v;\n" typ
1066 List.exists (function FileIn _ -> true | _ -> false) args in
1067 if has_filein then (
1068 pr " uint64_t progress_hint = 0;\n";
1069 pr " struct stat progress_stat;\n";
1071 pr " const uint64_t progress_hint = 0;\n";
1074 check_null_strings shortname style;
1075 reject_unknown_optargs shortname style;
1076 trace_call shortname style;
1078 (* Calculate the total size of all FileIn arguments to pass
1079 * as a progress bar hint.
1084 pr " if (stat (%s, &progress_stat) == 0 &&\n" n;
1085 pr " S_ISREG (progress_stat.st_mode))\n";
1086 pr " progress_hint += progress_stat.st_size;\n";
1091 (* Check we are in the right state for sending a request. *)
1092 pr " if (check_state (g, \"%s\") == -1) {\n" shortname;
1093 trace_return_error ~indent:4 shortname style errcode;
1094 pr " return %s;\n" (string_of_errcode errcode);
1096 pr " guestfs___set_busy (g);\n";
1099 (* Send the main header and arguments. *)
1100 if args = [] && optargs = [] then (
1101 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, progress_hint, 0,\n"
1102 (String.uppercase shortname);
1103 pr " NULL, NULL);\n"
1107 | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
1108 pr " args.%s = (char *) %s;\n" n n
1110 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
1111 | StringList n | DeviceList n ->
1112 pr " args.%s.%s_val = (char **) %s;\n" n n n;
1113 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
1115 pr " args.%s = %s;\n" n n
1117 pr " args.%s = %s;\n" n n
1119 pr " args.%s = %s;\n" n n
1120 | FileIn _ | FileOut _ -> ()
1122 pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
1123 pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
1124 trace_return_error ~indent:4 shortname style errcode;
1125 pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
1127 pr " guestfs___end_busy (g);\n";
1128 pr " return %s;\n" (string_of_errcode errcode);
1130 pr " args.%s.%s_val = (char *) %s;\n" n n n;
1131 pr " args.%s.%s_len = %s_size;\n" n n n
1132 | Pointer _ -> assert false
1137 let n = name_of_argt argt in
1138 let uc_shortname = String.uppercase shortname in
1139 let uc_n = String.uppercase n in
1140 pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK))\n"
1146 pr " args.%s = optargs->%s;\n" n n;
1148 pr " args.%s = 0;\n" n
1150 pr " args.%s = (char *) optargs->%s;\n" n n;
1152 pr " args.%s = (char *) \"\";\n" n
1157 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
1158 (String.uppercase shortname);
1159 pr " progress_hint, %s,\n"
1160 (if optargs <> [] then "optargs->bitmask" else "0");
1161 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
1164 pr " if (serial == -1) {\n";
1165 pr " guestfs___end_busy (g);\n";
1166 trace_return_error ~indent:4 shortname style errcode;
1167 pr " return %s;\n" (string_of_errcode errcode);
1171 (* Send any additional files (FileIn) requested. *)
1172 let need_read_reply_label = ref false in
1176 pr " r = guestfs___send_file (g, %s);\n" n;
1177 pr " if (r == -1) {\n";
1178 pr " guestfs___end_busy (g);\n";
1179 trace_return_error ~indent:4 shortname style errcode;
1180 pr " /* daemon will send an error reply which we discard */\n";
1181 pr " guestfs___recv_discard (g, \"%s\");\n" shortname;
1182 pr " return %s;\n" (string_of_errcode errcode);
1184 pr " if (r == -2) /* daemon cancelled */\n";
1185 pr " goto read_reply;\n";
1186 need_read_reply_label := true;
1191 (* Wait for the reply from the remote end. *)
1192 if !need_read_reply_label then pr " read_reply:\n";
1193 pr " memset (&hdr, 0, sizeof hdr);\n";
1194 pr " memset (&err, 0, sizeof err);\n";
1195 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
1197 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
1201 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
1204 pr " if (r == -1) {\n";
1205 pr " guestfs___end_busy (g);\n";
1206 trace_return_error ~indent:4 shortname style errcode;
1207 pr " return %s;\n" (string_of_errcode errcode);
1211 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
1212 (String.uppercase shortname);
1213 pr " guestfs___end_busy (g);\n";
1214 trace_return_error ~indent:4 shortname style errcode;
1215 pr " return %s;\n" (string_of_errcode errcode);
1219 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
1220 trace_return_error ~indent:4 shortname style errcode;
1221 pr " int errnum = 0;\n";
1222 pr " if (err.errno_string[0] != '\\0')\n";
1223 pr " errnum = guestfs___string_to_errno (err.errno_string);\n";
1224 pr " if (errnum <= 0)\n";
1225 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n"
1228 pr " guestfs_error_errno (g, errnum, \"%%s: %%s\", \"%s\",\n"
1230 pr " err.error_message);\n";
1231 pr " free (err.error_message);\n";
1232 pr " free (err.errno_string);\n";
1233 pr " guestfs___end_busy (g);\n";
1234 pr " return %s;\n" (string_of_errcode errcode);
1238 (* Expecting to receive further files (FileOut)? *)
1242 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
1243 pr " guestfs___end_busy (g);\n";
1244 trace_return_error ~indent:4 shortname style errcode;
1245 pr " return %s;\n" (string_of_errcode errcode);
1251 pr " guestfs___end_busy (g);\n";
1256 | RInt n | RInt64 n | RBool n ->
1257 pr " ret_v = ret.%s;\n" n
1258 | RConstString _ | RConstOptString _ ->
1259 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
1261 pr " ret_v = ret.%s; /* caller will free */\n" n
1262 | RStringList n | RHashtable n ->
1263 pr " /* caller will free this, but we need to add a NULL entry */\n";
1264 pr " ret.%s.%s_val =\n" n n;
1265 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
1266 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
1268 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
1269 pr " ret_v = ret.%s.%s_val;\n" n n
1271 pr " /* caller will free this */\n";
1272 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1273 | RStructList (n, _) ->
1274 pr " /* caller will free this */\n";
1275 pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
1277 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
1278 pr " * _val might be NULL here. To make the API saner for\n";
1279 pr " * callers, we turn this case into a unique pointer (using\n";
1280 pr " * malloc(1)).\n";
1282 pr " if (ret.%s.%s_len > 0) {\n" n n;
1283 pr " *size_r = ret.%s.%s_len;\n" n n;
1284 pr " ret_v = ret.%s.%s_val; /* caller will free */\n" n n;
1286 pr " free (ret.%s.%s_val);\n" n n;
1287 pr " char *p = safe_malloc (g, 1);\n";
1288 pr " *size_r = ret.%s.%s_len;\n" n n;
1292 trace_return shortname style "ret_v";
1293 pr " return ret_v;\n";
1297 (* Functions to free structures. *)
1298 pr "/* Structure-freeing functions. These rely on the fact that the\n";
1299 pr " * structure format is identical to the XDR format. See note in\n";
1300 pr " * generator.ml.\n";
1307 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
1309 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
1315 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
1317 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
1324 (* Functions which have optional arguments have two generated variants. *)
1327 | shortname, (ret, args, (_::_ as optargs) as style), _, _, _, _, _ ->
1328 let uc_shortname = String.uppercase shortname in
1330 (* Get the name of the last regular argument. *)
1334 | args -> name_of_argt (List.hd (List.rev args)) in
1338 | RErr | RInt _ | RBool _ -> "int "
1339 | RInt64 _ -> "int64_t "
1340 | RConstString _ | RConstOptString _ -> "const char *"
1341 | RString _ | RBufferOut _ -> "char *"
1342 | RStringList _ | RHashtable _ -> "char **"
1343 | RStruct (_, typ) -> sprintf "struct guestfs_%s *" typ
1344 | RStructList (_, typ) ->
1345 sprintf "struct guestfs_%s_list *" typ in
1347 (* The regular variable args function, just calls the _va variant. *)
1348 generate_prototype ~extern:false ~semicolon:false ~newline:true
1349 ~handle:"g" ~prefix:"guestfs_" shortname style;
1351 pr " va_list optargs;\n";
1353 pr " va_start (optargs, %s);\n" last_arg;
1354 pr " %sr = guestfs_%s_va " rtype shortname;
1355 generate_c_call_args ~handle:"g" style;
1357 pr " va_end (optargs);\n";
1362 generate_prototype ~extern:false ~semicolon:false ~newline:true
1363 ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
1366 pr " struct guestfs_%s_argv optargs_s;\n" shortname;
1367 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" shortname;
1370 pr " optargs_s.bitmask = 0;\n";
1372 pr " while ((i = va_arg (args, int)) >= 0) {\n";
1373 pr " switch (i) {\n";
1377 let n = name_of_argt argt in
1378 let uc_n = String.uppercase n in
1379 pr " case GUESTFS_%s_%s:\n" uc_shortname uc_n;
1380 pr " optargs_s.%s = va_arg (args, " n;
1382 | Bool _ | Int _ -> pr "int"
1383 | Int64 _ -> pr "int64_t"
1384 | String _ -> pr "const char *"
1392 match errcode_of_ret ret with
1393 | `CannotReturnError -> assert false
1394 | (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
1397 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";
1398 pr " \"%s\", i);\n" shortname;
1399 pr " return %s;\n" (string_of_errcode errcode);
1402 pr " uint64_t i_mask = UINT64_C(1) << i;\n";
1403 pr " if (optargs_s.bitmask & i_mask) {\n";
1404 pr " error (g, \"%%s: same optional argument specified more than once\",\n";
1405 pr " \"%s\");\n" shortname;
1406 pr " return %s;\n" (string_of_errcode errcode);
1408 pr " optargs_s.bitmask |= i_mask;\n";
1411 pr " return guestfs_%s_argv " shortname;
1412 generate_c_call_args ~handle:"g" style;
1416 ) all_functions_sorted
1418 (* Generate the linker script which controls the visibility of
1419 * symbols in the public ABI and ensures no other symbols get
1420 * exported accidentally.
1422 and generate_linker_script () =
1423 generate_header HashStyle GPLv2plus;
1428 "guestfs_delete_event_callback";
1429 "guestfs_first_private";
1430 "guestfs_get_error_handler";
1431 "guestfs_get_out_of_memory_handler";
1432 "guestfs_get_private";
1433 "guestfs_last_errno";
1434 "guestfs_last_error";
1435 "guestfs_next_private";
1436 "guestfs_set_close_callback";
1437 "guestfs_set_error_handler";
1438 "guestfs_set_event_callback";
1439 "guestfs_set_launch_done_callback";
1440 "guestfs_set_log_message_callback";
1441 "guestfs_set_out_of_memory_handler";
1442 "guestfs_set_private";
1443 "guestfs_set_progress_callback";
1444 "guestfs_set_subprocess_quit_callback";
1446 (* Unofficial parts of the API: the bindings code use these
1447 * functions, so it is useful to export them.
1449 "guestfs_safe_calloc";
1450 "guestfs_safe_malloc";
1451 "guestfs_safe_strdup";
1452 "guestfs_safe_memdup";
1454 "guestfs___for_each_disk";
1460 | name, (_, _, []), _, _, _, _, _ -> ["guestfs_" ^ name]
1461 | name, (_, _, _), _, _, _, _, _ ->
1463 "guestfs_" ^ name ^ "_va";
1464 "guestfs_" ^ name ^ "_argv"]
1469 List.map (fun (typ, _) ->
1470 ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
1473 let globals = List.sort compare (globals @ functions @ structs) in
1477 List.iter (pr " %s;\n") globals;
1484 and generate_max_proc_nr () =
1485 pr "%d\n" max_proc_nr