summaryrefslogtreecommitdiff
path: root/usr/src/lib/libds/common/libds.c
blob: 274b90132ac230acb2692672357f01a9f8e0422e (plain)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/*
 * 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 <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/sysevent.h>
#include <libsysevent.h>
#include <sys/vlds.h>
#include "libds.h"

#define	PTRTOUINT64(ptr)	((uint64_t)((uintptr_t)(ptr)))
static char vlds_device[] =
	"/devices/virtual-devices@100/channel-devices@200/"
	"virtual-domain-service@0:vlds";

typedef struct dslibentry {
	ds_hdl_t dsl_hdl;
	uint32_t dsl_flags;
	uint32_t dsl_tflags;
	char *dsl_service;
	ds_ops_t dsl_ops;
} dslibentry_t;

/* dsl_tflags */
#define	DSL_ENTRY_INUSE		0x0001	/* handle is currently active */

#define	MIN_DSLIB_ENTRIES	64
static dslibentry_t *dslibtab;
static int ndslib;

/*
 * Lock to protect the dslibtab table.  We only need to protect this
 * table for those functions which actually look at or modify the table:
 * service registration (ds_svc_reg/ds_clnt_reg), service unregistration
 * (ds_hdl_unreg) or during callbacks (ds_recv)
 */
static mutex_t dslib_lock;

static int ds_fd = -1;

static char *ds_sid_name = "vlds";

static evchan_t *ds_evchan;

/*
 * Static functions internal to dslib.
 */
static dslibentry_t *ds_hdl_to_dslibentry(ds_hdl_t hdl);
static dslibentry_t *ds_new_dslibentry(void);
static uint_t ds_service_count(char *service, boolean_t is_client);
static dslibentry_t *ds_lookup_dslibentry(char *service, boolean_t is_client);
static dslibentry_t *ds_register_dslibentry(ds_hdl_t hdl, char *service,
    boolean_t is_client);
static void ds_free_dslibentry(dslibentry_t *dsp, int force_unreg);
static int ds_recv(sysevent_t *sep, void *arg);
static void ds_string_arg(vlds_string_t *dsp, char *str);
static int ds_register(ds_capability_t *cap, ds_ops_t *ops, uint_t flags);

static dslibentry_t *
ds_hdl_to_dslibentry(ds_hdl_t hdl)
{
	int i;
	dslibentry_t *dsp;

	for (i = 0, dsp = dslibtab; i < ndslib; i++, dsp++) {
		if (hdl == dsp->dsl_hdl)
			return (dsp);
	}
	return (NULL);
}

static dslibentry_t *
ds_new_dslibentry(void)
{
	int newndslib;
	dslibentry_t *dsp;

	if ((dsp = ds_hdl_to_dslibentry(NULL)) != NULL)
		return (dsp);

	/* double the size */
	newndslib = ndslib << 1;
	if ((dslibtab = realloc(dslibtab, newndslib * sizeof (dslibentry_t)))
	    == NULL)
		return (NULL);
	dsp = &dslibtab[ndslib];
	(void) memset(dsp, 0, (newndslib - ndslib) * sizeof (dslibentry_t));
	ndslib = newndslib;
	return (dsp);
}

static uint_t
ds_service_count(char *service, boolean_t is_client)
{
	int i;
	dslibentry_t *dsp;
	uint_t is_client_flag = is_client ? VLDS_REG_CLIENT : 0;
	uint_t count = 0;

	for (i = 0, dsp = dslibtab; i < ndslib; i++, dsp++) {
		if (dsp->dsl_hdl != NULL &&
		    strcmp(dsp->dsl_service, service) == 0 &&
		    (dsp->dsl_flags & VLDS_REG_CLIENT) == is_client_flag) {
			count++;
		}
	}
	return (count);
}

static dslibentry_t *
ds_lookup_dslibentry(char *service, boolean_t is_client)
{
	int i;
	dslibentry_t *dsp;
	uint_t is_client_flag = is_client ? VLDS_REG_CLIENT : 0;

	for (i = 0, dsp = dslibtab; i < ndslib; i++, dsp++) {
		if (dsp->dsl_hdl != NULL &&
		    strcmp(dsp->dsl_service, service) == 0 &&
		    (dsp->dsl_flags & VLDS_REG_CLIENT) == is_client_flag) {
			return (dsp);
		}
	}
	return (NULL);
}

static dslibentry_t *
ds_register_dslibentry(ds_hdl_t hdl, char *service, boolean_t is_client)
{
	dslibentry_t *dsp, *orig_dsp;

	if ((dsp = ds_hdl_to_dslibentry(hdl)) != NULL) {
		dsp->dsl_tflags |= DSL_ENTRY_INUSE;
		return (dsp);
	}

	if ((orig_dsp = ds_lookup_dslibentry(service, is_client)) == NULL) {
		return (NULL);
	}

	if ((orig_dsp->dsl_tflags & DSL_ENTRY_INUSE) == 0) {
		/* use the original structure entry */
		orig_dsp->dsl_tflags |= DSL_ENTRY_INUSE;
		orig_dsp->dsl_hdl = hdl;
		return (orig_dsp);
	}

	/* allocate a new structure entry */
	if ((dsp = ds_new_dslibentry()) == NULL)
		return (NULL);

	*dsp = *orig_dsp;
	dsp->dsl_service = strdup(orig_dsp->dsl_service);
	dsp->dsl_hdl = hdl;
	return (dsp);
}

/*
 * Want to leave an entry in the dslib table even though all the
 * handles may have been unregistered for it.
 */
static void
ds_free_dslibentry(dslibentry_t *dsp, int force_unreg)
{
	uint_t nhdls;

	/*
	 * Find out if we have 1 or 2 or more handles for the given
	 * service.  Having one implies that we want to leave the entry
	 * intact but marked as not in use unless this is a ds_unreg_hdl
	 * (force_unreg is true).
	 */
	nhdls = ds_service_count(dsp->dsl_service,
	    (dsp->dsl_flags & VLDS_REG_CLIENT) != 0);

	if ((nhdls == 1 && force_unreg) || nhdls >= 2) {
		dsp->dsl_hdl = NULL;
		if (dsp->dsl_service) {
			free(dsp->dsl_service);
		}
		(void) memset(dsp, 0, sizeof (dslibentry_t));
	} else if (nhdls == 1) {
		dsp->dsl_tflags &= ~DSL_ENTRY_INUSE;
	}
}

/*ARGSUSED*/
static int
ds_recv(sysevent_t *sep, void *arg)
{
	nvlist_t *nvl;
	uint64_t hdl;
	ds_ver_t ver;
	ds_domain_hdl_t dhdl;
	uchar_t *bufp;
	boolean_t is_client;
	uint_t buflen;
	char *subclass;
	char *servicep;
	dslibentry_t *dsp;
	ds_cb_arg_t cb_arg;

	subclass = sysevent_get_subclass_name(sep);
	if (sysevent_get_attr_list(sep, &nvl) != 0) {
		return (0);
	}

	if (nvlist_lookup_uint64(nvl, VLDS_HDL, &hdl) == 0) {
		if (strcmp(subclass, ESC_VLDS_REGISTER) == 0) {
			void (*reg_cb)(ds_hdl_t, ds_cb_arg_t, ds_ver_t *,
			    ds_domain_hdl_t) = NULL;

			if (nvlist_lookup_string(nvl, VLDS_SERVICE_ID,
			    &servicep) == 0 &&
			    nvlist_lookup_boolean_value(nvl, VLDS_ISCLIENT,
			    &is_client) == 0) {
				(void) mutex_lock(&dslib_lock);
				if ((dsp = ds_register_dslibentry(hdl,
				    servicep, is_client)) != NULL) {
					reg_cb = dsp->dsl_ops.ds_reg_cb;
					cb_arg = dsp->dsl_ops.cb_arg;
				}
				(void) mutex_unlock(&dslib_lock);
				if (reg_cb != NULL &&
				    nvlist_lookup_uint64(nvl, VLDS_DOMAIN_HDL,
				    &dhdl) == 0 &&
				    nvlist_lookup_uint16(nvl, VLDS_VER_MAJOR,
				    &ver.major) == 0 &&
				    nvlist_lookup_uint16(nvl, VLDS_VER_MINOR,
				    &ver.minor) == 0) {
					(reg_cb)((ds_hdl_t)hdl, cb_arg, &ver,
					    dhdl);
				}
			}
		} else if (strcmp(subclass, ESC_VLDS_UNREGISTER) == 0) {
			void (*unreg_cb)(ds_hdl_t, ds_cb_arg_t) = NULL;

			(void) mutex_lock(&dslib_lock);
			if ((dsp = ds_hdl_to_dslibentry(hdl)) != NULL) {
				unreg_cb = dsp->dsl_ops.ds_unreg_cb;
				cb_arg = dsp->dsl_ops.cb_arg;
				ds_free_dslibentry(dsp, 0);
			}
			(void) mutex_unlock(&dslib_lock);
			if (unreg_cb != NULL) {
				(unreg_cb)((ds_hdl_t)hdl, cb_arg);
			}
		} else if (strcmp(subclass, ESC_VLDS_DATA) == 0) {
			void (*data_cb)(ds_hdl_t, ds_cb_arg_t, void *,
			    size_t) = NULL;

			(void) mutex_lock(&dslib_lock);
			if ((dsp = ds_hdl_to_dslibentry(hdl)) != NULL) {
				data_cb = dsp->dsl_ops.ds_data_cb;
				cb_arg = dsp->dsl_ops.cb_arg;
			}
			(void) mutex_unlock(&dslib_lock);
			if (data_cb != NULL &&
			    nvlist_lookup_byte_array(nvl, VLDS_DATA, &bufp,
			    &buflen) == 0) {
				(data_cb)((ds_hdl_t)hdl, cb_arg, bufp, buflen);
			}
		}
	}
	nvlist_free(nvl);
	return (0);
}

static void
ds_string_arg(vlds_string_t *dsp, char *str)
{
	if (str == NULL) {
		dsp->vlds_strp = NULL;
		dsp->vlds_strlen = 0;
	} else {
		dsp->vlds_strp = PTRTOUINT64(str);
		dsp->vlds_strlen = strlen(str) + 1;
	}
}

static int
ds_init_sysev(void)
{
	char evchan_name[MAX_CHNAME_LEN];

	(void) sprintf(evchan_name, VLDS_SYSEV_CHAN_FMT, (int)getpid());
	if (sysevent_evc_bind(evchan_name, &ds_evchan, 0) != 0) {
		return (errno);
	}
	if (sysevent_evc_subscribe(ds_evchan, ds_sid_name, EC_VLDS,
	    ds_recv, NULL, 0) != 0) {
		(void) sysevent_evc_unbind(ds_evchan);
		ds_evchan = NULL;
		return (errno);
	}
	return (0);
}

int
ds_init(void)
{
	if (ds_fd >= 0)
		return (0);

	if ((ds_fd = open(vlds_device, 0)) < 0)
		return (errno);

	if (dslibtab == NULL) {
		dslibtab = malloc(sizeof (dslibentry_t) * MIN_DSLIB_ENTRIES);
		if (dslibtab == NULL)
			return (errno = ENOMEM);
		ndslib = MIN_DSLIB_ENTRIES;
		(void) memset(dslibtab, 0, sizeof (dslibentry_t) * ndslib);
	}

	(void) mutex_init(&dslib_lock, USYNC_THREAD, NULL);
	return (0);
}

static int
ds_register(ds_capability_t *cap, ds_ops_t *ops, uint_t flags)
{
	dslibentry_t *dsp;
	vlds_svc_reg_arg_t vlds_arg;
	vlds_cap_t vlds_cap;
	vlds_ver_t vlds_vers[VLDS_MAX_VERS];
	uint64_t hdl_arg;
	ds_hdl_t hdl;
	uint_t nhdls;
	int i;

	if (cap == NULL || ops == NULL || cap->svc_id == NULL ||
	    cap->vers == NULL || (flags & (~VLDS_REG_CLIENT)) != 0) {
		return (errno = EINVAL);
	}

	if (cap->nvers > VLDS_MAX_VERS) {
		return (errno = EINVAL);
	}

	if (ds_fd < 0 && (errno = ds_init()) != 0) {
		return (errno);
	}

	if (ds_hdl_lookup(cap->svc_id, (flags & VLDS_REG_CLIENT), NULL, 1,
	    &nhdls) == 0 && nhdls == 1) {
		return (errno = EALREADY);
	}

	(void) mutex_lock(&dslib_lock);
	if ((dsp = ds_new_dslibentry()) == NULL) {
		(void) mutex_unlock(&dslib_lock);
		return (errno = ENOMEM);
	}

	/* Setup device driver capability structure. */

	/* service string */
	ds_string_arg(&vlds_cap.vlds_service, cap->svc_id);

	/* version array */
	for (i = 0; i < cap->nvers; i++) {
		vlds_vers[i].vlds_major = cap->vers[i].major;
		vlds_vers[i].vlds_minor = cap->vers[i].minor;
	}
	vlds_cap.vlds_versp = PTRTOUINT64(vlds_vers);
	vlds_cap.vlds_nver = cap->nvers;

	/*
	 * Format args for VLDS_SVC_REG ioctl.
	 */

	vlds_arg.vlds_capp = PTRTOUINT64(&vlds_cap);

	/* op flags */
	if (ops->ds_reg_cb != NULL)
		flags |= VLDS_REGCB_VALID;
	if (ops->ds_unreg_cb != NULL)
		flags |= VLDS_UNREGCB_VALID;
	if (ops->ds_data_cb != NULL)
		flags |= VLDS_DATACB_VALID;
	vlds_arg.vlds_reg_flags = flags;

	/* returned handle */
	vlds_arg.vlds_hdlp = PTRTOUINT64(&hdl_arg);

	if (ioctl(ds_fd, VLDS_SVC_REG, &vlds_arg) < 0) {
		(void) mutex_unlock(&dslib_lock);
		return (errno);
	}

	/*
	 * Setup user callback sysevent channel.
	 */
	if ((flags & VLDS_ANYCB_VALID) != 0 && ds_evchan == NULL &&
	    ds_init_sysev() != 0) {
		(void) mutex_unlock(&dslib_lock);
		(void) ioctl(ds_fd, VLDS_UNREG_HDL, &vlds_arg);
		return (errno);
	}

	hdl = hdl_arg;

	/*
	 * Set entry values in dslibtab.
	 */
	dsp->dsl_hdl = hdl;
	dsp->dsl_flags = flags;
	dsp->dsl_tflags = 0;
	dsp->dsl_service = strdup(cap->svc_id);
	dsp->dsl_ops = *ops;
	(void) mutex_unlock(&dslib_lock);
	return (0);
}

/*
 * Registers a service provider.  Kicks off the handshake with other
 * domain(s) to announce servce.  Callback events are as described above.
 */
int
ds_svc_reg(ds_capability_t *cap, ds_ops_t *ops)
{
	return (ds_register(cap, ops, 0));
}

/*
 * Registers interest in a service from a specific domain.  When that
 * service is registered, the register callback is invoked.  When that
 * service is unregistered, the unregister callback is invoked.  When
 * data is received, the receive data callback is invoked.
 */
int
ds_clnt_reg(ds_capability_t *cap, ds_ops_t *ops)
{
	return (ds_register(cap, ops, VLDS_REG_CLIENT));
}

/*
 * Given a service name and type, returns the existing handle(s), if
 * one or more exist.  This could be used to poll for the connection being
 * registered or unregistered, rather than using the register/unregister
 * callbacks.
 */
int
ds_hdl_lookup(char *service, boolean_t is_client, ds_hdl_t *hdlsp,
    uint_t maxhdls, uint_t *nhdlsp)
{
	vlds_hdl_lookup_arg_t vlds_arg;
	uint64_t nhdls_arg;

	errno = 0;
	if (ds_fd < 0) {
		return (errno = EBADF);
	}

	if (service == NULL) {
		return (errno = EINVAL);
	}

	ds_string_arg(&vlds_arg.vlds_service, service);
	vlds_arg.vlds_isclient = is_client ? VLDS_REG_CLIENT : 0;
	vlds_arg.vlds_hdlsp = PTRTOUINT64(hdlsp);
	vlds_arg.vlds_maxhdls = maxhdls;
	vlds_arg.vlds_nhdlsp = PTRTOUINT64(&nhdls_arg);

	if (ioctl(ds_fd, VLDS_HDL_LOOKUP, &vlds_arg) < 0) {
		return (errno);
	}

	*nhdlsp = nhdls_arg;
	return (0);
}

/*
 * Given a handle, return its associated domain.
 */
int
ds_domain_lookup(ds_hdl_t hdl, ds_domain_hdl_t *dhdlp)
{
	vlds_dmn_lookup_arg_t vlds_arg;
	uint64_t dhdl_arg;

	if (ds_fd < 0) {
		return (errno = EBADF);
	}

	vlds_arg.vlds_hdl = hdl;
	vlds_arg.vlds_dhdlp = PTRTOUINT64(&dhdl_arg);

	if (ioctl(ds_fd, VLDS_DMN_LOOKUP, &vlds_arg) < 0) {
		return (errno);
	}

	if (dhdlp) {
		*dhdlp = dhdl_arg;
	}

	return (0);
}

/*
 * Unregisters either a service or an interest in that service
 * indicated by the supplied handle.
 */
int
ds_unreg_hdl(ds_hdl_t hdl)
{
	dslibentry_t *dsp;
	vlds_unreg_hdl_arg_t vlds_arg;

	(void) mutex_lock(&dslib_lock);
	if ((dsp = ds_hdl_to_dslibentry(hdl)) != NULL) {
		ds_free_dslibentry(dsp, 1);
	}
	(void) mutex_unlock(&dslib_lock);

	if (ds_fd >= 0) {
		vlds_arg.vlds_hdl = hdl;
		(void) ioctl(ds_fd, VLDS_UNREG_HDL, &vlds_arg);
	}

	return (0);
}

/*
 * Send data to the appropriate service provider or client
 * indicated by the provided handle.  The sender will block
 * until the message has been sent.  There is no guarantee
 * that multiple calls to ds_send_msg by the same thread
 * will result in the data showing up at the receiver in
 * the same order as sent.  If multiple messages are required,
 * it will be up to the sender and receiver to implement a
 * protocol.
 */
int
ds_send_msg(ds_hdl_t hdl, void *buf, size_t buflen)
{
	vlds_send_msg_arg_t vlds_arg;

	if (ds_fd < 0) {
		return (errno = EBADF);
	}

	vlds_arg.vlds_hdl = hdl;
	vlds_arg.vlds_bufp = PTRTOUINT64(buf);
	vlds_arg.vlds_buflen = buflen;

	if (ioctl(ds_fd, VLDS_SEND_MSG, &vlds_arg) < 0) {
		return (errno);
	}

	return (0);
}

/*
 * Receive data from the appropriate service provider or client
 * indicated by the provided handle.  The sender will block
 * until a message has been received.
 */
int
ds_recv_msg(ds_hdl_t hdl, void *buf, size_t buflen, size_t *msglen)
{
	vlds_recv_msg_arg_t vlds_arg;
	uint64_t msglen_arg;

	if (ds_fd < 0) {
		return (errno = EBADF);
	}

	vlds_arg.vlds_hdl = hdl;
	vlds_arg.vlds_bufp = PTRTOUINT64(buf);
	vlds_arg.vlds_buflen = buflen;
	vlds_arg.vlds_msglenp = PTRTOUINT64(&msglen_arg);

	if (ioctl(ds_fd, VLDS_RECV_MSG, &vlds_arg) < 0) {
		if (errno == EFBIG && msglen) {
			*msglen = msglen_arg;
		}
		return (errno);
	}

	if (msglen) {
		*msglen = msglen_arg;
	}

	return (0);
}

int
ds_isready(ds_hdl_t hdl, boolean_t *is_ready)
{
	vlds_hdl_isready_arg_t vlds_arg;
	uint64_t is_ready_arg;

	if (ds_fd < 0) {
		return (errno = EBADF);
	}

	vlds_arg.vlds_hdl = hdl;
	vlds_arg.vlds_isreadyp = PTRTOUINT64(&is_ready_arg);

	if (ioctl(ds_fd, VLDS_HDL_ISREADY, &vlds_arg) < 0) {
		return (errno);
	}

	*is_ready = (is_ready_arg != 0);
	return (0);
}

/*
 * Given a domain name, return its associated domain handle.
 */
int
ds_dom_name_to_hdl(char *domain_name, ds_domain_hdl_t *dhdlp)
{
	vlds_dom_nam2hdl_arg_t vlds_arg;
	uint64_t dhdl_arg;

	if (ds_fd < 0) {
		return (errno = EBADF);
	}

	ds_string_arg(&vlds_arg.vlds_domain_name, domain_name);
	vlds_arg.vlds_dhdlp = PTRTOUINT64(&dhdl_arg);

	if (ioctl(ds_fd, VLDS_DOM_NAM2HDL, &vlds_arg) < 0) {
		return (errno);
	}

	if (dhdlp) {
		*dhdlp = dhdl_arg;
	}

	return (0);
}

/*
 * Given a domain handle, return its associated domain name.
 */
int
ds_dom_hdl_to_name(ds_domain_hdl_t dhdl, char *domain_name, uint_t maxnamlen)
{
	vlds_dom_hdl2nam_arg_t vlds_arg;

	if (ds_fd < 0) {
		return (errno = EBADF);
	}

	vlds_arg.vlds_dhdl = dhdl;
	vlds_arg.vlds_domain_name.vlds_strp = PTRTOUINT64(domain_name);
	vlds_arg.vlds_domain_name.vlds_strlen = maxnamlen;

	if (ioctl(ds_fd, VLDS_DOM_HDL2NAM, &vlds_arg) < 0) {
		return (errno);
	}

	return (0);
}

void
ds_unreg_svc(char *service, boolean_t is_client)
{
	ds_hdl_t hdl;
	uint_t nhdls;

	while (ds_hdl_lookup(service, is_client, &hdl, 1, &nhdls) == 0 &&
	    nhdls == 1) {
		(void) ds_unreg_hdl(hdl);
	}
}

void
ds_fini(void)
{
	int i;
	dslibentry_t *dsp;

	if (ds_fd >= 0) {
		(void) close(ds_fd);
		ds_fd = -1;
	}
	if (ds_evchan) {
		(void) sysevent_evc_unsubscribe(ds_evchan, ds_sid_name);
		(void) sysevent_evc_unbind(ds_evchan);
		ds_evchan = NULL;
	}
	if (ndslib > 0) {
		(void) mutex_lock(&dslib_lock);
		for (i = 0, dsp = dslibtab; i < ndslib; i++, dsp++) {
			if (dsp->dsl_hdl == NULL)
				continue;
			if (dsp->dsl_service) {
				free(dsp->dsl_service);
			}
		}
		free(dslibtab);
		ndslib = 0;
		dslibtab = NULL;
		(void) mutex_unlock(&dslib_lock);
		(void) mutex_destroy(&dslib_lock);
	}
}