]> rtime.felk.cvut.cz Git - frescor/forb.git/commitdiff
Add helper functions for daemonizing servers
authorMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 18 Jun 2010 23:37:51 +0000 (01:37 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 18 Jun 2010 23:37:51 +0000 (01:37 +0200)
src/Makefile.omk
src/daemon.c [new file with mode: 0644]
src/forb.h

index 8876fcc6e09285687468f23bb5e7a4a945b4728a..8a382b8d5d5f278c35871a00497a2238656c648d 100644 (file)
@@ -7,7 +7,7 @@ CFLAGS += -I.
 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
 
diff --git a/src/daemon.c b/src/daemon.c
new file mode 100644 (file)
index 0000000..5e51b10
--- /dev/null
@@ -0,0 +1,51 @@
+#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);
+}
+
index 187d0d33e4131c6212bb211f28b76bbc02f2a512..9e26590d4e201c4c4769a2bdaf7fdda62504361c 100644 (file)
@@ -226,6 +226,13 @@ forb_get_req_source(const forb_object obj, forb_server_id *req_source);
 const char *
 forb_strerror(CORBA_Environment *env);
 
+void
+forb_daemon_prepare(const char *pid);
+
+void
+forb_daemon_ready();
+
+
 #ifdef __cplusplus
 } /* extern "C"*/
 #endif