[5559712] | 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>
|
---|
[241f1985] | 34 | #include <str.h>
|
---|
[5559712] | 35 |
|
---|
| 36 | #include "log.h"
|
---|
| 37 | #include "unit.h"
|
---|
| 38 |
|
---|
| 39 | static const char *section_name = "Service";
|
---|
| 40 |
|
---|
| 41 | static config_item_t unit_configuration[] = {
|
---|
[dda2602] | 42 | {"ExecStart", &util_parse_command, offsetof(unit_svc_t, exec_start), NULL},
|
---|
[5559712] | 43 | CONFIGURATION_ITEM_SENTINEL
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | static void unit_svc_init(unit_t *unit)
|
---|
| 47 | {
|
---|
| 48 | unit_svc_t *u_svc = CAST_SVC(unit);
|
---|
| 49 | assert(u_svc);
|
---|
[dda2602] | 50 | util_command_init(&u_svc->exec_start);
|
---|
[5559712] | 51 | }
|
---|
| 52 |
|
---|
| 53 | static void unit_svc_destroy(unit_t *unit)
|
---|
| 54 | {
|
---|
| 55 | assert(unit->type == UNIT_SERVICE);
|
---|
| 56 | unit_svc_t *u_svc = CAST_SVC(unit);
|
---|
| 57 |
|
---|
[dda2602] | 58 | util_command_deinit(&u_svc->exec_start);
|
---|
[5559712] | 59 | }
|
---|
| 60 |
|
---|
[241f1985] | 61 | static errno_t unit_svc_load(unit_t *unit, ini_configuration_t *ini_conf,
|
---|
[5559712] | 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 |
|
---|
[241f1985] | 79 | static errno_t unit_svc_start(unit_t *unit)
|
---|
[5559712] | 80 | {
|
---|
| 81 | unit_svc_t *u_svc = CAST_SVC(unit);
|
---|
| 82 | assert(u_svc);
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | assert(unit->state == STATE_STOPPED);
|
---|
| 86 |
|
---|
[241f1985] | 87 | errno_t rc = task_spawnv(&u_svc->main_task_id, NULL, u_svc->exec_start.path,
|
---|
[dda2602] | 88 | u_svc->exec_start.argv);
|
---|
[4224ef7] | 89 |
|
---|
[5559712] | 90 | if (rc != EOK) {
|
---|
| 91 | unit->state = STATE_FAILED;
|
---|
| 92 | return rc;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[63a3276] | 95 | unit->state = STATE_STARTING;
|
---|
| 96 |
|
---|
[dda2602] | 97 | /*
|
---|
| 98 | * Workaround to see log output even after devman starts (and overrides
|
---|
[63a3276] | 99 | * kernel's frame buffer.
|
---|
[e8747bd8] | 100 | * TODO move to task retval/exposee created handler
|
---|
[dda2602] | 101 | */
|
---|
[63a3276] | 102 | if (str_cmp(unit->name, "devman.svc") == 0) {
|
---|
[241f1985] | 103 | fibril_usleep(100000);
|
---|
[63a3276] | 104 | if (console_kcon()) {
|
---|
| 105 | sysman_log(LVL_DEBUG2, "%s: Kconsole grabbed.", __func__);
|
---|
| 106 | } else {
|
---|
| 107 | sysman_log(LVL_DEBUG2, "%s: no kconsole.", __func__);
|
---|
| 108 | }
|
---|
[5559712] | 109 | }
|
---|
| 110 |
|
---|
| 111 | return EOK;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[241f1985] | 114 | static errno_t unit_svc_stop(unit_t *unit)
|
---|
[ed5367b] | 115 | {
|
---|
| 116 | unit_svc_t *u_svc = CAST_SVC(unit);
|
---|
| 117 | assert(u_svc);
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | // note: May change when job cancellation is possible.
|
---|
| 121 | assert(unit->state == STATE_STARTED);
|
---|
| 122 |
|
---|
[8d74fdd] | 123 | /*
|
---|
| 124 | * Ugly trick -- critical service is running despite being stopped
|
---|
| 125 | * circumvent killing ourselves during shutdown (TODO dependencies).
|
---|
| 126 | */
|
---|
| 127 | if (u_svc->critical) {
|
---|
| 128 | unit->state = STATE_STOPPED;
|
---|
| 129 | return EOK;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[241f1985] | 132 | errno_t rc = task_kill(u_svc->main_task_id);
|
---|
[ed5367b] | 133 |
|
---|
| 134 | if (rc != EOK) {
|
---|
| 135 | /* Task may still be running, but be conservative about unit's
|
---|
| 136 | * state. */
|
---|
| 137 | unit->state = STATE_FAILED;
|
---|
| 138 | return rc;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | unit->state = STATE_STOPPING;
|
---|
| 142 |
|
---|
| 143 | return EOK;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
[5559712] | 147 | static void unit_svc_exposee_created(unit_t *unit)
|
---|
| 148 | {
|
---|
[63a3276] | 149 | assert(CAST_SVC(unit));
|
---|
| 150 | assert(unit->state == STATE_STOPPED || unit->state == STATE_STARTING || unit->state==STATE_STARTED);
|
---|
| 151 |
|
---|
[e8747bd8] | 152 | /* Exposee itself doesn't represent started unit. */
|
---|
| 153 | //unit->state = STATE_STARTED;
|
---|
| 154 | //unit_notify_state(unit);
|
---|
[5559712] | 155 | }
|
---|
| 156 |
|
---|
| 157 | static void unit_svc_fail(unit_t *unit)
|
---|
| 158 | {
|
---|
| 159 | // TODO implement
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | DEFINE_UNIT_VMT(unit_svc)
|
---|
| 163 |
|
---|