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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <limits.h>
#include <libnvpair.h>
#include <dlfcn.h>
#include <libintl.h>
#include <sys/systeminfo.h>
#include <sys/fs_reparse.h>
#include "rp_plugin.h"
#define MAXISALEN 257 /* based on sysinfo(2) man page */
static rp_proto_handle_t rp_proto_handle;
static rp_proto_plugin_t *rp_proto_list;
int rp_plugin_init(void);
static void proto_plugin_fini(void);
static rp_plugin_ops_t *rp_find_protocol(const char *svctype);
extern int errno;
static int rp_plugin_inited = 0;
/*
* reparse_create()
*
* Create a symlink at the specified 'path' as a reparse point.
* This function will fail if path refers to an existing file system
* object or an object named string already exists at the given path.
*
* return 0 if ok else return error code.
*/
int
reparse_create(const char *path, const char *data)
{
int err;
struct stat sbuf;
if (path == NULL || data == NULL)
return (EINVAL);
if ((err = reparse_validate(data)) != 0)
return (err);
/* check if object exists */
if (lstat(path, &sbuf) == 0)
return (EEXIST);
return (symlink(data, path) ? errno : 0);
}
/*
* reparse_unparse()
*
* Convert an nvlist back to a string format suitable to write
* to the reparse point symlink body. The string returned is in
* allocated memory and must be freed by the caller.
*
* return 0 if ok else return error code.
*/
int
reparse_unparse(nvlist_t *nvl, char **stringp)
{
int err, buflen;
char *buf, *stype, *val;
nvpair_t *curr;
if (nvl == NULL || stringp == NULL ||
((curr = nvlist_next_nvpair(nvl, NULL)) == NULL))
return (EINVAL);
buflen = SYMLINK_MAX;
if ((buf = malloc(buflen)) == NULL)
return (ENOMEM);
err = 0;
(void) snprintf(buf, buflen, "%s", FS_REPARSE_TAG_STR);
while (curr != NULL) {
if (!(stype = nvpair_name(curr))) {
err = EINVAL;
break;
}
if ((strlcat(buf, FS_TOKEN_START_STR, buflen) >= buflen) ||
(strlcat(buf, stype, buflen) >= buflen) ||
(strlcat(buf, ":", buflen) >= buflen) ||
(nvpair_value_string(curr, &val) != 0) ||
(strlcat(buf, val, buflen) >= buflen) ||
(strlcat(buf, FS_TOKEN_END_STR, buflen) >= buflen)) {
err = E2BIG;
break;
}
curr = nvlist_next_nvpair(nvl, curr);
}
if (err != 0) {
free(buf);
return (err);
}
if (strlcat(buf, FS_REPARSE_TAG_END_STR, buflen) >= buflen) {
free(buf);
return (E2BIG);
}
*stringp = buf;
return (0);
}
/*
* reparse_deref()
*
* Accepts the service-specific item from the reparse point and returns
* the service-specific data requested. The caller specifies the size
* of the buffer provided via *bufsz.
*
* if ok return 0 and *bufsz is updated to contain the actual length of
* the returned results, else return error code. If the error code is
* EOVERFLOW; results do not fit in the buffer, *bufsz will be updated
* to contain the number of bytes needed to hold the results.
*/
int
reparse_deref(const char *svc_type, const char *svc_data, char *buf,
size_t *bufsz)
{
rp_plugin_ops_t *ops;
if ((svc_type == NULL) || (svc_data == NULL) || (buf == NULL) ||
(bufsz == NULL))
return (EINVAL);
ops = rp_find_protocol(svc_type);
if ((ops != NULL) && (ops->rpo_deref != NULL))
return (ops->rpo_deref(svc_type, svc_data, buf, bufsz));
/* no plugin, return error */
return (ENOTSUP);
}
/*
* reparse_delete()
*
* Delete a reparse point at a given pathname. It will fail if
* a reparse point does not exist at the given path or the pathname
* is not a symlink.
*
* return 0 if ok else return error code.
*/
int
reparse_delete(const char *path)
{
struct stat sbuf;
if (path == NULL)
return (EINVAL);
/* check if object exists */
if (lstat(path, &sbuf) != 0)
return (errno);
if ((sbuf.st_mode & S_IFLNK) != S_IFLNK)
return (EINVAL);
return (unlink(path) ? errno : 0);
}
/*
* reparse_add()
*
* Add a service type entry to a nvlist with a copy of svc_data,
* replacing one of the same type if already present.
*
* return 0 if ok else return error code.
*/
int
reparse_add(nvlist_t *nvl, const char *svc_type, const char *svc_data)
{
int err;
char *buf;
size_t bufsz;
rp_plugin_ops_t *ops;
if ((nvl == NULL) || (svc_type == NULL) || (svc_data == NULL))
return (EINVAL);
bufsz = SYMLINK_MAX; /* no need to mess around */
if ((buf = malloc(bufsz)) == NULL)
return (ENOMEM);
ops = rp_find_protocol(svc_type);
if ((ops != NULL) && (ops->rpo_form != NULL))
err = ops->rpo_form(svc_type, svc_data, buf, &bufsz);
else
err = ENOTSUP; /* no plugin */
if (err != 0) {
free(buf);
return (err);
}
err = nvlist_add_string(nvl, svc_type, buf);
free(buf);
return (err);
}
/*
* reparse_remove()
*
* Remove a service type entry from the nvlist, if present.
*
* return 0 if ok else return error code.
*/
int
reparse_remove(nvlist_t *nvl, const char *svc_type)
{
if ((nvl == NULL) || (svc_type == NULL))
return (EINVAL);
return (nvlist_remove_all(nvl, svc_type));
}
/*
* Returns true if name is "." or "..", otherwise returns false.
*/
static boolean_t
rp_is_dot_or_dotdot(const char *name)
{
if (*name != '.')
return (B_FALSE);
if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))
return (B_TRUE);
return (B_FALSE);
}
static void
proto_plugin_fini()
{
rp_proto_plugin_t *p;
/*
* Protocols may call this framework during _fini
*/
for (p = rp_proto_list; p != NULL; p = p->plugin_next) {
if (p->plugin_ops->rpo_fini)
p->plugin_ops->rpo_fini();
}
while ((p = rp_proto_list) != NULL) {
rp_proto_list = p->plugin_next;
if (p->plugin_handle != NULL)
(void) dlclose(p->plugin_handle);
free(p);
}
if (rp_proto_handle.rp_ops != NULL) {
free(rp_proto_handle.rp_ops);
rp_proto_handle.rp_ops = NULL;
}
rp_proto_handle.rp_num_proto = 0;
}
/*
* rp_plugin_init()
*
* Initialize the service type specific plugin modules.
* For each reparse service type, there should be a plugin library for it.
* This function walks /usr/lib/reparse directory for plugin libraries.
* For each plugin library found, initialize it and add it to the internal
* list of service type plugin. These are used for service type specific
* operations.
*/
int
rp_plugin_init()
{
int err, ret = RP_OK;
char isa[MAXISALEN], dirpath[MAXPATHLEN], path[MAXPATHLEN];
int num_protos = 0;
rp_proto_handle_t *rp_hdl;
rp_proto_plugin_t *proto, *tmp;
rp_plugin_ops_t *plugin_ops;
struct stat st;
void *dlhandle;
DIR *dir;
struct dirent *dent;
#if defined(_LP64)
if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
isa[0] = '\0';
#else
isa[0] = '\0';
#endif
(void) snprintf(dirpath, MAXPATHLEN,
"%s/%s", RP_LIB_DIR, isa);
if ((dir = opendir(dirpath)) == NULL)
return (RP_NO_PLUGIN_DIR);
while ((dent = readdir(dir)) != NULL) {
if (rp_is_dot_or_dotdot(dent->d_name))
continue;
(void) snprintf(path, MAXPATHLEN,
"%s/%s", dirpath, dent->d_name);
/*
* If file doesn't exist, don't try to map it
*/
if (stat(path, &st) < 0)
continue;
if ((dlhandle = dlopen(path, RTLD_FIRST|RTLD_LAZY)) == NULL)
continue;
plugin_ops = (rp_plugin_ops_t *)
dlsym(dlhandle, "rp_plugin_ops");
if (plugin_ops == NULL) {
(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
"Error in plugin ops for service type %s\n%s\n"),
dent->d_name, dlerror());
(void) dlclose(dlhandle);
continue;
}
proto = (rp_proto_plugin_t *)
calloc(1, sizeof (rp_proto_plugin_t));
if (proto == NULL) {
(void) dlclose(dlhandle);
(void) fprintf(stderr,
dgettext(TEXT_DOMAIN, "No memory for plugin %s\n"),
dent->d_name);
ret = RP_NO_MEMORY;
break;
}
proto->plugin_ops = plugin_ops;
proto->plugin_handle = dlhandle;
num_protos++;
proto->plugin_next = rp_proto_list;
rp_proto_list = proto;
}
(void) closedir(dir);
if ((num_protos == 0) && (ret == 0))
ret = RP_NO_PLUGIN;
/*
* There was an error, so cleanup prior to return of failure.
*/
if (ret != RP_OK) {
proto_plugin_fini();
return (ret);
}
rp_proto_handle.rp_ops = (rp_plugin_ops_t **)calloc(num_protos,
sizeof (rp_plugin_ops_t *));
if (!rp_proto_handle.rp_ops) {
proto_plugin_fini();
return (RP_NO_MEMORY);
}
rp_hdl = &rp_proto_handle;
rp_hdl->rp_num_proto = 0;
for (tmp = rp_proto_list; rp_hdl->rp_num_proto < num_protos &&
tmp != NULL; tmp = tmp->plugin_next) {
err = RP_OK;
if (tmp->plugin_ops->rpo_init != NULL)
err = tmp->plugin_ops->rpo_init();
if (err != RP_OK)
continue;
rp_hdl->rp_ops[rp_hdl->rp_num_proto++] = tmp->plugin_ops;
}
return (rp_hdl->rp_num_proto > 0 ? RP_OK : RP_NO_PLUGIN);
}
/*
* find_protocol()
*
* Search the plugin list for the specified protocol and return the
* ops vector. return NULL if protocol is not defined.
*/
static rp_plugin_ops_t *
rp_find_protocol(const char *svc_type)
{
int i;
rp_plugin_ops_t *ops = NULL;
if (svc_type == NULL)
return (NULL);
if (rp_plugin_inited == 0) {
if (rp_plugin_init() == RP_OK)
rp_plugin_inited = 1;
else
return (NULL);
}
for (i = 0; i < rp_proto_handle.rp_num_proto; i++) {
ops = rp_proto_handle.rp_ops[i];
if (ops->rpo_supports_svc(svc_type))
return (ops);
}
return (NULL);
}
|