New API: list-filesystems: list filesystems
[libguestfs.git] / generator / generator_daemon.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 open Generator_c
31
32 (* Generate daemon/actions.h. *)
33 let generate_daemon_actions_h () =
34   generate_header CStyle GPLv2plus;
35
36   pr "#include \"../src/guestfs_protocol.h\"\n";
37   pr "\n";
38
39   List.iter (
40     fun (name, style, _, _, _, _, _) ->
41       generate_prototype
42         ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
43         name style;
44   ) daemon_functions
45
46 (* Generate the server-side stubs. *)
47 and generate_daemon_actions () =
48   generate_header CStyle GPLv2plus;
49
50   pr "#include <config.h>\n";
51   pr "\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";
58   pr "\n";
59   pr "#include \"daemon.h\"\n";
60   pr "#include \"c-ctype.h\"\n";
61   pr "#include \"../src/guestfs_protocol.h\"\n";
62   pr "#include \"actions.h\"\n";
63   pr "\n";
64
65   List.iter (
66     fun (name, style, _, _, _, _, _) ->
67       (* Generate server-side stubs. *)
68       pr "static void %s_stub (XDR *xdr_in)\n" name;
69       pr "{\n";
70       let error_code =
71         match fst style with
72         | RErr | RInt _ -> pr "  int r;\n"; "-1"
73         | RInt64 _ -> pr "  int64_t r;\n"; "-1"
74         | RBool _ -> pr "  int r;\n"; "-1"
75         | RConstString _ | RConstOptString _ ->
76             failwithf "RConstString|RConstOptString cannot be used by daemon functions"
77         | RString _ -> pr "  char *r;\n"; "NULL"
78         | RStringList _ | RHashtable _ -> pr "  char **r;\n"; "NULL"
79         | RStruct (_, typ) -> pr "  guestfs_int_%s *r;\n" typ; "NULL"
80         | RStructList (_, typ) -> pr "  guestfs_int_%s_list *r;\n" typ; "NULL"
81         | RBufferOut _ ->
82             pr "  size_t size = 1;\n";
83             pr "  char *r;\n";
84             "NULL" in
85
86       (match snd style with
87        | [] -> ()
88        | args ->
89            pr "  struct guestfs_%s_args args;\n" name;
90            List.iter (
91              function
92              | Device n | Dev_or_Path n
93              | Pathname n
94              | String n
95              | Key n -> ()
96              | OptString n -> pr "  char *%s;\n" n
97              | StringList n | DeviceList n -> pr "  char **%s;\n" n
98              | Bool n -> pr "  int %s;\n" n
99              | Int n -> pr "  int %s;\n" n
100              | Int64 n -> pr "  int64_t %s;\n" n
101              | FileIn _ | FileOut _ -> ()
102              | BufferIn n ->
103                  pr "  const char *%s;\n" n;
104                  pr "  size_t %s_size;\n" n
105            ) args
106       );
107       pr "\n";
108
109       let is_filein =
110         List.exists (function FileIn _ -> true | _ -> false) (snd style) in
111
112       (match snd style with
113        | [] -> ()
114        | args ->
115            pr "  memset (&args, 0, sizeof args);\n";
116            pr "\n";
117            pr "  if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
118            if is_filein then
119              pr "    if (cancel_receive () != -2)\n";
120            pr "      reply_with_error (\"daemon failed to decode procedure arguments\");\n";
121            pr "    goto done;\n";
122            pr "  }\n";
123            let pr_args n =
124              pr "  char *%s = args.%s;\n" n n
125            in
126            let pr_list_handling_code n =
127              pr "  %s = realloc (args.%s.%s_val,\n" n n n;
128              pr "                sizeof (char *) * (args.%s.%s_len+1));\n" n n;
129              pr "  if (%s == NULL) {\n" n;
130              if is_filein then
131                pr "    if (cancel_receive () != -2)\n";
132              pr "      reply_with_perror (\"realloc\");\n";
133              pr "    goto done;\n";
134              pr "  }\n";
135              pr "  %s[args.%s.%s_len] = NULL;\n" n n n;
136              pr "  args.%s.%s_val = %s;\n" n n n;
137            in
138            List.iter (
139              function
140              | Pathname n ->
141                  pr_args n;
142                  pr "  ABS_PATH (%s, %s, goto done);\n"
143                    n (if is_filein then "cancel_receive ()" else "0");
144              | Device n ->
145                  pr_args n;
146                  pr "  RESOLVE_DEVICE (%s, %s, goto done);\n"
147                    n (if is_filein then "cancel_receive ()" else "0");
148              | Dev_or_Path n ->
149                  pr_args n;
150                  pr "  REQUIRE_ROOT_OR_RESOLVE_DEVICE (%s, %s, goto done);\n"
151                    n (if is_filein then "cancel_receive ()" else "0");
152              | String n | Key n -> pr_args n
153              | OptString n -> pr "  %s = args.%s ? *args.%s : NULL;\n" n n n
154              | StringList n ->
155                  pr_list_handling_code n;
156              | DeviceList n ->
157                  pr_list_handling_code n;
158                  pr "  /* Ensure that each is a device,\n";
159                  pr "   * and perform device name translation.\n";
160                  pr "   */\n";
161                  pr "  {\n";
162                  pr "    size_t i;\n";
163                  pr "    for (i = 0; %s[i] != NULL; ++i)\n" n;
164                  pr "      RESOLVE_DEVICE (%s[i], %s, goto done);\n" n
165                    (if is_filein then "cancel_receive ()" else "0");
166                  pr "  }\n";
167              | Bool n -> pr "  %s = args.%s;\n" n n
168              | Int n -> pr "  %s = args.%s;\n" n n
169              | Int64 n -> pr "  %s = args.%s;\n" n n
170              | FileIn _ | FileOut _ -> ()
171              | BufferIn n ->
172                  pr "  %s = args.%s.%s_val;\n" n n n;
173                  pr "  %s_size = args.%s.%s_len;\n" n n n
174            ) args;
175            pr "\n"
176       );
177
178       (* this is used at least for do_equal *)
179       if List.exists (function Pathname _ -> true | _ -> false) (snd style) then (
180         (* Emit NEED_ROOT just once, even when there are two or
181            more Pathname args *)
182         pr "  NEED_ROOT (%s, goto done);\n"
183           (if is_filein then "cancel_receive ()" else "0");
184       );
185
186       (* Don't want to call the impl with any FileIn or FileOut
187        * parameters, since these go "outside" the RPC protocol.
188        *)
189       let args' =
190         List.filter (function FileIn _ | FileOut _ -> false | _ -> true)
191           (snd style) in
192       pr "  r = do_%s " name;
193       generate_c_call_args (fst style, args');
194       pr ";\n";
195
196       (match fst style with
197        | RErr | RInt _ | RInt64 _ | RBool _
198        | RConstString _ | RConstOptString _
199        | RString _ | RStringList _ | RHashtable _
200        | RStruct (_, _) | RStructList (_, _) ->
201            pr "  if (r == %s)\n" error_code;
202            pr "    /* do_%s has already called reply_with_error */\n" name;
203            pr "    goto done;\n";
204            pr "\n"
205        | RBufferOut _ ->
206            pr "  /* size == 0 && r == NULL could be a non-error case (just\n";
207            pr "   * an ordinary zero-length buffer), so be careful ...\n";
208            pr "   */\n";
209            pr "  if (size == 1 && r == %s)\n" error_code;
210            pr "    /* do_%s has already called reply_with_error */\n" name;
211            pr "    goto done;\n";
212            pr "\n"
213       );
214
215       (* If there are any FileOut parameters, then the impl must
216        * send its own reply.
217        *)
218       let no_reply =
219         List.exists (function FileOut _ -> true | _ -> false) (snd style) in
220       if no_reply then
221         pr "  /* do_%s has already sent a reply */\n" name
222       else (
223         match fst style with
224         | RErr -> pr "  reply (NULL, NULL);\n"
225         | RInt n | RInt64 n | RBool n ->
226             pr "  struct guestfs_%s_ret ret;\n" name;
227             pr "  ret.%s = r;\n" n;
228             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
229               name
230         | RConstString _ | RConstOptString _ ->
231             failwithf "RConstString|RConstOptString cannot be used by daemon functions"
232         | RString n ->
233             pr "  struct guestfs_%s_ret ret;\n" name;
234             pr "  ret.%s = r;\n" n;
235             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
236               name;
237             pr "  free (r);\n"
238         | RStringList n | RHashtable n ->
239             pr "  struct guestfs_%s_ret ret;\n" name;
240             pr "  ret.%s.%s_len = count_strings (r);\n" n n;
241             pr "  ret.%s.%s_val = r;\n" n n;
242             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
243               name;
244             pr "  free_strings (r);\n"
245         | RStruct (n, _) ->
246             pr "  struct guestfs_%s_ret ret;\n" name;
247             pr "  ret.%s = *r;\n" n;
248             pr "  reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
249               name;
250             pr "  xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
251               name
252         | RStructList (n, _) ->
253             pr "  struct guestfs_%s_ret ret;\n" name;
254             pr "  ret.%s = *r;\n" n;
255             pr "  reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
256               name;
257             pr "  xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
258               name
259         | RBufferOut n ->
260             pr "  struct guestfs_%s_ret ret;\n" name;
261             pr "  ret.%s.%s_val = r;\n" n n;
262             pr "  ret.%s.%s_len = size;\n" n n;
263             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
264               name;
265             pr "  free (r);\n"
266       );
267
268       (* Free the args. *)
269       pr "done:\n";
270       (match snd style with
271        | [] -> ()
272        | _ ->
273            pr "  xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
274              name
275       );
276       pr "  return;\n";
277       pr "}\n\n";
278   ) daemon_functions;
279
280   (* Dispatch function. *)
281   pr "void dispatch_incoming_message (XDR *xdr_in)\n";
282   pr "{\n";
283   pr "  switch (proc_nr) {\n";
284
285   List.iter (
286     fun (name, style, _, _, _, _, _) ->
287       pr "    case GUESTFS_PROC_%s:\n" (String.uppercase name);
288       pr "      %s_stub (xdr_in);\n" name;
289       pr "      break;\n"
290   ) daemon_functions;
291
292   pr "    default:\n";
293   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";
294   pr "  }\n";
295   pr "}\n";
296   pr "\n";
297
298   (* LVM columns and tokenization functions. *)
299   (* XXX This generates crap code.  We should rethink how we
300    * do this parsing.
301    *)
302   List.iter (
303     function
304     | typ, cols ->
305         pr "static const char *lvm_%s_cols = \"%s\";\n"
306           typ (String.concat "," (List.map fst cols));
307         pr "\n";
308
309         pr "static int lvm_tokenize_%s (char *str, guestfs_int_lvm_%s *r)\n" typ typ;
310         pr "{\n";
311         pr "  char *tok, *p, *next;\n";
312         pr "  size_t i, j;\n";
313         pr "\n";
314         (*
315           pr "  fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
316           pr "\n";
317         *)
318         pr "  if (!str) {\n";
319         pr "    fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
320         pr "    return -1;\n";
321         pr "  }\n";
322         pr "  if (!*str || c_isspace (*str)) {\n";
323         pr "    fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
324         pr "    return -1;\n";
325         pr "  }\n";
326         pr "  tok = str;\n";
327         List.iter (
328           fun (name, coltype) ->
329             pr "  if (!tok) {\n";
330             pr "    fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
331             pr "    return -1;\n";
332             pr "  }\n";
333             pr "  p = strchrnul (tok, ',');\n";
334             pr "  if (*p) next = p+1; else next = NULL;\n";
335             pr "  *p = '\\0';\n";
336             (match coltype with
337              | FString ->
338                  pr "  r->%s = strdup (tok);\n" name;
339                  pr "  if (r->%s == NULL) {\n" name;
340                  pr "    perror (\"strdup\");\n";
341                  pr "    return -1;\n";
342                  pr "  }\n"
343              | FUUID ->
344                  pr "  for (i = j = 0; i < 32; ++j) {\n";
345                  pr "    if (tok[j] == '\\0') {\n";
346                  pr "      fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
347                  pr "      return -1;\n";
348                  pr "    } else if (tok[j] != '-')\n";
349                  pr "      r->%s[i++] = tok[j];\n" name;
350                  pr "  }\n";
351              | FBytes ->
352                  pr "  if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
353                  pr "    fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
354                  pr "    return -1;\n";
355                  pr "  }\n";
356              | FInt64 ->
357                  pr "  if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
358                  pr "    fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
359                  pr "    return -1;\n";
360                  pr "  }\n";
361              | FOptPercent ->
362                  pr "  if (tok[0] == '\\0')\n";
363                  pr "    r->%s = -1;\n" name;
364                  pr "  else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
365                  pr "    fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
366                  pr "    return -1;\n";
367                  pr "  }\n";
368              | FBuffer | FInt32 | FUInt32 | FUInt64 | FChar ->
369                  assert false (* can never be an LVM column *)
370             );
371             pr "  tok = next;\n";
372         ) cols;
373
374         pr "  if (tok != NULL) {\n";
375         pr "    fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
376         pr "    return -1;\n";
377         pr "  }\n";
378         pr "  return 0;\n";
379         pr "}\n";
380         pr "\n";
381
382         pr "guestfs_int_lvm_%s_list *\n" typ;
383         pr "parse_command_line_%ss (void)\n" typ;
384         pr "{\n";
385         pr "  char *out, *err;\n";
386         pr "  char *p, *pend;\n";
387         pr "  int r, i;\n";
388         pr "  guestfs_int_lvm_%s_list *ret;\n" typ;
389         pr "  void *newp;\n";
390         pr "\n";
391         pr "  ret = malloc (sizeof *ret);\n";
392         pr "  if (!ret) {\n";
393         pr "    reply_with_perror (\"malloc\");\n";
394         pr "    return NULL;\n";
395         pr "  }\n";
396         pr "\n";
397         pr "  ret->guestfs_int_lvm_%s_list_len = 0;\n" typ;
398         pr "  ret->guestfs_int_lvm_%s_list_val = NULL;\n" typ;
399         pr "\n";
400         pr "  r = command (&out, &err,\n";
401         pr "           \"lvm\", \"%ss\",\n" typ;
402         pr "           \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
403         pr "           \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
404         pr "  if (r == -1) {\n";
405         pr "    reply_with_error (\"%%s\", err);\n";
406         pr "    free (out);\n";
407         pr "    free (err);\n";
408         pr "    free (ret);\n";
409         pr "    return NULL;\n";
410         pr "  }\n";
411         pr "\n";
412         pr "  free (err);\n";
413         pr "\n";
414         pr "  /* Tokenize each line of the output. */\n";
415         pr "  p = out;\n";
416         pr "  i = 0;\n";
417         pr "  while (p) {\n";
418         pr "    pend = strchr (p, '\\n');       /* Get the next line of output. */\n";
419         pr "    if (pend) {\n";
420         pr "      *pend = '\\0';\n";
421         pr "      pend++;\n";
422         pr "    }\n";
423         pr "\n";
424         pr "    while (*p && c_isspace (*p))    /* Skip any leading whitespace. */\n";
425         pr "      p++;\n";
426         pr "\n";
427         pr "    if (!*p) {                      /* Empty line?  Skip it. */\n";
428         pr "      p = pend;\n";
429         pr "      continue;\n";
430         pr "    }\n";
431         pr "\n";
432         pr "    /* Allocate some space to store this next entry. */\n";
433         pr "    newp = realloc (ret->guestfs_int_lvm_%s_list_val,\n" typ;
434         pr "                sizeof (guestfs_int_lvm_%s) * (i+1));\n" typ;
435         pr "    if (newp == NULL) {\n";
436         pr "      reply_with_perror (\"realloc\");\n";
437         pr "      free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
438         pr "      free (ret);\n";
439         pr "      free (out);\n";
440         pr "      return NULL;\n";
441         pr "    }\n";
442         pr "    ret->guestfs_int_lvm_%s_list_val = newp;\n" typ;
443         pr "\n";
444         pr "    /* Tokenize the next entry. */\n";
445         pr "    r = lvm_tokenize_%s (p, &ret->guestfs_int_lvm_%s_list_val[i]);\n" typ typ;
446         pr "    if (r == -1) {\n";
447         pr "      reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
448         pr "      free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
449         pr "      free (ret);\n";
450         pr "      free (out);\n";
451         pr "      return NULL;\n";
452         pr "    }\n";
453         pr "\n";
454         pr "    ++i;\n";
455         pr "    p = pend;\n";
456         pr "  }\n";
457         pr "\n";
458         pr "  ret->guestfs_int_lvm_%s_list_len = i;\n" typ;
459         pr "\n";
460         pr "  free (out);\n";
461         pr "  return ret;\n";
462         pr "}\n"
463
464   ) ["pv", lvm_pv_cols; "vg", lvm_vg_cols; "lv", lvm_lv_cols]
465
466 (* Generate a list of function names, for debugging in the daemon.. *)
467 and generate_daemon_names () =
468   generate_header CStyle GPLv2plus;
469
470   pr "#include <config.h>\n";
471   pr "\n";
472   pr "#include \"daemon.h\"\n";
473   pr "\n";
474
475   pr "/* This array is indexed by proc_nr.  See guestfs_protocol.x. */\n";
476   pr "const char *function_names[] = {\n";
477   List.iter (
478     fun (name, _, proc_nr, _, _, _, _) -> pr "  [%d] = \"%s\",\n" proc_nr name
479   ) daemon_functions;
480   pr "};\n";
481
482 (* Generate the optional groups for the daemon to implement
483  * guestfs_available.
484  *)
485 and generate_daemon_optgroups_c () =
486   generate_header CStyle GPLv2plus;
487
488   pr "#include <config.h>\n";
489   pr "\n";
490   pr "#include \"daemon.h\"\n";
491   pr "#include \"optgroups.h\"\n";
492   pr "\n";
493
494   pr "struct optgroup optgroups[] = {\n";
495   List.iter (
496     fun (group, _) ->
497       pr "  { \"%s\", optgroup_%s_available },\n" group group
498   ) optgroups;
499   pr "  { NULL, NULL }\n";
500   pr "};\n"
501
502 and generate_daemon_optgroups_h () =
503   generate_header CStyle GPLv2plus;
504
505   List.iter (
506     fun (group, _) ->
507       pr "extern int optgroup_%s_available (void);\n" group
508   ) optgroups