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

« back to all changes in this revision

Viewing changes to shlibs/blkid/src/topology/dm.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
 
 * device-mapper (dm) 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
 
static int is_dm_device(dev_t devno)
25
 
{
26
 
        return blkid_driver_has_major("device-mapper", major(devno));
27
 
}
28
 
 
29
 
static int probe_dm_tp(blkid_probe pr, const struct blkid_idmag *mag)
30
 
{
31
 
        const char *paths[] = {
32
 
                "/usr/local/sbin/dmsetup",
33
 
                "/usr/sbin/dmsetup",
34
 
                "/sbin/dmsetup"
35
 
        };
36
 
        int i, dmpipe[] = { -1, -1 }, stripes, stripesize;
37
 
        char *cmd = NULL;
38
 
        FILE *stream = NULL;
39
 
        long long  offset, size;
40
 
        dev_t devno = blkid_probe_get_devno(pr);
41
 
 
42
 
        if (!devno)
43
 
                goto nothing;           /* probably not a block device */
44
 
        if (!is_dm_device(devno))
45
 
                goto nothing;
46
 
 
47
 
        for (i = 0; i < ARRAY_SIZE(paths); i++) {
48
 
                struct stat sb;
49
 
                if (stat(paths[i], &sb) == 0) {
50
 
                        cmd = (char *) paths[i];
51
 
                        break;
52
 
                }
53
 
        }
54
 
 
55
 
        if (!cmd)
56
 
                goto nothing;
57
 
        if (pipe(dmpipe) < 0) {
58
 
                DBG(DEBUG_LOWPROBE,
59
 
                        printf("Failed to open pipe: errno=%d", errno));
60
 
                goto nothing;
61
 
        }
62
 
 
63
 
        switch (fork()) {
64
 
        case 0:
65
 
        {
66
 
                char *dmargv[7], maj[16], min[16];
67
 
 
68
 
                /* Plumbing */
69
 
                close(dmpipe[0]);
70
 
 
71
 
                if (dmpipe[1] != STDOUT_FILENO)
72
 
                        dup2(dmpipe[1], STDOUT_FILENO);
73
 
 
74
 
                /* The libblkid library could linked with setuid programs */
75
 
                if (setgid(getgid()) < 0)
76
 
                         exit(1);
77
 
                if (setuid(getuid()) < 0)
78
 
                         exit(1);
79
 
 
80
 
                snprintf(maj, sizeof(maj), "%d", major(devno));
81
 
                snprintf(min, sizeof(min), "%d", minor(devno));
82
 
 
83
 
                dmargv[0] = cmd;
84
 
                dmargv[1] = "table";
85
 
                dmargv[2] = "-j";
86
 
                dmargv[3] = maj;
87
 
                dmargv[4] = "-m";
88
 
                dmargv[5] = min;
89
 
                dmargv[6] = NULL;
90
 
 
91
 
                execv(dmargv[0], dmargv);
92
 
 
93
 
                DBG(DEBUG_LOWPROBE,
94
 
                        printf("Failed to execute %s: errno=%d", cmd, errno));
95
 
                exit(1);
96
 
        }
97
 
        case -1:
98
 
                DBG(DEBUG_LOWPROBE,
99
 
                        printf("Failed to forking: errno=%d", errno));
100
 
                goto nothing;
101
 
        default:
102
 
                break;
103
 
        }
104
 
 
105
 
        stream = fdopen(dmpipe[0], "r");
106
 
        if (!stream)
107
 
                goto nothing;
108
 
 
109
 
        i = fscanf(stream, "%lld %lld striped %d %d ",
110
 
                        &offset, &size, &stripes, &stripesize);
111
 
        if (i != 4)
112
 
                goto nothing;
113
 
 
114
 
        blkid_topology_set_minimum_io_size(pr, stripesize << 9);
115
 
        blkid_topology_set_optimal_io_size(pr, (stripes * stripesize) << 9);
116
 
 
117
 
        fclose(stream);
118
 
        close(dmpipe[1]);
119
 
        return 0;
120
 
 
121
 
nothing:
122
 
        if (stream)
123
 
                fclose(stream);
124
 
        else if (dmpipe[0] != -1)
125
 
                close(dmpipe[0]);
126
 
        if (dmpipe[1] != -1)
127
 
                close(dmpipe[1]);
128
 
        return 1;
129
 
}
130
 
 
131
 
const struct blkid_idinfo dm_tp_idinfo =
132
 
{
133
 
        .name           = "dm",
134
 
        .probefunc      = probe_dm_tp,
135
 
        .magics         = BLKID_NONE_MAGIC
136
 
};
137