xref: /titanic_44/usr/src/cmd/vscan/vscand/vs_door.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 
29 /*
30  * vscand door server
31  */
32 
33 #include <door.h>
34 #include <errno.h>
35 #include <syslog.h>
36 #include <unistd.h>
37 #include <varargs.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <synch.h>
42 #include <fcntl.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/param.h>
46 #include <pthread.h>
47 #include "vs_incl.h"
48 
49 #define	VS_DOOR_VERSION	1
50 static int vs_door_cookie;
51 
52 /* function prototype */
53 static void vs_door_scan_req(void *, char *, size_t, door_desc_t *, uint_t);
54 
55 /* local data */
56 static int vs_door_fd = -1;
57 static pthread_mutex_t vs_door_mutex = PTHREAD_MUTEX_INITIALIZER;
58 
59 
60 /*
61  * vs_door_init
62  *
63  * Start the vscand door service.
64  * Returns 0 on success. Otherwise, -1.
65  */
66 int
67 vs_door_init(void)
68 {
69 	(void) pthread_mutex_lock(&vs_door_mutex);
70 
71 	if ((vs_door_fd = door_create(vs_door_scan_req,
72 	    &vs_door_cookie, (DOOR_UNREF | DOOR_REFUSE_DESC))) < 0) {
73 		syslog(LOG_ERR, "vscand: door create%s", strerror(errno));
74 		vs_door_fd = -1;
75 	}
76 
77 	(void) pthread_mutex_unlock(&vs_door_mutex);
78 	return (vs_door_fd);
79 }
80 
81 
82 /*
83  * vscan_door_fini
84  *
85  * Stop the vscand door service.
86  */
87 void
88 vs_door_fini(void)
89 {
90 	(void) pthread_mutex_lock(&vs_door_mutex);
91 
92 	if (vs_door_fd >= 0) {
93 		if (door_revoke(vs_door_fd) < 0)
94 			syslog(LOG_ERR, "vscand: door revoke %s",
95 			    strerror(errno));
96 	}
97 
98 	vs_door_fd = -1;
99 
100 	(void) pthread_mutex_unlock(&vs_door_mutex);
101 }
102 
103 
104 /*
105  * vs_door_scan_req
106  *
107  * Invoke the vscand door service.
108  */
109 /* ARGSUSED */
110 static void
111 vs_door_scan_req(void *cookie, char *ptr, size_t size, door_desc_t *dp,
112     uint_t n_desc)
113 {
114 	int flags = 0, access = VS_ACCESS_DENY;
115 	vs_scan_req_t scan_rsp;
116 	/* LINTED E_BAD_PTR_CAST_ALIGN - to be fixed with encoding */
117 	vs_scan_req_t *scan_req = (vs_scan_req_t *)ptr;
118 	char *fname = scan_req->vsr_path;
119 	char devname[MAXPATHLEN];
120 	uint64_t fsize = scan_req->vsr_size;
121 	vs_attr_t fattr;
122 
123 	(void) snprintf(devname, MAXPATHLEN, "%s%d",
124 	    VS_DRV_PATH, scan_req->vsr_id);
125 	fattr.vsa_size = fsize;
126 	fattr.vsa_modified = scan_req->vsr_modified;
127 	fattr.vsa_quarantined = scan_req->vsr_quarantined;
128 	(void) strlcpy(fattr.vsa_scanstamp, scan_req->vsr_scanstamp,
129 	    sizeof (vs_scanstamp_t));
130 
131 	access = vs_svc_scan_file(devname, fname, &fattr, flags);
132 
133 	/* process result */
134 	scan_rsp.vsr_access = access;
135 	scan_rsp.vsr_modified = fattr.vsa_modified;
136 	scan_rsp.vsr_quarantined = fattr.vsa_quarantined;
137 	(void) strlcpy(scan_rsp.vsr_scanstamp, fattr.vsa_scanstamp,
138 	    sizeof (vs_scanstamp_t));
139 
140 	(void) door_return((char *)&scan_rsp, sizeof (vs_scan_req_t), NULL, 0);
141 }
142