Persistently store taskmon configuration.
[helenos.git] / uspace / srv / taskmon / taskmon.c
blobfd594668e4dddec323e34fb818e9a17f4f70be9e
1 /*
2 * Copyright (c) 2024 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup taskmon
30 * @brief
31 * @{
33 /**
34 * @file
37 #include <async.h>
38 #include <errno.h>
39 #include <macros.h>
40 #include <ipc/services.h>
41 #include <ipc/corecfg.h>
42 #include <loc.h>
43 #include <sif.h>
44 #include <stdio.h>
45 #include <str.h>
46 #include <str_error.h>
47 #include <task.h>
49 #define NAME "taskmon"
51 static const char *taskmon_cfg_path = "/w/cfg/taskmon.sif";
53 static bool write_core_files;
55 static errno_t taskmon_load_cfg(const char *);
56 static errno_t taskmon_save_cfg(const char *);
57 static void corecfg_client_conn(ipc_call_t *, void *);
59 static void fault_event(ipc_call_t *call, void *arg)
61 const char *fname;
62 char *s_taskid;
63 char *dump_fname;
64 errno_t rc;
66 task_id_t taskid;
67 uintptr_t thread;
69 taskid = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
70 thread = ipc_get_arg3(call);
72 if (asprintf(&s_taskid, "%" PRIu64, taskid) < 0) {
73 printf("Memory allocation failed.\n");
74 return;
77 printf(NAME ": Task %" PRIu64 " fault in thread %p.\n", taskid,
78 (void *) thread);
80 fname = "/app/taskdump";
82 if (write_core_files) {
83 if (asprintf(&dump_fname, "/data/core%" PRIu64, taskid) < 0) {
84 printf("Memory allocation failed.\n");
85 return;
88 printf(NAME ": Executing %s -c %s -t %s\n", fname, dump_fname, s_taskid);
89 rc = task_spawnl(NULL, NULL, fname, fname, "-c", dump_fname, "-t", s_taskid,
90 NULL);
91 } else {
92 printf(NAME ": Executing %s -t %s\n", fname, s_taskid);
93 rc = task_spawnl(NULL, NULL, fname, fname, "-t", s_taskid, NULL);
96 if (rc != EOK) {
97 printf("%s: Error spawning %s (%s).\n", NAME, fname,
98 str_error(rc));
102 static void corecfg_get_enable_srv(ipc_call_t *icall)
104 async_answer_1(icall, EOK, write_core_files);
107 static void corecfg_set_enable_srv(ipc_call_t *icall)
109 write_core_files = ipc_get_arg1(icall);
110 async_answer_0(icall, EOK);
111 (void) taskmon_save_cfg(taskmon_cfg_path);
114 static void corecfg_client_conn(ipc_call_t *icall, void *arg)
116 /* Accept the connection */
117 async_accept_0(icall);
119 while (true) {
120 ipc_call_t call;
121 async_get_call(&call);
122 sysarg_t method = ipc_get_imethod(&call);
124 if (!method) {
125 /* The other side has hung up */
126 async_answer_0(&call, EOK);
127 return;
130 switch (method) {
131 case CORECFG_GET_ENABLE:
132 corecfg_get_enable_srv(&call);
133 break;
134 case CORECFG_SET_ENABLE:
135 corecfg_set_enable_srv(&call);
136 break;
137 default:
138 async_answer_0(&call, ENOTSUP);
143 /** Load task monitor configuration from SIF file.
145 * @param cfgpath Configuration file path
147 * @return EOK on success or an error code
149 static errno_t taskmon_load_cfg(const char *cfgpath)
151 sif_doc_t *doc = NULL;
152 sif_node_t *rnode;
153 sif_node_t *ncorefiles;
154 const char *ntype;
155 const char *swrite;
156 errno_t rc;
158 rc = sif_load(cfgpath, &doc);
159 if (rc != EOK)
160 goto error;
162 rnode = sif_get_root(doc);
163 ncorefiles = sif_node_first_child(rnode);
164 ntype = sif_node_get_type(ncorefiles);
165 if (str_cmp(ntype, "corefiles") != 0) {
166 rc = EIO;
167 goto error;
170 swrite = sif_node_get_attr(ncorefiles, "write");
171 if (swrite == NULL) {
172 rc = EIO;
173 goto error;
176 if (str_cmp(swrite, "y") == 0) {
177 write_core_files = true;
178 } else if (str_cmp(swrite, "n") == 0) {
179 write_core_files = false;
180 } else {
181 rc = EIO;
182 goto error;
185 sif_delete(doc);
186 return EOK;
187 error:
188 if (doc != NULL)
189 sif_delete(doc);
190 return rc;
193 /** Save task monitor configuration to SIF file.
195 * @param cfgpath Configuration file path
197 * @return EOK on success or an error code
199 static errno_t taskmon_save_cfg(const char *cfgpath)
201 sif_doc_t *doc = NULL;
202 sif_node_t *rnode;
203 sif_node_t *ncorefiles;
204 errno_t rc;
206 rc = sif_new(&doc);
207 if (rc != EOK)
208 goto error;
210 rnode = sif_get_root(doc);
211 rc = sif_node_append_child(rnode, "corefiles", &ncorefiles);
212 if (rc != EOK)
213 goto error;
215 rc = sif_node_set_attr(ncorefiles, "write",
216 write_core_files ? "y" : "n");
217 if (rc != EOK)
218 goto error;
220 rc = sif_save(doc, cfgpath);
221 if (rc != EOK)
222 goto error;
224 sif_delete(doc);
225 return EOK;
226 error:
227 if (doc != NULL)
228 sif_delete(doc);
229 return rc;
232 int main(int argc, char *argv[])
234 loc_srv_t *srv;
236 printf("%s: Task Monitoring Service\n", NAME);
238 #ifdef CONFIG_WRITE_CORE_FILES
239 write_core_files = true;
240 #else
241 write_core_files = false;
242 #endif
243 (void) taskmon_load_cfg(taskmon_cfg_path);
245 if (async_event_subscribe(EVENT_FAULT, fault_event, NULL) != EOK) {
246 printf("%s: Error registering fault notifications.\n", NAME);
247 return -1;
250 async_set_fallback_port_handler(corecfg_client_conn, NULL);
252 errno_t rc = loc_server_register(NAME, &srv);
253 if (rc != EOK) {
254 printf("%s: Failed registering server: %s.\n",
255 NAME, str_error(rc));
256 return -1;
259 service_id_t sid;
260 rc = loc_service_register(srv, SERVICE_NAME_CORECFG, &sid);
261 if (rc != EOK) {
262 loc_server_unregister(srv);
263 printf("%s: Failed registering service: %s.\n",
264 NAME, str_error(rc));
265 return -1;
268 task_retval(0);
269 async_manager();
271 return 0;
274 /** @}