artdaq_mfextensions  v1_06_02
ma_action_mail.cpp
1 #include "ErrorHandler/MessageAnalyzer/ma_action_mail.h"
2 
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <iostream>
6 #include <sstream>
7 //#include <sys/types.h>
8 //#include <sys/wait.h>
9 
10 using namespace novadaq::errorhandler;
11 
12 REG_MA_ACTION(mail, ma_action_mail)
13 
14 namespace {
15 
16 int RunCommand(std::string const& strCmd, std::string const& strParam)
17 {
18  int iForkId, iStatus;
19  iForkId = vfork();
20  if (iForkId == 0) // This is the child
21  {
22  std::string command = strCmd + " " + strParam;
23 
24  iStatus = execl("/bin/sh", "sh", "-c", command.c_str(), (char*)NULL);
25  exit(iStatus); // We must exit here,
26  // or we will have multiple
27  // mainlines running...
28  }
29  else if (iForkId > 0) // Parent, no error
30  {
31  iStatus = 0;
32  }
33  else // Parent, with error (iForkId == -1)
34  {
35  iStatus = -1;
36  }
37  return (iStatus);
38 }
39 
40 } // namespace
41 
42 ma_action_mail::ma_action_mail(ma_rule const* rule, pset_t const& pset)
43  : ma_action(rule, pset)
44 {
45  std::stringstream ss;
46  //ss << (std::string)getenv("FHICL_FILE_PATH");
47  ss << (std::string)getenv("SRT_PRIVATE_CONTEXT") << ":"
48  << (std::string)getenv("SRT_PUBLIC_CONTEXT") << ":.";
49  std::string token;
50  std::stringstream mail_file;
51 
52  // search for mail bash file using FHICL_FILE_PATH
53  while (std::getline(ss, token, ':'))
54  {
55  mail_file.str("");
56  mail_file << token << "/ErrorHandler/cxx/config/send_mail.sh";
57  std::stringstream sys_check;
58  sys_check << "-f " << mail_file.str();
59  // exit loop if file exists
60  if (system(&sys_check.str()[0])) break;
61  }
62 
63  script_name = mail_file.str(); //pset.get<std::string>("name");
64  script_para = pset.get<std::string>("param", std::string());
65 
66  param.init(rule, script_para);
67 }
68 
69 bool ma_action_mail::exec()
70 {
71  RunCommand(script_name, param.message());
72  return true;
73 }
base class - all customized fucntions are inherited from it
Definition: ma_action.h:25