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
|
/*
* 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.
*/
/*
* VNTSD main
*
* VNTSD takes the following options:
* -i <device instance>
* VCC device instance to use, e.g. virtual-console-concentrator@0.
* Required option.
* -p <ip address>
* IP address VNTSD listens to.
* -d
* Do not daemonize. This is only available in a DEBUG build.
* -t timeout for inactivity 0 = indefinite
* -A enable Authorization checking. Mutually exclusive with -p.
*/
#include <stdio.h>
#include <stdio_ext.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <time.h>
#include <netinet/in.h>
#include <thread.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <libintl.h>
#include <locale.h>
#include <syslog.h>
#include <sys/socket.h>
#include <netdb.h>
#include "vntsd.h"
#include "chars.h"
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't. */
#endif
/* global variables */
#ifdef DEBUG
int vntsddbg = 0x8;
#endif
#define MINUTE 60
#define VNTSD_INVALID_LISTEN_ADDR ((in_addr_t)-1)
#define LOCALHOST_IPv4 "127.0.0.1"
#define LOCALHOST_IPv6 "::1"
static vntsd_t *vntsdp;
static void vntsd_exit(void);
/* Signal handler for SIGINT, SIGKILL and SIGHUP */
static void
exit_sig_handler(int sig)
{
D1(stderr, "t@%d exit_sig_handler%d \n", thr_self(), sig);
if (thr_self() != vntsdp->tid) {
/* not main thread, pass to main thread */
(void) thr_kill(vntsdp->tid, sig);
} else {
exit(0);
}
}
/*
* Before a thread reads in client's input, it attaches to vntsd timer so that
* it can be waken up if a client does not access the connection for
* VNTSD_INPUT_TIMEOUT(10) minutes.
*/
/* attach a thread to timer */
int
vntsd_attach_timer(vntsd_timeout_t *tmop)
{
int rv;
if (vntsdp->timeout == 0) {
return (VNTSD_SUCCESS);
}
(void) mutex_lock(&vntsdp->tmo_lock);
rv = vntsd_que_append(&vntsdp->tmoq, (void *)tmop);
(void) mutex_unlock(&vntsdp->tmo_lock);
return (rv);
}
/* detach a thread from timer */
int
vntsd_detach_timer(vntsd_timeout_t *tmop)
{
int rv;
if (vntsdp->timeout == 0) {
return (VNTSD_SUCCESS);
}
(void) mutex_lock(&vntsdp->tmo_lock);
rv = vntsd_que_rm(&vntsdp->tmoq, (void *)tmop);
(void) mutex_unlock(&vntsdp->tmo_lock);
return (rv);
}
/* check threadd's timeout */
static boolean_t
chk_timeout(vntsd_timeout_t *tmop)
{
tmop->minutes++;
if (tmop->minutes == vntsdp->timeout) {
/* wake up the thread */
tmop->clientp->status |= VNTSD_CLIENT_TIMEOUT;
(void) thr_kill(tmop->tid, SIGALRM);
}
/* return false to walk the queue */
return (B_FALSE);
}
/* reset timer */
static boolean_t
reset_timeout(vntsd_timeout_t *tmop, thread_t tid)
{
if (tmop->tid == tid) {
tmop->minutes = 0;
}
/* return false to walk the queue */
return (B_FALSE);
}
void
vntsd_reset_timer(thread_t tid)
{
if (vntsdp->timeout == 0) {
return;
}
(void) mutex_lock(&vntsdp->tmo_lock);
(void) vntsd_que_find(vntsdp->tmoq, (compare_func_t)reset_timeout,
(void*)tid);
(void) mutex_unlock(&vntsdp->tmo_lock);
}
/*
* When alarm goes off, wake up timeout threads. Alarm is set off every
* minutes.
*/
static void
vntsd_alarm_sig_handler(int sig)
{
static thread_t main_thread = 0;
D1(stderr, "t@%d alarm signal %d\n", thr_self(), sig);
if (vntsdp->timeout == 0) {
DERR(stderr, "t@%d alarm signal should not recv %d\n",
thr_self(), sig);
return;
}
if (main_thread == 0) {
/* initialize thread id */
main_thread = thr_self();
} else if (main_thread != thr_self()) {
/* get signal because thread is timeout */
return;
}
/* in main thread */
(void) mutex_lock(&vntsdp->tmo_lock);
/* wake up timeout threads */
(void) vntsd_que_walk(vntsdp->tmoq, (el_func_t)chk_timeout);
(void) mutex_unlock(&vntsdp->tmo_lock);
/* reset alarm */
(void) alarm(MINUTE);
}
/* got a SIGUSER1 siginal */
static void
vntsd_sig_handler(int sig)
{
char err_msg[VNTSD_LINE_LEN];
(void) snprintf(err_msg, sizeof (err_msg), "sig_handler() sig=%d",
sig);
if (sig != SIGUSR1) {
vntsd_log(VNTSD_STATUS_SIG, err_msg);
}
}
/* vntsd exits */
static void
vntsd_exit(void)
{
D1(stderr, "t@%d vntsd_exit\n", thr_self());
(void) mutex_lock(&vntsdp->lock);
if (vntsdp->timeout > 0) {
/* cancel the timer */
(void) alarm(0);
}
/* delete all groups */
vntsd_free_que(&vntsdp->grouppq, (clean_func_t)vntsd_clean_group);
/* close control port */
(void) close(vntsdp->ctrl_fd);
assert(vntsdp->tmoq == NULL);
(void) mutex_unlock(&vntsdp->lock);
/* clean up vntsdp */
(void) mutex_destroy(&vntsdp->tmo_lock);
(void) mutex_destroy(&vntsdp->lock);
free(vntsdp);
closelog();
}
/*
* vntsd_help()
* print out valid command line options
*/
static void
vntsd_help(void)
{
(void) fprintf(stderr, gettext("Usage: vntsd -i <VCC device instance> "
"[-p <listen address>] [-t <timeout in minutes>] [-A]\n"));
}
/*
* get_listen_ip_addr()
* check for a valid control domain ip address in format of xxx.xxx.xxx.xxx.
* if ip address is valid and is assigned to this host, return ip address
* or else return VNTSD_INVALID_LISTEN_ADDR.
*/
static in_addr_t
get_listen_ip_addr(char *listen_addr)
{
char host_name[MAXPATHLEN];
in_addr_t addr;
struct addrinfo hints;
struct addrinfo *res, *infop;
int err;
struct sockaddr_in *sa;
if (gethostname(host_name, MAXPATHLEN) != 0) {
syslog(LOG_ERR, "Can not get host name!");
return (VNTSD_INVALID_LISTEN_ADDR);
}
if ((int)(addr = inet_addr(listen_addr)) == -1)
/* bad IP address format */
return (VNTSD_INVALID_LISTEN_ADDR);
bzero(&hints, sizeof (hints));
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
err = getaddrinfo(host_name, NULL, &hints, &res);
if (err != 0) {
syslog(LOG_ERR, "getaddrinfo failed: %s", gai_strerror(err));
return (VNTSD_INVALID_LISTEN_ADDR);
}
infop = res;
while (infop != NULL) {
/* LINTED E_BAD_PTR_CAST_ALIGN */
sa = (struct sockaddr_in *)infop->ai_addr;
if (sa->sin_addr.s_addr == addr) {
/* ip address found */
freeaddrinfo(res);
return (addr);
}
infop = infop->ai_next;
}
/* ip address not found */
freeaddrinfo(res);
return (VNTSD_INVALID_LISTEN_ADDR);
}
#ifdef DEBUG
#define DEBUG_OPTIONS "d"
#else
#define DEBUG_OPTIONS ""
#endif
int
main(int argc, char ** argv)
{
char *path;
struct pollfd poll_drv[1];
struct sigaction act;
struct rlimit rlim;
char *listen_addr = NULL;
pid_t pid;
int i;
int option;
int sz;
int fd;
int n;
/* internationalization */
(void) setlocale(LC_MESSAGES, "");
(void) textdomain(TEXT_DOMAIN);
vntsd_init_esctable_msgs();
/* initialization */
bzero(&act, sizeof (act));
/*
* ensure that we can obtain sufficient file descriptors for all
* the accept() calls when a machine contains many domains.
*/
(void) getrlimit(RLIMIT_NOFILE, &rlim);
if (rlim.rlim_cur < rlim.rlim_max)
rlim.rlim_cur = rlim.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
vntsd_log(VNTSD_STATUS_CONTINUE, "Unable to increase file "
"descriptor limit.");
(void) enable_extended_FILE_stdio(-1, -1);
vntsdp = calloc(sizeof (vntsd_t), 1);
if (vntsdp == NULL) {
vntsd_log(VNTSD_ERR_NO_MEM, "main:vntsdp");
exit(1);
}
vntsdp->ctrl_fd = -1;
vntsdp->devinst = NULL;
(void) mutex_init(&vntsdp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
(void) mutex_init(&vntsdp->tmo_lock, USYNC_THREAD|LOCK_ERRORCHECK,
NULL);
/* get CLI options */
while ((option = getopt(argc, argv, "i:t:p:A"DEBUG_OPTIONS)) != EOF) {
switch (option) {
#ifdef DEBUG
case 'd':
vntsdp->options |= VNTSD_OPT_DAEMON_OFF;
break;
#endif
case 'i':
vntsdp->devinst = optarg;
break;
case 'p':
listen_addr = optarg;
break;
case 't':
n = sscanf(optarg, "%d", &(vntsdp->timeout));
if (n != 1) {
vntsdp->timeout = -1;
}
break;
case 'A':
/*
* This option enables authorization checking of the
* user for the console(s) being accessed. As the
* authorization checking can be done only for a local
* client process, it requires that vntsd listen only
* on the loopback address. It means while this option
* is enabled, vntsd cannot listen on either INADDR_ANY
* or a specific ip address and thus the telnet client
* must also run on the local machine in order to
* connect to vntsd. The '-p' option if specified while
* this option is enabled, will be ignored and the auth
* checking takes precedence forcing vntsd to listen on
* the loopback interface.
*/
vntsdp->options |= VNTSD_OPT_AUTH_CHECK;
break;
default:
vntsd_help();
exit(1);
}
}
if ((vntsdp->devinst == NULL) || (vntsdp->timeout == -1)) {
vntsd_help();
exit(1);
}
if (listen_addr == NULL || strcmp(listen_addr, "localhost") == 0 ||
strcmp(listen_addr, LOCALHOST_IPv4) == 0 ||
strcmp(listen_addr, LOCALHOST_IPv6) == 0) {
/* by default listen on loopback interface */
vntsdp->ip_addr.s_addr = htonl(INADDR_LOOPBACK);
} else if ((vntsdp->options & VNTSD_OPT_AUTH_CHECK) != 0) {
vntsd_log(VNTSD_STATUS_AUTH_ENABLED,
"Listen address ignored as authorization checking "
"is enabled");
vntsdp->ip_addr.s_addr = htonl(INADDR_LOOPBACK);
} else if (strcmp(listen_addr, "any") == 0) {
vntsdp->ip_addr.s_addr = htonl(INADDR_ANY);
} else {
vntsdp->ip_addr.s_addr = get_listen_ip_addr(listen_addr);
if (vntsdp->ip_addr.s_addr == VNTSD_INVALID_LISTEN_ADDR) {
syslog(LOG_ERR,
"Invalid listen address '%s'\n",
listen_addr);
exit(2);
}
}
D3(stderr, "options = %llx, instance = %s, listen = %s\n",
vntsdp->options, vntsdp->devinst,
listen_addr ? listen_addr : "<null>");
/* open VCC driver control port */
sz = strlen(VCC_DEVICE_CTL_PATH) + strlen(vntsdp->devinst) + 1;
path = calloc(sz, 1);
if (path == NULL) {
vntsd_log(VNTSD_ERR_NO_MEM, "main(): alloc dev path");
exit(1);
}
(void) snprintf(path, sz-1, VCC_DEVICE_CTL_PATH, vntsdp->devinst,
sizeof (vntsdp->devinst));
vntsdp->ctrl_fd = open(path, O_RDWR);
if (vntsdp->ctrl_fd == -1) {
/* print error if device is not present */
syslog(LOG_ERR,
"Error opening VCC device control port: %s",
path);
/* tell SMF no retry */
exit(2);
}
free(path);
if ((vntsdp->options & VNTSD_OPT_DAEMON_OFF) == 0) {
/* daemonize it */
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid > 0) {
/* parent */
exit(0);
}
/*
* child process (daemon)
*
* Close all file descriptors other than 2 and the ctrl fd.
*/
(void) close(0);
(void) close(1);
for (i = 3; i < vntsdp->ctrl_fd; i++) {
(void) close(i);
}
closefrom(vntsdp->ctrl_fd + 1);
/* obtain a new process group */
(void) setsid();
fd = open("/dev/null", O_RDWR);
if (fd < 0) {
syslog(LOG_ERR, "Can not open /dev/null");
exit(1);
}
/* handle standard I/O */
if (dup2(fd, 0) < 0) {
syslog(LOG_ERR, "Failed dup2()");
exit(1);
}
if (dup2(fd, 1) < 0) {
syslog(LOG_ERR, "Failed dup2()");
exit(1);
}
/* ignore terminal signals */
(void) signal(SIGTSTP, SIG_IGN);
(void) signal(SIGTTOU, SIG_IGN);
(void) signal(SIGTTIN, SIG_IGN);
}
/* set up signal handlers */
/* exit signals */
act.sa_handler = exit_sig_handler;
(void) sigemptyset(&act.sa_mask);
(void) sigaction(SIGINT, &act, NULL);
(void) sigaction(SIGTERM, &act, NULL);
(void) sigaction(SIGHUP, &act, NULL);
/* vntsd internal signals */
act.sa_handler = vntsd_sig_handler;
(void) sigemptyset(&act.sa_mask);
(void) sigaction(SIGUSR1, &act, NULL);
act.sa_handler = vntsd_alarm_sig_handler;
(void) sigemptyset(&act.sa_mask);
(void) sigaction(SIGALRM, &act, NULL);
/* setup exit */
(void) atexit(vntsd_exit);
/* initialization */
openlog("vntsd", LOG_CONS, LOG_DAEMON);
/* set alarm */
if (vntsdp->timeout > 0) {
(void) alarm(MINUTE);
}
vntsdp->tid = thr_self();
/* get exiting consoles from vcc */
vntsd_get_config(vntsdp);
for (; ; ) {
/* poll vcc for configuration change */
bzero(poll_drv, sizeof (poll_drv));
poll_drv[0].fd = vntsdp->ctrl_fd;
poll_drv[0].events = POLLIN;
if (poll(poll_drv, 1, -1) == -1) {
if (errno == EINTR) {
/* wake up because a consle was deleted */
vntsd_delete_cons(vntsdp);
continue;
}
vntsd_log(VNTSD_ERR_VCC_POLL,
"vcc control poll err! aborting..");
exit(1);
}
D1(stderr, "t@%d driver event %x\n", thr_self(),
poll_drv[0].revents);
vntsd_daemon_wakeup(vntsdp);
/*
* Main thread may miss a console-delete signal when it is
* not polling vcc. check if any console is deleted.
*/
vntsd_delete_cons(vntsdp);
}
/*NOTREACHED*/
return (0);
}
/* export ip_addr */
struct in_addr
vntsd_ip_addr(void)
{
return (vntsdp->ip_addr);
}
/*
* ioctl to vcc control port
* Supported ioctls interface are:
* ioctl code parameters return data
* VCC_NUM_CONSOLE none uint_t no consoles
* VCC_CONS_TBL none array of vcc_cons_t
* VCC_INQUIRY none vcc_response_t response
* VCC_CONS_INFO uint_t portno vcc_cons_t
* VCC_CONS_STATUS uint_t portno
* VCC_FORCE_CLOSE uint_t portno
*/
int
vntsd_vcc_ioctl(int ioctl_code, uint_t portno, void *buf)
{
D1(stderr, "t@%d vcc_ioctl@%d code=%x\n", thr_self(), portno,
ioctl_code);
if ((ioctl_code == (VCC_CONS_INFO)) ||
(ioctl_code == (VCC_FORCE_CLOSE))) {
/* construct vcc in buf */
*((uint_t *)buf) = portno;
}
if (ioctl(vntsdp->ctrl_fd, ioctl_code, (caddr_t)buf)) {
/* ioctl request error */
return (VNTSD_STATUS_VCC_IO_ERR);
}
return (VNTSD_SUCCESS);
}
/*
* check if a vcc i/o error is caused by removal of a console. If so
* wake up main thread to cleanup the console.
*/
int
vntsd_vcc_err(vntsd_cons_t *consp)
{
vntsd_group_t *groupp;
assert(consp);
groupp = consp->group;
assert(groupp);
if (consp->status & VNTSD_CONS_DELETED) {
/* console was deleted */
return (VNTSD_STATUS_VCC_IO_ERR);
}
if (vntsd_vcc_cons_alive(consp)) {
/* console is ok */
return (VNTSD_STATUS_CONTINUE);
}
/* console needs to be deleted */
(void) mutex_lock(&consp->lock);
consp->status |= VNTSD_CONS_DELETED;
/*
* main thread will close all clients after receiving console
* delete signal.
*/
(void) mutex_unlock(&consp->lock);
/* mark the group */
(void) mutex_lock(&groupp->lock);
groupp->status |= VNTSD_GROUP_CLEAN_CONS;
(void) mutex_unlock(&groupp->lock);
/* signal main thread to deleted console */
(void) thr_kill(vntsdp->tid, SIGUSR1);
return (VNTSD_STATUS_VCC_IO_ERR);
}
|