MCS  0.3.3-alpha7
killmcs.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 void usage()
28 {
29  cout << "killmcs: kills a MCS server" << endl;
30  cout << "Giorgio Calderone <gcalderone@ifc.inaf.it>" << endl;
31  cout << endl;
32  cout << "Usage: killmcs [-h] appname [inipath]" << endl;
33 }
34 
35 
36 int main(int argc, char* argv[])
37 {
38  Env* env = NULL;
39  CommandParser cp;
40  string s;
41 
42  try {
43  cp.parseCmd(argc, argv);
44 
45  if (argc == 1 || //Invocation without any option or argument
46  cp.givenOpt("h") || //Option -h has been given
47  cp.argc() == 0 ){ //No argument has been given
48  usage();
49  exit(0);
50  }
51 
52  //At least one argument has been given
53  string appname = cp.arg(0).sval();
54 
55  //Eventually get the second argument
56  string inipath = (cp.argc() >= 2 ? cp.arg(1).sval() : "");
57 
58  if (inipath.empty())
59  env = new Env(appname);
60  else
61  env = new Env(appname, inipath);
62  env-> server = NULL;
63 
64  if (chdir(env->path.c_str()) != 0)
65  throw MCS_ERROR(MSG_DIRECTORY_NOT_FOUND, env->path);
66 
67  s = env->path + "/mcs.pid";
68  ifstream pidfile("mcs.pid");
69 
70  if (pidfile.fail())
71  throw MCS_ERROR(MSG_CANT_OPEN_FILE, s.c_str());
72 
73  pidfile >> s;
74  pidfile.close();
75  unlink("mcs.pid");
76 
77  s = "kill " + s;
78  cout << s << endl;
79 
80  exit(system(s.c_str()));
81  }
82  catch (Event& e) {
83  cerr << e.msg() << endl;
84  exit(3);
85  }
86 
87 
88  if (env) delete env;
89 }
void parseCmd(string c)
Parse a command line into tokens, arguments and options.
string sval(bool addWhiteSpaces=false) const
Convert internal data to a string object.
Definition: Data.cc:1555
Hold informations about an event.
Definition: mcs.hh:814
string path
Main server path (APPD).
Definition: mcs.hh:6929
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:964
string msg()
Returns the message.
Definition: Event.cc:104
int argc()
Return number of arguments.
bool givenOpt(string opt)
Check if a certain option has been given.
Parse command lines.
Definition: mcs.hh:5185
Main include file for all MCS based applications.
Hold all environment variables.
Definition: mcs.hh:6867
Data & arg(int i)
Return argument at i-th position.
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