78ba5ec7dc98391e836337f3ecc26d7cc6f916d0
[libguestfs.git] / generator / generator_c.ml
1 (* libguestfs
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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
17  *)
18
19 (* Please read generator/README first. *)
20
21 open Printf
22
23 open Generator_types
24 open Generator_utils
25 open Generator_pr
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
30
31 (* Generate C API. *)
32
33 (* Generate a C function prototype. *)
34 let rec generate_prototype ?(extern = true) ?(static = false)
35     ?(semicolon = true)
36     ?(single_line = false) ?(newline = false) ?(in_daemon = false)
37     ?(prefix = "")
38     ?handle name style =
39   if extern then pr "extern ";
40   if static then pr "static ";
41   (match fst style with
42    | RErr -> pr "int "
43    | RInt _ -> pr "int "
44    | RInt64 _ -> pr "int64_t "
45    | RBool _ -> pr "int "
46    | RConstString _ | RConstOptString _ -> pr "const char *"
47    | RString _ | RBufferOut _ -> pr "char *"
48    | RStringList _ | RHashtable _ -> pr "char **"
49    | RStruct (_, typ) ->
50        if not in_daemon then pr "struct guestfs_%s *" typ
51        else pr "guestfs_int_%s *" typ
52    | RStructList (_, typ) ->
53        if not in_daemon then pr "struct guestfs_%s_list *" typ
54        else pr "guestfs_int_%s_list *" typ
55   );
56   let is_RBufferOut = match fst style with RBufferOut _ -> true | _ -> false in
57   pr "%s%s (" prefix name;
58   if handle = None && List.length (snd style) = 0 && not is_RBufferOut then
59     pr "void"
60   else (
61     let comma = ref false in
62     (match handle with
63      | None -> ()
64      | Some handle -> pr "guestfs_h *%s" handle; comma := true
65     );
66     let next () =
67       if !comma then (
68         if single_line then pr ", " else pr ",\n\t\t"
69       );
70       comma := true
71     in
72     List.iter (
73       function
74       | Pathname n
75       | Device n | Dev_or_Path n
76       | String n
77       | OptString n
78       | Key n ->
79           next ();
80           pr "const char *%s" n
81       | StringList n | DeviceList n ->
82           next ();
83           pr "char *const *%s" n
84       | Bool n -> next (); pr "int %s" n
85       | Int n -> next (); pr "int %s" n
86       | Int64 n -> next (); pr "int64_t %s" n
87       | FileIn n
88       | FileOut n ->
89           if not in_daemon then (next (); pr "const char *%s" n)
90       | BufferIn n ->
91           next ();
92           pr "const char *%s" n;
93           next ();
94           pr "size_t %s_size" n
95     ) (snd style);
96     if is_RBufferOut then (next (); pr "size_t *size_r");
97   );
98   pr ")";
99   if semicolon then pr ";";
100   if newline then pr "\n"
101
102 (* Generate C call arguments, eg "(handle, foo, bar)" *)
103 and generate_c_call_args ?handle ?(decl = false) style =
104   pr "(";
105   let comma = ref false in
106   let next () =
107     if !comma then pr ", ";
108     comma := true
109   in
110   (match handle with
111    | None -> ()
112    | Some handle -> pr "%s" handle; comma := true
113   );
114   List.iter (
115     function
116     | BufferIn n ->
117         next ();
118         pr "%s, %s_size" n n
119     | arg ->
120         next ();
121         pr "%s" (name_of_argt arg)
122   ) (snd style);
123   (* For RBufferOut calls, add implicit &size parameter. *)
124   if not decl then (
125     match fst style with
126     | RBufferOut _ ->
127         next ();
128         pr "&size"
129     | _ -> ()
130   );
131   pr ")"
132
133 (* Generate the pod documentation for the C API. *)
134 and generate_actions_pod () =
135   List.iter (
136     fun (shortname, style, _, flags, _, _, longdesc) ->
137       if not (List.mem NotInDocs flags) then (
138         let name = "guestfs_" ^ shortname in
139         pr "=head2 %s\n\n" name;
140         pr " ";
141         generate_prototype ~extern:false ~handle:"g" name style;
142         pr "\n\n";
143         pr "%s\n\n" longdesc;
144         (match fst style with
145          | RErr ->
146              pr "This function returns 0 on success or -1 on error.\n\n"
147          | RInt _ ->
148              pr "On error this function returns -1.\n\n"
149          | RInt64 _ ->
150              pr "On error this function returns -1.\n\n"
151          | RBool _ ->
152              pr "This function returns a C truth value on success or -1 on error.\n\n"
153          | RConstString _ ->
154              pr "This function returns a string, or NULL on error.
155 The string is owned by the guest handle and must I<not> be freed.\n\n"
156          | RConstOptString _ ->
157              pr "This function returns a string which may be NULL.
158 There is no way to return an error from this function.
159 The string is owned by the guest handle and must I<not> be freed.\n\n"
160          | RString _ ->
161              pr "This function returns a string, or NULL on error.
162 I<The caller must free the returned string after use>.\n\n"
163          | RStringList _ ->
164              pr "This function returns a NULL-terminated array of strings
165 (like L<environ(3)>), or NULL if there was an error.
166 I<The caller must free the strings and the array after use>.\n\n"
167          | RStruct (_, typ) ->
168              pr "This function returns a C<struct guestfs_%s *>,
169 or NULL if there was an error.
170 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
171          | RStructList (_, typ) ->
172              pr "This function returns a C<struct guestfs_%s_list *>
173 (see E<lt>guestfs-structs.hE<gt>),
174 or NULL if there was an error.
175 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
176          | RHashtable _ ->
177              pr "This function returns a NULL-terminated array of
178 strings, or NULL if there was an error.
179 The array of strings will always have length C<2n+1>, where
180 C<n> keys and values alternate, followed by the trailing NULL entry.
181 I<The caller must free the strings and the array after use>.\n\n"
182          | RBufferOut _ ->
183              pr "This function returns a buffer, or NULL on error.
184 The size of the returned buffer is written to C<*size_r>.
185 I<The caller must free the returned buffer after use>.\n\n"
186         );
187         if List.mem Progress flags then
188           pr "%s\n\n" progress_message;
189         if List.mem ProtocolLimitWarning flags then
190           pr "%s\n\n" protocol_limit_warning;
191         if List.mem DangerWillRobinson flags then
192           pr "%s\n\n" danger_will_robinson;
193         if List.exists (function Key _ -> true | _ -> false) (snd style) then
194           pr "This function takes a key or passphrase parameter which
195 could contain sensitive material.  Read the section
196 L</KEYS AND PASSPHRASES> for more information.\n\n";
197         match deprecation_notice flags with
198         | None -> ()
199         | Some txt -> pr "%s\n\n" txt
200       )
201   ) all_functions_sorted
202
203 and generate_structs_pod () =
204   (* Structs documentation. *)
205   List.iter (
206     fun (typ, cols) ->
207       pr "=head2 guestfs_%s\n" typ;
208       pr "\n";
209       pr " struct guestfs_%s {\n" typ;
210       List.iter (
211         function
212         | name, FChar -> pr "   char %s;\n" name
213         | name, FUInt32 -> pr "   uint32_t %s;\n" name
214         | name, FInt32 -> pr "   int32_t %s;\n" name
215         | name, (FUInt64|FBytes) -> pr "   uint64_t %s;\n" name
216         | name, FInt64 -> pr "   int64_t %s;\n" name
217         | name, FString -> pr "   char *%s;\n" name
218         | name, FBuffer ->
219             pr "   /* The next two fields describe a byte array. */\n";
220             pr "   uint32_t %s_len;\n" name;
221             pr "   char *%s;\n" name
222         | name, FUUID ->
223             pr "   /* The next field is NOT nul-terminated, be careful when printing it: */\n";
224             pr "   char %s[32];\n" name
225         | name, FOptPercent ->
226             pr "   /* The next field is [0..100] or -1 meaning 'not present': */\n";
227             pr "   float %s;\n" name
228       ) cols;
229       pr " };\n";
230       pr " \n";
231       pr " struct guestfs_%s_list {\n" typ;
232       pr "   uint32_t len; /* Number of elements in list. */\n";
233       pr "   struct guestfs_%s *val; /* Elements. */\n" typ;
234       pr " };\n";
235       pr " \n";
236       pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
237       pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
238         typ typ;
239       pr "\n"
240   ) structs
241
242 and generate_availability_pod () =
243   (* Availability documentation. *)
244   pr "=over 4\n";
245   pr "\n";
246   List.iter (
247     fun (group, functions) ->
248       pr "=item B<%s>\n" group;
249       pr "\n";
250       pr "The following functions:\n";
251       List.iter (pr "L</guestfs_%s>\n") functions;
252       pr "\n"
253   ) optgroups;
254   pr "=back\n";
255   pr "\n"
256
257 (* Generate the guestfs-structs.h file. *)
258 and generate_structs_h () =
259   generate_header CStyle LGPLv2plus;
260
261   (* This is a public exported header file containing various
262    * structures.  The structures are carefully written to have
263    * exactly the same in-memory format as the XDR structures that
264    * we use on the wire to the daemon.  The reason for creating
265    * copies of these structures here is just so we don't have to
266    * export the whole of guestfs_protocol.h (which includes much
267    * unrelated and XDR-dependent stuff that we don't want to be
268    * public, or required by clients).
269    *
270    * To reiterate, we will pass these structures to and from the
271    * client with a simple assignment or memcpy, so the format
272    * must be identical to what rpcgen / the RFC defines.
273    *)
274
275   (* Public structures. *)
276   List.iter (
277     fun (typ, cols) ->
278       pr "struct guestfs_%s {\n" typ;
279       List.iter (
280         function
281         | name, FChar -> pr "  char %s;\n" name
282         | name, FString -> pr "  char *%s;\n" name
283         | name, FBuffer ->
284             pr "  uint32_t %s_len;\n" name;
285             pr "  char *%s;\n" name
286         | name, FUUID -> pr "  char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
287         | name, FUInt32 -> pr "  uint32_t %s;\n" name
288         | name, FInt32 -> pr "  int32_t %s;\n" name
289         | name, (FUInt64|FBytes) -> pr "  uint64_t %s;\n" name
290         | name, FInt64 -> pr "  int64_t %s;\n" name
291         | name, FOptPercent -> pr "  float %s; /* [0..100] or -1 */\n" name
292       ) cols;
293       pr "};\n";
294       pr "\n";
295       pr "struct guestfs_%s_list {\n" typ;
296       pr "  uint32_t len;\n";
297       pr "  struct guestfs_%s *val;\n" typ;
298       pr "};\n";
299       pr "\n";
300       pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
301       pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
302       pr "\n"
303   ) structs
304
305 (* Generate the guestfs-actions.h file. *)
306 and generate_actions_h () =
307   generate_header CStyle LGPLv2plus;
308   List.iter (
309     fun (shortname, style, _, flags, _, _, _) ->
310       let name = "guestfs_" ^ shortname in
311
312       let deprecated =
313         List.exists (function DeprecatedBy _ -> true | _ -> false) flags in
314       let test0 =
315         String.length shortname >= 5 && String.sub shortname 0 5 = "test0" in
316       let debug =
317         String.length shortname >= 5 && String.sub shortname 0 5 = "debug" in
318       if not deprecated && not test0 && not debug then
319         pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase shortname);
320
321       generate_prototype ~single_line:true ~newline:true ~handle:"g"
322         name style
323   ) all_functions_sorted
324
325 (* Generate the guestfs-internal-actions.h file. *)
326 and generate_internal_actions_h () =
327   generate_header CStyle LGPLv2plus;
328   List.iter (
329     fun (shortname, style, _, _, _, _, _) ->
330       let name = "guestfs__" ^ shortname in
331       generate_prototype ~single_line:true ~newline:true ~handle:"g"
332         name style
333   ) non_daemon_functions
334
335 (* Generate the client-side dispatch stubs. *)
336 and generate_client_actions () =
337   generate_header CStyle LGPLv2plus;
338
339   pr "\
340 #include <stdio.h>
341 #include <stdlib.h>
342 #include <stdint.h>
343 #include <string.h>
344 #include <inttypes.h>
345
346 #include \"guestfs.h\"
347 #include \"guestfs-internal.h\"
348 #include \"guestfs-internal-actions.h\"
349 #include \"guestfs_protocol.h\"
350
351 /* Check the return message from a call for validity. */
352 static int
353 check_reply_header (guestfs_h *g,
354                     const struct guestfs_message_header *hdr,
355                     unsigned int proc_nr, unsigned int serial)
356 {
357   if (hdr->prog != GUESTFS_PROGRAM) {
358     error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
359     return -1;
360   }
361   if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
362     error (g, \"wrong protocol version (%%d/%%d)\",
363            hdr->vers, GUESTFS_PROTOCOL_VERSION);
364     return -1;
365   }
366   if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
367     error (g, \"unexpected message direction (%%d/%%d)\",
368            hdr->direction, GUESTFS_DIRECTION_REPLY);
369     return -1;
370   }
371   if (hdr->proc != proc_nr) {
372     error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
373     return -1;
374   }
375   if (hdr->serial != serial) {
376     error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
377     return -1;
378   }
379
380   return 0;
381 }
382
383 /* Check we are in the right state to run a high-level action. */
384 static int
385 check_state (guestfs_h *g, const char *caller)
386 {
387   if (!guestfs__is_ready (g)) {
388     if (guestfs__is_config (g) || guestfs__is_launching (g))
389       error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
390         caller);
391     else
392       error (g, \"%%s called from the wrong state, %%d != READY\",
393         caller, guestfs__get_state (g));
394     return -1;
395   }
396   return 0;
397 }
398
399 ";
400
401   let error_code_of = function
402     | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
403     | RConstString _ | RConstOptString _
404     | RString _ | RStringList _
405     | RStruct _ | RStructList _
406     | RHashtable _ | RBufferOut _ -> "NULL"
407   in
408
409   (* Generate code to check String-like parameters are not passed in
410    * as NULL (returning an error if they are).
411    *)
412   let check_null_strings shortname style =
413     let pr_newline = ref false in
414     List.iter (
415       function
416       (* parameters which should not be NULL *)
417       | String n
418       | Device n
419       | Pathname n
420       | Dev_or_Path n
421       | FileIn n
422       | FileOut n
423       | BufferIn n
424       | StringList n
425       | DeviceList n
426       | Key n ->
427           pr "  if (%s == NULL) {\n" n;
428           pr "    error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
429           pr "           \"%s\", \"%s\");\n" shortname n;
430           pr "    return %s;\n" (error_code_of (fst style));
431           pr "  }\n";
432           pr_newline := true
433
434       (* can be NULL *)
435       | OptString _
436
437       (* not applicable *)
438       | Bool _
439       | Int _
440       | Int64 _ -> ()
441     ) (snd style);
442
443     if !pr_newline then pr "\n";
444   in
445
446   (* Generate code to generate guestfish call traces. *)
447   let trace_call shortname style =
448     pr "  if (guestfs__get_trace (g)) {\n";
449
450     let needs_i =
451       List.exists (function
452                    | StringList _ | DeviceList _ -> true
453                    | _ -> false) (snd style) in
454     if needs_i then (
455       pr "    size_t i;\n";
456       pr "\n"
457     );
458
459     pr "    fprintf (stderr, \"%s\");\n" shortname;
460     List.iter (
461       function
462       | String n                        (* strings *)
463       | Device n
464       | Pathname n
465       | Dev_or_Path n
466       | FileIn n
467       | FileOut n
468       | BufferIn n
469       | Key n ->
470           (* guestfish doesn't support string escaping, so neither do we *)
471           pr "    fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
472       | OptString n ->                  (* string option *)
473           pr "    if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
474           pr "    else fprintf (stderr, \" null\");\n"
475       | StringList n
476       | DeviceList n ->                 (* string list *)
477           pr "    fputc (' ', stderr);\n";
478           pr "    fputc ('\"', stderr);\n";
479           pr "    for (i = 0; %s[i]; ++i) {\n" n;
480           pr "      if (i > 0) fputc (' ', stderr);\n";
481           pr "      fputs (%s[i], stderr);\n" n;
482           pr "    }\n";
483           pr "    fputc ('\"', stderr);\n";
484       | Bool n ->                       (* boolean *)
485           pr "    fputs (%s ? \" true\" : \" false\", stderr);\n" n
486       | Int n ->                        (* int *)
487           pr "    fprintf (stderr, \" %%d\", %s);\n" n
488       | Int64 n ->
489           pr "    fprintf (stderr, \" %%\" PRIi64, %s);\n" n
490     ) (snd style);
491     pr "    fputc ('\\n', stderr);\n";
492     pr "  }\n";
493     pr "\n";
494   in
495
496   (* For non-daemon functions, generate a wrapper around each function. *)
497   List.iter (
498     fun (shortname, style, _, _, _, _, _) ->
499       let name = "guestfs_" ^ shortname in
500
501       generate_prototype ~extern:false ~semicolon:false ~newline:true
502         ~handle:"g" name style;
503       pr "{\n";
504       check_null_strings shortname style;
505       trace_call shortname style;
506       pr "  return guestfs__%s " shortname;
507       generate_c_call_args ~handle:"g" style;
508       pr ";\n";
509       pr "}\n";
510       pr "\n"
511   ) non_daemon_functions;
512
513   (* Client-side stubs for each function. *)
514   List.iter (
515     fun (shortname, style, _, _, _, _, _) ->
516       let name = "guestfs_" ^ shortname in
517       let error_code = error_code_of (fst style) in
518
519       (* Generate the action stub. *)
520       generate_prototype ~extern:false ~semicolon:false ~newline:true
521         ~handle:"g" name style;
522
523       pr "{\n";
524
525       (match snd style with
526        | [] -> ()
527        | _ -> pr "  struct %s_args args;\n" name
528       );
529
530       pr "  guestfs_message_header hdr;\n";
531       pr "  guestfs_message_error err;\n";
532       let has_ret =
533         match fst style with
534         | RErr -> false
535         | RConstString _ | RConstOptString _ ->
536             failwithf "RConstString|RConstOptString cannot be used by daemon functions"
537         | RInt _ | RInt64 _
538         | RBool _ | RString _ | RStringList _
539         | RStruct _ | RStructList _
540         | RHashtable _ | RBufferOut _ ->
541             pr "  struct %s_ret ret;\n" name;
542             true in
543
544       pr "  int serial;\n";
545       pr "  int r;\n";
546       pr "\n";
547       check_null_strings shortname style;
548       trace_call shortname style;
549       pr "  if (check_state (g, \"%s\") == -1) return %s;\n"
550         shortname error_code;
551       pr "  guestfs___set_busy (g);\n";
552       pr "\n";
553
554       (* Send the main header and arguments. *)
555       (match snd style with
556        | [] ->
557            pr "  serial = guestfs___send (g, GUESTFS_PROC_%s, NULL, NULL);\n"
558              (String.uppercase shortname)
559        | args ->
560            List.iter (
561              function
562              | Pathname n | Device n | Dev_or_Path n | String n | Key n ->
563                  pr "  args.%s = (char *) %s;\n" n n
564              | OptString n ->
565                  pr "  args.%s = %s ? (char **) &%s : NULL;\n" n n n
566              | StringList n | DeviceList n ->
567                  pr "  args.%s.%s_val = (char **) %s;\n" n n n;
568                  pr "  for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
569              | Bool n ->
570                  pr "  args.%s = %s;\n" n n
571              | Int n ->
572                  pr "  args.%s = %s;\n" n n
573              | Int64 n ->
574                  pr "  args.%s = %s;\n" n n
575              | FileIn _ | FileOut _ -> ()
576              | BufferIn n ->
577                  pr "  /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
578                  pr "  if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
579                  pr "    error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
580                    shortname;
581                  pr "    guestfs___end_busy (g);\n";
582                  pr "    return %s;\n" error_code;
583                  pr "  }\n";
584                  pr "  args.%s.%s_val = (char *) %s;\n" n n n;
585                  pr "  args.%s.%s_len = %s_size;\n" n n n
586            ) args;
587            pr "  serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
588              (String.uppercase shortname);
589            pr "        (xdrproc_t) xdr_%s_args, (char *) &args);\n"
590              name;
591       );
592       pr "  if (serial == -1) {\n";
593       pr "    guestfs___end_busy (g);\n";
594       pr "    return %s;\n" error_code;
595       pr "  }\n";
596       pr "\n";
597
598       (* Send any additional files (FileIn) requested. *)
599       let need_read_reply_label = ref false in
600       List.iter (
601         function
602         | FileIn n ->
603             pr "  r = guestfs___send_file (g, %s);\n" n;
604             pr "  if (r == -1) {\n";
605             pr "    guestfs___end_busy (g);\n";
606             pr "    return %s;\n" error_code;
607             pr "  }\n";
608             pr "  if (r == -2) /* daemon cancelled */\n";
609             pr "    goto read_reply;\n";
610             need_read_reply_label := true;
611             pr "\n";
612         | _ -> ()
613       ) (snd style);
614
615       (* Wait for the reply from the remote end. *)
616       if !need_read_reply_label then pr " read_reply:\n";
617       pr "  memset (&hdr, 0, sizeof hdr);\n";
618       pr "  memset (&err, 0, sizeof err);\n";
619       if has_ret then pr "  memset (&ret, 0, sizeof ret);\n";
620       pr "\n";
621       pr "  r = guestfs___recv (g, \"%s\", &hdr, &err,\n        " shortname;
622       if not has_ret then
623         pr "NULL, NULL"
624       else
625         pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
626       pr ");\n";
627
628       pr "  if (r == -1) {\n";
629       pr "    guestfs___end_busy (g);\n";
630       pr "    return %s;\n" error_code;
631       pr "  }\n";
632       pr "\n";
633
634       pr "  if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
635         (String.uppercase shortname);
636       pr "    guestfs___end_busy (g);\n";
637       pr "    return %s;\n" error_code;
638       pr "  }\n";
639       pr "\n";
640
641       pr "  if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
642       pr "    error (g, \"%%s: %%s\", \"%s\", err.error_message);\n" shortname;
643       pr "    free (err.error_message);\n";
644       pr "    guestfs___end_busy (g);\n";
645       pr "    return %s;\n" error_code;
646       pr "  }\n";
647       pr "\n";
648
649       (* Expecting to receive further files (FileOut)? *)
650       List.iter (
651         function
652         | FileOut n ->
653             pr "  if (guestfs___recv_file (g, %s) == -1) {\n" n;
654             pr "    guestfs___end_busy (g);\n";
655             pr "    return %s;\n" error_code;
656             pr "  }\n";
657             pr "\n";
658         | _ -> ()
659       ) (snd style);
660
661       pr "  guestfs___end_busy (g);\n";
662
663       (match fst style with
664        | RErr -> pr "  return 0;\n"
665        | RInt n | RInt64 n | RBool n ->
666            pr "  return ret.%s;\n" n
667        | RConstString _ | RConstOptString _ ->
668            failwithf "RConstString|RConstOptString cannot be used by daemon functions"
669        | RString n ->
670            pr "  return ret.%s; /* caller will free */\n" n
671        | RStringList n | RHashtable n ->
672            pr "  /* caller will free this, but we need to add a NULL entry */\n";
673            pr "  ret.%s.%s_val =\n" n n;
674            pr "    safe_realloc (g, ret.%s.%s_val,\n" n n;
675            pr "                  sizeof (char *) * (ret.%s.%s_len + 1));\n"
676              n n;
677            pr "  ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
678            pr "  return ret.%s.%s_val;\n" n n
679        | RStruct (n, _) ->
680            pr "  /* caller will free this */\n";
681            pr "  return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
682        | RStructList (n, _) ->
683            pr "  /* caller will free this */\n";
684            pr "  return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
685        | RBufferOut n ->
686            pr "  /* RBufferOut is tricky: If the buffer is zero-length, then\n";
687            pr "   * _val might be NULL here.  To make the API saner for\n";
688            pr "   * callers, we turn this case into a unique pointer (using\n";
689            pr "   * malloc(1)).\n";
690            pr "   */\n";
691            pr "  if (ret.%s.%s_len > 0) {\n" n n;
692            pr "    *size_r = ret.%s.%s_len;\n" n n;
693            pr "    return ret.%s.%s_val; /* caller will free */\n" n n;
694            pr "  } else {\n";
695            pr "    free (ret.%s.%s_val);\n" n n;
696            pr "    char *p = safe_malloc (g, 1);\n";
697            pr "    *size_r = ret.%s.%s_len;\n" n n;
698            pr "    return p;\n";
699            pr "  }\n";
700       );
701
702       pr "}\n\n"
703   ) daemon_functions;
704
705   (* Functions to free structures. *)
706   pr "/* Structure-freeing functions.  These rely on the fact that the\n";
707   pr " * structure format is identical to the XDR format.  See note in\n";
708   pr " * generator.ml.\n";
709   pr " */\n";
710   pr "\n";
711
712   List.iter (
713     fun (typ, _) ->
714       pr "void\n";
715       pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
716       pr "{\n";
717       pr "  xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
718       pr "  free (x);\n";
719       pr "}\n";
720       pr "\n";
721
722       pr "void\n";
723       pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
724       pr "{\n";
725       pr "  xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
726       pr "  free (x);\n";
727       pr "}\n";
728       pr "\n";
729
730   ) structs;
731
732 (* Generate the linker script which controls the visibility of
733  * symbols in the public ABI and ensures no other symbols get
734  * exported accidentally.
735  *)
736 and generate_linker_script () =
737   generate_header HashStyle GPLv2plus;
738
739   let globals = [
740     "guestfs_create";
741     "guestfs_close";
742     "guestfs_get_error_handler";
743     "guestfs_get_out_of_memory_handler";
744     "guestfs_get_private";
745     "guestfs_last_error";
746     "guestfs_set_close_callback";
747     "guestfs_set_error_handler";
748     "guestfs_set_launch_done_callback";
749     "guestfs_set_log_message_callback";
750     "guestfs_set_out_of_memory_handler";
751     "guestfs_set_private";
752     "guestfs_set_progress_callback";
753     "guestfs_set_subprocess_quit_callback";
754
755     (* Unofficial parts of the API: the bindings code use these
756      * functions, so it is useful to export them.
757      *)
758     "guestfs_safe_calloc";
759     "guestfs_safe_malloc";
760     "guestfs_safe_strdup";
761     "guestfs_safe_memdup";
762     "guestfs_tmpdir";
763   ] in
764   let functions =
765     List.map (fun (name, _, _, _, _, _, _) -> "guestfs_" ^ name)
766       all_functions in
767   let structs =
768     List.concat (
769       List.map (fun (typ, _) ->
770                   ["guestfs_free_" ^ typ; "guestfs_free_" ^ typ ^ "_list"])
771         structs
772     ) in
773   let globals = List.sort compare (globals @ functions @ structs) in
774
775   pr "{\n";
776   pr "    global:\n";
777   List.iter (pr "        %s;\n") globals;
778   pr "\n";
779
780   pr "    local:\n";
781   pr "        *;\n";
782   pr "};\n"
783
784 and generate_max_proc_nr () =
785   pr "%d\n" max_proc_nr