xref: /illumos-gate/usr/src/cmd/vscan/vscand/vs_door.c (revision bd97c7ce2344fa3252d8785c35895490916bc79b)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * vscand door server
28  */
29 
30 #include <door.h>
31 #include <errno.h>
32 #include <syslog.h>
33 #include <unistd.h>
34 #include <varargs.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <synch.h>
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/param.h>
43 #include <pthread.h>
44 #include "vs_incl.h"
45 
46 #define	VS_DOOR_VERSION	1
47 static int vs_door_cookie;
48 
49 /* function prototype */
50 static void vs_door_scan_req(void *, char *, size_t, door_desc_t *, uint_t);
51 
52 /* local data */
53 static int vs_door_fd = -1;
54 static pthread_mutex_t vs_door_mutex = PTHREAD_MUTEX_INITIALIZER;
55 
56 
57 /*
58  * vs_door_init
59  *
60  * Start the vscand door service.
61  * Returns 0 on success. Otherwise, -1.
62  */
63 int
64 vs_door_init(void)
65 {
66 	(void) pthread_mutex_lock(&vs_door_mutex);
67 
68 	if ((vs_door_fd = door_create(vs_door_scan_req,
69 	    &vs_door_cookie, (DOOR_UNREF | DOOR_REFUSE_DESC))) < 0) {
70 		syslog(LOG_ERR, "vscand: door create%s", strerror(errno));
71 		vs_door_fd = -1;
72 	}
73 
74 	(void) pthread_mutex_unlock(&vs_door_mutex);
75 	return (vs_door_fd);
76 }
77 
78 
79 /*
80  * vscan_door_fini
81  *
82  * Stop the vscand door service.
83  */
84 void
85 vs_door_fini(void)
86 {
87 	(void) pthread_mutex_lock(&vs_door_mutex);
88 
89 	if (vs_door_fd >= 0) {
90 		if (door_revoke(vs_door_fd) < 0)
91 			syslog(LOG_ERR, "vscand: door revoke %s",
92 			    strerror(errno));
93 	}
94 
95 	vs_door_fd = -1;
96 
97 	(void) pthread_mutex_unlock(&vs_door_mutex);
98 }
99 
100 
101 /*
102  * vs_door_scan_req
103  *
104  * Invoke the vscand door service.
105  */
106 /* ARGSUSED */
107 static void
108 vs_door_scan_req(void *cookie, char *ptr, size_t size, door_desc_t *dp,
109     uint_t n_desc)
110 {
111 	vs_scan_req_t *scan_req;
112 	uint32_t result = VS_STATUS_ERROR;
113 
114 	if (ptr != NULL) {
115 		/* LINTED E_BAD_PTR_CAST_ALIGN - to be fixed with encoding */
116 		scan_req = (vs_scan_req_t *)ptr;
117 		result = vs_svc_queue_scan_req(scan_req);
118 	}
119 
120 	(void) door_return((char *)&result, sizeof (uint32_t), NULL, 0);
121 }
122