MCS  0.3.3-alpha7
mcsexec.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 <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 
29 #include <iostream>
30 #include <string>
31 using namespace std;
32 
33 #define REPLACE 1
34 #define N_ARGS 7
35 
36 void printUsage(char* name)
37 {
38  cerr << name
39  << " MCS_CID MCS_USER MCS_PASS MCS_DBNAME WORK_DIR PROGNAME [PARS...]"
40  << endl << endl;
41 }
42 
43 void printArguments(int argc, char* argv[])
44 {
45  int i;
46  cerr << "Arguments: " << argc << endl;
47  for (i=0; i<argc; i++)
48  cerr << i << " " << argv[i] << "|" << endl;
49 }
50 
51 int main(int argc, char* argv[])
52 {
53  int ret, i;
54 
55  if (argc < N_ARGS) {
56  cerr << "MCSEXEC: Wrong number of parameters" << endl << endl;
57  printUsage(argv[0]);
58  printArguments(argc, argv);
59  exit(200);
60  }
61 
62  string cid = argv[1];
63  string user = argv[2];
64  string pass = argv[3];
65  string dbna = argv[4];
66  string wd = argv[5];
67  string path = argv[6];
68  string prog = path;
69  char* pc = (char*) strrchr(path.c_str(), '/');
70  if (pc)
71  prog = ++pc;
72 
73  int subargc = argc - N_ARGS;
74  char** subargv = (char**) malloc((subargc+2) * sizeof(char*));
75 
76  subargv[0] = (char*) malloc(strlen(prog.c_str()) + 1);
77  strcpy(subargv[0], prog.c_str());
78  for (i=0; i<subargc; i++)
79  {
80  subargv[i+1] = (char*) malloc(strlen(argv[N_ARGS + i]) + 1);
81  strcpy(subargv[i+1], argv[N_ARGS + i]);
82  }
83  *subargv[subargc+1] = '\0';
84 
85 
86  ret = chdir(wd.c_str());
87  if (ret != 0) {
88  cerr << strerror(errno) << endl;
89  exit(errno);
90  }
91 
92  pc = getcwd(NULL, 0);
93  ret = setenv("PWD", pc, REPLACE); free(pc);
94 
95  ret = setenv("MCS_CID" , cid.c_str() , REPLACE);
96 
97  ret = setenv("MCS_USER", user.c_str(), REPLACE);
98 
99  ret = setenv("MCS_PASS", pass.c_str(), REPLACE);
100 
101  ret = setenv("MCS_DBNAME", dbna.c_str(), REPLACE);
102 
103  ret = execv(path.c_str(), subargv);
104  if (ret == -1) {
105  cerr << strerror(errno) << endl;
106  exit(errno);
107  }
108 }

mcslogo

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