dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libshare / common / plugin.c
blob1dba19ff74517fc441e5ad1b17250a82a7682786
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <libshare.h>
31 #include "libshare_impl.h"
32 #include <dlfcn.h>
33 #include <link.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <libintl.h>
39 #include <sys/systeminfo.h>
40 #include <thread.h>
41 #include <synch.h>
43 #define MAXISALEN 257 /* based on sysinfo(2) man page */
46 * protocol plugin interface
48 * finds plugins and makes them accessible. This is only "used" by
49 * libshare.so.
52 struct sa_proto_plugin *sap_proto_list;
54 static struct sa_proto_handle sa_proto_handle;
56 void proto_plugin_fini();
59 * Returns true if name is "." or "..", otherwise returns false.
61 static boolean_t
62 proto_is_dot_or_dotdot(const char *name)
64 if (*name != '.')
65 return (B_FALSE);
67 if ((name[1] == '\0') || (name[1] == '.' && name[2] == '\0'))
68 return (B_TRUE);
70 return (B_FALSE);
74 * proto_plugin_init()
76 * Initialize the protocol specific plugin modules.
78 * Walk /usr/lib/fs/\* for libshare_*.so modules, for example,
79 * /usr/lib/fs/nfs/libshare_nfs.so. A protocol specific directory
80 * would have modules with names of the form libshare_<proto>.so.
81 * For each protocol found, initialize it and add it to the internal
82 * list of protocols. These are used for protocol specific operations.
85 int
86 proto_plugin_init()
88 struct sa_proto_plugin *proto;
89 int num_protos = 0;
90 struct sa_plugin_ops *plugin_ops;
91 void *dlhandle;
92 DIR *dir;
93 struct dirent *dent;
94 int ret = SA_OK;
95 struct stat st;
96 char isa[MAXISALEN];
98 #if defined(_LP64)
99 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
100 isa[0] = '\0';
101 #else
102 isa[0] = '\0';
103 #endif
105 if ((dir = opendir(SA_LIB_DIR)) == NULL)
106 return (SA_OK);
108 while ((dent = readdir(dir)) != NULL) {
109 char path[MAXPATHLEN];
111 if (proto_is_dot_or_dotdot(dent->d_name))
112 continue;
114 (void) snprintf(path, MAXPATHLEN,
115 "%s/%s/%s/libshare_%s.so.1", SA_LIB_DIR,
116 dent->d_name, isa, dent->d_name);
119 * If file doesn't exist, don't try to map it
121 if (stat(path, &st) < 0)
122 continue;
124 if ((dlhandle = dlopen(path, RTLD_FIRST|RTLD_LAZY)) == NULL) {
125 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
126 "Error in plugin for protocol %s: %s\n"),
127 dent->d_name, dlerror());
128 continue;
131 plugin_ops = (struct sa_plugin_ops *)
132 dlsym(dlhandle, "sa_plugin_ops");
133 if (plugin_ops == NULL) {
134 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
135 "Error in plugin ops for protocol %s: %s\n"),
136 dent->d_name, dlerror());
137 (void) dlclose(dlhandle);
138 continue;
141 proto = (struct sa_proto_plugin *)
142 calloc(1, sizeof (struct sa_proto_plugin));
143 if (proto == NULL) {
144 (void) dlclose(dlhandle);
145 ret = SA_NO_MEMORY;
146 continue;
149 proto->plugin_ops = plugin_ops;
150 proto->plugin_handle = dlhandle;
151 num_protos++;
152 proto->plugin_next = sap_proto_list;
153 sap_proto_list = proto;
156 (void) closedir(dir);
158 if (num_protos != 0) {
159 sa_proto_handle.sa_proto =
160 (char **)calloc(num_protos, sizeof (char *));
161 sa_proto_handle.sa_ops =
162 (struct sa_plugin_ops **)calloc(num_protos,
163 sizeof (struct sa_plugin_ops *));
164 if (sa_proto_handle.sa_proto != NULL &&
165 sa_proto_handle.sa_ops != NULL) {
166 int i;
167 struct sa_proto_plugin *tmp;
169 for (i = 0, tmp = sap_proto_list;
170 i < num_protos && tmp != NULL;
171 tmp = tmp->plugin_next) {
172 int err;
173 err = SA_OK;
174 if (tmp->plugin_ops->sa_init != NULL)
175 err = tmp->plugin_ops->sa_init();
176 if (err == SA_OK) {
178 * Only include if the init
179 * succeeded or was NULL
181 sa_proto_handle.sa_num_proto++;
182 sa_proto_handle.sa_ops[i] =
183 tmp->plugin_ops;
184 sa_proto_handle.sa_proto[i] =
185 tmp->plugin_ops->sa_protocol;
186 i++;
189 } else {
190 ret = SA_NO_MEMORY;
195 * There was an error, so cleanup prior to return of failure.
197 if (ret != SA_OK)
198 proto_plugin_fini();
200 return (ret);
204 * proto_plugin_fini()
206 * Uninitialize all the plugin modules.
209 void
210 proto_plugin_fini()
212 struct sa_proto_plugin *p;
215 * Protocols may call this framework during _fini
216 * (the smbfs plugin is known to do this) so do
217 * two passes: 1st call _fini; 2nd free, dlclose.
219 for (p = sap_proto_list; p != NULL; p = p->plugin_next)
220 p->plugin_ops->sa_fini();
222 while ((p = sap_proto_list) != NULL) {
223 sap_proto_list = p->plugin_next;
225 if (p->plugin_handle != NULL)
226 (void) dlclose(p->plugin_handle);
227 free(p);
229 if (sa_proto_handle.sa_ops != NULL) {
230 free(sa_proto_handle.sa_ops);
231 sa_proto_handle.sa_ops = NULL;
233 if (sa_proto_handle.sa_proto != NULL) {
234 free(sa_proto_handle.sa_proto);
235 sa_proto_handle.sa_proto = NULL;
237 sa_proto_handle.sa_num_proto = 0;
241 * find_protocol(proto)
243 * Search the plugin list for the specified protocol and return the
244 * ops vector. NULL if protocol is not defined.
247 static struct sa_plugin_ops *
248 find_protocol(char *proto)
250 int i;
251 struct sa_plugin_ops *ops = NULL;
252 extern mutex_t sa_global_lock;
254 (void) mutex_lock(&sa_global_lock);
255 if (proto != NULL) {
256 for (i = 0; i < sa_proto_handle.sa_num_proto; i++) {
257 if (strcmp(proto, sa_proto_handle.sa_proto[i]) == 0) {
258 ops = sa_proto_handle.sa_ops[i];
259 break;
263 (void) mutex_unlock(&sa_global_lock);
264 return (ops);
268 * sa_proto_share(proto, share)
270 * Activate a share for the specified protocol.
274 sa_proto_share(char *proto, sa_share_t share)
276 struct sa_plugin_ops *ops = find_protocol(proto);
277 int ret = SA_INVALID_PROTOCOL;
279 if (ops != NULL && ops->sa_share != NULL)
280 ret = ops->sa_share(share);
281 return (ret);
285 * sa_proto_unshare(proto, share)
287 * Deactivate (unshare) the share for this protocol.
291 sa_proto_unshare(sa_share_t share, char *proto, char *path)
293 struct sa_plugin_ops *ops = find_protocol(proto);
294 int ret = SA_INVALID_PROTOCOL;
296 if (ops != NULL && ops->sa_unshare != NULL)
297 ret = ops->sa_unshare(share, path);
298 return (ret);
302 * sa_proto_share_resource(char *proto, sa_resource_t resource)
304 * For protocols that actually enable at the resource level, do the
305 * protocol specific resource enable. If it doesn't, return an error.
306 * Note that the resource functions are optional so can return
307 * SA_NOT_SUPPORTED.
311 sa_proto_share_resource(char *proto, sa_resource_t resource)
313 struct sa_plugin_ops *ops = find_protocol(proto);
314 int ret = SA_INVALID_PROTOCOL;
316 if (ops != NULL) {
317 if (ops->sa_enable_resource != NULL)
318 ret = ops->sa_enable_resource(resource);
319 else
320 ret = SA_NOT_SUPPORTED;
322 return (ret);
326 * sa_proto_unshare_resource(char *proto, sa_resource_t resource)
328 * For protocols that actually disable at the resource level, do the
329 * protocol specific resource disable. If it doesn't, return an error.
333 sa_proto_unshare_resource(char *proto, sa_resource_t resource)
335 struct sa_plugin_ops *ops = find_protocol(proto);
336 int ret = SA_INVALID_PROTOCOL;
338 if (ops != NULL) {
339 if (ops->sa_disable_resource != NULL)
340 ret = ops->sa_disable_resource(resource);
341 else
342 ret = SA_NOT_SUPPORTED;
344 return (ret);
348 * sa_proto_valid_prop(handle, proto, prop, opt)
350 * Check to see if the specified prop is valid for this protocol.
354 sa_proto_valid_prop(sa_handle_t handle, char *proto, sa_property_t prop,
355 sa_optionset_t opt)
357 struct sa_plugin_ops *ops = find_protocol(proto);
358 int ret = 0;
360 if (ops != NULL && ops->sa_valid_prop != NULL)
361 ret = ops->sa_valid_prop(handle, prop, opt);
362 return (ret);
366 * sa_proto_valid_space(proto, space)
368 * Check if space is valid optionspace for proto.
369 * Protocols that don't implement this don't support spaces.
372 sa_proto_valid_space(char *proto, char *token)
374 struct sa_plugin_ops *ops = find_protocol(proto);
375 int ret = 0;
377 if (ops != NULL && ops->sa_valid_space != NULL)
378 ret = ops->sa_valid_space(token);
379 return (ret);
383 * sa_proto_space_alias(proto, space)
385 * If the name for space is an alias, return its proper name. This is
386 * used to translate "default" values into proper form.
388 char *
389 sa_proto_space_alias(char *proto, char *space)
391 struct sa_plugin_ops *ops = find_protocol(proto);
392 char *ret = space;
394 if (ops != NULL && ops->sa_space_alias != NULL)
395 ret = ops->sa_space_alias(space);
396 return (ret);
400 * sa_proto_security_prop(proto, token)
402 * Check to see if the property name in token is a valid named
403 * optionset property.
407 sa_proto_security_prop(char *proto, char *token)
409 struct sa_plugin_ops *ops = find_protocol(proto);
410 int ret = 0;
412 if (ops != NULL && ops->sa_security_prop != NULL)
413 ret = ops->sa_security_prop(token);
414 return (ret);
418 * sa_proto_legacy_opts(proto, grouup, options)
420 * Have the protocol specific parser parse the options string and add
421 * an appropriate optionset to group.
425 sa_proto_legacy_opts(char *proto, sa_group_t group, char *options)
427 struct sa_plugin_ops *ops = find_protocol(proto);
428 int ret = SA_INVALID_PROTOCOL;
430 if (ops != NULL && ops->sa_legacy_opts != NULL)
431 ret = ops->sa_legacy_opts(group, options);
432 return (ret);
436 * sa_proto_legacy_format(proto, group, hier)
438 * Return a legacy format string representing either the group's
439 * properties or the groups hierarchical properties.
442 char *
443 sa_proto_legacy_format(char *proto, sa_group_t group, int hier)
445 struct sa_plugin_ops *ops = find_protocol(proto);
446 char *ret = NULL;
448 if (ops != NULL && ops->sa_legacy_format != NULL)
449 ret = ops->sa_legacy_format(group, hier);
450 return (ret);
453 void
454 sa_format_free(char *str)
456 free(str);
460 * sharectl related API functions
464 * sa_proto_get_properties(proto)
466 * Return the set of properties that are specific to the
467 * protocol. These are usually in /etc/dfs/<proto> and related files,
468 * but only the protocol module knows which ones for sure.
471 sa_protocol_properties_t
472 sa_proto_get_properties(char *proto)
474 struct sa_plugin_ops *ops = find_protocol(proto);
475 sa_protocol_properties_t props = NULL;
477 if (ops != NULL && ops->sa_get_proto_set != NULL)
478 props = ops->sa_get_proto_set();
479 return (props);
483 * sa_proto_set_property(proto, prop)
485 * Update the protocol specific property.
489 sa_proto_set_property(char *proto, sa_property_t prop)
491 struct sa_plugin_ops *ops = find_protocol(proto);
492 int ret = SA_OK;
494 if (ops != NULL && ops->sa_set_proto_prop != NULL)
495 ret = ops->sa_set_proto_prop(prop);
496 return (ret);
500 * sa_valid_protocol(proto)
502 * Check to see if the protocol specified is defined by a
503 * plugin. Returns true (1) or false (0)
507 sa_valid_protocol(char *proto)
509 struct sa_plugin_ops *ops = find_protocol(proto);
510 return (ops != NULL);
514 * Return the current operational status of the protocol
517 char *
518 sa_get_protocol_status(char *proto)
520 struct sa_plugin_ops *ops = find_protocol(proto);
521 char *ret = NULL;
522 if (ops != NULL && ops->sa_get_proto_status != NULL)
523 ret = ops->sa_get_proto_status(proto);
524 return (ret);
528 * sa_proto_update_legacy(proto, share)
530 * Update the protocol specific legacy files if necessary for the
531 * specified share.
535 sa_proto_update_legacy(char *proto, sa_share_t share)
537 struct sa_plugin_ops *ops = find_protocol(proto);
538 int ret = SA_NOT_IMPLEMENTED;
540 if (ops != NULL) {
541 if (ops->sa_update_legacy != NULL)
542 ret = ops->sa_update_legacy(share);
544 return (ret);
548 * sa_delete_legacy(proto, share)
550 * Remove the specified share from the protocol specific legacy files.
554 sa_proto_delete_legacy(char *proto, sa_share_t share)
556 struct sa_plugin_ops *ops = find_protocol(proto);
557 int ret = SA_NOT_IMPLEMENTED;
559 if (ops != NULL) {
560 if (ops->sa_delete_legacy != NULL)
561 ret = ops->sa_delete_legacy(share);
562 } else {
563 if (proto != NULL)
564 ret = SA_NOT_IMPLEMENTED;
565 else
566 ret = SA_INVALID_PROTOCOL;
568 return (ret);
572 * sa_proto_delete_section(proto, section)
574 * Remove the specified section from the protocol specific legacy files,
575 * if supported.
579 sa_proto_delete_section(char *proto, char *section)
581 struct sa_plugin_ops *ops = find_protocol(proto);
582 int ret = SA_OK;
584 if (ops != NULL) {
585 if (ops->sa_delete_proto_section != NULL)
586 ret = ops->sa_delete_proto_section(section);
587 } else {
588 if (proto != NULL)
589 ret = SA_NOT_IMPLEMENTED;
590 else
591 ret = SA_INVALID_PROTOCOL;
593 return (ret);
597 * sa_proto_change_notify(share, char *protocol)
599 * Notify the protocol that a change has been made to the share
603 sa_proto_change_notify(sa_share_t share, char *proto)
605 struct sa_plugin_ops *ops = find_protocol(proto);
606 int ret = SA_NOT_IMPLEMENTED;
608 if (ops != NULL) {
609 if (ops->sa_change_notify != NULL)
610 ret = ops->sa_change_notify(share);
611 } else if (proto == NULL) {
613 ret = SA_INVALID_PROTOCOL;
615 return (ret);
619 * sa_proto_notify_resource(resource, char *protocol)
621 * Notify the protocol that a change has been made to the share
625 sa_proto_notify_resource(sa_resource_t resource, char *proto)
627 struct sa_plugin_ops *ops = find_protocol(proto);
628 int ret = SA_NOT_IMPLEMENTED;
630 if (ops != NULL) {
631 if (ops->sa_notify_resource != NULL)
632 ret = ops->sa_notify_resource(resource);
633 } else if (proto == NULL) {
634 ret = SA_INVALID_PROTOCOL;
636 return (ret);
640 * sa_proto_get_featureset(protocol)
642 * Get bitmask of defined features of the protocol. These are
643 * primarily things like SA_FEATURE_RESOURCE (shares are by resource
644 * name rather than path) and other operational features that affect
645 * behavior.
648 uint64_t
649 sa_proto_get_featureset(char *proto)
651 struct sa_plugin_ops *ops = find_protocol(proto);
652 uint64_t ret = 0;
654 if (ops != NULL) {
655 if (ops->sa_features != NULL)
656 ret = ops->sa_features();
658 /* if not implemented, zero is valid */
659 return (ret);
663 * sa_proto_get_transients(sa_handle_t)
665 * Called to get any protocol specific transient shares. NFS doesn't
666 * use this since the info is in sharetab which is processed as a
667 * common transient store.
669 * The protocol plugin should verify that the share isn't in the
670 * repository and then add it as a transient.
672 * Not having an entry is not a problem. It returns 0 in that case.
676 sa_proto_get_transients(sa_handle_t handle, char *proto)
678 struct sa_plugin_ops *ops = find_protocol(proto);
679 int ret = 0;
681 if (ops != NULL) {
682 if (ops->sa_get_transient_shares != NULL)
683 ret = ops->sa_get_transient_shares(handle);
685 return (ret);
689 * sa_proto_rename_resource(sa_handle_t, proto, sa_resource_t, newname)
691 * Protocols may need to know when a resource has changed names in
692 * order to notify clients. This must be done "before" the name in the
693 * resource has been changed. Not being implemented is not a problem.
697 sa_proto_rename_resource(sa_handle_t handle, char *proto,
698 sa_resource_t resource, char *newname)
700 struct sa_plugin_ops *ops = find_protocol(proto);
701 int ret = SA_OK;
703 if (ops != NULL) {
704 if (ops->sa_rename_resource != NULL)
705 ret = ops->sa_rename_resource(handle, resource,
706 newname);
708 return (ret);