Added blockdev_* calls. RInt64 type. Enhanced tests.
[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., 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 <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     "/sbin/blockdev",
42     switc,
43     NULL,
44     NULL,
45     NULL
46   };
47   char buf[64];
48
49   IS_DEVICE (device, -1);
50
51   if (extraarg > 0) {
52     snprintf (buf, sizeof buf, "%d", extraarg);
53     argv[2] = buf;
54     argv[3] = device;
55   } else
56     argv[2] = device;
57
58   r = commandv (&out, &err, argv);
59
60   if (r == -1) {
61     reply_with_error ("%s: %s", argv[0], err);
62     free (err);
63     free (out);
64     return -1;
65   }
66
67   rv = 0;
68
69   if (prints) {
70     if (sscanf (out, "%" SCNi64, &rv) != 1) {
71       reply_with_error ("%s: expected output, but got nothing");
72       free (out);
73       return -1;
74     }
75   }
76
77   free (out);
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 ("blockdev_setbsz: 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 }