39 #include <sys/types.h> 61 #include "ServerExitConditions.h" 62 #include "SocketListener.h" 63 #include "TcpSocket.h" 64 #include "UnixSocket.h" 65 #include "PPTServer.h" 66 #include "BESModuleApp.h" 67 #include "DaemonCommandHandler.h" 68 #include "BESServerUtils.h" 72 #include "TheBESKeys.h" 74 #include "BESDaemonConstants.h" 76 #define BES_SERVER "/beslistener" 77 #define BES_SERVER_PID "/bes.pid" 78 #define DAEMON_PORT_STR "BES.DaemonPort" 79 #define DAEMON_UNIX_SOCK_STR "BES.DaemonUnixSocket" 82 extern "C" int set_sups(
const int target_sups_size,
const gid_t*
const target_sups_list);
86 void unblock_signals();
87 int start_master_beslistener();
88 bool stop_all_beslisteners(
int sig);
90 static string daemon_name;
93 static string beslistener_path;
94 static string file_for_daemon_pid;
97 volatile int master_beslistener_status = BESLISTENER_STOPPED;
98 volatile int num_children = 0;
99 static volatile int master_beslistener_pid = -1;
101 typedef map<string, string> arg_map;
102 static arg_map global_args;
103 static string debug_sink =
"";
111 static volatile sig_atomic_t sigchild = 0;
112 static volatile sig_atomic_t sigterm = 0;
113 static volatile sig_atomic_t sighup = 0;
115 static string errno_str(
const string &msg)
118 oss << daemon_name << msg;
119 const char *perror_string = strerror(errno);
120 if (perror_string) oss << perror_string;
133 static int pr_exit(
int status)
135 if (WIFEXITED(status)) {
136 switch (WEXITSTATUS(status)) {
137 case SERVER_EXIT_NORMAL_SHUTDOWN:
140 case SERVER_EXIT_FATAL_CANNOT_START:
141 cerr << daemon_name <<
": server cannot start, exited with status " << WEXITSTATUS(status) << endl;
142 cerr <<
"Please check all error messages " <<
"and adjust server installation" << endl;
145 case SERVER_EXIT_ABNORMAL_TERMINATION:
146 cerr << daemon_name <<
": abnormal server termination, exited with status " << WEXITSTATUS(status) << endl;
149 case SERVER_EXIT_RESTART:
150 cerr << daemon_name <<
": server has been requested to re-start." << endl;
151 return SERVER_EXIT_RESTART;
157 else if (WIFSIGNALED(status)) {
158 cerr << daemon_name <<
": abnormal server termination, signaled with signal number " << WTERMSIG(status)
161 if (WCOREDUMP(status)) {
162 cerr << daemon_name <<
": server dumped core." << endl;
168 else if (WIFSTOPPED(status)) {
169 cerr << daemon_name <<
": abnormal server termination, stopped with signal number " << WSTOPSIG(status) << endl;
184 sigaddset(&set, SIGCHLD);
185 sigaddset(&set, SIGHUP);
186 sigaddset(&set, SIGTERM);
188 if (sigprocmask(SIG_BLOCK, &set, 0) < 0) {
189 cerr << errno_str(
": sigprocmask error, blocking signals in stop_all_beslisteners ");
194 void unblock_signals()
198 sigaddset(&set, SIGCHLD);
199 sigaddset(&set, SIGHUP);
200 sigaddset(&set, SIGTERM);
202 if (sigprocmask(SIG_UNBLOCK, &set, 0) < 0) {
203 cerr << errno_str(
": sigprocmask error unblocking signals in stop_all_beslisteners ");
220 bool stop_all_beslisteners(
int sig)
222 BESDEBUG(
"besdaemon",
"besdaemon: stopping listeners" << endl);
226 BESDEBUG(
"besdaemon",
"besdaemon: master_beslistener_pid " << master_beslistener_pid << endl);
229 int status = killpg(master_beslistener_pid, sig);
232 cerr <<
"The sig argument is not a valid signal number." << endl;
237 <<
"The sending process is not the super-user and one or more of the target processes has an effective user ID different from that of the sending process." 242 cerr <<
"No process can be found in the process group specified by the process group (" 243 << master_beslistener_pid <<
")." << endl;
250 bool mbes_status_caught =
false;
252 while ((pid = wait(&status)) > 0) {
253 BESDEBUG(
"besdaemon",
"besdaemon: caught listener: " << pid <<
" raw status: " << status << endl);
254 if (pid == master_beslistener_pid) {
255 master_beslistener_status = pr_exit(status);
256 mbes_status_caught =
true;
257 BESDEBUG(
"besdaemon",
258 "besdaemon: caught master beslistener: " << pid <<
" status: " << master_beslistener_status << endl);
262 BESDEBUG(
"besdaemon",
"besdaemon: done catching listeners (last pid:" << pid <<
")" << endl);
265 BESDEBUG(
"besdaemon",
"besdaemon: unblocking signals " << endl);
266 return mbes_status_caught;
276 char **update_beslistener_args()
278 char **arguments =
new char*[global_args.size() * 2 + 1];
282 arguments[0] = strdup(global_args[
"beslistener"].c_str());
285 arg_map::iterator it;
286 for (it = global_args.begin(); it != global_args.end(); ++it) {
287 BESDEBUG(
"besdaemon",
"besdaemon; global_args " << (*it).first <<
" => " << (*it).second << endl);
291 if ((*it).first ==
"-d") {
292 arguments[i++] = strdup(
"-d");
297 arguments[i++] = strdup(debug_opts.c_str());
299 else if ((*it).first !=
"beslistener") {
300 arguments[i++] = strdup((*it).first.c_str());
301 arguments[i++] = strdup((*it).second.c_str());
321 int start_master_beslistener()
326 if (pipe(pipefd) < 0) {
327 cerr << errno_str(
": pipe error ");
332 if ((pid = fork()) < 0) {
333 cerr << errno_str(
": fork error ");
347 if (dup2(pipefd[1], BESLISTENER_PIPE_FD) != BESLISTENER_PIPE_FD) {
348 cerr << errno_str(
": dup2 error ");
354 char **arguments = update_beslistener_args();
356 BESDEBUG(
"besdaemon",
"Starting: " << arguments[0] << endl);
361 if (command_server) command_server->closeConnection();
364 execvp(arguments[0], arguments);
367 cerr << errno_str(
": mounting listener, subprocess failed: ");
378 BESDEBUG(
"besdaemon",
"besdaemon: master beslistener pid: " << pid << endl);
381 int beslistener_start_status;
382 int status = read(pipefd[0], &beslistener_start_status,
sizeof(beslistener_start_status));
385 cerr <<
"Could not read master beslistener status; the master pid was not changed." << endl;
389 else if (beslistener_start_status != BESLISTENER_RUNNING) {
390 cerr <<
"The beslistener status is not 'BESLISTENER_RUNNING' (it is '" << beslistener_start_status
391 <<
"') the master pid was not changed." << endl;
396 BESDEBUG(
"besdaemon",
"besdaemon: master beslistener start status: " << beslistener_start_status << endl);
399 master_beslistener_pid = pid;
400 master_beslistener_status = BESLISTENER_RUNNING;
410 static void cleanup_resources()
416 if (!access(file_for_daemon_pid.c_str(), F_OK)) {
417 (void) remove(file_for_daemon_pid.c_str());
421 (void) remove(file_for_daemon_pid.c_str());
427 static void CatchSigChild(
int signal)
429 if (signal == SIGCHLD) {
434 static void CatchSigHup(
int signal)
436 if (signal == SIGHUP) {
441 static void CatchSigTerm(
int signal)
443 if (signal == SIGTERM) {
448 static void process_signals()
458 int pid = wait(&status);
465 if (pid == master_beslistener_pid) master_beslistener_status = pr_exit(status);
482 stop_all_beslisteners(SIGHUP);
485 if (start_master_beslistener() == 0) {
486 cerr <<
"Could not restart the master beslistener." << endl;
487 stop_all_beslisteners(SIGTERM);
500 stop_all_beslisteners(SIGTERM);
524 BESDEBUG(
"besdaemon",
"besdaemon: Starting command processor." << endl);
535 port = strtol(port_str.c_str(), &ptr, 10);
537 cerr <<
"Invalid port number for daemon command interface: " << port_str << endl;
543 BESDEBUG(
"besdaemon",
"besdaemon: listening on port: " << port << endl);
545 listener.listen(my_socket);
552 if (!usock_str.empty()) {
553 BESDEBUG(
"besdaemon",
"besdaemon: listening on unix socket: " << usock_str << endl);
555 listener.listen(unix_socket);
558 if (!port_found && !usock_found) {
559 BESDEBUG(
"besdaemon",
"Neither a port nor a unix socket was set for the daemon command interface." << endl);
563 BESDEBUG(
"besdaemon",
"besdaemon: starting command interface on port: " << port << endl);
564 command_server =
new PPTServer(&handler, &listener,
false);
574 command_server->closeConnection();
580 cerr <<
"daemon: " <<
"caught unknown exception" << endl;
583 delete command_server;
593 stop_all_beslisteners(SIGTERM);
606 static void register_signal_handlers()
608 struct sigaction act;
611 sigemptyset(&act.sa_mask);
612 sigaddset(&act.sa_mask, SIGCHLD);
613 sigaddset(&act.sa_mask, SIGTERM);
614 sigaddset(&act.sa_mask, SIGHUP);
617 BESDEBUG(
"besdaemon",
"besdaemon: setting restart for sigchld." << endl);
618 act.sa_flags |= SA_RESTART;
621 act.sa_handler = CatchSigChild;
622 if (sigaction(SIGCHLD, &act, 0)) {
623 cerr <<
"Could not register a handler to catch beslistener status." << endl;
627 act.sa_handler = CatchSigTerm;
628 if (sigaction(SIGTERM, &act, 0) < 0) {
629 cerr <<
"Could not register a handler to catch the terminate signal." << endl;
633 act.sa_handler = CatchSigHup;
634 if (sigaction(SIGHUP, &act, 0) < 0) {
635 cerr <<
"Could not register a handler to catch the hang-up signal." << endl;
646 static int daemon_init()
649 if ((pid = fork()) < 0)
663 static void store_daemon_id(
int pid)
665 ofstream f(file_for_daemon_pid.c_str());
667 cerr << errno_str(
": unable to create pid file " + file_for_daemon_pid +
": ");
675 mode_t new_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
676 (void) chmod(file_for_daemon_pid.c_str(), new_mode);
688 static bool load_names(
const string &install_dir,
const string &pid_dir)
690 string bindir =
"/bin";
691 if (!pid_dir.empty()) {
692 file_for_daemon_pid = pid_dir;
695 if (!install_dir.empty()) {
696 beslistener_path = install_dir;
697 beslistener_path += bindir;
698 if (file_for_daemon_pid.empty()) {
699 file_for_daemon_pid = install_dir +
"/var/run";
708 string prog = daemon_name;
709 string::size_type slash = prog.find_last_of(
'/');
710 if (slash != string::npos) {
711 beslistener_path = prog.substr(0, slash);
712 slash = prog.find_last_of(
'/');
713 if (slash != string::npos) {
714 string root = prog.substr(0, slash);
715 if (file_for_daemon_pid.empty()) {
716 file_for_daemon_pid = root +
"/var/run";
722 if (file_for_daemon_pid.empty()) {
723 file_for_daemon_pid = beslistener_path;
729 if (beslistener_path ==
"") {
730 beslistener_path =
".";
731 if (file_for_daemon_pid.empty()) {
732 file_for_daemon_pid =
"./run";
736 beslistener_path += BES_SERVER;
737 file_for_daemon_pid += BES_SERVER_PID;
739 if (access(beslistener_path.c_str(), F_OK) != 0) {
740 cerr << daemon_name <<
": cannot find " << beslistener_path << endl
741 <<
"Please either pass -i <install_dir> on the command line." << endl;
746 global_args[
"beslistener"] = beslistener_path;
751 static void set_group_id()
753 #if !defined(OS2) && !defined(TPF) 759 BESDEBUG(
"server",
"beslistener: Setting group id ... " << endl);
761 string key =
"BES.Group";
767 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
771 exit(SERVER_EXIT_FATAL_CANNOT_START);
774 if (!found || group_str.empty()) {
775 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
776 string err =
"FAILED: Group not specified in BES configuration file";
779 exit(SERVER_EXIT_FATAL_CANNOT_START);
781 BESDEBUG(
"server",
"to " << group_str <<
" ... " << endl);
784 if (group_str[0] ==
'#') {
786 const char *group_c = group_str.c_str();
788 new_gid = atoi(group_c);
793 ent = getgrnam(group_str.c_str());
795 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
796 string err = (string)
"FAILED: Group " + group_str +
" does not exist";
799 exit(SERVER_EXIT_FATAL_CANNOT_START);
801 new_gid = ent->gr_gid;
805 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
807 err <<
"FAILED: Group id " << new_gid <<
" not a valid group id for BES";
808 cerr << err.str() << endl;
809 LOG(err.str() << endl);
810 exit(SERVER_EXIT_FATAL_CANNOT_START);
813 BESDEBUG(
"server",
"to id " << new_gid <<
" ... " << endl);
814 if (setgid(new_gid) == -1) {
815 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
817 err <<
"FAILED: unable to set the group id to " << new_gid;
818 cerr << err.str() << endl;
819 LOG(err.str() << endl);
820 exit(SERVER_EXIT_FATAL_CANNOT_START);
823 BESDEBUG(
"server",
"OK" << endl);
825 BESDEBUG(
"server",
"beslistener: Groups not supported in this OS" << endl );
829 static void set_user_id()
831 BESDEBUG(
"server",
"beslistener: Setting user id ... " << endl);
838 string key =
"BES.User";
844 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
845 string err = (string)
"FAILED: " + e.
get_message();
848 exit(SERVER_EXIT_FATAL_CANNOT_START);
851 if (!found || user_str.empty()) {
852 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
853 string err = (string)
"FAILED: User not specified in BES config file";
856 exit(SERVER_EXIT_FATAL_CANNOT_START);
858 BESDEBUG(
"server",
"to " << user_str <<
" ... " << endl);
861 if (user_str[0] ==
'#') {
862 const char *user_str_c = user_str.c_str();
864 new_id = atoi(user_str_c);
868 ent = getpwnam(user_str.c_str());
870 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
871 string err = (string)
"FAILED: Bad user name specified: " + user_str;
874 exit(SERVER_EXIT_FATAL_CANNOT_START);
876 new_id = ent->pw_uid;
881 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
882 string err = (string)
"FAILED: BES cannot run as root";
885 exit(SERVER_EXIT_FATAL_CANNOT_START);
890 vector<gid_t> groups(1);
891 groups.at(0) = getegid();
892 if (set_sups(groups.size(), &groups[0]) == -1) {
893 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
895 err <<
"FAILED: Unable to relinquish supplementary groups (" << new_id <<
")";
896 cerr << err.str() << endl;
897 LOG(err.str() << endl);
898 exit(SERVER_EXIT_FATAL_CANNOT_START);
901 BESDEBUG(
"server",
"to " << new_id <<
" ... " << endl);
902 if (setuid(new_id) == -1) {
903 BESDEBUG(
"server",
"beslistener: FAILED" << endl);
905 err <<
"FAILED: Unable to set user id to " << new_id;
906 cerr << err.str() << endl;
907 LOG(err.str() << endl);
908 exit(SERVER_EXIT_FATAL_CANNOT_START);
911 BESDEBUG(
"server",
"OK" << endl);
917 int main(
int argc,
char *argv[])
919 uid_t curr_euid = geteuid();
921 #ifndef BES_DEVELOPER 924 cerr <<
"FAILED: Must be root to run BES" << endl;
925 exit(SERVER_EXIT_FATAL_CANNOT_START);
928 cerr <<
"Developer Mode: Not testing if BES is run by root" << endl;
931 daemon_name =
"besdaemon";
936 bool become_daemon =
true;
942 BESServerUtils::show_usage(daemon_name);
948 string config_file =
"";
950 unsigned short num_args = 1;
955 while ((c = getopt(argc, argv,
"hvsd:c:p:u:i:r:n")) != -1) {
958 BESServerUtils::show_version(daemon_name);
962 BESServerUtils::show_usage(daemon_name);
966 cerr <<
"Running in foreground!" << endl;
970 install_dir = optarg;
972 cout <<
"The specified install directory (-i option) " 973 <<
"is incorrectly formatted. Must be less than " 974 <<
"255 characters and include the characters " <<
"[0-9A-z_./-]" << endl;
977 global_args[
"-i"] = install_dir;
981 global_args[
"-s"] =
"";
987 cout <<
"The specified state directory (-r option) " 988 <<
"is incorrectly formatted. Must be less than " 989 <<
"255 characters and include the characters " <<
"[0-9A-z_./-]" << endl;
992 global_args[
"-r"] = pid_dir;
996 config_file = optarg;
998 cout <<
"The specified configuration file (-c option) " 999 <<
"is incorrectly formatted. Must be less than " 1000 <<
"255 characters and include the characters " <<
"[0-9A-z_./-]" << endl;
1003 global_args[
"-c"] = config_file;
1008 string check_path = optarg;
1010 cout <<
"The specified unix socket (-u option) " <<
"is incorrectly formatted. Must be less than " 1011 <<
"255 characters and include the characters " <<
"[0-9A-z_./-]" << endl;
1014 global_args[
"-u"] = check_path;
1020 string port_num = optarg;
1021 for (
unsigned int i = 0; i < port_num.length(); i++) {
1022 if (!isdigit(port_num[i])) {
1023 cout <<
"The specified port contains non-digit " <<
"characters: " << port_num << endl;
1027 global_args[
"-p"] = port_num;
1033 string check_arg = optarg;
1035 cout <<
"The specified debug options \"" << check_arg <<
"\" contains invalid characters" << endl;
1039 global_args[
"-d"] = check_arg;
1040 debug_sink = check_arg.substr(0, check_arg.find(
','));
1045 BESServerUtils::show_usage(daemon_name);
1053 if (argc > num_args) {
1054 cout << daemon_name <<
": too many arguments passed to the BES";
1055 BESServerUtils::show_usage(daemon_name);
1058 if (pid_dir.empty()) {
1059 pid_dir = install_dir;
1063 if (!config_file.empty()) {
1070 if (config_file.empty() && !install_dir.empty()) {
1071 if (install_dir[install_dir.length() - 1] !=
'/') {
1074 string conf_file = install_dir +
"etc/bes/bes.conf";
1081 cerr <<
"Caught BES Error while processing the daemon's options: " << e.
get_message() << endl;
1084 catch (std::exception &e) {
1085 cerr <<
"Caught C++ error while processing the daemon's options: " << e.what() << endl;
1089 cerr <<
"Caught unknown error while processing the daemon's options." << endl;
1095 if (!load_names(install_dir, pid_dir))
return 1;
1097 if (!access(file_for_daemon_pid.c_str(), F_OK)) {
1098 ifstream temp(file_for_daemon_pid.c_str());
1099 cout << daemon_name <<
": there seems to be a BES daemon already running at ";
1101 temp.getline(buf, 500);
1102 cout << buf << endl;
1111 store_daemon_id(getpid());
1113 register_signal_handlers();
1120 cerr <<
"Could not initialize the modules to get the log contexts." << endl;
1129 if (curr_euid == 0) {
1130 #ifdef BES_DEVELOPER 1131 cerr <<
"Developer Mode: Running as root - setting group and user ids" << endl;
1137 cerr <<
"Developer Mode: Not setting group or user ids" << endl;
1148 if (global_args.count(
"-d") == 0) {
1176 master_beslistener_pid = start_master_beslistener();
1177 if (master_beslistener_pid == 0) {
1178 cerr << daemon_name <<
": server cannot mount at first try (core dump). " 1179 <<
"Please correct problems on the process manager " << beslistener_path << endl;
1180 return master_beslistener_pid;
1183 BESDEBUG(
"besdaemon",
"besdaemon: master_beslistener_pid: " << master_beslistener_pid << endl);
1188 cerr <<
"Caught BES Error during initialization: " << e.
get_message() << endl;
1191 catch (std::exception &e) {
1192 cerr <<
"Caught C++ error during initialization: " << e.what() << endl;
1196 cerr <<
"Caught unknown error during initialization." << endl;
1206 status = start_command_processor(handler);
1217 BESDEBUG(
"besdaemon",
"besdaemon: master_beslistener_status: " << master_beslistener_status << endl);
1218 if (master_beslistener_status == BESLISTENER_RESTART) {
1219 master_beslistener_status = BESLISTENER_STOPPED;
1221 start_master_beslistener();
1224 else if (master_beslistener_status != BESLISTENER_RUNNING) {
1234 cerr <<
"Caught BES Error while starting the command handler: " << e.
get_message() << endl;
1236 catch (std::exception &e) {
1238 cerr <<
"Caught C++ error while starting the command handler: " << e.what() << endl;
1242 cerr <<
"Caught unknown error while starting the command handler." << endl;
1245 BESDEBUG(
"besdaemon",
"besdaemon: past the command processor start" << endl);
1247 cleanup_resources();
static bool pathname_ok(const string &path, bool strict)
Does the string name a potentailly valid pathname? Test the given pathname to verfiy that it is a val...
static void Register(const std::string &flagName)
register the specified debug flag
virtual std::string get_message()
get the error message for this exception
static bool command_line_arg_ok(const string &arg)
sanitize command line arguments
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static void SetUp(const std::string &values)
Sets up debugging for the bes.
virtual void initConnection()
Abstract exception class for the BES with basic string message.
static TheBESKeys * TheKeys()
static std::string GetOptionsString()
Base application object for all BES applications.
virtual int initialize(int argC, char **argV)
Load and initialize any BES modules.
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
static std::string ConfigFile