00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #include "libts.h"
00031 #include "ink_sys_control.h"
00032 #include "signals.h"
00033 #include "DiagsConfig.h"
00034 #include "Main.h"
00035 
00036 #include "Error.h"
00037 #include "P_EventSystem.h"
00038 #include "P_RecProcess.h"
00039 
00040 #include "ProcessManager.h"
00041 #include "MgmtUtils.h"
00042 
00043 #include "RecordsConfig.h"
00044 
00045 #define LOG_FILENAME_SIZE 255
00046 
00047 class HttpBodyFactory;
00048 
00049 
00050 extern int fds_limit;
00051 extern int cluster_port_number;
00052 
00053 int command_flag = 0;
00054 int http_accept_port_number = 0;
00055 int http_accept_file_descriptor = 0;
00056 int remote_management_flag = 0;
00057 int auto_clear_hostdb_flag = 0;
00058 char proxy_name[MAXDNAME + 1] = "unknown";
00059 
00060 char error_tags[1024] = "";
00061 char action_tags[1024] = "";
00062 char command_string[512] = "";
00063 
00064 
00065 
00066 DiagsConfig *diagsConfig = NULL;
00067 HttpBodyFactory *body_factory = NULL;
00068 AppVersionInfo appVersionInfo;
00069 
00070 
00071 
00072 
00073 
00074 static void
00075 init_system(bool notify_syslog)
00076 {
00077   fds_limit = ink_max_out_rlimit(RLIMIT_NOFILE, true, false);
00078 
00079   init_signals();
00080   if (notify_syslog) {
00081     syslog(LOG_NOTICE, "NOTE: --- %s Starting ---", appVersionInfo.AppStr);
00082     syslog(LOG_NOTICE, "NOTE: %s Version: %s", appVersionInfo.AppStr, appVersionInfo.FullVersionInfoStr);
00083   }
00084 }
00085 
00086 
00087 
00088 
00089 
00090 static void
00091 initialize_process_manager()
00092 {
00093   mgmt_use_syslog();
00094 
00095   
00096   if (getenv("PROXY_REMOTE_MGMT")) {
00097     remote_management_flag = true;
00098   }
00099 
00100   
00101   ink_assert(diags);
00102 
00103   RecProcessInit(remote_management_flag ? RECM_CLIENT : RECM_STAND_ALONE, diags);
00104 
00105   if (!remote_management_flag) {
00106     LibRecordsConfigInit();
00107     RecordsConfigOverrideFromEnvironment();
00108   }
00109 
00110   
00111   
00112   pmgmt = new ProcessManager(remote_management_flag);
00113 
00114   pmgmt->start();
00115 
00116   RecProcessInitMessage(remote_management_flag ? RECM_CLIENT : RECM_STAND_ALONE);
00117 
00118   pmgmt->reconfigure();
00119 
00120   
00121   
00122   
00123   RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.short", appVersionInfo.VersionStr, RECP_NON_PERSISTENT);
00124   RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.long", appVersionInfo.FullVersionInfoStr, RECP_NON_PERSISTENT);
00125   RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.build_number", appVersionInfo.BldNumStr, RECP_NON_PERSISTENT);
00126   RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.build_time", appVersionInfo.BldTimeStr, RECP_NON_PERSISTENT);
00127   RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.build_date", appVersionInfo.BldDateStr, RECP_NON_PERSISTENT);
00128   RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.build_machine", appVersionInfo.BldMachineStr, RECP_NON_PERSISTENT);
00129   RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.build_person", appVersionInfo.BldPersonStr, RECP_NON_PERSISTENT);
00130 
00131 
00132 
00133 
00134 }
00135 
00136 
00137 
00138 
00139 
00140 void
00141 shutdown_system()
00142 {
00143 }
00144 
00145 
00146 
00147 
00148 
00149 
00150 static void
00151 check_lockfile()
00152 {
00153   int err;
00154   pid_t holding_pid;
00155   char *lockfile = NULL;
00156 
00157   if (access(Layout::get()->runtimedir, R_OK | W_OK) == -1) {
00158     fprintf(stderr,"unable to access() dir'%s': %d, %s\n", Layout::get()->runtimedir, errno, strerror(errno));
00159     fprintf(stderr," please set correct path in env variable TS_ROOT \n");
00160     _exit(1);
00161   }
00162   lockfile = Layout::relative_to(Layout::get()->runtimedir, SERVER_LOCK);
00163 
00164   Lockfile server_lockfile(lockfile);
00165   err = server_lockfile.Get(&holding_pid);
00166 
00167   if (err != 1) {
00168     char *reason = strerror(-err);
00169     fprintf(stderr, "FATAL: Can't acquire lockfile '%s'", lockfile);
00170 
00171     if ((err == 0) && (holding_pid != -1)) {
00172 #if defined(solaris)
00173       fprintf(stderr, " (Lock file held by process ID %d)\n", (int)holding_pid);
00174 #else
00175       fprintf(stderr, " (Lock file held by process ID %d)\n", holding_pid);
00176 #endif
00177     } else if ((err == 0) && (holding_pid == -1)) {
00178       fprintf(stderr, " (Lock file exists, but can't read process ID)\n");
00179     } else if (reason) {
00180       fprintf(stderr, " (%s)\n", reason);
00181     } else {
00182       fprintf(stderr, "\n");
00183     }
00184     _exit(1);
00185   }
00186   ats_free(lockfile);
00187 }
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 void
00197 init_log_standalone(const char *pgm_name, bool one_copy)
00198 {
00199   char logfile[LOG_FILENAME_SIZE];
00200 
00201   
00202   
00203   if (one_copy) {
00204     check_lockfile();
00205   }
00206 
00207   snprintf(logfile, sizeof(logfile), "%s.log", pgm_name);
00208 
00209   
00210   
00211   setbuf(stdin, NULL);
00212   setbuf(stdout, NULL);
00213 
00214   openlog(pgm_name, LOG_PID | LOG_NDELAY | LOG_NOWAIT, LOG_DAEMON);
00215 
00216   init_system(true);
00217   initialize_process_manager();
00218   diagsConfig = new DiagsConfig(logfile, error_tags, action_tags);
00219   diags = diagsConfig->diags;
00220 }
00221 
00222 
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 void
00237 init_log_standalone_basic(const char *pgm_name)
00238 {
00239   char logfile[LOG_FILENAME_SIZE];
00240 
00241   snprintf(logfile, sizeof(logfile), "%s.log", pgm_name);
00242   openlog(pgm_name, LOG_PID | LOG_NDELAY | LOG_NOWAIT, LOG_DAEMON);
00243 
00244   init_system(false);
00245   const bool use_records = false;
00246   diagsConfig = new DiagsConfig(logfile, error_tags, action_tags, use_records);
00247   diags = diagsConfig->diags;
00248   
00249   
00250   setbuf(stdin, NULL);
00251   setbuf(stdout, NULL);
00252 
00253 }