Merge lp:~upstart-devel/upstart/stateful-reexec into lp:upstart
- stateful-reexec
- Merge into trunk
Status: | Merged |
---|---|
Merged at revision: | 1382 |
Proposed branch: | lp:~upstart-devel/upstart/stateful-reexec |
Merge into: | lp:upstart |
Diff against target: |
15453 lines (+11010/-675) 41 files modified
ChangeLog (+16/-0) configure.ac (+33/-0) extra/man/upstart-udev-bridge.8 (+4/-1) init/Makefile.am (+64/-33) init/blocked.c (+53/-0) init/blocked.h (+8/-0) init/conf.c (+93/-35) init/conf.h (+12/-4) init/control.c (+145/-2) init/control.h (+15/-0) init/event.c (+331/-0) init/event.h (+22/-0) init/event_operator.c (+175/-7) init/event_operator.h (+7/-4) init/job.c (+726/-7) init/job.h (+14/-2) init/job_class.c (+712/-3) init/job_class.h (+36/-1) init/job_process.c (+44/-7) init/job_process.h (+6/-0) init/log.c (+213/-15) init/log.h (+13/-5) init/main.c (+114/-26) init/parse_job.c (+42/-1) init/parse_job.h (+5/-0) init/process.c (+223/-0) init/process.h (+38/-3) init/session.c (+298/-33) init/session.h (+19/-6) init/state.c (+2008/-0) init/state.h (+1226/-0) init/tests/test_conf.c (+1/-43) init/tests/test_event_operator.c (+92/-1) init/tests/test_job.c (+215/-60) init/tests/test_job_process.c (+508/-291) init/tests/test_log.c (+19/-35) init/tests/test_state.c (+2637/-0) init/tests/test_util.c (+27/-0) init/tests/test_util.h (+304/-0) util/man/telinit.8 (+4/-3) util/tests/test_initctl.c (+488/-47) |
To merge this branch: | bzr merge lp:~upstart-devel/upstart/stateful-reexec |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Steve Langasek | Approve | ||
Review via email: mp+133275@code.launchpad.net |
This proposal supersedes a proposal from 2012-11-07.
Commit message
Description of the change
* Added stateful re-exec support such that when Upstart is asked
to restart itself using 'telinit u', the new instance of PID 1
will retain knowledge of all system jobs and running instances.
See also: https:/
- 1474. By James Hunt
-
Sync with lp:~upstart-devel/upstart/stateful-reexec.
- 1475. By James Hunt
-
* init/job_class.c: Formatting.
* init/log.c: Corrected documentation for log_unflushed_files.
* init/state.h: Explained why log_unflushed_files is not currently
serialised. - 1476. By James Hunt
-
* extra/man/
upstart- udev-bridge. 8: Corrected explanation of actions in
udev events (thanks to Stefan Bader for pointing out this problem).
* init/blocked.c: blocked_type_str_ to_enum( ): Assert argument.
* init/job_class.c: job_class_expect_ type_str_ to_enum( ): Assert
argument.
* init/job_process.c: job_process_run(): Reverted unnecessary check on 'p'.
* init/log.c: log_deserialise(): Added cast and explanation why return code
from state_toggle_cloexec( ) not checked.
* init/process.c: process_type_str_ to_enum( ): Assert argument.
* init/session.c:
- session_create_ conf_source( ): Removed redundant getenv() call.
- session_deserialise( ): Removed incorrect uid_t initialisation.
* init/state.h:
- state_get_json_str_ array_generic( ): Actually use env parameter.
- Remove warn_unused_result from state_toggle_cloexec( ) to avoid compile
failure with strict flags.
* init/tests/test_state. c:
- Use rlim_t.
- test_enums():
- Don't call *_str_to_enum() if previous *_enum_to_str() failed.
- Add additional checks for invalid enum values.
- main(): Remove redundant setenv().
* init/tests/test_util. c: string_check(): Make initial test less
cryptic.
James Hunt (jamesodhunt) wrote : | # |
> Hi James,
>
> Reviewing just the parts of the diff that are new since the last time I looked at the branch.
>
> This part of init/session.c looks strange:
>
> + if (getenv ("UPSTART_TESTS"))
> + return;
> +
> if (conf_source_reload (source) < 0) {
> NihError *err;
>
> The commit message only says:
> - session_
> testing mode.
> without explaining why.
I've now removed this: it was required for an earlier iteration of the test code, but is now redundant.
>
> In init/tests/
>
> + * Returns 0 if strings are identical or both NULL, else 1.
> + **/
> +int
> +string_check (const char *a, const char *b)
> +{
> + if ((a == b) && !a)
> + return 0;
> +
>
> This '&& !a' seems unnecessary - surely if a == b and are /not/ null, there's no need to call strcmp to confirm that the strings are equal.
That test is a just a more compact way of saying:
if (a == NULL && b == NULL)
... such that -- considered with the following test -- we never pass NULL to strcmp(). However, it is slightly cryptic, so for symmetry with the test that follows it I've changed the test to:
if (!a && !b)
>
>
> === modified file 'init/blocked.c'
> --- init/blocked.c 2012-07-30 14:07:37 +0000
> +++ init/blocked.c 2012-11-13 06:01:51 +0000
> @@ -131,6 +131,9 @@ blocked_
> BlockedType
> blocked_
> {
> + if (! type)
> + goto error;
> +
> state_str_to_enum (BLOCKED_JOB, type);
> state_str_to_enum (BLOCKED_EVENT, type);
> state_str_to_enum (BLOCKED_
> @@ -141,5 +144,6 @@ blocked_
> state_str_to_enum (BLOCKED_
> state_str_to_enum (BLOCKED_
>
> +error:
> return -1;
> }
>
>
> The above is documented as "Made resilient to error conditions". But this doesn't appear to be a real-world error condition at all; the only place in the code that blocked_
>
> if (! state_get_
> goto error;
>
> blocked_type = blocked_
>
> This means there's no possibility of the function ever being called with a null argument (and likewise for the other str_to_enum functions), so this must be here just for the test suite. I don't think it's a good idea to complicate either the code or the test suite by handling errors that the code is written to never allow! I've pushed a branch to address this at
> lp:~vorlon/upstart/stateful-reexec-dont-shape-code-to-impossible-tests and raised an MP for your consideration.
Thanks. I've changed this and the other *_str_to_enum() functions to assert their argument and updated the tests accordingly.
>
> === modified file 'init/job_
> --- init/job_process.c 2012-08-25 08:53:37 +0000
> +++ init/job_process.c 2012-11-13 10:47:50 +0000
> @@ -211,7 +211,7 @@ job_process_run (...
Preview Diff
1 | === modified file 'ChangeLog' | |||
2 | --- ChangeLog 2012-10-22 13:54:50 +0000 | |||
3 | +++ ChangeLog 2012-11-14 14:49:20 +0000 | |||
4 | @@ -1,3 +1,19 @@ | |||
5 | 1 | 2012-11-07 James Hunt <james.hunt@ubuntu.com> | ||
6 | 2 | |||
7 | 3 | * Added stateful re-exec support such that when Upstart is asked | ||
8 | 4 | to restart itself using 'telinit u', the new instance of PID 1 | ||
9 | 5 | will retain knowledge of all system jobs and running instances. | ||
10 | 6 | |||
11 | 7 | 2012-09-10 James Hunt <james.hunt@ubuntu.com> | ||
12 | 8 | |||
13 | 9 | * Merged lp:~jconti/upstart/fix_empty_chroot. | ||
14 | 10 | |||
15 | 11 | 2012-05-23 James Hunt <james.hunt@ubuntu.com> | ||
16 | 12 | |||
17 | 13 | * init/main.c: Add in "bare" re-exec handling from Ubuntu | ||
18 | 14 | branch. | ||
19 | 15 | * init/main.c: Unhide "restart" option. | ||
20 | 16 | |||
21 | 1 | 2012-10-22 James Hunt <james.hunt@ubuntu.com> | 17 | 2012-10-22 James Hunt <james.hunt@ubuntu.com> |
22 | 2 | 18 | ||
23 | 3 | * init/parse_job.c: stanza_kill(): Actually save parsed | 19 | * init/parse_job.c: stanza_kill(): Actually save parsed |
24 | 4 | 20 | ||
25 | === modified file 'configure.ac' | |||
26 | --- configure.ac 2012-03-22 11:37:20 +0000 | |||
27 | +++ configure.ac 2012-11-14 14:49:20 +0000 | |||
28 | @@ -32,6 +32,24 @@ | |||
29 | 32 | PKG_CHECK_MODULES([DBUS], [dbus-1 >= 1.2.16]) | 32 | PKG_CHECK_MODULES([DBUS], [dbus-1 >= 1.2.16]) |
30 | 33 | PKG_CHECK_MODULES([UDEV], [libudev >= 146], [have_udev=yes], [have_udev=no]) | 33 | PKG_CHECK_MODULES([UDEV], [libudev >= 146], [have_udev=yes], [have_udev=no]) |
31 | 34 | 34 | ||
32 | 35 | # Reasons for requiring this library version: | ||
33 | 36 | # | ||
34 | 37 | # 1) RFC 4627, the JSON "memo" (it is *NOT* a standard!) helpfully fails | ||
35 | 38 | # to specify the maximum size of an integer type. And yet, JSON is | ||
36 | 39 | # supposedly a subset of ECMA-262, which specifies a Number type to | ||
37 | 40 | # be 64-bits. | ||
38 | 41 | # | ||
39 | 42 | # The loose JSON "memo" might expalain why older versions of JSON-C encode | ||
40 | 43 | # a JSON Number in sizoef(int) bytes which is only 32-bits on 32-bit | ||
41 | 44 | # systems. This is not acceptable for Upstart which needs to be able | ||
42 | 45 | # to encode 'size_t' and 'unsigned long' types which can be larger | ||
43 | 46 | # than 'int'. | ||
44 | 47 | # | ||
45 | 48 | # 2) New json_tokener_parse_verbose() function required to detect invalid | ||
46 | 49 | # JSON (!) | ||
47 | 50 | # | ||
48 | 51 | PKG_CHECK_MODULES([JSON], [json >= 0.10]) | ||
49 | 52 | |||
50 | 35 | AM_CONDITIONAL([HAVE_UDEV], [test "$have_udev" = yes]) | 53 | AM_CONDITIONAL([HAVE_UDEV], [test "$have_udev" = yes]) |
51 | 36 | 54 | ||
52 | 37 | # Checks for header files. | 55 | # Checks for header files. |
53 | @@ -42,6 +60,21 @@ | |||
54 | 42 | AM_PROG_CC_C_O | 60 | AM_PROG_CC_C_O |
55 | 43 | NIH_C_THREAD | 61 | NIH_C_THREAD |
56 | 44 | 62 | ||
57 | 63 | AC_CHECK_SIZEOF(int) | ||
58 | 64 | AC_CHECK_SIZEOF(uid_t) | ||
59 | 65 | AC_CHECK_SIZEOF(pid_t) | ||
60 | 66 | AC_CHECK_SIZEOF(time_t) | ||
61 | 67 | AC_CHECK_SIZEOF(mode_t) | ||
62 | 68 | AC_CHECK_SIZEOF(size_t) | ||
63 | 69 | AC_CHECK_SIZEOF(ssize_t) | ||
64 | 70 | |||
65 | 71 | # Unlikely to hit this limit for a while, but it pays to be safe. | ||
66 | 72 | for type in int uid_t pid_t time_t mode_t size_t ssize_t | ||
67 | 73 | do | ||
68 | 74 | eval value="\$ac_cv_sizeof_${type}" | ||
69 | 75 | test "$value" -gt 8 && AC_MSG_ERROR([type $type is $value bytes, larger than JSON-C can represent]) | ||
70 | 76 | done | ||
71 | 77 | |||
72 | 45 | # Checks for library functions. | 78 | # Checks for library functions. |
73 | 46 | 79 | ||
74 | 47 | # Other checks | 80 | # Other checks |
75 | 48 | 81 | ||
76 | === modified file 'extra/man/upstart-udev-bridge.8' | |||
77 | --- extra/man/upstart-udev-bridge.8 2011-12-15 16:11:14 +0000 | |||
78 | +++ extra/man/upstart-udev-bridge.8 2012-11-14 14:49:20 +0000 | |||
79 | @@ -16,7 +16,10 @@ | |||
80 | 16 | events for them. | 16 | events for them. |
81 | 17 | 17 | ||
82 | 18 | It emits events which match the pattern "\fIS\fP\-device\-\fIA\fP" where | 18 | It emits events which match the pattern "\fIS\fP\-device\-\fIA\fP" where |
84 | 19 | \(aqS\(aq is the udev \fIsubsystem\fP and \(aqA\(aq is the udev \fIaction\fP. | 19 | \(aqS\(aq is the udev \fIsubsystem\fP and \(aqA\(aq is one of |
85 | 20 | \fIadded\fR, \fIchanged\fR or \fIremoved\fR, | ||
86 | 21 | which correspond to the past tense of the udev \(aqaction\(aq. | ||
87 | 22 | |||
88 | 20 | See \fBudev\fP(7) and for further details. | 23 | See \fBudev\fP(7) and for further details. |
89 | 21 | 24 | ||
90 | 22 | Assuming \fI/sys\fP is mounted, possible values for \fIsubsystem\fP for | 25 | Assuming \fI/sys\fP is mounted, possible values for \fIsubsystem\fP for |
91 | 23 | 26 | ||
92 | === modified file 'init/Makefile.am' | |||
93 | --- init/Makefile.am 2012-10-22 13:54:50 +0000 | |||
94 | +++ init/Makefile.am 2012-11-14 14:49:20 +0000 | |||
95 | @@ -5,7 +5,8 @@ | |||
96 | 5 | AM_CFLAGS = \ | 5 | AM_CFLAGS = \ |
97 | 6 | $(NIH_CFLAGS) \ | 6 | $(NIH_CFLAGS) \ |
98 | 7 | $(NIH_DBUS_CFLAGS) \ | 7 | $(NIH_DBUS_CFLAGS) \ |
100 | 8 | $(DBUS_CFLAGS) | 8 | $(DBUS_CFLAGS) \ |
101 | 9 | $(JSON_CFLAGS) | ||
102 | 9 | 10 | ||
103 | 10 | AM_CPPFLAGS = \ | 11 | AM_CPPFLAGS = \ |
104 | 11 | -DLOCALEDIR="\"$(localedir)\"" \ | 12 | -DLOCALEDIR="\"$(localedir)\"" \ |
105 | @@ -41,6 +42,7 @@ | |||
106 | 41 | environ.c environ.h \ | 42 | environ.c environ.h \ |
107 | 42 | process.c process.h \ | 43 | process.c process.h \ |
108 | 43 | session.c session.h \ | 44 | session.c session.h \ |
109 | 45 | state.c state.h \ | ||
110 | 44 | job_class.c job_class.h \ | 46 | job_class.c job_class.h \ |
111 | 45 | job_process.c job_process.h \ | 47 | job_process.c job_process.h \ |
112 | 46 | job.c job.h \ | 48 | job.c job.h \ |
113 | @@ -62,6 +64,7 @@ | |||
114 | 62 | $(NIH_LIBS) \ | 64 | $(NIH_LIBS) \ |
115 | 63 | $(NIH_DBUS_LIBS) \ | 65 | $(NIH_DBUS_LIBS) \ |
116 | 64 | $(DBUS_LIBS) \ | 66 | $(DBUS_LIBS) \ |
117 | 67 | $(JSON_LIBS) \ | ||
118 | 65 | -lrt | 68 | -lrt |
119 | 66 | 69 | ||
120 | 67 | 70 | ||
121 | @@ -109,7 +112,6 @@ | |||
122 | 109 | --default-interface=com.ubuntu.Upstart0_6.Instance \ | 112 | --default-interface=com.ubuntu.Upstart0_6.Instance \ |
123 | 110 | --output=$@ $< | 113 | --output=$@ $< |
124 | 111 | 114 | ||
125 | 112 | |||
126 | 113 | # These have to be built sources because we can't compile object files | 115 | # These have to be built sources because we can't compile object files |
127 | 114 | # without the header file existing first | 116 | # without the header file existing first |
128 | 115 | BUILT_SOURCES = \ | 117 | BUILT_SOURCES = \ |
129 | @@ -125,6 +127,8 @@ | |||
130 | 125 | 127 | ||
131 | 126 | EXTRA_DIST = init.supp | 128 | EXTRA_DIST = init.supp |
132 | 127 | 129 | ||
133 | 130 | test_util_SOURCES = \ | ||
134 | 131 | tests/test_util.c tests/test_util.h | ||
135 | 128 | 132 | ||
136 | 129 | TESTS = \ | 133 | TESTS = \ |
137 | 130 | test_system \ | 134 | test_system \ |
138 | @@ -134,6 +138,7 @@ | |||
139 | 134 | test_job_process \ | 138 | test_job_process \ |
140 | 135 | test_job \ | 139 | test_job \ |
141 | 136 | test_log \ | 140 | test_log \ |
142 | 141 | test_state \ | ||
143 | 137 | test_event \ | 142 | test_event \ |
144 | 138 | test_event_operator \ | 143 | test_event_operator \ |
145 | 139 | test_blocked \ | 144 | test_blocked \ |
146 | @@ -156,41 +161,44 @@ | |||
147 | 156 | environ.o \ | 161 | environ.o \ |
148 | 157 | $(NIH_LIBS) | 162 | $(NIH_LIBS) |
149 | 158 | 163 | ||
151 | 159 | test_process_SOURCES = tests/test_process.c | 164 | test_process_SOURCES = tests/test_process.c $(test_util_SOURCES) |
152 | 160 | test_process_LDADD = \ | 165 | test_process_LDADD = \ |
153 | 161 | system.o environ.o process.o \ | 166 | system.o environ.o process.o \ |
154 | 162 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 167 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
155 | 163 | parse_job.o parse_conf.o conf.o control.o \ | 168 | parse_job.o parse_conf.o conf.o control.o \ |
157 | 164 | session.o log.o \ | 169 | session.o log.o state.o \ |
158 | 165 | com.ubuntu.Upstart.o \ | 170 | com.ubuntu.Upstart.o \ |
159 | 166 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 171 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
160 | 167 | $(NIH_LIBS) \ | 172 | $(NIH_LIBS) \ |
161 | 168 | $(NIH_DBUS_LIBS) \ | 173 | $(NIH_DBUS_LIBS) \ |
163 | 169 | $(DBUS_LIBS) | 174 | $(DBUS_LIBS) \ |
164 | 175 | $(JSON_LIBS) | ||
165 | 170 | 176 | ||
166 | 171 | test_job_class_SOURCES = tests/test_job_class.c | 177 | test_job_class_SOURCES = tests/test_job_class.c |
167 | 172 | test_job_class_LDADD = \ | 178 | test_job_class_LDADD = \ |
168 | 173 | system.o environ.o process.o \ | 179 | system.o environ.o process.o \ |
169 | 174 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 180 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
170 | 175 | parse_job.o parse_conf.o conf.o control.o \ | 181 | parse_job.o parse_conf.o conf.o control.o \ |
172 | 176 | session.o log.o \ | 182 | session.o log.o state.o \ |
173 | 177 | com.ubuntu.Upstart.o \ | 183 | com.ubuntu.Upstart.o \ |
174 | 178 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 184 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
175 | 179 | $(NIH_LIBS) \ | 185 | $(NIH_LIBS) \ |
176 | 180 | $(NIH_DBUS_LIBS) \ | 186 | $(NIH_DBUS_LIBS) \ |
178 | 181 | $(DBUS_LIBS) | 187 | $(DBUS_LIBS) \ |
179 | 188 | $(JSON_LIBS) | ||
180 | 182 | 189 | ||
182 | 183 | test_job_process_SOURCES = tests/test_job_process.c | 190 | test_job_process_SOURCES = tests/test_job_process.c $(test_util_SOURCES) |
183 | 184 | test_job_process_LDADD = \ | 191 | test_job_process_LDADD = \ |
184 | 185 | system.o environ.o process.o \ | 192 | system.o environ.o process.o \ |
185 | 186 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 193 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
186 | 187 | parse_job.o parse_conf.o conf.o control.o \ | 194 | parse_job.o parse_conf.o conf.o control.o \ |
188 | 188 | session.o log.o \ | 195 | session.o log.o state.o \ |
189 | 189 | com.ubuntu.Upstart.o \ | 196 | com.ubuntu.Upstart.o \ |
190 | 190 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 197 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
191 | 191 | $(NIH_LIBS) \ | 198 | $(NIH_LIBS) \ |
192 | 192 | $(NIH_DBUS_LIBS) \ | 199 | $(NIH_DBUS_LIBS) \ |
193 | 193 | $(DBUS_LIBS) \ | 200 | $(DBUS_LIBS) \ |
194 | 201 | $(JSON_LIBS) \ | ||
195 | 194 | -lutil | 202 | -lutil |
196 | 195 | 203 | ||
197 | 196 | test_job_SOURCES = tests/test_job.c | 204 | test_job_SOURCES = tests/test_job.c |
198 | @@ -198,24 +206,40 @@ | |||
199 | 198 | system.o environ.o process.o \ | 206 | system.o environ.o process.o \ |
200 | 199 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 207 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
201 | 200 | parse_job.o parse_conf.o conf.o control.o \ | 208 | parse_job.o parse_conf.o conf.o control.o \ |
203 | 201 | session.o log.o \ | 209 | session.o log.o state.o \ |
204 | 202 | com.ubuntu.Upstart.o \ | 210 | com.ubuntu.Upstart.o \ |
205 | 203 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 211 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
206 | 204 | $(NIH_LIBS) \ | 212 | $(NIH_LIBS) \ |
207 | 205 | $(NIH_DBUS_LIBS) \ | 213 | $(NIH_DBUS_LIBS) \ |
209 | 206 | $(DBUS_LIBS) | 214 | $(DBUS_LIBS) \ |
210 | 215 | $(JSON_LIBS) | ||
211 | 207 | 216 | ||
213 | 208 | test_log_SOURCES = tests/test_log.c | 217 | test_log_SOURCES = tests/test_log.c $(test_util_SOURCES) |
214 | 209 | test_log_LDADD = \ | 218 | test_log_LDADD = \ |
215 | 210 | system.o environ.o process.o \ | 219 | system.o environ.o process.o \ |
216 | 211 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 220 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
217 | 212 | parse_job.o parse_conf.o conf.o control.o \ | 221 | parse_job.o parse_conf.o conf.o control.o \ |
224 | 213 | session.o log.o \ | 222 | session.o log.o state.o \ |
225 | 214 | com.ubuntu.Upstart.o \ | 223 | com.ubuntu.Upstart.o \ |
226 | 215 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 224 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
227 | 216 | $(NIH_LIBS) \ | 225 | $(NIH_LIBS) \ |
228 | 217 | $(NIH_DBUS_LIBS) \ | 226 | $(NIH_DBUS_LIBS) \ |
229 | 218 | $(DBUS_LIBS) \ | 227 | $(DBUS_LIBS) \ |
230 | 228 | $(JSON_LIBS) \ | ||
231 | 229 | -lutil | ||
232 | 230 | |||
233 | 231 | test_state_SOURCES = tests/test_state.c $(test_util_SOURCES) | ||
234 | 232 | test_state_LDADD = \ | ||
235 | 233 | system.o environ.o process.o \ | ||
236 | 234 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | ||
237 | 235 | parse_job.o parse_conf.o conf.o control.o \ | ||
238 | 236 | session.o log.o state.o \ | ||
239 | 237 | com.ubuntu.Upstart.o \ | ||
240 | 238 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | ||
241 | 239 | $(NIH_LIBS) \ | ||
242 | 240 | $(NIH_DBUS_LIBS) \ | ||
243 | 241 | $(DBUS_LIBS) \ | ||
244 | 242 | $(JSON_LIBS) \ | ||
245 | 219 | -lutil | 243 | -lutil |
246 | 220 | 244 | ||
247 | 221 | test_event_SOURCES = tests/test_event.c | 245 | test_event_SOURCES = tests/test_event.c |
248 | @@ -223,84 +247,91 @@ | |||
249 | 223 | system.o environ.o process.o \ | 247 | system.o environ.o process.o \ |
250 | 224 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 248 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
251 | 225 | parse_job.o parse_conf.o conf.o control.o \ | 249 | parse_job.o parse_conf.o conf.o control.o \ |
253 | 226 | session.o log.o \ | 250 | session.o log.o state.o \ |
254 | 227 | com.ubuntu.Upstart.o \ | 251 | com.ubuntu.Upstart.o \ |
255 | 228 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 252 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
256 | 229 | $(NIH_LIBS) \ | 253 | $(NIH_LIBS) \ |
257 | 230 | $(NIH_DBUS_LIBS) \ | 254 | $(NIH_DBUS_LIBS) \ |
259 | 231 | $(DBUS_LIBS) | 255 | $(DBUS_LIBS) \ |
260 | 256 | $(JSON_LIBS) | ||
261 | 232 | 257 | ||
262 | 233 | test_event_operator_SOURCES = tests/test_event_operator.c | 258 | test_event_operator_SOURCES = tests/test_event_operator.c |
263 | 234 | test_event_operator_LDADD = \ | 259 | test_event_operator_LDADD = \ |
264 | 235 | system.o environ.o process.o \ | 260 | system.o environ.o process.o \ |
265 | 236 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 261 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
266 | 237 | parse_job.o parse_conf.o conf.o control.o \ | 262 | parse_job.o parse_conf.o conf.o control.o \ |
268 | 238 | session.o log.o \ | 263 | session.o log.o state.o \ |
269 | 239 | com.ubuntu.Upstart.o \ | 264 | com.ubuntu.Upstart.o \ |
270 | 240 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 265 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
271 | 241 | $(NIH_LIBS) \ | 266 | $(NIH_LIBS) \ |
272 | 242 | $(NIH_DBUS_LIBS) \ | 267 | $(NIH_DBUS_LIBS) \ |
274 | 243 | $(DBUS_LIBS) | 268 | $(DBUS_LIBS) \ |
275 | 269 | $(JSON_LIBS) | ||
276 | 244 | 270 | ||
277 | 245 | test_blocked_SOURCES = tests/test_blocked.c | 271 | test_blocked_SOURCES = tests/test_blocked.c |
278 | 246 | test_blocked_LDADD = \ | 272 | test_blocked_LDADD = \ |
279 | 247 | system.o environ.o process.o \ | 273 | system.o environ.o process.o \ |
280 | 248 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 274 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
281 | 249 | parse_job.o parse_conf.o conf.o control.o \ | 275 | parse_job.o parse_conf.o conf.o control.o \ |
283 | 250 | session.o log.o \ | 276 | session.o log.o state.o \ |
284 | 251 | com.ubuntu.Upstart.o \ | 277 | com.ubuntu.Upstart.o \ |
285 | 252 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 278 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
286 | 253 | $(NIH_LIBS) \ | 279 | $(NIH_LIBS) \ |
287 | 254 | $(NIH_DBUS_LIBS) \ | 280 | $(NIH_DBUS_LIBS) \ |
289 | 255 | $(DBUS_LIBS) | 281 | $(DBUS_LIBS) \ |
290 | 282 | $(JSON_LIBS) | ||
291 | 256 | 283 | ||
292 | 257 | test_parse_job_SOURCES = tests/test_parse_job.c | 284 | test_parse_job_SOURCES = tests/test_parse_job.c |
293 | 258 | test_parse_job_LDADD = \ | 285 | test_parse_job_LDADD = \ |
294 | 259 | system.o environ.o process.o \ | 286 | system.o environ.o process.o \ |
295 | 260 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 287 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
296 | 261 | parse_job.o parse_conf.o conf.o control.o \ | 288 | parse_job.o parse_conf.o conf.o control.o \ |
298 | 262 | session.o log.o \ | 289 | session.o log.o state.o \ |
299 | 263 | com.ubuntu.Upstart.o \ | 290 | com.ubuntu.Upstart.o \ |
300 | 264 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 291 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
301 | 265 | $(NIH_LIBS) \ | 292 | $(NIH_LIBS) \ |
302 | 266 | $(NIH_DBUS_LIBS) \ | 293 | $(NIH_DBUS_LIBS) \ |
304 | 267 | $(DBUS_LIBS) | 294 | $(DBUS_LIBS) \ |
305 | 295 | $(JSON_LIBS) | ||
306 | 268 | 296 | ||
307 | 269 | test_parse_conf_SOURCES = tests/test_parse_conf.c | 297 | test_parse_conf_SOURCES = tests/test_parse_conf.c |
308 | 270 | test_parse_conf_LDADD = \ | 298 | test_parse_conf_LDADD = \ |
309 | 271 | system.o environ.o process.o \ | 299 | system.o environ.o process.o \ |
310 | 272 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 300 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
311 | 273 | parse_job.o parse_conf.o conf.o control.o \ | 301 | parse_job.o parse_conf.o conf.o control.o \ |
313 | 274 | session.o log.o \ | 302 | session.o log.o state.o \ |
314 | 275 | com.ubuntu.Upstart.o \ | 303 | com.ubuntu.Upstart.o \ |
315 | 276 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 304 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
316 | 277 | $(NIH_LIBS) \ | 305 | $(NIH_LIBS) \ |
317 | 278 | $(NIH_DBUS_LIBS) \ | 306 | $(NIH_DBUS_LIBS) \ |
319 | 279 | $(DBUS_LIBS) | 307 | $(DBUS_LIBS) \ |
320 | 308 | $(JSON_LIBS) | ||
321 | 280 | 309 | ||
323 | 281 | test_conf_SOURCES = tests/test_conf.c | 310 | test_conf_SOURCES = tests/test_conf.c $(test_util_SOURCES) |
324 | 282 | test_conf_LDADD = \ | 311 | test_conf_LDADD = \ |
325 | 283 | system.o environ.o process.o \ | 312 | system.o environ.o process.o \ |
326 | 284 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 313 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
327 | 285 | parse_job.o parse_conf.o conf.o control.o \ | 314 | parse_job.o parse_conf.o conf.o control.o \ |
329 | 286 | session.o log.o \ | 315 | session.o log.o state.o \ |
330 | 287 | com.ubuntu.Upstart.o \ | 316 | com.ubuntu.Upstart.o \ |
331 | 288 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 317 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
332 | 289 | $(NIH_LIBS) \ | 318 | $(NIH_LIBS) \ |
333 | 290 | $(NIH_DBUS_LIBS) \ | 319 | $(NIH_DBUS_LIBS) \ |
335 | 291 | $(DBUS_LIBS) | 320 | $(DBUS_LIBS) \ |
336 | 321 | $(JSON_LIBS) | ||
337 | 292 | 322 | ||
338 | 293 | test_control_SOURCES = tests/test_control.c | 323 | test_control_SOURCES = tests/test_control.c |
339 | 294 | test_control_LDADD = \ | 324 | test_control_LDADD = \ |
340 | 295 | system.o environ.o process.o \ | 325 | system.o environ.o process.o \ |
341 | 296 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ | 326 | job_class.o job_process.o job.o event.o event_operator.o blocked.o \ |
342 | 297 | parse_job.o parse_conf.o conf.o control.o \ | 327 | parse_job.o parse_conf.o conf.o control.o \ |
344 | 298 | session.o log.o \ | 328 | session.o log.o state.o \ |
345 | 299 | com.ubuntu.Upstart.o \ | 329 | com.ubuntu.Upstart.o \ |
346 | 300 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ | 330 | com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ |
347 | 301 | $(NIH_LIBS) \ | 331 | $(NIH_LIBS) \ |
348 | 302 | $(NIH_DBUS_LIBS) \ | 332 | $(NIH_DBUS_LIBS) \ |
350 | 303 | $(DBUS_LIBS) | 333 | $(DBUS_LIBS) \ |
351 | 334 | $(JSON_LIBS) | ||
352 | 304 | 335 | ||
353 | 305 | 336 | ||
354 | 306 | install-data-local: | 337 | install-data-local: |
355 | 307 | 338 | ||
356 | === modified file 'init/blocked.c' | |||
357 | --- init/blocked.c 2009-06-23 09:29:35 +0000 | |||
358 | +++ init/blocked.c 2012-11-14 14:49:20 +0000 | |||
359 | @@ -92,3 +92,56 @@ | |||
360 | 92 | 92 | ||
361 | 93 | return blocked; | 93 | return blocked; |
362 | 94 | } | 94 | } |
363 | 95 | |||
364 | 96 | |||
365 | 97 | /** | ||
366 | 98 | * blocked_enum_to_str: | ||
367 | 99 | * | ||
368 | 100 | * @type: BlockedType. | ||
369 | 101 | * | ||
370 | 102 | * Convert Blocked to a string representation. | ||
371 | 103 | * | ||
372 | 104 | * Returns: string representation of @type, or NULL if not known. | ||
373 | 105 | **/ | ||
374 | 106 | const char * | ||
375 | 107 | blocked_type_enum_to_str (BlockedType type) | ||
376 | 108 | { | ||
377 | 109 | state_enum_to_str (BLOCKED_JOB, type); | ||
378 | 110 | state_enum_to_str (BLOCKED_EVENT, type); | ||
379 | 111 | state_enum_to_str (BLOCKED_EMIT_METHOD, type); | ||
380 | 112 | state_enum_to_str (BLOCKED_JOB_START_METHOD, type); | ||
381 | 113 | state_enum_to_str (BLOCKED_JOB_STOP_METHOD, type); | ||
382 | 114 | state_enum_to_str (BLOCKED_JOB_RESTART_METHOD, type); | ||
383 | 115 | state_enum_to_str (BLOCKED_INSTANCE_START_METHOD, type); | ||
384 | 116 | state_enum_to_str (BLOCKED_INSTANCE_STOP_METHOD, type); | ||
385 | 117 | state_enum_to_str (BLOCKED_INSTANCE_RESTART_METHOD, type); | ||
386 | 118 | |||
387 | 119 | return NULL; | ||
388 | 120 | } | ||
389 | 121 | |||
390 | 122 | /** | ||
391 | 123 | * blocked_type_str_to_enum: | ||
392 | 124 | * | ||
393 | 125 | * @type: string BlockedType value. | ||
394 | 126 | * | ||
395 | 127 | * Convert @type back into enum value. | ||
396 | 128 | * | ||
397 | 129 | * Returns: BlockedType representation of @type, or -1 if not known. | ||
398 | 130 | **/ | ||
399 | 131 | BlockedType | ||
400 | 132 | blocked_type_str_to_enum (const char *type) | ||
401 | 133 | { | ||
402 | 134 | nih_assert (type); | ||
403 | 135 | |||
404 | 136 | state_str_to_enum (BLOCKED_JOB, type); | ||
405 | 137 | state_str_to_enum (BLOCKED_EVENT, type); | ||
406 | 138 | state_str_to_enum (BLOCKED_EMIT_METHOD, type); | ||
407 | 139 | state_str_to_enum (BLOCKED_JOB_START_METHOD, type); | ||
408 | 140 | state_str_to_enum (BLOCKED_JOB_STOP_METHOD, type); | ||
409 | 141 | state_str_to_enum (BLOCKED_JOB_RESTART_METHOD, type); | ||
410 | 142 | state_str_to_enum (BLOCKED_INSTANCE_START_METHOD, type); | ||
411 | 143 | state_str_to_enum (BLOCKED_INSTANCE_STOP_METHOD, type); | ||
412 | 144 | state_str_to_enum (BLOCKED_INSTANCE_RESTART_METHOD, type); | ||
413 | 145 | |||
414 | 146 | return -1; | ||
415 | 147 | } | ||
416 | 95 | 148 | ||
417 | === modified file 'init/blocked.h' | |||
418 | --- init/blocked.h 2009-06-23 09:29:35 +0000 | |||
419 | +++ init/blocked.h 2012-11-14 14:49:20 +0000 | |||
420 | @@ -79,6 +79,14 @@ | |||
421 | 79 | Blocked *blocked_new (const void *parent, BlockedType type, void *data) | 79 | Blocked *blocked_new (const void *parent, BlockedType type, void *data) |
422 | 80 | __attribute__ ((warn_unused_result, malloc)); | 80 | __attribute__ ((warn_unused_result, malloc)); |
423 | 81 | 81 | ||
424 | 82 | const char * | ||
425 | 83 | blocked_type_enum_to_str (BlockedType type) | ||
426 | 84 | __attribute__ ((warn_unused_result)); | ||
427 | 85 | |||
428 | 86 | BlockedType | ||
429 | 87 | blocked_type_str_to_enum (const char *type) | ||
430 | 88 | __attribute__ ((warn_unused_result)); | ||
431 | 89 | |||
432 | 82 | NIH_END_EXTERN | 90 | NIH_END_EXTERN |
433 | 83 | 91 | ||
434 | 84 | #endif /* INIT_BLOCKED_H */ | 92 | #endif /* INIT_BLOCKED_H */ |
435 | 85 | 93 | ||
436 | === modified file 'init/conf.c' | |||
437 | --- init/conf.c 2011-06-06 17:05:11 +0000 | |||
438 | +++ init/conf.c 2012-11-14 14:49:20 +0000 | |||
439 | @@ -952,8 +952,8 @@ | |||
440 | 952 | **/ | 952 | **/ |
441 | 953 | static int | 953 | static int |
442 | 954 | conf_reload_path (ConfSource *source, | 954 | conf_reload_path (ConfSource *source, |
445 | 955 | const char *path, | 955 | const char *path, |
446 | 956 | const char *override_path) | 956 | const char *override_path) |
447 | 957 | { | 957 | { |
448 | 958 | ConfFile *file = NULL; | 958 | ConfFile *file = NULL; |
449 | 959 | nih_local char *buf = NULL; | 959 | nih_local char *buf = NULL; |
450 | @@ -1209,52 +1209,58 @@ | |||
451 | 1209 | } | 1209 | } |
452 | 1210 | 1210 | ||
453 | 1211 | void | 1211 | void |
455 | 1212 | debug_show_job_class (const JobClass *job) | 1212 | debug_show_job_class (const JobClass *class) |
456 | 1213 | { | 1213 | { |
460 | 1214 | int i; | 1214 | nih_local char *start_on = NULL; |
461 | 1215 | char **env = (char **)job->env; | 1215 | nih_local char *stop_on = NULL; |
459 | 1216 | char **export = (char **)job->export; | ||
462 | 1217 | 1216 | ||
464 | 1218 | nih_assert (job); | 1217 | nih_assert (class); |
465 | 1219 | 1218 | ||
466 | 1220 | nih_debug ("JobClass %p: name='%s', path='%s', task=%d, " | 1219 | nih_debug ("JobClass %p: name='%s', path='%s', task=%d, " |
467 | 1221 | "respawn=%d, console=%x, deleted=%d, debug=%d", | 1220 | "respawn=%d, console=%x, deleted=%d, debug=%d", |
473 | 1222 | job, job->name, job->path, job->task, | 1221 | class, class->name, class->path, class->task, |
474 | 1223 | job->respawn, job->console, job->deleted, job->debug); | 1222 | class->respawn, class->console, class->deleted, class->debug); |
475 | 1224 | 1223 | ||
476 | 1225 | nih_debug ("\tstart_on=%p, stop_on=%p, emits=%p, process=%p", | 1224 | if (class->start_on) start_on = event_operator_collapse (class->start_on); |
477 | 1226 | job->start_on, job->stop_on, job->emits, job->process); | 1225 | if (class->stop_on) stop_on = event_operator_collapse (class->stop_on); |
478 | 1226 | |||
479 | 1227 | nih_debug ("\tstart_on=%p (%s), stop_on=%p (%s), emits=%p, process=%p", | ||
480 | 1228 | class->start_on, start_on ? start_on : "", | ||
481 | 1229 | class->stop_on, stop_on ? stop_on : "", | ||
482 | 1230 | class->emits, class->process); | ||
483 | 1231 | |||
484 | 1232 | for (int i = 0; i < PROCESS_LAST; i++) { | ||
485 | 1233 | if (class->process[i]) { | ||
486 | 1234 | nih_debug ("Process[%d]=%p: script=%d, cmd='%s'", | ||
487 | 1235 | i, class->process[i], | ||
488 | 1236 | class->process[i]->script, | ||
489 | 1237 | class->process[i]->command); | ||
490 | 1238 | } else { | ||
491 | 1239 | nih_debug ("Process[%d]=%p", | ||
492 | 1240 | i, class->process[i]); | ||
493 | 1241 | } | ||
494 | 1242 | |||
495 | 1243 | |||
496 | 1244 | } | ||
497 | 1227 | 1245 | ||
498 | 1228 | nih_debug ("\tauthor='%s', description='%s'", | 1246 | nih_debug ("\tauthor='%s', description='%s'", |
500 | 1229 | job->author, job->description); | 1247 | class->author, class->description); |
501 | 1230 | 1248 | ||
511 | 1231 | if (env && *env) { | 1249 | if (class->env && *class->env) { |
512 | 1232 | nih_debug ("\tenv:"); | 1250 | nih_local char *env = state_collapse_env ((const char **)class->env); |
513 | 1233 | i = 0; | 1251 | nih_debug ("\tenv:%s", env); |
505 | 1234 | while ( *env ) { | ||
506 | 1235 | nih_debug ("\t\tenv[%d]='%s' (len=%u+1)", | ||
507 | 1236 | i, *env, strlen (*env)); | ||
508 | 1237 | env++; | ||
509 | 1238 | ++i; | ||
510 | 1239 | } | ||
514 | 1240 | } else { | 1252 | } else { |
515 | 1241 | nih_debug ("\tenv: none."); | 1253 | nih_debug ("\tenv: none."); |
516 | 1242 | } | 1254 | } |
517 | 1243 | 1255 | ||
530 | 1244 | 1256 | if (class->export && *class->export) { | |
531 | 1245 | if (export && *export) { | 1257 | nih_local char *export = state_collapse_env ((const char **)class->export); |
532 | 1246 | nih_debug ("\texport:"); | 1258 | nih_debug ("\texport:%s", export); |
533 | 1247 | i = 0; | 1259 | } else { |
522 | 1248 | while ( *export ) { | ||
523 | 1249 | nih_debug ("\t\tenv[%d]='%s' (len=%u+1)", | ||
524 | 1250 | i, *export, strlen (*export)); | ||
525 | 1251 | export++; | ||
526 | 1252 | ++i; | ||
527 | 1253 | } | ||
528 | 1254 | } | ||
529 | 1255 | else { | ||
534 | 1256 | nih_debug ("\texport: none"); | 1260 | nih_debug ("\texport: none"); |
535 | 1257 | } | 1261 | } |
536 | 1262 | |||
537 | 1263 | debug_show_jobs (class->instances); | ||
538 | 1258 | } | 1264 | } |
539 | 1259 | 1265 | ||
540 | 1260 | void | 1266 | void |
541 | @@ -1269,6 +1275,58 @@ | |||
542 | 1269 | } | 1275 | } |
543 | 1270 | 1276 | ||
544 | 1271 | void | 1277 | void |
545 | 1278 | debug_show_job (const Job *job) | ||
546 | 1279 | { | ||
547 | 1280 | nih_local char *env = NULL; | ||
548 | 1281 | nih_local char *start_env = NULL; | ||
549 | 1282 | nih_local char *stop_env = NULL; | ||
550 | 1283 | nih_local char *stop_on = NULL; | ||
551 | 1284 | |||
552 | 1285 | nih_assert (job); | ||
553 | 1286 | |||
554 | 1287 | if (job->env) | ||
555 | 1288 | env = state_collapse_env ((const char **)job->env); | ||
556 | 1289 | |||
557 | 1290 | if (job->start_env) | ||
558 | 1291 | start_env = state_collapse_env ((const char **)job->start_env); | ||
559 | 1292 | |||
560 | 1293 | if (job->stop_env) | ||
561 | 1294 | stop_env = state_collapse_env ((const char **)job->stop_env); | ||
562 | 1295 | |||
563 | 1296 | if (job->stop_on) | ||
564 | 1297 | stop_on = event_operator_collapse (job->stop_on); | ||
565 | 1298 | |||
566 | 1299 | nih_debug ("Job %p: name=%s, class=%p (%s), path=%s, env='%s'" | ||
567 | 1300 | "start_env='%s', stop_env='%s', stop_on='%s', " | ||
568 | 1301 | "goal=%d, state=%d, failed=%d, exit_status=%d", | ||
569 | 1302 | job, | ||
570 | 1303 | job->name ? job->name : "", | ||
571 | 1304 | job->class, job->class->name, | ||
572 | 1305 | job->path, | ||
573 | 1306 | env ? env : "", | ||
574 | 1307 | start_env ? start_env : "", | ||
575 | 1308 | stop_env ? stop_env : "", | ||
576 | 1309 | stop_on ? stop_on : "", | ||
577 | 1310 | job->goal, | ||
578 | 1311 | job->state, | ||
579 | 1312 | job->failed, | ||
580 | 1313 | job->exit_status); | ||
581 | 1314 | } | ||
582 | 1315 | |||
583 | 1316 | void | ||
584 | 1317 | debug_show_jobs (const NihHash *instances) | ||
585 | 1318 | { | ||
586 | 1319 | nih_assert (instances); | ||
587 | 1320 | |||
588 | 1321 | nih_debug ("jobs:"); | ||
589 | 1322 | |||
590 | 1323 | NIH_HASH_FOREACH (instances, iter) { | ||
591 | 1324 | Job *job = (Job *)iter; | ||
592 | 1325 | debug_show_job (job); | ||
593 | 1326 | } | ||
594 | 1327 | } | ||
595 | 1328 | |||
596 | 1329 | void | ||
597 | 1272 | debug_show_event (const Event *event) | 1330 | debug_show_event (const Event *event) |
598 | 1273 | { | 1331 | { |
599 | 1274 | nih_assert (event); | 1332 | nih_assert (event); |
600 | @@ -1308,7 +1366,7 @@ | |||
601 | 1308 | source, source->path, source->type, source->flag); | 1366 | source, source->path, source->type, source->flag); |
602 | 1309 | 1367 | ||
603 | 1310 | nih_debug ("ConfSource %p files (%d):", source, | 1368 | nih_debug ("ConfSource %p files (%d):", source, |
605 | 1311 | debug_count_hash_entries (source->files)); | 1369 | (int)debug_count_hash_entries (source->files)); |
606 | 1312 | 1370 | ||
607 | 1313 | NIH_HASH_FOREACH (source->files, file_iter) { | 1371 | NIH_HASH_FOREACH (source->files, file_iter) { |
608 | 1314 | ConfFile *file = (ConfFile *)file_iter; | 1372 | ConfFile *file = (ConfFile *)file_iter; |
609 | 1315 | 1373 | ||
610 | === modified file 'init/conf.h' | |||
611 | --- init/conf.h 2011-06-06 12:52:08 +0000 | |||
612 | +++ init/conf.h 2012-11-14 14:49:20 +0000 | |||
613 | @@ -39,9 +39,9 @@ | |||
614 | 39 | * what they define within themselves. | 39 | * what they define within themselves. |
615 | 40 | **/ | 40 | **/ |
616 | 41 | typedef enum conf_source_type { | 41 | typedef enum conf_source_type { |
620 | 42 | CONF_FILE, | 42 | CONF_FILE, /* solitary file */ |
621 | 43 | CONF_DIR, | 43 | CONF_DIR, /* FIXME: */ |
622 | 44 | CONF_JOB_DIR, | 44 | CONF_JOB_DIR, /* directory tree of jobs */ |
623 | 45 | } ConfSourceType; | 45 | } ConfSourceType; |
624 | 46 | 46 | ||
625 | 47 | 47 | ||
626 | @@ -133,6 +133,7 @@ | |||
627 | 133 | #ifdef DEBUG | 133 | #ifdef DEBUG |
628 | 134 | 134 | ||
629 | 135 | /* used for debugging only */ | 135 | /* used for debugging only */ |
630 | 136 | #include "job.h" | ||
631 | 136 | 137 | ||
632 | 137 | size_t | 138 | size_t |
633 | 138 | debug_count_hash_entries (const NihHash *hash); | 139 | debug_count_hash_entries (const NihHash *hash); |
634 | @@ -142,7 +143,7 @@ | |||
635 | 142 | __attribute__ ((unused)); | 143 | __attribute__ ((unused)); |
636 | 143 | 144 | ||
637 | 144 | void | 145 | void |
639 | 145 | debug_show_job_class (const JobClass *job) | 146 | debug_show_job_class (const JobClass *class) |
640 | 146 | __attribute__ ((unused)); | 147 | __attribute__ ((unused)); |
641 | 147 | 148 | ||
642 | 148 | void | 149 | void |
643 | @@ -150,6 +151,13 @@ | |||
644 | 150 | __attribute__ ((unused)); | 151 | __attribute__ ((unused)); |
645 | 151 | 152 | ||
646 | 152 | void | 153 | void |
647 | 154 | debug_show_job (const Job *job) | ||
648 | 155 | __attribute__ ((unused)); | ||
649 | 156 | |||
650 | 157 | void | ||
651 | 158 | debug_show_jobs (const NihHash *instances) | ||
652 | 159 | __attribute__ ((unused)); | ||
653 | 160 | void | ||
654 | 153 | debug_show_event (const Event *event) | 161 | debug_show_event (const Event *event) |
655 | 154 | __attribute__ ((unused)); | 162 | __attribute__ ((unused)); |
656 | 155 | 163 | ||
657 | 156 | 164 | ||
658 | === modified file 'init/control.c' | |||
659 | --- init/control.c 2012-03-16 21:02:13 +0000 | |||
660 | +++ init/control.c 2012-11-14 14:49:20 +0000 | |||
661 | @@ -54,6 +54,7 @@ | |||
662 | 54 | #include "conf.h" | 54 | #include "conf.h" |
663 | 55 | #include "control.h" | 55 | #include "control.h" |
664 | 56 | #include "errors.h" | 56 | #include "errors.h" |
665 | 57 | #include "state.h" | ||
666 | 57 | 58 | ||
667 | 58 | #include "com.ubuntu.Upstart.h" | 59 | #include "com.ubuntu.Upstart.h" |
668 | 59 | 60 | ||
669 | @@ -62,6 +63,8 @@ | |||
670 | 62 | static void control_disconnected (DBusConnection *conn); | 63 | static void control_disconnected (DBusConnection *conn); |
671 | 63 | static void control_register_all (DBusConnection *conn); | 64 | static void control_register_all (DBusConnection *conn); |
672 | 64 | 65 | ||
673 | 66 | static void control_bus_flush (void); | ||
674 | 67 | |||
675 | 65 | /** | 68 | /** |
676 | 66 | * use_session_bus: | 69 | * use_session_bus: |
677 | 67 | * | 70 | * |
678 | @@ -188,7 +191,8 @@ | |||
679 | 188 | void | 191 | void |
680 | 189 | control_server_close (void) | 192 | control_server_close (void) |
681 | 190 | { | 193 | { |
683 | 191 | nih_assert (control_server != NULL); | 194 | if (! control_server) |
684 | 195 | return; | ||
685 | 192 | 196 | ||
686 | 193 | dbus_server_disconnect (control_server); | 197 | dbus_server_disconnect (control_server); |
687 | 194 | dbus_server_unref (control_server); | 198 | dbus_server_unref (control_server); |
688 | @@ -292,7 +296,7 @@ | |||
689 | 292 | * | 296 | * |
690 | 293 | * This function is called when the connection to the D-Bus system bus, | 297 | * This function is called when the connection to the D-Bus system bus, |
691 | 294 | * or a client connection to our D-Bus server, is dropped and our reference | 298 | * or a client connection to our D-Bus server, is dropped and our reference |
693 | 295 | * is about to be list. We clear the connection from our current list | 299 | * is about to be lost. We clear the connection from our current list |
694 | 296 | * and drop the control_bus global if relevant. | 300 | * and drop the control_bus global if relevant. |
695 | 297 | **/ | 301 | **/ |
696 | 298 | static void | 302 | static void |
697 | @@ -301,6 +305,10 @@ | |||
698 | 301 | nih_assert (conn != NULL); | 305 | nih_assert (conn != NULL); |
699 | 302 | 306 | ||
700 | 303 | if (conn == control_bus) { | 307 | if (conn == control_bus) { |
701 | 308 | DBusError error; | ||
702 | 309 | |||
703 | 310 | dbus_error_init (&error); | ||
704 | 311 | |||
705 | 304 | nih_warn (_("Disconnected from system bus")); | 312 | nih_warn (_("Disconnected from system bus")); |
706 | 305 | 313 | ||
707 | 306 | control_bus = NULL; | 314 | control_bus = NULL; |
708 | @@ -368,6 +376,8 @@ | |||
709 | 368 | nih_assert (message != NULL); | 376 | nih_assert (message != NULL); |
710 | 369 | 377 | ||
711 | 370 | nih_info (_("Reloading configuration")); | 378 | nih_info (_("Reloading configuration")); |
712 | 379 | |||
713 | 380 | /* This can only be called after deserialisation */ | ||
714 | 371 | conf_reload (); | 381 | conf_reload (); |
715 | 372 | 382 | ||
716 | 373 | return 0; | 383 | return 0; |
717 | @@ -806,3 +816,136 @@ | |||
718 | 806 | 816 | ||
719 | 807 | return 0; | 817 | return 0; |
720 | 808 | } | 818 | } |
721 | 819 | |||
722 | 820 | /** | ||
723 | 821 | * control_bus_flush: | ||
724 | 822 | * | ||
725 | 823 | * Drain any remaining messages in the D-Bus queue. | ||
726 | 824 | **/ | ||
727 | 825 | static void | ||
728 | 826 | control_bus_flush (void) | ||
729 | 827 | { | ||
730 | 828 | control_init (); | ||
731 | 829 | |||
732 | 830 | if (! control_bus) | ||
733 | 831 | return; | ||
734 | 832 | |||
735 | 833 | while (dbus_connection_dispatch (control_bus) == DBUS_DISPATCH_DATA_REMAINS) | ||
736 | 834 | ; | ||
737 | 835 | } | ||
738 | 836 | |||
739 | 837 | /** | ||
740 | 838 | * control_prepare_reexec: | ||
741 | 839 | * | ||
742 | 840 | * Prepare for a re-exec by allowing the bus connection to be retained | ||
743 | 841 | * over re-exec and clearing all queued messages. | ||
744 | 842 | **/ | ||
745 | 843 | void | ||
746 | 844 | control_prepare_reexec (void) | ||
747 | 845 | { | ||
748 | 846 | control_init (); | ||
749 | 847 | |||
750 | 848 | /* Necessary to disallow further commands but also to allow the | ||
751 | 849 | * new instance to open the control server. | ||
752 | 850 | */ | ||
753 | 851 | if (control_server) | ||
754 | 852 | control_server_close (); | ||
755 | 853 | |||
756 | 854 | control_bus_flush (); | ||
757 | 855 | |||
758 | 856 | } | ||
759 | 857 | |||
760 | 858 | |||
761 | 859 | /** | ||
762 | 860 | * control_conn_to_index: | ||
763 | 861 | * | ||
764 | 862 | * @event: event. | ||
765 | 863 | * | ||
766 | 864 | * Convert a control (DBusConnection) connection to an index number | ||
767 | 865 | * the list of control connections. | ||
768 | 866 | * | ||
769 | 867 | * Returns: connection index, or -1 on error. | ||
770 | 868 | **/ | ||
771 | 869 | int | ||
772 | 870 | control_conn_to_index (const DBusConnection *connection) | ||
773 | 871 | { | ||
774 | 872 | int conn_index = 0; | ||
775 | 873 | int found = FALSE; | ||
776 | 874 | |||
777 | 875 | nih_assert (connection); | ||
778 | 876 | |||
779 | 877 | NIH_LIST_FOREACH (control_conns, iter) { | ||
780 | 878 | NihListEntry *entry = (NihListEntry *)iter; | ||
781 | 879 | DBusConnection *conn = (DBusConnection *)entry->data; | ||
782 | 880 | |||
783 | 881 | if (connection == conn) { | ||
784 | 882 | found = TRUE; | ||
785 | 883 | break; | ||
786 | 884 | } | ||
787 | 885 | |||
788 | 886 | conn_index++; | ||
789 | 887 | } | ||
790 | 888 | if (! found) | ||
791 | 889 | return -1; | ||
792 | 890 | |||
793 | 891 | return conn_index; | ||
794 | 892 | } | ||
795 | 893 | |||
796 | 894 | /** | ||
797 | 895 | * control_conn_from_index: | ||
798 | 896 | * | ||
799 | 897 | * @conn_index: control connection index number. | ||
800 | 898 | * | ||
801 | 899 | * Lookup control connection based on index number. | ||
802 | 900 | * | ||
803 | 901 | * Returns: existing connection on success, or NULL if connection | ||
804 | 902 | * not found. | ||
805 | 903 | **/ | ||
806 | 904 | DBusConnection * | ||
807 | 905 | control_conn_from_index (int conn_index) | ||
808 | 906 | { | ||
809 | 907 | int i = 0; | ||
810 | 908 | |||
811 | 909 | nih_assert (conn_index >= 0); | ||
812 | 910 | nih_assert (control_conns); | ||
813 | 911 | |||
814 | 912 | NIH_LIST_FOREACH (control_conns, iter) { | ||
815 | 913 | NihListEntry *entry = (NihListEntry *)iter; | ||
816 | 914 | DBusConnection *conn = (DBusConnection *)entry->data; | ||
817 | 915 | |||
818 | 916 | if (i == conn_index) | ||
819 | 917 | return conn; | ||
820 | 918 | i++; | ||
821 | 919 | } | ||
822 | 920 | |||
823 | 921 | return NULL; | ||
824 | 922 | } | ||
825 | 923 | |||
826 | 924 | /** | ||
827 | 925 | * control_bus_release_name: | ||
828 | 926 | * | ||
829 | 927 | * Unregister well-known D-Bus name. | ||
830 | 928 | * | ||
831 | 929 | * Returns: 0 on success, -1 on raised error. | ||
832 | 930 | **/ | ||
833 | 931 | int | ||
834 | 932 | control_bus_release_name (void) | ||
835 | 933 | { | ||
836 | 934 | DBusError error; | ||
837 | 935 | int ret; | ||
838 | 936 | |||
839 | 937 | if (! control_bus) | ||
840 | 938 | return 0; | ||
841 | 939 | |||
842 | 940 | dbus_error_init (&error); | ||
843 | 941 | ret = dbus_bus_release_name (control_bus, | ||
844 | 942 | DBUS_SERVICE_UPSTART, | ||
845 | 943 | &error); | ||
846 | 944 | if (ret < 0) { | ||
847 | 945 | nih_dbus_error_raise (error.name, error.message); | ||
848 | 946 | dbus_error_free (&error); | ||
849 | 947 | return -1; | ||
850 | 948 | } | ||
851 | 949 | |||
852 | 950 | return 0; | ||
853 | 951 | } | ||
854 | 809 | 952 | ||
855 | === modified file 'init/control.h' | |||
856 | --- init/control.h 2011-06-06 17:05:11 +0000 | |||
857 | +++ init/control.h 2012-11-14 14:49:20 +0000 | |||
858 | @@ -23,10 +23,13 @@ | |||
859 | 23 | #include <dbus/dbus.h> | 23 | #include <dbus/dbus.h> |
860 | 24 | 24 | ||
861 | 25 | #include <nih/macros.h> | 25 | #include <nih/macros.h> |
862 | 26 | #include <nih/list.h> | ||
863 | 26 | 27 | ||
864 | 27 | #include <nih-dbus/dbus_connection.h> | 28 | #include <nih-dbus/dbus_connection.h> |
865 | 28 | #include <nih-dbus/dbus_message.h> | 29 | #include <nih-dbus/dbus_message.h> |
866 | 29 | 30 | ||
867 | 31 | #include <json.h> | ||
868 | 32 | |||
869 | 30 | /** | 33 | /** |
870 | 31 | * USE_SESSION_BUS_ENV: | 34 | * USE_SESSION_BUS_ENV: |
871 | 32 | * | 35 | * |
872 | @@ -90,6 +93,18 @@ | |||
873 | 90 | 93 | ||
874 | 91 | void control_handle_bus_type (void); | 94 | void control_handle_bus_type (void); |
875 | 92 | 95 | ||
876 | 96 | void control_prepare_reexec (void); | ||
877 | 97 | |||
878 | 98 | int control_conn_to_index (const DBusConnection *connection) | ||
879 | 99 | __attribute__ ((warn_unused_result)); | ||
880 | 100 | |||
881 | 101 | DBusConnection * | ||
882 | 102 | control_conn_from_index (int conn_index) | ||
883 | 103 | __attribute__ ((warn_unused_result)); | ||
884 | 104 | |||
885 | 105 | int control_bus_release_name (void) | ||
886 | 106 | __attribute__ ((warn_unused_result)); | ||
887 | 107 | |||
888 | 93 | NIH_END_EXTERN | 108 | NIH_END_EXTERN |
889 | 94 | 109 | ||
890 | 95 | #endif /* INIT_CONTROL_H */ | 110 | #endif /* INIT_CONTROL_H */ |
891 | 96 | 111 | ||
892 | === modified file 'init/event.c' | |||
893 | --- init/event.c 2011-06-06 17:05:11 +0000 | |||
894 | +++ init/event.c 2012-11-14 14:49:20 +0000 | |||
895 | @@ -52,6 +52,14 @@ | |||
896 | 52 | static void event_pending_handle_jobs (Event *event); | 52 | static void event_pending_handle_jobs (Event *event); |
897 | 53 | static void event_finished (Event *event); | 53 | static void event_finished (Event *event); |
898 | 54 | 54 | ||
899 | 55 | static const char * event_progress_enum_to_str (EventProgress progress) | ||
900 | 56 | __attribute__ ((warn_unused_result)); | ||
901 | 57 | |||
902 | 58 | static EventProgress | ||
903 | 59 | event_progress_str_to_enum (const char *name) | ||
904 | 60 | __attribute__ ((warn_unused_result)); | ||
905 | 61 | |||
906 | 62 | extern json_object *json_events; | ||
907 | 55 | 63 | ||
908 | 56 | /** | 64 | /** |
909 | 57 | * events: | 65 | * events: |
910 | @@ -501,3 +509,326 @@ | |||
911 | 501 | 509 | ||
912 | 502 | nih_free (event); | 510 | nih_free (event); |
913 | 503 | } | 511 | } |
914 | 512 | |||
915 | 513 | /** | ||
916 | 514 | * event_serialise: | ||
917 | 515 | * @event: event to serialise. | ||
918 | 516 | * | ||
919 | 517 | * Convert @event into a JSON representation for serialisation. | ||
920 | 518 | * Caller must free returned value using json_object_put(). | ||
921 | 519 | * | ||
922 | 520 | * Note that event->blocking is NOT serialised. Instead, those objects | ||
923 | 521 | * which event->blocking refers to encode the fact that they are blocked | ||
924 | 522 | * on the event (index in the JSON) in question. Deserialisation | ||
925 | 523 | * restores event->blocking via a 2-pass technique. event->blockers is | ||
926 | 524 | * recorded to allow a double-check. | ||
927 | 525 | * | ||
928 | 526 | * Returns: JSON-serialised Event object, or NULL on error. | ||
929 | 527 | **/ | ||
930 | 528 | json_object * | ||
931 | 529 | event_serialise (const Event *event) | ||
932 | 530 | { | ||
933 | 531 | json_object *json; | ||
934 | 532 | int session_index; | ||
935 | 533 | |||
936 | 534 | nih_assert (event); | ||
937 | 535 | nih_assert (event->name); | ||
938 | 536 | |||
939 | 537 | json = json_object_new_object (); | ||
940 | 538 | if (! json) | ||
941 | 539 | return NULL; | ||
942 | 540 | |||
943 | 541 | session_index = session_get_index (event->session); | ||
944 | 542 | if (session_index < 0) | ||
945 | 543 | goto error; | ||
946 | 544 | |||
947 | 545 | if (! state_set_json_int_var (json, "session", session_index)) | ||
948 | 546 | goto error; | ||
949 | 547 | |||
950 | 548 | if (! state_set_json_string_var_from_obj (json, event, name)) | ||
951 | 549 | goto error; | ||
952 | 550 | |||
953 | 551 | if (event->env) { | ||
954 | 552 | if (! state_set_json_str_array_from_obj (json, event, env)) | ||
955 | 553 | goto error; | ||
956 | 554 | } | ||
957 | 555 | |||
958 | 556 | if (! state_set_json_int_var_from_obj (json, event, fd)) | ||
959 | 557 | goto error; | ||
960 | 558 | |||
961 | 559 | if (! state_set_json_enum_var (json, | ||
962 | 560 | event_progress_enum_to_str, | ||
963 | 561 | "progress", event->progress)) | ||
964 | 562 | goto error; | ||
965 | 563 | |||
966 | 564 | if (! state_set_json_int_var_from_obj (json, event, failed)) | ||
967 | 565 | goto error; | ||
968 | 566 | |||
969 | 567 | if (! state_set_json_int_var_from_obj (json, event, blockers)) | ||
970 | 568 | goto error; | ||
971 | 569 | |||
972 | 570 | if (! NIH_LIST_EMPTY (&event->blocking)) { | ||
973 | 571 | json_object *json_blocking; | ||
974 | 572 | |||
975 | 573 | json_blocking = state_serialise_blocking (&event->blocking); | ||
976 | 574 | if (! json_blocking) | ||
977 | 575 | goto error; | ||
978 | 576 | |||
979 | 577 | json_object_object_add (json, "blocking", json_blocking); | ||
980 | 578 | } | ||
981 | 579 | |||
982 | 580 | return json; | ||
983 | 581 | |||
984 | 582 | error: | ||
985 | 583 | json_object_put (json); | ||
986 | 584 | return NULL; | ||
987 | 585 | } | ||
988 | 586 | |||
989 | 587 | /** | ||
990 | 588 | * event_serialise_all: | ||
991 | 589 | * | ||
992 | 590 | * Convert existing Event objects to JSON representation. | ||
993 | 591 | * | ||
994 | 592 | * Returns: JSON object containing array of Events, or NULL on error. | ||
995 | 593 | **/ | ||
996 | 594 | json_object * | ||
997 | 595 | event_serialise_all (void) | ||
998 | 596 | { | ||
999 | 597 | json_object *json; | ||
1000 | 598 | |||
1001 | 599 | event_init (); | ||
1002 | 600 | |||
1003 | 601 | json = json_object_new_array (); | ||
1004 | 602 | if (! json) | ||
1005 | 603 | return NULL; | ||
1006 | 604 | |||
1007 | 605 | NIH_LIST_FOREACH (events, iter) { | ||
1008 | 606 | Event *event = (Event *)iter; | ||
1009 | 607 | json_object *json_event; | ||
1010 | 608 | |||
1011 | 609 | json_event = event_serialise (event); | ||
1012 | 610 | |||
1013 | 611 | if (! json_event) | ||
1014 | 612 | goto error; | ||
1015 | 613 | |||
1016 | 614 | json_object_array_add (json, json_event); | ||
1017 | 615 | } | ||
1018 | 616 | |||
1019 | 617 | return json; | ||
1020 | 618 | |||
1021 | 619 | error: | ||
1022 | 620 | json_object_put (json); | ||
1023 | 621 | return NULL; | ||
1024 | 622 | } | ||
1025 | 623 | |||
1026 | 624 | /** | ||
1027 | 625 | * event_deserialise: | ||
1028 | 626 | * @json: JSON-serialised Event object to deserialise. | ||
1029 | 627 | * | ||
1030 | 628 | * Convert @json into an Event object. | ||
1031 | 629 | * | ||
1032 | 630 | * Returns: Event object, or NULL on error. | ||
1033 | 631 | **/ | ||
1034 | 632 | Event * | ||
1035 | 633 | event_deserialise (json_object *json) | ||
1036 | 634 | { | ||
1037 | 635 | json_object *json_env; | ||
1038 | 636 | Event *event = NULL; | ||
1039 | 637 | nih_local char *name = NULL; | ||
1040 | 638 | char **env = NULL; | ||
1041 | 639 | int session_index = -1; | ||
1042 | 640 | |||
1043 | 641 | nih_assert (json); | ||
1044 | 642 | |||
1045 | 643 | if (! state_check_json_type (json, object)) | ||
1046 | 644 | return NULL; | ||
1047 | 645 | |||
1048 | 646 | if (! state_get_json_string_var_strict (json, "name", NULL, name)) | ||
1049 | 647 | goto error; | ||
1050 | 648 | |||
1051 | 649 | if (json_object_object_get (json, "env")) { | ||
1052 | 650 | if (! state_get_json_var_full (json, "env", array, json_env)) | ||
1053 | 651 | goto error; | ||
1054 | 652 | |||
1055 | 653 | if (! state_deserialise_str_array (NULL, json_env, &env)) | ||
1056 | 654 | goto error; | ||
1057 | 655 | } | ||
1058 | 656 | |||
1059 | 657 | event = event_new (NULL, name, env); | ||
1060 | 658 | if (! event) | ||
1061 | 659 | return NULL; | ||
1062 | 660 | |||
1063 | 661 | if (! state_get_json_int_var_to_obj (json, event, fd)) | ||
1064 | 662 | goto error; | ||
1065 | 663 | |||
1066 | 664 | if (! state_get_json_int_var (json, "session", session_index)) | ||
1067 | 665 | goto error; | ||
1068 | 666 | |||
1069 | 667 | /* can't check return value here (as all values are legitimate) */ | ||
1070 | 668 | event->session = session_from_index (session_index); | ||
1071 | 669 | |||
1072 | 670 | if (! state_get_json_enum_var (json, | ||
1073 | 671 | event_progress_str_to_enum, | ||
1074 | 672 | "progress", event->progress)) | ||
1075 | 673 | goto error; | ||
1076 | 674 | |||
1077 | 675 | if (! state_set_json_int_var_from_obj (json, event, failed)) | ||
1078 | 676 | goto error; | ||
1079 | 677 | |||
1080 | 678 | return event; | ||
1081 | 679 | |||
1082 | 680 | error: | ||
1083 | 681 | nih_free (event); | ||
1084 | 682 | return NULL; | ||
1085 | 683 | } | ||
1086 | 684 | |||
1087 | 685 | /** | ||
1088 | 686 | * event_deserialise_all: | ||
1089 | 687 | * | ||
1090 | 688 | * @json: root of JSON-serialised state. | ||
1091 | 689 | * | ||
1092 | 690 | * Convert JSON representation of events back into Event objects. | ||
1093 | 691 | * | ||
1094 | 692 | * Returns: 0 on success, -1 on error. | ||
1095 | 693 | **/ | ||
1096 | 694 | int | ||
1097 | 695 | event_deserialise_all (json_object *json) | ||
1098 | 696 | { | ||
1099 | 697 | Event *event; | ||
1100 | 698 | |||
1101 | 699 | nih_assert (json); | ||
1102 | 700 | |||
1103 | 701 | event_init (); | ||
1104 | 702 | |||
1105 | 703 | nih_assert (NIH_LIST_EMPTY (events)); | ||
1106 | 704 | json_events = json_object_object_get (json, "events"); | ||
1107 | 705 | |||
1108 | 706 | if (! json_events) | ||
1109 | 707 | goto error; | ||
1110 | 708 | |||
1111 | 709 | if (! state_check_json_type (json_events, array)) | ||
1112 | 710 | goto error; | ||
1113 | 711 | |||
1114 | 712 | for (int i = 0; i < json_object_array_length (json_events); i++) { | ||
1115 | 713 | json_object *json_event; | ||
1116 | 714 | |||
1117 | 715 | json_event = json_object_array_get_idx (json_events, i); | ||
1118 | 716 | if (! json_event) | ||
1119 | 717 | goto error; | ||
1120 | 718 | |||
1121 | 719 | if (! state_check_json_type (json_event, object)) | ||
1122 | 720 | goto error; | ||
1123 | 721 | |||
1124 | 722 | event = event_deserialise (json_event); | ||
1125 | 723 | if (! event) | ||
1126 | 724 | goto error; | ||
1127 | 725 | } | ||
1128 | 726 | |||
1129 | 727 | return 0; | ||
1130 | 728 | |||
1131 | 729 | error: | ||
1132 | 730 | return -1; | ||
1133 | 731 | } | ||
1134 | 732 | |||
1135 | 733 | /** | ||
1136 | 734 | * event_progress_enum_to_str: | ||
1137 | 735 | * | ||
1138 | 736 | * @progress: event progress. | ||
1139 | 737 | * | ||
1140 | 738 | * Convert EventProgress to a string representation. | ||
1141 | 739 | * | ||
1142 | 740 | * Returns: string representation of @progress, or NULL if not known. | ||
1143 | 741 | **/ | ||
1144 | 742 | static const char * | ||
1145 | 743 | event_progress_enum_to_str (EventProgress progress) | ||
1146 | 744 | { | ||
1147 | 745 | state_enum_to_str (EVENT_PENDING, progress); | ||
1148 | 746 | state_enum_to_str (EVENT_HANDLING, progress); | ||
1149 | 747 | state_enum_to_str (EVENT_FINISHED, progress); | ||
1150 | 748 | |||
1151 | 749 | return NULL; | ||
1152 | 750 | } | ||
1153 | 751 | |||
1154 | 752 | /** | ||
1155 | 753 | * event_progress_str_to_enum: | ||
1156 | 754 | * | ||
1157 | 755 | * @: name of EventOperator value. | ||
1158 | 756 | * | ||
1159 | 757 | * Convert string representation of EventProgress into a | ||
1160 | 758 | * real EventProgress value. | ||
1161 | 759 | * | ||
1162 | 760 | * Returns: EventProgress representing @progress, or -1 if not known. | ||
1163 | 761 | **/ | ||
1164 | 762 | static EventProgress | ||
1165 | 763 | event_progress_str_to_enum (const char *progress) | ||
1166 | 764 | { | ||
1167 | 765 | state_str_to_enum (EVENT_PENDING, progress); | ||
1168 | 766 | state_str_to_enum (EVENT_HANDLING, progress); | ||
1169 | 767 | state_str_to_enum (EVENT_FINISHED, progress); | ||
1170 | 768 | |||
1171 | 769 | return -1; | ||
1172 | 770 | } | ||
1173 | 771 | |||
1174 | 772 | /** | ||
1175 | 773 | * event_to_index: | ||
1176 | 774 | * | ||
1177 | 775 | * @event: event. | ||
1178 | 776 | * | ||
1179 | 777 | * Convert an Event to an index number within | ||
1180 | 778 | * the list of events. | ||
1181 | 779 | * | ||
1182 | 780 | * Returns: event index, or -1 on error. | ||
1183 | 781 | **/ | ||
1184 | 782 | int | ||
1185 | 783 | event_to_index (const Event *event) | ||
1186 | 784 | { | ||
1187 | 785 | int event_index = 0; | ||
1188 | 786 | int found = FALSE; | ||
1189 | 787 | |||
1190 | 788 | nih_assert (event); | ||
1191 | 789 | event_init (); | ||
1192 | 790 | |||
1193 | 791 | NIH_LIST_FOREACH (events, iter) { | ||
1194 | 792 | Event *tmp = (Event *)iter; | ||
1195 | 793 | |||
1196 | 794 | if (tmp == event) { | ||
1197 | 795 | found = TRUE; | ||
1198 | 796 | break; | ||
1199 | 797 | } | ||
1200 | 798 | |||
1201 | 799 | event_index++; | ||
1202 | 800 | } | ||
1203 | 801 | |||
1204 | 802 | if (! found) | ||
1205 | 803 | return -1; | ||
1206 | 804 | |||
1207 | 805 | return event_index; | ||
1208 | 806 | } | ||
1209 | 807 | |||
1210 | 808 | /** | ||
1211 | 809 | * event_from_index: | ||
1212 | 810 | * | ||
1213 | 811 | * @event_index: event index number. | ||
1214 | 812 | * | ||
1215 | 813 | * Lookup Event based on index number. | ||
1216 | 814 | * | ||
1217 | 815 | * Returns: existing Event on success, or NULL if event not found. | ||
1218 | 816 | **/ | ||
1219 | 817 | Event * | ||
1220 | 818 | event_from_index (int event_index) | ||
1221 | 819 | { | ||
1222 | 820 | int i = 0; | ||
1223 | 821 | |||
1224 | 822 | nih_assert (event_index >= 0); | ||
1225 | 823 | event_init (); | ||
1226 | 824 | |||
1227 | 825 | NIH_LIST_FOREACH (events, iter) { | ||
1228 | 826 | Event *event = (Event *)iter; | ||
1229 | 827 | |||
1230 | 828 | if (i == event_index) | ||
1231 | 829 | return event; | ||
1232 | 830 | i++; | ||
1233 | 831 | } | ||
1234 | 832 | |||
1235 | 833 | return NULL; | ||
1236 | 834 | } | ||
1237 | 504 | 835 | ||
1238 | === modified file 'init/event.h' | |||
1239 | --- init/event.h 2011-06-06 17:05:11 +0000 | |||
1240 | +++ init/event.h 2012-11-14 14:49:20 +0000 | |||
1241 | @@ -24,7 +24,9 @@ | |||
1242 | 24 | #include <nih/list.h> | 24 | #include <nih/list.h> |
1243 | 25 | 25 | ||
1244 | 26 | #include "session.h" | 26 | #include "session.h" |
1245 | 27 | #include "state.h" | ||
1246 | 27 | 28 | ||
1247 | 29 | #include <json.h> | ||
1248 | 28 | 30 | ||
1249 | 29 | /** | 31 | /** |
1250 | 30 | * EventProgress: | 32 | * EventProgress: |
1251 | @@ -45,6 +47,8 @@ | |||
1252 | 45 | * @session: session the event is attached to, | 47 | * @session: session the event is attached to, |
1253 | 46 | * @name: string name of the event, | 48 | * @name: string name of the event, |
1254 | 47 | * @env: NULL-terminated array of environment variables, | 49 | * @env: NULL-terminated array of environment variables, |
1255 | 50 | * @fd: open file descriptor associated with a particular | ||
1256 | 51 | * socket-bridge socket (see socket-event(8)), | ||
1257 | 48 | * @progress: progress of event, | 52 | * @progress: progress of event, |
1258 | 49 | * @failed: whether this event has failed, | 53 | * @failed: whether this event has failed, |
1259 | 50 | * @blockers: number of blockers for finishing, | 54 | * @blockers: number of blockers for finishing, |
1260 | @@ -93,6 +97,24 @@ | |||
1261 | 93 | 97 | ||
1262 | 94 | void event_poll (void); | 98 | void event_poll (void); |
1263 | 95 | 99 | ||
1264 | 100 | json_object *event_serialise (const Event *event) | ||
1265 | 101 | __attribute__ ((malloc, warn_unused_result)); | ||
1266 | 102 | |||
1267 | 103 | Event *event_deserialise (json_object *json) | ||
1268 | 104 | __attribute__ ((malloc, warn_unused_result)); | ||
1269 | 105 | |||
1270 | 106 | json_object * event_serialise_all (void) | ||
1271 | 107 | __attribute__ ((malloc, warn_unused_result)); | ||
1272 | 108 | |||
1273 | 109 | int event_deserialise_all (json_object *json) | ||
1274 | 110 | __attribute__ ((warn_unused_result)); | ||
1275 | 111 | |||
1276 | 112 | int event_to_index (const Event *event) | ||
1277 | 113 | __attribute__ ((warn_unused_result)); | ||
1278 | 114 | |||
1279 | 115 | Event * event_from_index (int event_index) | ||
1280 | 116 | __attribute__ ((warn_unused_result)); | ||
1281 | 117 | |||
1282 | 96 | NIH_END_EXTERN | 118 | NIH_END_EXTERN |
1283 | 97 | 119 | ||
1284 | 98 | #endif /* INIT_EVENT_H */ | 120 | #endif /* INIT_EVENT_H */ |
1285 | 99 | 121 | ||
1286 | === modified file 'init/event_operator.c' | |||
1287 | --- init/event_operator.c 2010-12-10 07:18:34 +0000 | |||
1288 | +++ init/event_operator.c 2012-11-14 14:49:20 +0000 | |||
1289 | @@ -552,14 +552,31 @@ | |||
1290 | 552 | return *env; | 552 | return *env; |
1291 | 553 | } | 553 | } |
1292 | 554 | 554 | ||
1293 | 555 | /** | ||
1294 | 556 | * event_operator_fds: | ||
1295 | 557 | * @root: operator tree to update, | ||
1296 | 558 | * @parent: parent object for new array, | ||
1297 | 559 | * @fds: output location for array of ints | ||
1298 | 560 | * @num_fds: number of elements in @fds, | ||
1299 | 561 | * @env: NULL-terminated array of environment variables to add to, | ||
1300 | 562 | * @len: length of @env, | ||
1301 | 563 | * @key: key of variable to contain event names. | ||
1302 | 564 | * | ||
1303 | 565 | * Iterate over tree rooted at @root adding all file descriptor values found | ||
1304 | 566 | * to the dynamically allocated @fds array. In addition, all file | ||
1305 | 567 | * descriptors found are also added to @env will contain a new entry with key @key | ||
1306 | 568 | * whose value is a space-separated list of file descriptor numbers. | ||
1307 | 569 | * | ||
1308 | 570 | * Returns: 1 on success, NULL on failure. | ||
1309 | 571 | **/ | ||
1310 | 555 | int * | 572 | int * |
1318 | 556 | event_operator_fds (EventOperator *root, | 573 | event_operator_fds (EventOperator *root, |
1319 | 557 | const void *parent, | 574 | const void *parent, |
1320 | 558 | int **fds, | 575 | int **fds, |
1321 | 559 | size_t *num_fds, | 576 | size_t *num_fds, |
1322 | 560 | char ***env, | 577 | char ***env, |
1323 | 561 | size_t *len, | 578 | size_t *len, |
1324 | 562 | const char *key) | 579 | const char *key) |
1325 | 563 | { | 580 | { |
1326 | 564 | nih_local char *evlist = NULL; | 581 | nih_local char *evlist = NULL; |
1327 | 565 | 582 | ||
1328 | @@ -696,3 +713,154 @@ | |||
1329 | 696 | } | 713 | } |
1330 | 697 | } | 714 | } |
1331 | 698 | } | 715 | } |
1332 | 716 | |||
1333 | 717 | /** | ||
1334 | 718 | * event_operator_collapse: | ||
1335 | 719 | * | ||
1336 | 720 | * @condition: start on/stop on condition. | ||
1337 | 721 | * | ||
1338 | 722 | * Collapsed condition will be fully bracketed. Note that as such it may | ||
1339 | 723 | * not be lexicographically identical to the original expression that | ||
1340 | 724 | * resulted in @condition, but it will be logically identical. | ||
1341 | 725 | * | ||
1342 | 726 | * The condition is reconstructed from the EventOperator tree by using | ||
1343 | 727 | * a post-order traversal (since this allows the tree to be traversed | ||
1344 | 728 | * bottom-to-top). Leaf nodes (EVENT_MATCH) are ignored when visited, | ||
1345 | 729 | * allowing non-leaf nodes (EVENT_AND and EVENT_OR) to simply grab the | ||
1346 | 730 | * value of their children, construct a bracketed expression and add it | ||
1347 | 731 | * to a stack. If a child is a leaf node, the value can be read | ||
1348 | 732 | * directly. If a child is a non-leaf node, the value is obtained by | ||
1349 | 733 | * popping the stack before adding the new value back onto the stack. | ||
1350 | 734 | * When finally the root node is visited, the final expression can be | ||
1351 | 735 | * removed from the stack and returned. A single-node tree (comprising a | ||
1352 | 736 | * lone EVENT_MATCH at the root) is special-cased. | ||
1353 | 737 | * | ||
1354 | 738 | * Returns: newly-allocated flattened string representing @condition | ||
1355 | 739 | * on success, or NULL on error. | ||
1356 | 740 | **/ | ||
1357 | 741 | char * | ||
1358 | 742 | event_operator_collapse (EventOperator *condition) | ||
1359 | 743 | { | ||
1360 | 744 | nih_local NihList *stack = NULL; | ||
1361 | 745 | NihListEntry *latest = NULL; | ||
1362 | 746 | NihTree *root; | ||
1363 | 747 | |||
1364 | 748 | nih_assert (condition); | ||
1365 | 749 | |||
1366 | 750 | root = &condition->node; | ||
1367 | 751 | |||
1368 | 752 | stack = NIH_MUST (nih_list_new (NULL)); | ||
1369 | 753 | |||
1370 | 754 | NIH_TREE_FOREACH_POST (root, iter) { | ||
1371 | 755 | EventOperator *oper = (EventOperator *)iter; | ||
1372 | 756 | EventOperator *left; | ||
1373 | 757 | EventOperator *right; | ||
1374 | 758 | NihListEntry *expr; | ||
1375 | 759 | NihTree *tree; | ||
1376 | 760 | nih_local char *left_expr = NULL; | ||
1377 | 761 | nih_local char *right_expr = NULL; | ||
1378 | 762 | |||
1379 | 763 | tree = &oper->node; | ||
1380 | 764 | |||
1381 | 765 | left = (EventOperator *)tree->left; | ||
1382 | 766 | right = (EventOperator *)tree->right; | ||
1383 | 767 | |||
1384 | 768 | if (oper->type == EVENT_MATCH) { | ||
1385 | 769 | /* Entire expression comprises a single event, | ||
1386 | 770 | * so push it and leave. | ||
1387 | 771 | */ | ||
1388 | 772 | if (tree == root) { | ||
1389 | 773 | nih_local char *env = NULL; | ||
1390 | 774 | |||
1391 | 775 | if (oper->env) | ||
1392 | 776 | env = NIH_MUST (state_collapse_env ((const char **)oper->env)); | ||
1393 | 777 | |||
1394 | 778 | expr = NIH_MUST (nih_list_entry_new (stack)); | ||
1395 | 779 | expr->str = NIH_MUST (nih_sprintf (expr, "%s%s%s", | ||
1396 | 780 | oper->name, | ||
1397 | 781 | env ? " " : "", | ||
1398 | 782 | env ? env : "")); | ||
1399 | 783 | nih_list_add_after (stack, &expr->entry); | ||
1400 | 784 | break; | ||
1401 | 785 | } | ||
1402 | 786 | else { | ||
1403 | 787 | /* We build the expression from visiting the logical | ||
1404 | 788 | * operators (and their children) only. | ||
1405 | 789 | */ | ||
1406 | 790 | continue; | ||
1407 | 791 | } | ||
1408 | 792 | } | ||
1409 | 793 | |||
1410 | 794 | /* oper cannot now be a leaf node, so must have children */ | ||
1411 | 795 | nih_assert (left); | ||
1412 | 796 | nih_assert (right); | ||
1413 | 797 | |||
1414 | 798 | expr = NIH_MUST (nih_list_entry_new (stack)); | ||
1415 | 799 | |||
1416 | 800 | /* If a child is an EVENT_MATCH, expand its event | ||
1417 | 801 | * details and push onto the stack. | ||
1418 | 802 | * If a child is not an EVENT_MATCH, to obtains it | ||
1419 | 803 | * value, pop the stack. | ||
1420 | 804 | * | ||
1421 | 805 | * Having obtained the child values, construct a new | ||
1422 | 806 | * bracketed expression and push onto the stack. | ||
1423 | 807 | * | ||
1424 | 808 | * Note that we must consider the right child first. | ||
1425 | 809 | * This is because since the tree is traversed | ||
1426 | 810 | * left-child first, any value pushed onto the stack by | ||
1427 | 811 | * a right child is at the top so must be removed before | ||
1428 | 812 | * any left child value. Failure to do this results in | ||
1429 | 813 | * tree reflection which although logically equivalent | ||
1430 | 814 | * to the original could confuse as the resultant | ||
1431 | 815 | * expression will look rather different. | ||
1432 | 816 | */ | ||
1433 | 817 | if (right->type != EVENT_MATCH) { | ||
1434 | 818 | nih_assert (! NIH_LIST_EMPTY (stack)); | ||
1435 | 819 | |||
1436 | 820 | latest = (NihListEntry *)nih_list_remove (stack->next); | ||
1437 | 821 | right_expr = NIH_MUST (nih_strdup (NULL, latest->str)); | ||
1438 | 822 | } else { | ||
1439 | 823 | nih_local char *env = NULL; | ||
1440 | 824 | |||
1441 | 825 | if (right->env) | ||
1442 | 826 | env = NIH_MUST (state_collapse_env ((const char **)right->env)); | ||
1443 | 827 | |||
1444 | 828 | right_expr = NIH_MUST (nih_sprintf (NULL, "%s%s%s", | ||
1445 | 829 | right->name, | ||
1446 | 830 | env ? " " : "", | ||
1447 | 831 | env ? env : "")); | ||
1448 | 832 | } | ||
1449 | 833 | |||
1450 | 834 | if (left->type != EVENT_MATCH) { | ||
1451 | 835 | nih_assert (! NIH_LIST_EMPTY (stack)); | ||
1452 | 836 | |||
1453 | 837 | latest = (NihListEntry *)nih_list_remove (stack->next); | ||
1454 | 838 | left_expr = NIH_MUST (nih_strdup (NULL, latest->str)); | ||
1455 | 839 | } else { | ||
1456 | 840 | nih_local char *env = NULL; | ||
1457 | 841 | |||
1458 | 842 | if (left->env) | ||
1459 | 843 | env = NIH_MUST (state_collapse_env ((const char **)left->env)); | ||
1460 | 844 | |||
1461 | 845 | left_expr = NIH_MUST (nih_sprintf (NULL, "%s%s%s", | ||
1462 | 846 | left->name, | ||
1463 | 847 | env ? " " : "", | ||
1464 | 848 | env ? env : "")); | ||
1465 | 849 | } | ||
1466 | 850 | |||
1467 | 851 | expr->str = NIH_MUST (nih_sprintf (expr, "(%s %s %s)", | ||
1468 | 852 | left_expr, | ||
1469 | 853 | oper->type == EVENT_OR ? "or" : "and", | ||
1470 | 854 | right_expr)); | ||
1471 | 855 | |||
1472 | 856 | nih_list_add_after (stack, &expr->entry); | ||
1473 | 857 | } | ||
1474 | 858 | |||
1475 | 859 | nih_assert (! NIH_LIST_EMPTY (stack)); | ||
1476 | 860 | |||
1477 | 861 | latest = (NihListEntry *)nih_list_remove (stack->next); | ||
1478 | 862 | |||
1479 | 863 | nih_assert (NIH_LIST_EMPTY (stack)); | ||
1480 | 864 | |||
1481 | 865 | return NIH_MUST (nih_strdup (NULL, latest->str)); | ||
1482 | 866 | } | ||
1483 | 699 | 867 | ||
1484 | === modified file 'init/event_operator.h' | |||
1485 | --- init/event_operator.h 2010-12-10 07:18:34 +0000 | |||
1486 | +++ init/event_operator.h 2012-11-14 14:49:20 +0000 | |||
1487 | @@ -96,10 +96,10 @@ | |||
1488 | 96 | const void *parent, size_t *len, | 96 | const void *parent, size_t *len, |
1489 | 97 | const char *key); | 97 | const char *key); |
1490 | 98 | int * | 98 | int * |
1495 | 99 | event_operator_fds (EventOperator *root, | 99 | event_operator_fds (EventOperator *root, |
1496 | 100 | const void *parent, | 100 | const void *parent, |
1497 | 101 | int **fds, | 101 | int **fds, |
1498 | 102 | size_t *num_fds, | 102 | size_t *num_fds, |
1499 | 103 | char ***env, | 103 | char ***env, |
1500 | 104 | size_t *len, | 104 | size_t *len, |
1501 | 105 | const char *key); | 105 | const char *key); |
1502 | @@ -108,6 +108,9 @@ | |||
1503 | 108 | 108 | ||
1504 | 109 | void event_operator_reset (EventOperator *root); | 109 | void event_operator_reset (EventOperator *root); |
1505 | 110 | 110 | ||
1506 | 111 | char *event_operator_collapse (EventOperator *condition) | ||
1507 | 112 | __attribute__ ((malloc, warn_unused_result)); | ||
1508 | 113 | |||
1509 | 111 | NIH_END_EXTERN | 114 | NIH_END_EXTERN |
1510 | 112 | 115 | ||
1511 | 113 | #endif /* INIT_EVENT_OPERATOR_H */ | 116 | #endif /* INIT_EVENT_OPERATOR_H */ |
1512 | 114 | 117 | ||
1513 | === modified file 'init/job.c' | |||
1514 | --- init/job.c 2012-03-07 12:13:28 +0000 | |||
1515 | +++ init/job.c 2012-11-14 14:49:20 +0000 | |||
1516 | @@ -23,7 +23,6 @@ | |||
1517 | 23 | # include <config.h> | 23 | # include <config.h> |
1518 | 24 | #endif /* HAVE_CONFIG_H */ | 24 | #endif /* HAVE_CONFIG_H */ |
1519 | 25 | 25 | ||
1520 | 26 | |||
1521 | 27 | #include <sys/types.h> | 26 | #include <sys/types.h> |
1522 | 28 | 27 | ||
1523 | 29 | #include <errno.h> | 28 | #include <errno.h> |
1524 | @@ -55,10 +54,43 @@ | |||
1525 | 55 | #include "event_operator.h" | 54 | #include "event_operator.h" |
1526 | 56 | #include "blocked.h" | 55 | #include "blocked.h" |
1527 | 57 | #include "control.h" | 56 | #include "control.h" |
1528 | 57 | #include "parse_job.h" | ||
1529 | 58 | #include "state.h" | ||
1530 | 58 | 59 | ||
1531 | 59 | #include "com.ubuntu.Upstart.Job.h" | 60 | #include "com.ubuntu.Upstart.Job.h" |
1532 | 60 | #include "com.ubuntu.Upstart.Instance.h" | 61 | #include "com.ubuntu.Upstart.Instance.h" |
1533 | 61 | 62 | ||
1534 | 63 | /* Prototypes for static functions */ | ||
1535 | 64 | static const char * | ||
1536 | 65 | job_goal_enum_to_str (JobGoal goal) | ||
1537 | 66 | __attribute__ ((warn_unused_result)); | ||
1538 | 67 | |||
1539 | 68 | static JobGoal job_goal_str_to_enum (const char *goal) | ||
1540 | 69 | __attribute__ ((warn_unused_result)); | ||
1541 | 70 | |||
1542 | 71 | static const char * | ||
1543 | 72 | job_state_enum_to_str (JobState state) | ||
1544 | 73 | __attribute__ ((warn_unused_result)); | ||
1545 | 74 | |||
1546 | 75 | static JobState | ||
1547 | 76 | job_state_str_to_enum (const char *state) | ||
1548 | 77 | __attribute__ ((warn_unused_result)); | ||
1549 | 78 | |||
1550 | 79 | static const char * | ||
1551 | 80 | job_trace_state_enum_to_str (TraceState state) | ||
1552 | 81 | __attribute__ ((warn_unused_result)); | ||
1553 | 82 | |||
1554 | 83 | static TraceState | ||
1555 | 84 | job_trace_state_str_to_enum (const char *state) | ||
1556 | 85 | __attribute__ ((warn_unused_result)); | ||
1557 | 86 | |||
1558 | 87 | static json_object * | ||
1559 | 88 | job_serialise_kill_timer (NihTimer *timer) | ||
1560 | 89 | __attribute__ ((malloc, warn_unused_result)); | ||
1561 | 90 | |||
1562 | 91 | static NihTimer * | ||
1563 | 92 | job_deserialise_kill_timer (json_object *json) | ||
1564 | 93 | __attribute__ ((malloc, warn_unused_result)); | ||
1565 | 62 | 94 | ||
1566 | 63 | /** | 95 | /** |
1567 | 64 | * job_new: | 96 | * job_new: |
1568 | @@ -119,6 +151,7 @@ | |||
1569 | 119 | job->stop_env = NULL; | 151 | job->stop_env = NULL; |
1570 | 120 | 152 | ||
1571 | 121 | job->stop_on = NULL; | 153 | job->stop_on = NULL; |
1572 | 154 | |||
1573 | 122 | if (class->stop_on) { | 155 | if (class->stop_on) { |
1574 | 123 | job->stop_on = event_operator_copy (job, class->stop_on); | 156 | job->stop_on = event_operator_copy (job, class->stop_on); |
1575 | 124 | if (! job->stop_on) | 157 | if (! job->stop_on) |
1576 | @@ -152,10 +185,10 @@ | |||
1577 | 152 | nih_list_init (&job->blocking); | 185 | nih_list_init (&job->blocking); |
1578 | 153 | 186 | ||
1579 | 154 | job->kill_timer = NULL; | 187 | job->kill_timer = NULL; |
1581 | 155 | job->kill_process = -1; | 188 | job->kill_process = PROCESS_INVALID; |
1582 | 156 | 189 | ||
1583 | 157 | job->failed = FALSE; | 190 | job->failed = FALSE; |
1585 | 158 | job->failed_process = -1; | 191 | job->failed_process = PROCESS_INVALID; |
1586 | 159 | job->exit_status = 0; | 192 | job->exit_status = 0; |
1587 | 160 | 193 | ||
1588 | 161 | job->respawn_time = 0; | 194 | job->respawn_time = 0; |
1589 | @@ -262,7 +295,7 @@ | |||
1590 | 262 | * will finish naturally, so all we need do is change the goal and | 295 | * will finish naturally, so all we need do is change the goal and |
1591 | 263 | * we'll change direction through the state machine at that point. | 296 | * we'll change direction through the state machine at that point. |
1592 | 264 | * | 297 | * |
1594 | 265 | * The exceptions are the natural rest sates of waiting and a | 298 | * The exceptions are the natural rest states of waiting and a |
1595 | 266 | * running process; these need induction to get them moving. | 299 | * running process; these need induction to get them moving. |
1596 | 267 | */ | 300 | */ |
1597 | 268 | switch (goal) { | 301 | switch (goal) { |
1598 | @@ -358,7 +391,7 @@ | |||
1599 | 358 | 391 | ||
1600 | 359 | /* Clear any old failed information */ | 392 | /* Clear any old failed information */ |
1601 | 360 | job->failed = FALSE; | 393 | job->failed = FALSE; |
1603 | 361 | job->failed_process = -1; | 394 | job->failed_process = PROCESS_INVALID; |
1604 | 362 | job->exit_status = 0; | 395 | job->exit_status = 0; |
1605 | 363 | 396 | ||
1606 | 364 | job->blocker = job_emit_event (job); | 397 | job->blocker = job_emit_event (job); |
1607 | @@ -882,7 +915,7 @@ | |||
1608 | 882 | * if it was a respawn failure, we use the special "respawn" | 915 | * if it was a respawn failure, we use the special "respawn" |
1609 | 883 | * argument instead of the process name, | 916 | * argument instead of the process name, |
1610 | 884 | */ | 917 | */ |
1612 | 885 | if ((job->failed_process != (ProcessType)-1) | 918 | if ((job->failed_process != PROCESS_INVALID) |
1613 | 886 | && (job->exit_status != -1)) { | 919 | && (job->exit_status != -1)) { |
1614 | 887 | NIH_MUST (environ_set (&env, NULL, &len, TRUE, | 920 | NIH_MUST (environ_set (&env, NULL, &len, TRUE, |
1615 | 888 | "PROCESS=%s", | 921 | "PROCESS=%s", |
1616 | @@ -907,7 +940,7 @@ | |||
1617 | 907 | NIH_MUST (environ_set (&env, NULL, &len, TRUE, | 940 | NIH_MUST (environ_set (&env, NULL, &len, TRUE, |
1618 | 908 | "EXIT_STATUS=%d", job->exit_status)); | 941 | "EXIT_STATUS=%d", job->exit_status)); |
1619 | 909 | } | 942 | } |
1621 | 910 | } else if (job->failed_process != (ProcessType)-1) { | 943 | } else if (job->failed_process != PROCESS_INVALID) { |
1622 | 911 | NIH_MUST (environ_set (&env, NULL, &len, TRUE, | 944 | NIH_MUST (environ_set (&env, NULL, &len, TRUE, |
1623 | 912 | "PROCESS=%s", | 945 | "PROCESS=%s", |
1624 | 913 | process_name (job->failed_process))); | 946 | process_name (job->failed_process))); |
1625 | @@ -1492,3 +1525,689 @@ | |||
1626 | 1492 | 1525 | ||
1627 | 1493 | return 0; | 1526 | return 0; |
1628 | 1494 | } | 1527 | } |
1629 | 1528 | |||
1630 | 1529 | /** | ||
1631 | 1530 | * job_serialise: | ||
1632 | 1531 | * @job: job serialise. | ||
1633 | 1532 | * | ||
1634 | 1533 | * Convert @job into a JSON representation for serialisation. | ||
1635 | 1534 | * Caller must free returned value using json_object_put(). | ||
1636 | 1535 | * | ||
1637 | 1536 | * Note that the 'class' element is not encoded - it is assumed the | ||
1638 | 1537 | * caller will encode the returned JSON Job object as a child of the | ||
1639 | 1538 | * parent JSON-encoded JobClass object so a reference is not required. | ||
1640 | 1539 | * | ||
1641 | 1540 | * Returns: JSON-serialised Job object, or NULL on error. | ||
1642 | 1541 | **/ | ||
1643 | 1542 | json_object * | ||
1644 | 1543 | job_serialise (const Job *job) | ||
1645 | 1544 | { | ||
1646 | 1545 | json_object *json; | ||
1647 | 1546 | json_object *json_pid; | ||
1648 | 1547 | json_object *json_fds; | ||
1649 | 1548 | json_object *json_logs; | ||
1650 | 1549 | nih_local char *stop_on = NULL; | ||
1651 | 1550 | |||
1652 | 1551 | nih_assert (job); | ||
1653 | 1552 | |||
1654 | 1553 | json = json_object_new_object (); | ||
1655 | 1554 | if (! json) | ||
1656 | 1555 | return NULL; | ||
1657 | 1556 | |||
1658 | 1557 | if (! state_set_json_string_var_from_obj (json, job, name)) | ||
1659 | 1558 | goto error; | ||
1660 | 1559 | |||
1661 | 1560 | if (! state_set_json_string_var_from_obj (json, job, path)) | ||
1662 | 1561 | goto error; | ||
1663 | 1562 | |||
1664 | 1563 | if (! state_set_json_enum_var (json, | ||
1665 | 1564 | job_goal_enum_to_str, | ||
1666 | 1565 | "goal", job->goal)) | ||
1667 | 1566 | goto error; | ||
1668 | 1567 | |||
1669 | 1568 | if (! state_set_json_enum_var (json, | ||
1670 | 1569 | job_state_enum_to_str, | ||
1671 | 1570 | "state", job->state)) | ||
1672 | 1571 | goto error; | ||
1673 | 1572 | |||
1674 | 1573 | if (! state_set_json_str_array_from_obj (json, job, env)) | ||
1675 | 1574 | goto error; | ||
1676 | 1575 | |||
1677 | 1576 | if (! state_set_json_str_array_from_obj (json, job, start_env)) | ||
1678 | 1577 | goto error; | ||
1679 | 1578 | |||
1680 | 1579 | if (! state_set_json_str_array_from_obj (json, job, stop_env)) | ||
1681 | 1580 | goto error; | ||
1682 | 1581 | |||
1683 | 1582 | if (job->stop_on) { | ||
1684 | 1583 | stop_on = event_operator_collapse (job->stop_on); | ||
1685 | 1584 | if (! stop_on) | ||
1686 | 1585 | goto error; | ||
1687 | 1586 | |||
1688 | 1587 | if (! state_set_json_string_var (json, "stop_on", stop_on)) | ||
1689 | 1588 | goto error; | ||
1690 | 1589 | } | ||
1691 | 1590 | |||
1692 | 1591 | json_fds = state_serialise_int_array (int, job->fds, job->num_fds); | ||
1693 | 1592 | if (! json_fds) | ||
1694 | 1593 | goto error; | ||
1695 | 1594 | |||
1696 | 1595 | json_object_object_add (json, "fds", json_fds); | ||
1697 | 1596 | |||
1698 | 1597 | json_pid = state_serialise_int_array (pid_t, job->pid, | ||
1699 | 1598 | PROCESS_LAST); | ||
1700 | 1599 | if (! json_pid) | ||
1701 | 1600 | goto error; | ||
1702 | 1601 | |||
1703 | 1602 | json_object_object_add (json, "pid", json_pid); | ||
1704 | 1603 | |||
1705 | 1604 | /* Encode the blocking event as an index number which represents | ||
1706 | 1605 | * the event's position in the JSON events array. | ||
1707 | 1606 | */ | ||
1708 | 1607 | if (job->blocker) { | ||
1709 | 1608 | int event_index; | ||
1710 | 1609 | |||
1711 | 1610 | event_index = event_to_index (job->blocker); | ||
1712 | 1611 | if (event_index < 0) | ||
1713 | 1612 | goto error; | ||
1714 | 1613 | |||
1715 | 1614 | /* For consistency, it would be preferable to encode the | ||
1716 | 1615 | * event name, but the index is actually better since it is | ||
1717 | 1616 | * simple and unambiguous - encoding the name would also require | ||
1718 | 1617 | * us to encode the env to make the event unique. | ||
1719 | 1618 | */ | ||
1720 | 1619 | if (! state_set_json_int_var (json, "blocker", event_index)) | ||
1721 | 1620 | goto error; | ||
1722 | 1621 | |||
1723 | 1622 | } | ||
1724 | 1623 | |||
1725 | 1624 | if (! NIH_LIST_EMPTY (&job->blocking)) { | ||
1726 | 1625 | json_object *json_blocking; | ||
1727 | 1626 | |||
1728 | 1627 | json_blocking = state_serialise_blocking (&job->blocking); | ||
1729 | 1628 | if (! json_blocking) | ||
1730 | 1629 | goto error; | ||
1731 | 1630 | |||
1732 | 1631 | json_object_object_add (json, "blocking", json_blocking); | ||
1733 | 1632 | } | ||
1734 | 1633 | |||
1735 | 1634 | /* conditionally encode kill timer */ | ||
1736 | 1635 | if (job->kill_timer) { | ||
1737 | 1636 | json_object *kill_timer; | ||
1738 | 1637 | |||
1739 | 1638 | kill_timer = job_serialise_kill_timer (job->kill_timer); | ||
1740 | 1639 | |||
1741 | 1640 | if (! kill_timer) | ||
1742 | 1641 | goto error; | ||
1743 | 1642 | |||
1744 | 1643 | json_object_object_add (json, "kill_timer", kill_timer); | ||
1745 | 1644 | } | ||
1746 | 1645 | |||
1747 | 1646 | if (! state_set_json_enum_var (json, | ||
1748 | 1647 | process_type_enum_to_str, | ||
1749 | 1648 | "kill_process", job->kill_process)) | ||
1750 | 1649 | goto error; | ||
1751 | 1650 | |||
1752 | 1651 | if (! state_set_json_int_var_from_obj (json, job, failed)) | ||
1753 | 1652 | goto error; | ||
1754 | 1653 | |||
1755 | 1654 | if (! state_set_json_enum_var (json, | ||
1756 | 1655 | process_type_enum_to_str, | ||
1757 | 1656 | "failed_process", job->failed_process)) | ||
1758 | 1657 | goto error; | ||
1759 | 1658 | |||
1760 | 1659 | if (! state_set_json_int_var_from_obj (json, job, exit_status)) | ||
1761 | 1660 | goto error; | ||
1762 | 1661 | |||
1763 | 1662 | if (! state_set_json_int_var_from_obj (json, job, respawn_time)) | ||
1764 | 1663 | goto error; | ||
1765 | 1664 | |||
1766 | 1665 | if (! state_set_json_int_var_from_obj (json, job, respawn_count)) | ||
1767 | 1666 | goto error; | ||
1768 | 1667 | |||
1769 | 1668 | if (! state_set_json_int_var_from_obj (json, job, trace_forks)) | ||
1770 | 1669 | goto error; | ||
1771 | 1670 | |||
1772 | 1671 | if (! state_set_json_enum_var (json, | ||
1773 | 1672 | job_trace_state_enum_to_str, | ||
1774 | 1673 | "trace_state", job->trace_state)) | ||
1775 | 1674 | goto error; | ||
1776 | 1675 | |||
1777 | 1676 | json_logs = json_object_new_array (); | ||
1778 | 1677 | |||
1779 | 1678 | if (! json_logs) | ||
1780 | 1679 | return json; | ||
1781 | 1680 | |||
1782 | 1681 | for (int process = 0; process < PROCESS_LAST; process++) { | ||
1783 | 1682 | json_object *json_log; | ||
1784 | 1683 | |||
1785 | 1684 | json_log = log_serialise (job->log[process]); | ||
1786 | 1685 | if (! json_log) | ||
1787 | 1686 | goto error; | ||
1788 | 1687 | |||
1789 | 1688 | if (json_object_array_add (json_logs, json_log) < 0) | ||
1790 | 1689 | goto error; | ||
1791 | 1690 | } | ||
1792 | 1691 | |||
1793 | 1692 | json_object_object_add (json, "log", json_logs); | ||
1794 | 1693 | |||
1795 | 1694 | return json; | ||
1796 | 1695 | |||
1797 | 1696 | error: | ||
1798 | 1697 | json_object_put (json); | ||
1799 | 1698 | return NULL; | ||
1800 | 1699 | } | ||
1801 | 1700 | |||
1802 | 1701 | /** | ||
1803 | 1702 | * job_serialise_all: | ||
1804 | 1703 | * | ||
1805 | 1704 | * Convert existing Session objects to JSON representation. | ||
1806 | 1705 | * | ||
1807 | 1706 | * Returns: JSON object containing array of Job objects, or NULL on error. | ||
1808 | 1707 | **/ | ||
1809 | 1708 | json_object * | ||
1810 | 1709 | job_serialise_all (const NihHash *jobs) | ||
1811 | 1710 | { | ||
1812 | 1711 | int count = 0; | ||
1813 | 1712 | json_object *json; | ||
1814 | 1713 | |||
1815 | 1714 | nih_assert (jobs); | ||
1816 | 1715 | |||
1817 | 1716 | json = json_object_new_array (); | ||
1818 | 1717 | if (! json) | ||
1819 | 1718 | return NULL; | ||
1820 | 1719 | |||
1821 | 1720 | NIH_HASH_FOREACH (jobs, iter) { | ||
1822 | 1721 | json_object *json_job; | ||
1823 | 1722 | Job *job = (Job *)iter; | ||
1824 | 1723 | |||
1825 | 1724 | count++; | ||
1826 | 1725 | json_job = job_serialise (job); | ||
1827 | 1726 | |||
1828 | 1727 | if (! json_job) | ||
1829 | 1728 | goto error; | ||
1830 | 1729 | |||
1831 | 1730 | json_object_array_add (json, json_job); | ||
1832 | 1731 | } | ||
1833 | 1732 | |||
1834 | 1733 | /* Raise an error to avoid serialising job classes with | ||
1835 | 1734 | * no associated jobs. | ||
1836 | 1735 | */ | ||
1837 | 1736 | if (! count) | ||
1838 | 1737 | goto error; | ||
1839 | 1738 | |||
1840 | 1739 | return json; | ||
1841 | 1740 | |||
1842 | 1741 | error: | ||
1843 | 1742 | json_object_put (json); | ||
1844 | 1743 | return NULL; | ||
1845 | 1744 | } | ||
1846 | 1745 | |||
1847 | 1746 | /** | ||
1848 | 1747 | * job_deserialise: | ||
1849 | 1748 | * @parent: job class for JSON-encoded jobs, | ||
1850 | 1749 | * @json: JSON-serialised Job object to deserialise. | ||
1851 | 1750 | * | ||
1852 | 1751 | * XXX: All events must have been deserialised prior to this function | ||
1853 | 1752 | * XXX: being called. | ||
1854 | 1753 | * | ||
1855 | 1754 | * Returns: Job object, or NULL on error. | ||
1856 | 1755 | **/ | ||
1857 | 1756 | Job * | ||
1858 | 1757 | job_deserialise (JobClass *parent, json_object *json) | ||
1859 | 1758 | { | ||
1860 | 1759 | nih_local char *name = NULL; | ||
1861 | 1760 | Job *job = NULL; | ||
1862 | 1761 | json_object *json_kill_timer; | ||
1863 | 1762 | json_object *blocker; | ||
1864 | 1763 | json_object *json_fds; | ||
1865 | 1764 | json_object *json_pid; | ||
1866 | 1765 | json_object *json_logs; | ||
1867 | 1766 | size_t len; | ||
1868 | 1767 | int ret; | ||
1869 | 1768 | |||
1870 | 1769 | nih_assert (parent); | ||
1871 | 1770 | nih_assert (json); | ||
1872 | 1771 | |||
1873 | 1772 | if (! state_check_json_type (json, object)) | ||
1874 | 1773 | return NULL; | ||
1875 | 1774 | |||
1876 | 1775 | if (! state_get_json_string_var_strict (json, "name", NULL, name)) | ||
1877 | 1776 | goto error; | ||
1878 | 1777 | |||
1879 | 1778 | job = NIH_MUST (job_new (parent, name)); | ||
1880 | 1779 | |||
1881 | 1780 | if (! job) | ||
1882 | 1781 | return NULL; | ||
1883 | 1782 | |||
1884 | 1783 | if (! state_get_json_string_var_to_obj_strict (json, job, path)) | ||
1885 | 1784 | goto error; | ||
1886 | 1785 | |||
1887 | 1786 | if (! state_get_json_enum_var (json, | ||
1888 | 1787 | job_goal_str_to_enum, | ||
1889 | 1788 | "goal", job->goal)) | ||
1890 | 1789 | goto error; | ||
1891 | 1790 | |||
1892 | 1791 | if (! state_get_json_enum_var (json, | ||
1893 | 1792 | job_state_str_to_enum, | ||
1894 | 1793 | "state", job->state)) | ||
1895 | 1794 | goto error; | ||
1896 | 1795 | |||
1897 | 1796 | if (! state_get_json_env_array_to_obj (json, job, env)) | ||
1898 | 1797 | goto error; | ||
1899 | 1798 | |||
1900 | 1799 | if (! state_get_json_env_array_to_obj (json, job, start_env)) | ||
1901 | 1800 | goto error; | ||
1902 | 1801 | |||
1903 | 1802 | if (! state_get_json_env_array_to_obj (json, job, stop_env)) | ||
1904 | 1803 | goto error; | ||
1905 | 1804 | |||
1906 | 1805 | if (json_object_object_get (json, "stop_on")) { | ||
1907 | 1806 | nih_local char *stop_on = NULL; | ||
1908 | 1807 | |||
1909 | 1808 | if (! state_get_json_string_var_strict (json, "stop_on", NULL, stop_on)) | ||
1910 | 1809 | goto error; | ||
1911 | 1810 | |||
1912 | 1811 | if (*stop_on) { | ||
1913 | 1812 | nih_local JobClass *tmp = NULL; | ||
1914 | 1813 | |||
1915 | 1814 | tmp = NIH_MUST (job_class_new (NULL, "tmp", NULL)); | ||
1916 | 1815 | |||
1917 | 1816 | tmp->stop_on = parse_on_simple (tmp, "stop", stop_on); | ||
1918 | 1817 | if (! tmp->stop_on) { | ||
1919 | 1818 | NihError *err; | ||
1920 | 1819 | |||
1921 | 1820 | err = nih_error_get (); | ||
1922 | 1821 | |||
1923 | 1822 | nih_error ("%s %s: %s", | ||
1924 | 1823 | _("BUG"), | ||
1925 | 1824 | _("instance 'stop on' parse error"), | ||
1926 | 1825 | err->message); | ||
1927 | 1826 | |||
1928 | 1827 | nih_free (err); | ||
1929 | 1828 | |||
1930 | 1829 | goto error; | ||
1931 | 1830 | } | ||
1932 | 1831 | |||
1933 | 1832 | nih_free (job->stop_on); | ||
1934 | 1833 | job->stop_on = event_operator_copy (job, tmp->stop_on); | ||
1935 | 1834 | if (! job->stop_on) | ||
1936 | 1835 | goto error; | ||
1937 | 1836 | } | ||
1938 | 1837 | } | ||
1939 | 1838 | |||
1940 | 1839 | /* fds and num_fds handled by caller */ | ||
1941 | 1840 | /* pid handled by caller */ | ||
1942 | 1841 | |||
1943 | 1842 | /* blocking is handled by state_deserialise_blocking() */ | ||
1944 | 1843 | |||
1945 | 1844 | blocker = json_object_object_get (json, "blocker"); | ||
1946 | 1845 | |||
1947 | 1846 | if (blocker) { | ||
1948 | 1847 | int event_index = -1; | ||
1949 | 1848 | |||
1950 | 1849 | if (! state_get_json_int_var (json, "blocker", event_index)) | ||
1951 | 1850 | goto error; | ||
1952 | 1851 | job->blocker = event_from_index (event_index); | ||
1953 | 1852 | |||
1954 | 1853 | if (! job->blocker) | ||
1955 | 1854 | goto error; | ||
1956 | 1855 | } | ||
1957 | 1856 | |||
1958 | 1857 | if (! state_get_json_enum_var (json, | ||
1959 | 1858 | process_type_str_to_enum, | ||
1960 | 1859 | "kill_process", job->kill_process)) | ||
1961 | 1860 | goto error; | ||
1962 | 1861 | |||
1963 | 1862 | /* Check to see if a kill timer exists first since we do not | ||
1964 | 1863 | * want to end up creating a real but empty timer. | ||
1965 | 1864 | */ | ||
1966 | 1865 | json_kill_timer = json_object_object_get (json, "kill_timer"); | ||
1967 | 1866 | |||
1968 | 1867 | if (json_kill_timer) { | ||
1969 | 1868 | /* Found a partial kill timer, so create a new one and | ||
1970 | 1869 | * adjust its due time. By the time the main loop gets | ||
1971 | 1870 | * called, the due time will probably be in the past | ||
1972 | 1871 | * such that the job will be stopped. | ||
1973 | 1872 | * | ||
1974 | 1873 | * To be completely fair we should: | ||
1975 | 1874 | * | ||
1976 | 1875 | * - encode the time at the point of serialisation in a | ||
1977 | 1876 | * JSON 'meta' header. | ||
1978 | 1877 | * - query the time post-deserialisation and calculate | ||
1979 | 1878 | * the delta (being the time to perform the stateful | ||
1980 | 1879 | * re-exec). | ||
1981 | 1880 | * - add that time to all jobs with active kill timers | ||
1982 | 1881 | * to give their processes the full amount of time to | ||
1983 | 1882 | * end. | ||
1984 | 1883 | */ | ||
1985 | 1884 | nih_local NihTimer *kill_timer = job_deserialise_kill_timer (json_kill_timer); | ||
1986 | 1885 | if (! kill_timer) | ||
1987 | 1886 | goto error; | ||
1988 | 1887 | |||
1989 | 1888 | nih_assert (job->kill_process); | ||
1990 | 1889 | job_process_set_kill_timer (job, job->kill_process, | ||
1991 | 1890 | kill_timer->timeout); | ||
1992 | 1891 | job_process_adj_kill_timer (job, kill_timer->due); | ||
1993 | 1892 | } | ||
1994 | 1893 | |||
1995 | 1894 | if (! state_get_json_int_var_to_obj (json, job, failed)) | ||
1996 | 1895 | goto error; | ||
1997 | 1896 | |||
1998 | 1897 | if (! state_get_json_enum_var (json, | ||
1999 | 1898 | process_type_str_to_enum, | ||
2000 | 1899 | "failed_process", job->failed_process)) | ||
2001 | 1900 | goto error; | ||
2002 | 1901 | |||
2003 | 1902 | if (! state_get_json_int_var_to_obj (json, job, exit_status)) | ||
2004 | 1903 | goto error; | ||
2005 | 1904 | |||
2006 | 1905 | if (! state_get_json_int_var_to_obj (json, job, respawn_time)) | ||
2007 | 1906 | goto error; | ||
2008 | 1907 | |||
2009 | 1908 | if (! state_get_json_int_var_to_obj (json, job, respawn_count)) | ||
2010 | 1909 | goto error; | ||
2011 | 1910 | |||
2012 | 1911 | json_fds = json_object_object_get (json, "fds"); | ||
2013 | 1912 | if (! json_fds) | ||
2014 | 1913 | goto error; | ||
2015 | 1914 | |||
2016 | 1915 | ret = state_deserialise_int_array (job, json_fds, | ||
2017 | 1916 | int, &job->fds, &job->num_fds); | ||
2018 | 1917 | if (ret < 0) | ||
2019 | 1918 | goto error; | ||
2020 | 1919 | |||
2021 | 1920 | json_pid = json_object_object_get (json, "pid"); | ||
2022 | 1921 | if (! json_pid) | ||
2023 | 1922 | goto error; | ||
2024 | 1923 | |||
2025 | 1924 | ret = state_deserialise_int_array (job, json_pid, | ||
2026 | 1925 | pid_t, &job->pid, &len); | ||
2027 | 1926 | if (ret < 0) | ||
2028 | 1927 | goto error; | ||
2029 | 1928 | |||
2030 | 1929 | if (len != PROCESS_LAST) | ||
2031 | 1930 | goto error; | ||
2032 | 1931 | |||
2033 | 1932 | if (! state_get_json_int_var_to_obj (json, job, trace_forks)) | ||
2034 | 1933 | goto error; | ||
2035 | 1934 | |||
2036 | 1935 | if (! state_get_json_enum_var (json, | ||
2037 | 1936 | job_trace_state_str_to_enum, | ||
2038 | 1937 | "trace_state", job->trace_state)) | ||
2039 | 1938 | goto error; | ||
2040 | 1939 | |||
2041 | 1940 | json_logs = json_object_object_get (json, "log"); | ||
2042 | 1941 | |||
2043 | 1942 | if (! json_logs) | ||
2044 | 1943 | goto error; | ||
2045 | 1944 | |||
2046 | 1945 | if (! state_check_json_type (json_logs, array)) | ||
2047 | 1946 | goto error; | ||
2048 | 1947 | |||
2049 | 1948 | for (int process = 0; process < PROCESS_LAST; process++) { | ||
2050 | 1949 | json_object *json_log; | ||
2051 | 1950 | |||
2052 | 1951 | json_log = json_object_array_get_idx (json_logs, process); | ||
2053 | 1952 | if (! json_log) | ||
2054 | 1953 | goto error; | ||
2055 | 1954 | |||
2056 | 1955 | /* NULL if there was no log configured, or we failed to | ||
2057 | 1956 | * deserialise it; either way, this should be non-fatal. | ||
2058 | 1957 | */ | ||
2059 | 1958 | job->log[process] = log_deserialise (job->log, json_log); | ||
2060 | 1959 | } | ||
2061 | 1960 | |||
2062 | 1961 | return job; | ||
2063 | 1962 | |||
2064 | 1963 | error: | ||
2065 | 1964 | nih_free (job); | ||
2066 | 1965 | return NULL; | ||
2067 | 1966 | } | ||
2068 | 1967 | |||
2069 | 1968 | /** | ||
2070 | 1969 | * job_deserialise_all: | ||
2071 | 1970 | * | ||
2072 | 1971 | * @parent: job class for JSON-encoded jobs, | ||
2073 | 1972 | * @json: root of JSON-serialised state. | ||
2074 | 1973 | * | ||
2075 | 1974 | * Convert JSON representation of jobs back into Job objects associated | ||
2076 | 1975 | * with @parent. | ||
2077 | 1976 | * | ||
2078 | 1977 | * Returns: 0 on success, -1 on error. | ||
2079 | 1978 | **/ | ||
2080 | 1979 | int | ||
2081 | 1980 | job_deserialise_all (JobClass *parent, json_object *json) | ||
2082 | 1981 | { | ||
2083 | 1982 | json_object *json_jobs; | ||
2084 | 1983 | Job *job; | ||
2085 | 1984 | |||
2086 | 1985 | nih_assert (parent); | ||
2087 | 1986 | nih_assert (json); | ||
2088 | 1987 | |||
2089 | 1988 | json_jobs = json_object_object_get (json, "jobs"); | ||
2090 | 1989 | |||
2091 | 1990 | if (! json_jobs) | ||
2092 | 1991 | goto error; | ||
2093 | 1992 | |||
2094 | 1993 | if (! state_check_json_type (json_jobs, array)) | ||
2095 | 1994 | goto error; | ||
2096 | 1995 | |||
2097 | 1996 | for (int i = 0; i < json_object_array_length (json_jobs); i++) { | ||
2098 | 1997 | json_object *json_job; | ||
2099 | 1998 | |||
2100 | 1999 | json_job = json_object_array_get_idx (json_jobs, i); | ||
2101 | 2000 | if (! json_job) | ||
2102 | 2001 | goto error; | ||
2103 | 2002 | |||
2104 | 2003 | if (! state_check_json_type (json_job, object)) | ||
2105 | 2004 | goto error; | ||
2106 | 2005 | |||
2107 | 2006 | job = job_deserialise (parent, json_job); | ||
2108 | 2007 | if (! job) | ||
2109 | 2008 | goto error; | ||
2110 | 2009 | } | ||
2111 | 2010 | |||
2112 | 2011 | return 0; | ||
2113 | 2012 | |||
2114 | 2013 | error: | ||
2115 | 2014 | return -1; | ||
2116 | 2015 | } | ||
2117 | 2016 | |||
2118 | 2017 | /** | ||
2119 | 2018 | * job_goal_enum_to_str: | ||
2120 | 2019 | * | ||
2121 | 2020 | * @goal: JobGoal. | ||
2122 | 2021 | * | ||
2123 | 2022 | * Convert JobGoal to a string representation. | ||
2124 | 2023 | * | ||
2125 | 2024 | * Returns: string representation of @goal, or NULL if not known. | ||
2126 | 2025 | **/ | ||
2127 | 2026 | static const char * | ||
2128 | 2027 | job_goal_enum_to_str (JobGoal goal) | ||
2129 | 2028 | { | ||
2130 | 2029 | state_enum_to_str (JOB_STOP, goal); | ||
2131 | 2030 | state_enum_to_str (JOB_START, goal); | ||
2132 | 2031 | state_enum_to_str (JOB_RESPAWN, goal); | ||
2133 | 2032 | |||
2134 | 2033 | return NULL; | ||
2135 | 2034 | } | ||
2136 | 2035 | |||
2137 | 2036 | /** | ||
2138 | 2037 | * job_goal_str_to_enum: | ||
2139 | 2038 | * | ||
2140 | 2039 | * @goal: string JobGoal value. | ||
2141 | 2040 | * | ||
2142 | 2041 | * Convert @goal back into enum value. | ||
2143 | 2042 | * | ||
2144 | 2043 | * Returns: JobGoal representation of @goal, or -1 if not known. | ||
2145 | 2044 | **/ | ||
2146 | 2045 | static JobGoal | ||
2147 | 2046 | job_goal_str_to_enum (const char *goal) | ||
2148 | 2047 | { | ||
2149 | 2048 | state_str_to_enum (JOB_STOP, goal); | ||
2150 | 2049 | state_str_to_enum (JOB_START, goal); | ||
2151 | 2050 | state_str_to_enum (JOB_RESPAWN, goal); | ||
2152 | 2051 | |||
2153 | 2052 | return -1; | ||
2154 | 2053 | } | ||
2155 | 2054 | |||
2156 | 2055 | /** | ||
2157 | 2056 | * job_state_enum_to_str: | ||
2158 | 2057 | * | ||
2159 | 2058 | * @state: JobState. | ||
2160 | 2059 | * | ||
2161 | 2060 | * Convert JobState to a string representation. | ||
2162 | 2061 | * | ||
2163 | 2062 | * Returns: string representation of @state, or NULL if not known. | ||
2164 | 2063 | **/ | ||
2165 | 2064 | static const char * | ||
2166 | 2065 | job_state_enum_to_str (JobState state) | ||
2167 | 2066 | { | ||
2168 | 2067 | state_enum_to_str (JOB_WAITING, state); | ||
2169 | 2068 | state_enum_to_str (JOB_STARTING, state); | ||
2170 | 2069 | state_enum_to_str (JOB_PRE_START, state); | ||
2171 | 2070 | state_enum_to_str (JOB_SPAWNED, state); | ||
2172 | 2071 | state_enum_to_str (JOB_POST_START, state); | ||
2173 | 2072 | state_enum_to_str (JOB_RUNNING, state); | ||
2174 | 2073 | state_enum_to_str (JOB_PRE_STOP, state); | ||
2175 | 2074 | state_enum_to_str (JOB_STOPPING, state); | ||
2176 | 2075 | state_enum_to_str (JOB_KILLED, state); | ||
2177 | 2076 | state_enum_to_str (JOB_POST_STOP, state); | ||
2178 | 2077 | |||
2179 | 2078 | return NULL; | ||
2180 | 2079 | } | ||
2181 | 2080 | |||
2182 | 2081 | /** | ||
2183 | 2082 | * job_state_str_to_enum: | ||
2184 | 2083 | * | ||
2185 | 2084 | * @state: string JobState value. | ||
2186 | 2085 | * | ||
2187 | 2086 | * Convert @state back into enum value. | ||
2188 | 2087 | * | ||
2189 | 2088 | * Returns: JobState representation of @state, or -1 if not known. | ||
2190 | 2089 | **/ | ||
2191 | 2090 | static JobState | ||
2192 | 2091 | job_state_str_to_enum (const char *state) | ||
2193 | 2092 | { | ||
2194 | 2093 | state_str_to_enum (JOB_WAITING, state); | ||
2195 | 2094 | state_str_to_enum (JOB_STARTING, state); | ||
2196 | 2095 | state_str_to_enum (JOB_PRE_START, state); | ||
2197 | 2096 | state_str_to_enum (JOB_SPAWNED, state); | ||
2198 | 2097 | state_str_to_enum (JOB_POST_START, state); | ||
2199 | 2098 | state_str_to_enum (JOB_RUNNING, state); | ||
2200 | 2099 | state_str_to_enum (JOB_PRE_STOP, state); | ||
2201 | 2100 | state_str_to_enum (JOB_STOPPING, state); | ||
2202 | 2101 | state_str_to_enum (JOB_KILLED, state); | ||
2203 | 2102 | state_str_to_enum (JOB_POST_STOP, state); | ||
2204 | 2103 | |||
2205 | 2104 | return -1; | ||
2206 | 2105 | } | ||
2207 | 2106 | |||
2208 | 2107 | /** | ||
2209 | 2108 | * job_state_enum_to_str: | ||
2210 | 2109 | * | ||
2211 | 2110 | * @state: TraceState. | ||
2212 | 2111 | * | ||
2213 | 2112 | * Convert TraceState to a string representation. | ||
2214 | 2113 | * | ||
2215 | 2114 | * Returns: string representation of @state, or NULL if not known. | ||
2216 | 2115 | **/ | ||
2217 | 2116 | static const char * | ||
2218 | 2117 | job_trace_state_enum_to_str (TraceState state) | ||
2219 | 2118 | { | ||
2220 | 2119 | state_enum_to_str (TRACE_NONE, state); | ||
2221 | 2120 | state_enum_to_str (TRACE_NEW, state); | ||
2222 | 2121 | state_enum_to_str (TRACE_NEW_CHILD, state); | ||
2223 | 2122 | state_enum_to_str (TRACE_NORMAL, state); | ||
2224 | 2123 | |||
2225 | 2124 | return NULL; | ||
2226 | 2125 | } | ||
2227 | 2126 | |||
2228 | 2127 | /** | ||
2229 | 2128 | * job_trace_state_str_to_enum: | ||
2230 | 2129 | * | ||
2231 | 2130 | * @state: string TraceState value. | ||
2232 | 2131 | * | ||
2233 | 2132 | * Convert @state back into enum value. | ||
2234 | 2133 | * | ||
2235 | 2134 | * Returns: TraceState representation of @state, or -1 if not known. | ||
2236 | 2135 | **/ | ||
2237 | 2136 | static TraceState | ||
2238 | 2137 | job_trace_state_str_to_enum (const char *state) | ||
2239 | 2138 | { | ||
2240 | 2139 | state_str_to_enum (TRACE_NONE, state); | ||
2241 | 2140 | state_str_to_enum (TRACE_NEW, state); | ||
2242 | 2141 | state_str_to_enum (TRACE_NEW_CHILD, state); | ||
2243 | 2142 | state_str_to_enum (TRACE_NORMAL, state); | ||
2244 | 2143 | |||
2245 | 2144 | return -1; | ||
2246 | 2145 | } | ||
2247 | 2146 | |||
2248 | 2147 | /** | ||
2249 | 2148 | * job_serialise_kill_timer: | ||
2250 | 2149 | * | ||
2251 | 2150 | * @timer: NihTimer to serialise. | ||
2252 | 2151 | * | ||
2253 | 2152 | * Serialise @timer into JSON. | ||
2254 | 2153 | * | ||
2255 | 2154 | * Returns: JSON-serialised NihTimer object, or NULL on error. | ||
2256 | 2155 | **/ | ||
2257 | 2156 | static json_object * | ||
2258 | 2157 | job_serialise_kill_timer (NihTimer *timer) | ||
2259 | 2158 | { | ||
2260 | 2159 | json_object *json; | ||
2261 | 2160 | |||
2262 | 2161 | nih_assert (timer); | ||
2263 | 2162 | |||
2264 | 2163 | json = json_object_new_object (); | ||
2265 | 2164 | if (! json) | ||
2266 | 2165 | return NULL; | ||
2267 | 2166 | |||
2268 | 2167 | if (! state_set_json_int_var_from_obj (json, timer, timeout)) | ||
2269 | 2168 | goto error; | ||
2270 | 2169 | |||
2271 | 2170 | if (! state_set_json_int_var_from_obj (json, timer, due)) | ||
2272 | 2171 | goto error; | ||
2273 | 2172 | |||
2274 | 2173 | return json; | ||
2275 | 2174 | |||
2276 | 2175 | error: | ||
2277 | 2176 | json_object_put (json); | ||
2278 | 2177 | return NULL; | ||
2279 | 2178 | } | ||
2280 | 2179 | |||
2281 | 2180 | /** | ||
2282 | 2181 | * job_deserialise_kill_timer: | ||
2283 | 2182 | * | ||
2284 | 2183 | * @json: JSON representation of NihTimer. | ||
2285 | 2184 | * | ||
2286 | 2185 | * Deserialise @json back into an NihTimer. | ||
2287 | 2186 | * | ||
2288 | 2187 | * Returns: NihTimer on NULL on error. | ||
2289 | 2188 | **/ | ||
2290 | 2189 | static NihTimer * | ||
2291 | 2190 | job_deserialise_kill_timer (json_object *json) | ||
2292 | 2191 | { | ||
2293 | 2192 | NihTimer *timer; | ||
2294 | 2193 | |||
2295 | 2194 | nih_assert (json); | ||
2296 | 2195 | |||
2297 | 2196 | timer = nih_new (NULL, NihTimer); | ||
2298 | 2197 | if (! timer) | ||
2299 | 2198 | return NULL; | ||
2300 | 2199 | |||
2301 | 2200 | memset (timer, '\0', sizeof (NihTimer)); | ||
2302 | 2201 | |||
2303 | 2202 | if (! state_get_json_int_var_to_obj (json, timer, due)) | ||
2304 | 2203 | goto error; | ||
2305 | 2204 | |||
2306 | 2205 | if (! state_get_json_int_var_to_obj (json, timer, timeout)) | ||
2307 | 2206 | goto error; | ||
2308 | 2207 | |||
2309 | 2208 | return timer; | ||
2310 | 2209 | |||
2311 | 2210 | error: | ||
2312 | 2211 | nih_free (timer); | ||
2313 | 2212 | return NULL; | ||
2314 | 2213 | } | ||
2315 | 1495 | 2214 | ||
2316 | === modified file 'init/job.h' | |||
2317 | --- init/job.h 2012-03-07 12:13:28 +0000 | |||
2318 | +++ init/job.h 2012-11-14 14:49:20 +0000 | |||
2319 | @@ -103,6 +103,9 @@ | |||
2320 | 103 | * @start_env: environment to use next time the job is started, | 103 | * @start_env: environment to use next time the job is started, |
2321 | 104 | * @stop_env: environment to add for the next pre-stop script, | 104 | * @stop_env: environment to add for the next pre-stop script, |
2322 | 105 | * @stop_on: event operator expression that can stop this job. | 105 | * @stop_on: event operator expression that can stop this job. |
2323 | 106 | * @fds: array of file descriptors associated with events in parent | ||
2324 | 107 | * JobClasses @start_on condition, | ||
2325 | 108 | * @num_fds: number of elements in @fds, | ||
2326 | 106 | * @pid: current process ids, | 109 | * @pid: current process ids, |
2327 | 107 | * @blocker: emitted event we're waiting to finish, | 110 | * @blocker: emitted event we're waiting to finish, |
2328 | 108 | * @blocking: list of events we're blocking from finishing, | 111 | * @blocking: list of events we're blocking from finishing, |
2329 | @@ -136,8 +139,8 @@ | |||
2330 | 136 | char **stop_env; | 139 | char **stop_env; |
2331 | 137 | EventOperator *stop_on; | 140 | EventOperator *stop_on; |
2332 | 138 | 141 | ||
2335 | 139 | int *fds; | 142 | int *fds; |
2336 | 140 | size_t num_fds; | 143 | size_t num_fds; |
2337 | 141 | 144 | ||
2338 | 142 | pid_t *pid; | 145 | pid_t *pid; |
2339 | 143 | Event *blocker; | 146 | Event *blocker; |
2340 | @@ -208,6 +211,15 @@ | |||
2341 | 208 | JobProcessesElement ***processes) | 211 | JobProcessesElement ***processes) |
2342 | 209 | __attribute__ ((warn_unused_result)); | 212 | __attribute__ ((warn_unused_result)); |
2343 | 210 | 213 | ||
2344 | 214 | json_object *job_serialise (const Job *job); | ||
2345 | 215 | Job *job_deserialise (JobClass *parent, json_object *json); | ||
2346 | 216 | |||
2347 | 217 | json_object *job_serialise_all (const NihHash *jobs) | ||
2348 | 218 | __attribute__ ((malloc, warn_unused_result)); | ||
2349 | 219 | |||
2350 | 220 | int job_deserialise_all (JobClass *parent, json_object *json) | ||
2351 | 221 | __attribute__ ((warn_unused_result)); | ||
2352 | 222 | |||
2353 | 211 | NIH_END_EXTERN | 223 | NIH_END_EXTERN |
2354 | 212 | 224 | ||
2355 | 213 | #endif /* INIT_JOB_H */ | 225 | #endif /* INIT_JOB_H */ |
2356 | 214 | 226 | ||
2357 | === modified file 'init/job_class.c' | |||
2358 | --- init/job_class.c 2012-03-01 11:55:19 +0000 | |||
2359 | +++ init/job_class.c 2012-11-14 14:49:20 +0000 | |||
2360 | @@ -52,14 +52,18 @@ | |||
2361 | 52 | #include "blocked.h" | 52 | #include "blocked.h" |
2362 | 53 | #include "conf.h" | 53 | #include "conf.h" |
2363 | 54 | #include "control.h" | 54 | #include "control.h" |
2364 | 55 | #include "parse_job.h" | ||
2365 | 55 | 56 | ||
2366 | 56 | #include "com.ubuntu.Upstart.h" | 57 | #include "com.ubuntu.Upstart.h" |
2367 | 57 | #include "com.ubuntu.Upstart.Job.h" | 58 | #include "com.ubuntu.Upstart.Job.h" |
2368 | 58 | 59 | ||
2369 | 60 | #include <json.h> | ||
2370 | 61 | |||
2371 | 62 | extern json_object *json_classes; | ||
2372 | 59 | 63 | ||
2373 | 60 | /* Prototypes for static functions */ | 64 | /* Prototypes for static functions */ |
2376 | 61 | static void job_class_add (JobClass *class); | 65 | static void job_class_add (JobClass *class); |
2377 | 62 | static int job_class_remove (JobClass *class, const Session *session); | 66 | static int job_class_remove (JobClass *class, const Session *session); |
2378 | 63 | 67 | ||
2379 | 64 | /** | 68 | /** |
2380 | 65 | * default_console: | 69 | * default_console: |
2381 | @@ -356,6 +360,30 @@ | |||
2382 | 356 | } | 360 | } |
2383 | 357 | 361 | ||
2384 | 358 | /** | 362 | /** |
2385 | 363 | * job_class_add_safe: | ||
2386 | 364 | * @class: new class to select. | ||
2387 | 365 | * | ||
2388 | 366 | * Adds @class to the hash table iff no existing entry of the | ||
2389 | 367 | * same name exists. | ||
2390 | 368 | **/ | ||
2391 | 369 | void | ||
2392 | 370 | job_class_add_safe (JobClass *class) | ||
2393 | 371 | { | ||
2394 | 372 | JobClass *existing = NULL; | ||
2395 | 373 | |||
2396 | 374 | nih_assert (class); | ||
2397 | 375 | nih_assert (class->name); | ||
2398 | 376 | |||
2399 | 377 | control_init (); | ||
2400 | 378 | |||
2401 | 379 | existing = (JobClass *)nih_hash_search (job_classes, class->name, NULL); | ||
2402 | 380 | |||
2403 | 381 | nih_assert (! existing); | ||
2404 | 382 | |||
2405 | 383 | job_class_add (class); | ||
2406 | 384 | } | ||
2407 | 385 | |||
2408 | 386 | /** | ||
2409 | 359 | * job_class_remove: | 387 | * job_class_remove: |
2410 | 360 | * @class: class to remove, | 388 | * @class: class to remove, |
2411 | 361 | * @session: Session of @class. | 389 | * @session: Session of @class. |
2412 | @@ -720,7 +748,7 @@ | |||
2413 | 720 | * If the instance goal is already start, | 748 | * If the instance goal is already start, |
2414 | 721 | * the com.ubuntu.Upstart.Error.AlreadyStarted D-Bus error will be returned | 749 | * the com.ubuntu.Upstart.Error.AlreadyStarted D-Bus error will be returned |
2415 | 722 | * immediately. If the instance fails to start, the | 750 | * immediately. If the instance fails to start, the |
2417 | 723 | * com.ubuntu.Upstart.Error.JobFailed D-BUs error will be returned when the | 751 | * com.ubuntu.Upstart.Error.JobFailed D-Bus error will be returned when the |
2418 | 724 | * problem occurs. | 752 | * problem occurs. |
2419 | 725 | * | 753 | * |
2420 | 726 | * When @wait is TRUE the method call will not return until the job has | 754 | * When @wait is TRUE the method call will not return until the job has |
2421 | @@ -1112,6 +1140,39 @@ | |||
2422 | 1112 | return 0; | 1140 | return 0; |
2423 | 1113 | } | 1141 | } |
2424 | 1114 | 1142 | ||
2425 | 1143 | /** | ||
2426 | 1144 | * job_class_get: | ||
2427 | 1145 | * | ||
2428 | 1146 | * @name: name of job class, | ||
2429 | 1147 | * @session: session of job class. | ||
2430 | 1148 | * | ||
2431 | 1149 | * Obtain JobClass with name @name and session @session. | ||
2432 | 1150 | * | ||
2433 | 1151 | * Returns: JobClass, or NULL if no matching job class found. | ||
2434 | 1152 | **/ | ||
2435 | 1153 | JobClass * | ||
2436 | 1154 | job_class_get (const char *name, Session *session) | ||
2437 | 1155 | { | ||
2438 | 1156 | JobClass *class = NULL; | ||
2439 | 1157 | NihList *prev = NULL; | ||
2440 | 1158 | |||
2441 | 1159 | nih_assert (name); | ||
2442 | 1160 | |||
2443 | 1161 | do { | ||
2444 | 1162 | class = (JobClass *)nih_hash_search (job_classes, name, prev); | ||
2445 | 1163 | if (! class) | ||
2446 | 1164 | return NULL; | ||
2447 | 1165 | if (class && class->session == session) | ||
2448 | 1166 | return class; | ||
2449 | 1167 | |||
2450 | 1168 | nih_assert (class); | ||
2451 | 1169 | |||
2452 | 1170 | prev = (NihList *)class; | ||
2453 | 1171 | } while (TRUE); | ||
2454 | 1172 | |||
2455 | 1173 | nih_assert_not_reached (); | ||
2456 | 1174 | } | ||
2457 | 1175 | |||
2458 | 1115 | 1176 | ||
2459 | 1116 | /** | 1177 | /** |
2460 | 1117 | * job_class_get_name: | 1178 | * job_class_get_name: |
2461 | @@ -1500,3 +1561,651 @@ | |||
2462 | 1500 | 1561 | ||
2463 | 1501 | return 0; | 1562 | return 0; |
2464 | 1502 | } | 1563 | } |
2465 | 1564 | |||
2466 | 1565 | |||
2467 | 1566 | /** | ||
2468 | 1567 | * job_class_serialise: | ||
2469 | 1568 | * @class: job class to serialise. | ||
2470 | 1569 | * | ||
2471 | 1570 | * Convert @class into a JSON representation for serialisation. | ||
2472 | 1571 | * Caller must free returned value using json_object_put(). | ||
2473 | 1572 | * | ||
2474 | 1573 | * Returns: JSON-serialised JobClass object, or NULL on error. | ||
2475 | 1574 | **/ | ||
2476 | 1575 | json_object * | ||
2477 | 1576 | job_class_serialise (const JobClass *class) | ||
2478 | 1577 | { | ||
2479 | 1578 | json_object *json; | ||
2480 | 1579 | json_object *json_export; | ||
2481 | 1580 | json_object *json_emits; | ||
2482 | 1581 | json_object *json_processes; | ||
2483 | 1582 | json_object *json_normalexit; | ||
2484 | 1583 | json_object *json_limits; | ||
2485 | 1584 | json_object *json_jobs; | ||
2486 | 1585 | nih_local char *start_on = NULL; | ||
2487 | 1586 | nih_local char *stop_on = NULL; | ||
2488 | 1587 | int session_index; | ||
2489 | 1588 | |||
2490 | 1589 | nih_assert (class); | ||
2491 | 1590 | nih_assert (job_classes); | ||
2492 | 1591 | |||
2493 | 1592 | json = json_object_new_object (); | ||
2494 | 1593 | if (! json) | ||
2495 | 1594 | return NULL; | ||
2496 | 1595 | |||
2497 | 1596 | session_index = session_get_index (class->session); | ||
2498 | 1597 | if (session_index < 0) | ||
2499 | 1598 | goto error; | ||
2500 | 1599 | |||
2501 | 1600 | if (! state_set_json_int_var (json, "session", session_index)) | ||
2502 | 1601 | goto error; | ||
2503 | 1602 | |||
2504 | 1603 | if (! state_set_json_string_var_from_obj (json, class, name)) | ||
2505 | 1604 | goto error; | ||
2506 | 1605 | |||
2507 | 1606 | if (! state_set_json_string_var_from_obj (json, class, path)) | ||
2508 | 1607 | goto error; | ||
2509 | 1608 | |||
2510 | 1609 | if (! state_set_json_string_var_from_obj (json, class, instance)) | ||
2511 | 1610 | goto error; | ||
2512 | 1611 | |||
2513 | 1612 | json_jobs = job_serialise_all (class->instances); | ||
2514 | 1613 | |||
2515 | 1614 | if (! json_jobs) | ||
2516 | 1615 | goto error; | ||
2517 | 1616 | |||
2518 | 1617 | json_object_object_add (json, "jobs", json_jobs); | ||
2519 | 1618 | |||
2520 | 1619 | if (! state_set_json_string_var_from_obj (json, class, description)) | ||
2521 | 1620 | goto error; | ||
2522 | 1621 | |||
2523 | 1622 | if (! state_set_json_string_var_from_obj (json, class, author)) | ||
2524 | 1623 | goto error; | ||
2525 | 1624 | |||
2526 | 1625 | if (! state_set_json_string_var_from_obj (json, class, version)) | ||
2527 | 1626 | goto error; | ||
2528 | 1627 | |||
2529 | 1628 | if (! state_set_json_str_array_from_obj (json, class, env)) | ||
2530 | 1629 | goto error; | ||
2531 | 1630 | |||
2532 | 1631 | json_export = class->export | ||
2533 | 1632 | ? state_serialise_str_array (class->export) | ||
2534 | 1633 | : json_object_new_array (); | ||
2535 | 1634 | |||
2536 | 1635 | if (! json_export) | ||
2537 | 1636 | goto error; | ||
2538 | 1637 | json_object_object_add (json, "export", json_export); | ||
2539 | 1638 | |||
2540 | 1639 | if (class->start_on) { | ||
2541 | 1640 | start_on = event_operator_collapse (class->start_on); | ||
2542 | 1641 | if (! start_on) | ||
2543 | 1642 | goto error; | ||
2544 | 1643 | |||
2545 | 1644 | if (! state_set_json_string_var (json, "start_on", start_on)) | ||
2546 | 1645 | goto error; | ||
2547 | 1646 | } | ||
2548 | 1647 | |||
2549 | 1648 | if (class->stop_on) { | ||
2550 | 1649 | stop_on = event_operator_collapse (class->stop_on); | ||
2551 | 1650 | if (! stop_on) | ||
2552 | 1651 | goto error; | ||
2553 | 1652 | |||
2554 | 1653 | if (! state_set_json_string_var (json, "stop_on", stop_on)) | ||
2555 | 1654 | goto error; | ||
2556 | 1655 | } | ||
2557 | 1656 | |||
2558 | 1657 | json_emits = class->emits | ||
2559 | 1658 | ? state_serialise_str_array (class->emits) | ||
2560 | 1659 | : json_object_new_array (); | ||
2561 | 1660 | |||
2562 | 1661 | if (! json_emits) | ||
2563 | 1662 | goto error; | ||
2564 | 1663 | json_object_object_add (json, "emits", json_emits); | ||
2565 | 1664 | |||
2566 | 1665 | json_processes = process_serialise_all ( | ||
2567 | 1666 | (const Process const * const * const)class->process); | ||
2568 | 1667 | if (! json_processes) | ||
2569 | 1668 | goto error; | ||
2570 | 1669 | json_object_object_add (json, "process", json_processes); | ||
2571 | 1670 | |||
2572 | 1671 | if (! state_set_json_enum_var (json, | ||
2573 | 1672 | job_class_expect_type_enum_to_str, | ||
2574 | 1673 | "expect", class->expect)) | ||
2575 | 1674 | goto error; | ||
2576 | 1675 | |||
2577 | 1676 | if (! state_set_json_int_var_from_obj (json, class, task)) | ||
2578 | 1677 | goto error; | ||
2579 | 1678 | |||
2580 | 1679 | if (! state_set_json_int_var_from_obj (json, class, kill_timeout)) | ||
2581 | 1680 | goto error; | ||
2582 | 1681 | |||
2583 | 1682 | if (! state_set_json_int_var_from_obj (json, class, kill_signal)) | ||
2584 | 1683 | goto error; | ||
2585 | 1684 | |||
2586 | 1685 | if (! state_set_json_int_var_from_obj (json, class, respawn)) | ||
2587 | 1686 | goto error; | ||
2588 | 1687 | |||
2589 | 1688 | if (! state_set_json_int_var_from_obj (json, class, respawn_limit)) | ||
2590 | 1689 | goto error; | ||
2591 | 1690 | |||
2592 | 1691 | if (! state_set_json_int_var_from_obj (json, class, respawn_interval)) | ||
2593 | 1692 | goto error; | ||
2594 | 1693 | |||
2595 | 1694 | json_normalexit = state_serialise_int_array (int, class->normalexit, | ||
2596 | 1695 | class->normalexit_len); | ||
2597 | 1696 | if (! json_normalexit) | ||
2598 | 1697 | goto error; | ||
2599 | 1698 | |||
2600 | 1699 | json_object_object_add (json, "normalexit", json_normalexit); | ||
2601 | 1700 | |||
2602 | 1701 | if (! state_set_json_enum_var (json, | ||
2603 | 1702 | job_class_console_type_enum_to_str, | ||
2604 | 1703 | "console", class->console)) | ||
2605 | 1704 | goto error; | ||
2606 | 1705 | |||
2607 | 1706 | if (! state_set_json_int_var_from_obj (json, class, umask)) | ||
2608 | 1707 | goto error; | ||
2609 | 1708 | |||
2610 | 1709 | if (! state_set_json_int_var_from_obj (json, class, nice)) | ||
2611 | 1710 | goto error; | ||
2612 | 1711 | |||
2613 | 1712 | if (! state_set_json_int_var_from_obj (json, class, oom_score_adj)) | ||
2614 | 1713 | goto error; | ||
2615 | 1714 | |||
2616 | 1715 | json_limits = state_rlimit_serialise_all (class->limits); | ||
2617 | 1716 | if (! json_limits) | ||
2618 | 1717 | goto error; | ||
2619 | 1718 | json_object_object_add (json, "limits", json_limits); | ||
2620 | 1719 | |||
2621 | 1720 | if (! state_set_json_string_var_from_obj (json, class, chroot)) | ||
2622 | 1721 | goto error; | ||
2623 | 1722 | |||
2624 | 1723 | if (! state_set_json_string_var_from_obj (json, class, chdir)) | ||
2625 | 1724 | goto error; | ||
2626 | 1725 | |||
2627 | 1726 | if (! state_set_json_string_var_from_obj (json, class, setuid)) | ||
2628 | 1727 | goto error; | ||
2629 | 1728 | |||
2630 | 1729 | if (! state_set_json_string_var_from_obj (json, class, setgid)) | ||
2631 | 1730 | goto error; | ||
2632 | 1731 | |||
2633 | 1732 | if (! state_set_json_int_var_from_obj (json, class, deleted)) | ||
2634 | 1733 | goto error; | ||
2635 | 1734 | |||
2636 | 1735 | if (! state_set_json_int_var_from_obj (json, class, debug)) | ||
2637 | 1736 | goto error; | ||
2638 | 1737 | |||
2639 | 1738 | if (! state_set_json_string_var_from_obj (json, class, usage)) | ||
2640 | 1739 | goto error; | ||
2641 | 1740 | |||
2642 | 1741 | return json; | ||
2643 | 1742 | |||
2644 | 1743 | error: | ||
2645 | 1744 | json_object_put (json); | ||
2646 | 1745 | return NULL; | ||
2647 | 1746 | } | ||
2648 | 1747 | |||
2649 | 1748 | /** | ||
2650 | 1749 | * job_class_serialise_all: | ||
2651 | 1750 | * | ||
2652 | 1751 | * Convert existing JobClass objects to JSON representation. | ||
2653 | 1752 | * | ||
2654 | 1753 | * Returns: JSON object containing array of JobClass objects, | ||
2655 | 1754 | * or NULL on error. | ||
2656 | 1755 | **/ | ||
2657 | 1756 | json_object * | ||
2658 | 1757 | job_class_serialise_all (void) | ||
2659 | 1758 | { | ||
2660 | 1759 | json_object *json; | ||
2661 | 1760 | |||
2662 | 1761 | job_class_init (); | ||
2663 | 1762 | |||
2664 | 1763 | json = json_object_new_array (); | ||
2665 | 1764 | if (! json) | ||
2666 | 1765 | return NULL; | ||
2667 | 1766 | |||
2668 | 1767 | NIH_HASH_FOREACH (job_classes, iter) { | ||
2669 | 1768 | json_object *json_class; | ||
2670 | 1769 | JobClass *class = (JobClass *)iter; | ||
2671 | 1770 | |||
2672 | 1771 | json_class = job_class_serialise (class); | ||
2673 | 1772 | |||
2674 | 1773 | /* No object returned means the class doesn't need to be | ||
2675 | 1774 | * serialised. Even if this is a real failure, it's always | ||
2676 | 1775 | * better to serialise as much of the state as possible. | ||
2677 | 1776 | */ | ||
2678 | 1777 | if (! json_class) | ||
2679 | 1778 | continue; | ||
2680 | 1779 | |||
2681 | 1780 | json_object_array_add (json, json_class); | ||
2682 | 1781 | } | ||
2683 | 1782 | |||
2684 | 1783 | return json; | ||
2685 | 1784 | } | ||
2686 | 1785 | |||
2687 | 1786 | /** | ||
2688 | 1787 | * job_class_deserialise: | ||
2689 | 1788 | * @json: JSON-serialised JobClass object to deserialise. | ||
2690 | 1789 | * | ||
2691 | 1790 | * Create JobClass from provided JSON and add to the | ||
2692 | 1791 | * job classes table. | ||
2693 | 1792 | * | ||
2694 | 1793 | * Returns: JobClass object, or NULL on error. | ||
2695 | 1794 | **/ | ||
2696 | 1795 | JobClass * | ||
2697 | 1796 | job_class_deserialise (json_object *json) | ||
2698 | 1797 | { | ||
2699 | 1798 | json_object *json_normalexit; | ||
2700 | 1799 | JobClass *class = NULL; | ||
2701 | 1800 | int session_index = -1; | ||
2702 | 1801 | int ret; | ||
2703 | 1802 | nih_local char *name = NULL; | ||
2704 | 1803 | nih_local char *path = NULL; | ||
2705 | 1804 | |||
2706 | 1805 | nih_assert (json); | ||
2707 | 1806 | nih_assert (job_classes); | ||
2708 | 1807 | |||
2709 | 1808 | if (! state_check_json_type (json, object)) | ||
2710 | 1809 | goto error; | ||
2711 | 1810 | |||
2712 | 1811 | if (! state_get_json_int_var (json, "session", session_index)) | ||
2713 | 1812 | goto error; | ||
2714 | 1813 | |||
2715 | 1814 | if (session_index < 0) | ||
2716 | 1815 | goto error; | ||
2717 | 1816 | |||
2718 | 1817 | if (! state_get_json_string_var_strict (json, "name", NULL, name)) | ||
2719 | 1818 | goto error; | ||
2720 | 1819 | |||
2721 | 1820 | class = NIH_MUST (job_class_new (NULL, name, | ||
2722 | 1821 | session_from_index (session_index))); | ||
2723 | 1822 | if (! class) | ||
2724 | 1823 | goto error; | ||
2725 | 1824 | |||
2726 | 1825 | if (class->session != NULL) { | ||
2727 | 1826 | nih_warn ("XXX: WARNING (%s:%d): deserialisation of " | ||
2728 | 1827 | "user jobs and chroot sessions not currently supported", | ||
2729 | 1828 | __func__, __LINE__); | ||
2730 | 1829 | goto error; | ||
2731 | 1830 | } | ||
2732 | 1831 | |||
2733 | 1832 | /* job_class_new() sets path */ | ||
2734 | 1833 | if (! state_get_json_string_var_strict (json, "path", NULL, path)) | ||
2735 | 1834 | goto error; | ||
2736 | 1835 | |||
2737 | 1836 | nih_assert (! strcmp (class->path, path)); | ||
2738 | 1837 | |||
2739 | 1838 | /* Discard default instance as we're about to be handed a fresh | ||
2740 | 1839 | * string from the JSON. | ||
2741 | 1840 | */ | ||
2742 | 1841 | nih_free (class->instance); | ||
2743 | 1842 | |||
2744 | 1843 | if (! state_get_json_string_var_to_obj (json, class, instance)) | ||
2745 | 1844 | goto error; | ||
2746 | 1845 | |||
2747 | 1846 | if (! state_get_json_string_var_to_obj (json, class, description)) | ||
2748 | 1847 | goto error; | ||
2749 | 1848 | |||
2750 | 1849 | if (! state_get_json_string_var_to_obj (json, class, author)) | ||
2751 | 1850 | goto error; | ||
2752 | 1851 | |||
2753 | 1852 | if (! state_get_json_string_var_to_obj (json, class, version)) | ||
2754 | 1853 | goto error; | ||
2755 | 1854 | |||
2756 | 1855 | if (! state_get_json_env_array_to_obj (json, class, env)) | ||
2757 | 1856 | goto error; | ||
2758 | 1857 | |||
2759 | 1858 | if (! state_get_json_env_array_to_obj (json, class, export)) | ||
2760 | 1859 | goto error; | ||
2761 | 1860 | |||
2762 | 1861 | /* start and stop conditions are optional */ | ||
2763 | 1862 | if (json_object_object_get (json, "start_on")) { | ||
2764 | 1863 | nih_local char *start_on = NULL; | ||
2765 | 1864 | |||
2766 | 1865 | if (! state_get_json_string_var_strict (json, "start_on", NULL, start_on)) | ||
2767 | 1866 | goto error; | ||
2768 | 1867 | |||
2769 | 1868 | if (*start_on) { | ||
2770 | 1869 | class->start_on = parse_on_simple (class, "start", start_on); | ||
2771 | 1870 | if (! class->start_on) { | ||
2772 | 1871 | NihError *err; | ||
2773 | 1872 | |||
2774 | 1873 | err = nih_error_get (); | ||
2775 | 1874 | |||
2776 | 1875 | nih_error ("%s %s: %s", | ||
2777 | 1876 | _("BUG"), | ||
2778 | 1877 | _("'start on' parse error"), | ||
2779 | 1878 | err->message); | ||
2780 | 1879 | |||
2781 | 1880 | nih_free (err); | ||
2782 | 1881 | |||
2783 | 1882 | goto error; | ||
2784 | 1883 | } | ||
2785 | 1884 | } | ||
2786 | 1885 | } | ||
2787 | 1886 | |||
2788 | 1887 | if (json_object_object_get (json, "stop_on")) { | ||
2789 | 1888 | nih_local char *stop_on = NULL; | ||
2790 | 1889 | |||
2791 | 1890 | if (! state_get_json_string_var_strict (json, "stop_on", NULL, stop_on)) | ||
2792 | 1891 | goto error; | ||
2793 | 1892 | |||
2794 | 1893 | if (*stop_on) { | ||
2795 | 1894 | class->stop_on = parse_on_simple (class, "stop", stop_on); | ||
2796 | 1895 | if (! class->stop_on) { | ||
2797 | 1896 | NihError *err; | ||
2798 | 1897 | |||
2799 | 1898 | err = nih_error_get (); | ||
2800 | 1899 | |||
2801 | 1900 | nih_error ("%s %s: %s", | ||
2802 | 1901 | _("BUG"), | ||
2803 | 1902 | _("'stop on' parse error"), | ||
2804 | 1903 | err->message); | ||
2805 | 1904 | |||
2806 | 1905 | nih_free (err); | ||
2807 | 1906 | |||
2808 | 1907 | goto error; | ||
2809 | 1908 | } | ||
2810 | 1909 | } | ||
2811 | 1910 | } | ||
2812 | 1911 | |||
2813 | 1912 | if (! state_get_json_str_array_to_obj (json, class, emits)) | ||
2814 | 1913 | goto error; | ||
2815 | 1914 | |||
2816 | 1915 | if (! state_get_json_enum_var (json, | ||
2817 | 1916 | job_class_expect_type_str_to_enum, | ||
2818 | 1917 | "expect", class->expect)) | ||
2819 | 1918 | goto error; | ||
2820 | 1919 | |||
2821 | 1920 | if (! state_get_json_int_var_to_obj (json, class, task)) | ||
2822 | 1921 | goto error; | ||
2823 | 1922 | |||
2824 | 1923 | if (! state_get_json_int_var_to_obj (json, class, kill_timeout)) | ||
2825 | 1924 | goto error; | ||
2826 | 1925 | |||
2827 | 1926 | if (! state_get_json_int_var_to_obj (json, class, kill_signal)) | ||
2828 | 1927 | goto error; | ||
2829 | 1928 | |||
2830 | 1929 | if (! state_get_json_int_var_to_obj (json, class, respawn)) | ||
2831 | 1930 | goto error; | ||
2832 | 1931 | |||
2833 | 1932 | if (! state_get_json_int_var_to_obj (json, class, respawn_limit)) | ||
2834 | 1933 | goto error; | ||
2835 | 1934 | |||
2836 | 1935 | if (! state_get_json_int_var_to_obj (json, class, respawn_interval)) | ||
2837 | 1936 | goto error; | ||
2838 | 1937 | |||
2839 | 1938 | if (! state_get_json_enum_var (json, | ||
2840 | 1939 | job_class_console_type_str_to_enum, | ||
2841 | 1940 | "console", class->console)) | ||
2842 | 1941 | goto error; | ||
2843 | 1942 | |||
2844 | 1943 | if (! state_get_json_int_var_to_obj (json, class, umask)) | ||
2845 | 1944 | goto error; | ||
2846 | 1945 | |||
2847 | 1946 | if (! state_get_json_int_var_to_obj (json, class, nice)) | ||
2848 | 1947 | goto error; | ||
2849 | 1948 | |||
2850 | 1949 | if (! state_get_json_int_var_to_obj (json, class, oom_score_adj)) | ||
2851 | 1950 | goto error; | ||
2852 | 1951 | |||
2853 | 1952 | if (! state_get_json_string_var_to_obj (json, class, chroot)) | ||
2854 | 1953 | goto error; | ||
2855 | 1954 | |||
2856 | 1955 | if (! state_get_json_string_var_to_obj (json, class, chdir)) | ||
2857 | 1956 | goto error; | ||
2858 | 1957 | |||
2859 | 1958 | if (! state_get_json_string_var_to_obj (json, class, setuid)) | ||
2860 | 1959 | goto error; | ||
2861 | 1960 | |||
2862 | 1961 | if (! state_get_json_string_var_to_obj (json, class, setgid)) | ||
2863 | 1962 | goto error; | ||
2864 | 1963 | |||
2865 | 1964 | if (! state_get_json_int_var_to_obj (json, class, deleted)) | ||
2866 | 1965 | goto error; | ||
2867 | 1966 | |||
2868 | 1967 | if (! state_get_json_int_var_to_obj (json, class, debug)) | ||
2869 | 1968 | goto error; | ||
2870 | 1969 | |||
2871 | 1970 | if (! state_get_json_string_var_to_obj (json, class, usage)) | ||
2872 | 1971 | goto error; | ||
2873 | 1972 | |||
2874 | 1973 | json_normalexit = json_object_object_get (json, "normalexit"); | ||
2875 | 1974 | if (! json_normalexit) | ||
2876 | 1975 | goto error; | ||
2877 | 1976 | |||
2878 | 1977 | ret = state_deserialise_int_array (class, json_normalexit, | ||
2879 | 1978 | int, &class->normalexit, &class->normalexit_len); | ||
2880 | 1979 | if (ret < 0) | ||
2881 | 1980 | goto error; | ||
2882 | 1981 | |||
2883 | 1982 | if (state_rlimit_deserialise_all (json, class, &class->limits) < 0) | ||
2884 | 1983 | goto error; | ||
2885 | 1984 | |||
2886 | 1985 | if (process_deserialise_all (json, class->process, class->process) < 0) | ||
2887 | 1986 | goto error; | ||
2888 | 1987 | |||
2889 | 1988 | /* Force class to be known. | ||
2890 | 1989 | * | ||
2891 | 1990 | * We cannot use job_class_*consider() since the | ||
2892 | 1991 | * JobClasses have no associated ConfFile. | ||
2893 | 1992 | */ | ||
2894 | 1993 | job_class_add_safe (class); | ||
2895 | 1994 | |||
2896 | 1995 | /* Any jobs must be added after the class is registered | ||
2897 | 1996 | * (since you cannot add a job to a partially-created | ||
2898 | 1997 | * class). | ||
2899 | 1998 | */ | ||
2900 | 1999 | if (job_deserialise_all (class, json) < 0) | ||
2901 | 2000 | goto error; | ||
2902 | 2001 | |||
2903 | 2002 | return class; | ||
2904 | 2003 | |||
2905 | 2004 | error: | ||
2906 | 2005 | nih_free (class); | ||
2907 | 2006 | return NULL; | ||
2908 | 2007 | } | ||
2909 | 2008 | |||
2910 | 2009 | /** | ||
2911 | 2010 | * job_class_deserialise_all: | ||
2912 | 2011 | * | ||
2913 | 2012 | * @json: root of JSON-serialised state. | ||
2914 | 2013 | * | ||
2915 | 2014 | * Convert JSON representation of JobClasses back into JobClass objects. | ||
2916 | 2015 | * | ||
2917 | 2016 | * Returns: 0 on success, -1 on error. | ||
2918 | 2017 | **/ | ||
2919 | 2018 | int | ||
2920 | 2019 | job_class_deserialise_all (json_object *json) | ||
2921 | 2020 | { | ||
2922 | 2021 | JobClass *class = NULL; | ||
2923 | 2022 | |||
2924 | 2023 | nih_assert (json); | ||
2925 | 2024 | |||
2926 | 2025 | job_class_init (); | ||
2927 | 2026 | |||
2928 | 2027 | json_classes = json_object_object_get (json, "job_classes"); | ||
2929 | 2028 | |||
2930 | 2029 | if (! json_classes) | ||
2931 | 2030 | goto error; | ||
2932 | 2031 | |||
2933 | 2032 | if (! state_check_json_type (json_classes, array)) | ||
2934 | 2033 | goto error; | ||
2935 | 2034 | |||
2936 | 2035 | for (int i = 0; i < json_object_array_length (json_classes); i++) { | ||
2937 | 2036 | json_object *json_class; | ||
2938 | 2037 | |||
2939 | 2038 | json_class = json_object_array_get_idx (json_classes, i); | ||
2940 | 2039 | if (! json_class) | ||
2941 | 2040 | goto error; | ||
2942 | 2041 | |||
2943 | 2042 | if (! state_check_json_type (json_class, object)) | ||
2944 | 2043 | goto error; | ||
2945 | 2044 | |||
2946 | 2045 | class = job_class_deserialise (json_class); | ||
2947 | 2046 | if (! class) | ||
2948 | 2047 | goto error; | ||
2949 | 2048 | |||
2950 | 2049 | /* FIXME: | ||
2951 | 2050 | * | ||
2952 | 2051 | * If user sessions exist (ie 'initctl --session list' | ||
2953 | 2052 | * has been run), we get this failure: | ||
2954 | 2053 | * | ||
2955 | 2054 | * serialised path='/com/ubuntu/Upstart/jobs/1000/bang' | ||
2956 | 2055 | * path set by job_class_new()='/com/ubuntu/Upstart/jobs/_/1000/bang' | ||
2957 | 2056 | * | ||
2958 | 2057 | */ | ||
2959 | 2058 | |||
2960 | 2059 | } | ||
2961 | 2060 | |||
2962 | 2061 | return 0; | ||
2963 | 2062 | |||
2964 | 2063 | error: | ||
2965 | 2064 | if (class) | ||
2966 | 2065 | nih_free (class); | ||
2967 | 2066 | |||
2968 | 2067 | return -1; | ||
2969 | 2068 | } | ||
2970 | 2069 | |||
2971 | 2070 | |||
2972 | 2071 | /** | ||
2973 | 2072 | * job_class_expect_type_enum_to_str: | ||
2974 | 2073 | * | ||
2975 | 2074 | * @expect: ExpectType. | ||
2976 | 2075 | * | ||
2977 | 2076 | * Convert ExpectType to a string representation. | ||
2978 | 2077 | * | ||
2979 | 2078 | * Returns: string representation of @expect, or NULL if not known. | ||
2980 | 2079 | **/ | ||
2981 | 2080 | const char * | ||
2982 | 2081 | job_class_expect_type_enum_to_str (ExpectType expect) | ||
2983 | 2082 | { | ||
2984 | 2083 | state_enum_to_str (EXPECT_NONE, expect); | ||
2985 | 2084 | state_enum_to_str (EXPECT_STOP, expect); | ||
2986 | 2085 | state_enum_to_str (EXPECT_DAEMON, expect); | ||
2987 | 2086 | state_enum_to_str (EXPECT_FORK, expect); | ||
2988 | 2087 | |||
2989 | 2088 | return NULL; | ||
2990 | 2089 | } | ||
2991 | 2090 | |||
2992 | 2091 | /** | ||
2993 | 2092 | * job_class_expect_type_str_to_enum: | ||
2994 | 2093 | * | ||
2995 | 2094 | * @expect: string ExpectType value. | ||
2996 | 2095 | * | ||
2997 | 2096 | * Convert @expect back into an enum value. | ||
2998 | 2097 | * | ||
2999 | 2098 | * Returns: ExpectType representing @expect, or -1 if not known. | ||
3000 | 2099 | **/ | ||
3001 | 2100 | ExpectType | ||
3002 | 2101 | job_class_expect_type_str_to_enum (const char *expect) | ||
3003 | 2102 | { | ||
3004 | 2103 | nih_assert (expect); | ||
3005 | 2104 | |||
3006 | 2105 | state_str_to_enum (EXPECT_NONE, expect); | ||
3007 | 2106 | state_str_to_enum (EXPECT_STOP, expect); | ||
3008 | 2107 | state_str_to_enum (EXPECT_DAEMON, expect); | ||
3009 | 2108 | state_str_to_enum (EXPECT_FORK, expect); | ||
3010 | 2109 | |||
3011 | 2110 | return -1; | ||
3012 | 2111 | } | ||
3013 | 2112 | |||
3014 | 2113 | /** | ||
3015 | 2114 | * job_class_console_type_enum_to_str: | ||
3016 | 2115 | * | ||
3017 | 2116 | * @console: ConsoleType. | ||
3018 | 2117 | * | ||
3019 | 2118 | * Convert ConsoleType to a string representation. | ||
3020 | 2119 | * | ||
3021 | 2120 | * Returns: string representation of @console, or NULL if not known. | ||
3022 | 2121 | **/ | ||
3023 | 2122 | const char * | ||
3024 | 2123 | job_class_console_type_enum_to_str (ConsoleType console) | ||
3025 | 2124 | { | ||
3026 | 2125 | state_enum_to_str (CONSOLE_NONE, console); | ||
3027 | 2126 | state_enum_to_str (CONSOLE_OUTPUT, console); | ||
3028 | 2127 | state_enum_to_str (CONSOLE_OWNER, console); | ||
3029 | 2128 | state_enum_to_str (CONSOLE_LOG, console); | ||
3030 | 2129 | |||
3031 | 2130 | return NULL; | ||
3032 | 2131 | } | ||
3033 | 2132 | |||
3034 | 2133 | /** | ||
3035 | 2134 | * job_class_console_type_str_to_enum: | ||
3036 | 2135 | * | ||
3037 | 2136 | * @console: string ConsoleType value. | ||
3038 | 2137 | * | ||
3039 | 2138 | * Convert @console back into enum value. | ||
3040 | 2139 | * | ||
3041 | 2140 | * Returns: ExpectType representing @console, or -1 if not known. | ||
3042 | 2141 | **/ | ||
3043 | 2142 | ConsoleType | ||
3044 | 2143 | job_class_console_type_str_to_enum (const char *console) | ||
3045 | 2144 | { | ||
3046 | 2145 | if (! console) | ||
3047 | 2146 | goto error; | ||
3048 | 2147 | |||
3049 | 2148 | state_str_to_enum (CONSOLE_NONE, console); | ||
3050 | 2149 | state_str_to_enum (CONSOLE_OUTPUT, console); | ||
3051 | 2150 | state_str_to_enum (CONSOLE_OWNER, console); | ||
3052 | 2151 | state_str_to_enum (CONSOLE_LOG, console); | ||
3053 | 2152 | |||
3054 | 2153 | error: | ||
3055 | 2154 | return -1; | ||
3056 | 2155 | } | ||
3057 | 2156 | |||
3058 | 2157 | /** | ||
3059 | 2158 | * job_class_prepare_reexec: | ||
3060 | 2159 | * | ||
3061 | 2160 | * Prepare for a re-exec by clearing the CLOEXEC bit on all log object | ||
3062 | 2161 | * file descriptors associated with their parent jobs. | ||
3063 | 2162 | **/ | ||
3064 | 2163 | void | ||
3065 | 2164 | job_class_prepare_reexec (void) | ||
3066 | 2165 | { | ||
3067 | 2166 | job_class_init (); | ||
3068 | 2167 | |||
3069 | 2168 | NIH_HASH_FOREACH (job_classes, iter) { | ||
3070 | 2169 | JobClass *class = (JobClass *)iter; | ||
3071 | 2170 | |||
3072 | 2171 | NIH_HASH_FOREACH (class->instances, job_iter) { | ||
3073 | 2172 | Job *job = (Job *)job_iter; | ||
3074 | 2173 | |||
3075 | 2174 | nih_assert (job->log); | ||
3076 | 2175 | |||
3077 | 2176 | for (int process = 0; process < PROCESS_LAST; process++) { | ||
3078 | 2177 | int fd; | ||
3079 | 2178 | Log *log; | ||
3080 | 2179 | |||
3081 | 2180 | log = job->log[process]; | ||
3082 | 2181 | |||
3083 | 2182 | /* No associated job process or logger has detected | ||
3084 | 2183 | * remote end of pty has closed. | ||
3085 | 2184 | */ | ||
3086 | 2185 | if (! log || ! log->io) | ||
3087 | 2186 | continue; | ||
3088 | 2187 | |||
3089 | 2188 | nih_assert (log->io->watch); | ||
3090 | 2189 | |||
3091 | 2190 | fd = log->io->watch->fd; | ||
3092 | 2191 | if (fd < 0) | ||
3093 | 2192 | continue; | ||
3094 | 2193 | |||
3095 | 2194 | if (state_toggle_cloexec (fd, FALSE) < 0) | ||
3096 | 2195 | goto error; | ||
3097 | 2196 | |||
3098 | 2197 | fd = log->fd; | ||
3099 | 2198 | if (fd < 0) | ||
3100 | 2199 | continue; | ||
3101 | 2200 | |||
3102 | 2201 | if (state_toggle_cloexec (fd, FALSE) < 0) | ||
3103 | 2202 | goto error; | ||
3104 | 2203 | } | ||
3105 | 2204 | } | ||
3106 | 2205 | } | ||
3107 | 2206 | |||
3108 | 2207 | return; | ||
3109 | 2208 | |||
3110 | 2209 | error: | ||
3111 | 2210 | nih_warn (_("unable to clear CLOEXEC bit on log fd")); | ||
3112 | 2211 | } | ||
3113 | 1503 | 2212 | ||
3114 | === modified file 'init/job_class.h' | |||
3115 | --- init/job_class.h 2012-02-16 15:45:41 +0000 | |||
3116 | +++ init/job_class.h 2012-11-14 14:49:20 +0000 | |||
3117 | @@ -196,7 +196,7 @@ | |||
3118 | 196 | int task; | 196 | int task; |
3119 | 197 | 197 | ||
3120 | 198 | time_t kill_timeout; | 198 | time_t kill_timeout; |
3122 | 199 | int kill_signal; | 199 | int kill_signal; |
3123 | 200 | 200 | ||
3124 | 201 | int respawn; | 201 | int respawn; |
3125 | 202 | int respawn_limit; | 202 | int respawn_limit; |
3126 | @@ -238,6 +238,8 @@ | |||
3127 | 238 | int job_class_consider (JobClass *class); | 238 | int job_class_consider (JobClass *class); |
3128 | 239 | int job_class_reconsider (JobClass *class); | 239 | int job_class_reconsider (JobClass *class); |
3129 | 240 | 240 | ||
3130 | 241 | void job_class_add_safe (JobClass *class); | ||
3131 | 242 | |||
3132 | 241 | void job_class_register (JobClass *class, | 243 | void job_class_register (JobClass *class, |
3133 | 242 | DBusConnection *conn, int signal); | 244 | DBusConnection *conn, int signal); |
3134 | 243 | void job_class_unregister (JobClass *class, | 245 | void job_class_unregister (JobClass *class, |
3135 | @@ -306,9 +308,42 @@ | |||
3136 | 306 | NihDBusMessage *message, | 308 | NihDBusMessage *message, |
3137 | 307 | char **usage); | 309 | char **usage); |
3138 | 308 | 310 | ||
3139 | 311 | const char * | ||
3140 | 312 | job_class_console_type_enum_to_str (ConsoleType console) | ||
3141 | 313 | __attribute__ ((warn_unused_result)); | ||
3142 | 314 | |||
3143 | 315 | ConsoleType | ||
3144 | 316 | job_class_console_type_str_to_enum (const char *name) | ||
3145 | 317 | __attribute__ ((warn_unused_result)); | ||
3146 | 318 | |||
3147 | 319 | const char * | ||
3148 | 320 | job_class_expect_type_enum_to_str (ExpectType expect) | ||
3149 | 321 | __attribute__ ((warn_unused_result)); | ||
3150 | 322 | |||
3151 | 323 | ExpectType | ||
3152 | 324 | job_class_expect_type_str_to_enum (const char *name) | ||
3153 | 325 | __attribute__ ((warn_unused_result)); | ||
3154 | 326 | |||
3155 | 309 | ConsoleType job_class_console_type (const char *console) | 327 | ConsoleType job_class_console_type (const char *console) |
3156 | 310 | __attribute__ ((warn_unused_result)); | 328 | __attribute__ ((warn_unused_result)); |
3157 | 311 | 329 | ||
3158 | 330 | json_object *job_class_serialise (const JobClass *class) | ||
3159 | 331 | __attribute__ ((warn_unused_result, malloc)); | ||
3160 | 332 | |||
3161 | 333 | JobClass *job_class_deserialise (json_object *json) | ||
3162 | 334 | __attribute__ ((malloc, warn_unused_result)); | ||
3163 | 335 | |||
3164 | 336 | json_object * job_class_serialise_all (void) | ||
3165 | 337 | __attribute__ ((warn_unused_result, malloc)); | ||
3166 | 338 | |||
3167 | 339 | int job_class_deserialise_all (json_object *json) | ||
3168 | 340 | __attribute__ ((warn_unused_result)); | ||
3169 | 341 | |||
3170 | 342 | JobClass * job_class_get (const char *name, Session *session) | ||
3171 | 343 | __attribute__ ((warn_unused_result)); | ||
3172 | 344 | |||
3173 | 345 | void job_class_prepare_reexec (void); | ||
3174 | 346 | |||
3175 | 312 | NIH_END_EXTERN | 347 | NIH_END_EXTERN |
3176 | 313 | 348 | ||
3177 | 314 | #endif /* INIT_JOB_CLASS_H */ | 349 | #endif /* INIT_JOB_CLASS_H */ |
3178 | 315 | 350 | ||
3179 | === modified file 'init/job_process.c' | |||
3180 | --- init/job_process.c 2012-08-02 08:57:20 +0000 | |||
3181 | +++ init/job_process.c 2012-11-14 14:49:20 +0000 | |||
3182 | @@ -637,7 +637,7 @@ | |||
3183 | 637 | */ | 637 | */ |
3184 | 638 | setsid (); | 638 | setsid (); |
3185 | 639 | 639 | ||
3187 | 640 | /* Set the process environment from the function paramters. */ | 640 | /* Set the process environment from the function parameters. */ |
3188 | 641 | environ = (char **)env; | 641 | environ = (char **)env; |
3189 | 642 | 642 | ||
3190 | 643 | /* Handle unprivileged user job by dropping privileges to | 643 | /* Handle unprivileged user job by dropping privileges to |
3191 | @@ -1226,7 +1226,7 @@ | |||
3192 | 1226 | nih_assert (job != NULL); | 1226 | nih_assert (job != NULL); |
3193 | 1227 | nih_assert (job->pid[process] > 0); | 1227 | nih_assert (job->pid[process] > 0); |
3194 | 1228 | nih_assert (job->kill_timer == NULL); | 1228 | nih_assert (job->kill_timer == NULL); |
3196 | 1229 | nih_assert (job->kill_process = -1); | 1229 | nih_assert (job->kill_process == PROCESS_INVALID); |
3197 | 1230 | 1230 | ||
3198 | 1231 | nih_info (_("Sending %s signal to %s %s process (%d)"), | 1231 | nih_info (_("Sending %s signal to %s %s process (%d)"), |
3199 | 1232 | nih_signal_to_name (job->class->kill_signal), | 1232 | nih_signal_to_name (job->class->kill_signal), |
3200 | @@ -1246,13 +1246,50 @@ | |||
3201 | 1246 | return; | 1246 | return; |
3202 | 1247 | } | 1247 | } |
3203 | 1248 | 1248 | ||
3204 | 1249 | job_process_set_kill_timer (job, process, job->class->kill_timeout); | ||
3205 | 1250 | } | ||
3206 | 1251 | |||
3207 | 1252 | /** | ||
3208 | 1253 | * job_process_set_kill_timer: | ||
3209 | 1254 | * @job: job to set kill timer for, | ||
3210 | 1255 | * @process: process to be killed, | ||
3211 | 1256 | * @timeout: timeout to apply for timer. | ||
3212 | 1257 | * | ||
3213 | 1258 | * Set kill timer for specified @job @process with timeout @timeout. | ||
3214 | 1259 | **/ | ||
3215 | 1260 | void | ||
3216 | 1261 | job_process_set_kill_timer (Job *job, | ||
3217 | 1262 | ProcessType process, | ||
3218 | 1263 | time_t timeout) | ||
3219 | 1264 | { | ||
3220 | 1265 | nih_assert (job); | ||
3221 | 1266 | nih_assert (timeout); | ||
3222 | 1267 | |||
3223 | 1249 | job->kill_process = process; | 1268 | job->kill_process = process; |
3224 | 1250 | job->kill_timer = NIH_MUST (nih_timer_add_timeout ( | 1269 | job->kill_timer = NIH_MUST (nih_timer_add_timeout ( |
3226 | 1251 | job, job->class->kill_timeout, | 1270 | job, timeout, |
3227 | 1252 | (NihTimerCb)job_process_kill_timer, job)); | 1271 | (NihTimerCb)job_process_kill_timer, job)); |
3228 | 1253 | } | 1272 | } |
3229 | 1254 | 1273 | ||
3230 | 1255 | /** | 1274 | /** |
3231 | 1275 | * job_process_adj_kill_timer: | ||
3232 | 1276 | * | ||
3233 | 1277 | * @job: job whose kill timer is to be modified, | ||
3234 | 1278 | * @due: new due time to set for job kill timer. | ||
3235 | 1279 | * | ||
3236 | 1280 | * Adjust due time for @job's kill timer to @due. | ||
3237 | 1281 | **/ | ||
3238 | 1282 | void | ||
3239 | 1283 | job_process_adj_kill_timer (Job *job, time_t due) | ||
3240 | 1284 | { | ||
3241 | 1285 | nih_assert (job); | ||
3242 | 1286 | nih_assert (job->kill_timer); | ||
3243 | 1287 | nih_assert (due); | ||
3244 | 1288 | |||
3245 | 1289 | job->kill_timer->due = due; | ||
3246 | 1290 | } | ||
3247 | 1291 | |||
3248 | 1292 | /** | ||
3249 | 1256 | * job_process_kill_timer: | 1293 | * job_process_kill_timer: |
3250 | 1257 | * @job: job to kill process of, | 1294 | * @job: job to kill process of, |
3251 | 1258 | * @timer: timer that caused us to be called. | 1295 | * @timer: timer that caused us to be called. |
3252 | @@ -1270,13 +1307,13 @@ | |||
3253 | 1270 | nih_assert (job != NULL); | 1307 | nih_assert (job != NULL); |
3254 | 1271 | nih_assert (timer != NULL); | 1308 | nih_assert (timer != NULL); |
3255 | 1272 | nih_assert (job->kill_timer == timer); | 1309 | nih_assert (job->kill_timer == timer); |
3257 | 1273 | nih_assert (job->kill_process != (ProcessType)-1); | 1310 | nih_assert (job->kill_process != PROCESS_INVALID); |
3258 | 1274 | 1311 | ||
3259 | 1275 | process = job->kill_process; | 1312 | process = job->kill_process; |
3260 | 1276 | nih_assert (job->pid[process] > 0); | 1313 | nih_assert (job->pid[process] > 0); |
3261 | 1277 | 1314 | ||
3262 | 1278 | job->kill_timer = NULL; | 1315 | job->kill_timer = NULL; |
3264 | 1279 | job->kill_process = -1; | 1316 | job->kill_process = PROCESS_INVALID; |
3265 | 1280 | 1317 | ||
3266 | 1281 | nih_info (_("Sending %s signal to %s %s process (%d)"), | 1318 | nih_info (_("Sending %s signal to %s %s process (%d)"), |
3267 | 1282 | "KILL", | 1319 | "KILL", |
3268 | @@ -1547,7 +1584,7 @@ | |||
3269 | 1547 | job_name (job)); | 1584 | job_name (job)); |
3270 | 1548 | 1585 | ||
3271 | 1549 | failed = FALSE; | 1586 | failed = FALSE; |
3273 | 1550 | job_failed (job, -1, 0); | 1587 | job_failed (job, PROCESS_INVALID, 0); |
3274 | 1551 | } else { | 1588 | } else { |
3275 | 1552 | nih_warn (_("%s %s process ended, respawning"), | 1589 | nih_warn (_("%s %s process ended, respawning"), |
3276 | 1553 | job_name (job), | 1590 | job_name (job), |
3277 | @@ -1631,7 +1668,7 @@ | |||
3278 | 1631 | if (job->kill_timer) { | 1668 | if (job->kill_timer) { |
3279 | 1632 | nih_unref (job->kill_timer, job); | 1669 | nih_unref (job->kill_timer, job); |
3280 | 1633 | job->kill_timer = NULL; | 1670 | job->kill_timer = NULL; |
3282 | 1634 | job->kill_process = -1; | 1671 | job->kill_process = PROCESS_INVALID; |
3283 | 1635 | } | 1672 | } |
3284 | 1636 | 1673 | ||
3285 | 1637 | if (job->class->console == CONSOLE_LOG && job->log[process]) { | 1674 | if (job->class->console == CONSOLE_LOG && job->log[process]) { |
3286 | 1638 | 1675 | ||
3287 | === modified file 'init/job_process.h' | |||
3288 | --- init/job_process.h 2012-08-02 08:57:20 +0000 | |||
3289 | +++ init/job_process.h 2012-11-14 14:49:20 +0000 | |||
3290 | @@ -143,6 +143,12 @@ | |||
3291 | 143 | char *job_process_log_path (Job *job, int user_job) | 143 | char *job_process_log_path (Job *job, int user_job) |
3292 | 144 | __attribute__ ((malloc, warn_unused_result)); | 144 | __attribute__ ((malloc, warn_unused_result)); |
3293 | 145 | 145 | ||
3294 | 146 | void job_process_set_kill_timer (Job *job, | ||
3295 | 147 | ProcessType process, | ||
3296 | 148 | time_t timeout); | ||
3297 | 149 | |||
3298 | 150 | void job_process_adj_kill_timer (Job *job, time_t due); | ||
3299 | 151 | |||
3300 | 146 | NIH_END_EXTERN | 152 | NIH_END_EXTERN |
3301 | 147 | 153 | ||
3302 | 148 | #endif /* INIT_JOB_PROCESS_H */ | 154 | #endif /* INIT_JOB_PROCESS_H */ |
3303 | 149 | 155 | ||
3304 | === modified file 'init/log.c' | |||
3305 | --- init/log.c 2012-03-16 21:06:11 +0000 | |||
3306 | +++ init/log.c 2012-11-14 14:49:20 +0000 | |||
3307 | @@ -48,8 +48,13 @@ | |||
3308 | 48 | /** | 48 | /** |
3309 | 49 | * log_unflushed_files: | 49 | * log_unflushed_files: |
3310 | 50 | * | 50 | * |
3313 | 51 | * List of known sources of configuration; each item is an | 51 | * List of NihListEntry objects containing Log objects which are no |
3314 | 52 | * NihListEntry. | 52 | * longer associated with Job processes. |
3315 | 53 | * | ||
3316 | 54 | * All the entries in the list contain unflushed Log data. | ||
3317 | 55 | * | ||
3318 | 56 | * Used to capture job process output early in the boot process for | ||
3319 | 57 | * jobs that end before the log partition is mounted and writeable. | ||
3320 | 53 | **/ | 58 | **/ |
3321 | 54 | NihList *log_unflushed_files = NULL; | 59 | NihList *log_unflushed_files = NULL; |
3322 | 55 | 60 | ||
3323 | @@ -393,7 +398,7 @@ | |||
3324 | 393 | * | 398 | * |
3325 | 394 | * Opens log file associated with @log if not already open. | 399 | * Opens log file associated with @log if not already open. |
3326 | 395 | * | 400 | * |
3328 | 396 | * Returns 0 on success, -1 on failure. | 401 | * Returns: 0 on success, -1 on failure. |
3329 | 397 | **/ | 402 | **/ |
3330 | 398 | static int | 403 | static int |
3331 | 399 | log_file_open (Log *log) | 404 | log_file_open (Log *log) |
3332 | @@ -505,7 +510,7 @@ | |||
3333 | 505 | * a corrupted log file should space later become | 510 | * a corrupted log file should space later become |
3334 | 506 | * available. | 511 | * available. |
3335 | 507 | * | 512 | * |
3337 | 508 | * Returns 0 on success, -1 on failure. | 513 | * Returns: 0 on success, -1 on failure. |
3338 | 509 | **/ | 514 | **/ |
3339 | 510 | static int | 515 | static int |
3340 | 511 | log_file_write (Log *log, const char *buf, size_t len) | 516 | log_file_write (Log *log, const char *buf, size_t len) |
3341 | @@ -638,6 +643,8 @@ | |||
3342 | 638 | if (nih_io_buffer_resize (io->recv_buf, LOG_READ_SIZE) < 0) | 643 | if (nih_io_buffer_resize (io->recv_buf, LOG_READ_SIZE) < 0) |
3343 | 639 | break; | 644 | break; |
3344 | 640 | 645 | ||
3345 | 646 | errno = 0; | ||
3346 | 647 | |||
3347 | 641 | /* Append to buffer */ | 648 | /* Append to buffer */ |
3348 | 642 | len = read (io->watch->fd, | 649 | len = read (io->watch->fd, |
3349 | 643 | io->recv_buf->buf + io->recv_buf->len, | 650 | io->recv_buf->buf + io->recv_buf->len, |
3350 | @@ -680,22 +687,23 @@ | |||
3351 | 680 | * causes the loop to be exited. | 687 | * causes the loop to be exited. |
3352 | 681 | */ | 688 | */ |
3353 | 682 | if (len <= 0) { | 689 | if (len <= 0) { |
3354 | 690 | /* Job process has ended and we've drained all the data the job | ||
3355 | 691 | * produced, so remote end must have closed. | ||
3356 | 692 | * | ||
3357 | 693 | * This cannot be handled entirely by log_io_error_handler() | ||
3358 | 694 | * since the job may produce some output prior to disks being | ||
3359 | 695 | * writeable, then end without producing further output. | ||
3360 | 696 | * In this scenario the error handler is never called. | ||
3361 | 697 | * | ||
3362 | 698 | */ | ||
3363 | 699 | if (saved && saved != EAGAIN && saved != EWOULDBLOCK) | ||
3364 | 700 | log->remote_closed = 1; | ||
3365 | 701 | |||
3366 | 683 | close (log->fd); | 702 | close (log->fd); |
3367 | 684 | log->fd = -1; | 703 | log->fd = -1; |
3368 | 685 | break; | 704 | break; |
3369 | 686 | } | 705 | } |
3370 | 687 | } | 706 | } |
3371 | 688 | |||
3372 | 689 | /* Job process has ended and we've drained all the data the job | ||
3373 | 690 | * produced, so remote end must have closed. | ||
3374 | 691 | * | ||
3375 | 692 | * This cannot be handled entirely by log_io_error_handler() | ||
3376 | 693 | * since the job may produce some output prior to disks being | ||
3377 | 694 | * writeable, then end without producing further output. | ||
3378 | 695 | * In this scenario the error handler is never called. | ||
3379 | 696 | * | ||
3380 | 697 | */ | ||
3381 | 698 | log->remote_closed = 1; | ||
3382 | 699 | } | 707 | } |
3383 | 700 | 708 | ||
3384 | 701 | /** | 709 | /** |
3385 | @@ -810,3 +818,193 @@ | |||
3386 | 810 | 818 | ||
3387 | 811 | return 0; | 819 | return 0; |
3388 | 812 | } | 820 | } |
3389 | 821 | |||
3390 | 822 | /** | ||
3391 | 823 | * log_serialise: | ||
3392 | 824 | * @log: log to serialise. | ||
3393 | 825 | * | ||
3394 | 826 | * Convert @log into a JSON representation for serialisation. | ||
3395 | 827 | * Caller must free returned value using json_object_put(). | ||
3396 | 828 | * | ||
3397 | 829 | * Returns: JSON-serialised Log object, or NULL on error. | ||
3398 | 830 | **/ | ||
3399 | 831 | json_object * | ||
3400 | 832 | log_serialise (Log *log) | ||
3401 | 833 | { | ||
3402 | 834 | json_object *json; | ||
3403 | 835 | nih_local char *unflushed_hex = NULL; | ||
3404 | 836 | |||
3405 | 837 | json = json_object_new_object (); | ||
3406 | 838 | if (! json) | ||
3407 | 839 | return NULL; | ||
3408 | 840 | |||
3409 | 841 | if (! log || (! log->io && log->unflushed && ! log->unflushed->len)) { | ||
3410 | 842 | /* Create a "placeholder" log object for non-existent | ||
3411 | 843 | * log objects and for those that are no longer usable. | ||
3412 | 844 | */ | ||
3413 | 845 | if (! state_set_json_string_var (json, "path", NULL)) | ||
3414 | 846 | goto error; | ||
3415 | 847 | return json; | ||
3416 | 848 | } | ||
3417 | 849 | |||
3418 | 850 | /* Attempt to flush any cached data */ | ||
3419 | 851 | if (log->unflushed->len) { | ||
3420 | 852 | /* Don't check return values since if this fails and | ||
3421 | 853 | * unflushed data remains, we encode it below. | ||
3422 | 854 | */ | ||
3423 | 855 | if (log->fd < 0) | ||
3424 | 856 | (void)log_file_open (log); | ||
3425 | 857 | if (log->fd != -1) | ||
3426 | 858 | (void)log_file_write (log, NULL, 0); | ||
3427 | 859 | } | ||
3428 | 860 | |||
3429 | 861 | nih_assert (log->io); | ||
3430 | 862 | |||
3431 | 863 | if (! state_set_json_int_var_from_obj (json, log, fd)) | ||
3432 | 864 | goto error; | ||
3433 | 865 | |||
3434 | 866 | nih_assert (log->io->watch); | ||
3435 | 867 | |||
3436 | 868 | if (! state_set_json_int_var (json, "io_watch_fd", log->io->watch->fd)) | ||
3437 | 869 | goto error; | ||
3438 | 870 | |||
3439 | 871 | if (! state_set_json_string_var_from_obj (json, log, path)) | ||
3440 | 872 | goto error; | ||
3441 | 873 | |||
3442 | 874 | /* log->io itself is not encoded */ | ||
3443 | 875 | |||
3444 | 876 | if (! state_set_json_int_var_from_obj (json, log, uid)) | ||
3445 | 877 | goto error; | ||
3446 | 878 | |||
3447 | 879 | /* Encode unflushed data as hex to ensure any embedded | ||
3448 | 880 | * nulls are handled. | ||
3449 | 881 | */ | ||
3450 | 882 | if (log->unflushed->len) { | ||
3451 | 883 | unflushed_hex = state_data_to_hex (NULL, | ||
3452 | 884 | log->unflushed->buf, | ||
3453 | 885 | log->unflushed->len); | ||
3454 | 886 | |||
3455 | 887 | if (! unflushed_hex) | ||
3456 | 888 | goto error; | ||
3457 | 889 | |||
3458 | 890 | if (! state_set_json_string_var (json, "unflushed", unflushed_hex)) | ||
3459 | 891 | goto error; | ||
3460 | 892 | } | ||
3461 | 893 | |||
3462 | 894 | if (! state_set_json_int_var_from_obj (json, log, detached)) | ||
3463 | 895 | goto error; | ||
3464 | 896 | |||
3465 | 897 | if (! state_set_json_int_var_from_obj (json, log, remote_closed)) | ||
3466 | 898 | goto error; | ||
3467 | 899 | |||
3468 | 900 | if (! state_set_json_int_var_from_obj (json, log, open_errno)) | ||
3469 | 901 | goto error; | ||
3470 | 902 | |||
3471 | 903 | return json; | ||
3472 | 904 | |||
3473 | 905 | error: | ||
3474 | 906 | json_object_put (json); | ||
3475 | 907 | return NULL; | ||
3476 | 908 | } | ||
3477 | 909 | |||
3478 | 910 | /** | ||
3479 | 911 | * log_deserialise: | ||
3480 | 912 | * @json: JSON-serialised Log object to deserialise. | ||
3481 | 913 | * | ||
3482 | 914 | * Convert @json into a Log object. | ||
3483 | 915 | * | ||
3484 | 916 | * Returns: Log object, or NULL on error. | ||
3485 | 917 | **/ | ||
3486 | 918 | Log * | ||
3487 | 919 | log_deserialise (const void *parent, | ||
3488 | 920 | json_object *json) | ||
3489 | 921 | { | ||
3490 | 922 | Log *log; | ||
3491 | 923 | nih_local char *unflushed_hex = NULL; | ||
3492 | 924 | nih_local char *unflushed = NULL; | ||
3493 | 925 | int ret; | ||
3494 | 926 | size_t len; | ||
3495 | 927 | json_object *json_unflushed; | ||
3496 | 928 | nih_local char *path = NULL; | ||
3497 | 929 | int io_watch_fd = -1; | ||
3498 | 930 | uid_t uid = (uid_t)-1; | ||
3499 | 931 | |||
3500 | 932 | nih_assert (json); | ||
3501 | 933 | |||
3502 | 934 | log_unflushed_init (); | ||
3503 | 935 | |||
3504 | 936 | if (! state_check_json_type (json, object)) | ||
3505 | 937 | return NULL; | ||
3506 | 938 | |||
3507 | 939 | if (! state_get_json_string_var (json, "path", NULL, path)) | ||
3508 | 940 | return NULL; | ||
3509 | 941 | |||
3510 | 942 | if (! path) { | ||
3511 | 943 | /* placeholder log object */ | ||
3512 | 944 | return NULL; | ||
3513 | 945 | } | ||
3514 | 946 | |||
3515 | 947 | if (! state_get_json_int_var (json, "io_watch_fd", io_watch_fd)) | ||
3516 | 948 | return NULL; | ||
3517 | 949 | |||
3518 | 950 | nih_assert (io_watch_fd != -1); | ||
3519 | 951 | |||
3520 | 952 | /* re-apply CLOEXEC flag to stop job fd being leaked to children */ | ||
3521 | 953 | if (state_toggle_cloexec (io_watch_fd, TRUE) < 0) | ||
3522 | 954 | return NULL; | ||
3523 | 955 | |||
3524 | 956 | if (! state_get_json_int_var (json, "uid", uid)) | ||
3525 | 957 | return NULL; | ||
3526 | 958 | |||
3527 | 959 | log = log_new (parent, path, io_watch_fd, uid); | ||
3528 | 960 | if (! log) | ||
3529 | 961 | return NULL; | ||
3530 | 962 | |||
3531 | 963 | if (! state_get_json_int_var_to_obj (json, log, fd)) | ||
3532 | 964 | goto error; | ||
3533 | 965 | |||
3534 | 966 | /* Re-apply CLOEXEC flag to stop log file fd being leaked to children. | ||
3535 | 967 | * | ||
3536 | 968 | * Note we discard return value since if this fails, | ||
3537 | 969 | * we would never close the fd. | ||
3538 | 970 | */ | ||
3539 | 971 | if (log->fd != -1) | ||
3540 | 972 | (void)state_toggle_cloexec (log->fd, TRUE); | ||
3541 | 973 | |||
3542 | 974 | log->unflushed = nih_io_buffer_new (log); | ||
3543 | 975 | if (! log->unflushed) | ||
3544 | 976 | goto error; | ||
3545 | 977 | |||
3546 | 978 | json_unflushed = json_object_object_get (json, "unflushed"); | ||
3547 | 979 | if (json_unflushed) { | ||
3548 | 980 | if (! state_get_json_string_var_strict (json, "unflushed", NULL, unflushed_hex)) | ||
3549 | 981 | goto error; | ||
3550 | 982 | |||
3551 | 983 | ret = state_hex_to_data (NULL, | ||
3552 | 984 | unflushed_hex, | ||
3553 | 985 | strlen (unflushed_hex), | ||
3554 | 986 | &unflushed, | ||
3555 | 987 | &len); | ||
3556 | 988 | |||
3557 | 989 | if (ret < 0) | ||
3558 | 990 | goto error; | ||
3559 | 991 | |||
3560 | 992 | if (nih_io_buffer_push (log->unflushed, unflushed, len) < 0) | ||
3561 | 993 | goto error; | ||
3562 | 994 | } | ||
3563 | 995 | |||
3564 | 996 | if (! state_get_json_int_var_to_obj (json, log, detached)) | ||
3565 | 997 | goto error; | ||
3566 | 998 | |||
3567 | 999 | if (! state_get_json_int_var_to_obj (json, log, remote_closed)) | ||
3568 | 1000 | goto error; | ||
3569 | 1001 | |||
3570 | 1002 | if (! state_get_json_int_var_to_obj (json, log, open_errno)) | ||
3571 | 1003 | goto error; | ||
3572 | 1004 | |||
3573 | 1005 | return log; | ||
3574 | 1006 | |||
3575 | 1007 | error: | ||
3576 | 1008 | nih_free (log); | ||
3577 | 1009 | return NULL; | ||
3578 | 1010 | } | ||
3579 | 813 | 1011 | ||
3580 | === modified file 'init/log.h' | |||
3581 | --- init/log.h 2012-02-14 16:47:34 +0000 | |||
3582 | +++ init/log.h 2012-11-14 14:49:20 +0000 | |||
3583 | @@ -29,6 +29,8 @@ | |||
3584 | 29 | #include <nih/logging.h> | 29 | #include <nih/logging.h> |
3585 | 30 | #include <nih/error.h> | 30 | #include <nih/error.h> |
3586 | 31 | 31 | ||
3587 | 32 | #include "state.h" | ||
3588 | 33 | |||
3589 | 32 | /** LOG_DEFAULT_UMASK: | 34 | /** LOG_DEFAULT_UMASK: |
3590 | 33 | * | 35 | * |
3591 | 34 | * The default file creation mask for log files. | 36 | * The default file creation mask for log files. |
3592 | @@ -55,7 +57,7 @@ | |||
3593 | 55 | * @io: NihIo associated with jobs stdout and stderr, | 57 | * @io: NihIo associated with jobs stdout and stderr, |
3594 | 56 | * @uid: User ID of caller, | 58 | * @uid: User ID of caller, |
3595 | 57 | * @unflushed: Unflushed data, | 59 | * @unflushed: Unflushed data, |
3597 | 58 | * @detached: TRUE if log is no longer associated with a parent,(job), | 60 | * @detached: TRUE if log is no longer associated with a parent (job), |
3598 | 59 | * @remote_closed: TRUE if remote end of pty has been closed, | 61 | * @remote_closed: TRUE if remote end of pty has been closed, |
3599 | 60 | * @open_errno: value of errno immediately after last attempt to open @path. | 62 | * @open_errno: value of errno immediately after last attempt to open @path. |
3600 | 61 | **/ | 63 | **/ |
3601 | @@ -79,11 +81,17 @@ | |||
3602 | 79 | __attribute__ ((warn_unused_result, malloc)); | 81 | __attribute__ ((warn_unused_result, malloc)); |
3603 | 80 | void log_io_reader (Log *log, NihIo *io, const char *buf, size_t len); | 82 | void log_io_reader (Log *log, NihIo *io, const char *buf, size_t len); |
3604 | 81 | void log_io_error_handler (Log *log, NihIo *io); | 83 | void log_io_error_handler (Log *log, NihIo *io); |
3608 | 82 | int log_destroy (Log *log); | 84 | int log_destroy (Log *log) |
3609 | 83 | int log_handle_unflushed (void *parent, Log *log); | 85 | __attribute__ ((warn_unused_result)); |
3610 | 84 | int log_clear_unflushed (void); | 86 | int log_handle_unflushed (void *parent, Log *log) |
3611 | 87 | __attribute__ ((warn_unused_result)); | ||
3612 | 88 | int log_clear_unflushed (void) | ||
3613 | 89 | __attribute__ ((warn_unused_result)); | ||
3614 | 85 | void log_unflushed_init (void); | 90 | void log_unflushed_init (void); |
3616 | 86 | 91 | json_object * log_serialise (Log *log) | |
3617 | 92 | __attribute__ ((warn_unused_result, malloc)); | ||
3618 | 93 | Log * log_deserialise (const void *parent, json_object *json) | ||
3619 | 94 | __attribute__ ((warn_unused_result, malloc)); | ||
3620 | 87 | 95 | ||
3621 | 88 | NIH_END_EXTERN | 96 | NIH_END_EXTERN |
3622 | 89 | 97 | ||
3623 | 90 | 98 | ||
3624 | === modified file 'init/main.c' | |||
3625 | --- init/main.c 2012-08-07 16:39:00 +0000 | |||
3626 | +++ init/main.c 2012-11-14 14:49:20 +0000 | |||
3627 | @@ -60,12 +60,16 @@ | |||
3628 | 60 | #include "event.h" | 60 | #include "event.h" |
3629 | 61 | #include "conf.h" | 61 | #include "conf.h" |
3630 | 62 | #include "control.h" | 62 | #include "control.h" |
3631 | 63 | #include "state.h" | ||
3632 | 63 | 64 | ||
3633 | 64 | 65 | ||
3634 | 65 | /* Prototypes for static functions */ | 66 | /* Prototypes for static functions */ |
3635 | 66 | #ifndef DEBUG | 67 | #ifndef DEBUG |
3636 | 67 | static int logger_kmsg (NihLogLevel priority, const char *message); | 68 | static int logger_kmsg (NihLogLevel priority, const char *message); |
3637 | 68 | static void crash_handler (int signum); | 69 | static void crash_handler (int signum); |
3638 | 70 | #endif /* DEBUG */ | ||
3639 | 71 | static void term_handler (void *data, NihSignal *signal); | ||
3640 | 72 | #ifndef DEBUG | ||
3641 | 69 | static void cad_handler (void *data, NihSignal *signal); | 73 | static void cad_handler (void *data, NihSignal *signal); |
3642 | 70 | static void kbd_handler (void *data, NihSignal *signal); | 74 | static void kbd_handler (void *data, NihSignal *signal); |
3643 | 71 | static void pwr_handler (void *data, NihSignal *signal); | 75 | static void pwr_handler (void *data, NihSignal *signal); |
3644 | @@ -79,20 +83,12 @@ | |||
3645 | 79 | 83 | ||
3646 | 80 | 84 | ||
3647 | 81 | /** | 85 | /** |
3662 | 82 | * argv0: | 86 | * state_fd: |
3663 | 83 | * | 87 | * |
3664 | 84 | * Path to program executed, used for re-executing the init binary from the | 88 | * File descriptor to read serialised state from when performing |
3665 | 85 | * same location we were executed from. | 89 | * stateful re-exec. If value is not -1, attempt stateful re-exec. |
3666 | 86 | **/ | 90 | **/ |
3667 | 87 | static const char *argv0 = NULL; | 91 | static int state_fd = -1; |
3654 | 88 | |||
3655 | 89 | /** | ||
3656 | 90 | * restart: | ||
3657 | 91 | * | ||
3658 | 92 | * This is set to TRUE if we're being re-exec'd by an existing init | ||
3659 | 93 | * process. | ||
3660 | 94 | **/ | ||
3661 | 95 | static int restart = FALSE; | ||
3668 | 96 | 92 | ||
3669 | 97 | /** | 93 | /** |
3670 | 98 | * conf_dir: | 94 | * conf_dir: |
3671 | @@ -147,7 +143,13 @@ | |||
3672 | 147 | { 0, "no-startup-event", N_("do not emit any startup event (for testing)"), | 143 | { 0, "no-startup-event", N_("do not emit any startup event (for testing)"), |
3673 | 148 | NULL, NULL, &disable_startup_event, NULL }, | 144 | NULL, NULL, &disable_startup_event, NULL }, |
3674 | 149 | 145 | ||
3676 | 150 | { 0, "restart", NULL, NULL, NULL, &restart, NULL }, | 146 | /* Must be specified for both stateful and stateless re-exec */ |
3677 | 147 | { 0, "restart", N_("flag a re-exec has occurred"), | ||
3678 | 148 | NULL, NULL, &restart, NULL }, | ||
3679 | 149 | |||
3680 | 150 | /* Required for stateful re-exec */ | ||
3681 | 151 | { 0, "state-fd", N_("specify file descriptor to read serialisation data from"), | ||
3682 | 152 | NULL, "FD", &state_fd, nih_option_int }, | ||
3683 | 151 | 153 | ||
3684 | 152 | { 0, "session", N_("use D-Bus session bus rather than system bus (for testing)"), | 154 | { 0, "session", N_("use D-Bus session bus rather than system bus (for testing)"), |
3685 | 153 | NULL, NULL, &use_session_bus, NULL }, | 155 | NULL, NULL, &use_session_bus, NULL }, |
3686 | @@ -166,11 +168,12 @@ | |||
3687 | 166 | main (int argc, | 168 | main (int argc, |
3688 | 167 | char *argv[]) | 169 | char *argv[]) |
3689 | 168 | { | 170 | { |
3691 | 169 | char **args; | 171 | char **args = NULL; |
3692 | 170 | int ret; | 172 | int ret; |
3693 | 171 | 173 | ||
3696 | 172 | argv0 = argv[0]; | 174 | args_copy = NIH_MUST (nih_str_array_copy (NULL, NULL, argv)); |
3697 | 173 | nih_main_init (argv0); | 175 | |
3698 | 176 | nih_main_init (args_copy[0]); | ||
3699 | 174 | 177 | ||
3700 | 175 | nih_option_set_synopsis (_("Process management daemon.")); | 178 | nih_option_set_synopsis (_("Process management daemon.")); |
3701 | 176 | nih_option_set_help ( | 179 | nih_option_set_help ( |
3702 | @@ -295,8 +298,9 @@ | |||
3703 | 295 | */ | 298 | */ |
3704 | 296 | if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) { | 299 | if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) { |
3705 | 297 | NihError *err; | 300 | NihError *err; |
3707 | 298 | 301 | ||
3708 | 299 | err = nih_error_get (); | 302 | err = nih_error_get (); |
3709 | 303 | |||
3710 | 300 | nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"), | 304 | nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"), |
3711 | 301 | err->message); | 305 | err->message); |
3712 | 302 | nih_free (err); | 306 | nih_free (err); |
3713 | @@ -411,7 +415,16 @@ | |||
3714 | 411 | /* SIGUSR1 instructs us to reconnect to D-Bus */ | 415 | /* SIGUSR1 instructs us to reconnect to D-Bus */ |
3715 | 412 | nih_signal_set_handler (SIGUSR1, nih_signal_handler); | 416 | nih_signal_set_handler (SIGUSR1, nih_signal_handler); |
3716 | 413 | NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL)); | 417 | NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL)); |
3717 | 418 | |||
3718 | 414 | } | 419 | } |
3719 | 420 | |||
3720 | 421 | /* SIGTERM instructs us to re-exec ourselves; this should be the | ||
3721 | 422 | * last in the list to ensure that all other signals are handled | ||
3722 | 423 | * before a SIGTERM. | ||
3723 | 424 | */ | ||
3724 | 425 | nih_signal_set_handler (SIGTERM, nih_signal_handler); | ||
3725 | 426 | NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, term_handler, NULL)); | ||
3726 | 427 | |||
3727 | 415 | #endif /* DEBUG */ | 428 | #endif /* DEBUG */ |
3728 | 416 | 429 | ||
3729 | 417 | 430 | ||
3730 | @@ -456,6 +469,48 @@ | |||
3731 | 456 | } | 469 | } |
3732 | 457 | 470 | ||
3733 | 458 | 471 | ||
3734 | 472 | if (restart) { | ||
3735 | 473 | if (state_fd == -1) { | ||
3736 | 474 | nih_warn ("%s", | ||
3737 | 475 | _("Stateful re-exec supported but stateless re-exec requested")); | ||
3738 | 476 | } else if (state_read (state_fd) < 0) { | ||
3739 | 477 | nih_local char *arg = NULL; | ||
3740 | 478 | |||
3741 | 479 | /* Stateful re-exec has failed so try once more by | ||
3742 | 480 | * degrading to stateless re-exec, which even in | ||
3743 | 481 | * the case of low-memory scenarios will work. | ||
3744 | 482 | */ | ||
3745 | 483 | |||
3746 | 484 | /* Inform the child we've given up on stateful | ||
3747 | 485 | * re-exec. | ||
3748 | 486 | */ | ||
3749 | 487 | close (state_fd); | ||
3750 | 488 | |||
3751 | 489 | nih_error ("%s - %s", | ||
3752 | 490 | _("Failed to read serialisation data"), | ||
3753 | 491 | _("reverting to stateless re-exec")); | ||
3754 | 492 | |||
3755 | 493 | /* Remove any existing (but now stale) state fd | ||
3756 | 494 | * args which will effectively disable stateful | ||
3757 | 495 | * re-exec. | ||
3758 | 496 | */ | ||
3759 | 497 | clean_args (&args_copy); | ||
3760 | 498 | |||
3761 | 499 | /* Attempt stateless re-exec */ | ||
3762 | 500 | perform_reexec (); | ||
3763 | 501 | |||
3764 | 502 | nih_error ("%s", | ||
3765 | 503 | _("Both stateful and stateless re-execs failed")); | ||
3766 | 504 | |||
3767 | 505 | /* Out of options */ | ||
3768 | 506 | nih_assert_not_reached (); | ||
3769 | 507 | } else { | ||
3770 | 508 | close (state_fd); | ||
3771 | 509 | |||
3772 | 510 | nih_info ("Stateful re-exec completed"); | ||
3773 | 511 | } | ||
3774 | 512 | } | ||
3775 | 513 | |||
3776 | 459 | /* Read configuration */ | 514 | /* Read configuration */ |
3777 | 460 | NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE)); | 515 | NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE)); |
3778 | 461 | NIH_MUST (conf_source_new (NULL, conf_dir, CONF_JOB_DIR)); | 516 | NIH_MUST (conf_source_new (NULL, conf_dir, CONF_JOB_DIR)); |
3779 | @@ -479,7 +534,8 @@ | |||
3780 | 479 | } | 534 | } |
3781 | 480 | 535 | ||
3782 | 481 | /* Open connection to the appropriate D-Bus bus; we normally expect this to | 536 | /* Open connection to the appropriate D-Bus bus; we normally expect this to |
3784 | 482 | * fail and will try again later - don't let ENOMEM stop us though. | 537 | * fail (since dbus-daemon probably isn't running yet) and will try again |
3785 | 538 | * later - don't let ENOMEM stop us though. | ||
3786 | 483 | */ | 539 | */ |
3787 | 484 | while (control_bus_open () < 0) { | 540 | while (control_bus_open () < 0) { |
3788 | 485 | NihError *err; | 541 | NihError *err; |
3789 | @@ -529,9 +585,17 @@ | |||
3790 | 529 | } | 585 | } |
3791 | 530 | 586 | ||
3792 | 531 | } else { | 587 | } else { |
3796 | 532 | sigset_t mask; | 588 | sigset_t mask; |
3797 | 533 | 589 | ||
3798 | 534 | /* We're ok to receive signals again */ | 590 | /* We have been re-exec'd. Don't emit an initial event |
3799 | 591 | * as only Upstart is restarting - we don't want to restart | ||
3800 | 592 | * the system (another reason being that we don't yet support | ||
3801 | 593 | * upstart-in-initramfs to upstart-in-root-filesystem | ||
3802 | 594 | * state-passing transitions). | ||
3803 | 595 | */ | ||
3804 | 596 | |||
3805 | 597 | /* We're ok to receive signals again so restore signals | ||
3806 | 598 | * disabled by the term_handler */ | ||
3807 | 535 | sigemptyset (&mask); | 599 | sigemptyset (&mask); |
3808 | 536 | sigprocmask (SIG_SETMASK, &mask, NULL); | 600 | sigprocmask (SIG_SETMASK, &mask, NULL); |
3809 | 537 | } | 601 | } |
3810 | @@ -631,7 +695,7 @@ | |||
3811 | 631 | { | 695 | { |
3812 | 632 | pid_t pid; | 696 | pid_t pid; |
3813 | 633 | 697 | ||
3815 | 634 | nih_assert (argv0 != NULL); | 698 | nih_assert (args_copy[0] != NULL); |
3816 | 635 | 699 | ||
3817 | 636 | pid = fork (); | 700 | pid = fork (); |
3818 | 637 | if (pid == 0) { | 701 | if (pid == 0) { |
3819 | @@ -685,7 +749,30 @@ | |||
3820 | 685 | /* Goodbye, cruel world. */ | 749 | /* Goodbye, cruel world. */ |
3821 | 686 | exit (signum); | 750 | exit (signum); |
3822 | 687 | } | 751 | } |
3824 | 688 | 752 | #endif | |
3825 | 753 | |||
3826 | 754 | /** | ||
3827 | 755 | * term_handler: | ||
3828 | 756 | * @data: unused, | ||
3829 | 757 | * @signal: signal caught. | ||
3830 | 758 | * | ||
3831 | 759 | * This is called when we receive the TERM signal, which instructs us | ||
3832 | 760 | * to reexec ourselves. | ||
3833 | 761 | **/ | ||
3834 | 762 | static void | ||
3835 | 763 | term_handler (void *data, | ||
3836 | 764 | NihSignal *signal) | ||
3837 | 765 | { | ||
3838 | 766 | nih_assert (args_copy[0] != NULL); | ||
3839 | 767 | nih_assert (signal != NULL); | ||
3840 | 768 | |||
3841 | 769 | nih_warn (_("Re-executing %s"), args_copy[0]); | ||
3842 | 770 | |||
3843 | 771 | stateful_reexec (); | ||
3844 | 772 | } | ||
3845 | 773 | |||
3846 | 774 | |||
3847 | 775 | #ifndef DEBUG | ||
3848 | 689 | /** | 776 | /** |
3849 | 690 | * cad_handler: | 777 | * cad_handler: |
3850 | 691 | * @data: unused, | 778 | * @data: unused, |
3851 | @@ -835,7 +922,7 @@ | |||
3852 | 835 | * NihOption setter function to handle selection of default console | 922 | * NihOption setter function to handle selection of default console |
3853 | 836 | * type. | 923 | * type. |
3854 | 837 | * | 924 | * |
3856 | 838 | * Returns 0 on success, -1 on invalid console type. | 925 | * Returns: 0 on success, -1 on invalid console type. |
3857 | 839 | **/ | 926 | **/ |
3858 | 840 | static int | 927 | static int |
3859 | 841 | console_type_setter (NihOption *option, const char *arg) | 928 | console_type_setter (NihOption *option, const char *arg) |
3860 | @@ -851,3 +938,4 @@ | |||
3861 | 851 | 938 | ||
3862 | 852 | return 0; | 939 | return 0; |
3863 | 853 | } | 940 | } |
3864 | 941 | |||
3865 | 854 | 942 | ||
3866 | === modified file 'init/parse_job.c' | |||
3867 | --- init/parse_job.c 2012-10-22 13:29:45 +0000 | |||
3868 | +++ init/parse_job.c 2012-11-14 14:49:20 +0000 | |||
3869 | @@ -37,7 +37,6 @@ | |||
3870 | 37 | #include <nih/string.h> | 37 | #include <nih/string.h> |
3871 | 38 | #include <nih/list.h> | 38 | #include <nih/list.h> |
3872 | 39 | #include <nih/signal.h> | 39 | #include <nih/signal.h> |
3873 | 40 | #include <nih/config.h> | ||
3874 | 41 | #include <nih/logging.h> | 40 | #include <nih/logging.h> |
3875 | 42 | #include <nih/error.h> | 41 | #include <nih/error.h> |
3876 | 43 | 42 | ||
3877 | @@ -625,6 +624,48 @@ | |||
3878 | 625 | } | 624 | } |
3879 | 626 | 625 | ||
3880 | 627 | /** | 626 | /** |
3881 | 627 | * parse_on_simple: | ||
3882 | 628 | * @class: job class being parsed, | ||
3883 | 629 | * @stanza_name: name of stanza type to parse ("start" or "stop"), | ||
3884 | 630 | * @string: string to parse. | ||
3885 | 631 | * | ||
3886 | 632 | * Parse either a "start" or "stop" condition from @string (which must | ||
3887 | 633 | * start with the first byte beyond either "start on" or "stop on". | ||
3888 | 634 | * | ||
3889 | 635 | * Returns: EventOperator at root of expression tree on success, NULL | ||
3890 | 636 | * on raised error. | ||
3891 | 637 | **/ | ||
3892 | 638 | EventOperator * | ||
3893 | 639 | parse_on_simple (JobClass *class, const char *stanza_name, const char *string) | ||
3894 | 640 | { | ||
3895 | 641 | EventOperator *root = NULL; | ||
3896 | 642 | NihConfigStanza *stanza = NULL; | ||
3897 | 643 | size_t pos = 0; | ||
3898 | 644 | size_t lineno = 0; | ||
3899 | 645 | size_t len; | ||
3900 | 646 | |||
3901 | 647 | nih_assert (class); | ||
3902 | 648 | nih_assert (stanza_name); | ||
3903 | 649 | nih_assert (string); | ||
3904 | 650 | |||
3905 | 651 | /* Find the appropriate config stanza */ | ||
3906 | 652 | for (NihConfigStanza *s = stanzas; s->name; s++) { | ||
3907 | 653 | if (! strcmp (stanza_name, s->name)) { | ||
3908 | 654 | stanza = s; | ||
3909 | 655 | break; | ||
3910 | 656 | } | ||
3911 | 657 | } | ||
3912 | 658 | |||
3913 | 659 | nih_assert (stanza); | ||
3914 | 660 | |||
3915 | 661 | len = strlen (string); | ||
3916 | 662 | |||
3917 | 663 | root = parse_on (class, stanza, string, len, &pos, &lineno); | ||
3918 | 664 | |||
3919 | 665 | return root; | ||
3920 | 666 | } | ||
3921 | 667 | |||
3922 | 668 | /** | ||
3923 | 628 | * parse_on_operator: | 669 | * parse_on_operator: |
3924 | 629 | * @class: job class being parsed, | 670 | * @class: job class being parsed, |
3925 | 630 | * @stanza: stanza found, | 671 | * @stanza: stanza found, |
3926 | 631 | 672 | ||
3927 | === modified file 'init/parse_job.h' | |||
3928 | --- init/parse_job.h 2011-06-06 12:52:08 +0000 | |||
3929 | +++ init/parse_job.h 2012-11-14 14:49:20 +0000 | |||
3930 | @@ -21,6 +21,7 @@ | |||
3931 | 21 | #define INIT_PARSE_JOB_H | 21 | #define INIT_PARSE_JOB_H |
3932 | 22 | 22 | ||
3933 | 23 | #include <nih/macros.h> | 23 | #include <nih/macros.h> |
3934 | 24 | #include <nih/config.h> | ||
3935 | 24 | 25 | ||
3936 | 25 | #include "session.h" | 26 | #include "session.h" |
3937 | 26 | #include "job_class.h" | 27 | #include "job_class.h" |
3938 | @@ -34,6 +35,10 @@ | |||
3939 | 34 | size_t *pos, size_t *lineno) | 35 | size_t *pos, size_t *lineno) |
3940 | 35 | __attribute__ ((warn_unused_result, malloc)); | 36 | __attribute__ ((warn_unused_result, malloc)); |
3941 | 36 | 37 | ||
3942 | 38 | EventOperator * | ||
3943 | 39 | parse_on_simple (JobClass *class, const char *stanza_name, | ||
3944 | 40 | const char *string) | ||
3945 | 41 | __attribute__ ((warn_unused_result, malloc)); | ||
3946 | 37 | NIH_END_EXTERN | 42 | NIH_END_EXTERN |
3947 | 38 | 43 | ||
3948 | 39 | #endif /* INIT_PARSE_JOB_H */ | 44 | #endif /* INIT_PARSE_JOB_H */ |
3949 | 40 | 45 | ||
3950 | === modified file 'init/process.c' | |||
3951 | --- init/process.c 2009-06-23 09:29:35 +0000 | |||
3952 | +++ init/process.c 2012-11-14 14:49:20 +0000 | |||
3953 | @@ -29,8 +29,10 @@ | |||
3954 | 29 | #include <nih/macros.h> | 29 | #include <nih/macros.h> |
3955 | 30 | #include <nih/alloc.h> | 30 | #include <nih/alloc.h> |
3956 | 31 | #include <nih/logging.h> | 31 | #include <nih/logging.h> |
3957 | 32 | #include <nih/string.h> | ||
3958 | 32 | 33 | ||
3959 | 33 | #include "process.h" | 34 | #include "process.h" |
3960 | 35 | #include "state.h" | ||
3961 | 34 | 36 | ||
3962 | 35 | 37 | ||
3963 | 36 | /** | 38 | /** |
3964 | @@ -116,3 +118,224 @@ | |||
3965 | 116 | return -1; | 118 | return -1; |
3966 | 117 | } | 119 | } |
3967 | 118 | } | 120 | } |
3968 | 121 | |||
3969 | 122 | /** | ||
3970 | 123 | * process_serialise: | ||
3971 | 124 | * @process: process to serialise. | ||
3972 | 125 | * | ||
3973 | 126 | * Convert @process into a JSON representation for serialisation. | ||
3974 | 127 | * Caller must free returned value using json_object_put(). | ||
3975 | 128 | * | ||
3976 | 129 | * Returns: JSON-serialised Process object, or NULL on error. | ||
3977 | 130 | **/ | ||
3978 | 131 | json_object * | ||
3979 | 132 | process_serialise (const Process *process) | ||
3980 | 133 | { | ||
3981 | 134 | json_object *json; | ||
3982 | 135 | |||
3983 | 136 | nih_assert (process); | ||
3984 | 137 | |||
3985 | 138 | json = json_object_new_object (); | ||
3986 | 139 | if (! json) | ||
3987 | 140 | return NULL; | ||
3988 | 141 | |||
3989 | 142 | if (! state_set_json_int_var_from_obj (json, process, script)) | ||
3990 | 143 | goto error; | ||
3991 | 144 | |||
3992 | 145 | if (! state_set_json_string_var_from_obj (json, process, command)) | ||
3993 | 146 | goto error; | ||
3994 | 147 | |||
3995 | 148 | return json; | ||
3996 | 149 | |||
3997 | 150 | error: | ||
3998 | 151 | json_object_put (json); | ||
3999 | 152 | return NULL; | ||
4000 | 153 | |||
4001 | 154 | } | ||
4002 | 155 | |||
4003 | 156 | /** | ||
4004 | 157 | * process_serialise_all: | ||
4005 | 158 | * | ||
4006 | 159 | * @processes: array of Processes. | ||
4007 | 160 | * | ||
4008 | 161 | * Convert array of Process objects to JSON representation. | ||
4009 | 162 | * | ||
4010 | 163 | * Returns: JSON object containing array of Processes, or NULL on error. | ||
4011 | 164 | */ | ||
4012 | 165 | json_object * | ||
4013 | 166 | process_serialise_all (const Process * const * const processes) | ||
4014 | 167 | { | ||
4015 | 168 | json_object *json; | ||
4016 | 169 | json_object *json_process; | ||
4017 | 170 | Process dummy = { 0, NULL }; | ||
4018 | 171 | |||
4019 | 172 | nih_assert (processes); | ||
4020 | 173 | |||
4021 | 174 | json = json_object_new_array (); | ||
4022 | 175 | if (! json) | ||
4023 | 176 | return NULL; | ||
4024 | 177 | |||
4025 | 178 | for (int i = 0; i < PROCESS_LAST; i++) { | ||
4026 | 179 | /* We must encode a blank entry for missing array elements | ||
4027 | 180 | * to ensure correct deserialisation. | ||
4028 | 181 | */ | ||
4029 | 182 | json_process = process_serialise (processes[i] | ||
4030 | 183 | ? processes[i] | ||
4031 | 184 | : &dummy); | ||
4032 | 185 | |||
4033 | 186 | if (! json_process) | ||
4034 | 187 | goto error; | ||
4035 | 188 | |||
4036 | 189 | if (json_object_array_add (json, json_process) < 0) | ||
4037 | 190 | goto error; | ||
4038 | 191 | } | ||
4039 | 192 | |||
4040 | 193 | return json; | ||
4041 | 194 | |||
4042 | 195 | error: | ||
4043 | 196 | json_object_put (json); | ||
4044 | 197 | return NULL; | ||
4045 | 198 | } | ||
4046 | 199 | |||
4047 | 200 | /** | ||
4048 | 201 | * process_deserialise: | ||
4049 | 202 | * @json: JSON-serialised Process object to deserialise. | ||
4050 | 203 | * | ||
4051 | 204 | * Convert @json into a Process object. | ||
4052 | 205 | * | ||
4053 | 206 | * Returns: Process object, or NULL on error. | ||
4054 | 207 | **/ | ||
4055 | 208 | Process * | ||
4056 | 209 | process_deserialise (json_object *json, const void *parent) | ||
4057 | 210 | { | ||
4058 | 211 | Process *process = NULL; | ||
4059 | 212 | |||
4060 | 213 | nih_assert (json); | ||
4061 | 214 | |||
4062 | 215 | if (! state_check_json_type (json, object)) | ||
4063 | 216 | goto error; | ||
4064 | 217 | |||
4065 | 218 | process = NIH_MUST (process_new (parent)); | ||
4066 | 219 | |||
4067 | 220 | if (! state_get_json_int_var_to_obj (json, process, script)) | ||
4068 | 221 | goto error; | ||
4069 | 222 | |||
4070 | 223 | if (! state_get_json_string_var_to_obj (json, process, command)) | ||
4071 | 224 | goto error; | ||
4072 | 225 | |||
4073 | 226 | /* All Process slots have to be serialised in the JSON to | ||
4074 | 227 | * guarantee ordering on deserialisation. | ||
4075 | 228 | * | ||
4076 | 229 | * However, here we've found a Process that was merely | ||
4077 | 230 | * an ordering placeholder - no command has been defined, | ||
4078 | 231 | * so ignore it. | ||
4079 | 232 | */ | ||
4080 | 233 | if (! process->command) | ||
4081 | 234 | goto error; | ||
4082 | 235 | |||
4083 | 236 | return process; | ||
4084 | 237 | |||
4085 | 238 | error: | ||
4086 | 239 | nih_free (process); | ||
4087 | 240 | return NULL; | ||
4088 | 241 | } | ||
4089 | 242 | |||
4090 | 243 | /** | ||
4091 | 244 | * process_deserialise_all: | ||
4092 | 245 | * | ||
4093 | 246 | * @json: root of JSON-serialised state, | ||
4094 | 247 | * @parent: parent of @processes, | ||
4095 | 248 | * @processes: newly-allocated array of Process objects. | ||
4096 | 249 | * | ||
4097 | 250 | * Convert JSON representation of processes back into | ||
4098 | 251 | * an array of Process objects. | ||
4099 | 252 | * | ||
4100 | 253 | * Returns: 0 on success, -1 on error. | ||
4101 | 254 | **/ | ||
4102 | 255 | int | ||
4103 | 256 | process_deserialise_all (json_object *json, const void *parent, | ||
4104 | 257 | Process **processes) | ||
4105 | 258 | { | ||
4106 | 259 | json_object *json_processes; | ||
4107 | 260 | int i; | ||
4108 | 261 | |||
4109 | 262 | nih_assert (json); | ||
4110 | 263 | nih_assert (parent); | ||
4111 | 264 | nih_assert (processes); | ||
4112 | 265 | |||
4113 | 266 | json_processes = json_object_object_get (json, "process"); | ||
4114 | 267 | |||
4115 | 268 | if (! json_processes) | ||
4116 | 269 | goto error; | ||
4117 | 270 | |||
4118 | 271 | if (! state_check_json_type (json_processes, array)) | ||
4119 | 272 | goto error; | ||
4120 | 273 | |||
4121 | 274 | for (i = 0; i < json_object_array_length (json_processes); i++) { | ||
4122 | 275 | json_object *json_process; | ||
4123 | 276 | |||
4124 | 277 | nih_assert (i <= PROCESS_LAST); | ||
4125 | 278 | |||
4126 | 279 | json_process = json_object_array_get_idx (json_processes, i); | ||
4127 | 280 | if (! json_process) | ||
4128 | 281 | goto error; | ||
4129 | 282 | |||
4130 | 283 | if (! state_check_json_type (json_process, object)) | ||
4131 | 284 | goto error; | ||
4132 | 285 | |||
4133 | 286 | processes[i] = process_deserialise (json_process, parent); | ||
4134 | 287 | |||
4135 | 288 | } | ||
4136 | 289 | |||
4137 | 290 | return 0; | ||
4138 | 291 | |||
4139 | 292 | error: | ||
4140 | 293 | return -1; | ||
4141 | 294 | } | ||
4142 | 295 | |||
4143 | 296 | /** | ||
4144 | 297 | * process_type_enum_to_str: | ||
4145 | 298 | * | ||
4146 | 299 | * @type: ProcessType. | ||
4147 | 300 | * | ||
4148 | 301 | * Convert ProcessType to a string representation. | ||
4149 | 302 | * | ||
4150 | 303 | * Returns: string representation of @type, or NULL if not known. | ||
4151 | 304 | **/ | ||
4152 | 305 | const char * | ||
4153 | 306 | process_type_enum_to_str (ProcessType type) | ||
4154 | 307 | { | ||
4155 | 308 | state_enum_to_str (PROCESS_INVALID, type); | ||
4156 | 309 | state_enum_to_str (PROCESS_MAIN, type); | ||
4157 | 310 | state_enum_to_str (PROCESS_PRE_START, type); | ||
4158 | 311 | state_enum_to_str (PROCESS_POST_START, type); | ||
4159 | 312 | state_enum_to_str (PROCESS_PRE_STOP, type); | ||
4160 | 313 | state_enum_to_str (PROCESS_POST_STOP, type); | ||
4161 | 314 | |||
4162 | 315 | return NULL; | ||
4163 | 316 | } | ||
4164 | 317 | |||
4165 | 318 | /** | ||
4166 | 319 | * process_type_str_to_enum: | ||
4167 | 320 | * | ||
4168 | 321 | * @type: string ProcessType. | ||
4169 | 322 | * | ||
4170 | 323 | * Convert @type back into enum value. | ||
4171 | 324 | * | ||
4172 | 325 | * Returns: ProcessType representation of @type, or -1 if not known. | ||
4173 | 326 | **/ | ||
4174 | 327 | ProcessType | ||
4175 | 328 | process_type_str_to_enum (const char *type) | ||
4176 | 329 | { | ||
4177 | 330 | nih_assert (type); | ||
4178 | 331 | |||
4179 | 332 | state_str_to_enum (PROCESS_INVALID, type); | ||
4180 | 333 | state_str_to_enum (PROCESS_MAIN, type); | ||
4181 | 334 | state_str_to_enum (PROCESS_PRE_START, type); | ||
4182 | 335 | state_str_to_enum (PROCESS_POST_START, type); | ||
4183 | 336 | state_str_to_enum (PROCESS_PRE_STOP, type); | ||
4184 | 337 | state_str_to_enum (PROCESS_POST_STOP, type); | ||
4185 | 338 | |||
4186 | 339 | return -1; | ||
4187 | 340 | } | ||
4188 | 341 | |||
4189 | 119 | 342 | ||
4190 | === modified file 'init/process.h' | |||
4191 | --- init/process.h 2009-06-23 09:29:35 +0000 | |||
4192 | +++ init/process.h 2012-11-14 14:49:20 +0000 | |||
4193 | @@ -22,20 +22,30 @@ | |||
4194 | 22 | 22 | ||
4195 | 23 | #include <nih/macros.h> | 23 | #include <nih/macros.h> |
4196 | 24 | 24 | ||
4197 | 25 | #include <json.h> | ||
4198 | 25 | 26 | ||
4199 | 26 | /** | 27 | /** |
4200 | 27 | * ProcessType: | 28 | * ProcessType: |
4201 | 28 | * | 29 | * |
4202 | 29 | * This is used to enumerate the array of process definitions attached to | 30 | * This is used to enumerate the array of process definitions attached to |
4203 | 30 | * a job class, and the array of pids attached to a job instance. | 31 | * a job class, and the array of pids attached to a job instance. |
4204 | 32 | * | ||
4205 | 33 | * Note that PROCESS_INVALID would ideally be -1 but that isn't possible | ||
4206 | 34 | * since process_type_str_to_enum() would then not be able to distinguish | ||
4207 | 35 | * between an invalid ProcessType and the default value assigned to a | ||
4208 | 36 | * ProcessType. It also cannot be zero since that would upset iterating | ||
4209 | 37 | * through the (non-invalid) entries. | ||
4210 | 31 | **/ | 38 | **/ |
4211 | 32 | typedef enum process_type { | 39 | typedef enum process_type { |
4213 | 33 | PROCESS_MAIN, | 40 | /* initial value denoting no process */ |
4214 | 41 | PROCESS_INVALID = -2, | ||
4215 | 42 | |||
4216 | 43 | PROCESS_MAIN = 0, | ||
4217 | 34 | PROCESS_PRE_START, | 44 | PROCESS_PRE_START, |
4218 | 35 | PROCESS_POST_START, | 45 | PROCESS_POST_START, |
4219 | 36 | PROCESS_PRE_STOP, | 46 | PROCESS_PRE_STOP, |
4220 | 37 | PROCESS_POST_STOP, | 47 | PROCESS_POST_STOP, |
4222 | 38 | PROCESS_LAST | 48 | PROCESS_LAST, |
4223 | 39 | } ProcessType; | 49 | } ProcessType; |
4224 | 40 | 50 | ||
4225 | 41 | 51 | ||
4226 | @@ -65,7 +75,32 @@ | |||
4227 | 65 | 75 | ||
4228 | 66 | const char *process_name (ProcessType process) | 76 | const char *process_name (ProcessType process) |
4229 | 67 | __attribute__ ((const)); | 77 | __attribute__ ((const)); |
4231 | 68 | ProcessType process_from_name (const char *process); | 78 | |
4232 | 79 | json_object *process_serialise (const Process *process) | ||
4233 | 80 | __attribute__ ((malloc, warn_unused_result)); | ||
4234 | 81 | |||
4235 | 82 | Process *process_deserialise (json_object *json, const void *parent) | ||
4236 | 83 | __attribute__ ((malloc, warn_unused_result)); | ||
4237 | 84 | |||
4238 | 85 | json_object * | ||
4239 | 86 | process_serialise_all (const Process * const * const processes) | ||
4240 | 87 | __attribute__ ((malloc, warn_unused_result)); | ||
4241 | 88 | |||
4242 | 89 | ProcessType process_from_name (const char *process) | ||
4243 | 90 | __attribute__ ((warn_unused_result)); | ||
4244 | 91 | |||
4245 | 92 | int | ||
4246 | 93 | process_deserialise_all (json_object *json, const void *parent, | ||
4247 | 94 | Process **processes) | ||
4248 | 95 | __attribute__ ((warn_unused_result)); | ||
4249 | 96 | |||
4250 | 97 | const char * | ||
4251 | 98 | process_type_enum_to_str (ProcessType type) | ||
4252 | 99 | __attribute__ ((warn_unused_result)); | ||
4253 | 100 | |||
4254 | 101 | ProcessType | ||
4255 | 102 | process_type_str_to_enum (const char *type) | ||
4256 | 103 | __attribute__ ((warn_unused_result)); | ||
4257 | 69 | 104 | ||
4258 | 70 | NIH_END_EXTERN | 105 | NIH_END_EXTERN |
4259 | 71 | 106 | ||
4260 | 72 | 107 | ||
4261 | === modified file 'init/session.c' | |||
4262 | --- init/session.c 2011-12-09 14:07:11 +0000 | |||
4263 | +++ init/session.c 2012-11-14 14:49:20 +0000 | |||
4264 | @@ -47,6 +47,15 @@ | |||
4265 | 47 | #include "conf.h" | 47 | #include "conf.h" |
4266 | 48 | #include "paths.h" | 48 | #include "paths.h" |
4267 | 49 | 49 | ||
4268 | 50 | extern json_object *json_sessions; | ||
4269 | 51 | |||
4270 | 52 | /* Prototypes for static functions */ | ||
4271 | 53 | static json_object *session_serialise (const Session *session) | ||
4272 | 54 | __attribute__ ((malloc, warn_unused_result)); | ||
4273 | 55 | |||
4274 | 56 | static Session *session_deserialise (json_object *json) | ||
4275 | 57 | __attribute__ ((malloc, warn_unused_result)); | ||
4276 | 58 | |||
4277 | 50 | 59 | ||
4278 | 51 | /** | 60 | /** |
4279 | 52 | * sessions: | 61 | * sessions: |
4280 | @@ -66,8 +75,7 @@ | |||
4281 | 66 | 75 | ||
4282 | 67 | 76 | ||
4283 | 68 | /* Prototypes for static functions */ | 77 | /* Prototypes for static functions */ |
4286 | 69 | static void session_create_conf_source (Session *sesson); | 78 | static void session_create_conf_source (Session *sesson, int deserialised); |
4285 | 70 | |||
4287 | 71 | 79 | ||
4288 | 72 | /** | 80 | /** |
4289 | 73 | * session_init: | 81 | * session_init: |
4290 | @@ -90,7 +98,7 @@ | |||
4291 | 90 | * | 98 | * |
4292 | 91 | * Create a new session. | 99 | * Create a new session. |
4293 | 92 | * | 100 | * |
4295 | 93 | * Return new Session, or NULL on error. | 101 | * Returns: new Session, or NULL on error. |
4296 | 94 | **/ | 102 | **/ |
4297 | 95 | Session * | 103 | Session * |
4298 | 96 | session_new (const void *parent, | 104 | session_new (const void *parent, |
4299 | @@ -137,7 +145,7 @@ | |||
4300 | 137 | * | 145 | * |
4301 | 138 | * Create a new session, based on the specified D-Bus message. | 146 | * Create a new session, based on the specified D-Bus message. |
4302 | 139 | * | 147 | * |
4304 | 140 | * Return new Session, or NULL on error. | 148 | * Returns: new Session, or NULL on error. |
4305 | 141 | **/ | 149 | **/ |
4306 | 142 | Session * | 150 | Session * |
4307 | 143 | session_from_dbus (const void *parent, | 151 | session_from_dbus (const void *parent, |
4308 | @@ -248,7 +256,7 @@ | |||
4309 | 248 | continue; | 256 | continue; |
4310 | 249 | 257 | ||
4311 | 250 | /* Found a user with the same uid but different | 258 | /* Found a user with the same uid but different |
4313 | 251 | * conf_dir to the existing session user. Either the | 259 | * conf_path to the existing session user. Either the |
4314 | 252 | * original user has been deleted and a new user created | 260 | * original user has been deleted and a new user created |
4315 | 253 | * with the same uid, or the original users home | 261 | * with the same uid, or the original users home |
4316 | 254 | * directory has changed since they first started | 262 | * directory has changed since they first started |
4317 | @@ -273,7 +281,7 @@ | |||
4318 | 273 | } | 281 | } |
4319 | 274 | 282 | ||
4320 | 275 | if (! session->conf_path) | 283 | if (! session->conf_path) |
4322 | 276 | session_create_conf_source (session); | 284 | session_create_conf_source (session, FALSE); |
4323 | 277 | 285 | ||
4324 | 278 | return session; | 286 | return session; |
4325 | 279 | } | 287 | } |
4326 | @@ -282,46 +290,54 @@ | |||
4327 | 282 | /* Didn't find one, make a new one */ | 290 | /* Didn't find one, make a new one */ |
4328 | 283 | session = NIH_MUST (session_new (parent, unix_process_id ? root : NULL, | 291 | session = NIH_MUST (session_new (parent, unix_process_id ? root : NULL, |
4329 | 284 | unix_user)); | 292 | unix_user)); |
4331 | 285 | session_create_conf_source (session); | 293 | session_create_conf_source (session, FALSE); |
4332 | 286 | 294 | ||
4333 | 287 | return session; | 295 | return session; |
4334 | 288 | } | 296 | } |
4335 | 289 | 297 | ||
4336 | 290 | /** | 298 | /** |
4337 | 291 | * session_create_conf_source: | 299 | * session_create_conf_source: |
4339 | 292 | * @session: Session. | 300 | * @session: Session, |
4340 | 301 | * @deserialised: TRUE if ConfSource is to be created from a deserialised | ||
4341 | 302 | * session object. | ||
4342 | 293 | * | 303 | * |
4344 | 294 | * Create a new ConfSouce object and associate the specified session | 304 | * Create a new ConfSource object and associate the specified Session |
4345 | 295 | * with it. | 305 | * with it. |
4346 | 296 | **/ | 306 | **/ |
4347 | 297 | static void | 307 | static void |
4349 | 298 | session_create_conf_source (Session *session) | 308 | session_create_conf_source (Session *session, int deserialised) |
4350 | 299 | { | 309 | { |
4351 | 300 | ConfSource *source; | 310 | ConfSource *source; |
4352 | 301 | 311 | ||
4353 | 302 | nih_assert (session != NULL); | 312 | nih_assert (session != NULL); |
4370 | 303 | nih_assert (session->conf_path == NULL); | 313 | nih_assert (deserialised |
4371 | 304 | 314 | ? session->conf_path != NULL | |
4372 | 305 | if (session->chroot) | 315 | : session->conf_path == NULL); |
4373 | 306 | session->conf_path = NIH_MUST (nih_strdup (NULL, session->chroot)); | 316 | |
4374 | 307 | if (session->user) { | 317 | session_init (); |
4375 | 308 | struct passwd *pwd; | 318 | |
4376 | 309 | 319 | if (! deserialised) { | |
4377 | 310 | pwd = getpwuid (session->user); | 320 | if (session->chroot) |
4378 | 311 | if (! pwd) { | 321 | session->conf_path = NIH_MUST (nih_strdup (NULL, session->chroot)); |
4379 | 312 | nih_error ("%d: %s: %s", session->user, | 322 | if (session->user) { |
4380 | 313 | _("Unable to lookup home directory"), | 323 | struct passwd *pwd; |
4381 | 314 | strerror (errno)); | 324 | |
4382 | 315 | 325 | pwd = getpwuid (session->user); | |
4383 | 316 | nih_free (session->conf_path); | 326 | if (! pwd) { |
4384 | 317 | session->conf_path = NULL; | 327 | nih_error ("%d: %s: %s", session->user, |
4385 | 318 | return; | 328 | _("Unable to lookup home directory"), |
4386 | 329 | strerror (errno)); | ||
4387 | 330 | |||
4388 | 331 | nih_free (session->conf_path); | ||
4389 | 332 | session->conf_path = NULL; | ||
4390 | 333 | return; | ||
4391 | 334 | } | ||
4392 | 335 | |||
4393 | 336 | NIH_MUST (nih_strcat_sprintf (&session->conf_path, NULL, "%s/%s", | ||
4394 | 337 | pwd->pw_dir, USERCONFDIR)); | ||
4395 | 338 | } else { | ||
4396 | 339 | NIH_MUST (nih_strcat (&session->conf_path, NULL, CONFDIR)); | ||
4397 | 319 | } | 340 | } |
4398 | 320 | |||
4399 | 321 | NIH_MUST (nih_strcat_sprintf (&session->conf_path, NULL, "%s/%s", | ||
4400 | 322 | pwd->pw_dir, USERCONFDIR)); | ||
4401 | 323 | } else { | ||
4402 | 324 | NIH_MUST (nih_strcat (&session->conf_path, NULL, CONFDIR)); | ||
4403 | 325 | } | 341 | } |
4404 | 326 | 342 | ||
4405 | 327 | source = NIH_MUST (conf_source_new (session, session->conf_path, | 343 | source = NIH_MUST (conf_source_new (session, session->conf_path, |
4406 | @@ -339,8 +355,257 @@ | |||
4407 | 339 | nih_free (err); | 355 | nih_free (err); |
4408 | 340 | 356 | ||
4409 | 341 | nih_free (source); | 357 | nih_free (source); |
4410 | 342 | nih_free (session->conf_path); | ||
4411 | 343 | session->conf_path = NULL; | ||
4412 | 344 | return; | 358 | return; |
4413 | 345 | } | 359 | } |
4414 | 346 | } | 360 | } |
4415 | 361 | |||
4416 | 362 | /** | ||
4417 | 363 | * session_serialise: | ||
4418 | 364 | * @session: session to serialise. | ||
4419 | 365 | * | ||
4420 | 366 | * Convert @session into a JSON representation for serialisation. | ||
4421 | 367 | * | ||
4422 | 368 | * Caller must free returned value using json_object_put(). | ||
4423 | 369 | * | ||
4424 | 370 | * Returns: JSON-serialised Session object, or NULL on error. | ||
4425 | 371 | **/ | ||
4426 | 372 | static json_object * | ||
4427 | 373 | session_serialise (const Session *session) | ||
4428 | 374 | { | ||
4429 | 375 | json_object *json; | ||
4430 | 376 | json_object *conf_path = NULL; | ||
4431 | 377 | json_object *chroot = NULL; | ||
4432 | 378 | json_object *user; | ||
4433 | 379 | |||
4434 | 380 | nih_assert (session); | ||
4435 | 381 | |||
4436 | 382 | session_init (); | ||
4437 | 383 | |||
4438 | 384 | json = json_object_new_object (); | ||
4439 | 385 | if (! json) | ||
4440 | 386 | return NULL; | ||
4441 | 387 | |||
4442 | 388 | if (session->chroot) { | ||
4443 | 389 | chroot = json_object_new_string (session->chroot); | ||
4444 | 390 | if (! chroot) | ||
4445 | 391 | goto error; | ||
4446 | 392 | } | ||
4447 | 393 | |||
4448 | 394 | json_object_object_add (json, "chroot", chroot); | ||
4449 | 395 | |||
4450 | 396 | user = state_new_json_int (session->user); | ||
4451 | 397 | if (! user) | ||
4452 | 398 | goto error; | ||
4453 | 399 | |||
4454 | 400 | json_object_object_add (json, "user", user); | ||
4455 | 401 | |||
4456 | 402 | if (session->conf_path) { | ||
4457 | 403 | conf_path = json_object_new_string (session->conf_path); | ||
4458 | 404 | if (! conf_path) | ||
4459 | 405 | goto error; | ||
4460 | 406 | } | ||
4461 | 407 | |||
4462 | 408 | json_object_object_add (json, "conf_path", conf_path); | ||
4463 | 409 | |||
4464 | 410 | return json; | ||
4465 | 411 | |||
4466 | 412 | error: | ||
4467 | 413 | json_object_put (json); | ||
4468 | 414 | return NULL; | ||
4469 | 415 | |||
4470 | 416 | } | ||
4471 | 417 | |||
4472 | 418 | /** | ||
4473 | 419 | * session_serialise_all: | ||
4474 | 420 | * | ||
4475 | 421 | * Convert existing Session objects to JSON representation. | ||
4476 | 422 | * | ||
4477 | 423 | * Returns: JSON object containing array of Sessions, or NULL on error. | ||
4478 | 424 | **/ | ||
4479 | 425 | json_object * | ||
4480 | 426 | session_serialise_all (void) | ||
4481 | 427 | { | ||
4482 | 428 | json_object *json; | ||
4483 | 429 | json_object *json_session; | ||
4484 | 430 | |||
4485 | 431 | session_init (); | ||
4486 | 432 | |||
4487 | 433 | json = json_object_new_array (); | ||
4488 | 434 | if (! json) | ||
4489 | 435 | return NULL; | ||
4490 | 436 | |||
4491 | 437 | NIH_LIST_FOREACH (sessions, iter) { | ||
4492 | 438 | Session *session = (Session *)iter; | ||
4493 | 439 | |||
4494 | 440 | json_session = session_serialise (session); | ||
4495 | 441 | |||
4496 | 442 | if (! json_session) | ||
4497 | 443 | goto error; | ||
4498 | 444 | |||
4499 | 445 | if (json_object_array_add (json, json_session) < 0) | ||
4500 | 446 | goto error; | ||
4501 | 447 | } | ||
4502 | 448 | |||
4503 | 449 | return json; | ||
4504 | 450 | |||
4505 | 451 | error: | ||
4506 | 452 | json_object_put (json); | ||
4507 | 453 | return NULL; | ||
4508 | 454 | } | ||
4509 | 455 | |||
4510 | 456 | /** | ||
4511 | 457 | * session_deserialise: | ||
4512 | 458 | * @json: JSON-serialised Session object to deserialise. | ||
4513 | 459 | * | ||
4514 | 460 | * Convert @json into a Session object. | ||
4515 | 461 | * | ||
4516 | 462 | * Returns: Session object, or NULL on error. | ||
4517 | 463 | **/ | ||
4518 | 464 | static Session * | ||
4519 | 465 | session_deserialise (json_object *json) | ||
4520 | 466 | { | ||
4521 | 467 | Session *session = NULL; | ||
4522 | 468 | nih_local const char *chroot = NULL; | ||
4523 | 469 | uid_t user; | ||
4524 | 470 | |||
4525 | 471 | nih_assert (json); | ||
4526 | 472 | |||
4527 | 473 | if (! state_check_json_type (json, object)) | ||
4528 | 474 | return NULL; | ||
4529 | 475 | |||
4530 | 476 | /* Note no check on value returned since chroot may be NULL */ | ||
4531 | 477 | if (! state_get_json_string_var (json, "chroot", NULL, chroot)) | ||
4532 | 478 | return NULL; | ||
4533 | 479 | |||
4534 | 480 | if (! state_get_json_int_var (json, "user", user)) | ||
4535 | 481 | return NULL; | ||
4536 | 482 | |||
4537 | 483 | /* Create a new session */ | ||
4538 | 484 | session = NIH_MUST (session_new (NULL, chroot, user)); | ||
4539 | 485 | |||
4540 | 486 | if (! state_get_json_string_var_to_obj (json, session, conf_path)) | ||
4541 | 487 | goto error; | ||
4542 | 488 | |||
4543 | 489 | return session; | ||
4544 | 490 | |||
4545 | 491 | error: | ||
4546 | 492 | nih_free (session); | ||
4547 | 493 | return NULL; | ||
4548 | 494 | } | ||
4549 | 495 | |||
4550 | 496 | /** | ||
4551 | 497 | * session_deserialise_all: | ||
4552 | 498 | * | ||
4553 | 499 | * @json: root of JSON-serialised state. | ||
4554 | 500 | * | ||
4555 | 501 | * Convert JSON representation of sessions back into Session objects. | ||
4556 | 502 | * | ||
4557 | 503 | * Returns: 0 on success, -1 on error. | ||
4558 | 504 | **/ | ||
4559 | 505 | int | ||
4560 | 506 | session_deserialise_all (json_object *json) | ||
4561 | 507 | { | ||
4562 | 508 | Session *session; | ||
4563 | 509 | |||
4564 | 510 | nih_assert (json); | ||
4565 | 511 | |||
4566 | 512 | session_init (); | ||
4567 | 513 | |||
4568 | 514 | nih_assert (NIH_LIST_EMPTY (sessions)); | ||
4569 | 515 | |||
4570 | 516 | json_sessions = json_object_object_get (json, "sessions"); | ||
4571 | 517 | |||
4572 | 518 | if (! json_sessions) | ||
4573 | 519 | goto error; | ||
4574 | 520 | |||
4575 | 521 | if (! state_check_json_type (json_sessions, array)) | ||
4576 | 522 | goto error; | ||
4577 | 523 | |||
4578 | 524 | for (int i = 0; i < json_object_array_length (json_sessions); i++) { | ||
4579 | 525 | json_object *json_session; | ||
4580 | 526 | |||
4581 | 527 | json_session = json_object_array_get_idx (json_sessions, i); | ||
4582 | 528 | if (! json_session) | ||
4583 | 529 | goto error; | ||
4584 | 530 | |||
4585 | 531 | if (! state_check_json_type (json_session, object)) | ||
4586 | 532 | goto error; | ||
4587 | 533 | |||
4588 | 534 | session = session_deserialise (json_session); | ||
4589 | 535 | if (! session) | ||
4590 | 536 | goto error; | ||
4591 | 537 | |||
4592 | 538 | session_create_conf_source (session, TRUE); | ||
4593 | 539 | } | ||
4594 | 540 | |||
4595 | 541 | return 0; | ||
4596 | 542 | |||
4597 | 543 | error: | ||
4598 | 544 | return -1; | ||
4599 | 545 | } | ||
4600 | 546 | |||
4601 | 547 | /** | ||
4602 | 548 | * session_get_index: | ||
4603 | 549 | * | ||
4604 | 550 | * @session: session. | ||
4605 | 551 | * | ||
4606 | 552 | * Determine JSON-serialised array index for specified @session. | ||
4607 | 553 | * | ||
4608 | 554 | * Returns: zero-based array index for @session, or -1 on error. | ||
4609 | 555 | **/ | ||
4610 | 556 | int | ||
4611 | 557 | session_get_index (const Session *session) | ||
4612 | 558 | { | ||
4613 | 559 | int i; | ||
4614 | 560 | |||
4615 | 561 | /* Handle NULL session (which is not encoded) */ | ||
4616 | 562 | if (! session) | ||
4617 | 563 | return 0; | ||
4618 | 564 | |||
4619 | 565 | /* Sessions are serialised in order, so just return the list | ||
4620 | 566 | * index. | ||
4621 | 567 | */ | ||
4622 | 568 | i = 1; | ||
4623 | 569 | NIH_LIST_FOREACH (sessions, iter) { | ||
4624 | 570 | Session *s = (Session *)iter; | ||
4625 | 571 | |||
4626 | 572 | if (s == session) | ||
4627 | 573 | return i; | ||
4628 | 574 | |||
4629 | 575 | ++i; | ||
4630 | 576 | } | ||
4631 | 577 | |||
4632 | 578 | return -1; | ||
4633 | 579 | } | ||
4634 | 580 | |||
4635 | 581 | /** | ||
4636 | 582 | * session_from_index: | ||
4637 | 583 | * | ||
4638 | 584 | * @idx: zero-based index. | ||
4639 | 585 | * | ||
4640 | 586 | * Lookup session by index number. | ||
4641 | 587 | * | ||
4642 | 588 | * Returns: Session (which may be the NULL session). | ||
4643 | 589 | **/ | ||
4644 | 590 | Session * | ||
4645 | 591 | session_from_index (int idx) | ||
4646 | 592 | { | ||
4647 | 593 | int i; | ||
4648 | 594 | Session *session; | ||
4649 | 595 | |||
4650 | 596 | nih_assert (idx >= 0); | ||
4651 | 597 | |||
4652 | 598 | /* NULL session */ | ||
4653 | 599 | if (! idx) | ||
4654 | 600 | return NULL; | ||
4655 | 601 | |||
4656 | 602 | i = 1; | ||
4657 | 603 | NIH_LIST_FOREACH (sessions, iter) { | ||
4658 | 604 | session = (Session *)iter; | ||
4659 | 605 | |||
4660 | 606 | if (i == idx) | ||
4661 | 607 | return session; | ||
4662 | 608 | } | ||
4663 | 609 | |||
4664 | 610 | nih_assert_not_reached (); | ||
4665 | 611 | } | ||
4666 | 347 | 612 | ||
4667 | === modified file 'init/session.h' | |||
4668 | --- init/session.h 2011-07-25 14:28:33 +0000 | |||
4669 | +++ init/session.h 2012-11-14 14:49:20 +0000 | |||
4670 | @@ -27,6 +27,7 @@ | |||
4671 | 27 | 27 | ||
4672 | 28 | #include <nih-dbus/dbus_message.h> | 28 | #include <nih-dbus/dbus_message.h> |
4673 | 29 | 29 | ||
4674 | 30 | #include <json.h> | ||
4675 | 30 | 31 | ||
4676 | 31 | /** | 32 | /** |
4677 | 32 | * Session: | 33 | * Session: |
4678 | @@ -77,12 +78,24 @@ | |||
4679 | 77 | 78 | ||
4680 | 78 | extern NihList *sessions; | 79 | extern NihList *sessions; |
4681 | 79 | 80 | ||
4688 | 80 | void session_init (void); | 81 | void session_init (void); |
4689 | 81 | 82 | ||
4690 | 82 | Session *session_new (const void *parent, const char *chroot, uid_t user) | 83 | Session * session_new (const void *parent, const char *chroot, uid_t user) |
4691 | 83 | __attribute__ ((malloc, warn_unused_result)); | 84 | __attribute__ ((malloc, warn_unused_result)); |
4692 | 84 | 85 | ||
4693 | 85 | Session *session_from_dbus (const void *parent, NihDBusMessage *message); | 86 | Session * session_from_dbus (const void *parent, NihDBusMessage *message); |
4694 | 87 | |||
4695 | 88 | json_object * session_serialise_all (void) | ||
4696 | 89 | __attribute__ ((malloc, warn_unused_result)); | ||
4697 | 90 | |||
4698 | 91 | int session_deserialise_all (json_object *json) | ||
4699 | 92 | __attribute__ ((warn_unused_result)); | ||
4700 | 93 | |||
4701 | 94 | int session_get_index (const Session *session) | ||
4702 | 95 | __attribute__ ((warn_unused_result)); | ||
4703 | 96 | |||
4704 | 97 | Session * session_from_index (int idx) | ||
4705 | 98 | __attribute__ ((warn_unused_result)); | ||
4706 | 86 | 99 | ||
4707 | 87 | NIH_END_EXTERN | 100 | NIH_END_EXTERN |
4708 | 88 | 101 | ||
4709 | 89 | 102 | ||
4710 | === added file 'init/state.c' | |||
4711 | --- init/state.c 1970-01-01 00:00:00 +0000 | |||
4712 | +++ init/state.c 2012-11-14 14:49:20 +0000 | |||
4713 | @@ -0,0 +1,2008 @@ | |||
4714 | 1 | /* upstart | ||
4715 | 2 | * | ||
4716 | 3 | * state.c - serialisation and deserialisation support. | ||
4717 | 4 | * | ||
4718 | 5 | * Copyright © 2012 Canonical Ltd. | ||
4719 | 6 | * Author: James Hunt <james.hunt@canonical.com>. | ||
4720 | 7 | * | ||
4721 | 8 | * This program is free software; you can redistribute it and/or modify | ||
4722 | 9 | * it under the terms of the GNU General Public License version 2, as | ||
4723 | 10 | * published by the Free Software Foundation. | ||
4724 | 11 | * | ||
4725 | 12 | * This program is distributed in the hope that it will be useful, | ||
4726 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4727 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4728 | 15 | * GNU General Public License for more details. | ||
4729 | 16 | * | ||
4730 | 17 | * You should have received a copy of the GNU General Public License along | ||
4731 | 18 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
4732 | 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
4733 | 20 | */ | ||
4734 | 21 | |||
4735 | 22 | #include <string.h> | ||
4736 | 23 | #include <sys/types.h> | ||
4737 | 24 | #include <signal.h> | ||
4738 | 25 | #include <unistd.h> | ||
4739 | 26 | #include <errno.h> | ||
4740 | 27 | #include <sys/select.h> | ||
4741 | 28 | #include <unistd.h> | ||
4742 | 29 | #include <fcntl.h> | ||
4743 | 30 | |||
4744 | 31 | #include <nih/macros.h> | ||
4745 | 32 | #include <nih/logging.h> | ||
4746 | 33 | #include <nih/string.h> | ||
4747 | 34 | #include <nih/io.h> | ||
4748 | 35 | |||
4749 | 36 | #include "paths.h" | ||
4750 | 37 | #include "state.h" | ||
4751 | 38 | #include "session.h" | ||
4752 | 39 | #include "event.h" | ||
4753 | 40 | #include "job_class.h" | ||
4754 | 41 | #include "job.h" | ||
4755 | 42 | #include "environ.h" | ||
4756 | 43 | #include "blocked.h" | ||
4757 | 44 | #include "conf.h" | ||
4758 | 45 | #include "control.h" | ||
4759 | 46 | |||
4760 | 47 | json_object *json_sessions = NULL; | ||
4761 | 48 | json_object *json_events = NULL; | ||
4762 | 49 | json_object *json_classes = NULL; | ||
4763 | 50 | |||
4764 | 51 | extern int use_session_bus; | ||
4765 | 52 | |||
4766 | 53 | /** | ||
4767 | 54 | * args_copy: | ||
4768 | 55 | * | ||
4769 | 56 | * Copy of original argv used when re-executing to ensure same | ||
4770 | 57 | * command-line is used. Required since we clear the actual args for | ||
4771 | 58 | * ps(1) et al. | ||
4772 | 59 | */ | ||
4773 | 60 | char **args_copy = NULL; | ||
4774 | 61 | |||
4775 | 62 | /** | ||
4776 | 63 | * restart: | ||
4777 | 64 | * | ||
4778 | 65 | * This is set to TRUE if we're being re-exec'd by an existing init | ||
4779 | 66 | * process. | ||
4780 | 67 | **/ | ||
4781 | 68 | int restart = FALSE; | ||
4782 | 69 | |||
4783 | 70 | /* Prototypes for static functions */ | ||
4784 | 71 | static json_object * | ||
4785 | 72 | state_serialise_blocked (const Blocked *blocked) | ||
4786 | 73 | __attribute__ ((malloc, warn_unused_result)); | ||
4787 | 74 | |||
4788 | 75 | static Blocked * | ||
4789 | 76 | state_deserialise_blocked (void *parent, json_object *json, NihList *list) | ||
4790 | 77 | __attribute__ ((malloc, warn_unused_result)); | ||
4791 | 78 | |||
4792 | 79 | static JobClass * | ||
4793 | 80 | state_index_to_job_class (int job_class_index) | ||
4794 | 81 | __attribute__ ((warn_unused_result)); | ||
4795 | 82 | |||
4796 | 83 | static Job * | ||
4797 | 84 | state_get_job (const char *job_class, const char *job_name) | ||
4798 | 85 | __attribute__ ((warn_unused_result)); | ||
4799 | 86 | |||
4800 | 87 | /** | ||
4801 | 88 | * state_read: | ||
4802 | 89 | * | ||
4803 | 90 | * @fd: Open file descriptor to read JSON from. | ||
4804 | 91 | * | ||
4805 | 92 | * Read JSON-encoded state from specified file descriptor and recreate | ||
4806 | 93 | * all internal objects based on JSON representation. The read will | ||
4807 | 94 | * timeout, resulting in a failure after STATE_WAIT_SECS seconds | ||
4808 | 95 | * indicating a problem with the child. | ||
4809 | 96 | * | ||
4810 | 97 | * Returns: 0 on success, or -1 on error. | ||
4811 | 98 | **/ | ||
4812 | 99 | int | ||
4813 | 100 | state_read (int fd) | ||
4814 | 101 | { | ||
4815 | 102 | int nfds; | ||
4816 | 103 | int ret; | ||
4817 | 104 | fd_set readfds; | ||
4818 | 105 | struct timeval timeout; | ||
4819 | 106 | |||
4820 | 107 | nih_assert (fd != -1); | ||
4821 | 108 | |||
4822 | 109 | state_get_timeout (timeout.tv_sec); | ||
4823 | 110 | timeout.tv_usec = 0; | ||
4824 | 111 | |||
4825 | 112 | nfds = 1 + fd; | ||
4826 | 113 | |||
4827 | 114 | while (TRUE) { | ||
4828 | 115 | FD_ZERO (&readfds); | ||
4829 | 116 | FD_SET (fd, &readfds); | ||
4830 | 117 | |||
4831 | 118 | ret = select (nfds, &readfds, NULL, NULL, | ||
4832 | 119 | timeout.tv_sec < 0 ? NULL : &timeout); | ||
4833 | 120 | |||
4834 | 121 | if (ret < 0 && (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)) | ||
4835 | 122 | return -1; | ||
4836 | 123 | |||
4837 | 124 | if (FD_ISSET (fd, &readfds)) | ||
4838 | 125 | break; | ||
4839 | 126 | } | ||
4840 | 127 | |||
4841 | 128 | nih_assert (ret == 1); | ||
4842 | 129 | |||
4843 | 130 | /* Now, read the data */ | ||
4844 | 131 | if (state_read_objects (fd) < 0) | ||
4845 | 132 | return -1; | ||
4846 | 133 | |||
4847 | 134 | return 0; | ||
4848 | 135 | } | ||
4849 | 136 | |||
4850 | 137 | /** | ||
4851 | 138 | * state_write: | ||
4852 | 139 | * | ||
4853 | 140 | * @fd: Open file descriptor to write JSON to, | ||
4854 | 141 | * @state_data: JSON string representing internal object state, | ||
4855 | 142 | * @len: length of @state_data. | ||
4856 | 143 | * | ||
4857 | 144 | * Write internal state to specified file descriptor in JSON format. | ||
4858 | 145 | * | ||
4859 | 146 | * Signals are assumed to be blocked when this call is made. | ||
4860 | 147 | * | ||
4861 | 148 | * Note the timeout - it is possible that the new PID 1 instance may be | ||
4862 | 149 | * unable to read from its end of the file descriptor, either due to | ||
4863 | 150 | * some error scenario or more likely due to it not supporting stateful | ||
4864 | 151 | * re-exec. Hence, we must have a way to detect this and abort the | ||
4865 | 152 | * child. | ||
4866 | 153 | * | ||
4867 | 154 | * Returns: 0 on success, or -1 on error. | ||
4868 | 155 | **/ | ||
4869 | 156 | int | ||
4870 | 157 | state_write (int fd, const char *state_data, size_t len) | ||
4871 | 158 | { | ||
4872 | 159 | int nfds; | ||
4873 | 160 | int ret; | ||
4874 | 161 | fd_set writefds; | ||
4875 | 162 | struct timeval timeout; | ||
4876 | 163 | |||
4877 | 164 | nih_assert (fd != -1); | ||
4878 | 165 | nih_assert (state_data); | ||
4879 | 166 | nih_assert (len); | ||
4880 | 167 | |||
4881 | 168 | /* must be called from child process */ | ||
4882 | 169 | nih_assert (getpid () != (pid_t)1); | ||
4883 | 170 | |||
4884 | 171 | state_get_timeout (timeout.tv_sec); | ||
4885 | 172 | timeout.tv_usec = 0; | ||
4886 | 173 | |||
4887 | 174 | nfds = 1 + fd; | ||
4888 | 175 | |||
4889 | 176 | while (TRUE) { | ||
4890 | 177 | FD_ZERO (&writefds); | ||
4891 | 178 | FD_SET (fd, &writefds); | ||
4892 | 179 | |||
4893 | 180 | ret = select (nfds, NULL, &writefds, NULL, | ||
4894 | 181 | timeout.tv_sec < 0 ? NULL : &timeout); | ||
4895 | 182 | |||
4896 | 183 | if (ret < 0 && (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)) | ||
4897 | 184 | return -1; | ||
4898 | 185 | |||
4899 | 186 | if (FD_ISSET (fd, &writefds)) | ||
4900 | 187 | break; | ||
4901 | 188 | } | ||
4902 | 189 | |||
4903 | 190 | nih_assert (ret == 1); | ||
4904 | 191 | |||
4905 | 192 | if (state_write_objects (fd, state_data, len) < 0) | ||
4906 | 193 | return -1; | ||
4907 | 194 | |||
4908 | 195 | return 0; | ||
4909 | 196 | } | ||
4910 | 197 | |||
4911 | 198 | |||
4912 | 199 | /** | ||
4913 | 200 | * state_read_objects: | ||
4914 | 201 | * | ||
4915 | 202 | * @fd: file descriptor to read serialisation data from. | ||
4916 | 203 | * | ||
4917 | 204 | * Read serialisation data from specified file descriptor. | ||
4918 | 205 | * @fd is assumed to be open and readable. | ||
4919 | 206 | * | ||
4920 | 207 | * Returns: 0 on success, -1 on error. | ||
4921 | 208 | **/ | ||
4922 | 209 | int | ||
4923 | 210 | state_read_objects (int fd) | ||
4924 | 211 | { | ||
4925 | 212 | ssize_t ret; | ||
4926 | 213 | int initial_size = 4096; | ||
4927 | 214 | nih_local NihIoBuffer *buffer = NULL; | ||
4928 | 215 | nih_local char *buf = NULL; | ||
4929 | 216 | |||
4930 | 217 | nih_assert (fd != -1); | ||
4931 | 218 | |||
4932 | 219 | buffer = nih_io_buffer_new (NULL); | ||
4933 | 220 | |||
4934 | 221 | buf = nih_alloc (NULL, initial_size); | ||
4935 | 222 | if (! buf) | ||
4936 | 223 | goto error; | ||
4937 | 224 | |||
4938 | 225 | /* Read the JSON data into the buffer */ | ||
4939 | 226 | do { | ||
4940 | 227 | if (nih_io_buffer_resize (buffer, initial_size) < 0) | ||
4941 | 228 | goto error; | ||
4942 | 229 | |||
4943 | 230 | ret = read (fd, buf, sizeof (buf)); | ||
4944 | 231 | if (ret < 0) { | ||
4945 | 232 | if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) | ||
4946 | 233 | goto error; | ||
4947 | 234 | continue; | ||
4948 | 235 | } else if (! ret) | ||
4949 | 236 | break; | ||
4950 | 237 | |||
4951 | 238 | if (nih_io_buffer_push (buffer, buf, ret) < 0) | ||
4952 | 239 | goto error; | ||
4953 | 240 | } while (TRUE); | ||
4954 | 241 | |||
4955 | 242 | /* Recreate internal state from JSON */ | ||
4956 | 243 | if (state_from_string (buffer->buf) < 0) | ||
4957 | 244 | goto error; | ||
4958 | 245 | |||
4959 | 246 | return 0; | ||
4960 | 247 | |||
4961 | 248 | error: | ||
4962 | 249 | return -1; | ||
4963 | 250 | } | ||
4964 | 251 | |||
4965 | 252 | /** | ||
4966 | 253 | * state_write_objects: | ||
4967 | 254 | * | ||
4968 | 255 | * @fd: file descriptor to write serialisation data on, | ||
4969 | 256 | * @state_data: JSON string representing internal object state, | ||
4970 | 257 | * @len: length of @state_data. | ||
4971 | 258 | * | ||
4972 | 259 | * Write serialisation data to specified file descriptor. | ||
4973 | 260 | * @fd is assumed to be open and valid to write to. | ||
4974 | 261 | * | ||
4975 | 262 | * Returns: 0 on success, -1 on error. | ||
4976 | 263 | **/ | ||
4977 | 264 | int | ||
4978 | 265 | state_write_objects (int fd, const char *state_data, size_t len) | ||
4979 | 266 | { | ||
4980 | 267 | ssize_t ret; | ||
4981 | 268 | |||
4982 | 269 | nih_assert (fd != -1); | ||
4983 | 270 | nih_assert (state_data); | ||
4984 | 271 | nih_assert (len); | ||
4985 | 272 | |||
4986 | 273 | ret = write (fd, state_data, len); | ||
4987 | 274 | |||
4988 | 275 | return (ret < 0 ? -1 : 0); | ||
4989 | 276 | } | ||
4990 | 277 | |||
4991 | 278 | /** | ||
4992 | 279 | * state_to_string: | ||
4993 | 280 | * | ||
4994 | 281 | * @json_string; newly-allocated string, | ||
4995 | 282 | * @len: length of @json_string. | ||
4996 | 283 | * | ||
4997 | 284 | * Serialise internal data structures to a JSON string. | ||
4998 | 285 | * | ||
4999 | 286 | * Returns: 0 on success, -1 on error. | ||
5000 | 287 | **/ |
Hi James,
Reviewing just the parts of the diff that are new since the last time I looked at the branch.
This part of init/session.c looks strange:
+ if (getenv ("UPSTART_TESTS"))
NihError *err;
+ return;
+
if (conf_source_reload (source) < 0) {
The commit message only says: create_ conf_source( ): Don't reload conf sources when in
- session_
testing mode.
without explaining why.
In init/tests/ test_util. c:
+ * Returns 0 if strings are identical or both NULL, else 1.
+ **/
+int
+string_check (const char *a, const char *b)
+{
+ if ((a == b) && !a)
+ return 0;
+
This '&& !a' seems unnecessary - surely if a == b and are /not/ null, there's no need to call strcmp to confirm that the strings are equal.
=== modified file 'init/blocked.c' type_enum_ to_str (BlockedType ty type_str_ to_enum (const char *type)
state_ str_to_ enum (BLOCKED_JOB, type);
state_ str_to_ enum (BLOCKED_EVENT, type);
state_ str_to_ enum (BLOCKED_ EMIT_METHOD, type); type_str_ to_enum (const char *ty
state_ str_to_ enum (BLOCKED_ INSTANCE_ STOP_METHOD, type);
state_ str_to_ enum (BLOCKED_ INSTANCE_ RESTART_ METHOD, type);
--- init/blocked.c 2012-07-30 14:07:37 +0000
+++ init/blocked.c 2012-11-13 06:01:51 +0000
@@ -131,6 +131,9 @@ blocked_
BlockedType
blocked_
{
+ if (! type)
+ goto error;
+
@@ -141,5 +144,6 @@ blocked_
+error:
return -1;
}
The above is documented as "Made resilient to error conditions". But this doesn't appear to be a real-world error condition at all; the only place in the code that blocked_ type_str_ to_enum( ) is called is in state.c:
if (! state_get_ json_string_ var_strict (json, "type", NULL, blocked_type_str))
goto error;
This means there's no possibility of the function ever being called with a null argument (and likewise for the other str_to_enum functions), so this must be here just for the test suite. I don't think it's a good idea to complicate either the code or the test suite by handling errors that the code is written to never allow! I've pushed a branch to address this at
lp:~vorlon/upstart/stateful-reexec-dont-shape-code-to-impossible-tests and raised an MP for your consideration.
=== modified file 'init/job_ process. c'
while (p && (*p == '\n'))
p++;
--- init/job_process.c 2012-08-25 08:53:37 +0000
+++ init/job_process.c 2012-11-13 10:47:50 +0000
@@ -211,7 +211,7 @@ job_process_run (Job *job,
- if ((! nl) || (! *p)) {
/* Strip off the newline(s) */
if (nl)
*nl = '\0';
+ if ((! nl) || (p && ! *p)) {
This change is documented as "Stop potential NULL-pointer dereference". But here's the full context:
p = nl = strchr (script, '\n');
while (p && (*p == '\n'))
p++;
if ((! nl) || (p && ! *p)) {
/* Strip off the newline(s) */
if (nl)
...