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 | #include <ipc/sysman.h>
|
---|
42 |
|
---|
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 |
|
---|
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 |
|
---|
57 | typedef enum {
|
---|
58 | UNIT_CONDITION_NONE = 0,
|
---|
59 | UNIT_CONDITION_ARCHITECTURE = 1
|
---|
60 | } unit_condition_t;
|
---|
61 |
|
---|
62 | typedef struct {
|
---|
63 | /** Link to name-to-unit hash table */
|
---|
64 | ht_link_t units_by_name;
|
---|
65 |
|
---|
66 | /** Link to handle-to-unit hash table */
|
---|
67 | ht_link_t units_by_handle;
|
---|
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 | /** Mark during BFS traversal unit is already queued */
|
---|
76 | bool bfs_tag;
|
---|
77 |
|
---|
78 | /** Auxiliary data for BFS traverse users .*/
|
---|
79 | void *bfs_data;
|
---|
80 |
|
---|
81 | /** Job assigned to unit in transitional state */
|
---|
82 | job_t *job;
|
---|
83 |
|
---|
84 | /** Handle for IPC (immutable) */
|
---|
85 | unit_handle_t handle;
|
---|
86 |
|
---|
87 | /** Unit type (immutable) */
|
---|
88 | unit_type_t type;
|
---|
89 |
|
---|
90 | /** Unit name (immutable) */
|
---|
91 | char *name;
|
---|
92 |
|
---|
93 | unit_state_t state;
|
---|
94 |
|
---|
95 | repo_state_t repo_state;
|
---|
96 |
|
---|
97 | list_t edges_in;
|
---|
98 | list_t edges_out;
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * flag for conditions which were not meet
|
---|
102 | * determines if the unit should be started
|
---|
103 | */
|
---|
104 | unit_condition_t conditions;
|
---|
105 | } unit_t;
|
---|
106 |
|
---|
107 | #include "unit_cfg.h"
|
---|
108 | #include "unit_mnt.h"
|
---|
109 | #include "unit_tgt.h"
|
---|
110 | #include "unit_svc.h"
|
---|
111 |
|
---|
112 | #define DEFINE_CAST(NAME, TYPE, ENUM_TYPE) \
|
---|
113 | static inline TYPE *CAST_##NAME(unit_t *u) \
|
---|
114 | { \
|
---|
115 | if (u->type == ENUM_TYPE) \
|
---|
116 | return (TYPE *)u; \
|
---|
117 | else \
|
---|
118 | return NULL; \
|
---|
119 | }
|
---|
120 |
|
---|
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);
|
---|
125 |
|
---|
126 | struct unit_vmt {
|
---|
127 | size_t size;
|
---|
128 |
|
---|
129 | void (*init)(unit_t *);
|
---|
130 |
|
---|
131 | void (*destroy)(unit_t *);
|
---|
132 |
|
---|
133 | errno_t (*load)(unit_t *, ini_configuration_t *, text_parse_t *);
|
---|
134 |
|
---|
135 | errno_t (*start)(unit_t *);
|
---|
136 |
|
---|
137 | errno_t (*stop)(unit_t *);
|
---|
138 |
|
---|
139 | void (*exposee_created)(unit_t *);
|
---|
140 |
|
---|
141 | void (*fail)(unit_t *);
|
---|
142 | };
|
---|
143 |
|
---|
144 | extern unit_vmt_t *unit_type_vmts[];
|
---|
145 |
|
---|
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, \
|
---|
153 | .stop = &PREFIX##_stop, \
|
---|
154 | .exposee_created = &PREFIX##_exposee_created, \
|
---|
155 | .fail = &PREFIX##_fail \
|
---|
156 | };
|
---|
157 |
|
---|
158 | #define UNIT_VMT(UNIT) unit_type_vmts[(UNIT)->type]
|
---|
159 |
|
---|
160 | extern unit_t *unit_create(unit_type_t);
|
---|
161 | extern void unit_destroy(unit_t **);
|
---|
162 |
|
---|
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 *);
|
---|
166 | extern void unit_exposee_created(unit_t *);
|
---|
167 | extern void unit_fail(unit_t *);
|
---|
168 |
|
---|
169 | extern void unit_notify_state(unit_t *);
|
---|
170 |
|
---|
171 | extern unit_type_t unit_type_name_to_type(const char *);
|
---|
172 |
|
---|
173 | extern const char *unit_name(const unit_t *);
|
---|
174 | extern void unit_log_condition(unit_t *unit);
|
---|
175 |
|
---|
176 | extern bool unit_parse_unit_list(const char *, void *, text_parse_t *, size_t);
|
---|
177 | extern bool unit_parse_condition_architecture(const char *, void *, text_parse_t *, size_t);
|
---|
178 |
|
---|
179 | #endif
|
---|