Add mount_ro, mount_options, mount_vfs. Fix small bug in OCaml generator.
[libguestfs.git] / daemon / command.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "../src/guestfs_protocol.h"
26 #include "daemon.h"
27 #include "actions.h"
28
29 char *
30 do_command (char * const * const argv)
31 {
32   char *out, *err;
33   int r;
34
35   /* We need a root filesystem mounted to do this. */
36   NEED_ROOT (NULL);
37
38   /* Conveniently, argv is already a NULL-terminated argv-style array
39    * of parameters, so we can pass it straight in to our internal
40    * commandv.  We just have to check the list is non-empty.
41    */
42   if (argv[0] == NULL) {
43     reply_with_error ("command: passed an empty list");
44     return NULL;
45   }
46
47   CHROOT_IN;
48   r = commandv (&out, &err, argv);
49   CHROOT_OUT;
50
51   if (r == -1) {
52     reply_with_error ("%s", err);
53     free (out);
54     free (err);
55     return NULL;
56   }
57
58   free (err);
59
60   return out;                   /* Caller frees. */
61 }
62
63 char **
64 do_command_lines (char * const * const argv)
65 {
66   char *out;
67   char **lines = NULL;
68   int size = 0, alloc = 0;
69   char *p, *pend;
70
71   out = do_command (argv);
72   if (out == NULL)
73     return NULL;
74
75   /* Now convert the output to a list of lines. */
76   p = out;
77   while (p) {
78     pend = strchr (p, '\n');
79     if (pend) {
80       *pend = '\0';
81       pend++;
82
83       /* Final \n?  Don't return an empty final element. */
84       if (*pend == '\0') break;
85     }
86
87     if (add_string (&lines, &size, &alloc, p) == -1) {
88       free (out);
89       return NULL;
90     }
91
92     p = pend;
93   }
94
95   free (out);
96
97   if (add_string (&lines, &size, &alloc, NULL) == -1)
98     return NULL;
99
100   return lines;                 /* Caller frees. */
101 }