| 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 |
|
|---|
| 39 | static const char *section_name = "Service";
|
|---|
| 40 |
|
|---|
| 41 | static config_item_t unit_configuration[] = {
|
|---|
| 42 | { "ExecStart", &util_parse_command, offsetof(unit_svc_t, exec_start), NULL },
|
|---|
| 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);
|
|---|
| 50 | util_command_init(&u_svc->exec_start);
|
|---|
| 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 |
|
|---|
| 58 | util_command_deinit(&u_svc->exec_start);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | static 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 |
|
|---|
| 79 | static 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 | sysman_log(LVL_ERROR, "%s: failed to spawn task '%s' for unit '%s'.",
|
|---|
| 91 | __func__,
|
|---|
| 92 | u_svc->exec_start.path,
|
|---|
| 93 | unit_name(unit));
|
|---|
| 94 | unit->state = STATE_FAILED;
|
|---|
| 95 | return rc;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | unit->state = STATE_STARTING;
|
|---|
| 99 |
|
|---|
| 100 | return EOK;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | static errno_t unit_svc_stop(unit_t *unit)
|
|---|
| 104 | {
|
|---|
| 105 | unit_svc_t *u_svc = CAST_SVC(unit);
|
|---|
| 106 | assert(u_svc);
|
|---|
| 107 |
|
|---|
| 108 | // note: May change when job cancellation is possible.
|
|---|
| 109 | assert(unit->state == STATE_STARTED);
|
|---|
| 110 |
|
|---|
| 111 | /*
|
|---|
| 112 | * Ugly trick -- critical service is running despite being stopped
|
|---|
| 113 | * circumvent killing ourselves during shutdown (TODO dependencies).
|
|---|
| 114 | */
|
|---|
| 115 | if (u_svc->critical) {
|
|---|
| 116 | unit->state = STATE_STOPPED;
|
|---|
| 117 | return EOK;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | errno_t rc = task_kill(u_svc->main_task_id);
|
|---|
| 121 |
|
|---|
| 122 | if (rc != EOK) {
|
|---|
| 123 | /*
|
|---|
| 124 | * Task may still be running, but be conservative about unit's
|
|---|
| 125 | * state.
|
|---|
| 126 | */
|
|---|
| 127 | unit->state = STATE_FAILED;
|
|---|
| 128 | return rc;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | unit->state = STATE_STOPPING;
|
|---|
| 132 |
|
|---|
| 133 | return EOK;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static void unit_svc_exposee_created(unit_t *unit)
|
|---|
| 137 | {
|
|---|
| 138 | assert(CAST_SVC(unit));
|
|---|
| 139 | assert(unit->state == STATE_STOPPED || unit->state == STATE_STARTING || unit->state == STATE_STARTED);
|
|---|
| 140 |
|
|---|
| 141 | /* Exposee itself doesn't represent started unit. */
|
|---|
| 142 | //unit->state = STATE_STARTED;
|
|---|
| 143 | //unit_notify_state(unit);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static void unit_svc_fail(unit_t *unit)
|
|---|
| 147 | {
|
|---|
| 148 | // TODO implement
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | DEFINE_UNIT_VMT(unit_svc);
|
|---|