| 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 |
|
|---|
| 35 | #include "log.h"
|
|---|
| 36 | #include "unit.h"
|
|---|
| 37 |
|
|---|
| 38 | static const char *section_name = "Service";
|
|---|
| 39 |
|
|---|
| 40 | static config_item_t unit_configuration[] = {
|
|---|
| 41 | {"ExecStart", &util_parse_command, offsetof(unit_svc_t, exec_start), NULL},
|
|---|
| 42 | CONFIGURATION_ITEM_SENTINEL
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | static void unit_svc_init(unit_t *unit)
|
|---|
| 46 | {
|
|---|
| 47 | unit_svc_t *u_svc = CAST_SVC(unit);
|
|---|
| 48 | assert(u_svc);
|
|---|
| 49 | util_command_init(&u_svc->exec_start);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | static void unit_svc_destroy(unit_t *unit)
|
|---|
| 53 | {
|
|---|
| 54 | assert(unit->type == UNIT_SERVICE);
|
|---|
| 55 | unit_svc_t *u_svc = CAST_SVC(unit);
|
|---|
| 56 |
|
|---|
| 57 | util_command_deinit(&u_svc->exec_start);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | static int unit_svc_load(unit_t *unit, ini_configuration_t *ini_conf,
|
|---|
| 61 | text_parse_t *text_parse)
|
|---|
| 62 | {
|
|---|
| 63 | unit_svc_t *u_svc = CAST_SVC(unit);
|
|---|
| 64 | assert(u_svc);
|
|---|
| 65 |
|
|---|
| 66 | ini_section_t *section = ini_get_section(ini_conf, section_name);
|
|---|
| 67 | if (section == NULL) {
|
|---|
| 68 | sysman_log(LVL_ERROR,
|
|---|
| 69 | "Expected section '%s' in configuration of unit '%s'",
|
|---|
| 70 | section_name, unit_name(unit));
|
|---|
| 71 | return ENOENT;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return config_load_ini_section(unit_configuration, section, u_svc,
|
|---|
| 75 | text_parse);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | static int unit_svc_start(unit_t *unit)
|
|---|
| 79 | {
|
|---|
| 80 | unit_svc_t *u_svc = CAST_SVC(unit);
|
|---|
| 81 | assert(u_svc);
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | // TODO think about unit's lifecycle (is STOPPED only acceptable?)
|
|---|
| 85 | assert(unit->state == STATE_STOPPED);
|
|---|
| 86 |
|
|---|
| 87 | int rc = task_spawnv(&u_svc->main_task_id, NULL, u_svc->exec_start.path,
|
|---|
| 88 | u_svc->exec_start.argv);
|
|---|
| 89 |
|
|---|
| 90 | if (rc != EOK) {
|
|---|
| 91 | unit->state = STATE_FAILED;
|
|---|
| 92 | return rc;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | unit->state = STATE_STARTING;
|
|---|
| 96 |
|
|---|
| 97 | /*
|
|---|
| 98 | * Workaround to see log output even after devman starts (and overrides
|
|---|
| 99 | * kernel's frame buffer.
|
|---|
| 100 | * TODO move to task retval/exposee created handler
|
|---|
| 101 | */
|
|---|
| 102 | if (str_cmp(unit->name, "devman.svc") == 0) {
|
|---|
| 103 | async_usleep(100000);
|
|---|
| 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 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return EOK;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | static void unit_svc_exposee_created(unit_t *unit)
|
|---|
| 115 | {
|
|---|
| 116 | assert(CAST_SVC(unit));
|
|---|
| 117 | assert(unit->state == STATE_STOPPED || unit->state == STATE_STARTING || unit->state==STATE_STARTED);
|
|---|
| 118 |
|
|---|
| 119 | /* Exposee itself doesn't represent started unit. */
|
|---|
| 120 | //unit->state = STATE_STARTED;
|
|---|
| 121 | //unit_notify_state(unit);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | static void unit_svc_fail(unit_t *unit)
|
|---|
| 125 | {
|
|---|
| 126 | // TODO implement
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | DEFINE_UNIT_VMT(unit_svc)
|
|---|
| 130 |
|
|---|