summaryrefslogtreecommitdiff
path: root/src/pmdas/linux/devmapper.c
blob: 38f87d39090f23b9c58721771503f68cb1ae9ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Linux LVM Devices Cluster
 *
 * Copyright (c) 2013-2014 Red Hat.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <sys/stat.h>
#include "pmapi.h"
#include "impl.h"
#include "pmda.h"
#include "indom.h"
#include "devmapper.h"

int
refresh_dev_mapper(dev_mapper_t *lvs)
{
    int i;
    DIR *dirp;
    struct dirent *dentry;
    struct stat statbuf;
    char path[MAXPATHLEN];

    snprintf(path, sizeof(path), "%s/dev/mapper", linux_statspath);
    if ((dirp = opendir(path)) == NULL)
        return 1;
  
    for (i = 0; i < lvs->nlv; i++) {
        free(lvs->lv[i].dev_name);
        free(lvs->lv[i].lv_name);
    }
    lvs->nlv = 0;
    lvs->lv = NULL;
    while ((dentry = readdir(dirp)) != NULL) {
        char linkname[MAXPATHLEN];
        int linkname_len;

        snprintf(path, sizeof(path),
		 "%s/dev/mapper/%s", linux_statspath, dentry->d_name);

        if (stat(path, &statbuf) == -1)
            continue;
        if (!S_ISBLK(statbuf.st_mode))
            continue;

        if ((linkname_len = readlink(path, linkname, sizeof(linkname)-1)) < 0)
	    continue;
	linkname[linkname_len] = '\0';

        i = lvs->nlv;
        lvs->nlv++;
      
        lvs->lv = (lv_entry_t *)realloc(lvs->lv, lvs->nlv * sizeof(lv_entry_t));
        lvs->lv[i].id = lvs->nlv;

        lvs->lv[i].dev_name = malloc(strlen(dentry->d_name)+1);
        strcpy(lvs->lv[i].dev_name, dentry->d_name);

        lvs->lv[i].lv_name = malloc(linkname_len+1);
        strcpy(lvs->lv[i].lv_name, linkname);
    }
    closedir(dirp);

    if (lvs->lv_indom->it_numinst != lvs->nlv) {
        lvs->lv_indom->it_numinst = lvs->nlv;
        lvs->lv_indom->it_set = (pmdaInstid *)
                realloc(lvs->lv_indom->it_set, lvs->nlv * sizeof(pmdaInstid));
    }
    for (i = 0; i < lvs->nlv; i++) {
        int skip_prefix = 0;
        lvs->lv_indom->it_set[i].i_inst = lvs->lv[i].id;
        if (strncmp(lvs->lv[i].lv_name, "../", 3) == 0)
            skip_prefix = 3;
        lvs->lv_indom->it_set[i].i_name = lvs->lv[i].lv_name + skip_prefix;
    }
    return 0;
}