build: remove trailing blanks, enable syntax-check
[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", device, NULL);
38   else
39     r = command (NULL, &err, "/sbin/mkswap", 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   IS_DEVICE (device, -1);
56
57   return mkswap (device, NULL, NULL);
58 }
59
60 int
61 do_mkswap_L (char *label, char *device)
62 {
63   IS_DEVICE (device, -1);
64
65   return mkswap (device, "-L", label);
66 }
67
68 int
69 do_mkswap_U (char *uuid, char *device)
70 {
71   IS_DEVICE (device, -1);
72
73   return mkswap (device, "-U", uuid);
74 }
75
76 int
77 do_mkswap_file (char *path)
78 {
79   char *buf;
80   int r;
81
82   NEED_ROOT (-1);
83   ABS_PATH (path, -1);
84
85   buf = sysroot_path (path);
86   if (!buf) {
87     reply_with_perror ("malloc");
88     return -1;
89   }
90
91   r = mkswap (buf, NULL, NULL);
92   free (buf);
93   return r;
94 }
95
96 static int
97 swaponoff (const char *cmd, const char *flag, const char *value)
98 {
99   char *err;
100   int r;
101
102   if (!flag)
103     r = command (NULL, &err, cmd, value, NULL);
104   else
105     r = command (NULL, &err, cmd, flag, value, NULL);
106
107   if (r == -1) {
108     reply_with_error ("%s: %s: %s", cmd, value, err);
109     free (err);
110     return -1;
111   }
112
113   free (err);
114
115   return 0;
116 }
117
118 int
119 do_swapon_device (char *device)
120 {
121   IS_DEVICE (device, -1);
122
123   return swaponoff ("/sbin/swapon", NULL, device);
124 }
125
126 int
127 do_swapoff_device (char *device)
128 {
129   IS_DEVICE (device, -1);
130
131   return swaponoff ("/sbin/swapoff", NULL, device);
132 }
133
134 int
135 do_swapon_file (char *path)
136 {
137   char *buf;
138   int r;
139
140   NEED_ROOT (-1);
141   ABS_PATH (path, -1);
142
143   buf = sysroot_path (path);
144   if (!buf) {
145     reply_with_perror ("malloc");
146     return -1;
147   }
148
149   r = swaponoff ("/sbin/swapon", NULL, buf);
150   free (buf);
151   return r;
152 }
153
154 int
155 do_swapoff_file (char *path)
156 {
157   char *buf;
158   int r;
159
160   NEED_ROOT (-1);
161   ABS_PATH (path, -1);
162
163   buf = sysroot_path (path);
164   if (!buf) {
165     reply_with_perror ("malloc");
166     return -1;
167   }
168
169   r = swaponoff ("/sbin/swapoff", NULL, buf);
170   free (buf);
171   return r;
172 }
173
174 int
175 do_swapon_label (char *label)
176 {
177   return swaponoff ("/sbin/swapon", "-L", label);
178 }
179
180 int
181 do_swapoff_label (char *label)
182 {
183   return swaponoff ("/sbin/swapoff", "-L", label);
184 }
185
186 int
187 do_swapon_uuid (char *uuid)
188 {
189   return swaponoff ("/sbin/swapon", "-U", uuid);
190 }
191
192 int
193 do_swapoff_uuid (char *uuid)
194 {
195   return swaponoff ("/sbin/swapoff", "-U", uuid);
196 }