Add support for custom smartctl info options using a [info: ...] tag

This commit is contained in:
Andrea Mazzoleni 2026-06-15 14:41:03 +02:00
parent 7c9473027c
commit b9d181d9cb
56 changed files with 631 additions and 221 deletions

View file

@ -289,7 +289,7 @@ extern int exit_sync_needed;
/**
* SMART max attribute length.
*/
#define SMART_MAX 64
#define SMART_MAX 128
/**
* Value for unassigned SMART attribute.
@ -330,7 +330,8 @@ struct devinfo_struct {
uint64_t device; /**< Device ID. */
char name[PATH_MAX]; /**< Name of the disk combined with the split index if any */
char mount[PATH_MAX]; /**< Mount point or other contained directory. */
char smartctl[PATH_MAX]; /**< Options for smartctl. */
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Attribues to ignore */
char file[PATH_MAX]; /**< File device. */
#ifdef _WIN32

View file

@ -1051,6 +1051,7 @@ int state_device(struct snapraid_state* state, int operation, tommy_list* filter
device_name_set(entry, disk->name, 0);
pathcpy(entry->mount, sizeof(entry->mount), disk->dir);
pathcpy(entry->smartctl, sizeof(entry->smartctl), disk->smartctl);
pathcpy(entry->smartctl_info, sizeof(entry->smartctl_info), disk->smartctl_info);
memcpy(entry->smartignore, disk->smartignore, sizeof(entry->smartignore));
tommy_list_insert_tail(&high, &entry->node, entry);
@ -1078,6 +1079,7 @@ int state_device(struct snapraid_state* state, int operation, tommy_list* filter
device_name_set(entry, lev_config_name(j), s);
pathcpy(entry->mount, sizeof(entry->mount), state->parity[j].split_map[s].path);
pathcpy(entry->smartctl, sizeof(entry->smartctl), state->parity[j].smartctl);
pathcpy(entry->smartctl_info, sizeof(entry->smartctl_info), state->parity[j].smartctl_info);
memcpy(entry->smartignore, state->parity[j].smartignore, sizeof(entry->smartignore));
pathcut(entry->mount); /* remove the parity file */
@ -1109,7 +1111,8 @@ int state_device(struct snapraid_state* state, int operation, tommy_list* filter
device_name_set(entry, extra->name, 0);
pathcpy(entry->mount, sizeof(entry->mount), extra->dir);
pathcpy(entry->smartctl, sizeof(entry->smartctl), extra->smartctl);
memcpy(entry->smartignore, extra->smartignore, sizeof(extra->smartignore));
pathcpy(entry->smartctl_info, sizeof(entry->smartctl_info), extra->smartctl_info);
memcpy(entry->smartignore, extra->smartignore, sizeof(entry->smartignore));
tommy_list_insert_tail(&high, &entry->node, entry);
}

View file

@ -843,6 +843,7 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev
#endif
disk->smartctl[0] = 0;
disk->smartctl_info[0] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i)
disk->smartignore[i] = 0;
disk->fstype[0] = 0;
@ -915,6 +916,7 @@ struct snapraid_extra* extra_alloc(const char* name, const char* dir, uint64_t d
pathslash(extra->dir, sizeof(extra->dir));
extra->smartctl[0] = 0;
extra->smartctl_info[0] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i)
extra->smartignore[i] = 0;
extra->fstype[0] = 0;

View file

@ -343,7 +343,8 @@ struct snapraid_extent {
struct snapraid_extra {
char name[PATH_MAX]; /**< Name of the disk. */
char dir[PATH_MAX]; /**< Mount point of the disk. It always terminates with /. */
char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
char uuid[UUID_MAX]; /**< UUID of the disk. They are probed during the config reading. */
char fstype[FSINFO_MAX]; /**< Filesystem type */
@ -381,7 +382,8 @@ struct snapraid_disk {
*/
struct fssnapshot_struct fss;
char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
char uuid[UUID_MAX]; /**< UUID of the disk. They are probed during the config reading. */
char fstype[FSINFO_MAX]; /**< Filesystem type */
@ -534,7 +536,8 @@ struct snapraid_split {
struct snapraid_parity {
struct snapraid_split split_map[SPLIT_MAX]; /**< Parity splits. */
unsigned split_mac; /**< Number of parity splits. */
char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
block_off_t total_blocks; /**< Number of total blocks. */
block_off_t free_blocks; /**< Number of free blocks at the last sync. */

View file

@ -241,10 +241,10 @@ static int devstat(uint64_t device, const char* name, const char* wfile, uint64_
/**
* Get SMART attributes.
*/
static int devsmart(uint64_t device, const char* name, const char* smartctl, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* inter)
static int devsmart(uint64_t device, const char* name, const char* smartctl, const char* smartctl_info, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* inter)
{
char conv_buf[CONV_MAX];
WCHAR cmd[PATH_MAX + 128];
WCHAR cmd[PATH_MAX + SMART_MAX];
char file[128];
FILE* f;
int ret;
@ -252,13 +252,15 @@ static int devsmart(uint64_t device, const char* name, const char* smartctl, str
snprintf(file, sizeof(file), "/dev/pd%" PRIu64, device);
const char* info_opts = smartctl_info[0] ? smartctl_info : "-a";
/* if there is a custom smartctl command */
if (smartctl[0]) {
char option[128];
char option[SMART_MAX];
snprintf(option, sizeof(option), smartctl, file);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -a %s", windows_exedir(), option);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" %s %s", windows_exedir(), info_opts, option);
} else {
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -a %s", windows_exedir(), file);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" %s %s", windows_exedir(), info_opts, file);
}
count = 0;
@ -314,7 +316,7 @@ retry:
&& info[INFO_ROTATION_RATE] == SMART_UNASSIGNED
) {
/* retry using the "sat" type */
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -a -d sat %s", windows_exedir(), file);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" %s -d sat %s", windows_exedir(), info_opts, file);
++count;
goto retry;
@ -563,10 +565,10 @@ static int devpower(uint64_t device, const char* name, const char* wfile)
/**
* Get POWER state
*/
static int devprobe(uint64_t device, const char* name, const char* smartctl, int* power, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* interf)
static int devprobe(uint64_t device, const char* name, const char* smartctl, const char* smartctl_info, int* power, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* interf)
{
char conv_buf[CONV_MAX];
WCHAR cmd[PATH_MAX + 128];
WCHAR cmd[PATH_MAX + SMART_MAX];
char file[128];
FILE* f;
int ret;
@ -574,13 +576,15 @@ static int devprobe(uint64_t device, const char* name, const char* smartctl, int
snprintf(file, sizeof(file), "/dev/pd%" PRIu64, device);
const char* info_opts = smartctl_info[0] ? smartctl_info : "-a";
/* if there is a custom smartctl command */
if (smartctl[0]) {
char option[128];
char option[SMART_MAX];
snprintf(option, sizeof(option), smartctl, file);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -n standby,3 -a %s", windows_exedir(), option);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -n standby,3 %s %s", windows_exedir(), info_opts, option);
} else {
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -n standby,3 -a %s", windows_exedir(), file);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -n standby,3 %s %s", windows_exedir(), info_opts, file);
}
count = 0;
@ -629,7 +633,7 @@ retry:
*/
if (ret == 2) {
/* retry using the "sat" type */
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -n standby,3 -a -d sat %s", windows_exedir(), file);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -n standby,3 %s -d sat %s", windows_exedir(), info_opts, file);
++count;
goto retry;
@ -657,7 +661,7 @@ retry:
static int devdown(uint64_t device, const char* name, const char* smartctl)
{
char conv_buf[CONV_MAX];
WCHAR cmd[PATH_MAX + 128];
WCHAR cmd[PATH_MAX + SMART_MAX];
char file[128];
FILE* f;
int ret;
@ -667,7 +671,7 @@ static int devdown(uint64_t device, const char* name, const char* smartctl)
/* if there is a custom smartctl command */
if (smartctl[0]) {
char option[128];
char option[SMART_MAX];
snprintf(option, sizeof(option), smartctl, file);
snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -s standby,now %s", windows_exedir(), option);
} else {
@ -743,11 +747,11 @@ retry:
/**
* Spin down a specific device if it's up
*/
static int devdownifup(uint64_t device, const char* name, const char* smartctl, int* power)
static int devdownifup(uint64_t device, const char* name, const char* smartctl, const char* smartctl_info, int* power)
{
*power = POWER_UNKNOWN;
if (devprobe(device, name, smartctl, power, 0, 0, 0, 0, 0, 0) != 0)
if (devprobe(device, name, smartctl, smartctl_info, power, 0, 0, 0, 0, 0, 0) != 0)
return -1;
if (*power == POWER_ACTIVE)
@ -847,7 +851,7 @@ static void* thread_spinup(void* arg)
msg_status("Spunup device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
/* after the spin up, get SMART info */
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -900,7 +904,7 @@ static void* thread_spindownifup(void* arg)
start = os_tick_ms();
if (devdownifup(devinfo->device, devinfo->name, devinfo->smartctl, &power) != 0) {
if (devdownifup(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, &power) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -919,7 +923,7 @@ static void* thread_smart(void* arg)
{
devinfo_t* devinfo = arg;
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -943,7 +947,7 @@ static void* thread_probe(void* arg)
{
devinfo_t* devinfo = arg;
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, &devinfo->power, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, &devinfo->power, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */

View file

@ -87,12 +87,10 @@ typedef struct OS_FILE {
*
* @param argv A NULL-terminated array of strings representing the argument vector.
* argv[0] must contain the absolute path to the verified executable.
* @param extra_args A mutable string containing additional arguments separated by
* spaces, or NULL if no extra arguments are needed.
* @return A pointer to an initialized OS_FILE structure on success, or NULL on failure.
* On failure, errno is set appropriately.
*/
OS_FILE* os_popen(const char** argv, char* extra_args);
OS_FILE* os_popen(const char** argv);
/**
* Reads a line from an OS execution stream.

View file

@ -841,6 +841,97 @@ static void test_wnmatch(void)
}
}
static void test_parse_smartctl(void)
{
char smartctl[SMART_MAX];
char smartctl_info[SMART_MAX];
/* empty default case */
if (parse_smartctl("smartctl d1 %s", smartctl, sizeof(smartctl), smartctl_info, sizeof(smartctl_info)) != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl empty test 1\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (strcmp(smartctl, "smartctl d1 %s") != 0 || smartctl_info[0] != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl empty test 2\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (parse_smartctl("smartctl d1 /dev/sda", smartctl, sizeof(smartctl), smartctl_info, sizeof(smartctl_info)) != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl empty test 3\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (strcmp(smartctl, "smartctl d1 /dev/sda") != 0 || smartctl_info[0] != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl empty test 4\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* basic default case */
if (parse_smartctl("smartctl d1 -d sat %s", smartctl, sizeof(smartctl), smartctl_info, sizeof(smartctl_info)) != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl basic test 1\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (strcmp(smartctl, "smartctl d1 -d sat %s") != 0 || smartctl_info[0] != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl basic test 2\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* case with [info: xxx] tag */
if (parse_smartctl("smartctl d1 [info: -H -i -A] -d sat %s", smartctl, sizeof(smartctl), smartctl_info, sizeof(smartctl_info)) != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl info test 1\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (strcmp(smartctl, "smartctl d1 -d sat %s") != 0 || strcmp(smartctl_info, "-H -i -A") != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl info test 2\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* case with spaces in tag */
if (parse_smartctl("smartctl d2 [info: -H -I ] -d sat", smartctl, sizeof(smartctl), smartctl_info, sizeof(smartctl_info)) != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl space test 1\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (strcmp(smartctl, "smartctl d2 -d sat") != 0 || strcmp(smartctl_info, "-H -I") != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl space test 2\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* invalid case: multiple info tags */
if (parse_smartctl("smartctl [info: -H] [info: -i] %s", smartctl, sizeof(smartctl), smartctl_info, sizeof(smartctl_info)) == 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl multiple tags test\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* invalid case: missing closing bracket */
if (parse_smartctl("smartctl [info: -H %s", smartctl, sizeof(smartctl), smartctl_info, sizeof(smartctl_info)) == 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed parse_smartctl missing bracket test\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
}
void selftest(void)
{
log_tag("selftest:\n");
@ -866,6 +957,7 @@ void selftest(void)
test_crc32c();
test_tommy();
test_wnmatch();
test_parse_smartctl();
if (raid_selftest() != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed SELF test\n");

View file

@ -145,6 +145,7 @@ void state_init(struct snapraid_state* state)
state->parity[l].split_map[s].device = 0;
}
state->parity[l].smartctl[0] = 0;
state->parity[l].smartctl_info[0] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i)
state->parity[l].smartignore[i] = 0;
state->parity[l].total_blocks = 0;
@ -517,15 +518,75 @@ static void state_config_check(struct snapraid_state* state, const char* path, t
}
/**
* Validate the smartctl command.
* Parse the smartctl command.
*
* It must contains only one %s string, and not other % chars.
* It extracts the optional [info: xxx] tag, validates, and returns
* the cleaned smartctl command and smartctl_info options.
*/
static int validate_smartctl(const char* custom)
int parse_smartctl(const char* custom, char* smartctl, size_t smartctl_size, char* smartctl_info, size_t smartctl_info_size)
{
const char* s = custom;
const char* s;
const char* info_start = 0;
size_t info_len = 0;
size_t smartctl_len = 0;
int arg = 0;
/* initialize output buffers */
smartctl[0] = 0;
smartctl_info[0] = 0;
/* look for "[info:" */
s = custom;
while (*s) {
if (strncmp(s, "[info:", 6) == 0) {
if (info_start) {
/* multiple info tags not allowed */
return -1;
}
info_start = s;
s += 6;
/* skip spaces inside bracket */
while (isspace((unsigned char)*s))
++s;
/* find matching ']' */
const char* val_start = s;
while (*s && *s != ']')
++s;
if (*s != ']') {
/* missing closing bracket */
return -1;
}
/* trim trailing spaces inside bracket */
const char* val_end = s;
while (val_end > val_start && isspace((unsigned char)val_end[-1]))
--val_end;
info_len = val_end - val_start;
if (info_len >= smartctl_info_size) {
/* info string too long */
return -1;
}
memcpy(smartctl_info, val_start, info_len);
smartctl_info[info_len] = 0;
++s; /* skip ']' */
} else {
if (smartctl_len + 1 >= smartctl_size) {
return -1;
}
smartctl[smartctl_len++] = *s++;
}
}
smartctl[smartctl_len] = 0;
/* trim extra spaces from the smartctl options */
strtrim(smartctl);
/* validate the cleaned smartctl command */
s = smartctl;
while (*s) {
if (s[0] == '%' && s[1] == 's') {
if (arg) {
@ -534,12 +595,23 @@ static int validate_smartctl(const char* custom)
/* LCOV_EXCL_STOP */
}
arg = 1;
++s;
} else if (s[0] == '%') {
/* LCOV_EXCL_START */
return -1;
/* LCOV_EXCL_STOP */
}
++s;
}
/* validate the smartctl_info command */
s = smartctl_info;
while (*s) {
if (s[0] == '%') {
/* LCOV_EXCL_START */
return -1;
/* LCOV_EXCL_STOP */
}
++s;
}
@ -1094,6 +1166,8 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
tommy_list_insert_tail(&state->extralist, &extra->node, extra);
} else if (strcmp(tag, "smartctl") == 0) {
char custom[PATH_MAX];
char parsed_smartctl[SMART_MAX];
char parsed_smartctl_info[SMART_MAX];
ret = sgettok(f, buffer, sizeof(buffer));
if (ret < 0) {
@ -1127,7 +1201,7 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
/* LCOV_EXCL_STOP */
}
if (validate_smartctl(custom) != 0) {
if (parse_smartctl(custom, parsed_smartctl, sizeof(parsed_smartctl), parsed_smartctl_info, sizeof(parsed_smartctl_info)) != 0) {
/* LCOV_EXCL_START */
log_fatal(EUSER, "Invalid 'smartctl' option in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
@ -1143,7 +1217,8 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
/* LCOV_EXCL_STOP */
}
pathcpy(state->parity[level].smartctl, sizeof(state->parity[level].smartctl), custom);
pathcpy(state->parity[level].smartctl, sizeof(state->parity[level].smartctl), parsed_smartctl);
pathcpy(state->parity[level].smartctl_info, sizeof(state->parity[level].smartctl_info), parsed_smartctl_info);
} else {
/* search the disk */
struct snapraid_disk* found_disk = 0;
@ -1170,7 +1245,8 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
/* LCOV_EXCL_STOP */
}
pathcpy(found_disk->smartctl, sizeof(found_disk->smartctl), custom);
pathcpy(found_disk->smartctl, sizeof(found_disk->smartctl), parsed_smartctl);
pathcpy(found_disk->smartctl_info, sizeof(found_disk->smartctl_info), parsed_smartctl_info);
} else if (found_extra) {
if (found_extra->smartctl[0] != 0) {
/* LCOV_EXCL_START */
@ -1179,7 +1255,8 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
/* LCOV_EXCL_STOP */
}
pathcpy(found_extra->smartctl, sizeof(found_extra->smartctl), custom);
pathcpy(found_extra->smartctl, sizeof(found_extra->smartctl), parsed_smartctl);
pathcpy(found_extra->smartctl_info, sizeof(found_extra->smartctl_info), parsed_smartctl_info);
} else {
/* LCOV_EXCL_START */
log_fatal(EUSER, "Unknown 'smartctl' name '%s' at line %u\n", buffer, line);

View file

@ -460,5 +460,10 @@ void state_snapshot_cleanup(struct snapraid_state* state);
*/
void generate_configuration(const char* content);
/**
* Parse smartctl command.
*/
int parse_smartctl(const char* custom, char* smartctl, size_t smartctl_size, char* smartctl_info, size_t smartctl_info_size);
#endif

View file

@ -598,30 +598,10 @@ int os_spawn_and_wait(const char** argv)
return -1;
}
#define ARGV_MAX 64
OS_FILE* os_popen(const char** argv, char* extra_args)
OS_FILE* os_popen(const char** argv)
{
const char* combined_argv[ARGV_MAX];
unsigned argc = 0;
while (argv[argc] && argc < ARGV_MAX) {
combined_argv[argc] = argv[argc];
++argc;
}
if (extra_args)
argc += argsplit(combined_argv + argc, ARGV_MAX - argc, extra_args);
if (argc >= ARGV_MAX) {
errno = E2BIG;
return NULL;
}
combined_argv[argc] = 0;
int stdout_fd = -1;
pid_t pid = os_spawn_stdout(combined_argv, &stdout_fd);
pid_t pid = os_spawn_stdout(argv, &stdout_fd);
if (pid < 0) {
return 0;
}

View file

@ -839,7 +839,7 @@ static int extract_zfs(const char* dir, char* dataset, size_t dataset_size, char
0
};
OS_FILE* fp = os_popen(argv, 0);
OS_FILE* fp = os_popen(argv);
if (!fp) {
/* LCOV_EXCL_START */
return -1;
@ -922,7 +922,7 @@ static int devdereference_zfs(uint64_t device, const char* dir, tommy_list* devl
0
};
OS_FILE* fp = os_popen(argv, 0);
OS_FILE* fp = os_popen(argv);
if (!fp) {
/* LCOV_EXCL_START */
return -1;
@ -2769,6 +2769,7 @@ static int devtree(devinfo_t* parent, dev_t device, tommy_list* list)
devinfo->device = device;
pathcpy(devinfo->name, sizeof(devinfo->name), parent->name);
pathcpy(devinfo->smartctl, sizeof(devinfo->smartctl), parent->smartctl);
pathcpy(devinfo->smartctl_info, sizeof(devinfo->smartctl_info), parent->smartctl_info);
memcpy(devinfo->smartignore, parent->smartignore, sizeof(devinfo->smartignore));
pathcpy(devinfo->file, sizeof(devinfo->file), path);
devinfo->parent = parent;
@ -2857,17 +2858,22 @@ static int devstat(dev_t device, uint64_t* count)
}
#endif
/**
* Max number of arguments supported.
*/
#define ARGS_MAX 64
/**
* Get SMART attributes.
*/
#if HAVE_LINUX_DEVICE
static int devsmart(dev_t device, const char* name, const char* smartctl, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* interface)
static int devsmart(dev_t device, const char* name, const char* smartctl, const char* smartctl_info, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* interface)
{
char extra_args[PATH_MAX];
char file[PATH_MAX];
OS_FILE* f;
int ret;
const char* x;
const char* info_opts = smartctl_info[0] ? smartctl_info : "-a";
x = find_smartctl();
if (!x) {
@ -2884,32 +2890,33 @@ static int devsmart(dev_t device, const char* name, const char* smartctl, struct
/* LCOV_EXCL_STOP */
}
/* if there is a custom smartctl command */
const char* args = "-a";
const char* argv[ARGS_MAX];
unsigned argc = 0;
argv[argc++] = x;
char info_buf[SMART_MAX];
pathcpy(info_buf, sizeof(info_buf), info_opts);
argc += argsplit(argv + argc, ARGS_MAX - argc - 1, info_buf);
char extra_args[PATH_MAX];
if (smartctl[0]) {
pathprint(extra_args, sizeof(extra_args), smartctl, file);
const char* argv[] = {
x,
"-a",
0
};
log_tag("smartctl:%s:%s:run: %s %s %s\n", file, name, x, args, extra_args);
f = os_popen(argv, extra_args);
argc += argsplit(argv + argc, ARGS_MAX - argc - 1, extra_args);
log_tag("smartctl:%s:%s:run: %s [info: %s] %s\n", file, name, x, info_opts, extra_args);
} else {
pathprint(extra_args, sizeof(extra_args), "%s", file);
const char* argv[] = {
x,
"-a",
file,
0
};
log_tag("smartctl:%s:%s:run: %s %s %s\n", file, name, x, args, extra_args);
f = os_popen(argv, 0);
argv[argc++] = file;
log_tag("smartctl:%s:%s:run: %s [info: %s] %s\n", file, name, x, info_opts, file);
}
argv[argc++] = 0;
f = os_popen(argv);
if (!f) {
/* LCOV_EXCL_START */
log_tag("device:%s:%s:spawn\n", file, name);
log_fatal(EEXTERNAL, "Failed to run '%s %s %s'.\n", x, args, extra_args);
if (smartctl[0])
log_fatal(EEXTERNAL, "Failed to run '%s %s %s'.\n", x, info_opts, extra_args);
else
log_fatal(EEXTERNAL, "Failed to run '%s %s %s'.\n", x, info_opts, file);
return -1;
/* LCOV_EXCL_STOP */
}
@ -2929,7 +2936,10 @@ static int devsmart(dev_t device, const char* name, const char* smartctl, struct
if (!WIFEXITED(ret)) {
/* LCOV_EXCL_START */
log_tag("device:%s:%s:abort\n", file, name);
log_fatal(EEXTERNAL, "Failed to run '%s %s %s' (not exited).\n", x, args, extra_args);
if (smartctl[0])
log_fatal(EEXTERNAL, "Failed to run '%s %s %s' (not exited).\n", x, info_opts, extra_args);
else
log_fatal(EEXTERNAL, "Failed to run '%s %s %s' (not exited).\n", x, info_opts, file);
return -1;
/* LCOV_EXCL_STOP */
}
@ -3014,13 +3024,13 @@ static void devattr(dev_t device, uint64_t* info, char* serial, char* family, ch
* Get POWER state.
*/
#if HAVE_LINUX_DEVICE
static int devprobe(dev_t device, const char* name, const char* smartctl, int* power, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* interface)
static int devprobe(dev_t device, const char* name, const char* smartctl, const char* smartctl_info, int* power, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* interface)
{
char extra_args[PATH_MAX];
char file[PATH_MAX];
OS_FILE* f;
int ret;
const char* x;
const char* info_opts = smartctl_info[0] ? smartctl_info : "-a";
x = find_smartctl();
if (!x) {
@ -3037,36 +3047,35 @@ static int devprobe(dev_t device, const char* name, const char* smartctl, int* p
/* LCOV_EXCL_STOP */
}
/* if there is a custom smartctl command */
const char* args = "-n standby,3 -a";
const char* argv[ARGS_MAX];
unsigned argc = 0;
argv[argc++] = x;
argv[argc++] = "-n";
argv[argc++] = "standby,3";
char info_buf[SMART_MAX];
pathcpy(info_buf, sizeof(info_buf), info_opts);
argc += argsplit(argv + argc, ARGS_MAX - argc - 1, info_buf);
char extra_args[PATH_MAX];
if (smartctl[0]) {
pathprint(extra_args, sizeof(extra_args), smartctl, file);
const char* argv[] = {
x,
"-n",
"standby,3",
"-a",
0
};
log_tag("smartctl:%s:%s:run: %s %s %s\n", file, name, x, args, extra_args);
f = os_popen(argv, extra_args);
argc += argsplit(argv + argc, ARGS_MAX - argc - 1, extra_args);
log_tag("smartctl:%s:%s:run: %s -n standby,3 [info: %s] %s\n", file, name, x, info_opts, extra_args);
} else {
pathprint(extra_args, sizeof(extra_args), "%s", file);
const char* argv[] = {
x,
"-n",
"standby,3",
"-a",
file,
0
};
log_tag("smartctl:%s:%s:run: %s %s %s\n", file, name, x, args, extra_args);
f = os_popen(argv, 0);
argv[argc++] = file;
log_tag("smartctl:%s:%s:run: %s -n standby,3 [info: %s] %s\n", file, name, x, info_opts, file);
}
argv[argc++] = 0;
f = os_popen(argv);
if (!f) {
/* LCOV_EXCL_START */
log_tag("device:%s:%s:spawn\n", file, name);
log_fatal(EEXTERNAL, "Failed to run '%s %s %s'.\n", x, args, extra_args);
if (smartctl[0])
log_fatal(EEXTERNAL, "Failed to run '%s -n standby,3 %s %s'.\n", x, info_opts, extra_args);
else
log_fatal(EEXTERNAL, "Failed to run '%s -n standby,3 %s %s'.\n", x, info_opts, file);
return -1;
/* LCOV_EXCL_STOP */
}
@ -3086,7 +3095,10 @@ static int devprobe(dev_t device, const char* name, const char* smartctl, int* p
if (!WIFEXITED(ret)) {
/* LCOV_EXCL_START */
log_tag("device:%s:%s:abort\n", file, name);
log_fatal(EEXTERNAL, "Failed to run '%s %s %s' (not exited).\n", x, args, extra_args);
if (smartctl[0])
log_fatal(EEXTERNAL, "Failed to run '%s -n standby,3 %s %s' (not exited).\n", x, info_opts, extra_args);
else
log_fatal(EEXTERNAL, "Failed to run '%s -n standby,3 %s %s' (not exited).\n", x, info_opts, file);
return -1;
/* LCOV_EXCL_STOP */
}
@ -3134,34 +3146,30 @@ static int devdown(dev_t device, const char* name, const char* smartctl)
/* LCOV_EXCL_STOP */
}
/* if there is a custom smartctl command */
const char* args = "-s standby,now";
const char* argv[ARGS_MAX];
unsigned argc = 0;
argv[argc++] = x;
argv[argc++] = "-s";
argv[argc++] = "standby,now";
if (smartctl[0]) {
pathprint(extra_args, sizeof(extra_args), smartctl, file);
const char* argv[] = {
x,
"-s",
"standby,now",
0
};
log_tag("smartctl:%s:%s:run: %s %s %s\n", file, name, x, args, extra_args);
f = os_popen(argv, extra_args);
argc += argsplit(argv + argc, ARGS_MAX - argc - 1, extra_args);
log_tag("smartctl:%s:%s:run: %s -s standby,now %s\n", file, name, x, extra_args);
} else {
pathprint(extra_args, sizeof(extra_args), "%s", file);
const char* argv[] = {
x,
"-s",
"standby,now",
file,
0
};
log_tag("smartctl:%s:%s:run: %s %s %s\n", file, name, x, args, extra_args);
f = os_popen(argv, 0);
argv[argc++] = file;
log_tag("smartctl:%s:%s:run: %s -s standby,now %s\n", file, name, x, file);
}
argv[argc++] = 0;
f = os_popen(argv);
if (!f) {
/* LCOV_EXCL_START */
log_tag("device:%s:%s:spawn\n", file, name);
log_fatal(EEXTERNAL, "Failed to run '%s %s %s'.\n", x, args, extra_args);
if (smartctl[0])
log_fatal(EEXTERNAL, "Failed to run '%s -s standby,now %s'.\n", x, extra_args);
else
log_fatal(EEXTERNAL, "Failed to run '%s -s standby,now %s'.\n", x, file);
return -1;
/* LCOV_EXCL_STOP */
}
@ -3181,7 +3189,10 @@ static int devdown(dev_t device, const char* name, const char* smartctl)
if (!WIFEXITED(ret)) {
/* LCOV_EXCL_START */
log_tag("device:%s:%s:abort\n", file, name);
log_fatal(EEXTERNAL, "Failed to run '%s %s %s' (not exited).\n", x, args, extra_args);
if (smartctl[0])
log_fatal(EEXTERNAL, "Failed to run '%s -s standby,now %s' (not exited).\n", x, extra_args);
else
log_fatal(EEXTERNAL, "Failed to run '%s -s standby,now %s' (not exited).\n", x, file);
return -1;
/* LCOV_EXCL_STOP */
}
@ -3189,7 +3200,10 @@ static int devdown(dev_t device, const char* name, const char* smartctl)
if (WEXITSTATUS(ret) != 0) {
/* LCOV_EXCL_START */
log_tag("device:%s:%s:exit:%d\n", file, name, WEXITSTATUS(ret));
log_fatal(EEXTERNAL, "Failed to run '%s %s %s' with return code %xh.\n", x, args, extra_args, WEXITSTATUS(ret));
if (smartctl[0])
log_fatal(EEXTERNAL, "Failed to run '%s -s standby,now %s' with return code %xh.\n", x, extra_args, WEXITSTATUS(ret));
else
log_fatal(EEXTERNAL, "Failed to run '%s -s standby,now %s' with return code %xh.\n", x, file, WEXITSTATUS(ret));
return -1;
/* LCOV_EXCL_STOP */
}
@ -3204,11 +3218,11 @@ static int devdown(dev_t device, const char* name, const char* smartctl)
* Spin down a specific device if it's up.
*/
#if HAVE_LINUX_DEVICE
static int devdownifup(dev_t device, const char* name, const char* smartctl, int* power)
static int devdownifup(dev_t device, const char* name, const char* smartctl, const char* smartctl_info, int* power)
{
*power = POWER_UNKNOWN;
if (devprobe(device, name, smartctl, power, 0, 0, 0, 0, 0, 0) != 0)
if (devprobe(device, name, smartctl, smartctl_info, power, 0, 0, 0, 0, 0, 0) != 0)
return -1;
if (*power == POWER_ACTIVE)
@ -3372,7 +3386,7 @@ static void* thread_spinup(void* arg)
msg_status("Spunup device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
/* after the spin up, get SMART info */
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -3435,7 +3449,7 @@ static void* thread_spindownifup(void* arg)
start = os_tick_ms();
if (devdownifup(devinfo->device, devinfo->name, devinfo->smartctl, &power) != 0) {
if (devdownifup(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, &power) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -3459,7 +3473,7 @@ static void* thread_smart(void* arg)
#if HAVE_LINUX_DEVICE
devinfo_t* devinfo = arg;
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */
@ -3488,7 +3502,7 @@ static void* thread_probe(void* arg)
#if HAVE_LINUX_DEVICE
devinfo_t* devinfo = arg;
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, &devinfo->power, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
if (devprobe(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smartctl_info, &devinfo->power, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
/* LCOV_EXCL_START */
return (void*)-1;
/* LCOV_EXCL_STOP */