Carsten Haitzler (The Rasterman) wrote:
On Mon, 24 Jul 2006 20:58:27 +0100 Essien Ita Essien <[EMAIL PROTECTED]>
babbled:

a quick scan of the patch - coding style. we can argue forever which is
best/right/wrong - but we won't. follow the existing style. i.e.

my bad. I've fixed the indentation here (and learn a lot about vim and formating in the process!). Next puzzle? :P

int
func(int x)
{
  int y, z;
y = 0;
  z = y;
if (x) {
      blah(x);
      blah(y);
    }

  if ((y == z) && (x))
    blah(z);
  return x;
}

etc.

see existing code. vim indent rules:

/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */

:)

the reason for this is - it is hard to follow code when it keeps hanging style
as you read it - a consistent style maks it easier to follow - regardless of
the style itself :) choice of style is fairly arbitrary though, but that
choice was made a long time ago.
Hiya all,

Doing some more cleanups in Entrance has led me to require a proper daemon library in the EFL. After acertaining that none such existed, I've ported a small library which i use for my personal coding to an Ecore subsystem Ecore_Daemon.

The process I followed are outlined here: http://www.linuxprofilm.com/articles/linux-daemon-howto.html

The patch contains the first incarnation of Ecore_Daemon after porting it from my code to an Ecore subsystem. The patch also contains an example in examples/daemon_example.c

Its a small contained body of code, so here we go.

The library is designed around ecore_daemon_daemonize(), which does all the work. You can set params before calling daemonize, but you can just use it as it is, as ecore_daemon_init() sets sane params for you already.

Please review [*hint* rephorm, handyande *end-hint* :D] and advice. (Especially all them autoKUNGfoo. I tested it properly, but i'm not a 3rd dan autoKUNGfoo practitioner so I may invariably break stuff?)

Once this is sorted out, I can get back to using it from within Entrance. Makes things a whole lot more nicer.

Cheers,
Essien





diff -Naur ecore/configure.in ecore.daemon/configure.in
--- ecore/configure.in	2006-07-25 23:21:52.000000000 +0100
+++ ecore.daemon/configure.in	2006-07-26 00:34:47.000000000 +0100
@@ -975,6 +975,52 @@
 AC_SUBST(ecore_dbus_cflags)
 AC_SUBST(ecore_dbus_libs)
 
+AC_MSG_CHECKING(whether ecore_daemon module is to be built)
+want_ecore_daemon="yes";
+have_ecore_daemon="no";
+ecore_daemon_cflags="";
+ecore_daemon_libs="";
+daemon_dir="";
+daemon_includes="";
+daemon_cflags="";
+daemon_libs="";
+
+AC_ARG_ENABLE(ecore-daemon,
+  [  --disable-ecore-daemon        disable the ecore_daemon module],
+  [
+    if test x"$enableval" = x"yes" ; then
+      AC_MSG_RESULT(yes)
+    else
+      AC_MSG_RESULT(no)
+      want_ecore_daemon="no"
+    fi
+  ], [
+    AC_MSG_RESULT(yes)
+  ]
+)
+
+if test "x$want_ecore_daemon" = "xyes"; then
+  AM_CONDITIONAL(BUILD_ECORE_DAEMON, true)
+  AC_DEFINE(BUILD_ECORE_DAEMON, 1, [Build Ecore_Daemon Module])
+  have_ecore_daemon="yes"
+  #daemon_dir="/usr";
+  #daemon_includes="";
+  #daemon_cflags="-I"$daemon_dir"/include/daemon-1.0 -I"$daemon_dir"/lib/daemon-1.0/include"
+  #daemon_libs="-L"$daemon_dir"/lib"
+  #daemon_ldflags="-ldaemon-1";
+  ecore_daemon_libs="-lecore_daemon";
+else
+  AM_CONDITIONAL(BUILD_ECORE_DAEMON, false)
+fi
+
+AC_SUBST(daemon_cflags)
+AC_SUBST(daemon_includes)
+AC_SUBST(daemon_ldflags)
+AC_SUBST(daemon_libs)
+
+AC_SUBST(ecore_daemon_cflags)
+AC_SUBST(ecore_daemon_libs)
+
 
 AC_MSG_CHECKING(whether ecore_config module is to be built)
 
@@ -1231,6 +1277,7 @@
 src/lib/ecore_config/Makefile
 src/lib/ecore_file/Makefile
 src/lib/ecore_dbus/Makefile
+src/lib/ecore_daemon/Makefile
 src/lib/ecore_directfb/Makefile
 examples/Makefile
 ecore-config
@@ -1261,6 +1308,7 @@
 echo "  Ecore_Ipc....................: $have_ecore_ipc (OpenSSL: $use_openssl)"
 echo "  Ecore_Config.................: $have_ecore_config"
 echo "  Ecore_DBUS...................: $have_ecore_dbus"
+echo "  Ecore_Daemon.................: $have_ecore_daemon"
 #echo "  Ecore_File...................: $have_ecore_file (Inotify: $use_inotify) (FAM: $use_fam) (Poll: $use_poll)"
 echo "  Ecore_File...................: $have_ecore_file (Inotify: $use_inotify) (Poll: $use_poll) (CURL: $use_curl)"
 echo
diff -Naur ecore/ecore-config.in ecore.daemon/ecore-config.in
--- ecore/ecore-config.in	2006-06-25 20:28:26.000000000 +0100
+++ ecore.daemon/ecore-config.in	2006-07-26 00:34:47.000000000 +0100
@@ -6,7 +6,7 @@
 
 usage() {
       echo "Usage: ecore-config [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--libs] [--cflags]" 1>&2
-      echo "                    [--libs-con] [--libs-config] [--libs-dbus] [--libs-evas] [--libs-fb]" 1>&2
+      echo "                    [--libs-con] [--libs-config] [--libs-dbus] [--libs-daemon] [--libs-evas] [--libs-fb]" 1>&2
       echo "                    [--libs-file] [--libs-ipc] [--libs-job] [--libs-txt] [--libs-x]" 1>&2
 }
 
@@ -49,7 +49,7 @@
       ;;
     --libs)
       [EMAIL PROTECTED]@
-      echo $libdirs -lecore @ecore_job_libs@ @ecore_x_libs@ @ecore_evas_libs@ @ecore_con_libs@ @ecore_ipc_libs@ @ecore_txt_libs@ @ecore_fb_libs@ @ecore_config_libs@ @ecore_file_libs@ @ecore_dbus_libs@ @EET_LIBS@ -lm
+      echo $libdirs -lecore @ecore_job_libs@ @ecore_x_libs@ @ecore_evas_libs@ @ecore_con_libs@ @ecore_ipc_libs@ @ecore_txt_libs@ @ecore_fb_libs@ @ecore_config_libs@ @ecore_file_libs@ @ecore_dbus_libs@ @ecore_daemon_libs@ @EET_LIBS@ -lm
       ;;
     --libs-con)
       [EMAIL PROTECTED]@
@@ -63,7 +63,11 @@
       [EMAIL PROTECTED]@
       echo $libdirs -lecore @ecore_dbus_libs@ -lm
       ;;
-    --libs-evas)
+    --libs-daemon)
+      [EMAIL PROTECTED]@
+      echo $libdirs -lecore @ecore_daemon_libs@ -lm
+      ;;
+     --libs-evas)
       [EMAIL PROTECTED]@
       echo $libdirs -lecore @ecore_evas_libs@ -lm
       ;;
diff -Naur ecore/ecore.spec.in ecore.daemon/ecore.spec.in
--- ecore/ecore.spec.in	2006-06-02 00:13:28.000000000 +0100
+++ ecore.daemon/ecore.spec.in	2006-07-26 00:34:47.000000000 +0100
@@ -27,7 +27,7 @@
 Group: Development/Libraries
 Requires: %{name} = %{version}
 Requires: openssl-devel, evas-devel, eet-devel
-Requires: ecore-con, ecore-config, ecore-dbus, ecore-evas, ecore-fb, ecore-file, ecore-ipc, ecore-job, ecore-txt, ecore-x
+Requires: ecore-con, ecore-config, ecore-dbus, ecore-daemon, ecore-evas, ecore-fb, ecore-file, ecore-ipc, ecore-job, ecore-txt, ecore-x
 
 %description devel
 Ecore development files
@@ -72,6 +72,14 @@
 %description dbus
 Ecore DBus Library
 
+%package daemon
+Summary: Ecore Daemon Library
+Group: Development/Libraries
+Requires: %{name} = %{version}
+
+%description daemon
+Ecore Daemon Library
+
 %package evas
 Summary: Ecore Evas Wrapper Library
 Group: Development/Libraries
@@ -189,6 +197,10 @@
 %defattr(-, root, root)
 %{_libdir}/libecore_dbus.so.*
 
+%files daemon
+%defattr(-, root, root)
+%{_libdir}/libecore_daemon.so.*
+
 %files evas
 %defattr(-, root, root)
 %{_libdir}/libecore_evas.so.*
diff -Naur ecore/examples/Makefile.am ecore.daemon/examples/Makefile.am
--- ecore/examples/Makefile.am	2006-01-23 18:36:04.000000000 +0100
+++ ecore.daemon/examples/Makefile.am	2006-07-26 00:34:47.000000000 +0100
@@ -22,13 +22,18 @@
 X_EXAMPLES = x_window_example
 endif
 
-noinst_PROGRAMS = $(EXAMPLES) $(CON_EXAMPLES) $(CONFIG_EXAMPLES) $(X_EXAMPLES)
+if BUILD_ECORE_DAEMON
+DAEMON_EXAMPLES = daemon_example
+endif
+
+noinst_PROGRAMS = $(EXAMPLES) $(CON_EXAMPLES) $(CONFIG_EXAMPLES) $(X_EXAMPLES) $(DAEMON_EXAMPLES)
 
 INCLUDES = \
 -I$(top_srcdir)/src/lib/ecore \
 -I$(top_srcdir)/src/lib/ecore_config \
 -I$(top_srcdir)/src/lib/ecore_x \
 -I$(top_srcdir)/src/lib/ecore_con \
+-I$(top_srcdir)/src/lib/ecore_daemon
 @EVAS_CFLAGS@
 
 LDADD = \
@@ -77,3 +82,8 @@
                                 $(top_builddir)/src/lib/ecore_x/libecore_x.la
 				
 endif
+
+if BUILD_ECORE_DAEMON
+daemon_example_SOURCES      = daemon_example.c
+daemon_example_LDADD        = $(top_builddir)/src/lib/ecore_daemon/libecore_daemon.la
+endif
diff -Naur ecore/examples/daemon_example.c ecore.daemon/examples/daemon_example.c
--- ecore/examples/daemon_example.c	1970-01-01 01:00:00.000000000 +0100
+++ ecore.daemon/examples/daemon_example.c	2006-07-26 00:38:14.000000000 +0100
@@ -0,0 +1,30 @@
+/*
+ * vim:ts=8:sw=3:sts=3:noexpandtab:cino=>5n-3f0^-2{2
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <Ecore_Daemon.h>
+
+int main()
+{
+	if(!ecore_daemon_init()) 
+	{
+		fprintf(stderr, "Error init'ing ecore_daemon subsystem\n");
+		exit(EXIT_FAILURE);
+	}
+
+	printf("doing nothing in the foreground for 5 secs! You'll see this\n");
+	sleep(5);
+	printf("Going into daemon mode now!\n"
+			"From now on, you won't see anything.\n"
+			"Do a 'px aux | grep daemon_example' to see if its still there and running.\n"
+			"It will run for 20 secs\n"
+			"Watch that the pid has changed.\n");
+	ecore_daemon_daemonize();
+	printf("Now a daemon. You WONT see this\n");
+	sleep(20);
+
+	ecore_daemon_shutdown();
+	exit(EXIT_SUCCESS);
+}
diff -Naur ecore/src/lib/Makefile.am ecore.daemon/src/lib/Makefile.am
--- ecore/src/lib/Makefile.am	2005-10-15 11:37:37.000000000 +0100
+++ ecore.daemon/src/lib/Makefile.am	2006-07-26 00:34:47.000000000 +0100
@@ -11,4 +11,5 @@
 ecore_evas \
 ecore_config \
 ecore_file \
-ecore_dbus 
+ecore_dbus \
+ecore_daemon
diff -Naur ecore/src/lib/ecore_daemon/Ecore_Daemon.h ecore.daemon/src/lib/ecore_daemon/Ecore_Daemon.h
--- ecore/src/lib/ecore_daemon/Ecore_Daemon.h	1970-01-01 01:00:00.000000000 +0100
+++ ecore.daemon/src/lib/ecore_daemon/Ecore_Daemon.h	2006-07-26 00:35:58.000000000 +0100
@@ -0,0 +1,59 @@
+/*
+ * vim:ts=8:sw=3:sts=3:noexpandtab:cino=>5n-3f0^-2{2
+ */
+#ifndef _ECORE_DAEMON_H
+#define _ECORE_DAEMON_H
+#endif
+
+#ifdef EAPI
+#undef EAPI
+#endif
+#ifdef WIN32
+# ifdef BUILDING_DLL
+#  define EAPI __declspec(dllexport)
+# else
+#  define EAPI __declspec(dllimport)
+# endif
+#else
+# ifdef __GNUC__
+#  if __GNUC__ >= 4
+#   define EAPI __attribute__ ((visibility("default")))
+#  else
+#   define EAPI
+#  endif
+# else
+#  define EAPI
+# endif
+#endif
+
+#define ECORE_DAEMON_MAJOR_PROTOCOL_VERSION 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+
+	EAPI int ecore_daemon_init();
+	EAPI void ecore_daemon_shutdown();
+
+	EAPI pid_t ecore_daemon_daemonize();
+
+	EAPI int ecore_daemon_get_umask();
+	EAPI void  ecore_daemon_set_umask(int);
+
+	EAPI char* ecore_daemon_get_work_dir();
+	EAPI void ecore_daemon_set_work_dir(const char*);
+
+	EAPI char* ecore_daemon_get_stdin();
+	EAPI void ecore_daemon_set_stdin(const char*);
+
+	EAPI char* ecore_daemon_get_stdout();
+	EAPI void ecore_daemon_set_stdout(const char*);
+
+	EAPI char* ecore_daemon_get_stderr();
+	EAPI void ecore_daemon_set_stderr(const char*);
+
+#ifdef __cplusplus
+}
+#endif
diff -Naur ecore/src/lib/ecore_daemon/Makefile.am ecore.daemon/src/lib/ecore_daemon/Makefile.am
--- ecore/src/lib/ecore_daemon/Makefile.am	1970-01-01 01:00:00.000000000 +0100
+++ ecore.daemon/src/lib/ecore_daemon/Makefile.am	2006-07-26 00:35:58.000000000 +0100
@@ -0,0 +1,32 @@
+MAINTAINERCLEANFILES = Makefile.in
+
+INCLUDES = \
+-I$(top_builddir)/src/lib/ecore \
+-I$(top_builddir)/src/lib/ecore_daemon \
+-I$(top_srcdir)/src/lib/ecore \
+-I$(top_srcdir)/src/lib/ecore_daemon \
[EMAIL PROTECTED]@
+
+libecore_daemon_la_LDFLAGS = -version-info 1:0:0 \
+-L$(top_builddir)/src/lib/ecore/.libs
+
+if BUILD_ECORE_DAEMON
+
+lib_LTLIBRARIES = libecore_daemon.la
+include_HEADERS = \
+Ecore_Daemon.h
+
+libecore_daemon_la_SOURCES = \
+ecore_daemon.c
+
+libecore_daemon_la_LIBADD = \
+$(top_builddir)/src/lib/ecore/libecore.la \
[EMAIL PROTECTED]@
+
+libecore_daemon_la_DEPENDENCIES = \
+$(top_builddir)/src/lib/ecore/libecore.la
+
+endif
+
+EXTRA_DIST = \
+ecore_daemon.c 
diff -Naur ecore/src/lib/ecore_daemon/ecore_daemon.c ecore.daemon/src/lib/ecore_daemon/ecore_daemon.c
--- ecore/src/lib/ecore_daemon/ecore_daemon.c	1970-01-01 01:00:00.000000000 +0100
+++ ecore.daemon/src/lib/ecore_daemon/ecore_daemon.c	2006-07-26 00:36:33.000000000 +0100
@@ -0,0 +1,203 @@
+/*
+ * vim:ts=8:sw=3:sts=3:noexpandtab:cino=>5n-3f0^-2{2
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+
+struct __Ecore_Daemon 
+{
+   int umask;
+   char* work_dir;
+   char* fstdin;
+   char* fstdout;
+   char* fstderr;
+};
+
+typedef struct __Ecore_Daemon _Ecore_Daemon;
+typedef struct __Ecore_Daemon *Ecore_Daemon;
+
+static Ecore_Daemon _ecore_daemon_new();
+static void _ecore_daemon_free(Ecore_Daemon);
+
+static int _do_fork();
+static void _close_fds();
+static void _reopen_std_fds(const char*, const char*, const char*);
+
+static Ecore_Daemon _ecd;
+
+int 
+ecore_daemon_init()
+{
+   _ecd = _ecore_daemon_new();
+   if(_ecd)
+     return 1;
+   else 
+     return 0; 
+}
+
+void 
+ecore_daemon_shutdown()
+{
+   _ecore_daemon_free(_ecd);
+}
+
+pid_t 
+ecore_daemon_daemonize()
+{
+   if(!_ecd) 
+     return 0;
+
+   if(!_do_fork()) 
+     return 0;
+
+   if(!setsid()) 
+     return 0;
+
+   if(!_do_fork()) 
+     return 0;
+
+   /*TODO how do we handle errno and perror so apps
+    * can still access similar? How does the rest of Ecore
+    * handle such?
+    * */
+   if(chdir(_ecd->work_dir) == -1) 
+     return 0;
+
+   umask(_ecd->umask);
+
+   _close_fds();
+   _reopen_std_fds(_ecd->fstdin, _ecd->fstdout, _ecd->fstderr);
+
+   return getpid();
+}
+
+int 
+ecore_daemon_get_umask()
+{
+   return _ecd->umask;
+}
+
+void 
+ecore_daemon_set_umask(int mask)
+{
+   _ecd->umask = mask;
+}
+
+char* 
+ecore_daemon_get_work_dir()
+{
+   return _ecd->work_dir;
+}
+
+void 
+ecore_daemon_set_work_dir(const char *work_dir)
+{
+   free(_ecd->work_dir);
+   _ecd->work_dir = strdup(work_dir);
+}
+
+char* 
+ecore_daemon_get_stdin()
+{
+   return _ecd->fstdin;
+}
+
+void 
+ecore_daemon_set_stdin(const char *f)
+{
+   free(_ecd->fstdin);
+   _ecd->fstdin = strdup(f);
+}
+
+char*
+ecore_daemon_get_stdout()
+{
+   return _ecd->fstdout;
+}
+
+void 
+ecore_daemon_set_stdout(const char *f)
+{
+   free(_ecd->fstdout);
+   _ecd->fstdout = strdup(f);
+}
+
+char* 
+ecore_daemon_get_stderr()
+{
+   return _ecd->fstderr;
+}
+
+void 
+ecore_daemon_set_stderr(const char *f)
+{
+   free(_ecd->fstderr);
+   _ecd->fstderr = strdup(f);
+}
+
+
+/*privates*/
+
+static Ecore_Daemon 
+_ecore_daemon_new()
+{
+   Ecore_Daemon d = calloc(1, sizeof(_Ecore_Daemon));
+
+   if(!d) 
+     return NULL;
+
+   d->umask = 0;
+   d->work_dir = strdup("/");
+   d->fstdin = strdup("/dev/null");
+   d->fstdout = strdup("/dev/null");
+   d->fstderr = strdup("/dev/null");
+
+   return d;
+}
+
+static void 
+_ecore_daemon_free(Ecore_Daemon _ecd)
+{
+   free(_ecd->work_dir);
+
+   free(_ecd->fstdin);
+   free(_ecd->fstdout);
+   free(_ecd->fstderr);
+
+   free(_ecd);
+}
+
+static int 
+_do_fork()
+{
+   int pid = fork();
+   if(pid < 0) 
+     return 0; 
+   else if(pid > 0) /*this is parent*/ 
+     _exit(EXIT_SUCCESS);
+
+   return 1;
+}
+
+
+static void 
+_close_fds()
+{
+   close(0);
+   close(1);
+   close(2);
+}
+
+static void 
+_reopen_std_fds(const char* fin, const char *fout, const char *ferr)
+{
+   /*TODO: Error checks and proper status messages*/
+   freopen(fin, "r", stdin);
+   freopen(fout, "w", stdout);
+   freopen(ferr, "w", stderr);
+}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to