1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 18:45:10 +00:00

Make conky resolve symlinks before using them. (#238)

* Make dev_name resolve symlinks before resolving the name.

- When symlinks are not resolved first, you will get an error on startup when
  using path in /dev/disk/by-id, for example, in diskiograph_*.

* Improve diskio device name resolution by always resolving symlinks.

- Any device path will be resolved of any symlinks before getting the usual
  checks for labels et device name.

* Check if diskio device is really a block device, not only if it exists.

- Current check would succeed in case the device is a path, like when using
  label: with an empty argument.
This commit is contained in:
Jérôme Poulin 2016-05-03 08:04:14 -04:00 committed by Brenden Matthews
parent d8c659e7de
commit 48122dcf82
2 changed files with 13 additions and 16 deletions

View File

@ -122,17 +122,15 @@
*/ */
const char *dev_name(const char *path) const char *dev_name(const char *path)
{ {
static char buf[255]; /* should be enough for pathnames */ static char buf[PATH_MAX];
ssize_t buflen;
if (!path) if (!path)
return NULL; return NULL;
#define DEV_NAME(x) \ #define DEV_NAME(x) \
x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
if ((buflen = readlink(path, buf, 254)) == -1) if (realpath(path, buf) == NULL)
return DEV_NAME(path); return DEV_NAME(path);
buf[buflen] = '\0';
return DEV_NAME(buf); return DEV_NAME(buf);
#undef DEV_NAME #undef DEV_NAME
} }

View File

@ -60,23 +60,22 @@ struct diskio_stat *prepare_diskio_stat(const char *s)
struct stat sb; struct stat sb;
std::vector<char> stat_name(text_buffer_size.get(*state)), device_name(text_buffer_size.get(*state)), device_s(text_buffer_size.get(*state)); std::vector<char> stat_name(text_buffer_size.get(*state)), device_name(text_buffer_size.get(*state)), device_s(text_buffer_size.get(*state));
struct diskio_stat *cur = &stats; struct diskio_stat *cur = &stats;
char * rpbuf;
if (!s) { if (!s) {
return &stats; return &stats;
} else {
strncpy(&(device_s[0]), s, text_buffer_size.get(*state));
} }
if (strncmp(&(device_s[0]), "label:", 6) == 0) { if (strncmp(s, "label:", 6) == 0) {
snprintf(&(device_name[0]), text_buffer_size.get(*state), "/dev/disk/by-label/%s", &(device_s[6])); snprintf(&(device_name[0]), text_buffer_size.get(*state), "/dev/disk/by-label/%s", s+6);
char * rpbuf;
rpbuf = realpath(&device_name[0], NULL); rpbuf = realpath(&device_name[0], NULL);
} else {
rpbuf = realpath(s, NULL);
}
if (rpbuf) { if (rpbuf) {
strncpy(&device_s[0], rpbuf, text_buffer_size.get(*state)); strncpy(&device_s[0], rpbuf, text_buffer_size.get(*state));
}
free(rpbuf); free(rpbuf);
} else { } else {
strncpy(&device_s[0], s, text_buffer_size.get(*state)); strncpy(&device_s[0], s, text_buffer_size.get(*state));
} }
@ -90,8 +89,8 @@ struct diskio_stat *prepare_diskio_stat(const char *s)
snprintf(&(stat_name[0]), text_buffer_size.get(*state), "/dev/%s", &(device_name[0])); snprintf(&(stat_name[0]), text_buffer_size.get(*state), "/dev/%s", &(device_name[0]));
if (stat(&(stat_name[0]), &sb)) { if (stat(&(stat_name[0]), &sb) || !S_ISBLK(sb.st_mode)) {
NORM_ERR("diskio device '%s' does not exist", s); NORM_ERR("diskio device '%s' does not exist", &device_s[0]);
} }
/* lookup existing */ /* lookup existing */