Commit f3d944580c88b0a8b6909cac684ea4f8fb105154
1 parent
ea7368b5
Exists in
master
Implementando serviço.
Showing
3 changed files
with
33 additions
and
3 deletions
Show diff stats
.gitignore
conf/systemd.service.in
1 | 1 | [Unit] |
2 | 2 | Description=@PACKAGE_DESCRIPTION@ |
3 | -After=syslog.target network.target | |
3 | +After=syslog.target network.target dbus.service | |
4 | 4 | |
5 | 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 | 9 | PIDFile=/var/run/@PACKAGE_NAME@.pid |
9 | 10 | KillMode=process |
10 | 11 | Restart=on-failure |
11 | 12 | |
12 | 13 | [Install] |
13 | 14 | WantedBy=default.target |
15 | + | ... | ... |
src/main.c
... | ... | @@ -33,6 +33,7 @@ |
33 | 33 | #include "private.h" |
34 | 34 | #include <stdio.h> |
35 | 35 | #include <stdlib.h> |
36 | +#include <string.h> | |
36 | 37 | #include <glib.h> |
37 | 38 | #include <glib-object.h> |
38 | 39 | #include <dbus/dbus.h> |
... | ... | @@ -172,14 +173,40 @@ static void cleanup() { |
172 | 173 | |
173 | 174 | int main(int argc, char *argv[]) { |
174 | 175 | |
176 | + static gboolean asDaemon = FALSE; | |
177 | + | |
175 | 178 | #if defined( HAVE_SYSLOG ) |
176 | 179 | openlog(g_get_prgname(), LOG_NDELAY, LOG_USER); |
177 | 180 | g_log_set_default_handler(g_syslog,NULL); |
178 | 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 | 202 | initialize(); |
181 | 203 | init_3270(); |
182 | 204 | |
205 | + if(asDaemon && daemon(0,0)) { | |
206 | + fprintf(stderr,"%s\n",strerror(errno)); | |
207 | + exit(-1); | |
208 | + } | |
209 | + | |
183 | 210 | atexit(cleanup); |
184 | 211 | if(pidfile) { |
185 | 212 | FILE * hpid = fopen(pidfile,"w"); | ... | ... |