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 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2012 Joyent, Inc. All rights reserved.
26 */
27
28 /*
29 * Console support for zones requires a significant infrastructure. The
30 * core pieces are contained in this file, but other portions of note
31 * are in the zlogin(1M) command, the zcons(7D) driver, and in the
32 * devfsadm(1M) misc_link generator.
33 *
34 * Care is taken to make the console behave in an "intuitive" fashion for
35 * administrators. Essentially, we try as much as possible to mimic the
36 * experience of using a system via a tip line and system controller.
37 *
38 * The zone console architecture looks like this:
39 *
40 * Global Zone | Non-Global Zone
41 * .--------------. |
42 * .-----------. | zoneadmd -z | | .--------. .---------.
43 * | zlogin -C | | myzone | | | ttymon | | syslogd |
44 * `-----------' `--------------' | `--------' `---------'
45 * | | | | | | |
492 return (-1);
493 }
494
495 static void
496 destroy_console_sock(int servfd)
497 {
498 char path[MAXPATHLEN];
499
500 (void) snprintf(path, sizeof (path), CONSOLE_SOCKPATH, zone_name);
501 (void) unlink(path);
502 (void) shutdown(servfd, SHUT_RDWR);
503 (void) close(servfd);
504 }
505
506 /*
507 * Read the "ident" string from the client's descriptor; this routine also
508 * tolerates being called with pid=NULL, for times when you want to "eat"
509 * the ident string from a client without saving it.
510 */
511 static int
512 get_client_ident(int clifd, pid_t *pid, char *locale, size_t locale_len)
513 {
514 char buf[BUFSIZ], *bufp;
515 size_t buflen = sizeof (buf);
516 char c = '\0';
517 int i = 0, r;
518
519 /* "eat up the ident string" case, for simplicity */
520 if (pid == NULL) {
521 assert(locale == NULL && locale_len == 0);
522 while (read(clifd, &c, 1) == 1) {
523 if (c == '\n')
524 return (0);
525 }
526 }
527
528 bzero(buf, sizeof (buf));
529 while ((buflen > 1) && (r = read(clifd, &c, 1)) == 1) {
530 buflen--;
531 if (c == '\n')
532 break;
533
534 buf[i] = c;
535 i++;
536 }
537 if (r == -1)
538 return (-1);
539
540 /*
541 * We've filled the buffer, but still haven't seen \n. Keep eating
542 * until we find it; we don't expect this to happen, but this is
543 * defensive.
544 */
545 if (c != '\n') {
546 while ((r = read(clifd, &c, sizeof (c))) > 0)
547 if (c == '\n')
548 break;
549 }
550
551 /*
552 * Parse buffer for message of the form: IDENT <pid> <locale>
553 */
554 bufp = buf;
555 if (strncmp(bufp, "IDENT ", 6) != 0)
556 return (-1);
557 bufp += 6;
558 errno = 0;
559 *pid = strtoll(bufp, &bufp, 10);
560 if (errno != 0)
561 return (-1);
562
563 while (*bufp != '\0' && isspace(*bufp))
564 bufp++;
565 (void) strlcpy(locale, bufp, locale_len);
566
567 return (0);
568 }
569
570 static int
571 accept_client(int servfd, pid_t *pid, char *locale, size_t locale_len)
572 {
573 int connfd;
574 struct sockaddr_un cliaddr;
575 socklen_t clilen;
576
577 clilen = sizeof (cliaddr);
578 connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
579 if (connfd == -1)
580 return (-1);
581 if (get_client_ident(connfd, pid, locale, locale_len) == -1) {
582 (void) shutdown(connfd, SHUT_RDWR);
583 (void) close(connfd);
584 return (-1);
585 }
586 (void) write(connfd, "OK\n", 3);
587 return (connfd);
588 }
589
590 static void
591 reject_client(int servfd, pid_t clientpid)
592 {
593 int connfd;
594 struct sockaddr_un cliaddr;
595 socklen_t clilen;
596 char nak[MAXPATHLEN];
597
598 clilen = sizeof (cliaddr);
599 connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
600
601 /*
602 * After hear its ident string, tell client to get lost.
603 */
604 if (get_client_ident(connfd, NULL, NULL, 0) == 0) {
605 (void) snprintf(nak, sizeof (nak), "%lu\n",
606 clientpid);
607 (void) write(connfd, nak, strlen(nak));
608 }
609 (void) shutdown(connfd, SHUT_RDWR);
610 (void) close(connfd);
611 }
612
613 static void
614 event_message(int clifd, char *clilocale, zone_evt_t evt)
615 {
616 char *str, *lstr = NULL;
617 char lmsg[BUFSIZ];
618 char outbuf[BUFSIZ];
619
620 if (clifd == -1)
621 return;
622
623 switch (evt) {
624 case Z_EVT_ZONE_BOOTING:
625 if (*boot_args == '\0') {
626 str = "NOTICE: Zone booting up";
627 break;
628 }
629 /*LINTED*/
630 (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
631 "NOTICE: Zone booting up with arguments: %s"), boot_args);
632 lstr = lmsg;
633 break;
634 case Z_EVT_ZONE_READIED:
635 str = "NOTICE: Zone readied";
636 break;
637 case Z_EVT_ZONE_HALTED:
638 str = "NOTICE: Zone halted";
639 break;
640 case Z_EVT_ZONE_REBOOTING:
641 if (*boot_args == '\0') {
642 str = "NOTICE: Zone rebooting";
643 break;
644 }
645 /*LINTED*/
646 (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
647 "NOTICE: Zone rebooting with arguments: %s"), boot_args);
648 lstr = lmsg;
649 break;
650 case Z_EVT_ZONE_UNINSTALLING:
651 str = "NOTICE: Zone is being uninstalled. Disconnecting...";
652 break;
653 case Z_EVT_ZONE_BOOTFAILED:
654 str = "NOTICE: Zone boot failed";
655 break;
656 case Z_EVT_ZONE_BADARGS:
657 /*LINTED*/
658 (void) snprintf(lmsg, sizeof (lmsg),
659 localize_msg(clilocale,
660 "WARNING: Ignoring invalid boot arguments: %s"),
661 bad_boot_arg);
662 lstr = lmsg;
663 break;
664 default:
665 return;
666 }
667
668 if (lstr == NULL)
669 lstr = localize_msg(clilocale, str);
670 (void) snprintf(outbuf, sizeof (outbuf), "\r\n[%s]\r\n", lstr);
671 (void) write(clifd, outbuf, strlen(outbuf));
672 }
673
691 * and disconnects any clients that might try to hook up with the zone while
692 * the console is in use.
693 *
694 * When the client first calls us up, it is expected to send a line giving
695 * its "identity"; this consists of the string 'IDENT <pid> <locale>'.
696 * This is so that we can report that the console is busy along with
697 * some diagnostics about who has it busy; the locale is used so that
698 * asynchronous messages about zone state (like the NOTICE: zone halted
699 * messages) can be output in the user's locale.
700 */
701 static void
702 do_console_io(zlog_t *zlogp, int consfd, int servfd)
703 {
704 struct pollfd pollfds[4];
705 char ibuf[BUFSIZ];
706 int cc, ret;
707 int clifd = -1;
708 int pollerr = 0;
709 char clilocale[MAXPATHLEN];
710 pid_t clipid = 0;
711
712 /* console side, watch for read events */
713 pollfds[0].fd = consfd;
714 pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND |
715 POLLPRI | POLLERR | POLLHUP | POLLNVAL;
716
717 /* client side, watch for read events */
718 pollfds[1].fd = clifd;
719 pollfds[1].events = pollfds[0].events;
720
721 /* the server socket; watch for events (new connections) */
722 pollfds[2].fd = servfd;
723 pollfds[2].events = pollfds[0].events;
724
725 /* the eventstram; watch for events (e.g.: zone halted) */
726 pollfds[3].fd = eventstream[1];
727 pollfds[3].events = pollfds[0].events;
728
729 for (;;) {
730 pollfds[0].revents = pollfds[1].revents = 0;
784 if (pollfds[2].revents &&
785 (pollfds[2].revents & (POLLIN | POLLRDNORM))) {
786 if (clifd != -1) {
787 /*
788 * Test the client to see if it is really
789 * still alive. If it has died but we
790 * haven't yet detected that, we might
791 * deny a legitimate connect attempt. If it
792 * is dead, we break out; once we tear down
793 * the old connection, the new connection
794 * will happen.
795 */
796 if (test_client(clifd) == -1) {
797 break;
798 }
799 /* we're already handling a client */
800 reject_client(servfd, clipid);
801
802
803 } else if ((clifd = accept_client(servfd, &clipid,
804 clilocale, sizeof (clilocale))) != -1) {
805 pollfds[1].fd = clifd;
806
807 } else {
808 break;
809 }
810 }
811
812 /*
813 * Watch for events on the eventstream. This is how we get
814 * notified of the zone halting, etc. It provides us a
815 * "wakeup" from poll when important things happen, which
816 * is good.
817 */
818 if (pollfds[3].revents) {
819 int evt = eventstream_read();
820 /*
821 * After we drain out the event, if we aren't servicing
822 * a console client, we hop back out to our caller,
823 * which will check to see if it is time to shutdown
824 * the daemon, or if we should take another console
825 * service lap.
826 */
827 if (clifd == -1) {
828 break;
829 }
830 event_message(clifd, clilocale, evt);
831 /*
832 * Special handling for the message that the zone is
833 * uninstalling; we boot the client, then break out
834 * of this function. When we return to the
835 * serve_console loop, we will see that the zone is
836 * in a state < READY, and so zoneadmd will shutdown.
837 */
838 if (evt == Z_EVT_ZONE_UNINSTALLING) {
839 break;
840 }
841 }
842
843 }
844
845 if (clifd != -1) {
846 (void) shutdown(clifd, SHUT_RDWR);
847 (void) close(clifd);
848 }
849 }
850
851 int
852 init_console(zlog_t *zlogp)
853 {
854 if (init_console_dev(zlogp) == -1) {
855 zerror(zlogp, B_FALSE,
856 "console setup: device initialization failed");
857 return (-1);
858 }
859
860 if ((serverfd = init_console_sock(zlogp)) == -1) {
|
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 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2012 Joyent, Inc. All rights reserved.
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 */
28
29 /*
30 * Console support for zones requires a significant infrastructure. The
31 * core pieces are contained in this file, but other portions of note
32 * are in the zlogin(1M) command, the zcons(7D) driver, and in the
33 * devfsadm(1M) misc_link generator.
34 *
35 * Care is taken to make the console behave in an "intuitive" fashion for
36 * administrators. Essentially, we try as much as possible to mimic the
37 * experience of using a system via a tip line and system controller.
38 *
39 * The zone console architecture looks like this:
40 *
41 * Global Zone | Non-Global Zone
42 * .--------------. |
43 * .-----------. | zoneadmd -z | | .--------. .---------.
44 * | zlogin -C | | myzone | | | ttymon | | syslogd |
45 * `-----------' `--------------' | `--------' `---------'
46 * | | | | | | |
493 return (-1);
494 }
495
496 static void
497 destroy_console_sock(int servfd)
498 {
499 char path[MAXPATHLEN];
500
501 (void) snprintf(path, sizeof (path), CONSOLE_SOCKPATH, zone_name);
502 (void) unlink(path);
503 (void) shutdown(servfd, SHUT_RDWR);
504 (void) close(servfd);
505 }
506
507 /*
508 * Read the "ident" string from the client's descriptor; this routine also
509 * tolerates being called with pid=NULL, for times when you want to "eat"
510 * the ident string from a client without saving it.
511 */
512 static int
513 get_client_ident(int clifd, pid_t *pid, char *locale, size_t locale_len,
514 int *disconnect)
515 {
516 char buf[BUFSIZ], *bufp;
517 size_t buflen = sizeof (buf);
518 char c = '\0';
519 int i = 0, r;
520
521 /* "eat up the ident string" case, for simplicity */
522 if (pid == NULL) {
523 assert(locale == NULL && locale_len == 0);
524 while (read(clifd, &c, 1) == 1) {
525 if (c == '\n')
526 return (0);
527 }
528 }
529
530 bzero(buf, sizeof (buf));
531 while ((buflen > 1) && (r = read(clifd, &c, 1)) == 1) {
532 buflen--;
533 if (c == '\n')
534 break;
535
536 buf[i] = c;
537 i++;
538 }
539 if (r == -1)
540 return (-1);
541
542 /*
543 * We've filled the buffer, but still haven't seen \n. Keep eating
544 * until we find it; we don't expect this to happen, but this is
545 * defensive.
546 */
547 if (c != '\n') {
548 while ((r = read(clifd, &c, sizeof (c))) > 0)
549 if (c == '\n')
550 break;
551 }
552
553 /*
554 * Parse buffer for message of the form:
555 * IDENT <pid> <locale> <disconnect flag>
556 */
557 bufp = buf;
558 if (strncmp(bufp, "IDENT ", 6) != 0)
559 return (-1);
560 bufp += 6;
561 errno = 0;
562 *pid = strtoll(bufp, &bufp, 10);
563 if (errno != 0)
564 return (-1);
565
566 while (*bufp != '\0' && isspace(*bufp))
567 bufp++;
568 buflen = strlen(bufp) - 1;
569 *disconnect = atoi(&bufp[buflen]);
570 bufp[buflen - 1] = '\0';
571 (void) strlcpy(locale, bufp, locale_len);
572
573 return (0);
574 }
575
576 static int
577 accept_client(int servfd, pid_t *pid, char *locale, size_t locale_len,
578 int *disconnect)
579 {
580 int connfd;
581 struct sockaddr_un cliaddr;
582 socklen_t clilen;
583
584 clilen = sizeof (cliaddr);
585 connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
586 if (connfd == -1)
587 return (-1);
588 if (get_client_ident(connfd, pid, locale, locale_len,
589 disconnect) == -1) {
590 (void) shutdown(connfd, SHUT_RDWR);
591 (void) close(connfd);
592 return (-1);
593 }
594 (void) write(connfd, "OK\n", 3);
595 return (connfd);
596 }
597
598 static void
599 reject_client(int servfd, pid_t clientpid)
600 {
601 int connfd;
602 struct sockaddr_un cliaddr;
603 socklen_t clilen;
604 char nak[MAXPATHLEN];
605
606 clilen = sizeof (cliaddr);
607 connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
608
609 /*
610 * After hear its ident string, tell client to get lost.
611 */
612 if (get_client_ident(connfd, NULL, NULL, 0, NULL) == 0) {
613 (void) snprintf(nak, sizeof (nak), "%lu\n",
614 clientpid);
615 (void) write(connfd, nak, strlen(nak));
616 }
617 (void) shutdown(connfd, SHUT_RDWR);
618 (void) close(connfd);
619 }
620
621 static void
622 event_message(int clifd, char *clilocale, zone_evt_t evt, int dflag)
623 {
624 char *str, *lstr = NULL;
625 char lmsg[BUFSIZ];
626 char outbuf[BUFSIZ];
627
628 if (clifd == -1)
629 return;
630
631 switch (evt) {
632 case Z_EVT_ZONE_BOOTING:
633 if (*boot_args == '\0') {
634 str = "NOTICE: Zone booting up";
635 break;
636 }
637 /*LINTED*/
638 (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
639 "NOTICE: Zone booting up with arguments: %s"), boot_args);
640 lstr = lmsg;
641 break;
642 case Z_EVT_ZONE_READIED:
643 str = "NOTICE: Zone readied";
644 break;
645 case Z_EVT_ZONE_HALTED:
646 if (dflag)
647 str = "NOTICE: Zone halted. Disconnecting...";
648 else
649 str = "NOTICE: Zone halted";
650 break;
651 case Z_EVT_ZONE_REBOOTING:
652 if (*boot_args == '\0') {
653 str = "NOTICE: Zone rebooting";
654 break;
655 }
656 /*LINTED*/
657 (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
658 "NOTICE: Zone rebooting with arguments: %s"), boot_args);
659 lstr = lmsg;
660 break;
661 case Z_EVT_ZONE_UNINSTALLING:
662 str = "NOTICE: Zone is being uninstalled. Disconnecting...";
663 break;
664 case Z_EVT_ZONE_BOOTFAILED:
665 if (dflag)
666 str = "NOTICE: Zone boot failed. Disconnecting...";
667 else
668 str = "NOTICE: Zone boot failed";
669 break;
670 case Z_EVT_ZONE_BADARGS:
671 /*LINTED*/
672 (void) snprintf(lmsg, sizeof (lmsg),
673 localize_msg(clilocale,
674 "WARNING: Ignoring invalid boot arguments: %s"),
675 bad_boot_arg);
676 lstr = lmsg;
677 break;
678 default:
679 return;
680 }
681
682 if (lstr == NULL)
683 lstr = localize_msg(clilocale, str);
684 (void) snprintf(outbuf, sizeof (outbuf), "\r\n[%s]\r\n", lstr);
685 (void) write(clifd, outbuf, strlen(outbuf));
686 }
687
705 * and disconnects any clients that might try to hook up with the zone while
706 * the console is in use.
707 *
708 * When the client first calls us up, it is expected to send a line giving
709 * its "identity"; this consists of the string 'IDENT <pid> <locale>'.
710 * This is so that we can report that the console is busy along with
711 * some diagnostics about who has it busy; the locale is used so that
712 * asynchronous messages about zone state (like the NOTICE: zone halted
713 * messages) can be output in the user's locale.
714 */
715 static void
716 do_console_io(zlog_t *zlogp, int consfd, int servfd)
717 {
718 struct pollfd pollfds[4];
719 char ibuf[BUFSIZ];
720 int cc, ret;
721 int clifd = -1;
722 int pollerr = 0;
723 char clilocale[MAXPATHLEN];
724 pid_t clipid = 0;
725 int disconnect = 0;
726
727 /* console side, watch for read events */
728 pollfds[0].fd = consfd;
729 pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND |
730 POLLPRI | POLLERR | POLLHUP | POLLNVAL;
731
732 /* client side, watch for read events */
733 pollfds[1].fd = clifd;
734 pollfds[1].events = pollfds[0].events;
735
736 /* the server socket; watch for events (new connections) */
737 pollfds[2].fd = servfd;
738 pollfds[2].events = pollfds[0].events;
739
740 /* the eventstram; watch for events (e.g.: zone halted) */
741 pollfds[3].fd = eventstream[1];
742 pollfds[3].events = pollfds[0].events;
743
744 for (;;) {
745 pollfds[0].revents = pollfds[1].revents = 0;
799 if (pollfds[2].revents &&
800 (pollfds[2].revents & (POLLIN | POLLRDNORM))) {
801 if (clifd != -1) {
802 /*
803 * Test the client to see if it is really
804 * still alive. If it has died but we
805 * haven't yet detected that, we might
806 * deny a legitimate connect attempt. If it
807 * is dead, we break out; once we tear down
808 * the old connection, the new connection
809 * will happen.
810 */
811 if (test_client(clifd) == -1) {
812 break;
813 }
814 /* we're already handling a client */
815 reject_client(servfd, clipid);
816
817
818 } else if ((clifd = accept_client(servfd, &clipid,
819 clilocale, sizeof (clilocale),
820 &disconnect)) != -1) {
821 pollfds[1].fd = clifd;
822
823 } else {
824 break;
825 }
826 }
827
828 /*
829 * Watch for events on the eventstream. This is how we get
830 * notified of the zone halting, etc. It provides us a
831 * "wakeup" from poll when important things happen, which
832 * is good.
833 */
834 if (pollfds[3].revents) {
835 int evt = eventstream_read();
836 /*
837 * After we drain out the event, if we aren't servicing
838 * a console client, we hop back out to our caller,
839 * which will check to see if it is time to shutdown
840 * the daemon, or if we should take another console
841 * service lap.
842 */
843 if (clifd == -1) {
844 break;
845 }
846 event_message(clifd, clilocale, evt, disconnect);
847 /*
848 * Special handling for the message that the zone is
849 * uninstalling; we boot the client, then break out
850 * of this function. When we return to the
851 * serve_console loop, we will see that the zone is
852 * in a state < READY, and so zoneadmd will shutdown.
853 */
854 if (evt == Z_EVT_ZONE_UNINSTALLING) {
855 break;
856 }
857 /*
858 * Diconnect if -C and -d options were specified and
859 * zone was halted or failed to boot.
860 */
861 if ((evt == Z_EVT_ZONE_HALTED ||
862 evt == Z_EVT_ZONE_BOOTFAILED) && disconnect) {
863 break;
864 }
865 }
866
867 }
868
869 if (clifd != -1) {
870 (void) shutdown(clifd, SHUT_RDWR);
871 (void) close(clifd);
872 }
873 }
874
875 int
876 init_console(zlog_t *zlogp)
877 {
878 if (init_console_dev(zlogp) == -1) {
879 zerror(zlogp, B_FALSE,
880 "console setup: device initialization failed");
881 return (-1);
882 }
883
884 if ((serverfd = init_console_sock(zlogp)) == -1) {
|