~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/libfsimage/common/fsimage_plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 
3
 * of this software and associated documentation files (the "Software"), to
 
4
 * deal in the Software without restriction, including without limitation the
 
5
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 
6
 * sell copies of the Software, and to permit persons to whom the Software is
 
7
 * furnished to do so, subject to the following conditions:
 
8
 * 
 
9
 * The above copyright notice and this permission notice shall be included in
 
10
 * all copies or substantial portions of the Software.
 
11
 * 
 
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
17
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
18
 * DEALINGS IN THE SOFTWARE.
 
19
 *
 
20
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 
21
 * Use is subject to license terms.
 
22
 */
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <strings.h>
 
27
#include <string.h>
 
28
#include <dirent.h>
 
29
#include <dlfcn.h>
 
30
#include <errno.h>
 
31
 
 
32
#include "fsimage_plugin.h"
 
33
#include "fsimage_priv.h"
 
34
 
 
35
static fsi_plugin_t *plugins;
 
36
 
 
37
void
 
38
fsip_fs_set_data(fsi_t *fsi, void *data)
 
39
{
 
40
        fsi->f_data = data;
 
41
}
 
42
 
 
43
fsi_file_t *
 
44
fsip_file_alloc(fsi_t *fsi, void *data)
 
45
{
 
46
        fsi_file_t *ffi = malloc(sizeof (fsi_file_t));
 
47
        if (ffi == NULL)
 
48
                return (NULL);
 
49
 
 
50
        bzero(ffi, sizeof (fsi_file_t));
 
51
 
 
52
        ffi->ff_fsi = fsi;
 
53
        ffi->ff_data = data;
 
54
        return (ffi);
 
55
}
 
56
 
 
57
void
 
58
fsip_file_free(fsi_file_t *ffi)
 
59
{
 
60
        free(ffi);
 
61
}
 
62
 
 
63
fsi_t *
 
64
fsip_fs(fsi_file_t *ffi)
 
65
{
 
66
        return (ffi->ff_fsi);
 
67
}
 
68
 
 
69
uint64_t
 
70
fsip_fs_offset(fsi_t *fsi)
 
71
{
 
72
        return (fsi->f_off);
 
73
}
 
74
 
 
75
void *
 
76
fsip_fs_data(fsi_t *fsi)
 
77
{
 
78
        return (fsi->f_data);
 
79
}
 
80
 
 
81
void *
 
82
fsip_file_data(fsi_file_t *ffi)
 
83
{
 
84
        return (ffi->ff_data);
 
85
}
 
86
 
 
87
static int init_plugin(const char *lib)
 
88
{
 
89
        fsi_plugin_init_t init;
 
90
        fsi_plugin_t *fp = malloc(sizeof (fsi_plugin_t));
 
91
 
 
92
        if (fp == NULL)
 
93
                return (-1);
 
94
 
 
95
        bzero(fp, sizeof (fsi_plugin_t));
 
96
 
 
97
        if ((fp->fp_dlh = dlopen(lib, RTLD_LAZY | RTLD_LOCAL)) == NULL) {
 
98
                free(fp);
 
99
                return (0);
 
100
        }
 
101
 
 
102
        init = dlsym(fp->fp_dlh, "fsi_init_plugin");
 
103
 
 
104
        if (init == NULL)
 
105
                goto fail;
 
106
 
 
107
        fp->fp_ops = init(FSIMAGE_PLUGIN_VERSION, fp, &fp->fp_name);
 
108
        if (fp->fp_ops == NULL ||
 
109
            fp->fp_ops->fpo_version > FSIMAGE_PLUGIN_VERSION)
 
110
                goto fail;
 
111
 
 
112
        fp->fp_next = plugins;
 
113
        plugins = fp;
 
114
 
 
115
        return (0);
 
116
fail:
 
117
        (void) dlclose(fp->fp_dlh);
 
118
        free(fp);
 
119
        return (-1);
 
120
}
 
121
 
 
122
static int load_plugins(void)
 
123
{
 
124
        const char *fsdir = getenv("FSIMAGE_FSDIR");
 
125
        const char *isadir = "";
 
126
        struct dirent *dp = NULL;
 
127
        struct dirent *dpp;
 
128
        DIR *dir = NULL;
 
129
        char *tmp = NULL;
 
130
        size_t name_max;
 
131
        int err;
 
132
        int ret = -1;
 
133
 
 
134
#if defined(FSIMAGE_FSDIR)
 
135
        if (fsdir == NULL)
 
136
                fsdir = FSIMAGE_FSDIR;
 
137
#elif defined(__sun__)
 
138
        if (fsdir == NULL)
 
139
                fsdir = "/usr/lib/fs";
 
140
 
 
141
        if (sizeof(void *) == 8)
 
142
                isadir = "64/";
 
143
#elif defined(__ia64__)
 
144
        if (fsdir == NULL)
 
145
                fsdir = "/usr/lib/fs";
 
146
#else
 
147
        if (fsdir == NULL) {
 
148
                if (sizeof(void *) == 8)
 
149
                        fsdir = "/usr/lib64/fs";
 
150
                else
 
151
                        fsdir = "/usr/lib/fs";
 
152
        }
 
153
#endif
 
154
 
 
155
        if ((name_max = pathconf(fsdir, _PC_NAME_MAX)) == -1)
 
156
                goto fail;
 
157
 
 
158
        if ((tmp = malloc(name_max + 1)) == NULL)
 
159
                goto fail;
 
160
 
 
161
        if ((dp = malloc(sizeof (struct dirent) + name_max + 1)) == NULL)
 
162
                goto fail;
 
163
 
 
164
        if ((dir = opendir(fsdir)) == NULL)
 
165
                goto fail;
 
166
 
 
167
        bzero(dp, sizeof (struct dirent) + name_max + 1);
 
168
 
 
169
        while (readdir_r(dir, dp, &dpp) == 0 && dpp != NULL) {
 
170
                if (strcmp(dpp->d_name, ".") == 0)
 
171
                        continue;
 
172
                if (strcmp(dpp->d_name, "..") == 0)
 
173
                        continue;
 
174
 
 
175
                (void) snprintf(tmp, name_max, "%s/%s/%sfsimage.so", fsdir,
 
176
                    dpp->d_name, isadir);
 
177
 
 
178
                if (init_plugin(tmp) != 0)
 
179
                        goto fail;
 
180
        }
 
181
 
 
182
        ret = 0;
 
183
 
 
184
fail:
 
185
        err = errno;
 
186
        if (dir != NULL)
 
187
                (void) closedir(dir);
 
188
        free(tmp);
 
189
        free(dp);
 
190
        errno = err;
 
191
        return (ret);
 
192
}
 
193
 
 
194
int find_plugin(fsi_t *fsi, const char *path, const char *options)
 
195
{
 
196
        fsi_plugin_t *fp;
 
197
        int ret = 0;
 
198
 
 
199
        if (plugins == NULL && (ret = load_plugins()) != 0)
 
200
                goto out;
 
201
 
 
202
        for (fp = plugins; fp != NULL; fp = fp->fp_next) {
 
203
                fsi->f_plugin = fp;
 
204
                if (fp->fp_ops->fpo_mount(fsi, path, options) == 0)
 
205
                        goto out;
 
206
        }
 
207
 
 
208
        ret = -1;
 
209
        errno = ENOTSUP;
 
210
out:
 
211
        return (ret);
 
212
}