From 7f14931def91afb3c6533df4e6ff1b6fa17d713c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 29 Nov 2015 10:43:58 +0100 Subject: typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/examples/daemon-depedencies/.gitignore | 10 -- doc/examples/daemon-depedencies/Makefile | 14 --- doc/examples/daemon-depedencies/README | 19 ---- doc/examples/daemon-depedencies/announce.c | 28 ----- doc/examples/daemon-depedencies/await-ready.c | 122 ---------------------- doc/examples/daemon-depedencies/await-started.c | 127 ----------------------- doc/examples/daemon-depedencies/cleanup.c | 25 ----- doc/examples/daemon-depedencies/d-network | 9 -- doc/examples/daemon-depedencies/d-ntp | 11 -- doc/examples/daemon-depedencies/d-ssh | 13 --- doc/examples/daemon-depedencies/init.c | 127 ----------------------- doc/examples/daemon-depedencies/require.c | 38 ------- doc/examples/daemon-depedencies/start-daemon.c | 32 ------ doc/examples/daemon-depedencies/test-daemon.c | 35 ------- doc/examples/daemon-dependencies/.gitignore | 10 ++ doc/examples/daemon-dependencies/Makefile | 14 +++ doc/examples/daemon-dependencies/README | 19 ++++ doc/examples/daemon-dependencies/announce.c | 28 +++++ doc/examples/daemon-dependencies/await-ready.c | 122 ++++++++++++++++++++++ doc/examples/daemon-dependencies/await-started.c | 127 +++++++++++++++++++++++ doc/examples/daemon-dependencies/cleanup.c | 25 +++++ doc/examples/daemon-dependencies/d-network | 9 ++ doc/examples/daemon-dependencies/d-ntp | 11 ++ doc/examples/daemon-dependencies/d-ssh | 13 +++ doc/examples/daemon-dependencies/init.c | 127 +++++++++++++++++++++++ doc/examples/daemon-dependencies/require.c | 38 +++++++ doc/examples/daemon-dependencies/start-daemon.c | 32 ++++++ doc/examples/daemon-dependencies/test-daemon.c | 35 +++++++ 28 files changed, 610 insertions(+), 610 deletions(-) delete mode 100644 doc/examples/daemon-depedencies/.gitignore delete mode 100644 doc/examples/daemon-depedencies/Makefile delete mode 100644 doc/examples/daemon-depedencies/README delete mode 100644 doc/examples/daemon-depedencies/announce.c delete mode 100644 doc/examples/daemon-depedencies/await-ready.c delete mode 100644 doc/examples/daemon-depedencies/await-started.c delete mode 100644 doc/examples/daemon-depedencies/cleanup.c delete mode 100755 doc/examples/daemon-depedencies/d-network delete mode 100755 doc/examples/daemon-depedencies/d-ntp delete mode 100755 doc/examples/daemon-depedencies/d-ssh delete mode 100644 doc/examples/daemon-depedencies/init.c delete mode 100644 doc/examples/daemon-depedencies/require.c delete mode 100644 doc/examples/daemon-depedencies/start-daemon.c delete mode 100644 doc/examples/daemon-depedencies/test-daemon.c create mode 100644 doc/examples/daemon-dependencies/.gitignore create mode 100644 doc/examples/daemon-dependencies/Makefile create mode 100644 doc/examples/daemon-dependencies/README create mode 100644 doc/examples/daemon-dependencies/announce.c create mode 100644 doc/examples/daemon-dependencies/await-ready.c create mode 100644 doc/examples/daemon-dependencies/await-started.c create mode 100644 doc/examples/daemon-dependencies/cleanup.c create mode 100755 doc/examples/daemon-dependencies/d-network create mode 100755 doc/examples/daemon-dependencies/d-ntp create mode 100755 doc/examples/daemon-dependencies/d-ssh create mode 100644 doc/examples/daemon-dependencies/init.c create mode 100644 doc/examples/daemon-dependencies/require.c create mode 100644 doc/examples/daemon-dependencies/start-daemon.c create mode 100644 doc/examples/daemon-dependencies/test-daemon.c (limited to 'doc/examples') diff --git a/doc/examples/daemon-depedencies/.gitignore b/doc/examples/daemon-depedencies/.gitignore deleted file mode 100644 index 0dac119..0000000 --- a/doc/examples/daemon-depedencies/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ -run/ -announce -await-ready -await-started -cleanup -init -require -start-daemon -test-daemon - diff --git a/doc/examples/daemon-depedencies/Makefile b/doc/examples/daemon-depedencies/Makefile deleted file mode 100644 index bf6fe88..0000000 --- a/doc/examples/daemon-depedencies/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -COMMANDS = announce await-ready await-started cleanup init require start-daemon test-daemon - -all: ${COMMANDS} - -%: %.c - ${CC} -Wall -Wextra -pedantic -std=c99 -lbus -o $@ $< - -clean: - -rm ${COMMANDS} - -rm -r run - - -.PHONY: all clean - diff --git a/doc/examples/daemon-depedencies/README b/doc/examples/daemon-depedencies/README deleted file mode 100644 index a9f5783..0000000 --- a/doc/examples/daemon-depedencies/README +++ /dev/null @@ -1,19 +0,0 @@ -Use-case example. - -This example shows how bus can be used in a init -system to provide "aggressivly" parallel startup -of daemons. - - -First of, run make to build this example. - -To start the example run ./init. It will print in -red export-statement you may want to run i other -terminals. You will need to select at least one -daemon, for example you can run `./init d-ntp`. -The available pretend daemons are: d-network, -d-ntp and d-ssh. - -When you are done run ./cleanup with BUS_INIT -exported with the value printed by ./init. - diff --git a/doc/examples/daemon-depedencies/announce.c b/doc/examples/daemon-depedencies/announce.c deleted file mode 100644 index d928adb..0000000 --- a/doc/examples/daemon-depedencies/announce.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - -static char arg[4098]; - - -int -main(int argc, char *argv[]) -{ - bus_t bus; - if (argc < 3) - return fprintf(stderr, "USAGE: %s state daemon", *argv), 2; - t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); - sprintf(arg, "%ji %s %s", (intmax_t)getppid(), argv[1], argv[2]); - t(bus_write(&bus, arg, 0)); - t(bus_close(&bus)); - return 0; - -fail: - perror("announce"); - return 1; -} diff --git a/doc/examples/daemon-depedencies/await-ready.c b/doc/examples/daemon-depedencies/await-ready.c deleted file mode 100644 index fea2382..0000000 --- a/doc/examples/daemon-depedencies/await-ready.c +++ /dev/null @@ -1,122 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - -static char arg[4098]; -static int argc; -static char **argv; -static int remaining = 0; -static char *started = NULL; -static char msg[BUS_MEMORY_SIZE]; - - -static void -announce_wait(pid_t pid) -{ - bus_t bus; - int i; - t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); - for (i = 1; i < argc; i++) { - if (!started[i]) { - sprintf(arg, "%ji awaiting-ready %s", (intmax_t)pid, argv[i]); - t(bus_write(&bus, arg, 0)); - } - } - t(bus_close(&bus)); - return; - -fail: - perror("await-ready"); -} - - -static int -callback(const char *message, void *user_data) -{ - int i; - char *arg2; - char *arg3; - pid_t pid; - pid_t ppid; - - if (!message) { - ppid = getppid(); - pid = fork(); - if (pid == 0) { - if (fork() == 0) - announce_wait(ppid); - exit(0); - } else { - (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ - } - return 1; - } - - strncpy(msg, message, BUS_MEMORY_SIZE - 1); - msg[BUS_MEMORY_SIZE - 1] = 0; - - arg2 = strchr(msg, ' '); - if (!arg2) - return 1; - arg3 = strchr(++arg2, ' '); - if (!arg3) - return 1; - *arg3++ = 0; - - if (strcmp(arg2, "ready")) - return 1; - - for (i = 1; i < argc; i++) - if (!started[i] && !strcmp(argv[i], arg3)) - started[i] = 1, remaining--; - - return !!remaining; - (void) user_data; -} - - -int -main(int argc_, char *argv_[]) -{ - bus_t bus; - int i; - - argc = argc_; - argv = argv_; - - if (argc < 2) - return fprintf(stderr, "USAGE: %s daemon...", *argv), 2; - t(bus_open(&bus, getenv("BUS_INIT"), BUS_RDONLY)); - started = calloc(argc, sizeof(char)); - t(started == NULL); - - started[0] = 1; - for (i = 1; i < argc; i++) { - sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null", argv[i]); - if (!WEXITSTATUS(system(arg))) - started[i] = 1; - else - remaining++; - } - - if (remaining) - bus_read(&bus, callback, NULL); - - bus_close(&bus); - free(started); - return 0; - -fail: - perror("await-ready"); - bus_close(&bus); - free(started); - return 1; -} - diff --git a/doc/examples/daemon-depedencies/await-started.c b/doc/examples/daemon-depedencies/await-started.c deleted file mode 100644 index 99ba14f..0000000 --- a/doc/examples/daemon-depedencies/await-started.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - -static char arg[4098]; -static int argc; -static char **argv; -static int remaining = 0; -static char *started = NULL; -static char msg[BUS_MEMORY_SIZE]; - - -static void -announce_wait(pid_t pid) -{ - bus_t bus; - int i; - t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); - for (i = 1; i < argc; i++) { - if (!started[i]) { - sprintf(arg, "%ji awaiting-started %s", (intmax_t)pid, argv[i]); - t(bus_write(&bus, arg, 0)); - } - } - t(bus_close(&bus)); - return; - -fail: - perror("await-started"); -} - - -static int -callback(const char *message, void *user_data) -{ - int i; - char *arg2; - char *arg3; - pid_t pid; - pid_t ppid; - - if (!message) { - ppid = getppid(); - pid = fork(); - if (pid == 0) { - if (fork() == 0) - announce_wait(ppid); - exit(0); - } else { - (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ - } - return 1; - } - - strncpy(msg, message, BUS_MEMORY_SIZE - 1); - msg[BUS_MEMORY_SIZE - 1] = 0; - - arg2 = strchr(msg, ' '); - if (!arg2) - return 1; - arg3 = strchr(++arg2, ' '); - if (!arg3) - return 1; - *arg3++ = 0; - - if (strcmp(arg2, "started") && strcmp(arg2, "ready")) - return 1; - - for (i = 1; i < argc; i++) - if (!started[i] && !strcmp(argv[i], arg3)) - started[i] = 1, remaining--; - - return !!remaining; - (void) user_data; -} - - -int -main(int argc_, char *argv_[]) -{ - bus_t bus; - int i; - - argc = argc_; - argv = argv_; - - if (argc < 2) - return fprintf(stderr, "USAGE: %s daemon...", *argv), 2; - t(bus_open(&bus, getenv("BUS_INIT"), BUS_RDONLY)); - started = calloc(argc, sizeof(char)); - t(started == NULL); - - started[0] = 1; - for (i = 1; i < argc; i++) { - sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null", argv[i]); - if (!WEXITSTATUS(system(arg))) { - started[i] = 1; - } else { - sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null", argv[i]); - if (!WEXITSTATUS(system(arg))) - started[i] = 1; - else - remaining++; - } - } - - if (remaining) - bus_read(&bus, callback, NULL); - - bus_close(&bus); - free(started); - return 0; - -fail: - perror("await-started"); - bus_close(&bus); - free(started); - return 1; -} - diff --git a/doc/examples/daemon-depedencies/cleanup.c b/doc/examples/daemon-depedencies/cleanup.c deleted file mode 100644 index f9ead6d..0000000 --- a/doc/examples/daemon-depedencies/cleanup.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - - -int -main() -{ - char *bus_address = getenv("BUS_INIT"); - if (!bus_address || !*bus_address) { - fprintf(stderr, "$BUS_INIT has not been set, its export statement " - "should have been printed in bold red by ./init\n"); - return 1; - } - t(bus_unlink(bus_address)); - return 0; - -fail: - perror("cleanup"); - return 1; -} - diff --git a/doc/examples/daemon-depedencies/d-network b/doc/examples/daemon-depedencies/d-network deleted file mode 100755 index 1b8c7dd..0000000 --- a/doc/examples/daemon-depedencies/d-network +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh -PATH=.:$PATH -d=d-network - -echo $d: starting -sleep 2 -echo $d: ready -announce ready $d - diff --git a/doc/examples/daemon-depedencies/d-ntp b/doc/examples/daemon-depedencies/d-ntp deleted file mode 100755 index 0f29c7c..0000000 --- a/doc/examples/daemon-depedencies/d-ntp +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh -PATH=.:$PATH -d=d-ntp - -require d-network -echo $d: started -announce started $d -await-ready d-network -echo $d: ready -announce ready $d - diff --git a/doc/examples/daemon-depedencies/d-ssh b/doc/examples/daemon-depedencies/d-ssh deleted file mode 100755 index 487354e..0000000 --- a/doc/examples/daemon-depedencies/d-ssh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh -PATH=.:$PATH -d=d-ssh - -require d-network -echo $d: starting -sleep 1 -echo $d: started -announce started $d -sleep 1 -echo $d: ready -announce ready $d - diff --git a/doc/examples/daemon-depedencies/init.c b/doc/examples/daemon-depedencies/init.c deleted file mode 100644 index b7182c6..0000000 --- a/doc/examples/daemon-depedencies/init.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - -static char msg[BUS_MEMORY_SIZE]; -static int argc; -static char **argv; -static char arg[4098]; - - -static void -start_daemons() -{ - int i; - for (i = 1; i < argc; i++) - if (fork() == 0) - execl("./start-daemon", "./start-daemon", argv[i], NULL); -} - - -static int -callback(const char *message, void *user_data) -{ - pid_t pid; - char *arg2; - char *arg3; - if (!message) { - pid = fork(); - t(pid == -1); - if (pid == 0) { - if (fork() == 0) { - start_daemons(); - } - exit(0); - } else { - (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ - } - return 1; - } - - strncpy(msg, message, BUS_MEMORY_SIZE - 1); - msg[BUS_MEMORY_SIZE - 1] = 0; - - pid = fork(); - t(pid == -1); - - if (pid == 0) { - if (fork() == 0) { - arg2 = strchr(msg, ' '); - if (arg2 == NULL) - exit(0); - arg3 = strchr(++arg2, ' '); - if (arg3 == NULL) - exit(0); - *arg3++ = 0; - if (!strcmp(arg2, "require")) { - execl("./start-daemon", "./start-daemon", arg3, NULL); - } else if (!strcmp(arg2, "awaiting-started")) { - execl("./test-daemon", "./test-daemon", arg3, "started", NULL); - } else if (!strcmp(arg2, "awaiting-ready") || !strcmp(arg2, "awaiting")) { - execl("./test-daemon", "./test-daemon", arg3, "ready", NULL); - } else if (!strcmp(arg2, "started")) { - sprintf(arg, - "grep '^%s\\$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null || " - "echo %s >> \"${XDG_RUNTIME_DIR}/started-daemons\"", - arg3, arg3); - execlp("sh", "sh", "-c", arg, NULL); - } else if (!strcmp(arg2, "ready")) { - sprintf(arg, - "grep '^%s\\$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null || " - "echo %s >> \"${XDG_RUNTIME_DIR}/ready-daemons\"", - arg3, arg3); - execlp("sh", "sh", "-c", arg, NULL); - } - } - exit(0); - } else { - (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ - } - - return 1; - (void) user_data; - -fail: - perror("init"); - return -1; -} - - -int -main(int argc_, char *argv_[]) -{ - char *bus_address = NULL; - bus_t bus; - argv = argv_; - argc = argc_; - if (argc < 2) { - fprintf(stderr, "USAGE: %s daemon...\n", *argv); - return 1; - } - t(setenv("XDG_RUNTIME_DIR", "./run", 1)); /* Real init systems with not have the period. */ - system("mkdir -p -- \"${XDG_RUNTIME_DIR}\""); - system("truncate -s 0 -- \"${XDG_RUNTIME_DIR}/started-daemons\""); - system("truncate -s 0 -- \"${XDG_RUNTIME_DIR}/ready-daemons\""); - t(bus_create(NULL, 1, &bus_address)); - fprintf(stderr, "\033[00;01;31mexport BUS_INIT=%s\033[00m\n", bus_address); - fprintf(stderr, "\033[00;31mexport XDG_RUNTIME_DIR=./run\033[00m\n"); - t(setenv("BUS_INIT", bus_address, 1)); - t(bus_open(&bus, bus_address, BUS_RDONLY)); - t(bus_read(&bus, callback, NULL)); - bus_close(&bus); - free(bus_address); - return 0; - -fail: - perror("init"); - bus_close(&bus); - free(bus_address); - return 1; -} - diff --git a/doc/examples/daemon-depedencies/require.c b/doc/examples/daemon-depedencies/require.c deleted file mode 100644 index e378431..0000000 --- a/doc/examples/daemon-depedencies/require.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - -static char arg[4098]; - - -int -main(int argc, char *argv[]) -{ - bus_t bus; - int i; - if (argc < 2) - return fprintf(stderr, "USAGE: %s daemon...", *argv), 2; - t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); - - for (i = 1; i < argc; i++) { - sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null", argv[i]); - if (WEXITSTATUS(system(arg))) { - sprintf(arg, "%ji require %s", (intmax_t)getppid(), argv[i]); - t(bus_write(&bus, arg, 0)); - } - } - - bus_close(&bus); - return 0; - -fail: - perror("require"); - bus_close(&bus); - return 1; -} - diff --git a/doc/examples/daemon-depedencies/start-daemon.c b/doc/examples/daemon-depedencies/start-daemon.c deleted file mode 100644 index 7c224d5..0000000 --- a/doc/examples/daemon-depedencies/start-daemon.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - -static char arg[4098]; - - -int -main(int argc, char *argv[]) -{ - if (argc != 2) - return fprintf(stderr, "This program should be called from ./init\n"), 2; - - sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null", argv[1]); - if (!WEXITSTATUS(system(arg))) - return 0; - sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null", argv[1]); - if (!WEXITSTATUS(system(arg))) - return 0; - - sprintf(arg, "./%s", argv[1]); - execlp(arg, arg, NULL); - perror("start-daemon"); - return 1; -} - diff --git a/doc/examples/daemon-depedencies/test-daemon.c b/doc/examples/daemon-depedencies/test-daemon.c deleted file mode 100644 index 32ee545..0000000 --- a/doc/examples/daemon-depedencies/test-daemon.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include -#include - -#define t(stmt) if (stmt) goto fail - - -static char arg[4098]; - - -int -main(int argc, char *argv[]) -{ - bus_t bus; - if (argc != 3) - return fprintf(stderr, "This program should be called from ./init\n"), 2; -retry: - sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/%s-daemons\" >/dev/null", argv[1], argv[2]); - if (!WEXITSTATUS(system(arg))) { - t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); - sprintf(arg, "0 %s %s", argv[2], argv[1]); - t(bus_write(&bus, arg, 0)); - bus_close(&bus); - } else if (!strcmp(argv[2], "started")) { - argv[2] = "ready"; - goto retry; - } - return 0; - -fail: - perror("test-daemon"); - return 1; -} - diff --git a/doc/examples/daemon-dependencies/.gitignore b/doc/examples/daemon-dependencies/.gitignore new file mode 100644 index 0000000..0dac119 --- /dev/null +++ b/doc/examples/daemon-dependencies/.gitignore @@ -0,0 +1,10 @@ +run/ +announce +await-ready +await-started +cleanup +init +require +start-daemon +test-daemon + diff --git a/doc/examples/daemon-dependencies/Makefile b/doc/examples/daemon-dependencies/Makefile new file mode 100644 index 0000000..bf6fe88 --- /dev/null +++ b/doc/examples/daemon-dependencies/Makefile @@ -0,0 +1,14 @@ +COMMANDS = announce await-ready await-started cleanup init require start-daemon test-daemon + +all: ${COMMANDS} + +%: %.c + ${CC} -Wall -Wextra -pedantic -std=c99 -lbus -o $@ $< + +clean: + -rm ${COMMANDS} + -rm -r run + + +.PHONY: all clean + diff --git a/doc/examples/daemon-dependencies/README b/doc/examples/daemon-dependencies/README new file mode 100644 index 0000000..a9f5783 --- /dev/null +++ b/doc/examples/daemon-dependencies/README @@ -0,0 +1,19 @@ +Use-case example. + +This example shows how bus can be used in a init +system to provide "aggressivly" parallel startup +of daemons. + + +First of, run make to build this example. + +To start the example run ./init. It will print in +red export-statement you may want to run i other +terminals. You will need to select at least one +daemon, for example you can run `./init d-ntp`. +The available pretend daemons are: d-network, +d-ntp and d-ssh. + +When you are done run ./cleanup with BUS_INIT +exported with the value printed by ./init. + diff --git a/doc/examples/daemon-dependencies/announce.c b/doc/examples/daemon-dependencies/announce.c new file mode 100644 index 0000000..d928adb --- /dev/null +++ b/doc/examples/daemon-dependencies/announce.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + +static char arg[4098]; + + +int +main(int argc, char *argv[]) +{ + bus_t bus; + if (argc < 3) + return fprintf(stderr, "USAGE: %s state daemon", *argv), 2; + t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); + sprintf(arg, "%ji %s %s", (intmax_t)getppid(), argv[1], argv[2]); + t(bus_write(&bus, arg, 0)); + t(bus_close(&bus)); + return 0; + +fail: + perror("announce"); + return 1; +} diff --git a/doc/examples/daemon-dependencies/await-ready.c b/doc/examples/daemon-dependencies/await-ready.c new file mode 100644 index 0000000..fea2382 --- /dev/null +++ b/doc/examples/daemon-dependencies/await-ready.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + +static char arg[4098]; +static int argc; +static char **argv; +static int remaining = 0; +static char *started = NULL; +static char msg[BUS_MEMORY_SIZE]; + + +static void +announce_wait(pid_t pid) +{ + bus_t bus; + int i; + t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); + for (i = 1; i < argc; i++) { + if (!started[i]) { + sprintf(arg, "%ji awaiting-ready %s", (intmax_t)pid, argv[i]); + t(bus_write(&bus, arg, 0)); + } + } + t(bus_close(&bus)); + return; + +fail: + perror("await-ready"); +} + + +static int +callback(const char *message, void *user_data) +{ + int i; + char *arg2; + char *arg3; + pid_t pid; + pid_t ppid; + + if (!message) { + ppid = getppid(); + pid = fork(); + if (pid == 0) { + if (fork() == 0) + announce_wait(ppid); + exit(0); + } else { + (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ + } + return 1; + } + + strncpy(msg, message, BUS_MEMORY_SIZE - 1); + msg[BUS_MEMORY_SIZE - 1] = 0; + + arg2 = strchr(msg, ' '); + if (!arg2) + return 1; + arg3 = strchr(++arg2, ' '); + if (!arg3) + return 1; + *arg3++ = 0; + + if (strcmp(arg2, "ready")) + return 1; + + for (i = 1; i < argc; i++) + if (!started[i] && !strcmp(argv[i], arg3)) + started[i] = 1, remaining--; + + return !!remaining; + (void) user_data; +} + + +int +main(int argc_, char *argv_[]) +{ + bus_t bus; + int i; + + argc = argc_; + argv = argv_; + + if (argc < 2) + return fprintf(stderr, "USAGE: %s daemon...", *argv), 2; + t(bus_open(&bus, getenv("BUS_INIT"), BUS_RDONLY)); + started = calloc(argc, sizeof(char)); + t(started == NULL); + + started[0] = 1; + for (i = 1; i < argc; i++) { + sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null", argv[i]); + if (!WEXITSTATUS(system(arg))) + started[i] = 1; + else + remaining++; + } + + if (remaining) + bus_read(&bus, callback, NULL); + + bus_close(&bus); + free(started); + return 0; + +fail: + perror("await-ready"); + bus_close(&bus); + free(started); + return 1; +} + diff --git a/doc/examples/daemon-dependencies/await-started.c b/doc/examples/daemon-dependencies/await-started.c new file mode 100644 index 0000000..99ba14f --- /dev/null +++ b/doc/examples/daemon-dependencies/await-started.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + +static char arg[4098]; +static int argc; +static char **argv; +static int remaining = 0; +static char *started = NULL; +static char msg[BUS_MEMORY_SIZE]; + + +static void +announce_wait(pid_t pid) +{ + bus_t bus; + int i; + t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); + for (i = 1; i < argc; i++) { + if (!started[i]) { + sprintf(arg, "%ji awaiting-started %s", (intmax_t)pid, argv[i]); + t(bus_write(&bus, arg, 0)); + } + } + t(bus_close(&bus)); + return; + +fail: + perror("await-started"); +} + + +static int +callback(const char *message, void *user_data) +{ + int i; + char *arg2; + char *arg3; + pid_t pid; + pid_t ppid; + + if (!message) { + ppid = getppid(); + pid = fork(); + if (pid == 0) { + if (fork() == 0) + announce_wait(ppid); + exit(0); + } else { + (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ + } + return 1; + } + + strncpy(msg, message, BUS_MEMORY_SIZE - 1); + msg[BUS_MEMORY_SIZE - 1] = 0; + + arg2 = strchr(msg, ' '); + if (!arg2) + return 1; + arg3 = strchr(++arg2, ' '); + if (!arg3) + return 1; + *arg3++ = 0; + + if (strcmp(arg2, "started") && strcmp(arg2, "ready")) + return 1; + + for (i = 1; i < argc; i++) + if (!started[i] && !strcmp(argv[i], arg3)) + started[i] = 1, remaining--; + + return !!remaining; + (void) user_data; +} + + +int +main(int argc_, char *argv_[]) +{ + bus_t bus; + int i; + + argc = argc_; + argv = argv_; + + if (argc < 2) + return fprintf(stderr, "USAGE: %s daemon...", *argv), 2; + t(bus_open(&bus, getenv("BUS_INIT"), BUS_RDONLY)); + started = calloc(argc, sizeof(char)); + t(started == NULL); + + started[0] = 1; + for (i = 1; i < argc; i++) { + sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null", argv[i]); + if (!WEXITSTATUS(system(arg))) { + started[i] = 1; + } else { + sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null", argv[i]); + if (!WEXITSTATUS(system(arg))) + started[i] = 1; + else + remaining++; + } + } + + if (remaining) + bus_read(&bus, callback, NULL); + + bus_close(&bus); + free(started); + return 0; + +fail: + perror("await-started"); + bus_close(&bus); + free(started); + return 1; +} + diff --git a/doc/examples/daemon-dependencies/cleanup.c b/doc/examples/daemon-dependencies/cleanup.c new file mode 100644 index 0000000..f9ead6d --- /dev/null +++ b/doc/examples/daemon-dependencies/cleanup.c @@ -0,0 +1,25 @@ +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + + +int +main() +{ + char *bus_address = getenv("BUS_INIT"); + if (!bus_address || !*bus_address) { + fprintf(stderr, "$BUS_INIT has not been set, its export statement " + "should have been printed in bold red by ./init\n"); + return 1; + } + t(bus_unlink(bus_address)); + return 0; + +fail: + perror("cleanup"); + return 1; +} + diff --git a/doc/examples/daemon-dependencies/d-network b/doc/examples/daemon-dependencies/d-network new file mode 100755 index 0000000..1b8c7dd --- /dev/null +++ b/doc/examples/daemon-dependencies/d-network @@ -0,0 +1,9 @@ +#!/bin/sh +PATH=.:$PATH +d=d-network + +echo $d: starting +sleep 2 +echo $d: ready +announce ready $d + diff --git a/doc/examples/daemon-dependencies/d-ntp b/doc/examples/daemon-dependencies/d-ntp new file mode 100755 index 0000000..0f29c7c --- /dev/null +++ b/doc/examples/daemon-dependencies/d-ntp @@ -0,0 +1,11 @@ +#!/bin/sh +PATH=.:$PATH +d=d-ntp + +require d-network +echo $d: started +announce started $d +await-ready d-network +echo $d: ready +announce ready $d + diff --git a/doc/examples/daemon-dependencies/d-ssh b/doc/examples/daemon-dependencies/d-ssh new file mode 100755 index 0000000..487354e --- /dev/null +++ b/doc/examples/daemon-dependencies/d-ssh @@ -0,0 +1,13 @@ +#!/bin/sh +PATH=.:$PATH +d=d-ssh + +require d-network +echo $d: starting +sleep 1 +echo $d: started +announce started $d +sleep 1 +echo $d: ready +announce ready $d + diff --git a/doc/examples/daemon-dependencies/init.c b/doc/examples/daemon-dependencies/init.c new file mode 100644 index 0000000..b7182c6 --- /dev/null +++ b/doc/examples/daemon-dependencies/init.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + +static char msg[BUS_MEMORY_SIZE]; +static int argc; +static char **argv; +static char arg[4098]; + + +static void +start_daemons() +{ + int i; + for (i = 1; i < argc; i++) + if (fork() == 0) + execl("./start-daemon", "./start-daemon", argv[i], NULL); +} + + +static int +callback(const char *message, void *user_data) +{ + pid_t pid; + char *arg2; + char *arg3; + if (!message) { + pid = fork(); + t(pid == -1); + if (pid == 0) { + if (fork() == 0) { + start_daemons(); + } + exit(0); + } else { + (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ + } + return 1; + } + + strncpy(msg, message, BUS_MEMORY_SIZE - 1); + msg[BUS_MEMORY_SIZE - 1] = 0; + + pid = fork(); + t(pid == -1); + + if (pid == 0) { + if (fork() == 0) { + arg2 = strchr(msg, ' '); + if (arg2 == NULL) + exit(0); + arg3 = strchr(++arg2, ' '); + if (arg3 == NULL) + exit(0); + *arg3++ = 0; + if (!strcmp(arg2, "require")) { + execl("./start-daemon", "./start-daemon", arg3, NULL); + } else if (!strcmp(arg2, "awaiting-started")) { + execl("./test-daemon", "./test-daemon", arg3, "started", NULL); + } else if (!strcmp(arg2, "awaiting-ready") || !strcmp(arg2, "awaiting")) { + execl("./test-daemon", "./test-daemon", arg3, "ready", NULL); + } else if (!strcmp(arg2, "started")) { + sprintf(arg, + "grep '^%s\\$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null || " + "echo %s >> \"${XDG_RUNTIME_DIR}/started-daemons\"", + arg3, arg3); + execlp("sh", "sh", "-c", arg, NULL); + } else if (!strcmp(arg2, "ready")) { + sprintf(arg, + "grep '^%s\\$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null || " + "echo %s >> \"${XDG_RUNTIME_DIR}/ready-daemons\"", + arg3, arg3); + execlp("sh", "sh", "-c", arg, NULL); + } + } + exit(0); + } else { + (void) waitpid(pid, NULL, 0); /* Let's pretend everything will go swimmingly. */ + } + + return 1; + (void) user_data; + +fail: + perror("init"); + return -1; +} + + +int +main(int argc_, char *argv_[]) +{ + char *bus_address = NULL; + bus_t bus; + argv = argv_; + argc = argc_; + if (argc < 2) { + fprintf(stderr, "USAGE: %s daemon...\n", *argv); + return 1; + } + t(setenv("XDG_RUNTIME_DIR", "./run", 1)); /* Real init systems with not have the period. */ + system("mkdir -p -- \"${XDG_RUNTIME_DIR}\""); + system("truncate -s 0 -- \"${XDG_RUNTIME_DIR}/started-daemons\""); + system("truncate -s 0 -- \"${XDG_RUNTIME_DIR}/ready-daemons\""); + t(bus_create(NULL, 1, &bus_address)); + fprintf(stderr, "\033[00;01;31mexport BUS_INIT=%s\033[00m\n", bus_address); + fprintf(stderr, "\033[00;31mexport XDG_RUNTIME_DIR=./run\033[00m\n"); + t(setenv("BUS_INIT", bus_address, 1)); + t(bus_open(&bus, bus_address, BUS_RDONLY)); + t(bus_read(&bus, callback, NULL)); + bus_close(&bus); + free(bus_address); + return 0; + +fail: + perror("init"); + bus_close(&bus); + free(bus_address); + return 1; +} + diff --git a/doc/examples/daemon-dependencies/require.c b/doc/examples/daemon-dependencies/require.c new file mode 100644 index 0000000..e378431 --- /dev/null +++ b/doc/examples/daemon-dependencies/require.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + +static char arg[4098]; + + +int +main(int argc, char *argv[]) +{ + bus_t bus; + int i; + if (argc < 2) + return fprintf(stderr, "USAGE: %s daemon...", *argv), 2; + t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); + + for (i = 1; i < argc; i++) { + sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null", argv[i]); + if (WEXITSTATUS(system(arg))) { + sprintf(arg, "%ji require %s", (intmax_t)getppid(), argv[i]); + t(bus_write(&bus, arg, 0)); + } + } + + bus_close(&bus); + return 0; + +fail: + perror("require"); + bus_close(&bus); + return 1; +} + diff --git a/doc/examples/daemon-dependencies/start-daemon.c b/doc/examples/daemon-dependencies/start-daemon.c new file mode 100644 index 0000000..7c224d5 --- /dev/null +++ b/doc/examples/daemon-dependencies/start-daemon.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + +static char arg[4098]; + + +int +main(int argc, char *argv[]) +{ + if (argc != 2) + return fprintf(stderr, "This program should be called from ./init\n"), 2; + + sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/started-daemons\" >/dev/null", argv[1]); + if (!WEXITSTATUS(system(arg))) + return 0; + sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/ready-daemons\" >/dev/null", argv[1]); + if (!WEXITSTATUS(system(arg))) + return 0; + + sprintf(arg, "./%s", argv[1]); + execlp(arg, arg, NULL); + perror("start-daemon"); + return 1; +} + diff --git a/doc/examples/daemon-dependencies/test-daemon.c b/doc/examples/daemon-dependencies/test-daemon.c new file mode 100644 index 0000000..32ee545 --- /dev/null +++ b/doc/examples/daemon-dependencies/test-daemon.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + +static char arg[4098]; + + +int +main(int argc, char *argv[]) +{ + bus_t bus; + if (argc != 3) + return fprintf(stderr, "This program should be called from ./init\n"), 2; +retry: + sprintf(arg, "grep '^%s$' < \"${XDG_RUNTIME_DIR}/%s-daemons\" >/dev/null", argv[1], argv[2]); + if (!WEXITSTATUS(system(arg))) { + t(bus_open(&bus, getenv("BUS_INIT"), BUS_WRONLY)); + sprintf(arg, "0 %s %s", argv[2], argv[1]); + t(bus_write(&bus, arg, 0)); + bus_close(&bus); + } else if (!strcmp(argv[2], "started")) { + argv[2] = "ready"; + goto retry; + } + return 0; + +fail: + perror("test-daemon"); + return 1; +} + -- cgit v1.2.3-70-g09d2