17a286c4Dai Ngo/*
27a286c4Dai Ngo * CDDL HEADER START
37a286c4Dai Ngo *
47a286c4Dai Ngo * The contents of this file are subject to the terms of the
57a286c4Dai Ngo * Common Development and Distribution License (the "License").
67a286c4Dai Ngo * You may not use this file except in compliance with the License.
77a286c4Dai Ngo *
87a286c4Dai Ngo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97a286c4Dai Ngo * or http://www.opensolaris.org/os/licensing.
107a286c4Dai Ngo * See the License for the specific language governing permissions
117a286c4Dai Ngo * and limitations under the License.
127a286c4Dai Ngo *
137a286c4Dai Ngo * When distributing Covered Code, include this CDDL HEADER in each
147a286c4Dai Ngo * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157a286c4Dai Ngo * If applicable, add the following below this CDDL HEADER, with the
167a286c4Dai Ngo * fields enclosed by brackets "[]" replaced with your own identifying
177a286c4Dai Ngo * information: Portions Copyright [yyyy] [name of copyright owner]
187a286c4Dai Ngo *
197a286c4Dai Ngo * CDDL HEADER END
207a286c4Dai Ngo */
217a286c4Dai Ngo
227a286c4Dai Ngo/*
237a286c4Dai Ngo * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247a286c4Dai Ngo * Use is subject to license terms.
257a286c4Dai Ngo */
267a286c4Dai Ngo
27bcb78a4John Levon/*
28bcb78a4John Levon * Copyright (c) 2018, Joyent, Inc.
29bcb78a4John Levon */
30bcb78a4John Levon
317a286c4Dai Ngo#include <stdio.h>
327a286c4Dai Ngo#include <stdlib.h>
337a286c4Dai Ngo#include <unistd.h>
347a286c4Dai Ngo#include <strings.h>
357a286c4Dai Ngo#include <string.h>
367a286c4Dai Ngo#include <dirent.h>
377a286c4Dai Ngo#include <sys/types.h>
387a286c4Dai Ngo#include <sys/stat.h>
397a286c4Dai Ngo#include <sys/param.h>
407a286c4Dai Ngo#include <sys/errno.h>
417a286c4Dai Ngo#include <limits.h>
427a286c4Dai Ngo#include <libnvpair.h>
437a286c4Dai Ngo#include <dlfcn.h>
447a286c4Dai Ngo#include <libintl.h>
457a286c4Dai Ngo#include <sys/systeminfo.h>
467a286c4Dai Ngo#include <sys/fs_reparse.h>
477a286c4Dai Ngo#include "rp_plugin.h"
487a286c4Dai Ngo
497a286c4Dai Ngo#define	MAXISALEN	257	/* based on sysinfo(2) man page */
507a286c4Dai Ngo
517a286c4Dai Ngostatic rp_proto_handle_t rp_proto_handle;
527a286c4Dai Ngostatic rp_proto_plugin_t *rp_proto_list;
537a286c4Dai Ngo
547a286c4Dai Ngoint rp_plugin_init(void);
557a286c4Dai Ngostatic void proto_plugin_fini(void);
567a286c4Dai Ngostatic rp_plugin_ops_t *rp_find_protocol(const char *svctype);
577a286c4Dai Ngo
587a286c4Dai Ngoextern int errno;
597a286c4Dai Ngostatic int rp_plugin_inited = 0;
607a286c4Dai Ngo
617a286c4Dai Ngo/*
627a286c4Dai Ngo * reparse_create()
637a286c4Dai Ngo *
647a286c4Dai Ngo * Create a symlink at the specified 'path' as a reparse point.
657a286c4Dai Ngo * This function will fail if path refers to an existing file system
667a286c4Dai Ngo * object or an object named string already exists at the given path.
677a286c4Dai Ngo *
687a286c4Dai Ngo * return 0 if ok else return error code.
697a286c4Dai Ngo */
707a286c4Dai Ngoint
716a1af1aRichard Lowereparse_create(const char *path, const char *data)
727a286c4Dai Ngo{
737a286c4Dai Ngo	int err;
747a286c4Dai Ngo	struct stat sbuf;
757a286c4Dai Ngo
766a1af1aRichard Lowe	if (path == NULL || data == NULL)
777a286c4Dai Ngo		return (EINVAL);
787a286c4Dai Ngo
796a1af1aRichard Lowe	if ((err = reparse_validate(data)) != 0)
807a286c4Dai Ngo		return (err);
817a286c4Dai Ngo
827a286c4Dai Ngo	/* check if object exists */
837a286c4Dai Ngo	if (lstat(path, &sbuf) == 0)
847a286c4Dai Ngo		return (EEXIST);
857a286c4Dai Ngo
866a1af1aRichard Lowe	return (symlink(data, path) ? errno : 0);
877a286c4Dai Ngo}
887a286c4Dai Ngo
897a286c4Dai Ngo/*
907a286c4Dai Ngo * reparse_unparse()
917a286c4Dai Ngo *
927a286c4Dai Ngo * Convert an nvlist back to a string format suitable to write
937a286c4Dai Ngo * to the reparse point symlink body.  The string returned is in
947a286c4Dai Ngo * allocated memory and must be freed by the caller.
957a286c4Dai Ngo *
967a286c4Dai Ngo * return 0 if ok else return error code.
977a286c4Dai Ngo */
987a286c4Dai Ngoint
997a286c4Dai Ngoreparse_unparse(nvlist_t *nvl, char **stringp)
1007a286c4Dai Ngo{
1017a286c4Dai Ngo	int err, buflen;
1027a286c4Dai Ngo	char *buf, *stype, *val;
1037a286c4Dai Ngo	nvpair_t *curr;
1047a286c4Dai Ngo
1057a286c4Dai Ngo	if (nvl == NULL || stringp == NULL ||
1067a286c4Dai Ngo	    ((curr = nvlist_next_nvpair(nvl, NULL)) == NULL))
1077a286c4Dai Ngo		return (EINVAL);
1087a286c4Dai Ngo
1097a286c4Dai Ngo	buflen = SYMLINK_MAX;
1107a286c4Dai Ngo	if ((buf = malloc(buflen)) == NULL)
1117a286c4Dai Ngo		return (ENOMEM);
1127a286c4Dai Ngo
1137a286c4Dai Ngo	err = 0;
1147a286c4Dai Ngo	(void) snprintf(buf, buflen, "%s", FS_REPARSE_TAG_STR);
1157a286c4Dai Ngo	while (curr != NULL) {
1167a286c4Dai Ngo		if (!(stype = nvpair_name(curr))) {
1177a286c4Dai Ngo			err = EINVAL;
1187a286c4Dai Ngo			break;
1197a286c4Dai Ngo		}
1207a286c4Dai Ngo		if ((strlcat(buf, FS_TOKEN_START_STR, buflen) >= buflen) ||
1217a286c4Dai Ngo		    (strlcat(buf, stype, buflen) >= buflen) ||
1227a286c4Dai Ngo		    (strlcat(buf, ":", buflen) >= buflen) ||
1237a286c4Dai Ngo		    (nvpair_value_string(curr, &val) != 0) ||
1247a286c4Dai Ngo		    (strlcat(buf, val, buflen) >= buflen) ||
1257a286c4Dai Ngo		    (strlcat(buf, FS_TOKEN_END_STR, buflen) >= buflen)) {
1267a286c4Dai Ngo			err = E2BIG;
1277a286c4Dai Ngo			break;
1287a286c4Dai Ngo		}
1297a286c4Dai Ngo		curr = nvlist_next_nvpair(nvl, curr);
1307a286c4Dai Ngo	}
1317a286c4Dai Ngo	if (err != 0) {
1327a286c4Dai Ngo		free(buf);
1337a286c4Dai Ngo		return (err);
1347a286c4Dai Ngo	}
1357a286c4Dai Ngo	if (strlcat(buf, FS_REPARSE_TAG_END_STR, buflen) >= buflen) {
1367a286c4Dai Ngo		free(buf);
1377a286c4Dai Ngo		return (E2BIG);
1387a286c4Dai Ngo	}
1397a286c4Dai Ngo
1407a286c4Dai Ngo	*stringp = buf;
1417a286c4Dai Ngo	return (0);
1427a286c4Dai Ngo}
1437a286c4Dai Ngo
1447a286c4Dai Ngo/*
1457a286c4Dai Ngo * reparse_deref()
1467a286c4Dai Ngo *
1477a286c4Dai Ngo * Accepts the service-specific item from the reparse point and returns
1487a286c4Dai Ngo * the service-specific data requested.  The caller specifies the size
1497a286c4Dai Ngo * of the buffer provided via *bufsz.
1507a286c4Dai Ngo *
1517a286c4Dai Ngo * if ok return 0 and *bufsz is updated to contain the actual length of
1527a286c4Dai Ngo * the returned results, else return error code. If the error code is
1537a286c4Dai Ngo * EOVERFLOW; results do not fit in the buffer, *bufsz will be updated
1547a286c4Dai Ngo * to contain the number of bytes needed to hold the results.
1557a286c4Dai Ngo */
1567a286c4Dai Ngoint
1577a286c4Dai Ngoreparse_deref(const char *svc_type, const char *svc_data, char *buf,
1587a286c4Dai Ngo    size_t *bufsz)
1597a286c4Dai Ngo{
1607a286c4Dai Ngo	rp_plugin_ops_t *ops;
1617a286c4Dai Ngo
1627a286c4Dai Ngo	if ((svc_type == NULL) || (svc_data == NULL) || (buf == NULL) ||
1637a286c4Dai Ngo	    (bufsz == NULL))
1647a286c4Dai Ngo		return (EINVAL);
1657a286c4Dai Ngo
1667a286c4Dai Ngo	ops = rp_find_protocol(svc_type);
1677a286c4Dai Ngo	if ((ops != NULL) && (ops->rpo_deref != NULL))
1687a286c4Dai Ngo		return (ops->rpo_deref(svc_type, svc_data, buf, bufsz));
1697a286c4Dai Ngo
1707a286c4Dai Ngo	/* no plugin, return error */
1717a286c4Dai Ngo	return (ENOTSUP);
1727a286c4Dai Ngo}
1737a286c4Dai Ngo
1747a286c4Dai Ngo/*
1757a286c4Dai Ngo * reparse_delete()
1767a286c4Dai Ngo *
1777a286c4Dai Ngo * Delete a reparse point at a given pathname.  It will fail if
1787a286c4Dai Ngo * a reparse point does not exist at the given path or the pathname
1797a286c4Dai Ngo * is not a symlink.
1807a286c4Dai Ngo *
1817a286c4Dai Ngo * return 0 if ok else return error code.
1827a286c4Dai Ngo */
1837a286c4Dai Ngoint
1847a286c4Dai Ngoreparse_delete(const char *path)
1857a286c4Dai Ngo{
1867a286c4Dai Ngo	struct stat sbuf;
1877a286c4Dai Ngo
1887a286c4Dai Ngo	if (path == NULL)
1897a286c4Dai Ngo		return (EINVAL);
1907a286c4Dai Ngo
1917a286c4Dai Ngo	/* check if object exists */
1927a286c4Dai Ngo	if (lstat(path, &sbuf) != 0)
1937a286c4Dai Ngo		return (errno);
1947a286c4Dai Ngo
1957a286c4Dai Ngo	if ((sbuf.st_mode & S_IFLNK) != S_IFLNK)
1967a286c4Dai Ngo		return (EINVAL);
1977a286c4Dai Ngo
1987a286c4Dai Ngo	return (unlink(path) ? errno : 0);
1997a286c4Dai Ngo}
2007a286c4Dai Ngo
2017a286c4Dai Ngo/*
2027a286c4Dai Ngo * reparse_add()
2037a286c4Dai Ngo *
2047a286c4Dai Ngo * Add a service type entry to a nvlist with a copy of svc_data,
2057a286c4Dai Ngo * replacing one of the same type if already present.
2067a286c4Dai Ngo *
2077a286c4Dai Ngo * return 0 if ok else return error code.
2087a286c4Dai Ngo */
2097a286c4Dai Ngoint
2107a286c4Dai Ngoreparse_add(nvlist_t *nvl, const char *svc_type, const char *svc_data)
2117a286c4Dai Ngo{
2127a286c4Dai Ngo	int err;
2137a286c4Dai Ngo	char *buf;
2147a286c4Dai Ngo	size_t bufsz;
2157a286c4Dai Ngo	rp_plugin_ops_t *ops;
2167a286c4Dai Ngo
2177a286c4Dai Ngo	if ((nvl == NULL) || (svc_type == NULL) || (svc_data == NULL))
2187a286c4Dai Ngo		return (EINVAL);
2197a286c4Dai Ngo
2207a286c4Dai Ngo	bufsz = SYMLINK_MAX;		/* no need to mess around */
2217a286c4Dai Ngo	if ((buf = malloc(bufsz)) == NULL)
2227a286c4Dai Ngo		return (ENOMEM);
2237a286c4Dai Ngo
2247a286c4Dai Ngo	ops = rp_find_protocol(svc_type);
2257a286c4Dai Ngo	if ((ops != NULL) && (ops->rpo_form != NULL))
2267a286c4Dai Ngo		err = ops->rpo_form(svc_type, svc_data, buf, &bufsz);
2277a286c4Dai Ngo	else
2287a286c4Dai Ngo		err = ENOTSUP;		/* no plugin */
2297a286c4Dai Ngo
2307a286c4Dai Ngo	if (err != 0) {
2317a286c4Dai Ngo		free(buf);
2327a286c4Dai Ngo		return (err);
2337a286c4Dai Ngo	}
2347a286c4Dai Ngo
2357a286c4Dai Ngo	err =  nvlist_add_string(nvl, svc_type, buf);
2367a286c4Dai Ngo	free(buf);
2377a286c4Dai Ngo	return (err);
2387a286c4Dai Ngo}
2397a286c4Dai Ngo
2407a286c4Dai Ngo/*
2417a286c4Dai Ngo * reparse_remove()
2427a286c4Dai Ngo *
2437a286c4Dai Ngo * Remove a service type entry from the nvlist, if present.
2447a286c4Dai Ngo *
2457a286c4Dai Ngo * return 0 if ok else return error code.
2467a286c4Dai Ngo */
2477a286c4Dai Ngoint
2487a286c4Dai Ngoreparse_remove(nvlist_t *nvl, const char *svc_type)
2497a286c4Dai Ngo{
2507a286c4Dai Ngo	if ((nvl == NULL) || (svc_type == NULL))
2517a286c4Dai Ngo		return (EINVAL);
2527a286c4Dai Ngo
2537a286c4Dai Ngo	return (nvlist_remove_all(nvl, svc_type));
2547a286c4Dai Ngo}
2557a286c4Dai Ngo
2567a286c4Dai Ngo/*
2577a286c4Dai Ngo * Returns true if name is "." or "..", otherwise returns false.
2587a286c4Dai Ngo */
2597a286c4Dai Ngostatic boolean_t
2607a286c4Dai Ngorp_is_dot_or_dotdot(const char *name)
2617a286c4Dai Ngo{
2627a286c4Dai Ngo	if (*name != '.')
2637a286c4Dai Ngo		return (B_FALSE);
2647a286c4Dai Ngo
2657a286c4Dai Ngo	if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))
2667a286c4Dai Ngo		return (B_TRUE);
2677a286c4Dai Ngo
2687a286c4Dai Ngo	return (B_FALSE);
2697a286c4Dai Ngo}
2707a286c4Dai Ngo
2717a286c4Dai Ngostatic void
2727a286c4Dai Ngoproto_plugin_fini()
2737a286c4Dai Ngo{
2747a286c4Dai Ngo	rp_proto_plugin_t *p;
2757a286c4Dai Ngo
2767a286c4Dai Ngo	/*
2777a286c4Dai Ngo	 * Protocols may call this framework during _fini
2787a286c4Dai Ngo	 */
2797a286c4Dai Ngo	for (p = rp_proto_list; p != NULL; p = p->plugin_next) {
2807a286c4Dai Ngo		if (p->plugin_ops->rpo_fini)
281bcb78a4John Levon			(void) p->plugin_ops->rpo_fini();
2827a286c4Dai Ngo	}
2837a286c4Dai Ngo	while ((p = rp_proto_list) != NULL) {
2847a286c4Dai Ngo		rp_proto_list = p->plugin_next;
2857a286c4Dai Ngo		if (p->plugin_handle != NULL)
2867a286c4Dai Ngo			(void) dlclose(p->plugin_handle);
2877a286c4Dai Ngo		free(p);
2887a286c4Dai Ngo	}
2897a286c4Dai Ngo
2907a286c4Dai Ngo	if (rp_proto_handle.rp_ops != NULL) {
2917a286c4Dai Ngo		free(rp_proto_handle.rp_ops);
2927a286c4Dai Ngo		rp_proto_handle.rp_ops = NULL;
2937a286c4Dai Ngo	}
2947a286c4Dai Ngo	rp_proto_handle.rp_num_proto = 0;
2957a286c4Dai Ngo}
2967a286c4Dai Ngo
2977a286c4Dai Ngo/*
2987a286c4Dai Ngo * rp_plugin_init()
2997a286c4Dai Ngo *
3007a286c4Dai Ngo * Initialize the service type specific plugin modules.
3017a286c4Dai Ngo * For each reparse service type, there should be a plugin library for it.
3027a286c4Dai Ngo * This function walks /usr/lib/reparse directory for plugin libraries.
3037a286c4Dai Ngo * For each plugin library found, initialize it and add it to the internal
3047a286c4Dai Ngo * list of service type plugin. These are used for service type specific
3057a286c4Dai Ngo * operations.
3067a286c4Dai Ngo */
3077a286c4Dai Ngoint
3087a286c4Dai Ngorp_plugin_init()
3097a286c4Dai Ngo{
3107a286c4Dai Ngo	int err, ret = RP_OK;
3117a286c4Dai Ngo	char isa[MAXISALEN], dirpath[MAXPATHLEN], path[MAXPATHLEN];
3127a286c4Dai Ngo	int num_protos = 0;
3137a286c4Dai Ngo	rp_proto_handle_t *rp_hdl;
3147a286c4Dai Ngo	rp_proto_plugin_t *proto, *tmp;
3157a286c4Dai Ngo	rp_plugin_ops_t *plugin_ops;
3167a286c4Dai Ngo	struct stat st;
3177a286c4Dai Ngo	void *dlhandle;
3187a286c4Dai Ngo	DIR *dir;
3197a286c4Dai Ngo	struct dirent *dent;
3207a286c4Dai Ngo
3217a286c4Dai Ngo#if defined(_LP64)
3227a286c4Dai Ngo	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
3237a286c4Dai Ngo		isa[0] = '\0';
3247a286c4Dai Ngo#else
3257a286c4Dai Ngo	isa[0] = '\0';
3267a286c4Dai Ngo#endif
3277a286c4Dai Ngo
3287a286c4Dai Ngo	(void) snprintf(dirpath, MAXPATHLEN,
3297a286c4Dai Ngo	    "%s/%s", RP_LIB_DIR, isa);
3307a286c4Dai Ngo
3317a286c4Dai Ngo	if ((dir = opendir(dirpath)) == NULL)
3327a286c4Dai Ngo		return (RP_NO_PLUGIN_DIR);
3337a286c4Dai Ngo
3347a286c4Dai Ngo	while ((dent = readdir(dir)) != NULL) {
3357a286c4Dai Ngo		if (rp_is_dot_or_dotdot(dent->d_name))
3367a286c4Dai Ngo			continue;
3377a286c4Dai Ngo
3387a286c4Dai Ngo		(void) snprintf(path, MAXPATHLEN,
3397a286c4Dai Ngo		    "%s/%s", dirpath, dent->d_name);
3407a286c4Dai Ngo
3417a286c4Dai Ngo		/*
3427a286c4Dai Ngo		 * If file doesn't exist, don't try to map it
3437a286c4Dai Ngo		 */
3447a286c4Dai Ngo		if (stat(path, &st) < 0)
3457a286c4Dai Ngo			continue;
3467a286c4Dai Ngo		if ((dlhandle = dlopen(path, RTLD_FIRST|RTLD_LAZY)) == NULL)
3477a286c4Dai Ngo			continue;
3487a286c4Dai Ngo
3497a286c4Dai Ngo		plugin_ops = (rp_plugin_ops_t *)
3507a286c4Dai Ngo		    dlsym(dlhandle, "rp_plugin_ops");
3517a286c4Dai Ngo		if (plugin_ops == NULL) {
3527a286c4Dai Ngo			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3537a286c4Dai Ngo			    "Error in plugin ops for service type %s\n%s\n"),
3547a286c4Dai Ngo			    dent->d_name, dlerror());
3557a286c4Dai Ngo			(void) dlclose(dlhandle);
3567a286c4Dai Ngo			continue;
3577a286c4Dai Ngo		}
3587a286c4Dai Ngo		proto = (rp_proto_plugin_t *)
3597a286c4Dai Ngo		    calloc(1, sizeof (rp_proto_plugin_t));
3607a286c4Dai Ngo		if (proto == NULL) {
3617a286c4Dai Ngo			(void) dlclose(dlhandle);
3627a286c4Dai Ngo			(void) fprintf(stderr,
3637a286c4Dai Ngo			    dgettext(TEXT_DOMAIN, "No memory for plugin %s\n"),
3647a286c4Dai Ngo			    dent->d_name);
3657a286c4Dai Ngo			ret = RP_NO_MEMORY;
3667a286c4Dai Ngo			break;
3677a286c4Dai Ngo		}
3687a286c4Dai Ngo
3697a286c4Dai Ngo		proto->plugin_ops = plugin_ops;
3707a286c4Dai Ngo		proto->plugin_handle = dlhandle;
3717a286c4Dai Ngo		num_protos++;
3727a286c4Dai Ngo		proto->plugin_next = rp_proto_list;
3737a286c4Dai Ngo		rp_proto_list = proto;
3747a286c4Dai Ngo	}
3757a286c4Dai Ngo
3767a286c4Dai Ngo	(void) closedir(dir);
3777a286c4Dai Ngo
3787a286c4Dai Ngo	if ((num_protos == 0) && (ret == 0))
3797a286c4Dai Ngo		ret = RP_NO_PLUGIN;
3807a286c4Dai Ngo	/*
3817a286c4Dai Ngo	 * There was an error, so cleanup prior to return of failure.
3827a286c4Dai Ngo	 */
3837a286c4Dai Ngo	if (ret != RP_OK) {
3847a286c4Dai Ngo		proto_plugin_fini();
3857a286c4Dai Ngo		return (ret);
3867a286c4Dai Ngo	}
3877a286c4Dai Ngo
3887a286c4Dai Ngo	rp_proto_handle.rp_ops = (rp_plugin_ops_t **)calloc(num_protos,
3897a286c4Dai Ngo	    sizeof (rp_plugin_ops_t *));
3907a286c4Dai Ngo	if (!rp_proto_handle.rp_ops) {
3917a286c4Dai Ngo		proto_plugin_fini();
3927a286c4Dai Ngo		return (RP_NO_MEMORY);
3937a286c4Dai Ngo	}
3947a286c4Dai Ngo
3957a286c4Dai Ngo	rp_hdl = &rp_proto_handle;
3967a286c4Dai Ngo	rp_hdl->rp_num_proto = 0;
3977a286c4Dai Ngo	for (tmp = rp_proto_list; rp_hdl->rp_num_proto < num_protos &&
3987a286c4Dai Ngo	    tmp != NULL; tmp = tmp->plugin_next) {
3997a286c4Dai Ngo
4007a286c4Dai Ngo		err = RP_OK;
4017a286c4Dai Ngo		if (tmp->plugin_ops->rpo_init != NULL)
4027a286c4Dai Ngo			err = tmp->plugin_ops->rpo_init();
4037a286c4Dai Ngo		if (err != RP_OK)
4047a286c4Dai Ngo			continue;
4057a286c4Dai Ngo		rp_hdl->rp_ops[rp_hdl->rp_num_proto++] = tmp->plugin_ops;
4067a286c4Dai Ngo	}
4077a286c4Dai Ngo
4087a286c4Dai Ngo	return (rp_hdl->rp_num_proto > 0 ? RP_OK : RP_NO_PLUGIN);
4097a286c4Dai Ngo}
4107a286c4Dai Ngo
4117a286c4Dai Ngo
4127a286c4Dai Ngo/*
4137a286c4Dai Ngo * find_protocol()
4147a286c4Dai Ngo *
4157a286c4Dai Ngo * Search the plugin list for the specified protocol and return the
4167a286c4Dai Ngo * ops vector.  return NULL if protocol is not defined.
4177a286c4Dai Ngo */
4187a286c4Dai Ngostatic rp_plugin_ops_t *
4197a286c4Dai Ngorp_find_protocol(const char *svc_type)
4207a286c4Dai Ngo{
4217a286c4Dai Ngo	int i;
4227a286c4Dai Ngo	rp_plugin_ops_t *ops = NULL;
4237a286c4Dai Ngo
4247a286c4Dai Ngo	if (svc_type == NULL)
4257a286c4Dai Ngo		return (NULL);
4267a286c4Dai Ngo
4277a286c4Dai Ngo	if (rp_plugin_inited == 0) {
4287a286c4Dai Ngo		if (rp_plugin_init() == RP_OK)
4297a286c4Dai Ngo			rp_plugin_inited = 1;
4307a286c4Dai Ngo		else
4317a286c4Dai Ngo			return (NULL);
4327a286c4Dai Ngo	}
4337a286c4Dai Ngo
4347a286c4Dai Ngo	for (i = 0; i < rp_proto_handle.rp_num_proto; i++) {
4357a286c4Dai Ngo		ops = rp_proto_handle.rp_ops[i];
4367a286c4Dai Ngo		if (ops->rpo_supports_svc(svc_type))
4377a286c4Dai Ngo			return (ops);
4387a286c4Dai Ngo
4397a286c4Dai Ngo	}
4407a286c4Dai Ngo	return (NULL);
4417a286c4Dai Ngo}
442