Commit f3d944580c88b0a8b6909cac684ea4f8fb105154

Authored by Perry Werneck
1 parent ea7368b5
Exists in master

Implementando serviço.

@@ -16,3 +16,4 @@ dbus-glue.h @@ -16,3 +16,4 @@ dbus-glue.h
16 .obj 16 .obj
17 .src 17 .src
18 *.service 18 *.service
  19 +*.pid
conf/systemd.service.in
1 [Unit] 1 [Unit]
2 Description=@PACKAGE_DESCRIPTION@ 2 Description=@PACKAGE_DESCRIPTION@
3 -After=syslog.target network.target 3 +After=syslog.target network.target dbus.service
4 4
5 [Service] 5 [Service]
6 -ExecStart=/usr/sbin/@PACKAGE_NAME@  
7 -Type=forking 6 +ExecStart=/usr/sbin/@PACKAGE_NAME@ --daemon
  7 +Type=dbus
  8 +BusName=br.com.bb.pw3270.service
8 PIDFile=/var/run/@PACKAGE_NAME@.pid 9 PIDFile=/var/run/@PACKAGE_NAME@.pid
9 KillMode=process 10 KillMode=process
10 Restart=on-failure 11 Restart=on-failure
11 12
12 [Install] 13 [Install]
13 WantedBy=default.target 14 WantedBy=default.target
  15 +
@@ -33,6 +33,7 @@ @@ -33,6 +33,7 @@
33 #include "private.h" 33 #include "private.h"
34 #include <stdio.h> 34 #include <stdio.h>
35 #include <stdlib.h> 35 #include <stdlib.h>
  36 +#include <string.h>
36 #include <glib.h> 37 #include <glib.h>
37 #include <glib-object.h> 38 #include <glib-object.h>
38 #include <dbus/dbus.h> 39 #include <dbus/dbus.h>
@@ -172,14 +173,40 @@ static void cleanup() { @@ -172,14 +173,40 @@ static void cleanup() {
172 173
173 int main(int argc, char *argv[]) { 174 int main(int argc, char *argv[]) {
174 175
  176 + static gboolean asDaemon = FALSE;
  177 +
175 #if defined( HAVE_SYSLOG ) 178 #if defined( HAVE_SYSLOG )
176 openlog(g_get_prgname(), LOG_NDELAY, LOG_USER); 179 openlog(g_get_prgname(), LOG_NDELAY, LOG_USER);
177 g_log_set_default_handler(g_syslog,NULL); 180 g_log_set_default_handler(g_syslog,NULL);
178 #endif // HAVE_SYSLOG 181 #endif // HAVE_SYSLOG
179 182
  183 + // Verifica argumentos
  184 + static const GOptionEntry app_options[] = {
  185 + { "pidfile", 'p', 0, G_OPTION_ARG_STRING, &pidfile, "Path to pidfile" , NULL },
  186 + { "daemon", 'd', 0, G_OPTION_ARG_NONE, &asDaemon, "Run as daemon", NULL },
  187 + { NULL }
  188 + };
  189 +
  190 + GOptionContext * context = g_option_context_new (PACKAGE_DESCRIPTION);
  191 + GError * error = NULL;
  192 + GOptionGroup * group = g_option_group_new( PACKAGE_NAME, NULL, NULL, NULL, NULL);
  193 +
  194 + g_option_context_set_main_group(context, group);
  195 + g_option_context_add_main_entries(context, app_options, NULL);
  196 + if(!g_option_context_parse( context, &argc, &argv, &error )) {
  197 + fprintf(stderr,"%s\n",error->message);
  198 + exit(-1);
  199 + }
  200 +
  201 + // Inicia serviços
180 initialize(); 202 initialize();
181 init_3270(); 203 init_3270();
182 204
  205 + if(asDaemon && daemon(0,0)) {
  206 + fprintf(stderr,"%s\n",strerror(errno));
  207 + exit(-1);
  208 + }
  209 +
183 atexit(cleanup); 210 atexit(cleanup);
184 if(pidfile) { 211 if(pidfile) {
185 FILE * hpid = fopen(pidfile,"w"); 212 FILE * hpid = fopen(pidfile,"w");