| 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 | /*
|
|---|
| 30 | * Unit terminology and OOP based on systemd.
|
|---|
| 31 | */
|
|---|
| 32 | #ifndef SYSMAN_UNIT_H
|
|---|
| 33 | #define SYSMAN_UNIT_H
|
|---|
| 34 |
|
|---|
| 35 | #include <adt/hash_table.h>
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <conf/configuration.h>
|
|---|
| 38 | #include <conf/ini.h>
|
|---|
| 39 | #include <conf/text_parse.h>
|
|---|
| 40 | #include <fibril_synch.h>
|
|---|
| 41 |
|
|---|
| 42 | typedef enum {
|
|---|
| 43 | UNIT_TYPE_INVALID = -1,
|
|---|
| 44 | UNIT_CONFIGURATION = 0,
|
|---|
| 45 | UNIT_MOUNT,
|
|---|
| 46 | UNIT_SERVICE,
|
|---|
| 47 | UNIT_TARGET
|
|---|
| 48 | } unit_type_t;
|
|---|
| 49 |
|
|---|
| 50 | typedef enum {
|
|---|
| 51 | STATE_EMBRYO = 0,
|
|---|
| 52 | STATE_STARTING,
|
|---|
| 53 | STATE_STARTED,
|
|---|
| 54 | STATE_STOPPED,
|
|---|
| 55 | STATE_FAILED
|
|---|
| 56 | } unit_state_t;
|
|---|
| 57 |
|
|---|
| 58 | /* Forward declarations */
|
|---|
| 59 | typedef struct unit_vmt unit_vmt_t;
|
|---|
| 60 | struct unit_vmt;
|
|---|
| 61 |
|
|---|
| 62 | typedef struct job job_t;
|
|---|
| 63 | struct job;
|
|---|
| 64 |
|
|---|
| 65 | typedef struct {
|
|---|
| 66 | /** Link to name-to-unit hash table */
|
|---|
| 67 | ht_link_t units_by_name;
|
|---|
| 68 |
|
|---|
| 69 | /** Link to list of all units */
|
|---|
| 70 | link_t units;
|
|---|
| 71 |
|
|---|
| 72 | /** Link to queue, when BFS traversing units */
|
|---|
| 73 | link_t bfs_link;
|
|---|
| 74 |
|
|---|
| 75 | /** Auxiliary job created during BFS traverse, its presence serves also
|
|---|
| 76 | * as BFS tag */
|
|---|
| 77 | job_t *bfs_job;
|
|---|
| 78 |
|
|---|
| 79 | /** Job assigned to unit in transitional state */
|
|---|
| 80 | job_t *job;
|
|---|
| 81 |
|
|---|
| 82 | unit_type_t type;
|
|---|
| 83 | char *name;
|
|---|
| 84 |
|
|---|
| 85 | unit_state_t state;
|
|---|
| 86 |
|
|---|
| 87 | list_t dependencies;
|
|---|
| 88 | list_t dependants;
|
|---|
| 89 | } unit_t;
|
|---|
| 90 |
|
|---|
| 91 | #include "unit_cfg.h"
|
|---|
| 92 | #include "unit_mnt.h"
|
|---|
| 93 | #include "unit_tgt.h"
|
|---|
| 94 | #include "unit_svc.h"
|
|---|
| 95 |
|
|---|
| 96 | #define DEFINE_CAST(NAME, TYPE, ENUM_TYPE) \
|
|---|
| 97 | static inline TYPE *CAST_##NAME(unit_t *u) \
|
|---|
| 98 | { \
|
|---|
| 99 | if (u->type == ENUM_TYPE) \
|
|---|
| 100 | return (TYPE *)u; \
|
|---|
| 101 | else \
|
|---|
| 102 | return NULL; \
|
|---|
| 103 | } \
|
|---|
| 104 |
|
|---|
| 105 | DEFINE_CAST(CFG, unit_cfg_t, UNIT_CONFIGURATION)
|
|---|
| 106 | DEFINE_CAST(MNT, unit_mnt_t, UNIT_MOUNT)
|
|---|
| 107 | DEFINE_CAST(TGT, unit_tgt_t, UNIT_TARGET)
|
|---|
| 108 | DEFINE_CAST(SVC, unit_svc_t, UNIT_SERVICE)
|
|---|
| 109 |
|
|---|
| 110 | struct unit_vmt {
|
|---|
| 111 | size_t size;
|
|---|
| 112 |
|
|---|
| 113 | void (*init)(unit_t *);
|
|---|
| 114 |
|
|---|
| 115 | void (*destroy)(unit_t *);
|
|---|
| 116 |
|
|---|
| 117 | int (*load)(unit_t *, ini_configuration_t *, text_parse_t *);
|
|---|
| 118 |
|
|---|
| 119 | int (*start)(unit_t *);
|
|---|
| 120 |
|
|---|
| 121 | void (*exposee_created)(unit_t *);
|
|---|
| 122 |
|
|---|
| 123 | void (*fail)(unit_t *);
|
|---|
| 124 | };
|
|---|
| 125 |
|
|---|
| 126 | extern unit_vmt_t *unit_type_vmts[];
|
|---|
| 127 |
|
|---|
| 128 | #define DEFINE_UNIT_VMT(PREFIX) \
|
|---|
| 129 | unit_vmt_t PREFIX##_vmt = { \
|
|---|
| 130 | .size = sizeof(PREFIX##_t), \
|
|---|
| 131 | .init = &PREFIX##_init, \
|
|---|
| 132 | .load = &PREFIX##_load, \
|
|---|
| 133 | .destroy = &PREFIX##_destroy, \
|
|---|
| 134 | .start = &PREFIX##_start, \
|
|---|
| 135 | .exposee_created = &PREFIX##_exposee_created, \
|
|---|
| 136 | .fail = &PREFIX##_fail \
|
|---|
| 137 | };
|
|---|
| 138 |
|
|---|
| 139 | #define UNIT_VMT(UNIT) unit_type_vmts[(UNIT)->type]
|
|---|
| 140 |
|
|---|
| 141 | extern unit_t *unit_create(unit_type_t);
|
|---|
| 142 | extern void unit_destroy(unit_t **);
|
|---|
| 143 |
|
|---|
| 144 | extern int unit_load(unit_t *, ini_configuration_t *, text_parse_t *);
|
|---|
| 145 | extern int unit_start(unit_t *);
|
|---|
| 146 | extern void unit_exposee_created(unit_t *);
|
|---|
| 147 | extern void unit_fail(unit_t *);
|
|---|
| 148 |
|
|---|
| 149 | extern void unit_notify_state(unit_t *);
|
|---|
| 150 |
|
|---|
| 151 | extern unit_type_t unit_type_name_to_type(const char *);
|
|---|
| 152 |
|
|---|
| 153 | extern const char *unit_name(const unit_t *);
|
|---|
| 154 |
|
|---|
| 155 | extern bool unit_parse_unit_list(const char *, void *, text_parse_t *, size_t);
|
|---|
| 156 |
|
|---|
| 157 | #endif
|
|---|