| 1 |
/** |
| 2 |
* collectd - src/utils_mount.c |
| 3 |
* Copyright (C) 2005,2006 Niki W. Waibel |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/ |
| 6 |
* or modify it under the terms of the GNU General Public Li- |
| 7 |
* cence as published by the Free Software Foundation; either |
| 8 |
* version 2 of the Licence, or any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be use- |
| 11 |
* ful, but WITHOUT ANY WARRANTY; without even the implied war- |
| 12 |
* ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public Licence for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* Author: |
| 20 |
* Niki W. Waibel <[email protected]> |
| 21 |
**/ |
| 22 |
|
| 23 |
#if HAVE_CONFIG_H |
| 24 |
#include "config.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#define _GNU_SOURCE |
| 28 |
|
| 29 |
#include "collectd.h" |
| 30 |
|
| 31 |
#include "utils/mount/mount.h" |
| 32 |
|
| 33 |
#if HAVE_XFS_XQM_H |
| 34 |
#include <xfs/xqm.h> |
| 35 |
#define XFS_SUPER_MAGIC_STR "XFSB" |
| 36 |
#define XFS_SUPER_MAGIC2_STR "BSFX" |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "plugin.h" /* ERROR() macro */ |
| 40 |
#include "utils/common/common.h" /* sstrncpy() et alii */ |
| 41 |
|
| 42 |
#if HAVE_GETVFSSTAT |
| 43 |
#if HAVE_SYS_TYPES_H |
| 44 |
#include <sys/types.h> |
| 45 |
#endif |
| 46 |
#if HAVE_SYS_STATVFS_H |
| 47 |
#include <sys/statvfs.h> |
| 48 |
#endif |
| 49 |
/* #endif HAVE_GETVFSSTAT */ |
| 50 |
|
| 51 |
#elif HAVE_GETFSSTAT |
| 52 |
#if HAVE_SYS_PARAM_H |
| 53 |
#include <sys/param.h> |
| 54 |
#endif |
| 55 |
#if HAVE_SYS_UCRED_H |
| 56 |
#include <sys/ucred.h> |
| 57 |
#endif |
| 58 |
#if HAVE_SYS_MOUNT_H |
| 59 |
#include <sys/mount.h> |
| 60 |
#endif |
| 61 |
#endif /* HAVE_GETFSSTAT */ |
| 62 |
|
| 63 |
#if HAVE_MNTENT_H |
| 64 |
#include <mntent.h> |
| 65 |
#endif |
| 66 |
#if HAVE_SYS_MNTTAB_H |
| 67 |
#include <sys/mnttab.h> |
| 68 |
#endif |
| 69 |
|
| 70 |
#if HAVE_PATHS_H |
| 71 |
#include <paths.h> |
| 72 |
#endif |
| 73 |
|
| 74 |
#ifdef COLLECTD_MNTTAB |
| 75 |
#undef COLLECTD_MNTTAB |
| 76 |
#endif |
| 77 |
|
| 78 |
#if defined(_PATH_MOUNTED) /* glibc */ |
| 79 |
#define COLLECTD_MNTTAB _PATH_MOUNTED |
| 80 |
#elif defined(MNTTAB) /* Solaris */ |
| 81 |
#define COLLECTD_MNTTAB MNTTAB |
| 82 |
#elif defined(MNT_MNTTAB) |
| 83 |
#define COLLECTD_MNTTAB MNT_MNTTAB |
| 84 |
#elif defined(MNTTABNAME) |
| 85 |
#define COLLECTD_MNTTAB MNTTABNAME |
| 86 |
#elif defined(KMTAB) |
| 87 |
#define COLLECTD_MNTTAB KMTAB |
| 88 |
#else |
| 89 |
#define COLLECTD_MNTTAB "/etc/mnttab" |
| 90 |
#endif |
| 91 |
|
| 92 |
/* *** *** *** ********************************************* *** *** *** */ |
| 93 |
/* *** *** *** *** *** *** private functions *** *** *** *** *** *** */ |
| 94 |
/* *** *** *** ********************************************* *** *** *** */ |
| 95 |
|
| 96 |
/* stolen from quota-3.13 (quota-tools) */ |
| 97 |
|
| 98 |
#define PROC_PARTITIONS "/proc/partitions" |
| 99 |
#define DEVLABELDIR "/dev" |
| 100 |
#define UUID 1 |
| 101 |
#define VOL 2 |
| 102 |
|
| 103 |
static struct uuidCache_s { |
| 104 |
struct uuidCache_s *next; |
| 105 |
char uuid[16]; |
| 106 |
char *label; |
| 107 |
char *device; |
| 108 |
} *uuidCache = NULL; |
| 109 |
|
| 110 |
#define EXT2_SUPER_MAGIC 0xEF53 |
| 111 |
struct ext2_super_block { |
| 112 |
unsigned char s_dummy1[56]; |
| 113 |
unsigned char s_magic[2]; |
| 114 |
unsigned char s_dummy2[46]; |
| 115 |
unsigned char s_uuid[16]; |
| 116 |
char s_volume_name[16]; |
| 117 |
}; |
| 118 |
#define ext2magic(s) \ |
| 119 |
((unsigned int)s.s_magic[0] + (((unsigned int)s.s_magic[1]) << 8)) |
| 120 |
|
| 121 |
#if HAVE_XFS_XQM_H |
| 122 |
struct xfs_super_block { |
| 123 |
unsigned char s_magic[4]; |
| 124 |
unsigned char s_dummy[28]; |
| 125 |
unsigned char s_uuid[16]; |
| 126 |
unsigned char s_dummy2[60]; |
| 127 |
char s_fsname[12]; |
| 128 |
}; |
| 129 |
#endif /* HAVE_XFS_XQM_H */ |
| 130 |
|
| 131 |
#define REISER_SUPER_MAGIC "ReIsEr2Fs" |
| 132 |
struct reiserfs_super_block { |
| 133 |
unsigned char s_dummy1[52]; |
| 134 |
unsigned char s_magic[10]; |
| 135 |
unsigned char s_dummy2[22]; |
| 136 |
unsigned char s_uuid[16]; |
| 137 |
char s_volume_name[16]; |
| 138 |
}; |
| 139 |
|
| 140 |
/* for now, only ext2 and xfs are supported */ |
| 141 |
static int get_label_uuid(const char *device, char **label, char *uuid) { |
| 142 |
/* start with ext2 and xfs tests, taken from mount_guess_fstype */ |
| 143 |
/* should merge these later */ |
| 144 |
int fd, rv = 1; |
| 145 |
size_t namesize; |
| 146 |
struct ext2_super_block e2sb; |
| 147 |
#if HAVE_XFS_XQM_H |
| 148 |
struct xfs_super_block xfsb; |
| 149 |
#endif |
| 150 |
struct reiserfs_super_block reisersb; |
| 151 |
|
| 152 |
fd = open(device, O_RDONLY); |
| 153 |
if (fd == -1) { |
| 154 |
return rv; |
| 155 |
} |
| 156 |
|
| 157 |
if (lseek(fd, 1024, SEEK_SET) == 1024 && |
| 158 |
read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb) && |
| 159 |
ext2magic(e2sb) == EXT2_SUPER_MAGIC) { |
| 160 |
memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid)); |
| 161 |
namesize = sizeof(e2sb.s_volume_name); |
| 162 |
*label = smalloc(namesize + 1); |
| 163 |
sstrncpy(*label, e2sb.s_volume_name, namesize); |
| 164 |
rv = 0; |
| 165 |
#if HAVE_XFS_XQM_H |
| 166 |
} else if (lseek(fd, 0, SEEK_SET) == 0 && |
| 167 |
read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb) && |
| 168 |
(strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 || |
| 169 |
strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) { |
| 170 |
memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid)); |
| 171 |
namesize = sizeof(xfsb.s_fsname); |
| 172 |
*label = smalloc(namesize + 1); |
| 173 |
sstrncpy(*label, xfsb.s_fsname, namesize); |
| 174 |
rv = 0; |
| 175 |
#endif /* HAVE_XFS_XQM_H */ |
| 176 |
} else if (lseek(fd, 65536, SEEK_SET) == 65536 && |
| 177 |
read(fd, (char *)&reisersb, sizeof(reisersb)) == |
| 178 |
sizeof(reisersb) && |
| 179 |
!strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) { |
| 180 |
memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid)); |
| 181 |
namesize = sizeof(reisersb.s_volume_name); |
| 182 |
*label = smalloc(namesize + 1); |
| 183 |
sstrncpy(*label, reisersb.s_volume_name, namesize); |
| 184 |
rv = 0; |
| 185 |
} |
| 186 |
close(fd); |
| 187 |
return rv; |
| 188 |
} |
| 189 |
|
| 190 |
static void uuidcache_addentry(char *device, char *label, char *uuid) { |
| 191 |
struct uuidCache_s *last; |
| 192 |
|
| 193 |
if (!uuidCache) { |
| 194 |
last = uuidCache = smalloc(sizeof(*uuidCache)); |
| 195 |
} else { |
| 196 |
for (last = uuidCache; last->next; last = last->next) |
| 197 |
; |
| 198 |
last->next = smalloc(sizeof(*uuidCache)); |
| 199 |
last = last->next; |
| 200 |
} |
| 201 |
last->next = NULL; |
| 202 |
last->device = device; |
| 203 |
last->label = label; |
| 204 |
memcpy(last->uuid, uuid, sizeof(last->uuid)); |
| 205 |
} |
| 206 |
|
| 207 |
static void uuidcache_init(void) { |
| 208 |
char line[100]; |
| 209 |
char *s; |
| 210 |
int ma, mi, sz; |
| 211 |
static char ptname[100]; |
| 212 |
FILE *procpt; |
| 213 |
char uuid[16], *label = NULL; |
| 214 |
char device[110]; |
| 215 |
int handleOnFirst; |
| 216 |
|
| 217 |
if (uuidCache) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
procpt = fopen(PROC_PARTITIONS, "r"); |
| 222 |
if (procpt == NULL) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
for (int firstPass = 1; firstPass >= 0; firstPass--) { |
| 227 |
fseek(procpt, 0, SEEK_SET); |
| 228 |
while (fgets(line, sizeof(line), procpt)) { |
| 229 |
if (sscanf(line, " %d %d %d %[^\n ]", &ma, &mi, &sz, ptname) != 4) { |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
/* skip extended partitions (heuristic: size 1) */ |
| 234 |
if (sz == 1) { |
| 235 |
continue; |
| 236 |
} |
| 237 |
|
| 238 |
/* look only at md devices on first pass */ |
| 239 |
handleOnFirst = !strncmp(ptname, "md", 2); |
| 240 |
if (firstPass != handleOnFirst) { |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
/* skip entire disk (minor 0, 64, ... on ide; |
| 245 |
0, 16, ... on sd) */ |
| 246 |
/* heuristic: partition name ends in a digit */ |
| 247 |
|
| 248 |
for (s = ptname; *s; s++) |
| 249 |
; |
| 250 |
|
| 251 |
if (isdigit((int)s[-1])) { |
| 252 |
/* |
| 253 |
* Note: this is a heuristic only - there is no reason |
| 254 |
* why these devices should live in /dev. |
| 255 |
* Perhaps this directory should be specifiable by option. |
| 256 |
* One might for example have /devlabel with links to /dev |
| 257 |
* for the devices that may be accessed in this way. |
| 258 |
* (This is useful, if the cdrom on /dev/hdc must not |
| 259 |
* be accessed.) |
| 260 |
*/ |
| 261 |
snprintf(device, sizeof(device), "%s/%s", DEVLABELDIR, ptname); |
| 262 |
if (!get_label_uuid(device, &label, uuid)) { |
| 263 |
uuidcache_addentry(sstrdup(device), label, uuid); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
fclose(procpt); |
| 269 |
} |
| 270 |
|
| 271 |
static unsigned char fromhex(char c) { |
| 272 |
if (isdigit((int)c)) { |
| 273 |
return c - '0'; |
| 274 |
} else if (islower((int)c)) { |
| 275 |
return c - 'a' + 10; |
| 276 |
} else { |
| 277 |
return c - 'A' + 10; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
static char *get_spec_by_x(int n, const char *t) { |
| 282 |
struct uuidCache_s *uc; |
| 283 |
|
| 284 |
uuidcache_init(); |
| 285 |
uc = uuidCache; |
| 286 |
|
| 287 |
while (uc) { |
| 288 |
switch (n) { |
| 289 |
case UUID: |
| 290 |
if (!memcmp(t, uc->uuid, sizeof(uc->uuid))) { |
| 291 |
return sstrdup(uc->device); |
| 292 |
} |
| 293 |
break; |
| 294 |
case VOL: |
| 295 |
if (!strcmp(t, uc->label)) { |
| 296 |
return sstrdup(uc->device); |
| 297 |
} |
| 298 |
break; |
| 299 |
} |
| 300 |
uc = uc->next; |
| 301 |
} |
| 302 |
return NULL; |
| 303 |
} |
| 304 |
|
| 305 |
static char *get_spec_by_uuid(const char *s) { |
| 306 |
char uuid[16]; |
| 307 |
|
| 308 |
if (strlen(s) != 36 || s[8] != '-' || s[13] != '-' || s[18] != '-' || |
| 309 |
s[23] != '-') { |
| 310 |
goto bad_uuid; |
| 311 |
} |
| 312 |
|
| 313 |
for (int i = 0; i < 16; i++) { |
| 314 |
if (*s == '-') { |
| 315 |
s++; |
| 316 |
} |
| 317 |
if (!isxdigit((int)s[0]) || !isxdigit((int)s[1])) { |
| 318 |
goto bad_uuid; |
| 319 |
} |
| 320 |
uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1])); |
| 321 |
s += 2; |
| 322 |
} |
| 323 |
return get_spec_by_x(UUID, uuid); |
| 324 |
|
| 325 |
bad_uuid: |
| 326 |
DEBUG("utils_mount: Found an invalid UUID: %s", s); |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
static char *get_spec_by_volume_label(const char *s) { |
| 331 |
return get_spec_by_x(VOL, s); |
| 332 |
} |
| 333 |
|
| 334 |
static char *get_device_name(const char *optstr) { |
| 335 |
char *rc; |
| 336 |
|
| 337 |
if (optstr == NULL) { |
| 338 |
return NULL; |
| 339 |
} else if (strncmp(optstr, "UUID=", 5) == 0) { |
| 340 |
DEBUG("utils_mount: TODO: check UUID= code!"); |
| 341 |
rc = get_spec_by_uuid(optstr + 5); |
| 342 |
} else if (strncmp(optstr, "LABEL=", 6) == 0) { |
| 343 |
DEBUG("utils_mount: TODO: check LABEL= code!"); |
| 344 |
rc = get_spec_by_volume_label(optstr + 6); |
| 345 |
} else { |
| 346 |
rc = sstrdup(optstr); |
| 347 |
} |
| 348 |
|
| 349 |
if (!rc) { |
| 350 |
DEBUG("utils_mount: Error checking device name: optstr = %s", optstr); |
| 351 |
} |
| 352 |
return rc; |
| 353 |
} |
| 354 |
|
| 355 |
/* What weird OS is this..? I can't find any info with google :/ -octo */ |
| 356 |
#if HAVE_LISTMNTENT && 0 |
| 357 |
static cu_mount_t *cu_mount_listmntent(void) { |
| 358 |
cu_mount_t *last = *list; |
| 359 |
struct mntent *mnt; |
| 360 |
|
| 361 |
struct tabmntent *mntlist; |
| 362 |
if (listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) { |
| 363 |
#if COLLECT_DEBUG |
| 364 |
DEBUG("utils_mount: calling listmntent() failed: %s", STRERRNO); |
| 365 |
#endif /* COLLECT_DEBUG */ |
| 366 |
} |
| 367 |
|
| 368 |
for (struct tabmntent *p = mntlist; p; p = p->next) { |
| 369 |
char *loop = NULL, *device = NULL; |
| 370 |
|
| 371 |
mnt = p->ment; |
| 372 |
loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop="); |
| 373 |
if (loop == NULL) { /* no loop= mount */ |
| 374 |
device = get_device_name(mnt->mnt_fsname); |
| 375 |
if (device == NULL) { |
| 376 |
DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)" |
| 377 |
": ignored", |
| 378 |
mnt->mnt_type, mnt->mnt_dir, mnt->mnt_fsname); |
| 379 |
continue; |
| 380 |
} |
| 381 |
} else { |
| 382 |
device = loop; |
| 383 |
} |
| 384 |
if (*list == NULL) { |
| 385 |
*list = (cu_mount_t *)smalloc(sizeof(cu_mount_t)); |
| 386 |
last = *list; |
| 387 |
} else { |
| 388 |
while (last->next != NULL) { /* is last really last? */ |
| 389 |
last = last->next; |
| 390 |
} |
| 391 |
last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t)); |
| 392 |
last = last->next; |
| 393 |
} |
| 394 |
last->dir = sstrdup(mnt->mnt_dir); |
| 395 |
last->spec_device = sstrdup(mnt->mnt_fsname); |
| 396 |
last->device = device; |
| 397 |
last->type = sstrdup(mnt->mnt_type); |
| 398 |
last->options = sstrdup(mnt->mnt_opts); |
| 399 |
last->next = NULL; |
| 400 |
} /* for(p = mntlist; p; p = p->next) */ |
| 401 |
|
| 402 |
return last; |
| 403 |
} /* cu_mount_t *cu_mount_listmntent(void) */ |
| 404 |
/* #endif HAVE_LISTMNTENT */ |
| 405 |
|
| 406 |
/* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */ |
| 407 |
#elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT |
| 408 |
static cu_mount_t *cu_mount_getfsstat(void) { |
| 409 |
#if HAVE_GETFSSTAT |
| 410 |
#define STRUCT_STATFS struct statfs |
| 411 |
#define CMD_STATFS getfsstat |
| 412 |
#define FLAGS_STATFS MNT_NOWAIT |
| 413 |
/* #endif HAVE_GETFSSTAT */ |
| 414 |
#elif HAVE_GETVFSSTAT |
| 415 |
#define STRUCT_STATFS struct statvfs |
| 416 |
#define CMD_STATFS getvfsstat |
| 417 |
#define FLAGS_STATFS ST_NOWAIT |
| 418 |
#endif /* HAVE_GETVFSSTAT */ |
| 419 |
|
| 420 |
int bufsize; |
| 421 |
STRUCT_STATFS *buf; |
| 422 |
|
| 423 |
int num; |
| 424 |
|
| 425 |
cu_mount_t *first = NULL; |
| 426 |
cu_mount_t *last = NULL; |
| 427 |
cu_mount_t *new = NULL; |
| 428 |
|
| 429 |
/* Get the number of mounted file systems */ |
| 430 |
if ((bufsize = CMD_STATFS(NULL, 0, FLAGS_STATFS)) < 1) { |
| 431 |
#if COLLECT_DEBUG |
| 432 |
DEBUG("utils_mount: getv?fsstat failed: %s", STRERRNO); |
| 433 |
#endif /* COLLECT_DEBUG */ |
| 434 |
return NULL; |
| 435 |
} |
| 436 |
|
| 437 |
if ((buf = calloc(bufsize, sizeof(*buf))) == NULL) |
| 438 |
return NULL; |
| 439 |
|
| 440 |
/* The bufsize needs to be passed in bytes. Really. This is not in the |
| 441 |
* manpage.. -octo */ |
| 442 |
if ((num = CMD_STATFS(buf, bufsize * sizeof(STRUCT_STATFS), FLAGS_STATFS)) < |
| 443 |
1) { |
| 444 |
#if COLLECT_DEBUG |
| 445 |
DEBUG("utils_mount: getv?fsstat failed: %s", STRERRNO); |
| 446 |
#endif /* COLLECT_DEBUG */ |
| 447 |
free(buf); |
| 448 |
return NULL; |
| 449 |
} |
| 450 |
|
| 451 |
for (int i = 0; i < num; i++) { |
| 452 |
if ((new = calloc(1, sizeof(*new))) == NULL) |
| 453 |
break; |
| 454 |
|
| 455 |
/* Copy values from `struct mnttab' */ |
| 456 |
new->dir = sstrdup(buf[i].f_mntonname); |
| 457 |
new->spec_device = sstrdup(buf[i].f_mntfromname); |
| 458 |
new->type = sstrdup(buf[i].f_fstypename); |
| 459 |
new->options = NULL; |
| 460 |
new->device = get_device_name(new->options); |
| 461 |
new->next = NULL; |
| 462 |
|
| 463 |
/* Append to list */ |
| 464 |
if (first == NULL) { |
| 465 |
first = new; |
| 466 |
last = new; |
| 467 |
} else { |
| 468 |
last->next = new; |
| 469 |
last = new; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
free(buf); |
| 474 |
|
| 475 |
return first; |
| 476 |
} |
| 477 |
/* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */ |
| 478 |
|
| 479 |
/* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */ |
| 480 |
#elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT |
| 481 |
static cu_mount_t *cu_mount_gen_getmntent(void) { |
| 482 |
struct mnttab mt; |
| 483 |
FILE *fp; |
| 484 |
|
| 485 |
cu_mount_t *first = NULL; |
| 486 |
cu_mount_t *last = NULL; |
| 487 |
cu_mount_t *new = NULL; |
| 488 |
|
| 489 |
DEBUG("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB); |
| 490 |
|
| 491 |
if ((fp = fopen(COLLECTD_MNTTAB, "r")) == NULL) { |
| 492 |
ERROR("fopen (%s): %s", COLLECTD_MNTTAB, STRERRNO); |
| 493 |
return NULL; |
| 494 |
} |
| 495 |
|
| 496 |
while (getmntent(fp, &mt) == 0) { |
| 497 |
if ((new = calloc(1, sizeof(*new))) == NULL) |
| 498 |
break; |
| 499 |
|
| 500 |
/* Copy values from `struct mnttab' */ |
| 501 |
new->dir = sstrdup(mt.mnt_mountp); |
| 502 |
new->spec_device = sstrdup(mt.mnt_special); |
| 503 |
new->type = sstrdup(mt.mnt_fstype); |
| 504 |
new->options = sstrdup(mt.mnt_mntopts); |
| 505 |
new->device = get_device_name(new->options); |
| 506 |
new->next = NULL; |
| 507 |
|
| 508 |
/* Append to list */ |
| 509 |
if (first == NULL) { |
| 510 |
first = new; |
| 511 |
last = new; |
| 512 |
} else { |
| 513 |
last->next = new; |
| 514 |
last = new; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
fclose(fp); |
| 519 |
|
| 520 |
return first; |
| 521 |
} /* static cu_mount_t *cu_mount_gen_getmntent (void) */ |
| 522 |
|
| 523 |
#elif HAVE_SEQ_GETMNTENT |
| 524 |
#warn "This version of `getmntent' hat not yet been implemented!" |
| 525 |
/* #endif HAVE_SEQ_GETMNTENT */ |
| 526 |
|
| 527 |
#elif HAVE_GETMNTENT_R |
| 528 |
static cu_mount_t *cu_mount_getmntent(void) { |
| 529 |
FILE *fp; |
| 530 |
struct mntent me; |
| 531 |
char mntbuf[1024]; |
| 532 |
|
| 533 |
cu_mount_t *first = NULL; |
| 534 |
cu_mount_t *last = NULL; |
| 535 |
cu_mount_t *new = NULL; |
| 536 |
|
| 537 |
DEBUG("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB); |
| 538 |
|
| 539 |
if ((fp = setmntent(COLLECTD_MNTTAB, "r")) == NULL) { |
| 540 |
ERROR("setmntent (%s): %s", COLLECTD_MNTTAB, STRERRNO); |
| 541 |
return NULL; |
| 542 |
} |
| 543 |
|
| 544 |
while (getmntent_r(fp, &me, mntbuf, sizeof(mntbuf))) { |
| 545 |
if ((new = calloc(1, sizeof(*new))) == NULL) |
| 546 |
break; |
| 547 |
|
| 548 |
/* Copy values from `struct mntent *' */ |
| 549 |
new->dir = sstrdup(me.mnt_dir); |
| 550 |
new->spec_device = sstrdup(me.mnt_fsname); |
| 551 |
new->type = sstrdup(me.mnt_type); |
| 552 |
new->options = sstrdup(me.mnt_opts); |
| 553 |
new->device = get_device_name(new->options); |
| 554 |
new->next = NULL; |
| 555 |
|
| 556 |
DEBUG("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options " |
| 557 |
"= %s, device = %s}", |
| 558 |
new->dir, new->spec_device, new->type, new->options, new->device); |
| 559 |
|
| 560 |
/* Append to list */ |
| 561 |
if (first == NULL) { |
| 562 |
first = new; |
| 563 |
last = new; |
| 564 |
} else { |
| 565 |
last->next = new; |
| 566 |
last = new; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
endmntent(fp); |
| 571 |
|
| 572 |
DEBUG("utils_mount: return 0x%p", (void *)first); |
| 573 |
|
| 574 |
return first; |
| 575 |
} /* HAVE_GETMNTENT_R */ |
| 576 |
|
| 577 |
#elif HAVE_ONE_GETMNTENT |
| 578 |
static cu_mount_t *cu_mount_getmntent(void) { |
| 579 |
FILE *fp; |
| 580 |
struct mntent *me; |
| 581 |
|
| 582 |
cu_mount_t *first = NULL; |
| 583 |
cu_mount_t *last = NULL; |
| 584 |
cu_mount_t *new = NULL; |
| 585 |
|
| 586 |
DEBUG("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB); |
| 587 |
|
| 588 |
if ((fp = setmntent(COLLECTD_MNTTAB, "r")) == NULL) { |
| 589 |
ERROR("setmntent (%s): %s", COLLECTD_MNTTAB, STRERRNO); |
| 590 |
return NULL; |
| 591 |
} |
| 592 |
|
| 593 |
while ((me = getmntent(fp)) != NULL) { |
| 594 |
if ((new = calloc(1, sizeof(*new))) == NULL) |
| 595 |
break; |
| 596 |
|
| 597 |
/* Copy values from `struct mntent *' */ |
| 598 |
new->dir = sstrdup(me->mnt_dir); |
| 599 |
new->spec_device = sstrdup(me->mnt_fsname); |
| 600 |
new->type = sstrdup(me->mnt_type); |
| 601 |
new->options = sstrdup(me->mnt_opts); |
| 602 |
new->device = get_device_name(new->options); |
| 603 |
new->next = NULL; |
| 604 |
|
| 605 |
DEBUG("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options " |
| 606 |
"= %s, device = %s}", |
| 607 |
new->dir, new->spec_device, new->type, new->options, new->device); |
| 608 |
|
| 609 |
/* Append to list */ |
| 610 |
if (first == NULL) { |
| 611 |
first = new; |
| 612 |
last = new; |
| 613 |
} else { |
| 614 |
last->next = new; |
| 615 |
last = new; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
endmntent(fp); |
| 620 |
|
| 621 |
DEBUG("utils_mount: return 0x%p", (void *)first); |
| 622 |
|
| 623 |
return first; |
| 624 |
} |
| 625 |
#endif /* HAVE_ONE_GETMNTENT */ |
| 626 |
|
| 627 |
/* *** *** *** ******************************************** *** *** *** */ |
| 628 |
/* *** *** *** *** *** *** public functions *** *** *** *** *** *** */ |
| 629 |
/* *** *** *** ******************************************** *** *** *** */ |
| 630 |
|
| 631 |
cu_mount_t *cu_mount_getlist(cu_mount_t **list) { |
| 632 |
cu_mount_t *new; |
| 633 |
cu_mount_t *first = NULL; |
| 634 |
cu_mount_t *last = NULL; |
| 635 |
|
| 636 |
if (list == NULL) |
| 637 |
return NULL; |
| 638 |
|
| 639 |
if (*list != NULL) { |
| 640 |
first = *list; |
| 641 |
last = first; |
| 642 |
while (last->next != NULL) |
| 643 |
last = last->next; |
| 644 |
} |
| 645 |
|
| 646 |
#if HAVE_LISTMNTENT && 0 |
| 647 |
new = cu_mount_listmntent(); |
| 648 |
#elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT |
| 649 |
new = cu_mount_getfsstat(); |
| 650 |
#elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT |
| 651 |
new = cu_mount_gen_getmntent(); |
| 652 |
#elif HAVE_SEQ_GETMNTENT |
| 653 |
#error "This version of `getmntent' hat not yet been implemented!" |
| 654 |
#elif HAVE_GETMNTENT_R |
| 655 |
new = cu_mount_getmntent(); |
| 656 |
#elif HAVE_ONE_GETMNTENT |
| 657 |
new = cu_mount_getmntent(); |
| 658 |
#else |
| 659 |
#error "Could not determine how to find mountpoints." |
| 660 |
#endif |
| 661 |
|
| 662 |
if (first != NULL) { |
| 663 |
last->next = new; |
| 664 |
} else { |
| 665 |
first = new; |
| 666 |
last = new; |
| 667 |
*list = first; |
| 668 |
} |
| 669 |
|
| 670 |
while ((last != NULL) && (last->next != NULL)) |
| 671 |
last = last->next; |
| 672 |
|
| 673 |
return last; |
| 674 |
} /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */ |
| 675 |
|
| 676 |
void cu_mount_freelist(cu_mount_t *list) { |
| 677 |
cu_mount_t *next; |
| 678 |
|
| 679 |
for (cu_mount_t *this = list; this != NULL; this = next) { |
| 680 |
next = this->next; |
| 681 |
|
| 682 |
sfree(this->dir); |
| 683 |
sfree(this->spec_device); |
| 684 |
sfree(this->device); |
| 685 |
sfree(this->type); |
| 686 |
sfree(this->options); |
| 687 |
sfree(this); |
| 688 |
} |
| 689 |
} /* void cu_mount_freelist(cu_mount_t *list) */ |
| 690 |
|
| 691 |
char *cu_mount_checkoption(char *line, const char *keyword, int full) { |
| 692 |
char *line2, *l2, *p1, *p2; |
| 693 |
size_t l; |
| 694 |
|
| 695 |
if (line == NULL || keyword == NULL) { |
| 696 |
return NULL; |
| 697 |
} |
| 698 |
|
| 699 |
if (full != 0) { |
| 700 |
full = 1; |
| 701 |
} |
| 702 |
|
| 703 |
line2 = sstrdup(line); |
| 704 |
l2 = line2; |
| 705 |
while (*l2 != '\0') { |
| 706 |
if (*l2 == ',') { |
| 707 |
*l2 = '\0'; |
| 708 |
} |
| 709 |
l2++; |
| 710 |
} |
| 711 |
|
| 712 |
l = strlen(keyword); |
| 713 |
p1 = line - 1; |
| 714 |
p2 = strchr(line, ','); |
| 715 |
do { |
| 716 |
if (strncmp(line2 + (p1 - line) + 1, keyword, l + full) == 0) { |
| 717 |
free(line2); |
| 718 |
return p1 + 1; |
| 719 |
} |
| 720 |
p1 = p2; |
| 721 |
if (p1 != NULL) { |
| 722 |
p2 = strchr(p1 + 1, ','); |
| 723 |
} |
| 724 |
} while (p1 != NULL); |
| 725 |
|
| 726 |
free(line2); |
| 727 |
return NULL; |
| 728 |
} /* char *cu_mount_checkoption(char *line, char *keyword, int full) */ |
| 729 |
|
| 730 |
char *cu_mount_getoptionvalue(char *line, const char *keyword) { |
| 731 |
char *r; |
| 732 |
|
| 733 |
r = cu_mount_checkoption(line, keyword, 0); |
| 734 |
if (r != NULL) { |
| 735 |
char *p; |
| 736 |
r += strlen(keyword); |
| 737 |
p = strchr(r, ','); |
| 738 |
if (p == NULL) { |
| 739 |
return sstrdup(r); |
| 740 |
} else { |
| 741 |
char *m; |
| 742 |
if ((p - r) == 1) { |
| 743 |
return NULL; |
| 744 |
} |
| 745 |
m = smalloc(p - r + 1); |
| 746 |
sstrncpy(m, r, p - r + 1); |
| 747 |
return m; |
| 748 |
} |
| 749 |
} |
| 750 |
return r; |
| 751 |
} /* char *cu_mount_getoptionvalue(char *line, const char *keyword) */ |
| 752 |
|
| 753 |
int cu_mount_type(const char *type) { |
| 754 |
if (strcmp(type, "ext3") == 0) |
| 755 |
return CUMT_EXT3; |
| 756 |
if (strcmp(type, "ext2") == 0) |
| 757 |
return CUMT_EXT2; |
| 758 |
if (strcmp(type, "ufs") == 0) |
| 759 |
return CUMT_UFS; |
| 760 |
if (strcmp(type, "vxfs") == 0) |
| 761 |
return CUMT_VXFS; |
| 762 |
if (strcmp(type, "zfs") == 0) |
| 763 |
return CUMT_ZFS; |
| 764 |
return CUMT_UNKNOWN; |
| 765 |
} /* int cu_mount_type(const char *type) */ |