daemon/readdir: avoid a small leak
[libguestfs.git] / src / generator.ml
index df5ff7e..c65e717 100755 (executable)
@@ -121,7 +121,7 @@ type flags =
   | NotInDocs            (* do not add this function to documentation *)
 
 let protocol_limit_warning =
-  "Because of the message protocol, there is a transfer limit 
+  "Because of the message protocol, there is a transfer limit
 of somewhere between 2MB and 4MB.  To transfer large files you should use
 FTP."
 
@@ -378,7 +378,7 @@ just want to read the image or write access if you want to modify the
 image).
 
 This is equivalent to the qemu parameter
-C<-drive file=filename,cache=off,if=virtio>.
+C<-drive file=filename,cache=off,if=...>.
 
 Note that this call checks for the existence of C<filename>.  This
 stops you from specifying other types of drive which are supported
@@ -412,7 +412,7 @@ handle is closed.  We don't currently have any method to enable
 changes to be committed, although qemu can support this.
 
 This is equivalent to the qemu parameter
-C<-drive file=filename,snapshot=on,if=virtio>.
+C<-drive file=filename,snapshot=on,if=...>.
 
 Note that this call checks for the existence of C<filename>.  This
 stops you from specifying other types of drive which are supported
@@ -641,7 +641,7 @@ see L<guestfs(3)>.");
 let daemon_functions = [
   ("mount", (RErr, [String "device"; String "mountpoint"]), 1, [],
    [InitEmpty, Always, TestOutput (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkfs"; "ext2"; "/dev/sda1"];
        ["mount"; "/dev/sda1"; "/"];
        ["write_file"; "/new"; "new file contents"; "0"];
@@ -738,7 +738,7 @@ The full block device names are returned, eg. C</dev/sda>");
    [InitBasicFS, Always, TestOutputListOfDevices (
       [["list_partitions"]], ["/dev/sda1"]);
     InitEmpty, Always, TestOutputListOfDevices (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
    "list the partitions",
    "\
@@ -753,7 +753,7 @@ call C<guestfs_lvs>.");
    [InitBasicFSonLVM, Always, TestOutputListOfDevices (
       [["pvs"]], ["/dev/sda1"]);
     InitEmpty, Always, TestOutputListOfDevices (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["pvcreate"; "/dev/sda1"];
        ["pvcreate"; "/dev/sda2"];
        ["pvcreate"; "/dev/sda3"];
@@ -772,7 +772,7 @@ See also C<guestfs_pvs_full>.");
    [InitBasicFSonLVM, Always, TestOutputList (
       [["vgs"]], ["VG"]);
     InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["pvcreate"; "/dev/sda1"];
        ["pvcreate"; "/dev/sda2"];
        ["pvcreate"; "/dev/sda3"];
@@ -793,7 +793,7 @@ See also C<guestfs_vgs_full>.");
    [InitBasicFSonLVM, Always, TestOutputList (
       [["lvs"]], ["/dev/VG/LV"]);
     InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["pvcreate"; "/dev/sda1"];
        ["pvcreate"; "/dev/sda2"];
        ["pvcreate"; "/dev/sda3"];
@@ -1146,7 +1146,7 @@ See also C<guestfs_stat>.");
 
   ("pvcreate", (RErr, [String "device"]), 39, [],
    [InitEmpty, Always, TestOutputListOfDevices (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["pvcreate"; "/dev/sda1"];
        ["pvcreate"; "/dev/sda2"];
        ["pvcreate"; "/dev/sda3"];
@@ -1159,7 +1159,7 @@ as C</dev/sda1>.");
 
   ("vgcreate", (RErr, [String "volgroup"; StringList "physvols"]), 40, [],
    [InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["pvcreate"; "/dev/sda1"];
        ["pvcreate"; "/dev/sda2"];
        ["pvcreate"; "/dev/sda3"];
@@ -1173,7 +1173,7 @@ from the non-empty list of physical volumes C<physvols>.");
 
   ("lvcreate", (RErr, [String "logvol"; String "volgroup"; Int "mbytes"]), 41, [],
    [InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["pvcreate"; "/dev/sda1"];
        ["pvcreate"; "/dev/sda2"];
        ["pvcreate"; "/dev/sda3"];
@@ -1194,7 +1194,7 @@ on the volume group C<volgroup>, with C<size> megabytes.");
 
   ("mkfs", (RErr, [String "fstype"; String "device"]), 42, [],
    [InitEmpty, Always, TestOutput (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkfs"; "ext2"; "/dev/sda1"];
        ["mount"; "/dev/sda1"; "/"];
        ["write_file"; "/new"; "new file contents"; "0"];
@@ -1269,12 +1269,12 @@ use C<guestfs_upload>.");
 
   ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"],
    [InitEmpty, Always, TestOutputListOfDevices (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkfs"; "ext2"; "/dev/sda1"];
        ["mount"; "/dev/sda1"; "/"];
        ["mounts"]], ["/dev/sda1"]);
     InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkfs"; "ext2"; "/dev/sda1"];
        ["mount"; "/dev/sda1"; "/"];
        ["umount"; "/"];
@@ -1301,7 +1301,7 @@ Some internal mounts are not shown.");
        ["mounts"]], []);
     (* check that umount_all can unmount nested mounts correctly: *)
     InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
+      [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
        ["mkfs"; "ext2"; "/dev/sda1"];
        ["mkfs"; "ext2"; "/dev/sda2"];
        ["mkfs"; "ext2"; "/dev/sda3"];
@@ -1825,7 +1825,7 @@ to find out what you can do.");
 
   ("lvremove", (RErr, [String "device"]), 77, [],
    [InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -1833,7 +1833,7 @@ to find out what you can do.");
        ["lvremove"; "/dev/VG/LV1"];
        ["lvs"]], ["/dev/VG/LV2"]);
     InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -1841,7 +1841,7 @@ to find out what you can do.");
        ["lvremove"; "/dev/VG"];
        ["lvs"]], []);
     InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -1858,7 +1858,7 @@ the VG name, C</dev/VG>.");
 
   ("vgremove", (RErr, [String "vgname"]), 78, [],
    [InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -1866,7 +1866,7 @@ the VG name, C</dev/VG>.");
        ["vgremove"; "VG"];
        ["lvs"]], []);
     InitEmpty, Always, TestOutputList (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -1882,7 +1882,7 @@ group (if any).");
 
   ("pvremove", (RErr, [String "device"]), 79, [],
    [InitEmpty, Always, TestOutputListOfDevices (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -1891,7 +1891,7 @@ group (if any).");
        ["pvremove"; "/dev/sda1"];
        ["lvs"]], []);
     InitEmpty, Always, TestOutputListOfDevices (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -1900,7 +1900,7 @@ group (if any).");
        ["pvremove"; "/dev/sda1"];
        ["vgs"]], []);
     InitEmpty, Always, TestOutputListOfDevices (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["pvcreate"; "/dev/sda1"];
        ["vgcreate"; "VG"; "/dev/sda1"];
        ["lvcreate"; "LV1"; "VG"; "50"];
@@ -2181,7 +2181,7 @@ the human-readable, canonical hex dump of the file.");
 
   ("zerofree", (RErr, [String "device"]), 97, [],
    [InitNone, Always, TestOutput (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkfs"; "ext3"; "/dev/sda1"];
        ["mount"; "/dev/sda1"; "/"];
        ["write_file"; "/new"; "test file"; "0"];
@@ -2279,7 +2279,7 @@ are activated or deactivated.");
 
   ("lvresize", (RErr, [String "device"; Int "mbytes"]), 105, [],
    [InitNone, Always, TestOutput (
-    [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+    [["sfdiskM"; "/dev/sda"; ","];
      ["pvcreate"; "/dev/sda1"];
      ["vgcreate"; "VG"; "/dev/sda1"];
      ["lvcreate"; "LV"; "VG"; "10"];
@@ -2370,11 +2370,11 @@ Sleep for C<secs> seconds.");
 
   ("ntfs_3g_probe", (RInt "status", [Bool "rw"; String "device"]), 110, [],
    [InitNone, Always, TestOutputInt (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkfs"; "ntfs"; "/dev/sda1"];
        ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 0);
     InitNone, Always, TestOutputInt (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkfs"; "ext2"; "/dev/sda1"];
        ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 12)],
    "probe NTFS volume",
@@ -2661,7 +2661,7 @@ the command C<mount -o loop file mountpoint>.");
 
   ("mkswap", (RErr, [String "device"]), 130, [],
    [InitEmpty, Always, TestRun (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkswap"; "/dev/sda1"]])],
    "create a swap partition",
    "\
@@ -2669,7 +2669,7 @@ Create a swap partition on C<device>.");
 
   ("mkswap_L", (RErr, [String "label"; String "device"]), 131, [],
    [InitEmpty, Always, TestRun (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkswap_L"; "hello"; "/dev/sda1"]])],
    "create a swap partition with a label",
    "\
@@ -2677,7 +2677,7 @@ Create a swap partition on C<device> with label C<label>.");
 
   ("mkswap_U", (RErr, [String "uuid"; String "device"]), 132, [],
    [InitEmpty, Always, TestRun (
-      [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+      [["sfdiskM"; "/dev/sda"; ","];
        ["mkswap_U"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"; "/dev/sda1"]])],
    "create a swap partition with an explicit UUID",
    "\
@@ -2767,6 +2767,18 @@ This function is primarily intended for use by programs.  To
 get a simple list of names, use C<guestfs_ls>.  To get a printable
 directory for human consumption, use C<guestfs_ll>.");
 
+  ("sfdiskM", (RErr, [String "device"; StringList "lines"]), 139, [DangerWillRobinson],
+   [],
+   "create partitions on a block device",
+   "\
+This is a simplified interface to the C<guestfs_sfdisk>
+command, where partition sizes are specified in megabytes
+only (rounded to the nearest cylinder) and you don't need
+to specify the cyls, heads and sectors parameters which
+were rarely if ever used anyway.
+
+See also C<guestfs_sfdisk> and the L<sfdisk(8)> manpage.");
+
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions
@@ -2889,6 +2901,17 @@ type callt =
   | CallInt of int
   | CallBool of bool
 
+(* Used to memoize the result of pod2text. *)
+let pod2text_memo_filename = "src/.pod2text.data"
+let pod2text_memo : ((int * string * string), string list) Hashtbl.t =
+  try
+    let chan = open_in pod2text_memo_filename in
+    let v = input_value chan in
+    close_in chan;
+    v
+  with
+    _ -> Hashtbl.create 13
+
 (* Useful functions.
  * Note we don't want to use any external OCaml libraries which
  * makes this a bit harder than it should be.
@@ -4307,6 +4330,22 @@ and generate_daemon_actions () =
 
   ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
 
+(* Generate a list of function names, for debugging in the daemon.. *)
+and generate_daemon_names () =
+  generate_header CStyle GPLv2;
+
+  pr "#include <config.h>\n";
+  pr "\n";
+  pr "#include \"daemon.h\"\n";
+  pr "\n";
+
+  pr "/* This array is indexed by proc_nr.  See guestfs_protocol.x. */\n";
+  pr "const char *function_names[] = {\n";
+  List.iter (
+    fun (name, _, proc_nr, _, _, _, _) -> pr "  [%d] = \"%s\",\n" proc_nr name
+  ) daemon_functions;
+  pr "};\n";
+
 (* Generate the tests. *)
 and generate_tests () =
   generate_header CStyle GPLv2;
@@ -4614,7 +4653,7 @@ and generate_one_test_body name i test_name init test =
         [["blockdev_setrw"; "/dev/sda"];
          ["umount_all"];
          ["lvm_remove_all"];
-         ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+         ["sfdiskM"; "/dev/sda"; ","];
          ["mkfs"; "ext2"; "/dev/sda1"];
          ["mount"; "/dev/sda1"; "/"]]
    | InitBasicFSonLVM ->
@@ -4624,7 +4663,7 @@ and generate_one_test_body name i test_name init test =
         [["blockdev_setrw"; "/dev/sda"];
          ["umount_all"];
          ["lvm_remove_all"];
-         ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
+         ["sfdiskM"; "/dev/sda"; ","];
          ["pvcreate"; "/dev/sda1"];
          ["vgcreate"; "VG"; "/dev/sda1"];
          ["lvcreate"; "LV"; "VG"; "8"];
@@ -6357,7 +6396,7 @@ Sys::Guestfs - Perl bindings for libguestfs
 =head1 SYNOPSIS
 
  use Sys::Guestfs;
+
  my $h = Sys::Guestfs->new ();
  $h->add_drive ('guest.img');
  $h->launch ();
@@ -7032,32 +7071,42 @@ class GuestFS:
 (* Useful if you need the longdesc POD text as plain text.  Returns a
  * list of lines.
  *
- * This is the slowest thing about autogeneration.
+ * Because this is very slow (the slowest part of autogeneration),
+ * we memoize the results.
  *)
 and pod2text ~width name longdesc =
-  let filename, chan = Filename.open_temp_file "gen" ".tmp" in
-  fprintf chan "=head1 %s\n\n%s\n" name longdesc;
-  close_out chan;
-  let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
-  let chan = Unix.open_process_in cmd in
-  let lines = ref [] in
-  let rec loop i =
-    let line = input_line chan in
-    if i = 1 then              (* discard the first line of output *)
-      loop (i+1)
-    else (
-      let line = triml line in
-      lines := line :: !lines;
-      loop (i+1)
-    ) in
-  let lines = try loop 1 with End_of_file -> List.rev !lines in
-  Unix.unlink filename;
-  match Unix.close_process_in chan with
-  | Unix.WEXITED 0 -> lines
-  | Unix.WEXITED i ->
-      failwithf "pod2text: process exited with non-zero status (%d)" i
-  | Unix.WSIGNALED i | Unix.WSTOPPED i ->
-      failwithf "pod2text: process signalled or stopped by signal %d" i
+  let key = width, name, longdesc in
+  try Hashtbl.find pod2text_memo key
+  with Not_found ->
+    let filename, chan = Filename.open_temp_file "gen" ".tmp" in
+    fprintf chan "=head1 %s\n\n%s\n" name longdesc;
+    close_out chan;
+    let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
+    let chan = Unix.open_process_in cmd in
+    let lines = ref [] in
+    let rec loop i =
+      let line = input_line chan in
+      if i = 1 then            (* discard the first line of output *)
+       loop (i+1)
+      else (
+       let line = triml line in
+       lines := line :: !lines;
+       loop (i+1)
+      ) in
+    let lines = try loop 1 with End_of_file -> List.rev !lines in
+    Unix.unlink filename;
+    (match Unix.close_process_in chan with
+     | Unix.WEXITED 0 -> ()
+     | Unix.WEXITED i ->
+        failwithf "pod2text: process exited with non-zero status (%d)" i
+     | Unix.WSIGNALED i | Unix.WSTOPPED i ->
+        failwithf "pod2text: process signalled or stopped by signal %d" i
+    );
+    Hashtbl.add pod2text_memo key lines;
+    let chan = open_out pod2text_memo_filename in
+    output_value chan pod2text_memo;
+    close_out chan;
+    lines
 
 (* Generate ruby bindings. *)
 and generate_ruby_c () =
@@ -8095,6 +8144,8 @@ and generate_bindtests () =
 #include \"guestfs_protocol.h\"
 
 #define error guestfs_error
+#define safe_calloc guestfs_safe_calloc
+#define safe_malloc guestfs_safe_malloc
 
 static void
 print_strings (char * const* const argv)
@@ -8170,70 +8221,70 @@ print_strings (char * const* const argv)
             pr "  char **strs;\n";
             pr "  int n, i;\n";
             pr "  sscanf (val, \"%%d\", &n);\n";
-            pr "  strs = malloc ((n+1) * sizeof (char *));\n";
+            pr "  strs = safe_malloc (g, (n+1) * sizeof (char *));\n";
             pr "  for (i = 0; i < n; ++i) {\n";
-            pr "    strs[i] = malloc (16);\n";
+            pr "    strs[i] = safe_malloc (g, 16);\n";
             pr "    snprintf (strs[i], 16, \"%%d\", i);\n";
             pr "  }\n";
             pr "  strs[n] = NULL;\n";
             pr "  return strs;\n"
         | RIntBool _ ->
             pr "  struct guestfs_int_bool *r;\n";
-            pr "  r = malloc (sizeof (struct guestfs_int_bool));\n";
+            pr "  r = safe_malloc (g, sizeof *r);\n";
             pr "  sscanf (val, \"%%\" SCNi32, &r->i);\n";
             pr "  r->b = 0;\n";
             pr "  return r;\n"
         | RPVList _ ->
             pr "  struct guestfs_lvm_pv_list *r;\n";
             pr "  int i;\n";
-            pr "  r = malloc (sizeof (struct guestfs_lvm_pv_list));\n";
+            pr "  r = safe_malloc (g, sizeof *r);\n";
             pr "  sscanf (val, \"%%d\", &r->len);\n";
-            pr "  r->val = calloc (r->len, sizeof (struct guestfs_lvm_pv));\n";
+            pr "  r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
             pr "  for (i = 0; i < r->len; ++i) {\n";
-            pr "    r->val[i].pv_name = malloc (16);\n";
+            pr "    r->val[i].pv_name = safe_malloc (g, 16);\n";
             pr "    snprintf (r->val[i].pv_name, 16, \"%%d\", i);\n";
             pr "  }\n";
             pr "  return r;\n"
         | RVGList _ ->
             pr "  struct guestfs_lvm_vg_list *r;\n";
             pr "  int i;\n";
-            pr "  r = malloc (sizeof (struct guestfs_lvm_vg_list));\n";
+            pr "  r = safe_malloc (g, sizeof *r);\n";
             pr "  sscanf (val, \"%%d\", &r->len);\n";
-            pr "  r->val = calloc (r->len, sizeof (struct guestfs_lvm_vg));\n";
+            pr "  r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
             pr "  for (i = 0; i < r->len; ++i) {\n";
-            pr "    r->val[i].vg_name = malloc (16);\n";
+            pr "    r->val[i].vg_name = safe_malloc (g, 16);\n";
             pr "    snprintf (r->val[i].vg_name, 16, \"%%d\", i);\n";
             pr "  }\n";
             pr "  return r;\n"
         | RLVList _ ->
             pr "  struct guestfs_lvm_lv_list *r;\n";
             pr "  int i;\n";
-            pr "  r = malloc (sizeof (struct guestfs_lvm_lv_list));\n";
+            pr "  r = safe_malloc (g, sizeof *r);\n";
             pr "  sscanf (val, \"%%d\", &r->len);\n";
-            pr "  r->val = calloc (r->len, sizeof (struct guestfs_lvm_lv));\n";
+            pr "  r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
             pr "  for (i = 0; i < r->len; ++i) {\n";
-            pr "    r->val[i].lv_name = malloc (16);\n";
+            pr "    r->val[i].lv_name = safe_malloc (g, 16);\n";
             pr "    snprintf (r->val[i].lv_name, 16, \"%%d\", i);\n";
             pr "  }\n";
             pr "  return r;\n"
         | RStat _ ->
             pr "  struct guestfs_stat *r;\n";
-            pr "  r = calloc (1, sizeof (*r));\n";
+            pr "  r = safe_calloc (g, 1, sizeof (*r));\n";
             pr "  sscanf (val, \"%%\" SCNi64, &r->dev);\n";
             pr "  return r;\n"
         | RStatVFS _ ->
             pr "  struct guestfs_statvfs *r;\n";
-            pr "  r = calloc (1, sizeof (*r));\n";
+            pr "  r = safe_calloc (g, 1, sizeof (*r));\n";
             pr "  sscanf (val, \"%%\" SCNi64, &r->bsize);\n";
             pr "  return r;\n"
         | RHashtable _ ->
             pr "  char **strs;\n";
             pr "  int n, i;\n";
             pr "  sscanf (val, \"%%d\", &n);\n";
-            pr "  strs = malloc ((n*2+1) * sizeof (char *));\n";
+            pr "  strs = safe_malloc (g, (n*2+1) * sizeof (*strs));\n";
             pr "  for (i = 0; i < n; ++i) {\n";
-            pr "    strs[i*2] = malloc (16);\n";
-            pr "    strs[i*2+1] = malloc (16);\n";
+            pr "    strs[i*2] = safe_malloc (g, 16);\n";
+            pr "    strs[i*2+1] = safe_malloc (g, 16);\n";
             pr "    snprintf (strs[i*2], 16, \"%%d\", i);\n";
             pr "    snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
             pr "  }\n";
@@ -8242,9 +8293,9 @@ print_strings (char * const* const argv)
         | RDirentList _ ->
             pr "  struct guestfs_dirent_list *r;\n";
             pr "  int i;\n";
-            pr "  r = malloc (sizeof (struct guestfs_dirent_list));\n";
+            pr "  r = safe_malloc (g, sizeof *r);\n";
             pr "  sscanf (val, \"%%d\", &r->len);\n";
-            pr "  r->val = calloc (r->len, sizeof (struct guestfs_dirent));\n";
+            pr "  r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
             pr "  for (i = 0; i < r->len; ++i)\n";
             pr "    r->val[i].ino = i;\n";
             pr "  return r;\n"
@@ -8589,6 +8640,10 @@ Run it from the top source directory using the command
   generate_daemon_actions ();
   close ();
 
+  let close = output_to "daemon/names.c" in
+  generate_daemon_names ();
+  close ();
+
   let close = output_to "capitests/tests.c" in
   generate_tests ();
   close ();
@@ -8712,3 +8767,10 @@ Run it from the top source directory using the command
   let close = output_to "src/MAX_PROC_NR" in
   generate_max_proc_nr ();
   close ();
+
+  (* Always generate this file last, and unconditionally.  It's used
+   * by the Makefile to know when we must re-run the generator.
+   *)
+  let chan = open_out "src/stamp-generator" in
+  fprintf chan "1\n";
+  close_out chan