From 52157aac9dc289f874f4a4081610b5a940fe3262 Mon Sep 17 00:00:00 2001 From: PerryWerneck Date: Sat, 4 Mar 2017 05:30:48 -0300 Subject: [PATCH] Implementando serviço SystemD --- Makefile.in | 7 +++++++ conf/systemd.service.in | 13 +++++++++++++ configure.ac | 22 ++++++++++++++++++++++ src/main.c | 46 ++++++++++++++++++++++++++++++++++++---------- 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 conf/systemd.service.in diff --git a/Makefile.in b/Makefile.in index f1f9362..dfe59a7 100644 --- a/Makefile.in +++ b/Makefile.in @@ -37,6 +37,7 @@ exec_prefix=@exec_prefix@ bindir=@bindir@ sbindir=@sbindir@ libdir=@libdir@ +unitdir=@SYSTEMD_SYSTEMUNITDIR@ BASEDIR=@BASEDIR@ SRCDIR=$(BASEDIR)/.src/$(PACKAGE_TARNAME)-$(PACKAGE_VERSION) @@ -124,6 +125,12 @@ Release: \ install: \ $(BINRLS)/$(PACKAGE_NAME) + @$(MKDIR) $(DESTDIR)/$(sbindir) + @$(INSTALL_PROGRAM) $(BINRLS)/$(PACKAGE_NAME) $(DESTDIR)/$(sbindir)/$(PACKAGE_NAME) + + @$(MKDIR) $(DESTDIR)/$(unitdir) + @$(INSTALL_DATA) conf/systemd.service $(DESTDIR)/$(unitdir)/$(PACKAGE_NAME).service + bz2: \ $(PACKAGE_TARNAME)-$(PACKAGE_VERSION).tar.bz2 diff --git a/conf/systemd.service.in b/conf/systemd.service.in new file mode 100644 index 0000000..6116a7e --- /dev/null +++ b/conf/systemd.service.in @@ -0,0 +1,13 @@ +[Unit] +Description=@PACKAGE_DESCRIPTION@ +After=syslog.target network.target + +[Service] +ExecStart=/usr/sbin/@PACKAGE_NAME@ +Type=forking +PIDFile=/var/run/@PACKAGE_NAME@.pid +KillMode=process +Restart=on-failure + +[Install] +WantedBy=default.target diff --git a/configure.ac b/configure.ac index 00a1277..15c8c83 100644 --- a/configure.ac +++ b/configure.ac @@ -234,6 +234,27 @@ AC_SUBST(LIB3270_LIBS) AC_SUBST(LIB3270_CFLAGS) dnl --------------------------------------------------------------------------- +dnl Check for systemd +dnl --------------------------------------------------------------------------- + +AC_ARG_WITH([systemdsystemunitdir], + AC_HELP_STRING([--with-systemdsystemunitdir=DIR], + [path to systemd system unit directory]), + [path_systemunitdir=${withval}]) + + +if test -z "${path_systemunitdir}"; then + AC_MSG_CHECKING([systemd system unit dir]) + path_systemunitdir="`$PKG_CONFIG --variable=systemdsystemunitdir systemd`" + if (test -z "${path_systemunitdir}"); then + AC_MSG_ERROR([systemd system unit directory is required]) + fi + AC_MSG_RESULT([${path_systemunitdir}]) +fi + +AC_SUBST(SYSTEMD_SYSTEMUNITDIR, [${path_systemunitdir}]) + +dnl --------------------------------------------------------------------------- dnl Check for C++ 2011 support dnl --------------------------------------------------------------------------- AC_DEFUN([AX_CHECK_COMPILE_FLAG], @@ -336,6 +357,7 @@ dnl Configure which files to generate. dnl --------------------------------------------------------------------------- AC_CONFIG_FILES(Makefile) +AC_CONFIG_FILES(conf/systemd.service) dnl --------------------------------------------------------------------------- dnl Output the generated config.status script. diff --git a/src/main.c b/src/main.c index 3ee766e..680b092 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,9 @@ * */ +#include "private.h" +#include +#include #include #include #include @@ -58,6 +61,12 @@ static DBusGConnection * connection = NULL; static DBusGProxy * proxy = NULL; +#ifdef DEBUG + static gchar * pidfile = PACKAGE_NAME ".pid"; +#else + static gchar * pidfile = "/var/run/" PACKAGE_NAME ".pid"; +#endif // DEBUG + GMainLoop * main_loop = NULL; /*---[ Implement ]----------------------------------------------------------------------------------*/ @@ -98,6 +107,11 @@ static void g_syslog(const gchar *log_domain,GLogLevelFlags log_level,const gcha *ptr = ' '; } +#ifdef DEBUG + printf("%s\n",text); + fflush(stdout); +#endif // DEBUG + syslog(logtype[f].priority,"%s",text); g_free(text); return; @@ -105,8 +119,8 @@ static void g_syslog(const gchar *log_domain,GLogLevelFlags log_level,const gcha } #ifdef DEBUG - fprintf(stderr,"%s\n",message); - fflush(stderr); + printf("%s\n",message); + fflush(stdout); #endif // DEBUG syslog(LOG_INFO,"%s %s",log_domain ? log_domain : "", message); @@ -118,7 +132,7 @@ static gboolean do_idle_check(gpointer dunno) { return TRUE; } -static int initialize(void) { +static void initialize() { GError * error = NULL; guint result; @@ -126,18 +140,17 @@ static int initialize(void) { connection = dbus_g_bus_get_private(bustype, g_main_context_default(), &error); if(error) { - g_message("Error \"%s\" getting D-Bus connection",error->message); + g_error("Error \"%s\" getting D-Bus connection",error->message); g_error_free(error); - return -1; + return; } proxy = dbus_g_proxy_new_for_name(connection,DBUS_SERVICE_DBUS,DBUS_PATH_DBUS,DBUS_INTERFACE_DBUS); org_freedesktop_DBus_request_name(proxy, PW3270_SERVICE_DBUS_SERVICE, DBUS_NAME_FLAG_DO_NOT_QUEUE, &result, &error); if(error) { - g_message("Error \"%s\" getting D-Bus name",error->message); - g_error_free(error); - return -1; + g_error("Error \"%s\" getting D-Bus name",error->message); + return; } GType object_type = PW3270_TYPE_DBUS; @@ -147,10 +160,15 @@ static int initialize(void) { g_timeout_add_seconds (1, do_idle_check, NULL); - return 0; } +static void cleanup() { + + if(pidfile) { + remove(pidfile); + } +} int main(int argc, char *argv[]) { @@ -162,8 +180,16 @@ int main(int argc, char *argv[]) { initialize(); init_3270(); - main_loop = g_main_loop_new(NULL, FALSE); + atexit(cleanup); + if(pidfile) { + FILE * hpid = fopen(pidfile,"w"); + if(hpid) { + fprintf(hpid,"%u",(unsigned int) getpid()); + fclose(hpid); + } + } + main_loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(main_loop); return 0; -- libgit2 0.21.2