MCS  0.3.3-alpha7
BaseThread.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 
23 
24 #include "mcs.hh"
25 using namespace mcs;
26 
27 
28 //--------------------------------------------------------
30 {
31  char buf[MCS_COMMBUFSIZE];
32  char* p = buf;
33  string str;
34 
35 
36  const char *format="%Y%m%d %H%M%S "; //Format time string
37  const int lenfmt=strlen(format)+3; //Three more for century and NULL
38  time_t tp=time(NULL);
39  p+=strftime(p, lenfmt, format, localtime(&tp)); //Time-stamp
40  p+=sprintf(p, "%s ", tID); //Thread id
41 
42  str = buf;
43  str += e.msg();
44 
45  if (e.type() != OK) {
46  str += " [";
47  switch (e.type()) {
48  case WARN:
49  str += "W"; break;
50  case ERROR:
51  str += "E"; break;
52  case FATAL:
53  str += "F"; break;
54  default:
55  break;
56  }
57  str += " " + e.where() + "]";
58  }
59 
60  if (env->cl_logstdout) cout << str << endl;
61  if (env->cl_logfile) if (env->flog) (*(env->flog)) << str << endl;
62 
63  if ((env->local) &&
64  (e.code() != MSG_THREAD_CREATE) &&
65  (e.code() != MSG_THREAD_DESTROY) &&
66  (id() > 0) ) {
67 
68  env->local->hk_log((UserThread*) this, e);
69  }
70 
71  return e.type();
72 }
73 
74 
75 mcs::BaseThread::BaseThread(Thread* parent, int ID) : Thread(ID, parent)
76 {
77  sprintf(tID, "%03d", ID);
78 }
79 
80 
81 mcs::BaseThread::BaseThread(Thread* parent, const char *ltID) : Thread(-1, parent)
82 {
83  sprintf(tID, "%3s", ltID);
84 }
85 
86 
88 {}
89 
90 const char* mcs::BaseThread::tid()
91 {
92  return tID;
93 }
94 
95 
96 
97 
99 
100 
101 
102 
103 
104 //--------------------------------------------------------
105 #define FT_TEMPL_BATCH "#BATCH"
106 #define FT_TEMPL_SQL "#SQL"
107 #define FT_TEMPL_SCRIPT "#SCRIPT"
108 #define FT_TEMPL_BIN "\177ELF"
109 
110 
111 
113 {
114  char buffer[10];
115  buffer[9] = '\0';
116 
117  ifstream fin;
118  fin.open(fn.csz);
119  if (!(fin.is_open()))
120  throw MCS_ERROR(MSG_CANT_OPEN_FILE, fn.csz);
121 
122  fin.read(buffer, 9);
123  fin.close();
124  if (0)
125  ;
126  else if (! memcmp(buffer, FT_TEMPL_BATCH, strlen(FT_TEMPL_BATCH)))
127  return MCS_FT_BATCH;
128  else if (! memcmp(buffer, FT_TEMPL_SQL, strlen(FT_TEMPL_SQL)))
129  return MCS_FT_SQL;
130  else if (! memcmp(buffer, FT_TEMPL_SCRIPT, strlen(FT_TEMPL_SCRIPT)))
131  return MCS_FT_SCRIPT;
132  else if (! memcmp(buffer, FT_TEMPL_BIN, strlen(FT_TEMPL_BIN)))
133  return MCS_FT_BIN;
134 
135  return MCS_FT_UNKNOWN;
136 }
137 
138 
139 
141 {
142  int i;
143  string fn = env->cnf->sval(env->appname + "_EXTERNAL" , s, "");
144 
145  if (! fn.empty()) {
146  i = fileType(fn); //Check file type
147  if (i == MCS_FT_UNKNOWN)
148  throw MCS_ERROR(MSG_UNKNOWN_FILE_TYPE, fn.csz);
149  }
150  else
151  throw MCS_WARN(MSG_EXT_NOT_AVAILABLE, s.c_str());
152 
153  s = fn;
154  return i;
155 }
156 
157 
158 
159 int mcs::BaseThread::spawn(string fn, string pars,
160  string wpath, string thrID,
161  string user, string pass, string dbname,
162  string fout, string ferr)
163 {
164  //We need to check for redirection operators '<', '>' and pipes
165  //'|'. If one of these operators are present in the command line
166  //we'll throw an error.
167 
168  static string bl = " ";
169  string tmp;
170  tmp = fn + bl + pars;
171 
172  if (strchr(tmp.c_str(), '>'))
173  throw MCS_WARN(MSG_CHARACTER_NOT_ALLOWED, ">");
174 
175  if (strchr(tmp.c_str(), '<'))
176  throw MCS_WARN(MSG_CHARACTER_NOT_ALLOWED, "<");
177 
178  if (strchr(tmp.c_str(), '|'))
179  throw MCS_WARN(MSG_CHARACTER_NOT_ALLOWED, "|");
180 
181  int i = fileType(fn); //Check file type
182  if ((i != MCS_FT_SCRIPT) &&
183  (i != MCS_FT_BIN))
184  throw MCS_ERROR(MSG_CANT_EXECUTE_FILE, fn);
185 
186 
187  tmp = BINDIR + string("/mcsexec "); //external spawner, pars:
188  tmp += thrID + bl; //client id
189  tmp += user + bl + pass + bl + dbname + bl; //username, password, dbname,
190  tmp += wpath + bl; //path to work directory
191 
192  if (i == MCS_FT_SCRIPT) //In this case the real executable is the shell
193  tmp += "/bin/bash ";
194 
195  tmp += fn + bl + pars; //filename of executable
196  tmp += " > " + fout ; //redirect stdout
197  tmp += " 2> " + ferr; //redirect stderr
198 
199  i=system(tmp.csz);
200  if (WIFEXITED(i)) {
201  i = WEXITSTATUS(i);
202  }
203  else
204  throw MCS_ERROR( MSG_EXT_BIN_UNEXPECTED );
205 
206  return i;
207 }
208 
209 
#define MCS_FT_SQL
Return value for BaseThread.fileType(), SQL file.
Definition: mcs.hh:6257
RetValue Log(Event e)
Logging facility.
Definition: BaseThread.cc:29
Definition: mcs.hh:472
string sval(string section, string key)
Search for the specified section/key value and return it as a string.
Definition: Conf.cc:135
#define MCS_COMMBUFSIZE
Max length of a message being sent between client and server.
Definition: mcs.hh:210
char tID[4]
Thread identifier stored as a null terminated string.
Definition: mcs.hh:6103
The server side client thread.
Definition: mcs.hh:6365
string where()
Returns the place where the error occurred formatted as FILE:LINE (CODE).
Definition: Event.cc:110
Hold informations about an event.
Definition: mcs.hh:814
static Env * env
Pointer to the actual Env object, this can be seen in all threaded object.
Definition: mcs.hh:6116
int chkExt(string &s)
Check if an external program or script is registered in the configuration file.
Definition: BaseThread.cc:140
Thread * parent()
Returns the address of the parent.
Definition: Thread.cc:316
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:964
static int fileType(string fn)
Determine the file type of an external program or script.
Definition: BaseThread.cc:112
string msg()
Returns the message.
Definition: Event.cc:104
#define MCS_FT_BATCH
Return value for BaseThread.fileType(), batch file.
Definition: mcs.hh:6254
string appname
Application name.
Definition: mcs.hh:6923
RetValue
Return value for MCS routines.
Definition: mcs.hh:471
RetValue type()
Return the event type.
Definition: Event.cc:99
#define MCS_FT_SCRIPT
Return value for BaseThread.fileType(), script file.
Definition: mcs.hh:6260
BaseThread(const BaseThread &)
Declared to avoid using of default copy constructor.
Main include file for all MCS based applications.
~BaseThread()
Destructor.
Definition: BaseThread.cc:87
#define MCS_WARN(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:961
int code()
Returns the event code.
Definition: Event.cc:92
#define MCS_FT_UNKNOWN
Return value for BaseThread.fileType(), unknown type.
Definition: mcs.hh:6251
#define MCS_FT_BIN
Return value for BaseThread.fileType(), binary file.
Definition: mcs.hh:6263
Hold all environment variables.
Definition: mcs.hh:6867
static int spawn(string fn, string pars, string wpath=".", string thrID="x", string user="x", string pass="x", string dbname="x", string fout="out", string ferr="err")
Execute an external program or script in a dedicated environment.
Definition: BaseThread.cc:159
A class to create separate threads.
Definition: mcs.hh:2487
Conf * cnf
Pointer to the Conf object used to read the configuration file.
Definition: mcs.hh:6920
const char * tid()
Return the thread identifier.
Definition: BaseThread.cc:90
Namespace for MCS library.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018