update all NEED_ROOT uses
[libguestfs.git] / daemon / swap.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
26 #include "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 static int
31 mkswap (char *device, const char *flag, const char *value)
32 {
33   char *err;
34   int r;
35
36   if (!flag)
37     r = command (NULL, &err, "/sbin/mkswap", "-f", device, NULL);
38   else
39     r = command (NULL, &err, "/sbin/mkswap", "-f", flag, value, device, NULL);
40
41   if (r == -1) {
42     reply_with_error ("mkswap: %s", err);
43     free (err);
44     return -1;
45   }
46
47   free (err);
48
49   return 0;
50 }
51
52 int
53 do_mkswap (char *device)
54 {
55   return mkswap (device, NULL, NULL);
56 }
57
58 int
59 do_mkswap_L (char *label, char *device)
60 {
61   return mkswap (device, "-L", label);
62 }
63
64 int
65 do_mkswap_U (char *uuid, char *device)
66 {
67   return mkswap (device, "-U", uuid);
68 }
69
70 int
71 do_mkswap_file (char *path)
72 {
73   char *buf;
74   int r;
75
76   NEED_ROOT (return -1);
77   ABS_PATH (path, return -1);
78
79   buf = sysroot_path (path);
80   if (!buf) {
81     reply_with_perror ("malloc");
82     return -1;
83   }
84
85   r = mkswap (buf, NULL, NULL);
86   free (buf);
87   return r;
88 }
89
90 static int
91 swaponoff (const char *cmd, const char *flag, const char *value)
92 {
93   char *err;
94   int r;
95
96   if (!flag)
97     r = command (NULL, &err, cmd, value, NULL);
98   else
99     r = command (NULL, &err, cmd, flag, value, NULL);
100
101   if (r == -1) {
102     reply_with_error ("%s: %s: %s", cmd, value, err);
103     free (err);
104     return -1;
105   }
106
107   free (err);
108
109   return 0;
110 }
111
112 int
113 do_swapon_device (char *device)
114 {
115   return swaponoff ("/sbin/swapon", NULL, device);
116 }
117
118 int
119 do_swapoff_device (char *device)
120 {
121   return swaponoff ("/sbin/swapoff", NULL, device);
122 }
123
124 int
125 do_swapon_file (char *path)
126 {
127   char *buf;
128   int r;
129
130   NEED_ROOT (return -1);
131   ABS_PATH (path, return -1);
132
133   buf = sysroot_path (path);
134   if (!buf) {
135     reply_with_perror ("malloc");
136     return -1;
137   }
138
139   r = swaponoff ("/sbin/swapon", NULL, buf);
140   free (buf);
141   return r;
142 }
143
144 int
145 do_swapoff_file (char *path)
146 {
147   char *buf;
148   int r;
149
150   NEED_ROOT (return -1);
151   ABS_PATH (path, return -1);
152
153   buf = sysroot_path (path);
154   if (!buf) {
155     reply_with_perror ("malloc");
156     return -1;
157   }
158
159   r = swaponoff ("/sbin/swapoff", NULL, buf);
160   free (buf);
161   return r;
162 }
163
164 int
165 do_swapon_label (char *label)
166 {
167   return swaponoff ("/sbin/swapon", "-L", label);
168 }
169
170 int
171 do_swapoff_label (char *label)
172 {
173   return swaponoff ("/sbin/swapoff", "-L", label);
174 }
175
176 int
177 do_swapon_uuid (char *uuid)
178 {
179   return swaponoff ("/sbin/swapon", "-U", uuid);
180 }
181
182 int
183 do_swapoff_uuid (char *uuid)
184 {
185   return swaponoff ("/sbin/swapoff", "-U", uuid);
186 }