Debian Codegraph @

src/plugins/smart/libatasmart.c

raw
1 /*
2 * Copyright (C) 2014-2024 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Tomas Bzatek <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <errno.h>
25
26 #include <atasmart.h>
27
28 #include <blockdev/utils.h>
29 #include <check_deps.h>
30
31 #include "smart.h"
32 #include "smart-private.h"
33
34 G_GNUC_INTERNAL
35 void _smart_close_plugin (void) {
36 /* nothing to do here */
37 }
38
39 /**
40 * bd_smart_check_deps:
41 *
42 * Returns: whether the plugin's runtime dependencies are satisfied or not
43 *
44 * Function checking plugin's runtime dependencies.
45 *
46 * Deprecated: 3.5: use %bd_smart_is_tech_avail instead
47 */
48 gboolean bd_smart_check_deps (void) {
49 return TRUE;
50 }
51
52 /**
53 * bd_smart_is_tech_avail:
54 * @tech: the queried tech
55 * @mode: a bit mask of queried modes of operation (#BDSmartTechMode) for @tech
56 * @error: (out) (nullable): place to store error (details about why the @tech-@mode combination is not available)
57 *
58 * Returns: whether the @tech-@mode combination is available -- supported by the
59 * plugin implementation and having all the runtime dependencies available
60 */
61 gboolean bd_smart_is_tech_avail (BDSmartTech tech, G_GNUC_UNUSED guint64 mode, GError **error) {
62 switch (tech) {
63 case BD_SMART_TECH_ATA:
64 return TRUE;
65 case BD_SMART_TECH_SCSI:
66 g_set_error_literal (error, BD_SMART_ERROR, BD_SMART_ERROR_TECH_UNAVAIL, "SCSI SMART is unavailable with libatasmart");
67 return FALSE;
68 default:
69 g_set_error_literal (error, BD_SMART_ERROR, BD_SMART_ERROR_TECH_UNAVAIL, "Unknown technology");
70 return FALSE;
71 }
72 }
73
74 /* copied from libatasmart/atasmart.c (a non-public symbol) */
75 static gchar *print_value (uint64_t pretty_value, SkSmartAttributeUnit pretty_unit) {
76 switch (pretty_unit) {
77 case SK_SMART_ATTRIBUTE_UNIT_MSECONDS:
78 if (pretty_value >= 1000LLU*60LLU*60LLU*24LLU*365LLU)
79 return g_strdup_printf ("%0.1f years", ((double) pretty_value)/(1000.0*60*60*24*365));
80 if (pretty_value >= 1000LLU*60LLU*60LLU*24LLU*30LLU)
81 return g_strdup_printf ("%0.1f months", ((double) pretty_value)/(1000.0*60*60*24*30));
82 if (pretty_value >= 1000LLU*60LLU*60LLU*24LLU)
83 return g_strdup_printf ("%0.1f days", ((double) pretty_value)/(1000.0*60*60*24));
84 if (pretty_value >= 1000LLU*60LLU*60LLU)
85 return g_strdup_printf ("%0.1f h", ((double) pretty_value)/(1000.0*60*60));
86 if (pretty_value >= 1000LLU*60LLU)
87 return g_strdup_printf ("%0.1f min", ((double) pretty_value)/(1000.0*60));
88 if (pretty_value >= 1000LLU)
89 return g_strdup_printf ("%0.1f s", ((double) pretty_value)/(1000.0));
90 else
91 return g_strdup_printf ("%llu ms", (unsigned long long) pretty_value);
92 break;
93 case SK_SMART_ATTRIBUTE_UNIT_MKELVIN:
94 return g_strdup_printf ("%0.1f C", ((double) pretty_value - 273150) / 1000);
95 break;
96 case SK_SMART_ATTRIBUTE_UNIT_SECTORS:
97 return g_strdup_printf ("%llu sectors", (unsigned long long) pretty_value);
98 break;
99 case SK_SMART_ATTRIBUTE_UNIT_PERCENT:
100 return g_strdup_printf ("%llu%%", (unsigned long long) pretty_value);
101 break;
102 case SK_SMART_ATTRIBUTE_UNIT_SMALL_PERCENT:
103 return g_strdup_printf ("%0.3f%%", (double) pretty_value);
104 break;
105 case SK_SMART_ATTRIBUTE_UNIT_MB:
106 if (pretty_value >= 1000000LLU)
107 return g_strdup_printf ("%0.3f TB", (double) pretty_value / 1000000LLU);
108 if (pretty_value >= 1000LLU)
109 return g_strdup_printf ("%0.3f GB", (double) pretty_value / 1000LLU);
110 else
111 return g_strdup_printf ("%llu MB", (unsigned long long) pretty_value);
112 break;
113 case SK_SMART_ATTRIBUTE_UNIT_NONE:
114 return g_strdup_printf ("%llu", (unsigned long long) pretty_value);
115 break;
116 case SK_SMART_ATTRIBUTE_UNIT_UNKNOWN:
117 return g_strdup_printf ("n/a");
118 break;
119 case _SK_SMART_ATTRIBUTE_UNIT_MAX:
120 g_warn_if_reached ();
121 return NULL;
122 }
123 return NULL;
124 }
125
126 struct ParseTempData {
127 guint attr_id;
128 guint64 value;
129 DriveDBAttr **drivedb_attrs;
130 };
131
132 static void parse_temp_attr_cb (G_GNUC_UNUSED SkDisk *d,
133 const SkSmartAttributeParsedData *a,
134 void *user_data) {
135 struct ParseTempData *data = user_data;
136
137 if (a->id != data->attr_id)
138 return;
139 if ((data->attr_id == 194 || data->attr_id == 190) &&
140 a->pretty_unit != SK_SMART_ATTRIBUTE_UNIT_MKELVIN)
141 return;
142 /* TODO: Validate against drivedb when we have backwards mapping
143 * from smartmontools attribute names back to libatasmart names.
144 * At this point the well_known_attrs[] table serves only
145 * for forward validation.
146 */
147 data->value = a->pretty_value;
148 }
149
150 static guint64 calculate_temperature(SkDisk *d, DriveDBAttr **drivedb_attrs) {
151 const guint temp_attrs[2] = {194, 190, /* 9, 220 */ }; /* as defined in smartmontools/atacmds.cpp:ata_return_temperature_value() */
152 unsigned long int i;
153
154 for (i = 0; i < G_N_ELEMENTS (temp_attrs); i++) {
155 struct ParseTempData parse_data = {
156 .attr_id = temp_attrs[i],
157 .drivedb_attrs = drivedb_attrs,
158 .value = 0,
159 };
160
161 if (sk_disk_smart_parse_attributes (d, parse_temp_attr_cb, &parse_data) != 0)
162 break;
163 if (parse_data.value != 0)
164 return parse_data.value;
165 }
166 return 0;
167 }
168
169 struct ParseData {
170 GPtrArray *ptr_array;
171 const SkIdentifyParsedData *identify_data;
172 DriveDBAttr **drivedb_attrs;
173 };
174
175 static void parse_attr_cb (G_GNUC_UNUSED SkDisk *d,
176 const SkSmartAttributeParsedData *a,
177 void *user_data) {
178 struct ParseData *data = user_data;
179 BDSmartATAAttribute *attr;
180
181 attr = g_new0 (BDSmartATAAttribute, 1);
182 attr->id = a->id;
183 attr->name = g_strdup (a->name);
184 attr->well_known_name = g_strdup (a->name);
185 attr->value = a->current_value_valid ? a->current_value : -1;
186 attr->worst = a->worst_value_valid ? a->worst_value : -1;
187 attr->threshold = a->threshold_valid ? a->threshold : -1;
188 attr->failed_past = attr->worst > 0 && attr->threshold > 0 && attr->worst <= attr->threshold;
189 attr->failing_now = attr->value > 0 && attr->threshold > 0 && attr->value <= attr->threshold;
190 attr->value_raw = ((uint64_t) a->raw[0]) |
191 (((uint64_t) a->raw[1]) << 8) |
192 (((uint64_t) a->raw[2]) << 16) |
193 (((uint64_t) a->raw[3]) << 24) |
194 (((uint64_t) a->raw[4]) << 32) |
195 (((uint64_t) a->raw[5]) << 40);
196 attr->flags = a->flags;
197 attr->pretty_value = a->pretty_value;
198 switch (a->pretty_unit) {
199 case SK_SMART_ATTRIBUTE_UNIT_UNKNOWN:
200 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_UNKNOWN;
201 break;
202 case SK_SMART_ATTRIBUTE_UNIT_NONE:
203 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_NONE;
204 break;
205 case SK_SMART_ATTRIBUTE_UNIT_MSECONDS:
206 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_MSECONDS;
207 break;
208 case SK_SMART_ATTRIBUTE_UNIT_SECTORS:
209 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_SECTORS;
210 break;
211 case SK_SMART_ATTRIBUTE_UNIT_MKELVIN:
212 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_MKELVIN;
213 break;
214 case SK_SMART_ATTRIBUTE_UNIT_SMALL_PERCENT:
215 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_SMALL_PERCENT;
216 break;
217 case SK_SMART_ATTRIBUTE_UNIT_PERCENT:
218 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_PERCENT;
219 break;
220 case SK_SMART_ATTRIBUTE_UNIT_MB:
221 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_MB;
222 break;
223 default:
224 attr->pretty_value_unit = BD_SMART_ATA_ATTRIBUTE_UNIT_UNKNOWN;
225 g_warn_if_reached ();
226 }
227 attr->pretty_value_string = print_value (a->pretty_value, a->pretty_unit);
228
229 /* validate against drivedb */
230 if (data->drivedb_attrs) {
231 DriveDBAttr **l;
232
233 for (l = data->drivedb_attrs; *l; l++) {
234 if ((*l)->id == a->id && well_known_attrs[a->id].libatasmart_name) {
235 const gchar * const *n;
236 gboolean match = FALSE;
237
238 for (n = well_known_attrs[a->id].smartmontools_names; *n; n++)
239 if (g_ascii_strcasecmp ((*l)->name, *n) == 0) {
240 match = TRUE;
241 break;
242 }
243
244 if (!match) {
245 g_free (attr->well_known_name);
246 attr->well_known_name = NULL;
247 bd_utils_log_format (BD_UTILS_LOG_DEBUG,
248 "libatasmart: drive %s: attribute [%d] \"%s\"/\"%s\" not whitelisted, reporting unknown attr",
249 data->identify_data->model, a->id, a->name, (*l)->name);
250 }
251 }
252 }
253 }
254
255 g_ptr_array_add (data->ptr_array, attr);
256 }
257
258 static BDSmartATA * parse_sk_data (SkDisk *d, GError **error) {
259 SkBool good = FALSE;
260 SkBool available = FALSE;
261 uint64_t power_on_msec = 0;
262 const SkSmartParsedData *parsed_data;
263 BDSmartATA *data;
264 GPtrArray *ptr_array;
265 struct ParseData parse_data = {0,};
266
267 if (sk_disk_smart_read_data (d) != 0) {
268 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
269 "Error reading SMART data from device: %s",
270 strerror_l (errno, _C_LOCALE));
271 return NULL;
272 }
273
274 if (sk_disk_smart_status (d, &good) != 0) {
275 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
276 "Error checking SMART data status: %s",
277 strerror_l (errno, _C_LOCALE));
278 return NULL;
279 }
280
281 if (sk_disk_smart_parse (d, &parsed_data) != 0) {
282 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
283 "Error parsing SMART data: %s",
284 strerror_l (errno, _C_LOCALE));
285 return NULL;
286 }
287
288 data = g_new0 (BDSmartATA, 1);
289
290 sk_disk_smart_is_available (d, &available);
291 data->smart_supported = available;
292 /* At this point when SMART is not and cannot be enabled,
293 * sk_disk_smart_read_data() would've already returned an error.
294 */
295 data->smart_enabled = TRUE;
296 data->overall_status_passed = good;
297
298 switch (parsed_data->offline_data_collection_status) {
299 case SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_NEVER:
300 data->offline_data_collection_status = BD_SMART_ATA_OFFLINE_DATA_COLLECTION_STATUS_NEVER_STARTED;
301 break;
302 case SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_SUCCESS:
303 data->offline_data_collection_status = BD_SMART_ATA_OFFLINE_DATA_COLLECTION_STATUS_NO_ERROR;
304 break;
305 case SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_INPROGRESS:
306 data->offline_data_collection_status = BD_SMART_ATA_OFFLINE_DATA_COLLECTION_STATUS_IN_PROGRESS;
307 break;
308 case SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_SUSPENDED:
309 data->offline_data_collection_status = BD_SMART_ATA_OFFLINE_DATA_COLLECTION_STATUS_SUSPENDED_INTR;
310 break;
311 case SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_ABORTED:
312 data->offline_data_collection_status = BD_SMART_ATA_OFFLINE_DATA_COLLECTION_STATUS_ABORTED_INTR;
313 break;
314 case SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_FATAL:
315 data->offline_data_collection_status = BD_SMART_ATA_OFFLINE_DATA_COLLECTION_STATUS_ABORTED_ERROR;
316 break;
317 case SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_UNKNOWN:
318 data->offline_data_collection_status = BD_SMART_ATA_OFFLINE_DATA_COLLECTION_STATUS_VENDOR_SPECIFIC;
319 break;
320 default:
321 g_warn_if_reached ();
322 }
323
324 data->auto_offline_data_collection_enabled = FALSE; /* TODO */
325 data->offline_data_collection_completion = parsed_data->total_offline_data_collection_seconds;
326 data->offline_data_collection_capabilities = 0; /* TODO */
327
328 switch (parsed_data->self_test_execution_status) {
329 case SK_SMART_SELF_TEST_EXECUTION_STATUS_SUCCESS_OR_NEVER:
330 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_COMPLETED_NO_ERROR;
331 break;
332 case SK_SMART_SELF_TEST_EXECUTION_STATUS_ABORTED:
333 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_ABORTED_HOST;
334 break;
335 case SK_SMART_SELF_TEST_EXECUTION_STATUS_INTERRUPTED:
336 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_INTR_HOST_RESET;
337 break;
338 case SK_SMART_SELF_TEST_EXECUTION_STATUS_FATAL:
339 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_ERROR_FATAL;
340 break;
341 case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_UNKNOWN:
342 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_ERROR_UNKNOWN;
343 break;
344 case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_ELECTRICAL:
345 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_ERROR_ELECTRICAL;
346 break;
347 case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_SERVO:
348 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_ERROR_SERVO;
349 break;
350 case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_READ:
351 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_ERROR_READ;
352 break;
353 case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_HANDLING:
354 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_ERROR_HANDLING;
355 break;
356 case SK_SMART_SELF_TEST_EXECUTION_STATUS_INPROGRESS:
357 data->self_test_status = BD_SMART_ATA_SELF_TEST_STATUS_IN_PROGRESS;
358 break;
359 default:
360 g_warn_if_reached ();
361 }
362
363 data->self_test_percent_remaining = parsed_data->self_test_execution_percent_remaining;
364 data->self_test_polling_short = parsed_data->short_test_polling_minutes;
365 data->self_test_polling_extended = parsed_data->extended_test_polling_minutes;
366 data->self_test_polling_conveyance = parsed_data->conveyance_test_polling_minutes;
367
368 data->smart_capabilities = 0; /* TODO */
369
370 sk_disk_smart_get_power_on (d, &power_on_msec);
371 data->power_on_time = power_on_msec / 1000 / 60;
372
373 sk_disk_smart_get_power_cycle (d, &data->power_cycle_count);
374
375 ptr_array = g_ptr_array_new_full (0, (GDestroyNotify) bd_smart_ata_attribute_free);
376 parse_data.ptr_array = ptr_array;
377 #ifdef HAVE_DRIVEDB_H
378 sk_disk_identify_parse (d, &parse_data.identify_data);
379 if (parse_data.identify_data)
380 parse_data.drivedb_attrs = drivedb_lookup_drive (parse_data.identify_data->model,
381 parse_data.identify_data->firmware,
382 FALSE /* include_defaults */);
383 #endif
384 data->temperature = calculate_temperature (d, parse_data.drivedb_attrs) / 1000;
385
386 if (sk_disk_smart_parse_attributes (d, parse_attr_cb, &parse_data) != 0) {
387 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
388 "Error parsing SMART data: %s",
389 strerror_l (errno, _C_LOCALE));
390 g_ptr_array_free (ptr_array, TRUE);
391 bd_smart_ata_free (data);
392 return NULL;
393 }
394 free_drivedb_attrs (parse_data.drivedb_attrs);
395 g_ptr_array_add (ptr_array, NULL);
396 data->attributes = (BDSmartATAAttribute **) g_ptr_array_free (ptr_array, FALSE);
397
398 return data;
399 }
400
401 /**
402 * bd_smart_ata_get_info:
403 * @device: device to check.
404 * @extra: (nullable) (array zero-terminated=1): extra options to pass through.
405 * @error: (out) (optional): place to store error (if any).
406 *
407 * Retrieve SMART information from the drive.
408 *
409 * Returns: (transfer full): ATA SMART log or %NULL in case of an error (with @error set).
410 *
411 * Tech category: %BD_SMART_TECH_ATA-%BD_SMART_TECH_MODE_INFO
412 */
413 BDSmartATA * bd_smart_ata_get_info (const gchar *device, G_GNUC_UNUSED const BDExtraArg **extra, GError **error) {
414 SkDisk *d;
415 BDSmartATA *data;
416
417 g_warn_if_fail (device != NULL);
418
419 if (sk_disk_open (device, &d) != 0) {
420 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
421 "Error opening device %s: %s",
422 device,
423 strerror_l (errno, _C_LOCALE));
424 return NULL;
425 }
426
427 data = parse_sk_data (d, error);
428 sk_disk_free (d);
429
430 return data;
431 }
432
433
434 /**
435 * bd_smart_ata_get_info_from_data:
436 * @data: (array length=data_len): binary data to parse.
437 * @data_len: length of the data supplied.
438 * @error: (out) (optional): place to store error (if any).
439 *
440 * Retrieve SMART information from the supplied data.
441 *
442 * Returns: (transfer full): ATA SMART log or %NULL in case of an error (with @error set).
443 *
444 * Tech category: %BD_SMART_TECH_ATA-%BD_SMART_TECH_MODE_INFO
445 */
446 BDSmartATA * bd_smart_ata_get_info_from_data (const guint8 *data, gsize data_len, GError **error) {
447 SkDisk *d;
448 BDSmartATA *ata_data;
449
450 g_warn_if_fail (data != NULL);
451 g_warn_if_fail (data_len > 0);
452
453 if (sk_disk_open (NULL, &d) != 0) {
454 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
455 "Error parsing blob data: %s",
456 strerror_l (errno, _C_LOCALE));
457 return NULL;
458 }
459
460 if (sk_disk_set_blob (d, data, data_len) != 0) {
461 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
462 "Error parsing blob data: %s",
463 strerror_l (errno, _C_LOCALE));
464 return NULL;
465 }
466
467 ata_data = parse_sk_data (d, error);
468 sk_disk_free (d);
469
470 return ata_data;
471 }
472
473
474 /**
475 * bd_smart_scsi_get_info:
476 * @device: device to check.
477 * @extra: (nullable) (array zero-terminated=1): extra options to pass through.
478 * @error: (out) (optional): place to store error (if any).
479 *
480 * Retrieve SMART information from SCSI or SAS-compliant drive.
481 *
482 * Returns: (transfer full): SCSI SMART log or %NULL in case of an error (with @error set).
483 *
484 * Tech category: %BD_SMART_TECH_SCSI-%BD_SMART_TECH_MODE_INFO
485 */
486 BDSmartSCSI * bd_smart_scsi_get_info (G_GNUC_UNUSED const gchar *device, G_GNUC_UNUSED const BDExtraArg **extra, GError **error) {
487 g_set_error_literal (error, BD_SMART_ERROR, BD_SMART_ERROR_TECH_UNAVAIL, "SCSI SMART is unavailable with libatasmart");
488 return FALSE;
489 }
490
491
492 /**
493 * bd_smart_set_enabled:
494 * @device: SMART-capable device.
495 * @enabled: whether to enable or disable the SMART functionality
496 * @extra: (nullable) (array zero-terminated=1): extra options to pass through.
497 * @error: (out) (optional): place to store error (if any).
498 *
499 * Enables or disables SMART functionality on device.
500 *
501 * Returns: %TRUE when the functionality was set successfully or %FALSE in case of an error (with @error set).
502 *
503 * Tech category: %BD_SMART_TECH_ATA-%BD_SMART_TECH_MODE_INFO
504 */
505 gboolean bd_smart_set_enabled (G_GNUC_UNUSED const gchar *device, G_GNUC_UNUSED gboolean enabled, G_GNUC_UNUSED const BDExtraArg **extra, GError **error) {
506 g_set_error_literal (error, BD_SMART_ERROR, BD_SMART_ERROR_TECH_UNAVAIL, "Enabling/disabling ATA SMART functionality is unavailable with libatasmart");
507 return FALSE;
508 }
509
510
511 /**
512 * bd_smart_device_self_test:
513 * @device: device to trigger the test on.
514 * @operation: #BDSmartSelfTestOp self-test operation.
515 * @extra: (nullable) (array zero-terminated=1): extra options to pass through.
516 * @error: (out) (optional): place to store error (if any).
517 *
518 * Executes or aborts device self-test.
519 *
520 * Returns: %TRUE when the self-test was triggered successfully or %FALSE in case of an error (with @error set).
521 *
522 * Tech category: %BD_SMART_TECH_ATA-%BD_SMART_TECH_MODE_SELFTEST
523 */
524 gboolean bd_smart_device_self_test (const gchar *device, BDSmartSelfTestOp operation, G_GNUC_UNUSED const BDExtraArg **extra, GError **error) {
525 SkDisk *d;
526 SkSmartSelfTest op;
527 gboolean ret;
528
529 switch (operation) {
530 case BD_SMART_SELF_TEST_OP_ABORT:
531 op = SK_SMART_SELF_TEST_ABORT;
532 break;
533 case BD_SMART_SELF_TEST_OP_SHORT:
534 op = SK_SMART_SELF_TEST_SHORT;
535 break;
536 case BD_SMART_SELF_TEST_OP_LONG:
537 case BD_SMART_SELF_TEST_OP_OFFLINE:
538 op = SK_SMART_SELF_TEST_EXTENDED;
539 break;
540 case BD_SMART_SELF_TEST_OP_CONVEYANCE:
541 op = SK_SMART_SELF_TEST_CONVEYANCE;
542 break;
543 default:
544 g_set_error_literal (error, BD_SMART_ERROR, BD_SMART_ERROR_INVALID_ARGUMENT,
545 "Invalid self-test operation.");
546 return FALSE;
547 }
548
549 if (sk_disk_open (device, &d) != 0) {
550 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
551 "Error opening device %s: %s",
552 device,
553 strerror_l (errno, _C_LOCALE));
554 return FALSE;
555 }
556
557 ret = sk_disk_smart_self_test (d, op) == 0;
558 sk_disk_free (d);
559 if (!ret) {
560 g_set_error (error, BD_SMART_ERROR, BD_SMART_ERROR_FAILED,
561 "Error triggering device self-test: %s",
562 strerror_l (errno, _C_LOCALE));
563 return FALSE;
564 }
565
566 return TRUE;
567 }