Newer
Older
#include <sys/stat.h>
#include <fcntl.h>
/*!
** Program entry point
**
** @param argc arg count
** @param argv arg vector
**
** @return program err code
*/
// Set default options
daemon.options.merge(get_default_options());
// Parse and merge line options
lineopt->getoptions(argc, argv);
daemon.options.merge(*lineopt);
// Get and apply options if conffile is specified
if (!euidaccess(daemon.options.conffile.c_str(), R_OK)) {
daemon.options.getoptions(daemon.options.conffile);
//apply arg line options
daemon.options.merge(*lineopt);
}
}
catch (const Error &e) {
e.print();
return e.code();
}
// Set a log file if necessary
if (daemon.options.logfile != "") {
logout.file(daemon.options.logfile);
logerr.file(daemon.options.logfile);
if (!register_signals())
return ERR_SIGNAL;
// Switch in daemon mode
::daemon(0, 0);
// if no daemon, can dup on quiet mode
else if (daemon.options.verbose == 0) {
int dn = open("/dev/null", O_RDWR);
if (dn == -1) {
logerr << "sld: open(/dev/null, O_RDWR): " << strerror(errno) << ".\n";
return ERR_UNKNOWN;
}
dup2(dn, STDOUT_FILENO);
dup2(dn, STDERR_FILENO);
close(dn);
}
// Write pid in a file
if (daemon.options.pidfile != "") {
FILE *fs = fopen(daemon.options.pidfile.c_str(), "w");
if (fs) {
fprintf(fs, "%d", getpid());
fclose(fs);
} else logerr << "sld: fopen: " << strerror(errno) << ".\n";
}
// run daemon
try { daemon.run(); }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*!
** Register signals
**
** @return success status
*/
static bool register_signals() {
// register sigchild handler
if (signal(SIGCHLD, sigchild) == SIG_ERR) {
logerr << "sld: error: " << strerror(errno) << ".\n";
return false;
}
// register sigint handler
if (signal(SIGINT, sigint) == SIG_ERR) {
logerr << "sld: error: " << strerror(errno) << ".\n";
return false;
}
// register sigkill handler
if (signal(SIGTERM, sigint) == SIG_ERR) {
logerr << "sld: error: " << strerror(errno) << ".\n";
return false;
}
// register sigkill handler
if (signal(SIGHUP, sigint) == SIG_ERR) {
logerr << "sld: error: " << strerror(errno) << ".\n";
return false;
}
return true;
}
/*!
** Set default options
**
** @param opt option struc to set
**
*/
options.login = buf;
options.port = 18136;
options.conffile = "/etc/sldrc";
options.retrydelay = 60;
options.closefd = 1;
/*!
** Retrieve complete path of this executable
**
** @return program path
*/
string getbinpath() {
int pid = getpid();
char buf2[PATH_MAX] = "";
snprintf(buf, PATH_MAX, "/proc/%d/exe", pid);
if (readlink(buf, buf2, PATH_MAX) == 0) {
logerr << "sld: getbinpath: readlink: " << strerror(errno) << ".\n";
return (string) "";
}
return (string) buf2;
}
/*!
** copy src to dst
**
** @param src copy file src
** @param dst copy file dst
**
** @return true if all is copied
*/
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
bool cp(const char *src, const char *dst) {
assert(src);
assert(dst);
//open source file
FILE *fsrc = fopen(src, "r");
if (!fsrc)
return false;
//open destination file
FILE *fdst = fopen(dst, "w");
if (!fdst) {
fclose(fsrc);
return false;
}
//do copy
char buf[512];
size_t nread;
size_t nwrite;
while ((nread = fread(buf, 1, 512, fsrc)) > 0) {
nwrite = fwrite(buf, 1, nread, fdst);
if (nwrite != nread) {
fclose(fsrc);
fclose(fdst);
return false;
}
}
fclose(fsrc);
fclose(fdst);
return true;