ia64/xen-unstable

view tools/libfsimage/common/fsimage_plugin.c @ 15005:1cb58c70d813

ia64 doesn't use /usr/lib64 on any distribution. When it's present, it's a symlink to /usr/lib
Signed-off-by: Aron Griffis <aron@hp.com>
author kfraser@localhost.localdomain
date Thu May 03 09:32:35 2007 +0100 (2007-05-03)
parents 89ca591a2c21
children 2c48ed7e303d
line source
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 */
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>
32 #include "fsimage_plugin.h"
33 #include "fsimage_priv.h"
35 static fsi_plugin_t *plugins;
37 void
38 fsip_fs_set_data(fsi_t *fsi, void *data)
39 {
40 fsi->f_data = data;
41 }
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);
50 bzero(ffi, sizeof (fsi_file_t));
52 ffi->ff_fsi = fsi;
53 ffi->ff_data = data;
54 return (ffi);
55 }
57 void
58 fsip_file_free(fsi_file_t *ffi)
59 {
60 free(ffi);
61 }
63 fsi_t *
64 fsip_fs(fsi_file_t *ffi)
65 {
66 return (ffi->ff_fsi);
67 }
69 uint64_t
70 fsip_fs_offset(fsi_t *fsi)
71 {
72 return (fsi->f_off);
73 }
75 void *
76 fsip_fs_data(fsi_t *fsi)
77 {
78 return (fsi->f_data);
79 }
81 void *
82 fsip_file_data(fsi_file_t *ffi)
83 {
84 return (ffi->ff_data);
85 }
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));
92 if (fp == NULL)
93 return (-1);
95 bzero(fp, sizeof (fsi_plugin_t));
97 if ((fp->fp_dlh = dlopen(lib, RTLD_LAZY | RTLD_LOCAL)) == NULL) {
98 free(fp);
99 return (0);
100 }
102 init = dlsym(fp->fp_dlh, "fsi_init_plugin");
104 if (init == NULL)
105 goto fail;
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;
112 fp->fp_next = plugins;
113 plugins = fp;
115 return (0);
116 fail:
117 (void) dlclose(fp->fp_dlh);
118 free(fp);
119 return (-1);
120 }
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;
134 #ifdef __sun__
135 if (fsdir == NULL)
136 fsdir = "/usr/lib/fs";
138 if (sizeof(void *) == 8)
139 isadir = "64/";
140 #elif defined(__ia64__)
141 if (fsdir == NULL)
142 fsdir = "/usr/lib/fs";
143 #else
144 if (fsdir == NULL) {
145 if (sizeof(void *) == 8)
146 fsdir = "/usr/lib64/fs";
147 else
148 fsdir = "/usr/lib/fs";
149 }
150 #endif
152 if ((name_max = pathconf(fsdir, _PC_NAME_MAX)) == -1)
153 goto fail;
155 if ((tmp = malloc(name_max + 1)) == NULL)
156 goto fail;
158 if ((dp = malloc(sizeof (struct dirent) + name_max + 1)) == NULL)
159 goto fail;
161 if ((dir = opendir(fsdir)) == NULL)
162 goto fail;
164 bzero(dp, sizeof (struct dirent) + name_max + 1);
166 while (readdir_r(dir, dp, &dpp) == 0 && dpp != NULL) {
167 if (strcmp(dpp->d_name, ".") == 0)
168 continue;
169 if (strcmp(dpp->d_name, "..") == 0)
170 continue;
172 (void) snprintf(tmp, name_max, "%s/%s/%sfsimage.so", fsdir,
173 dpp->d_name, isadir);
175 if (init_plugin(tmp) != 0)
176 goto fail;
177 }
179 ret = 0;
181 fail:
182 err = errno;
183 if (dir != NULL)
184 (void) closedir(dir);
185 free(tmp);
186 free(dp);
187 errno = err;
188 return (ret);
189 }
191 int find_plugin(fsi_t *fsi, const char *path, const char *options)
192 {
193 fsi_plugin_t *fp;
194 int ret = 0;
196 if (plugins == NULL && (ret = load_plugins()) != 0)
197 goto out;
199 for (fp = plugins; fp != NULL; fp = fp->fp_next) {
200 fsi->f_plugin = fp;
201 if (fp->fp_ops->fpo_mount(fsi, path, options) == 0)
202 goto out;
203 }
205 ret = -1;
206 errno = ENOTSUP;
207 out:
208 return (ret);
209 }