Newer
Older
/*!
** 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();
}
// register sigchild handler
if (signal(SIGCHLD, sigchild) == SIG_ERR) {
perror("sld: error");
return ERR_UNKNOWN;
// Switch in daemon mode
::daemon(0, !(daemon.options.closefd == 1));
// 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 perror("sld: fopen");
}
// run daemon
try { daemon.run(); }
std::cerr <<"sld: error: " << e.message() << "." << std::endl;
/*!
** 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) {
perror("sld: getbinpath: readlink: ");
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
*/
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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;