• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

LogStandalone.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /***************************************************************************
00025  LogStandalone.cc
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 // Needs LibRecordsConfigInit()
00043 #include "RecordsConfig.h"
00044 
00045 #define LOG_FILENAME_SIZE 255
00046 
00047 class HttpBodyFactory;
00048 
00049 // globals the rest of the system depends on
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 //Diags *diags = NULL;
00066 DiagsConfig *diagsConfig = NULL;
00067 HttpBodyFactory *body_factory = NULL;
00068 AppVersionInfo appVersionInfo;
00069 
00070 /*-------------------------------------------------------------------------
00071   init_system
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   initialize_process_manager
00088   -------------------------------------------------------------------------*/
00089 
00090 static void
00091 initialize_process_manager()
00092 {
00093   mgmt_use_syslog();
00094 
00095   // Temporary Hack to Enable Communuication with LocalManager
00096   if (getenv("PROXY_REMOTE_MGMT")) {
00097     remote_management_flag = true;
00098   }
00099 
00100   // diags should have been initialized by caller, e.g.: sac.cc
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   // Start up manager
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   // Define version info records
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 //    RecRegisterStatString(RECT_PROCESS,
00131 //                         "proxy.process.version.server.build_compile_flags",
00132 //                         appVersionInfo.BldCompileFlagsStr,
00133 //                         RECP_NON_PERSISTENT);
00134 }
00135 
00136 /*-------------------------------------------------------------------------
00137   shutdown_system
00138   -------------------------------------------------------------------------*/
00139 
00140 void
00141 shutdown_system()
00142 {
00143 }
00144 
00145 
00146 /*-------------------------------------------------------------------------
00147   check_lockfile
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   init_log_standalone
00191 
00192   This routine should be called from the main() function of the standalone
00193   program.
00194   -------------------------------------------------------------------------*/
00195 
00196 void
00197 init_log_standalone(const char *pgm_name, bool one_copy)
00198 {
00199   char logfile[LOG_FILENAME_SIZE];
00200 
00201   // ensure that only one copy of the sac is running
00202   //
00203   if (one_copy) {
00204     check_lockfile();
00205   }
00206 
00207   snprintf(logfile, sizeof(logfile), "%s.log", pgm_name);
00208 
00209   // set stdin/stdout to be unbuffered
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   init_log_standalone_basic
00224 
00225   This routine is similar to init_log_standalone, but it is intended for
00226   simple standalone applications that do not read the records.config file
00227   and that do not need a process manager, thus it:
00228 
00229   1) does not call initialize_process_manager
00230   2) initializes the diags with use_records = false
00231   3) does not call Machine::init()
00232   4) assumes multiple copies of the application can run, so does not
00233      do lock checking
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   // set stdin/stdout to be unbuffered
00249   //
00250   setbuf(stdin, NULL);
00251   setbuf(stdout, NULL);
00252 
00253 }

Generated by  doxygen 1.7.1