[c7dc8ad] | 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 |
|
---|
| 29 | /** @addtogroup redir Redirector
|
---|
| 30 | * @brief Redirect stdin/stdout/stderr.
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdlib.h>
|
---|
[19f857a] | 38 | #include <str.h>
|
---|
[c7dc8ad] | 39 | #include <stdio.h>
|
---|
| 40 | #include <task.h>
|
---|
[d9fae235] | 41 | #include <str_error.h>
|
---|
[0485135] | 42 | #include <errno.h>
|
---|
[fcab7ef] | 43 | #include <vfs/vfs.h>
|
---|
[d9fae235] | 44 |
|
---|
| 45 | #define NAME "redir"
|
---|
[c7dc8ad] | 46 |
|
---|
| 47 | static void usage(void)
|
---|
| 48 | {
|
---|
[4688fab8] | 49 | fprintf(stderr, "Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
|
---|
[d9fae235] | 50 | NAME);
|
---|
[c7dc8ad] | 51 | }
|
---|
| 52 |
|
---|
[b19e892] | 53 | static void reopen(FILE **stream, int fd, const char *path, int flags, int mode,
|
---|
| 54 | const char *fmode)
|
---|
[c7dc8ad] | 55 | {
|
---|
| 56 | if (fclose(*stream))
|
---|
| 57 | return;
|
---|
[a35b458] | 58 |
|
---|
[c7dc8ad] | 59 | *stream = NULL;
|
---|
[a35b458] | 60 |
|
---|
[f77c1c9] | 61 | int oldfd;
|
---|
[b7fd2a0] | 62 | errno_t rc = vfs_lookup_open(path, WALK_REGULAR | flags, mode, &oldfd);
|
---|
[f77c1c9] | 63 | if (rc != EOK)
|
---|
[c7dc8ad] | 64 | return;
|
---|
[a35b458] | 65 |
|
---|
[c7dc8ad] | 66 | if (oldfd != fd) {
|
---|
[f77c1c9] | 67 | int newfd;
|
---|
| 68 | if (vfs_clone(oldfd, fd, false, &newfd) != EOK)
|
---|
[c7dc8ad] | 69 | return;
|
---|
[a35b458] | 70 |
|
---|
[f77c1c9] | 71 | assert(newfd == fd);
|
---|
| 72 |
|
---|
[9c4cf0d] | 73 | if (vfs_put(oldfd))
|
---|
[c7dc8ad] | 74 | return;
|
---|
| 75 | }
|
---|
[a35b458] | 76 |
|
---|
[b19e892] | 77 | *stream = fdopen(fd, fmode);
|
---|
[c7dc8ad] | 78 | }
|
---|
| 79 |
|
---|
[1c635d6] | 80 | static task_id_t spawn(task_wait_t *wait, int argc, char *argv[])
|
---|
[c7dc8ad] | 81 | {
|
---|
[0485135] | 82 | const char **args;
|
---|
[a7e7716] | 83 | task_id_t id = 0;
|
---|
[b7fd2a0] | 84 | errno_t rc;
|
---|
[0485135] | 85 |
|
---|
| 86 | args = (const char **) calloc(argc + 1, sizeof(char *));
|
---|
[c7dc8ad] | 87 | if (!args) {
|
---|
[4688fab8] | 88 | fprintf(stderr, "No memory available\n");
|
---|
[c7dc8ad] | 89 | return 0;
|
---|
| 90 | }
|
---|
[a35b458] | 91 |
|
---|
[c7dc8ad] | 92 | int i;
|
---|
| 93 | for (i = 0; i < argc; i++)
|
---|
| 94 | args[i] = argv[i];
|
---|
[a35b458] | 95 |
|
---|
[c7dc8ad] | 96 | args[argc] = NULL;
|
---|
[a35b458] | 97 |
|
---|
[1c635d6] | 98 | rc = task_spawnv(&id, wait, argv[0], args);
|
---|
[a35b458] | 99 |
|
---|
[c7dc8ad] | 100 | free(args);
|
---|
[a35b458] | 101 |
|
---|
[0485135] | 102 | if (rc != EOK) {
|
---|
[4688fab8] | 103 | fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, argv[0],
|
---|
[0485135] | 104 | str_error(rc));
|
---|
[a7e7716] | 105 | return 0;
|
---|
[0485135] | 106 | }
|
---|
[a35b458] | 107 |
|
---|
[c7dc8ad] | 108 | return id;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | int main(int argc, char *argv[])
|
---|
| 112 | {
|
---|
| 113 | if (argc < 3) {
|
---|
| 114 | usage();
|
---|
| 115 | return -1;
|
---|
| 116 | }
|
---|
[a35b458] | 117 |
|
---|
[c7dc8ad] | 118 | int i;
|
---|
| 119 | for (i = 1; i < argc; i++) {
|
---|
| 120 | if (str_cmp(argv[i], "-i") == 0) {
|
---|
| 121 | i++;
|
---|
| 122 | if (i >= argc) {
|
---|
| 123 | usage();
|
---|
| 124 | return -2;
|
---|
| 125 | }
|
---|
[b19e892] | 126 | reopen(&stdin, 0, argv[i], 0, MODE_READ, "r");
|
---|
[c7dc8ad] | 127 | } else if (str_cmp(argv[i], "-o") == 0) {
|
---|
| 128 | i++;
|
---|
| 129 | if (i >= argc) {
|
---|
| 130 | usage();
|
---|
| 131 | return -3;
|
---|
| 132 | }
|
---|
[b19e892] | 133 | reopen(&stdout, 1, argv[i], WALK_MAY_CREATE, MODE_WRITE,
|
---|
| 134 | "w");
|
---|
[c7dc8ad] | 135 | } else if (str_cmp(argv[i], "-e") == 0) {
|
---|
| 136 | i++;
|
---|
| 137 | if (i >= argc) {
|
---|
| 138 | usage();
|
---|
| 139 | return -4;
|
---|
| 140 | }
|
---|
[b19e892] | 141 | reopen(&stderr, 2, argv[i], WALK_MAY_CREATE, MODE_WRITE,
|
---|
| 142 | "w");
|
---|
[c7dc8ad] | 143 | } else if (str_cmp(argv[i], "--") == 0) {
|
---|
| 144 | i++;
|
---|
| 145 | break;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[a35b458] | 148 |
|
---|
[c7dc8ad] | 149 | if (i >= argc) {
|
---|
| 150 | usage();
|
---|
| 151 | return -5;
|
---|
| 152 | }
|
---|
[a35b458] | 153 |
|
---|
[c7dc8ad] | 154 | /*
|
---|
| 155 | * FIXME: fdopen() should actually detect that we are opening a console
|
---|
| 156 | * and it should set line-buffering mode automatically.
|
---|
| 157 | */
|
---|
| 158 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
|
---|
[1c635d6] | 159 |
|
---|
[1b20da0] | 160 | task_wait_t wait;
|
---|
[1c635d6] | 161 | task_id_t id = spawn(&wait, argc - i, argv + i);
|
---|
[a35b458] | 162 |
|
---|
[c7dc8ad] | 163 | if (id != 0) {
|
---|
| 164 | task_exit_t texit;
|
---|
| 165 | int retval;
|
---|
[1c635d6] | 166 | task_wait(&wait, &texit, &retval);
|
---|
[a35b458] | 167 |
|
---|
[c7dc8ad] | 168 | return retval;
|
---|
| 169 | }
|
---|
[a35b458] | 170 |
|
---|
[c7dc8ad] | 171 | return -6;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /** @}
|
---|
| 175 | */
|
---|