Changeset bb154c6 in mainline for uspace/srv/sysman/unit.c


Ignore:
Timestamp:
2019-08-03T08:15:25Z (5 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
Children:
09a8006
Parents:
6006f35
git-author:
Michal Koutný <xm.koutny+hos@…> (2015-04-15 15:14:58)
git-committer:
Matthieu Riolo <matthieu.riolo@…> (2019-08-03 08:15:25)
Message:

Add skeleton for configuration files loading

  • Create content of /cfg directory,
  • create sample configuration file,
  • refactored polymorphism.

Conflicts:

boot/Makefile

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/sysman/unit.c

    r6006f35 rbb154c6  
    22#include <errno.h>
    33#include <fibril_synch.h>
     4#include <conf/configuration.h>
     5#include <conf/ini.h>
    46#include <mem.h>
     7#include <stddef.h>
    58#include <stdio.h>
    69#include <stdlib.h>
     10#include <str.h>
    711
     12#include "dep.h"
    813#include "log.h"
    914#include "unit.h"
    1015
    1116/** Virtual method table for each unit type */
    12 static unit_ops_t *unit_type_vmts[] = {
     17unit_vmt_t *unit_type_vmts[] = {
    1318        [UNIT_TARGET]        = &unit_tgt_ops,
    1419        [UNIT_MOUNT]         = &unit_mnt_ops,
     
    1621};
    1722
     23static const char *section_name = "Unit";
     24
     25static config_item_t unit_configuration[] = {
     26        {"After", &unit_parse_unit_list, 0, ""},
     27        CONFIGURATION_ITEM_SENTINEL
     28};
     29
     30
    1831static void unit_init(unit_t *unit, unit_type_t type)
    1932{
    2033        assert(unit);
    2134
     35        // TODO is this necessary?
    2236        memset(unit, 0, sizeof(unit_t));
    23         link_initialize(&unit->units);
    2437       
    2538        unit->type = type;
     39        unit->name = NULL;
     40
    2641        unit->state = STATE_EMBRYO;
    2742        fibril_mutex_initialize(&unit->state_mtx);
     
    3146        list_initialize(&unit->dependencies);
    3247
    33         unit_type_vmts[unit->type]->init(unit);
     48        UNIT_VMT(unit)->init(unit);
    3449}
    3550
    3651unit_t *unit_create(unit_type_t type)
    3752{
    38         unit_t *unit = malloc(sizeof(unit_t));
     53        size_t size = unit_type_vmts[type]->size;
     54        unit_t *unit = malloc(size);
    3955        if (unit != NULL) {
    4056                unit_init(unit, type);
     
    5066                return;
    5167
    52         unit_type_vmts[unit->type]->destroy(unit);
     68        UNIT_VMT(unit)->destroy(unit);
    5369        /* TODO:
    5470         *      edges,
     
    5672         *      other resources to come
    5773         */
     74        free(unit->name);
    5875        free(unit);
    5976        unit_ptr = NULL;
     
    7592int unit_start(unit_t *unit)
    7693{
    77         sysman_log(LVL_DEBUG, "%s(%p)", __func__, unit);
    78         return unit_type_vmts[unit->type]->start(unit);
     94        sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
     95        return UNIT_VMT(unit)->start(unit);
    7996}
     97
     98int unit_load(unit_t *unit, ini_configuration_t *ini_conf,
     99    text_parse_t *text_parse)
     100{
     101        sysman_log(LVL_DEBUG, "%s('%s')", __func__, unit_name(unit));
     102
     103        int rc = EOK;
     104        ini_section_t *unit_section = ini_get_section(ini_conf, section_name);
     105        if (unit_section) {
     106                rc = config_load_ini_section(unit_configuration,
     107                    unit_section, unit, text_parse);
     108        }
     109                               
     110        if (rc != EOK) {
     111                return rc;
     112        } else {
     113                return UNIT_VMT(unit)->load(unit, ini_conf, text_parse);
     114        }
     115}
     116
     117unit_type_t unit_type_name_to_type(const char *type_name)
     118{
     119        if (str_cmp(type_name, "cfg") == 0)
     120                return UNIT_CONFIGURATION;
     121
     122        else if (str_cmp(type_name, "mnt") == 0)
     123                return UNIT_MOUNT;
     124
     125        else if (str_cmp(type_name, "tgt") == 0)
     126                return UNIT_TARGET;
     127
     128        else
     129                return UNIT_TYPE_INVALID;
     130}
     131
     132/** Format unit name to be presented to user */
     133const char *unit_name(const unit_t *unit)
     134{
     135        return unit->name ? unit->name : "";
     136}
     137
     138
     139
     140
     141bool unit_parse_unit_list(const char *value, void *dst, text_parse_t *parse,
     142    size_t lineno)
     143{
     144        unit_t *unit = dst;
     145        bool result;
     146        char *my_value = str_dup(value);
     147
     148        if (!my_value) {
     149                result = false;
     150                goto finish;
     151        }
     152
     153        char *to_split = my_value;
     154        char *cur_tok;
     155
     156        while ((cur_tok = str_tok(to_split, " ", &to_split))) {
     157                if (dep_sprout_dependency(unit, cur_tok) != EOK) {
     158                        result = false;
     159                        goto finish;
     160                }
     161        }
     162
     163        result = true;
     164
     165finish:
     166        free(my_value);
     167        return result;
     168}
Note: See TracChangeset for help on using the changeset viewer.