[49f78c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[df747bd8] | 29 | /** @addtogroup getterm GetTerm
|
---|
[49f78c7] | 30 | * @brief Console initialization task.
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[8d2dd7f2] | 37 | #include <stdint.h>
|
---|
[49f78c7] | 38 | #include <stdio.h>
|
---|
| 39 | #include <task.h>
|
---|
[d9fae235] | 40 | #include <str_error.h>
|
---|
[0485135] | 41 | #include <errno.h>
|
---|
[593e023] | 42 | #include <loc.h>
|
---|
[fcab7ef] | 43 | #include <vfs/vfs.h>
|
---|
[49f78c7] | 44 | #include "version.h"
|
---|
[4deb8b5] | 45 | #include "welcome.h"
|
---|
[49f78c7] | 46 |
|
---|
[d9fae235] | 47 | #define APP_NAME "getterm"
|
---|
| 48 |
|
---|
[49f78c7] | 49 | static void usage(void)
|
---|
| 50 | {
|
---|
[593e023] | 51 | printf("Usage: %s <terminal> <locfs> [--msg] [--wait] -- "
|
---|
| 52 | "<command> [<arguments...>]\n", APP_NAME);
|
---|
| 53 | printf(" <terminal> Terminal device\n");
|
---|
| 54 | printf(" <locfs> Mount point of locfs\n");
|
---|
| 55 | printf(" --msg Print welcome message\n");
|
---|
| 56 | printf(" --wait Wait for the terminal to be ready\n");
|
---|
[49f78c7] | 57 | }
|
---|
| 58 |
|
---|
[b19e892] | 59 | static void reopen(FILE **stream, int fd, const char *path, int mode,
|
---|
| 60 | const char *fmode)
|
---|
[49f78c7] | 61 | {
|
---|
[2b88074b] | 62 | if (fclose(*stream))
|
---|
| 63 | return;
|
---|
[49f78c7] | 64 |
|
---|
[2b88074b] | 65 | *stream = NULL;
|
---|
| 66 |
|
---|
[b19e892] | 67 | int oldfd = vfs_lookup_open(path, WALK_REGULAR, mode);
|
---|
[2b88074b] | 68 | if (oldfd < 0)
|
---|
| 69 | return;
|
---|
| 70 |
|
---|
| 71 | if (oldfd != fd) {
|
---|
[fcab7ef] | 72 | if (vfs_clone(oldfd, fd, false) != fd)
|
---|
[2b88074b] | 73 | return;
|
---|
| 74 |
|
---|
[9c4cf0d] | 75 | if (vfs_put(oldfd))
|
---|
[2b88074b] | 76 | return;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[b19e892] | 79 | *stream = fdopen(fd, fmode);
|
---|
[49f78c7] | 80 | }
|
---|
| 81 |
|
---|
[d52b0044] | 82 | int main(int argc, char *argv[])
|
---|
[49f78c7] | 83 | {
|
---|
[dd567c6] | 84 | argv++;
|
---|
| 85 | argc--;
|
---|
[593e023] | 86 | if (argc < 4) {
|
---|
[49f78c7] | 87 | usage();
|
---|
[593e023] | 88 | return 1;
|
---|
[49f78c7] | 89 | }
|
---|
[593e023] | 90 |
|
---|
| 91 | char *term = *argv;
|
---|
| 92 | argv++;
|
---|
| 93 | argc--;
|
---|
| 94 |
|
---|
| 95 | char *locfs = *argv;
|
---|
| 96 | argv++;
|
---|
| 97 | argc--;
|
---|
| 98 |
|
---|
| 99 | bool print_msg = false;
|
---|
| 100 | bool wait = false;
|
---|
| 101 |
|
---|
| 102 | while ((argc > 0) && (str_cmp(*argv, "--") != 0)) {
|
---|
| 103 | if (str_cmp(*argv, "--msg") == 0) {
|
---|
| 104 | print_msg = true;
|
---|
| 105 | } else if (str_cmp(*argv, "--wait") == 0) {
|
---|
| 106 | wait = true;
|
---|
| 107 | } else {
|
---|
| 108 | usage();
|
---|
| 109 | return 2;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[dd567c6] | 112 | argv++;
|
---|
| 113 | argc--;
|
---|
[4deb8b5] | 114 | }
|
---|
[593e023] | 115 |
|
---|
| 116 | if (argc < 1) {
|
---|
[4deb8b5] | 117 | usage();
|
---|
[593e023] | 118 | return 3;
|
---|
[4deb8b5] | 119 | }
|
---|
[49f78c7] | 120 |
|
---|
[593e023] | 121 | /* Skip "--" */
|
---|
| 122 | argv++;
|
---|
| 123 | argc--;
|
---|
| 124 |
|
---|
| 125 | char *cmd = *argv;
|
---|
| 126 | char **args = argv;
|
---|
| 127 |
|
---|
| 128 | if (wait) {
|
---|
| 129 | /* Wait for the terminal service to be ready */
|
---|
| 130 | service_id_t service_id;
|
---|
| 131 | int rc = loc_service_get_id(term, &service_id, IPC_FLAG_BLOCKING);
|
---|
| 132 | if (rc != EOK) {
|
---|
| 133 | printf("%s: Error waiting on %s (%s)\n", APP_NAME, term,
|
---|
| 134 | str_error(rc));
|
---|
| 135 | return rc;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | char term_node[LOC_NAME_MAXLEN];
|
---|
| 140 | snprintf(term_node, LOC_NAME_MAXLEN, "%s/%s", locfs, term);
|
---|
| 141 |
|
---|
[b19e892] | 142 | reopen(&stdin, 0, term_node, MODE_READ, "r");
|
---|
| 143 | reopen(&stdout, 1, term_node, MODE_WRITE, "w");
|
---|
| 144 | reopen(&stderr, 2, term_node, MODE_WRITE, "w");
|
---|
[49f78c7] | 145 |
|
---|
[2b88074b] | 146 | if (stdin == NULL)
|
---|
[593e023] | 147 | return 4;
|
---|
[49f78c7] | 148 |
|
---|
[2b88074b] | 149 | if (stdout == NULL)
|
---|
[593e023] | 150 | return 5;
|
---|
[2b88074b] | 151 |
|
---|
| 152 | if (stderr == NULL)
|
---|
[593e023] | 153 | return 6;
|
---|
[2b88074b] | 154 |
|
---|
[884c56b] | 155 | /*
|
---|
| 156 | * FIXME: fdopen() should actually detect that we are opening a console
|
---|
| 157 | * and it should set line-buffering mode automatically.
|
---|
| 158 | */
|
---|
| 159 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
|
---|
| 160 |
|
---|
[4deb8b5] | 161 | version_print(term);
|
---|
[593e023] | 162 | if (print_msg)
|
---|
[4deb8b5] | 163 | welcome_msg_print();
|
---|
[593e023] | 164 |
|
---|
| 165 | task_id_t id;
|
---|
[1c635d6] | 166 | task_wait_t twait;
|
---|
[593e023] | 167 |
|
---|
[1c635d6] | 168 | int rc = task_spawnv(&id, &twait, cmd, (const char * const *) args);
|
---|
[d52b0044] | 169 | if (rc != EOK) {
|
---|
[593e023] | 170 | printf("%s: Error spawning %s (%s)\n", APP_NAME, cmd,
|
---|
[d52b0044] | 171 | str_error(rc));
|
---|
[593e023] | 172 | return rc;
|
---|
[c7dc8ad] | 173 | }
|
---|
[593e023] | 174 |
|
---|
| 175 | task_exit_t texit;
|
---|
| 176 | int retval;
|
---|
[1c635d6] | 177 | rc = task_wait(&twait, &texit, &retval);
|
---|
[d52b0044] | 178 | if (rc != EOK) {
|
---|
[593e023] | 179 | printf("%s: Error waiting for %s (%s)\n", APP_NAME, cmd,
|
---|
[d52b0044] | 180 | str_error(rc));
|
---|
[593e023] | 181 | return rc;
|
---|
[d52b0044] | 182 | }
|
---|
[593e023] | 183 |
|
---|
[d52b0044] | 184 | return 0;
|
---|
[49f78c7] | 185 | }
|
---|
| 186 |
|
---|
| 187 | /** @}
|
---|
| 188 | */
|
---|