mcsexec.cc

00001 // ----------------------------------------------------------------------^
00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
00003 // (mailto: <gcalderone@ifc.inaf.it>)
00004 // 
00005 // This file is part of MCS.
00006 // 
00007 // MCS is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // MCS is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with MCS; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 // 
00021 // ----------------------------------------------------------------------$
00022 
00023 
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include <unistd.h>
00027 #include <errno.h>
00028 
00029 #include <iostream>
00030 #include <string>
00031 using namespace std;
00032 
00033 #define REPLACE 1
00034 #define N_ARGS 7
00035 
00036 void printUsage(char* name)
00037 {
00038     cerr << name
00039      << " MCS_CID MCS_USER MCS_PASS MCS_DBNAME WORK_DIR PROGNAME [PARS...]"
00040      << endl << endl;
00041 }
00042 
00043 void printArguments(int argc, char* argv[])
00044 {
00045     int i;
00046     cerr << "Arguments: " << argc << endl;
00047     for (i=0; i<argc; i++)
00048     cerr << i << " " << argv[i] << "|" << endl;
00049 }
00050 
00051 int main(int argc, char* argv[])
00052 {
00053     int ret, i;
00054 
00055     if (argc < N_ARGS) {
00056     cerr << "MCSEXEC: Wrong number of parameters" << endl << endl;
00057     printUsage(argv[0]);
00058     printArguments(argc, argv);
00059     exit(200);
00060     }
00061     
00062     string cid  = argv[1];
00063     string user = argv[2];
00064     string pass = argv[3];
00065     string dbna = argv[4];
00066     string wd   = argv[5];
00067     string path = argv[6];
00068     string prog = path;
00069     char* pc = (char*) strrchr(path.c_str(), '/');
00070     if (pc)
00071     prog = ++pc;
00072 
00073     int subargc = argc - N_ARGS;
00074     char** subargv = (char**) malloc((subargc+2) * sizeof(char*));
00075 
00076     subargv[0] = (char*) malloc(strlen(prog.c_str()) + 1);
00077     strcpy(subargv[0], prog.c_str());
00078     for (i=0; i<subargc; i++)
00079     {
00080     subargv[i+1] = (char*) malloc(strlen(argv[N_ARGS + i]) + 1);
00081     strcpy(subargv[i+1], argv[N_ARGS + i]);
00082     }
00083     subargv[subargc+1] = '\0';
00084 
00085 
00086     ret = chdir(wd.c_str());
00087     if (ret != 0) {
00088     cerr << strerror(errno) << endl;
00089     exit(errno);
00090     }
00091 
00092     pc = getcwd(NULL, 0);
00093     ret = setenv("PWD", pc, REPLACE);  free(pc);
00094 
00095     ret = setenv("MCS_CID" , cid.c_str() , REPLACE);
00096 
00097     ret = setenv("MCS_USER", user.c_str(), REPLACE);
00098 
00099     ret = setenv("MCS_PASS", pass.c_str(), REPLACE);
00100 
00101     ret = setenv("MCS_DBNAME", dbna.c_str(), REPLACE);
00102 
00103     ret = execv(path.c_str(), subargv);
00104     if (ret == -1) {
00105     cerr << strerror(errno) << endl;
00106     exit(errno);
00107     }
00108 }

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012