[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 <sys/types.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <fcntl.h>
|
---|
| 40 | #include <unistd.h>
|
---|
| 41 | #include <string.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <task.h>
|
---|
| 44 |
|
---|
| 45 | static void usage(void)
|
---|
| 46 | {
|
---|
[49647be] | 47 | printf("Usage: redir [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n");
|
---|
[c7dc8ad] | 48 | }
|
---|
| 49 |
|
---|
| 50 | static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
|
---|
| 51 | {
|
---|
| 52 | if (fclose(*stream))
|
---|
| 53 | return;
|
---|
| 54 |
|
---|
| 55 | *stream = NULL;
|
---|
| 56 |
|
---|
| 57 | int oldfd = open(path, flags);
|
---|
| 58 | if (oldfd < 0)
|
---|
| 59 | return;
|
---|
| 60 |
|
---|
| 61 | if (oldfd != fd) {
|
---|
| 62 | if (dup2(oldfd, fd) != fd)
|
---|
| 63 | return;
|
---|
| 64 |
|
---|
| 65 | if (close(oldfd))
|
---|
| 66 | return;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | *stream = fdopen(fd, mode);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | static task_id_t spawn(int argc, char *argv[])
|
---|
| 73 | {
|
---|
[36e9cd1] | 74 | char **args = (char **) calloc(argc + 1, sizeof(char *));
|
---|
[c7dc8ad] | 75 | if (!args) {
|
---|
| 76 | printf("No memory available\n");
|
---|
| 77 | return 0;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | int i;
|
---|
| 81 | for (i = 0; i < argc; i++)
|
---|
| 82 | args[i] = argv[i];
|
---|
| 83 |
|
---|
| 84 | args[argc] = NULL;
|
---|
| 85 |
|
---|
| 86 | task_id_t id = task_spawn(argv[0], args);
|
---|
| 87 |
|
---|
| 88 | free(args);
|
---|
| 89 |
|
---|
| 90 | if (id == 0)
|
---|
| 91 | printf("Error spawning %s\n", argv[0]);
|
---|
| 92 |
|
---|
| 93 | return id;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | int main(int argc, char *argv[])
|
---|
| 97 | {
|
---|
| 98 | if (argc < 3) {
|
---|
| 99 | usage();
|
---|
| 100 | return -1;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | int i;
|
---|
| 104 | for (i = 1; i < argc; i++) {
|
---|
| 105 | if (str_cmp(argv[i], "-i") == 0) {
|
---|
| 106 | i++;
|
---|
| 107 | if (i >= argc) {
|
---|
| 108 | usage();
|
---|
| 109 | return -2;
|
---|
| 110 | }
|
---|
| 111 | reopen(&stdin, 0, argv[i], O_RDONLY, "r");
|
---|
| 112 | } else if (str_cmp(argv[i], "-o") == 0) {
|
---|
| 113 | i++;
|
---|
| 114 | if (i >= argc) {
|
---|
| 115 | usage();
|
---|
| 116 | return -3;
|
---|
| 117 | }
|
---|
| 118 | reopen(&stdout, 1, argv[i], O_WRONLY | O_CREAT, "w");
|
---|
| 119 | } else if (str_cmp(argv[i], "-e") == 0) {
|
---|
| 120 | i++;
|
---|
| 121 | if (i >= argc) {
|
---|
| 122 | usage();
|
---|
| 123 | return -4;
|
---|
| 124 | }
|
---|
| 125 | reopen(&stderr, 2, argv[i], O_WRONLY | O_CREAT, "w");
|
---|
| 126 | } else if (str_cmp(argv[i], "--") == 0) {
|
---|
| 127 | i++;
|
---|
| 128 | break;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | if (i >= argc) {
|
---|
| 133 | usage();
|
---|
| 134 | return -5;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /*
|
---|
| 138 | * FIXME: fdopen() should actually detect that we are opening a console
|
---|
| 139 | * and it should set line-buffering mode automatically.
|
---|
| 140 | */
|
---|
| 141 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
|
---|
| 142 |
|
---|
| 143 | task_id_t id = spawn(argc - i, argv + i);
|
---|
| 144 |
|
---|
| 145 | if (id != 0) {
|
---|
| 146 | task_exit_t texit;
|
---|
| 147 | int retval;
|
---|
| 148 | task_wait(id, &texit, &retval);
|
---|
| 149 |
|
---|
| 150 | return retval;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | return -6;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /** @}
|
---|
| 157 | */
|
---|