[09a8006] | 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 |
|
---|
[bb154c6] | 29 | /*
|
---|
| 30 | * Unit terminology and OOP based on systemd.
|
---|
| 31 | */
|
---|
[f42ee6f] | 32 | #ifndef SYSMAN_UNIT_H
|
---|
| 33 | #define SYSMAN_UNIT_H
|
---|
| 34 |
|
---|
[bb154c6] | 35 | #include <adt/hash_table.h>
|
---|
[f42ee6f] | 36 | #include <adt/list.h>
|
---|
[bb154c6] | 37 | #include <conf/configuration.h>
|
---|
| 38 | #include <conf/ini.h>
|
---|
| 39 | #include <conf/text_parse.h>
|
---|
[694253c] | 40 | #include <fibril_synch.h>
|
---|
[b55f62a] | 41 | #include <ipc/sysman.h>
|
---|
[bb154c6] | 42 |
|
---|
[dda2602] | 43 | /* Forward declarations */
|
---|
| 44 | typedef struct unit_vmt unit_vmt_t;
|
---|
| 45 | struct unit_vmt;
|
---|
| 46 |
|
---|
| 47 | typedef struct job job_t;
|
---|
| 48 | struct job;
|
---|
| 49 |
|
---|
[8ae8262] | 50 | /* Represents presence of unit in repo during modifications */
|
---|
| 51 | typedef enum {
|
---|
| 52 | REPO_EMBRYO,
|
---|
| 53 | REPO_LIVING,
|
---|
| 54 | REPO_ZOMBIE
|
---|
| 55 | } repo_state_t;
|
---|
| 56 |
|
---|
[aa0faeca] | 57 | typedef enum {
|
---|
| 58 | UNIT_CONDITION_NONE = 0,
|
---|
| 59 | UNIT_CONDITION_ARCHITECTURE = 1
|
---|
| 60 | } unit_condition_t;
|
---|
| 61 |
|
---|
[bb154c6] | 62 | typedef struct {
|
---|
[dda2602] | 63 | /** Link to name-to-unit hash table */
|
---|
| 64 | ht_link_t units_by_name;
|
---|
| 65 |
|
---|
[b55f62a] | 66 | /** Link to handle-to-unit hash table */
|
---|
| 67 | ht_link_t units_by_handle;
|
---|
| 68 |
|
---|
[dda2602] | 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 |
|
---|
[c64e254] | 75 | /** Mark during BFS traversal unit is already queued */
|
---|
| 76 | bool bfs_tag;
|
---|
| 77 |
|
---|
| 78 | /** Auxiliary data for BFS traverse users .*/
|
---|
| 79 | void *bfs_data;
|
---|
[dda2602] | 80 |
|
---|
[63a3276] | 81 | /** Job assigned to unit in transitional state */
|
---|
[dda2602] | 82 | job_t *job;
|
---|
[f42ee6f] | 83 |
|
---|
[ed5367b] | 84 | /** Handle for IPC (immutable) */
|
---|
[b55f62a] | 85 | unit_handle_t handle;
|
---|
[ed5367b] | 86 |
|
---|
| 87 | /** Unit type (immutable) */
|
---|
[f42ee6f] | 88 | unit_type_t type;
|
---|
[ed5367b] | 89 |
|
---|
| 90 | /** Unit name (immutable) */
|
---|
[bb154c6] | 91 | char *name;
|
---|
[694253c] | 92 |
|
---|
[f42ee6f] | 93 | unit_state_t state;
|
---|
| 94 |
|
---|
[8ae8262] | 95 | repo_state_t repo_state;
|
---|
| 96 |
|
---|
[9532981] | 97 | list_t edges_in;
|
---|
| 98 | list_t edges_out;
|
---|
[aa0faeca] | 99 |
|
---|
| 100 | /**
|
---|
| 101 | * flag for conditions which were not meet
|
---|
| 102 | * determines if the unit should be started
|
---|
| 103 | */
|
---|
| 104 | unit_condition_t conditions;
|
---|
[bb154c6] | 105 | } unit_t;
|
---|
| 106 |
|
---|
| 107 | #include "unit_cfg.h"
|
---|
| 108 | #include "unit_mnt.h"
|
---|
| 109 | #include "unit_tgt.h"
|
---|
[5559712] | 110 | #include "unit_svc.h"
|
---|
[bb154c6] | 111 |
|
---|
[102f641] | 112 | #define DEFINE_CAST(NAME, TYPE, ENUM_TYPE) \
|
---|
| 113 | static inline TYPE *CAST_##NAME(unit_t *u) \
|
---|
| 114 | { \
|
---|
| 115 | if (u->type == ENUM_TYPE) \
|
---|
[5559712] | 116 | return (TYPE *)u; \
|
---|
[102f641] | 117 | else \
|
---|
[5559712] | 118 | return NULL; \
|
---|
[102f641] | 119 | }
|
---|
[f42ee6f] | 120 |
|
---|
[102f641] | 121 | DEFINE_CAST(CFG, unit_cfg_t, UNIT_CONFIGURATION);
|
---|
| 122 | DEFINE_CAST(MNT, unit_mnt_t, UNIT_MOUNT);
|
---|
| 123 | DEFINE_CAST(TGT, unit_tgt_t, UNIT_TARGET);
|
---|
| 124 | DEFINE_CAST(SVC, unit_svc_t, UNIT_SERVICE);
|
---|
[bb154c6] | 125 |
|
---|
| 126 | struct unit_vmt {
|
---|
| 127 | size_t size;
|
---|
| 128 |
|
---|
| 129 | void (*init)(unit_t *);
|
---|
| 130 |
|
---|
| 131 | void (*destroy)(unit_t *);
|
---|
| 132 |
|
---|
[25697163] | 133 | errno_t (*load)(unit_t *, ini_configuration_t *, text_parse_t *);
|
---|
[bb154c6] | 134 |
|
---|
[25697163] | 135 | errno_t (*start)(unit_t *);
|
---|
[5559712] | 136 |
|
---|
[25697163] | 137 | errno_t (*stop)(unit_t *);
|
---|
[ed5367b] | 138 |
|
---|
[5559712] | 139 | void (*exposee_created)(unit_t *);
|
---|
| 140 |
|
---|
| 141 | void (*fail)(unit_t *);
|
---|
[6efec7e3] | 142 | };
|
---|
[f42ee6f] | 143 |
|
---|
[bb154c6] | 144 | extern unit_vmt_t *unit_type_vmts[];
|
---|
| 145 |
|
---|
[5559712] | 146 | #define DEFINE_UNIT_VMT(PREFIX) \
|
---|
| 147 | unit_vmt_t PREFIX##_vmt = { \
|
---|
| 148 | .size = sizeof(PREFIX##_t), \
|
---|
| 149 | .init = &PREFIX##_init, \
|
---|
| 150 | .load = &PREFIX##_load, \
|
---|
| 151 | .destroy = &PREFIX##_destroy, \
|
---|
| 152 | .start = &PREFIX##_start, \
|
---|
[ed5367b] | 153 | .stop = &PREFIX##_stop, \
|
---|
[5559712] | 154 | .exposee_created = &PREFIX##_exposee_created, \
|
---|
| 155 | .fail = &PREFIX##_fail \
|
---|
[bb154c6] | 156 | };
|
---|
| 157 |
|
---|
| 158 | #define UNIT_VMT(UNIT) unit_type_vmts[(UNIT)->type]
|
---|
[f42ee6f] | 159 |
|
---|
| 160 | extern unit_t *unit_create(unit_type_t);
|
---|
| 161 | extern void unit_destroy(unit_t **);
|
---|
| 162 |
|
---|
[25697163] | 163 | extern errno_t unit_load(unit_t *, ini_configuration_t *, text_parse_t *);
|
---|
| 164 | extern errno_t unit_start(unit_t *);
|
---|
| 165 | extern errno_t unit_stop(unit_t *);
|
---|
[5559712] | 166 | extern void unit_exposee_created(unit_t *);
|
---|
| 167 | extern void unit_fail(unit_t *);
|
---|
| 168 |
|
---|
| 169 | extern void unit_notify_state(unit_t *);
|
---|
[f42ee6f] | 170 |
|
---|
[bb154c6] | 171 | extern unit_type_t unit_type_name_to_type(const char *);
|
---|
| 172 |
|
---|
| 173 | extern const char *unit_name(const unit_t *);
|
---|
[aa0faeca] | 174 | extern void unit_log_condition(unit_t *unit);
|
---|
[bb154c6] | 175 |
|
---|
| 176 | extern bool unit_parse_unit_list(const char *, void *, text_parse_t *, size_t);
|
---|
[aa0faeca] | 177 | extern bool unit_parse_condition_architecture(const char *, void *, text_parse_t *, size_t);
|
---|
[bb154c6] | 178 |
|
---|
[f42ee6f] | 179 | #endif
|
---|