source: mainline/uspace/app/redir/redir.c@ 2c7fdaa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2c7fdaa was 1c635d6, checked in by Martin Sucha <sucha14@…>, 11 years ago

Do not hold a task's return value after it has disconnected.

Holding the task's return value meant that if nobody waited
for task's result, it polluted NS's memory. This was apparently
done because of a race between spawning a task and waiting for it.

We solve this problem in another way: ns discards the return value
as soon as the task disconnects from it. This typically happens
when the task finishes its execution. In order to avoid the race,
we send the wait request to ns while spawning the task (i.e. when
we talk to the loader), but before we allow the loaded program
to run.

Fixes #132

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