Fix error handling of external sfdisk command.
[libguestfs.git] / daemon / sfdisk.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 #include <unistd.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include "daemon.h"
30 #include "actions.h"
31
32 static int
33 sfdisk (char *device, int n, int cyls, int heads, int sectors,
34         char * const* const lines)
35 {
36   FILE *fp;
37   char buf[256];
38   int i;
39
40   IS_DEVICE (device, -1);
41
42   strcpy (buf, "/sbin/sfdisk");
43   if (n > 0)
44     sprintf (buf + strlen (buf), " -N %d", n);
45   if (cyls)
46     sprintf (buf + strlen (buf), " -C %d", cyls);
47   if (heads)
48     sprintf (buf + strlen (buf), " -H %d", heads);
49   if (sectors)
50     sprintf (buf + strlen (buf), " -S %d", sectors);
51   /* Safe because of IS_DEVICE above: */
52   sprintf (buf + strlen (buf), " %s", device);
53
54   if (verbose)
55     printf ("%s\n", buf);
56
57   fp = popen (buf, "w");
58   if (fp == NULL) {
59     reply_with_perror (buf);
60     return -1;
61   }
62
63   for (i = 0; lines[i] != NULL; ++i) {
64     if (fprintf (fp, "%s\n", lines[i]) < 0) {
65       reply_with_perror (buf);
66       pclose (fp);
67       return -1;
68     }
69   }
70
71   if (pclose (fp) != 0) {
72     reply_with_error ("%s: external command failed", buf);
73     return -1;
74   }
75
76   return 0;
77 }
78
79 int
80 do_sfdisk (char *device, int cyls, int heads, int sectors,
81            char **lines)
82 {
83   return sfdisk (device, 0, cyls, heads, sectors, lines);
84 }
85
86 int
87 do_sfdisk_N (char *device, int n, int cyls, int heads, int sectors,
88              char *line)
89 {
90   const char *lines[2] = { line, NULL };
91
92   return sfdisk (device, n, cyls, heads, sectors, lines);
93 }
94
95 static char *
96 sfdisk_flag (char *device, const char *flag)
97 {
98   char *out, *err;
99   int r;
100
101   IS_DEVICE (device, NULL);
102
103   r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL);
104   if (r == -1) {
105     reply_with_error ("sfdisk: %s: %s", device, err);
106     free (out);
107     free (err);
108     return NULL;
109   }
110
111   free (err);
112
113   return out;                   /* caller frees */
114 }
115
116 char *
117 do_sfdisk_l (char *device)
118 {
119   return sfdisk_flag (device, "-l");
120 }
121
122 char *
123 do_sfdisk_kernel_geometry (char *device)
124 {
125   return sfdisk_flag (device, "-g");
126 }
127
128 char *
129 do_sfdisk_disk_geometry (char *device)
130 {
131   return sfdisk_flag (device, "-G");
132 }