Additional test programs for Perl, Python, OCaml bindings.
[libguestfs.git] / daemon / stubs.c
1 /* libguestfs generated file
2  * WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.
3  * ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.
4  *
5  * Copyright (C) 2009 Red Hat Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #define _GNU_SOURCE // for strchrnul
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <inttypes.h>
28 #include <ctype.h>
29 #include <rpc/types.h>
30 #include <rpc/xdr.h>
31
32 #include "daemon.h"
33 #include "../src/guestfs_protocol.h"
34 #include "actions.h"
35
36 static void mount_stub (XDR *xdr_in)
37 {
38   int r;
39   struct guestfs_mount_args args;
40   const char *device;
41   const char *mountpoint;
42
43   memset (&args, 0, sizeof args);
44
45   if (!xdr_guestfs_mount_args (xdr_in, &args)) {
46     reply_with_error ("%s: daemon failed to decode procedure arguments", "mount");
47     return;
48   }
49   device = args.device;
50   mountpoint = args.mountpoint;
51
52   r = do_mount (device, mountpoint);
53   if (r == -1)
54     /* do_mount has already called reply_with_error */
55     goto done;
56
57   reply (NULL, NULL);
58 done:
59   xdr_free ((xdrproc_t) xdr_guestfs_mount_args, (char *) &args);
60 }
61
62 static void sync_stub (XDR *xdr_in)
63 {
64   int r;
65
66   r = do_sync ();
67   if (r == -1)
68     /* do_sync has already called reply_with_error */
69     goto done;
70
71   reply (NULL, NULL);
72 done: ;
73 }
74
75 static void touch_stub (XDR *xdr_in)
76 {
77   int r;
78   struct guestfs_touch_args args;
79   const char *path;
80
81   memset (&args, 0, sizeof args);
82
83   if (!xdr_guestfs_touch_args (xdr_in, &args)) {
84     reply_with_error ("%s: daemon failed to decode procedure arguments", "touch");
85     return;
86   }
87   path = args.path;
88
89   r = do_touch (path);
90   if (r == -1)
91     /* do_touch has already called reply_with_error */
92     goto done;
93
94   reply (NULL, NULL);
95 done:
96   xdr_free ((xdrproc_t) xdr_guestfs_touch_args, (char *) &args);
97 }
98
99 static void cat_stub (XDR *xdr_in)
100 {
101   char *r;
102   struct guestfs_cat_args args;
103   const char *path;
104
105   memset (&args, 0, sizeof args);
106
107   if (!xdr_guestfs_cat_args (xdr_in, &args)) {
108     reply_with_error ("%s: daemon failed to decode procedure arguments", "cat");
109     return;
110   }
111   path = args.path;
112
113   r = do_cat (path);
114   if (r == NULL)
115     /* do_cat has already called reply_with_error */
116     goto done;
117
118   struct guestfs_cat_ret ret;
119   ret.content = r;
120   reply ((xdrproc_t) &xdr_guestfs_cat_ret, (char *) &ret);
121   free (r);
122 done:
123   xdr_free ((xdrproc_t) xdr_guestfs_cat_args, (char *) &args);
124 }
125
126 static void ll_stub (XDR *xdr_in)
127 {
128   char *r;
129   struct guestfs_ll_args args;
130   const char *directory;
131
132   memset (&args, 0, sizeof args);
133
134   if (!xdr_guestfs_ll_args (xdr_in, &args)) {
135     reply_with_error ("%s: daemon failed to decode procedure arguments", "ll");
136     return;
137   }
138   directory = args.directory;
139
140   r = do_ll (directory);
141   if (r == NULL)
142     /* do_ll has already called reply_with_error */
143     goto done;
144
145   struct guestfs_ll_ret ret;
146   ret.listing = r;
147   reply ((xdrproc_t) &xdr_guestfs_ll_ret, (char *) &ret);
148   free (r);
149 done:
150   xdr_free ((xdrproc_t) xdr_guestfs_ll_args, (char *) &args);
151 }
152
153 static void ls_stub (XDR *xdr_in)
154 {
155   char **r;
156   struct guestfs_ls_args args;
157   const char *directory;
158
159   memset (&args, 0, sizeof args);
160
161   if (!xdr_guestfs_ls_args (xdr_in, &args)) {
162     reply_with_error ("%s: daemon failed to decode procedure arguments", "ls");
163     return;
164   }
165   directory = args.directory;
166
167   r = do_ls (directory);
168   if (r == NULL)
169     /* do_ls has already called reply_with_error */
170     goto done;
171
172   struct guestfs_ls_ret ret;
173   ret.listing.listing_len = count_strings (r);
174   ret.listing.listing_val = r;
175   reply ((xdrproc_t) &xdr_guestfs_ls_ret, (char *) &ret);
176   free_strings (r);
177 done:
178   xdr_free ((xdrproc_t) xdr_guestfs_ls_args, (char *) &args);
179 }
180
181 static void list_devices_stub (XDR *xdr_in)
182 {
183   char **r;
184
185   r = do_list_devices ();
186   if (r == NULL)
187     /* do_list_devices has already called reply_with_error */
188     goto done;
189
190   struct guestfs_list_devices_ret ret;
191   ret.devices.devices_len = count_strings (r);
192   ret.devices.devices_val = r;
193   reply ((xdrproc_t) &xdr_guestfs_list_devices_ret, (char *) &ret);
194   free_strings (r);
195 done: ;
196 }
197
198 static void list_partitions_stub (XDR *xdr_in)
199 {
200   char **r;
201
202   r = do_list_partitions ();
203   if (r == NULL)
204     /* do_list_partitions has already called reply_with_error */
205     goto done;
206
207   struct guestfs_list_partitions_ret ret;
208   ret.partitions.partitions_len = count_strings (r);
209   ret.partitions.partitions_val = r;
210   reply ((xdrproc_t) &xdr_guestfs_list_partitions_ret, (char *) &ret);
211   free_strings (r);
212 done: ;
213 }
214
215 static void pvs_stub (XDR *xdr_in)
216 {
217   char **r;
218
219   r = do_pvs ();
220   if (r == NULL)
221     /* do_pvs has already called reply_with_error */
222     goto done;
223
224   struct guestfs_pvs_ret ret;
225   ret.physvols.physvols_len = count_strings (r);
226   ret.physvols.physvols_val = r;
227   reply ((xdrproc_t) &xdr_guestfs_pvs_ret, (char *) &ret);
228   free_strings (r);
229 done: ;
230 }
231
232 static void vgs_stub (XDR *xdr_in)
233 {
234   char **r;
235
236   r = do_vgs ();
237   if (r == NULL)
238     /* do_vgs has already called reply_with_error */
239     goto done;
240
241   struct guestfs_vgs_ret ret;
242   ret.volgroups.volgroups_len = count_strings (r);
243   ret.volgroups.volgroups_val = r;
244   reply ((xdrproc_t) &xdr_guestfs_vgs_ret, (char *) &ret);
245   free_strings (r);
246 done: ;
247 }
248
249 static void lvs_stub (XDR *xdr_in)
250 {
251   char **r;
252
253   r = do_lvs ();
254   if (r == NULL)
255     /* do_lvs has already called reply_with_error */
256     goto done;
257
258   struct guestfs_lvs_ret ret;
259   ret.logvols.logvols_len = count_strings (r);
260   ret.logvols.logvols_val = r;
261   reply ((xdrproc_t) &xdr_guestfs_lvs_ret, (char *) &ret);
262   free_strings (r);
263 done: ;
264 }
265
266 static void pvs_full_stub (XDR *xdr_in)
267 {
268   guestfs_lvm_int_pv_list *r;
269
270   r = do_pvs_full ();
271   if (r == NULL)
272     /* do_pvs_full has already called reply_with_error */
273     goto done;
274
275   struct guestfs_pvs_full_ret ret;
276   ret.physvols = *r;
277   reply ((xdrproc_t) xdr_guestfs_pvs_full_ret, (char *) &ret);
278   xdr_free ((xdrproc_t) xdr_guestfs_pvs_full_ret, (char *) &ret);
279 done: ;
280 }
281
282 static void vgs_full_stub (XDR *xdr_in)
283 {
284   guestfs_lvm_int_vg_list *r;
285
286   r = do_vgs_full ();
287   if (r == NULL)
288     /* do_vgs_full has already called reply_with_error */
289     goto done;
290
291   struct guestfs_vgs_full_ret ret;
292   ret.volgroups = *r;
293   reply ((xdrproc_t) xdr_guestfs_vgs_full_ret, (char *) &ret);
294   xdr_free ((xdrproc_t) xdr_guestfs_vgs_full_ret, (char *) &ret);
295 done: ;
296 }
297
298 static void lvs_full_stub (XDR *xdr_in)
299 {
300   guestfs_lvm_int_lv_list *r;
301
302   r = do_lvs_full ();
303   if (r == NULL)
304     /* do_lvs_full has already called reply_with_error */
305     goto done;
306
307   struct guestfs_lvs_full_ret ret;
308   ret.logvols = *r;
309   reply ((xdrproc_t) xdr_guestfs_lvs_full_ret, (char *) &ret);
310   xdr_free ((xdrproc_t) xdr_guestfs_lvs_full_ret, (char *) &ret);
311 done: ;
312 }
313
314 static void read_lines_stub (XDR *xdr_in)
315 {
316   char **r;
317   struct guestfs_read_lines_args args;
318   const char *path;
319
320   memset (&args, 0, sizeof args);
321
322   if (!xdr_guestfs_read_lines_args (xdr_in, &args)) {
323     reply_with_error ("%s: daemon failed to decode procedure arguments", "read_lines");
324     return;
325   }
326   path = args.path;
327
328   r = do_read_lines (path);
329   if (r == NULL)
330     /* do_read_lines has already called reply_with_error */
331     goto done;
332
333   struct guestfs_read_lines_ret ret;
334   ret.lines.lines_len = count_strings (r);
335   ret.lines.lines_val = r;
336   reply ((xdrproc_t) &xdr_guestfs_read_lines_ret, (char *) &ret);
337   free_strings (r);
338 done:
339   xdr_free ((xdrproc_t) xdr_guestfs_read_lines_args, (char *) &args);
340 }
341
342 static void aug_init_stub (XDR *xdr_in)
343 {
344   int r;
345   struct guestfs_aug_init_args args;
346   const char *root;
347   int flags;
348
349   memset (&args, 0, sizeof args);
350
351   if (!xdr_guestfs_aug_init_args (xdr_in, &args)) {
352     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_init");
353     return;
354   }
355   root = args.root;
356   flags = args.flags;
357
358   r = do_aug_init (root, flags);
359   if (r == -1)
360     /* do_aug_init has already called reply_with_error */
361     goto done;
362
363   reply (NULL, NULL);
364 done:
365   xdr_free ((xdrproc_t) xdr_guestfs_aug_init_args, (char *) &args);
366 }
367
368 static void aug_close_stub (XDR *xdr_in)
369 {
370   int r;
371
372   r = do_aug_close ();
373   if (r == -1)
374     /* do_aug_close has already called reply_with_error */
375     goto done;
376
377   reply (NULL, NULL);
378 done: ;
379 }
380
381 static void aug_defvar_stub (XDR *xdr_in)
382 {
383   int r;
384   struct guestfs_aug_defvar_args args;
385   const char *name;
386   const char *expr;
387
388   memset (&args, 0, sizeof args);
389
390   if (!xdr_guestfs_aug_defvar_args (xdr_in, &args)) {
391     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_defvar");
392     return;
393   }
394   name = args.name;
395   expr = args.expr ? *args.expr : NULL;
396
397   r = do_aug_defvar (name, expr);
398   if (r == -1)
399     /* do_aug_defvar has already called reply_with_error */
400     goto done;
401
402   struct guestfs_aug_defvar_ret ret;
403   ret.nrnodes = r;
404   reply ((xdrproc_t) &xdr_guestfs_aug_defvar_ret, (char *) &ret);
405 done:
406   xdr_free ((xdrproc_t) xdr_guestfs_aug_defvar_args, (char *) &args);
407 }
408
409 static void aug_defnode_stub (XDR *xdr_in)
410 {
411   guestfs_aug_defnode_ret *r;
412   struct guestfs_aug_defnode_args args;
413   const char *name;
414   const char *expr;
415   const char *val;
416
417   memset (&args, 0, sizeof args);
418
419   if (!xdr_guestfs_aug_defnode_args (xdr_in, &args)) {
420     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_defnode");
421     return;
422   }
423   name = args.name;
424   expr = args.expr;
425   val = args.val;
426
427   r = do_aug_defnode (name, expr, val);
428   if (r == NULL)
429     /* do_aug_defnode has already called reply_with_error */
430     goto done;
431
432   reply ((xdrproc_t) xdr_guestfs_aug_defnode_ret, (char *) r);
433   xdr_free ((xdrproc_t) xdr_guestfs_aug_defnode_ret, (char *) r);
434 done:
435   xdr_free ((xdrproc_t) xdr_guestfs_aug_defnode_args, (char *) &args);
436 }
437
438 static void aug_get_stub (XDR *xdr_in)
439 {
440   char *r;
441   struct guestfs_aug_get_args args;
442   const char *path;
443
444   memset (&args, 0, sizeof args);
445
446   if (!xdr_guestfs_aug_get_args (xdr_in, &args)) {
447     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_get");
448     return;
449   }
450   path = args.path;
451
452   r = do_aug_get (path);
453   if (r == NULL)
454     /* do_aug_get has already called reply_with_error */
455     goto done;
456
457   struct guestfs_aug_get_ret ret;
458   ret.val = r;
459   reply ((xdrproc_t) &xdr_guestfs_aug_get_ret, (char *) &ret);
460   free (r);
461 done:
462   xdr_free ((xdrproc_t) xdr_guestfs_aug_get_args, (char *) &args);
463 }
464
465 static void aug_set_stub (XDR *xdr_in)
466 {
467   int r;
468   struct guestfs_aug_set_args args;
469   const char *path;
470   const char *val;
471
472   memset (&args, 0, sizeof args);
473
474   if (!xdr_guestfs_aug_set_args (xdr_in, &args)) {
475     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_set");
476     return;
477   }
478   path = args.path;
479   val = args.val;
480
481   r = do_aug_set (path, val);
482   if (r == -1)
483     /* do_aug_set has already called reply_with_error */
484     goto done;
485
486   reply (NULL, NULL);
487 done:
488   xdr_free ((xdrproc_t) xdr_guestfs_aug_set_args, (char *) &args);
489 }
490
491 static void aug_insert_stub (XDR *xdr_in)
492 {
493   int r;
494   struct guestfs_aug_insert_args args;
495   const char *path;
496   const char *label;
497   int before;
498
499   memset (&args, 0, sizeof args);
500
501   if (!xdr_guestfs_aug_insert_args (xdr_in, &args)) {
502     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_insert");
503     return;
504   }
505   path = args.path;
506   label = args.label;
507   before = args.before;
508
509   r = do_aug_insert (path, label, before);
510   if (r == -1)
511     /* do_aug_insert has already called reply_with_error */
512     goto done;
513
514   reply (NULL, NULL);
515 done:
516   xdr_free ((xdrproc_t) xdr_guestfs_aug_insert_args, (char *) &args);
517 }
518
519 static void aug_rm_stub (XDR *xdr_in)
520 {
521   int r;
522   struct guestfs_aug_rm_args args;
523   const char *path;
524
525   memset (&args, 0, sizeof args);
526
527   if (!xdr_guestfs_aug_rm_args (xdr_in, &args)) {
528     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_rm");
529     return;
530   }
531   path = args.path;
532
533   r = do_aug_rm (path);
534   if (r == -1)
535     /* do_aug_rm has already called reply_with_error */
536     goto done;
537
538   struct guestfs_aug_rm_ret ret;
539   ret.nrnodes = r;
540   reply ((xdrproc_t) &xdr_guestfs_aug_rm_ret, (char *) &ret);
541 done:
542   xdr_free ((xdrproc_t) xdr_guestfs_aug_rm_args, (char *) &args);
543 }
544
545 static void aug_mv_stub (XDR *xdr_in)
546 {
547   int r;
548   struct guestfs_aug_mv_args args;
549   const char *src;
550   const char *dest;
551
552   memset (&args, 0, sizeof args);
553
554   if (!xdr_guestfs_aug_mv_args (xdr_in, &args)) {
555     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_mv");
556     return;
557   }
558   src = args.src;
559   dest = args.dest;
560
561   r = do_aug_mv (src, dest);
562   if (r == -1)
563     /* do_aug_mv has already called reply_with_error */
564     goto done;
565
566   reply (NULL, NULL);
567 done:
568   xdr_free ((xdrproc_t) xdr_guestfs_aug_mv_args, (char *) &args);
569 }
570
571 static void aug_match_stub (XDR *xdr_in)
572 {
573   char **r;
574   struct guestfs_aug_match_args args;
575   const char *path;
576
577   memset (&args, 0, sizeof args);
578
579   if (!xdr_guestfs_aug_match_args (xdr_in, &args)) {
580     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_match");
581     return;
582   }
583   path = args.path;
584
585   r = do_aug_match (path);
586   if (r == NULL)
587     /* do_aug_match has already called reply_with_error */
588     goto done;
589
590   struct guestfs_aug_match_ret ret;
591   ret.matches.matches_len = count_strings (r);
592   ret.matches.matches_val = r;
593   reply ((xdrproc_t) &xdr_guestfs_aug_match_ret, (char *) &ret);
594   free_strings (r);
595 done:
596   xdr_free ((xdrproc_t) xdr_guestfs_aug_match_args, (char *) &args);
597 }
598
599 static void aug_save_stub (XDR *xdr_in)
600 {
601   int r;
602
603   r = do_aug_save ();
604   if (r == -1)
605     /* do_aug_save has already called reply_with_error */
606     goto done;
607
608   reply (NULL, NULL);
609 done: ;
610 }
611
612 static void aug_load_stub (XDR *xdr_in)
613 {
614   int r;
615
616   r = do_aug_load ();
617   if (r == -1)
618     /* do_aug_load has already called reply_with_error */
619     goto done;
620
621   reply (NULL, NULL);
622 done: ;
623 }
624
625 static void aug_ls_stub (XDR *xdr_in)
626 {
627   char **r;
628   struct guestfs_aug_ls_args args;
629   const char *path;
630
631   memset (&args, 0, sizeof args);
632
633   if (!xdr_guestfs_aug_ls_args (xdr_in, &args)) {
634     reply_with_error ("%s: daemon failed to decode procedure arguments", "aug_ls");
635     return;
636   }
637   path = args.path;
638
639   r = do_aug_ls (path);
640   if (r == NULL)
641     /* do_aug_ls has already called reply_with_error */
642     goto done;
643
644   struct guestfs_aug_ls_ret ret;
645   ret.matches.matches_len = count_strings (r);
646   ret.matches.matches_val = r;
647   reply ((xdrproc_t) &xdr_guestfs_aug_ls_ret, (char *) &ret);
648   free_strings (r);
649 done:
650   xdr_free ((xdrproc_t) xdr_guestfs_aug_ls_args, (char *) &args);
651 }
652
653 static void rm_stub (XDR *xdr_in)
654 {
655   int r;
656   struct guestfs_rm_args args;
657   const char *path;
658
659   memset (&args, 0, sizeof args);
660
661   if (!xdr_guestfs_rm_args (xdr_in, &args)) {
662     reply_with_error ("%s: daemon failed to decode procedure arguments", "rm");
663     return;
664   }
665   path = args.path;
666
667   r = do_rm (path);
668   if (r == -1)
669     /* do_rm has already called reply_with_error */
670     goto done;
671
672   reply (NULL, NULL);
673 done:
674   xdr_free ((xdrproc_t) xdr_guestfs_rm_args, (char *) &args);
675 }
676
677 static void rmdir_stub (XDR *xdr_in)
678 {
679   int r;
680   struct guestfs_rmdir_args args;
681   const char *path;
682
683   memset (&args, 0, sizeof args);
684
685   if (!xdr_guestfs_rmdir_args (xdr_in, &args)) {
686     reply_with_error ("%s: daemon failed to decode procedure arguments", "rmdir");
687     return;
688   }
689   path = args.path;
690
691   r = do_rmdir (path);
692   if (r == -1)
693     /* do_rmdir has already called reply_with_error */
694     goto done;
695
696   reply (NULL, NULL);
697 done:
698   xdr_free ((xdrproc_t) xdr_guestfs_rmdir_args, (char *) &args);
699 }
700
701 static void rm_rf_stub (XDR *xdr_in)
702 {
703   int r;
704   struct guestfs_rm_rf_args args;
705   const char *path;
706
707   memset (&args, 0, sizeof args);
708
709   if (!xdr_guestfs_rm_rf_args (xdr_in, &args)) {
710     reply_with_error ("%s: daemon failed to decode procedure arguments", "rm_rf");
711     return;
712   }
713   path = args.path;
714
715   r = do_rm_rf (path);
716   if (r == -1)
717     /* do_rm_rf has already called reply_with_error */
718     goto done;
719
720   reply (NULL, NULL);
721 done:
722   xdr_free ((xdrproc_t) xdr_guestfs_rm_rf_args, (char *) &args);
723 }
724
725 static void mkdir_stub (XDR *xdr_in)
726 {
727   int r;
728   struct guestfs_mkdir_args args;
729   const char *path;
730
731   memset (&args, 0, sizeof args);
732
733   if (!xdr_guestfs_mkdir_args (xdr_in, &args)) {
734     reply_with_error ("%s: daemon failed to decode procedure arguments", "mkdir");
735     return;
736   }
737   path = args.path;
738
739   r = do_mkdir (path);
740   if (r == -1)
741     /* do_mkdir has already called reply_with_error */
742     goto done;
743
744   reply (NULL, NULL);
745 done:
746   xdr_free ((xdrproc_t) xdr_guestfs_mkdir_args, (char *) &args);
747 }
748
749 static void mkdir_p_stub (XDR *xdr_in)
750 {
751   int r;
752   struct guestfs_mkdir_p_args args;
753   const char *path;
754
755   memset (&args, 0, sizeof args);
756
757   if (!xdr_guestfs_mkdir_p_args (xdr_in, &args)) {
758     reply_with_error ("%s: daemon failed to decode procedure arguments", "mkdir_p");
759     return;
760   }
761   path = args.path;
762
763   r = do_mkdir_p (path);
764   if (r == -1)
765     /* do_mkdir_p has already called reply_with_error */
766     goto done;
767
768   reply (NULL, NULL);
769 done:
770   xdr_free ((xdrproc_t) xdr_guestfs_mkdir_p_args, (char *) &args);
771 }
772
773 static void chmod_stub (XDR *xdr_in)
774 {
775   int r;
776   struct guestfs_chmod_args args;
777   int mode;
778   const char *path;
779
780   memset (&args, 0, sizeof args);
781
782   if (!xdr_guestfs_chmod_args (xdr_in, &args)) {
783     reply_with_error ("%s: daemon failed to decode procedure arguments", "chmod");
784     return;
785   }
786   mode = args.mode;
787   path = args.path;
788
789   r = do_chmod (mode, path);
790   if (r == -1)
791     /* do_chmod has already called reply_with_error */
792     goto done;
793
794   reply (NULL, NULL);
795 done:
796   xdr_free ((xdrproc_t) xdr_guestfs_chmod_args, (char *) &args);
797 }
798
799 static void chown_stub (XDR *xdr_in)
800 {
801   int r;
802   struct guestfs_chown_args args;
803   int owner;
804   int group;
805   const char *path;
806
807   memset (&args, 0, sizeof args);
808
809   if (!xdr_guestfs_chown_args (xdr_in, &args)) {
810     reply_with_error ("%s: daemon failed to decode procedure arguments", "chown");
811     return;
812   }
813   owner = args.owner;
814   group = args.group;
815   path = args.path;
816
817   r = do_chown (owner, group, path);
818   if (r == -1)
819     /* do_chown has already called reply_with_error */
820     goto done;
821
822   reply (NULL, NULL);
823 done:
824   xdr_free ((xdrproc_t) xdr_guestfs_chown_args, (char *) &args);
825 }
826
827 static void exists_stub (XDR *xdr_in)
828 {
829   int r;
830   struct guestfs_exists_args args;
831   const char *path;
832
833   memset (&args, 0, sizeof args);
834
835   if (!xdr_guestfs_exists_args (xdr_in, &args)) {
836     reply_with_error ("%s: daemon failed to decode procedure arguments", "exists");
837     return;
838   }
839   path = args.path;
840
841   r = do_exists (path);
842   if (r == -1)
843     /* do_exists has already called reply_with_error */
844     goto done;
845
846   struct guestfs_exists_ret ret;
847   ret.existsflag = r;
848   reply ((xdrproc_t) &xdr_guestfs_exists_ret, (char *) &ret);
849 done:
850   xdr_free ((xdrproc_t) xdr_guestfs_exists_args, (char *) &args);
851 }
852
853 static void is_file_stub (XDR *xdr_in)
854 {
855   int r;
856   struct guestfs_is_file_args args;
857   const char *path;
858
859   memset (&args, 0, sizeof args);
860
861   if (!xdr_guestfs_is_file_args (xdr_in, &args)) {
862     reply_with_error ("%s: daemon failed to decode procedure arguments", "is_file");
863     return;
864   }
865   path = args.path;
866
867   r = do_is_file (path);
868   if (r == -1)
869     /* do_is_file has already called reply_with_error */
870     goto done;
871
872   struct guestfs_is_file_ret ret;
873   ret.fileflag = r;
874   reply ((xdrproc_t) &xdr_guestfs_is_file_ret, (char *) &ret);
875 done:
876   xdr_free ((xdrproc_t) xdr_guestfs_is_file_args, (char *) &args);
877 }
878
879 static void is_dir_stub (XDR *xdr_in)
880 {
881   int r;
882   struct guestfs_is_dir_args args;
883   const char *path;
884
885   memset (&args, 0, sizeof args);
886
887   if (!xdr_guestfs_is_dir_args (xdr_in, &args)) {
888     reply_with_error ("%s: daemon failed to decode procedure arguments", "is_dir");
889     return;
890   }
891   path = args.path;
892
893   r = do_is_dir (path);
894   if (r == -1)
895     /* do_is_dir has already called reply_with_error */
896     goto done;
897
898   struct guestfs_is_dir_ret ret;
899   ret.dirflag = r;
900   reply ((xdrproc_t) &xdr_guestfs_is_dir_ret, (char *) &ret);
901 done:
902   xdr_free ((xdrproc_t) xdr_guestfs_is_dir_args, (char *) &args);
903 }
904
905 static void pvcreate_stub (XDR *xdr_in)
906 {
907   int r;
908   struct guestfs_pvcreate_args args;
909   const char *device;
910
911   memset (&args, 0, sizeof args);
912
913   if (!xdr_guestfs_pvcreate_args (xdr_in, &args)) {
914     reply_with_error ("%s: daemon failed to decode procedure arguments", "pvcreate");
915     return;
916   }
917   device = args.device;
918
919   r = do_pvcreate (device);
920   if (r == -1)
921     /* do_pvcreate has already called reply_with_error */
922     goto done;
923
924   reply (NULL, NULL);
925 done:
926   xdr_free ((xdrproc_t) xdr_guestfs_pvcreate_args, (char *) &args);
927 }
928
929 static void vgcreate_stub (XDR *xdr_in)
930 {
931   int r;
932   struct guestfs_vgcreate_args args;
933   const char *volgroup;
934   char **physvols;
935
936   memset (&args, 0, sizeof args);
937
938   if (!xdr_guestfs_vgcreate_args (xdr_in, &args)) {
939     reply_with_error ("%s: daemon failed to decode procedure arguments", "vgcreate");
940     return;
941   }
942   volgroup = args.volgroup;
943   args.physvols.physvols_val = realloc (args.physvols.physvols_val, sizeof (char *) * (args.physvols.physvols_len+1));
944   args.physvols.physvols_val[args.physvols.physvols_len] = NULL;
945   physvols = args.physvols.physvols_val;
946
947   r = do_vgcreate (volgroup, physvols);
948   if (r == -1)
949     /* do_vgcreate has already called reply_with_error */
950     goto done;
951
952   reply (NULL, NULL);
953 done:
954   xdr_free ((xdrproc_t) xdr_guestfs_vgcreate_args, (char *) &args);
955 }
956
957 static void lvcreate_stub (XDR *xdr_in)
958 {
959   int r;
960   struct guestfs_lvcreate_args args;
961   const char *logvol;
962   const char *volgroup;
963   int mbytes;
964
965   memset (&args, 0, sizeof args);
966
967   if (!xdr_guestfs_lvcreate_args (xdr_in, &args)) {
968     reply_with_error ("%s: daemon failed to decode procedure arguments", "lvcreate");
969     return;
970   }
971   logvol = args.logvol;
972   volgroup = args.volgroup;
973   mbytes = args.mbytes;
974
975   r = do_lvcreate (logvol, volgroup, mbytes);
976   if (r == -1)
977     /* do_lvcreate has already called reply_with_error */
978     goto done;
979
980   reply (NULL, NULL);
981 done:
982   xdr_free ((xdrproc_t) xdr_guestfs_lvcreate_args, (char *) &args);
983 }
984
985 static void mkfs_stub (XDR *xdr_in)
986 {
987   int r;
988   struct guestfs_mkfs_args args;
989   const char *fstype;
990   const char *device;
991
992   memset (&args, 0, sizeof args);
993
994   if (!xdr_guestfs_mkfs_args (xdr_in, &args)) {
995     reply_with_error ("%s: daemon failed to decode procedure arguments", "mkfs");
996     return;
997   }
998   fstype = args.fstype;
999   device = args.device;
1000
1001   r = do_mkfs (fstype, device);
1002   if (r == -1)
1003     /* do_mkfs has already called reply_with_error */
1004     goto done;
1005
1006   reply (NULL, NULL);
1007 done:
1008   xdr_free ((xdrproc_t) xdr_guestfs_mkfs_args, (char *) &args);
1009 }
1010
1011 static void sfdisk_stub (XDR *xdr_in)
1012 {
1013   int r;
1014   struct guestfs_sfdisk_args args;
1015   const char *device;
1016   int cyls;
1017   int heads;
1018   int sectors;
1019   char **lines;
1020
1021   memset (&args, 0, sizeof args);
1022
1023   if (!xdr_guestfs_sfdisk_args (xdr_in, &args)) {
1024     reply_with_error ("%s: daemon failed to decode procedure arguments", "sfdisk");
1025     return;
1026   }
1027   device = args.device;
1028   cyls = args.cyls;
1029   heads = args.heads;
1030   sectors = args.sectors;
1031   args.lines.lines_val = realloc (args.lines.lines_val, sizeof (char *) * (args.lines.lines_len+1));
1032   args.lines.lines_val[args.lines.lines_len] = NULL;
1033   lines = args.lines.lines_val;
1034
1035   r = do_sfdisk (device, cyls, heads, sectors, lines);
1036   if (r == -1)
1037     /* do_sfdisk has already called reply_with_error */
1038     goto done;
1039
1040   reply (NULL, NULL);
1041 done:
1042   xdr_free ((xdrproc_t) xdr_guestfs_sfdisk_args, (char *) &args);
1043 }
1044
1045 static void write_file_stub (XDR *xdr_in)
1046 {
1047   int r;
1048   struct guestfs_write_file_args args;
1049   const char *path;
1050   const char *content;
1051   int size;
1052
1053   memset (&args, 0, sizeof args);
1054
1055   if (!xdr_guestfs_write_file_args (xdr_in, &args)) {
1056     reply_with_error ("%s: daemon failed to decode procedure arguments", "write_file");
1057     return;
1058   }
1059   path = args.path;
1060   content = args.content;
1061   size = args.size;
1062
1063   r = do_write_file (path, content, size);
1064   if (r == -1)
1065     /* do_write_file has already called reply_with_error */
1066     goto done;
1067
1068   reply (NULL, NULL);
1069 done:
1070   xdr_free ((xdrproc_t) xdr_guestfs_write_file_args, (char *) &args);
1071 }
1072
1073 static void umount_stub (XDR *xdr_in)
1074 {
1075   int r;
1076   struct guestfs_umount_args args;
1077   const char *pathordevice;
1078
1079   memset (&args, 0, sizeof args);
1080
1081   if (!xdr_guestfs_umount_args (xdr_in, &args)) {
1082     reply_with_error ("%s: daemon failed to decode procedure arguments", "umount");
1083     return;
1084   }
1085   pathordevice = args.pathordevice;
1086
1087   r = do_umount (pathordevice);
1088   if (r == -1)
1089     /* do_umount has already called reply_with_error */
1090     goto done;
1091
1092   reply (NULL, NULL);
1093 done:
1094   xdr_free ((xdrproc_t) xdr_guestfs_umount_args, (char *) &args);
1095 }
1096
1097 static void mounts_stub (XDR *xdr_in)
1098 {
1099   char **r;
1100
1101   r = do_mounts ();
1102   if (r == NULL)
1103     /* do_mounts has already called reply_with_error */
1104     goto done;
1105
1106   struct guestfs_mounts_ret ret;
1107   ret.devices.devices_len = count_strings (r);
1108   ret.devices.devices_val = r;
1109   reply ((xdrproc_t) &xdr_guestfs_mounts_ret, (char *) &ret);
1110   free_strings (r);
1111 done: ;
1112 }
1113
1114 static void umount_all_stub (XDR *xdr_in)
1115 {
1116   int r;
1117
1118   r = do_umount_all ();
1119   if (r == -1)
1120     /* do_umount_all has already called reply_with_error */
1121     goto done;
1122
1123   reply (NULL, NULL);
1124 done: ;
1125 }
1126
1127 static void lvm_remove_all_stub (XDR *xdr_in)
1128 {
1129   int r;
1130
1131   r = do_lvm_remove_all ();
1132   if (r == -1)
1133     /* do_lvm_remove_all has already called reply_with_error */
1134     goto done;
1135
1136   reply (NULL, NULL);
1137 done: ;
1138 }
1139
1140 void dispatch_incoming_message (XDR *xdr_in)
1141 {
1142   switch (proc_nr) {
1143     case GUESTFS_PROC_MOUNT:
1144       mount_stub (xdr_in);
1145       break;
1146     case GUESTFS_PROC_SYNC:
1147       sync_stub (xdr_in);
1148       break;
1149     case GUESTFS_PROC_TOUCH:
1150       touch_stub (xdr_in);
1151       break;
1152     case GUESTFS_PROC_CAT:
1153       cat_stub (xdr_in);
1154       break;
1155     case GUESTFS_PROC_LL:
1156       ll_stub (xdr_in);
1157       break;
1158     case GUESTFS_PROC_LS:
1159       ls_stub (xdr_in);
1160       break;
1161     case GUESTFS_PROC_LIST_DEVICES:
1162       list_devices_stub (xdr_in);
1163       break;
1164     case GUESTFS_PROC_LIST_PARTITIONS:
1165       list_partitions_stub (xdr_in);
1166       break;
1167     case GUESTFS_PROC_PVS:
1168       pvs_stub (xdr_in);
1169       break;
1170     case GUESTFS_PROC_VGS:
1171       vgs_stub (xdr_in);
1172       break;
1173     case GUESTFS_PROC_LVS:
1174       lvs_stub (xdr_in);
1175       break;
1176     case GUESTFS_PROC_PVS_FULL:
1177       pvs_full_stub (xdr_in);
1178       break;
1179     case GUESTFS_PROC_VGS_FULL:
1180       vgs_full_stub (xdr_in);
1181       break;
1182     case GUESTFS_PROC_LVS_FULL:
1183       lvs_full_stub (xdr_in);
1184       break;
1185     case GUESTFS_PROC_READ_LINES:
1186       read_lines_stub (xdr_in);
1187       break;
1188     case GUESTFS_PROC_AUG_INIT:
1189       aug_init_stub (xdr_in);
1190       break;
1191     case GUESTFS_PROC_AUG_CLOSE:
1192       aug_close_stub (xdr_in);
1193       break;
1194     case GUESTFS_PROC_AUG_DEFVAR:
1195       aug_defvar_stub (xdr_in);
1196       break;
1197     case GUESTFS_PROC_AUG_DEFNODE:
1198       aug_defnode_stub (xdr_in);
1199       break;
1200     case GUESTFS_PROC_AUG_GET:
1201       aug_get_stub (xdr_in);
1202       break;
1203     case GUESTFS_PROC_AUG_SET:
1204       aug_set_stub (xdr_in);
1205       break;
1206     case GUESTFS_PROC_AUG_INSERT:
1207       aug_insert_stub (xdr_in);
1208       break;
1209     case GUESTFS_PROC_AUG_RM:
1210       aug_rm_stub (xdr_in);
1211       break;
1212     case GUESTFS_PROC_AUG_MV:
1213       aug_mv_stub (xdr_in);
1214       break;
1215     case GUESTFS_PROC_AUG_MATCH:
1216       aug_match_stub (xdr_in);
1217       break;
1218     case GUESTFS_PROC_AUG_SAVE:
1219       aug_save_stub (xdr_in);
1220       break;
1221     case GUESTFS_PROC_AUG_LOAD:
1222       aug_load_stub (xdr_in);
1223       break;
1224     case GUESTFS_PROC_AUG_LS:
1225       aug_ls_stub (xdr_in);
1226       break;
1227     case GUESTFS_PROC_RM:
1228       rm_stub (xdr_in);
1229       break;
1230     case GUESTFS_PROC_RMDIR:
1231       rmdir_stub (xdr_in);
1232       break;
1233     case GUESTFS_PROC_RM_RF:
1234       rm_rf_stub (xdr_in);
1235       break;
1236     case GUESTFS_PROC_MKDIR:
1237       mkdir_stub (xdr_in);
1238       break;
1239     case GUESTFS_PROC_MKDIR_P:
1240       mkdir_p_stub (xdr_in);
1241       break;
1242     case GUESTFS_PROC_CHMOD:
1243       chmod_stub (xdr_in);
1244       break;
1245     case GUESTFS_PROC_CHOWN:
1246       chown_stub (xdr_in);
1247       break;
1248     case GUESTFS_PROC_EXISTS:
1249       exists_stub (xdr_in);
1250       break;
1251     case GUESTFS_PROC_IS_FILE:
1252       is_file_stub (xdr_in);
1253       break;
1254     case GUESTFS_PROC_IS_DIR:
1255       is_dir_stub (xdr_in);
1256       break;
1257     case GUESTFS_PROC_PVCREATE:
1258       pvcreate_stub (xdr_in);
1259       break;
1260     case GUESTFS_PROC_VGCREATE:
1261       vgcreate_stub (xdr_in);
1262       break;
1263     case GUESTFS_PROC_LVCREATE:
1264       lvcreate_stub (xdr_in);
1265       break;
1266     case GUESTFS_PROC_MKFS:
1267       mkfs_stub (xdr_in);
1268       break;
1269     case GUESTFS_PROC_SFDISK:
1270       sfdisk_stub (xdr_in);
1271       break;
1272     case GUESTFS_PROC_WRITE_FILE:
1273       write_file_stub (xdr_in);
1274       break;
1275     case GUESTFS_PROC_UMOUNT:
1276       umount_stub (xdr_in);
1277       break;
1278     case GUESTFS_PROC_MOUNTS:
1279       mounts_stub (xdr_in);
1280       break;
1281     case GUESTFS_PROC_UMOUNT_ALL:
1282       umount_all_stub (xdr_in);
1283       break;
1284     case GUESTFS_PROC_LVM_REMOVE_ALL:
1285       lvm_remove_all_stub (xdr_in);
1286       break;
1287     default:
1288       reply_with_error ("dispatch_incoming_message: unknown procedure number %d", proc_nr);
1289   }
1290 }
1291
1292 static const char *lvm_pv_cols = "pv_name,pv_uuid,pv_fmt,pv_size,dev_size,pv_free,pv_used,pv_attr,pv_pe_count,pv_pe_alloc_count,pv_tags,pe_start,pv_mda_count,pv_mda_free";
1293
1294 static int lvm_tokenize_pv (char *str, struct guestfs_lvm_int_pv *r)
1295 {
1296   char *tok, *p, *next;
1297   int i, j;
1298
1299   if (!str) {
1300     fprintf (stderr, "%s: failed: passed a NULL string\n", __func__);
1301     return -1;
1302   }
1303   if (!*str || isspace (*str)) {
1304     fprintf (stderr, "%s: failed: passed a empty string or one beginning with whitespace\n", __func__);
1305     return -1;
1306   }
1307   tok = str;
1308   if (!tok) {
1309     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_name");
1310     return -1;
1311   }
1312   p = strchrnul (tok, ',');
1313   if (*p) next = p+1; else next = NULL;
1314   *p = '\0';
1315   r->pv_name = strdup (tok);
1316   if (r->pv_name == NULL) {
1317     perror ("strdup");
1318     return -1;
1319   }
1320   tok = next;
1321   if (!tok) {
1322     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_uuid");
1323     return -1;
1324   }
1325   p = strchrnul (tok, ',');
1326   if (*p) next = p+1; else next = NULL;
1327   *p = '\0';
1328   for (i = j = 0; i < 32; ++j) {
1329     if (tok[j] == '\0') {
1330       fprintf (stderr, "%s: failed to parse UUID from '%s'\n", __func__, tok);
1331       return -1;
1332     } else if (tok[j] != '-')
1333       r->pv_uuid[i++] = tok[j];
1334   }
1335   tok = next;
1336   if (!tok) {
1337     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_fmt");
1338     return -1;
1339   }
1340   p = strchrnul (tok, ',');
1341   if (*p) next = p+1; else next = NULL;
1342   *p = '\0';
1343   r->pv_fmt = strdup (tok);
1344   if (r->pv_fmt == NULL) {
1345     perror ("strdup");
1346     return -1;
1347   }
1348   tok = next;
1349   if (!tok) {
1350     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_size");
1351     return -1;
1352   }
1353   p = strchrnul (tok, ',');
1354   if (*p) next = p+1; else next = NULL;
1355   *p = '\0';
1356   if (sscanf (tok, "%"SCNu64, &r->pv_size) != 1) {
1357     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "pv_size");
1358     return -1;
1359   }
1360   tok = next;
1361   if (!tok) {
1362     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "dev_size");
1363     return -1;
1364   }
1365   p = strchrnul (tok, ',');
1366   if (*p) next = p+1; else next = NULL;
1367   *p = '\0';
1368   if (sscanf (tok, "%"SCNu64, &r->dev_size) != 1) {
1369     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "dev_size");
1370     return -1;
1371   }
1372   tok = next;
1373   if (!tok) {
1374     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_free");
1375     return -1;
1376   }
1377   p = strchrnul (tok, ',');
1378   if (*p) next = p+1; else next = NULL;
1379   *p = '\0';
1380   if (sscanf (tok, "%"SCNu64, &r->pv_free) != 1) {
1381     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "pv_free");
1382     return -1;
1383   }
1384   tok = next;
1385   if (!tok) {
1386     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_used");
1387     return -1;
1388   }
1389   p = strchrnul (tok, ',');
1390   if (*p) next = p+1; else next = NULL;
1391   *p = '\0';
1392   if (sscanf (tok, "%"SCNu64, &r->pv_used) != 1) {
1393     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "pv_used");
1394     return -1;
1395   }
1396   tok = next;
1397   if (!tok) {
1398     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_attr");
1399     return -1;
1400   }
1401   p = strchrnul (tok, ',');
1402   if (*p) next = p+1; else next = NULL;
1403   *p = '\0';
1404   r->pv_attr = strdup (tok);
1405   if (r->pv_attr == NULL) {
1406     perror ("strdup");
1407     return -1;
1408   }
1409   tok = next;
1410   if (!tok) {
1411     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_pe_count");
1412     return -1;
1413   }
1414   p = strchrnul (tok, ',');
1415   if (*p) next = p+1; else next = NULL;
1416   *p = '\0';
1417   if (sscanf (tok, "%"SCNi64, &r->pv_pe_count) != 1) {
1418     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "pv_pe_count");
1419     return -1;
1420   }
1421   tok = next;
1422   if (!tok) {
1423     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_pe_alloc_count");
1424     return -1;
1425   }
1426   p = strchrnul (tok, ',');
1427   if (*p) next = p+1; else next = NULL;
1428   *p = '\0';
1429   if (sscanf (tok, "%"SCNi64, &r->pv_pe_alloc_count) != 1) {
1430     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "pv_pe_alloc_count");
1431     return -1;
1432   }
1433   tok = next;
1434   if (!tok) {
1435     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_tags");
1436     return -1;
1437   }
1438   p = strchrnul (tok, ',');
1439   if (*p) next = p+1; else next = NULL;
1440   *p = '\0';
1441   r->pv_tags = strdup (tok);
1442   if (r->pv_tags == NULL) {
1443     perror ("strdup");
1444     return -1;
1445   }
1446   tok = next;
1447   if (!tok) {
1448     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pe_start");
1449     return -1;
1450   }
1451   p = strchrnul (tok, ',');
1452   if (*p) next = p+1; else next = NULL;
1453   *p = '\0';
1454   if (sscanf (tok, "%"SCNu64, &r->pe_start) != 1) {
1455     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "pe_start");
1456     return -1;
1457   }
1458   tok = next;
1459   if (!tok) {
1460     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_mda_count");
1461     return -1;
1462   }
1463   p = strchrnul (tok, ',');
1464   if (*p) next = p+1; else next = NULL;
1465   *p = '\0';
1466   if (sscanf (tok, "%"SCNi64, &r->pv_mda_count) != 1) {
1467     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "pv_mda_count");
1468     return -1;
1469   }
1470   tok = next;
1471   if (!tok) {
1472     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_mda_free");
1473     return -1;
1474   }
1475   p = strchrnul (tok, ',');
1476   if (*p) next = p+1; else next = NULL;
1477   *p = '\0';
1478   if (sscanf (tok, "%"SCNu64, &r->pv_mda_free) != 1) {
1479     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "pv_mda_free");
1480     return -1;
1481   }
1482   tok = next;
1483   if (tok != NULL) {
1484     fprintf (stderr, "%s: failed: extra tokens at end of string\n", __func__);
1485     return -1;
1486   }
1487   return 0;
1488 }
1489
1490 guestfs_lvm_int_pv_list *
1491 parse_command_line_pvs (void)
1492 {
1493   char *out, *err;
1494   char *p, *pend;
1495   int r, i;
1496   guestfs_lvm_int_pv_list *ret;
1497   void *newp;
1498
1499   ret = malloc (sizeof *ret);
1500   if (!ret) {
1501     reply_with_perror ("malloc");
1502     return NULL;
1503   }
1504
1505   ret->guestfs_lvm_int_pv_list_len = 0;
1506   ret->guestfs_lvm_int_pv_list_val = NULL;
1507
1508   r = command (&out, &err,
1509                "/sbin/lvm", "pvs",
1510                "-o", lvm_pv_cols, "--unbuffered", "--noheadings",
1511                "--nosuffix", "--separator", ",", "--units", "b", NULL);
1512   if (r == -1) {
1513     reply_with_error ("%s", err);
1514     free (out);
1515     free (err);
1516     return NULL;
1517   }
1518
1519   free (err);
1520
1521   /* Tokenize each line of the output. */
1522   p = out;
1523   i = 0;
1524   while (p) {
1525     pend = strchr (p, '\n');    /* Get the next line of output. */
1526     if (pend) {
1527       *pend = '\0';
1528       pend++;
1529     }
1530
1531     while (*p && isspace (*p))  /* Skip any leading whitespace. */
1532       p++;
1533
1534     if (!*p) {                  /* Empty line?  Skip it. */
1535       p = pend;
1536       continue;
1537     }
1538
1539     /* Allocate some space to store this next entry. */
1540     newp = realloc (ret->guestfs_lvm_int_pv_list_val,
1541                     sizeof (guestfs_lvm_int_pv) * (i+1));
1542     if (newp == NULL) {
1543       reply_with_perror ("realloc");
1544       free (ret->guestfs_lvm_int_pv_list_val);
1545       free (ret);
1546       free (out);
1547       return NULL;
1548     }
1549     ret->guestfs_lvm_int_pv_list_val = newp;
1550
1551     /* Tokenize the next entry. */
1552     r = lvm_tokenize_pv (p, &ret->guestfs_lvm_int_pv_list_val[i]);
1553     if (r == -1) {
1554       reply_with_error ("failed to parse output of 'pvs' command");
1555       free (ret->guestfs_lvm_int_pv_list_val);
1556       free (ret);
1557       free (out);
1558       return NULL;
1559     }
1560
1561     ++i;
1562     p = pend;
1563   }
1564
1565   ret->guestfs_lvm_int_pv_list_len = i;
1566
1567   free (out);
1568   return ret;
1569 }
1570 static const char *lvm_vg_cols = "vg_name,vg_uuid,vg_fmt,vg_attr,vg_size,vg_free,vg_sysid,vg_extent_size,vg_extent_count,vg_free_count,max_lv,max_pv,pv_count,lv_count,snap_count,vg_seqno,vg_tags,vg_mda_count,vg_mda_free";
1571
1572 static int lvm_tokenize_vg (char *str, struct guestfs_lvm_int_vg *r)
1573 {
1574   char *tok, *p, *next;
1575   int i, j;
1576
1577   if (!str) {
1578     fprintf (stderr, "%s: failed: passed a NULL string\n", __func__);
1579     return -1;
1580   }
1581   if (!*str || isspace (*str)) {
1582     fprintf (stderr, "%s: failed: passed a empty string or one beginning with whitespace\n", __func__);
1583     return -1;
1584   }
1585   tok = str;
1586   if (!tok) {
1587     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_name");
1588     return -1;
1589   }
1590   p = strchrnul (tok, ',');
1591   if (*p) next = p+1; else next = NULL;
1592   *p = '\0';
1593   r->vg_name = strdup (tok);
1594   if (r->vg_name == NULL) {
1595     perror ("strdup");
1596     return -1;
1597   }
1598   tok = next;
1599   if (!tok) {
1600     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_uuid");
1601     return -1;
1602   }
1603   p = strchrnul (tok, ',');
1604   if (*p) next = p+1; else next = NULL;
1605   *p = '\0';
1606   for (i = j = 0; i < 32; ++j) {
1607     if (tok[j] == '\0') {
1608       fprintf (stderr, "%s: failed to parse UUID from '%s'\n", __func__, tok);
1609       return -1;
1610     } else if (tok[j] != '-')
1611       r->vg_uuid[i++] = tok[j];
1612   }
1613   tok = next;
1614   if (!tok) {
1615     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_fmt");
1616     return -1;
1617   }
1618   p = strchrnul (tok, ',');
1619   if (*p) next = p+1; else next = NULL;
1620   *p = '\0';
1621   r->vg_fmt = strdup (tok);
1622   if (r->vg_fmt == NULL) {
1623     perror ("strdup");
1624     return -1;
1625   }
1626   tok = next;
1627   if (!tok) {
1628     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_attr");
1629     return -1;
1630   }
1631   p = strchrnul (tok, ',');
1632   if (*p) next = p+1; else next = NULL;
1633   *p = '\0';
1634   r->vg_attr = strdup (tok);
1635   if (r->vg_attr == NULL) {
1636     perror ("strdup");
1637     return -1;
1638   }
1639   tok = next;
1640   if (!tok) {
1641     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_size");
1642     return -1;
1643   }
1644   p = strchrnul (tok, ',');
1645   if (*p) next = p+1; else next = NULL;
1646   *p = '\0';
1647   if (sscanf (tok, "%"SCNu64, &r->vg_size) != 1) {
1648     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "vg_size");
1649     return -1;
1650   }
1651   tok = next;
1652   if (!tok) {
1653     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_free");
1654     return -1;
1655   }
1656   p = strchrnul (tok, ',');
1657   if (*p) next = p+1; else next = NULL;
1658   *p = '\0';
1659   if (sscanf (tok, "%"SCNu64, &r->vg_free) != 1) {
1660     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "vg_free");
1661     return -1;
1662   }
1663   tok = next;
1664   if (!tok) {
1665     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_sysid");
1666     return -1;
1667   }
1668   p = strchrnul (tok, ',');
1669   if (*p) next = p+1; else next = NULL;
1670   *p = '\0';
1671   r->vg_sysid = strdup (tok);
1672   if (r->vg_sysid == NULL) {
1673     perror ("strdup");
1674     return -1;
1675   }
1676   tok = next;
1677   if (!tok) {
1678     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_extent_size");
1679     return -1;
1680   }
1681   p = strchrnul (tok, ',');
1682   if (*p) next = p+1; else next = NULL;
1683   *p = '\0';
1684   if (sscanf (tok, "%"SCNu64, &r->vg_extent_size) != 1) {
1685     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "vg_extent_size");
1686     return -1;
1687   }
1688   tok = next;
1689   if (!tok) {
1690     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_extent_count");
1691     return -1;
1692   }
1693   p = strchrnul (tok, ',');
1694   if (*p) next = p+1; else next = NULL;
1695   *p = '\0';
1696   if (sscanf (tok, "%"SCNi64, &r->vg_extent_count) != 1) {
1697     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "vg_extent_count");
1698     return -1;
1699   }
1700   tok = next;
1701   if (!tok) {
1702     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_free_count");
1703     return -1;
1704   }
1705   p = strchrnul (tok, ',');
1706   if (*p) next = p+1; else next = NULL;
1707   *p = '\0';
1708   if (sscanf (tok, "%"SCNi64, &r->vg_free_count) != 1) {
1709     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "vg_free_count");
1710     return -1;
1711   }
1712   tok = next;
1713   if (!tok) {
1714     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "max_lv");
1715     return -1;
1716   }
1717   p = strchrnul (tok, ',');
1718   if (*p) next = p+1; else next = NULL;
1719   *p = '\0';
1720   if (sscanf (tok, "%"SCNi64, &r->max_lv) != 1) {
1721     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "max_lv");
1722     return -1;
1723   }
1724   tok = next;
1725   if (!tok) {
1726     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "max_pv");
1727     return -1;
1728   }
1729   p = strchrnul (tok, ',');
1730   if (*p) next = p+1; else next = NULL;
1731   *p = '\0';
1732   if (sscanf (tok, "%"SCNi64, &r->max_pv) != 1) {
1733     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "max_pv");
1734     return -1;
1735   }
1736   tok = next;
1737   if (!tok) {
1738     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "pv_count");
1739     return -1;
1740   }
1741   p = strchrnul (tok, ',');
1742   if (*p) next = p+1; else next = NULL;
1743   *p = '\0';
1744   if (sscanf (tok, "%"SCNi64, &r->pv_count) != 1) {
1745     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "pv_count");
1746     return -1;
1747   }
1748   tok = next;
1749   if (!tok) {
1750     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_count");
1751     return -1;
1752   }
1753   p = strchrnul (tok, ',');
1754   if (*p) next = p+1; else next = NULL;
1755   *p = '\0';
1756   if (sscanf (tok, "%"SCNi64, &r->lv_count) != 1) {
1757     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "lv_count");
1758     return -1;
1759   }
1760   tok = next;
1761   if (!tok) {
1762     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "snap_count");
1763     return -1;
1764   }
1765   p = strchrnul (tok, ',');
1766   if (*p) next = p+1; else next = NULL;
1767   *p = '\0';
1768   if (sscanf (tok, "%"SCNi64, &r->snap_count) != 1) {
1769     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "snap_count");
1770     return -1;
1771   }
1772   tok = next;
1773   if (!tok) {
1774     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_seqno");
1775     return -1;
1776   }
1777   p = strchrnul (tok, ',');
1778   if (*p) next = p+1; else next = NULL;
1779   *p = '\0';
1780   if (sscanf (tok, "%"SCNi64, &r->vg_seqno) != 1) {
1781     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "vg_seqno");
1782     return -1;
1783   }
1784   tok = next;
1785   if (!tok) {
1786     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_tags");
1787     return -1;
1788   }
1789   p = strchrnul (tok, ',');
1790   if (*p) next = p+1; else next = NULL;
1791   *p = '\0';
1792   r->vg_tags = strdup (tok);
1793   if (r->vg_tags == NULL) {
1794     perror ("strdup");
1795     return -1;
1796   }
1797   tok = next;
1798   if (!tok) {
1799     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_mda_count");
1800     return -1;
1801   }
1802   p = strchrnul (tok, ',');
1803   if (*p) next = p+1; else next = NULL;
1804   *p = '\0';
1805   if (sscanf (tok, "%"SCNi64, &r->vg_mda_count) != 1) {
1806     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "vg_mda_count");
1807     return -1;
1808   }
1809   tok = next;
1810   if (!tok) {
1811     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "vg_mda_free");
1812     return -1;
1813   }
1814   p = strchrnul (tok, ',');
1815   if (*p) next = p+1; else next = NULL;
1816   *p = '\0';
1817   if (sscanf (tok, "%"SCNu64, &r->vg_mda_free) != 1) {
1818     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "vg_mda_free");
1819     return -1;
1820   }
1821   tok = next;
1822   if (tok != NULL) {
1823     fprintf (stderr, "%s: failed: extra tokens at end of string\n", __func__);
1824     return -1;
1825   }
1826   return 0;
1827 }
1828
1829 guestfs_lvm_int_vg_list *
1830 parse_command_line_vgs (void)
1831 {
1832   char *out, *err;
1833   char *p, *pend;
1834   int r, i;
1835   guestfs_lvm_int_vg_list *ret;
1836   void *newp;
1837
1838   ret = malloc (sizeof *ret);
1839   if (!ret) {
1840     reply_with_perror ("malloc");
1841     return NULL;
1842   }
1843
1844   ret->guestfs_lvm_int_vg_list_len = 0;
1845   ret->guestfs_lvm_int_vg_list_val = NULL;
1846
1847   r = command (&out, &err,
1848                "/sbin/lvm", "vgs",
1849                "-o", lvm_vg_cols, "--unbuffered", "--noheadings",
1850                "--nosuffix", "--separator", ",", "--units", "b", NULL);
1851   if (r == -1) {
1852     reply_with_error ("%s", err);
1853     free (out);
1854     free (err);
1855     return NULL;
1856   }
1857
1858   free (err);
1859
1860   /* Tokenize each line of the output. */
1861   p = out;
1862   i = 0;
1863   while (p) {
1864     pend = strchr (p, '\n');    /* Get the next line of output. */
1865     if (pend) {
1866       *pend = '\0';
1867       pend++;
1868     }
1869
1870     while (*p && isspace (*p))  /* Skip any leading whitespace. */
1871       p++;
1872
1873     if (!*p) {                  /* Empty line?  Skip it. */
1874       p = pend;
1875       continue;
1876     }
1877
1878     /* Allocate some space to store this next entry. */
1879     newp = realloc (ret->guestfs_lvm_int_vg_list_val,
1880                     sizeof (guestfs_lvm_int_vg) * (i+1));
1881     if (newp == NULL) {
1882       reply_with_perror ("realloc");
1883       free (ret->guestfs_lvm_int_vg_list_val);
1884       free (ret);
1885       free (out);
1886       return NULL;
1887     }
1888     ret->guestfs_lvm_int_vg_list_val = newp;
1889
1890     /* Tokenize the next entry. */
1891     r = lvm_tokenize_vg (p, &ret->guestfs_lvm_int_vg_list_val[i]);
1892     if (r == -1) {
1893       reply_with_error ("failed to parse output of 'vgs' command");
1894       free (ret->guestfs_lvm_int_vg_list_val);
1895       free (ret);
1896       free (out);
1897       return NULL;
1898     }
1899
1900     ++i;
1901     p = pend;
1902   }
1903
1904   ret->guestfs_lvm_int_vg_list_len = i;
1905
1906   free (out);
1907   return ret;
1908 }
1909 static const char *lvm_lv_cols = "lv_name,lv_uuid,lv_attr,lv_major,lv_minor,lv_kernel_major,lv_kernel_minor,lv_size,seg_count,origin,snap_percent,copy_percent,move_pv,lv_tags,mirror_log,modules";
1910
1911 static int lvm_tokenize_lv (char *str, struct guestfs_lvm_int_lv *r)
1912 {
1913   char *tok, *p, *next;
1914   int i, j;
1915
1916   if (!str) {
1917     fprintf (stderr, "%s: failed: passed a NULL string\n", __func__);
1918     return -1;
1919   }
1920   if (!*str || isspace (*str)) {
1921     fprintf (stderr, "%s: failed: passed a empty string or one beginning with whitespace\n", __func__);
1922     return -1;
1923   }
1924   tok = str;
1925   if (!tok) {
1926     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_name");
1927     return -1;
1928   }
1929   p = strchrnul (tok, ',');
1930   if (*p) next = p+1; else next = NULL;
1931   *p = '\0';
1932   r->lv_name = strdup (tok);
1933   if (r->lv_name == NULL) {
1934     perror ("strdup");
1935     return -1;
1936   }
1937   tok = next;
1938   if (!tok) {
1939     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_uuid");
1940     return -1;
1941   }
1942   p = strchrnul (tok, ',');
1943   if (*p) next = p+1; else next = NULL;
1944   *p = '\0';
1945   for (i = j = 0; i < 32; ++j) {
1946     if (tok[j] == '\0') {
1947       fprintf (stderr, "%s: failed to parse UUID from '%s'\n", __func__, tok);
1948       return -1;
1949     } else if (tok[j] != '-')
1950       r->lv_uuid[i++] = tok[j];
1951   }
1952   tok = next;
1953   if (!tok) {
1954     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_attr");
1955     return -1;
1956   }
1957   p = strchrnul (tok, ',');
1958   if (*p) next = p+1; else next = NULL;
1959   *p = '\0';
1960   r->lv_attr = strdup (tok);
1961   if (r->lv_attr == NULL) {
1962     perror ("strdup");
1963     return -1;
1964   }
1965   tok = next;
1966   if (!tok) {
1967     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_major");
1968     return -1;
1969   }
1970   p = strchrnul (tok, ',');
1971   if (*p) next = p+1; else next = NULL;
1972   *p = '\0';
1973   if (sscanf (tok, "%"SCNi64, &r->lv_major) != 1) {
1974     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "lv_major");
1975     return -1;
1976   }
1977   tok = next;
1978   if (!tok) {
1979     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_minor");
1980     return -1;
1981   }
1982   p = strchrnul (tok, ',');
1983   if (*p) next = p+1; else next = NULL;
1984   *p = '\0';
1985   if (sscanf (tok, "%"SCNi64, &r->lv_minor) != 1) {
1986     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "lv_minor");
1987     return -1;
1988   }
1989   tok = next;
1990   if (!tok) {
1991     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_kernel_major");
1992     return -1;
1993   }
1994   p = strchrnul (tok, ',');
1995   if (*p) next = p+1; else next = NULL;
1996   *p = '\0';
1997   if (sscanf (tok, "%"SCNi64, &r->lv_kernel_major) != 1) {
1998     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "lv_kernel_major");
1999     return -1;
2000   }
2001   tok = next;
2002   if (!tok) {
2003     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_kernel_minor");
2004     return -1;
2005   }
2006   p = strchrnul (tok, ',');
2007   if (*p) next = p+1; else next = NULL;
2008   *p = '\0';
2009   if (sscanf (tok, "%"SCNi64, &r->lv_kernel_minor) != 1) {
2010     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "lv_kernel_minor");
2011     return -1;
2012   }
2013   tok = next;
2014   if (!tok) {
2015     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_size");
2016     return -1;
2017   }
2018   p = strchrnul (tok, ',');
2019   if (*p) next = p+1; else next = NULL;
2020   *p = '\0';
2021   if (sscanf (tok, "%"SCNu64, &r->lv_size) != 1) {
2022     fprintf (stderr, "%s: failed to parse size '%s' from token %s\n", __func__, tok, "lv_size");
2023     return -1;
2024   }
2025   tok = next;
2026   if (!tok) {
2027     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "seg_count");
2028     return -1;
2029   }
2030   p = strchrnul (tok, ',');
2031   if (*p) next = p+1; else next = NULL;
2032   *p = '\0';
2033   if (sscanf (tok, "%"SCNi64, &r->seg_count) != 1) {
2034     fprintf (stderr, "%s: failed to parse int '%s' from token %s\n", __func__, tok, "seg_count");
2035     return -1;
2036   }
2037   tok = next;
2038   if (!tok) {
2039     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "origin");
2040     return -1;
2041   }
2042   p = strchrnul (tok, ',');
2043   if (*p) next = p+1; else next = NULL;
2044   *p = '\0';
2045   r->origin = strdup (tok);
2046   if (r->origin == NULL) {
2047     perror ("strdup");
2048     return -1;
2049   }
2050   tok = next;
2051   if (!tok) {
2052     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "snap_percent");
2053     return -1;
2054   }
2055   p = strchrnul (tok, ',');
2056   if (*p) next = p+1; else next = NULL;
2057   *p = '\0';
2058   if (tok[0] == '\0')
2059     r->snap_percent = -1;
2060   else if (sscanf (tok, "%f", &r->snap_percent) != 1) {
2061     fprintf (stderr, "%s: failed to parse float '%s' from token %s\n", __func__, tok, "snap_percent");
2062     return -1;
2063   }
2064   tok = next;
2065   if (!tok) {
2066     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "copy_percent");
2067     return -1;
2068   }
2069   p = strchrnul (tok, ',');
2070   if (*p) next = p+1; else next = NULL;
2071   *p = '\0';
2072   if (tok[0] == '\0')
2073     r->copy_percent = -1;
2074   else if (sscanf (tok, "%f", &r->copy_percent) != 1) {
2075     fprintf (stderr, "%s: failed to parse float '%s' from token %s\n", __func__, tok, "copy_percent");
2076     return -1;
2077   }
2078   tok = next;
2079   if (!tok) {
2080     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "move_pv");
2081     return -1;
2082   }
2083   p = strchrnul (tok, ',');
2084   if (*p) next = p+1; else next = NULL;
2085   *p = '\0';
2086   r->move_pv = strdup (tok);
2087   if (r->move_pv == NULL) {
2088     perror ("strdup");
2089     return -1;
2090   }
2091   tok = next;
2092   if (!tok) {
2093     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "lv_tags");
2094     return -1;
2095   }
2096   p = strchrnul (tok, ',');
2097   if (*p) next = p+1; else next = NULL;
2098   *p = '\0';
2099   r->lv_tags = strdup (tok);
2100   if (r->lv_tags == NULL) {
2101     perror ("strdup");
2102     return -1;
2103   }
2104   tok = next;
2105   if (!tok) {
2106     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "mirror_log");
2107     return -1;
2108   }
2109   p = strchrnul (tok, ',');
2110   if (*p) next = p+1; else next = NULL;
2111   *p = '\0';
2112   r->mirror_log = strdup (tok);
2113   if (r->mirror_log == NULL) {
2114     perror ("strdup");
2115     return -1;
2116   }
2117   tok = next;
2118   if (!tok) {
2119     fprintf (stderr, "%s: failed: string finished early, around token %s\n", __func__, "modules");
2120     return -1;
2121   }
2122   p = strchrnul (tok, ',');
2123   if (*p) next = p+1; else next = NULL;
2124   *p = '\0';
2125   r->modules = strdup (tok);
2126   if (r->modules == NULL) {
2127     perror ("strdup");
2128     return -1;
2129   }
2130   tok = next;
2131   if (tok != NULL) {
2132     fprintf (stderr, "%s: failed: extra tokens at end of string\n", __func__);
2133     return -1;
2134   }
2135   return 0;
2136 }
2137
2138 guestfs_lvm_int_lv_list *
2139 parse_command_line_lvs (void)
2140 {
2141   char *out, *err;
2142   char *p, *pend;
2143   int r, i;
2144   guestfs_lvm_int_lv_list *ret;
2145   void *newp;
2146
2147   ret = malloc (sizeof *ret);
2148   if (!ret) {
2149     reply_with_perror ("malloc");
2150     return NULL;
2151   }
2152
2153   ret->guestfs_lvm_int_lv_list_len = 0;
2154   ret->guestfs_lvm_int_lv_list_val = NULL;
2155
2156   r = command (&out, &err,
2157                "/sbin/lvm", "lvs",
2158                "-o", lvm_lv_cols, "--unbuffered", "--noheadings",
2159                "--nosuffix", "--separator", ",", "--units", "b", NULL);
2160   if (r == -1) {
2161     reply_with_error ("%s", err);
2162     free (out);
2163     free (err);
2164     return NULL;
2165   }
2166
2167   free (err);
2168
2169   /* Tokenize each line of the output. */
2170   p = out;
2171   i = 0;
2172   while (p) {
2173     pend = strchr (p, '\n');    /* Get the next line of output. */
2174     if (pend) {
2175       *pend = '\0';
2176       pend++;
2177     }
2178
2179     while (*p && isspace (*p))  /* Skip any leading whitespace. */
2180       p++;
2181
2182     if (!*p) {                  /* Empty line?  Skip it. */
2183       p = pend;
2184       continue;
2185     }
2186
2187     /* Allocate some space to store this next entry. */
2188     newp = realloc (ret->guestfs_lvm_int_lv_list_val,
2189                     sizeof (guestfs_lvm_int_lv) * (i+1));
2190     if (newp == NULL) {
2191       reply_with_perror ("realloc");
2192       free (ret->guestfs_lvm_int_lv_list_val);
2193       free (ret);
2194       free (out);
2195       return NULL;
2196     }
2197     ret->guestfs_lvm_int_lv_list_val = newp;
2198
2199     /* Tokenize the next entry. */
2200     r = lvm_tokenize_lv (p, &ret->guestfs_lvm_int_lv_list_val[i]);
2201     if (r == -1) {
2202       reply_with_error ("failed to parse output of 'lvs' command");
2203       free (ret->guestfs_lvm_int_lv_list_val);
2204       free (ret);
2205       free (out);
2206       return NULL;
2207     }
2208
2209     ++i;
2210     p = pend;
2211   }
2212
2213   ret->guestfs_lvm_int_lv_list_len = i;
2214
2215   free (out);
2216   return ret;
2217 }