Remove daemonize code (#5181)

Signed-off-by: shengjun.li <shengjun.li@zilliz.com>
pull/5204/head
shengjun.li 2021-05-13 09:19:06 +08:00 committed by GitHub
parent 0ec2a6d33a
commit 2e4aaff531
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 134 deletions

View File

@ -29,8 +29,6 @@ print_help(const std::string& app_name) {
std::cout << " Options:" << std::endl;
std::cout << " -h --help Print this help." << std::endl;
std::cout << " -c --conf_file filename Read configuration from the file." << std::endl;
std::cout << " -d --daemon Daemonize this application." << std::endl;
std::cout << " -p --pid_file filename PID file used by daemonized app." << std::endl;
std::cout << std::endl;
}
@ -102,16 +100,6 @@ main(int argc, char* argv[]) {
std::cout << "Loading configuration from: " << config_filename << std::endl;
break;
}
case 'p': {
char* pid_filename_ptr = strdup(optarg);
pid_filename = pid_filename_ptr;
free(pid_filename_ptr);
std::cout << pid_filename << std::endl;
break;
}
case 'd':
start_daemonized = 1;
break;
case 'h':
print_help(app_name);
return EXIT_SUCCESS;
@ -132,7 +120,7 @@ main(int argc, char* argv[]) {
signal(SIGUSR2, milvus::server::SignalUtil::HandleSignal);
signal(SIGTERM, milvus::server::SignalUtil::HandleSignal);
server.Init(start_daemonized, pid_filename, config_filename);
server.Init(config_filename);
s = server.Start();
if (s.ok()) {

View File

@ -52,102 +52,12 @@ Server::GetInstance() {
}
void
Server::Init(int64_t daemonized, const std::string& pid_filename, const std::string& config_filename) {
daemonized_ = daemonized;
pid_filename_ = pid_filename;
Server::Init(const std::string& config_filename) {
config_filename_ = config_filename;
}
void
Server::Daemonize() {
if (daemonized_ == 0) {
return;
}
std::cout << "Milvus server run in daemonize mode";
pid_t pid = 0;
// Fork off the parent process
pid = fork();
// An error occurred
if (pid < 0) {
exit(EXIT_FAILURE);
}
// Success: terminate parent
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// On success: The child process becomes session leader
if (setsid() < 0) {
exit(EXIT_FAILURE);
}
// Ignore signal sent from child to parent process
signal(SIGCHLD, SIG_IGN);
// Fork off for the second time
pid = fork();
// An error occurred
if (pid < 0) {
exit(EXIT_FAILURE);
}
// Terminate the parent
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// Set new file permissions
umask(0);
// Change the working directory to root
int ret = chdir("/");
if (ret != 0) {
return;
}
// Close all open fd
for (int64_t fd = sysconf(_SC_OPEN_MAX); fd > 0; fd--) {
close(fd);
}
std::cout << "Redirect stdin/stdout/stderr to /dev/null";
// Redirect stdin/stdout/stderr to /dev/null
stdin = fopen("/dev/null", "r");
stdout = fopen("/dev/null", "w+");
stderr = fopen("/dev/null", "w+");
// Try to write PID of daemon to lockfile
if (!pid_filename_.empty()) {
pid_fd_ = open(pid_filename_.c_str(), O_RDWR | O_CREAT, 0640);
if (pid_fd_ < 0) {
std::cerr << "Can't open filename: " + pid_filename_ + ", Error: " + strerror(errno);
exit(EXIT_FAILURE);
}
if (lockf(pid_fd_, F_TLOCK, 0) < 0) {
std::cerr << "Can't lock filename: " + pid_filename_ + ", Error: " + strerror(errno);
exit(EXIT_FAILURE);
}
std::string pid_file_context = std::to_string(getpid());
ssize_t res = write(pid_fd_, pid_file_context.c_str(), pid_file_context.size());
if (res != 0) {
return;
}
}
}
Status
Server::Start() {
if (daemonized_ != 0) {
Daemonize();
}
try {
/* Read config file */
Status s = LoadConfig();
@ -327,29 +237,6 @@ Server::Stop() {
}
#endif
/* Unlock and close lockfile */
if (pid_fd_ != -1) {
int ret = lockf(pid_fd_, F_ULOCK, 0);
if (ret != 0) {
std::cerr << "ERROR: Can't lock file: " << strerror(errno) << std::endl;
exit(0);
}
ret = close(pid_fd_);
if (ret != 0) {
std::cerr << "ERROR: Can't close file: " << strerror(errno) << std::endl;
exit(0);
}
}
/* delete lockfile */
if (!pid_filename_.empty()) {
int ret = unlink(pid_filename_.c_str());
if (ret != 0) {
std::cerr << "ERROR: Can't unlink file: " << strerror(errno) << std::endl;
exit(0);
}
}
StopService();
std::cerr << "Milvus server exit..." << std::endl;

View File

@ -23,7 +23,7 @@ class Server {
GetInstance();
void
Init(int64_t daemonized, const std::string& pid_filename, const std::string& config_filename);
Init(const std::string& config_filename);
Status
Start();
@ -34,9 +34,6 @@ class Server {
Server() = default;
~Server() = default;
void
Daemonize();
Status
LoadConfig();
@ -46,9 +43,6 @@ class Server {
StopService();
private:
int64_t daemonized_ = 0;
int pid_fd_ = -1;
std::string pid_filename_;
std::string config_filename_;
}; // Server