~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to shlibs/blkid/src/topology/lvm.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * lvm topology
3
 
 * -- this is fallback for old systems where the topology information is not
4
 
 *    exported by sysfs
5
 
 *
6
 
 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
7
 
 *
8
 
 * This file may be redistributed under the terms of the
9
 
 * GNU Lesser General Public License.
10
 
 *
11
 
 */
12
 
#include <errno.h>
13
 
#include <fcntl.h>
14
 
#include <stdint.h>
15
 
#include <stdio.h>
16
 
#include <stdlib.h>
17
 
#include <string.h>
18
 
#include <sys/stat.h>
19
 
#include <sys/types.h>
20
 
#include <unistd.h>
21
 
 
22
 
#include "topology.h"
23
 
 
24
 
#ifndef LVM_BLK_MAJOR
25
 
# define LVM_BLK_MAJOR     58
26
 
#endif
27
 
 
28
 
static int is_lvm_device(dev_t devno)
29
 
{
30
 
        if (major(devno) == LVM_BLK_MAJOR)
31
 
                return 1;
32
 
        return blkid_driver_has_major("lvm", major(devno));
33
 
}
34
 
 
35
 
static int probe_lvm_tp(blkid_probe pr, const struct blkid_idmag *mag)
36
 
{
37
 
        const char *paths[] = {
38
 
                "/usr/local/sbin/lvdisplay",
39
 
                "/usr/sbin/lvdisplay",
40
 
                "/sbin/lvdisplay"
41
 
        };
42
 
        int i, lvpipe[] = { -1, -1 }, stripes = 0, stripesize = 0;
43
 
        FILE *stream = NULL;
44
 
        char *cmd = NULL, *devname = NULL, buf[1024];
45
 
        dev_t devno = blkid_probe_get_devno(pr);
46
 
 
47
 
        if (!devno)
48
 
                goto nothing;           /* probably not a block device */
49
 
        if (!is_lvm_device(devno))
50
 
                goto nothing;
51
 
 
52
 
        for (i = 0; i < ARRAY_SIZE(paths); i++) {
53
 
                struct stat sb;
54
 
                if (stat(paths[i], &sb) == 0) {
55
 
                        cmd = (char *) paths[i];
56
 
                        break;
57
 
                }
58
 
        }
59
 
 
60
 
        if (!cmd)
61
 
                goto nothing;
62
 
 
63
 
        devname = blkid_devno_to_devname(devno);
64
 
        if (!devname)
65
 
                goto nothing;
66
 
 
67
 
        if (pipe(lvpipe) < 0) {
68
 
                DBG(DEBUG_LOWPROBE,
69
 
                        printf("Failed to open pipe: errno=%d", errno));
70
 
                goto nothing;
71
 
        }
72
 
 
73
 
        switch (fork()) {
74
 
        case 0:
75
 
        {
76
 
                char *lvargv[3];
77
 
 
78
 
                /* Plumbing */
79
 
                close(lvpipe[0]);
80
 
 
81
 
                if (lvpipe[1] != STDOUT_FILENO)
82
 
                        dup2(lvpipe[1], STDOUT_FILENO);
83
 
 
84
 
                /* The libblkid library could linked with setuid programs */
85
 
                if (setgid(getgid()) < 0)
86
 
                         exit(1);
87
 
                if (setuid(getuid()) < 0)
88
 
                         exit(1);
89
 
 
90
 
                lvargv[0] = cmd;
91
 
                lvargv[1] = devname;
92
 
                lvargv[2] = NULL;
93
 
 
94
 
                execv(lvargv[0], lvargv);
95
 
 
96
 
                DBG(DEBUG_LOWPROBE,
97
 
                        printf("Failed to execute %s: errno=%d", cmd, errno));
98
 
                exit(1);
99
 
        }
100
 
        case -1:
101
 
                DBG(DEBUG_LOWPROBE,
102
 
                        printf("Failed to forking: errno=%d", errno));
103
 
                goto nothing;
104
 
        default:
105
 
                break;
106
 
        }
107
 
 
108
 
        stream = fdopen(lvpipe[0], "r");
109
 
        if (!stream)
110
 
                goto nothing;
111
 
 
112
 
        while (fgets(buf, sizeof(buf), stream) != NULL) {
113
 
                if (!strncmp(buf, "Stripes", 7))
114
 
                        sscanf(buf, "Stripes %d", &stripes);
115
 
 
116
 
                if (!strncmp(buf, "Stripe size", 11))
117
 
                        sscanf(buf, "Stripe size (KByte) %d", &stripesize);
118
 
        }
119
 
 
120
 
        if (!stripes)
121
 
                goto nothing;
122
 
 
123
 
        blkid_topology_set_minimum_io_size(pr, stripesize << 10);
124
 
        blkid_topology_set_optimal_io_size(pr, (stripes * stripesize) << 10);
125
 
 
126
 
        free(devname);
127
 
        fclose(stream);
128
 
        close(lvpipe[1]);
129
 
        return 0;
130
 
 
131
 
nothing:
132
 
        free(devname);
133
 
        if (stream)
134
 
                fclose(stream);
135
 
        else if (lvpipe[0] != -1)
136
 
                close(lvpipe[0]);
137
 
        if (lvpipe[1] != -1)
138
 
                close(lvpipe[1]);
139
 
        return 1;
140
 
}
141
 
 
142
 
const struct blkid_idinfo lvm_tp_idinfo =
143
 
{
144
 
        .name           = "lvm",
145
 
        .probefunc      = probe_lvm_tp,
146
 
        .magics         = BLKID_NONE_MAGIC
147
 
};
148