Skip to content

Commit 7bebbc4

Browse files
committed
dinit-service: Do parameter substitution in consumer-of
Seems useful for s6-log/vlogger based logging systems. Signed-off-by: Mobin Aydinfar <mobin@mobintestserver.ir>
1 parent f349b88 commit 7bebbc4

File tree

8 files changed

+63
-2
lines changed

8 files changed

+63
-2
lines changed

doc/manpages/dinit-service.5.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ Specifies that this service consumes (as its standard input) the output of anoth
489489
For example, this allows this service to act as a logging agent for another service.
490490
The named service must be a process-based service with \fBlog\-type\fR set to \fBpipe\fR.
491491
This setting is only valid for \fBprocess\fR and \fBbgprocess\fR services.
492+
The \fIservice-name\fR is subject to pre-load variable substitution
493+
(see \fBVARIABLE SUBSTITUTION\fR).
492494
.TP
493495
\fBoptions\fR: \fIoption\fR...
494496
Specifies various options for this service. See the \fBOPTIONS\fR section.

src/igr-tests/igr-runner.cc

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void offline_enable_test();
4141
void xdg_config_test();
4242
void cycles_test();
4343
void svc_arg_test();
44+
void svc_arg_consumer_test();
4445

4546
int main(int argc, char **argv)
4647
{
@@ -57,7 +58,7 @@ int main(int argc, char **argv)
5758
{ "before-after2", before_after2_test }, { "log-via-pipe", log_via_pipe_test },
5859
{ "catlog", catlog_test }, { "offline-enable", offline_enable_test },
5960
{ "xdg-config", xdg_config_test }, { "cycles", cycles_test },
60-
{ "svc-arg", svc_arg_test } };
61+
{ "svc-arg", svc_arg_test }, { "svc-arg-consumer", svc_arg_consumer_test } };
6162
constexpr int num_tests = sizeof(tests) / sizeof(tests[0]);
6263

6364
dinit_bindir = "../..";
@@ -998,3 +999,47 @@ void svc_arg_test()
998999
"foo\n" +
9991000
"bar\n");
10001001
}
1002+
1003+
void svc_arg_consumer_test()
1004+
{
1005+
// Test for using minimal variable substitution in consumer services
1006+
igr_test_setup setup("svc-arg-consumer");
1007+
1008+
std::string logged_output_file = setup.prep_output_file("logged-output");
1009+
std::string socket_path = setup.prep_socket_path();
1010+
1011+
// "boot" service brings up "consumer" service and consumer writes its $1 value
1012+
dinit_proc dinit_p;
1013+
dinit_p.start("svc-arg-consumer", {"-u", "-d", "sd", "-p", socket_path, "-q"}, true);
1014+
1015+
nanosleepx(0, 1000000000u / 10u);
1016+
1017+
igr_assert_eq(read_file_contents(logged_output_file), "producer\n");
1018+
1019+
// Start and stop the producer for printing output to consumer
1020+
dinitctl_proc dinitctl_p;
1021+
dinitctl_p.start("svc-arg-consumer", {"-u", "-p", socket_path, "start", "producer"});
1022+
int status = dinitctl_p.wait_for_term({1, 0}); /* max 1 second */
1023+
igr_assert(status == 0, "dinitctl did not exit cleanly");
1024+
1025+
dinitctl_p.start("svc-arg-consumer", {"-u", "-p", socket_path, "stop", "producer"});
1026+
status = dinitctl_p.wait_for_term({1, 0}); /* max 1 second */
1027+
igr_assert(status == 0, "dinitctl did not exit cleanly");
1028+
1029+
nanosleepx(0, (1000000000u / 10u) * 2u);
1030+
1031+
igr_assert_eq(read_file_contents(logged_output_file), "producer\n" "Producing output...\n");
1032+
1033+
// Another start and stop to make sure that producer is linked to consumer properly
1034+
dinitctl_p.start("svc-arg-consumer", {"-u", "-p", socket_path, "start", "producer"});
1035+
status = dinitctl_p.wait_for_term({1, 0}); /* max 1 second */
1036+
igr_assert(status == 0, "dinitctl did not exit cleanly");
1037+
1038+
dinitctl_p.start("svc-arg-consumer", {"-u", "-p", socket_path, "stop", "producer"});
1039+
status = dinitctl_p.wait_for_term({1, 0}); /* max 1 second */
1040+
igr_assert(status == 0, "dinitctl did not exit cleanly");
1041+
1042+
nanosleepx(0, (1000000000u / 10u) * 2u);
1043+
1044+
igr_assert_eq(read_file_contents(logged_output_file), "producer\n" "Producing output...\n" "Producing output...\n");
1045+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
echo "$1" > "$IGR_OUTPUT/logged-output"
3+
cat - >> "$IGR_OUTPUT/logged-output" 2>&1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
echo "Producing output..."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type = internal
2+
waits-for = consumer@producer
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type = process
2+
command = ../scripts/consumer.sh $1
3+
consumer-of = $1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type = scripted
2+
command = ../scripts/producer.sh
3+
log-type = pipe

src/includes/load-service.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,8 @@ void process_service_line(settings_wrapper &settings, const char *name, const ch
19461946
}
19471947
case setting_id_t::CONSUMER_OF:
19481948
{
1949-
string consumed_svc_name = read_setting_value(input_pos, i, end);
1949+
string consumed_svc_name = read_value_resolved(setting.c_str(), input_pos, i, end,
1950+
service_arg, lookup_var);
19501951
if (consumed_svc_name == name) {
19511952
throw service_description_exc(name, "service cannot be its own consumer", "consumer-of",
19521953
input_pos);

0 commit comments

Comments
 (0)