Newer
Older
SLDaemon &daemon = SLDaemon::Instance();
SLDaemon::Options *lineopt;
SLDaemon::Options *fileopt = NULL;
try {
//parse arg line options
lineopt = daemon.getoptions(argc, argv);
assert(lineopt);
if (lineopt->conffile != "") {
//parse conf file options
fileopt = daemon.getoptions(lineopt->conffile);
}
}
catch (const Error &e) {
e.print();
return e.code();
}
try {
//apply config file option
if (fileopt)
daemon.applyoptions(fileopt);
// register sigchild handler
if (signal(SIGCHLD, sigchild) == SIG_ERR) {
perror("sld: error");
return ERR_UNKNOWN;
}
//execute receive command
daemon.run();
}
catch (const Error &e) {
std::cerr <<"sld: error: " << e.message() << "." << std::endl;
string getbinpath() {
int pid = getpid();
58
59
60
61
62
63
64
65
66
67
68
69
70
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
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;