Newer
Older
static SLDaemon::Options get_default_options();
// retrieve arg line option and default options
// order is very important
lineopt = get_default_options();
lineopt.merge(daemon.getoptions(argc, argv));
// Get and apply options if conffile is specified
if (!euidaccess(lineopt.conffile.c_str(), R_OK))
daemon.applyoptions(daemon.getoptions(lineopt.conffile));
//apply arg line options
daemon.applyoptions(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;
// run daemon
try { daemon.run(); }
std::cerr <<"sld: error: " << e.message() << "." << std::endl;
/*!
** Set default options
**
** @param opt option struc to set
**
*/
static SLDaemon::Options get_default_options() {
char buf[128];
SLDaemon::Options options;
if (gethostname(buf, 128) == 0) {
options.login = buf;
}
options.port = 18136;
options.conffile = "/etc/sldrc";
options.retrydelay = 60;
return options;
}
string getbinpath() {
int pid = getpid();
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
}
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;