43 |
#endif |
#endif |
44 |
|
|
45 |
#include <stdlib.h> |
#include <stdlib.h> |
46 |
#include <getopt.h> |
#include <stdio.h> |
47 |
#include <signal.h> |
#include <signal.h> |
48 |
|
#include <sys/types.h> |
49 |
|
#include <sys/stat.h> |
50 |
|
#include <fcntl.h> |
51 |
|
#include <unistd.h> |
52 |
|
#include <errno.h> |
53 |
|
#include <string.h> |
54 |
#ifdef WITH_MODEM |
#ifdef WITH_MODEM |
55 |
# include <guile/gh.h> |
# include <guile/gh.h> |
56 |
#endif |
#endif |
70 |
char *program_name; |
char *program_name; |
71 |
|
|
72 |
void |
void |
73 |
handle_signals () |
handle_signals (void) |
74 |
{ |
{ |
75 |
signal (SIGINT, sig_exit); |
signal (SIGINT, sig_exit); |
76 |
signal (SIGTERM, sig_exit); |
signal (SIGTERM, sig_exit); |
78 |
signal (SIGHUP, sig_exit); |
signal (SIGHUP, sig_exit); |
79 |
} |
} |
80 |
|
|
81 |
|
void |
82 |
|
daemon_mode (void) |
83 |
|
{ |
84 |
|
int pid; |
85 |
|
int f; |
86 |
|
|
87 |
|
pid=fork(); |
88 |
|
if(pid == 0) |
89 |
|
{ |
90 |
|
f=open("/dev/null", O_RDWR); |
91 |
|
if(f<0) |
92 |
|
log (LOG_ERR, "Unable to open /dev/null: %s (%d)\n", strerror(errno), errno); |
93 |
|
else |
94 |
|
{ |
95 |
|
if(dup2(f, 0) < 0) |
96 |
|
log (LOG_ERR, "Error calling dup2 stdin: %s (%d)\n", strerror(errno), errno); |
97 |
|
if(dup2(f, 1) < 0) |
98 |
|
log (LOG_ERR, "Error calling dup2 stdout: %s (%d)\n", strerror(errno), errno); |
99 |
|
if(dup2(f, 2) < 0 ) |
100 |
|
log (LOG_ERR, "Error calling dup2 stderr: %s (%d)\n", strerror(errno), errno); |
101 |
|
if(close(f) < 0) |
102 |
|
log (LOG_ERR, "Error calling close /dev/null: %s (%d)\n", strerror(errno), errno); |
103 |
|
} |
104 |
|
if( setsid() <0 ) |
105 |
|
log (LOG_ERR, "Error calling setsid: %s (%d)\n", strerror(errno), errno); |
106 |
|
} |
107 |
|
else if(pid > 0) |
108 |
|
{ |
109 |
|
exit(0); |
110 |
|
} |
111 |
|
else |
112 |
|
{ |
113 |
|
log (LOG_ERR, "Unable to fork: %s (%d)\n", strerror(errno), errno); |
114 |
|
exit(1); |
115 |
|
} |
116 |
|
} |
117 |
|
|
118 |
#ifndef WITH_MODEM |
#ifndef WITH_MODEM |
119 |
int |
int |
120 |
main (argc, argv) |
main (argc, argv) |
149 |
exit (1); |
exit (1); |
150 |
} |
} |
151 |
|
|
152 |
|
if(PARAM_DAEMON) |
153 |
|
daemon_mode(); |
154 |
|
|
155 |
handle_signals (); |
handle_signals (); |
156 |
|
|
157 |
if (!resolve_functions ()) |
if (!resolve_functions ()) |
159 |
log (LOG_ERR, "Fatal error, exiting.\n"); |
log (LOG_ERR, "Fatal error, exiting.\n"); |
160 |
exit (1); |
exit (1); |
161 |
} |
} |
162 |
|
|
163 |
if (iface->connect () && haccess->connect ()) |
if (iface->connect () && haccess->connect ()) |
164 |
{ |
{ |
165 |
if (!engine_init ()) |
if (!engine_init ()) |