source: mainline/uspace/srv/taskmon/taskmon.c@ 4c6fd56

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4c6fd56 was 4c6fd56, checked in by Jiri Svoboda <jiri@…>, 22 months ago

loc_server_register() should be callable more than once (API only)

Now loc_server_register() returns a pointer to a loc_srv_t object,
that is then passed to loc_service_register() and
loc_service_add_to_cat().

Added loc_server_unregister() that unregisters the server
and frees the loc_srv_t object.

Updated all callers. The implementation, however, is a stub.
It is not actually possible to call loc_server_register() more
than once, yet.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[a074b4f]1/*
[4c6fd56]2 * Copyright (c) 2023 Jiri Svoboda
[a074b4f]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 <task.h>
[92c1680]41#include <ipc/corecfg.h>
42#include <loc.h>
[a074b4f]43#include <macros.h>
44#include <errno.h>
[d9fae235]45#include <str_error.h>
[a074b4f]46
47#define NAME "taskmon"
48
[92c1680]49static bool write_core_files;
50
[984a9ba]51static void corecfg_client_conn(ipc_call_t *, void *);
[92c1680]52
[01c3bb4]53static void fault_event(ipc_call_t *call, void *arg)
[a074b4f]54{
[a000878c]55 const char *fname;
[a074b4f]56 char *s_taskid;
[92c1680]57 char *dump_fname;
[b7fd2a0]58 errno_t rc;
[a074b4f]59
60 task_id_t taskid;
61 uintptr_t thread;
62
[fafb8e5]63 taskid = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
64 thread = ipc_get_arg3(call);
[a074b4f]65
[7e752b2]66 if (asprintf(&s_taskid, "%" PRIu64, taskid) < 0) {
[a074b4f]67 printf("Memory allocation failed.\n");
68 return;
69 }
70
[7e752b2]71 printf(NAME ": Task %" PRIu64 " fault in thread %p.\n", taskid,
72 (void *) thread);
[0485135]73
74 fname = "/app/taskdump";
75
[92c1680]76 if (write_core_files) {
77 if (asprintf(&dump_fname, "/data/core%" PRIu64, taskid) < 0) {
78 printf("Memory allocation failed.\n");
79 return;
80 }
[0485135]81
[92c1680]82 printf(NAME ": Executing %s -c %s -t %s\n", fname, dump_fname, s_taskid);
[1c635d6]83 rc = task_spawnl(NULL, NULL, fname, fname, "-c", dump_fname, "-t", s_taskid,
[92c1680]84 NULL);
85 } else {
86 printf(NAME ": Executing %s -t %s\n", fname, s_taskid);
[1c635d6]87 rc = task_spawnl(NULL, NULL, fname, fname, "-t", s_taskid, NULL);
[03333bc]88 }
89
[0485135]90 if (rc != EOK) {
[d9fae235]91 printf("%s: Error spawning %s (%s).\n", NAME, fname,
[0485135]92 str_error(rc));
93 }
[a074b4f]94}
95
[984a9ba]96static void corecfg_get_enable_srv(ipc_call_t *icall)
[92c1680]97{
[984a9ba]98 async_answer_1(icall, EOK, write_core_files);
[92c1680]99}
100
[984a9ba]101static void corecfg_set_enable_srv(ipc_call_t *icall)
[92c1680]102{
[fafb8e5]103 write_core_files = ipc_get_arg1(icall);
[984a9ba]104 async_answer_0(icall, EOK);
[92c1680]105}
106
[984a9ba]107static void corecfg_client_conn(ipc_call_t *icall, void *arg)
[92c1680]108{
109 /* Accept the connection */
[beb83c1]110 async_accept_0(icall);
[92c1680]111
112 while (true) {
113 ipc_call_t call;
[984a9ba]114 async_get_call(&call);
[fafb8e5]115 sysarg_t method = ipc_get_imethod(&call);
[92c1680]116
117 if (!method) {
118 /* The other side has hung up */
[984a9ba]119 async_answer_0(&call, EOK);
[92c1680]120 return;
121 }
122
123 switch (method) {
124 case CORECFG_GET_ENABLE:
[984a9ba]125 corecfg_get_enable_srv(&call);
[92c1680]126 break;
127 case CORECFG_SET_ENABLE:
[984a9ba]128 corecfg_set_enable_srv(&call);
[92c1680]129 break;
[984a9ba]130 default:
131 async_answer_0(&call, ENOTSUP);
[92c1680]132 }
133 }
134}
135
[a074b4f]136int main(int argc, char *argv[])
137{
[4c6fd56]138 loc_srv_t *srv;
139
[79ae36dd]140 printf("%s: Task Monitoring Service\n", NAME);
[a35b458]141
[92c1680]142#ifdef CONFIG_WRITE_CORE_FILES
143 write_core_files = true;
144#else
145 write_core_files = false;
146#endif
[8820544]147 if (async_event_subscribe(EVENT_FAULT, fault_event, NULL) != EOK) {
[79ae36dd]148 printf("%s: Error registering fault notifications.\n", NAME);
[a074b4f]149 return -1;
150 }
[a35b458]151
[b688fd8]152 async_set_fallback_port_handler(corecfg_client_conn, NULL);
[a35b458]153
[4c6fd56]154 errno_t rc = loc_server_register(NAME, &srv);
[92c1680]155 if (rc != EOK) {
[c1694b6b]156 printf("%s: Failed registering server: %s.\n",
157 NAME, str_error(rc));
[92c1680]158 return -1;
159 }
[a35b458]160
[92c1680]161 service_id_t sid;
[4c6fd56]162 rc = loc_service_register(srv, SERVICE_NAME_CORECFG, &sid);
[92c1680]163 if (rc != EOK) {
[4c6fd56]164 loc_server_unregister(srv);
[c1694b6b]165 printf("%s: Failed registering service: %s.\n",
166 NAME, str_error(rc));
[92c1680]167 return -1;
168 }
[a35b458]169
[79ae36dd]170 task_retval(0);
[a074b4f]171 async_manager();
[a35b458]172
[a074b4f]173 return 0;
174}
175
176/** @}
177 */
Note: See TracBrowser for help on using the repository browser.