daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / daemon / blockdev.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27
28 #include "daemon.h"
29 #include "actions.h"
30
31 /* These functions are all about using the blockdev command, so
32  * we centralize it in one call.
33  */
34 static int64_t
35 call_blockdev (const char *device, const char *switc, int extraarg, int prints)
36 {
37   int r;
38   int64_t rv;
39   char *out, *err;
40   const char *argv[] = {
41     "blockdev",
42     switc,
43     NULL,
44     NULL,
45     NULL
46   };
47   char buf[64];
48
49   if (extraarg > 0) {
50     snprintf (buf, sizeof buf, "%d", extraarg);
51     argv[2] = buf;
52     argv[3] = device;
53   } else
54     argv[2] = device;
55
56   r = commandv (&out, &err, argv);
57
58   if (r == -1) {
59     reply_with_error ("%s: %s", argv[0], err);
60     free (err);
61     free (out);
62     return -1;
63   }
64
65   rv = 0;
66
67   if (prints) {
68     if (sscanf (out, "%" SCNi64, &rv) != 1) {
69       reply_with_error ("%s: expected output, but got nothing", argv[0]);
70       free (out);
71       free (err);
72       return -1;
73     }
74   }
75
76   free (out);
77   free (err);
78
79   return rv;
80 }
81
82 int
83 do_blockdev_setro (const char *device)
84 {
85   return (int) call_blockdev (device, "--setro", 0, 0);
86 }
87
88 int
89 do_blockdev_setrw (const char *device)
90 {
91   return (int) call_blockdev (device, "--setrw", 0, 0);
92 }
93
94 int
95 do_blockdev_getro (const char *device)
96 {
97   return (int) call_blockdev (device, "--getro", 0, 1);
98 }
99
100 int
101 do_blockdev_getss (const char *device)
102 {
103   return (int) call_blockdev (device, "--getss", 0, 1);
104 }
105
106 int
107 do_blockdev_getbsz (const char *device)
108 {
109   return (int) call_blockdev (device, "--getbsz", 0, 1);
110 }
111
112 int
113 do_blockdev_setbsz (const char *device, int blocksize)
114 {
115   if (blocksize <= 0 /* || blocksize >= what? */) {
116     reply_with_error ("blocksize must be > 0");
117     return -1;
118   }
119   return (int) call_blockdev (device, "--setbsz", blocksize, 0);
120 }
121
122 int64_t
123 do_blockdev_getsz (const char *device)
124 {
125   return call_blockdev (device, "--getsz", 0, 1);
126 }
127
128 int64_t
129 do_blockdev_getsize64 (const char *device)
130 {
131   return call_blockdev (device, "--getsize64", 0, 1);
132 }
133
134 int
135 do_blockdev_flushbufs (const char *device)
136 {
137   return call_blockdev (device, "--flushbufs", 0, 0);
138 }
139
140 int
141 do_blockdev_rereadpt (const char *device)
142 {
143   return call_blockdev (device, "--rereadpt", 0, 0);
144 }