Line data Source code
1 16 : GQuark bd_swap_error_quark (void) {
2 16 : return g_quark_from_static_string ("g-bd-swap-error-quark");
3 : }
4 :
5 0 : static gboolean bd_swap_is_tech_avail_stub (BDSwapTech tech G_GNUC_UNUSED, guint64 mode G_GNUC_UNUSED, GError **error) {
6 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_is_tech_avail' called, but not implemented!");
7 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
8 : "The function 'bd_swap_is_tech_avail' called, but not implemented!");
9 0 : return FALSE;
10 : }
11 :
12 : static gboolean (*_bd_swap_is_tech_avail) (BDSwapTech tech, guint64 mode, GError **error) = bd_swap_is_tech_avail_stub;
13 :
14 : /**
15 : * bd_swap_is_tech_avail:
16 : * @tech: the queried tech
17 : * @mode: a bit mask of queried modes of operation (#BDSwapTechMode) for @tech
18 : * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
19 : *
20 : * Returns: whether the @tech-@mode combination is available -- supported by the
21 : * plugin implementation and having all the runtime dependencies available
22 : */
23 11 : gboolean bd_swap_is_tech_avail (BDSwapTech tech, guint64 mode, GError **error) {
24 11 : return _bd_swap_is_tech_avail (tech, mode, error);
25 : }
26 :
27 :
28 0 : static gboolean bd_swap_mkswap_stub (const gchar *device G_GNUC_UNUSED, const gchar *label G_GNUC_UNUSED, const gchar *uuid G_GNUC_UNUSED, const BDExtraArg **extra G_GNUC_UNUSED, GError **error) {
29 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_mkswap' called, but not implemented!");
30 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
31 : "The function 'bd_swap_mkswap' called, but not implemented!");
32 0 : return FALSE;
33 : }
34 :
35 : static gboolean (*_bd_swap_mkswap) (const gchar *device, const gchar *label, const gchar *uuid, const BDExtraArg **extra, GError **error) = bd_swap_mkswap_stub;
36 :
37 : /**
38 : * bd_swap_mkswap:
39 : * @device: a device to create swap space on
40 : * @label: (nullable): a label for the swap space device
41 : * @uuid: (nullable): UUID for the swap space device
42 : * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
43 : * passed to the 'mkswap' utility)
44 : * @error: (out) (optional): place to store error (if any)
45 : *
46 : * Returns: whether the swap space was successfully created or not
47 : *
48 : * Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_CREATE
49 : */
50 5 : gboolean bd_swap_mkswap (const gchar *device, const gchar *label, const gchar *uuid, const BDExtraArg **extra, GError **error) {
51 5 : return _bd_swap_mkswap (device, label, uuid, extra, error);
52 : }
53 :
54 :
55 0 : static gboolean bd_swap_swapon_stub (const gchar *device G_GNUC_UNUSED, gint priority G_GNUC_UNUSED, GError **error) {
56 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_swapon' called, but not implemented!");
57 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
58 : "The function 'bd_swap_swapon' called, but not implemented!");
59 0 : return FALSE;
60 : }
61 :
62 : static gboolean (*_bd_swap_swapon) (const gchar *device, gint priority, GError **error) = bd_swap_swapon_stub;
63 :
64 : /**
65 : * bd_swap_swapon:
66 : * @device: swap device to activate
67 : * @priority: priority of the activated device or -1 to use the default
68 : * @error: (out) (optional): place to store error (if any)
69 : *
70 : * Returns: whether the swap device was successfully activated or not
71 : *
72 : * Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_ACTIVATE_DEACTIVATE
73 : */
74 8 : gboolean bd_swap_swapon (const gchar *device, gint priority, GError **error) {
75 8 : return _bd_swap_swapon (device, priority, error);
76 : }
77 :
78 :
79 0 : static gboolean bd_swap_swapoff_stub (const gchar *device G_GNUC_UNUSED, GError **error) {
80 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_swapoff' called, but not implemented!");
81 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
82 : "The function 'bd_swap_swapoff' called, but not implemented!");
83 0 : return FALSE;
84 : }
85 :
86 : static gboolean (*_bd_swap_swapoff) (const gchar *device, GError **error) = bd_swap_swapoff_stub;
87 :
88 : /**
89 : * bd_swap_swapoff:
90 : * @device: swap device to deactivate
91 : * @error: (out) (optional): place to store error (if any)
92 : *
93 : * Returns: whether the swap device was successfully deactivated or not
94 : *
95 : * Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_ACTIVATE_DEACTIVATE
96 : */
97 10 : gboolean bd_swap_swapoff (const gchar *device, GError **error) {
98 10 : return _bd_swap_swapoff (device, error);
99 : }
100 :
101 :
102 0 : static gboolean bd_swap_swapstatus_stub (const gchar *device G_GNUC_UNUSED, GError **error) {
103 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_swapstatus' called, but not implemented!");
104 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
105 : "The function 'bd_swap_swapstatus' called, but not implemented!");
106 0 : return FALSE;
107 : }
108 :
109 : static gboolean (*_bd_swap_swapstatus) (const gchar *device, GError **error) = bd_swap_swapstatus_stub;
110 :
111 : /**
112 : * bd_swap_swapstatus:
113 : * @device: swap device to get status of
114 : * @error: (out) (optional): place to store error (if any)
115 : *
116 : * Returns: %TRUE if the swap device is active, %FALSE if not active or failed
117 : * to determine (@error) is set not a non-NULL value in such case)
118 : *
119 : * Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_QUERY
120 : */
121 7 : gboolean bd_swap_swapstatus (const gchar *device, GError **error) {
122 7 : return _bd_swap_swapstatus (device, error);
123 : }
124 :
125 :
126 0 : static gboolean bd_swap_check_label_stub (const gchar *label G_GNUC_UNUSED, GError **error) {
127 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_check_label' called, but not implemented!");
128 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
129 : "The function 'bd_swap_check_label' called, but not implemented!");
130 0 : return FALSE;
131 : }
132 :
133 : static gboolean (*_bd_swap_check_label) (const gchar *label, GError **error) = bd_swap_check_label_stub;
134 :
135 : /**
136 : * bd_swap_check_label:
137 : * @label: label to check
138 : * @error: (out) (optional): place to store error
139 : *
140 : * Returns: whether @label is a valid label for swap or not
141 : * (reason is provided in @error)
142 : *
143 : * Tech category: always available
144 : */
145 1 : gboolean bd_swap_check_label (const gchar *label, GError **error) {
146 1 : return _bd_swap_check_label (label, error);
147 : }
148 :
149 :
150 0 : static gboolean bd_swap_set_label_stub (const gchar *device G_GNUC_UNUSED, const gchar *label G_GNUC_UNUSED, GError **error) {
151 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_set_label' called, but not implemented!");
152 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
153 : "The function 'bd_swap_set_label' called, but not implemented!");
154 0 : return FALSE;
155 : }
156 :
157 : static gboolean (*_bd_swap_set_label) (const gchar *device, const gchar *label, GError **error) = bd_swap_set_label_stub;
158 :
159 : /**
160 : * bd_swap_set_label:
161 : * @device: a device to set label on
162 : * @label: label that will be set
163 : * @error: (out) (optional): place to store error (if any)
164 : *
165 : * Returns: whether the label was successfully set or not
166 : *
167 : * Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_SET_LABEL
168 : */
169 1 : gboolean bd_swap_set_label (const gchar *device, const gchar *label, GError **error) {
170 1 : return _bd_swap_set_label (device, label, error);
171 : }
172 :
173 :
174 0 : static gboolean bd_swap_check_uuid_stub (const gchar *uuid G_GNUC_UNUSED, GError **error) {
175 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_check_uuid' called, but not implemented!");
176 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
177 : "The function 'bd_swap_check_uuid' called, but not implemented!");
178 0 : return FALSE;
179 : }
180 :
181 : static gboolean (*_bd_swap_check_uuid) (const gchar *uuid, GError **error) = bd_swap_check_uuid_stub;
182 :
183 : /**
184 : * bd_swap_check_uuid:
185 : * @uuid: UUID to check
186 : * @error: (out) (optional): place to store error
187 : *
188 : * Returns: whether @uuid is a valid UUID for swap or not
189 : * (reason is provided in @error)
190 : *
191 : * Tech category: always available
192 : */
193 1 : gboolean bd_swap_check_uuid (const gchar *uuid, GError **error) {
194 1 : return _bd_swap_check_uuid (uuid, error);
195 : }
196 :
197 :
198 0 : static gboolean bd_swap_set_uuid_stub (const gchar *device G_GNUC_UNUSED, const gchar *uuid G_GNUC_UNUSED, GError **error) {
199 0 : bd_utils_log_format (BD_UTILS_LOG_CRIT, "The function 'bd_swap_set_uuid' called, but not implemented!");
200 0 : g_set_error (error, BD_INIT_ERROR, BD_INIT_ERROR_NOT_IMPLEMENTED,
201 : "The function 'bd_swap_set_uuid' called, but not implemented!");
202 0 : return FALSE;
203 : }
204 :
205 : static gboolean (*_bd_swap_set_uuid) (const gchar *device, const gchar *uuid, GError **error) = bd_swap_set_uuid_stub;
206 :
207 : /**
208 : * bd_swap_set_uuid:
209 : * @device: a device to set UUID on
210 : * @uuid: UUID that will be set
211 : * @error: (out) (optional): place to store error (if any)
212 : *
213 : * Returns: whether the UUID was successfully set or not
214 : *
215 : * Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_SET_UUID
216 : */
217 1 : gboolean bd_swap_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
218 1 : return _bd_swap_set_uuid (device, uuid, error);
219 : }
220 :
221 :
222 21 : static gpointer load_swap_from_plugin(const gchar *so_name) {
223 21 : void *handle = NULL;
224 21 : char *error = NULL;
225 21 : gboolean (*init_fn) (void) = NULL;
226 :
227 21 : handle = dlopen(so_name, RTLD_LAZY);
228 21 : if (!handle) {
229 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load module swap: %s", dlerror());
230 0 : return NULL;
231 : }
232 :
233 21 : dlerror();
234 21 : * (void**) (&init_fn) = dlsym(handle, "bd_swap_init");
235 21 : if ((error = dlerror()) != NULL)
236 2 : bd_utils_log_format (BD_UTILS_LOG_DEBUG, "failed to load the init() function for swap: %s", error);
237 : /* coverity[dead_error_condition] */
238 21 : if (init_fn && !init_fn()) {
239 0 : dlclose(handle);
240 0 : return NULL;
241 : }
242 21 : init_fn = NULL;
243 :
244 21 : dlerror();
245 21 : * (void**) (&_bd_swap_is_tech_avail) = dlsym(handle, "bd_swap_is_tech_avail");
246 21 : if ((error = dlerror()) != NULL)
247 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_is_tech_avail: %s", error);
248 :
249 21 : dlerror();
250 21 : * (void**) (&_bd_swap_mkswap) = dlsym(handle, "bd_swap_mkswap");
251 21 : if ((error = dlerror()) != NULL)
252 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_mkswap: %s", error);
253 :
254 21 : dlerror();
255 21 : * (void**) (&_bd_swap_swapon) = dlsym(handle, "bd_swap_swapon");
256 21 : if ((error = dlerror()) != NULL)
257 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_swapon: %s", error);
258 :
259 21 : dlerror();
260 21 : * (void**) (&_bd_swap_swapoff) = dlsym(handle, "bd_swap_swapoff");
261 21 : if ((error = dlerror()) != NULL)
262 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_swapoff: %s", error);
263 :
264 21 : dlerror();
265 21 : * (void**) (&_bd_swap_swapstatus) = dlsym(handle, "bd_swap_swapstatus");
266 21 : if ((error = dlerror()) != NULL)
267 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_swapstatus: %s", error);
268 :
269 21 : dlerror();
270 21 : * (void**) (&_bd_swap_check_label) = dlsym(handle, "bd_swap_check_label");
271 21 : if ((error = dlerror()) != NULL)
272 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_check_label: %s", error);
273 :
274 21 : dlerror();
275 21 : * (void**) (&_bd_swap_set_label) = dlsym(handle, "bd_swap_set_label");
276 21 : if ((error = dlerror()) != NULL)
277 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_set_label: %s", error);
278 :
279 21 : dlerror();
280 21 : * (void**) (&_bd_swap_check_uuid) = dlsym(handle, "bd_swap_check_uuid");
281 21 : if ((error = dlerror()) != NULL)
282 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_check_uuid: %s", error);
283 :
284 21 : dlerror();
285 21 : * (void**) (&_bd_swap_set_uuid) = dlsym(handle, "bd_swap_set_uuid");
286 21 : if ((error = dlerror()) != NULL)
287 0 : bd_utils_log_format (BD_UTILS_LOG_WARNING, "failed to load bd_swap_set_uuid: %s", error);
288 :
289 21 : return handle;
290 : }
291 :
292 21 : static gboolean unload_swap (gpointer handle) {
293 21 : char *error = NULL;
294 21 : gboolean (*close_fn) (void) = NULL;
295 :
296 21 : _bd_swap_is_tech_avail = bd_swap_is_tech_avail_stub;
297 21 : _bd_swap_mkswap = bd_swap_mkswap_stub;
298 21 : _bd_swap_swapon = bd_swap_swapon_stub;
299 21 : _bd_swap_swapoff = bd_swap_swapoff_stub;
300 21 : _bd_swap_swapstatus = bd_swap_swapstatus_stub;
301 21 : _bd_swap_check_label = bd_swap_check_label_stub;
302 21 : _bd_swap_set_label = bd_swap_set_label_stub;
303 21 : _bd_swap_check_uuid = bd_swap_check_uuid_stub;
304 21 : _bd_swap_set_uuid = bd_swap_set_uuid_stub;
305 :
306 21 : dlerror();
307 21 : * (void**) (&close_fn) = dlsym(handle, "bd_swap_close");
308 21 : if (((error = dlerror()) != NULL) || !close_fn)
309 2 : bd_utils_log_format (BD_UTILS_LOG_DEBUG, "failed to load the close_plugin() function for swap: %s", error);
310 : /* coverity[dead_error_condition] */
311 21 : if (close_fn) {
312 19 : close_fn();
313 : }
314 :
315 21 : return dlclose(handle) == 0;
316 : }
317 :
|