2 * Copyright (C) 2009-2010 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_optgroups
28 open Generator_actions
29 open Generator_structs
32 (* Generate daemon/actions.h. *)
33 let generate_daemon_actions_h () =
34 generate_header CStyle GPLv2plus;
36 pr "#include \"guestfs_protocol.h\"\n";
40 fun (name, style, _, _, _, _, _) ->
42 ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
46 (* Generate the server-side stubs. *)
47 and generate_daemon_actions () =
48 generate_header CStyle GPLv2plus;
50 pr "#include <config.h>\n";
52 pr "#include <stdio.h>\n";
53 pr "#include <stdlib.h>\n";
54 pr "#include <string.h>\n";
55 pr "#include <inttypes.h>\n";
56 pr "#include <rpc/types.h>\n";
57 pr "#include <rpc/xdr.h>\n";
59 pr "#include \"daemon.h\"\n";
60 pr "#include \"c-ctype.h\"\n";
61 pr "#include \"guestfs_protocol.h\"\n";
62 pr "#include \"actions.h\"\n";
66 fun (name, (ret, args, optargs), _, _, _, _, _) ->
68 failwithf "optional arguments not supported in the daemon yet";
70 (* Generate server-side stubs. *)
71 pr "static void %s_stub (XDR *xdr_in)\n" name;
75 | RErr | RInt _ -> pr " int r;\n"; "-1"
76 | RInt64 _ -> pr " int64_t r;\n"; "-1"
77 | RBool _ -> pr " int r;\n"; "-1"
78 | RConstString _ | RConstOptString _ ->
79 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
80 | RString _ -> pr " char *r;\n"; "NULL"
81 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
82 | RStruct (_, typ) -> pr " guestfs_int_%s *r;\n" typ; "NULL"
83 | RStructList (_, typ) -> pr " guestfs_int_%s_list *r;\n" typ; "NULL"
85 pr " size_t size = 1;\n";
92 pr " struct guestfs_%s_args args;\n" name;
95 | Device n | Dev_or_Path n
99 | OptString n -> pr " char *%s;\n" n
100 | StringList n | DeviceList n -> pr " char **%s;\n" n
101 | Bool n -> pr " int %s;\n" n
102 | Int n -> pr " int %s;\n" n
103 | Int64 n -> pr " int64_t %s;\n" n
104 | FileIn _ | FileOut _ -> ()
106 pr " const char *%s;\n" n;
107 pr " size_t %s_size;\n" n
108 | Pointer _ -> assert false
114 List.exists (function FileIn _ -> true | _ -> false) args in
119 pr " memset (&args, 0, sizeof args);\n";
121 pr " if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
123 pr " if (cancel_receive () != -2)\n";
124 pr " reply_with_error (\"daemon failed to decode procedure arguments\");\n";
128 pr " char *%s = args.%s;\n" n n
130 let pr_list_handling_code n =
131 pr " %s = realloc (args.%s.%s_val,\n" n n n;
132 pr " sizeof (char *) * (args.%s.%s_len+1));\n" n n;
133 pr " if (%s == NULL) {\n" n;
135 pr " if (cancel_receive () != -2)\n";
136 pr " reply_with_perror (\"realloc\");\n";
139 pr " %s[args.%s.%s_len] = NULL;\n" n n n;
140 pr " args.%s.%s_val = %s;\n" n n n;
146 pr " ABS_PATH (%s, %s, goto done);\n"
147 n (if is_filein then "cancel_receive ()" else "0");
150 pr " RESOLVE_DEVICE (%s, %s, goto done);\n"
151 n (if is_filein then "cancel_receive ()" else "0");
154 pr " REQUIRE_ROOT_OR_RESOLVE_DEVICE (%s, %s, goto done);\n"
155 n (if is_filein then "cancel_receive ()" else "0");
156 | String n | Key n -> pr_args n
157 | OptString n -> pr " %s = args.%s ? *args.%s : NULL;\n" n n n
159 pr_list_handling_code n;
161 pr_list_handling_code n;
162 pr " /* Ensure that each is a device,\n";
163 pr " * and perform device name translation.\n";
167 pr " for (i = 0; %s[i] != NULL; ++i)\n" n;
168 pr " RESOLVE_DEVICE (%s[i], %s, goto done);\n" n
169 (if is_filein then "cancel_receive ()" else "0");
171 | Bool n -> pr " %s = args.%s;\n" n n
172 | Int n -> pr " %s = args.%s;\n" n n
173 | Int64 n -> pr " %s = args.%s;\n" n n
174 | FileIn _ | FileOut _ -> ()
176 pr " %s = args.%s.%s_val;\n" n n n;
177 pr " %s_size = args.%s.%s_len;\n" n n n
178 | Pointer _ -> assert false
183 (* this is used at least for do_equal *)
184 if List.exists (function Pathname _ -> true | _ -> false) args then (
185 (* Emit NEED_ROOT just once, even when there are two or
186 more Pathname args *)
187 pr " NEED_ROOT (%s, goto done);\n"
188 (if is_filein then "cancel_receive ()" else "0");
191 (* Don't want to call the impl with any FileIn or FileOut
192 * parameters, since these go "outside" the RPC protocol.
195 List.filter (function FileIn _ | FileOut _ -> false | _ -> true) args in
196 pr " r = do_%s " name;
197 generate_c_call_args (ret, args', optargs);
201 | RErr | RInt _ | RInt64 _ | RBool _
202 | RConstString _ | RConstOptString _
203 | RString _ | RStringList _ | RHashtable _
204 | RStruct (_, _) | RStructList (_, _) ->
205 pr " if (r == %s)\n" error_code;
206 pr " /* do_%s has already called reply_with_error */\n" name;
210 pr " /* size == 0 && r == NULL could be a non-error case (just\n";
211 pr " * an ordinary zero-length buffer), so be careful ...\n";
213 pr " if (size == 1 && r == %s)\n" error_code;
214 pr " /* do_%s has already called reply_with_error */\n" name;
219 (* If there are any FileOut parameters, then the impl must
220 * send its own reply.
223 List.exists (function FileOut _ -> true | _ -> false) args in
225 pr " /* do_%s has already sent a reply */\n" name
228 | RErr -> pr " reply (NULL, NULL);\n"
229 | RInt n | RInt64 n | RBool n ->
230 pr " struct guestfs_%s_ret ret;\n" name;
231 pr " ret.%s = r;\n" n;
232 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
234 | RConstString _ | RConstOptString _ ->
235 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
237 pr " struct guestfs_%s_ret ret;\n" name;
238 pr " ret.%s = r;\n" n;
239 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
242 | RStringList n | RHashtable n ->
243 pr " struct guestfs_%s_ret ret;\n" name;
244 pr " ret.%s.%s_len = count_strings (r);\n" n n;
245 pr " ret.%s.%s_val = r;\n" n n;
246 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
248 pr " free_strings (r);\n"
250 pr " struct guestfs_%s_ret ret;\n" name;
251 pr " ret.%s = *r;\n" n;
252 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
254 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
256 | RStructList (n, _) ->
257 pr " struct guestfs_%s_ret ret;\n" name;
258 pr " ret.%s = *r;\n" n;
259 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
261 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
264 pr " struct guestfs_%s_ret ret;\n" name;
265 pr " ret.%s.%s_val = r;\n" n n;
266 pr " ret.%s.%s_len = size;\n" n n;
267 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
277 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
284 (* Dispatch function. *)
285 pr "void dispatch_incoming_message (XDR *xdr_in)\n";
287 pr " switch (proc_nr) {\n";
290 fun (name, _, _, _, _, _, _) ->
291 pr " case GUESTFS_PROC_%s:\n" (String.uppercase name);
292 pr " %s_stub (xdr_in);\n" name;
297 pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d, set LIBGUESTFS_PATH to point to the matching libguestfs appliance directory\", proc_nr);\n";
302 (* LVM columns and tokenization functions. *)
303 (* XXX This generates crap code. We should rethink how we
309 pr "static const char *lvm_%s_cols = \"%s\";\n"
310 typ (String.concat "," (List.map fst cols));
313 pr "static int lvm_tokenize_%s (char *str, guestfs_int_lvm_%s *r)\n" typ typ;
315 pr " char *tok, *p, *next;\n";
316 pr " size_t i, j;\n";
319 pr " fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
323 pr " fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
326 pr " if (!*str || c_isspace (*str)) {\n";
327 pr " fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
332 fun (name, coltype) ->
334 pr " fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
337 pr " p = strchrnul (tok, ',');\n";
338 pr " if (*p) next = p+1; else next = NULL;\n";
342 pr " r->%s = strdup (tok);\n" name;
343 pr " if (r->%s == NULL) {\n" name;
344 pr " perror (\"strdup\");\n";
348 pr " for (i = j = 0; i < 32; ++j) {\n";
349 pr " if (tok[j] == '\\0') {\n";
350 pr " fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
352 pr " } else if (tok[j] != '-')\n";
353 pr " r->%s[i++] = tok[j];\n" name;
356 pr " if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
357 pr " fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
361 pr " if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
362 pr " fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
366 pr " if (tok[0] == '\\0')\n";
367 pr " r->%s = -1;\n" name;
368 pr " else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
369 pr " fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
372 | FBuffer | FInt32 | FUInt32 | FUInt64 | FChar ->
373 assert false (* can never be an LVM column *)
378 pr " if (tok != NULL) {\n";
379 pr " fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
386 pr "guestfs_int_lvm_%s_list *\n" typ;
387 pr "parse_command_line_%ss (void)\n" typ;
389 pr " char *out, *err;\n";
390 pr " char *p, *pend;\n";
392 pr " guestfs_int_lvm_%s_list *ret;\n" typ;
395 pr " ret = malloc (sizeof *ret);\n";
397 pr " reply_with_perror (\"malloc\");\n";
398 pr " return NULL;\n";
401 pr " ret->guestfs_int_lvm_%s_list_len = 0;\n" typ;
402 pr " ret->guestfs_int_lvm_%s_list_val = NULL;\n" typ;
404 pr " r = command (&out, &err,\n";
405 pr " \"lvm\", \"%ss\",\n" typ;
406 pr " \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
407 pr " \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
408 pr " if (r == -1) {\n";
409 pr " reply_with_error (\"%%s\", err);\n";
413 pr " return NULL;\n";
418 pr " /* Tokenize each line of the output. */\n";
422 pr " pend = strchr (p, '\\n'); /* Get the next line of output. */\n";
424 pr " *pend = '\\0';\n";
428 pr " while (*p && c_isspace (*p)) /* Skip any leading whitespace. */\n";
431 pr " if (!*p) { /* Empty line? Skip it. */\n";
436 pr " /* Allocate some space to store this next entry. */\n";
437 pr " newp = realloc (ret->guestfs_int_lvm_%s_list_val,\n" typ;
438 pr " sizeof (guestfs_int_lvm_%s) * (i+1));\n" typ;
439 pr " if (newp == NULL) {\n";
440 pr " reply_with_perror (\"realloc\");\n";
441 pr " free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
444 pr " return NULL;\n";
446 pr " ret->guestfs_int_lvm_%s_list_val = newp;\n" typ;
448 pr " /* Tokenize the next entry. */\n";
449 pr " r = lvm_tokenize_%s (p, &ret->guestfs_int_lvm_%s_list_val[i]);\n" typ typ;
450 pr " if (r == -1) {\n";
451 pr " reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
452 pr " free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
455 pr " return NULL;\n";
462 pr " ret->guestfs_int_lvm_%s_list_len = i;\n" typ;
468 ) ["pv", lvm_pv_cols; "vg", lvm_vg_cols; "lv", lvm_lv_cols]
470 (* Generate a list of function names, for debugging in the daemon.. *)
471 and generate_daemon_names () =
472 generate_header CStyle GPLv2plus;
474 pr "#include <config.h>\n";
476 pr "#include \"daemon.h\"\n";
479 pr "/* This array is indexed by proc_nr. See guestfs_protocol.x. */\n";
480 pr "const char *function_names[] = {\n";
482 fun (name, _, proc_nr, _, _, _, _) -> pr " [%d] = \"%s\",\n" proc_nr name
486 (* Generate the optional groups for the daemon to implement
489 and generate_daemon_optgroups_c () =
490 generate_header CStyle GPLv2plus;
492 pr "#include <config.h>\n";
494 pr "#include \"daemon.h\"\n";
495 pr "#include \"optgroups.h\"\n";
498 pr "struct optgroup optgroups[] = {\n";
501 pr " { \"%s\", optgroup_%s_available },\n" group group
503 pr " { NULL, NULL }\n";
506 and generate_daemon_optgroups_h () =
507 generate_header CStyle GPLv2plus;
511 pr "extern int optgroup_%s_available (void);\n" group