43 |
#endif |
#endif |
44 |
|
|
45 |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <stdio.h> |
|
|
#include <signal.h> |
|
|
#include <sys/types.h> |
|
|
#include <sys/stat.h> |
|
|
#include <fcntl.h> |
|
46 |
#include <unistd.h> |
#include <unistd.h> |
|
#include <errno.h> |
|
|
#include <string.h> |
|
|
#include <libintl.h> |
|
47 |
#ifdef WITH_MODEM |
#ifdef WITH_MODEM |
48 |
# include <guile/gh.h> |
# include <guile/gh.h> |
49 |
#endif |
#endif |
50 |
|
|
51 |
#include "common.h" |
#include "common.h" |
52 |
|
#include "gettext.h" |
53 |
#include "log.h" |
#include "log.h" |
54 |
|
#include "misc.h" |
55 |
#include "options.h" |
#include "options.h" |
56 |
#include "resolver.h" |
#include "resolver.h" |
|
#include "sighndl.h" |
|
57 |
#include "engine.h" |
#include "engine.h" |
58 |
#include "access.h" |
#include "access.h" |
59 |
#include "protocol.h" |
#include "protocol.h" |
62 |
|
|
63 |
/* defined in common.h */ |
/* defined in common.h */ |
64 |
char *program_name; |
char *program_name; |
65 |
|
status_t status = sinit; |
66 |
|
|
|
void |
|
|
handle_signals (void) |
|
|
{ |
|
|
signal (SIGINT, sig_exit); |
|
|
signal (SIGTERM, sig_exit); |
|
|
if (!PARAM_DAEMON) |
|
|
signal (SIGHUP, sig_exit); |
|
|
} |
|
67 |
|
|
68 |
void |
void |
69 |
daemon_mode (void) |
clean_exit(n) |
70 |
|
int n; |
71 |
{ |
{ |
72 |
int pid; |
haccess->disconnect (); |
73 |
int f; |
iface->disconnect (); |
74 |
|
|
75 |
pid = fork (); |
if(PARAM_PID_FILE) |
76 |
if (pid == 0) |
remove_pidfile (); |
77 |
{ |
|
78 |
f = open ("/dev/null", O_RDWR); |
exit(n); |
|
if (f < 0) |
|
|
log (LOG_WARNING, gettext ("Unable to open /dev/null: %s (%d)\n"), |
|
|
strerror (errno), errno); |
|
|
else |
|
|
{ |
|
|
if (dup2 (f, 0) < 0) |
|
|
log (LOG_WARNING, gettext ("Error calling dup2 stdin: %s (%d)\n"), |
|
|
strerror (errno), errno); |
|
|
if (dup2 (f, 1) < 0) |
|
|
log (LOG_WARNING, |
|
|
gettext ("Error calling dup2 stdout: %s (%d)\n"), |
|
|
strerror (errno), errno); |
|
|
if (dup2 (f, 2) < 0) |
|
|
log (LOG_WARNING, |
|
|
gettext ("Error calling dup2 stderr: %s (%d)\n"), |
|
|
strerror (errno), errno); |
|
|
if (close (f) < 0) |
|
|
log (LOG_WARNING, |
|
|
gettext ("Error calling close /dev/null: %s (%d)\n"), |
|
|
strerror (errno), errno); |
|
|
} |
|
|
if (setsid () < 0) |
|
|
log (LOG_WARNING, gettext ("Error calling setsid: %s (%d)\n"), |
|
|
strerror (errno), errno); |
|
|
} |
|
|
else if (pid > 0) |
|
|
{ |
|
|
exit (0); |
|
|
} |
|
|
else |
|
|
{ |
|
|
log (LOG_ERR, gettext ("Unable to fork: %s (%d)\n"), strerror (errno), |
|
|
errno); |
|
|
exit (1); |
|
|
} |
|
79 |
} |
} |
80 |
|
|
81 |
|
|
82 |
#ifndef WITH_MODEM |
#ifndef WITH_MODEM |
83 |
int |
int |
84 |
main (argc, argv) |
main (argc, argv) |
115 |
exit (1); |
exit (1); |
116 |
} |
} |
117 |
|
|
118 |
|
if (!resolve_functions ()) |
119 |
|
exit (1); |
120 |
|
|
121 |
|
if(PARAM_PID_FILE && running_from_pidfile ()) |
122 |
|
exit (1); |
123 |
|
|
124 |
if (PARAM_DAEMON) |
if (PARAM_DAEMON) |
125 |
daemon_mode (); |
daemon_mode (); |
126 |
|
|
127 |
handle_signals (); |
if(PARAM_PID_FILE) |
128 |
|
write_pidfile (); |
129 |
|
|
130 |
if (!resolve_functions ()) |
handle_signals (); |
|
{ |
|
|
log (LOG_ERR, gettext ("Fatal error, exiting.\n")); |
|
|
exit (1); |
|
|
} |
|
131 |
|
|
132 |
if (iface->connect () && haccess->connect ()) |
do { |
133 |
{ |
status=sconnect; |
134 |
if (!engine_init ()) |
if (iface->connect () && haccess->connect ()) |
135 |
{ |
{ |
136 |
/* Should not happen */ |
if (!engine_init ()) |
137 |
log (LOG_ERR, gettext ("Unable to init engine, exiting.\n")); |
{ |
138 |
exit (1); |
/* Should not happen */ |
139 |
} |
log (LOG_ERR, gettext ("Unable to init engine, exiting.\n")); |
140 |
|
clean_exit (1); |
141 |
protocol->register_to_engine (haccess); |
} |
142 |
fdo_init (); |
|
143 |
engine_loop (); |
protocol->register_to_engine (haccess); |
144 |
haccess->disconnect (); |
fdo_init (); |
145 |
iface->disconnect (); |
status = srun; |
146 |
} |
engine_loop (); |
147 |
else |
haccess->disconnect (); |
148 |
{ |
iface->disconnect (); |
149 |
log (LOG_ERR, gettext ("Fatal error, exiting.\n")); |
} |
150 |
exit (1); |
else |
151 |
} |
{ |
152 |
|
if(!PARAM_AUTO_RECONNECT) |
153 |
|
{ |
154 |
|
log (LOG_ERR, gettext ("Fatal error, exiting.\n")); |
155 |
|
clean_exit (1); |
156 |
|
} |
157 |
|
else |
158 |
|
{ |
159 |
|
if(PARAM_RECONNECT_DELAY) |
160 |
|
{ |
161 |
|
log (LOG_NOTICE, gettext ("Reconnection in %d seconds...\n"), |
162 |
|
PARAM_RECONNECT_DELAY); |
163 |
|
sleep(PARAM_RECONNECT_DELAY); |
164 |
|
} |
165 |
|
else |
166 |
|
log (LOG_NOTICE, gettext ("Immediate reconnection...\n")); |
167 |
|
} |
168 |
|
|
169 |
|
} |
170 |
|
} |
171 |
|
while (PARAM_AUTO_RECONNECT && status!=sexit); |
172 |
|
|
173 |
#ifndef WITH_MODEM |
#ifndef WITH_MODEM |
174 |
return 0; |
return 0; |