summaryrefslogtreecommitdiff
path: root/usr/src/cmd/vscan/vscand/vs_main.c
blob: 5e13bf587079b529900d581d66f71c90b5cdef7f (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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * vscand Daemon Program
 */

#include <stdio.h>
#include <sys/stat.h>
#include <sys/filio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <zone.h>
#include <tsol/label.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <wait.h>
#include <unistd.h>
#include <getopt.h>
#include <stdarg.h>
#include <libscf.h>
#include <signal.h>
#include <atomic.h>
#include <libintl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <pthread.h>
#include <syslog.h>
#include <locale.h>
#include <pwd.h>
#include <grp.h>
#include <priv_utils.h>
#include <rctl.h>
#include "vs_incl.h"

#define	VS_FILE_DESCRIPTORS	512

static int vscand_fg = 0; /* daemon by default */
static vs_daemon_state_t vscand_state = VS_STATE_INIT;
static volatile uint_t vscand_sigval = 0;
static volatile uint_t vscand_n_refresh = 0;
static int vscand_kdrv_fd = -1;
static pthread_mutex_t vscand_cfg_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t vscand_cfg_cv;
static pthread_t vscand_cfg_tid = 0;

/* virus log path */
static char vscand_vlog[MAXPATHLEN];

/* user and group ids - default to 0 */
static uid_t root_uid = 0, daemon_uid = 0;
static gid_t sys_gid = 0;


/* local function prototypes */
static void vscand_sig_handler(int);
static int vscand_parse_args(int, char **);
static void vscand_get_uid_gid();
static int vscand_init_file(char *, uid_t, gid_t, mode_t);
static void vscand_usage(char *);
static int vscand_daemonize_init(void);
static void vscand_daemonize_fini(int, int);
static int vscand_init(void);
static void vscand_fini(void);
static int vscand_cfg_init(void);
static void vscand_cfg_fini(void);
static void *vscand_cfg_handler(void *);
static int vscand_configure(void);
static void vscand_dtrace_cfg(vs_props_all_t *);
static int vscand_kernel_bind(void);
static void vscand_kernel_unbind(void);
static int vscand_kernel_enable(int);
static void vscand_kernel_disable(void);
static int vscand_kernel_config(vs_config_t *);
static int vscand_kernel_max_req(uint32_t *);
static void vscand_error(const char *);
static int vscand_get_viruslog(void);
static int vscand_set_resource_limits(void);


/*
 * Enable libumem debugging by default on DEBUG builds.
 */
#ifdef DEBUG
const char *
_umem_debug_init(void)
{
	return ("default,verbose"); /* $UMEM_DEBUG setting */
}

const char *
_umem_logging_init(void)
{
	return ("fail,contents"); /* $UMEM_LOGGING setting */
}
#endif


/*
 * vscand_sig_handler
 */
static void
vscand_sig_handler(int sig)
{
	if (vscand_sigval == 0)
		(void) atomic_swap_uint(&vscand_sigval, sig);

	if (sig == SIGHUP)
		atomic_inc_uint(&vscand_n_refresh);
}


/*
 * main
 *
 * main must return SMF return code (see smf_method(7)) if vscand
 * is invoked directly by smf (see manifest: vscan.xml)
 * Exit codes: SMF_EXIT_ERR_CONFIG - error
 *             SMF_EXIT_ERR_FATAL - fatal error
 *             SMF_EXIT_OK - success
 */
int
main(int argc, char **argv)
{
	int err_stat = 0, pfd = -1;
	sigset_t set;
	struct sigaction act;
	int sigval;

	mode_t log_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
	mode_t door_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

	(void) setlocale(LC_ALL, "");
	openlog("vscand", 0, LOG_DAEMON);

	/* check if running in global zone; other zones not supported */
	if (getzoneid() != GLOBAL_ZONEID) {
		vscand_error(gettext("non-global zone not supported"));
		exit(SMF_EXIT_ERR_FATAL);
	}

	/* check for a Trusted Solaris environment; not supported */
	if (is_system_labeled()) {
		vscand_error(gettext("Trusted Extensions not supported"));
		exit(SMF_EXIT_ERR_FATAL);
	}

	/* Parse arguments */
	if (vscand_parse_args(argc, argv) != 0)
		exit(SMF_EXIT_ERR_CONFIG);

	vscand_get_uid_gid();

	/*
	 * Initializetion of virus log and statistic door file
	 * MUST be done BEFORE vscand_daemonize_init resets uid/gid.
	 * Only root can create the files in /var/log and /var/run.
	 */
	if ((vscand_get_viruslog() != 0) ||
	    (vscand_vlog[0] == '\0') ||
	    (vscand_init_file(vscand_vlog, root_uid, sys_gid, log_mode) != 0)) {
		*vscand_vlog = 0;
	}

	(void) vscand_init_file(VS_STATS_DOOR_NAME,
	    daemon_uid, sys_gid, door_mode);

	/*
	 * Once we're done setting our global state up, set up signal handlers
	 * for ensuring orderly termination on SIGTERM.
	 */
	(void) sigfillset(&set);
	(void) sigdelset(&set, SIGABRT); /* always unblocked for ASSERT() */

	(void) sigfillset(&act.sa_mask);
	act.sa_handler = vscand_sig_handler;
	act.sa_flags = 0;

	(void) sigaction(SIGTERM, &act, NULL);
	(void) sigaction(SIGHUP, &act, NULL); /* Refresh config */
	(void) sigaction(SIGINT, &act, NULL);
	(void) sigaction(SIGPIPE, &act, NULL);
	(void) sigdelset(&set, SIGTERM);
	(void) sigdelset(&set, SIGHUP);
	(void) sigdelset(&set, SIGINT);
	(void) sigdelset(&set, SIGPIPE);

	if (vscand_fg) {
		(void) sigdelset(&set, SIGTSTP);
		(void) sigdelset(&set, SIGTTIN);
		(void) sigdelset(&set, SIGTTOU);

		if (vscand_init() != 0) {
			vscand_error(gettext("failed to initialize service"));
			exit(SMF_EXIT_ERR_CONFIG);
		}
	} else {
		/*
		 * "pfd" is a pipe descriptor -- any fatal errors
		 * during subsequent initialization of the child
		 * process should be written to this pipe and the
		 * parent will report this error as the exit status.
		 */
		pfd = vscand_daemonize_init();

		if (vscand_init() != 0) {
			vscand_error(gettext("failed to initialize service"));
			exit(SMF_EXIT_ERR_CONFIG);
		}

		vscand_daemonize_fini(pfd, err_stat);
	}

	vscand_state = VS_STATE_RUNNING;

	/* Wait here until shutdown */
	while (vscand_state == VS_STATE_RUNNING) {
		if (vscand_sigval == 0 && vscand_n_refresh == 0)
			(void) sigsuspend(&set);

		sigval = atomic_swap_uint(&vscand_sigval, 0);

		switch (sigval) {
		case 0:
		case SIGPIPE:
		case SIGHUP:
			break;
		default:
			vscand_state = VS_STATE_SHUTDOWN;
			break;
		}

		if (atomic_swap_uint(&vscand_n_refresh, 0) != 0)
			(void) pthread_cond_signal(&vscand_cfg_cv);
	}

	vscand_fini();
	return (SMF_EXIT_OK);
}


/*
 * vscand_parse_args
 * Routine to parse the arguments to the daemon program
 * 'f' argument runs process in the foreground instead of as a daemon
 */
int
vscand_parse_args(int argc, char **argv)
{
	int	optchar;

	while ((optchar = getopt(argc, argv, "f?")) != EOF) {
		switch (optchar) {
		case 'f':
			vscand_fg = 1;
			break;
		default:
			vscand_usage(argv[0]);
			return (-1);
		}
	}
	return (0);
}


/*
 * vscand_usage
 */
static void
vscand_usage(char *progname)
{
	char buf[128];

	(void) snprintf(buf, sizeof (buf), "%s %s [-f]",
	    gettext("Usage"), progname);
	vscand_error(buf);

	(void) snprintf(buf, sizeof (buf), "\t-f %s\n",
	    gettext("run program in foreground"));
	vscand_error(buf);
}


/*
 * vscand_get_uid_gid
 *
 * failure to access a uid/gid results in the default (0) being used.
 */
static void
vscand_get_uid_gid()
{
	struct passwd *pwd;
	struct group *grp;

	if ((pwd = getpwnam("root")) != NULL)
		root_uid = pwd->pw_uid;

	if ((pwd = getpwnam("daemon")) != NULL)
		daemon_uid = pwd->pw_uid;

	if ((grp = getgrnam("sys")) != NULL)
		sys_gid = grp->gr_gid;
}


/*
 * vscand_daemonize_init
 *
 * This function will fork off a child process, from which
 * only the child will return.
 */
static int
vscand_daemonize_init(void)
{
	int status, pfds[2];
	sigset_t set, oset;
	pid_t pid;

	/*
	 * Reset process owner/group to daemon/sys. Root ownership is only
	 * required to initialize virus log file in /var/log
	 */
	if (__init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS,
	    daemon_uid, sys_gid,
	    PRIV_PROC_AUDIT, PRIV_FILE_DAC_SEARCH, PRIV_FILE_DAC_READ,
	    PRIV_FILE_FLAG_SET, NULL) != 0) {
		vscand_error(gettext("failed to initialize privileges"));
		_exit(SMF_EXIT_ERR_FATAL);
	}

	/*
	 * Block all signals prior to the fork and leave them blocked in the
	 * parent so we don't get in a situation where the parent gets SIGINT
	 * and returns non-zero exit status and the child is actually running.
	 * In the child, restore the signal mask once we've done our setsid().
	 */
	(void) sigfillset(&set);
	(void) sigdelset(&set, SIGABRT);
	(void) sigprocmask(SIG_BLOCK, &set, &oset);

	if (pipe(pfds) == -1) {
		vscand_error(gettext("failed to create pipe for daemonize"));
		_exit(SMF_EXIT_ERR_FATAL);
	}

	if ((pid = fork()) == -1) {
		vscand_error(gettext("failed to fork for daemonize"));
		_exit(SMF_EXIT_ERR_FATAL);
	}

	/*
	 * If we're the parent process, wait for either the child to send us
	 * the appropriate exit status over the pipe or for the read to fail
	 * (presumably with 0 for EOF if our child terminated abnormally).
	 * If the read fails, exit with either the child's exit status if it
	 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
	 */
	if (pid != 0) {
		(void) close(pfds[1]);

		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
			_exit(status);

		if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
			_exit(WEXITSTATUS(status));

		vscand_error(gettext("failed to daemonize"));
		_exit(SMF_EXIT_ERR_FATAL);
	}


	(void) setsid();
	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
	(void) chdir("/");
	(void) umask(022);
	(void) close(pfds[0]);

	return (pfds[1]);
}


/*
 * vscand_daemonize_fini
 * Now that we're running, if a pipe fd was specified, write an exit
 * status to it to indicate that our parent process can safely detach.
 */
static void
vscand_daemonize_fini(int fd, int err_status)
{
	if (fd >= 0)
		(void) write(fd, &err_status, sizeof (err_status));

	(void) close(fd);

	/* Restore standard file descriptors */
	if ((fd = open("/dev/null", O_RDWR)) >= 0) {
		(void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
		(void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
		(void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
		(void) close(fd);
	}

	/* clear basic privileges not required by vscand */
	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
}


/*
 * vscand_init_file
 *
 * create specified file and set its uid, gid and mode
 */
static int
vscand_init_file(char *filepath, uid_t uid, gid_t gid, mode_t access_mode)
{
	int fd, rc = 0;
	struct stat stat_buf;
	char buf[MAXPATHLEN];

	if ((fd = open(filepath, O_RDONLY | O_CREAT, access_mode)) == -1) {
		rc = -1;
	} else {
		if (fstat(fd, &stat_buf) != 0) {
			rc = -1;
		} else {
			if ((stat_buf.st_mode & S_IAMB) != access_mode) {
				if (fchmod(fd, access_mode) != 0)
					rc = -1;
			}

			if ((stat_buf.st_uid != uid) ||
			    (stat_buf.st_gid != gid)) {
				if (fchown(fd, uid, gid) != 0)
					rc = -1;
			}
		}

		(void) close(fd);
	}

	if (rc == -1) {
		(void) snprintf(buf, MAXPATHLEN, "%s %s",
		    gettext("Failed to initialize"), filepath);
		vscand_error(buf);
	}

	return (rc);
}


/*
 * vscand_init
 *
 * There are some requirements on the order in which the daemon
 * initialization functions are called.
 *
 * - vscand_kernel_bind - bind to kernel module
 * - vs_eng_init populates vs_icap data and thus vs_icap_init MUST be
 *   called before vs_eng_init
 * - vscand_configure - load the configuration
 * - vs_door_init - start vscan door server
 * - vscand_kernel_enable - enable scan requests from kernel
 */
static int
vscand_init(void)
{
	int door_fd = -1;
	uint32_t max_req;

	if (vscand_kernel_bind() < 0)
		return (-1);

	if (vscand_kernel_max_req(&max_req) == -1)
		return (-1);

	if (vs_svc_init(max_req) != 0)
		return (-1);

	if (vs_stats_init() != 0)
		vscand_error(
		    gettext("failed to initialize statistics interface"));

	vs_icap_init();
	vs_eng_init();

	/* initialize configuration and handler thread */
	if (vscand_cfg_init() != 0) {
		vscand_error(gettext("failed to initialize configuration"));
		vscand_fini();
		return (-1);
	}

	(void) vscand_set_resource_limits();

	if (((door_fd = vs_door_init()) < 0) ||
	    (vscand_kernel_enable(door_fd) < 0)) {
		vscand_fini();
		return (-1);
	}

	return (0);
}


/*
 * vscand_fini
 *
 * vscand_kernel_disable - should be called first to ensure that no
 *	more scan requests are initiated from the kernel module
 * vs_svc_terminate - terminate requests and wait for thread completion
 * vs_xxx_fini - module cleanup routines
 * vscand_kernel_unbind - should be called last to tell the kernel module
 *	that vscand is shutdown.
 */
static void
vscand_fini(void)
{
	vscand_kernel_disable();

	/* terminate reconfiguration handler thread */
	vscand_cfg_fini();

	/* terminate requests and wait for completion */
	vs_svc_terminate();

	/* clean up */
	vs_svc_fini();
	vs_eng_fini();
	vs_icap_fini();
	vs_door_fini();
	vs_stats_fini();

	vscand_kernel_unbind();
}


/*
 * vscand_cfg_init
 *
 * initialize configuration and reconfiguration handler thread
 */
static int
vscand_cfg_init(void)
{
	int rc;

	(void) pthread_cond_init(&vscand_cfg_cv, NULL);

	(void) pthread_mutex_lock(&vscand_cfg_mutex);
	rc = vscand_configure();
	(void) pthread_mutex_unlock(&vscand_cfg_mutex);

	if (rc != 0)
		return (-1);

	if (pthread_create(&vscand_cfg_tid, NULL, vscand_cfg_handler, 0) != 0) {
		vscand_cfg_tid = 0;
		return (-1);
	}

	return (0);
}


/*
 * vscand_cfg_fini
 *
 * terminate reconfiguration handler thread
 */
static void
vscand_cfg_fini()
{
	if (vscand_cfg_tid != 0) {
		(void) pthread_cond_signal(&vscand_cfg_cv);
		(void) pthread_join(vscand_cfg_tid, NULL);
		vscand_cfg_tid = 0;
	}
	(void) pthread_cond_destroy(&vscand_cfg_cv);
}


/*
 * vscand_cfg_handler
 * wait for reconfiguration event and reload configuration
 * exit on VS_STATE_SHUTDOWN
 */
/*ARGSUSED*/
static void *
vscand_cfg_handler(void *arg)
{
	(void) pthread_mutex_lock(&vscand_cfg_mutex);

	while (pthread_cond_wait(&vscand_cfg_cv, &vscand_cfg_mutex) == 0) {
		if (vscand_state == VS_STATE_SHUTDOWN)
			break;

		(void) vscand_configure();
	}

	(void) pthread_mutex_unlock(&vscand_cfg_mutex);

	return (NULL);
}


/*
 * vscand_configure
 */
static int
vscand_configure(void)
{
	uint32_t len;
	vs_config_t kconfig;
	vs_props_all_t config;

	(void) memset(&config, 0, sizeof (vs_props_all_t));
	if (vs_props_get_all(&config) != VS_ERR_NONE) {
		vscand_error(gettext("configuration data error"));
		return (-1);
	}

	(void) memset(&kconfig, 0, sizeof (vs_config_t));
	len = sizeof (kconfig.vsc_types);
	if (vs_parse_types(config.va_props.vp_types,
	    kconfig.vsc_types, &len) != 0) {
		vscand_error(gettext("configuration data error - types"));
		return (-1);
	}
	kconfig.vsc_types_len = len;

	/* Convert the maxfsize string from the configuration into bytes */
	if (vs_strtonum(config.va_props.vp_maxsize,
	    &kconfig.vsc_max_size) != 0) {
		vscand_error(gettext("configuration data error - max-size"));
		return (-1);
	}
	kconfig.vsc_allow = config.va_props.vp_maxsize_action ? 1LL : 0LL;

	/* Send configuration update to kernel */
	if (vscand_kernel_config(&kconfig) != 0) {
		return (-1);
	}

	/* dtrace the configuration data */
	vscand_dtrace_cfg(&config);

	/* propagate configuration changes */
	vs_eng_config(&config);
	vs_stats_config(&config);

	return (0);
}


/*
 * vscand_get_state
 */
vs_daemon_state_t
vscand_get_state(void)
{
	return (vscand_state);
}


/*
 * vscand_get_viruslog
 */
static int
vscand_get_viruslog()
{
	vs_props_t props;
	uint64_t propids;
	int rc;

	propids = VS_PROPID_VLOG;
	if ((rc = vs_props_get(&props, propids)) != VS_ERR_NONE) {
		vscand_error(vs_strerror(rc));
		return (-1);
	}

	(void) strlcpy(vscand_vlog, props.vp_vlog, sizeof (vscand_vlog));
	return (0);
}


/*
 * vscand_viruslog
 */
char *
vscand_viruslog(void)
{
	if (vscand_vlog[0] == '\0')
		return (NULL);

	return (vscand_vlog);
}


/*
 * vscand_kernel_bind
 */
static int
vscand_kernel_bind(void)
{
	char devname[MAXPATHLEN];
	int inst = 0;

	(void) snprintf(devname, MAXPATHLEN, "%s%d", VS_DRV_PATH, inst);

	if ((vscand_kdrv_fd = open(devname, O_RDONLY)) < 0) {
		vscand_error(gettext("failed to bind to kernel"));
		return (-1);
	}

	return (0);
}


/*
 * vscand_kernel_unbind
 */
static void
vscand_kernel_unbind(void)
{
	if (vscand_kdrv_fd >= 0)
		(void) close(vscand_kdrv_fd);
}


/*
 * vscand_kernel_enable
 */
static int
vscand_kernel_enable(int door_fd)
{
	if (ioctl(vscand_kdrv_fd, VS_IOCTL_ENABLE, door_fd) < 0) {
		vscand_error(gettext("failed to bind to kernel"));
		(void) close(vscand_kdrv_fd);
		vscand_kdrv_fd = -1;
		return (-1);
	}
	return (0);
}


/*
 * vscand_kernel_disable
 */
static void
vscand_kernel_disable()
{
	if (vscand_kdrv_fd >= 0)
		(void) ioctl(vscand_kdrv_fd, VS_IOCTL_DISABLE);
}


/*
 * vscand_kernel_config
 */
int
vscand_kernel_config(vs_config_t *conf)
{
	if ((vscand_kdrv_fd < 0) ||
	    (ioctl(vscand_kdrv_fd, VS_IOCTL_CONFIG, conf) < 0)) {
		vscand_error(gettext("failed to send config to kernel"));
		return (-1);
	}

	return (0);
}


/*
 * vscand_kernel_result
 */
int
vscand_kernel_result(vs_scan_rsp_t *scan_rsp)
{
	if ((vscand_kdrv_fd < 0) ||
	    (ioctl(vscand_kdrv_fd, VS_IOCTL_RESULT, scan_rsp) < 0)) {
		vscand_error(gettext("failed to send result to kernel"));
		return (-1);
	}

	return (0);
}


/*
 * vscand_kernel_max_req
 */
int
vscand_kernel_max_req(uint32_t *max_req)
{
	if ((vscand_kdrv_fd < 0) ||
	    (ioctl(vscand_kdrv_fd, VS_IOCTL_MAX_REQ, max_req) < 0)) {
		vscand_error(gettext("failed to get config data from kernel"));
		return (-1);
	}

	return (0);
}


/*
 * vscand_set_resource_limits
 *
 * If the process's max file descriptor limit is less than
 * VS_FILE_DESCRIPTORS, increae it to VS_FILE_DESCRIPTORS.
 */
static int
vscand_set_resource_limits(void)
{
	int rc = -1;
	rctlblk_t *rblk;
	char *limit = "process.max-file-descriptor";

	rblk = (rctlblk_t *)malloc(rctlblk_size());

	if (rblk != NULL) {
		rc = getrctl(limit, NULL, rblk, 0);
		if ((rc == 0) &&
		    (rctlblk_get_value(rblk) < VS_FILE_DESCRIPTORS)) {
			rctlblk_set_value(rblk, VS_FILE_DESCRIPTORS);
			rc = setrctl(limit, NULL, rblk, 0);
		}
		(void) free(rblk);
	}

	return (rc);
}


/*
 * vscand_error
 */
static void
vscand_error(const char *errmsg)
{
	(void) fprintf(stderr, "vscand: %s", errmsg);
	syslog(LOG_ERR, "%s\n", errmsg);
}


/*
 * vscand_dtrace_cfg
 * vscand_dtrace_gen
 * vscand_dtrace_eng
 *
 * Support for dtracing vscand configuration when processing
 * a reconfiguration event (SIGHUP)
 */
/*ARGSUSED*/
static void
vscand_dtrace_eng(char *id, boolean_t enable, char *host, int port, int conn)
{
}
/*ARGSUSED*/
static void
vscand_dtrace_gen(char *size, boolean_t action, char *types, char *log)
{
}
static void
vscand_dtrace_cfg(vs_props_all_t *config)
{
	int i;

	vscand_dtrace_gen(config->va_props.vp_maxsize,
	    config->va_props.vp_maxsize_action,
	    config->va_props.vp_types,
	    config->va_props.vp_vlog);

	for (i = 0; i < VS_SE_MAX; i++) {
		if (config->va_se[i].vep_engid[0] != 0)
				vscand_dtrace_eng(config->va_se[i].vep_engid,
				    config->va_se[i].vep_enable,
				    config->va_se[i].vep_host,
				    config->va_se[i].vep_port,
				    config->va_se[i].vep_maxconn);
	}
}