source: mainline/uspace/srv/sysman/units/unit_svc.c@ 102f641

Last change on this file since 102f641 was 102f641, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Correcting syntax according to ccheck

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Copyright (c) 2015 Michal Koutny
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#include <assert.h>
30#include <errno.h>
31#include <io/console.h> // temporary
32#include <stdlib.h>
33#include <task.h>
34#include <str.h>
35
36#include "log.h"
37#include "unit.h"
38
39static const char *section_name = "Service";
40
41static config_item_t unit_configuration[] = {
42 { "ExecStart", &util_parse_command, offsetof(unit_svc_t, exec_start), NULL },
43 CONFIGURATION_ITEM_SENTINEL
44};
45
46static void unit_svc_init(unit_t *unit)
47{
48 unit_svc_t *u_svc = CAST_SVC(unit);
49 assert(u_svc);
50 util_command_init(&u_svc->exec_start);
51}
52
53static void unit_svc_destroy(unit_t *unit)
54{
55 assert(unit->type == UNIT_SERVICE);
56 unit_svc_t *u_svc = CAST_SVC(unit);
57
58 util_command_deinit(&u_svc->exec_start);
59}
60
61static errno_t unit_svc_load(unit_t *unit, ini_configuration_t *ini_conf,
62 text_parse_t *text_parse)
63{
64 unit_svc_t *u_svc = CAST_SVC(unit);
65 assert(u_svc);
66
67 ini_section_t *section = ini_get_section(ini_conf, section_name);
68 if (section == NULL) {
69 sysman_log(LVL_ERROR,
70 "Expected section '%s' in configuration of unit '%s'",
71 section_name, unit_name(unit));
72 return ENOENT;
73 }
74
75 return config_load_ini_section(unit_configuration, section, u_svc,
76 text_parse);
77}
78
79static errno_t unit_svc_start(unit_t *unit)
80{
81 unit_svc_t *u_svc = CAST_SVC(unit);
82 assert(u_svc);
83
84 assert(unit->state == STATE_STOPPED);
85
86 errno_t rc = task_spawnv(&u_svc->main_task_id, NULL, u_svc->exec_start.path,
87 u_svc->exec_start.argv);
88
89 if (rc != EOK) {
90 unit->state = STATE_FAILED;
91 return rc;
92 }
93
94 unit->state = STATE_STARTING;
95
96 /*
97 * Workaround to see log output even after devman starts (and overrides
98 * kernel's frame buffer.
99 * TODO move to task retval/exposee created handler
100 */
101 if (str_cmp(unit->name, "devman.svc") == 0) {
102 fibril_usleep(100000);
103 if (console_kcon()) {
104 sysman_log(LVL_DEBUG2, "%s: Kconsole grabbed.", __func__);
105 } else {
106 sysman_log(LVL_DEBUG2, "%s: no kconsole.", __func__);
107 }
108 }
109
110 return EOK;
111}
112
113static errno_t unit_svc_stop(unit_t *unit)
114{
115 unit_svc_t *u_svc = CAST_SVC(unit);
116 assert(u_svc);
117
118 // note: May change when job cancellation is possible.
119 assert(unit->state == STATE_STARTED);
120
121 /*
122 * Ugly trick -- critical service is running despite being stopped
123 * circumvent killing ourselves during shutdown (TODO dependencies).
124 */
125 if (u_svc->critical) {
126 unit->state = STATE_STOPPED;
127 return EOK;
128 }
129
130 errno_t rc = task_kill(u_svc->main_task_id);
131
132 if (rc != EOK) {
133 /*
134 * Task may still be running, but be conservative about unit's
135 * state.
136 */
137 unit->state = STATE_FAILED;
138 return rc;
139 }
140
141 unit->state = STATE_STOPPING;
142
143 return EOK;
144}
145
146static void unit_svc_exposee_created(unit_t *unit)
147{
148 assert(CAST_SVC(unit));
149 assert(unit->state == STATE_STOPPED || unit->state == STATE_STARTING || unit->state == STATE_STARTED);
150
151 /* Exposee itself doesn't represent started unit. */
152 //unit->state = STATE_STARTED;
153 //unit_notify_state(unit);
154}
155
156static void unit_svc_fail(unit_t *unit)
157{
158 // TODO implement
159}
160
161DEFINE_UNIT_VMT(unit_svc);
Note: See TracBrowser for help on using the repository browser.