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 struct {
|
---|
58 | /** Link to name-to-unit hash table */
|
---|
59 | ht_link_t units_by_name;
|
---|
60 |
|
---|
61 | /** Link to handle-to-unit hash table */
|
---|
62 | ht_link_t units_by_handle;
|
---|
63 |
|
---|
64 | /** Link to list of all units */
|
---|
65 | link_t units;
|
---|
66 |
|
---|
67 | /** Link to queue, when BFS traversing units */
|
---|
68 | link_t bfs_link;
|
---|
69 |
|
---|
70 | /** Mark during BFS traversal unit is already queued */
|
---|
71 | bool bfs_tag;
|
---|
72 |
|
---|
73 | /** Auxiliary data for BFS traverse users .*/
|
---|
74 | void *bfs_data;
|
---|
75 |
|
---|
76 | /** Job assigned to unit in transitional state */
|
---|
77 | job_t *job;
|
---|
78 |
|
---|
79 | /** Handle for IPC (immutable) */
|
---|
80 | unit_handle_t handle;
|
---|
81 |
|
---|
82 | /** Unit type (immutable) */
|
---|
83 | unit_type_t type;
|
---|
84 |
|
---|
85 | /** Unit name (immutable) */
|
---|
86 | char *name;
|
---|
87 |
|
---|
88 | unit_state_t state;
|
---|
89 |
|
---|
90 | repo_state_t repo_state;
|
---|
91 |
|
---|
92 | list_t edges_in;
|
---|
93 | list_t edges_out;
|
---|
94 | } unit_t;
|
---|
95 |
|
---|
96 | #include "unit_cfg.h"
|
---|
97 | #include "unit_mnt.h"
|
---|
98 | #include "unit_tgt.h"
|
---|
99 | #include "unit_svc.h"
|
---|
100 |
|
---|
101 | #define DEFINE_CAST(NAME, TYPE, ENUM_TYPE) \
|
---|
102 | static inline TYPE *CAST_##NAME(unit_t *u) \
|
---|
103 | { \
|
---|
104 | if (u->type == ENUM_TYPE) \
|
---|
105 | return (TYPE *)u; \
|
---|
106 | else \
|
---|
107 | return NULL; \
|
---|
108 | }
|
---|
109 |
|
---|
110 | DEFINE_CAST(CFG, unit_cfg_t, UNIT_CONFIGURATION);
|
---|
111 | DEFINE_CAST(MNT, unit_mnt_t, UNIT_MOUNT);
|
---|
112 | DEFINE_CAST(TGT, unit_tgt_t, UNIT_TARGET);
|
---|
113 | DEFINE_CAST(SVC, unit_svc_t, UNIT_SERVICE);
|
---|
114 |
|
---|
115 | struct unit_vmt {
|
---|
116 | size_t size;
|
---|
117 |
|
---|
118 | void (*init)(unit_t *);
|
---|
119 |
|
---|
120 | void (*destroy)(unit_t *);
|
---|
121 |
|
---|
122 | errno_t (*load)(unit_t *, ini_configuration_t *, text_parse_t *);
|
---|
123 |
|
---|
124 | errno_t (*start)(unit_t *);
|
---|
125 |
|
---|
126 | errno_t (*stop)(unit_t *);
|
---|
127 |
|
---|
128 | void (*exposee_created)(unit_t *);
|
---|
129 |
|
---|
130 | void (*fail)(unit_t *);
|
---|
131 | };
|
---|
132 |
|
---|
133 | extern unit_vmt_t *unit_type_vmts[];
|
---|
134 |
|
---|
135 | #define DEFINE_UNIT_VMT(PREFIX) \
|
---|
136 | unit_vmt_t PREFIX##_vmt = { \
|
---|
137 | .size = sizeof(PREFIX##_t), \
|
---|
138 | .init = &PREFIX##_init, \
|
---|
139 | .load = &PREFIX##_load, \
|
---|
140 | .destroy = &PREFIX##_destroy, \
|
---|
141 | .start = &PREFIX##_start, \
|
---|
142 | .stop = &PREFIX##_stop, \
|
---|
143 | .exposee_created = &PREFIX##_exposee_created, \
|
---|
144 | .fail = &PREFIX##_fail \
|
---|
145 | };
|
---|
146 |
|
---|
147 | #define UNIT_VMT(UNIT) unit_type_vmts[(UNIT)->type]
|
---|
148 |
|
---|
149 | extern unit_t *unit_create(unit_type_t);
|
---|
150 | extern void unit_destroy(unit_t **);
|
---|
151 |
|
---|
152 | extern errno_t unit_load(unit_t *, ini_configuration_t *, text_parse_t *);
|
---|
153 | extern errno_t unit_start(unit_t *);
|
---|
154 | extern errno_t unit_stop(unit_t *);
|
---|
155 | extern void unit_exposee_created(unit_t *);
|
---|
156 | extern void unit_fail(unit_t *);
|
---|
157 |
|
---|
158 | extern void unit_notify_state(unit_t *);
|
---|
159 |
|
---|
160 | extern unit_type_t unit_type_name_to_type(const char *);
|
---|
161 |
|
---|
162 | extern const char *unit_name(const unit_t *);
|
---|
163 |
|
---|
164 | extern bool unit_parse_unit_list(const char *, void *, text_parse_t *, size_t);
|
---|
165 |
|
---|
166 | #endif
|
---|