shared_LIBRARIES += forb
forb_SOURCES = forb.c cdr.c sha1.c uuid.c iop.c proto.c syncobj.c \
request.c executor.c object.c peer.c port.c refcnt.c \
- exec_req.c regref.c discovery.c
+ exec_req.c regref.c discovery.c daemon.c
forb_CLIENT_IDL = types.idl iop-idl.idl forb-idl.idl
forb_SERVER_IDL = forb-idl.idl
--- /dev/null
+#include <forb.h>
+#include <stdio.h>
+#include <unistd.h>
+
+/* Helper functions to "daemonize" forb servers. To make debugging
+ * easier, we do not detach from terminal. The idea behind daemonizing
+ * is to exit the parent when the server is ready to receive requests,
+ * so that is is not necessary to put delays between invocation of
+ * different servers. */
+
+static int daemon_pipe[2] = { 0, 0 };
+
+void forb_daemon_prepare(const char *pid)
+{
+ pid_t child;
+ int ret;
+ FILE *f;
+ char tmp;
+
+ ret = pipe(daemon_pipe);
+ if (ret) {
+ perror("pipe");
+ exit(1);
+ }
+ child = fork();
+ if (child == -1) {
+ perror("fork");
+ exit(1);
+ }
+ if (child > 0) {
+ close(daemon_pipe[1]);
+ ret = read(daemon_pipe[0], &tmp, 1);
+ if (pid) {
+ f = fopen(pid, "w");
+ if (f) {
+ fprintf(f, "%d\n", child);
+ fclose(f);
+ }
+ }
+ exit(ret > 0 ? 0 : 1);
+ } else {
+ close(daemon_pipe[0]);
+ }
+}
+
+void forb_daemon_ready()
+{
+ if (daemon_pipe[1])
+ write(daemon_pipe[1], "", 1);
+}
+