source: mainline/uspace/srv/taskmon/taskmon.c@ d7dadcb4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d7dadcb4 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: 4.4 KB
Line 
1/*
2 * Copyright (c) 2013 Jiri Svoboda
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 taskmon
30 * @brief
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdio.h>
38#include <async.h>
39#include <ipc/services.h>
40#include <sys/typefmt.h>
41#include <task.h>
42#include <ipc/corecfg.h>
43#include <loc.h>
44#include <macros.h>
45#include <errno.h>
46#include <str_error.h>
47
48#define NAME "taskmon"
49
50static bool write_core_files;
51
52static void corecfg_client_conn(ipc_callid_t , ipc_call_t *, void *);
53
54static void fault_event(ipc_callid_t callid, ipc_call_t *call, void *arg)
55{
56 const char *fname;
57 char *s_taskid;
58 char *dump_fname;
59 int rc;
60
61 task_id_t taskid;
62 uintptr_t thread;
63
64 taskid = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
65 thread = IPC_GET_ARG3(*call);
66
67 if (asprintf(&s_taskid, "%" PRIu64, taskid) < 0) {
68 printf("Memory allocation failed.\n");
69 return;
70 }
71
72 printf(NAME ": Task %" PRIu64 " fault in thread %p.\n", taskid,
73 (void *) thread);
74
75 fname = "/app/taskdump";
76
77 if (write_core_files) {
78 if (asprintf(&dump_fname, "/data/core%" PRIu64, taskid) < 0) {
79 printf("Memory allocation failed.\n");
80 return;
81 }
82
83 printf(NAME ": Executing %s -c %s -t %s\n", fname, dump_fname, s_taskid);
84 rc = task_spawnl(NULL, NULL, fname, fname, "-c", dump_fname, "-t", s_taskid,
85 NULL);
86 } else {
87 printf(NAME ": Executing %s -t %s\n", fname, s_taskid);
88 rc = task_spawnl(NULL, NULL, fname, fname, "-t", s_taskid, NULL);
89 }
90
91 if (rc != EOK) {
92 printf("%s: Error spawning %s (%s).\n", NAME, fname,
93 str_error(rc));
94 }
95}
96
97static void corecfg_get_enable_srv(ipc_callid_t iid, ipc_call_t *icall)
98{
99 async_answer_1(iid, EOK, write_core_files);
100}
101
102static void corecfg_set_enable_srv(ipc_callid_t iid, ipc_call_t *icall)
103{
104 write_core_files = IPC_GET_ARG1(*icall);
105 async_answer_0(iid, EOK);
106}
107
108static void corecfg_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
109{
110 /* Accept the connection */
111 async_answer_0(iid, EOK);
112
113 while (true) {
114 ipc_call_t call;
115 ipc_callid_t callid = async_get_call(&call);
116 sysarg_t method = IPC_GET_IMETHOD(call);
117
118 if (!method) {
119 /* The other side has hung up */
120 async_answer_0(callid, EOK);
121 return;
122 }
123
124 switch (method) {
125 case CORECFG_GET_ENABLE:
126 corecfg_get_enable_srv(callid, &call);
127 break;
128 case CORECFG_SET_ENABLE:
129 corecfg_set_enable_srv(callid, &call);
130 break;
131 }
132 }
133}
134
135int main(int argc, char *argv[])
136{
137 printf("%s: Task Monitoring Service\n", NAME);
138
139#ifdef CONFIG_WRITE_CORE_FILES
140 write_core_files = true;
141#else
142 write_core_files = false;
143#endif
144 if (async_event_subscribe(EVENT_FAULT, fault_event, NULL) != EOK) {
145 printf("%s: Error registering fault notifications.\n", NAME);
146 return -1;
147 }
148
149 async_set_client_connection(corecfg_client_conn);
150
151 int rc = loc_server_register(NAME);
152 if (rc != EOK) {
153 printf("%s: Failed registering server (%d).\n",
154 NAME, rc);
155 return -1;
156 }
157
158 service_id_t sid;
159 rc = loc_service_register(SERVICE_NAME_CORECFG, &sid);
160 if (rc != EOK) {
161 printf("%s: Failed registering service (%d).\n",
162 NAME, rc);
163 return -1;
164 }
165
166 task_retval(0);
167 async_manager();
168
169 return 0;
170}
171
172/** @}
173 */
Note: See TracBrowser for help on using the repository browser.